Skip to content

Commit cfbfdc5

Browse files
committed
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
1 parent d0b4399 commit cfbfdc5

File tree

11 files changed

+128
-14
lines changed

11 files changed

+128
-14
lines changed

doc/src/sgml/ref/alter_database.sgml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_database.sgml,v 1.11 2003/11/29 19:51:38 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_database.sgml,v 1.12 2004/05/26 13:56:42 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -23,6 +23,8 @@ PostgreSQL documentation
2323
ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> SET <replaceable>parameter</replaceable> { TO | = } { <replaceable>value</replaceable> | DEFAULT }
2424
ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> RESET <replaceable>parameter</replaceable>
2525

26+
ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
27+
2628
ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable>newname</replaceable>
2729
</synopsis>
2830
</refsynopsisdiv>
@@ -54,6 +56,11 @@ ALTER DATABASE <replaceable class="PARAMETER">name</replaceable> RENAME TO <repl
5456
be renamed. (Connect to a different database if you need to do
5557
that.)
5658
</para>
59+
60+
<para>
61+
The fourth form changes the owner of the database. Only a superuser
62+
can change the database's owner.
63+
</para>
5764
</refsect1>
5865

5966
<refsect1>

src/backend/commands/dbcommands.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.133 2004/05/26 04:41:10 neilc Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.134 2004/05/26 13:56:45 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -776,6 +776,52 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
776776
}
777777

778778

779+
/*
780+
* ALTER DATABASE name OWNER TO newowner
781+
*/
782+
void
783+
AlterDatabaseOwner(const char *dbname, const char *newowner)
784+
{
785+
AclId newdatdba;
786+
HeapTuple tuple,
787+
newtuple;
788+
Relation rel;
789+
ScanKeyData scankey;
790+
SysScanDesc scan;
791+
792+
rel = heap_openr(DatabaseRelationName, RowExclusiveLock);
793+
ScanKeyInit(&scankey,
794+
Anum_pg_database_datname,
795+
BTEqualStrategyNumber, F_NAMEEQ,
796+
NameGetDatum(dbname));
797+
scan = systable_beginscan(rel, DatabaseNameIndex, true,
798+
SnapshotNow, 1, &scankey);
799+
tuple = systable_getnext(scan);
800+
if (!HeapTupleIsValid(tuple))
801+
ereport(ERROR,
802+
(errcode(ERRCODE_UNDEFINED_DATABASE),
803+
errmsg("database \"%s\" does not exist", dbname)));
804+
805+
/* obtain sysid of proposed owner */
806+
newdatdba = get_usesysid(newowner); /* will ereport if no such user */
807+
808+
/* changing owner's database for someone else: must be superuser */
809+
/* note that the someone else need not have any permissions */
810+
if (!superuser())
811+
ereport(ERROR,
812+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
813+
errmsg("must be superuser to change owner's database for another user")));
814+
815+
/* change owner */
816+
newtuple = heap_copytuple(tuple);
817+
((Form_pg_database) GETSTRUCT(newtuple))->datdba = newdatdba;
818+
simple_heap_update(rel, &tuple->t_self, newtuple);
819+
CatalogUpdateIndexes(rel, newtuple);
820+
821+
systable_endscan(scan);
822+
heap_close(rel, NoLock);
823+
}
824+
779825

