Skip to content

Commit 5bcf8ed

Browse files
committed
Add ALTER FOREIGN DATA WRAPPER / RENAME and ALTER SERVER / RENAME
1 parent 9f0d2bd commit 5bcf8ed

File tree

8 files changed

+183
-1
lines changed

8 files changed

+183
-1
lines changed

doc/src/sgml/ref/alter_foreign_data_wrapper.sgml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ALTER FOREIGN DATA WRAPPER <replaceable class="parameter">name</replaceable>
2626
[ VALIDATOR <replaceable class="parameter">validator_function</replaceable> | NO VALIDATOR ]
2727
[ OPTIONS ( [ ADD | SET | DROP ] <replaceable class="PARAMETER">option</replaceable> ['<replaceable class="PARAMETER">value</replaceable>'] [, ... ]) ]
2828
ALTER FOREIGN DATA WRAPPER <replaceable class="parameter">name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
29+
ALTER FOREIGN DATA WRAPPER <replaceable class="parameter">name</replaceable> RENAME TO <replaceable>new_name</replaceable>
2930
</synopsis>
3031
</refsynopsisdiv>
3132

@@ -122,6 +123,24 @@ ALTER FOREIGN DATA WRAPPER <replaceable class="parameter">name</replaceable> OWN
122123
</para>
123124
</listitem>
124125
</varlistentry>
126+
127+
<varlistentry>
128+
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
129+
<listitem>
130+
<para>
131+
The user name of the new owner of the foreign-data wrapper.
132+
</para>
133+
</listitem>
134+
</varlistentry>
135+
136+
<varlistentry>
137+
<term><replaceable class="parameter">new_name</replaceable></term>
138+
<listitem>
139+
<para>
140+
The new name for the foreign-data wrapper.
141+
</para>
142+
</listitem>
143+
</varlistentry>
125144
</variablelist>
126145
</refsect1>
127146

@@ -150,7 +169,8 @@ ALTER FOREIGN DATA WRAPPER dbi VALIDATOR bob.myvalidator;
150169
<para>
151170
<command>ALTER FOREIGN DATA WRAPPER</command> conforms to ISO/IEC
152171
9075-9 (SQL/MED), except that the <literal>HANDLER</literal>,
153-
<literal>VALIDATOR</> and <literal>OWNER TO</> clauses are extensions.
172+
<literal>VALIDATOR</>, <literal>OWNER TO</>, and <literal>RENAME</literal>
173+
clauses are extensions.
154174
</para>
155175
</refsect1>
156176

doc/src/sgml/ref/alter_server.sgml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PostgreSQL documentation
2424
ALTER SERVER <replaceable class="parameter">server_name</replaceable> [ VERSION '<replaceable class="parameter">new_version</replaceable>' ]
2525
[ OPTIONS ( [ ADD | SET | DROP ] <replaceable class="PARAMETER">option</replaceable> ['<replaceable class="PARAMETER">value</replaceable>'] [, ... ] ) ]
2626
ALTER SERVER <replaceable class="PARAMETER">server_name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
27+
ALTER SERVER <replaceable class="PARAMETER">server_name</replaceable> RENAME TO <replaceable>new_name</replaceable>
2728
</synopsis>
2829
</refsynopsisdiv>
2930

@@ -82,6 +83,24 @@ ALTER SERVER <replaceable class="PARAMETER">server_name</replaceable> OWNER TO <
8283
</para>
8384
</listitem>
8485
</varlistentry>
86+
87+
<varlistentry>
88+
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
89+
<listitem>
90+
<para>
91+
The user name of the new owner of the foreign server.
92+
</para>
93+
</listitem>
94+
</varlistentry>
95+
96+
<varlistentry>
97+
<term><replaceable class="parameter">new_name</replaceable></term>
98+
<listitem>
99+
<para>
100+
The new name for the foreign server.
101+
</para>
102+
</listitem>
103+
</varlistentry>
85104
</variablelist>
86105
</refsect1>
87106

@@ -108,6 +127,8 @@ ALTER SERVER foo VERSION '8.4' OPTIONS (SET host 'baz');
108127

109128
<para>
110129
<command>ALTER SERVER</command> conforms to ISO/IEC 9075-9 (SQL/MED).
130+
The <literal>OWNER TO</literal> and <literal>RENAME</literal> forms are
131+
PostgreSQL extensions.
111132
</para>
112133
</refsect1>
113134

src/backend/commands/alter.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ ExecRenameStmt(RenameStmt *stmt)
6565
RenameDatabase(stmt->subname, stmt->newname);
6666
break;
6767

