Stop Fighting Synchronous Replication: Meet WAIT FOR LSN – Part 3 – numbers, tuning and bugs

December 3, 2026
Author
Jobin Augustine
Share this Post:

WAIT FOR LSN in PostgreSQL 19, a 3-part series

In Part 1, I explained how WAIT FOR LSN lets the application decide when to wait for a standby, instead of making every commit wait as synchronous replication does. In Part 2, we played with it using psql.

A few psql commands can show that a feature works. They can’t show what happens with thousands of transactions per second. So I wrote a small load generator and ran several hundred thousand waits against PostgreSQL 19.0. In this final part, I will share:

  • how the demo application works,
  • the throughput, latency and read-your-writes numbers against synchronous replication,
  • two rare stall bugs which came up during this testing, their status in 19.0, and a workaround for the one which is not fixed yet.

That last point is also why I suggested in Part 1 to wait for PostgreSQL 19.1 before going to production.

The demo application

All the numbers below come from a small Rust program I wrote for this evaluation (waitforlsn). It is a closed-loop load generator that runs one of two modes:

  • –mode sync is the baseline: commit on the primary, done. Optionally it fetches the LSN and reads the row back from the standby (–verify) to show what happens without a wait.
  • –mode waitfor is the new pattern: async commit on the primary, then WAIT FOR LSN on the standby.

How it works:

  • It is built on tokio and deadpool-postgres, and uses two separate connection pools: one to the primary, one to the standby, sized separately (–primary-pool, –standby-pool).
  • Each worker task does: get a primary connection → autocommit INSERT … RETURNING id → SELECT pg_current_wal_insert_lsn() on the same connection → give the primary connection back → get a standby connection → WAIT FOR LSN ‘<lsn>’ WITH (MODE …, TIMEOUT …, NO_THROW) → optionally SELECT the row to verify visibility.
  • Returning the primary connection before waiting is important. Otherwise replication lag would eat up your primary connections.
  • synchronous_commit is set per primary session through the libpq startup options, so the same cluster serves both tests.
  • It records HDR histograms for every step (pool acquire, commit, LSN fetch, wait, verify), counts the WAIT FOR statuses, samples pool usage, and appends everything to a CSV for comparison.
  • When a wait times out, it logs a snapshot of all WAL positions (primary insert/write/flush, walsender sent, standby receive/replay). This is what helped me find the two bugs below.

A typical run:

You can see that most of the time goes into wait_for_lsn (8.2 ms p50), and the commit itself is only 1.5 ms. Also look at the pool usage: the standby pool is the busy one, because each waiting session holds a standby connection.

Numbers from PostgreSQL 19.0

Please note that these figures are for directional guidance only and should not be treated as formal benchmarks. The tests were run locally on a single laptop using the postgres:19 Docker image (PostgreSQL 19.0) with one primary instance and one streaming standby. The workload consisted of 32 concurrent clients executing single, small INSERT transactions over 20-second runs. When –verify is enabled, the application immediately reads the inserted row back from the standby once the transaction completes. Because both instances run on the same machine, there is no network latency, and laptop fsync behavior does not reflect enterprise-grade server storage. The primary goal of this post is to illustrate the underlying concepts and benefits rather than measure production performance.

I plan to repeat this on server grade hardware on separate hosts.

For the WAIT FOR runs, the WAL writer was tuned as explained in Part 1 (wal_writer_delay = 10ms, wal_writer_flush_after = 0), except in the last row.

How the app considers the transaction complete TPS p50 (ms) p99 (ms) Row missing on standby read
Async replication, synchronous_commit = on (local flush only), then read standby 2,352 13.4 19.2 44,713 of 47,080 (95%)
Sync replication, synchronous_commit = on 2,446 12.8 19.0 6 of 48,965
Sync replication, synchronous_commit = remote_apply 2,264 13.8 21.8 0
Async commit + WAIT FOR standby_replay (wal_writer 10ms / flush_after 0) 2,065 – 2,360 13.0 – 14.9 21.6 – 26.0 0
Async commit + WAIT FOR standby_flush (wal_writer 10ms / flush_after 0) 2,450 12.2 23.7 (not verified)
Async commit + WAIT FOR standby_replay, default WAL writer settings 108 215 442 0

What we learn from this:

  • Reading from a replica without any wait is a lie. 95% of the reads did not find the row which was just committed. Even synchronous replication with synchronous_commit = on missed 6 rows, because “flushed on the standby” doesn’t mean “replayed on the standby”.
  • WAIT FOR with standby_replay never missed a row, in any run.
  • With the WAL writer tuned, the throughput and latency of WAIT FOR are in the same range as synchronous replication. On a single box it doesn’t magically beat it. That is fine, because the real benefit is NOT speed. It is that the primary never hangs and the application is in control of the data.
  • With default WAL writer settings, the pattern is not usable for OLTP. Please don’t skip the tuning part.
  • With only 1–4 clients, WAIT FOR is slower than the local-flush baseline, because there is less batching and the extra round trip dominates. This is expected.

