Sub topic 3
Query conflicts, max_standby_streaming_delay and hot_standby_feedback
This chapter assumes you have a working primary and standby, can edit postgresql.conf, and can run psql. You should also know what a commit is and what WAL (write-ahead log, the change journal PostgreSQL replays) does.
Query conflicts on a hot standby
A hot standby applies WAL changes while it runs read-only queries. A replayed change can remove rows that a running query still needs. PostgreSQL then cancels that query with a conflict error.
Two settings control how long the standby waits before it cancels:
max_standby_streaming_delay: the time replay may wait for a conflicting query. The default is30s. Set it to-1to wait forever, which stops conflicts but lets the standby fall behind.hot_standby_feedback: whenon, the standby reports its oldest open transaction to the primary, so the primary's VACUUM keeps the rows the standby needs. The cost is table bloat on the primary.
# postgresql.conf on the standby
max_standby_streaming_delay = 60s
hot_standby_feedback = onSynchronous replication
By default, a commit returns before any standby has the WAL. Synchronous replication makes the commit wait for a named standby.
# postgresql.conf on the primary
synchronous_standby_names = 'FIRST 1 (standby1, standby2)'
synchronous_commit = onThe names must match each standby's application_name. synchronous_commit controls how far the WAL must reach before the commit returns. The value on waits for the standby to flush WAL to disk.
Promoting a standby and planning failover
Promotion turns a standby into a writable primary and starts a new timeline. Run this on the standby:
SELECT pg_promote();Plan the old primary's return before you need it. After promotion, the old primary may hold commits the new primary never received. Rejoin it with pg_rewind, which needs data checksums or wal_log_hints = on:
pg_rewind --target-pgdata=/var/lib/postgresql/data \
--source-server='host=new-primary.example.com user=replicator dbname=postgres'Combining streaming with WAL archiving
A standby that falls far behind can need WAL the primary has already recycled. Archiving keeps copies of completed WAL segments. The standby then uses restore_command to fetch them:
# postgresql.conf on the standby
restore_command = 'cp /mnt/wal_archive/%f %p'Cascading standbys
A standby can stream WAL from another standby. This spreads read load and reduces the number of connections to the primary. The diagram below shows the layout.
Timelines
Each promotion creates a new timeline. A timeline ID tells the server which WAL history it follows. Standbys follow the new timeline after promotion. Use pg_rewind or a fresh base backup to bring the old primary onto the new timeline.
When to use logical replication instead
Physical replication copies the whole cluster byte for byte. It cannot copy a subset of tables, replicate between different major versions, or feed a different schema. Logical replication sends row changes for chosen tables. Choose it for those cases, and accept that it does not copy schema changes automatically.
Exercise
A standby cancels long reports with a conflict error. Name one setting that makes the standby wait longer, and state its cost.
Show answer
Raise maxstandbystreaming_delay. The standby then lags behind the primary for up to that time, so reads on it see older data.