DBTrail

Reference

bintrail-pg for a headless deployment, its flags, resuming and rotation, the PostgreSQL port's status line, and every option-file setting for PostgreSQL.

The details for running PostgreSQL capture without the web interface, and every setting that applies to a PostgreSQL server or to the PostgreSQL port. For everyday use, the web interface and the dbtrail command cover all of it: Add and run a server.

Headless: bintrail-pg

bintrail-pg: standalone capture binary plus the shared read and recovery commands, as a Docker image, .deb/.rpm, or from source. Prefer a headless deployment, or capturing into an index you already run? bintrail-pg ships as its own artifact alongside the core bintrail binary.

Docker image:

docker pull ghcr.io/dbtrail/bintrail-pg:latest
docker run --rm ghcr.io/dbtrail/bintrail-pg:latest --version

Linux packages (.deb / .rpm, amd64 + arm64) are attached to every release as bintrail-pg_*:

# Debian/Ubuntu
sudo dpkg -i bintrail-pg_*_linux_amd64.deb
# RHEL/Fedora
sudo rpm -i bintrail-pg_*_linux_amd64.rpm

From source:

git clone https://github.com/dbtrail/dbtrail && cd dbtrail
make build-pg        # produces ./bintrail-pg (requires CGO: DuckDB is embedded)

bintrail-pg carries the PostgreSQL capture command (stream) plus the shared read/recovery commands (query, recover, reconstruct, status, shim): the same ones the core binary exposes, working over the same index.


Running it

PostgreSQL needs two connection strings, a protocol constraint, not a quirk:

  • --repl-dsn: a replication connection. Its connection string must include replication=database. A replication connection runs in walsender mode and cannot run ordinary SQL, which is why a second connection is needed.
  • --query-dsn: an ordinary connection, used for primary-key lookups in the catalog and the startup validations.

Both are standard PostgreSQL/libpq URLs (so TLS is configured the usual way, via sslmode= in the DSN).

bintrail-pg stream \
  --index-dsn   "$IDX" \
  --repl-dsn    'postgres://dbtrail:pw@pg-host:5432/shop?replication=database&sslmode=require' \
  --query-dsn   'postgres://dbtrail:pw@pg-host:5432/shop?sslmode=require' \
  --slot        bintrail_shop \
  --publication bintrail_pub \
  --server-id   201 \
  --schemas     shop

$IDX stands for the --index-dsn value, in all the commands on this page.

That command: validates wal_level and the publication, and warns about any table not at REPLICA IDENTITY FULL (not captured; the other tables are); creates the slot bintrail_shop if absent; bootstraps the index tables if needed; then streams every row change into the index. Send SIGINT/SIGTERM for a graceful shutdown (it flushes the batch and writes a final checkpoint).

Flags

FlagRequiredMeaning
--index-dsnyesDBTrail's history store, the index (env BINTRAIL_INDEX_DSN); its format: index-dsn.
--repl-dsnyesPostgreSQL replication DSN, must carry replication=database (env BINTRAIL_PG_REPL_DSN).
--query-dsnyesPostgreSQL ordinary DSN for catalog/PK lookups (env BINTRAIL_PG_QUERY_DSN).
--slotyesLogical replication slot name; created if absent (env BINTRAIL_PG_SLOT).
--publicationyesPublication name covering the tables to capture (env BINTRAIL_PG_PUBLICATION).
--server-idyesA unique number identifying this source in the index: must differ from every other source (env BINTRAIL_SERVER_ID).
--schemasnoOnly index these schemas (comma-separated).
--tablesnoOnly index these tables (comma-separated, e.g. public.orders).
--start-lsnnoExplicit start LSN; first run only, ignored once a checkpoint exists.
--batch-sizenoEvents per batch insert (default 1000).
--checkpointnoCheckpoint interval in seconds (default 5).
--partitionsnoIndex partitions for the one-time bootstrap (default 48).
--metrics-addrnoAddress to expose Prometheus metrics on, e.g. :9090; empty (the default) exposes none (env BINTRAIL_METRICS_ADDR). See Metrics.
--rotate-retainnoBuilt-in rotation: drop index partitions older than this (Nd/Nh; off disables). Default 48h for a new index; an index created before that default changed keeps the window it was created under (env BINTRAIL_ROTATE_RETAIN).
--rotate-intervalnoBuilt-in rotation: how often a rotation cycle runs (default 1h, env BINTRAIL_ROTATE_INTERVAL).
--rotate-add-futurenoBuilt-in rotation: keep at least N future hourly partitions ready (default 3, env BINTRAIL_ROTATE_ADD_FUTURE).

