DBTrail

Connect a client

Run reports on the copy of a PostgreSQL server from psql, pgx, psycopg or JDBC, on port 5433, without touching your PostgreSQL.

Point psql, a driver or a BI tool at port 5433. Your reports then run on DBTrail's copy of your PostgreSQL, and your PostgreSQL does no work for them.

Copy and paste

Turn the port on

the DBTrail machine
./dbtrail port on

It prints the psql line and the port's password. The password is shown only once: keep it. If the port was already on, it prints the line again and says when the password was made.

Lost the password? This makes a new one, and the old one stops working:

./dbtrail port new-password

Connect

any machine with psql
psql "host=127.0.0.1 port=5433 dbname=shop user=app sslmode=require"

Put your server's name in place of shop. ./dbtrail server list shows it. The user name can be anything. Paste the password from step 1.

Run a report

the psql you just opened
SELECT status, count(*) AS n FROM orders GROUP BY status ORDER BY n DESC;
SHOW dbtrail.router_status;

The second line says who answered the first. last_answer says copy: your PostgreSQL ran nothing.

Connect your application

your application's settings

The usual connection strings work. Put the port's password in place of PASSWORD:

pgx (Go)       postgres://app:PASSWORD@127.0.0.1:5433/shop?sslmode=require
psycopg 3      host=127.0.0.1 port=5433 dbname=shop user=app password=PASSWORD sslmode=require
JDBC           jdbc:postgresql://127.0.0.1:5433/shop?sslmode=require   (user app)
  • From another machine: the port answers only on the DBTrail machine until you open it: From another machine.
  • Nothing answers? The port is on and I cannot connect.
  • Prefer a browser? In the web interface: Settings → PostgreSQL clients → Turn on, in the Connect a SQL client panel. It shows the same psql line and password.

In detail

The PostgreSQL port is new in 0.133.0. How current an answer is, and who answers it:

  • Near real time, in fresh mode. Fresh mode is the default: each statement lays the changes captured since the last snapshot over the copy. How it is measured, and what an answer can miss during an update: How current an answer is.
  • As old as the last snapshot update, in fast mode. The cheaper answer, chosen per server on its Snapshots page or with dbtrail port mode <server> fast. A connection already open when a server goes from fresh to fast gets a notice with its next answer: from then on it reads older data than it just did.
  • The port serves a server only when it can forward. From 0.140.0 a server the port cannot send to your PostgreSQL is refused at login, with an error that names it, says why, and gives both ways to serve it. Either way serves it: the yes about the user DBTrail already connects with, or a forwarding user of its own (Let the port ask your PostgreSQL).
  • When it forwards, your PostgreSQL answers what is cheap there. From 0.138.0 the copy gets a statement only when its plan on your PostgreSQL is expensive. A read by a primary key stays on your PostgreSQL, where it takes about a millisecond (Who answers a statement).
  • Reports stay on the copy with a forwarding user. Since 0.136.0 the copy answers, as PostgreSQL does, round(a / b, k), round(avg(x), k) and floor(a / b) over numerics (50.00, not 50.0), ntile, and names a query gives its own numbers (SUM(x) AS units, a column of a WITH) compared or sorted. Of 14 TPC-C reporting queries, 11 go to the copy. Of the other 3, two are cheap on your PostgreSQL and stay there, and one sorts text, which the copy orders differently, and goes there too.

1. Turn the port on

In the web interface: Settings → PostgreSQL clients → Turn on, in the Connect a SQL client panel. The panel shows the psql line to copy. Or in a terminal:

dbtrail port on

Either way you get a password, shown only then. It is the same switch and the same password for every SQL port of this DBTrail.

2. Connect

The database name picks the server: its id or its name, as the web interface shows them. The username can be anything.

psql "host=127.0.0.1 port=5433 dbname=shop user=app sslmode=require"
SELECT status, count(*) AS n FROM orders GROUP BY status ORDER BY n DESC;
You should see

The answer, from the copy. Your PostgreSQL ran nothing.

From an application, the usual connection strings work:

pgx (Go)       postgres://app:PASSWORD@127.0.0.1:5433/shop?sslmode=require
psycopg 3      host=127.0.0.1 port=5433 dbname=shop user=app password=PASSWORD sslmode=require
JDBC           jdbc:postgresql://127.0.0.1:5433/shop?sslmode=require   (user app)