68+
case OBJECT_FDW:
69+
RenameForeignDataWrapper(stmt->subname, stmt->newname);
70+
break;
71+
72+
case OBJECT_FOREIGN_SERVER:
73+
RenameForeignServer(stmt->subname, stmt->newname);
74+
break;
75+
6876
case OBJECT_FUNCTION:
6977
RenameFunction(stmt->object, stmt->objarg, stmt->newname);
7078
break;

src/backend/commands/foreigncmds.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,82 @@ GetUserOidFromMapping(const char *username, bool missing_ok)
200200
}
201201

202202

203+
/*
204+
* Rename foreign-data wrapper
205+
*/
206+
void
207+
RenameForeignDataWrapper(const char *oldname, const char *newname)
208+
{
209+
HeapTuple tup;
210+
Relation rel;
211+
212+
rel = heap_open(ForeignDataWrapperRelationId, RowExclusiveLock);
213+
214+
tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(oldname));
215+
if (!HeapTupleIsValid(tup))
216+
ereport(ERROR,
217+
(errcode(ERRCODE_UNDEFINED_OBJECT),
218+
errmsg("foreign-data wrapper \"%s\" does not exist", oldname)));
219+
220+
/* make sure the new name doesn't exist */
221+
if (SearchSysCacheExists1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(newname)))
222+
ereport(ERROR,
223+
(errcode(ERRCODE_DUPLICATE_OBJECT),
224+
errmsg("foreign-data wrapper \"%s\" already exists", newname)));
225+
226+
/* must be owner of FDW */
227+
if (!pg_foreign_data_wrapper_ownercheck(HeapTupleGetOid(tup), GetUserId()))
228+
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_FDW,
229+
oldname);
230+
231+
/* rename */
232+
namestrcpy(&(((Form_pg_foreign_data_wrapper) GETSTRUCT(tup))->fdwname), newname);
233+
simple_heap_update(rel, &tup->t_self, tup);
234+
CatalogUpdateIndexes(rel, tup);
235+
236+
heap_close(rel, NoLock);
237+
heap_freetuple(tup);
238+
}
239+
240+
241+
/*
242+
* Rename foreign server
243+
*/
244+
void
245+
RenameForeignServer(const char *oldname, const char *newname)
246+
{
247+
HeapTuple tup;
248+
Relation rel;
249+
250+
rel = heap_open(ForeignServerRelationId, RowExclusiveLock);
251+
252+
tup = SearchSysCacheCopy1(FOREIGNSERVERNAME, CStringGetDatum(oldname));
253+
if (!HeapTupleIsValid(tup))
254+
ereport(ERROR,
255+
(errcode(ERRCODE_UNDEFINED_OBJECT),
256+
errmsg("server \"%s\" does not exist", oldname)));
257+
258+
/* make sure the new name doesn't exist */
259+
if (SearchSysCacheExists1(FOREIGNSERVERNAME, CStringGetDatum(newname)))
260+
ereport(ERROR,
261+
(errcode(ERRCODE_DUPLICATE_OBJECT),
262+
errmsg("server \"%s\" already exists", newname)));
263+
264+
/* must be owner of server */
265+
if (!pg_foreign_server_ownercheck(HeapTupleGetOid(tup), GetUserId()))
266+
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_FOREIGN_SERVER,
267+
oldname);
268+
269+
/* rename */
270+
namestrcpy(&(((Form_pg_foreign_server) GETSTRUCT(tup))->srvname), newname);
271+
simple_heap_update(rel, &tup->t_self, tup);
272+
CatalogUpdateIndexes(rel, tup);
273+
274+
heap_close(rel, NoLock);
275+
heap_freetuple(tup);
276+
}
277+
278+
203279
/*
204280
* Change foreign-data wrapper owner.
205281
*

src/backend/parser/gram.y

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6434,6 +6434,14 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
64346434
n->newname = $6;
64356435
$$ = (Node *)n;
64366436
}
6437+
| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
6438+
{
6439+
RenameStmt *n = makeNode(RenameStmt);
6440+
n->renameType = OBJECT_FDW;
6441+
n->subname = $5;
6442+
n->newname = $8;
6443+
$$ = (Node *)n;
6444+
}
64376445
| ALTER FUNCTION function_with_argtypes RENAME TO name
64386446
{
64396447
RenameStmt *n = makeNode(RenameStmt);
@@ -6485,6 +6493,14 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
64856493
n->newname = $6;
64866494
$$ = (Node *)n;
64876495
}
6496+
| ALTER SERVER name RENAME TO name
6497+
{
6498+
RenameStmt *n = makeNode(RenameStmt);
6499+
n->renameType = OBJECT_FOREIGN_SERVER;
6500+
n->subname = $3;
6501+
n->newname = $6;
6502+
$$ = (Node *)n;
6503+
}
64886504
| ALTER TABLE relation_expr RENAME TO name
64896505
{
64906506
RenameStmt *n = makeNode(RenameStmt);

src/include/commands/defrem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ extern text *serialize_deflist(List *deflist);
146146
extern List *deserialize_deflist(Datum txt);
147147

148148
/* commands/foreigncmds.c */
149+
extern void RenameForeignServer(const char *oldname, const char *newname);
150+
extern void RenameForeignDataWrapper(const char *oldname, const char *newname);
149151
extern void AlterForeignServerOwner(const char *name, Oid newOwnerId);
150152
extern void AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId);
151153
extern void CreateForeignDataWrapper(CreateFdwStmt *stmt);

