Skip to content

Commit dbca84f

Browse files
committed
In rebuild_relation(), don't access an already-closed relcache entry.
This reliably fails with -DRELCACHE_FORCE_RELEASE, as reported by Andrew Dunstan, and could sometimes fail in normal operation, resulting in a wrong persistence value being used for the transient table. It's not immediately clear to me what effects that might have beyond the risk of a crash while accessing OldHeap->rd_rel->relpersistence, but it's probably not good. Bug introduced by commit f41872d, and made substantially worse by commit 85b506b, which added a second such access significantly later than the heap_close. I doubt the first reference could fail in a production scenario, but the second one definitely could. Discussion: https://postgr.es/m/7b52f900-0579-cda9-ae2e-de5da17090e6@2ndQuadrant.com
1 parent d77ff69 commit dbca84f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/backend/commands/cluster.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
557557
Oid tableOid = RelationGetRelid(OldHeap);
558558
Oid tableSpace = OldHeap->rd_rel->reltablespace;
559559
Oid OIDNewHeap;
560+
char relpersistence;
560561
bool is_system_catalog;
561562
bool swap_toast_by_content;
562563
TransactionId frozenXid;
@@ -566,15 +567,16 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
566567
if (OidIsValid(indexOid))
567568
mark_index_clustered(OldHeap, indexOid, true);
568569

569-
/* Remember if it's a system catalog */
570+
/* Remember info about rel before closing OldHeap */
571+
relpersistence = OldHeap->rd_rel->relpersistence;
570572
is_system_catalog = IsSystemRelation(OldHeap);
571573

572574
/* Close relcache entry, but keep lock until transaction commit */
573575
heap_close(OldHeap, NoLock);
574576

575577
/* Create the transient table that will receive the re-ordered data */
576578
OIDNewHeap = make_new_heap(tableOid, tableSpace,
577-
OldHeap->rd_rel->relpersistence,
579+
relpersistence,
578580
AccessExclusiveLock);
579581

580582
/* Copy the heap data into the new table in the desired order */
@@ -588,7 +590,7 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
588590
finish_heap_swap(tableOid, OIDNewHeap, is_system_catalog,
589591
swap_toast_by_content, false, true,
590592
frozenXid, cutoffMulti,
591-
OldHeap->rd_rel->relpersistence);
593+
relpersistence);
592594
}
593595

594596

0 commit comments

Comments
 (0)