From 21beeed4c89aa6e77e2e81de87f57cc33feaa7ef Mon Sep 17 00:00:00 2001 From: Erik Wienhold Date: Tue, 5 Aug 2025 00:05:43 +0200 Subject: [PATCH v7 2/3] Handle default tablespace in AlterTableInternal Move handling of default tablespace for CREATE OR REPLACE MATERIALIZED VIEW from create_ctas_internal to ATPrepSetTableSpace. It feels cleaner that way in my opinion by not having to resolve the tablespace name just to pass it to AlterTableInternal. The default table space is passed as empty string to AlterTableInternal. --- src/backend/commands/createas.c | 21 ++------------------- src/backend/commands/tablecmds.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index 1620273f965..30ca0a21903 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -24,7 +24,6 @@ */ #include "postgres.h" -#include "miscadmin.h" #include "access/heapam.h" #include "access/reloptions.h" #include "access/tableam.h" @@ -35,7 +34,6 @@ #include "commands/matview.h" #include "commands/prepare.h" #include "commands/tablecmds.h" -#include "commands/tablespace.h" #include "commands/view.h" #include "executor/execdesc.h" #include "executor/executor.h" @@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into) /* tablespace */ atcmd = makeNode(AlterTableCmd); atcmd->subtype = AT_SetTableSpace; - if (into->tableSpaceName != NULL) - atcmd->name = into->tableSpaceName; - else - { - Oid spcid; - - /* - * Resolve the name of the default or database tablespace because - * we need to specify the tablespace by name. - * - * TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then. - */ - spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false); - if (!OidIsValid(spcid)) - spcid = MyDatabaseTableSpace; - atcmd->name = get_tablespace_name(spcid); - } + /* use empty string to specify default tablespace */ + atcmd->name = into->tableSpaceName ? into->tableSpaceName : ""; atcmds = lappend(atcmds, atcmd); /* storage options */ diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 44dcd2c5b0d..6ec0b87f841 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -16580,8 +16580,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen { Oid tablespaceId; - /* Check that the tablespace exists */ - tablespaceId = get_tablespace_oid(tablespacename, false); + if (tablespacename != NULL && tablespacename[0] == '\0') + { + /* Use default tablespace if name is empty string */ + tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition); + if (!OidIsValid(tablespaceId)) + tablespaceId = MyDatabaseTableSpace; + } + else + { + /* Check that the tablespace exists */ + tablespaceId = get_tablespace_oid(tablespacename, false); + } /* Check permissions except when moving to database's default */ if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace) -- 2.50.1