]> granicus.if.org Git - postgresql/commitdiff
Allow SET TABLESPACE to database default
authorStephen Frost <sfrost@snowman.net>
Sat, 18 Jan 2014 23:41:52 +0000 (18:41 -0500)
committerStephen Frost <sfrost@snowman.net>
Sat, 18 Jan 2014 23:50:47 +0000 (18:50 -0500)
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.

src/backend/commands/tablecmds.c

index b0dac88471ef1bfbd526d3cbfd0b203243a198da..541c75ab962f8c371afac675b8a3f0bd82572b42 100644 (file)
@@ -6557,7 +6557,6 @@ static void
 ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename)
 {
        Oid                     tablespaceId;
-       AclResult       aclresult;
 
        /* Check that the tablespace exists */
        tablespaceId = get_tablespace_oid(tablespacename);
@@ -6566,16 +6565,22 @@ ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel, char *tablespacename)
                                (errcode(ERRCODE_UNDEFINED_OBJECT),
                                 errmsg("tablespace \"%s\" does not exist", tablespacename)));
 
-       /* Check its permissions */
-       aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
-       if (aclresult != ACLCHECK_OK)
-               aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
+       /* Check permissions except when moving to database's default */
+       if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
+       {
+               AclResult       aclresult;
+
+               aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(), ACL_CREATE);
+               if (aclresult != ACLCHECK_OK)
+                       aclcheck_error(aclresult, ACL_KIND_TABLESPACE, tablespacename);
+       }
 
        /* Save info for Phase 3 to do the real work */
        if (OidIsValid(tab->newTableSpace))
                ereport(ERROR,
                                (errcode(ERRCODE_SYNTAX_ERROR),
                                 errmsg("cannot have multiple SET TABLESPACE subcommands")));
+
        tab->newTableSpace = tablespaceId;
 }