From ad0913702cb08f64507e6d9cef2f7ee31b42dad5 Mon Sep 17 00:00:00 2001 From: Narek Galstyan Date: Mon, 2 Nov 2026 15:12:40 -0500 Subject: [PATCH 2/2] Fix init fork (unlogged table) WAL-logging of bloom indexes Since commit ccadf73163c, blbuildempty() wrote the metapage of the init fork through generic WAL, which emits no WAL record for an unlogged relation. The init fork must be WAL-logged as it is used to recreate main fork of an unlogged relation after a crash. Without this fix, after a crash, an unlogged and bloom-indexed relation gets a zero-ed out index meta page. As a result, insertions and index scans fail with "Relation is not a bloom index" or "could not read blocks 0..0", until an index rebuild. --- contrib/bloom/blinsert.c | 16 +++++++++++++--- contrib/bloom/bloom.h | 2 +- contrib/bloom/blutils.c | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/contrib/bloom/blinsert.c b/contrib/bloom/blinsert.c index df24856d9ae..535da35736f 100644 --- a/contrib/bloom/blinsert.c +++ b/contrib/bloom/blinsert.c @@ -19,6 +19,7 @@ #include "miscadmin.h" #include "nodes/execnodes.h" #include "storage/bufmgr.h" +#include "storage/bulk_write.h" #include "utils/memutils.h" #include "utils/rel.h" @@ -130,7 +131,7 @@ blbuild(Relation heap, Relation index, IndexInfo *indexInfo) RelationGetRelationName(index)); /* Initialize the meta page */ - BloomInitMetapage(index, MAIN_FORKNUM); + BloomInitMetapage(index); /* Initialize the bloom build state */ memset(&buildstate, 0, sizeof(buildstate)); @@ -164,8 +165,17 @@ blbuild(Relation heap, Relation index, IndexInfo *indexInfo) void blbuildempty(Relation index) { - /* Initialize the meta page */ - BloomInitMetapage(index, INIT_FORKNUM); + BulkWriteState *bulkstate; + BulkWriteBuffer metabuf; + + bulkstate = smgr_bulk_start_rel(index, INIT_FORKNUM); + + /* Construct metapage. */ + metabuf = smgr_bulk_get_buf(bulkstate); + BloomFillMetapage(index, (Page) metabuf); + smgr_bulk_write(bulkstate, BLOOM_METAPAGE_BLKNO, metabuf, true); + + smgr_bulk_finish(bulkstate); } /* diff --git a/contrib/bloom/bloom.h b/contrib/bloom/bloom.h index 9a4125bdfb1..f5d2fbb7adb 100644 --- a/contrib/bloom/bloom.h +++ b/contrib/bloom/bloom.h @@ -174,7 +174,7 @@ typedef BloomScanOpaqueData *BloomScanOpaque; /* blutils.c */ extern void initBloomState(BloomState *state, Relation index); extern void BloomFillMetapage(Relation index, Page metaPage); -extern void BloomInitMetapage(Relation index, ForkNumber forknum); +extern void BloomInitMetapage(Relation index); extern void BloomInitPage(Page page, uint16 flags); extern Buffer BloomNewBuffer(Relation index); extern void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno); diff --git a/contrib/bloom/blutils.c b/contrib/bloom/blutils.c index 5111cdc6dd6..dc0b9fec38f 100644 --- a/contrib/bloom/blutils.c +++ b/contrib/bloom/blutils.c @@ -449,7 +449,7 @@ BloomFillMetapage(Relation index, Page metaPage) * Initialize metapage for bloom index. */ void -BloomInitMetapage(Relation index, ForkNumber forknum) +BloomInitMetapage(Relation index) { Buffer metaBuffer; Page metaPage; @@ -460,7 +460,7 @@ BloomInitMetapage(Relation index, ForkNumber forknum) * block number 0 (BLOOM_METAPAGE_BLKNO). No need to hold the extension * lock because there cannot be concurrent inserters yet. */ - metaBuffer = ReadBufferExtended(index, forknum, P_NEW, RBM_NORMAL, NULL); + metaBuffer = ReadBuffer(index, P_NEW); LockBuffer(metaBuffer, BUFFER_LOCK_EXCLUSIVE); Assert(BufferGetBlockNumber(metaBuffer) == BLOOM_METAPAGE_BLKNO); -- 2.50.1 (Apple Git-155)