DBTrail

Run and maintain DBTrail

The option file, what an update of the copy writes, table deltas and the index retention, where each server's snapshots live, the Status page, and the metrics of a PostgreSQL capture.

What to check and set once DBTrail records your PostgreSQL. Run these in the DBTrail folder, on the DBTrail machine.

Copy and paste

Check that it works

./dbtrail server list            # each server, and whether its changes are recorded
./dbtrail snapshot status shop   # how the last copy ended, or that one is running

The Status page of the web interface shows the same, with the replication slot's health (The Status page).

Where the copies are kept

./dbtrail snapshot location shop                                              # where they are now
./dbtrail snapshot location shop --keep-newest 3                              # keep the 3 newest in the folder
./dbtrail snapshot location shop --s3 s3://bucket/snapshots/ --s3-delete-after 30d   # also send them to S3

Each command changes only what it names. S3 uses the machine's own AWS credentials, unless you give the server others with ./dbtrail server edit.

The settings file

./dbtrail config check   # lines of the file that are not used, and why
./dbtrail config show    # every setting in effect, and where it comes from

With Docker, the file is dbtrail-config/dbtrail.cnf in the DBTrail folder. After a change, run docker compose restart. Everything about the file: The option file.

In detail

What you set and watch once DBTrail captures your PostgreSQL: the settings file, how the copy is kept up to date on disk, where snapshots are kept, and how to see that capture is healthy.

The option file

Needs DBTrail 0.131.0 or newer.

Every DBTrail program reads /etc/dbtrail/dbtrail.cnf when it starts: options under a [dbtrail] group, one per line.

[dbtrail]
pg-table-deltas = off
rotate-retain   = 7d
index-dsn       = 'dbtrail:secret@tcp(127.0.0.1:3306)/dbtrail_index'

An option is the name of its environment variable without BINTRAIL_, in lower case, with dashes: BINTRAIL_PG_TABLE_DELTAS is pg-table-deltas. pg_table_deltas reads the same. The settings a PostgreSQL server uses are listed in Option file settings. From 0.135.0, every install comes with a file that lists every option, explained and commented out.

Check it

dbtrail config check

It lists the settings the file sets, with their lines, and every line that is not used, and why. It exits with an error when it finds one, so a deploy can stop there. It never prints a value, since a value can hold a password.

See what is in effect

Needs DBTrail 0.135.0 or newer.

dbtrail config show
dbtrail config show --all

It lists every setting that has a value, the value and where it comes from: the environment, .bintrail.env or config.env, or a line of the file. A password, a token or a key is never printed, and neither is any other value that looks like it carries one. --all also lists the settings with no value.

What wins

  1. An option on the command line.
  2. The environment DBTrail was started with. A variable set to an empty value does not hide the file.
  3. .bintrail.env in the current folder, or ~/.config/bintrail/config.env.
  4. /etc/dbtrail/dbtrail.cnf.
  5. The default.

A change to the file is used at the next start. DBTrail never writes to it: what you save in the web interface stays where the web interface saves it.

For index rotation and the backup settings, a value saved in the web interface comes before all of these. The web interface says which value it is using instead of what, and where that is set, and dbtrail config check says when a line of the file is not used because of it.

The file is only read from /etc/dbtrail/dbtrail.cnf, never from the folder you are in. The dbtrail commands that talk to a running DBTrail send a password, and a file in whatever folder you happen to be in must not decide where it goes.

Lines that are not used

DBTrail starts anyway and prints a warning with the line number:

  • An option DBTrail does not have. The warning names the nearest one. Write loose- in front (loose-new-option = 1) to keep a line for a newer version without the warning.
  • An option with no value, or one before any [group].
  • A name DBTrail sets for its own processes, one that no longer does anything, or one for its tests.
  • An option the environment or .bintrail.env already sets.
  • !include and !includedir, which are not followed.
  • Text after the ] of a group, or a [bintrail] group with no [dbtrail].

Other groups are left alone. When an option is written twice, the last line that can be used wins.

Values

A # outside quotes starts a comment, so a password with a # goes in quotes; DBTrail warns when a # cuts a value that can hold a password. A backslash starts an escape (\n, \t, \s), so write \\ for a backslash.

Permissions

  • A file every user on the machine can change is not used at all: it could point DBTrail at another server. Fix it with chmod o-w /etc/dbtrail/dbtrail.cnf.
  • A file every user can read that sets a DSN or another secret is used, with a warning. Fix it with chmod o-r /etc/dbtrail/dbtrail.cnf.

With Docker Compose

DBTrail runs inside containers and reads the file there. From 0.135.0, put it in dbtrail-config/dbtrail.cnf, in the folder that holds docker-compose.yml: the compose file mounts that folder in every DBTrail service. The installer leaves a copy there with every option commented out. Restart the stack after a change: docker compose restart.

The services read it as user 999. Once it holds a password, chown 999 and chmod 600 it. The ./dbtrail config check of the stack runs inside the container, with DBTrail's own environment.

With the packages

