Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb76134

Browse files
committed
Fix incorrect code in new REINDEX CONCURRENTLY code
The previous code was adding pointers to transient variables to a list, but by the time the list was read, the variable might be gone, depending on the compiler. Fix it by making copies in the proper memory context.
1 parent 5dc92b8 commit bb76134

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/backend/commands/indexcmds.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,7 +2813,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28132813
Relation indexRel;
28142814
Relation heapRel;
28152815
Relation newIndexRel;
2816-
LockRelId lockrelid;
2816+
LockRelId *lockrelid;
28172817

28182818
indexRel = index_open(indexId, ShareUpdateExclusiveLock);
28192819
heapRel = table_open(indexRel->rd_index->indrelid,
@@ -2847,10 +2847,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28472847
* avoid multiple locks taken on the same relation, instead we rely on
28482848
* parentRelationIds built earlier.
28492849
*/
2850-
lockrelid = indexRel->rd_lockInfo.lockRelId;
2851-
relationLocks = lappend(relationLocks, &lockrelid);
2852-
lockrelid = newIndexRel->rd_lockInfo.lockRelId;
2853-
relationLocks = lappend(relationLocks, &lockrelid);
2850+
lockrelid = palloc(sizeof(*lockrelid));
2851+
*lockrelid = indexRel->rd_lockInfo.lockRelId;
2852+
relationLocks = lappend(relationLocks, lockrelid);
2853+
lockrelid = palloc(sizeof(*lockrelid));
2854+
*lockrelid = newIndexRel->rd_lockInfo.lockRelId;
2855+
relationLocks = lappend(relationLocks, lockrelid);
28542856

28552857
MemoryContextSwitchTo(oldcontext);
28562858

@@ -2866,19 +2868,21 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28662868
foreach(lc, heapRelationIds)
28672869
{
28682870
Relation heapRelation = table_open(lfirst_oid(lc), ShareUpdateExclusiveLock);
2869-
LockRelId lockrelid = heapRelation->rd_lockInfo.lockRelId;
2871+
LockRelId *lockrelid;
28702872
LOCKTAG *heaplocktag;
28712873

28722874
/* Save the list of locks in private context */
28732875
oldcontext = MemoryContextSwitchTo(private_context);
28742876

28752877
/* Add lockrelid of heap relation to the list of locked relations */
2876-
relationLocks = lappend(relationLocks, &lockrelid);
2878+
lockrelid = palloc(sizeof(*lockrelid));
2879+
*lockrelid = heapRelation->rd_lockInfo.lockRelId;
2880+
relationLocks = lappend(relationLocks, lockrelid);
28772881

28782882
heaplocktag = (LOCKTAG *) palloc(sizeof(LOCKTAG));
28792883

28802884
/* Save the LOCKTAG for this parent relation for the wait phase */
2881-
SET_LOCKTAG_RELATION(*heaplocktag, lockrelid.dbId, lockrelid.relId);
2885+
SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);
28822886
lockTags = lappend(lockTags, heaplocktag);
28832887

28842888
MemoryContextSwitchTo(oldcontext);
@@ -2890,9 +2894,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
28902894
/* Get a session-level lock on each table. */
28912895
foreach(lc, relationLocks)
28922896
{
2893-
LockRelId lockRel = *((LockRelId *) lfirst(lc));
2897+
LockRelId *lockrelid = (LockRelId *) lfirst(lc);
28942898

2895-
LockRelationIdForSession(&lockRel, ShareUpdateExclusiveLock);
2899+
LockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
28962900
}
28972901

28982902
PopActiveSnapshot();
@@ -3127,9 +3131,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
31273131
*/
31283132
foreach(lc, relationLocks)
31293133
{
3130-
LockRelId lockRel = *((LockRelId *) lfirst(lc));
3134+
LockRelId *lockrelid = (LockRelId *) lfirst(lc);
31313135

3132-
UnlockRelationIdForSession(&lockRel, ShareUpdateExclusiveLock);
3136+
UnlockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
31333137
}
31343138

31353139
/* Start a new transaction to finish process properly */

0 commit comments

Comments
 (0)