Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Lock before setting relhassubclass on RELKIND_PARTITIONED_INDEX.
authorNoah Misch <noah@leadboat.com>
Fri, 28 Jun 2024 02:21:05 +0000 (19:21 -0700)
committerNoah Misch <noah@leadboat.com>
Fri, 28 Jun 2024 02:21:12 +0000 (19:21 -0700)
Commit 5b562644fec696977df4a82790064e8287927891 added a comment that
SetRelationHasSubclass() callers must hold this lock.  When commit
17f206fbc824d2b4b14480199ca9ff7dea417eda 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

src/backend/catalog/index.c
src/backend/commands/indexcmds.c
src/backend/commands/tablecmds.c
src/backend/storage/lmgr/lmgr.c
src/backend/storage/lmgr/lock.c
src/include/storage/lmgr.h
src/include/storage/lock.h

index f5f886444556746628b540dd1f8caf2408b4147c..5ac1b3237dbfafa1cd68621635ffa9771f0cbc6c 100644 (file)
@@ -1010,6 +1010,7 @@ index_create(Relation heapRelation,
    if (OidIsValid(parentIndexRelid))
    {
        StoreSingleInheritance(indexRelationId, parentIndexRelid, 1);
+       LockRelationOid(parentIndexRelid, ShareUpdateExclusiveLock);
        SetRelationHasSubclass(parentIndexRelid, true);
    }
 
index 4a7cfb3ce42bfd5c9af15694ee3100a7552da334..9d208717c6dd92d94d8dcd55d8fd5ada292067b6 100644 (file)
@@ -3762,7 +3762,10 @@ IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
 
    /* set relhassubclass if an index partition has been added to the parent */
    if (OidIsValid(parentOid))
+   {
+       LockRelationOid(parentOid, ShareUpdateExclusiveLock);
        SetRelationHasSubclass(parentOid, true);
+   }
 
    /* set relispartition correctly on the partition */
    update_relispartition(partRelid, OidIsValid(parentOid));
index 7ab3a511360f6626f6866f991e78935ebae848bd..0866a680d7c3325761425fc2696932970f2dc1d4 100644 (file)
@@ -3017,8 +3017,15 @@ findAttrByName(const char *attributeName, List *schema)
  * SetRelationHasSubclass
  *     Set the value of the relation's relhassubclass field in pg_class.
  *
- * NOTE: caller must be holding an appropriate lock on the relation.
- * ShareUpdateExclusiveLock is sufficient.
+ * It's always safe to set this field to true, because all SQL commands are
+ * ready to see true and then find no children.  On the other hand, commands
+ * generally assume zero children if this is false.
+ *
+ * Caller must hold any self-exclusive lock until end of transaction.  If the
+ * new value is false, caller must have acquired that lock before reading the
+ * evidence that justified the false value.  That way, it properly waits if
+ * another backend is simultaneously concluding no need to change the tuple
+ * (new and old values are true).
  *
  * NOTE: an important side-effect of this operation is that an SI invalidation
  * message is sent out to all backends --- including me --- causing plans
@@ -3033,6 +3040,11 @@ SetRelationHasSubclass(Oid relationId, bool relhassubclass)
    HeapTuple   tuple;
    Form_pg_class classtuple;
 
+   Assert(CheckRelationOidLockedByMe(relationId,
+                                     ShareUpdateExclusiveLock, false) ||
+          CheckRelationOidLockedByMe(relationId,
+                                     ShareRowExclusiveLock, true));
+
    /*
     * Fetch a modifiable copy of the tuple, modify it, update pg_class.
     */
index d4fa5f6ebc43bcc58ad05d07fb9b6b09522dfa13..79068795af4c755fe3bd1fccc77d2a2429695ddb 100644 (file)
@@ -307,32 +307,26 @@ CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
                         relation->rd_lockInfo.lockRelId.dbId,
                         relation->rd_lockInfo.lockRelId.relId);
 
-   if (LockHeldByMe(&tag, lockmode))
-       return true;
+   return (orstronger ?
+           LockOrStrongerHeldByMe(&tag, lockmode) :
+           LockHeldByMe(&tag, lockmode));
+}
 
