From: Robert Haas Date: Tue, 14 Mar 2017 15:51:11 +0000 (-0400) Subject: Fix failure to mark init buffers as BM_PERMANENT. X-Git-Tag: REL9_2_21~35 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=b2ae1d6c4da12be54260d39f866bcd9f31768037;p=postgresql.git 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 brin, 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 --- diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 8dbe0f169f6..a6bbfbd90cd 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -856,10 +856,15 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, * paranoia. We also reset the usage_count since any recency of use of * the old content is no longer relevant. (The usage_count starts out at * 1 so that the buffer can survive one clock-sweep pass.) + * + * Make sure BM_PERMANENT is set for buffers that must be written at every + * checkpoint. Unlogged buffers only need to be written at shutdown + * checkpoints, except for their "init" forks, which need to be treated + * just like permanent relations. */ buf->tag = newTag; buf->flags &= ~(BM_VALID | BM_DIRTY | BM_JUST_DIRTIED | BM_CHECKPOINT_NEEDED | BM_IO_ERROR | BM_PERMANENT); - if (relpersistence == RELPERSISTENCE_PERMANENT) + if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM) buf->flags |= BM_TAG_VALID | BM_PERMANENT; else buf->flags |= BM_TAG_VALID; diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index f4f4e69d739..835461dda05 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -39,7 +39,7 @@ #define BM_PIN_COUNT_WAITER (1 << 6) /* have waiter for sole pin */ #define BM_CHECKPOINT_NEEDED (1 << 7) /* must write for checkpoint */ #define BM_PERMANENT (1 << 8) /* permanent relation (not - * unlogged) */ + * unlogged, or init fork) */ typedef bits16 BufFlags;