An application login opens its own server

A server's application login opens that server only. From 0.139.1, a connection with such a login that names another server of this DBTrail as its database, by id or by name, is refused with error 28000. The error names the server the login belongs to:

user "app" is the application login of server "prod" and opens that server only, and database "staging" is another server; connect with dbname=prod, or use the other server's login

Before, that connection silently read the login's own server. These still connect to the login's own server: its id or name, and any name that is no server at all (a database name passed on by a proxy, postgres, no database). A connection without an application login is not changed: there the database name picks the server.

What to know about the port

PostgreSQL clients connect to the PostgreSQL port, 5433. The Compose stack serves it from the pg-router service and the Linux packages from bintrail-pg-router; both start with DBTrail and listen only while the port for SQL clients is turned on (Settings, Connect a SQL client, or dbtrail port on).

psql "host=127.0.0.1 port=5433 dbname=<server id or name> user=app sslmode=require"
  • The database name picks the server, and the user can be anything. The password is the one created when the port was turned on, or the access token.
  • Who answers depends on whether the server forwards. From 0.138.0 server add asks one question: may the port send to your PostgreSQL, read-only, a SELECT whose every table is a table of the copy, as the user DBTrail already connects with? Enter is yes, and no second account is needed. Without a yes, the copy answers every statement and nothing is sent to your PostgreSQL. dbtrail server forward-user <server> --capture-account says yes later. A forwarding user of its own (dbtrail server forward-user <server> -u <user> -p) is still there for a port that should read more: Let the port ask your PostgreSQL. Either way, a statement that is not a read is refused.
  • When the server forwards, your PostgreSQL answers what is cheap there, a read by a primary key for one, and the copy answers what is expensive. SHOW dbtrail.router_status on the same connection says which side answered the last statement and why: Who answers a statement.
  • With the user DBTrail connects with, the port's connections to your PostgreSQL are sessions of that role: they count toward its CONNECTION LIMIT and toward max_connections, 16 at most. Leave the role without a limit, or with room for them and for capture.
  • One row as it was at a past moment, on the same connection: SELECT * FROM _flashback.orders AS OF '5 minutes ago' WHERE id = 42. A server that hides rows or columns refuses it, and says why.
  • The login is SCRAM-SHA-256, so the password never crosses the network. A client that asks for TLS gets it; the port does not turn away one that asks for none, so keep sslmode=require in the client.
  • The port answers on this machine only unless you publish it elsewhere. If 5433 is taken, the installer leaves it unpublished and says so.

With the Linux packages, a service of its own, bintrail-pg-router, answers the port on 5433. The package starts it. On a machine that runs a second PostgreSQL cluster, 5433 is often that cluster's: set BINTRAIL_PG_ROUTER_LISTEN in the settings file.

How current an answer is

Since 0.134.0 the PostgreSQL port answers in fresh mode unless you choose fast mode for a server: How current an answer is.

Limits

Small on purpose, so the port fits beside capture.

ReadsonlyA statement that is not a read is refused with an error. Nothing here can change your data.
10,000rowsThe default cap on the copy. A bigger result is refused, not cut, or answered by your PostgreSQL when the server forwards and the plan shows it. A very long cell is refused the same way.
5 minper statementThe port stops a statement that runs longer. A shorter statement_timeout is applied too.
4at onceStatements that run on the copy at once, with 2 threads each. The rest wait their turn. 256 clients can connect.

When the port is full. A connection holds one of the port's places from the moment it is accepted, and has 30 seconds to log in. From 0.139.1, when every place is taken and a new connection arrives, the port closes the connection that has held its place longest without sending its proof of the password, if that is more than 1 second, and gives its place to the newcomer. A client that was only slow connects again. A client that has logged in is never closed to make room: with every place a client's, the newcomer gets error 53300. This protects the port from connections that sit idle. It does not protect it from a client that connects again as fast as it is closed, and it is not a limit per address, so keep the port behind a firewall or a proxy.

Results waiting for a client to read them are held in memory, up to 256 MB for all clients together. Past that, the next statement is refused with error 53000; run it again once the others have read their answers. A client that reads nothing of a result for 60 seconds is disconnected.

On this page