PostgreSQL Source Code  git master
foreign.c File Reference
Include dependency graph for foreign.c:

Go to the source code of this file.

Data Structures

struct  ConnectionOption
 

Functions

ForeignDataWrapperGetForeignDataWrapper (Oid fdwid)
 
ForeignDataWrapperGetForeignDataWrapperByName (const char *fdwname, bool missing_ok)
 
ForeignServerGetForeignServer (Oid serverid)
 
ForeignServerGetForeignServerByName (const char *srvname, bool missing_ok)
 
UserMappingGetUserMapping (Oid userid, Oid serverid)
 
ForeignTableGetForeignTable (Oid relid)
 
ListGetForeignColumnOptions (Oid relid, AttrNumber attnum)
 
FdwRoutineGetFdwRoutine (Oid fdwhandler)
 
Oid GetForeignServerIdByRelId (Oid relid)
 
FdwRoutineGetFdwRoutineByServerId (Oid serverid)
 
FdwRoutineGetFdwRoutineByRelId (Oid relid)
 
FdwRoutineGetFdwRoutineForRelation (Relation relation, bool makecopy)
 
bool IsImportableForeignTable (const char *tablename, ImportForeignSchemaStmt *stmt)
 
static void deflist_to_tuplestore (ReturnSetInfo *rsinfo, List *options)
 
Datum pg_options_to_table (PG_FUNCTION_ARGS)
 
static bool is_conninfo_option (const char *option, Oid context)
 
Datum postgresql_fdw_validator (PG_FUNCTION_ARGS)
 
Oid get_foreign_data_wrapper_oid (const char *fdwname, bool missing_ok)
 
Oid get_foreign_server_oid (const char *servername, bool missing_ok)
 
PathGetExistingLocalJoinPath (RelOptInfo *joinrel)
 

Variables

static struct ConnectionOption libpq_conninfo_options []
 

Function Documentation

◆ deflist_to_tuplestore()

static void deflist_to_tuplestore ( ReturnSetInfo rsinfo,
List options 
)
static

Definition at line 474 of file foreign.c.

References ReturnSetInfo::allowedModes, DefElem::arg, CreateTupleDescCopy(), CStringGetTextDatum, DefElem::defname, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, IsA, lfirst, MemoryContextSwitchTo(), ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), values, and work_mem.

Referenced by pg_options_to_table().

475 {
476  ListCell *cell;
477  TupleDesc tupdesc;
478  Tuplestorestate *tupstore;
479  Datum values[2];
480  bool nulls[2];
481  MemoryContext per_query_ctx;
482  MemoryContext oldcontext;
483 
484  /* check to see if caller supports us returning a tuplestore */
485  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
486  ereport(ERROR,
487  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
488  errmsg("set-valued function called in context that cannot accept a set")));
489  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
490  rsinfo->expectedDesc == NULL)
491  ereport(ERROR,
492  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
493  errmsg("materialize mode required, but it is not allowed in this context")));
494 
495  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
496  oldcontext = MemoryContextSwitchTo(per_query_ctx);
497 
498  /*
499  * Now prepare the result set.
500  */
501  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
502  tupstore = tuplestore_begin_heap(true, false, work_mem);
503  rsinfo->returnMode = SFRM_Materialize;
504  rsinfo->setResult = tupstore;
505  rsinfo->setDesc = tupdesc;
506 
507  foreach(cell, options)
508  {
509  DefElem *def = lfirst(cell);
510 
511  values[0] = CStringGetTextDatum(def->defname);
512  nulls[0] = false;
513  if (def->arg)
514  {
515  values[1] = CStringGetTextDatum(((Value *) (def->arg))->val.str);
516  nulls[1] = false;
517  }
518  else
519  {
520  values[1] = (Datum) 0;
521  nulls[1] = true;
522  }
523  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
524  }
525 
526  /* clean up and return the tuplestore */
527  tuplestore_donestoring(tupstore);
528 
529  MemoryContextSwitchTo(oldcontext);
530 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:102
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
TupleDesc expectedDesc
Definition: execnodes.h:268
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:720
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:372
int work_mem
Definition: globals.c:113
int allowedModes
Definition: execnodes.h:269
SetFunctionReturnMode returnMode
Definition: execnodes.h:271
#define lfirst(lc)
Definition: pg_list.h:106
Definition: value.h:42
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:203
Tuplestorestate * setResult
Definition: execnodes.h:274
static Datum values[MAXATTR]
Definition: bootstrap.c:164
ExprContext * econtext
Definition: execnodes.h:267
TupleDesc setDesc
Definition: execnodes.h:275
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
char * defname
Definition: parsenodes.h:719

