agora inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH v20 4/6] Move conversion of a "historic" to MVCC snapshot to a separate function.
130+ messages / 2 participants
[nested] [flat]

* [PATCH v20 4/6] Move conversion of a "historic" to MVCC snapshot to a separate function.
@ 2025-08-11 13:23 Antonin Houska <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Antonin Houska @ 2025-08-11 13:23 UTC (permalink / raw)

The conversion is now handled by SnapBuildMVCCFromHistoric(). REPACK
CONCURRENTLY will also need it.
---
 src/backend/replication/logical/snapbuild.c | 51 +++++++++++++++++----
 src/backend/utils/time/snapmgr.c            |  3 +-
 src/include/replication/snapbuild.h         |  1 +
 src/include/utils/snapmgr.h                 |  1 +
 4 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 98ddee20929..a2f1803622c 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -440,10 +440,7 @@ Snapshot
 SnapBuildInitialSnapshot(SnapBuild *builder)
 {
 	Snapshot	snap;
-	TransactionId xid;
 	TransactionId safeXid;
-	TransactionId *newxip;
-	int			newxcnt = 0;
 
 	Assert(XactIsoLevel == XACT_REPEATABLE_READ);
 	Assert(builder->building_full_snapshot);
@@ -485,6 +482,31 @@ SnapBuildInitialSnapshot(SnapBuild *builder)
 
 	MyProc->xmin = snap->xmin;
 
+	/* Convert the historic snapshot to MVCC snapshot. */
+	return SnapBuildMVCCFromHistoric(snap, true);
+}
+
+/*
+ * Turn a historic MVCC snapshot into an ordinary MVCC snapshot.
+ *
+ * Unlike a regular (non-historic) MVCC snapshot, the xip array of this
+ * snapshot contains not only running main transactions, but also their
+ * subtransactions. This difference does has no impact on XidInMVCCSnapshot().
+ *
+ * Pass true for 'in_place' if you don't care about modifying the source
+ * snapshot. If you need a new instance, and one that was allocated as a
+ * single chunk of memory, pass false.
+ */
+Snapshot
+SnapBuildMVCCFromHistoric(Snapshot snapshot, bool in_place)
+{
+	TransactionId xid;
+	TransactionId *oldxip = snapshot->xip;
+	uint32		oldxcnt = snapshot->xcnt;
+	TransactionId *newxip;
+	int			newxcnt = 0;
+	Snapshot	result;
+
 	/* allocate in transaction context */
 	newxip = (TransactionId *)
 		palloc(sizeof(TransactionId) * GetMaxSnapshotXidCount());
@@ -495,7 +517,7 @@ SnapBuildInitialSnapshot(SnapBuild *builder)
 	 * classical snapshot by marking all non-committed transactions as
 	 * in-progress. This can be expensive.
 	 */
-	for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
+	for (xid = snapshot->xmin; NormalTransactionIdPrecedes(xid, snapshot->xmax);)
 	{
 		void	   *test;
 
@@ -503,7 +525,7 @@ SnapBuildInitialSnapshot(SnapBuild *builder)
 		 * Check whether transaction committed using the decoding snapshot
 		 * meaning of ->xip.
 		 */
-		test = bsearch(&xid, snap->xip, snap->xcnt,
+		test = bsearch(&xid, snapshot->xip, snapshot->xcnt,
 					   sizeof(TransactionId), xidComparator);
 
 		if (test == NULL)
@@ -520,11 +542,22 @@ SnapBuildInitialSnapshot(SnapBuild *builder)
 	}
 
 	/* adjust remaining snapshot fields as needed */
-	snap->snapshot_type = SNAPSHOT_MVCC;
-	snap->xcnt = newxcnt;
-	snap->xip = newxip;
+	snapshot->xcnt = newxcnt;
+	snapshot->xip = newxip;
 
-	return snap;
+	if (in_place)
+		result = snapshot;
+	else
+	{
+		result = CopySnapshot(snapshot);
+
+		/* Restore the original values so the source is intact. */
+		snapshot->xip = oldxip;
+		snapshot->xcnt = oldxcnt;
+	}
+	result->snapshot_type = SNAPSHOT_MVCC;
+
+	return result;
 }
 
 /*
diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c
index 65561cc6bc3..bc7840052fe 100644
--- a/src/backend/utils/time/snapmgr.c
+++ b/src/backend/utils/time/snapmgr.c
@@ -212,7 +212,6 @@ typedef struct ExportedSnapshot
 static List *exportedSnapshots = NIL;
 
 /* Prototypes for local functions */
-static Snapshot CopySnapshot(Snapshot snapshot);
 static void UnregisterSnapshotNoOwner(Snapshot snapshot);
 static void FreeSnapshot(Snapshot snapshot);
 static void SnapshotResetXmin(void);
@@ -602,7 +601,7 @@ SetTransactionSnapshot(Snapshot sourcesnap, VirtualTransactionId *sourcevxid,
  * The copy is palloc'd in TopTransactionContext and has initial refcounts set
  * to 0.  The returned snapshot has the copied flag set.
  */
-static Snapshot
+Snapshot
 CopySnapshot(Snapshot snapshot)
 {
 	Snapshot	newsnap;
diff --git a/src/include/replication/snapbuild.h b/src/include/replication/snapbuild.h
index 44031dcf6e3..6d4d2d1814c 100644
--- a/src/include/replication/snapbuild.h
+++ b/src/include/replication/snapbuild.h
@@ -73,6 +73,7 @@ extern void FreeSnapshotBuilder(SnapBuild *builder);
 extern void SnapBuildSnapDecRefcount(Snapshot snap);
 
 extern Snapshot SnapBuildInitialSnapshot(SnapBuild *builder);
+extern Snapshot SnapBuildMVCCFromHistoric(Snapshot snapshot, bool in_place);
 extern const char *SnapBuildExportSnapshot(SnapBuild *builder);
 extern void SnapBuildClearExportedSnapshot(void);
 extern void SnapBuildResetExportedSnapshotState(void);
diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h
index 604c1f90216..f65f83c85cd 100644
--- a/src/include/utils/snapmgr.h
+++ b/src/include/utils/snapmgr.h
@@ -63,6 +63,7 @@ extern Snapshot GetTransactionSnapshot(void);
 extern Snapshot GetLatestSnapshot(void);
 extern void SnapshotSetCommandId(CommandId curcid);
 
+extern Snapshot CopySnapshot(Snapshot snapshot);
 extern Snapshot GetCatalogSnapshot(Oid relid);
 extern Snapshot GetNonHistoricCatalogSnapshot(Oid relid);
 extern void InvalidateCatalogSnapshot(void);
-- 
2.39.5


--3q6txozlgl6t37td
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment;
	filename="v20-0005-Add-CONCURRENTLY-option-to-REPACK-command.patch"



^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread

* [PATCH] change get_tables_to_repack_partitioned
@ 2026-07-09 14:55 Álvaro Herrera <[email protected]>
  0 siblings, 0 replies; 130+ messages in thread

From: Álvaro Herrera @ 2026-07-09 14:55 UTC (permalink / raw)

---
 src/backend/commands/repack.c | 127 +++++++++++++++++-----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c
index 02883fe34a4..b3589ceff51 100644
--- a/src/backend/commands/repack.c
+++ b/src/backend/commands/repack.c
@@ -169,8 +169,8 @@ static void copy_table_data(Relation NewHeap, Relation OldHeap, Relation OldInde
 							MultiXactId *pCutoffMulti);
 static List *get_tables_to_repack(RepackCommand cmd, bool usingindex,
 								  MemoryContext permcxt);
-static List *get_tables_to_repack_partitioned(RepackCommand cmd,
-											  Oid relid, bool rel_is_index,
+static List *get_tables_to_repack_partitioned(RepackStmt *stmt,
+											  Relation rel,
 											  MemoryContext permcxt);
 static bool repack_is_permitted_for_relation(RepackCommand cmd,
 											 Oid relid, Oid userid);
@@ -387,58 +387,8 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel)
 	}
 	else
 	{
-		Oid			relid;
-		bool		rel_is_index;
-
-		Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
-
-		/*
-		 * If USING INDEX was specified, resolve the index name now and pass
-		 * it down.
-		 */
-		if (stmt->usingindex)
-		{
-			/*
-			 * If no index name was specified when repacking a partitioned
-			 * table, punt for now.  Maybe we can improve this later.
-			 */
-			if (!stmt->indexname)
-			{
-				if (stmt->command == REPACK_COMMAND_CLUSTER)
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-							errmsg("there is no previously clustered index for table \"%s\"",
-								   RelationGetRelationName(rel)));
-				else
-					ereport(ERROR,
-							errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-					/*- translator: first %s is name of a SQL command, eg. REPACK */
-							errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
-								   RepackCommandAsString(stmt->command),
-								   RelationGetRelationName(rel)));
-			}
-
-			relid = determine_clustered_index(rel, stmt->usingindex,
-											  stmt->indexname);
-			if (!OidIsValid(relid))
-				elog(ERROR, "unable to determine index to cluster on");
-			check_index_is_clusterable(rel, relid, AccessExclusiveLock);
-
-			rel_is_index = true;
-		}
-		else
-		{
-			relid = RelationGetRelid(rel);
-			rel_is_index = false;
-		}
-
-		rtcs = get_tables_to_repack_partitioned(stmt->command,
-												relid, rel_is_index,
-												repack_context);
-
-		/* close parent relation, releasing lock on it */
-		table_close(rel, AccessExclusiveLock);
-		rel = NULL;
+		rtcs = get_tables_to_repack_partitioned(stmt, rel, repack_context);
+		rel = NULL;			/* clobber no longer valid pointer */
 	}
 
 	/* Commit to get out of starting transaction */
