Skip to content

Commit 51aedfa

Browse files
ewieCommitfest Bot
authored andcommitted
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.
1 parent d79aaff commit 51aedfa

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/backend/commands/createas.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
#include "postgres.h"
2626

27-
#include "miscadmin.h"
2827
#include "access/heapam.h"
2928
#include "access/reloptions.h"
3029
#include "access/tableam.h"
@@ -35,7 +34,6 @@
3534
#include "commands/matview.h"
3635
#include "commands/prepare.h"
3736
#include "commands/tablecmds.h"
38-
#include "commands/tablespace.h"
3937
#include "commands/view.h"
4038
#include "executor/execdesc.h"
4139
#include "executor/executor.h"
@@ -160,23 +158,8 @@ create_ctas_internal(List *attrList, IntoClause *into)
160158
/* tablespace */
161159
atcmd = makeNode(AlterTableCmd);
162160
atcmd->subtype = AT_SetTableSpace;
163-
if (into->tableSpaceName != NULL)
164-
atcmd->name = into->tableSpaceName;
165-
else
166-
{
167-
Oid spcid;
168-
169-
/*
170-
* Resolve the name of the default or database tablespace because
171-
* we need to specify the tablespace by name.
172-
*
173-
* TODO: Move that to ATPrepSetTableSpace? Must allow AlterTableCmd.name to be NULL then.
174-
*/
175-
spcid = GetDefaultTablespace(RELPERSISTENCE_PERMANENT, false);
176-
if (!OidIsValid(spcid))
177-
spcid = MyDatabaseTableSpace;
178-
atcmd->name = get_tablespace_name(spcid);
179-
}
161+
/* use empty string to specify default tablespace */
162+
atcmd->name = into->tableSpaceName ? into->tableSpaceName : "";
180163
atcmds = lappend(atcmds, atcmd);
181164

182165
/* storage options */

src/backend/commands/tablecmds.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16608,8 +16608,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
1660816608
{
1660916609
Oid tablespaceId;
1661016610

16611-
/* Check that the tablespace exists */
16612-
tablespaceId = get_tablespace_oid(tablespacename, false);
16611+
if (tablespacename != NULL && tablespacename[0] == '\0')
16612+
{
16613+
/* Use default tablespace if name is empty string */
16614+
tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
16615+
if (!OidIsValid(tablespaceId))
16616+
tablespaceId = MyDatabaseTableSpace;
16617+
}
16618+
else
16619+
{
16620+
/* Check that the tablespace exists */
16621+
tablespaceId = get_tablespace_oid(tablespacename, false);
16622+
}
1661316623

1661416624
/* Check permissions except when moving to database's default */
1661516625
if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)

0 commit comments

Comments
 (0)