◆ get_foreign_data_wrapper_oid()

Oid get_foreign_data_wrapper_oid ( const char *  fdwname,
bool  missing_ok 
)

Definition at line 659 of file foreign.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, FOREIGNDATAWRAPPERNAME, GetSysCacheOid1, and OidIsValid.

Referenced by convert_foreign_data_wrapper_name(), get_object_address_unqualified(), GetForeignDataWrapperByName(), and objectNamesToOids().

660 {
661  Oid oid;
662 
664  if (!OidIsValid(oid) && !missing_ok)
665  ereport(ERROR,
666  (errcode(ERRCODE_UNDEFINED_OBJECT),
667  errmsg("foreign-data wrapper \"%s\" does not exist",
668  fdwname)));
669  return oid;
670 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ get_foreign_server_oid()

Oid get_foreign_server_oid ( const char *  servername,
bool  missing_ok 
)

Definition at line 680 of file foreign.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, FOREIGNSERVERNAME, GetSysCacheOid1, and OidIsValid.

Referenced by convert_server_name(), get_object_address_unqualified(), GetForeignServerByName(), and objectNamesToOids().

681 {
682  Oid oid;
683 
685  if (!OidIsValid(oid) && !missing_ok)
686  ereport(ERROR,
687  (errcode(ERRCODE_UNDEFINED_OBJECT),
688  errmsg("server \"%s\" does not exist", servername)));
689  return oid;
690 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:584
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ GetExistingLocalJoinPath()

Path* GetExistingLocalJoinPath ( RelOptInfo joinrel)

Definition at line 716 of file foreign.c.

References Assert, ForeignPath::fdw_outerpath, JoinPath::innerjoinpath, IS_JOIN_REL, IsA, lfirst, makeNode, JoinPath::outerjoinpath, Path::param_info, Path::parent, ForeignPath::path, RelOptInfo::pathlist, Path::pathtype, T_HashJoin, T_MergeJoin, and T_NestLoop.

Referenced by postgresGetForeignJoinPaths().

717 {
718  ListCell *lc;
719 
720  Assert(IS_JOIN_REL(joinrel));
721 
722  foreach(lc, joinrel->pathlist)
723  {
724  Path *path = (Path *) lfirst(lc);
725  JoinPath *joinpath = NULL;
726 
727  /* Skip parameterized paths. */
728  if (path->param_info != NULL)
729  continue;
730 
731  switch (path->pathtype)
732  {
733  case T_HashJoin:
734  {
735  HashPath *hash_path = makeNode(HashPath);
736 
737  memcpy(hash_path, path, sizeof(HashPath));
738  joinpath = (JoinPath *) hash_path;
739  }
740  break;
741 
742  case T_NestLoop:
743  {
744  NestPath *nest_path = makeNode(NestPath);
745 
746  memcpy(nest_path, path, sizeof(NestPath));
747  joinpath = (JoinPath *) nest_path;
748  }
749  break;
750 
751  case T_MergeJoin:
752  {
753  MergePath *merge_path = makeNode(MergePath);
754 
755  memcpy(merge_path, path, sizeof(MergePath));
756  joinpath = (JoinPath *) merge_path;
757  }
758  break;
759 
760  default:
761 
762  /*
763  * Just skip anything else. We don't know if corresponding
764  * plan would build the output row from whole-row references
765  * of base relations and execute the EPQ checks.
766  */
767  break;
768  }
769 
770  /* This path isn't good for us, check next. */
771  if (!joinpath)
772  continue;
773 
774  /*
775  * If either inner or outer path is a ForeignPath corresponding to a
776  * pushed down join, replace it with the fdw_outerpath, so that we
777  * maintain path for EPQ checks built entirely of local join
778  * strategies.
779  */
780  if (IsA(joinpath->outerjoinpath, ForeignPath))
781  {
782  ForeignPath *foreign_path;
783 
784  foreign_path = (ForeignPath *) joinpath->outerjoinpath;
785  if (IS_JOIN_REL(foreign_path->path.parent))
786  joinpath->outerjoinpath = foreign_path->fdw_outerpath;
787  }
788 
789  if (IsA(joinpath->innerjoinpath, ForeignPath))
790  {
791  ForeignPath *foreign_path;
792 
793  foreign_path = (ForeignPath *) joinpath->innerjoinpath;
794  if (IS_JOIN_REL(foreign_path->path.parent))
795  joinpath->innerjoinpath = foreign_path->fdw_outerpath;
796  }
797 
798  return (Path *) joinpath;
799  }
800  return NULL;
801 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
Path * fdw_outerpath
Definition: relation.h:1219
Path * innerjoinpath
Definition: relation.h:1391
#define IS_JOIN_REL(rel)
Definition: relation.h:566
ParamPathInfo * param_info
Definition: relation.h:1045
NodeTag pathtype
Definition: relation.h:1040
RelOptInfo * parent
Definition: relation.h:1042
Path * outerjoinpath
Definition: relation.h:1390
#define makeNode(_type_)
Definition: nodes.h:560
#define Assert(condition)
Definition: c.h:680
#define lfirst(lc)
Definition: pg_list.h:106
List * pathlist
Definition: relation.h:599

◆ GetFdwRoutine()

FdwRoutine* GetFdwRoutine ( Oid  fdwhandler)

Definition at line 287 of file foreign.c.

References DatumGetPointer, elog, ERROR, IsA, and OidFunctionCall0.

Referenced by GetFdwRoutineByServerId(), and ImportForeignSchema().

288 {
289  Datum datum;
290  FdwRoutine *routine;
291 
292  datum = OidFunctionCall0(fdwhandler);
293  routine = (FdwRoutine *) DatumGetPointer(datum);
294 
295  if (routine == NULL || !IsA(routine, FdwRoutine))
296  elog(ERROR, "foreign-data wrapper handler function %u did not return an FdwRoutine struct",
297  fdwhandler);
298 
299  return routine;
300 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
#define ERROR
Definition: elog.h:43
uintptr_t Datum
Definition: postgres.h:372
#define OidFunctionCall0(functionId)
Definition: fmgr.h:621
#define DatumGetPointer(X)
Definition: postgres.h:555
#define elog
Definition: elog.h:219

◆ GetFdwRoutineByRelId()

FdwRoutine* GetFdwRoutineByRelId ( Oid  relid)

Definition at line 372 of file foreign.c.

References GetFdwRoutineByServerId(), and GetForeignServerIdByRelId().

Referenced by GetFdwRoutineForRelation(), make_modifytable(), and select_rowmark_type().

373 {
374  Oid serverid;
375 
376  /* Get server OID for the foreign table. */
377  serverid = GetForeignServerIdByRelId(relid);
378 
379  /* Now retrieve server's FdwRoutine struct. */
380  return GetFdwRoutineByServerId(serverid);
381 }
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition: foreign.c:330
unsigned int Oid
Definition: postgres_ext.h:31
Oid GetForeignServerIdByRelId(Oid relid)
Definition: foreign.c:308

◆ GetFdwRoutineByServerId()

FdwRoutine* GetFdwRoutineByServerId ( Oid  serverid)

Definition at line 330 of file foreign.c.

References elog, ereport, errcode(), errmsg(), ERROR, FOREIGNDATAWRAPPEROID, FOREIGNSERVEROID, GetFdwRoutine(), GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), and SearchSysCache1().

Referenced by ExecInitForeignScan(), and GetFdwRoutineByRelId().

331 {
332  HeapTuple tp;
334  Form_pg_foreign_server serverform;
335  Oid fdwid;
336  Oid fdwhandler;
337 
338  /* Get foreign-data wrapper OID for the server. */
340  if (!HeapTupleIsValid(tp))
341  elog(ERROR, "cache lookup failed for foreign server %u", serverid);
342  serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
343  fdwid = serverform->srvfdw;
344  ReleaseSysCache(tp);
345 
346  /* Get handler function OID for the FDW. */
348  if (!HeapTupleIsValid(tp))
349  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
350  fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp);
351  fdwhandler = fdwform->fdwhandler;
352 
353  /* Complain if FDW has been set to NO HANDLER. */
354  if (!OidIsValid(fdwhandler))
355  ereport(ERROR,
356  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
357  errmsg("foreign-data wrapper \"%s\" has no handler",
358  NameStr(fdwform->fdwname))));
359 
360  ReleaseSysCache(tp);
361 
362  /* And finally, call the handler function. */
363  return GetFdwRoutine(fdwhandler);
364 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
FdwRoutine * GetFdwRoutine(Oid fdwhandler)
Definition: foreign.c:287
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:557
#define elog
Definition: elog.h:219

◆ GetFdwRoutineForRelation()

FdwRoutine* GetFdwRoutineForRelation ( Relation  relation,
bool  makecopy 
)

Definition at line 395 of file foreign.c.

References CacheMemoryContext, GetFdwRoutineByRelId(), MemoryContextAlloc(), palloc(), RelationData::rd_fdwroutine, and RelationGetRelid.

Referenced by acquire_inherited_sample_rows(), analyze_rel(), CheckValidRowMarkRel(), EvalPlanQualFetchRowMarks(), ExecInitForeignScan(), ExecLockRows(), get_relation_info(), InitResultRelInfo(), relation_is_updatable(), and rewriteTargetListUD().

396 {
397  FdwRoutine *fdwroutine;
398  FdwRoutine *cfdwroutine;
399 
400  if (relation->rd_fdwroutine == NULL)
401  {
402  /* Get the info by consulting the catalogs and the FDW code */
403  fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation));
404 
405  /* Save the data for later reuse in CacheMemoryContext */
407  sizeof(FdwRoutine));
408  memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine));
409  relation->rd_fdwroutine = cfdwroutine;
410 
411  /* Give back the locally palloc'd copy regardless of makecopy */
412  return fdwroutine;
413  }
414 
415  /* We have valid cached data --- does the caller want a copy? */
416  if (makecopy)
417  {
418  fdwroutine = (FdwRoutine *) palloc(sizeof(FdwRoutine));
419  memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine));
420  return fdwroutine;
421  }
422 
423  /* Only a short-lived reference is needed, so just hand back cached copy */
424  return relation->rd_fdwroutine;
425 }
struct FdwRoutine * rd_fdwroutine
Definition: rel.h:204
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition: foreign.c:372
void * palloc(Size size)
Definition: mcxt.c:835
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
#define RelationGetRelid(relation)
Definition: rel.h:425
MemoryContext CacheMemoryContext
Definition: mcxt.c:46

