WAIT FOR LSN in PostgreSQL 19, a 3-part series
- Part 1: Stop Fighting Synchronous Replication: Meet WAIT FOR LSN : Understand the benefits
- Part 2: Stop Fighting Synchronous Replication: Meet WAIT FOR LSN : A walkthrough for DBAs
- Part 3: WAIT FOR LSN under load: numbers, tuning and two bugs I found in PostgreSQL 19.0 (you are here)
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:
That last point is also why I suggested in Part 1 to wait for PostgreSQL 19.1 before going to production.
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:
How it works:
A typical run:
|
1 2 3 |
./target/release/waitforlsn --mode waitfor --wait-mode standby_replay \ --tasks 32 --primary-pool 16 --standby-pool 32 \ --duration 20s --wait-timeout 5s --verify |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
== Summary (20.0s) == completed txns : 47238 (2359.5 tps) WAIT FOR status : success=47238 timeout=0 not_in_recovery=0 other=0 verify (row visible): ok=47238 missing=0 page-header LSNs : 48 (corrected to the page boundary) errors : 0 pool usage max_size in_use avg in_use max util avg waiting avg waiting max primary 16 7.3 16 45.9% 0.5 16 standby 32 24.1 32 75.4% 0.0 0 latency (ms) count mean p50 p95 p99 p99.9 max total 47238 13.532 13.007 17.951 21.583 38.175 225.791 primary_acquire 47238 0.212 0.003 1.659 2.687 4.283 6.211 commit 47238 1.628 1.510 2.611 3.873 5.807 14.455 lsn_fetch 47238 1.395 1.286 2.277 3.239 5.375 9.847 standby_acquire 47238 0.004 0.003 0.007 0.027 0.150 0.538 wait_for_lsn 47238 8.703 8.207 12.423 15.407 34.079 215.807 verify 47238 1.576 1.454 2.573 3.613 5.795 11.055 |
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.
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:
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.
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:
|
1 2 3 4 5 6 7 8 |
[task 1] note: REPRO: insert LSN 0/03302018 is just past a WAL page header (boundary 0/03302000); waiting for it as-is (--raw-insert-lsn) [task 3] note: REPRO: insert LSN 0/03302018 is just past a WAL page header (boundary 0/03302000); waiting for it as-is (--raw-insert-lsn) ... [task 1] wait_for_lsn error: timeout waiting for LSN 0/03302018 (target=0/03302018 | standby receive=0/03302000 replay=0/03302000 | primary flush=0/03302000 write=0/03302000 insert=0/03302018 walsender sent=standby1:0/03302000) ... completed txns : 23130 (385.5 tps) WAIT FOR status : success=23130 timeout=12 not_in_recovery=0 other=0 page-header LSNs : 16 (waited for as-is: --raw-insert-lsn) |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
NOTICE: hit after 1695 commits start_lsn | end_lsn | resource_manager | record_type | record_length ------------+------------+------------------+-------------+--------------- 0/0F207FD8 | 0/0F208000 | Transaction | COMMIT | 34 pg_current_wal_insert_lsn | pg_current_wal_flush_lsn | insert_lsn_page_offset ---------------------------+--------------------------+------------------------ 0/0F208018 | 0/0F208000 | 24 --- same session: WAIT FOR the insert LSN on the primary status --------- timeout == 2. standby: WAIT FOR the insert LSN 0/0F208018 in each mode, in parallel (TIMEOUT 2s) standby_replay : timeout standby_write : timeout standby_flush : timeout == 4. standby: WAIT FOR the page boundary 0/0F208000 instead: immediate success |
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):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
CREATE FUNCTION wal_insert_end_lsn(l pg_lsn DEFAULT pg_current_wal_insert_lsn()) RETURNS pg_lsn LANGUAGE sql STABLE AS $$ SELECT CASE WHEN (l - '0/0'::pg_lsn) % pg_size_bytes(current_setting('wal_segment_size')) < current_setting('wal_block_size')::int -- first page of a segment THEN CASE WHEN (l - '0/0'::pg_lsn) % current_setting('wal_block_size')::int = 40 THEN l - 40 ELSE l END ELSE CASE WHEN (l - '0/0'::pg_lsn) % current_setting('wal_block_size')::int = 24 THEN l - 24 ELSE l END END $$; postgres=# SELECT wal_insert_end_lsn('0/0F208018'), wal_insert_end_lsn('0/0F000028'), wal_insert_end_lsn('0/0F208100'); wal_insert_end_lsn | wal_insert_end_lsn | wal_insert_end_lsn --------------------+--------------------+-------------------- 0/0F208000 | 0/0F000000 | 0/0F208100 (1 row) |
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.
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.
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:
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.