#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