PostgreSQL 19Dev & Eng
Content type: Hands-on
7 min read
PostgreSQL 19 brings WAIT FOR LSN as an alternative to synchronous replication
- Reading
- 7 min
- Tables
- 2
- Code
- 2
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.
O PostgreSQL 19 arrives with a new SQL command, WAIT FOR LSN, which tackles a classic problem for those operating databases in production: how to guarantee durability without leaving the entire database hostage to a slow or offline replica. The full technical description is in a post from December 1, 2026 by engineer Jobin Augustine, on the Percona blog, the first in a series of three about the feature.
According to the author, it's the highest-impact feature of the new version. He took part in the testing and in the discussions on the PostgreSQL hackers mailing list before the release.
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, author on the Percona blog
The problem synchronous replication never solved well
When a client asks for "the data can't be lost" or "the user needs to see what they just saved, even when reading from a replica", the traditional answer is to configure synchronous_standby_names and turn on synchronous replication. The problem is that this guarantee applies to EVERY transaction, including that audit log insert that nobody needs to wait for.
The most serious side effect appears when the replica goes down or the network partitions: the session that's committing waits forever, because there's no built-in timeout for that wait. Setting statement_timeout doesn't help, because PostgreSQL explicitly documents that this parameter doesn't cover the wait for synchronous replication. Manually canceling the wait doesn't undo anything either: PostgreSQL warns that the transaction has already been committed locally, but may not have reached the replica.
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.
In practice, this pushes complexity onto high-availability tools. Patroni, for example, needs to reconfigure synchronous_standby_names every time a replica comes up or goes down, and teams end up configuring quorums like ANY 1 (s1, s2) just to get some slack. A configuration mistake here turns into downtime.
What WAIT FOR LSN actually does
The command is straightforward: the application grabs an LSN (Log Sequence Number, the position in the WAL that PostgreSQL generates on every write) and tells the server to wait until it reaches that position.
WAIT FOR LSN 'lsn' [ WITH ( option [, ...] ) ]
-- option can be:
-- MODE 'standby_replay' | 'standby_write' | 'standby_flush' | 'primary_flush'
-- TIMEOUT 'timeout' -- in milliseconds, or with a unit: '100ms', '5s'
-- NO_THROW -- returns a status instead of throwing an errorThe return is a single row with a status column, which can be success, timeout, or not in recovery. The MODE parameter changes what "reaching" means:
| MODE | Runs on | Success when | Guarantees |
|---|---|---|---|
standby_replay (default) | Standby | WAL has been replayed | Change visible on the replica (read-your-writes) |
standby_flush | Standby | WAL has been flushed to disk (fsync) on the replica | Durability on the replica |
standby_write | Standby | WAL written to the OS, no fsync yet | Survives a PostgreSQL crash, not an OS crash |
primary_flush | Primary | WAL flushed to disk on the primary | Useful with synchronous_commit = off |
A few restrictions matter from the very first use. Without TIMEOUT, or with TIMEOUT '0', the wait is infinite: always provide a value. Without NO_THROW, a timeout triggers an error; with NO_THROW, the application gets the status and needs to check it. And the command only runs as a top-level statement: it doesn't work inside a function, procedure, or DO block, nor while the transaction is holding a snapshot or a lock, because that could stall the very replica it's waiting on.
The usage pattern: commit on the primary, wait on the standby
Augustine describes the flow in four steps: the application commits on the primary; on the same connection, it grabs the current position with SELECT pg_current_wal_insert_lsn(); it goes to the replica and runs WAIT FOR LSN with the chosen MODE, TIMEOUT, and NO_THROW; it only tells the user it "worked" when the status comes back success.
The choice of pg_current_wal_insert_lsn() instead of pg_current_wal_flush_lsn() isn't an accident. With synchronous_commit = off, the commit record may not have been flushed to disk yet when COMMIT returns, so the flush LSN grabbed right after could be behind the commit, and the wait would return success too soon. The insert LSN always sits at the position of the end of the commit or later, which is what PostgreSQL's own documentation recommends. Another precaution mentioned by the source: don't send the write and the SELECT pg_current_wal_insert_lsn() as a single multi-statement string, because that runs as an implicit transaction and returns the LSN from before the commit.
Why this replaces synchronous replication
The piece that closes the puzzle is combining WAIT FOR LSN with synchronous_commit = off on the primary. The primary confirms the commit without waiting for its own WAL flush, which is cheap. Since the physical walsender only sends WAL that's already been flushed to disk on the primary, when WAIT FOR ... MODE 'standby_flush' returns success, the transaction is already on disk on both servers.
In other words: async commit plus WAIT FOR standby_flush delivers a durability guarantee similar to traditional synchronous replication, and standby_replay is comparable to the remote_apply mode, with the bonus of real read-your-writes on replicas.
| Synchronous replication | Async + WAIT FOR LSN | |
|---|---|---|
| Who waits | Every session, by server configuration | Only the transaction the application decides on |
| Standby down | Commits stall forever | Primary keeps running; wait returns timeout |
| Action on the problem | Cancel and accept "already committed locally" | Application decides: retry, read from the primary, alert |
| Granularity | One setting for everyone | Chosen per call (write, flush, replay) |
| Standby used | Defined in synchronous_standby_names | Whatever the application is reading from |
| Standby promoted | - | Returns not in recovery immediately |
What the feature is not
The source is explicit about the limitations. The primary confirms the commit before the standby has the data: if the primary 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 just the primary can lose data. For that reason, the application should only report success when the wait returns success, and a timeout should be treated as "unknown result", not as success.
Another point: other sessions can already see the data on the primary before it reaches the standby, but that was also already true with synchronous replication. The guarantee now lives in the application code, not in the server: a developer who forgets to call WAIT FOR LSN simply ends up with plain asynchronous replication, with no warning. Augustine recommends embedding the call in the data access layer, rather than spreading it throughout the code.
There's also a practical effect on the connection pool: a session waiting on the standby holds the connection to the standby for the entire wait. In the author's tests, with 32 clients, the standby pool used an average of 24 of the 32 connections, versus just 7 on the primary. The suggested rule of thumb is to size the standby pool by clients × wait time / total time, which even changes how one should think about read/write splitting.
The tweak that decides whether the feature works
In the first tests, the wait took 200 to 400 ms, because the walsender only sends WAL that's already been flushed to disk on the primary, and with synchronous_commit = off it's the WAL writer process that does that flush, which by default wakes up every wal_writer_delay (200 ms). Investigating the source code (XLogSetAsyncXactLSN() in xlog.c), Augustine found the setting that fixes this:
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(); -- both reload without restartingWith wal_writer_flush_after = 0, every asynchronous commit wakes up the WAL writer and forces an immediate flush. These two settings only affect asynchronous commits, so sessions with synchronous_commit = on don't change behavior. With the default values, the author measured just 108 TPS; after the tweak, more than 2,000 TPS, in the same range as synchronous replication.
What remains open
A warning is worth noting for anyone already thinking about deploying this to production: the author himself recommends waiting for PostgreSQL 19.1 before putting WAIT FOR LSN into production, because version 19.0 has a critical bug related to the pg_current_wal_insert_lsn() function at WAL page boundaries, with no fix included in the initial release. The technical details of the bug, a workaround for those stuck on 19.0, and the full benchmark numbers are left for Part 3 of the series; Part 2 brings a hands-on walkthrough using just psql.
For anyone designing high-availability architecture today, the central takeaway of the post is that the decision of whether to wait for the replica stops being a global server parameter and becomes a per-transaction choice, made in the application code, right where it should always have been made.
Translated from the Brazilian Portuguese original · Read the original
Percona Benchmark Shows Exoscale 3.5 Times Faster Than Scaleway in Managed MySQL
Percona tested managed MySQL from five cloud providers smaller than the hyperscalers, all at the same nominal configuration. The result shows that





