PostgreSQL 19Dev & Eng
Content type: Hands-on
6 min read
PostgreSQL 19 Brings WAIT FOR LSN, an Alternative to Synchronous Commit for Controlling Replica Consistency
- Reading
- 6 min
- Code
- 2
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.
PostgreSQL 19 introduced the WAIT FOR LSN command, which lets a session explicitly wait until a standby has replicated (or persisted) a given point in the WAL before proceeding. It is an alternative to traditional synchronous replication, configured via synchronous_standby_names, which blocks the commit on the primary until the standby confirms. In the second article of a three-part series published on Percona's blog on December 2, DBA Jobin Augustine sets aside the theory (presented in Part 1) and shows, using two psql sessions, how any DBA can reproduce the behavior without needing any application.
The core mechanics, according to the author, are simple to describe: commit on the primary, capture pg_current_wal_insert_lsn() on the same connection, and then run WAIT FOR LSN on the standby before telling the user the operation finished. Augustine's test environment uses a primary on port 5433 and an asynchronous standby on port 5434, both running PostgreSQL 19.0 via Docker Compose.
Broken Read-Your-Writes, Live
The article's most revealing demonstration uses a ryw.sql script that inserts a row on the primary, stores the id and the LSN in psql variables with \gset, switches the connection to the standby with \c, and then tries to read the same row. In Augustine's test, the first SELECT on the standby returns zero rows: the replica had not yet applied the WAL for that commit. Only after a WAIT FOR LSN that took 7.5 milliseconds does the row appear.
This is exactly the bug that affects applications that distribute reads across the primary and replicas via round-robin: the user saves an order, gets redirected to a replica on the next request, and can't find their own data. Without a mechanism like WAIT FOR LSN, the only way out used to be synchronous replication (which penalizes every write) or accepting the inconsistency.
\c "host=localhost port=5434 user=postgres dbname=postgres"
SELECT * FROM orders WHERE id = :id;
WAIT FOR LSN :'lsn';
SELECT * FROM orders WHERE id = :id;ERROR or NO_THROW: Who Decides What to Do with the Timeout
The command accepts a TIMEOUT and a NO_THROW modifier. Without it, exceeding the deadline raises an error that interrupts the flow; with NO_THROW, the session receives a timeout status and keeps running, leaving it to the application to decide the next step. Augustine summarizes the criterion for choosing:
Use NO_THROW when the application has a plan for a timeout (retry, read from the primary, report lag).
Jobin Augustine, DBA and author on Percona's blog
If the application doesn't have that plan B, the author's recommendation is to leave NO_THROW out and let the error interrupt the flow, since proceeding without the guarantee can be worse than failing explicitly.
Native Observability in pg_stat_activity
While a session waits, it shows up in pg_stat_activity with its own wait_event, WaitForWalReplay, which lets a DBA query how many sessions are waiting and for how long, without additional instrumentation in the application:
SELECT pid, state, wait_event_type, wait_event, query
FROM pg_stat_activity WHERE query LIKE 'WAIT FOR%';The Decisive Test: Frozen Standby
The article's most striking part compares both worlds under failure. With synchronous replication (synchronous_standby_names) and the standby stopped, Augustine set statement_timeout = '5s' and fired off an INSERT. The result: the session hung for more than two minutes, completely ignoring statement_timeout, and was only released after a manual pg_cancel_backend(), with a warning that the transaction had already been committed locally but might not have reached the standby. In production, this is the scenario where all of an application's sessions pile up waiting on a sick replica.
With asynchronous replication plus WAIT FOR LSN, Augustine simulated the same problem by pausing WAL replay on the standby with pg_wal_replay_pause(). The primary suffered no impact at all: the commit went through normally. On the standby, a WAIT FOR LSN with MODE 'standby_replay' and TIMEOUT '2s' respected exactly the configured deadline and returned timeout. But the same LSN, queried with MODE 'standby_flush', returned success immediately, even with replay paused: the data was already persisted in the standby's WAL, it just wasn't visible to queries yet.
In short: an application that only needs durability, without requiring the data to already be visible for reading, isn't affected by replay delays (conflicting long-running queries, recovery_min_apply_delay, etc.). With synchronous replication in remote_apply mode, the same situation would block every commit on the primary until the replay happened.
The Command's Restrictions, and Why They Exist
The article also documents the cases where WAIT FOR LSN simply refuses to execute, each with its own specific error message:
- Inside a
REPEATABLE READorSERIALIZABLEtransaction: the command is refused because the transaction already holds a snapshot from its first query, and waiting under these conditions would create inconsistency between what the transaction sees and what was just replicated. - Inside a function, procedure, or
DOblock:WAIT FORcan only run as a top-level statement, never viaEXECUTEinside PL/pgSQL. - While the session holds locks: the reason, according to the error message Augustine captured, is that a lock held by the waiting session could make the WAL replay itself wait on that session, a deadlock risk between primary and replica.
- The LSN must be a literal, not a bind parameter: trying to use
PREPAREwith$1in place of the LSN results in a syntax error.
There's also a useful detail for architectures with automatic failover: running WAIT FOR LSN on the wrong server (for example, with MODE 'primary_flush' on a replica, or the default mode on a primary that isn't in recovery) returns a status like not in recovery in about 1 millisecond in the author's tests. This gives the application a quick signal that the topology has changed and needs to be re-queried, without the delay of a full timeout.
What Changes for Those Building the Application
The most important difference for developers, and one the article leaves implicit by showing everything with two psql sessions, is that WAIT FOR LSN isn't a configuration swap like synchronous_standby_names: it's a code change. The application needs to capture the LSN on the same connection as the commit, carry it over to the connection that will read from the replica, and decide what to do with NO_THROW in case of a timeout. That means touching connection pools, drivers, and routing logic between primary and replicas, not just a parameter in postgresql.conf.
In exchange, the gain is granularity: each read decides for itself whether it needs the consistency guarantee or can accept potentially stale data, without imposing the cost of synchronous replication on every single write in the system. Part 3 of the series, according to the article's own summary, promises load numbers, tuning adjustments, and two bugs Augustine found in PostgreSQL 19.0, which should show whether this granularity survives under real production pressure.
Translated from the Brazilian Portuguese original · Read the original
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.