780826
/*
781827
* Helper functions

src/backend/nodes/copyfuncs.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.282 2004/05/26 04:41:18 neilc Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.283 2004/05/26 13:56:47 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2068,6 +2068,17 @@ _copyCreatedbStmt(CreatedbStmt *from)
20682068
return newnode;
20692069
}
20702070

2071+
static AlterDbOwnerStmt *
2072+
_copyAlterDbOwnerStmt(AlterDbOwnerStmt *from)
2073+
{
2074+
AlterDbOwnerStmt *newnode = makeNode(AlterDbOwnerStmt);
2075+
2076+
COPY_STRING_FIELD(dbname);
2077+
COPY_STRING_FIELD(uname);
2078+
2079+
return newnode;
2080+
}
2081+
20712082
static AlterDatabaseSetStmt *
20722083
_copyAlterDatabaseSetStmt(AlterDatabaseSetStmt *from)
20732084
{
@@ -2860,6 +2871,9 @@ copyObject(void *from)
28602871
case T_CreatedbStmt:
28612872
retval = _copyCreatedbStmt(from);
28622873
break;
2874+
case T_AlterDbOwnerStmt:
2875+
retval = _copyAlterDbOwnerStmt(from);
2876+
break;
28632877
case T_AlterDatabaseSetStmt:
28642878
retval = _copyAlterDatabaseSetStmt(from);
28652879
break;

src/backend/nodes/equalfuncs.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.221 2004/05/26 04:41:19 neilc Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.222 2004/05/26 13:56:47 momjian Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1099,6 +1099,15 @@ _equalCreatedbStmt(CreatedbStmt *a, CreatedbStmt *b)
10991099
return true;
11001100
}
11011101

1102+
static bool
1103+
_equalAlterDbOwnerStmt(AlterDbOwnerStmt *a, AlterDbOwnerStmt *b)
1104+
{
1105+
COMPARE_STRING_FIELD(dbname);
1106+
COMPARE_STRING_FIELD(uname);
1107+
1108+
return true;
1109+
}
1110+
11021111
static bool
11031112
_equalAlterDatabaseSetStmt(AlterDatabaseSetStmt *a, AlterDatabaseSetStmt *b)
11041113
{
@@ -2005,6 +2014,9 @@ equal(void *a, void *b)
20052014
case T_CreatedbStmt:
20062015
retval = _equalCreatedbStmt(a, b);
20072016
break;
2017+
case T_AlterDbOwnerStmt:
2018+
retval = _equalAlterDbOwnerStmt(a, b);
2019+
break;
20082020
case T_AlterDatabaseSetStmt:
20092021
retval = _equalAlterDatabaseSetStmt(a, b);
20102022
break;

src/backend/parser/gram.y

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.455 2004/05/26 04:41:29 neilc Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.456 2004/05/26 13:56:51 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -152,6 +152,7 @@ static void doNegateFloat(Value *v);
152152
VariableResetStmt VariableSetStmt VariableShowStmt
153153
ViewStmt CheckPointStmt CreateConversionStmt
154154
DeallocateStmt PrepareStmt ExecuteStmt
155+
AlterDbOwnerStmt
155156

156157
%type <node> select_no_parens select_with_parens select_clause
157158
simple_select
@@ -486,7 +487,8 @@ stmtmulti: stmtmulti ';' stmt
486487
;
487488

488489
stmt :
489-
AlterDatabaseSetStmt
490+
AlterDbOwnerStmt
491+
| AlterDatabaseSetStmt
490492
| AlterDomainStmt
491493
| AlterGroupStmt
492494
| AlterSeqStmt
@@ -3918,6 +3920,15 @@ opt_equal: '=' {}
39183920
*
39193921
*****************************************************************************/
39203922