Every flag has a BINTRAIL_* environment equivalent and can be set in a .bintrail.env file (which files are read, and in what order).

Resuming and re-seeding

The durable checkpoint is the last committed LSN, stored in the index's stream_state. On restart bintrail-pg resumes from it automatically: re-running the same command is idempotent, and --start-lsn is ignored once a checkpoint exists.

To start over from scratch you must drop the slot and clear the checkpoint, because in PostgreSQL the slot governs the resume position, so clearing the checkpoint alone does not rewind. bintrail-pg reset does both (it drops the slot first, so an interrupted reset fails safe: "slot gone, checkpoint stale" rather than "checkpoint cleared, slot live"):

bintrail-pg reset --query-dsn "$PG" --index-dsn "$IDX" --slot bintrail_shop --force

--force confirms the destructive teardown. If the slot is already gone or lost (e.g. after a max_slot_wal_keep_size invalidation), pass --index-only to clear just the checkpoint:

bintrail-pg reset --index-dsn "$IDX" --index-only --force

This removes only capture-resume state; the index's recovery data is untouched. Under the hood it is the two-system teardown that otherwise has to be done by hand (SELECT pg_drop_replication_slot('bintrail_shop') on the source + clearing the position/counter columns of the stream_state row on the index: the row is never DELETEd, so a recorded gap_lost_at/gap_lost_detail continuity-loss record is never erased by a reset; a real-checkpoint reset replaces the record's detail with the reset's own and withdraws any acknowledgement of the earlier loss, a no-checkpoint reset preserves it verbatim).

A reset that discards a real checkpoint is itself recorded as a permanent continuity loss: dropping and recreating the slot skips whatever the old slot had not yet streamed, so the discarded LSN is stamped into gap_lost_at/gap_lost_detail: bintrail status shows the EVENTS PERMANENTLY LOST banner and status --fail-on-gap exits non-zero, exactly as after a slot invalidation. After a reset, re-seed the baseline and re-run bintrail-pg stream.

Index rotation and retention

The index's events table is split into hourly partitions. New hours need new partitions or every event eventually lands in the catch-all p_future partition, which can never be dropped: so without rotation the index grows until the disk fills.

bintrail-pg stream runs a built-in rotation loop so this is handled for you: by default it drops partitions older than 30 days and keeps a few future partitions ready, running one cycle at startup and then every hour. Tune it with --rotate-retain / --rotate-interval / --rotate-add-future (or the BINTRAIL_ROTATE_* env vars), or turn it off with --rotate-retain off if you prefer to run retention separately.

The built-in loop is drop-only: it does not archive partitions to S3 before dropping them. For an archive-then-drop retention policy, or for manual/offline maintenance, run the standalone command against the same index (it is the same index-side command the core bintrail binary exposes, now available on bintrail-pg too):

# One-off: drop partitions older than 7 days
bintrail-pg rotate --index-dsn "$IDX" --retain 7d

# Daemon: archive each expired partition to S3, then drop it
bintrail-pg rotate --index-dsn "$IDX" --retain 30d --daemon \
  --archive-dir /var/lib/bintrail/archives --archive-s3 s3://my-bucket/archives/

# Re-sync the archive registry with the files actually on disk / in S3
bintrail-pg archive reconcile --index-dsn "$IDX" \
  --archive-dir /var/lib/bintrail/archives --archive-s3 s3://my-bucket/archives/

Upgrading an existing PostgreSQL install that predates built-in rotation. If you ran bintrail-pg stream before this feature, its index has no future partitions and everything has piled into p_future. No data is lost (the loop refuses to drop deep history until you choose a retention explicitly, and it back-fills the missing partitions), but the first catch-up can be slow because each cycle rewrites the large p_future. To catch up in one pass instead, run a standalone rotation with enough headroom before (or alongside) the stream, e.g. bintrail-pg rotate --index-dsn "$IDX" --add-future <hours-since-first-event>, then set --rotate-retain explicitly (e.g. 30d) so the loop begins pruning.


Adding more servers

Capture each PostgreSQL source by running its own bintrail-pg stream process with a unique --server-id and a unique --slot. There is no shared daemon for PostgreSQL sources in this release: one process per source (systemd unit, container, etc.).

