Key/value storage
A table does not have to live in files. You can place it on a key/value store: an S3-compatible object store (AWS S3, MinIO, RustFS, Ceph RGW, Cloudflare R2, Backblaze B2, Wasabi) or a local directory laid out the same way. Use it to keep large, cold or append-mostly tables in cheap object storage while the rest of the database stays on local disk. One database can hold file tables, in-memory tables and key/value tables at the same time, and they are queried the same way.
This is not the Redis-compatible cache. That is a separate in-memory key/value service; this page is about where a SQL table's bytes are stored.
How it works
A table on a store keeps its data as objects under the store's prefix. Each commit writes one object per table file it changed (usually two: rows and row pointers), describing only the bytes that changed. A background compaction folds those changes into larger base objects. Reads are served from a page cache in memory, in front of ranged reads from the store.
What this means for you:
- A commit is one or two requests, however much it changed, and returns only when the store has accepted them.
- Reads come from memory once cached. A cold read is a round trip to the store; a cached one costs about the same as a file table.
- Restarts start cold. There is no local disk cache, so the first reads after a restart fetch from the store.
- There is no write-ahead log for these tables. Their durability is the object written at each commit.
Define a store
A store is defined per database, with a STORAGE ADD STORE statement run while connected to that database. Nothing needs a restart or a file edited by hand.
An S3 store
STORAGE ADD STORE archive S3 WITH (
ENDPOINT = 'https://s3.eu-central-1.amazonaws.com',
REGION = 'eu-central-1',
BUCKET = 'acme-rdb',
PREFIX = 'prod/archive',
ADDRESSING = 'virtual'
);For MinIO or RustFS on the same network, use path-style addressing:
STORAGE ADD STORE archive S3 WITH (
ENDPOINT = 'http://10.0.0.20:9000',
BUCKET = 'rdb',
PREFIX = 'prod/archive',
ADDRESSING = 'path',
ACCESS_KEY_ID = 'rdb-writer',
SECRET_ACCESS_KEY = 'change-me-please'
);| Option | Required | Default | Meaning |
|---|---|---|---|
ENDPOINT | yes | https://host[:port] or http://host[:port]. A missing scheme means HTTPS. | |
BUCKET | yes | The bucket. It must already exist. | |
REGION | no | us-east-1 | The signing region. MinIO, Ceph and R2 accept any value. |
PREFIX | no | rdb | Key prefix all of this store's objects go under. |
ADDRESSING | no | virtual | virtual (bucket in the host name) or path (bucket in the path: MinIO, RustFS, and any bucket name that is not DNS-safe). |
ACCESS_KEY_ID, SECRET_ACCESS_KEY | no | from the environment | Credentials for this store only. Give both or neither. |
SESSION_TOKEN | no | from the environment | For temporary credentials (STS, IRSA, instance roles). |
A store given no credentials signs with AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN from the server's environment:
export AWS_ACCESS_KEY_ID=AKIAEXAMPLE
export AWS_SECRET_ACCESS_KEY=example-secret
rasterizeddb --location /var/lib/rasterizeddbA directory store
A directory store uses the same object layout on a local or mounted disk. It is useful for testing and for keeping a table in the store format without a network.
STORAGE ADD STORE scratch DIRECTORY WITH (PATH = '/mnt/fast/kv', PREFIX = 'scratch');It takes PATH (required) and PREFIX (default rdb), and refuses credentials.
Tuning options
Any of these may be given in WITH (...) when the store is added, or changed later with STORAGE ALTER STORE. Sizes take 4MiB, 512KB or a plain byte count.
| Option | Default | What it does |
|---|---|---|
CHUNK_SIZE | 4MiB | Bytes per base object. Larger means fewer requests per scan and more bytes rewritten per compaction. Must be a whole number of pages. |
PAGE_SIZE | 64KiB | The unit of caching and of ranged reads. Must divide CHUNK_SIZE. |
CACHE_BYTES | 256MiB | Memory for cached data, shared by every table on this store. |
CHUNK_FETCH_PAGES | 4 | How many pages of one object a read must want before the whole object is fetched. |
READAHEAD_CHUNKS | 2 | Objects fetched ahead of a sequential scan. |
MAX_DELTAS | 64 | Change objects allowed per file before a compaction is forced. |
COMPACT_CHUNK_BYTES | 1MiB | Changed bytes in one chunk that make folding it worthwhile. |
FLUSH_INTERVAL_MS | 200 | How long writes made outside a transaction (schema headers, the row-id sequence) may wait before they are stored. |
SPILL_BYTES | 8MiB | Unstored changes held in memory before they are written out early. |
SHOW STORAGE KEYS lists them with a one-line description each.
Running the definition on every start
A deployment that sets itself up on each start can use IF NOT EXISTS:
STORAGE ADD STORE IF NOT EXISTS archive S3 WITH (
ENDPOINT = 'https://s3.eu-central-1.amazonaws.com',
REGION = 'eu-central-1',
BUCKET = 'acme-rdb',
PREFIX = 'prod/archive'
);Re-issuing the same definition does nothing. Re-issuing a different one is refused and names the fields that differ. Credentials given on this path are applied, which is how such a deployment rotates a key.
Adding a store does not contact it
STORAGE ADD STORE makes no request. A wrong endpoint, a missing bucket or a rejected key shows up only when the first table is placed there and fails to open. Check the opened column of SHOW STORAGE STORES after creating the first table.
Put a table on a store
Pin the table to the store before creating it, then create it as usual:
STORAGE PLACE TABLE events AT 'kv:archive';
CREATE TABLE events (
id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL,
at TIMESTAMP NOT NULL,
body TEXT
);events will be created in key/value store 'archive' when it is created; a backup copies it as of each snapshot, with no point-in-time replaySHOW TABLES reports where each table ended up in its storage column: s3, kv-directory or kv-memory for a store, file or memory otherwise. SHOW TABLE STATS events includes the store's page cache figures; the miss count is the number of reads that went to the store.
Move an existing table onto or off a store
STORAGE MOVE TABLE converts a table between any two backends:
STORAGE MOVE TABLE events TO 'kv:archive'; -- files to S3
STORAGE MOVE TABLE events TO 'kv:scratch'; -- S3 to a directory store
STORAGE MOVE TABLE events TO 'memory:'; -- into this process only
STORAGE MOVE TABLE events TO '/var/lib/rasterizeddb'; -- back onto filesThe table is copied, its placement switched, and the originals deleted. Statements on that table wait for the move; a crash at any point leaves the table whole on one side. A directory destination must be a storage location of this database, and a store destination must be defined; both are checked before anything is copied. Each move's output says what the table gained or lost (write-ahead log, point-in-time backup).
Partitioned tables on a store
A hash-partitioned table can keep its partitions on a store. Place it before creating it:
STORAGE PLACE TABLE access_logs AT 'kv:logs';
CREATE TABLE access_logs (
domain TEXT NOT NULL,
id BIGINT NOT NULL,
at TIMESTAMP NOT NULL,
path TEXT,
PRIMARY KEY (domain, id)
) PARTITION BY HASH (domain);The partitions' row and pointer files go to the store; the table's small metadata files stay in the database directory. Single partitions can be moved between a store and a directory:
STORAGE MOVE PARTITION access_logs FOR (domain = 'example.com') TO 'kv:logs';
STORAGE MOVE PARTITION access_logs FOR (domain = 'example.com') TO '/var/lib/rasterizeddb';Automatic compaction works per partition on a store too, and gives storage back: a compacted partition's objects past its new end are deleted, leaving at most one chunk (CHUNK_SIZE) per file beyond the live data. See Maintenance.
STORAGE PLACE TABLE and STORAGE MOVE TABLE are refused for a partitioned table that already exists, and STORAGE REBALANCE PARTITIONS leaves partitions on a store where they are.
Change, rotate and drop a store
-- Tuning can change at any time.
STORAGE ALTER STORE archive SET CACHE_BYTES = '1GiB', READAHEAD_CHUNKS = 4;
-- Rotate credentials, even while tables are on the store.
STORAGE ALTER STORE archive SET ACCESS_KEY_ID = 'AKIANEWKEY', SECRET_ACCESS_KEY = 'new-secret';
-- Remove a store nothing is placed on.
STORAGE DROP STORE scratch;- A store that the running server already has open keeps the settings and credentials it was opened with until the server restarts. The statement says so.
- Changing a store's address (
ENDPOINT,BUCKET,PREFIX,REGION,ADDRESSING,PATH) is refused while any table or partition is placed on it, because a new address moves no data: the table would read as empty. Move the tables off first. It is also refused while the store is open; restart first. STORAGE DROP STOREis refused while any table or partition is placed on the store, and names them. It does not delete the store's objects; remove them from the bucket yourself if nothing else needs them.- A value in
WITH (...)may contain commas,),=or the wordWITH. There is no escape sequence: a value cannot contain the quote it is wrapped in, so wrap a value containing'in"instead.
Inspect stores
SHOW STORAGE STORES returns a row per store:
| Column | Meaning |
|---|---|
store | The store's name. |
kind | s3 or dir. |
endpoint, region, bucket, prefix, addressing | Its address. |
settings | Tuning options set on it. |
credentials | Where its credentials come from, store or environment. Never the credentials themselves. |
tables | Tables and partitioned tables placed on it. |
opened | Whether this server has opened it yet. |
supported | Whether this build can open it. |
SHOW STORAGE also lists each store on a summary line.
Where the definitions are kept
Store definitions are saved in db_kv.cfg in the database directory, a tab-separated file the server rewrites atomically. It holds no secrets and is safe to paste into a ticket. Every gap is a single TAB character; a space instead of a tab makes the line unreadable and the store disappears.
version 1
store archive s3 https://s3.eu-central-1.amazonaws.com eu-central-1 acme-rdb prod/archive virtual
store scratch dir /mnt/fast/kv scratchCredentials given with ACCESS_KEY_ID/SECRET_ACCESS_KEY are kept in db_kv_secrets.cfg beside it, created with mode 0600, and are never shown by any statement, log line or error. A copy or backup of the database directory carries that file, so treat backups of a database with store credentials as being as sensitive as the key.
Backups
An S3 backup snapshot copies every table on a store as one consistent cut, and a restore brings it back as an ordinary file table in the restored directory, never onto the store. It is restored as of the snapshot: with no write-ahead log, there is nothing to replay to a later point in time.
Not copied, and recorded on the snapshot as not covered (so SHOW BACKUPS and SHOW BACKUP STATUS say the backup is incomplete): in-memory tables, tables on a store the server cannot reach, and partitions of a partitioned table placed on a store.
Costs
- Requests. Each commit is one PUT per table file it changed. Compaction reads and rewrites base objects in the background. Cached reads make no request.
- Memory.
CACHE_BYTESper store, plus changes not yet compacted, plus whatever a long transaction has written before it commits (held in memory until then). - Speed. Point reads from cache cost about the same as on files. Reads of many rows cost up to roughly twice the CPU of a file table, and bulk loads take several times longer. The first read of anything after a restart waits on the store.
- Throttling. A store answering 429 or 503 is retried up to 10 times with back-off (about 25 seconds in all) before a statement fails.
- Index start-up. With
--index-storage snapshot, a table on a store loads its indexes from a snapshot written at clean shutdown, when the store shows nothing changed since; otherwise they are rebuilt from rows.
Limits
- One server per prefix. Two servers writing to the same bucket and prefix corrupt each other's tables. Nothing prevents it; give every server, and every database, its own prefix.
- No local disk cache: every restart starts cold.
- No write-ahead log, so no point-in-time restore for these tables, and the free-space placement policy and
STORAGE REBALANCEignore them. - A table placed on a store the server cannot open (its definition removed from
db_kv.cfg, or a build without the feature) is left out of the database: queries say it does not exist, and the log says why. - Moving a table between two directories does not apply to a store table;
STORAGE MOVE TABLE t TO '<dir>'converts it back into a file table instead. - An in-memory database has no stores.
STORAGEstatements are not replicated; define the same stores (with their own prefixes) on each node.- Defining, changing and dropping stores needs a superuser.