Skip to content
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, call pg_current_wal_insert_lsn() on the same primary connection, then run WAIT FOR LSN on the target standby. Sending the write and LSN lookup in one simple-query string can capture a pre-commit position. With synchronous_commit = off, pg_current_wal_flush_lsn() can still lag behind the commit record when COMMIT returns.
  • The default standby_replay mode waits for WAL to be applied, enabling read-your-writes. standby_flush waits 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 LSN must run as a top-level statement, outside a transaction holding a snapshot or lock that could block replay. Omitting TIMEOUT or setting it to 0 waits indefinitely. With NO_THROW, the application checks the returned status and treats a timeout as an unknown outcome, not a rollback. A promoted standby immediately returns not 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 ms with defaults. Setting wal_writer_flush_after to 0 and wal_writer_delay to 10ms raised throughput from 108 TPS to above 2,000 TPS.
  • Each wait occupies a standby connection. In a test with 32 clients, the standby pool averaged 24 of 32 connections in use, versus about 7 on 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.

Read Official Source

Related PostgreSQL Reference