You can view and recover PostgreSQL-captured data in the read-only web interface (bintrail-console), which reads the shared index. The web interface presents PostgreSQL sources natively: LSN/slot vocabulary, a lost-slot badge, a connection-id note, and a live replication-health panel (slot WAL-retention, lag, RI-FULL); see What the web interface shows. What it does not drive is the capture a bintrail-pg stream process does, so run bintrail-pg stream for the capture and use the web interface (or the query/recover CLI) to browse and recover.


The PostgreSQL port's status line

From 0.138.0 the PostgreSQL port writes one line a minute to its log for each PostgreSQL server:

docker compose logs -f pg-router | grep msg=status      # Docker
journalctl -u bintrail-pg-router -f | grep msg=status   # Linux packages
msg=status service=pg-port server=shop interval=1m0s queries=124 dbtrail=110 postgresql=12 refused=2 errors=2 dbtrail_avg_ms=420 postgresql_avg_ms=30 copy_behind_s=45

queries counts the statements the port saw that minute, dbtrail those the copy answered and postgresql those your PostgreSQL answered, then the ones refused and the errors. dbtrail_avg_ms and postgresql_avg_ms are the mean time of each. copy_behind_s is how old the data of the copy's last answer was; it is left out when that is not known, as in fast mode. The numbers count the minute, not everything since the start, and a quiet minute still writes a line.

STATUS_LOG_INTERVAL=30s in .env (Docker), or BINTRAIL_STATUS_LOG_INTERVAL=30s in /etc/bintrail/bintrail-console.env (Linux packages), changes how often. off writes none. With Docker, the PostgreSQL port takes STATUS_LOG_INTERVAL from the compose file of 0.138.0 on; with an older file it writes its line once a minute.

Option file settings

Each setting is a line of DBTrail's option file (where it goes), and can also be given as the environment variable named under it.

index-dsn

Where DBTrail's history store lives: the connection string of the database DBTrail writes captured changes into and reads them back from. Every program that records, queries or recovers needs it. It is written user:password@tcp(host:port)/database.

  • Default: none. watch, bintrail-pg stream and most bintrail-pg commands refuse to start without it; serve can start without it if its server list has entries.
  • Example: index-dsn = dbtrail:password@tcp(127.0.0.1:3306)/dbtrail_index
  • Environment variable: BINTRAIL_INDEX_DSN
  • Read by: watch, serve, and bintrail-pg (stream, flashback, reset, and the shared query, recover and rotate commands).
  • Holds a secret: dbtrail config show never prints it, and a file every user can read that sets it gets a warning. A password with a # goes in quotes (values).

pg-table-deltas

For PostgreSQL servers only: when a snapshot is updated, a changed table keeps its previous file and gets small files with its changes beside it, instead of being written again in full. Everything that reads the copy on this machine reads those files with the table's file. 0, false, no or off turns it off: every changed table is written in full at every update.

  • Default: on.
  • Example: pg-table-deltas = off
  • Environment variable: BINTRAIL_PG_TABLE_DELTAS
  • Read by: watch (PostgreSQL servers).

The PostgreSQL port (bintrail-console pg-router)

pg-router-listen

Address (host:port) the PostgreSQL-protocol port listens on, where psql and PostgreSQL drivers connect.

  • Default: 127.0.0.1:5433.
  • Example: pg-router-listen = 0.0.0.0:5433
  • Environment variable: BINTRAIL_PG_ROUTER_LISTEN
  • Read by: pg-router.

pg-router-tls-cert

Certificate file (PEM, with its chain after it if any) the PostgreSQL port offers to clients that ask for TLS, so they can verify it (sslmode verify-ca or verify-full). Must be set together with pg-router-tls-key; read only at start.

  • Default: none: clients that ask for TLS get a certificate made up at start that they cannot verify.
  • Example: pg-router-tls-cert = /etc/dbtrail/tls/router.crt
  • Environment variable: BINTRAIL_PG_ROUTER_TLS_CERT
  • Read by: pg-router.

pg-router-tls-key

Private key file (PEM, not encrypted) that goes with pg-router-tls-cert for the PostgreSQL port. Must be set together with the certificate.

  • Default: none.
  • Example: pg-router-tls-key = /etc/dbtrail/tls/router.key
  • Environment variable: BINTRAIL_PG_ROUTER_TLS_KEY
  • Read by: pg-router.

pg-router-workers