◆ GetForeignColumnOptions()

List* GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 254 of file foreign.c.

References Anum_pg_attribute_attfdwoptions, ATTNUM, elog, ERROR, HeapTupleIsValid, Int16GetDatum, NIL, ObjectIdGetDatum, options, ReleaseSysCache(), SearchSysCache2(), SysCacheGetAttr(), and untransformRelOptions().

Referenced by deparseAnalyzeSql(), deparseColumnRef(), and get_file_fdw_attribute_options().

255 {
256  List *options;
257  HeapTuple tp;
258  Datum datum;
259  bool isnull;
260 
261  tp = SearchSysCache2(ATTNUM,
262  ObjectIdGetDatum(relid),
263  Int16GetDatum(attnum));
264  if (!HeapTupleIsValid(tp))
265  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
266  attnum, relid);
267  datum = SysCacheGetAttr(ATTNUM,
268  tp,
270  &isnull);
271  if (isnull)
272  options = NIL;
273  else
274  options = untransformRelOptions(datum);
275 
276  ReleaseSysCache(tp);
277 
278  return options;
279 }
#define NIL
Definition: pg_list.h:69
#define Int16GetDatum(X)
Definition: postgres.h:457
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
static char ** options
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
#define Anum_pg_attribute_attfdwoptions
Definition: pg_attribute.h:216
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ GetForeignDataWrapper()

