From 4a40d24688b49ee25baec6593e393060f5c68e7e Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Wed, 1 Apr 2020 22:39:14 -0500
Subject: [PATCH v17 8/8] Also pass the table's tablespace as char*..

.. for consistency with the index tablespace
---
 src/backend/commands/cluster.c      | 28 ++++++++++++----------------
 src/backend/commands/vacuum.c       | 18 ++++++------------
 src/backend/postmaster/autovacuum.c |  2 +-
 src/include/commands/cluster.h      |  2 +-
 src/include/commands/vacuum.h       |  2 +-
 5 files changed, 21 insertions(+), 31 deletions(-)

diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 8601d36072..f70c2e9455 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -70,7 +70,7 @@ typedef struct
 } RelToCluster;
 
 
-static void rebuild_relation(Relation OldHeap, Oid indexOid, Oid NewTableSpaceOid, const char *NewIdxTableSpace, bool verbose);
+static void rebuild_relation(Relation OldHeap, Oid indexOid, const char *NewTableSpace, const char *NewIdxTableSpace, bool verbose);
 static void copy_table_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex,
 							bool verbose, bool *pSwapToastByContent,
 							TransactionId *pFreezeXid, MultiXactId *pCutoffMulti);
@@ -108,7 +108,6 @@ cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 	/* Name and Oid of tablespaces to use for clustered relations. */
 	char	*tablespaceName = NULL,
 			*idxtablespaceName = NULL;
-	Oid tablespaceOid;
 
 	/* Parse list of generic parameter not handled by the parser */
 	foreach(lc, stmt->params)
@@ -130,10 +129,6 @@ cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 					 parser_errposition(pstate, opt->location)));
 	}
 
-	/* Get tablespaces to use. */
-	tablespaceOid = tablespaceName ?
-		get_tablespace_oid(tablespaceName, false) : InvalidOid;
-
 	if (stmt->relation != NULL)
 	{
 		/* This is the single-relation case. */
@@ -215,7 +210,7 @@ cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 		table_close(rel, NoLock);
 
 		/* Do the job. */
-		cluster_rel(tableOid, indexOid, tablespaceOid, idxtablespaceName, stmt->options);
+		cluster_rel(tableOid, indexOid, tablespaceName, idxtablespaceName, stmt->options);
 	}
 	else
 	{
@@ -263,7 +258,7 @@ cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
 			/* functions in indexes may want a snapshot set */
 			PushActiveSnapshot(GetTransactionSnapshot());
 			/* Do the job. */
-			cluster_rel(rvtc->tableOid, rvtc->indexOid, tablespaceOid,
+			cluster_rel(rvtc->tableOid, rvtc->indexOid, tablespaceName,
 						 idxtablespaceName, stmt->options | CLUOPT_RECHECK);
 			PopActiveSnapshot();
 			CommitTransactionCommand();
@@ -298,7 +293,7 @@ cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
  * InvalidOid, use the tablespace in-use instead.
  */
 void
-cluster_rel(Oid tableOid, Oid indexOid, Oid tablespaceOid, const char *idxtablespace, int options)
+cluster_rel(Oid tableOid, Oid indexOid, const char *tablespace, const char *idxtablespace, int options)
 {
 	Relation	OldHeap;
 	bool		verbose = ((options & CLUOPT_VERBOSE) != 0);
@@ -411,7 +406,7 @@ cluster_rel(Oid tableOid, Oid indexOid, Oid tablespaceOid, const char *idxtables
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				 errmsg("cannot cluster a shared catalog")));
 
-	if (OidIsValid(tablespaceOid) &&
+	if (tablespace != NULL &&
 		!allowSystemTableMods && IsSystemRelation(OldHeap))
 		ereport(ERROR,
 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
@@ -422,7 +417,7 @@ cluster_rel(Oid tableOid, Oid indexOid, Oid tablespaceOid, const char *idxtables
 	 * We cannot support moving mapped relations into different tablespaces.
 	 * (In particular this eliminates all shared catalogs.)
 	 */
-	if (OidIsValid(tablespaceOid) && RelationIsMapped(OldHeap))
+	if (tablespace != NULL && RelationIsMapped(OldHeap))
 		ereport(ERROR,
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				 errmsg("cannot change tablespace of mapped relation \"%s\"",
@@ -478,7 +473,7 @@ cluster_rel(Oid tableOid, Oid indexOid, Oid tablespaceOid, const char *idxtables
 	TransferPredicateLocksToHeapRelation(OldHeap);
 
 	/* rebuild_relation does all the dirty work */
-	rebuild_relation(OldHeap, indexOid, tablespaceOid, idxtablespace, verbose);
+	rebuild_relation(OldHeap, indexOid, tablespace, idxtablespace, verbose);
 
 	/* NB: rebuild_relation does table_close() on OldHeap */
 
@@ -637,7 +632,7 @@ mark_index_clustered(Relation rel, Oid indexOid, bool is_internal)
  * NB: this routine closes OldHeap at the right time; caller should not.
  */
 static void
-rebuild_relation(Relation OldHeap, Oid indexOid, Oid NewTablespaceOid, const char *NewIdxTablespace, bool verbose)
+rebuild_relation(Relation OldHeap, Oid indexOid, const char *NewTablespace, const char *NewIdxTablespace, bool verbose)
 {
 	Oid			tableOid = RelationGetRelid(OldHeap);
 	Oid			tableSpace = OldHeap->rd_rel->reltablespace;
@@ -649,15 +644,16 @@ rebuild_relation(Relation OldHeap, Oid indexOid, Oid NewTablespaceOid, const cha
 	MultiXactId cutoffMulti;
 
 	/* Use new tablespace if passed. */
-	if (OidIsValid(NewTablespaceOid))
+	if (NewTablespace)
 	{
-		tableSpace = NewTablespaceOid;
+		tableSpace = get_tablespace_oid(NewTablespace, false);
 		/* XXX: It's not a shared catalog, so refuse to move it to shared tablespace */
+		/* Must not be a shared catalog, so refuse to move it to shared tablespace */
 		if (tableSpace == GLOBALTABLESPACE_OID)
 			ereport(ERROR,
 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 					 errmsg("cannot move non-shared relation to tablespace \"%s\"",
-							get_tablespace_name(tableSpace))));
+							NewTablespace)));
 	}
 
 	/* Mark the correct index as clustered */
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index c8a307b1a9..9e09d37a71 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -110,9 +110,6 @@ ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 	bool		parallel_option = false;
 	ListCell   *lc;
 
-	/* Tablespace to use for relations after VACUUM FULL. */
-	char		*tablespacename = NULL;
-
 	/* Set default value */
 	params.index_cleanup = VACOPT_TERNARY_DEFAULT;
 	params.truncate = VACOPT_TERNARY_DEFAULT;
@@ -120,6 +117,7 @@ ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 	/* By default parallel vacuum is enabled */
 	params.nworkers = 0;
 
+	params.tablespace = NULL;
 	params.idxtablespace = NULL;
 
 	/* Parse options list */
@@ -152,7 +150,7 @@ ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 		else if (strcmp(opt->defname, "truncate") == 0)
 			params.truncate = get_vacopt_ternary_value(opt);
 		else if (strcmp(opt->defname, "tablespace") == 0)
-			tablespacename = defGetString(opt);
+			params.tablespace = defGetString(opt);
 		else if (strcmp(opt->defname, "index_tablespace") == 0)
 			params.idxtablespace = defGetString(opt);
 		else if (strcmp(opt->defname, "parallel") == 0)
@@ -217,16 +215,12 @@ ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 				 errmsg("cannot specify both FULL and PARALLEL options")));
 
 	if ((params.options & VACOPT_FULL) == 0 &&
-		(tablespacename || params.idxtablespace))
+		(params.tablespace || params.idxtablespace))
 		ereport(ERROR,
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				errmsg("incompatible TABLESPACE option"),
 				errdetail("You can only use TABLESPACE with VACUUM FULL.")));
 
-	/* Get tablespaces to use. */
-	params.tablespace_oid = tablespacename ?
-		get_tablespace_oid(tablespacename, false) : InvalidOid;
-
 	/*
 	 * Make sure VACOPT_ANALYZE is specified if any column lists are present.
 	 */
@@ -1835,10 +1829,10 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
 	 * unless allow_system_table_mods=1.
 	 */
 	if (params->options & VACOPT_FULL &&
-		OidIsValid(params->tablespace_oid) &&
+		params->tablespace != NULL &&
 		IsSystemRelation(onerel) && !allowSystemTableMods)
 	{
-		params->tablespace_oid = InvalidOid;
+		params->tablespace = NULL;
 		ereport(WARNING,
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				errmsg("skipping tablespace change of \"%s\"",
@@ -1915,7 +1909,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
 			cluster_options |= CLUOPT_VERBOSE;
 
 		/* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
-		cluster_rel(relid, InvalidOid, params->tablespace_oid,
+		cluster_rel(relid, InvalidOid, params->tablespace,
 				params->idxtablespace, cluster_options);
 	}
 	else
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 8f191c9ead..cb83d3110f 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -2897,7 +2897,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
 		tab->at_params.multixact_freeze_table_age = multixact_freeze_table_age;
 		tab->at_params.is_wraparound = wraparound;
 		tab->at_params.log_min_duration = log_min_duration;
-		tab->at_params.tablespace_oid = InvalidOid;
+		tab->at_params.tablespace = NULL;
 		tab->at_params.idxtablespace = NULL;
 		tab->at_vacuum_cost_limit = vac_cost_limit;
 		tab->at_vacuum_cost_delay = vac_cost_delay;
diff --git a/src/include/commands/cluster.h b/src/include/commands/cluster.h
index 80f9120138..0d2eb25558 100644
--- a/src/include/commands/cluster.h
+++ b/src/include/commands/cluster.h
@@ -20,7 +20,7 @@
 
 
 extern void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel);
-extern void cluster_rel(Oid tableOid, Oid indexOid, Oid tablespaceOid, const char *indextablespace, int options);
+extern void cluster_rel(Oid tableOid, Oid indexOid, const char *tablespace, const char *indextablespace, int options);
 extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
 									   bool recheck, LOCKMODE lockmode);
 extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h
index edbb123681..b0850f97b3 100644
--- a/src/include/commands/vacuum.h
+++ b/src/include/commands/vacuum.h
@@ -231,7 +231,7 @@ typedef struct VacuumParams
 	int			nworkers;
 
 	/* tablespaces to use for relations rebuilt by VACUUM FULL */
-	Oid			tablespace_oid;
+	char			*tablespace;
 	char			*idxtablespace;
 } VacuumParams;
 
-- 
2.17.0

