wrkm.app
Search
Databases

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

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 replicat

Editor
Dec 3, 2026 · 13 min read
Jobin Augustine: Stop Fighting Synchronous Replication: Meet WAIT FOR LSN – Part 3 – numbers, tuning and bugs

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:

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 ‘’ 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:./target/release/waitforlsn --mode waitfor --wait-mode standby_replay
--tasks 32 --primary-pool 16 --standby-pool 32
--duration 20s --wait-timeout 5s --verify== 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.055You 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:[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: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 successDiscussion 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.

pgsql-hackers discussion: https://postgr.es/m/CABPTF7WZ1yuYz8V=xsbghg8e7qaAm5MpyNw6BthWcbN7+P6biw@mail.gmail.com

Workaround until you are on 19.1: correct the LSN before waiting. Here is a SQL function (tested on 19.0):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. 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).

pgsql-hackers discussion: https://postgr.es/m/mvx5bs7q.33b99c32-67d8-4373-8611-7a8250fb2fd3@we.are.superhuman.com

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:

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. Always use a TIMEOUT, and treat timeout as “outcome unknown”, not as success. Don’t report success to the user before the wait returns success. Size the standby connection pool for the waiting sessions. 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.  

The post Stop Fighting Synchronous Replication: Meet WAIT FOR LSN – Part 3 – numbers, tuning and bugs appeared first on Percona.

Kaynak: Planet PostgreSQL

#PostgreSQL
Share

From other categories