ForeignDataWrapper* GetForeignDataWrapper ( Oid  fdwid)

Definition at line 35 of file foreign.c.

References Anum_pg_foreign_data_wrapper_fdwoptions, elog, ERROR, ForeignDataWrapper::fdwhandler, ForeignDataWrapper::fdwid, ForeignDataWrapper::fdwname, ForeignDataWrapper::fdwvalidator, FOREIGNDATAWRAPPEROID, GETSTRUCT, HeapTupleIsValid, NameStr, NIL, ObjectIdGetDatum, ForeignDataWrapper::options, ForeignDataWrapper::owner, palloc(), pstrdup(), ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and untransformRelOptions().

Referenced by AlterForeignServer(), AlterForeignServerOwner_internal(), AlterUserMapping(), ATExecAlterColumnGenericOptions(), ATExecGenericOptions(), CreateForeignTable(), CreateUserMapping(), fileGetOptions(), get_connect_string(), GetForeignDataWrapperByName(), getObjectDescription(), getObjectIdentityParts(), and ImportForeignSchema().

36 {
38  ForeignDataWrapper *fdw;
39  Datum datum;
40  HeapTuple tp;
41  bool isnull;
42 
44 
45  if (!HeapTupleIsValid(tp))
46  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
47 
49 
50  fdw = (ForeignDataWrapper *) palloc(sizeof(ForeignDataWrapper));
51  fdw->fdwid = fdwid;
52  fdw->owner = fdwform->fdwowner;
53  fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
54  fdw->fdwhandler = fdwform->fdwhandler;
55  fdw->fdwvalidator = fdwform->fdwvalidator;
56 
57  /* Extract the fdwoptions */
59  tp,
61  &isnull);
62  if (isnull)
63  fdw->options = NIL;
64  else
65  fdw->options = untransformRelOptions(datum);
66 
67  ReleaseSysCache(tp);
68 
69  return fdw;
70 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
char * fdwname
Definition: foreign.h:39
char * pstrdup(const char *in)
Definition: mcxt.c:1063
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
List * options
Definition: foreign.h:42
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
void * palloc(Size size)
Definition: mcxt.c:835
#define NameStr(name)
Definition: c.h:557
#define elog
Definition: elog.h:219
#define Anum_pg_foreign_data_wrapper_fdwoptions

