NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / RASTERIZEDDB SERVER / STORAGE POOLS

Storage pools

A storage pool lets one database keep its tables in several directories, usually on several disks. Use it when one disk is too small or too slow for the whole database, when you want new tables spread across disks automatically, or when you want a second disk to hold a mirrored copy of every write.

A database that has only ever been given one directory is already a pool of one: its own directory, no backups, and the primary policy. Nothing changes for it until you add a location.

How a pool works

A pool is an ordered list of locations (directories). The first location is always the database's own directory. It holds the catalogue and the pool's own file, and it cannot be removed or demoted.

Each location has:

PropertyValuesMeaning
roleprimary (default) or backupA primary location can hold tables. A backup location receives a mirrored copy of every write and never holds a table on its own.
weighta positive whole number, default 1Multiplies the location's share under the free-space policy.
enabledenabled (default) or disabledA disabled location keeps the tables it already holds and serves them normally, but takes no new ones.

Roles combine. "One data disk and one mirror" is one primary plus one backup. "Four data disks, all mirrored to a fifth" is four primaries plus one backup.

A placement policy decides which primary location a new table goes to:

PolicyA new table goes to
primary (default)the first enabled primary location
round-robinthe enabled primary holding the fewest tables (ties go to the one listed first)
free-spacethe enabled primary with the most free space multiplied by its weight

A location with less free space than the minimum free setting (default 256MiB) is skipped. If every location is below it, the emptiest one is used and a warning is logged, rather than refusing the CREATE TABLE.

Where each table lives is recorded when the table is created and never recomputed. Changing the policy or adding a disk does not move existing tables; only STORAGE MOVE TABLE and STORAGE REBALANCE do.

Each location gets its own write-ahead log, created when the database starts, so a table on a second disk commits as fast as one in the database directory.

Set up a pool at start-up

The server flags describe locations for the default database (the one in --location). They are added on top of what the pool already records, never replacing it, so it is safe to repeat them on every start, for example in a service unit.

sh
rasterizeddb \
  --location /var/lib/rasterizeddb \
  --storage-location /mnt/nvme2/rasterizeddb \
  --storage-location /mnt/nvme3/rasterizeddb:primary:2 \
  --storage-location /mnt/backup/rasterizeddb:backup \
  --storage-policy free-space \
  --storage-min-free 4GiB
FlagDefaultWhat it does
--storage-location <PATH[:ROLE[:WEIGHT]]>noneAn extra directory this database may write into. Repeatable. ROLE is primary or backup; WEIGHT multiplies a primary's share under free-space. A location already in the pool is left as it is.
--storage-policy <POLICY>primaryprimary, round-robin or free-space.
--storage-min-free <SIZE>256MiBA location with less free space than this is not chosen for new tables. Write 512MB, 2GiB or a plain byte count.

If a location given by flag cannot be added (a relative path, a path that overlaps another location, a directory the server cannot write), the server refuses to start and says why.

Flags only reach the default database. For any other database, use the STORAGE statements below while connected to it.

Manage a pool with SQL

Every change made with a STORAGE statement is saved in the database's pool file and survives a restart. Statements apply to the database the session is connected to.

Add, change and remove locations

sql
-- A second data disk.
STORAGE ADD LOCATION '/mnt/nvme2/rasterizeddb';

-- A data disk that gets twice the share under the free-space policy.
STORAGE ADD LOCATION '/mnt/nvme3/rasterizeddb' WITH (ROLE = 'primary', WEIGHT = 2);

-- A mirror of every write.
STORAGE ADD LOCATION '/mnt/backup/rasterizeddb' WITH (ROLE = 'backup');

-- Added, but taking no tables yet.
STORAGE ADD LOCATION '/mnt/nvme4/rasterizeddb' WITH (ENABLED = false);

-- Change a location.
STORAGE ALTER LOCATION '/mnt/nvme3/rasterizeddb' SET WEIGHT = 3;
STORAGE ALTER LOCATION '/mnt/nvme4/rasterizeddb' SET ROLE = 'backup';

-- Stop new tables going to a location, or let them again.
STORAGE DISABLE LOCATION '/mnt/nvme2/rasterizeddb';
STORAGE ENABLE LOCATION '/mnt/nvme2/rasterizeddb';