-   if (orstronger)
-   {
-       LOCKMODE    slockmode;
+/*
+ *     CheckRelationOidLockedByMe
+ *
+ * Like the above, but takes an OID as argument.
+ */
+bool
+CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
+{
+   LOCKTAG     tag;
 
-       for (slockmode = lockmode + 1;
-            slockmode <= MaxLockMode;
-            slockmode++)
-       {
-           if (LockHeldByMe(&tag, slockmode))
-           {
-#ifdef NOT_USED
-               /* Sometimes this might be useful for debugging purposes */
-               elog(WARNING, "lock mode %s substituted for %s on relation %s",
-                    GetLockmodeName(tag.locktag_lockmethodid, slockmode),
-                    GetLockmodeName(tag.locktag_lockmethodid, lockmode),
-                    RelationGetRelationName(relation));
-#endif
-               return true;
-           }
-       }
-   }
+   SetLocktagRelationOid(&tag, relid);
 
-   return false;
+   return (orstronger ?
+           LockOrStrongerHeldByMe(&tag, lockmode) :
+           LockHeldByMe(&tag, lockmode));
 }
 
 /*
index 31933693d66ccfdbd52f78162eb17a7f140f0eed..bc888d2108dccbf2aecc62c820f9c3f312f25097 100644 (file)
@@ -579,11 +579,17 @@ DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
 }
 
 /*
- * LockHeldByMe -- test whether lock 'locktag' is held with mode 'lockmode'
- *     by the current transaction
+ * LockHeldByMeExtended -- test whether lock 'locktag' is held by the current
+ *     transaction
+ *
+ * Returns true if current transaction holds a lock on 'tag' of mode
+ * 'lockmode'.  If 'orstronger' is true, a stronger lockmode is also OK.
+ * ("Stronger" is defined as "numerically higher", which is a bit
+ * semantically dubious but is OK for the purposes we use this for.)
  */
-bool
-LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
+static bool
+LockHeldByMeExtended(const LOCKTAG *locktag,
+                    LOCKMODE lockmode, bool orstronger)
 {
    LOCALLOCKTAG localtag;
    LOCALLOCK  *locallock;
@@ -599,7 +605,35 @@ LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
                                          (void *) &localtag,
                                          HASH_FIND, NULL);
 
-   return (locallock && locallock->nLocks > 0);
+   if (locallock && locallock->nLocks > 0)
+       return true;
+
+   if (orstronger)
+   {
+       LOCKMODE    slockmode;
+
+       for (slockmode = lockmode + 1;
+            slockmode <= MaxLockMode;
+            slockmode++)
+       {
+           if (LockHeldByMeExtended(locktag, slockmode, false))
+               return true;
+       }
+   }
+
+   return false;
+}
+
+bool
+LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
+{
+   return LockHeldByMeExtended(locktag, lockmode, false);
+}
+
+bool
+LockOrStrongerHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
+{
+   return LockHeldByMeExtended(locktag, lockmode, true);
 }
 
 #ifdef USE_ASSERT_CHECKING
index ec5ef2b878845a1c467a28b7eb9320075a92b2a9..7023d7fb7820ae6dc67b6358d5a6f242e542e029 100644 (file)
@@ -47,6 +47,8 @@ extern bool ConditionalLockRelation(Relation relation, LOCKMODE lockmode);
 extern void UnlockRelation(Relation relation, LOCKMODE lockmode);
 extern bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode,
                                    bool orstronger);
+extern bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode,
+                                      bool orstronger);
 extern bool LockHasWaitersRelation(Relation relation, LOCKMODE lockmode);
 
 extern void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode);
index ab42f6b0802b85df8a1324e5bd0ffaa31dc24a40..6e10a6a4249adaccfd0ecd0d76aafd6bedaea07f 100644 (file)
@@ -558,6 +558,7 @@ extern void LockReleaseSession(LOCKMETHODID lockmethodid);
 extern void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks);
 extern void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks);
 extern bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode);
+extern bool LockOrStrongerHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode);
 #ifdef USE_ASSERT_CHECKING
 extern HTAB *GetLockMethodLocalHash(void);
 #endif