Skip to content

Commit e70c428

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 2346c38 commit e70c428

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
@@ -6829,7 +6829,6 @@ static void
68296829
ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename)
68306830
{
68316831
Oid tablespaceId;
6832-
AclResult aclresult;
68336832

68346833
/* Check that the tablespace exists */
68356834
tablespaceId = get_tablespace_oid(tablespacename);
@@ -6838,16 +6837,22 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename)
68386837
(errcode(ERRCODE_UNDEFINED_OBJECT),
68396838
errmsg("tablespace \"%s\" does not exist", tablespacename)));
68406839

6841-
/* Check its permissions */
6842-
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
6843-
if (aclresult != ACLCHECK_OK)
6844-
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
6840+
/* Check permissions except when moving to database's default */
6841+
if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
6842+
{
6843+
AclResult aclresult;
6844+
6845+
aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
6846+
if (aclresult != ACLCHECK_OK)
6847+
aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
6848+
}
68456849

68466850
/* Save info for Phase 3 to do the real work */
68476851
if (OidIsValid(tab->newTableSpace))
68486852
ereport(ERROR,
68496853
(errcode(ERRCODE_SYNTAX_ERROR),
68506854
errmsg("cannot have multiple SET TABLESPACE subcommands")));
6855+
68516856
tab->newTableSpace = tablespaceId;
68526857
}
68536858

0 commit comments

Comments
 (0)