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 <[email protected]>
Reported-by: Rogers Wang <[email protected]>
Reviewed-by: surya poondla <[email protected]>
Reviewed-by: Robert Haas <[email protected]>
Reviewed-by: Zhijie Hou <[email protected]>
Author: Melanie Plageman <[email protected]>
Discussion: 
https://postgr.es/m/flat/CAEze2WgNjODiAiTU18E06_BrRK0nqwJ1kdS_bN61c2_DpFDSnQ%40mail.gmail.com
Backpatch-through: 15

Branch
------
REL_18_STABLE

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

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

Reply via email to