PostgreSQL 19 Adds WAIT FOR LSN for Consistent Reads on Standbys
PostgreSQL 19 introduces the WAIT FOR LSN command, enabling 'read-your-writes' consistency on standby servers. This lets applications reliably read their own recent writes from replicas, closing a key capability gap for large-scale and highly available deployments.
What changed?
PostgreSQL 19 introduces a new SQL command, WAIT FOR LSN, that enables a session on a standby server to wait until the standby has replayed the write-ahead log (WAL) up to a specific log sequence number (LSN). This addition allows applications to achieve 'read-your-writes' consistency, so a client can read data it just wrote to the primary once the data is available on a standby. If a timeout is specified and the LSN is not reached in time, an error is returned.
Why does it matter to an everyday developer?
Previously, developers building applications that read from PostgreSQL standbys could not easily ensure that queries would see their own recent writes. This consistency gap created surprises such as users not seeing their most recent changes if the standby lagged behind the primary. The new WAIT FOR LSN command addresses this by allowing applications to track the primary's LSN after write operations and wait for the same data to become available on a replica. This improves consistency for workloads that scale reads over high-availability clusters, matching behavior found in large deployments such as Wikipedia's MediaWiki platform.
What can the developer do now?
Developers can implement 'read-your-writes' guarantees in applications that distribute reads to PostgreSQL replicas. After each write, use pg_current_wal_insert_lsn() to get the current LSN. Store this value—such as in a session object or signed cookie. On subsequent reads from a standby, issue a WAIT FOR LSN command with the saved LSN (optionally with a short timeout) before running SELECT queries. This ensures users read data at least as recent as their last write on the primary.
UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
SELECT pg_current_wal_insert_lsn();