]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix unlogged sequence corruption after standby promotion
authorFujii Masao <fujii@postgresql.org>
Mon, 29 Jun 2026 23:48:47 +0000 (08:48 +0900)
committerFujii Masao <fujii@postgresql.org>
Mon, 29 Jun 2026 23:48:47 +0000 (08:48 +0900)
commit8e684ce11ddaac2604375b8efcae97f6b36af4e7
treeba86a25c0fcc2322c0fba6e443be8896e2b540bc
parentefa59a500457f310abbc38dc472f03e959ccd5b8
Fix unlogged sequence corruption after standby promotion

Previously, if an unlogged sequence was created on the primary and
replicated to a standby, reading the sequence after promoting the
standby (for example, with nextval()) could trigger the following
assertion failure:

    TRAP: failed Assert("((const PageHeaderData *) page)->pd_special >= SizeOfPageHeaderData")

In non-assert builds, the same operation could instead fail with an
error such as:

    ERROR:  bad magic number in sequence

The problem was that seq_redo() updated the init fork page in shared
buffers but did not flush it to disk. During promotion,
ResetUnloggedRelations() recreates the main fork of unlogged
relations by copying the init fork from disk, bypassing shared
buffers. As a result, the main fork could be recreated from a stale
init fork instead of the WAL-replayed page.

Fix this by introducing a helper to flush init fork buffers
immediately, and make seq_redo() use it. As a result, the main fork
of an unlogged sequence is recreated from the up-to-date init fork on
disk, allowing the unlogged sequence to be read successfully after
standby promotion.

Backpatch to v15, where unlogged sequences were introduced.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: vignesh C <vignesh21@gmail.com>
Discussion: https://postgr.es/m/CAHGQGwH1Ssze3XM6wjoTjSLVOR041c6xP+vsdLP951=w8oG8bA@mail.gmail.com
Backpatch-through: 15
src/backend/access/hash/hash_xlog.c
src/backend/access/transam/xlogutils.c
src/backend/commands/sequence_xlog.c
src/include/access/xlogutils.h
src/test/recovery/meson.build
src/test/recovery/t/054_unlogged_sequence_promotion.pl [new file with mode: 0644]