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