◆ GetForeignDataWrapperByName()

ForeignDataWrapper* GetForeignDataWrapperByName ( const char *  fdwname,
bool  missing_ok 
)

Definition at line 78 of file foreign.c.

References get_foreign_data_wrapper_oid(), GetForeignDataWrapper(), and OidIsValid.

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

79 {
80  Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok);
81 
82  if (!OidIsValid(fdwId))
83  return NULL;
84 
85  return GetForeignDataWrapper(fdwId);
86 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition: foreign.c:659
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition: foreign.c:35

◆ GetForeignServer()

ForeignServer* GetForeignServer ( Oid  serverid)

Definition at line 93 of file foreign.c.

References Anum_pg_foreign_server_srvoptions, Anum_pg_foreign_server_srvtype, Anum_pg_foreign_server_srvversion, elog, ERROR, ForeignServer::fdwid, FOREIGNSERVEROID, GETSTRUCT, HeapTupleIsValid, NameStr, NIL, ObjectIdGetDatum, ForeignServer::options, ForeignServer::owner, palloc(), pstrdup(), ReleaseSysCache(), SearchSysCache1(), ForeignServer::serverid, ForeignServer::servername, ForeignServer::servertype, ForeignServer::serverversion, SysCacheGetAttr(), TextDatumGetCString, and untransformRelOptions().

Referenced by ATExecAlterColumnGenericOptions(), ATExecGenericOptions(), fileGetOptions(), GetConnection(), GetForeignServerByName(), getObjectDescription(), getObjectIdentityParts(), pgfdw_reject_incomplete_xact_state_change(), postgresAcquireSampleRowsFunc(), postgresGetForeignRelSize(), postgresImportForeignSchema(), and postgresIsForeignRelUpdatable().

