How does Postgres replication work?
Learn more
ALTER TABLE
.
wal_level
parameter to logical in the postgresql.conf file on the publisher server. This ensures that the WAL contains enough information for logical decoding.
INSERT
| UPDATE
| DELETE
)CREATE TABLE
| ALTER TABLE
| etc.)INSERT
| UPDATE
| DELETE
operations on tablesTRUNCATE
operations (configurable)CREATE TABLE
| ALTER TABLE
)REPLICA IDENTITY FULL
(within an ALTER TABLE
) which will treat the entire row as
the primary key.
Learn more
ALTER TABLE
), a trigger (SQL function) can be called. Within this function one can extract the name of the table being modified. Unfortunately, the exact schema change is not available, but it is possible to query the current schema for that table. If setting up a custom replica, it may then be possible to compare the new schema to the old schema to determine the changes that occurred.
While it is also possible to detect new index creation, or altered indexes via triggers, it becomes more complicated to automatically make the changes on the replica. Typically the index must be dropped and recreated. Recreating an index may require resorting the entire table (e.g., for a primary index).
Manual synchronization is another method for synchronizing schema changes. It requires the changes to be manually applied to both the publisher and subscriber. When doing so, careful planning is required to coordinate schema changes to ensure consistency across all instances. E.g., when manually handling schema changes:
ALTER TABLE
: Apply changes manually to both publisher and subscriber.CREATE TABLE
: Create the table on the subscriber before setting up replication for it.UPDATE
| INSERT
| BEGIN
| COMMIT
message).Learn more
Learn more
BEGIN
message and ends with a COMMIT
message. The Postgres configuration parameter: logical_decoding_work_mem
, controls how much data is buffered before switching to streaming mode (if streaming mode is enabled).
STREAM START
, STREAM STOP
, STREAM COMMIT
, and STREAM ABORT
messages. Since operations are streamed prior to the commit or abort, streaming mode must also support sub-transactions (generated by SAVEPOINTs
), as well as interleaved transactions. Messages between a STREAM START
and STREAM STOP
belong to the same parent transaction. The STREAM START
will indicate the Postgres Transaction ID or XID of the parent transaction. After a STREAM STOP
message, either a STREAM START
, STREAM COMMIT
, STREAM ABORT
or full transaction will follow.
To support sub-transactions in streaming mode, the normal replication messages are modified to contain an XID to indicate the (sub) transaction to which they belong. In streaming mode, a sub-transaction is indicated through a different XID than that of the parent. The parent (or top-level) transaction ID is indicated by the STREAM START
operation. If a sub-transaction is aborted, then an explicit STREAM ABORT
message will be issued containing both the parent and sub-transaction XID.
It is possible to have nested sub-transactions by nesting SAVEPOINT
commands. Replication messages will always contain the XID of the latest active sub-transaction. It is possible to abort multiple sub-transactions at once (e.g., rolling back to an early SAVEPOINT
), in this case, multiple STREAM ABORT
messages will be generated, one for each sub-transaction that is aborted. As well, when a SAVEPOINT
is released, messages will contain the XID of the latest previous SAVEPOINT
or the parent transaction (if nesting is fully unrolled).
The STREAM COMMIT
, unlike the abort, only specifies the parent XID; it implicitly commits all non-aborted sub-transactions. Both STREAM COMMIT
and STREAM ABORT
messages are sent after a STREAM STOP
.
Learn more
pg_replication_slots
table in Postgres. The column confirmed_flush_lsn
holds the last flushed LSN that Postgres has received from the replica.