Skip to content

Commit 1fe0659

Browse files
committed
Allow SET TABLESPACE to database default
We've always allowed CREATE TABLE to create tables in the database's default tablespace without checking for CREATE permissions on that tablespace. Unfortunately, the original implementation of ALTER TABLE ... SET TABLESPACE didn't pick up on that exception. This changes ALTER TABLE ... SET TABLESPACE to allow the database's default tablespace without checking for CREATE rights on that tablespace, just as CREATE TABLE works today. Users could always do this through a series of commands (CREATE TABLE ... AS SELECT * FROM ...; DROP TABLE ...; etc), so let's fix the oversight in SET TABLESPACE's original implementation.
1 parent 526e387 commit 1fe0659

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/backend/commands/tablecmds.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8462,21 +8462,26 @@ static void
84628462
ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename, LOCKMODE lockmode)
84638463
{
84648464
Oid tablespaceId;
8465-
AclResult aclresult;
84668465

84678466
/* Check that the tablespace exists */
84688467
tablespaceId = get_tablespace_oid(tablespacename, false);
84698468

8470-
/* Check its permissions */
8471-
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
8472-
if (aclresult != ACLCHECK_OK)
8473-
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
8469+
/* Check permissions except when moving to database's default */
8470+
if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
8471+
{
8472+
AclResult aclresult;
8473+
8474+
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
8475+
if (aclresult != ACLCHECK_OK)
8476+
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
8477+
}
84748478

84758479
/* Save info for Phase 3 to do the real work */
84768480
if (OidIsValid(tab->newTableSpace))
84778481
ereport(ERROR,
84788482
(errcode(ERRCODE_SYNTAX_ERROR),
84798483
errmsg("cannot have multiple SET TABLESPACE subcommands")));
8484+
84808485
tab->newTableSpace = tablespaceId;
84818486
}
84828487

0 commit comments

Comments
 (0)