Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8be543

Browse files
committed
Avoid creating init fork for unlogged indexes when it already exists.
Report by Greg Sabino Mullane, diagnosis and preliminary patch by Andres Freund, corrections by me.
1 parent 3980f7f commit b8be543

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/backend/catalog/index.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,14 @@ index_build(Relation heapRelation,
17411741
Assert(PointerIsValid(stats));
17421742

17431743
/*
1744-
* If this is an unlogged index, we need to write out an init fork for it.
1745-
*/
1746-
if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
1744+
* If this is an unlogged index, we may need to write out an init fork for
1745+
* it -- but we must first check whether one already exists. If, for
1746+
* example, an unlogged relation is truncated in the transaction that
1747+
* created it, or truncated twice in a subsequent transaction, the
1748+
* relfilenode won't change, and nothing needs to be done here.
1749+
*/
1750+
if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED
1751+
&& !smgrexists(indexRelation->rd_smgr, INIT_FORKNUM))
17471752
{
17481753
RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty;
17491754

0 commit comments

Comments
 (0)