WAIT FOR LSN in PostgreSQL 19, a 3-part series
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. PostgreSQL generates a new LSN (Log Sequence Number) wherever something is written to WAL. A facility to wait for LSN to reach standby is impactful because it can change the way applications are designed. It is the only feature in which I personally spend time and effort in testing and participating in hackers mailing lists with minor problems before the release of PostgreSQL 19.
A word of caution: You may start designing and developing your application around this feature. But I suggest waiting for PostgreSQL 19.1 before going for production, because a critical fix is not included in 19.0. The details, and a workaround for 19.0, are explained in Part 3 of this series.
For many years, whenever a customer wanted “the data should not be lost” or “the user must see what he just saved, even if the read goes to a replica”, the answer was mostly one: synchronous replication. And many of those customers came back to us later with a different problem: “the whole database froze”, “all commits are hanging”, “we had an outage because the standby went down”. With PostgreSQL 19, we finally have one more option. The application itself can decide when and where it wants to wait.
The content became too big for a single post, so I split it into three. In this first part, I will explain the problem, the feature, and how it can be used as a replacement for synchronous replication. Part 2 shows how DBAs can play with it using just psql, and the long history behind it. Part 3 has the numbers from my tests on the PostgreSQL 19.0 release, and the bugs I found on the way.
Let me first explain the problem we are trying to solve. Synchronous replication is a good feature, but it comes with a cost, and the cost is not only performance.
|
1 2 |
<span style="color: #000000;">WARNING: canceling wait for synchronous replication due to user request DETAIL: The transaction has already committed locally, but might not have been replicated to the standby.</span> |
Generally the requirement of an application is NOT like “Primary connection needs to wait until the transaction reaches standby on every transaction commit”. What they need is either:
Both of these are decisions which only the application can make correctly. And that is exactly what WAIT FOR LSN gives.
It is a new SQL command in PostgreSQL 19 (the documentation lists it under the name WAIT):
|
1 2 3 4 5 6 |
<span style="color: #000000;">WAIT FOR LSN 'lsn' [ WITH ( option [, ...] ) ] -- where option can be: -- MODE 'standby_replay' | 'standby_write' | 'standby_flush' | 'primary_flush' -- TIMEOUT 'timeout' -- integer milliseconds, or with units: '100ms', '5s' -- NO_THROW -- return a status instead of raising an error</span> |
The command just waits until the server reaches the given WAL position (LSN), and then returns one row with a status column. The value is success, timeout or not in recovery.
The MODE decides what “reached” means:
| MODE | Runs on | Returns success when | What it guarantees |
|---|---|---|---|
| standby_replay (default) | Standby | WAL is replayed (applied) | The change is visible on the standby. This is read-your-writes |
| standby_flush | Standby | WAL is fsynced on the standby | The change is durable on the standby |
| standby_write | Standby | WAL is written to the OS, not yet fsynced | Survives a standby PostgreSQL crash, but not an OS crash |
| primary_flush | Primary | WAL is flushed on the primary | Useful along with synchronous_commit = off |
A few points to keep in mind from day one:
The idea is very simple:

Why pg_current_wal_insert_lsn() and not pg_current_wal_flush_lsn() ? If you run with synchronous_commit = off (I’ll explain below why you will want to), the commit record may not be flushed yet when COMMIT returns. So the flush LSN taken right after the commit can be behind your commit record, and waiting for it can return success too early. The insert position is always at or after the end of your commit record, so it is safe. The PostgreSQL documentation also recommends this. (On 19.0 there is one catch with this function at WAL page boundaries. See Part 3.)
One more trap: don’t send the write and the SELECT pg_current_wal_insert_lsn() as one multi-statement string (simple query protocol). That runs as one implicit transaction, and you will get the LSN from before the commit.
Here is the interesting part. You can combine WAIT FOR LSN with synchronous_commit = off on the primary:
That means “async commit + WAIT FOR standby_flush” gives you a durability guarantee which is stronger than plain synchronous_commit = on without a sync standby (local flush only), and similar to synchronous replication with synchronous_commit = on . With standby_replay, it is similar to remote_apply, and you get read-your-writes too.
Let me put the comparison in a table:
| Synchronous replication | Async replication + WAIT FOR LSN | |
|---|---|---|
| Who waits | Every committing session, decided by server config | Only the transactions where the application asks for it |
| Standby down / very slow | Commits hang forever (statement_timeout doesn’t help) | The primary keeps running. The wait returns timeout after the time you chose |
| What to do on a problem | Cancel it, and get “already committed locally” | The app gets a status and decides: retry, read from the primary, alert, or mark the result as unconfirmed |
| Which guarantee | One setting for everyone (on, remote_write, remote_apply) | Chosen per call: standby_write, standby_flush, standby_replay |
| Which standby | Defined in synchronous_standby_names, managed by the HA tool | Whichever standby the application reads from |
| HA tool complexity | Must manage synchronous_standby_names | Nothing special: plain async replication |
| Promoted standby | — | Returns not in recovery immediately, so the app knows the topology changed |
I don’t want to hype this feature up too much and cause unrealistic expectations. Please understand the differences before you drop synchronous replication:
When I started testing, the first numbers were terrible. With default settings, the WAIT FOR part was taking around 200–400 ms. The reason is what I explained above: the walsender sends only flushed WAL, and with synchronous_commit = off the flushing is done by the WAL writer process, which wakes up every wal_writer_delay (default 200ms).
Reading XLogSetAsyncXactLSN() in xlog.c gave the answer. If wal_writer_flush_after = 0 , every async commit wakes up the WAL writer, and it flushes right away. So these two settings on the primary are very important for this pattern:
|
1 2 3 |
<span style="color: #000000;">ALTER SYSTEM SET wal_writer_flush_after = 0; -- default 1MB ALTER SYSTEM SET wal_writer_delay = '10ms'; -- default 200ms SELECT pg_reload_conf(); -- both are reloadable, no restart needed</span> |
Note that these settings affect only async commits, so sessions using synchronous_commit = on are not affected. With the defaults, I measured only 108 TPS. After this tuning, it was above 2,000 TPS, in the same range as synchronous replication. The full numbers are in Part 3.
WAIT FOR LSN moves the “wait for the standby” decision from the server configuration into the application, where it belongs. That gives us:
But remember the homework:
This feature took more than six years and many people to reach this shape. I tell that story, and thank everyone involved, at the end of Part 2.
Next in this series: Part 2: Hands-on with WAIT FOR LSN: a psql walkthrough for DBAs. You will see read-your-writes break and get fixed, a stuck standby with and without synchronous replication, and all the restrictions, with actual outputs from PostgreSQL 19.0.
Resources
RELATED POSTS