From 0.135.0, the bintrail-console package (.deb and .rpm) installs the file at /etc/dbtrail/dbtrail.cnf, every option commented out, and an upgrade never replaces it. Once it holds a password, chgrp bintrail and chmod 640 it, which keeps it readable by the service. dbtrail config check compares the file with what the web interface saved in the service's server list: name it with --servers-file /var/lib/bintrail/console-servers.yaml.

What an update of the copy writes

Since 0.139.0, an update of a PostgreSQL server's copy does this, per table:

  • Nothing changed: the previous file is linked. It takes no extra disk.
  • Something changed: two small files beside the table's file hold the changes (<table>.<six digits>.pgdead and .pgnew). The table's file is not touched.
  • Rewritten in full once those small files are a day old, or together grow past half of the table's file (under 1 MiB they are left alone), and in a few other cases. The chain then starts over. This rebuilds the file from the previous snapshot; it does not read your PostgreSQL.
  • The first update after a full read writes every table once.

DuckDB reads the table's file together with its small files on every query, through views.sql. Another tool that reads the Parquet files directly, or a copy that takes only the *.parquet files, sees the table as it was when the chain started, with no error.

To write every changed table in full at every update, as before 0.139.0, set pg-table-deltas = off in the option file, or BINTRAIL_PG_TABLE_DELTAS=0 in the service's environment, then restart DBTrail.

Table deltas and the index retention

The small files of an update are built from the changes in the index, so they need the index to still hold every change since the table's file was written, with a margin of one hour plus one update interval. So the retention must be longer than two update intervals plus one hour. On a shorter retention, every update writes its changed tables in full, however little changed.

That line is the minimum. Rotation drops whole hours, so the oldest hour the index keeps can be up to an hour newer than the retention says; two update intervals plus two hours leaves room for that.

Update everyMinimum retentionWith room for rotation
5m2h3h
20m2h3h
6h14h14h

With updates a day apart or more, every update writes its changed tables in full on any retention, since the small files are kept for a day at most.

The retention is set in the web interface under Settings → Rotation, or with --rotate-retain (Index rotation and retention). The Rotation dialog does not count a PostgreSQL server when it works out the shortest retention it saves, so for a PostgreSQL server keep it above the minimum yourself, or turn table deltas off with pg-table-deltas = off.

Where each server's snapshots live

Web interface → Snapshots → Settings, under Where and how often. What you save there takes effect at once: the location of each server, Delete by age, and the schedule (Update the copy), which runs from its next slot.

Each server has a Local folder and an S3 location. Set either or both, then Save or Enter.

  • Keep by count is how many snapshots stay in the server's local folder, with or without an S3 location (since 0.130.0). A server you add starts at 3; empty keeps them all.
  • No location under a server means it has neither a Local folder nor an S3 location, so it keeps no snapshots: time travel, verification, scheduled snapshots and restores all wait until you save one.
  • A location is the server's own (since 0.92.0). Two servers saved on the same folder or S3 prefix keep reading, but snapshots, restores and the schedule refuse for both until each has its own; the page says so under each of them.
  • "The schedule cannot run as things stand." Usually a location cleared after the schedule was made. Every slot refuses until you save one.

For the free space a snapshot needs, see Disk for a PostgreSQL snapshot.

The Status page

Web interface → Status, per server. The web interface reads only DBTrail's history store, never your PostgreSQL.

Replication health

  • Position is shown as an LSN, and the source is labeled "PostgreSQL · logical replication". The slot and publication names are not stored in the history store, so the web interface does not show them.
  • The replication slot's state: wal_status, the WAL it retains, and the margin before PostgreSQL invalidates it.
  • Published tables that are not captured, in red as N tables NOT captured, each with why. A table that is not at REPLICA IDENTITY FULL, or has a partition that is not, is one of them (Prepare your PostgreSQL).
  • How recently it was checked. The capture reads the slot about every 30 seconds and saves what it found; the web interface shows that. A reading older than about 90 seconds is shown muted with a warning, since a stopped capture leaves its last reading behind. When the capture cannot read the slot at all (on a standby, for example), the panel shows probe failing with the reason. For a live check at any moment, run bintrail-pg doctor.

Changes lost for good

When the replication slot is invalidated or lost, the Status page shows a red badge. The history store is valid up to that point, and capture must start again from a new full read.

Stopping the server acknowledges the loss: the red badge goes away and the record stays. A snapshot update or a reconstruction whose window crosses the loss is still refused; a full snapshot taken after the loss is not affected. In place of the green no gaps, the page reads changes lost, on record in a neutral colour, with a box that says when the changes were lost and when that was acknowledged. The box stays after a new full read (Read database now, on the Snapshots page) for as long as the index still holds changes from the hour of the loss. Once rotation has dropped that hour, the page reads no gaps again, and the Restore coverage card goes back to continuity ok at the same moment. A loss nobody acknowledged keeps its red badge however old it is.

The same verdict is in bintrail-pg status --format json as continuity.status: ok, gap_lost, gap_acknowledged, or unknown (the index cannot be evaluated: treat it as not proven).

Index disk

