Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfbfdc5

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 hometo the new syntax. The command is also supported in ECPG. Doc updates are attached too. Only superusers can change the ownerof the database. New owners don't need any aditionalprivileges.Euler Taveira de Oliveira
1 parentd0b4399 commitcfbfdc5

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/2604:41:10 neilc Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.134 2004/05/2613: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(constchar*dbname,constchar*newowner)
784+
{
785+
AclIdnewdatdba;
786+
HeapTupletuple,
787+
newtuple;
788+
Relationrel;
789+
ScanKeyDatascankey;
790+
SysScanDescscan;
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/2604:41:18 neilc Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.283 2004/05/2613:56:47 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2068,6 +2068,17 @@ _copyCreatedbStmt(CreatedbStmt *from)
20682068
returnnewnode;
20692069
}
20702070

2071+
staticAlterDbOwnerStmt*
2072+
_copyAlterDbOwnerStmt(AlterDbOwnerStmt*from)
2073+
{
2074+
AlterDbOwnerStmt*newnode=makeNode(AlterDbOwnerStmt);
2075+
2076+
COPY_STRING_FIELD(dbname);
2077+
COPY_STRING_FIELD(uname);
2078+
2079+
returnnewnode;
2080+
}
2081+
20712082
staticAlterDatabaseSetStmt*
20722083
_copyAlterDatabaseSetStmt(AlterDatabaseSetStmt*from)
20732084
{
@@ -2860,6 +2871,9 @@ copyObject(void *from)
28602871
caseT_CreatedbStmt:
28612872
retval=_copyCreatedbStmt(from);
28622873
break;
2874+
caseT_AlterDbOwnerStmt:
2875+
retval=_copyAlterDbOwnerStmt(from);
2876+
break;
28632877
caseT_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/2604:41:19 neilc Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.222 2004/05/2613:56:47 momjian Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1099,6 +1099,15 @@ _equalCreatedbStmt(CreatedbStmt *a, CreatedbStmt *b)
10991099
return true;
11001100
}
11011101

1102+
staticbool
1103+
_equalAlterDbOwnerStmt(AlterDbOwnerStmt*a,AlterDbOwnerStmt*b)
1104+
{
1105+
COMPARE_STRING_FIELD(dbname);
1106+
COMPARE_STRING_FIELD(uname);
1107+
1108+
return true;
1109+
}
1110+
11021111
staticbool
11031112
_equalAlterDatabaseSetStmt(AlterDatabaseSetStmt*a,AlterDatabaseSetStmt*b)
11041113
{
@@ -2005,6 +2014,9 @@ equal(void *a, void *b)
20052014
caseT_CreatedbStmt:
20062015
retval=_equalCreatedbStmt(a,b);
20072016
break;
2017+
caseT_AlterDbOwnerStmt:
2018+
retval=_equalAlterDbOwnerStmt(a,b);
2019+
break;
20082020
caseT_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/2604:41:29 neilc Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.456 2004/05/2613:56:51 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -152,6 +152,7 @@ static void doNegateFloat(Value *v);
152152
VariableResetStmtVariableSetStmtVariableShowStmt
153153
ViewStmtCheckPointStmtCreateConversionStmt
154154
DeallocateStmtPrepareStmtExecuteStmt
155+
AlterDbOwnerStmt
155156

156157
%type<node>select_no_parensselect_with_parensselect_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/2604:41:35 neilc Exp $
13+
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.217 2004/05/2613:56:54 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -234,6 +234,7 @@ check_xact_readonly(Node *parsetree)
234234
switch (nodeTag(parsetree))
235235
{
236236
caseT_AlterDatabaseSetStmt:
237+
caseT_AlterDbOwnerStmt:
237238
caseT_AlterDomainStmt:
238239
caseT_AlterGroupStmt:
239240
caseT_AlterSeqStmt:
@@ -675,6 +676,13 @@ ProcessUtility(Node *parsetree,
675676
createdb((CreatedbStmt*)parsetree);
676677
break;
677678

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

1314+
caseT_AlterDbOwnerStmt:
1315+
tag="ALTER DATABASE";
1316+
break;
1317+
13061318
caseT_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
staticconstchar*constlist_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:59pgsql Exp $
10+
* $PostgreSQL: pgsql/src/include/commands/dbcommands.h,v 1.31 2004/05/26 13:56:59momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,6 +20,7 @@ extern void createdb(const CreatedbStmt *stmt);
2020
externvoiddropdb(constchar*dbname);
2121
externvoidRenameDatabase(constchar*oldname,constchar*newname);
2222
externvoidAlterDatabaseSet(AlterDatabaseSetStmt*stmt);
23+
externvoidAlterDatabaseOwner(constchar*dbname,constchar*uname);
2324

2425
externOidget_database_oid(constchar*dbname);
2526
externchar*get_database_name(Oiddbid);

‎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/2604:41:45 neilc Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.156 2004/05/2613: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+
typedefstructAlterDbOwnerStmt
1527+
{
1528+
NodeTagtype;
1529+
char*dbname;
1530+
char*uname;
1531+
}AlterDbOwnerStmt;
1532+
15261533
typedefstructAlterDatabaseSetStmt
15271534
{
15281535
NodeTagtype;

‎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>CharacterWithoutLengthBitWithLengthBitWithoutLength
520520
%type<str>ConstBitGenericTypeTableFuncElementListopt_analyze
521521
%type<str>opt_sort_clausetransaction_access_modesubquery_Op
522-
523522
%type<str>ECPGWheneverECPGConnectconnection_targetECPGOpen
524523
%type<str>indicatorECPGExecuteECPGPrepareecpg_usingecpg_into
525524
%type<str>storage_declarationstorage_clauseopt_initializerc_anything
@@ -544,6 +543,7 @@ add_additional_variables(char *name, bool insert)
544543
%type<str>inf_val_listinf_col_listusing_descriptorinto_descriptor
545544
%type<str>ecpg_into_usingprepared_namestruct_union_type_with_symbol
546545
%type<str>ECPGunreservedECPGunreserved_intervalcvariable
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:ALTERDATABASEdatabase_nameOWNERTOUserId
2540+
{$$ = cat_str(4, make_str("alter database"),$3, make_str("owner to"),$6); }
25382541
AlterDatabaseSetStmt:ALTERDATABASEdatabase_nameSETset_rest
25392542
{$$ = cat_str(4, make_str("alter database"),$3, make_str("set"),$5); }
25402543
|ALTERDATABASEdatabase_nameVariableResetStmt

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp