PostgreSQL 19Dev & Eng
Content type: Hands-on
8 min read
PostgreSQL 19 lets you wait for replication without blocking commits with WAIT FOR LSN
- Reading
- 8 min
- Tables
- 1
- Code
- 2
Jobin Augustine, from Percona, details in a three-part series how the new WAIT FOR LSN command moves the decision to wait for the standby from server configuration to application code.
The problem synchronous replication never solved well
In a post published on December 1, 2026 on the Percona blog (reproduced on Planet PostgreSQL), DBA Jobin Augustine describes what he considers the most impactful feature in PostgreSQL 19: the WAIT FOR LSN command. The motivation is a problem familiar to anyone who has operated synchronous replicas for years: guaranteeing that "data cannot be lost" or that "the user must see what they just saved, even when reading from a replica" has always required blocking the commit until the standby confirms it.
The price of that guarantee was never just performance. With synchronous_standby_names configured, every transaction with synchronous_commit = on waits for the standby, including the audit log insert that nobody is watching. If the standby goes down or the network partitions, the session hangs indefinitely, and statement_timeout doesn't help: Augustine himself shows, in Part 2 of the series, an INSERT with a five-second timeout that stayed stuck for two minutes.
Canceling the wait also doesn't undo anything. The server honestly warns that the transaction has already been committed locally but may not have reached the standby. In other words: the "synchronous" guarantee had already evaporated at the moment of cancellation, and high-availability tools like Patroni end up having to manage synchronous_standby_names in real time to reduce that risk.
If someone asks me what would be the biggest impactful feature in PostgreSQL 19, I don't have any doubt in saying that it is WAIT FOR LSN from standby.
Jobin Augustine, Percona
What the command actually does
WAIT FOR LSN is a new SQL command that waits for the server to reach a specific position in the WAL (the Log Sequence Number, or LSN). The syntax accepts a mode, a timeout, and an option to avoid throwing an error:
Expert views
What people are saying about it.
Vercel's AI Gateway added Step 5, from China's StepFun, to the catalog of models available for front-end agents. The news matters less for the model's name and more for what it reveals about how to integrate, swap, and compare LLMs without rewriting code.
The This Week in Rails bulletin from November 27, 2026 brings a command to audit secrets without revealing them, the end of a per-attachment query in Action Text, and an Action Mailer that no longer needs to drag Active Job along at boot.
WAIT FOR LSN 'lsn' WITH (
MODE 'standby_replay' | 'standby_write' | 'standby_flush' | 'primary_flush',
TIMEOUT '5s',
NO_THROW
);MODE defines what "reaching" means, and this choice is the core of the feature:
| Mode | Where it runs | Returns success when | What it guarantees |
|---|---|---|---|
standby_replay (default) | Standby | WAL has been replayed (applied) | The change is already visible on the standby: read-your-writes |
standby_flush | Standby | WAL has been written to disk (fsync) | The change is durable on the standby |
standby_write | Standby | WAL has been written to the OS, without fsync yet | Survives a PostgreSQL crash on the standby, not an OS crash |
primary_flush | Primary | WAL has been flushed on the primary | Useful together with synchronous_commit = off |
Without TIMEOUT, or with TIMEOUT '0', the command waits forever: it's not a "check once" operation. Augustine recommends always specifying an explicit timeout. Another important restriction: the command must be a top-level statement, it cannot run inside a function, procedure, or DO block, nor while the transaction holds a snapshot or a lock (there's a risk of self-blocking).
The usage pattern: commit on the primary, wait on the standby
The flow proposed by Augustine is straightforward. The application commits the transaction on the primary and, on the same connection, captures the WAL insert position with SELECT pg_current_wal_insert_lsn(). It then goes to the standby and executes WAIT FOR LSN with that value. Only when the status returns success does the system respond "saved" to the user, or read from the standby.
One technical detail separates those who implement this correctly from those who get it wrong: why use pg_current_wal_insert_lsn() and not pg_current_wal_flush_lsn(). With synchronous_commit = off, the commit record may not yet be flushed when COMMIT returns, so the flush position captured right afterward may lag behind the actual commit, causing the wait to return success too early. The insert position is always at or after the commit position, and that is the recommendation the PostgreSQL documentation itself adopts.
There's a protocol trap that easily goes unnoticed: sending the write and the SELECT pg_current_wal_insert_lsn() call as a single multi-statement string (simple query protocol) makes both run as an implicit transaction, and the captured LSN ends up being the one from before the commit. The call needs to be made in a separate round trip.
Why this replaces (in part) synchronous replication
The interesting combination, according to Augustine, is to use WAIT FOR LSN together with synchronous_commit = off on the primary. The primary confirms the commit without waiting for its own WAL flush, which is cheap. The physical walsender, by design, only sends the standby WAL that is already flushed on the primary (Augustine cites the source code, walsender.c, XLogSendPhysical(), to support this guarantee).
The result is that, when WAIT FOR ... MODE 'standby_flush' returns success, the transaction is already on disk on both the primary and the standby. This gives durability equivalent to synchronous replication with synchronous_commit = on, and the standby_replay mode is equivalent to the old remote_apply, including read-your-writes.
In summary, the practical difference between the two approaches is who decides to wait and what happens when the standby fails:
- Synchronous replication: every session that commits waits, by the server's decision; if the standby goes down, commits hang indefinitely, and canceling doesn't undo the local commit.
- Async +
WAIT FOR LSN: only whoever requested it waits, with the mode and timeout the application chose; if the standby goes down or is slow, the wait returnstimeoutwithin the configured period, and the primary continues responding normally to all the rest of the traffic.
The limits DBAs need to know before trusting this
Augustine is explicit about not selling the feature as a magic solution, and it's worth repeating the warning here with the same rigor. The primary confirms the commit before the standby has the data: if it goes down between the COMMIT and a successful wait, the transaction can be lost in a failover, and with synchronous_commit = off even a crash of the primary alone can lose the commit. That's why the application cannot report success to the user before the wait returns success: a timeout must be treated as an "unknown result," never as success.
Another point that changes connection design: a session waiting on the standby keeps the connection busy for the entire wait. In Augustine's test with 32 clients, the standby pool ended up using an average of 24 out of 32 connections, while the primary pool needed only about 7. This reverses the traditional logic of pool sizing between reads and writes, and directly affects any read-write split strategy already in production.
And there's a design risk that no tool solves on its own: the guarantee lives in the application code, not on the server. A developer who forgets to call WAIT FOR LSN simply ends up with plain asynchronous replication, with no warnings. Augustine's recommendation is to encapsulate the call in the data access layer or an internal library, never scatter the call throughout business code.
The setting that decides whether this works: the WAL writer
In Augustine's first tests, with default configuration, the waiting part of WAIT FOR LSN took between 200 and 400 milliseconds: too long for a feature meant to be lightweight. The cause is that, with synchronous_commit = off, it's the WAL writer process that flushes the WAL, and by default it wakes up every wal_writer_delay (200ms).
The fix lies in two parameters, both reloadable without restarting the server:
ALTER SYSTEM SET wal_writer_flush_after = 0; -- default is 1MB
ALTER SYSTEM SET wal_writer_delay = '10ms'; -- default is 200ms
SELECT pg_reload_conf();With wal_writer_flush_after = 0, every asynchronous commit wakes up the WAL writer and triggers an immediate flush. Augustine records the effect on throughput: with default settings, he measured only 108 TPS; after the adjustment, more than 2,000 TPS, in the same range as synchronous replication. It's worth noting that these settings only affect asynchronous commits: sessions with synchronous_commit = on remain as they were.
Before going to production
The most important warning in the piece, and the one every DBA should take seriously before designing an application around the new command, is about the PostgreSQL version itself. Augustine explicitly recommends waiting for PostgreSQL 19.1 before going to production, because a critical fix is not included in version 19.0: the details and a temporary workaround are left for Part 3 of the series, which will also bring the load numbers and the bugs found during testing.
Part 2, not yet published at the time of this writing, promises a practical walkthrough via psql and the history of more than six years of discussion that brought the feature to its final form.
For those administering critical databases in Brazil, the correct takeaway is not to swap synchronous replication for WAIT FOR LSN overnight, but to recognize that PostgreSQL 19 offers, for the first time, a tool to decide this guarantee on a per-transaction basis, instead of imposing it globally via synchronous_standby_names. This requires equivalent engineering discipline: encapsulating the call, testing the timeout as a normal code path, sizing the standby pool, and above all, waiting for the 19.1 fix before trusting the feature to production.
Translated from the Brazilian Portuguese original · Read the original
PostgreSQL 19 brings WAIT FOR LSN as an alternative to synchronous replication
New command shifts to the application the decision of whether to wait for a replica, promising to end the stalled commits of traditional synchronous replication.





