DBTrail

The copy and its snapshots

How DBTrail keeps its Parquet copy of your PostgreSQL tables current, what an update writes, and when it reads your PostgreSQL in full instead.

The copy is a set of files, one per table, that your reports read instead of your PostgreSQL. The first copy reads your PostgreSQL once. Each update after it is built from the previous copy and the recorded changes, without reading your PostgreSQL again.

Copy and paste

./dbtrail snapshot run shop                   # read every table now: the first copy, or a fresh one
./dbtrail snapshot schedule shop --every 1h   # update the copy every hour
./dbtrail snapshot status shop                # how the last one ended
./dbtrail snapshot schedule shop --off        # stop the updates

Put your server's name in place of shop. snapshot run waits until the copy is ready; Ctrl-C stops the waiting, not the read. --every takes 30m, 1h, 6h, 1d and so on.

When to read every table again. An update refuses a table it cannot bring up to date safely: after a column change, a TRUNCATE, a DROP or a rename, among others (every rule). The schedule then reads your PostgreSQL in full by itself, where it may. To do it now, run ./dbtrail snapshot run shop.

Prefer a browser? The Snapshots page has the same: Read database now, and Update the copy under Where and how often.

In detail

The copy is a set of Parquet files, one per table, that SQL on the copy reads. The first snapshot reads your PostgreSQL in full; each one after it is built from the previous snapshot and the changes captured since. This page says what an update writes, when it reads the source again instead, and what refuses an update.

SQL on the copy

DBTrail keeps a copy of your tables as Parquet files, and SQL runs on that copy, not on your PostgreSQL: from the SQL page of the web interface, from the AI connection, or from psql.

Numbers, dates, booleans, uuid, arrays, time and interval read as their types, not as text. Statements run by PostgreSQL's rules where they change the answer: NULL sorts last ascending, two integers divide to an integer, and text compares with case. What still differs, and how each type reads: how each type reads in SQL on the copy.

The SQL page and the AI connection answer as of the copy's last update. The PostgreSQL port answers near real time. A snapshot schedule updates the copy this way: the first snapshot reads the source in full, and each one after it is built from the previous snapshot and the changes captured since, without reading the source again. A table added to the publication after a snapshot has no file to update, so the schedule takes one full read to bring it into the copy.

From 0.139.1 an update also notices a column change on a table nothing wrote to afterwards (DROP COLUMN, ADD COLUMN, RENAME COLUMN, ALTER COLUMN ... TYPE). It publishes nothing, the run names the table and what changed, and the schedule takes a full read in its place where it may. Until that read, the copy stays as it was. An index, a default or NOT NULL stops nothing.

An update does not write again the tables that did not change: the new snapshot reuses their files, which take no extra disk. Since 0.139.0 a table that did change is not written in full either: its file is kept, and the update writes small files of its changes beside it (<table>.<six digits>.pgdead and .pgnew), which everything that reads the copy reads with the file (what an update writes). The table is written in full again from time to time, and the first update after a full read writes every table once. To write every changed table in full at every update, as before 0.139.0, set pg-table-deltas = off in dbtrail.cnf (where it goes), or BINTRAIL_PG_TABLE_DELTAS=0. Table deltas also need the index to keep every change since a table's file was written: on a shorter retention, every update writes its changed tables in full (retention).

What sets how far behind the copy is: How current an answer is.

Every rule in full

bintrail reconstruct --output-format parquet --tables public.orders --at … and bintrail baseline refresh take a PostgreSQL snapshot (from bintrail-pg baseline, or a previous fold) and the indexed changes after it, and write a snapshot of the whole table at the requested point. Each one records where the next fold starts (an LSN) and how far back in time the changes after it can reach, so a chain of folds applies every transaction exactly once.

A snapshot schedule set on a PostgreSQL server in the web interface does the same on its own. The first snapshot is a full read of the source; each slot after it builds the next snapshot from the previous one and the changes captured since, without reading the source again. An update holds the changes up to capture's last saved position, which capture saves every 10 seconds. A table added to the publication after the previous snapshot has no file to update, so the update publishes without it and names it, and the schedule then takes one full read of the source to bring it into the copy, where the schedule may take full reads, and no more than a few times for the same table.

In that same read of the source, each update also asks the catalog what columns every table of the publication has now, and compares them with the columns the table's file in the previous snapshot records: names, types, and each column's position number, which tells a column that was dropped and added again under the same name from the one the snapshot holds values for. PostgreSQL's change stream describes a table only when it next carries a change of it, so a column dropped, added, renamed or retyped on a table that nothing has written to since is recorded nowhere else. A table that differs stops the update the way a column change the stream did record stops it: no snapshot is published by that update, the run names each such table with what changed (dropped amount, added region, retyped total), and the schedule takes a full read of the source in its place where it may take full reads. Where it may not, updates stay refused, with the tables named, until a full read is taken. An index, a default, NOT NULL, a comment or a grant changes no column and stops nothing. Only the catalog is read: nothing is created or written on the source.

