Review
Add a pg_wal_preallocate() SQL function to eagerly create future WAL segments
- Proposal version
- 11
- CommitFest entry
- 7052
- Base commit
- 1c4628d0b3c5d38b7a965f686aa69fe579038b3d
Review: pg_wal_preallocate() (v11)
Reviewed v11, posted on 2026-11-20 and described in the thread as a rebase. Compared with v10, the only change is that the TAP test was renumbered from 061 to 062. The patch adds one superuser-only (by default) SQL function, pg_wal_preallocate(bytes bigint DEFAULT NULL) returns bigint. It creates the missing future WAL segment files ahead of the current insert location and returns how many it created.
Nothing from this thread is committed on master yet. A search of the master log for the thread's first Message-ID (plain and URL-encoded) and for the function name finds nothing, so the whole patch is reviewed.
Applying, building, testing
The patch applies cleanly with git am on current master (1c4628d0b3c5). Built with meson, cassert, debug and injection points. The setup, regress, isolation, recovery and pg_rewind suites pass (77 of 77), including the new recovery/062_wal_preallocate.pl (8 subtests). No catversion bump is included. That is right for a posted patch, but the committer has to add it, since pg_proc changes. The new OID 9888 is in the development range, as expected.
Motivation and design
The motivation is clear and backed by numbers in the thread. When the pool of future segments is empty (right after initdb, or before a bulk load or benchmark), foreground backends pay for open, zero-fill and fsync of each new segment. Benchmarks posted in the thread show 12-25% faster pgbench initialization on ext4, and no gain on CoW file systems with wal_init_zero off. The documentation now says this.
The main design alternative raised was automatic, heuristic preallocation by the checkpointer or walwriter. The thread treated it as complementary rather than a replacement: heuristics need history, and this targets cases with none. That seems right to me, and a function is a reasonable first step. The "force" escape hatch was removed, so a call is now capped at max_wal_size worth of segments. max_wal_size is PGC_SIGHUP, so a session cannot raise the cap. Repeated calls are idempotent, because existing segments are skipped. In my tests a second identical call returned 0 and pg_wal did not grow.
Catalog, on-disk and protocol compatibility: there is one new pg_proc row and no change to the WAL format, control file or protocol. The preallocated files are ordinary future segments. They are not archived, and pg_rewind removes them, as was checked in the thread.
Comments
-
Parallel safety. The pg_proc entry takes the default proparallel 's'. With debug_parallel_query = on, the function runs inside a parallel worker, behind a single-copy Gather. pg_switch_wal() and pg_create_restore_point() have the same marking, so there is precedent. Still, a function whose only purpose is a file-system side effect would be better marked 'r' (like pg_backup_start()) or 'u', so that it always runs in the leader.
-
The no-argument form depends on min_wal_size. Nothing stops min_wal_size from being larger than max_wal_size. With 128MB and 64MB, a bare pg_wal_preallocate() fails with "WAL preallocation request exceeds max_wal_size", which is confusing when no size was given. Either clamp the implicit request to max_wal_size, or drop the default and make the function strict. One reviewer preferred the strict version, and the question was explicitly left to the committer. I lean towards strict with no default. Today the implicit form is also the least tested part (see 5).
-
Concurrency. With four concurrent calls on an empty pool, each asking for max_wal_size (64 segments of 1MB), the returned counts summed to 66 and 66 future segments existed afterwards, across 67 positions (one gap). This is consistent across runs. XLogFileInitInternal() installs with find_free, so a call that loses the race for segment N puts its file at the next free slot, up to N + CheckPointSegments. The cap is therefore exceeded by about the number of concurrent callers. That is harmless, but the comment and docs say a call "cannot request more than max_wal_size". Either mention that the cap is per call and approximate under concurrency, or have PreallocNXlogFiles() stop once a file lands past the sampled window.
Under a concurrent pgbench insert load with repeated pairs of calls, I saw no errors, no leftover xlogtemp files and nothing in the server log. pg_stat_io (object wal, context init) attributes the creations to the calling client backend, which is a useful way to verify the effect and worth a sentence in the docs.
-
Disk accounting. The docs say preallocated files survive checkpoints until WAL advances into them. Please also say plainly that this adds up to max_wal_size on top of whatever pg_wal already holds for checkpoints, slots and wal_keep_size, so peak pg_wal usage can be about twice max_wal_size. Operators planning disk space need that number.
-
Tests. The "omitted size uses min_wal_size" check only asserts 0, because by then the 8 explicit segments already cover min_wal_size = 2MB. A small reordering would make it a real check: call the no-argument form first and expect min_wal_size / segment size. The rest of the test is compact and covers the exact-boundary case mentioned in the thread.
-
Smaller points:
- PreallocNXlogFiles() ignores the timeline returned by GetWALInsertionTimeLine() changing during the loop. That cannot happen outside recovery, but an Assert would document it.
- The pg_proc descr "preallocate future WAL files, return number created" could drop the second half to match neighbouring entries.
- The hint text for the recovery case is copied from the other WAL control functions, which is fine.
Summary
This is a small, well-scoped feature with a clear motivation and benchmarks. The design was refined through review, and the tests pass. I found no correctness bug. Before commit, please consider:
- fix the parallel-safety marking;
- settle the NULL/min_wal_size default (and its interaction with min_wal_size > max_wal_size);
- document the per-call cap and peak disk usage;
- tighten the default-argument test.