PostgreSQL 19 WAIT FOR LSN: Performance, Tuning and Two Replication Stalls
- Jobin Augustine
- 367 reads
With WAL writer tuning, WAIT FOR LSN delivered replica read-after-write consistency at throughput near synchronous replication in laptop tests.

PostgreSQL 19’s WAIT FOR LSN gives applications a way to request replica visibility after a write without making every transaction wait for a standby. Tests with a small Rust load generator suggest that, with WAL writer settings tuned, this approach can deliver throughput in the same range as synchronous replication while ensuring the inserted row is visible on the standby before the application reads it.

The tests also exposed a page-boundary stall in PostgreSQL 19.0, which was fixed on the REL_19_STABLE branch and is expected in 19.1. A separate physical WAL sender wakeup issue was fixed in 19.0. These details matter: the feature’s behavior depends on WAL tuning, connection pool capacity, and knowing which version-specific issue your deployment may encounter.
Making the wait an application decision
The test program, written in Rust with tokio and deadpool-postgres, runs transactions in two modes. In the baseline mode, it commits on the primary and can then try reading from the standby without waiting. In the WAIT FOR mode, it commits asynchronously on the primary, obtains the transaction’s WAL insert LSN, and asks the standby to wait for that position. An optional read checks whether the row is visible.
Each worker uses separate connection pools for the primary and standby. It returns the primary connection to its pool before waiting on the standby. That ordering is practical: if workers held primary connections throughout replication lag, waiting sessions could exhaust the primary pool and prevent other transactions from progressing.
The program records latency at each stage, pool usage, wait outcomes, and visibility checks. When a wait times out, it captures WAL positions on both servers, including primary insert, write and flush positions, the walsender’s sent position, and standby receive and replay positions. Those snapshots helped distinguish slow replication from the two stalls described below.
One representative 20-second run used 32 tasks, a 16-connection primary pool, and a 32-connection standby pool. It completed 47,238 transactions, or 2,359.5 per second, with no wait timeouts and no missing rows. Median end-to-end latency was 13.0 ms. The median WAIT FOR time was 8.2 ms, compared with 1.5 ms for the commit itself. The standby pool averaged 24.1 connections in use and reached 32; the primary pool averaged 7.3 in use and also reached its maximum of 16.
That profile points to a capacity consideration: each worker waiting on the standby occupies a standby connection. Pool sizing should account for concurrent waits, while the primary pool can return connections to other work during replication delay.
What the comparison shows
The figures are directional, not formal benchmarks. They came from PostgreSQL 19.0 in Docker on one laptop, with a primary and streaming standby on the same machine. The workload used 32 concurrent clients, each issuing small single-row inserts over 20 seconds. With verification enabled, the program immediately queried the standby after the transaction completed. Local networking and laptop storage behavior limit how far these results can be applied to production servers.
For WAIT FOR runs, the WAL writer used wal_writer_delay = 10ms and wal_writer_flush_after = 0, except in the default-settings comparison. Under those tuned settings, async commit followed by a standby replay wait delivered between 2,065 and 2,360 transactions per second. Median latency ranged from 13.0 to 14.9 ms, and p99 latency from 21.6 to 26.0 ms. No verified reads missed the inserted row.
For context, synchronous replication with synchronous_commit = on reached 2,446 transactions per second, with 12.8 ms median and 19.0 ms p99 latency. The remote_apply setting reached 2,264 transactions per second, with 13.8 ms median and 21.8 ms p99 latency. The tuned WAIT FOR results sit in a similar range, though they don't show a speed advantage in this single-machine setup.
The consistency comparison is more striking. In the asynchronous baseline, the application committed locally and immediately read from the standby without waiting; 44,713 of 47,080 reads, or 95%, failed to find the row. With synchronous_commit = on, six of 48,965 reads still missed it: confirmation that WAL has been flushed on the standby doesn't guarantee that the standby has replayed it. The replay wait returned zero missing rows in the tested runs. A standby flush wait reached 2,450 transactions per second, but that run did not verify row visibility.
Using default WAL writer settings changed the result sharply. Async commit followed by a replay wait fell to 108 transactions per second, with median latency of 215 ms and p99 latency of 442 ms. The author attributes the difference to WAL writer tuning: with synchronous_commit = off, a wait can otherwise incur a delay of around 200 ms. At low concurrency, around one to four clients, WAIT FOR also ran slower than the local-flush baseline because there was less opportunity to batch work and an extra round trip remained.
A 19.0 page-boundary stall
The more consequential defect appeared roughly once in 2,000 waits during testing. When it struck, multiple clients could wait on the same LSN until timeout, stalling a closed-loop workload.
The underlying issue was how PostgreSQL reported an insert position at a WAL page boundary. When a record ended exactly at the boundary, pg_current_wal_insert_lsn() could return a position 24 bytes into the next page for its header, or 40 bytes at the start of a segment. That position marks where a subsequent record could begin. The flush, write, and replay positions used to satisfy WAIT FOR remained at the prior boundary until more WAL was written. The target therefore couldn't be reached by the existing record positions.
A reproduction on PostgreSQL 19.0 showed the primary flush position and standby positions stopped at 0/03302000 while the requested LSN was 0/03302018. Waiting for the boundary itself succeeded immediately. In a four-client test with the workaround disabled, 12 waits timed out over 60 seconds and throughput was 385 transactions per second. With the workaround, 144,724 waits completed without a timeout and throughput rose to 470 per second.
The fix, committed to REL_19_STABLE, makes WAIT FOR map these targets back to the page start. Existing applications using pg_current_wal_insert_lsn() should not need a workaround after upgrading to a version containing that fix. Until then, the provided SQL function corrects the LSN before the wait: applications can call wal_insert_end_lsn() in place of reading the raw insert LSN. The issue remained in 19.0 because that version was tagged two days before the fix was committed.
A physical walsender wakeup issue
The second stall occurred only twice in roughly 140,000 waits, on ordinary LSNs. In those cases the primary had flushed WAL, but the walsender had not sent it and remained idle for about three seconds. The suspected cause was a lost wakeup: the sender could read the flush position, then join a condition variable’s wait list after the notification had already been sent.
An upstream fix addressed lost wakeups in physical WAL senders and was included in PostgreSQL 19.0, with changes also applied to earlier supported versions. The same issue could affect synchronous replication, where a reported case stalled commits for about 10 seconds; it wasn't specific to WAIT FOR. A corresponding issue in logical walsenders was fixed later, after 19.0.
With the page-boundary workaround enabled, roughly 470,000 waits across runs with 4, 8, and 32 clients produced no timeouts. That sample doesn't establish a statistical guarantee, but it aligns with the physical sender fix being present in 19.0.
Operational takeaways
WAIT FOR LSN shifts the decision to wait for replica progress into the application. The test results suggest it can provide read-after-write behavior at throughput near synchronous replication after WAL writer tuning, while allowing other primary work to proceed during a wait. Teams evaluating it should tune wal_writer_flush_after to 0 and use a short wal_writer_delay when combining it with asynchronous commit.
Every wait should have a timeout, and a timeout should be treated as an unknown outcome rather than a successful write. The application should report success only after the wait succeeds, and the standby pool should be sized for sessions that may be waiting concurrently. PostgreSQL 19.0 deployments also need the page-boundary LSN correction until they can use a version containing the fix.
The next useful evidence will come from tests on separate server-grade hosts, where network delay and storage behavior differ from a laptop. For now, the practical case is clearest for applications that need selective replica read-after-write consistency and can accommodate the extra standby connection and wait step.