PostgreSQL 19's WAIT FOR LSN lets a read on an asynchronous replica wait until that replica has replayed your write. You get read-your-writes consistency without sending every read to the primary.
The problem: the write succeeded, the read missed it
Read-write splitting is a common way to scale: writes go to the primary, reads spread across asynchronous replicas. It breaks a simple expectation:
- A user renames their profile from Alex to Sam.
- The primary commits. Success.
- The user refreshes, and the read lands on a replica.
- The replica hasn't replayed the change yet, so it returns Alex.
Nothing failed. Replication lag let the user miss their own write. The guarantee that this never happens is called read-your-writes (RYW) consistency.
Why not just read from the primary?
You can, and it's a valid fix. Route reads that must see the latest write to the primary. The cost is load: the more traffic does this, the more you lose the reason you added replicas.
A common variation is sticky primary routing: after a write, read from the primary for a fixed window, say 5 seconds. But replication lag is unpredictable:
- The replica catches up in 20 ms. The primary served almost 5 seconds of reads it didn't need to.
- The replica lags 8 seconds. The window closes at 5 seconds, and reads hit a stale replica anyway.
We're using time to guess replication progress.
What if we waited for actual progress instead?
PostgreSQL 19: WAIT FOR LSN
PostgreSQL replicates by streaming its Write-Ahead Log (WAL). A Log Sequence Number (LSN) is a position in that stream. If you know the LSN of your commit, you know exactly how far a replica must get before it can show your write.
The pattern takes five steps:
- Commit the write on the primary.
- Capture the WAL position right after the commit.
- Send that LSN along with the next read, to a replica.
- On the replica, wait until it has replayed up to that LSN.
- Run the read on the same replica. Its fresh snapshot includes your write.
On the primary, after committing:
SELECT pg_current_wal_insert_lsn(); -- 0/0306EE20 On the replica, before the read:
WAIT FOR LSN '0/0306EE20' WITH (TIMEOUT '200ms', NO_THROW); If the replica has already caught up, the command returns immediately. Otherwise it waits until the LSN is replayed or the timeout expires. With NO_THROW, a timeout returns a status (success or timeout) instead of raising an error, so check the status before running the read.
Run the wait before your transaction takes a snapshot: outside a transaction block, or as its first statement. Earlier versions could do this with application-side polling. PostgreSQL 19 makes it a native command.
Don't make every write wait. Make only the reads that need consistency wait.
Is this a new idea?
Not really. Other databases have tackled the same problem.
MySQL can wait on transactions instead of log positions, using Global Transaction Identifiers (GTIDs):
SELECT WAIT_FOR_EXECUTED_GTID_SET('required-gtid-set', 0.2); The read waits until the replica has executed those transactions, with a 0.2-second timeout.
SQL Server (Always On Availability Groups) has LSNs too, and its sys.dm_hadr_database_replica_states view reports last_hardened_lsn and last_redone_lsn per replica. Synchronous commit guarantees the log is hardened on the secondary. Queries only see a change after redo replays it into the data pages, and redo can lag. There's no directly equivalent built-in wait command, so you would poll that view yourself.
Replication durability and read consistency are different guarantees.
Data can be safe on a replica before a query there can see it.
Where it fits in production
Every approach pays somewhere:
| Approach | Trade-off |
|---|---|
| Primary routing | More load on the primary |
| Sticky primary routing | Assumes replication progress |
| Synchronous replay | Higher write latency |
Replication-aware reads (WAIT FOR LSN) | Read latency and token management |
Replica waits don't replace primary routing. Waiting requests hold database connections, raise tail latency, and need careful timeout and failover handling.
A practical setup combines all three:
- Regular reads go straight to replicas, with no waiting.
- Reads right after a write run
WAIT FOR LSNon a replica first. - If the replica can't catch up in time (
timeout), fall back to the primary.
Takeaway
WAIT FOR LSN doesn't remove replication lag or add a new consistency model. It lets you keep asynchronous replication and make only the reads that need consistency wait, instead of making every write wait.
Read-your-writes is a consistency requirement, not a routing strategy.
Sometimes reading from the primary is the right answer. Sometimes waiting on a replica is the better trade-off. Let the architecture decide.
