agora inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH v2 1/4] tests: Improve test names in connect_fails()/connect_ok()
318+ messages / 2 participants
[nested] [flat]

* [PATCH v2 1/4] tests: Improve test names in connect_fails()/connect_ok()
@ 2025-03-07 14:44  Andres Freund <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Andres Freund @ 2025-03-07 14:44 UTC (permalink / raw)

connect_fails() didn't mention that stderr matched, whereas connect_ok() did.

Neither connect_fails() nor connect_ok() mentioned what they were checking
when checking psql's return status.

Reviewed-by: Heikki Linnakangas <[email protected]>
Discussion: https://postgr.es/m/ggflhkciwdyotpoie323chu2c2idpjk5qimrn462encwx2io7s@thmcxl7i6dpw
---
 src/test/perl/PostgreSQL/Test/Cluster.pm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm
index b105cba05a6..883532e1cd3 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -2554,7 +2554,7 @@ sub connect_ok
 		connstr => "$connstr",
 		on_error_stop => 0);
 
-	is($ret, 0, $test_name);
+	is($ret, 0, "$test_name: connect succeeds, as expected");
 
 	if (defined($params{expected_stdout}))
 	{
@@ -2619,11 +2619,11 @@ sub connect_fails
 		extra_params => ['-w'],
 		connstr => "$connstr");
 
-	isnt($ret, 0, $test_name);
+	isnt($ret, 0, "$test_name: connect fails, as expected");
 
 	if (defined($params{expected_stderr}))
 	{
-		like($stderr, $params{expected_stderr}, "$test_name: matches");
+		like($stderr, $params{expected_stderr}, "$test_name: stderr matches");
 	}
 
 	$self->log_check($test_name, $log_location, %params);
-- 
2.48.1.76.g4e746b1a31.dirty


--q4d36jgrlhdhudrc
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v2-0002-tests-Add-note-if-BackgroundPsql-wait_connect-fai.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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

* [PATCH v7 2/3] Handle default tablespace in AlterTableInternal
@ 2025-08-04 22:05  Erik Wienhold <[email protected]>
  0 siblings, 0 replies; 318+ messages in thread

From: Erik Wienhold @ 2025-08-04 22:05 UTC (permalink / raw)

Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED
VIEW from create_ctas_internal to ATPrepSetTableSpace.  It feels cleaner
that way in my opinion by not having to resolve the tablespace name just
to pass it to AlterTableInternal.  The default table space is passed as
empty string to AlterTableInternal.
---
 src/backend/commands/createas.c  | 21 ++-------------------
 src/backend/commands/tablecmds.c | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 1620273f965..30ca0a21903 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -24,7 +24,6 @@
  */
 #include "postgres.h"
 
-#include "miscadmin.h"
 #include "access/heapam.h"
 #include "access/reloptions.h"
 #include "access/tableam.h"
@@ -35,7 +34,6 @@
 #include "commands/matview.h"
 #include "commands/prepare.h"
 #include "commands/tablecmds.h"
-#include "commands/tablespace.h"
 #include "commands/view.h"
 #include "executor/execdesc.h"
 #include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
 		/* tablespace */
 		atcmd = makeNode(AlterTableCmd);
 		atcmd->subtype = AT_SetTableSpace;
-		if (into->tableSpaceName != NULL)
-			atcmd->name = into->tableSpaceName;
-		else
-		{
-			Oid spcid;
-
-			/*
-			 * Resolve the name of the default or database tablespace because
-			 * we need to specify the tablespace by name.
-			 *
-			 * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
-			 */
-			spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
-			if (!OidIsValid(spcid))
-				spcid = MyDatabaseTableSpace;
-			atcmd->name = get_tablespace_name(spcid);
-		}
+		/* use empty string to specify default tablespace */
+		atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
 		atcmds = lappend(atcmds, atcmd);
 
 		/* storage options */
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 44dcd2c5b0d..6ec0b87f841 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
 {
 	Oid			tablespaceId;
 
-	/* Check that the tablespace exists */
-	tablespaceId = get_tablespace_oid(tablespacename, false);
+	if (tablespacename != NULL && tablespacename[0] == '\0')
+	{
+		/* Use default tablespace if name is empty string */
+		tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
+		if (!OidIsValid(tablespaceId))
+			tablespaceId = MyDatabaseTableSpace;
+	}
+	else
+	{
+		/* Check that the tablespace exists */
+		tablespaceId = get_tablespace_oid(tablespacename, false);
+	}
 
 	/* Check permissions except when moving to database's default */
 	if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
-- 
2.50.1


--r33ycpluvyfavkwy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment;
	filename="v7-0003-Add-WITH-OLD-DATA-to-CREATE-OR-REPLACE-MATERIALIZ.patch"



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


end of thread, other threads:[~2025-08-04 22:05 UTC | newest]

Thread overview: 318+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-07 14:44 [PATCH v2 1/4] tests: Improve test names in connect_fails()/connect_ok() Andres Freund <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[email protected]>
2025-08-04 22:05 [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Erik Wienhold <[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