Skip to content

Commit 86e58ae

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 586bea6 commit 86e58ae

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
@@ -8534,21 +8534,26 @@ static void
85348534
ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename, LOCKMODE lockmode)
85358535
{
85368536
Oid tablespaceId;
8537-
AclResult aclresult;
85388537

85398538
/* Check that the tablespace exists */
85408539
tablespaceId = get_tablespace_oid(tablespacename, false);
85418540

8542-
/* Check its permissions */
8543-
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
8544-
if (aclresult != ACLCHECK_OK)
8545-
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
8541+
/* Check permissions except when moving to database's default */
8542+
if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
8543+
{
8544+
AclResult aclresult;
8545+
8546+
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
8547+
if (aclresult != ACLCHECK_OK)
8548+
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
8549+
}
85468550

85478551
/* Save info for Phase 3 to do the real work */
85488552
if (OidIsValid(tab->newTableSpace))
85498553
ereport(ERROR,
85508554
(errcode(ERRCODE_SYNTAX_ERROR),
85518555
errmsg("cannot have multiple SET TABLESPACE subcommands")));
8556+
85528557
tab->newTableSpace = tablespaceId;
85538558
}
85548559

0 commit comments

Comments
 (0)