From acbad3ce1d2dc766b84e9e2be35aa9206ce48cad Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Sat, 5 Dec 2026 18:27:53 -0300
Subject: [PATCH v1] Tolerate concurrently dropped objects in RangeVar
 callbacks

RangeVarCallbackForDropRelation(), RangeVarCallbackForReindexIndex()
and RangeVarCallbackForAttachIndex() look up the catalog before
RangeVarGetRelidExtended() locks the relation.  If another session
drops the relation, or detaches the partition, in that window, they
raise internal errors ("could not find tuple for parent of relation",
"cache lookup failed for index") or a misleading one ("... is not an
index") instead of letting the retry report that the relation does not
exist.

Add get_partition_parent_extended() with a missing_ok argument and use
it in the DROP callback, as that callback already does for the index
case with IndexGetRelation(..., true).  In the REINDEX INDEX callback,
return again when the index is gone, which 079480dc202 had turned into
errors, and only release the previous table lock if one was taken,
which avoids a "you don't own a lock" warning on the retry.  In the
ATTACH PARTITION callback for indexes, use IndexGetRelation(..., true).

Backpatch-through: 15
---
 src/backend/catalog/partition.c  | 24 ++++++++++++++++++++++++
 src/backend/commands/indexcmds.c | 18 ++++++++++++++----
 src/backend/commands/tablecmds.c | 15 ++++++++++-----
 src/include/catalog/partition.h  |  2 ++
 4 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 5efa7d7fa84..4d2febd4ceb 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -51,6 +51,23 @@ static void get_partition_ancestors_worker(Relation inhRel, Oid relid,
  */
 Oid
 get_partition_parent(Oid relid, bool even_if_detached)
+{
+	return get_partition_parent_extended(relid, even_if_detached, false);
+}
+
+/*
+ * get_partition_parent_extended
+ *		Same as get_partition_parent(), but if missing_ok is true, return
+ *		InvalidOid instead of raising an error when the relation has no
+ *		pg_inherits entry.
+ *
+ * That happens when a caller saw relispartition = true without holding a
+ * lock on the relation, and a concurrent DETACH PARTITION or DROP has
+ * committed since.
+ */
+Oid
+get_partition_parent_extended(Oid relid, bool even_if_detached,
+							  bool missing_ok)
 {
 	Relation	catalogRelation;
 	Oid			result;
@@ -62,7 +79,14 @@ get_partition_parent(Oid relid, bool even_if_detached)
 										 &detach_pending);
 
 	if (!OidIsValid(result))
+	{
+		if (missing_ok)
+		{
+			table_close(catalogRelation, AccessShareLock);
+			return InvalidOid;
+		}
 		elog(ERROR, "could not find tuple for parent of relation %u", relid);
+	}
 
 	if (detach_pending && !even_if_detached)
 		elog(ERROR, "relation %u has no parent because it's being detached",
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 0f2e9c3dd74..7341e3664e8 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -3193,9 +3193,9 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation,
 	/*
 	 * If we previously locked some other index's heap, and the name we're
 	 * looking up no longer refers to that relation, release the now-useless
-	 * lock.
+	 * lock.  There is none if the previous index was concurrently dropped.
 	 */
-	if (relId != oldRelId && OidIsValid(oldRelId))
+	if (relId != oldRelId && OidIsValid(state->locked_table_oid))
 	{
 		UnlockRelationOid(state->locked_table_oid, table_lockmode);
 		state->locked_table_oid = InvalidOid;
@@ -3205,8 +3205,16 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation,
 	if (!OidIsValid(relId))
 		return;
 
-	/* If the relation does exist, check whether it's an index. */
+	/*
+	 * If the relation does exist, check whether it's an index.  Nothing is
+	 * locked yet, so it may have been dropped since the name lookup; if so,
+	 * return, and RangeVarGetRelidExtended() will retry the lookup and report
+	 * that the relation does not exist.  The same applies to the parent
+	 * table lookup below.
+	 */
 	relkind = get_rel_relkind(relId);
+	if (!relkind)
+		return;
 	if (relkind != RELKIND_INDEX &&
 		relkind != RELKIND_PARTITIONED_INDEX)
 		ereport(ERROR,
@@ -3214,7 +3222,9 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation,
 				 errmsg("\"%s\" is not an index", relation->relname)));
 
 	/* Look up the index's table. */
-	table_oid = IndexGetRelation(relId, false);
+	table_oid = IndexGetRelation(relId, true);
+	if (!OidIsValid(table_oid) && relId != oldRelId)
+		return;
 
 	/*
 	 * In the unlikely event that, upon retry, we get the same index OID with
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9755943f110..9d900ceea32 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -1876,11 +1876,14 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
 	 * Similarly, if the relation is a partition, we must acquire lock on its
 	 * parent before locking the partition.  That's because queries lock the
 	 * parent before its partitions, so we risk deadlock if we do it the other
-	 * way around.
+	 * way around.  We haven't locked the relation yet, so a concurrent DETACH
+	 * PARTITION or DROP may have removed its pg_inherits entry since we read
+	 * relispartition; no error if so, as for the index case above.
 	 */
 	if (is_partition && relOid != oldRelOid)
 	{
-		state->partParentOid = get_partition_parent(relOid, true);
+		state->partParentOid = get_partition_parent_extended(relOid, true,
+															 true);
 		if (OidIsValid(state->partParentOid))
 			LockRelationOid(state->partParentOid, AccessExclusiveLock);
 	}
@@ -22980,10 +22983,12 @@ RangeVarCallbackForAttachIndex(const RangeVar *rv, Oid relOid, Oid oldRelOid,
 
 	/*
 	 * Since we need only examine the heap's tupledesc, an access share lock
-	 * on it (preventing any DDL) is sufficient.
+	 * on it (preventing any DDL) is sufficient.  The index isn't locked yet,
+	 * so it may have been dropped concurrently: nothing to do then.
 	 */
-	state->partitionOid = IndexGetRelation(relOid, false);
-	LockRelationOid(state->partitionOid, AccessShareLock);
+	state->partitionOid = IndexGetRelation(relOid, true);
+	if (OidIsValid(state->partitionOid))
+		LockRelationOid(state->partitionOid, AccessShareLock);
 }
 
 /*
diff --git a/src/include/catalog/partition.h b/src/include/catalog/partition.h
index a93cf081dd2..71551a9ef13 100644
--- a/src/include/catalog/partition.h
+++ b/src/include/catalog/partition.h
@@ -20,6 +20,8 @@
 #define HASH_PARTITION_SEED UINT64CONST(0x7A5B22367996DCFD)
 
 extern Oid	get_partition_parent(Oid relid, bool even_if_detached);
+extern Oid	get_partition_parent_extended(Oid relid, bool even_if_detached,
+										  bool missing_ok);
 extern List *get_partition_ancestors(Oid relid);
 extern Oid	index_get_partition(Relation partition, Oid indexId);
 extern List *map_partition_varattnos(List *expr, int fromrel_varno,

base-commit: 91d71a71cbe325189af30b552310c27dbf31c2c8
-- 
2.56.1