How many statements the PostgreSQL router runs on the copy at the same time, 1 to 16. The rest wait their turn. From 0.141.1 the copy has one worker more, kept for statements whose plan reads one table and joins nothing, so up to this plus 1 run at once; every other statement runs on this many at most.

  • Default: 4.
  • Example: pg-router-workers = 8
  • Environment variable: BINTRAIL_PG_ROUTER_WORKERS
  • Read by: pg-router.

pg-router-threads

How many CPU threads each statement on the copy may use in the PostgreSQL router, 1 to 16.

  • Default: 2.
  • Example: pg-router-threads = 4
  • Environment variable: BINTRAIL_PG_ROUTER_THREADS
  • Read by: pg-router.

pg-router-max-connections

How many clients may be connected to the PostgreSQL port at once, 1 to 1024. One more is refused with an error.

  • Default: 256.
  • Example: pg-router-max-connections = 512
  • Environment variable: BINTRAIL_PG_ROUTER_MAX_CONNECTIONS
  • Read by: pg-router.

pg-router-max-source-connections

How many connections to your own PostgreSQL servers the PostgreSQL router holds at once, across all servers, 1 to 1024. A client uses one while a statement the source answers (or its transaction) runs there; past the limit that statement waits for one to be given back, up to 30 seconds, and is then refused with error 53300. This keeps the port from using up the source's own connection slots.

  • Default: 16.
  • Example: pg-router-max-source-connections = 32
  • Environment variable: BINTRAIL_PG_ROUTER_MAX_SOURCE_CONNECTIONS
  • Read by: pg-router.

pg-router-cost-threshold

For a server with a forwarding user: a statement the copy answers as PostgreSQL does goes to the copy only when it is expensive on your PostgreSQL. This is the plan cost that counts as expensive: the Total Cost at the top of the plan EXPLAIN gives on the source, asked before the statement runs. Below it, and with no sequential scan over BINTRAIL_PG_ROUTER_SCAN_ROWS rows, the source answers, by its indexes (a read by a primary key costs about 1 to 12). 0 turns this rule off.

  • Default: 50000.
  • Example: pg-router-cost-threshold = 100000
  • Environment variable: BINTRAIL_PG_ROUTER_COST_THRESHOLD
  • Read by: pg-router.

pg-router-scan-rows

For a server with a forwarding user: a statement the copy answers as PostgreSQL does goes to the copy when the plan EXPLAIN gives on the source has a sequential scan (any Seq Scan, at any depth) estimated at this many rows or more, whatever its cost. 0 turns this rule off; it cannot be 0 together with BINTRAIL_PG_ROUTER_COST_THRESHOLD.

  • Default: 1000000.
  • Example: pg-router-scan-rows = 500000
  • Environment variable: BINTRAIL_PG_ROUTER_SCAN_ROWS
  • Read by: pg-router.

pg-router-max-wait

How long a statement waits for a free worker in the PostgreSQL router when all are busy, written with a unit like 30s or 2m; past it the client gets an error. Empty or 0 sets no limit of its own, and the five minutes a single statement may take (wait included) is what ends it.

  • Default: no limit of its own (the 5-minute statement limit applies).
  • Example: pg-router-max-wait = 30s
  • Environment variable: BINTRAIL_PG_ROUTER_MAX_WAIT
  • Read by: pg-router.

pg-router-status-addr

Address (host:port) where the PostgreSQL router serves /status (JSON saying, per server, which side answered each statement and why) and /metrics (Prometheus). It asks for no password. The web interface reads it through console-pg-router-url.

  • Default: none: no status or metrics are served.
  • Example: pg-router-status-addr = 127.0.0.1:9092
  • Environment variable: BINTRAIL_PG_ROUTER_STATUS_ADDR
  • Read by: pg-router.

pg-router-status-statements

When true, the PostgreSQL router keeps a row per statement shape and side in /status (values blanked, table and column names kept). Anyone who reaches the status address can then read the names of the tables and columns your clients query.

  • Default: off.
  • Example: pg-router-status-statements = true
  • Environment variable: BINTRAIL_PG_ROUTER_STATUS_STATEMENTS
  • Read by: pg-router.

PostgreSQL sources (bintrail-pg)

pg-repl-dsn

Connection string to the PostgreSQL source opened in replication mode (it must include replication=database), used to read changes through a logical replication slot. bintrail-pg stream requires it; bintrail-pg baseline uses it only to create the slot when it does not exist yet.

  • Default: none: stream refuses to start.
  • Example: pg-repl-dsn = postgres://repl_user:password@pg-host:5432/appdb?replication=database
  • Environment variable: BINTRAIL_PG_REPL_DSN
  • Read by: bintrail-pg (stream, baseline).
  • Holds a secret: dbtrail config show never prints it, and a file every user can read that sets it gets a warning.

