]> granicus.if.org Git - postgresql/commitdiff
Fix failure to mark init buffers as BM_PERMANENT.
authorRobert Haas <rhaas@postgresql.org>
Tue, 14 Mar 2017 15:51:11 +0000 (11:51 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 14 Mar 2017 15:51:11 +0000 (11:51 -0400)
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

src/backend/storage/buffer/bufmgr.c
src/include/storage/buf_internals.h

index 3cb51204dcb6cf0a1f630c0e1098179121232384..2ca2ab9753c2613c47ffdac50d215f07956dcfd2 100644 (file)
@@ -1292,12 +1292,17 @@ 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_state &= ~(BM_VALID | BM_DIRTY | BM_JUST_DIRTIED |
                                   BM_CHECKPOINT_NEEDED | BM_IO_ERROR | BM_PERMANENT |
                                   BUF_USAGECOUNT_MASK);
-       if (relpersistence == RELPERSISTENCE_PERMANENT)
+       if (relpersistence == RELPERSISTENCE_PERMANENT || forkNum == INIT_FORKNUM)
                buf_state |= BM_TAG_VALID | BM_PERMANENT | BUF_USAGECOUNT_ONE;
        else
                buf_state |= BM_TAG_VALID | BUF_USAGECOUNT_ONE;
index d117b66537e17e23ed5d791e12836472394a08c0..ff99f6b5e205af28533c6542ab94b769f3e82607 100644 (file)
@@ -64,8 +64,8 @@
 #define BM_JUST_DIRTIED                        (1U << 28)              /* dirtied since write started */
 #define BM_PIN_COUNT_WAITER            (1U << 29)              /* have waiter for sole pin */
 #define BM_CHECKPOINT_NEEDED   (1U << 30)              /* must write for checkpoint */
-#define BM_PERMANENT                   (1U << 31)              /* permanent relation (not
-                                                                                                * unlogged) */
+#define BM_PERMANENT                   (1U << 31)              /* permanent buffer (not
+                                                                                                * unlogged, or init fork) */
 /*
  * The maximum allowed value of usage_count represents a tradeoff between
  * accuracy and speed of the clock-sweep buffer management algorithm.  A