Skip to content

Commit 6bd7816

Browse files
committed
Fix failure to mark init buffers as BM_PERMANENT.
This could result in corruption of the init fork of an unlogged index if the ambuildempty routine for that index used shared buffers to create the init fork, which was true for gin, gist, and hash indexes. Patch by me, based on an earlier patch by Michael Paquier, who also reviewed this one. This also incorporates an idea from Artur Zakirov. Discussion: http://postgr.es/m/CACYUyc8yccE4xfxhqxfh_Mh38j7dRFuxfaK1p6dSNAEUakxUyQ@mail.gmail.com
1 parent 8c6b940 commit 6bd7816

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,15 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
870870
* paranoia. We also reset the usage_count since any recency of use of
871871
* the old content is no longer relevant. (The usage_count starts out at
872872
* 1 so that the buffer can survive one clock-sweep pass.)
873+
*
874+
* Make sure BM_PERMANENT is set for buffers that must be written at every
875+
* checkpoint. Unlogged buffers only need to be written at shutdown
876+
* checkpoints, except for their "init" forks, which need to be treated
877+
* just like permanent relations.
873878
*/
874879
buf->tag = newTag;
875880
buf->flags &= ~(BM_VALID | BM_DIRTY | BM_JUST_DIRTIED | BM_CHECKPOINT_NEEDED | BM_IO_ERROR | BM_PERMANENT);
876-
if (relpersistence == RELPERSISTENCE_PERMANENT)
881+
if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM)
877882
buf->flags |= BM_TAG_VALID | BM_PERMANENT;
878883
else
879884
buf->flags |= BM_TAG_VALID;

src/include/storage/buf_internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#define BM_PIN_COUNT_WAITER (1 << 6) /* have waiter for sole pin */
4040
#define BM_CHECKPOINT_NEEDED (1 << 7) /* must write for checkpoint */
4141
#define BM_PERMANENT (1 << 8) /* permanent relation (not
42-
* unlogged) */
42+
* unlogged, or init fork) */
4343

4444
typedef bits16 BufFlags;
4545

0 commit comments

Comments
 (0)