94 {
95  Form_pg_foreign_server serverform;
96  ForeignServer *server;
97  HeapTuple tp;
98  Datum datum;
99  bool isnull;
100 
102 
103  if (!HeapTupleIsValid(tp))
104  elog(ERROR, "cache lookup failed for foreign server %u", serverid);
105 
106  serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
107 
108  server = (ForeignServer *) palloc(sizeof(ForeignServer));
109  server->serverid = serverid;
110  server->servername = pstrdup(NameStr(serverform->srvname));
111  server->owner = serverform->srvowner;
112  server->fdwid = serverform->srvfdw;
113 
114  /* Extract server type */
116  tp,
118  &isnull);
119  server->servertype = isnull ? NULL : TextDatumGetCString(datum);
120 
121  /* Extract server version */
123  tp,
125  &isnull);
126  server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
127 
128  /* Extract the srvoptions */
130  tp,
132  &isnull);
133  if (isnull)
134  server->options = NIL;
135  else
136  server->options = untransformRelOptions(datum);
137 
138  ReleaseSysCache(tp);
139 
140  return server;
141 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define Anum_pg_foreign_server_srvversion
char * pstrdup(const char *in)
Definition: mcxt.c:1063
char * serverversion
Definition: foreign.h:52
char * servertype
Definition: foreign.h:51
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Anum_pg_foreign_server_srvtype
void * palloc(Size size)
Definition: mcxt.c:835
char * servername
Definition: foreign.h:50
#define NameStr(name)
Definition: c.h:557
#define elog
Definition: elog.h:219
#define Anum_pg_foreign_server_srvoptions
List * options
Definition: foreign.h:53
Oid serverid
Definition: foreign.h:47

◆ GetForeignServerByName()

ForeignServer* GetForeignServerByName ( const char *  srvname,
bool  missing_ok 
)

Definition at line 148 of file foreign.c.

References get_foreign_server_oid(), GetForeignServer(), and OidIsValid.

Referenced by AlterUserMapping(), CreateForeignServer(), CreateForeignTable(), CreateUserMapping(), get_connect_string(), get_object_address_usermapping(), ImportForeignSchema(), and RemoveUserMapping().

149 {
150  Oid serverid = get_foreign_server_oid(srvname, missing_ok);
151 
152  if (!OidIsValid(serverid))
153  return NULL;
154 
155  return GetForeignServer(serverid);
156 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:586
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:93
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition: foreign.c:680

◆ GetForeignServerIdByRelId()

Oid GetForeignServerIdByRelId ( Oid  relid)

Definition at line 308 of file foreign.c.

References elog, ERROR, FOREIGNTABLEREL, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), and SearchSysCache1().

Referenced by get_relation_info(), and GetFdwRoutineByRelId().

