Skip to content

Commit a338e41

Browse files
committed
Lock before setting relhassubclass on RELKIND_PARTITIONED_INDEX.
Commit 5b56264 added a comment that SetRelationHasSubclass() callers must hold this lock. When commit 17f206f extended use of this column to partitioned indexes, it didn't take the lock. As the latter commit message mentioned, we currently never reset a partitioned index to relhassubclass=f. That largely avoids harm from the lock omission. The cause for fixing this now is to unblock introducing a rule about locks required to heap_update() a pg_class row. This might cause more deadlocks. It gives minor user-visible benefits: - If an ALTER INDEX SET TABLESPACE runs concurrently with ALTER TABLE ATTACH PARTITION or CREATE PARTITION OF, one transaction blocks instead of failing with "tuple concurrently updated". (Many cases of DDL concurrency still fail that way.) - Match ALTER INDEX ATTACH PARTITION in choosing to lock the index. While not user-visible today, we'll need this if we ever make something set the flag to false for a partitioned index, like ANALYZE does today for tables. Back-patch to v12 (all supported versions), the plan for the commit relying on the new rule. In back branches, add LockOrStrongerHeldByMe() instead of adding a LockHeldByMe() parameter. Reviewed (in an earlier version) by Robert Haas. Discussion: https://postgr.es/m/20240611024525.9f.nmisch@google.com
1 parent 24561b4 commit a338e41

File tree

7 files changed

+77
-30
lines changed

7 files changed

+77
-30
lines changed

src/backend/catalog/index.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ index_create(Relation heapRelation,
10341034
if (OidIsValid(parentIndexRelid))
10351035
{
10361036
StoreSingleInheritance(indexRelationId, parentIndexRelid, 1);
1037+
LockRelationOid(parentIndexRelid, ShareUpdateExclusiveLock);
10371038
SetRelationHasSubclass(parentIndexRelid, true);
10381039
}
10391040

src/backend/commands/indexcmds.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,7 +4233,10 @@ IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
42334233

42344234
/* set relhassubclass if an index partition has been added to the parent */
42354235
if (OidIsValid(parentOid))
4236+
{
4237+
LockRelationOid(parentOid, ShareUpdateExclusiveLock);
42364238
SetRelationHasSubclass(parentOid, true);
4239+
}
42374240

42384241
/* set relispartition correctly on the partition */
42394242
update_relispartition(partRelid, OidIsValid(parentOid));

src/backend/commands/tablecmds.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,8 +3228,15 @@ findAttrByName(const char *attributeName, List *schema)
32283228
* SetRelationHasSubclass
32293229
* Set the value of the relation's relhassubclass field in pg_class.
32303230
*
3231-
* NOTE: caller must be holding an appropriate lock on the relation.
3232-
* ShareUpdateExclusiveLock is sufficient.
3231+
* It's always safe to set this field to true, because all SQL commands are
3232+
* ready to see true and then find no children. On the other hand, commands
3233+
* generally assume zero children if this is false.
3234+
*
3235+
* Caller must hold any self-exclusive lock until end of transaction. If the
3236+
* new value is false, caller must have acquired that lock before reading the
3237+
* evidence that justified the false value. That way, it properly waits if
3238+
* another backend is simultaneously concluding no need to change the tuple
3239+
* (new and old values are true).
32333240
*
32343241
* NOTE: an important side-effect of this operation is that an SI invalidation
32353242
* message is sent out to all backends --- including me --- causing plans
@@ -3244,6 +3251,11 @@ SetRelationHasSubclass(Oid relationId, bool relhassubclass)
32443251
HeapTuple tuple;
32453252
Form_pg_class classtuple;
32463253

3254+
Assert(CheckRelationOidLockedByMe(relationId,
3255+
ShareUpdateExclusiveLock, false) ||
3256+
CheckRelationOidLockedByMe(relationId,
3257+
ShareRowExclusiveLock, true));
3258+
32473259
/*
32483260
* Fetch a modifiable copy of the tuple, modify it, update pg_class.
32493261
*/

src/backend/storage/lmgr/lmgr.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -336,32 +336,26 @@ CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
336336
relation->rd_lockInfo.lockRelId.dbId,
337337
relation->rd_lockInfo.lockRelId.relId);
338338

339-
if (LockHeldByMe(&tag, lockmode))
340-
return true;
339+
return (orstronger ?
340+
LockOrStrongerHeldByMe(&tag, lockmode) :
341+
LockHeldByMe(&tag, lockmode));
342+
}
341343

342-
if (orstronger)
343-
{
344-
LOCKMODE slockmode;
344+
/*
345+
* CheckRelationOidLockedByMe
346+
*
347+
* Like the above, but takes an OID as argument.
348+
*/
349+
bool
350+
CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
351+
{
352+
LOCKTAG tag;
345353

346-
for (slockmode = lockmode + 1;
347-
slockmode <= MaxLockMode;
348-
slockmode++)
349-
{
350-
if (LockHeldByMe(&tag, slockmode))
351-
{
352-
#ifdef NOT_USED
353-
/* Sometimes this might be useful for debugging purposes */
354-
elog(WARNING, "lock mode %s substituted for %s on relation %s",
355-
GetLockmodeName(tag.locktag_lockmethodid, slockmode),
356-
GetLockmodeName(tag.locktag_lockmethodid, lockmode),
357-
RelationGetRelationName(relation));
358-
#endif
359-
return true;
360-
}
361-
}
362-
}
354+
SetLocktagRelationOid(&tag, relid);
363355

364-
return false;
356+
return (orstronger ?
357+
LockOrStrongerHeldByMe(&tag, lockmode) :
358+
LockHeldByMe(&tag, lockmode));
365359
}
366360

367361
/*

src/backend/storage/lmgr/lock.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,17 @@ DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
579579
}
580580

581581
/*
582-
* LockHeldByMe -- test whether lock 'locktag' is held with mode 'lockmode'
583-
* by the current transaction
582+
* LockHeldByMeExtended -- test whether lock 'locktag' is held by the current
583+
* transaction
584+
*
585+
* Returns true if current transaction holds a lock on 'tag' of mode
586+
* 'lockmode'. If 'orstronger' is true, a stronger lockmode is also OK.
587+
* ("Stronger" is defined as "numerically higher", which is a bit
588+
* semantically dubious but is OK for the purposes we use this for.)
584589
*/
585-
bool
586-
LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
590+
static bool
591+
LockHeldByMeExtended(const LOCKTAG *locktag,
592+
LOCKMODE lockmode, bool orstronger)
587593
{
588594
LOCALLOCKTAG localtag;
589595
LOCALLOCK *locallock;
@@ -599,7 +605,35 @@ LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
599605
(void *) &localtag,
600606
HASH_FIND, NULL);
601607

602-
return (locallock && locallock->nLocks > 0);
608+
if (locallock && locallock->nLocks > 0)
609+
return true;
610+
611+
if (orstronger)
612+
{
613+
LOCKMODE slockmode;
614+
615+
for (slockmode = lockmode + 1;
616+
slockmode <= MaxLockMode;
617+
slockmode++)
618+
{
619+
if (LockHeldByMeExtended(locktag, slockmode, false))
620+
return true;
621+
}
622+
}
623+
624+
return false;
625+
}
626+
627+
bool
628+
LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
629+
{
630+
return LockHeldByMeExtended(locktag, lockmode, false);
631+
}
632+
633+
bool
634+
LockOrStrongerHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
635+
{
636+
return LockHeldByMeExtended(locktag, lockmode, true);
603637
}
604638

605639
#ifdef USE_ASSERT_CHECKING

src/include/storage/lmgr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ extern bool ConditionalLockRelation(Relation relation, LOCKMODE lockmode);
4848
extern void UnlockRelation(Relation relation, LOCKMODE lockmode);
4949
extern bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode,
5050
bool orstronger);
51+
extern bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode,
52+
bool orstronger);
5153
extern bool LockHasWaitersRelation(Relation relation, LOCKMODE lockmode);
5254

5355
extern void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode);

src/include/storage/lock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ extern void LockReleaseSession(LOCKMETHODID lockmethodid);
561561
extern void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks);
562562
extern void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks);
563563
extern bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode);
564+
extern bool LockOrStrongerHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode);
564565
#ifdef USE_ASSERT_CHECKING
565566
extern HTAB *GetLockMethodLocalHash(void);
566567
#endif

0 commit comments

Comments
 (0)