3923+
AlterDbOwnerStmt: ALTER DATABASE database_name OWNER TO UserId
3924+
{
3925+
AlterDbOwnerStmt *n = makeNode(AlterDbOwnerStmt);
3926+
n->dbname = $3;
3927+
n->uname = $6;
3928+
$$ = (Node *)n;
3929+
}
3930+
;
3931+
39213932
AlterDatabaseSetStmt:
39223933
ALTER DATABASE database_name SET set_rest
39233934
{

src/backend/tcop/utility.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.216 2004/05/26 04:41:35 neilc Exp $
13+
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.217 2004/05/26 13:56:54 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -234,6 +234,7 @@ check_xact_readonly(Node *parsetree)
234234
switch (nodeTag(parsetree))
235235
{
236236
case T_AlterDatabaseSetStmt:
237+
case T_AlterDbOwnerStmt:
237238
case T_AlterDomainStmt:
238239
case T_AlterGroupStmt:
239240
case T_AlterSeqStmt:
@@ -675,6 +676,13 @@ ProcessUtility(Node *parsetree,
675676
createdb((CreatedbStmt *) parsetree);
676677
break;
677678

679+
case T_AlterDbOwnerStmt:
680+
{
681+
AlterDbOwnerStmt *stmt = (AlterDbOwnerStmt *) parsetree;
682+
AlterDatabaseOwner(stmt->dbname, stmt->uname);
683+
}
684+
break;
685+
678686
case T_AlterDatabaseSetStmt:
679687
AlterDatabaseSet((AlterDatabaseSetStmt *) parsetree);
680688
break;
@@ -1303,6 +1311,10 @@ CreateCommandTag(Node *parsetree)
13031311
tag = "CREATE DATABASE";
13041312
break;
13051313

1314+
case T_AlterDbOwnerStmt:
1315+
tag = "ALTER DATABASE";
1316+
break;
1317+
13061318
case T_AlterDatabaseSetStmt:
13071319
tag = "ALTER DATABASE";
13081320
break;

src/bin/psql/tab-complete.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.106 2004/05/12 13:38:46 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.107 2004/05/26 13:56:55 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -642,7 +642,7 @@ psql_completion(char *text, int start, int end)
642642
pg_strcasecmp(prev2_wd, "DATABASE") == 0)
643643
{
644644
static const char *const list_ALTERDATABASE[] =
645-
{"RESET", "SET", "RENAME TO", NULL};
645+
{"RESET", "SET", "OWNER TO", "RENAME TO", NULL};
646646

647647
COMPLETE_WITH_LIST(list_ALTERDATABASE);
648648
}

src/include/commands/dbcommands.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.30 2003/11/29 22:40:59 pgsql Exp $
10+
* $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.31 2004/05/26 13:56:59 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,6 +20,7 @@ extern void createdb(const CreatedbStmt *stmt);
2020
extern void dropdb(const char *dbname);
2121
extern void RenameDatabase(const char *oldname, const char *newname);
2222
extern void AlterDatabaseSet(AlterDatabaseSetStmt *stmt);
23+
extern void AlterDatabaseOwner(const char *dbname, const char *uname);
2324

2425
extern Oid get_database_oid(const char *dbname);
2526
extern char *get_database_name(Oid dbid);

src/include/nodes/nodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.155 2004/05/26 04:41:45 neilc Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.156 2004/05/26 13:57:02 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -266,6 +266,7 @@ typedef enum NodeTag
266266
T_ExecuteStmt,
267267
T_DeallocateStmt,
268268
T_DeclareCursorStmt,
269+
T_AlterDbOwnerStmt,
269270

270271
T_A_Expr = 800,
271272
T_ColumnRef,

src/include/nodes/parsenodes.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.255 2004/05/05 04:48:47 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.256 2004/05/26 13:57:02 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1523,6 +1523,13 @@ typedef struct CreatedbStmt
15231523
* Alter Database
15241524
* ----------------------
15251525
*/
1526+
typedef struct AlterDbOwnerStmt
1527+
{
1528+
NodeTag type;
1529+
char *dbname;
1530+
char *uname;
1531+
} AlterDbOwnerStmt;
1532+
15261533
typedef struct AlterDatabaseSetStmt
15271534
{
15281535
NodeTag type;

src/interfaces/ecpg/preproc/preproc.y

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.283 2004/05/21 13:50:12 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.284 2004/05/26 13:57:04 momjian Exp $ */
22

33
/* Copyright comment */
44
%{
@@ -519,7 +519,6 @@ add_additional_variables(char *name, bool insert)
519519
%type <str> CharacterWithoutLength BitWithLength BitWithoutLength
520520
%type <str> ConstBit GenericType TableFuncElementList opt_analyze
521521
%type <str> opt_sort_clause transaction_access_mode subquery_Op
522-
523522
%type <str> ECPGWhenever ECPGConnect connection_target ECPGOpen
524523
%type <str> indicator ECPGExecute ECPGPrepare ecpg_using ecpg_into
525524
%type <str> storage_declaration storage_clause opt_initializer c_anything
@@ -544,6 +543,7 @@ add_additional_variables(char *name, bool insert)
544543
%type <str> inf_val_list inf_col_list using_descriptor into_descriptor
545544
%type <str> ecpg_into_using prepared_name struct_union_type_with_symbol
546545
%type <str> ECPGunreserved ECPGunreserved_interval cvariable
546+
%type <str> AlterDatabaseOwnerStmt
547547

548548
%type <struct_union> s_struct_union_symbol
549549

@@ -594,6 +594,7 @@ opt_at: AT connection_target
594594
};
595595

596596
stmt: AlterDatabaseSetStmt { output_statement($1, 0, connection); }
597+
| AlterDatabaseOwnerStmt { output_statement($1, 0, connection); }
597598
| AlterDomainStmt { output_statement($1, 0, connection); }
598599
| AlterGroupStmt { output_statement($1, 0, connection); }
599600
| AlterSeqStmt { output_statement($1, 0, connection); }
@@ -2535,6 +2536,8 @@ opt_equal: '=' { $$ = make_str("="); }
25352536
*
25362537
*****************************************************************************/
25372538

2539+
AlterDatabaseOwnerStmt: ALTER DATABASE database_name OWNER TO UserId
2540+
{ $$ = cat_str(4, make_str("alter database"), $3, make_str("owner to"), $6); }
25382541
AlterDatabaseSetStmt: ALTER DATABASE database_name SET set_rest
25392542
{ $$ = cat_str(4, make_str("alter database"), $3, make_str("set"), $5); }
25402543
| ALTER DATABASE database_name VariableResetStmt

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy