Let the port ask your PostgreSQL
How the PostgreSQL port sends reads to your PostgreSQL, read-only, as the user DBTrail already connects with or as a forwarding user of its own, and what it checks first.
The PostgreSQL port answers what it can from the copy. The rest, and what is cheap on your PostgreSQL, it sends to your PostgreSQL, read-only. This is called forwarding. A server must allow it, or the port does not serve that server.
Copy and paste
The common case. It lets the port read from your PostgreSQL as the login DBTrail already uses. Nothing to create on your PostgreSQL:
./dbtrail server forward-user shop --capture-accountPut your server's name in place of shop. The next connection to the port is
served, with no restart. To take it back:
./dbtrail server forward-user shop --no-capture-accountserver add --capture-account, as in the Quickstart,
already did this.
A login of its own, to read more
Use this when the port should read more than the copy's tables. It makes a login that can only read, then gives it to DBTrail:
export FWD_PW=$(openssl rand -hex 16)
psql -v pw="$FWD_PW" <<'SQL'
CREATE ROLE app_ro LOGIN PASSWORD :'pw';
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_ro;
SQL
echo "$FWD_PW" | ./dbtrail server forward-user shop -u app_ro --password-stdinRun it all on the DBTrail machine, in the terminal of
Prepare, step 1, where psql reaches your
PostgreSQL as a superuser. Tables in another schema: put its name in place of public
and also run GRANT USAGE ON SCHEMA <schema> TO app_ro;. DBTrail does not
try the login when it saves it. To check it,
open a block through the port once the first copy is ready:
psql "host=127.0.0.1 port=5433 dbname=shop user=app sslmode=require" -c "BEGIN" -c "SELECT 1" -c "COMMIT"To stop using it: ./dbtrail server forward-user shop --remove.
Prefer a browser? Both are on the server's form (Servers, Edit): the box Let the PostgreSQL port send to this PostgreSQL, read-only and as the source user above, a SELECT the copy does not answer, and the fields behind Advanced: use a separate forwarding user.
In detail
The PostgreSQL port answers what it can from the copy. What the copy does not answer as PostgreSQL does, and what is cheap on your PostgreSQL, it sends to your PostgreSQL, read-only. This page says the two ways to let it do that, what changes for your PostgreSQL, and every check a statement passes before anything of it is sent.
Two ways to forward
The port sends to your PostgreSQL, read-only, what the copy does not answer as PostgreSQL does. There are two ways, and the first needs nothing created on your PostgreSQL. From 0.140.0 a server with neither is not served: its clients get this at login, and its capture goes on.
FATAL: the PostgreSQL port does not serve server "shop": it has no forwarding user, and forwarding as the account DBTrail captures with is turned off for it. ...The rest of the message gives both ways out. The server's Overview shows the same words on a card, and the daemon says them once at start.
With the user DBTrail already connects with
New in 0.138.0. dbtrail server add --flavor postgres asks one question once
the server is added: 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, when the copy does not answer it? Enter is yes. A
script that says nothing gets no.
When that user is a superuser, or can bypass row-level security, the checks
warn, and so does saving the yes. What the port sends as that user is still
a read-only SELECT whose every table is a plain table of the copy, and a
table under row-level security is never sent; the warning gives the
statements for a role that can only replicate and read the published
tables, to point the server's source at. A forwarding user of its own is not
held to that check: it reads whatever its own grants allow.
For a server that is already added, or to change the answer:
dbtrail server forward-user shop --capture-account
dbtrail server forward-user shop --no-capture-account # takes it backIn the web interface, from 0.139.1, it is the box on the server's form (Servers, Edit, or the form a PostgreSQL is added with): Let the PostgreSQL port send to this PostgreSQL, read-only and as the source user above, a SELECT the copy does not answer. It saves the same answer as the two commands. On a new server it is ticked. A session that may not change servers sees the box and cannot change it. The SQL clients page says, for the server picked in the sidebar, what the port sends to its PostgreSQL.
-
Taking the yes back closes the connections that forward as this user. Without a forwarding user of its own the server is then not served.
-
Saying yes serves the server at the next connection, with no restart.
-
With a forwarding user of its own, the box changes nothing. The port keeps using that user and no connection is closed.
-
A server that hides tables or columns sends nothing to your PostgreSQL, ticked or not, and the form says so.
-
A server saved before 0.140.0 with no answer is not served after the upgrade until it gets one: run
dbtrail server forward-user <server> --capture-account, or tick the box. Its capture keeps running. -
A saved forwarding user that is the user DBTrail connects with is used as that user once the server says yes, instead of being refused.
-
Without a terminal nothing is asked, and the answer is no. That is a script, or the Docker
dbtrailcommand when it sends an option file on standard input. The command says so and how to turn it on, and until then the port does not serve the server. To say yes there, add--capture-account, or this to the option file:[dbtrail-forward] capture-account = yes--no-capture-account, orcapture-account = no, says no. -
Editing the server so that it points at another user or another host takes the yes back. A new password keeps it.
That user can read more than the copy holds, so the port checks every statement before anything of it is sent. It goes to your PostgreSQL only when all of this holds:
- it is one
SELECT(orWITH ... SELECT) whose tables are all named in itsFROMclauses, and it calls only built-in functions known to read no table; - the session's
search_pathdoes not namepg_catalogorpg_temp; - your PostgreSQL confirms, on the same connection and right before, that each name is a plain table (not a view, a partitioned or inherited table, or one with row-level security) and that no operator, cast or function of yours could run in it;
- every one of those tables is a table of the copy, by schema and name.
Anything else is refused with error 0A000 and the reason, and nothing of it
is sent.
These connections count against that user's limits. The port logs in to
your PostgreSQL as the same role capture uses. Each connection is a session
of that role, so it counts toward the role's CONNECTION LIMIT and toward
your PostgreSQL's max_connections. The port holds at most 16 of them at
once, across every server (pg-router-max-source-connections). A role whose
CONNECTION LIMIT is lower than that plus what capture itself holds can
leave capture unable to log in again after a restart or a dropped connection.
Leave the role without a limit, which is the default, or give it one with
room for both. A forwarding user of its own keeps the two apart.
With a forwarding user of its own
Still available, for a port that should read more than the copy's tables, or
run what the check above leaves out on purpose: a SAVEPOINT, a cursor and
an EXPLAIN inside a transaction block, and a statement sent in the middle
of an extended batch the port has already sent something of.
CREATE ROLE app_ro LOGIN PASSWORD <a password>;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_ro;dbtrail server forward-user shop -u app_ro -pWhen adding the server, --forward-user app_ro does the same, or user and
password under [dbtrail-forward] in the option file; the question above
is then not asked. --no-forward forwards nothing and asks nothing, so the
port does not serve the server until it can forward. In the
web interface the two fields are behind Advanced: use a separate forwarding
user on the server's form, and stay in sight whenever the server has one
saved.
- It must be another user than the one DBTrail captures with.
- When a server has one, it is used instead of the capture user, whatever was answered above.
- Changing or removing it closes the connections that use it. Their clients reconnect.
- Removing it works differently in the two places. In the web interface
it also takes back the yes above, so the port stops forwarding for the
server; to forward as the source user, tick the box once the removal is
saved. On a terminal,
dbtrail server forward-user <server> --removeremoves the user alone: with a yes saved, the source user takes its place, and the command says so. - No login is tried when it is saved. A wrong password, or a role that cannot
log in, shows with the first statement the port sends. To check it, run
BEGIN; SELECT 1; COMMIT;through the port once the first copy is ready. - Give it your application's grants and no more: whoever has the port's password can read what it can.
Either way the connection to your PostgreSQL is read-only, and a statement that is not a read is refused before it is sent. A server that hides tables or columns sends nothing to its source.
Every check in full
A transaction block a client opens on the PostgreSQL port (BEGIN or
START TRANSACTION to COMMIT or ROLLBACK) runs on the server's own
PostgreSQL, every statement in it, and so does a statement on the system
catalog that the port does not answer from the copy
(listing tables), and
every statement outside a block that the copy is not known to answer as
PostgreSQL does
(which side answers a statement). It needs the
server's forwarding user, a role of its own on the source:
CREATE ROLE app_ro LOGIN PASSWORD <a password>;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_ro;server add takes it when the server is added: it asks for it on a terminal,
or reads --forward-user app_ro, or [dbtrail-forward] in the option file.
To set or change it on a server that is already added:
bintrail-console server forward-user <server> -u app_ro -por in the web interface, edit the server (Advanced: use a separate forwarding
user, then Forwarding user and password). It is
laid over the source's own address, database and parameters (sslmode
included), and must be another user than the one DBTrail captures with: the
port never runs anything as that one, which holds REPLICATION and reads every
published table, unless the server says yes to that
(above). A server with neither is
not served. Changing the forwarding user, or removing the
server, closes the connections that use it; clients reconnect.
- Read-only, three ways. A statement that is not a read is refused before
it is sent (
25006, namingINSERT,UPDATE,CREATE...). The connection to the source is opened withdefault_transaction_read_only = on, so PostgreSQL itself refuses what the text does not show (a function that writes, aWITHthat deletes,SELECT ... INTO,nextval). The port watches that setting, which PostgreSQL 14 and later report, and closes the connection if anything turns it off. A source older than 14 is not forwarded to.BEGIN READ WRITEandSET default_transaction_read_only = offare refused. - The text is read as PostgreSQL reads it, and what cannot be read is not
sent. Before a statement goes to the source the port cuts it into words the
way PostgreSQL's own scanner does: a
--comment ends at a line feed or a carriage return, block comments nest, anE'...'string and a dollar quote start only where PostgreSQL starts one, and a string written in two pieces a line apart is one string. A text it cannot cut with certainty (a quote or a comment left open, a character that is not SQL outside a string) is refused with0A000and nothing of it is sent. BEGIN,COMMIT,ROLLBACKandSET TRANSACTIONare sent in the port's own words, written from what it read of yours (BEGIN READ ONLY, with the isolation level you named), never as your text. After itsBEGINthe port asks the source whether the block is read-only, in the same round trip, and uses the block only when the answer is yes; otherwise the connection to the source is closed and the statement answers08006.- Refused by name:
COPY(0A000; read rows withSELECT),LISTEN(a notification would reach the client only when it next sent a statement),NOTIFYandpg_notify(),DO,PREPARE/EXECUTE, advisory locks,set_config(),dblink, and names written withU&"..."escapes. A transaction statement inside a longer text (SELECT 1; COMMIT) is refused: send it on its own. - Refused although it begins as a read (
25006):SELECT ... INTO, aSELECTthat locks its rows (FOR UPDATE,FOR SHARE), aWITHthat holds anINSERT,UPDATE,DELETEorMERGE, andEXPLAINof anything but a query. PostgreSQL refuses each of them in a read-only transaction too; the port says so first, with the reason. In aWITH, anEXPLAINor aDECLARE, a column or alias namedinsert,update,deleteormergehas to be written in double quotes. - Functions that act beyond a read are refused by family (
0A000), schema-qualified or quoted alike: stopping or cancelling a session (pg_terminate_backend,pg_cancel_backend), the server's files (pg_read_file,pg_ls_*,pg_stat_file), large objects (lo_*), WAL, backups, replication slots and origins,pg_logical_emit_message,pg_reload_conf, the statistics resets (pg_stat_reset*),nextvalandsetval,txid_currentandpg_current_xact_id, andts_rewrite.query_to_xmlandts_stat, which run a text as a statement, are sent only when that text is a plain string written in the statement and is itself a read. Most of these an ordinary role cannot run anyway; the ones it can (pg_logical_emit_message,txid_current, cancelling its own sessions) are not stopped by a read-only transaction, which is why the port refuses them. - The source's answers as it gives them: its rows as they come, never held
whole in this process; its errors with their code, detail and position, which
fail the block as on PostgreSQL; its notices (
RAISE NOTICE). Savepoints (ROLLBACK TO SAVEPOINTbrings a failed block back, and the session's settings to what they were at the savepoint, as psql'sON_ERROR_ROLLBACKand JDBC'sautosaveexpect; a savepoint's name is told apart as PostgreSQL tells it, so"A"in quotes is nota), cursors and the extended protocol work as on the source: a statement a driver prepared outside the block (pgx, JDBC) is prepared on the source the first time it runs inside one. When the copy had described its columns with another type than the source sends in binary, that execution is refused withcached plan must not change result type(0A000), the words drivers prepare again on, never sent as the wrong value. - The session goes with it:
TimeZone,search_path,statement_timeout,lock_timeout,extra_float_digits,client_min_messagesand the default isolation are set on the source before a statement is sent, and the formats the port writes in (DateStyleISO, MDY,IntervalStyle,bytea_output, UTF8) are the source connection's from the start. - Cancel and disconnect reach the source. A cancel request stops the
statement there (
57014); a client that leaves in the middle of a statement has it cancelled there. A source that does not stop a cancelled statement within ten seconds has its connection closed. - At most one connection to the source per client, opened when a
statement needs it and given back as soon as nothing of the client's lives
there (the block ended, the batch synced, no portal open), so a connection
pool's clients do not hold one each for good; at most
--max-source-connectionsacross every server. The next one is given the session's settings again, and a prepared statement is prepared there again the first time it runs; a cursorWITH HOLD, which would outlive the block, is refused. A connection that is lost, or whose read-only guard was turned off, is not replaced behind the client's back: a statement that needs the source answers08006 ... reconnect(a block open at that moment fails, and only itsROLLBACKis answered), while reads on the copy go on, except one the copy would answer with data older than what the source already answered that connection (see below), which gets the same08006. - Every wait for the source is bounded by the port's five-minute cap on a
statement, besides the client's
statement_timeoutand cancel. The wait for a connection there, when the port holds as many as it opens, is bounded by 30 seconds, thestatement_timeoutand a cancel request; one deadline covers every wait of a statement. - When the source cannot be reached:
08001 cannot connect to the source; when it refuses the forwarding user,08004with PostgreSQL's reason (not28P01, which would read as the client's own password being wrong). TheBEGINfails and the connection stays open on the copy. - TLS to the source follows the forwarding user's URL: the source's own
sslmodewhen the user was laid over it,preferwhen none is written. REPEATABLE READandSERIALIZABLEare still refused, as on the copy: inside a block the port still answers some statements itself (SHOW, the catalog statements it recognizes, time travel).