Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * foreigncmds.c
4 : : * foreign-data wrapper/server creation/manipulation commands
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/commands/foreigncmds.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : : #include "postgres.h"
15 : :
16 : : #include "access/htup_details.h"
17 : : #include "access/reloptions.h"
18 : : #include "access/table.h"
19 : : #include "access/xact.h"
20 : : #include "catalog/catalog.h"
21 : : #include "catalog/dependency.h"
22 : : #include "catalog/indexing.h"
23 : : #include "catalog/objectaccess.h"
24 : : #include "catalog/pg_foreign_data_wrapper.h"
25 : : #include "catalog/pg_foreign_server.h"
26 : : #include "catalog/pg_foreign_table.h"
27 : : #include "catalog/pg_proc.h"
28 : : #include "catalog/pg_type.h"
29 : : #include "catalog/pg_user_mapping.h"
30 : : #include "commands/defrem.h"
31 : : #include "foreign/fdwapi.h"
32 : : #include "foreign/foreign.h"
33 : : #include "miscadmin.h"
34 : : #include "parser/parse_func.h"
35 : : #include "tcop/utility.h"
36 : : #include "utils/acl.h"
37 : : #include "utils/builtins.h"
38 : : #include "utils/lsyscache.h"
39 : : #include "utils/rel.h"
40 : : #include "utils/syscache.h"
41 : :
42 : :
43 : : typedef struct
44 : : {
45 : : char *tablename;
46 : : char *cmd;
47 : : } import_error_callback_arg;
48 : :
49 : : /* Internal functions */
50 : : static void import_error_callback(void *arg);
51 : :
52 : :
53 : : /*
54 : : * Convert a DefElem list to the text array format that is used in
55 : : * pg_foreign_data_wrapper, pg_foreign_server, pg_user_mapping, and
56 : : * pg_foreign_table.
57 : : *
58 : : * Returns the array in the form of a Datum, or PointerGetDatum(NULL)
59 : : * if the list is empty.
60 : : *
61 : : * Note: The array is usually stored to database without further
62 : : * processing, hence any validation should be done before this
63 : : * conversion.
64 : : */
65 : : static Datum
6295 peter_e@gmx.net 66 :CBC 825 : optionListToArray(List *options)
67 : : {
6121 bruce@momjian.us 68 : 825 : ArrayBuildState *astate = NULL;
69 : : ListCell *cell;
70 : :
6294 tgl@sss.pgh.pa.us 71 [ + + + + : 2064 : foreach(cell, options)
+ + ]
72 : : {
6295 peter_e@gmx.net 73 : 1240 : DefElem *def = lfirst(cell);
74 : : const char *name;
75 : : const char *value;
76 : : Size len;
77 : : text *t;
78 : :
286 tgl@sss.pgh.pa.us 79 : 1240 : name = def->defname;
6295 peter_e@gmx.net 80 : 1240 : value = defGetString(def);
81 : :
82 : : /* Insist that name not contain "=", else "a=b=c" is ambiguous */
286 tgl@sss.pgh.pa.us 83 [ + + ]: 1240 : if (strchr(name, '=') != NULL)
84 [ + - ]: 1 : ereport(ERROR,
85 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
86 : : errmsg("invalid option name \"%s\": must not contain \"=\"",
87 : : name)));
88 : :
89 : 1239 : len = VARHDRSZ + strlen(name) + 1 + strlen(value);
90 : : /* +1 leaves room for sprintf's trailing null */
6295 peter_e@gmx.net 91 : 1239 : t = palloc(len + 1);
92 : 1239 : SET_VARSIZE(t, len);
286 tgl@sss.pgh.pa.us 93 : 1239 : sprintf(VARDATA(t), "%s=%s", name, value);
94 : :
6295 peter_e@gmx.net 95 : 1239 : astate = accumArrayResult(astate, PointerGetDatum(t),
96 : : false, TEXTOID,
97 : : CurrentMemoryContext);
98 : : }
99 : :
100 [ + + ]: 824 : if (astate)
101 : 543 : return makeArrayResult(astate, CurrentMemoryContext);
102 : :
103 : 281 : return PointerGetDatum(NULL);
104 : : }
105 : :
106 : :
107 : : /*
108 : : * Transform a list of DefElem into text array format. This is substantially
109 : : * the same thing as optionListToArray(), except we recognize SET/ADD/DROP
110 : : * actions for modifying an existing list of options, which is passed in
111 : : * Datum form as oldOptions. Also, if fdwvalidator isn't InvalidOid
112 : : * it specifies a validator function to call on the result.
113 : : *
114 : : * Returns the array in the form of a Datum, or PointerGetDatum(NULL)
115 : : * if the list is empty.
116 : : *
117 : : * This is used by CREATE/ALTER of FOREIGN DATA WRAPPER/SERVER/USER MAPPING/
118 : : * FOREIGN TABLE.
119 : : */
120 : : Datum
5926 heikki.linnakangas@i 121 : 837 : transformGenericOptions(Oid catalogId,
122 : : Datum oldOptions,
123 : : List *options,
124 : : Oid fdwvalidator)
125 : : {
6121 bruce@momjian.us 126 : 837 : List *resultOptions = untransformRelOptions(oldOptions);
127 : : ListCell *optcell;
128 : : Datum result;
129 : :
6189 tgl@sss.pgh.pa.us 130 [ + + + + : 1688 : foreach(optcell, options)
+ + ]
131 : : {
6121 bruce@momjian.us 132 : 863 : DefElem *od = lfirst(optcell);
133 : : ListCell *cell;
134 : :
135 : : /*
136 : : * Find the element in resultOptions. We need this for validation in
137 : : * all cases.
138 : : */
139 [ + + + + : 1950 : foreach(cell, resultOptions)
+ + ]
140 : : {
141 : 1202 : DefElem *def = lfirst(cell);
142 : :
6189 tgl@sss.pgh.pa.us 143 [ + + ]: 1202 : if (strcmp(def->defname, od->defname) == 0)
6295 peter_e@gmx.net 144 : 115 : break;
145 : : }
146 : :
147 : : /*
148 : : * It is possible to perform multiple SET/DROP actions on the same
149 : : * option. The standard permits this, as long as the options to be
150 : : * added are unique. Note that an unspecified action is taken to be
151 : : * ADD.
152 : : */
6189 tgl@sss.pgh.pa.us 153 [ + + + - ]: 863 : switch (od->defaction)
154 : : {
155 : 58 : case DEFELEM_DROP:
6295 peter_e@gmx.net 156 [ + + ]: 58 : if (!cell)
157 [ + - ]: 3 : ereport(ERROR,
158 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
159 : : errmsg("option \"%s\" not found",
160 : : od->defname)));
2435 tgl@sss.pgh.pa.us 161 : 55 : resultOptions = list_delete_cell(resultOptions, cell);
6295 peter_e@gmx.net 162 : 55 : break;
163 : :
6189 tgl@sss.pgh.pa.us 164 : 57 : case DEFELEM_SET:
6295 peter_e@gmx.net 165 [ + + ]: 57 : if (!cell)
166 [ + - ]: 3 : ereport(ERROR,
167 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
168 : : errmsg("option \"%s\" not found",
169 : : od->defname)));
6189 tgl@sss.pgh.pa.us 170 : 54 : lfirst(cell) = od;
6295 peter_e@gmx.net 171 : 54 : break;
172 : :
6189 tgl@sss.pgh.pa.us 173 : 748 : case DEFELEM_ADD:
174 : : case DEFELEM_UNSPEC:
6295 peter_e@gmx.net 175 [ + + ]: 748 : if (cell)
176 [ + - ]: 6 : ereport(ERROR,
177 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
178 : : errmsg("option \"%s\" provided more than once",
179 : : od->defname)));
6189 tgl@sss.pgh.pa.us 180 : 742 : resultOptions = lappend(resultOptions, od);
6295 peter_e@gmx.net 181 : 742 : break;
182 : :
6295 peter_e@gmx.net 183 :UBC 0 : default:
184 [ # # ]: 0 : elog(ERROR, "unrecognized action %d on option \"%s\"",
185 : : (int) od->defaction, od->defname);
186 : : break;
187 : : }
188 : : }
189 : :
6228 peter_e@gmx.net 190 :CBC 825 : result = optionListToArray(resultOptions);
191 : :
5367 tgl@sss.pgh.pa.us 192 [ + + ]: 824 : if (OidIsValid(fdwvalidator))
193 : : {
5026 bruce@momjian.us 194 : 477 : Datum valarg = result;
195 : :
196 : : /*
197 : : * Pass a null options list as an empty array, so that validators
198 : : * don't have to be declared non-strict to handle the case.
199 : : */
5367 tgl@sss.pgh.pa.us 200 [ + + ]: 477 : if (DatumGetPointer(valarg) == NULL)
201 : 68 : valarg = PointerGetDatum(construct_empty_array(TEXTOID));
202 : 477 : OidFunctionCall2(fdwvalidator, valarg, ObjectIdGetDatum(catalogId));
203 : : }
204 : :
6228 peter_e@gmx.net 205 : 746 : return result;
206 : : }
207 : :
208 : :
209 : : /*
210 : : * Internal workhorse for changing a data wrapper's owner.
211 : : *
212 : : * Allow this only for superusers; also the new owner must be a
213 : : * superuser.
214 : : */
215 : : static void
5136 alvherre@alvh.no-ip. 216 : 10 : AlterForeignDataWrapperOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
217 : : {
218 : : Form_pg_foreign_data_wrapper form;
219 : : Datum repl_val[Natts_pg_foreign_data_wrapper];
220 : : bool repl_null[Natts_pg_foreign_data_wrapper];
221 : : bool repl_repl[Natts_pg_foreign_data_wrapper];
222 : : Acl *newAcl;
223 : : Datum aclDatum;
224 : : bool isNull;
225 : :
226 : 10 : form = (Form_pg_foreign_data_wrapper) GETSTRUCT(tup);
227 : :
228 : : /* Must be a superuser to change a FDW owner */
6295 peter_e@gmx.net 229 [ + + ]: 10 : if (!superuser())
230 [ + - ]: 3 : ereport(ERROR,
231 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
232 : : errmsg("permission denied to change owner of foreign-data wrapper \"%s\"",
233 : : NameStr(form->fdwname)),
234 : : errhint("Must be superuser to change owner of a foreign-data wrapper.")));
235 : :
236 : : /* New owner must also be a superuser */
237 [ + + ]: 7 : if (!superuser_arg(newOwnerId))
238 [ + - ]: 3 : ereport(ERROR,
239 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
240 : : errmsg("permission denied to change owner of foreign-data wrapper \"%s\"",
241 : : NameStr(form->fdwname)),
242 : : errhint("The owner of a foreign-data wrapper must be a superuser.")));
243 : :
244 [ + + ]: 4 : if (form->fdwowner != newOwnerId)
245 : : {
4070 bruce@momjian.us 246 : 3 : memset(repl_null, false, sizeof(repl_null));
247 : 3 : memset(repl_repl, false, sizeof(repl_repl));
248 : :
249 : 3 : repl_repl[Anum_pg_foreign_data_wrapper_fdwowner - 1] = true;
250 : 3 : repl_val[Anum_pg_foreign_data_wrapper_fdwowner - 1] = ObjectIdGetDatum(newOwnerId);
251 : :
252 : 3 : aclDatum = heap_getattr(tup,
253 : : Anum_pg_foreign_data_wrapper_fdwacl,
254 : : RelationGetDescr(rel),
255 : : &isNull);
256 : : /* Null ACLs do not require changes */
257 [ - + ]: 3 : if (!isNull)
258 : : {
4070 bruce@momjian.us 259 :UBC 0 : newAcl = aclnewowner(DatumGetAclP(aclDatum),
260 : : form->fdwowner, newOwnerId);
261 : 0 : repl_repl[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true;
262 : 0 : repl_val[Anum_pg_foreign_data_wrapper_fdwacl - 1] = PointerGetDatum(newAcl);
263 : : }
264 : :
4070 bruce@momjian.us 265 :CBC 3 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), repl_val, repl_null,
266 : : repl_repl);
267 : :
3330 alvherre@alvh.no-ip. 268 : 3 : CatalogTupleUpdate(rel, &tup->t_self, tup);
269 : :
270 : : /* Update owner dependency reference */
6295 peter_e@gmx.net 271 : 3 : changeDependencyOnOwner(ForeignDataWrapperRelationId,
272 : : form->oid,
273 : : newOwnerId);
274 : : }
275 : :
4746 rhaas@postgresql.org 276 [ - + ]: 4 : InvokeObjectPostAlterHook(ForeignDataWrapperRelationId,
277 : : form->oid, 0);
5136 alvherre@alvh.no-ip. 278 : 4 : }
279 : :
280 : : /*
281 : : * Change foreign-data wrapper owner -- by name
282 : : *
283 : : * Note restrictions in the "_internal" function, above.
284 : : */
285 : : ObjectAddress
286 : 10 : AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId)
287 : : {
288 : : Oid fdwId;
289 : : HeapTuple tup;
290 : : Relation rel;
291 : : ObjectAddress address;
292 : : Form_pg_foreign_data_wrapper form;
293 : :
294 : :
2610 andres@anarazel.de 295 : 10 : rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
296 : :
5136 alvherre@alvh.no-ip. 297 : 10 : tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(name));
298 : :
299 [ - + ]: 10 : if (!HeapTupleIsValid(tup))
5136 alvherre@alvh.no-ip. 300 [ # # ]:UBC 0 : ereport(ERROR,
301 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
302 : : errmsg("foreign-data wrapper \"%s\" does not exist", name)));
303 : :
2672 andres@anarazel.de 304 :CBC 10 : form = (Form_pg_foreign_data_wrapper) GETSTRUCT(tup);
305 : 10 : fdwId = form->oid;
306 : :
5136 alvherre@alvh.no-ip. 307 : 10 : AlterForeignDataWrapperOwner_internal(rel, tup, newOwnerId);
308 : :
4030 309 : 4 : ObjectAddressSet(address, ForeignDataWrapperRelationId, fdwId);
310 : :
6295 peter_e@gmx.net 311 : 4 : heap_freetuple(tup);
312 : :
2610 andres@anarazel.de 313 : 4 : table_close(rel, RowExclusiveLock);
314 : :
4030 alvherre@alvh.no-ip. 315 : 4 : return address;
316 : : }
317 : :
318 : : /*
319 : : * Change foreign-data wrapper owner -- by OID
320 : : *
321 : : * Note restrictions in the "_internal" function, above.
322 : : */
323 : : void
5136 alvherre@alvh.no-ip. 324 :UBC 0 : AlterForeignDataWrapperOwner_oid(Oid fwdId, Oid newOwnerId)
325 : : {
326 : : HeapTuple tup;
327 : : Relation rel;
328 : :
2610 andres@anarazel.de 329 : 0 : rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
330 : :
5136 alvherre@alvh.no-ip. 331 : 0 : tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fwdId));
332 : :
6295 peter_e@gmx.net 333 [ # # ]: 0 : if (!HeapTupleIsValid(tup))
6121 bruce@momjian.us 334 [ # # ]: 0 : ereport(ERROR,
335 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
336 : : errmsg("foreign-data wrapper with OID %u does not exist", fwdId)));
337 : :
5136 alvherre@alvh.no-ip. 338 : 0 : AlterForeignDataWrapperOwner_internal(rel, tup, newOwnerId);
339 : :
340 : 0 : heap_freetuple(tup);
341 : :
2610 andres@anarazel.de 342 : 0 : table_close(rel, RowExclusiveLock);
5136 alvherre@alvh.no-ip. 343 : 0 : }
344 : :
345 : : /*
346 : : * Internal workhorse for changing a foreign server's owner
347 : : */
348 : : static void
5136 alvherre@alvh.no-ip. 349 :CBC 40 : AlterForeignServerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
350 : : {
351 : : Form_pg_foreign_server form;
352 : : Datum repl_val[Natts_pg_foreign_server];
353 : : bool repl_null[Natts_pg_foreign_server];
354 : : bool repl_repl[Natts_pg_foreign_server];
355 : : Acl *newAcl;
356 : : Datum aclDatum;
357 : : bool isNull;
358 : :
6295 peter_e@gmx.net 359 : 40 : form = (Form_pg_foreign_server) GETSTRUCT(tup);
360 : :
361 [ + + ]: 40 : if (form->srvowner != newOwnerId)
362 : : {
363 : : /* Superusers can always do it */
364 [ + + ]: 36 : if (!superuser())
365 : : {
366 : : Oid srvId;
367 : : AclResult aclresult;
368 : :
2672 andres@anarazel.de 369 : 15 : srvId = form->oid;
370 : :
371 : : /* Must be owner */
1218 peter@eisentraut.org 372 [ + + ]: 15 : if (!object_ownercheck(ForeignServerRelationId, srvId, GetUserId()))
3025 peter_e@gmx.net 373 : 6 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER,
5136 alvherre@alvh.no-ip. 374 : 6 : NameStr(form->srvname));
375 : :
376 : : /* Must be able to become new owner */
1213 rhaas@postgresql.org 377 : 9 : check_can_set_role(GetUserId(), newOwnerId);
378 : :
379 : : /* New owner must have USAGE privilege on foreign-data wrapper */
1218 peter@eisentraut.org 380 : 6 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, form->srvfdw, newOwnerId, ACL_USAGE);
6295 peter_e@gmx.net 381 [ + + ]: 6 : if (aclresult != ACLCHECK_OK)
382 : : {
383 : 3 : ForeignDataWrapper *fdw = GetForeignDataWrapper(form->srvfdw);
384 : :
3025 385 : 3 : aclcheck_error(aclresult, OBJECT_FDW, fdw->fdwname);
386 : : }
387 : : }
388 : :
4070 bruce@momjian.us 389 : 24 : memset(repl_null, false, sizeof(repl_null));
390 : 24 : memset(repl_repl, false, sizeof(repl_repl));
391 : :
392 : 24 : repl_repl[Anum_pg_foreign_server_srvowner - 1] = true;
393 : 24 : repl_val[Anum_pg_foreign_server_srvowner - 1] = ObjectIdGetDatum(newOwnerId);
394 : :
395 : 24 : aclDatum = heap_getattr(tup,
396 : : Anum_pg_foreign_server_srvacl,
397 : : RelationGetDescr(rel),
398 : : &isNull);
399 : : /* Null ACLs do not require changes */
400 [ + + ]: 24 : if (!isNull)
401 : : {
402 : 9 : newAcl = aclnewowner(DatumGetAclP(aclDatum),
403 : : form->srvowner, newOwnerId);
404 : 9 : repl_repl[Anum_pg_foreign_server_srvacl - 1] = true;
405 : 9 : repl_val[Anum_pg_foreign_server_srvacl - 1] = PointerGetDatum(newAcl);
406 : : }
407 : :
408 : 24 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), repl_val, repl_null,
409 : : repl_repl);
410 : :
3330 alvherre@alvh.no-ip. 411 : 24 : CatalogTupleUpdate(rel, &tup->t_self, tup);
412 : :
413 : : /* Update owner dependency reference */
2672 andres@anarazel.de 414 : 24 : changeDependencyOnOwner(ForeignServerRelationId, form->oid,
415 : : newOwnerId);
416 : : }
417 : :
4746 rhaas@postgresql.org 418 [ - + ]: 28 : InvokeObjectPostAlterHook(ForeignServerRelationId,
419 : : form->oid, 0);
5136 alvherre@alvh.no-ip. 420 : 28 : }
421 : :
422 : : /*
423 : : * Change foreign server owner -- by name
424 : : */
425 : : ObjectAddress
426 : 34 : AlterForeignServerOwner(const char *name, Oid newOwnerId)
427 : : {
428 : : Oid servOid;
429 : : HeapTuple tup;
430 : : Relation rel;
431 : : ObjectAddress address;
432 : : Form_pg_foreign_server form;
433 : :
2610 andres@anarazel.de 434 : 34 : rel = table_open(ForeignServerRelationId, RowExclusiveLock);
435 : :
5136 alvherre@alvh.no-ip. 436 : 34 : tup = SearchSysCacheCopy1(FOREIGNSERVERNAME, CStringGetDatum(name));
437 : :
438 [ - + ]: 34 : if (!HeapTupleIsValid(tup))
5136 alvherre@alvh.no-ip. 439 [ # # ]:UBC 0 : ereport(ERROR,
440 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
441 : : errmsg("server \"%s\" does not exist", name)));
442 : :
2672 andres@anarazel.de 443 :CBC 34 : form = (Form_pg_foreign_server) GETSTRUCT(tup);
444 : 34 : servOid = form->oid;
445 : :
5136 alvherre@alvh.no-ip. 446 : 34 : AlterForeignServerOwner_internal(rel, tup, newOwnerId);
447 : :
4030 448 : 22 : ObjectAddressSet(address, ForeignServerRelationId, servOid);
449 : :
6295 peter_e@gmx.net 450 : 22 : heap_freetuple(tup);
451 : :
2610 andres@anarazel.de 452 : 22 : table_close(rel, RowExclusiveLock);
453 : :
4030 alvherre@alvh.no-ip. 454 : 22 : return address;
455 : : }
456 : :
457 : : /*
458 : : * Change foreign server owner -- by OID
459 : : */
460 : : void
5136 461 : 6 : AlterForeignServerOwner_oid(Oid srvId, Oid newOwnerId)
462 : : {
463 : : HeapTuple tup;
464 : : Relation rel;
465 : :
2610 andres@anarazel.de 466 : 6 : rel = table_open(ForeignServerRelationId, RowExclusiveLock);
467 : :
5136 alvherre@alvh.no-ip. 468 : 6 : tup = SearchSysCacheCopy1(FOREIGNSERVEROID, ObjectIdGetDatum(srvId));
469 : :
470 [ - + ]: 6 : if (!HeapTupleIsValid(tup))
5136 alvherre@alvh.no-ip. 471 [ # # ]:UBC 0 : ereport(ERROR,
472 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
473 : : errmsg("foreign server with OID %u does not exist", srvId)));
474 : :
5136 alvherre@alvh.no-ip. 475 :CBC 6 : AlterForeignServerOwner_internal(rel, tup, newOwnerId);
476 : :
477 : 6 : heap_freetuple(tup);
478 : :
2610 andres@anarazel.de 479 : 6 : table_close(rel, RowExclusiveLock);
5136 alvherre@alvh.no-ip. 480 : 6 : }
481 : :
482 : : /*
483 : : * Convert a handler function name passed from the parser to an Oid.
484 : : */
485 : : static Oid
5503 tgl@sss.pgh.pa.us 486 : 24 : lookup_fdw_handler_func(DefElem *handler)
487 : : {
488 : : Oid handlerOid;
489 : :
490 [ + - - + ]: 24 : if (handler == NULL || handler->arg == NULL)
5503 tgl@sss.pgh.pa.us 491 :UBC 0 : return InvalidOid;
492 : :
493 : : /* handlers have no arguments */
2315 alvherre@alvh.no-ip. 494 :CBC 24 : handlerOid = LookupFuncName((List *) handler->arg, 0, NULL, false);
495 : :
496 : : /* check that handler has correct return type */
5503 tgl@sss.pgh.pa.us 497 [ + + ]: 24 : if (get_func_rettype(handlerOid) != FDW_HANDLEROID)
498 [ + - ]: 6 : ereport(ERROR,
499 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
500 : : errmsg("function %s must return type %s",
501 : : NameListToString((List *) handler->arg), "fdw_handler")));
502 : :
503 : 18 : return handlerOid;
504 : : }
505 : :
506 : : /*
507 : : * Convert a validator function name passed from the parser to an Oid.
508 : : */
509 : : static Oid
510 : 30 : lookup_fdw_validator_func(DefElem *validator)
511 : : {
512 : : Oid funcargtypes[2];
513 : :
514 [ + - + + ]: 30 : if (validator == NULL || validator->arg == NULL)
515 : 3 : return InvalidOid;
516 : :
517 : : /* validators take text[], oid */
6228 peter_e@gmx.net 518 : 27 : funcargtypes[0] = TEXTARRAYOID;
519 : 27 : funcargtypes[1] = OIDOID;
520 : :
5503 tgl@sss.pgh.pa.us 521 : 27 : return LookupFuncName((List *) validator->arg, 2, funcargtypes, false);
522 : : /* validator's return value is ignored, so we don't check the type */
523 : : }
524 : :
525 : : /*
526 : : * Convert a connection string function name passed from the parser to an Oid.
527 : : */
528 : : static Oid
9 jdavis@postgresql.or 529 :GNC 8 : lookup_fdw_connection_func(DefElem *connection)
530 : : {
531 : : Oid connectionOid;
532 : : Oid funcargtypes[3];
533 : :
534 [ + - - + ]: 8 : if (connection == NULL || connection->arg == NULL)
9 jdavis@postgresql.or 535 :UNC 0 : return InvalidOid;
536 : :
537 : : /* connection string functions take user oid, server oid */
9 jdavis@postgresql.or 538 :GNC 8 : funcargtypes[0] = OIDOID;
539 : 8 : funcargtypes[1] = OIDOID;
540 : 8 : funcargtypes[2] = INTERNALOID;
541 : :
542 : 8 : connectionOid = LookupFuncName((List *) connection->arg, 3, funcargtypes, false);
543 : :
544 : : /* check that connection string function has correct return type */
545 [ - + ]: 8 : if (get_func_rettype(connectionOid) != TEXTOID)
9 jdavis@postgresql.or 546 [ # # ]:UNC 0 : ereport(ERROR,
547 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
548 : : errmsg("function %s must return type %s",
549 : : NameListToString((List *) connection->arg), "text")));
550 : :
9 jdavis@postgresql.or 551 :GNC 8 : return connectionOid;
552 : : }
553 : :
554 : : /*
555 : : * Process function options of CREATE/ALTER FDW
556 : : */
557 : : static void
1704 dean.a.rasheed@gmail 558 :CBC 152 : parse_func_options(ParseState *pstate, List *func_options,
559 : : bool *handler_given, Oid *fdwhandler,
560 : : bool *validator_given, Oid *fdwvalidator,
561 : : bool *connection_given, Oid *fdwconnection)
562 : : {
563 : : ListCell *cell;
564 : :
5503 tgl@sss.pgh.pa.us 565 : 152 : *handler_given = false;
566 : 152 : *validator_given = false;
9 jdavis@postgresql.or 567 :GNC 152 : *connection_given = false;
568 : : /* return InvalidOid if not given */
5503 tgl@sss.pgh.pa.us 569 :CBC 152 : *fdwhandler = InvalidOid;
570 : 152 : *fdwvalidator = InvalidOid;
9 jdavis@postgresql.or 571 :GNC 152 : *fdwconnection = InvalidOid;
572 : :
5503 tgl@sss.pgh.pa.us 573 [ + + + + :CBC 202 : foreach(cell, func_options)
+ + ]
574 : : {
575 : 68 : DefElem *def = (DefElem *) lfirst(cell);
576 : :
577 [ + + ]: 68 : if (strcmp(def->defname, "handler") == 0)
578 : : {
579 [ + + ]: 30 : if (*handler_given)
1704 dean.a.rasheed@gmail 580 : 6 : errorConflictingDefElem(def, pstate);
5503 tgl@sss.pgh.pa.us 581 : 24 : *handler_given = true;
582 : 24 : *fdwhandler = lookup_fdw_handler_func(def);
583 : : }
584 [ + + ]: 38 : else if (strcmp(def->defname, "validator") == 0)
585 : : {
586 [ - + ]: 30 : if (*validator_given)
1704 dean.a.rasheed@gmail 587 :UBC 0 : errorConflictingDefElem(def, pstate);
5503 tgl@sss.pgh.pa.us 588 :CBC 30 : *validator_given = true;
589 : 30 : *fdwvalidator = lookup_fdw_validator_func(def);
590 : : }
9 jdavis@postgresql.or 591 [ + - ]:GNC 8 : else if (strcmp(def->defname, "connection") == 0)
592 : : {
593 [ - + ]: 8 : if (*connection_given)
9 jdavis@postgresql.or 594 :UNC 0 : errorConflictingDefElem(def, pstate);
9 jdavis@postgresql.or 595 :GNC 8 : *connection_given = true;
596 : 8 : *fdwconnection = lookup_fdw_connection_func(def);
597 : : }
598 : : else
5503 tgl@sss.pgh.pa.us 599 [ # # ]:UBC 0 : elog(ERROR, "option \"%s\" not recognized",
600 : : def->defname);
601 : : }
5503 tgl@sss.pgh.pa.us 602 :CBC 134 : }
603 : :
604 : : /*
605 : : * Create a foreign-data wrapper
606 : : */
607 : : ObjectAddress
1704 dean.a.rasheed@gmail 608 : 108 : CreateForeignDataWrapper(ParseState *pstate, CreateFdwStmt *stmt)
609 : : {
610 : : Relation rel;
611 : : Datum values[Natts_pg_foreign_data_wrapper];
612 : : bool nulls[Natts_pg_foreign_data_wrapper];
613 : : HeapTuple tuple;
614 : : Oid fdwId;
615 : : bool handler_given;
616 : : bool validator_given;
617 : : bool connection_given;
618 : : Oid fdwhandler;
619 : : Oid fdwvalidator;
620 : : Oid fdwconnection;
621 : : Datum fdwoptions;
622 : : Oid ownerId;
623 : : ObjectAddress myself;
624 : : ObjectAddress referenced;
625 : :
2610 andres@anarazel.de 626 : 108 : rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
627 : :
628 : : /* Must be superuser */
6295 peter_e@gmx.net 629 [ + + ]: 108 : if (!superuser())
630 [ + - ]: 10 : ereport(ERROR,
631 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
632 : : errmsg("permission denied to create foreign-data wrapper \"%s\"",
633 : : stmt->fdwname),
634 : : errhint("Must be superuser to create a foreign-data wrapper.")));
635 : :
636 : : /* For now the owner cannot be specified on create. Use effective user ID. */
637 : 98 : ownerId = GetUserId();
638 : :
639 : : /*
640 : : * Check that there is no other foreign-data wrapper by this name.
641 : : */
642 [ + + ]: 98 : if (GetForeignDataWrapperByName(stmt->fdwname, true) != NULL)
643 [ + - ]: 3 : ereport(ERROR,
644 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
645 : : errmsg("foreign-data wrapper \"%s\" already exists",
646 : : stmt->fdwname)));
647 : :
648 : : /*
649 : : * Insert tuple into pg_foreign_data_wrapper.
650 : : */
6294 tgl@sss.pgh.pa.us 651 : 95 : memset(values, 0, sizeof(values));
652 : 95 : memset(nulls, false, sizeof(nulls));
653 : :
2672 andres@anarazel.de 654 : 95 : fdwId = GetNewOidWithIndex(rel, ForeignDataWrapperOidIndexId,
655 : : Anum_pg_foreign_data_wrapper_oid);
656 : 95 : values[Anum_pg_foreign_data_wrapper_oid - 1] = ObjectIdGetDatum(fdwId);
6295 peter_e@gmx.net 657 : 95 : values[Anum_pg_foreign_data_wrapper_fdwname - 1] =
658 : 95 : DirectFunctionCall1(namein, CStringGetDatum(stmt->fdwname));
659 : 95 : values[Anum_pg_foreign_data_wrapper_fdwowner - 1] = ObjectIdGetDatum(ownerId);
660 : :
661 : : /* Lookup handler and validator functions, if given */
1704 dean.a.rasheed@gmail 662 : 95 : parse_func_options(pstate, stmt->func_options,
663 : : &handler_given, &fdwhandler,
664 : : &validator_given, &fdwvalidator,
665 : : &connection_given, &fdwconnection);
666 : :
5503 tgl@sss.pgh.pa.us 667 : 86 : values[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = ObjectIdGetDatum(fdwhandler);
668 : 86 : values[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = ObjectIdGetDatum(fdwvalidator);
9 jdavis@postgresql.or 669 :GNC 86 : values[Anum_pg_foreign_data_wrapper_fdwconnection - 1] = ObjectIdGetDatum(fdwconnection);
670 : :
6228 peter_e@gmx.net 671 :CBC 86 : nulls[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true;
672 : :
5926 heikki.linnakangas@i 673 : 86 : fdwoptions = transformGenericOptions(ForeignDataWrapperRelationId,
674 : : PointerGetDatum(NULL),
675 : : stmt->options,
676 : : fdwvalidator);
677 : :
172 peter@eisentraut.org 678 [ + + ]:GNC 83 : if (DatumGetPointer(fdwoptions) != NULL)
6295 peter_e@gmx.net 679 :CBC 9 : values[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = fdwoptions;
680 : : else
681 : 74 : nulls[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true;
682 : :
683 : 83 : tuple = heap_form_tuple(rel->rd_att, values, nulls);
684 : :
2672 andres@anarazel.de 685 : 83 : CatalogTupleInsert(rel, tuple);
686 : :
6295 peter_e@gmx.net 687 : 83 : heap_freetuple(tuple);
688 : :
689 : : /* record dependencies */
5514 tgl@sss.pgh.pa.us 690 : 83 : myself.classId = ForeignDataWrapperRelationId;
691 : 83 : myself.objectId = fdwId;
692 : 83 : myself.objectSubId = 0;
693 : :
5503 694 [ + + ]: 83 : if (OidIsValid(fdwhandler))
695 : : {
696 : 9 : referenced.classId = ProcedureRelationId;
697 : 9 : referenced.objectId = fdwhandler;
698 : 9 : referenced.objectSubId = 0;
699 : 9 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
700 : : }
701 : :
702 [ + + ]: 83 : if (OidIsValid(fdwvalidator))
703 : : {
6228 peter_e@gmx.net 704 : 18 : referenced.classId = ProcedureRelationId;
705 : 18 : referenced.objectId = fdwvalidator;
706 : 18 : referenced.objectSubId = 0;
707 : 18 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
708 : : }
709 : :
6295 710 : 83 : recordDependencyOnOwner(ForeignDataWrapperRelationId, fdwId, ownerId);
711 : :
712 : : /* dependency on extension */
5349 tgl@sss.pgh.pa.us 713 : 83 : recordDependencyOnCurrentExtension(&myself, false);
714 : :
715 : : /* Post creation hook for new foreign data wrapper */
4757 rhaas@postgresql.org 716 [ - + ]: 83 : InvokeObjectPostCreateHook(ForeignDataWrapperRelationId, fdwId, 0);
717 : :
2610 andres@anarazel.de 718 : 83 : table_close(rel, RowExclusiveLock);
719 : :
4030 alvherre@alvh.no-ip. 720 : 83 : return myself;
721 : : }
722 : :
723 : :
724 : : /*
725 : : * Alter foreign-data wrapper
726 : : */
727 : : ObjectAddress
1704 dean.a.rasheed@gmail 728 : 69 : AlterForeignDataWrapper(ParseState *pstate, AlterFdwStmt *stmt)
729 : : {
730 : : Relation rel;
731 : : HeapTuple tp;
732 : : Form_pg_foreign_data_wrapper fdwForm;
733 : : Datum repl_val[Natts_pg_foreign_data_wrapper];
734 : : bool repl_null[Natts_pg_foreign_data_wrapper];
735 : : bool repl_repl[Natts_pg_foreign_data_wrapper];
736 : : Oid fdwId;
737 : : bool isnull;
738 : : Datum datum;
739 : : bool handler_given;
740 : : bool validator_given;
741 : : bool connection_given;
742 : : Oid fdwhandler;
743 : : Oid fdwvalidator;
744 : : Oid fdwconnection;
745 : : ObjectAddress myself;
746 : :
2610 andres@anarazel.de 747 : 69 : rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
748 : :
749 : : /* Must be superuser */
6295 peter_e@gmx.net 750 [ + + ]: 69 : if (!superuser())
751 [ + - ]: 12 : ereport(ERROR,
752 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
753 : : errmsg("permission denied to alter foreign-data wrapper \"%s\"",
754 : : stmt->fdwname),
755 : : errhint("Must be superuser to alter a foreign-data wrapper.")));
756 : :
5873 rhaas@postgresql.org 757 : 57 : tp = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME,
758 : : CStringGetDatum(stmt->fdwname));
759 : :
6295 peter_e@gmx.net 760 [ - + ]: 57 : if (!HeapTupleIsValid(tp))
6295 peter_e@gmx.net 761 [ # # ]:UBC 0 : ereport(ERROR,
762 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
763 : : errmsg("foreign-data wrapper \"%s\" does not exist", stmt->fdwname)));
764 : :
5503 tgl@sss.pgh.pa.us 765 :CBC 57 : fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp);
2672 andres@anarazel.de 766 : 57 : fdwId = fdwForm->oid;
767 : :
6295 peter_e@gmx.net 768 : 57 : memset(repl_val, 0, sizeof(repl_val));
769 : 57 : memset(repl_null, false, sizeof(repl_null));
770 : 57 : memset(repl_repl, false, sizeof(repl_repl));
771 : :
1704 dean.a.rasheed@gmail 772 : 57 : parse_func_options(pstate, stmt->func_options,
773 : : &handler_given, &fdwhandler,
774 : : &validator_given, &fdwvalidator,
775 : : &connection_given, &fdwconnection);
776 : :
5503 tgl@sss.pgh.pa.us 777 [ + + ]: 48 : if (handler_given)
778 : : {
779 : 3 : repl_val[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = ObjectIdGetDatum(fdwhandler);
780 : 3 : repl_repl[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = true;
781 : :
782 : : /*
783 : : * It could be that the behavior of accessing foreign table changes
784 : : * with the new handler. Warn about this.
785 : : */
786 [ + - ]: 3 : ereport(WARNING,
787 : : (errmsg("changing the foreign-data wrapper handler can change behavior of existing foreign tables")));
788 : : }
789 : :
790 [ + + ]: 48 : if (validator_given)
791 : : {
6228 peter_e@gmx.net 792 : 6 : repl_val[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = ObjectIdGetDatum(fdwvalidator);
793 : 6 : repl_repl[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = true;
794 : :
795 : : /*
796 : : * It could be that existing options for the FDW or dependent SERVER,
797 : : * USER MAPPING or FOREIGN TABLE objects are no longer valid according
798 : : * to the new validator. Warn about this.
799 : : */
5503 tgl@sss.pgh.pa.us 800 [ + + ]: 6 : if (OidIsValid(fdwvalidator))
6228 peter_e@gmx.net 801 [ + - ]: 3 : ereport(WARNING,
802 : : (errmsg("changing the foreign-data wrapper validator can cause "
803 : : "the options for dependent objects to become invalid")));
804 : : }
805 : : else
806 : : {
807 : : /*
808 : : * Validator is not changed, but we need it for validating options.
809 : : */
5503 tgl@sss.pgh.pa.us 810 : 42 : fdwvalidator = fdwForm->fdwvalidator;
811 : : }
812 : :
9 jdavis@postgresql.or 813 [ + + ]:GNC 48 : if (connection_given)
814 : : {
815 : 8 : repl_val[Anum_pg_foreign_data_wrapper_fdwconnection - 1] = ObjectIdGetDatum(fdwconnection);
816 : 8 : repl_repl[Anum_pg_foreign_data_wrapper_fdwconnection - 1] = true;
817 : : }
818 : :
819 : : /*
820 : : * If options specified, validate and update.
821 : : */
6295 peter_e@gmx.net 822 [ + + ]:CBC 48 : if (stmt->options)
823 : : {
824 : : /* Extract the current options */
825 : 31 : datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID,
826 : : tp,
827 : : Anum_pg_foreign_data_wrapper_fdwoptions,
828 : : &isnull);
6294 tgl@sss.pgh.pa.us 829 [ + + ]: 31 : if (isnull)
830 : 10 : datum = PointerGetDatum(NULL);
831 : :
832 : : /* Transform the options */
5926 heikki.linnakangas@i 833 : 31 : datum = transformGenericOptions(ForeignDataWrapperRelationId,
834 : : datum,
835 : : stmt->options,
836 : : fdwvalidator);
837 : :
172 peter@eisentraut.org 838 [ + - ]:GNC 15 : if (DatumGetPointer(datum) != NULL)
6294 tgl@sss.pgh.pa.us 839 :CBC 15 : repl_val[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = datum;
840 : : else
6295 peter_e@gmx.net 841 :UBC 0 : repl_null[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true;
842 : :
6295 peter_e@gmx.net 843 :CBC 15 : repl_repl[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true;
844 : : }
845 : :
846 : : /* Everything looks good - update the tuple */
847 : 32 : tp = heap_modify_tuple(tp, RelationGetDescr(rel),
848 : : repl_val, repl_null, repl_repl);
849 : :
3330 alvherre@alvh.no-ip. 850 : 32 : CatalogTupleUpdate(rel, &tp->t_self, tp);
851 : :
6295 peter_e@gmx.net 852 : 32 : heap_freetuple(tp);
853 : :
4030 alvherre@alvh.no-ip. 854 : 32 : ObjectAddressSet(myself, ForeignDataWrapperRelationId, fdwId);
855 : :
856 : : /* Update function dependencies if we changed them */
5503 tgl@sss.pgh.pa.us 857 [ + + + + ]: 32 : if (handler_given || validator_given)
858 : : {
859 : : ObjectAddress referenced;
860 : :
861 : : /*
862 : : * Flush all existing dependency records of this FDW on functions; we
863 : : * assume there can be none other than the ones we are fixing.
864 : : */
865 : 9 : deleteDependencyRecordsForClass(ForeignDataWrapperRelationId,
866 : : fdwId,
867 : : ProcedureRelationId,
868 : : DEPENDENCY_NORMAL);
869 : :
870 : : /* And build new ones. */
871 : :
872 [ + + ]: 9 : if (OidIsValid(fdwhandler))
873 : : {
874 : 3 : referenced.classId = ProcedureRelationId;
875 : 3 : referenced.objectId = fdwhandler;
876 : 3 : referenced.objectSubId = 0;
877 : 3 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
878 : : }
879 : :
880 [ + + ]: 9 : if (OidIsValid(fdwvalidator))
881 : : {
882 : 3 : referenced.classId = ProcedureRelationId;
883 : 3 : referenced.objectId = fdwvalidator;
884 : 3 : referenced.objectSubId = 0;
885 : 3 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
886 : : }
887 : : }
888 : :
4746 rhaas@postgresql.org 889 [ - + ]: 32 : InvokeObjectPostAlterHook(ForeignDataWrapperRelationId, fdwId, 0);
890 : :
2610 andres@anarazel.de 891 : 32 : table_close(rel, RowExclusiveLock);
892 : :
4030 alvherre@alvh.no-ip. 893 : 32 : return myself;
894 : : }
895 : :
896 : :
897 : : /*
898 : : * Create a foreign server
899 : : */
900 : : ObjectAddress
6295 peter_e@gmx.net 901 : 156 : CreateForeignServer(CreateForeignServerStmt *stmt)
902 : : {
903 : : Relation rel;
904 : : Datum srvoptions;
905 : : Datum values[Natts_pg_foreign_server];
906 : : bool nulls[Natts_pg_foreign_server];
907 : : HeapTuple tuple;
908 : : Oid srvId;
909 : : Oid ownerId;
910 : : AclResult aclresult;
911 : : ObjectAddress myself;
912 : : ObjectAddress referenced;
913 : : ForeignDataWrapper *fdw;
914 : :
2610 andres@anarazel.de 915 : 156 : rel = table_open(ForeignServerRelationId, RowExclusiveLock);
916 : :
917 : : /* For now the owner cannot be specified on create. Use effective user ID. */
6295 peter_e@gmx.net 918 : 156 : ownerId = GetUserId();
919 : :
920 : : /*
921 : : * Check that there is no other foreign server by this name. If there is
922 : : * one, do nothing if IF NOT EXISTS was specified.
923 : : */
1315 tgl@sss.pgh.pa.us 924 : 156 : srvId = get_foreign_server_oid(stmt->servername, true);
925 [ + + ]: 156 : if (OidIsValid(srvId))
926 : : {
3282 andrew@dunslane.net 927 [ + + ]: 8 : if (stmt->if_not_exists)
928 : : {
929 : : /*
930 : : * If we are in an extension script, insist that the pre-existing
931 : : * object be a member of the extension, to avoid security risks.
932 : : */
1315 tgl@sss.pgh.pa.us 933 : 5 : ObjectAddressSet(myself, ForeignServerRelationId, srvId);
934 : 5 : checkMembershipInCurrentExtension(&myself);
935 : :
936 : : /* OK to skip */
3282 andrew@dunslane.net 937 [ + + ]: 4 : ereport(NOTICE,
938 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
939 : : errmsg("server \"%s\" already exists, skipping",
940 : : stmt->servername)));
2610 andres@anarazel.de 941 : 4 : table_close(rel, RowExclusiveLock);
3282 andrew@dunslane.net 942 : 4 : return InvalidObjectAddress;
943 : : }
944 : : else
945 [ + - ]: 3 : ereport(ERROR,
946 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
947 : : errmsg("server \"%s\" already exists",
948 : : stmt->servername)));
949 : : }
950 : :
951 : : /*
952 : : * Check that the FDW exists and that we have USAGE on it. Also get the
953 : : * actual FDW for option validation etc.
954 : : */
6295 peter_e@gmx.net 955 : 148 : fdw = GetForeignDataWrapperByName(stmt->fdwname, false);
956 : :
1218 peter@eisentraut.org 957 : 145 : aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdw->fdwid, ownerId, ACL_USAGE);
6295 peter_e@gmx.net 958 [ + + ]: 145 : if (aclresult != ACLCHECK_OK)
3025 959 : 13 : aclcheck_error(aclresult, OBJECT_FDW, fdw->fdwname);
960 : :
961 : : /*
962 : : * Insert tuple into pg_foreign_server.
963 : : */
6294 tgl@sss.pgh.pa.us 964 : 132 : memset(values, 0, sizeof(values));
965 : 132 : memset(nulls, false, sizeof(nulls));
966 : :
2672 andres@anarazel.de 967 : 132 : srvId = GetNewOidWithIndex(rel, ForeignServerOidIndexId,
968 : : Anum_pg_foreign_server_oid);
969 : 132 : values[Anum_pg_foreign_server_oid - 1] = ObjectIdGetDatum(srvId);
6295 peter_e@gmx.net 970 : 132 : values[Anum_pg_foreign_server_srvname - 1] =
971 : 132 : DirectFunctionCall1(namein, CStringGetDatum(stmt->servername));
972 : 132 : values[Anum_pg_foreign_server_srvowner - 1] = ObjectIdGetDatum(ownerId);
973 : 132 : values[Anum_pg_foreign_server_srvfdw - 1] = ObjectIdGetDatum(fdw->fdwid);
974 : :
975 : : /* Add server type if supplied */
976 [ + + ]: 132 : if (stmt->servertype)
977 : 9 : values[Anum_pg_foreign_server_srvtype - 1] =
978 : 9 : CStringGetTextDatum(stmt->servertype);
979 : : else
980 : 123 : nulls[Anum_pg_foreign_server_srvtype - 1] = true;
981 : :
982 : : /* Add server version if supplied */
983 [ + + ]: 132 : if (stmt->version)
984 : 9 : values[Anum_pg_foreign_server_srvversion - 1] =
985 : 9 : CStringGetTextDatum(stmt->version);
986 : : else
987 : 123 : nulls[Anum_pg_foreign_server_srvversion - 1] = true;
988 : :
989 : : /* Start with a blank acl */
990 : 132 : nulls[Anum_pg_foreign_server_srvacl - 1] = true;
991 : :
992 : : /* Add server options */
5926 heikki.linnakangas@i 993 : 132 : srvoptions = transformGenericOptions(ForeignServerRelationId,
994 : : PointerGetDatum(NULL),
995 : : stmt->options,
996 : : fdw->fdwvalidator);
997 : :
172 peter@eisentraut.org 998 [ + + ]:GNC 127 : if (DatumGetPointer(srvoptions) != NULL)
6295 peter_e@gmx.net 999 :CBC 35 : values[Anum_pg_foreign_server_srvoptions - 1] = srvoptions;
1000 : : else
1001 : 92 : nulls[Anum_pg_foreign_server_srvoptions - 1] = true;
1002 : :
1003 : 127 : tuple = heap_form_tuple(rel->rd_att, values, nulls);
1004 : :
2672 andres@anarazel.de 1005 : 127 : CatalogTupleInsert(rel, tuple);
1006 : :
6295 peter_e@gmx.net 1007 : 127 : heap_freetuple(tuple);
1008 : :
1009 : : /* record dependencies */
1010 : 127 : myself.classId = ForeignServerRelationId;
1011 : 127 : myself.objectId = srvId;
1012 : 127 : myself.objectSubId = 0;
1013 : :
1014 : 127 : referenced.classId = ForeignDataWrapperRelationId;
1015 : 127 : referenced.objectId = fdw->fdwid;
1016 : 127 : referenced.objectSubId = 0;
1017 : 127 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1018 : :
1019 : 127 : recordDependencyOnOwner(ForeignServerRelationId, srvId, ownerId);
1020 : :
1021 : : /* dependency on extension */
5349 tgl@sss.pgh.pa.us 1022 : 127 : recordDependencyOnCurrentExtension(&myself, false);
1023 : :
1024 : : /* Post creation hook for new foreign server */
4757 rhaas@postgresql.org 1025 [ - + ]: 127 : InvokeObjectPostCreateHook(ForeignServerRelationId, srvId, 0);
1026 : :
2610 andres@anarazel.de 1027 : 127 : table_close(rel, RowExclusiveLock);
1028 : :
4030 alvherre@alvh.no-ip. 1029 : 127 : return myself;
1030 : : }
1031 : :
1032 : :
1033 : : /*
1034 : : * Alter foreign server
1035 : : */
1036 : : ObjectAddress
6295 peter_e@gmx.net 1037 : 116 : AlterForeignServer(AlterForeignServerStmt *stmt)
1038 : : {
1039 : : Relation rel;
1040 : : HeapTuple tp;
1041 : : Datum repl_val[Natts_pg_foreign_server];
1042 : : bool repl_null[Natts_pg_foreign_server];
1043 : : bool repl_repl[Natts_pg_foreign_server];
1044 : : Oid srvId;
1045 : : Form_pg_foreign_server srvForm;
1046 : : ObjectAddress address;
1047 : :
2610 andres@anarazel.de 1048 : 116 : rel = table_open(ForeignServerRelationId, RowExclusiveLock);
1049 : :
5873 rhaas@postgresql.org 1050 : 116 : tp = SearchSysCacheCopy1(FOREIGNSERVERNAME,
1051 : : CStringGetDatum(stmt->servername));
1052 : :
6295 peter_e@gmx.net 1053 [ + + ]: 116 : if (!HeapTupleIsValid(tp))
1054 [ + - ]: 3 : ereport(ERROR,
1055 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1056 : : errmsg("server \"%s\" does not exist", stmt->servername)));
1057 : :
1058 : 113 : srvForm = (Form_pg_foreign_server) GETSTRUCT(tp);
2672 andres@anarazel.de 1059 : 113 : srvId = srvForm->oid;
1060 : :
1061 : : /*
1062 : : * Only owner or a superuser can ALTER a SERVER.
1063 : : */
1218 peter@eisentraut.org 1064 [ + + ]: 113 : if (!object_ownercheck(ForeignServerRelationId, srvId, GetUserId()))
3025 peter_e@gmx.net 1065 : 12 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER,
6295 1066 : 12 : stmt->servername);
1067 : :
1068 : 101 : memset(repl_val, 0, sizeof(repl_val));
1069 : 101 : memset(repl_null, false, sizeof(repl_null));
1070 : 101 : memset(repl_repl, false, sizeof(repl_repl));
1071 : :
1072 [ + + ]: 101 : if (stmt->has_version)
1073 : : {
1074 : : /*
1075 : : * Change the server VERSION string.
1076 : : */
1077 [ + - ]: 12 : if (stmt->version)
1078 : 12 : repl_val[Anum_pg_foreign_server_srvversion - 1] =
1079 : 12 : CStringGetTextDatum(stmt->version);
1080 : : else
6295 peter_e@gmx.net 1081 :UBC 0 : repl_null[Anum_pg_foreign_server_srvversion - 1] = true;
1082 : :
6295 peter_e@gmx.net 1083 :CBC 12 : repl_repl[Anum_pg_foreign_server_srvversion - 1] = true;
1084 : : }
1085 : :
1086 [ + + ]: 101 : if (stmt->options)
1087 : : {
1088 : 92 : ForeignDataWrapper *fdw = GetForeignDataWrapper(srvForm->srvfdw);
1089 : : Datum datum;
1090 : : bool isnull;
1091 : :
1092 : : /* Extract the current srvoptions */
1093 : 92 : datum = SysCacheGetAttr(FOREIGNSERVEROID,
1094 : : tp,
1095 : : Anum_pg_foreign_server_srvoptions,
1096 : : &isnull);
6294 tgl@sss.pgh.pa.us 1097 [ + + ]: 92 : if (isnull)
1098 : 9 : datum = PointerGetDatum(NULL);
1099 : :
1100 : : /* Prepare the options array */
5926 heikki.linnakangas@i 1101 : 92 : datum = transformGenericOptions(ForeignServerRelationId,
1102 : : datum,
1103 : : stmt->options,
1104 : : fdw->fdwvalidator);
1105 : :
172 peter@eisentraut.org 1106 [ + + ]:GNC 80 : if (DatumGetPointer(datum) != NULL)
6295 peter_e@gmx.net 1107 :CBC 77 : repl_val[Anum_pg_foreign_server_srvoptions - 1] = datum;
1108 : : else
1109 : 3 : repl_null[Anum_pg_foreign_server_srvoptions - 1] = true;
1110 : :
1111 : 80 : repl_repl[Anum_pg_foreign_server_srvoptions - 1] = true;
1112 : : }
1113 : :
1114 : : /* Everything looks good - update the tuple */
1115 : 89 : tp = heap_modify_tuple(tp, RelationGetDescr(rel),
1116 : : repl_val, repl_null, repl_repl);
1117 : :
3330 alvherre@alvh.no-ip. 1118 : 89 : CatalogTupleUpdate(rel, &tp->t_self, tp);
1119 : :
4746 rhaas@postgresql.org 1120 [ - + ]: 89 : InvokeObjectPostAlterHook(ForeignServerRelationId, srvId, 0);
1121 : :
4030 alvherre@alvh.no-ip. 1122 : 89 : ObjectAddressSet(address, ForeignServerRelationId, srvId);
1123 : :
6295 peter_e@gmx.net 1124 : 89 : heap_freetuple(tp);
1125 : :
2610 andres@anarazel.de 1126 : 89 : table_close(rel, RowExclusiveLock);
1127 : :
4030 alvherre@alvh.no-ip. 1128 : 89 : return address;
1129 : : }
1130 : :
1131 : :
1132 : : /*
1133 : : * Common routine to check permission for user-mapping-related DDL
1134 : : * commands. We allow server owners to operate on any mapping, and
1135 : : * users to operate on their own mapping.
1136 : : */
1137 : : static void
6263 peter_e@gmx.net 1138 : 224 : user_mapping_ddl_aclcheck(Oid umuserid, Oid serverid, const char *servername)
1139 : : {
1140 : 224 : Oid curuserid = GetUserId();
1141 : :
1218 peter@eisentraut.org 1142 [ + + ]: 224 : if (!object_ownercheck(ForeignServerRelationId, serverid, curuserid))
1143 : : {
6263 peter_e@gmx.net 1144 [ + + ]: 44 : if (umuserid == curuserid)
1145 : : {
1146 : : AclResult aclresult;
1147 : :
1218 peter@eisentraut.org 1148 : 17 : aclresult = object_aclcheck(ForeignServerRelationId, serverid, curuserid, ACL_USAGE);
6263 peter_e@gmx.net 1149 [ + + ]: 17 : if (aclresult != ACLCHECK_OK)
3025 1150 : 4 : aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, servername);
1151 : : }
1152 : : else
1153 : 27 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER,
1154 : : servername);
1155 : : }
6263 1156 : 193 : }
1157 : :
1158 : :
1159 : : /*
1160 : : * Create user mapping
1161 : : */
1162 : : ObjectAddress
6295 1163 : 136 : CreateUserMapping(CreateUserMappingStmt *stmt)
1164 : : {
1165 : : Relation rel;
1166 : : Datum useoptions;
1167 : : Datum values[Natts_pg_user_mapping];
1168 : : bool nulls[Natts_pg_user_mapping];
1169 : : HeapTuple tuple;
1170 : : Oid useId;
1171 : : Oid umId;
1172 : : ObjectAddress myself;
1173 : : ObjectAddress referenced;
1174 : : ForeignServer *srv;
1175 : : ForeignDataWrapper *fdw;
4024 alvherre@alvh.no-ip. 1176 : 136 : RoleSpec *role = (RoleSpec *) stmt->user;
1177 : :
2610 andres@anarazel.de 1178 : 136 : rel = table_open(UserMappingRelationId, RowExclusiveLock);
1179 : :
4024 alvherre@alvh.no-ip. 1180 [ + + ]: 136 : if (role->roletype == ROLESPEC_PUBLIC)
1181 : 38 : useId = ACL_ID_PUBLIC;
1182 : : else
1183 : 98 : useId = get_rolespec_oid(stmt->user, false);
1184 : :
1185 : : /* Check that the server exists. */
6295 peter_e@gmx.net 1186 : 132 : srv = GetForeignServerByName(stmt->servername, false);
1187 : :
6263 1188 : 129 : user_mapping_ddl_aclcheck(useId, srv->serverid, stmt->servername);
1189 : :
1190 : : /*
1191 : : * Check that the user mapping is unique within server.
1192 : : */
2672 andres@anarazel.de 1193 : 116 : umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid,
1194 : : ObjectIdGetDatum(useId),
1195 : : ObjectIdGetDatum(srv->serverid));
1196 : :
6295 peter_e@gmx.net 1197 [ + + ]: 116 : if (OidIsValid(umId))
1198 : : {
3282 andrew@dunslane.net 1199 [ + + ]: 9 : if (stmt->if_not_exists)
1200 : : {
1201 : : /*
1202 : : * Since user mappings aren't members of extensions (see comments
1203 : : * below), no need for checkMembershipInCurrentExtension here.
1204 : : */
1205 [ + - + - ]: 3 : ereport(NOTICE,
1206 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
1207 : : errmsg("user mapping for \"%s\" already exists for server \"%s\", skipping",
1208 : : MappingUserName(useId),
1209 : : stmt->servername)));
1210 : :
2610 andres@anarazel.de 1211 : 3 : table_close(rel, RowExclusiveLock);
3282 andrew@dunslane.net 1212 : 3 : return InvalidObjectAddress;
1213 : : }
1214 : : else
1215 [ + - + - ]: 6 : ereport(ERROR,
1216 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
1217 : : errmsg("user mapping for \"%s\" already exists for server \"%s\"",
1218 : : MappingUserName(useId),
1219 : : stmt->servername)));
1220 : : }
1221 : :
6295 peter_e@gmx.net 1222 : 107 : fdw = GetForeignDataWrapper(srv->fdwid);
1223 : :
1224 : : /*
1225 : : * Insert tuple into pg_user_mapping.
1226 : : */
6294 tgl@sss.pgh.pa.us 1227 : 107 : memset(values, 0, sizeof(values));
1228 : 107 : memset(nulls, false, sizeof(nulls));
1229 : :
2672 andres@anarazel.de 1230 : 107 : umId = GetNewOidWithIndex(rel, UserMappingOidIndexId,
1231 : : Anum_pg_user_mapping_oid);
1232 : 107 : values[Anum_pg_user_mapping_oid - 1] = ObjectIdGetDatum(umId);
6295 peter_e@gmx.net 1233 : 107 : values[Anum_pg_user_mapping_umuser - 1] = ObjectIdGetDatum(useId);
1234 : 107 : values[Anum_pg_user_mapping_umserver - 1] = ObjectIdGetDatum(srv->serverid);
1235 : :
1236 : : /* Add user options */
5926 heikki.linnakangas@i 1237 : 107 : useoptions = transformGenericOptions(UserMappingRelationId,
1238 : : PointerGetDatum(NULL),
1239 : : stmt->options,
1240 : : fdw->fdwvalidator);
1241 : :
172 peter@eisentraut.org 1242 [ + + ]:GNC 99 : if (DatumGetPointer(useoptions) != NULL)
6295 peter_e@gmx.net 1243 :CBC 49 : values[Anum_pg_user_mapping_umoptions - 1] = useoptions;
1244 : : else
1245 : 50 : nulls[Anum_pg_user_mapping_umoptions - 1] = true;
1246 : :
1247 : 99 : tuple = heap_form_tuple(rel->rd_att, values, nulls);
1248 : :
2672 andres@anarazel.de 1249 : 99 : CatalogTupleInsert(rel, tuple);
1250 : :
6295 peter_e@gmx.net 1251 : 99 : heap_freetuple(tuple);
1252 : :
1253 : : /* Add dependency on the server */
1254 : 99 : myself.classId = UserMappingRelationId;
1255 : 99 : myself.objectId = umId;
1256 : 99 : myself.objectSubId = 0;
1257 : :
1258 : 99 : referenced.classId = ForeignServerRelationId;
1259 : 99 : referenced.objectId = srv->serverid;
1260 : 99 : referenced.objectSubId = 0;
1261 : 99 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1262 : :
1263 [ + + ]: 99 : if (OidIsValid(useId))
1264 : : {
1265 : : /* Record the mapped user dependency */
1266 : 73 : recordDependencyOnOwner(UserMappingRelationId, umId, useId);
1267 : : }
1268 : :
1269 : : /*
1270 : : * Perhaps someday there should be a recordDependencyOnCurrentExtension
1271 : : * call here; but since roles aren't members of extensions, it seems like
1272 : : * user mappings shouldn't be either. Note that the grammar and pg_dump
1273 : : * would need to be extended too if we change this.
1274 : : */
1275 : :
1276 : : /* Post creation hook for new user mapping */
4757 rhaas@postgresql.org 1277 [ - + ]: 99 : InvokeObjectPostCreateHook(UserMappingRelationId, umId, 0);
1278 : :
2610 andres@anarazel.de 1279 : 99 : table_close(rel, RowExclusiveLock);
1280 : :
4030 alvherre@alvh.no-ip. 1281 : 99 : return myself;
1282 : : }
1283 : :
1284 : :
1285 : : /*
1286 : : * Alter user mapping
1287 : : */
1288 : : ObjectAddress
6295 peter_e@gmx.net 1289 : 59 : AlterUserMapping(AlterUserMappingStmt *stmt)
1290 : : {
1291 : : Relation rel;
1292 : : HeapTuple tp;
1293 : : Datum repl_val[Natts_pg_user_mapping];
1294 : : bool repl_null[Natts_pg_user_mapping];
1295 : : bool repl_repl[Natts_pg_user_mapping];
1296 : : Oid useId;
1297 : : Oid umId;
1298 : : ForeignServer *srv;
1299 : : ObjectAddress address;
4024 alvherre@alvh.no-ip. 1300 : 59 : RoleSpec *role = (RoleSpec *) stmt->user;
1301 : :
2610 andres@anarazel.de 1302 : 59 : rel = table_open(UserMappingRelationId, RowExclusiveLock);
1303 : :
4024 alvherre@alvh.no-ip. 1304 [ + + ]: 59 : if (role->roletype == ROLESPEC_PUBLIC)
1305 : 18 : useId = ACL_ID_PUBLIC;
1306 : : else
1307 : 41 : useId = get_rolespec_oid(stmt->user, false);
1308 : :
6295 peter_e@gmx.net 1309 : 55 : srv = GetForeignServerByName(stmt->servername, false);
1310 : :
2672 andres@anarazel.de 1311 : 52 : umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid,
1312 : : ObjectIdGetDatum(useId),
1313 : : ObjectIdGetDatum(srv->serverid));
6295 peter_e@gmx.net 1314 [ + + ]: 52 : if (!OidIsValid(umId))
1315 [ + - - + ]: 3 : ereport(ERROR,
1316 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1317 : : errmsg("user mapping for \"%s\" does not exist for server \"%s\"",
1318 : : MappingUserName(useId), stmt->servername)));
1319 : :
6263 1320 : 49 : user_mapping_ddl_aclcheck(useId, srv->serverid, stmt->servername);
1321 : :
5873 rhaas@postgresql.org 1322 : 40 : tp = SearchSysCacheCopy1(USERMAPPINGOID, ObjectIdGetDatum(umId));
1323 : :
6295 peter_e@gmx.net 1324 [ - + ]: 40 : if (!HeapTupleIsValid(tp))
6295 peter_e@gmx.net 1325 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for user mapping %u", umId);
1326 : :
6295 peter_e@gmx.net 1327 :CBC 40 : memset(repl_val, 0, sizeof(repl_val));
1328 : 40 : memset(repl_null, false, sizeof(repl_null));
1329 : 40 : memset(repl_repl, false, sizeof(repl_repl));
1330 : :
1331 [ + - ]: 40 : if (stmt->options)
1332 : : {
1333 : : ForeignDataWrapper *fdw;
1334 : : Datum datum;
1335 : : bool isnull;
1336 : :
1337 : : /*
1338 : : * Process the options.
1339 : : */
1340 : :
1341 : 40 : fdw = GetForeignDataWrapper(srv->fdwid);
1342 : :
1343 : 40 : datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
1344 : : tp,
1345 : : Anum_pg_user_mapping_umoptions,
1346 : : &isnull);
6294 tgl@sss.pgh.pa.us 1347 [ + + ]: 40 : if (isnull)
1348 : 10 : datum = PointerGetDatum(NULL);
1349 : :
1350 : : /* Prepare the options array */
5926 heikki.linnakangas@i 1351 : 40 : datum = transformGenericOptions(UserMappingRelationId,
1352 : : datum,
1353 : : stmt->options,
1354 : : fdw->fdwvalidator);
1355 : :
172 peter@eisentraut.org 1356 [ + + ]:GNC 29 : if (DatumGetPointer(datum) != NULL)
6295 peter_e@gmx.net 1357 :CBC 24 : repl_val[Anum_pg_user_mapping_umoptions - 1] = datum;
1358 : : else
1359 : 5 : repl_null[Anum_pg_user_mapping_umoptions - 1] = true;
1360 : :
1361 : 29 : repl_repl[Anum_pg_user_mapping_umoptions - 1] = true;
1362 : : }
1363 : :
1364 : : /* Everything looks good - update the tuple */
1365 : 29 : tp = heap_modify_tuple(tp, RelationGetDescr(rel),
1366 : : repl_val, repl_null, repl_repl);
1367 : :
3330 alvherre@alvh.no-ip. 1368 : 29 : CatalogTupleUpdate(rel, &tp->t_self, tp);
1369 : :
2122 michael@paquier.xyz 1370 [ - + ]: 29 : InvokeObjectPostAlterHook(UserMappingRelationId,
1371 : : umId, 0);
1372 : :
4030 alvherre@alvh.no-ip. 1373 : 29 : ObjectAddressSet(address, UserMappingRelationId, umId);
1374 : :
6295 peter_e@gmx.net 1375 : 29 : heap_freetuple(tp);
1376 : :
2610 andres@anarazel.de 1377 : 29 : table_close(rel, RowExclusiveLock);
1378 : :
4030 alvherre@alvh.no-ip. 1379 : 29 : return address;
1380 : : }
1381 : :
1382 : :
1383 : : /*
1384 : : * Drop user mapping
1385 : : */
1386 : : Oid
6295 peter_e@gmx.net 1387 : 66 : RemoveUserMapping(DropUserMappingStmt *stmt)
1388 : : {
1389 : : ObjectAddress object;
1390 : : Oid useId;
1391 : : Oid umId;
1392 : : ForeignServer *srv;
4024 alvherre@alvh.no-ip. 1393 : 66 : RoleSpec *role = (RoleSpec *) stmt->user;
1394 : :
1395 [ + + ]: 66 : if (role->roletype == ROLESPEC_PUBLIC)
1396 : 16 : useId = ACL_ID_PUBLIC;
1397 : : else
1398 : : {
1399 : 50 : useId = get_rolespec_oid(stmt->user, stmt->missing_ok);
1400 [ + + ]: 46 : if (!OidIsValid(useId))
1401 : : {
1402 : : /*
1403 : : * IF EXISTS specified, role not found and not public. Notice this
1404 : : * and leave.
1405 : : */
1406 [ + - ]: 4 : elog(NOTICE, "role \"%s\" does not exist, skipping",
1407 : : role->rolename);
1408 : 4 : return InvalidOid;
1409 : : }
1410 : : }
1411 : :
1412 : 58 : srv = GetForeignServerByName(stmt->servername, true);
1413 : :
6295 peter_e@gmx.net 1414 [ + + ]: 58 : if (!srv)
1415 : : {
1416 [ + + ]: 6 : if (!stmt->missing_ok)
1417 [ + - ]: 3 : ereport(ERROR,
1418 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1419 : : errmsg("server \"%s\" does not exist",
1420 : : stmt->servername)));
1421 : : /* IF EXISTS, just note it */
2503 alvherre@alvh.no-ip. 1422 [ + - ]: 3 : ereport(NOTICE,
1423 : : (errmsg("server \"%s\" does not exist, skipping",
1424 : : stmt->servername)));
4824 rhaas@postgresql.org 1425 : 3 : return InvalidOid;
1426 : : }
1427 : :
2672 andres@anarazel.de 1428 : 52 : umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid,
1429 : : ObjectIdGetDatum(useId),
1430 : : ObjectIdGetDatum(srv->serverid));
1431 : :
6295 peter_e@gmx.net 1432 [ + + ]: 52 : if (!OidIsValid(umId))
1433 : : {
1434 [ + + ]: 6 : if (!stmt->missing_ok)
1435 [ + - - + ]: 3 : ereport(ERROR,
1436 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1437 : : errmsg("user mapping for \"%s\" does not exist for server \"%s\"",
1438 : : MappingUserName(useId), stmt->servername)));
1439 : :
1440 : : /* IF EXISTS specified, just note it */
1441 [ + - - + ]: 3 : ereport(NOTICE,
1442 : : (errmsg("user mapping for \"%s\" does not exist for server \"%s\", skipping",
1443 : : MappingUserName(useId), stmt->servername)));
4824 rhaas@postgresql.org 1444 : 3 : return InvalidOid;
1445 : : }
1446 : :
6263 peter_e@gmx.net 1447 : 46 : user_mapping_ddl_aclcheck(useId, srv->serverid, srv->servername);
1448 : :
1449 : : /*
1450 : : * Do the deletion
1451 : : */
6295 1452 : 37 : object.classId = UserMappingRelationId;
1453 : 37 : object.objectId = umId;
1454 : 37 : object.objectSubId = 0;
1455 : :
5162 rhaas@postgresql.org 1456 : 37 : performDeletion(&object, DROP_CASCADE, 0);
1457 : :
4824 1458 : 37 : return umId;
1459 : : }
1460 : :
1461 : :
1462 : : /*
1463 : : * Create a foreign table
1464 : : * call after DefineRelation().
1465 : : */
1466 : : void
5552 1467 : 240 : CreateForeignTable(CreateForeignTableStmt *stmt, Oid relid)
1468 : : {
1469 : : Relation ftrel;
1470 : : Datum ftoptions;
1471 : : Datum values[Natts_pg_foreign_table];
1472 : : bool nulls[Natts_pg_foreign_table];
1473 : : HeapTuple tuple;
1474 : : AclResult aclresult;
1475 : : ObjectAddress myself;
1476 : : ObjectAddress referenced;
1477 : : Oid ownerId;
1478 : : ForeignDataWrapper *fdw;
1479 : : ForeignServer *server;
1480 : :
1481 : : /*
1482 : : * Advance command counter to ensure the pg_attribute tuple is visible;
1483 : : * the tuple might be updated to add constraints in previous step.
1484 : : */
1485 : 240 : CommandCounterIncrement();
1486 : :
2610 andres@anarazel.de 1487 : 240 : ftrel = table_open(ForeignTableRelationId, RowExclusiveLock);
1488 : :
1489 : : /*
1490 : : * For now the owner cannot be specified on create. Use effective user ID.
1491 : : */
5552 rhaas@postgresql.org 1492 : 240 : ownerId = GetUserId();
1493 : :
1494 : : /*
1495 : : * Check that the foreign server exists and that we have USAGE on it. Also
1496 : : * get the actual FDW for option validation etc.
1497 : : */
1498 : 240 : server = GetForeignServerByName(stmt->servername, false);
1218 peter@eisentraut.org 1499 : 237 : aclresult = object_aclcheck(ForeignServerRelationId, server->serverid, ownerId, ACL_USAGE);
5552 rhaas@postgresql.org 1500 [ - + ]: 237 : if (aclresult != ACLCHECK_OK)
3025 peter_e@gmx.net 1501 :UBC 0 : aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, server->servername);
1502 : :
5552 rhaas@postgresql.org 1503 :CBC 237 : fdw = GetForeignDataWrapper(server->fdwid);
1504 : :
1505 : : /*
1506 : : * Insert tuple into pg_foreign_table.
1507 : : */
1508 : 237 : memset(values, 0, sizeof(values));
1509 : 237 : memset(nulls, false, sizeof(nulls));
1510 : :
1511 : 237 : values[Anum_pg_foreign_table_ftrelid - 1] = ObjectIdGetDatum(relid);
1512 : 237 : values[Anum_pg_foreign_table_ftserver - 1] = ObjectIdGetDatum(server->serverid);
1513 : : /* Add table generic options */
1514 : 237 : ftoptions = transformGenericOptions(ForeignTableRelationId,
1515 : : PointerGetDatum(NULL),
1516 : : stmt->options,
1517 : : fdw->fdwvalidator);
1518 : :
172 peter@eisentraut.org 1519 [ + + ]:GNC 202 : if (DatumGetPointer(ftoptions) != NULL)
5552 rhaas@postgresql.org 1520 :CBC 146 : values[Anum_pg_foreign_table_ftoptions - 1] = ftoptions;
1521 : : else
1522 : 56 : nulls[Anum_pg_foreign_table_ftoptions - 1] = true;
1523 : :
1524 : 202 : tuple = heap_form_tuple(ftrel->rd_att, values, nulls);
1525 : :
3330 alvherre@alvh.no-ip. 1526 : 202 : CatalogTupleInsert(ftrel, tuple);
1527 : :
5552 rhaas@postgresql.org 1528 : 202 : heap_freetuple(tuple);
1529 : :
1530 : : /* Add pg_class dependency on the server */
1531 : 202 : myself.classId = RelationRelationId;
1532 : 202 : myself.objectId = relid;
1533 : 202 : myself.objectSubId = 0;
1534 : :
1535 : 202 : referenced.classId = ForeignServerRelationId;
1536 : 202 : referenced.objectId = server->serverid;
1537 : 202 : referenced.objectSubId = 0;
1538 : 202 : recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1539 : :
2610 andres@anarazel.de 1540 : 202 : table_close(ftrel, RowExclusiveLock);
5552 rhaas@postgresql.org 1541 : 202 : }
1542 : :
1543 : : /*
1544 : : * Import a foreign schema
1545 : : */
1546 : : void
4266 tgl@sss.pgh.pa.us 1547 : 24 : ImportForeignSchema(ImportForeignSchemaStmt *stmt)
1548 : : {
1549 : : ForeignServer *server;
1550 : : ForeignDataWrapper *fdw;
1551 : : FdwRoutine *fdw_routine;
1552 : : AclResult aclresult;
1553 : : List *cmd_list;
1554 : : ListCell *lc;
1555 : :
1556 : : /* Check that the foreign server exists and that we have USAGE on it */
1557 : 24 : server = GetForeignServerByName(stmt->server_name, false);
1218 peter@eisentraut.org 1558 : 23 : aclresult = object_aclcheck(ForeignServerRelationId, server->serverid, GetUserId(), ACL_USAGE);
4266 tgl@sss.pgh.pa.us 1559 [ - + ]: 23 : if (aclresult != ACLCHECK_OK)
3025 peter_e@gmx.net 1560 :UBC 0 : aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, server->servername);
1561 : :
1562 : : /* Check that the schema exists and we have CREATE permissions on it */
4266 tgl@sss.pgh.pa.us 1563 :CBC 23 : (void) LookupCreationNamespace(stmt->local_schema);
1564 : :
1565 : : /* Get the FDW and check it supports IMPORT */
1566 : 22 : fdw = GetForeignDataWrapper(server->fdwid);
1567 [ + + ]: 22 : if (!OidIsValid(fdw->fdwhandler))
1568 [ + - ]: 12 : ereport(ERROR,
1569 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1570 : : errmsg("foreign-data wrapper \"%s\" has no handler",
1571 : : fdw->fdwname)));
1572 : 10 : fdw_routine = GetFdwRoutine(fdw->fdwhandler);
1573 [ - + ]: 10 : if (fdw_routine->ImportForeignSchema == NULL)
4266 tgl@sss.pgh.pa.us 1574 [ # # ]:UBC 0 : ereport(ERROR,
1575 : : (errcode(ERRCODE_FDW_NO_SCHEMAS),
1576 : : errmsg("foreign-data wrapper \"%s\" does not support IMPORT FOREIGN SCHEMA",
1577 : : fdw->fdwname)));
1578 : :
1579 : : /* Call FDW to get a list of commands */
4266 tgl@sss.pgh.pa.us 1580 :CBC 10 : cmd_list = fdw_routine->ImportForeignSchema(stmt, server->serverid);
1581 : :
1582 : : /* Parse and execute each command */
1583 [ + - + + : 39 : foreach(lc, cmd_list)
+ + ]
1584 : : {
1585 : 32 : char *cmd = (char *) lfirst(lc);
1586 : : import_error_callback_arg callback_arg;
1587 : : ErrorContextCallback sqlerrcontext;
1588 : : List *raw_parsetree_list;
1589 : : ListCell *lc2;
1590 : :
1591 : : /*
1592 : : * Setup error traceback support for ereport(). This is so that any
1593 : : * error in the generated SQL will be displayed nicely.
1594 : : */
1595 : 32 : callback_arg.tablename = NULL; /* not known yet */
1596 : 32 : callback_arg.cmd = cmd;
1597 : 32 : sqlerrcontext.callback = import_error_callback;
472 peter@eisentraut.org 1598 : 32 : sqlerrcontext.arg = &callback_arg;
4266 tgl@sss.pgh.pa.us 1599 : 32 : sqlerrcontext.previous = error_context_stack;
1600 : 32 : error_context_stack = &sqlerrcontext;
1601 : :
1602 : : /*
1603 : : * Parse the SQL string into a list of raw parse trees.
1604 : : */
1605 : 32 : raw_parsetree_list = pg_parse_query(cmd);
1606 : :
1607 : : /*
1608 : : * Process each parse tree (we allow the FDW to put more than one
1609 : : * command per string, though this isn't really advised).
1610 : : */
1611 [ + - + + : 62 : foreach(lc2, raw_parsetree_list)
+ + ]
1612 : : {
3261 1613 : 32 : RawStmt *rs = lfirst_node(RawStmt, lc2);
3347 1614 : 32 : CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) rs->stmt;
1615 : : PlannedStmt *pstmt;
1616 : :
1617 : : /*
1618 : : * Because we only allow CreateForeignTableStmt, we can skip parse
1619 : : * analysis, rewrite, and planning steps here.
1620 : : */
4266 1621 [ - + ]: 32 : if (!IsA(cstmt, CreateForeignTableStmt))
4266 tgl@sss.pgh.pa.us 1622 [ # # ]:UBC 0 : elog(ERROR,
1623 : : "foreign-data wrapper \"%s\" returned incorrect statement type %d",
1624 : : fdw->fdwname, (int) nodeTag(cstmt));
1625 : :
1626 : : /* Ignore commands for tables excluded by filter options */
4266 tgl@sss.pgh.pa.us 1627 [ - + ]:CBC 32 : if (!IsImportableForeignTable(cstmt->base.relation->relname, stmt))
4266 tgl@sss.pgh.pa.us 1628 :UBC 0 : continue;
1629 : :
1630 : : /* Enable reporting of current table's name on error */
4266 tgl@sss.pgh.pa.us 1631 :CBC 32 : callback_arg.tablename = cstmt->base.relation->relname;
1632 : :
1633 : : /* Ensure creation schema is the one given in IMPORT statement */
1634 : 32 : cstmt->base.relation->schemaname = pstrdup(stmt->local_schema);
1635 : :
1636 : : /* No planning needed, just make a wrapper PlannedStmt */
3347 1637 : 32 : pstmt = makeNode(PlannedStmt);
1638 : 32 : pstmt->commandType = CMD_UTILITY;
1639 : 32 : pstmt->canSetTag = false;
1640 : 32 : pstmt->utilityStmt = (Node *) cstmt;
1641 : 32 : pstmt->stmt_location = rs->stmt_location;
1642 : 32 : pstmt->stmt_len = rs->stmt_len;
227 michael@paquier.xyz 1643 :GNC 32 : pstmt->planOrigin = PLAN_STMT_INTERNAL;
1644 : :
1645 : : /* Execute statement */
1731 tgl@sss.pgh.pa.us 1646 :CBC 32 : ProcessUtility(pstmt, cmd, false,
1647 : : PROCESS_UTILITY_SUBCOMMAND, NULL, NULL,
1648 : : None_Receiver, NULL);
1649 : :
1650 : : /* Be sure to advance the command counter between subcommands */
4266 1651 : 30 : CommandCounterIncrement();
1652 : :
1653 : 30 : callback_arg.tablename = NULL;
1654 : : }
1655 : :
1656 : 30 : error_context_stack = sqlerrcontext.previous;
1657 : : }
1658 : 7 : }
1659 : :
1660 : : /*
1661 : : * error context callback to let us supply the failing SQL statement's text
1662 : : */
1663 : : static void
1664 : 2 : import_error_callback(void *arg)
1665 : : {
1666 : 2 : import_error_callback_arg *callback_arg = (import_error_callback_arg *) arg;
1667 : : int syntaxerrposition;
1668 : :
1669 : : /* If it's a syntax error, convert to internal syntax error report */
1670 : 2 : syntaxerrposition = geterrposition();
1671 [ + + ]: 2 : if (syntaxerrposition > 0)
1672 : : {
1673 : 1 : errposition(0);
1674 : 1 : internalerrposition(syntaxerrposition);
1675 : 1 : internalerrquery(callback_arg->cmd);
1676 : : }
1677 : :
1678 [ + - ]: 2 : if (callback_arg->tablename)
1679 : 2 : errcontext("importing foreign table \"%s\"",
1680 : : callback_arg->tablename);
1681 : 2 : }
|