src/test/regress/expected/foreign_data.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ RESET ROLE;
177177
postgresql | foreign_data_user | - | postgresql_fdw_validator | | |
178178
(3 rows)
179179

180+
ALTER FOREIGN DATA WRAPPER foo RENAME TO foo1;
181+
\dew+
182+
List of foreign-data wrappers
183+
Name | Owner | Handler | Validator | Access privileges | FDW Options | Description
184+
------------+-------------------------+---------+--------------------------+-------------------+------------------------------+-------------
185+
dummy | foreign_data_user | - | - | | | useless
186+
foo1 | regress_test_role_super | - | - | | (b '3', c '4', a '2', d '5') |
187+
postgresql | foreign_data_user | - | postgresql_fdw_validator | | |
188+
(3 rows)
189+
190+
ALTER FOREIGN DATA WRAPPER foo1 RENAME TO foo;
180191
-- DROP FOREIGN DATA WRAPPER
181192
DROP FOREIGN DATA WRAPPER nonexistent; -- ERROR
182193
ERROR: foreign-data wrapper "nonexistent" does not exist
@@ -427,6 +438,26 @@ privileges for foreign-data wrapper foo
427438
t2 | regress_test_role | foo | | | | |
428439
(10 rows)
429440

441+
ALTER SERVER s8 RENAME to s8new;
442+
\des+
443+
List of foreign servers
444+
Name | Owner | Foreign-data wrapper | Access privileges | Type | Version | FDW Options | Description
445+
-------+-----------------------+----------------------+-----------------------------------------+--------+---------+--------------------------------------+-------------
446+
s1 | regress_test_indirect | foo | foreign_data_user=U/foreign_data_user +| | 1.1 | (servername 's1') |
447+
| | | regress_test_role=U/foreign_data_user | | | |
448+
s2 | foreign_data_user | foo | | | 1.1 | (host 'a', dbname 'b') |
449+
s3 | foreign_data_user | foo | | oracle | | ("tns name" 'orcl', port '1521') |
450+
s4 | foreign_data_user | foo | | oracle | | (host 'a', dbname 'b') |
451+
s5 | foreign_data_user | foo | | | 15.0 | |
452+
s6 | foreign_data_user | foo | foreign_data_user=U/foreign_data_user +| | 16.0 | (host 'a', dbname 'b') |
453+
| | | regress_test_role2=U*/foreign_data_user | | | |
454+
s7 | foreign_data_user | foo | | oracle | 17.0 | (host 'a', dbname 'b') |
455+
s8new | foreign_data_user | postgresql | | | | (dbname 'db1', connect_timeout '30') |
456+
t1 | regress_test_role | foo | | | | |
457+
t2 | regress_test_role | foo | | | | |
458+
(10 rows)
459+
460+
ALTER SERVER s8new RENAME to s8;
430461
-- DROP SERVER
431462
DROP SERVER nonexistent; -- ERROR
432463
ERROR: server "nonexistent" does not exist

src/test/regress/sql/foreign_data.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ ALTER FOREIGN DATA WRAPPER foo OPTIONS (ADD e '6'); -- ERROR
8484
RESET ROLE;
8585
\dew+
8686

87+
ALTER FOREIGN DATA WRAPPER foo RENAME TO foo1;
88+
\dew+
89+
ALTER FOREIGN DATA WRAPPER foo1 RENAME TO foo;
90+
8791
-- DROP FOREIGN DATA WRAPPER
8892
DROP FOREIGN DATA WRAPPER nonexistent; -- ERROR
8993
DROP FOREIGN DATA WRAPPER IF EXISTS nonexistent;
@@ -181,6 +185,10 @@ RESET ROLE;
181185
DROP ROLE regress_test_indirect; -- ERROR
182186
\des+
183187

188+
ALTER SERVER s8 RENAME to s8new;
189+
\des+
190+
ALTER SERVER s8new RENAME to s8;
191+
184192
-- DROP SERVER
185193
DROP SERVER nonexistent; -- ERROR
186194
DROP SERVER IF EXISTS nonexistent;

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