mirror of
https://github.com/postgres/postgres.git
synced 2025-11-12 05:01:15 +03:00
This patch implement the TODO [ALTER DATABASE foo OWNER TO bar].
It was necessary to touch in grammar and create a new node to make home to the new syntax. The command is also supported in E CPG. Doc updates are attached too. Only superusers can change the owner of the database. New owners don't need any aditional privileges. Euler Taveira de Oliveira
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.133 2004/05/26 04:41:10 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.134 2004/05/26 13:56:45 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -776,6 +776,52 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ALTER DATABASE name OWNER TO newowner
|
||||
*/
|
||||
void
|
||||
AlterDatabaseOwner(const char *dbname, const char *newowner)
|
||||
{
|
||||
AclId newdatdba;
|
||||
HeapTuple tuple,
|
||||
newtuple;
|
||||
Relation rel;
|
||||
ScanKeyData scankey;
|
||||
SysScanDesc scan;
|
||||
|
||||
rel = heap_openr(DatabaseRelationName, RowExclusiveLock);
|
||||
ScanKeyInit(&scankey,
|
||||
Anum_pg_database_datname,
|
||||
BTEqualStrategyNumber, F_NAMEEQ,
|
||||
NameGetDatum(dbname));
|
||||
scan = systable_beginscan(rel, DatabaseNameIndex, true,
|
||||
SnapshotNow, 1, &scankey);
|
||||
tuple = systable_getnext(scan);
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_DATABASE),
|
||||
errmsg("database \"%s\" does not exist", dbname)));
|
||||
|
||||
/* obtain sysid of proposed owner */
|
||||
newdatdba = get_usesysid(newowner); /* will ereport if no such user */
|
||||
|
||||
/* changing owner's database for someone else: must be superuser */
|
||||
/* note that the someone else need not have any permissions */
|
||||
if (!superuser())
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
errmsg("must be superuser to change owner's database for another user")));
|
||||
|
||||
/* change owner */
|
||||
newtuple = heap_copytuple(tuple);
|
||||
((Form_pg_database) GETSTRUCT(newtuple))->datdba = newdatdba;
|
||||
simple_heap_update(rel, &tuple->t_self, newtuple);
|
||||
CatalogUpdateIndexes(rel, newtuple);
|
||||
|
||||
systable_endscan(scan);
|
||||
heap_close(rel, NoLock);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Helper functions
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.282 2004/05/26 04:41:18 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.283 2004/05/26 13:56:47 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -2068,6 +2068,17 @@ _copyCreatedbStmt(CreatedbStmt *from)
|
||||
return newnode;
|
||||
}
|
||||
|
||||
static AlterDbOwnerStmt *
|
||||
_copyAlterDbOwnerStmt(AlterDbOwnerStmt *from)
|
||||
{
|
||||
AlterDbOwnerStmt *newnode = makeNode(AlterDbOwnerStmt);
|
||||
|
||||
COPY_STRING_FIELD(dbname);
|
||||
COPY_STRING_FIELD(uname);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
static AlterDatabaseSetStmt *
|
||||
_copyAlterDatabaseSetStmt(AlterDatabaseSetStmt *from)
|
||||
{
|
||||
@@ -2860,6 +2871,9 @@ copyObject(void *from)
|
||||
case T_CreatedbStmt:
|
||||
retval = _copyCreatedbStmt(from);
|
||||
break;
|
||||
case T_AlterDbOwnerStmt:
|
||||
retval = _copyAlterDbOwnerStmt(from);
|
||||
break;
|
||||
case T_AlterDatabaseSetStmt:
|
||||
retval = _copyAlterDatabaseSetStmt(from);
|
||||
break;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.221 2004/05/26 04:41:19 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.222 2004/05/26 13:56:47 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -1099,6 +1099,15 @@ _equalCreatedbStmt(CreatedbStmt *a, CreatedbStmt *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalAlterDbOwnerStmt(AlterDbOwnerStmt *a, AlterDbOwnerStmt *b)
|
||||
{
|
||||
COMPARE_STRING_FIELD(dbname);
|
||||
COMPARE_STRING_FIELD(uname);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalAlterDatabaseSetStmt(AlterDatabaseSetStmt *a, AlterDatabaseSetStmt *b)
|
||||
{
|
||||
@@ -2005,6 +2014,9 @@ equal(void *a, void *b)
|
||||
case T_CreatedbStmt:
|
||||
retval = _equalCreatedbStmt(a, b);
|
||||
break;
|
||||
case T_AlterDbOwnerStmt:
|
||||
retval = _equalAlterDbOwnerStmt(a, b);
|
||||
break;
|
||||
case T_AlterDatabaseSetStmt:
|
||||
retval = _equalAlterDatabaseSetStmt(a, b);
|
||||
break;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.455 2004/05/26 04:41:29 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.456 2004/05/26 13:56:51 momjian Exp $
|
||||
*
|
||||
* HISTORY
|
||||
* AUTHOR DATE MAJOR EVENT
|
||||
@@ -152,6 +152,7 @@ static void doNegateFloat(Value *v);
|
||||
VariableResetStmt VariableSetStmt VariableShowStmt
|
||||
ViewStmt CheckPointStmt CreateConversionStmt
|
||||
DeallocateStmt PrepareStmt ExecuteStmt
|
||||
AlterDbOwnerStmt
|
||||
|
||||
%type <node> select_no_parens select_with_parens select_clause
|
||||
simple_select
|
||||
@@ -486,7 +487,8 @@ stmtmulti: stmtmulti ';' stmt
|
||||
;
|
||||
|
||||
stmt :
|
||||
AlterDatabaseSetStmt
|
||||
AlterDbOwnerStmt
|
||||
| AlterDatabaseSetStmt
|
||||
| AlterDomainStmt
|
||||
| AlterGroupStmt
|
||||
| AlterSeqStmt
|
||||
@@ -3918,6 +3920,15 @@ opt_equal: '=' {}
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
AlterDbOwnerStmt: ALTER DATABASE database_name OWNER TO UserId
|
||||
{
|
||||
AlterDbOwnerStmt *n = makeNode(AlterDbOwnerStmt);
|
||||
n->dbname = $3;
|
||||
n->uname = $6;
|
||||
$$ = (Node *)n;
|
||||
}
|
||||
;
|
||||
|
||||
AlterDatabaseSetStmt:
|
||||
ALTER DATABASE database_name SET set_rest
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.216 2004/05/26 04:41:35 neilc Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.217 2004/05/26 13:56:54 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -234,6 +234,7 @@ check_xact_readonly(Node *parsetree)
|
||||
switch (nodeTag(parsetree))
|
||||
{
|
||||
case T_AlterDatabaseSetStmt:
|
||||
case T_AlterDbOwnerStmt:
|
||||
case T_AlterDomainStmt:
|
||||
case T_AlterGroupStmt:
|
||||
case T_AlterSeqStmt:
|
||||
@@ -675,6 +676,13 @@ ProcessUtility(Node *parsetree,
|
||||
createdb((CreatedbStmt *) parsetree);
|
||||
break;
|
||||
|
||||
case T_AlterDbOwnerStmt:
|
||||
{
|
||||
AlterDbOwnerStmt *stmt = (AlterDbOwnerStmt *) parsetree;
|
||||
AlterDatabaseOwner(stmt->dbname, stmt->uname);
|
||||
}
|
||||
break;
|
||||
|
||||
case T_AlterDatabaseSetStmt:
|
||||
AlterDatabaseSet((AlterDatabaseSetStmt *) parsetree);
|
||||
break;
|
||||
@@ -1303,6 +1311,10 @@ CreateCommandTag(Node *parsetree)
|
||||
tag = "CREATE DATABASE";
|
||||
break;
|
||||
|
||||
case T_AlterDbOwnerStmt:
|
||||
tag = "ALTER DATABASE";
|
||||
break;
|
||||
|
||||
case T_AlterDatabaseSetStmt:
|
||||
tag = "ALTER DATABASE";
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user