Tolgahan Bora,PostgreSQL 19 read-your-writes consistency

PostgreSQL 19 Can Wait for Your Write—but Your Read Router Still Has Work to Do

A user saves a setting, refreshes the page, and sees the old value. The write succeeded; the read landed on an asynchronous standby that had not replayed it yet. PostgreSQL 19, released on November 19, 2026, adds WAIT FOR LSN to make that gap manageable without making every write synchronous. The useful change is not that replicas become instantly consistent. It is that an application can ask a particular standby to catch up before a particular read. The engineering decision is where to carry that requirement, how long to wait, and what to do when the standby cannot satisfy it. (postgresql.org)

The guarantee is narrower than “fresh reads”

In a physical streaming-replication setup, the primary records changes in its write-ahead log, or WAL. An asynchronous standby receives and replays that log independently. A successful commit on the primary does not mean every standby can immediately show it. (postgresql.org)

Read-your-writes means a client can observe its own committed changes on a later read. It does not mean the read returns an immutable copy of the row as the client wrote it: another transaction may have changed the row since. Nor does it make all replicas globally current, make a cache fresh, or protect acknowledged writes from loss during an asynchronous failover.

Think of the LSN, or log sequence number, as a lower-bound marker. The read path asks: has this standby replayed at least the WAL position associated with my write? PostgreSQL 19 can wait for that condition on the server instead of requiring the application to poll for it. (postgresql.org)

Capture the position after the commit

The ordering matters. On the primary, commit the write first, then obtain the WAL insertion position:

-- On the primary
BEGIN;
UPDATE account_settings
SET display_name = 'Ada'
WHERE account_id = 42;
COMMIT;

SELECT pg_current_wal_insert_lsn();
-- Example result: 0/0306EE20

The application retains the returned LSN as a consistency token for the later read. Obtain it from the same primary after the commit succeeds—not inside the transaction before COMMIT. The post-commit position is at or beyond the end of the relevant commit record. PostgreSQL's example uses pg_current_wal_insert_lsn() even when synchronous_commit could be off. (postgresql.org)

This is a safe lower bound, not necessarily the exact LSN of that transaction. Other transactions may insert WAL before the position is sampled. A busy primary can therefore give you a marker later than strictly necessary, making the standby wait for extra replay. If the commit succeeds but fetching the marker fails, do not blindly retry the write: its outcome may already be committed. Route the follow-up to a verified primary or use an application-specific recovery path.

Wait on the standby that will serve the read

Pass the token to the selected standby and wait before querying:

-- On the selected physical standby
WAIT FOR LSN '0/0306EE20'
  WITH (MODE 'standby_replay', TIMEOUT '100ms', NO_THROW);
-- Continue to the read only if status is 'success'.

SELECT display_name
FROM account_settings
WHERE account_id = 42;

The LSN is illustrative; use the value returned after your own commit. standby_replay is the default, but spelling it out makes the intent clear. standby_write and standby_flush are not substitutes for this read guarantee: receiving or flushing WAL does not necessarily mean the change has been applied and is visible. (postgresql.org)

With NO_THROW, inspect the returned status. success permits the read; timeout and not in recovery do not. Other errors can still be raised. Without a timeout—or with a timeout of zero—the wait can be indefinite. The 100 ms above is an example budget, not a recommended universal value. (postgresql.org)

The SQL is not the whole implementation. The wait and read must reach the same standby server. Pin the connection or otherwise ensure your pooler and load balancer cannot send the next statement to a different replica. If a connection fails after the wait, repeat the wait on whichever standby will actually serve the retry.

WAIT is a top-level command, not a function to wrap inside a SQL procedure. Run it outside a transaction or as the first statement of the read transaction, before taking locks or a snapshot. This is especially important for REPEATABLE READ and SERIALIZABLE: an already-held snapshot cannot simply become newer after replay advances. (postgresql.org)

Choose a token and fallback policy before rollout

I would start with one flow where stale data is visibly confusing, such as saving a profile and immediately loading it again. Return the write result directly when that answers the next screen; use a consistency token when the client must make a separate read.

For a token-based path, make these choices explicit:

If generating a WAIT statement dynamically, validate the LSN format and use a driver-supported safe SQL-literal mechanism; do not concatenate an arbitrary request value into SQL. Test the exact behavior of your driver and pooler, including its routing of top-level commands.

Failover changes the question

A successful numeric LSN comparison is not proof that the intended WAL history survived a failover. PostgreSQL documents that WAIT compares LSNs, not timelines; a standby following a new timeline can pass the same numeric position for different history. A promoted standby may also return not in recovery while a wait is pending. (postgresql.org)

Treat a topology or timeline change as a consistency-token boundary. Validate the expected history using your failover system, or stop honoring the old token until the write's presence is established. Falling back to the new primary is not automatically sufficient: asynchronous replication may have lost the acknowledged write. If the system cannot establish its presence, surface uncertainty or reconcile using an application-level operation identifier. (postgresql.org)

When a simpler design wins

For a small number of sensitive reads, reading from the primary after a write may be easier to operate. WAIT FOR LSN becomes more attractive when replica reads matter, the application can propagate a token reliably, and the team can tolerate bounded waiting and fallback complexity.

Synchronous replication with synchronous_commit = remote_apply is another option, but it moves the wait to commit and applies to the configured synchronous standbys; an arbitrary replica is not automatically covered. Choose it for the durability and visibility contract you need, not merely to avoid adding a token. (postgresql.org)

Before production, test a normal catch-up, a delayed standby, a timeout, a pooler reroute, a lost connection, a concurrent write, and a failover with a potentially missing write. The acceptance criterion is not “the wait succeeded in a demo.” It is that the application never claims this guarantee when it cannot establish it.

FAQ

Does WAIT FOR LSN eliminate replica lag?

No. It waits for a chosen WAL position; it does not make replication instantaneous or guarantee that later writes have been replayed.

Can I use this with logical replication?

The pattern described here is for physical standbys replaying the primary's WAL. Do not assume a publisher LSN maps to subscriber visibility under logical replication without a separately documented mechanism.

Should every read carry a token?

Not necessarily. Apply it where the product requires read-your-writes, and allow explicitly eventual reads elsewhere. Measure the latency and fallback cost before expanding the policy.