Skip to content

Commit 0e115f5

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 cd91b02 commit 0e115f5

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
@@ -16583,8 +16583,18 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, const char *tablespacen
1658316583
{
1658416584
Oid tablespaceId;
1658516585

16586-
/* Check that the tablespace exists */
16587-
tablespaceId = get_tablespace_oid(tablespacename, false);
16586+
if (tablespacename != NULL && tablespacename[0] == '\0')
16587+
{
16588+
/* Use default tablespace if name is empty string */
16589+
tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence, rel->rd_rel->relispartition);
16590+
if (!OidIsValid(tablespaceId))
16591+
tablespaceId = MyDatabaseTableSpace;
16592+
}
16593+
else
16594+
{
16595+
/* Check that the tablespace exists */
16596+
tablespaceId = get_tablespace_oid(tablespacename, false);
16597+
}
1658816598

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

0 commit comments

Comments
 (0)