postgres.git / summary / log / commit / refs

commit    fb68198d226d60cb019ee85cda14a5f74d312b29
Author:   Melanie Plageman <melanieplageman@gmail.com>
Date:     Thu Nov 19 21:07:24 2026 +0000

    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@gmail.com>
    Reported-by: Rogers Wang <rogers.ww@qq.com>
    Reviewed-by: surya poondla <suryapoondla4@gmail.com>
    Reviewed-by: Robert Haas <robertmhaas@gmail.com>
    Reviewed-by: Zhijie Hou <houzhijie22@gmail.com>
    Author: Melanie Plageman <melanieplageman@gmail.com>
    Discussion: https://postgr.es/m/flat/CAEze2WgNjODiAiTU18E06_BrRK0nqwJ1kdS_bN61c2_DpFDSnQ%40mail.gmail.com
    Backpatch-through: 15


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(-) diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index f8510dfe325..96e69d7c710 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -662,16 +662,13 @@ XLogRecordAssemble(RmgrId rmid, uint8 info, */ if (regbuf->flags & REGBUF_STANDARD) { - /* Assume we can omit data between pd_lower and pd_upper */ - uint16 lower = ((PageHeader) page)->pd_lower; - uint16 upper = ((PageHeader) page)->pd_upper; + const PageHeaderData *phdr = (const PageHeaderData *) page; - if (lower >= SizeOfPageHeaderData && - upper > lower && - upper <= BLCKSZ) + /* Assume we can omit data between pd_lower and pd_upper */ + if (PageFreeSpaceBoundsAreValid(page)) { - bimg.hole_offset = lower; - cbimg.hole_length = upper - lower; + bimg.hole_offset = phdr->pd_lower; + cbimg.hole_length = phdr->pd_upper - phdr->pd_lower; } else { diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 364c747a330..e66d6ae28e9 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -36,6 +36,7 @@ #include "catalog/dependency.h" #include "catalog/indexing.h" #include "catalog/objectaccess.h" +#include "catalog/pg_am_d.h" #include "catalog/pg_authid.h" #include "catalog/pg_collation.h" #include "catalog/pg_database.h" @@ -106,6 +107,7 @@ typedef struct CreateDBRelInfo RelFileLocator rlocator; /* physical relation identifier */ Oid reloid; /* relation oid */ bool permanent; /* relation is permanent or unlogged */ + bool mainfork_page_std; /* main fork has standard page layout */ } CreateDBRelInfo; @@ -213,7 +215,9 @@ CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid, LockRelationId(&dstrelid, AccessShareLock); /* Copy relation storage from source to the destination. */ - CreateAndCopyRelationData(srcrlocator, dstrlocator, relinfo->permanent); + CreateAndCopyRelationDataExtended(srcrlocator, dstrlocator, + relinfo->permanent, + relinfo->mainfork_page_std); /* Release the relation locks. */ UnlockRelationId(&srcrelid, AccessShareLock); @@ -445,6 +449,19 @@ ScanSourceDatabasePgClassTuple(HeapTupleData *tuple, Oid tbid, Oid dbid, relinfo->permanent = (classForm->relpersistence == RELPERSISTENCE_PERMANENT) ? true : false; + /* + * Heap tables and B-tree indexes are common cases whose main forks are + * known to use the standard page layout. Other access methods may do so + * as well, but some, such as GIN, have legacy page formats that differ + * from their current format. Keep the classification conservative by + * limiting it to heap and B-tree. + */ + relinfo->mainfork_page_std = + (RELKIND_HAS_TABLE_AM(classForm->relkind) && + classForm->relam == HEAP_TABLE_AM_OID) || + (classForm->relkind == RELKIND_INDEX && + classForm->relam == BTREE_AM_OID); + return relinfo; } diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 5999b6caeb2..6284ba796b2 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -62,6 +62,7 @@ #include "storage/smgr.h" #include "storage/standby.h" #include "utils/memdebug.h" +#include "utils/memutils.h" #include "utils/ps_status.h" #include "utils/rel.h" #include "utils/resowner.h" @@ -541,7 +542,8 @@ static void FindAndDropRelationBuffers(RelFileLocator rlocator, BlockNumber firstDelBlock); static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator, RelFileLocator dstlocator, - ForkNumber forkNum, bool permanent); + ForkNumber forkNum, bool permanent, + bool page_std); static void AtProcExit_Buffers(int code, Datum arg); static void CheckForBufferLeaks(void); #ifdef USE_ASSERT_CHECKING @@ -5148,7 +5150,8 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels) static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator, RelFileLocator dstlocator, - ForkNumber forkNum, bool permanent) + ForkNumber forkNum, bool permanent, + bool page_std) { Buffer srcBuf; Buffer dstBuf; @@ -5234,7 +5237,24 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator, /* WAL-log the copied page. */ if (use_wal) - log_newpage_buffer(dstBuf, true); + { + bool allow_page_std = page_std; + + /* + * For page layouts we don't recognize, hole removal is still safe + * if the exact range that WAL would omit contains only zeroes. + */ + if (!allow_page_std && PageFreeSpaceBoundsAreValid(dstPage)) + { + PageHeader phdr = (PageHeader) dstPage; + + allow_page_std = + pg_memory_is_all_zeros(dstPage + phdr->pd_lower, + phdr->pd_upper - phdr->pd_lower); + } + + log_newpage_buffer(dstBuf, allow_page_std); + } END_CRIT_SECTION(); @@ -5256,12 +5276,28 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator, * * Pass permanent as true for permanent relations and false for * unlogged relations. Currently this API is not supported for - * temporary relations. + * temporary relations. mainfork_page_std indicates whether the main + * fork uses the standard page layout; all other forks are treated as + * non-standard. * -------------------------------------------------------------------- */ void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent) +{ + CreateAndCopyRelationDataExtended(src_rlocator, dst_rlocator, permanent, + false); +} + +/* + * As above, but allow callers that know the main fork's page layout to retain + * full-page image hole removal. + */ +void +CreateAndCopyRelationDataExtended(RelFileLocator src_rlocator, + RelFileLocator dst_rlocator, + bool permanent, + bool mainfork_page_std) { char relpersistence; SMgrRelation src_rel; @@ -5284,7 +5320,7 @@ CreateAndCopyRelationData(RelFileLocator src_rlocator, /* copy main fork. */ RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, MAIN_FORKNUM, - permanent); + permanent, mainfork_page_std); /* copy those extra forks that exist */ for (ForkNumber forkNum = MAIN_FORKNUM + 1; @@ -5303,7 +5339,7 @@ CreateAndCopyRelationData(RelFileLocator src_rlocator, /* Copy a fork's data, block by block. */ RelationCopyStorageUsingBuffer(src_rlocator, dst_rlocator, forkNum, - permanent); + permanent, false); } } } diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index 41fdc1e7693..1293b32566a 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -272,6 +272,10 @@ extern void FlushRelationsAllBuffers(struct SMgrRelationData **smgrs, int nrels) extern void CreateAndCopyRelationData(RelFileLocator src_rlocator, RelFileLocator dst_rlocator, bool permanent); +extern void CreateAndCopyRelationDataExtended(RelFileLocator src_rlocator, + RelFileLocator dst_rlocator, + bool permanent, + bool mainfork_page_std); extern void FlushDatabaseBuffers(Oid dbid); extern void DropRelationBuffers(struct SMgrRelationData *smgr_reln, ForkNumber *forkNum, diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h index aeb67c498c5..578e6e03b75 100644 --- a/src/include/storage/bufpage.h +++ b/src/include/storage/bufpage.h @@ -217,6 +217,20 @@ typedef PageHeaderData *PageHeader; */ #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp)) +/* + * Returns true if the page header describes a non-empty, in-bounds free space + * range. This does not prove that the page uses the standard page layout. + */ +static inline bool +PageFreeSpaceBoundsAreValid(const PageData *page) +{ + const PageHeaderData *phdr = (const PageHeaderData *) page; + + return phdr->pd_lower >= SizeOfPageHeaderData && + phdr->pd_upper > phdr->pd_lower && + phdr->pd_upper <= BLCKSZ; +} + /* * PageIsEmpty * returns true iff no itemid has been allocated on the page diff --git a/src/test/recovery/Makefile b/src/test/recovery/Makefile index f6e4bc8d1a7..fbd70033c30 100644 --- a/src/test/recovery/Makefile +++ b/src/test/recovery/Makefile @@ -11,6 +11,7 @@ EXTRA_INSTALL=contrib/pg_prewarm \ contrib/pg_stat_statements \ + contrib/pg_visibility \ contrib/test_decoding \ src/test/modules/injection_points diff --git a/src/test/recovery/t/034_create_database.pl b/src/test/recovery/t/034_create_database.pl index 351d085e8dd..1ecf1498669 100644 --- a/src/test/recovery/t/034_create_database.pl +++ b/src/test/recovery/t/034_create_database.pl @@ -10,9 +10,18 @@ use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('node'); -$node->init; +$node->init(allows_streaming => 1); +# Keep the template's pg_class pages in shared buffers between CREATE DATABASE +# and CREATE TABLE. If CREATE TABLE reloads them after eviction, the checkpoint +# can flush its changes even if CREATE DATABASE failed to mark them permanent. +$node->append_conf('postgresql.conf', 'shared_buffers = 16MB'); $node->start; +$node->backup('backup'); +my $standby = PostgreSQL::Test::Cluster->new('standby'); +$standby->init_from_backup($node, 'backup', has_streaming => 1); +$standby->start; + # This checks that any DDLs run on the template database that modify pg_class # are persisted after creating a database from it using the WAL_LOG strategy, # as a direct copy of the template database's pg_class is used in this case. @@ -42,4 +51,36 @@ is($result, "0", "check that there are no tables from template on new database after crash" ); +# Create a table with a populated visibility map in the template database and +# copy it into a second database. +$node->safe_psql($db_template, q{ + CREATE TABLE tab_db_before_create_2 AS + SELECT generate_series(1, 10000) AS a; + VACUUM (FREEZE) tab_db_before_create_2; +}); +my $db_vm = "test_db_2"; +$node->safe_psql("postgres", + "CREATE DATABASE $db_vm STRATEGY WAL_LOG TEMPLATE $db_template;"); +$node->safe_psql($db_vm, "CREATE EXTENSION pg_visibility;"); + +$node->wait_for_replay_catchup($standby); + +my $visibility_map_query = q{ + SELECT blkno, all_visible, all_frozen + FROM pg_visibility_map('tab_db_before_create_2') + ORDER BY blkno; +}; +my $primary_visibility_map = + $node->safe_psql($db_vm, $visibility_map_query); +my $standby_visibility_map = + $standby->safe_psql($db_vm, $visibility_map_query); +my $primary_has_frozen = $node->safe_psql( + $db_vm, + q{SELECT bool_or(all_frozen) + FROM pg_visibility_map('tab_db_before_create_2');}); + +is($primary_has_frozen, 't', 'copied table has an all-frozen page'); +is($standby_visibility_map, $primary_visibility_map, + "copied table visibility map matches standby"); + done_testing(); [parent: 514d9ce3078a]