From 42ae05132151bc881f1e280bf4fff563a1ff2d37 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 21 Jan 2026 11:27:03 +0800
Subject: [PATCH v7 1/2] tablecmds: reject CLUSTER ON for partitioned tables
 earlier

ALTER TABLE ... CLUSTER ON and SET WITHOUT CLUSTER are not supported for
partitioned tables and already fail today, but only at exec time.

Reject these commands earlier via ATSimplePermissions(), matching the
handling of other unsupported ALTER TABLE actions on partitioned tables
(such as SET LOGGED / SET UNLOGGED). This centralizes the relation-kind
check in the ALTER TABLE preparation phase, improving consistency and
maintainability.

As a result, partitioned tables now report the standard ATSimplePermissions()
error for unsupported ALTER TABLE actions.

Also, replace ereport(ERROR) with an Assert() in mark_index_clustered(),
because after this change no code path should call it for a partitioned table.

Author: Chao Li <lic@highgo.com>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://postgr.es/m/CAEoWx2kggo1N2kDH6OSfXHL_5gKg3DqQ0PdNuL4LH4XSTKJ3-g@mail.gmail.com
---
 src/backend/commands/cluster.c        | 7 ++-----
 src/backend/commands/tablecmds.c      | 2 +-
 src/test/regress/expected/cluster.out | 6 ++++--
 3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 60a4617a585..f9ee5a5ab60 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -558,11 +558,8 @@ mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
 	Relation	pg_index;
 	ListCell   *index;
 
-	/* Disallow applying to a partitioned table */
-	if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
-		ereport(ERROR,
-				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-				 errmsg("cannot mark index clustered in partitioned table")));
+	/* This function is only valid for non-partitioned tables */
+	Assert(rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE);
 
 	/*
 	 * If the index is already marked clustered, no need to do anything.
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index f976c0e5c7e..3e9f62e9d37 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5142,7 +5142,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 		case AT_ClusterOn:		/* CLUSTER ON */
 		case AT_DropCluster:	/* SET WITHOUT CLUSTER */
 			ATSimplePermissions(cmd->subtype, rel,
-								ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_MATVIEW);
+								ATT_TABLE | ATT_MATVIEW);
 			/* These commands never recurse */
 			/* No command-specific prep needed */
 			pass = AT_PASS_MISC;
diff --git a/src/test/regress/expected/cluster.out b/src/test/regress/expected/cluster.out
index 4d40a6809ab..07c52e647f7 100644
--- a/src/test/regress/expected/cluster.out
+++ b/src/test/regress/expected/cluster.out
@@ -492,9 +492,11 @@ Number of partitions: 3 (Use \d+ to list them.)
 CLUSTER clstrpart;
 ERROR:  there is no previously clustered index for table "clstrpart"
 ALTER TABLE clstrpart SET WITHOUT CLUSTER;
-ERROR:  cannot mark index clustered in partitioned table
+ERROR:  ALTER action SET WITHOUT CLUSTER cannot be performed on relation "clstrpart"
+DETAIL:  This operation is not supported for partitioned tables.
 ALTER TABLE clstrpart CLUSTER ON clstrpart_idx;
-ERROR:  cannot mark index clustered in partitioned table
+ERROR:  ALTER action CLUSTER ON cannot be performed on relation "clstrpart"
+DETAIL:  This operation is not supported for partitioned tables.
 DROP TABLE clstrpart;
 -- Ownership of partitions is checked
 CREATE TABLE ptnowner(i int unique) PARTITION BY LIST (i);
-- 
2.50.1 (Apple Git-155)

