Engineering
PostgreSQL 19 Internals: REPACK (CONCURRENTLY), pg_plan_advice, WAIT FOR LSN & Benchmarks
Released on November 19, 2026, PostgreSQL 19 brings online table repacking into core with REPACK (CONCURRENTLY), introduces deterministic planner constraints via pg_plan_advice, adds standby read-your-writes with WAIT FOR LSN, and flips jit to off by default. Here is how the internals work, what independent tests show, and which caveats matter before upgrading.
8 min read
PostgreSQL 19 Internals: REPACK (CONCURRENTLY), pg_plan_advice, WAIT FOR LSN & Benchmarks
Executive Summary
On November 19, 2026, the PostgreSQL Global Development Group released PostgreSQL 19.0, introducing major operational and query-engine primitives that previously required external extensions or downtime. For database engineers and distributed-systems architects, three core additions stand out:
- Online Bloat Reclamation in Core (
REPACK (CONCURRENTLY)): ReplacesVACUUM FULLandCLUSTERwith a unifiedREPACKcommand that rewrites bloated tables under aSHARE UPDATE EXCLUSIVElock while capturing concurrent writes via logical decoding—taking anACCESS EXCLUSIVElock only for the final swap. - Planner Constraints (
pg_plan_advice&pg_stash_advice): Ships a native contrib module for constraining scan methods, join order, and parallelism, alongside query-ID stashing—though November 25–26 testing confirms a single misapplied nested-loop hint can slow a query ~117x (39.5 ms to 4,629 ms). - Standby Read-Your-Writes & Safer Defaults: Adds
WAIT FOR LSNfor bounded replica consistency, disablesjitby default (off), switches default TOAST compression tolz4, enableslog_lock_waitsby default, and delivers 22,814 TPS vs. PostgreSQL 18's 22,299 TPS (~2% higher) in ClickHouse's 500 GB PostgresBench synchronous-standby run.
Confidence level: High for all SQL syntax, locking constraints, GUC defaults, and benchmark figures verified against official PostgreSQL 19 documentation and published November 2026 engineering benchmarks.
The Bottleneck & Scale Constraints
Why Legacy Table Maintenance, Plan Flips & Replica Lag Broke at Scale
As PostgreSQL adoption scaled across multi-terabyte OLTP clusters, three long-standing architectural friction points forced teams into brittle workarounds:
- Table Bloat vs. Lock Contention: Heavy
UPDATE/DELETEworkloads leave dead tuples that standardVACUUMmarks reusable without shrinking the physical disk file. Historically, returning space to the OS requiredVACUUM FULLorCLUSTER, both of which held anACCESS EXCLUSIVElock for the entire rewrite—blocking all reads and writes for hours and forcing teams to install the third-partypg_repackextension. - Query Plan Instability: When correlated columns skew cardinality estimates, the PostgreSQL planner can flip from a hash join to a catastrophic nested loop. Without native plan-pinning, teams either rewrote SQL with
OFFSET 0/ CTE fences or maintained out-of-tree hint extensions. - Read-Replica Staleness &
jitLatency Spikes: Applications routing reads to asynchronous standbys lacked a built-in SQL primitive to wait for a specific Write-Ahead Log (WAL) Log Sequence Number (LSN) without enabling expensivesynchronous_commitglobally. Meanwhile,jit = onby default caused unpredictable compilation latency spikes on medium-cost OLTP queries.
Inside the Solution: Architecture & Internals
1. How REPACK (CONCURRENTLY) Rewrites Tables Live
According to the PostgreSQL 19 REPACK documentation and ClickHouse's November 24 engineering breakdown, REPACK (CONCURRENTLY) coordinates a four-phase online rewrite using a dedicated logical replication slot pool (max_repack_replication_slots):
flowchart LR
A[Acquire SHARE UPDATE EXCLUSIVE<br/>Reads & Writes Continue] --> B[Create Logical Replication Slot<br/>max_repack_replication_slots]
B --> C[Copy Live Tuples & Rebuild Indexes<br/>Track Progress: pg_stat_progress_repack]
C --> D[Replay Captured Concurrent Changes<br/>Up to ~104M Updated/Deleted Rows]
D --> E[Brief ACCESS EXCLUSIVE Swap<br/>Use lock_timeout to Prevent Queueing]Safe operational SQL patterns for REPACK and WAIT FOR LSN:
REPACK (CONCURRENTLY, VERBOSE) orders;
REPACK orders USING INDEX orders_created_at_idx;
SELECT pg_current_wal_insert_lsn();
WAIT FOR LSN '0/306EE20' WITH (TIMEOUT '2s', NO_THROW);
2. Hard Limits & Lock Caveats in REPACK (CONCURRENTLY)
As ClickHouse's PostgreSQL 19 analysis and the official REPACK manual emphasize, CONCURRENTLY is not magic—it carries strict operational boundaries:
- Not MVCC-Safe for Older Snapshots: Once the repack commits, transactions whose snapshots predate the repack start will see the repacked table as empty.
- 104-Million-Row Change Ceiling: Concurrent updates/deletes require a small fixed memory allocation per tuple (independent of
maintenance_work_mem); if more than ~104 million rows are updated or deleted during the run,REPACKaborts. - Unsupported Relations:
CONCURRENTLYcannot run on partitioned tables,UNLOGGEDtables, materialized views, system catalogs, TOAST tables, non-heap access methods, or tables lacking a primary key / index-based replica identity. - Disk Space & Wraparound:
REPACKwrites a full copy of the table and all indexes (plus a temporary sort file up to table size ifUSING INDEXsorts). It also holds an activebackend_xid, so it must not be used as a substitute forVACUUMwhen approaching transaction ID wraparound.
3. pg_plan_advice & pg_stash_advice Internals
Shipped as a core contrib module, pg_plan_advice emits a compact constraint language via EXPLAIN (PLAN_ADVICE) (e.g., JOIN_ORDER(c o), HASH_JOIN(o), SEQ_SCAN(s o), NO_GATHER(o s)) and enforces supplied constraints via pg_plan_advice.advice. Its companion pg_stash_advice maps normalized query identifiers (ignoring literal constants) to advice strings in shared memory and persists them to pg_stash_advice.tsv every 30 seconds by default.
Importantly, pg_plan_advice constrains candidate plans rather than replacing the planner—it cannot force a path the planner refused to consider, and it does not cover aggregations or set operations (UNION).
Verified Results, Benchmarks & Default Shifts
PostgreSQL 19 vs. PostgreSQL 18 Benchmarks & Planner Tests
In ClickHouse's side-by-side PostgresBench 500 GB run (pgbench TPC-B-like workload, 256 clients, 16 vCPU, 64 GiB RAM, 1 synchronous standby), PostgreSQL 19 averaged 22,814 TPS compared to PostgreSQL 18's 22,299 TPS (~2% higher, within run-to-run variance), while PostgreSQL's official release notes report nearly 2x faster bulk inserts on foreign-key-heavy workloads due to optimized FK checks.
Meanwhile, independent local testing of pg_plan_advice on PostgreSQL 19.0 by NiharDaily (November 25–26, 2026) quantified both the hazard of naive plan pinning and the value of fixing statistics first:
| Benchmark / Test Scenario | Configuration / Condition | Measured Result | Engineering Takeaway |
|---|---|---|---|
| PostgresBench 500 GB (1 Sync Standby) | 256 clients, 16 vCPU, 64 GiB RAM | 22,814 TPS (PG 19) vs. 22,299 TPS (PG 18) | ~2% gain; stable parity at GA |
| Correlated 3-Column Join (1M × 50K) | Planner default hash join | 39.5 ms (0 rows removed by filter) | Planner overcame 1-row misestimate |
Same Join with NESTED_LOOP_PLAIN(s) |
Advised nested loop (matched) |
4,629 ms (99,998,000 rows filtered) | ~117x slower; PG obeys bad advice |
Extended Statistics (dependencies, mcv) |
STATISTICS 1000 + ANALYZE |
Estimate 1,963 vs. 2,000 actual | Fixes root cause across all queries |
| Stashed PK Lookup Overhead (4 clients) | pg_stash_advice active vs. off |
77,297 vs. 74,414 lookups/sec | Within run noise on simple PKs |
Critical Configuration & Default Changes in PostgreSQL 19
| Parameter / Feature | PostgreSQL 18 Default | PostgreSQL 19 Default | Upgrade / Operational Impact |
|---|---|---|---|
jit |
on |
off |
Eliminates surprise LLVM JIT latency spikes on OLTP queries |
default_toast_compression |
pglz |
lz4 (when built with LZ4) |
Faster compression/decompression for wide JSONB/text columns |
log_lock_waits |
off |
on |
Automatic visibility into lock contention without manual tuning |
max_locks_per_transaction |
64 |
128 |
Lock allocation changed; double prior custom values to preserve capacity |
| Async I/O Workers | io_workers (static) |
io_min_workers / io_max_workers |
Dynamic worker scaling when io_method = worker |
| Authentication | md5 deprecated |
md5 warnings; RADIUS removed |
Migrate legacy clients to scram-sha-256; SNI via pg_hosts.conf |
Practical Takeaways for Your Stack
1. Replace pg_repack Gradually—With lock_timeout Guardrails
If you run REPACK (CONCURRENTLY) in production, always set a short lock_timeout (e.g., SET lock_timeout = '2s';) so the final ACCESS EXCLUSIVE swap aborts cleanly instead of piling application queries behind a long-running analytical transaction. Verify you have >2x table + index free disk space before starting.
2. Fix Planner Statistics Before Reaching for pg_plan_advice
Never treat pg_plan_advice as a substitute for ANALYZE or CREATE STATISTICS. Only pin trimmed constraints (JOIN_ORDER, HASH_JOIN) via pg_stash_advice after reproducing the regression on production-like row counts, and pair every stash entry with a review date—because SQL text changes alter the query identifier and silently orphan stashed rules.
3. Combine WAIT FOR LSN with Explicit Timeouts and Primary Fallback
Use WAIT FOR LSN '<lsn>' WITH (TIMEOUT '2s', NO_THROW) only on user-facing read-after-write paths (such as profile updates or order confirmations) routed to read replicas, and always code an explicit fallback to the primary if replication lag exceeds your timeout budget.
4. Audit PostgreSQL 19 Breaking Defaults Before Upgrading
Before cutting over from PostgreSQL 17 or 18:
- Remove deprecated
io_workersfrompostgresql.confand configureio_min_workers/io_max_workers. - Double any custom
max_locks_per_transactionsetting to match PostgreSQL 19's revised lock-table allocation. - Verify no legacy systems depend on RADIUS authentication (removed) or silent
md5logins (now emits warnings unlessmd5_password_warnings = off). - Note that logical replication of sequences (
FOR ALL SEQUENCES/REFRESH SEQUENCES) requires a PostgreSQL 19+ publisher, so it will not automate sequence syncing when upgrading from 17 or 18 into 19.
Frequently Asked Questions
Does REPACK (CONCURRENTLY) completely eliminate locks in PostgreSQL 19?
No. While the table copy and index rebuild run under a SHARE UPDATE EXCLUSIVE lock (allowing concurrent reads and writes), REPACK (CONCURRENTLY) still requires a brief ACCESS EXCLUSIVE lock for the final table swap. Always set a lock_timeout to prevent queueing behind long-running queries.
Can I run REPACK (CONCURRENTLY) on a partitioned table in PostgreSQL 19?
No. PostgreSQL 19's REPACK documentation specifies that CONCURRENTLY is not supported on partitioned tables, UNLOGGED tables, materialized views, system catalogs, or tables without a primary key or index-based replica identity. Running REPACK without CONCURRENTLY on a partitioned table repacks each partition with an ACCESS EXCLUSIVE lock.
Why did PostgreSQL 19 turn jit off by default?
The PostgreSQL Global Development Group disabled jit by default because the cost estimates PostgreSQL used to trigger LLVM compilation proved unreliable on mixed OLTP workloads, often adding compilation overhead that slowed queries down. Analytical deployments with heavy, long-running scans can still re-enable jit = on.
How does pg_plan_advice differ from traditional SQL query hints?
pg_plan_advice constrains the planner's existing search space (using tags like JOIN_ORDER, HASH_JOIN, and INDEX_SCAN) rather than overriding the planner outright. It cannot force a plan the planner never considered, and as November 2026 benchmarks showed, forcing a poor join method can still degrade latency by over 100x.
Does WAIT FOR LSN work for read-your-writes when migrating from PostgreSQL 18?
WAIT FOR LSN is a native PostgreSQL 19 standby command, so the replica receiving the WAIT command must be running PostgreSQL 19. Because replicas can still lag behind the primary during network or storage spikes, always include TIMEOUT and NO_THROW options with a primary-read fallback.
Sources
- PostgreSQL Global Development Group (Primary): PostgreSQL 19 Released! (Nov 19, 2026)
- PostgreSQL 19 Documentation (Primary): PostgreSQL 19.0 Release Notes,
REPACKSQL Reference, &pg_plan_adviceModule Docs (Nov 19, 2026) - ClickHouse Engineering: PostgreSQL 19 on ClickHouse Managed Postgres, from day one (Nov 24, 2026)
- NiharDaily: PostgreSQL 19
pg_plan_advice: pin query plans, tested (Nov 25–26, 2026)