DBTrail

How current an answer is

Fresh mode on the PostgreSQL port lays the latest captured changes over the copy, so answers are near real time. What that means, what it costs, and what can make an answer older.

Out of the box, answers on the PostgreSQL port are about a second behind your PostgreSQL (1.2 seconds at the median, measured). This is fresh mode. Fast mode is cheaper, and answers as of the copy's last update instead. You choose one per server.

Copy and paste

./dbtrail port mode shop          # which mode the server has
./dbtrail port mode shop fast     # from the last update of the copy: cheaper, minutes behind
./dbtrail port mode shop fresh    # back to the default

Put your server's name in place of shop. The port picks the change up within a few seconds, with no restart. The first command says what is saved.

In fast mode, how old an answer can be depends on how often the copy is updated:

./dbtrail snapshot schedule shop --every 30m

Prefer a browser? The mode is on the server's Snapshots page.

In detail

The PostgreSQL port answers near real time: in fresh mode, the default, each statement reads the copy with the changes captured since its last update laid over it. This page says how far behind an answer can be, what it costs, and what can make an answer older than you expect. The SQL page of the web interface and the AI connection answer as of the copy's last update.

Fresh mode

Since 0.134.0 the PostgreSQL port answers in fresh mode unless you choose otherwise: the changes captured since the last snapshot update are laid over the copy, so a row written a moment ago shows without waiting for the next update.

1.2seconds behindAt the median, and never more than 3.2, measured with tables of 2 and 10 million rows while 400 transactions a second were written.
2 to 3times the costOf the same statement in fast mode. The size of the table counts for more than the mode.

dbtrail port mode <server> fast answers from the last snapshot update alone (minutes behind, and cheaper); fresh goes back.

What to know:

  • A statement answers fresh only when every table it reads does. When one cannot, the statement is answered as of the last update for all its tables, and the client is told by a NOTICE.
  • During an update of the recent changes, a statement does not wait, and may answer from the version before that update, with no notice. That is usually seconds behind, and a minute or more in a long update. On PostgreSQL there is no switch to make it wait. What does protect is partial: a connection your PostgreSQL already answered about a table waits for the copy to reach that answer, or goes to your PostgreSQL. That does not cover an application that writes on one connection and reads on another. A table with no version yet (the first statement on it, or the first after a new snapshot) does wait for the running update, for at most half of the statement's time limit (30 of 60 seconds by default). While the index holds no captured change at all, the first change can be missing from statements that run during the update that brings it.
  • Use 0.136.0 or newer. Before it, three things made answers older than they should be, or stopped the copy:
    • a table nothing writes to (a lookup table) stopped the copy from being updated at all, and every statement that joined it was answered as of the last update;
    • after every snapshot update a table's recent changes were gathered again from nothing, and until they were in, answers came from the new snapshot alone: a few seconds older than the one before on a small database, minutes on a large one;
    • updating the copy got slower the longer DBTrail had been running.
  • Right after a full read of the database (the first copy, or one taken again), a table's recent changes are not laid over it until DBTrail has caught up with the moment it was read: seconds, normally. Until then it is answered as of that read.
  • Reading one row by its key reads the whole table: the copy has no index. When the server forwards, such a statement is answered by your PostgreSQL.
  • Switching a server from fresh to fast makes the connections already open read from the last snapshot update, which can be older than their last answer. Each is told once, by a NOTICE.
  • Out of the box the port runs 4 statements at once and takes 256 clients. PG_ROUTER_WORKERS and PG_ROUTER_MAX_CONNECTIONS in .env change that; each statement can take up to 2 GB.
  • A short statement does not wait behind a report. From 0.141.1 the copy has one worker more than PG_ROUTER_WORKERS, kept for statements whose plan reads one table and joins nothing (a sum over one warehouse, a count with a filter). Reports run on PG_ROUTER_WORKERS at most, as before. Many short statements arriving together still wait for each other.
  • The SQL page and the AI connection are not in fresh mode yet.

What clients it takes (prepared statements, typed columns, the catalog queries of psql 13 to 18, JDBC, SQLAlchemy and Grafana), certificates clients can verify, and every option: Connect a client.

What sets how far behind the copy is

  • A very large transaction. PostgreSQL sends a transaction only once it has committed, whole, and the changes committed after it wait behind it. One that changed millions of rows can leave the copy minutes behind, however briefly it was open. Committing bulk changes in batches keeps the copy close.
  • A server with no changes stays updatable: DBTrail follows the moment capture was last caught up with the source, so an idle server is not read in full again.

On this page