-- Remove an empty location.
STORAGE DROP LOCATION '/mnt/nvme4/rasterizeddb';
text
added /mnt/nvme2/rasterizeddb as a primary location (weight 1, enabled)

WITH (...) on ADD LOCATION accepts ROLE, WEIGHT and ENABLED. ALTER LOCATION ... SET accepts ROLE and WEIGHT.

A new location is created if it does not exist and test-written at once, so a bad path fails at ADD LOCATION, not later.

Change the policy and the free-space floor

sql
STORAGE SET POLICY = 'round-robin';
STORAGE SET MIN_FREE = '2GiB';
text
placement policy is now round-robin
the next table created would go to /mnt/nvme2/rasterizeddb

Sizes take B, KB/KiB, MB/MiB, GB/GiB and TB/TiB. Both spellings of each unit mean the binary size (1MB is 1,048,576 bytes).

Choose where one table goes

To put a table on a particular location, pin it before creating it:

sql
STORAGE PLACE TABLE orders AT '/mnt/nvme2/rasterizeddb';
CREATE TABLE orders (id BIGINT PRIMARY KEY, total NUMERIC(12,2));
text
orders will be created in /mnt/nvme2/rasterizeddb when it is created

STORAGE PLACE TABLE is refused for a table that already exists; move it instead. The target may also be 'memory:' (the table lives only in this process and is lost at restart) or 'kv:<store>' (a key/value store, see Key/value storage).

Move a table

sql
STORAGE MOVE TABLE orders TO '/mnt/nvme3/rasterizeddb';
text
moved orders from /mnt/nvme2/rasterizeddb to /mnt/nvme3/rasterizeddb (1.2 GiB)

The move copies the table's files to the new location, switches the recorded placement, then deletes the originals. Statements on that table wait until the move finishes; other tables are not affected. A crash at any point leaves the table whole in one place or the other, with at most some orphaned copies on disk, never a lost table.

The destination must be a primary location of this database (or 'memory:' or 'kv:<store>'). A table cannot be moved into a backup location.

Drain a disk before removing it

sql
STORAGE DISABLE LOCATION '/mnt/nvme2/rasterizeddb';
STORAGE REBALANCE DRY RUN;
STORAGE REBALANCE;
STORAGE DROP LOCATION '/mnt/nvme2/rasterizeddb';

STORAGE DROP LOCATION refuses a location that still holds tables or partitions, and names them.

Rebalance

The server never moves a table on its own. STORAGE REBALANCE moves what the current policy would move; STORAGE REBALANCE DRY RUN prints the plan (table, size, from, to and why) without moving anything.

sql
STORAGE REBALANCE DRY RUN;
STORAGE REBALANCE;

The plan, in order:

  1. Evacuation. Every table on a disabled location, or on one that is no longer a primary, is moved off it.
  2. Policy. Under round-robin, tables move from the location holding the most to the one holding the fewest, smallest first, until the counts differ by at most one. Under free-space, tables move off any location below the minimum free, largest first. Under primary, nothing more moves.

No move takes a location below the minimum free. Moves run one at a time; the first failure stops the run, keeping the moves already done, and running STORAGE REBALANCE again plans from where it stopped.

STORAGE REBALANCE never moves partitions of a partitioned table. Use STORAGE REBALANCE PARTITIONS [DRY RUN] for those (see Partitioning). Tables on a key/value store or in memory are not rebalanced.

Inspect the pool

StatementReturns
SHOW STORAGEOne summary line for the policy, then one per location and per key/value store.
SHOW STORAGE STATSThe same facts as (metric, value, detail) rows.
SHOW STORAGE LOCATIONSA row per location: location, role, weight, enabled, is_database_directory, tables, available_bytes, total_bytes.
SHOW STORAGE PLACEMENTS (or SHOW STORAGE TABLES)A row per table: table_name, location, mirrored.
SHOW STORAGE STORESA row per key/value store (see Key/value storage).
SHOW STORAGE POLICYThe policy, the minimum free and where the next table would go.
SHOW STORAGE KEYSThe settings STORAGE SET accepts.
sql
SHOW STORAGE;
text
policy free-space, min free 4.0 GiB
/var/lib/rasterizeddb (database directory): primary, weight 1, 12 table(s), 180.3 GiB free of 467.4 GiB (39%)
/mnt/nvme2/rasterizeddb: primary, weight 1, 7 table(s), 701.2 GiB free of 931.5 GiB (75%)
/mnt/backup/rasterizeddb: backup, weight 1, 0 table(s), 3.1 TiB free of 3.6 TiB (85%)

