]> granicus.if.org Git - postgresql/commitdiff
Log the creation of an init fork unconditionally.
authorRobert Haas <rhaas@postgresql.org>
Thu, 8 Dec 2016 19:09:09 +0000 (14:09 -0500)
committerRobert Haas <rhaas@postgresql.org>
Thu, 8 Dec 2016 19:19:25 +0000 (14:19 -0500)
Previously, it was thought that this only needed to be done for the
benefit of possible standbys, so wal_level = minimal skipped it.
But that's not safe, because during crash recovery we might replay
XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE record which recursively
removes the directory that contains the new init fork.  So log it
always.

The user-visible effect of this bug is that if you create a database
or tablespace, then create an unlogged table, then crash without
checkpointing, then restart, accessing the table will fail, because
the it won't have been properly reset.  This commit fixes that.

Michael Paquier, per a report from Konstantin Knizhnik.  Wording of
the comments per a suggestion from me.

src/backend/access/nbtree/nbtree.c
src/backend/access/spgist/spginsert.c
src/backend/catalog/heap.c

index 6b2ebf024b50a253203938052d9e11221757f7ec..b79cb3efee326517582bbb269b4acd19ccaf78ba 100644 (file)
@@ -215,12 +215,17 @@ btbuildempty(PG_FUNCTION_ARGS)
        metapage = (Page) palloc(BLCKSZ);
        _bt_initmetapage(metapage, P_NONE, 0);
 
-       /* Write the page.  If archiving/streaming, XLOG it. */
+       /*
+        * Write the page and log it.  It might seem that an immediate sync
+        * would be sufficient to guarantee that the file exists on disk, but
+        * recovery itself might remove it while replaying, for example, an
+        * XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE record.  Therefore, we
+        * need this even when wal_level=minimal.
+        */
        smgrwrite(index->rd_smgr, INIT_FORKNUM, BTREE_METAPAGE,
                          (char *) metapage, true);
-       if (XLogIsNeeded())
-               log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
-                                       BTREE_METAPAGE, metapage);
+       log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
+                               BTREE_METAPAGE, metapage);
 
        /*
         * An immediate sync is required even if we xlog'd the page, because the
index 50dd1b74126b428f54460762ec88fc70fc33c6d9..2fb81bf01c1cc6f8f6063111195db6ae98f9cf8a 100644 (file)
@@ -164,30 +164,33 @@ spgbuildempty(PG_FUNCTION_ARGS)
        page = (Page) palloc(BLCKSZ);
        SpGistInitMetapage(page);
 
-       /* Write the page.  If archiving/streaming, XLOG it. */
+       /*
+        * Write the page and log it unconditionally.  This is important
+        * particularly for indexes created on tablespaces and databases
+        * whose creation happened after the last redo pointer as recovery
+        * removes any of their existing content when the corresponding
+        * create records are replayed.
+        */
        smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_METAPAGE_BLKNO,
                          (char *) page, true);
-       if (XLogIsNeeded())
-               log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
-                                       SPGIST_METAPAGE_BLKNO, page);
+       log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
+                               SPGIST_METAPAGE_BLKNO, page);
 
        /* Likewise for the root page. */
        SpGistInitPage(page, SPGIST_LEAF);
 
        smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_ROOT_BLKNO,
                          (char *) page, true);
-       if (XLogIsNeeded())
-               log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
-                                       SPGIST_ROOT_BLKNO, page);
+       log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
+                               SPGIST_ROOT_BLKNO, page);
 
        /* Likewise for the null-tuples root page. */
        SpGistInitPage(page, SPGIST_LEAF | SPGIST_NULLS);
 
        smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_NULL_BLKNO,
                          (char *) page, true);
-       if (XLogIsNeeded())
-               log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
-                                       SPGIST_NULL_BLKNO, page);
+       log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
+                               SPGIST_NULL_BLKNO, page);
 
        /*
         * An immediate sync is required even if we xlog'd the pages, because the
index 3097ec53c104089866a8173548bf3e9b7bb380f0..89c08fc87681a1367350a91b797d2f320a183d96 100644 (file)
@@ -1325,18 +1325,19 @@ heap_create_with_catalog(const char *relname,
 
 /*
  * Set up an init fork for an unlogged table so that it can be correctly
- * reinitialized on restart.  Since we're going to do an immediate sync, we
- * only need to xlog this if archiving or streaming is enabled.  And the
- * immediate sync is required, because otherwise there's no guarantee that
- * this will hit the disk before the next checkpoint moves the redo pointer.
+ * reinitialized on restart.  An immediate sync is required even if the
+ * page has been logged, because the write did not go through
+ * shared_buffers and therefore a concurrent checkpoint may have moved
+ * the redo pointer past our xlog record.  Recovery may as well remove it
+ * while replaying, for example, XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE
+ * record. Therefore, logging is necessary even if wal_level=minimal.
  */
 void
 heap_create_init_fork(Relation rel)
 {
        RelationOpenSmgr(rel);
        smgrcreate(rel->rd_smgr, INIT_FORKNUM, false);
-       if (XLogIsNeeded())
-               log_smgrcreate(&rel->rd_smgr->smgr_rnode.node, INIT_FORKNUM);
+       log_smgrcreate(&rel->rd_smgr->smgr_rnode.node, INIT_FORKNUM);
        smgrimmedsync(rel->rd_smgr, INIT_FORKNUM);
 }