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