Two limits. The catalog is read once, at the start of the update: a column changed after that read and before the snapshot is published is in that snapshot with its old columns, and the next update sees it. And when the source cannot be reached for that read, the update still publishes from the changes it captured, as it does for new tables, and the run says that neither new tables nor changed columns were looked for. An update that has no captured change to apply is skipped before it reads anything, so on a source with no writes at all a changed column is seen at the first update after the next write to any table. bintrail baseline refresh on the command line does not read the source and makes no such comparison.

A full read copies tables in parallel, each on a connection of its own to the source. One started by DBTrail itself (the Create button, a schedule, snapshot run) copies four tables at once, so at most four connections to the source copy tables at a time. bintrail-pg baseline takes its number from --parallelism, one per CPU of the machine it runs on unless set.

A full read starts from the replication slot's position, so it needs the slot to have one. On a busy source capture can take a while to get its slot: PostgreSQL finishes creating a slot only when every write transaction that was open when the creation began has ended. A snapshot asked for while capture is creating the slot (the Create button, snapshot run, bintrail-pg baseline) waits for it, up to 5 minutes, and then reads; it does not read while the slot has no position, because a change committed before the slot got one would be in neither the snapshot nor the captured changes. Past the 5 minutes the snapshot is refused with nothing read, and running it again once capture is attached works. A source whose slot already has its position is read at once, as before. When there is no slot yet, the snapshot creates it, and that creation has no limit. Neither has the one capture does when it starts: the server shows CREATING SLOT with the oldest write transaction open (its session and for how long; its user and database when it is in the captured database; a prepared transaction without its name, which pg_prepared_xacts lists), and capture starts by itself when that ends. While either waits, the log says once a minute how long it has waited; the transaction holding it is the one with the oldest xact_start in pg_stat_activity among those with a backend_xid. A snapshot asked for while capture is creating the slot still waits only the 5 minutes above.

The slot is looked at again when the tables are read. A snapshot whose slot was removed meanwhile, or whose changes start before the position capture started reading from, fails and is not kept. A snapshot taken by an earlier version that starts before capture did is not updated: the update is refused for its tables and the schedule reads the source again in its place.

