postgresql/src
Fujii Masao 6276057130 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
2026-06-30 08:50:50 +09:00
..
backend Fix unlogged sequence corruption after standby promotion 2026-06-30 08:50:50 +09:00
bin Fix handling of copy_file_range() return value 2026-06-29 13:01:57 +02:00
common Fix off-by-one with NFC recomposition for Hangul U+11A7 (TBASE) 2026-06-05 07:50:12 +09:00
fe_utils psql: Fix expanded aligned output 2026-06-08 14:37:56 +09:00
include Fix unlogged sequence corruption after standby promotion 2026-06-30 08:50:50 +09:00
interfaces Fix null-pointer crash in ECPG compiler. 2026-06-25 16:58:29 -04:00
makefiles pgxs.mk: remove unreachable rule for deleting regress.def. 2025-06-20 12:12:29 -04:00
pl plpython: Fix NULL pointer dereferences for broken sequence and mapping objects 2026-06-29 11:39:50 +09:00
port Make pg_mkdir_p() tolerant of a concurrent directory creation. 2026-06-19 12:52:00 -04:00
template thread-safety: gmtime_r(), localtime_r() 2024-08-23 07:43:04 +02:00
test Fix unlogged sequence corruption after standby promotion 2026-06-30 08:50:50 +09:00
timezone Guard against unsafe conditions in usage of pg_strftime(). 2026-05-11 05:13:47 -07:00
tools Modernize pg_bsd_indent's error/warning reporting code. 2026-06-15 12:22:55 -04:00
tutorial Doc: simplify the tutorial's window-function examples. 2025-01-21 14:43:21 -05:00
.gitignore
DEVELOPERS
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
Makefile.global.in Don't put library-supplied -L/-I switches before user-supplied ones. 2025-07-29 15:17:40 -04:00
Makefile.shlib Use exported symbols list on macOS for loadable modules as well 2025-06-10 07:04:43 +02:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00