postgres.git / summary / log / commit / refs
commit ecb260853526019a1340ba8a6b4c6de27a07d69b
Author: Melanie Plageman <melanieplageman@gmail.com>
Date: Fri Nov 20 20:38:23 2026 +0000
Reduce WAL generated when copying relation storage
RelationCopyStorage() treated every page as nonstandard, logging the
region between pd_lower and pd_upper in full-page images even when it
was known to be free space.
Apply the same policy that db26517e79c established for
RelationCopyStorageUsingBuffer(): omit that region for heap and B-tree
main-fork pages, which are known to use the standard page layout. For
other pages, omit it only if the bounds are valid and the region
contains only zeroes. This reduces WAL volume without losing data on
pages with nonstandard layouts.
Author: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: surya poondla <suryapoondla4@gmail.com>
Discussion: https://postgr.es/m/CAAKRu_b9r-Pz9gzYvC%2B5EHnXqNgfsJhcH%3DhuUGbXzvcnbHtxCg%40mail.gmail.com
src/backend/access/heap/heapam_handler.c | 11 ++++++++---
src/backend/catalog/storage.c | 20 +++++++++++++++-----
src/backend/commands/tablecmds.c | 11 ++++++++---
src/include/catalog/storage.h | 3 ++-
src/test/recovery/t/018_wal_optimize.pl | 13 +++++++++++++
5 files changed, 46 insertions(+), 12 deletions(-)
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 05a81e4557a..1437274b0c6 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -31,6 +31,7 @@
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/index.h"
+#include "catalog/pg_am_d.h"
#include "catalog/storage.h"
#include "catalog/storage_xlog.h"
#include "commands/progress.h"
@@ -553,9 +554,13 @@ heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
*/
dstrel = RelationCreateStorage(*newrlocator, rel->rd_rel->relpersistence, true);
- /* copy main fork */
+ /*
+ * Other table AMs can reuse this callback, so ensure we are only passing
+ * page_std true for standard main-fork pages of the built-in heap AM.
+ */
RelationCopyStorage(RelationGetSmgr(rel), dstrel, MAIN_FORKNUM,
- rel->rd_rel->relpersistence);
+ rel->rd_rel->relpersistence,
+ rel->rd_rel->relam == HEAP_TABLE_AM_OID);
/* copy those extra forks that exist */
for (ForkNumber forkNum = MAIN_FORKNUM + 1;
@@ -574,7 +579,7 @@ heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
forkNum == INIT_FORKNUM))
log_smgrcreate(newrlocator, forkNum);
RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
- rel->rd_rel->relpersistence);
+ rel->rd_rel->relpersistence, false);
}
}
diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index b2c1f42009e..79027541657 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -476,7 +476,7 @@ RelationPreTruncate(Relation rel)
*/
void
RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
- ForkNumber forkNum, char relpersistence)
+ ForkNumber forkNum, char relpersistence, bool page_std)
{
bool use_wal;
bool copying_initfork;
@@ -508,6 +508,7 @@ RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
for (blkno = 0; blkno < nblocks; blkno++)
{
BulkWriteBuffer buf;
+ bool allow_page_std = page_std;
int piv_flags;
bool checksum_failure;
bool verified;
@@ -551,11 +552,20 @@ RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
}
/*
- * Queue the page for WAL-logging and writing out. Unfortunately we
- * don't know what kind of a page this is, so we have to log the full
- * page including any unused space.
+ * For page layouts we don't recognize, hole removal is still safe if
+ * the exact range that WAL would omit contains only zeroes.
*/
- smgr_bulk_write(bulkstate, blkno, buf, false);
+ if (use_wal && !allow_page_std &&
+ PageFreeSpaceBoundsAreValid((Page) buf))
+ {
+ PageHeader phdr = (PageHeader) buf;
+
+ allow_page_std =
+ pg_memory_is_all_zeros((char *) buf + phdr->pd_lower,
+ phdr->pd_upper - phdr->pd_lower);
+ }
+
+ smgr_bulk_write(bulkstate, blkno, buf, allow_page_std);
}
smgr_bulk_finish(bulkstate);
}
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index b6700929459..9786a5af58a 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -18529,9 +18529,14 @@ index_copy_data(Relation rel, RelFileLocator newrlocator)
*/
dstrel = RelationCreateStorage(newrlocator, rel->rd_rel->relpersistence, true);
- /* copy main fork */
+ /*
+ * Assume standard main-fork pages only for B-tree. Other index AMs may
+ * use standard pages today but still have nonstandard pages from older
+ * versions (e.g. GIN).
+ */
RelationCopyStorage(RelationGetSmgr(rel), dstrel, MAIN_FORKNUM,
- rel->rd_rel->relpersistence);
+ rel->rd_rel->relpersistence,
+ rel->rd_rel->relam == BTREE_AM_OID);
/* copy those extra forks that exist */
for (ForkNumber forkNum = MAIN_FORKNUM + 1;
@@ -18550,7 +18555,7 @@ index_copy_data(Relation rel, RelFileLocator newrlocator)
forkNum == INIT_FORKNUM))
log_smgrcreate(&newrlocator, forkNum);
RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
- rel->rd_rel->relpersistence);
+ rel->rd_rel->relpersistence, false);
}
}
diff --git a/src/include/catalog/storage.h b/src/include/catalog/storage.h
index 70f619a6d6f..68211e51c1e 100644
--- a/src/include/catalog/storage.h
+++ b/src/include/catalog/storage.h
@@ -30,7 +30,8 @@ extern void RelationPreserveStorage(RelFileLocator rlocator, bool atCommit);
extern void RelationPreTruncate(Relation rel);
extern void RelationTruncate(Relation rel, BlockNumber nblocks);
extern void RelationCopyStorage(SMgrRelation src, SMgrRelation dst,
- ForkNumber forkNum, char relpersistence);
+ ForkNumber forkNum, char relpersistence,
+ bool page_std);
extern bool RelFileLocatorSkippingWAL(RelFileLocator rlocator);
extern Size EstimatePendingSyncsSpace(void);
extern void SerializePendingSyncs(Size maxSize, char *startAddress);
diff --git a/src/test/recovery/t/018_wal_optimize.pl b/src/test/recovery/t/018_wal_optimize.pl
index 8f25b5dd165..ffe868e82f5 100644
--- a/src/test/recovery/t/018_wal_optimize.pl
+++ b/src/test/recovery/t/018_wal_optimize.pl
@@ -391,6 +391,19 @@ wal_skip_threshold = 0
check_orphan_relfilenodes($node,
"wal_level = $wal_level, no orphan relfilenode remains");
+ # Move the table back, this time with a populated visibility map.
+ $node->safe_psql('postgres', q{
+ CREATE EXTENSION pg_visibility;
+ VACUUM (FREEZE) moved;
+ ALTER TABLE moved SET TABLESPACE pg_default;
+ });
+ $node->stop('immediate');
+ $node->start;
+ $result = $node->safe_psql('postgres',
+ "SELECT all_visible, all_frozen FROM pg_visibility_map('moved', 0)");
+ is($result, 't|t',
+ "wal_level = $wal_level, moved visibility map survives recovery");
+
return;
}
[parent: 65ebed1f5e2f]