DBTrail

Schema changes

How DBTrail sees an ALTER TABLE, a new table or a TRUNCATE on a PostgreSQL server, and what it does with each.

When you add a table, change a table's columns, or empty one, DBTrail notices and records it. Three things are yours to do, below.

Copy and paste

You created a new table

Set it to send the whole row, let DBTrail read it, then add it to the publication, in this order. Put your table in place of public.items:

psql -c "ALTER TABLE public.items REPLICA IDENTITY FULL" \
     -c "GRANT SELECT ON public.items TO dbtrail" \
     -c "ALTER PUBLICATION bintrail_pub ADD TABLE public.items"

Run it right after the CREATE TABLE, before the table's first rows. In the other order, its changes in between are not captured. The copy has no file for the table yet, so the schedule takes one full read to bring it in.

You changed a table's columns

Updates of the copy refuse that table until it is read again in full. The schedule does that by itself where it may. To do it now:

./dbtrail snapshot run shop

You want the exact time of each change

Out of the box DBTrail knows a change happened between two moments. With commit times kept by PostgreSQL, it often knows the exact one:

psql -c "ALTER SYSTEM SET track_commit_timestamp = on"
sudo systemctl restart postgresql

It takes a restart. On managed PostgreSQL, check that your provider lets you set it.

In detail

PostgreSQL's change stream carries row changes, not the statements that changed a table's shape. This page says how DBTrail learns that a table changed, what it records, and what it refuses until a new snapshot is taken.

How a change is seen

PostgreSQL's change stream carries row changes, not schema statements: no CREATE TABLE, ALTER TABLE or DROP TABLE ever reaches bintrail-pg as text. What the stream does carry is a description of each table (its name and its columns with their types) sent before the first change made under a new shape. bintrail-pg compares each description with the last one it saw for the same table, identified by the table's internal number (its OID, which survives a rename), and records what changed in schema_changes:

What the stream showsRecorded as
Same table, new name or new schemaRENAME TABLE (one row under each name)
Same table, other columnsALTER TABLE, listing the columns added, dropped and retyped (a renamed column reads as one dropped plus one added, and the text says so)
A new table under a name another table held, the old one goneDROP TABLE then CREATE TABLE
A new table under a name another table held, the old one still there under another nameRENAME TABLE for the old one, then CREATE TABLE
A table that was not in the publication when capture startedCREATE TABLE (labelled when it is a partition), or ALTER PUBLICATION when the catalog shows it was added to the publication after it existed
A table taken out of the publication and added backALTER PUBLICATION, marked gap=uncaptured: its changes while it was out were never captured, and reconstruct and the snapshot-pair verify refuse a window across it
A TRUNCATETRUNCATE TABLE

Nothing is installed in your database for this; it is read from the stream alone.

A change no write followed is not in the stream. Until the next change made to the table, nothing of an ALTER TABLE reaches bintrail-pg, and the copy keeps the columns of the table's snapshot until its next snapshot. The PostgreSQL port does not wait for either: before the copy answers a statement, it compares the columns of the tables the statement reads with the source's catalog, and sends the statement to the source when they differ (see Which side answers a statement).

Which rows belong to which shape is exact. PostgreSQL decodes every change with the table as it was when that change was made, so a row is never assigned to the wrong set of columns.

When the change happened is a range, not an instant. The stream says the change happened after the last change made under the old shape and at or before the first one under the new shape, nothing finer. Each inferred row starts with a marker carrying that range, for example:

/* dbtrail: inferred from relation shape; oid=16411; instant=at-or-before; low=0/16B3748@2026-10-17T10:00:00Z; high=0/16B9A20@2026-10-17T10:20:00Z */ ALTER TABLE shop.orders: added note (text)

The exact instant, when the server keeps commit times. With track_commit_timestamp = on (a server setting that needs a restart; on managed PostgreSQL, check that your provider lets you set it; bintrail-pg doctor says whether it is on), bintrail-pg reads from the catalog when the statement that gave the table the shape the stream shows committed, and the marker says instant=exact; at=<time>, the range still beside it. When several changes fall in one range (a column added, then retyped), that is the last of them. It does so only where the catalog can prove it:

  • an added column, or a retyped one whose change rewrote the table, when the column's catalog row and the table's still agree and the column has no column-level grant (a later statement on that column, such as SET STATISTICS or SET NOT NULL, or on the table, such as a GRANT, moves one of them; a retype that does not rewrite the table, such as varchar(10) to varchar(20), is never exact);
  • a dropped column, whose catalog row is never written again;
  • a rename or a schema move, from the table's row type, which a later GRANT or column change does not move;
  • a new table, from its row type, when the table's earliest column carries the same time.

Not caught, and read as the change's time: an ALTER ... OWNER TO later in the range for a rename, and one later statement that rewrites both a changed column and the table (a multi-part ALTER). The catalog must also show the table with the columns the stream does (when bintrail-pg is catching up past a later change, it does not), and the time must lie inside the range. Anything else stays "at or before". A renamed column is never exact. Reading the times uses a connection of its own and never stops capture: if the catalog cannot answer, bintrail-pg logs a warning (at most every ten minutes, with how many reads failed) and dates the rows by range.

The row's detected_at is the exact instant when there is one; otherwise the start of the range (the end, when the range has no start). Its stream position is the end. reconstruct places an inferred row by its range either way. reconstruct refuses a point in time whose window overlaps the range of an inferred DROP TABLE or RENAME TABLE, since the table may or may not have changed yet at that point; the refusal says so and says which points in time can still be rebuilt. A TRUNCATE TABLE is reported by PostgreSQL with its exact transaction, so it has no range and refuses exactly where it happened. A table that was not written to for a while after the change has a wide range.

Tables added to the publication. A table added to a FOR TABLE publication has a row in the catalog written when it was added, and with track_commit_timestamp = on bintrail-pg reads its time. A table first seen after capture started whose catalog row is later than the table itself is recorded as added (ALTER PUBLICATION), not created; its changes from before are outside what capture covers, like the changes before capture started. A known table seen again after it was added back is recorded with the gap, so a rebuild that would need the changes made while it was out refuses instead of missing them. A table that no change has shown yet is caught too when it was in the publication when capture started and its row is later than that. When the catalog shows an add later than the change being read (bintrail-pg is behind), the stretch is refused as well, since the table may also have been out before. Changing a table's row filter or column list (PostgreSQL 15+) rewrites the same row and reads the same way; the row says so. Not seen: a table added back while track_commit_timestamp is off, or while the catalog cannot be read (bintrail-pg warns); a table no change had shown, taken out and added back while bintrail-pg was stopped; a table taken out again and not yet added back; tables covered by a FOR ALL TABLES or FOR TABLES IN SCHEMA publication or through a published parent (they are not added one by one); and a server clock stepped back across the add, since the add is placed by time. The live-source verify compares with the source as it is now and does not read these rows: a gap shows there as a difference.

What cannot be seen. Anything that changes neither a column's name or type nor the table's name or schema: indexes, constraints, defaults, NOT NULL, ownership, grants, comments, SET LOGGED / UNLOGGED. A DROP TABLE that is not followed by a new table of the same name leaves no row: the table simply stops appearing. A table created while bintrail-pg was stopped is taken as already existing when capture starts again, so it gets no CREATE TABLE row. On the first restart after upgrading to a version that records table identities, a table whose stored shape predates that is matched by name: if its columns differ, the change is recorded as an ALTER TABLE whose text says it could also have been a drop and a create, and reconstruct does not refuse on it the way it does on a DROP TABLE. The statement text and the role that ran it are never available.

If detection cannot start. What detection needs is read when capture starts. If that read fails (for example a very slow index database), bintrail-pg captures rows as usual and does not detect schema changes until its next restart, and logs that at ERROR level. The table shapes seen during that run are stored without their position in the stream, so the next start compares against the last shapes detection did see, and a change made in between is still found then (its range covers the whole run).

After a restart. PostgreSQL resends changes from the point it last confirmed, which can be earlier than what bintrail-pg had already recorded. Each table description is compared with the shape that table had just before it in the stream, so a resent stretch is read exactly as it was the first time, and a change already recorded is not recorded again.

Changes that look like DDL but are not. Narrowing a publication's column list (ALTER PUBLICATION ... SET TABLE t (a, b), PostgreSQL 15+), or on PostgreSQL 18 turning publish_generated_columns on or off, changes the columns the stream carries without changing the table. It is recorded as an ALTER TABLE.

Where names come from during catch-up. When bintrail-pg is behind and a name moved to a new table, whether the old table was dropped or renamed (and to what) is read from the database as it is now, which can be later than the point being processed. The row says so.

On this page