Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 6 min read Deep dive mediumAdept

Read your writes: WAIT FOR in PostgreSQL 19

PostgreSQL 19 adds a top-level `WAIT FOR LSN` command that blocks a session until a standby has written, flushed or replayed WAL up to a given LSN, giving read-your-writes consistency on asynchronous replicas without the per-commit cost of `synchronous_commit = remote_apply`. The pattern is to capture `pg_current_wal_insert_lsn()` after a write on the primary, then run `WAIT FOR` on the standby before reading. Options include MODE (standby_replay, standby_flush, standby_write, primary_flush), TIMEOUT and NO_THROW. It must run as a snapshot-free utility command outside functions and above READ COMMITTED, because a held snapshot can stall WAL replay and self-deadlock. The feature took about 10 years and three reverts to land. Timeline changes after promotion are not tracked, and connection poolers and proxies are natural places to inject it transparently.

Assumes: PostgreSQL streaming replicationWAL and LSNsMVCC snapshots

Related posts

  • Don't add a read replica until you've read this

    incident.io mediumAdept

    incident.io moved nearly all Postgres read traffic to a read replica and had to preserve read-after-write consistency without hand-routing each query. Within a request, a Go context flag is tainted on any write or transaction so later reads go to the primary, a simple SELECT-prefix check classifies queries, and a circuit breaker falls back to the primary if the replica is unreachable. Across async boundaries, 0.1–0.5% of queue messages hit NotFound errors because workers ran faster than replication. The fix stamps each message and each mutating API actor (user or API key) with the primary's WAL LSN and checks pg_last_wal_replay_lsn() before reading from the replica. Lagging messages are nacked with exponential backoff, which doubles as backpressure, and replica lag is best monitored in bytes rather than time.

  • PostgreSQL 19 on ClickHouse Managed Postgres, from day one

    ClickHouse mediumAdept

    PostgreSQL 19 has been generally available on ClickHouse Managed Postgres since its November 2026 release day. That was possible because the service runs unforked community Postgres from the PGDG repository, builds VM images from release candidates with Ubicloud, gates the version behind a feature flag, and stress-tests HA failovers in staging. The post then covers the PostgreSQL 19 features that matter most for operators: REPACK CONCURRENTLY and its caveats (not MVCC-safe, a final ACCESS EXCLUSIVE lock, disk space needs), WAIT FOR LSN for read-your-writes on replicas, sequence replication and publication EXCEPT, parallel autovacuum index workers with scoring, and pg_plan_advice. A pgbench run with one synchronous standby showed about 2% higher TPS than PostgreSQL 18, and several defaults changed, including jit off, lz4 TOAST compression and max_locks_per_transaction at 128.

  • Patching the PostgreSQL JDBC Driver

    Zalando Engineering mediumAdept

    Postgres logical replication slots for clients watching low-traffic tables never advance, so the server retains all WAL generated by other tables until disk fills and the node goes read-only. Zalando fixed this upstream in PgJDBC: the driver now tracks the last received and last confirmed replication LSNs, and when they match and a KeepAlive message reports a higher server LSN, it confirms that LSN, safely advancing the slot without skipping relevant events. The fix, merged for PgJDBC 42.7.0, removes the need for the common dummy-write workaround and was validated by comparing WAL size graphs before and after a patched Debezium image rollout across hundreds of event streams.

  • More load is faster: Tuning Postgres read replicas

    ClickHouse hardMaster

    Postgres read replicas fed from archived WAL (rather than streaming replication) show a U-shaped lag curve: idle systems wait up to archive_timeout (60s) for a 16MB segment to fill, while very high write rates make archive upload fall behind. Moderate load gives about 3s lag by default. Setting archive_timeout=5s and wal_retrieve_retry_interval=500ms is a safe starting point. More aggressive options give diminishing returns because each poll costs about 0.8s of WAL-G calls, re-fetching and decompressing the previous segment, and adds cross-AZ network cost with synchronous HA. These options include sub-second pg_switch_wal() loops and a persistent WAL-G daemon client. Streaming replication still wins on lag but brings VACUUM, slot and failover hazards.

  • Everyone gets faster writes: We turned off FPW's in Neon

    Neon hardMaster

    Neon disabled Postgres full-page writes fleet-wide because its stateless compute streams WAL to a Paxos safekeeper quorum and has no local disk pages that can tear. To keep delta chains bounded without FPW images, the pageserver now generates page images itself once a page accumulates more deltas than a threshold, rather than relying on the checkpoint cycle. HammerDB TPROC-C throughput rose up to 4.5x on 32 vCPU, and WAL per transaction fell from 58KB to under 4KB (94%). In production, p99 read latency dropped 30-50%, and the change was rolled out without restarts via the XLOG_FPW_CHANGE WAL record.

The quest for software engineering knowledge.

Look:
Layout: