Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit480b58f

Browse files
committed
Lock before setting relhassubclass on RELKIND_PARTITIONED_INDEX.
Commit5b56264 added a comment thatSetRelationHasSubclass() callers must hold this lock. When commit17f206f extended use of this column topartitioned indexes, it didn't take the lock. As the latter commitmessage mentioned, we currently never reset a partitioned index torelhassubclass=f. That largely avoids harm from the lock omission. Thecause for fixing this now is to unblock introducing a rule about locksrequired to heap_update() a pg_class row. This might cause moredeadlocks. 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 somethingset the flag to false for a partitioned index, like ANALYZE does todayfor tables. Back-patch to v12 (all supported versions), the plan forthe commit relying on the new rule. In back branches, addLockOrStrongerHeldByMe() 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 parent112d055 commit480b58f

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
@@ -1046,6 +1046,7 @@ index_create(Relation heapRelation,
10461046
if (OidIsValid(parentIndexRelid))
10471047
{
10481048
StoreSingleInheritance(indexRelationId,parentIndexRelid,1);
1049+
LockRelationOid(parentIndexRelid,ShareUpdateExclusiveLock);
10491050
SetRelationHasSubclass(parentIndexRelid, true);
10501051
}
10511052

‎src/backend/commands/indexcmds.c

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

43054305
/* set relhassubclass if an index partition has been added to the parent */
43064306
if (OidIsValid(parentOid))
4307+
{
4308+
LockRelationOid(parentOid,ShareUpdateExclusiveLock);
43074309
SetRelationHasSubclass(parentOid, true);
4310+
}
43084311

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

‎src/backend/commands/tablecmds.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,8 +3325,15 @@ findAttrByName(const char *attributeName, List *schema)
33253325
* SetRelationHasSubclass
33263326
*Set the value of the relation's relhassubclass field in pg_class.
33273327
*
3328-
* NOTE: caller must be holding an appropriate lock on the relation.
3329-
* ShareUpdateExclusiveLock is sufficient.
3328+
* It's always safe to set this field to true, because all SQL commands are
3329+
* ready to see true and then find no children. On the other hand, commands
3330+
* generally assume zero children if this is false.
3331+
*
3332+
* Caller must hold any self-exclusive lock until end of transaction. If the
3333+
* new value is false, caller must have acquired that lock before reading the
3334+
* evidence that justified the false value. That way, it properly waits if
3335+
* another backend is simultaneously concluding no need to change the tuple
3336+
* (new and old values are true).
33303337
*
33313338
* NOTE: an important side-effect of this operation is that an SI invalidation
33323339
* message is sent out to all backends --- including me --- causing plans
@@ -3341,6 +3348,11 @@ SetRelationHasSubclass(Oid relationId, bool relhassubclass)
33413348
HeapTupletuple;
33423349
Form_pg_class classtuple;
33433350

3351+
Assert(CheckRelationOidLockedByMe(relationId,
3352+
ShareUpdateExclusiveLock, false) ||
3353+
CheckRelationOidLockedByMe(relationId,
3354+
ShareRowExclusiveLock, true));
3355+
33443356
/*
33453357
* Fetch a modifiable copy of the tuple, modify it, update pg_class.
33463358
*/

‎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-
LOCKMODEslockmode;
344+
/*
345+
*CheckRelationOidLockedByMe
346+
*
347+
* Like the above, but takes an OID as argument.
348+
*/
349+
bool
350+
CheckRelationOidLockedByMe(Oidrelid,LOCKMODElockmode,boolorstronger)
351+
{
352+
LOCKTAGtag;
345353

346-
for (slockmode=lockmode+1;
347-
slockmode <=MaxLockMode;
348-
slockmode++)
349-
{
350-
if (LockHeldByMe(&tag,slockmode))
351-
{
352-
#ifdefNOT_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(constLOCKTAG*locktag,LOCKMODElockmode)
590+
staticbool
591+
LockHeldByMeExtended(constLOCKTAG*locktag,
592+
LOCKMODElockmode,boolorstronger)
587593
{
588594
LOCALLOCKTAGlocaltag;
589595
LOCALLOCK*locallock;
@@ -599,7 +605,35 @@ LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
599605
&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+
LOCKMODEslockmode;
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(constLOCKTAG*locktag,LOCKMODElockmode)
629+
{
630+
returnLockHeldByMeExtended(locktag,lockmode, false);
631+
}
632+
633+
bool
634+
LockOrStrongerHeldByMe(constLOCKTAG*locktag,LOCKMODElockmode)
635+
{
636+
returnLockHeldByMeExtended(locktag,lockmode, true);
603637
}
604638

605639
#ifdefUSE_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
externvoidUnlockRelation(Relationrelation,LOCKMODElockmode);
4949
externboolCheckRelationLockedByMe(Relationrelation,LOCKMODElockmode,
5050
boolorstronger);
51+
externboolCheckRelationOidLockedByMe(Oidrelid,LOCKMODElockmode,
52+
boolorstronger);
5153
externboolLockHasWaitersRelation(Relationrelation,LOCKMODElockmode);
5254

5355
externvoidLockRelationIdForSession(LockRelId*relid,LOCKMODElockmode);

‎src/include/storage/lock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ extern void LockReleaseSession(LOCKMETHODID lockmethodid);
569569
externvoidLockReleaseCurrentOwner(LOCALLOCK**locallocks,intnlocks);
570570
externvoidLockReassignCurrentOwner(LOCALLOCK**locallocks,intnlocks);
571571
externboolLockHeldByMe(constLOCKTAG*locktag,LOCKMODElockmode);
572+
externboolLockOrStrongerHeldByMe(constLOCKTAG*locktag,LOCKMODElockmode);
572573
#ifdefUSE_ASSERT_CHECKING
573574
externHTAB*GetLockMethodLocalHash(void);
574575
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp