Skip to content

Commit c17a3f5

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 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
1 parent d999b89 commit c17a3f5

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
@@ -1169,10 +1169,15 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
11691169
* paranoia. We also reset the usage_count since any recency of use of
11701170
* the old content is no longer relevant. (The usage_count starts out at
11711171
* 1 so that the buffer can survive one clock-sweep pass.)
1172+
*
1173+
* Make sure BM_PERMANENT is set for buffers that must be written at every
1174+
* checkpoint. Unlogged buffers only need to be written at shutdown
1175+
* checkpoints, except for their "init" forks, which need to be treated
1176+
* just like permanent relations.
11721177
*/
11731178
buf->tag = newTag;
11741179
buf->flags &= ~(BM_VALID | BM_DIRTY | BM_JUST_DIRTIED | BM_CHECKPOINT_NEEDED | BM_IO_ERROR | BM_PERMANENT);
1175-
if (relpersistence == RELPERSISTENCE_PERMANENT)
1180+
if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM)
11761181
buf->flags |= BM_TAG_VALID | BM_PERMANENT;
11771182
else
11781183
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)