PostgreSQL 19Dev & Eng
Content type: Comparison
9 min read
Load test reveals how PostgreSQL 19's WAIT FOR LSN compares to synchronous replication
- Reading
- 9 min
- Tables
- 1
- Code
- 1
A load test with hundreds of thousands of transactions shows that PostgreSQL 19's new WAIT FOR LSN command matches synchronous replication in performance, provided the WAL writer is tuned, and exposes two rare bugs every DBA should know about before taking the feature to production.
PostgreSQL 19 introduced the WAIT FOR LSN command, which takes the decision of whether to wait for a replica to reach a given WAL position before reading data away from the server and hands it to the application. In the first two parts of the series, published on Percona's blog, Jobin Augustine explained the concept and showed the command working via psql. In the third and final part, published on December 3, 2026 and which is the focus of this article, he leaves the comfortable territory of psql and puts the feature through a load generator written for the test, with hundreds of thousands of waits recorded.
The difference matters for anyone who manages replicas in production. A command that works in three manually typed lines can behave differently under 32 concurrent clients competing for connection pool, WAL writer, and walsender at the same time. That is exactly the scenario the test tries to reproduce, and it is from it that the numbers and the two bugs the author found come.
How the test was set up
Augustine wrote a Rust program called waitforlsn, using tokio and deadpool-postgres, with two separate connection pools: one for the primary, another for the replica, each with a size configurable via --primary-pool and --standby-pool. The flow of each task is straightforward: open a connection on the primary, run an INSERT ... RETURNING id, capture the LSN with pg_current_wal_insert_lsn(), return the primary connection to the pool, open a connection on the replica, run WAIT FOR LSN with TIMEOUT and NO_THROW, and optionally read the row back to confirm visibility.
One design detail is worth highlighting for anyone implementing this: the primary connection is returned to the pool before the wait begins. If the application kept the primary connection held during the WAIT FOR, replication lag would consume the connections of the main database, not the replica's. The program also records per-stage HDR histograms (pool acquisition, commit, LSN lookup, wait, verification), and when a wait exceeds the timeout, it logs a snapshot of all the WAL positions involved, which was decisive in finding the two bugs described below.
The author himself makes the right caveat: the test ran on a single laptop, with the postgres:19 Docker image, primary and replica on the same machine, with no network latency and none of the fsync behavior of server storage. The numbers serve as direction, not as a formal benchmark, and he himself plans to repeat the test on server hardware with separate hosts.
The numbers: parity with synchronous replication, but only after tuning
The table below summarizes the six configurations tested, with 32 concurrent clients in 20-second runs:
| Mode | TPS | p50 | p99 | Missing row on replica |
|---|---|---|---|---|
Asynchronous, synchronous_commit = on, reads the replica without waiting | 2,352 | 13.4 ms | 19.2 ms | 44,713 of 47,080 (95%) |
Synchronous, synchronous_commit = on | 2,446 | 12.8 ms | 19.0 ms | 6 of 48,965 |
Synchronous, synchronous_commit = remote_apply | 2,264 | 13.8 ms | 21.8 ms | 0 |
WAIT FOR standby_replay, tuned WAL writer | 2,065–2,360 | 13.0–14.9 ms | 21.6–26.0 ms | 0 in all runs |
WAIT FOR standby_flush, tuned WAL writer | 2,450 | 12.2 ms | 23.7 ms | not verified |
WAIT FOR standby_replay, default WAL writer | 108 | 215 ms | 442 ms | not verified |
The central result is that, with the WAL writer tuned, WAIT FOR LSN delivers throughput and latency in the same range as traditional synchronous replication. On local disk, it does not beat synchronous replication in raw speed, and Augustine is direct about that: the gain is not performance, it's that the primary never blocks and the application decides when (and whether) to wait.
The replica without waiting lies
The row that matters most to anyone making an architecture decision is the first one in the table. With no synchronization whatsoever, 95% of the reads made on the replica right after the commit on the primary did not find the newly inserted row. This is not exaggeration or an edge case, it is the default behavior of any asynchronous replica under any minimal load.
Even more revealing: even synchronous replication with synchronous_commit = on missed 6 out of 48,965 reads, because "written (flush) on the replica" is not the same as "replayed (replay) on the replica". Only remote_apply and WAIT FOR standby_replay guaranteed full visibility in the tests.
Reading from a replica without any wait is a lie.
Jobin Augustine, author of the series, Percona blog
For anyone designing replica read patterns assuming that synchronous_commit = on already solves read-your-writes, the test is a direct warning: it doesn't.
WAL writer tuning is not optional
The most drastic row in the table is the last one: with default WAL writer settings, the same WAIT FOR standby_replay scenario dropped to 108 TPS and a p50 of 215 ms, unusable for OLTP. The cause is simple: without wal_writer_flush_after = 0 and a small wal_writer_delay (10 ms, in the test), the WAL is only written to disk at the writer's default intervals, and every wait becomes hostage to that cycle.
In short: anyone adopting WAIT FOR LSN with synchronous_commit = off needs to treat WAL writer tuning as a prerequisite, not as a later optimization. Skipping that step drops throughput from about 2,000 TPS to about 108 TPS, a nearly 20-fold decrease, turning a promising feature into a serious bottleneck.
It's also worth noting that, with 1 to 4 clients, WAIT FOR was slower than the asynchronous baseline without waiting, because there is less batching and the extra round trip to the replica weighs proportionally more. This is expected and reinforces that the feature is designed for concurrent load, not for low-volume serial scripts.
Bug 1: the lock at the WAL page boundary
The first bug found occurs when the last WAL insert position falls exactly on a page boundary. In that case, pg_current_wal_insert_lsn() returns the boundary plus the header size of the next page (24 bytes, or 40 at the start of a segment), and that position is only reached when some other, unrelated WAL record arrives. Until then, WAIT FOR stays stuck, and in a closed-loop application this blocks all clients at once, because they tend to stop at the same LSN.
In the author's test, about 1 in every 2,000 waits ran to the full timeout because of this. The bug was identified by Xuneng Zhou in a TAP test (049_wait_for_lsn) and reported on the pgsql-hackers list; the fix was made by Alexander Korotkov on November 18, 2026 (commits 24723e7cf39 on master and 19aa5713be2 on REL_19_STABLE). The problem is that PostgreSQL 19.0 had already been tagged two days earlier, on November 16, so the fix only arrives in version 19.1.
Until then, the workaround is to correct the LSN before waiting, with an SQL function:
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
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
$$;The application starts calling SELECT wal_insert_end_lsn(); instead of SELECT pg_current_wal_insert_lsn(); right after the commit. In the author's test, applying the workaround eliminated timeouts across 144,724 waits and even raised throughput from 385 to 470 TPS, because fewer clients got stuck waiting out the full timeout.
Bug 2: the walsender that forgets to wake up
The second problem is rarer (2 occurrences in about 140,000 waits) and more subtle: the primary had already written (flushed) the WAL, but the walsender did not send the information, staying idle for up to 3 seconds. The author's hypothesis is a lost wakeup: the walsender reads the flush position in XLogSendPhysical() and only afterward enters the condition variable's wait list; if the flush and the broadcast happen in that interval, nobody wakes the process up.
This bug had already been identified and fixed before this very evaluation: "Fix lost wakeups in physical WAL senders", authored by Alex Speller and committed by Fujii Masao (a9b32f4c818), backported as far as version 16, where the condition variable was introduced. This is a relevant detail because the problem is not exclusive to WAIT FOR LSN: it affects traditional synchronous replication too, and in Speller's original report it caused stalls of up to 10 seconds in synchronous commits. A variant in logical walsenders was fixed later, in d10bb70b623, already after the 19.0 release.
In the roughly 470,000 waits run with the bug 1 workaround already applied, the author did not record any timeout attributable to this second problem, which is consistent with it already being fixed at the 19.0 tag.
What changes for anyone managing replicas
WAIT FOR LSN does not replace synchronous replication or make it obsolete: it moves the decision of when to wait into the application, instead of embedding it in server configuration via synchronous_standby_names. This has an operational cost the test makes clear: the replica's connection pool needs to be sized for the sessions waiting (in the test, the replica pool reached 75% average utilization), and monitoring needs to treat timeout as an "unknown result", never as a silent success.
The feature is also not free in terms of complexity: it's a new command, launched in this major version, with at least one bug still unfixed in the first release (19.0) and fixed only in 19.1. For anyone deciding whether to adopt it now or wait, Augustine's own list of pending items sums up the checklist:
- Adjust
wal_writer_flush_after = 0and a smallwal_writer_delayif usingsynchronous_commit = off, or else every wait costs about 200 ms more. - Always use
TIMEOUTand treat the timeout as an undefined state, neither as failure nor as success. - Only report success to the user after
WAIT FORreturns success, never before. - Size the replica's connection pool for the sessions that are left waiting.
- On 19.0, apply the
wal_insert_end_lsn()function as a workaround for the page boundary bug, or wait for 19.1.
For workloads with few concurrent clients or applications that tolerate eventually consistent reads, the gain from WAIT FOR LSN does not offset the added complexity: the test showed that, with 1 to 4 clients, the pattern is slower than simply not waiting. For systems that read immediately from the replica right after writing, such as listings that appear on the same screen as a registration form, the feature does solve a real problem, but only after proper tuning and, preferably, starting from version 19.1.
Translated from the Brazilian Portuguese original · Read the original
PostgreSQL 19's WAIT FOR LSN Matches Synchronous Replication in Tests, But Carries an Unfixed Bug
Load tests with hundreds of thousands of transactions show that PostgreSQL 19's WAIT FOR LSN delivers consistent reads on replicas without paying the price of traditional synchronous replication, but two bugs found along the way require attention before going to production.