The card shows the index size, its write rate, the size it settles at for your retention, the free space and the time until the disk is full. A warn or fail grade names the cause and the fix.

A full index disk loses data

A full volume stops capture. While capture is stopped, the replication slot makes your PostgreSQL keep WAL, until its disk fills or max_slot_wal_keep_size invalidates the slot and the changes are lost. Alert before that: Metrics.

From a terminal

bintrail-pg status --index-dsn "$IDX" --format json   # continuity.status and the last checkpoint
bintrail-pg status --index-dsn "$IDX" --fail-on-gap   # non-zero on a lost or unknown gap, for cron
bintrail-pg doctor ...                                # a live check of the source and the slot

$IDX stands for the history store's connection string (index-dsn).

GET /api/healthz answers whether the web interface is up, with no login. GET /api/status is the Status view as JSON, and needs an access token.

Metrics

A PostgreSQL capture exposes Prometheus metrics. On a standalone bintrail-pg stream, turn the endpoint on with --metrics-addr:

bintrail-pg stream --metrics-addr :9090 --index-dsn ... --repl-dsn ... --query-dsn ... --slot bintrail_slot --publication bintrail_pub --server-id 100
# scrape http://localhost:9090/metrics

Under bintrail-console watch there is nothing to turn on per server: the daemon's --metrics-addr serves every capture it runs. With no --metrics-addr, no metrics are exposed.

The source label is the server's entry ID under watch, and the replication slot's name on a standalone bintrail-pg stream.

Stream metrics (bintrail_stream_*)

MetricTypeMeaning
bintrail_stream_events_received_totalcounterRow changes received from the replication stream
bintrail_stream_events_indexed_totalcounterRow changes written to the index
bintrail_stream_batch_flushes_totalcounterBatch inserts run, including ones inside an index transaction a stop later rolled back; events_indexed_total counts only committed rows
bintrail_stream_checkpoint_saves_totalcounterCheckpoints saved
bintrail_stream_transaction_splits_totalcounterTimes a source transaction too large or too slow for one index transaction was made visible in parts; readers can see part of it until its end is captured
bintrail_stream_last_event_timestamp_secondsgaugeTime of the last change processed
bintrail_stream_replication_lag_secondsgaugeNow minus the time of the last change processed, counted when it is received, before it can be queried
bintrail_stream_index_commit_latency_secondshistogramSeconds from receiving a change to it being queryable in the index
bintrail_stream_availability_lag_secondsgaugeAbout how many seconds from the source commit to queryable. It carries any clock difference between the source and this machine
bintrail_stream_last_flush_timestamp_secondsgaugeWhen the last batch was committed to the index. Alert on time() - <metric>, never on its value
bintrail_stream_errors_totalcounterErrors, by type
bintrail_stream_batch_sizehistogramChanges per insert batch

What these mean on PostgreSQL where it is worth saying:

  • bintrail_stream_last_event_timestamp_seconds and the two lag gauges start from the source transaction's commit time. PostgreSQL sends a transaction when it commits, so the lag of a long transaction is measured from its commit, not from its first statement.
  • bintrail_stream_replication_lag_seconds can read "caught up" while a change still sits in a batch that is not written yet. To know how long a change was not yet recoverable, use bintrail_stream_index_commit_latency_seconds.
  • bintrail_stream_errors_total uses the types batch_flush and checkpoint, and health_probe: a reading of the slot's health that failed (the reading runs every 30 seconds).
  • A capture that is starting answers the scrape before it has connected to the index or the source, with every series at zero.

PostgreSQL capture (bintrail_pg_*)

MetricTypeMeaning
bintrail_pg_last_commit_lsngaugeCommit LSN of the last source transaction saved in the checkpoint, as a number of bytes. Capture resumes from here after a restart. 0 until the first checkpoint
bintrail_pg_slot_retained_wal_bytesgaugeWAL the source is keeping for the replication slot, as of the last reading of the slot. The same number as the Status page's slot health
bintrail_pg_slot_health_timestamp_secondsgaugeWhen the slot was last read and found. The reading above is kept when a reading fails or the slot is gone, so check its age with this
bintrail_pg_caught_up_timestamp_secondsgaugeThe last moment the capture is known to have held everything the source had. Set on a quiet source too, which the lag gauges are not

To compare bintrail_pg_last_commit_lsn with the source, write the source's position the same way: SELECT pg_current_wal_lsn() - '0/0'::pg_lsn.

# the slot is holding more than 10 GiB of WAL on the source
bintrail_pg_slot_retained_wal_bytes > 10 * 1024 * 1024 * 1024

# the slot has not been read for 5 minutes: the number above is old
time() - bintrail_pg_slot_health_timestamp_seconds > 300

# the capture has not been caught up with its source for 5 minutes
time() - bintrail_pg_caught_up_timestamp_seconds > 300

The first one is the alert that protects your PostgreSQL: a slot whose consumer is stopped or slow makes PostgreSQL keep WAL until the disk fills, or until max_slot_wal_keep_size invalidates the slot and the changes are lost.

A PostgreSQL capture does not publish the bintrail_index_* gauges.

On this page