@@ -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 *
0 commit comments