Skip to content
Planet PostgreSQL·· 1d agoFeatured

PostgreSQL 19 DBA Walkthrough, Part 2: Read Your Writes with WAIT FOR LSN

Jobin Augustine: Stop Fighting Synchronous Replication: Meet WAIT FOR LSN – Part 2 – walkthrough for DBAs

Brief · Based on the source excerpt

A two-session psql walkthrough shows how PostgreSQL 19 uses WAIT FOR LSN to read your writes on an asynchronous standby. It captures the WAL insert LSN on the same primary connection after commit and also covers timeouts, monitoring waiters, and a stalled standby.

Why It Matters

When reads from an asynchronous standby must reflect a just committed write, the application can wait before reporting success without adding that wait to the primary’s synchronous commit path.

Technical Breakdown · Distilled from the original source

Commit on the Primary, Then Wait on the Standby

  • The walkthrough starts with a read-your-writes problem: even after the primary commits a write, a direct query on the asynchronous standby may not see it yet. The walkthrough contrasts that read with one guarded by WAIT FOR LSN.
  • After committing on the primary, use the same primary connection to call pg_current_wal_insert_lsn() and capture the WAL insert position. Run WAIT FOR LSN on the standby before telling the user the operation is done. Two psql sessions are enough to try the sequence without writing an application.

Test Setup and Stalled Standby Comparison

  • The captured output comes from PostgreSQL 19.0 with one primary on port 5433 and one asynchronous streaming standby on port 5434. The waitforlsn directory contains a docker-compose.yml for recreating that setup.
  • The initial standby check runs SELECT pg_is_in_recovery(), pg_last_wal_replay_lsn();. The example returns t and 0/0E981C38, recording the recovery state and WAL replay position before the write exercise.
  • The walkthrough also sets out to compare synchronous replication and WAIT FOR LSN when a standby stalls, and to cover timeouts, monitoring waiting sessions, errors, and restrictions. The excerpt ends at the start of the primary write step, before it shows the results of those exercises.
Source: Planet PostgreSQL· postgr.es

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference