Percona PostgreSQL Blog· Jobin Augustine·· 2d agoFeatured
How PostgreSQL 19’s WAIT FOR LSN Can Replace Synchronous Replication
Stop Fighting Synchronous Replication: Meet WAIT FOR LSN – Part 1
Key Takeaway
PostgreSQL 19’s WAIT FOR LSN lets apps wait after commit for a standby flush or replay, for durability on both servers or read-your-writes. A timeout leaves the outcome unknown. Percona’s author recommends waiting for 19.1 before production use because 19.0 lacks a critical fix.
Why It Matters
Waiting on demand keeps standby outages from stalling primary commits, but standby connection pools need capacity for sessions held open while they wait.
Technical Breakdown · Distilled from the original source
Capture the LSN After Commit
- After
COMMIT, callpg_current_wal_insert_lsn()on the same primary connection, then runWAIT FOR LSNon the target standby. Sending the write and LSN lookup in one simple-query string can capture a pre-commit position. Withsynchronous_commit = off,pg_current_wal_flush_lsn()can still lag behind the commit record whenCOMMITreturns. - The default
standby_replaymode waits for WAL to be applied, enabling read-your-writes.standby_flushwaits for the standby to flush WAL. Since the physical WAL sender only sends WAL already flushed on the primary, success in this mode means the relevant WAL is durable on both servers.
Timeouts and Production Readiness
WAIT FOR LSNmust run as a top-level statement, outside a transaction holding a snapshot or lock that could block replay. OmittingTIMEOUTor setting it to0waits indefinitely. WithNO_THROW, the application checks the returned status and treats a timeout as an unknown outcome, not a rollback. A promoted standby immediately returnsnot in recovery.- Percona’s author flags a WAL page boundary issue with
pg_current_wal_insert_lsn()in PostgreSQL 19.0 and recommends waiting for 19.1 before production use.
WAL Flushing and Standby Pool Sizing
- In the author’s async commit tests, waits took roughly
200–400 mswith defaults. Settingwal_writer_flush_afterto0andwal_writer_delayto10msraised throughput from108 TPSto above2,000 TPS. - Each wait occupies a standby connection. In a test with
32clients, the standby pool averaged24of32connections in use, versus about7on the primary.
Source: Percona PostgreSQL Blog· percona.com
PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.