309 {
310  HeapTuple tp;
311  Form_pg_foreign_table tableform;
312  Oid serverid;
313 
315  if (!HeapTupleIsValid(tp))
316  elog(ERROR, "cache lookup failed for foreign table %u", relid);
317  tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
318  serverid = tableform->ftserver;
319  ReleaseSysCache(tp);
320 
321  return serverid;
322 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
unsigned int Oid
Definition: postgres_ext.h:31
FormData_pg_foreign_table * Form_pg_foreign_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define elog
Definition: elog.h:219

◆ GetForeignTable()

ForeignTable* GetForeignTable ( Oid  relid)

Definition at line 216 of file foreign.c.

References Anum_pg_foreign_table_ftoptions, elog, ERROR, FOREIGNTABLEREL, GETSTRUCT, HeapTupleIsValid, NIL, ObjectIdGetDatum, ForeignTable::options, palloc(), ReleaseSysCache(), ForeignTable::relid, SearchSysCache1(), ForeignTable::serverid, SysCacheGetAttr(), and untransformRelOptions().

Referenced by check_selective_binary_conversion(), deparseRelation(), fileGetOptions(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignModify(), postgresBeginForeignScan(), postgresGetForeignRelSize(), and postgresIsForeignRelUpdatable().

217 {
218  Form_pg_foreign_table tableform;
219  ForeignTable *ft;
220  HeapTuple tp;
221  Datum datum;
222  bool isnull;
223 
225  if (!HeapTupleIsValid(tp))
226  elog(ERROR, "cache lookup failed for foreign table %u", relid);
227  tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
228 
229  ft = (ForeignTable *) palloc(sizeof(ForeignTable));
230  ft->relid = relid;
231  ft->serverid = tableform->ftserver;
232 
233  /* Extract the ftoptions */
235  tp,
237  &isnull);
238  if (isnull)
239  ft->options = NIL;
240  else
241  ft->options = untransformRelOptions(datum);
242 
243  ReleaseSysCache(tp);
244 
245  return ft;
246 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
FormData_pg_foreign_table * Form_pg_foreign_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid relid
Definition: foreign.h:66
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
#define Anum_pg_foreign_table_ftoptions
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Oid serverid
Definition: foreign.h:67
List * options
Definition: foreign.h:68
void * palloc(Size size)
Definition: mcxt.c:835
#define elog
Definition: elog.h:219

◆ GetUserMapping()

UserMapping* GetUserMapping ( Oid  userid,
Oid  serverid 
)

Definition at line 166 of file foreign.c.

References Anum_pg_user_mapping_umoptions, ereport, errcode(), errmsg(), ERROR, HeapTupleGetOid, HeapTupleIsValid, InvalidOid, MappingUserName, NIL, ObjectIdGetDatum, UserMapping::options, palloc(), ReleaseSysCache(), SearchSysCache2(), UserMapping::serverid, SysCacheGetAttr(), UserMapping::umid, untransformRelOptions(), and UserMapping::userid.

Referenced by get_connect_string(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignModify(), postgresBeginForeignScan(), postgresGetForeignRelSize(), and postgresImportForeignSchema().

167 {
168  Datum datum;
169  HeapTuple tp;
170  bool isnull;
171  UserMapping *um;
172 
173  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
174  ObjectIdGetDatum(userid),
175  ObjectIdGetDatum(serverid));
176 
177  if (!HeapTupleIsValid(tp))
178  {
179  /* Not found for the specific user -- try PUBLIC */
180  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
182  ObjectIdGetDatum(serverid));
183  }
184 
185  if (!HeapTupleIsValid(tp))
186  ereport(ERROR,
187  (errcode(ERRCODE_UNDEFINED_OBJECT),
188  errmsg("user mapping not found for \"%s\"",
189  MappingUserName(userid))));
190 
191  um = (UserMapping *) palloc(sizeof(UserMapping));
192  um->umid = HeapTupleGetOid(tp);
193  um->userid = userid;
194  um->serverid = serverid;
195 
196  /* Extract the umoptions */
197  datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
198  tp,
200  &isnull);
201  if (isnull)
202  um->options = NIL;
203  else
204  um->options = untransformRelOptions(datum);
205 
206  ReleaseSysCache(tp);
207 
208  return um;
209 }
Oid umid
Definition: foreign.h:58
#define NIL
Definition: pg_list.h:69
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid userid
Definition: foreign.h:59
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define Anum_pg_user_mapping_umoptions
List * options
Definition: foreign.h:61
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
Oid serverid
Definition: foreign.h:60
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1123
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700
#define MappingUserName(userid)
Definition: foreign.h:20

◆ is_conninfo_option()

static bool is_conninfo_option ( const char *  option,
Oid  context 
)
static

Definition at line 588 of file foreign.c.

References ConnectionOption::optcontext, and ConnectionOption::optname.

Referenced by postgresql_fdw_validator().

589 {
590  struct ConnectionOption *opt;
591 
592  for (opt = libpq_conninfo_options; opt->optname; opt++)
593  if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
594  return true;
595  return false;
596 }
const char * optname
Definition: foreign.c:554
static struct ConnectionOption libpq_conninfo_options[]
Definition: foreign.c:563

◆ IsImportableForeignTable()

bool IsImportableForeignTable ( const char *  tablename,
ImportForeignSchemaStmt stmt 
)

Definition at line 435 of file foreign.c.