Bugs I found while testing, and their status in 19.0

I ran several hundred thousand waits during this evaluation, and two rare problems came up. Both are interesting for anyone planning to use this feature, so let me explain them.

Bug 1: WAIT FOR stalls when the LSN is just past a WAL page header

Status: still present in PostgreSQL 19.0. Fixed in REL_19_STABLE, so expected in 19.1.

About 1 in 2000 waits ran until the full timeout. And when it happens, all the clients usually get stuck on the same LSN, so a closed-loop application stalls completely.

The cause: when the last WAL record ends exactly at a WAL page boundary, pg_current_wal_insert_lsn() returns the boundary plus the size of the next page’s header (24 bytes, or 40 at the start of a segment). This is the position where the next record would start. But all the positions WAIT FOR compares against (primary flush, standby write/flush/replay) are end-of-record positions, and they stay at the boundary until some other, unrelated WAL is written. So the wait sleeps until some other WAL arrives, or until the timeout.

I verified it again on the 19.0 release (Docker image postgres:19), using my program with the workaround disabled (–raw-insert-lsn), 4 clients, 60 seconds, 3s timeout:

Everything is at …000 (the boundary), and the target is at …018. Nothing can ever reach it until more WAL is written. With the workaround on, the same test gave 0 timeouts in 144,724 waits, and the throughput went up from 385 to 470 TPS.

The standalone psql reproducer also shows it on 19.0. Note that primary_flush also hangs, so you can reproduce it on a single server without any standby:

Discussion and fix: Xuneng Zhou first noticed this as an unexplained delay in the TAP test 049_wait_for_lsn and posted the root cause on pgsql-hackers. I reported the application-level impact and a reproducer in the same thread. Alexander Korotkov committed the fix on 2026-11-18 (“Don’t make WAIT FOR LSN depend on WAL written after a page boundary”, 24723e7cf39 on master, 19aa5713be2 on REL_19_STABLE). WAIT itself now maps such targets to the page start, so existing users of pg_current_wal_insert_lsn() don’t need any workaround. Unfortunately, 19.0 was tagged on 2026-11-16, two days before.

Workaround until you are on 19.1: correct the LSN before waiting. Here is a SQL function (tested on 19.0):

So the application can just run SELECT wal_insert_end_lsn(); after commit instead of SELECT pg_current_wal_insert_lsn();. My demo program does the same thing in its own code.

Bug 2: the walsender sometimes doesn’t send flushed WAL

Status: fixed in PostgreSQL 19.0.

Even with the page-boundary workaround, I saw something rarer: 2 stalls in about 140k waits, on ordinary LSNs. The snapshot showed that the primary had flushed the WAL, but the walsender had not sent it, and sat idle for 3 seconds. My guess was a lost wakeup: the walsender reads the flush position in XLogSendPhysical(), and only later joins the condition variable’s wait list in WalSndWait(). If a flush and its broadcast happen in between, nobody wakes the walsender. It then sleeps until the next standby status message or keepalive.

That is exactly what was found and fixed upstream: “Fix lost wakeups in physical WAL senders”, authored by Alex Speller and committed by Fujii Masao (a9b32f4c818), back-patched to v16, where the condition-variable wakeup was introduced. In Alex’s case, synchronous commits stalled for about 10 seconds. So this bug affected synchronous replication also, not only WAIT FOR. The same problem in logical walsenders was fixed later (d10bb70b623, after 19.0).

On 19.0 I ran about 470,000 waits (4, 8 and 32 clients) with the page-boundary workaround, and got zero timeouts. Because the problem was rare, this doesn’t prove anything statistically, but together with the commit being in the 19.0 tag, I am happy to call it fixed.

Conclusion of the series

Across these three posts, we saw that WAIT FOR LSN moves the “wait for the standby” decision from the server configuration into the application, where it belongs. The numbers in this part show that it does this without losing throughput compared to synchronous replication, once the WAL writer is tuned, and that it gives real read-your-writes on replicas.

The complete homework list:

  1. Tune wal_writer_flush_after = 0 and a small wal_writer_delay if you use synchronous_commit = off, otherwise every wait costs around 200 ms.
  2. Always use a TIMEOUT, and treat timeout as “outcome unknown”, not as success.
  3. Don’t report success to the user before the wait returns success.
  4. Size the standby connection pool for the waiting sessions.
  5. On 19.0, correct the insert LSN at page boundaries with the function above (or upgrade to 19.1 when it is out).

I strongly believe that this feature will change how we design applications on PostgreSQL in the coming years. Try it with the psql steps in Part 2, and please share your experience. If you find a problem, report it on pgsql-hackers. As we saw, the community is very quick to fix it.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved