From f495c1981886478a1beda401111e58074e05d850 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Wed, 1 Apr 2020 14:53:22 -0500
Subject: [PATCH v18 4/6] indexcmds: remove redundant checks..

These are redundant with the checks in index.c rebuild_index(), which should
check them anyway, since it's called by cluster_rel by way of reindex_relation.
---
 src/backend/catalog/index.c      |  9 ++++
 src/backend/commands/indexcmds.c | 76 +++-----------------------------
 2 files changed, 16 insertions(+), 69 deletions(-)

diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 5267ecfffb..d7db92219d 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -56,6 +56,7 @@
 #include "commands/event_trigger.h"
 #include "commands/progress.h"
 #include "commands/tablecmds.h"
+#include "commands/tablespace.h"
 #include "commands/trigger.h"
 #include "executor/executor.h"
 #include "miscadmin.h"
@@ -3508,6 +3509,14 @@ reindex_index(Oid indexId, Oid tablespaceOid, bool skip_constraint_checks,
 				 errmsg("cannot change tablespace of mapped relation \"%s\"",
 						RelationGetRelationName(iRel))));
 
+	/* It's not a shared catalog, so refuse to move it to shared tablespace */
+	if (tablespaceOid == GLOBALTABLESPACE_OID)
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot move non-shared relation to tablespace \"%s\"",
+					 get_tablespace_name(tablespaceOid))));
+
+
 	/*
 	 * Don't allow reindex on temp tables of other backends ... their local
 	 * buffer manager is not going to cope.
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 1fd2de2bff..2dd63a84da 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -2463,17 +2463,8 @@ ReindexIndex(ReindexStmt *stmt)
 
 	/* Define new tablespaceOid if requested */
 	if (stmt->tablespacename)
-	{
 		tablespaceOid = get_tablespace_oid(stmt->tablespacename, false);
 
-		/* Can't move a non-shared relation into pg_global */
-		if (tablespaceOid == GLOBALTABLESPACE_OID)
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("cannot move non-shared relation to tablespace \"%s\"",
-							stmt->tablespacename)));
-	}
-
 	index_close(irel, NoLock);
 
 	if (stmt->concurrent && persistence != RELPERSISTENCE_TEMP)
@@ -2580,17 +2571,8 @@ ReindexTable(ReindexStmt *stmt)
 
 	/* Define new tablespaceOid if requested */
 	if (stmt->tablespacename)
-	{
 		tablespaceOid = get_tablespace_oid(stmt->tablespacename, false);
 
-		/* Can't move a non-shared relation into pg_global */
-		if (tablespaceOid == GLOBALTABLESPACE_OID)
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("cannot move non-shared relation to tablespace \"%s\"",
-							stmt->tablespacename)));
-	}
-
 	if (stmt->concurrent && get_rel_persistence(heapOid) != RELPERSISTENCE_TEMP)
 	{
 		result = ReindexRelationConcurrently(heapOid, tablespaceOid, stmt->options);
@@ -2682,17 +2664,8 @@ ReindexMultipleTables(ReindexStmt *stmt)
 
 	/* Define new tablespaceOid if requested */
 	if (stmt->tablespacename)
-	{
 		tablespaceOid = get_tablespace_oid(stmt->tablespacename, false);
 
-		/* Can't move a non-shared relation into pg_global */
-		if (tablespaceOid == GLOBALTABLESPACE_OID)
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("cannot move non-shared relation to tablespace \"%s\"",
-							stmt->tablespacename)));
-	}
-
 	/*
 	 * Create a memory context that will survive forced transaction commits we
 	 * do below.  Since it is a child of PortalContext, it will go away
@@ -2976,27 +2949,6 @@ ReindexRelationConcurrently(Oid relationOid, Oid tablespaceOid, int options)
 				/* Open relation to get its indexes */
 				heapRelation = table_open(relationOid, ShareUpdateExclusiveLock);
 
-				/*
-				 * We don't support moving system relations into different tablespaces,
-				 * unless allow_system_table_mods=1.
-				 */
-				if (OidIsValid(tablespaceOid) &&
-					!allowSystemTableMods && IsSystemRelation(heapRelation))
-					ereport(ERROR,
-							(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-							errmsg("permission denied: \"%s\" is a system catalog",
-									RelationGetRelationName(heapRelation))));
-
-				/*
-				 * We cannot support moving mapped relations into different tablespaces.
-				 * (In particular this eliminates all shared catalogs.)
-				 */
-				if (OidIsValid(tablespaceOid) && RelationIsMapped(heapRelation))
-					ereport(ERROR,
-							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-							 errmsg("cannot change tablespace of indexes on mapped relation \"%s\"",
-									RelationGetRelationName(heapRelation))));
-
 				/* Add all the valid indexes of relation to list */
 				foreach(lc, RelationGetIndexList(heapRelation))
 				{
@@ -3127,6 +3079,13 @@ ReindexRelationConcurrently(Oid relationOid, Oid tablespaceOid, int options)
 			break;
 	}
 
+	/* It's not a shared catalog, so refuse to move it to shared tablespace */
+	if (tablespaceOid == GLOBALTABLESPACE_OID)
+		ereport(ERROR,
+				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				 errmsg("cannot move non-shared relation to tablespace \"%s\"",
+					 get_tablespace_name(tablespaceOid))));
+
 	/* Definitely no indexes, so leave */
 	if (indexIds == NIL)
 	{
@@ -3179,27 +3138,6 @@ ReindexRelationConcurrently(Oid relationOid, Oid tablespaceOid, int options)
 		if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
 			elog(ERROR, "cannot reindex a temporary table concurrently");
 
-		/*
-		 * We don't support moving system relations into different tablespaces,
-		 * unless allow_system_table_mods=1.
-		 */
-		if (OidIsValid(tablespaceOid) &&
-			!allowSystemTableMods && IsSystemRelation(indexRel))
-			ereport(ERROR,
-					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
-					 errmsg("permission denied: \"%s\" is a system catalog",
-							RelationGetRelationName(indexRel))));
-
-		/*
-		 * We cannot support moving mapped relations into different tablespaces.
-		 * (In particular this eliminates all shared catalogs.)
-		 */
-		if (OidIsValid(tablespaceOid) && RelationIsMapped(indexRel))
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("cannot change tablespace of mapped relation \"%s\"",
-							RelationGetRelationName(indexRel))));
-
 		pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
 									  RelationGetRelid(heapRel));
 		pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
-- 
2.17.0

