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 / SETUP SCRIPTS / SEVERAL DISKS FOR ONE DATABASE

Several disks for one database

Starts a server whose tables are spread over more than one directory, usually one per disk. The data directory keeps the catalogue and logs; extra primary directories take new tables as a placement policy decides; an optional backup directory receives a mirrored copy of every write, so it can be opened as the database if the primary disk is lost. It runs in the foreground.

Before you start

  • The server binary (see the install recipe).
  • One directory per disk, each on its own path. Directories may not be inside the data directory or inside each other, and must be absolute (the scripts make them absolute for you).
  • A password for the role rasterized in RASTERIZEDDB_PG_PASSWORD.

Settings

bash variablePowerShell parameterDefaultWhat it does
RDB_BIN-RdbBinrasterizeddb / rasterizeddb.exeThe server binary.
RDB_DATA-Data./dataThe database's own directory. It is always the first primary location and holds the catalogue, the logs and the pool file db_storage.cfg.
RDB_PRIMARIES-PrimariesnoneExtra table directories, space-separated in bash (;-separated in $env:RDB_PRIMARIES). Add :WEIGHT to one to give it a larger share under free-space, for example /mnt/nvme2/rdb:2.
RDB_MIRROR-MirrornoneA backup directory that gets a copy of every write.
RDB_POLICY-Policyfree-spaceWhere a new table goes: primary (the first enabled primary), round-robin (the primary holding the fewest tables) or free-space (the most free space times weight).
RDB_MIN_FREE-MinFree2GiBA directory with less free space than this takes no new tables. Written as 512MB, 2GiB or a byte count. The server's own default is 256 MiB.
RDB_LISTEN-Listen127.0.0.1The SQL listeners' address.

The script turns these into --storage-location DIR:primary[:WEIGHT], --storage-location DIR:backup, --storage-policy and --storage-min-free. Locations are added to the pool file the first time and remembered; naming them again at every start, as a service would, does nothing more. Removing a flag does not remove a location.

Run it

sh
export RASTERIZEDDB_PG_PASSWORD='use-a-long-secret'
RDB_DATA=/var/lib/rasterizeddb RDB_PRIMARIES="/mnt/nvme1/rdb /mnt/nvme2/rdb:2" RDB_MIRROR=/mnt/backup/rdb ./setup.sh
powershell
$env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
.\setup.ps1 -Data C:\rdb\data -Primaries 'D:\rdb', 'E:\rdb:2' -Mirror 'F:\rdb-mirror'

Under the systemd-service recipe, put the same flags in RASTERIZEDDB_ARGS and allow the directories with a drop-in (ReadWritePaths=/mnt/nvme1/rdb /mnt/nvme2/rdb /mnt/backup/rdb), and make them owned by the service user.

Check that it works

sh
export PGPASSWORD='use-a-long-secret'
psql "postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require" \
  -c "SHOW STORAGE LOCATIONS" \
  -c "CREATE TABLE events (id INT PRIMARY KEY, body TEXT)" \
  -c "SHOW STORAGE PLACEMENTS"

SHOW STORAGE LOCATIONS has a row per directory and SHOW STORAGE PLACEMENTS a row per table, showing which directory it landed in. SHOW STORAGE sums both up.

Manage it while it runs

sql
STORAGE ADD LOCATION '/mnt/nvme3/rdb' WITH (ROLE = 'primary', WEIGHT = 2);
STORAGE PLACE TABLE audit AT '/mnt/nvme3/rdb';       -- before CREATE TABLE audit
STORAGE MOVE TABLE events TO '/mnt/nvme2/rdb';       -- copies, switches, then deletes the old files
STORAGE DISABLE LOCATION '/mnt/nvme1/rdb';           -- keeps its tables, takes no new ones
STORAGE REBALANCE DRY RUN;                           -- what a rebalance would move, and why
STORAGE REBALANCE;                                   -- move it: drains disabled locations first
STORAGE DROP LOCATION '/mnt/nvme1/rdb';              -- only once it holds no tables
STORAGE SET POLICY = 'round-robin';

The server never moves a table by itself; moves happen only on STORAGE MOVE TABLE and STORAGE REBALANCE.

Stop or undo

Ctrl+C stops the server cleanly. To retire a disk: STORAGE DISABLE LOCATION, STORAGE REBALANCE, then STORAGE DROP LOCATION. Never delete a location's directory while the pool still lists it: its tables would open as missing.

Limits

  • Where each table lives is recorded in db_storage.cfg by absolute path. Copying or moving the whole database to other paths needs that file edited while the server is stopped; a restore from S3 rewrites it for you.
  • A backup location is a mirror, not a backup in time: a DROP TABLE or a bad UPDATE is mirrored too. Keep S3 backups as well (s3-backup-pitr).
  • A location's directory holding tables cannot be made a backup, and a table cannot be moved into a backup location.
  • In bash, RDB_PRIMARIES cannot hold paths with spaces; pass such directories in the script's args line directly.

The files

setup.sh · Linux and macOS (Bash)DOWNLOAD ↓
#!/usr/bin/env bash
# RasterizedDB across several disks: new tables are spread over extra primary
# directories by a placement policy, and an optional backup directory gets a
# mirrored copy of every write. Runs in the foreground.
#
# Every setting can be overridden from the environment, for example:
#   RDB_PRIMARIES="/mnt/nvme1/rdb /mnt/nvme2/rdb" RDB_MIRROR=/mnt/backup/rdb ./setup.sh
set -euo pipefail

RDB_BIN="${RDB_BIN:-rasterizeddb}"
RDB_DATA="${RDB_DATA:-./data}"            # the database's own directory: catalogue, logs, the pool file
RDB_PRIMARIES="${RDB_PRIMARIES:-}"        # extra directories that hold tables, space-separated; DIR:WEIGHT for a weight
RDB_MIRROR="${RDB_MIRROR:-}"              # a directory that receives a copy of every write
RDB_POLICY="${RDB_POLICY:-free-space}"    # primary, round-robin or free-space
RDB_MIN_FREE="${RDB_MIN_FREE:-2GiB}"      # a directory with less free space takes no new tables
RDB_LISTEN="${RDB_LISTEN:-127.0.0.1}"

die() { echo "error: $*" >&2; exit 1; }

command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the server binary"
: "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}"
export RASTERIZEDDB_PG_PASSWORD
[ -n "$RDB_PRIMARIES$RDB_MIRROR" ] || die "set RDB_PRIMARIES and/or RDB_MIRROR to the extra directories"

mkdir -p "$RDB_DATA"
RDB_DATA="$(cd "$RDB_DATA" && pwd)"

# Locations must be absolute paths, outside the data directory and each other.
abs() { mkdir -p "$1" && (cd "$1" && pwd); }
args=(--location "$RDB_DATA" --protocol both --pg-addr "$RDB_LISTEN" --rdb-addr "$RDB_LISTEN"
  --storage-policy "$RDB_POLICY" --storage-min-free "$RDB_MIN_FREE")
for p in $RDB_PRIMARIES; do
  dir="$p"
  weight=""
  if [[ "$p" =~ ^(.+):([0-9]+)$ ]]; then
    dir="${BASH_REMATCH[1]}"
    weight="${BASH_REMATCH[2]}"
  fi
  dir="$(abs "$dir")"
  args+=(--storage-location "$dir:primary${weight:+:$weight}")
  echo "primary: $dir${weight:+ (weight $weight)}"
done
if [ -n "$RDB_MIRROR" ]; then
  dir="$(abs "$RDB_MIRROR")"
  args+=(--storage-location "$dir:backup")
  echo "backup:  $dir"
fi

echo "Policy $RDB_POLICY, minimum free $RDB_MIN_FREE. Locations are added to the pool once and remembered."
echo "PostgreSQL: postgresql://rasterized@$RDB_LISTEN:5432/postgres?sslmode=require"
echo "Check:      psql … -c 'SHOW STORAGE'"
echo

exec "$RDB_BIN" "${args[@]}"
setup.ps1 · Windows (PowerShell)DOWNLOAD ↓
#Requires -Version 5.1
<#
.SYNOPSIS
  RasterizedDB across several disks: new tables are spread over extra primary
  directories by a placement policy, and an optional backup directory gets a
  mirrored copy of every write. Runs in the foreground.
.EXAMPLE
  $env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
  .\setup.ps1 -Primaries 'D:\rdb', 'E:\rdb:2' -Mirror 'F:\rdb-mirror'
#>
[CmdletBinding()]
param(
    [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }),
    # The database's own directory: catalogue, logs, the pool file.
    [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }),
    # Extra directories that hold tables; DIR:WEIGHT gives one a weight.
    [string[]]$Primaries = $(if ($env:RDB_PRIMARIES) { $env:RDB_PRIMARIES -split ';' } else { @() }),
    # A directory that receives a copy of every write.
    [string]$Mirror = $env:RDB_MIRROR,
    [ValidateSet('primary', 'round-robin', 'free-space')]
    [string]$Policy = $(if ($env:RDB_POLICY) { $env:RDB_POLICY } else { 'free-space' }),
    # A directory with less free space takes no new tables.
    [string]$MinFree = $(if ($env:RDB_MIN_FREE) { $env:RDB_MIN_FREE } else { '2GiB' }),
    [string]$Listen = $(if ($env:RDB_LISTEN) { $env:RDB_LISTEN } else { '127.0.0.1' })
)
$ErrorActionPreference = 'Stop'

$cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue
if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." }
if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized.' }
if (-not $Primaries -and -not $Mirror) { throw 'Pass -Primaries and/or -Mirror with the extra directories.' }

function Get-AbsoluteDir([string]$Path) {
    New-Item -ItemType Directory -Force -Path $Path | Out-Null
    (Resolve-Path -LiteralPath $Path).Path
}

$Data = Get-AbsoluteDir $Data
$serverArgs = @('--location', $Data, '--protocol', 'both', '--pg-addr', $Listen, '--rdb-addr', $Listen,
    '--storage-policy', $Policy, '--storage-min-free', $MinFree)
foreach ($p in $Primaries) {
    $dir = $p; $weight = ''
    if ($p -match '^(.+):(\d+)$') { $dir = $Matches[1]; $weight = $Matches[2] }
    $dir = Get-AbsoluteDir $dir
    $spec = "${dir}:primary"
    if ($weight) { $spec += ":$weight" }
    $serverArgs += @('--storage-location', $spec)
    Write-Host "primary: $dir$(if ($weight) { " (weight $weight)" })"
}
if ($Mirror) {
    $dir = Get-AbsoluteDir $Mirror
    $serverArgs += @('--storage-location', "${dir}:backup")
    Write-Host "backup:  $dir"
}

Write-Host "Policy $Policy, minimum free $MinFree. Locations are added to the pool once and remembered."
Write-Host "PostgreSQL: postgresql://rasterized@${Listen}:5432/postgres?sslmode=require"
Write-Host "Check:      psql … -c 'SHOW STORAGE'"
Write-Host ''

& $cmd.Source @serverArgs
exit $LASTEXITCODE
✓ page rendered in 134 µs axum + tera + htmx · no other javascript