AIARTICLE

PostgreSQL 19 arrives with WAIT FOR LSN to solve the problem of outdated replicas

PostgreSQL 19 arrives with WAIT FOR LSN to solve the problem of outdated replicas
Image: Alan Andrade
Article · AI
Reading
5 min
Code
2
In short

Released in November 2026 after a two-month delay caused by a wave of AI-assisted vulnerability reports, PostgreSQL 19 brings the WAIT FOR LSN command, which lets an application guarantee that it has just read, on a replica, the data it itself wrote to the primary database.

PostgreSQL 19 was released on November 19, 2026, according to a report by Simon Willison on his blog. It is a release outside the usual calendar: the project typically ships its annual version in September, but this time the team had to deal with a flood of vulnerability reports generated with the help of AI tools, which pushed the release back by two months.

That detail is not a footnote. Open source projects for critical infrastructure have been facing a growing volume of automated disclosures, many of them low quality, which consume the security team's triage time before any new feature work can move forward. For anyone running a database in production, it's a reminder that the release cycle of the tools you depend on is also affected by this noise.

The problem read-your-writes solves

The most relevant addition in this version, in Willison's reading, targets directly those who split reads across replicas (standbys) and writes to the primary database. It's a common pattern in architectures that scale reads horizontally: the application writes to the primary and sends most queries to one or more replicas, easing the load on the main node.

The problem is asynchronous replication. Between the UPDATE on the primary and the moment the replica replays that write-ahead log (WAL), there's a delay window. If the very user who just saved a piece of data then makes a read that lands on a lagging replica, they see the old version: they edit a profile and, upon reloading the page, the field reverts to its previous value. It's a classic UX bug in systems with read replicas.

How WAIT FOR LSN works

PostgreSQL 19 tackles this with the WAIT FOR LSN command, which makes a session on the replica wait until the WAL has been replayed up to a specific point, identified by an LSN (log sequence number), before running the next query. In practice, the flow described by Willison works like this on the primary, right after the write:

The excerpt from Simon Willison's article shows the WAIT FOR LSN command with the log sequence number value and the 0.1 second TIMEOUT clause
The excerpt from Simon Willison's article shows the WAIT FOR LSN command with the log sequence number value and the 0.1 second TIMEOUT clause. Reprodução: simonwillison.net.
sql
UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
SELECT pg_current_wal_insert_lsn();

The function pg_current_wal_insert_lsn() returns a value like 0/0306EE20. The application stores this value (a signed cookie is Willison's suggestion) and, on the next request, before querying the replica, sends:

sql
WAIT FOR LSN '0/0306EE20' WITH (TIMEOUT '0.1s');
SELECT * FROM movie WHERE genre = 'Drama';

The TIMEOUT is optional, but it's the part that matters for anyone running this in production: without it, an abnormally lagging replica blocks the session indefinitely. With it, the application gets an error if the replica doesn't reach the requested LSN within the deadline, and can decide what to do: retry, fall back to the primary, or simply show a notice that the data is still syncing.

A pattern Wikipedia already solved by hand

Willison notes that this pattern isn't new: he first encountered it in MediaWiki, the software behind Wikipedia, which runs on MySQL and implements the same idea manually. MediaWiki saves the primary binlog position in the user's session and, at the start of each request, waits for the replica to reach that position before doing any read on it.

MediaWiki's code today uses MySQL's WAIT_FOR_EXECUTED_GTID_SET() function, with a fallback to SOURCE_POS_WAIT() on older setups. In other words: teams that needed this guarantee had been reimplementing, each in their own way, a mechanism that PostgreSQL now offers as a native command. That reduces the infrastructure code each application had to maintain and test on its own.

When this changes your architecture (and when it doesn't)

The difference between the native solution and the homegrown workaround matters for whoever decides your data stack. Before PostgreSQL 19, guaranteeing read-your-writes on a replica required one of three paths: always forcing post-write reads to the primary (losing the benefit of scaling reads), implementing your own LSN-tracking system in the application, or accepting eventual inconsistency and absorbing the cost in UX.

In short: WAIT FOR LSN moves this logic inside the database, with a few-line API, configurable timeout, and no need for application code replicating what MySQL required reimplementing.

It's not something every application needs. If your system doesn't use read replicas, or if the replication lag window is irrelevant to the use case (an analytics dashboard, for instance, can tolerate seconds of delay without issue), the command doesn't change anything in your day-to-day. It matters for those who already suffer from the classic "saved and vanished" symptom: forms, shopping carts, social feeds, and any flow where the author of the write is the first reader of it.

There's also an operational caution worth noting: WAIT FOR LSN adds network and waiting latency on the replica every time it's called, so it's not something to use on every query by default. The right choice is to apply it selectively, at the points where the user has just written and needs to see their own result, letting the rest of the read traffic proceed without this extra check.

For those who administer production databases with replicas, the practical recommendation is to map the endpoints that suffer from this problem today (usually via a manual workaround, like forcing reads to the primary for a few seconds after a write) and evaluate migrating to WAIT FOR LSN with a short timeout, testing the fallback behavior when the replica can't keep up.

Translated from the Brazilian Portuguese original · Read the original

View profile →