PostgreSQL 19Dev & Eng
Content type: Hands-on
6 min read
PostgreSQL 19 brings the WAIT FOR LSN command to solve read-your-writes on replicas
The second article in Percona's series on WAIT FOR LSN shows, with real commands in psql, how the PostgreSQL 19 feature solves read-your-writes without requiring synchronous replication or locking up the primary when the replica stalls.
- Reading
- 6 min
- Tables
- 1
- Code
- 2
The second article in Percona's series on WAIT FOR LSN shows, with real commands in psql, how the PostgreSQL 19 feature solves read-your-writes without requiring synchronous replication or locking up the primary when the replica stalls.
The problem the command targets
The WAIT FOR LSN command is part of PostgreSQL 19 and targets a problem every application with a read replica knows well: the commit returns on the primary, but the next query can hit a standby that hasn't yet applied that WAL. The result is the classic broken read-your-writes bug, today worked around with a fixed sleep, blind retries, or simply accepting stale data. In Part 1 of this series, published on the Percona blog, Jobin Augustine had already argued that this is PostgreSQL 19's highest practical-impact feature.
In this Part 2, published on December 2, 2026, Augustine builds a reproducible walkthrough with two psql sessions and no application code, so any DBA can check the behavior firsthand. The tests ran against a real PostgreSQL 19.0 instance, spun up via docker-compose, with the primary on port 5433 and an asynchronous standby on port 5434.
The basic flow: commit, LSN, wait
The mechanics are simple to describe. After the commit on the primary, the same connection calls pg_current_wal_insert_lsn() to capture the exact WAL point generated by that transaction. That LSN travels, as text, to the session that will read on the replica, which runs WAIT FOR LSN with that value before running the query.
In the author's test, a script inserts an order with synchronous_commit = off, grabs the commit's LSN, and switches connection (via \c) to the standby. Without WAIT FOR, the first read on the replica returns zero rows; after a 7.513 ms wait, the row appears:
-- on the primary
SET synchronous_commit = off;
INSERT INTO orders(item) VALUES ('phone') RETURNING id;
SELECT pg_current_wal_insert_lsn(); -- 0/0E981FD8
-- on the replica
SELECT * FROM orders WHERE id = 2; -- 0 rows: hasn't arrived yet
WAIT FOR LSN '0/0E981FD8';
SELECT * FROM orders WHERE id = 2; -- now it appearsTimeout with an escape route: ERROR or NO_THROW
By default, WAIT FOR LSN blocks until the configured TIMEOUT and then raises an ERROR. With the NO_THROW option, it returns a status column (success or timeout) instead of aborting the transaction, giving the application the chance to decide the next step: retry, read from the primary, or tell the user the data is on its way. For whoever writes the application, the choice between the two forms is an architecture decision: use NO_THROW when there's a plan B; let the ERROR fire when the guarantee is non-negotiable.
You can see who's waiting
While a session waits, it shows up in pg_stat_activity with wait_event_type equal to IPC and wait_event equal to WaitForWalReplay. This means you can monitor, with a simple query, how many sessions are stalled and for how long, similarly to what traditional synchronous replication also exposes via pg_stat_activity (pid, wait_event, and wait time).
SELECT pid, wait_event_type, wait_event, query
FROM pg_stat_activity
WHERE query LIKE 'WAIT FOR%';The central point: a stuck standby doesn't lock up the primary
Here's the article's most relevant demonstration. With traditional synchronous replication (synchronous_standby_names configured), Augustine took down the standby and ran an INSERT with a statement_timeout of 5 seconds. The INSERT hung for more than two minutes before being cancelled manually, because statement_timeout doesn't interrupt a wait for synchronous replication.
Two minutes!, and the statement_timeout of 5s did nothing. In production, this is every session of the application piling up.
Jobin Augustine, author of the article on the Percona blog
With WAIT FOR LSN, the scenario changes because replication stays asynchronous the whole time: there's no synchronous_standby_names configured, and the primary never waits for anything. Augustine paused WAL replay on the standby (SELECT pg_wal_replay_pause();) and ran WAIT FOR LSN in standby_replay mode with TIMEOUT '2s' and NO_THROW: the return was a clean timeout, exactly at the two seconds requested, without locking anything up.
The most important detail appears when switching to standby_flush mode: even with the replica still paused, the call returned success almost instantly. The WAL was already durably written to the standby's disk, even though the row wasn't yet visible in a SELECT. An application that only needs durability, not immediate reads, isn't affected by a delay in WAL replay.
| Mode | Where it runs | What it guarantees |
|---|---|---|
standby_replay | Standby | The row is already visible in a SELECT |
standby_flush | Standby | The WAL is durable on the standby, even if not yet applied |
primary_flush | Primary | The WAL is durable on the primary itself |
The restrictions, and why each one exists
The command has four deliberate limitations, all documented in the walkthrough:
- It doesn't run inside a transaction that already holds a snapshot (
REPEATABLE READor above): the transaction has already seen the database at a fixed point in time, so waiting for a future LSN inside it doesn't make logical sense. - It doesn't run inside a function, procedure, or
DOblock: it's a top-level statement, likeCOMMIT, and can't be called viaEXECUTEinside PL/pgSQL. - It doesn't run while the session holds a lock: this avoids a possible deadlock between the waiting session and the recovery process, which in turn may need that same lock to proceed.
- The LSN needs to be a literal, not a bind parameter: trying to use
PREPAREwithWAIT FOR LSN $1gives a syntax error, so the application needs to build the string with the value already resolved.
What changes for whoever writes the application
None of these restrictions requires a new driver or extension: it's plain SQL, any client that speaks the Postgres protocol can issue the command. The real care is at the connection layer: since WAIT FOR LSN can't run inside a transaction with an open snapshot, ORMs that wrap every call in BEGIN/COMMIT by default need an explicit exit to run this command outside that envelope.
In practice, the gain is separating two decisions that today lived glued together: the latency of the commit on the primary (which stays fast, in asynchronous mode) and the read guarantee on the replica (requested point by point, only where the code actually needs it). This means it's no longer necessary to configure synchronous_standby_names just to solve read-your-writes: WAIT FOR LSN delivers that guarantee per query, without paying the latency cost on every commit.
What's still to come
This is the second of three pieces in the series. Part 3, according to Augustine himself, will bring performance numbers under load, tuning guidance, and two bugs he found in PostgreSQL 19.0 during testing. It's worth holding off on production adoption until reading that chapter: since this is a .0 release, rough edges in this kind of new feature are expected, and whoever manages replicas in production will want to know exactly which ones before relying on the command for read consistency.
Translated from the Brazilian Portuguese original · Read the original
PostgreSQL 19 Brings WAIT FOR LSN, an Alternative to Synchronous Commit for Controlling Replica Consistency
In an article published on December 2 on Percona's blog, Jobin Augustine shows, using two psql sessions, how PostgreSQL 19's WAIT FOR LSN command solves the classic read-your-writes problem without resorting to traditional synchronous replication.