References FDW_IMPORT_SCHEMA_ALL, FDW_IMPORT_SCHEMA_EXCEPT, FDW_IMPORT_SCHEMA_LIMIT_TO, lfirst, ImportForeignSchemaStmt::list_type, RangeVar::relname, and ImportForeignSchemaStmt::table_list.

Referenced by ImportForeignSchema().

437 {
438  ListCell *lc;
439 
440  switch (stmt->list_type)
441  {
443  return true;
444 
446  foreach(lc, stmt->table_list)
447  {
448  RangeVar *rv = (RangeVar *) lfirst(lc);
449 
450  if (strcmp(tablename, rv->relname) == 0)
451  return true;
452  }
453  return false;
454 
456  foreach(lc, stmt->table_list)
457  {
458  RangeVar *rv = (RangeVar *) lfirst(lc);
459 
460  if (strcmp(tablename, rv->relname) == 0)
461  return false;
462  }
463  return true;
464  }
465  return false; /* shouldn't get here */
466 }
char * relname
Definition: primnodes.h:68
#define lfirst(lc)
Definition: pg_list.h:106
ImportForeignSchemaType list_type
Definition: parsenodes.h:2311

◆ pg_options_to_table()

Datum pg_options_to_table ( PG_FUNCTION_ARGS  )

Definition at line 538 of file foreign.c.

References deflist_to_tuplestore(), PG_GETARG_DATUM, and untransformRelOptions().

539 {
540  Datum array = PG_GETARG_DATUM(0);
541 
542  deflist_to_tuplestore((ReturnSetInfo *) fcinfo->resultinfo,
543  untransformRelOptions(array));
544 
545  return (Datum) 0;
546 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
static void deflist_to_tuplestore(ReturnSetInfo *rsinfo, List *options)
Definition: foreign.c:474
uintptr_t Datum
Definition: postgres.h:372
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917

◆ postgresql_fdw_validator()

Datum postgresql_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 612 of file foreign.c.

References appendStringInfo(), buf, StringInfoData::data, DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, initStringInfo(), is_conninfo_option(), StringInfoData::len, lfirst, ConnectionOption::optcontext, ConnectionOption::optname, PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_BOOL, and untransformRelOptions().

613 {
614  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
615  Oid catalog = PG_GETARG_OID(1);
616 
617  ListCell *cell;
618 
619  foreach(cell, options_list)
620  {
621  DefElem *def = lfirst(cell);
622 
623  if (!is_conninfo_option(def->defname, catalog))
624  {
625  struct ConnectionOption *opt;
627 
628  /*
629  * Unknown option specified, complain about it. Provide a hint
630  * with list of valid options for the object.
631  */
632  initStringInfo(&buf);
633  for (opt = libpq_conninfo_options; opt->optname; opt++)
634  if (catalog == opt->optcontext)
635  appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
636  opt->optname);
637 
638  ereport(ERROR,
639  (errcode(ERRCODE_SYNTAX_ERROR),
640  errmsg("invalid option \"%s\"", def->defname),
641  errhint("Valid options in this context are: %s",
642  buf.data)));
643 
644  PG_RETURN_BOOL(false);
645  }
646  }
647 
648  PG_RETURN_BOOL(true);
649 }
int errhint(const char *fmt,...)
Definition: elog.c:987
static bool is_conninfo_option(const char *option, Oid context)
Definition: foreign.c:588
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
List * untransformRelOptions(Datum options)
Definition: reloptions.c:917
#define lfirst(lc)
Definition: pg_list.h:106
const char * optname
Definition: foreign.c:554
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
Definition: pg_list.h:45
static struct ConnectionOption libpq_conninfo_options[]
Definition: foreign.c:563

Variable Documentation

◆ libpq_conninfo_options

struct ConnectionOption libpq_conninfo_options[]
static
Initial value:
= {
{"authtype", ForeignServerRelationId},
{"password", UserMappingRelationId},
{"connect_timeout", ForeignServerRelationId},
{"hostaddr", ForeignServerRelationId},
{"requiressl", ForeignServerRelationId},
{NULL, InvalidOid}
}
#define InvalidOid
Definition: postgres_ext.h:36
#define ForeignServerRelationId
#define UserMappingRelationId

Definition at line 563 of file foreign.c.