What to know:

  • The output is a snapshot, or SQL psql loads. A snapshot's values are PostgreSQL's own text, as bintrail-pg baseline writes them. --output-format psql instead writes one file per table, <schema>.<table>.sql, that psql -v ON_ERROR_STOP=1 -f <file> loads in one transaction: the settings the values were rendered under, a CREATE TABLE built from the snapshot's recorded shape (columns, types, NOT NULL, defaults, identity and serial sequences, STORED generated columns, the primary key), the rows as COPY ... FROM stdin, and each sequence moved past the loaded rows. Not made: other indexes and constraints, collations, comments, grants, and any type or sequence a default refers to that the table does not own; a snapshot taken before these facts were recorded is refused (take a new one). --output-format mydumper is refused. The web interface's Build a .sql backup for any moment on a PostgreSQL server builds these same files, and its download is one .tar.gz of them.
  • A change to the table's columns or primary key refuses, a type-only change included: the fold compares the shape the snapshot recorded with every shape the stream saw up to the point, and the key the changes were captured under with the one rows are joined by. Take a new snapshot with bintrail-pg baseline.
  • A TRUNCATE, a DROP or a rename refuses, including one that committed at the instant the snapshot opened: it writes no row changes, so the fold would bring the rows back.
  • A table taken out of the publication and added back refuses across that stretch: its changes while it was out were never captured. This is seen only for a FOR TABLE publication with track_commit_timestamp = on (see Schema changes on PostgreSQL); otherwise the fold cannot tell, and the chain's "every transaction exactly once" holds only for what capture received.
  • An UPDATE that changes the primary key is applied as a delete of the old key and an insert of the new one, each ordered against every other change of that key by the commit LSN (and, inside one transaction, by the order the changes were made in). This covers a key changed twice, changed and changed back, moved onto a key that was deleted, and two rows swapping keys through a third value, each change in its own transaction, and several rows moved by one statement when no row lands on a key that changes again in that statement's transaction. One such change still refuses the table, with the reason in the message, when it cannot be proven whole: the change has no commit LSN in the index, its new row holds a large value PostgreSQL did not send (the table was not at REPLICA IDENTITY FULL, and the value belongs to the row at the old key), a key column is missing from either row, or the key the index stores for it is not the one its old row spells. One more case refuses out of caution: the old row has NULL in every column outside the key and the new row has a NULL outside the key too. Such an old row looks the same as one captured with the key only, where a large value the UPDATE did not touch would have been stored as NULL. And one case refuses because the changes alone cannot say what happened: inside one transaction, a key receives a row (an INSERT, or a row whose key changed to it) and is changed again later in that transaction, with a key change among those changes. When a primary key is DEFERRABLE, two rows can hold the same key value inside a transaction, so UPDATE t SET id = id + 1 over rows 1, 2 and 3 is captured as "1 to 2, 2 to 3, 3 to 4", the same as one row moved three times, and the two leave different rows. This also refuses a row whose key is changed twice in one transaction, and a row inserted and re-keyed in the same transaction. The same statements in separate transactions are applied. A refused change refuses even when a later change overwrote it, and one in the minute before the snapshot, which the fold reads again (see above), counts too.
  • A transaction whose changes of one key do not describe one row refuses. A DEFERRABLE primary key is checked at the end of the statement or of the transaction, so until then two rows can hold the same key value, with no key change involved: a transaction inserts a new row 1 while the old row 1 is still there and then deletes the old one. The captured changes are an INSERT and a DELETE of key 1, the last one is the DELETE, and a copy that kept the last change would have no row 1 where the source has the new one. The fold therefore checks, for the changes one transaction makes to one key, that each starts from the row the one before it left: an INSERT from no row, an UPDATE or a DELETE from exactly the row the previous change wrote. When that does not hold, or the rows cannot be compared, the table is refused with a message that names the key and the transaction's commit LSN. The ordinary transactions are applied as before: a row inserted and deleted again, inserted and updated, updated twice, deleted and replaced under the same key. A transaction that capture delivered twice after a restart is recognized and applied once, when it looks exactly like one: the key's first change of the transaction comes back, and the changes after it repeat the changes before it, one by one, for as long as those ran (the first delivery may have been cut short; the last one is whole). A first change that comes back followed by anything else refuses. Two limits: when a snapshot update holds so many changed rows that it moves them to disk and a transaction delivered twice has its changes on both sides of that move, it is refused, not checked; and a transaction delivered twice that holds a key-changing UPDATE is refused by the rule of the previous point (the new key receives the row twice). What is not seen, in every case because the rows are equal in every column, the key included: (1) a row inserted as an exact copy of one that already existed, followed by a delete of the original, which reads like inserting a row and deleting it again; (2) the same row inserted twice and one of the two deleted, or any series of changes made identically to two identical rows, which reads like a transaction cut short and then delivered whole: the source keeps one row and the copy none. Both need a DEFERRABLE key; the index does not record whether a key is deferrable. (3) On a database whose encoding is SQL_ASCII, text that is not valid UTF-8 is stored with replacement characters, so two rows that differ only in such bytes compare equal.
  • An unchanged-TOAST marker is the place of a large value an UPDATE did not touch and PostgreSQL did not send. Under REPLICA IDENTITY FULL there is none to meet: capture replaces it with the real value from the old row of the same change, for an ordinary UPDATE and for one that changes the key. A change captured with the old row's key only (a table that is not at FULL) keeps the marker, and a fold gives the row the value of its newest earlier change, or of its row in the table's copy: in a snapshot update the table's previous file, in fresh mode the snapshot the statement reads (the table delta beside its file first, then the file), read by key for all such rows of a table at once and within the memory fresh mode may hold. What neither holds is refused by name (in fresh mode the table then reads as of its snapshot until the next one), as is a marker in any other place (beside a whole old row, in an INSERT, in the new row of a change of key); a value is never written as NULL in its stead.
  • A table with no change is carried forward under --carry-forward-unchanged: the new snapshot gets the previous file (a hard link where the filesystem allows one) and the table is not read or written again. Two cases still rewrite it. The first update after a full read rewrites every table once, because a full read's files do not record how far back their missing changes reach. And a table that has gone unchanged for long is rewritten before the index drops the changes its file starts from. Only the scheduled updates of the web console carry tables forward: bintrail baseline refresh on the command line does not know how long the index keeps its changes, and rewrites. A changed table keeps its file too, with small files of its changes beside it (<table>.000000.pgdead and .pgnew), unless BINTRAIL_PG_TABLE_DELTAS=0 (pg-table-deltas = off) turns that off. Each update adds one pair of those files, and the table is written in full again when it has 12 pairs (an hour of updates five minutes apart: every statement on the copy reads the file and all its pairs, so a long chain makes the reports slower), when the chain is a day old or its files pass half of the table file's, when its file grows too old for the index to reach back, or when the delta cannot be written. Everything that reads the copy reads them with the file; a views file that follows later snapshots (bintrail views without --pin-snapshot) refuses such a table and says to pin it. --table-deltas does not apply to a PostgreSQL server.
  • Capture that is behind is safe for the chain, not for one file. A snapshot folded while capture is behind holds the table as of the newest change the index has, under the requested point's name, and the run warns. The next fold applies what capture delivers later: the folded snapshot's time bound is set from the newest change the index holds, not from the point.
  • A transaction the stream has not checkpointed is left whole to the next fold, never applied in part.
  • One index database follows one PostgreSQL source: the fold reads where it stops from the whole index.

On this page