]> granicus.if.org Git - postgresql/commitdiff
Avoid creating init fork for unlogged indexes when it already exists.
authorRobert Haas <rhaas@postgresql.org>
Thu, 2 Jun 2011 17:28:52 +0000 (13:28 -0400)
committerRobert Haas <rhaas@postgresql.org>
Thu, 2 Jun 2011 17:28:52 +0000 (13:28 -0400)
Report by Greg Sabino Mullane, diagnosis and preliminary patch by
Andres Freund, corrections by me.

src/backend/catalog/index.c

index a0898e00488a84d963239fbf847d07a5d1b65145..53b4c3c59bf78cacf8def18ac4d46940a3a29b71 100644 (file)
@@ -1741,9 +1741,14 @@ index_build(Relation heapRelation,
        Assert(PointerIsValid(stats));
 
        /*
-        * If this is an unlogged index, we need to write out an init fork for it.
-        */
-       if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
+        * If this is an unlogged index, we may need to write out an init fork for
+        * it -- but we must first check whether one already exists.  If, for
+        * example, an unlogged relation is truncated in the transaction that
+        * created it, or truncated twice in a subsequent transaction, the
+        * relfilenode won't change, and nothing needs to be done here.
+        */
+       if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED
+           && !smgrexists(indexRelation->rd_smgr, INIT_FORKNUM))
        {
                RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty;