PostgreSQL 19 WAIT FOR LSN: A More Selective Alternative to Synchronous Replication
- Jobin Augustine
- 476 reads
PostgreSQL 19 lets applications wait for a specific WAL position on a standby, with per-transaction control over durability, visibility and timeouts.

PostgreSQL 19’s WAIT FOR LSN gives applications a way to ask a replica to catch up with a particular write, rather than making every commit wait through synchronous replication. That shift can help teams protect selected transactions and support read-your-writes on replicas without making a slow or unavailable standby stall all commits.
The trade-off is that the guarantee now depends on application code. A timeout doesn’t mean a transaction failed, and the application shouldn’t report success until the requested standby position has been reached. PostgreSQL 19.0 also has a critical issue the feature’s author advises resolving before production use: wait for 19.1. A WAL page-boundary caveat is also noted for the LSN function used in the pattern below; further details and a workaround are described in Part 3.
Why move the wait into the application?
Synchronous replication makes the server enforce a common rule: with synchronous_standby_names configured and the default synchronous_commit = on, each affected commit waits for a standby’s acknowledgement. That may suit transactions where remote confirmation is essential, but it also applies to routine writes such as session updates or audit records—even when their callers don’t need that guarantee.
Availability is the larger operational concern. If the synchronous standby disappears or the network connection is interrupted, commit sessions can remain waiting. The documented behavior of statement_timeout doesn’t cap the wait for synchronous replication; a test described in the series found an insert with a five-second statement timeout still waiting for two minutes until manually cancelled.
Cancellation doesn’t roll back the transaction. PostgreSQL warns that it has already committed locally and may not have replicated to the standby. The result is an awkward combination: the primary has accepted the change, but the client can’t assume the standby received it.
High-availability tooling can reduce the risk of a single standby failure by managing multiple candidates or quorum settings. But those configurations bring their own operational work, including keeping synchronous_standby_names aligned with the available servers. A mistake can leave applications waiting when they need to make progress.
In practice, application requirements are often narrower than “make every commit wait.” A business transaction may need confirmation on two servers before the user sees success, while a different operation may need only to ensure that a subsequent read from a replica can see the user’s write. WAIT FOR LSN lets the application choose when to impose that wait and what kind of confirmation it needs.
What the command waits for
PostgreSQL records write activity in WAL, identifying positions with Log Sequence Numbers, or LSNs. The new SQL command waits until a server reaches a specified position:
WAIT FOR LSN 'lsn' [ WITH ( MODE 'standby_replay' | 'standby_write' | 'standby_flush' | 'primary_flush', TIMEOUT 'timeout', NO_THROW ) ]
A successful call returns a status row. The mode defines what “reached” means:
| Mode | Where it runs | Success means |
|---|---|---|
standby_replay (default) | Standby | The WAL has been replayed, so the change is visible to reads on that standby. |
standby_flush | Standby | The WAL has been flushed to durable storage there. |
standby_write | Standby | The WAL has been written to the operating system, but not necessarily flushed to storage. |
primary_flush | Primary | The WAL has been flushed on the primary; this can be useful with synchronous_commit = off. |
These modes offer different guarantees. A write acknowledged with standby_write can survive a PostgreSQL process crash on the standby, but not necessarily an operating-system crash. standby_flush waits for the standby’s durable flush, while standby_replay is the relevant choice when the next read must see the change.
Timeout handling needs care. Omitting TIMEOUT, or setting it to zero, means wait indefinitely—not check once. Set an explicit limit in the application. By default, a timeout raises an error; adding NO_THROW returns a status instead, which the caller must inspect and handle.
There are also execution constraints. WAIT FOR LSN must run as a top-level statement, not inside a function, procedure or DO block. It can’t run while the transaction holds a snapshot or a lock that could prevent the standby from replaying the WAL being awaited. That restriction avoids a self-blocking wait, but it means applications need to structure the operation as separate steps.
A practical write-then-wait sequence
The basic flow separates the write from confirmation on the replica:
- Commit the transaction on the primary.
- On the same connection, call
SELECT pg_current_wal_insert_lsn();to capture a position at or beyond the commit record. - Connect to the standby and call WAIT FOR LSN with that position, a chosen mode and a finite timeout.
- Report success to the user or read from the standby only after the result is successful.