Backup locations (mirroring)

A backup location receives every write made to every primary location, including the catalogue, so the backup directory is itself a complete database: point --location at it and it opens.

  • It is a warm copy, not a synchronous replica. A commit waits for the primary's write; the backup's is issued at the same time and flushed by its own log, so a crash can leave the backup a few records behind. The copy is repaired when the file is next opened: a backup file whose length differs from the primary's is rewritten.
  • Adding a backup location mirrors tables created or opened after the statement. Tables already open, and the catalogue, are mirrored from the next restart. The statement's output says so, and SHOW STORAGE PLACEMENTS shows which tables are mirrored. Restart the server after adding a backup location to cover everything.
  • A backup location protects against losing a disk. It does not protect against losing the machine; use replication or S3 backups for that.

Opening a copy of a data directory

If you copy a data directory somewhere else by hand and open it with --location, the server notices that the pool file names other paths:

  • If everything was in the one database directory (or in memory), the pool file is rewritten for the copy before anything opens, backup locations are dropped (so the copy never writes into the original's mirror), and the log says what changed.
  • If tables were on other disks, on a key/value store, or behind a restore's path mappings, the database is not opened, and the error names the file and why. Edit or remove the pool file by hand (with the server stopped), or restore from a backup, which repoints such copies itself.

Backups and restores

S3 backups cover every primary location: each location's files are copied and its log archived. A restore onto the original directory puts each location's files back on its own disk, which must be empty. A restore into a new directory places other locations' files inside the restored directory, so it opens on a single machine. Backup locations are not copied, since they mirror a primary that is. See Backup and restore.

The pool file

The pool is saved in db_storage.cfg in the database directory: tab-separated text the server rewrites atomically on every change. It can be read, and edited while the server is stopped, for example to recover a database onto different mount points. Every gap is a single TAB character.

text
# rasterizeddb storage pool. Rewritten by the server; edit while it is stopped.
version	1
policy	free-space
min_free_bytes	4294967296
location	primary	1	enabled	/var/lib/rasterizeddb
location	primary	2	enabled	/mnt/nvme2/rasterizeddb
location	backup	1	enabled	/mnt/backup/rasterizeddb
table	/mnt/nvme2/rasterizeddb	orders

A table with no table line is in the database directory. A pool file that cannot be read is logged, and the database opens with its directory only; tables on other disks are then missing until the file is fixed.

Several databases

Each database has its own pool and its own db_storage.cfg. Locations are used exactly as given: a second database does not get its own subdirectory automatically. Give each database its own directory, for example /mnt/nvme2/analytics for a database named analytics, and connect to that database before running STORAGE ADD LOCATION.

Permissions

STORAGE PLACE TABLE, STORAGE MOVE TABLE, STORAGE SET and STORAGE REBALANCE need a superuser. Adding, altering, enabling, disabling or dropping a location needs a superuser too.

Statements that name a new server path (ADD, ALTER, ENABLE, DISABLE and DROP LOCATION, and the store statements) are reserved to a superuser of the default database; a superuser of another database can still place, move and rebalance its own tables.

STORAGE statements are not replicated. Run them on each node that should have the same layout.

Limits

  • Paths must be absolute. A location that overlaps another (one inside the other) is refused, compared by whole path components, so /data/db2 does not overlap /data/db.
  • The database directory cannot be dropped or made a backup. A location that holds tables cannot be dropped or made a backup, and a table cannot be moved into a backup location.
  • Partitioned tables cannot be created in a pool that has a backup location.
  • Free space is read from the filesystem. Where it cannot be read, a location counts as not full, and free-space falls back to the first candidate.
  • Rows on a key/value store do not count against any location's free space.
✓ page rendered in 182 µs axum + tera + htmx · no other javascript