pg-query-dsn

Ordinary connection string to the PostgreSQL source. stream uses it to look up table and primary key details; baseline runs the snapshot copy on it; doctor runs its checks on it; reset uses it to drop the replication slot.

  • Default: none: stream, baseline and doctor refuse to start (reset needs it unless --index-only).
  • Example: pg-query-dsn = postgres://app_reader:password@pg-host:5432/appdb
  • Environment variable: BINTRAIL_PG_QUERY_DSN
  • Read by: bintrail-pg (stream, baseline, doctor, reset).
  • Holds a secret: dbtrail config show never prints it, and a file every user can read that sets it gets a warning.

pg-slot

Name of the PostgreSQL logical replication slot that keeps the source's change log for DBTrail. stream creates it if missing; baseline anchors its snapshot to it; doctor inspects it; reset drops it.

  • Default: none: required.
  • Example: pg-slot = dbtrail_slot
  • Environment variable: BINTRAIL_PG_SLOT
  • Read by: bintrail-pg (stream, baseline, doctor, reset).

pg-publication

Name of the PostgreSQL publication that lists which tables are captured. DBTrail only checks and uses it; it does not create it.

  • Default: none: required.
  • Example: pg-publication = dbtrail_pub
  • Environment variable: BINTRAIL_PG_PUBLICATION
  • Read by: bintrail-pg (stream, baseline, doctor).

pg-start-lsn

Position in the PostgreSQL change log (an LSN, like 0/1A2B3C4) where the first run of bintrail-pg stream starts reading. Ignored once a saved checkpoint exists.

  • Default: none: start where the slot stands.
  • Example: pg-start-lsn = 0/1A2B3C4
  • Environment variable: BINTRAIL_PG_START_LSN
  • Read by: bintrail-pg (stream).

pg-flashback-listen

The address (host:port) where bintrail-pg flashback (time travel for one row over the PostgreSQL protocol) listens for psql and other PostgreSQL clients.

  • Default: 127.0.0.1:5433.
  • Example: pg-flashback-listen = 127.0.0.1:5434
  • Environment variable: BINTRAIL_PG_FLASHBACK_LISTEN
  • Read by: bintrail-pg (flashback).

pg-flashback-tls-cert

Certificate file (PEM, with its chain after it) bintrail-pg flashback offers to clients that ask for TLS, so they can verify it (sslmode verify-ca or verify-full). Must be set with the key; read at start.

  • Default: none: clients that ask for TLS get a certificate made up at start.
  • Example: pg-flashback-tls-cert = /etc/dbtrail/tls/flashback.crt
  • Environment variable: BINTRAIL_PG_FLASHBACK_TLS_CERT
  • Read by: bintrail-pg (flashback).

pg-flashback-tls-key

Private key file (PEM, not encrypted) that goes with the bintrail-pg flashback TLS certificate.

  • Default: none.
  • Example: pg-flashback-tls-key = /etc/dbtrail/tls/flashback.key
  • Environment variable: BINTRAIL_PG_FLASHBACK_TLS_KEY
  • Read by: bintrail-pg (flashback).

pg-shim-config

The path of the tenants file (shim.yaml: users, passwords and the schemas each may read) that bintrail-pg flashback checks logins against.

  • Default: shim.yaml (in the working directory).
  • Example: pg-shim-config = /etc/dbtrail/shim.yaml
  • Environment variable: BINTRAIL_PG_SHIM_CONFIG
  • Read by: bintrail-pg (flashback).

pg-baseline-dir

Local folder with baseline snapshots, which lets bintrail-pg flashback answer _snapshot queries for rows that were not changed in the captured window. If baseline-s3 is also set, S3 wins.

  • Default: none: _snapshot has no baseline.
  • Example: pg-baseline-dir = /var/lib/dbtrail/pg-baselines
  • Environment variable: BINTRAIL_PG_BASELINE_DIR
  • Read by: bintrail-pg (flashback).

pg-baseline-s3

S3 location (prefix) of baseline snapshots for bintrail-pg flashback's _snapshot queries. Takes precedence over the local baseline folder.

  • Default: none.
  • Example: pg-baseline-s3 = s3://my-bucket/dbtrail/pg-baselines/
  • Environment variable: BINTRAIL_PG_BASELINE_S3
  • Read by: bintrail-pg (flashback).

On this page