Capturing the insert position matters when the primary uses synchronous_commit = off. The commit can return before its WAL record has been flushed, so an immediate call to pg_current_wal_flush_lsn() might return a position behind that commit. Waiting for that earlier position could finish before the transaction’s record is covered. The insert position is at or beyond the end of the commit record, making it the safer target. PostgreSQL’s documentation also recommends this approach.
Keep the commit and LSN query as separate protocol operations. If the write and the query are sent as one multi-statement string using the simple query protocol, they run within one implicit transaction. In that case, the LSN can be captured before the commit rather than after it.
Async commits with a remote durability check
WAIT FOR LSN can be paired with asynchronous commits. With synchronous_commit = off, the primary acknowledges a commit without waiting for its own WAL flush. A physical WAL sender, however, sends only WAL that has already been flushed on the primary. So if the application waits until standby_flush succeeds, the transaction’s WAL has been flushed on both servers.
That gives the application a way to trade a global wait for a targeted one. Compared with local-flush synchronous commit, a successful standby flush adds a durable copy on the standby. With standby_replay, the caller can instead wait for the change to become visible there—a behavior comparable to remote apply, with read-your-writes support.
The key difference from synchronous replication is who waits. Server configuration determines which commits block under synchronous replication; with WAIT FOR LSN, application logic requests confirmation only for the operations that need it. If the standby is slow, the primary can continue accepting other commits, while the waiting call returns a timeout if it reaches the limit chosen by the application. The application can then retry, read from the primary, raise an alert or mark the outcome unconfirmed.
The application can also wait on the particular standby it intends to read from. There’s no need to select that standby through synchronous replication settings. If the standby has been promoted, the command returns “not in recovery,” giving the application a signal that the topology has changed.
Where the guarantee ends
WAIT FOR LSN doesn’t make the primary’s acknowledgement synchronous. If the primary fails after returning from COMMIT but before the wait succeeds, the transaction may be absent from a standby promoted after the failure. With synchronous_commit = off, a primary-only crash can also lose a recently acknowledged commit. The application must wait for success before telling the user the operation is confirmed. A timeout means the result is unknown, not that the write was undone.
Other sessions can see a committed change on the primary before the standby receives it. And because the server doesn’t enforce the wait for every write, a code path that forgets to call WAIT FOR LSN falls back to ordinary asynchronous replication. A shared data-access layer is a safer place to implement the pattern than scattered, optional calls.
Waiting also consumes connections on the standby. In one test with 32 clients, the standby pool averaged 24 connections in use, while the primary pool needed about seven. The pool-sizing rule offered for this pattern is standby connections ≈ clients × wait time / total time. In effect, some connection demand shifts from the primary to the standby, so teams should account for the waiting workload when sizing pools.
WAL writer settings can dominate the wait
Initial tests with default settings put the wait at roughly 200–400 milliseconds. The reason is the path WAL takes: the physical sender won’t transmit unflushed WAL, and with asynchronous commits the WAL writer handles the flush. By default, that process wakes every 200 milliseconds.
The suggested primary-side settings are:
ALTER SYSTEM SET wal_writer_flush_after = 0; -- default 1MB ALTER SYSTEM SET wal_writer_delay = '10ms'; -- default 200ms SELECT pg_reload_conf();
Both settings can be reloaded without a restart. Setting wal_writer_flush_after to zero makes each asynchronous commit wake the WAL writer for an immediate flush; lowering wal_writer_delay also reduces the interval between flushes. The settings affect asynchronous commits, not sessions using synchronous_commit = on.
In the reported test, throughput rose from 108 TPS with defaults to more than 2,000 TPS after tuning, in the same range as synchronous replication. Those figures are specific to that test; the practical point is that a poorly tuned WAL writer can turn a selective wait into a noticeable delay.
What teams should check before adopting it
WAIT FOR LSN makes replication guarantees more granular, but it doesn’t remove the need to reason about failure cases. Set a timeout, treat its expiry as an uncertain outcome, and don’t confirm success to the user until the selected mode returns success. Ensure the standby pool can hold connections for sessions waiting on replay or flush, and tune the WAL writer if asynchronous commits are part of the design.
Most importantly, don’t build a production rollout around PostgreSQL 19.0 without accounting for the critical fix absent from that release. The feature’s promise is a more precise choice about which writes wait and what confirmation they require. Its value will depend on correct application handling, release readiness and measured behavior under the workload the system actually serves.