pgsql: Fix WAL-logged CREATE DATABASE with nonstandard pages

From: Melanie Plageman <melanieplageman(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix WAL-logged CREATE DATABASE with nonstandard pages
Date: 2026-11-19 21:03:40
Message-ID: E1xU9IB-00000000Qdn-1aek@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix WAL-logged CREATE DATABASE with nonstandard pages

RelationCopyStorageUsingBuffer() passed page_std=true when WAL-logging
every copied relation fork. This allowed WAL insertion to omit the
region between pd_lower and pd_upper from full-page images and restore
it as zeroes during replay.

That is safe only for pages using the standard page layout. On
nonstandard pages, the apparent hole can contain meaningful data,
causing the primary and standby to diverge. Known affected cases include
free space maps, visibility maps, and old GIN indexes, though any access
method using nonstandard pages could be affected.

Losing VM contents can lead to data corruption after promotion. For
example, a tuple lock on a promoted standby may find the all-frozen bit
already clear and therefore emit no WAL to clear it. The old primary
then replays the xmax change without clearing the all-frozen bit,
retaining the stale bit. This can cause vacuum failures and make
invisible data visible again.

Only remove the apparent free-space region from WAL when copying a heap
table or B-tree index, whose pages are known to use the standard layout.
For other relation types, remove the region only when it contains all
zeroes, ensuring that replay reconstructs the page without data loss.
Use a shared bounds helper so that RelationCopyStorageUsingBuffer()
checks the same region that XLogRecordAssemble() omits.

This may increase WAL volume for access methods other than heap and
B-tree when the space between pd_lower and pd_upper contains stale data
that was previously omitted. Such access methods can preserve hole
removal by zeroing that space.

Reported-by: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Reported-by: Rogers Wang <rogers(dot)ww(at)qq(dot)com>
Reviewed-by: surya poondla <suryapoondla4(at)gmail(dot)com>
Reviewed-by: Robert Haas <robertmhaas(at)gmail(dot)com>
Reviewed-by: Zhijie Hou <houzhijie22(at)gmail(dot)com>
Author: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Discussion: https://postgr.es/m/flat/CAEze2WgNjODiAiTU18E06_BrRK0nqwJ1kdS_bN61c2_DpFDSnQ%40mail.gmail.com
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/db26517e79c182b14075e948acf4b23e30f074fd

Modified Files
--------------
src/backend/access/transam/xloginsert.c | 13 ++++-----
src/backend/commands/dbcommands.c | 18 ++++++++++++-
src/backend/storage/buffer/bufmgr.c | 37 ++++++++++++++++++++-----
src/include/storage/bufmgr.h | 3 ++-
src/include/storage/bufpage.h | 14 ++++++++++
src/test/recovery/Makefile | 1 +
src/test/recovery/t/034_create_database.pl | 43 +++++++++++++++++++++++++++++-
7 files changed, 111 insertions(+), 18 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Melanie Plageman 2026-11-19 21:06:23 pgsql: Fix WAL-logged CREATE DATABASE with nonstandard pages
Previous Message Tom Lane 2026-11-19 20:49:12 pgsql: Give a good error message when we can neither hash nor sort a UN