@@ -2255,19 +2205,66 @@ get_tables_to_repack(RepackCommand cmd, bool usingindex, MemoryContext permcxt)
 }
 
 /*
- * Given a partitioned table or its index, return a list of RelToCluster for
- * all the leaf child tables/indexes.
+ * Resolve a partitioned table named as target of REPACK to the list of
+ * its partitions, and return it as a list of RelToCluster.
  *
- * 'rel_is_index' tells whether 'relid' is that of an index (true) or of the
- * owning relation.
+ * The partitioned table in question was already opened and locked by caller
+ * and is given as argument; it is closed and unlocked here before return.
  */
 static List *
-get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
-								 bool rel_is_index, MemoryContext permcxt)
+get_tables_to_repack_partitioned(RepackStmt *stmt, Relation rel,
+								 MemoryContext permcxt)
 {
+	Oid			relid;
+	bool		rel_is_index;
 	List	   *inhoids;
 	List	   *rtcs = NIL;
 
+	Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
+	Assert(CheckRelationLockedByMe(rel, AccessExclusiveLock, false));
+
+	/*
+	 * We find the list of tables by looking for inheritors.  If USING INDEX
+	 * was given, look for inheritors of that index, whose name we resolve now.
+	 *
+	 * Otherwise we look for inheritors of the table itself.
+	 */
+	if (stmt->usingindex)
+	{
+		/*
+		 * If no index name was specified when repacking a partitioned
+		 * table, punt for now.  Maybe we can improve this later.
+		 */
+		if (!stmt->indexname)
+		{
+			if (stmt->command == REPACK_COMMAND_CLUSTER)
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						errmsg("there is no previously clustered index for table \"%s\"",
+							   RelationGetRelationName(rel)));
+			else
+				ereport(ERROR,
+						errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+						/*- translator: first %s is name of a SQL command, eg. REPACK */
+						errmsg("cannot execute %s on partitioned table \"%s\" USING INDEX with no index name",
+							   RepackCommandAsString(stmt->command),
+							   RelationGetRelationName(rel)));
+		}
+
+		relid = determine_clustered_index(rel, stmt->usingindex,
+										  stmt->indexname);
+		if (!OidIsValid(relid))
+			elog(ERROR, "unable to determine index to cluster on");
+		check_index_is_clusterable(rel, relid, AccessExclusiveLock);
+
+		rel_is_index = true;
+	}
+	else
+	{
+		relid = RelationGetRelid(rel);
+		rel_is_index = false;
+	}
+
 	/*
 	 * Do not lock the children until they're processed.  Note that we do hold
 	 * a lock on the parent partitioned table.
@@ -2286,7 +2283,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 			if (get_rel_relkind(child_oid) != RELKIND_INDEX)
 				continue;
 
-			table_oid = IndexGetRelation(child_oid, false);
+			table_oid = IndexGetRelation(child_oid, true);
+			if (!OidIsValid(table_oid))
+				continue;
 			index_oid = child_oid;
 		}
 		else
@@ -2304,7 +2303,8 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		 * leaf partition despite having them on the partitioned table.  Skip
 		 * if so.
 		 */
-		if (!repack_is_permitted_for_relation(cmd, table_oid, GetUserId()))
+		if (!repack_is_permitted_for_relation(stmt->command, table_oid,
+											  GetUserId()))
 			continue;
 
 		/* Use a permanent memory context for the result list */
@@ -2316,6 +2316,9 @@ get_tables_to_repack_partitioned(RepackCommand cmd, Oid relid,
 		MemoryContextSwitchTo(oldcxt);
 	}
 
+	/* close parent relation, releasing lock on it */
+	table_close(rel, AccessExclusiveLock);
+
 	return rtcs;
 }
 
-- 
2.47.3


--f4uihmgjmvhgvze3--





^ permalink  raw  reply  [nested|flat] 130+ messages in thread


end of thread, other threads:[~2026-07-09 14:55 UTC | newest]

Thread overview: 130+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-08-11 13:23 [PATCH v20 4/6] Move conversion of a "historic" to MVCC snapshot to a separate function. Antonin Houska <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>
2026-07-09 14:55 [PATCH] change get_tables_to_repack_partitioned Álvaro Herrera <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox