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)
 
ForeignDataWrapperGetForeignDataWrapperExtended (Oid fdwid, bits16 flags)
 
ForeignDataWrapperGetForeignDataWrapperByName (const char *fdwname, bool missing_ok)
 
ForeignServerGetForeignServer (Oid serverid)
 
ForeignServerGetForeignServerExtended (Oid serverid, bits16 flags)
 
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)
 
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 const struct ConnectionOption libpq_conninfo_options []
 

Function Documentation

◆ 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.

660 {
661  Oid oid;
662 
664  Anum_pg_foreign_data_wrapper_oid,
665  CStringGetDatum(fdwname));
666  if (!OidIsValid(oid) && !missing_ok)
667  ereport(ERROR,
668  (errcode(ERRCODE_UNDEFINED_OBJECT),
669  errmsg("foreign-data wrapper \"%s\" does not exist",
670  fdwname)));
671  return oid;
672 }
#define OidIsValid(objectId)
Definition: c.h:710
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define CStringGetDatum(X)
Definition: postgres.h:622
unsigned int Oid
Definition: postgres_ext.h:31
@ FOREIGNDATAWRAPPERNAME
Definition: syscache.h:61
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:197

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

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

◆ get_foreign_server_oid()

Oid get_foreign_server_oid ( const char *  servername,
bool  missing_ok 
)

Definition at line 682 of file foreign.c.

683 {
684  Oid oid;
685 
686  oid = GetSysCacheOid1(FOREIGNSERVERNAME, Anum_pg_foreign_server_oid,
687  CStringGetDatum(servername));
688  if (!OidIsValid(oid) && !missing_ok)
689  ereport(ERROR,
690  (errcode(ERRCODE_UNDEFINED_OBJECT),
691  errmsg("server \"%s\" does not exist", servername)));
692  return oid;
693 }
@ FOREIGNSERVERNAME
Definition: syscache.h:63

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

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

◆ GetExistingLocalJoinPath()

Path* GetExistingLocalJoinPath ( RelOptInfo joinrel)

Definition at line 719 of file foreign.c.

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

References Assert(), ForeignPath::fdw_outerpath, if(), 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().

◆ GetFdwRoutine()

FdwRoutine* GetFdwRoutine ( Oid  fdwhandler)

Definition at line 320 of file foreign.c.

321 {
322  Datum datum;
323  FdwRoutine *routine;
324 
325  datum = OidFunctionCall0(fdwhandler);
326  routine = (FdwRoutine *) DatumGetPointer(datum);
327 
328  if (routine == NULL || !IsA(routine, FdwRoutine))
329  elog(ERROR, "foreign-data wrapper handler function %u did not return an FdwRoutine struct",
330  fdwhandler);
331 
332  return routine;
333 }
#define elog(elevel,...)
Definition: elog.h:218
#define OidFunctionCall0(functionId)
Definition: fmgr.h:667
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593

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

Referenced by GetFdwRoutineByServerId(), and ImportForeignSchema().

◆ GetFdwRoutineByRelId()

FdwRoutine* GetFdwRoutineByRelId ( Oid  relid)

Definition at line 405 of file foreign.c.

406 {
407  Oid serverid;
408 
409  /* Get server OID for the foreign table. */
410  serverid = GetForeignServerIdByRelId(relid);
411 
412  /* Now retrieve server's FdwRoutine struct. */
413  return GetFdwRoutineByServerId(serverid);
414 }
Oid GetForeignServerIdByRelId(Oid relid)
Definition: foreign.c:341
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition: foreign.c:363

References GetFdwRoutineByServerId(), and GetForeignServerIdByRelId().

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

◆ GetFdwRoutineByServerId()

FdwRoutine* GetFdwRoutineByServerId ( Oid  serverid)

Definition at line 363 of file foreign.c.

364 {
365  HeapTuple tp;
367  Form_pg_foreign_server serverform;
368  Oid fdwid;
369  Oid fdwhandler;
370 
371  /* Get foreign-data wrapper OID for the server. */
373  if (!HeapTupleIsValid(tp))
374  elog(ERROR, "cache lookup failed for foreign server %u", serverid);
375  serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
376  fdwid = serverform->srvfdw;
377  ReleaseSysCache(tp);
378 
379  /* Get handler function OID for the FDW. */
381  if (!HeapTupleIsValid(tp))
382  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
383  fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp);
384  fdwhandler = fdwform->fdwhandler;
385 
386  /* Complain if FDW has been set to NO HANDLER. */
387  if (!OidIsValid(fdwhandler))
388  ereport(ERROR,
389  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
390  errmsg("foreign-data wrapper \"%s\" has no handler",
391  NameStr(fdwform->fdwname))));
392 
393  ReleaseSysCache(tp);
394 
395  /* And finally, call the handler function. */
396  return GetFdwRoutine(fdwhandler);
397 }
#define NameStr(name)
Definition: c.h:681
FdwRoutine * GetFdwRoutine(Oid fdwhandler)
Definition: foreign.c:320
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
FormData_pg_foreign_server * Form_pg_foreign_server
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
@ FOREIGNSERVEROID
Definition: syscache.h:64
@ FOREIGNDATAWRAPPEROID
Definition: syscache.h:62

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

Referenced by ExecInitForeignScan(), ExecuteTruncateGuts(), GetFdwRoutineByRelId(), and truncate_check_rel().

◆ GetFdwRoutineForRelation()

FdwRoutine* GetFdwRoutineForRelation ( Relation  relation,
bool  makecopy 
)

Definition at line 428 of file foreign.c.

429 {
430  FdwRoutine *fdwroutine;
431  FdwRoutine *cfdwroutine;
432 
433  if (relation->rd_fdwroutine == NULL)
434  {
435  /* Get the info by consulting the catalogs and the FDW code */
436  fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation));
437 
438  /* Save the data for later reuse in CacheMemoryContext */
440  sizeof(FdwRoutine));
441  memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine));
442  relation->rd_fdwroutine = cfdwroutine;
443 
444  /* Give back the locally palloc'd copy regardless of makecopy */
445  return fdwroutine;
446  }
447 
448  /* We have valid cached data --- does the caller want a copy? */
449  if (makecopy)
450  {
451  fdwroutine = (FdwRoutine *) palloc(sizeof(FdwRoutine));
452  memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine));
453  return fdwroutine;
454  }
455 
456  /* Only a short-lived reference is needed, so just hand back cached copy */
457  return relation->rd_fdwroutine;
458 }
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition: foreign.c:405
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
MemoryContext CacheMemoryContext
Definition: mcxt.c:51
void * palloc(Size size)
Definition: mcxt.c:1068
#define RelationGetRelid(relation)
Definition: rel.h:488
struct FdwRoutine * rd_fdwroutine
Definition: rel.h:235

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

Referenced by acquire_inherited_sample_rows(), add_row_identity_columns(), analyze_rel(), CheckValidRowMarkRel(), EvalPlanQualFetchRowMark(), ExecInitForeignScan(), ExecLockRows(), get_relation_info(), InitResultRelInfo(), and relation_is_updatable().

◆ GetForeignColumnOptions()

List* GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 287 of file foreign.c.

288 {
289  List *options;
290  HeapTuple tp;
291  Datum datum;
292  bool isnull;
293 
294  tp = SearchSysCache2(ATTNUM,
295  ObjectIdGetDatum(relid),
297  if (!HeapTupleIsValid(tp))
298  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
299  attnum, relid);
300  datum = SysCacheGetAttr(ATTNUM,
301  tp,
302  Anum_pg_attribute_attfdwoptions,
303  &isnull);
304  if (isnull)
305  options = NIL;
306  else
308 
309  ReleaseSysCache(tp);
310 
311  return options;
312 }
int16 attnum
Definition: pg_attribute.h:83
#define NIL
Definition: pg_list.h:65
static char ** options
#define Int16GetDatum(X)
Definition: postgres.h:495
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1334
Definition: pg_list.h:51
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
@ ATTNUM
Definition: syscache.h:41

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

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

◆ GetForeignDataWrapper()

◆ GetForeignDataWrapperByName()

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

Definition at line 95 of file foreign.c.

96 {
97  Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok);
98 
99  if (!OidIsValid(fdwId))
100  return NULL;
101 
102  return GetForeignDataWrapper(fdwId);
103 }
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition: foreign.c:36
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition: foreign.c:659

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper* GetForeignDataWrapperExtended ( Oid  fdwid,
bits16  flags 
)

Definition at line 48 of file foreign.c.

49 {
51  ForeignDataWrapper *fdw;
52  Datum datum;
53  HeapTuple tp;
54  bool isnull;
55 
57 
58  if (!HeapTupleIsValid(tp))
59  {
60  if ((flags & FDW_MISSING_OK) == 0)
61  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
62  return NULL;
63  }
64 
66 
67  fdw = (ForeignDataWrapper *) palloc(sizeof(ForeignDataWrapper));
68  fdw->fdwid = fdwid;
69  fdw->owner = fdwform->fdwowner;
70  fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
71  fdw->fdwhandler = fdwform->fdwhandler;
72  fdw->fdwvalidator = fdwform->fdwvalidator;
73 
74  /* Extract the fdwoptions */
76  tp,
77  Anum_pg_foreign_data_wrapper_fdwoptions,
78  &isnull);
79  if (isnull)
80  fdw->options = NIL;
81  else
82  fdw->options = untransformRelOptions(datum);
83 
84  ReleaseSysCache(tp);
85 
86  return fdw;
87 }
#define FDW_MISSING_OK
Definition: foreign.h:64
char * pstrdup(const char *in)
Definition: mcxt.c:1305
char * fdwname
Definition: foreign.h:28
List * options
Definition: foreign.h:31

References elog, ERROR, FDW_MISSING_OK, 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 GetForeignDataWrapper(), getObjectDescription(), and getObjectIdentityParts().

◆ GetForeignServer()

◆ GetForeignServerByName()

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

Definition at line 181 of file foreign.c.

182 {
183  Oid serverid = get_foreign_server_oid(srvname, missing_ok);
184 
185  if (!OidIsValid(serverid))
186  return NULL;
187 
188  return GetForeignServer(serverid);
189 }
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition: foreign.c:682
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:110

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

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

◆ GetForeignServerExtended()

ForeignServer* GetForeignServerExtended ( Oid  serverid,
bits16  flags 
)

Definition at line 122 of file foreign.c.

123 {
124  Form_pg_foreign_server serverform;
125  ForeignServer *server;
126  HeapTuple tp;
127  Datum datum;
128  bool isnull;
129 
131 
132  if (!HeapTupleIsValid(tp))
133  {
134  if ((flags & FSV_MISSING_OK) == 0)
135  elog(ERROR, "cache lookup failed for foreign server %u", serverid);
136  return NULL;
137  }
138 
139  serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
140 
141  server = (ForeignServer *) palloc(sizeof(ForeignServer));
142  server->serverid = serverid;
143  server->servername = pstrdup(NameStr(serverform->srvname));
144  server->owner = serverform->srvowner;
145  server->fdwid = serverform->srvfdw;
146 
147  /* Extract server type */
149  tp,
150  Anum_pg_foreign_server_srvtype,
151  &isnull);
152  server->servertype = isnull ? NULL : TextDatumGetCString(datum);
153 
154  /* Extract server version */
156  tp,
157  Anum_pg_foreign_server_srvversion,
158  &isnull);
159  server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
160 
161  /* Extract the srvoptions */
163  tp,
164  Anum_pg_foreign_server_srvoptions,
165  &isnull);
166  if (isnull)
167  server->options = NIL;
168  else
169  server->options = untransformRelOptions(datum);
170 
171  ReleaseSysCache(tp);
172 
173  return server;
174 }
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define FSV_MISSING_OK
Definition: foreign.h:61
List * options
Definition: foreign.h:42
char * serverversion
Definition: foreign.h:41
char * servername
Definition: foreign.h:39
Oid serverid
Definition: foreign.h:36
char * servertype
Definition: foreign.h:40

References elog, ERROR, ForeignServer::fdwid, FOREIGNSERVEROID, FSV_MISSING_OK, 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 disconnect_cached_connections(), GetForeignServer(), getObjectDescription(), getObjectIdentityParts(), and postgres_fdw_get_connections().

◆ GetForeignServerIdByRelId()

Oid GetForeignServerIdByRelId ( Oid  relid)

Definition at line 341 of file foreign.c.

342 {
343  HeapTuple tp;
344  Form_pg_foreign_table tableform;
345  Oid serverid;
346 
348  if (!HeapTupleIsValid(tp))
349  elog(ERROR, "cache lookup failed for foreign table %u", relid);
350  tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
351  serverid = tableform->ftserver;
352  ReleaseSysCache(tp);
353 
354  return serverid;
355 }
FormData_pg_foreign_table * Form_pg_foreign_table
@ FOREIGNTABLEREL
Definition: syscache.h:65

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

Referenced by ExecuteTruncateGuts(), get_relation_info(), GetFdwRoutineByRelId(), and truncate_check_rel().

◆ GetForeignTable()

ForeignTable* GetForeignTable ( Oid  relid)

Definition at line 249 of file foreign.c.

250 {
251  Form_pg_foreign_table tableform;
252  ForeignTable *ft;
253  HeapTuple tp;
254  Datum datum;
255  bool isnull;
256 
258  if (!HeapTupleIsValid(tp))
259  elog(ERROR, "cache lookup failed for foreign table %u", relid);
260  tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
261 
262  ft = (ForeignTable *) palloc(sizeof(ForeignTable));
263  ft->relid = relid;
264  ft->serverid = tableform->ftserver;
265 
266  /* Extract the ftoptions */
268  tp,
269  Anum_pg_foreign_table_ftoptions,
270  &isnull);
271  if (isnull)
272  ft->options = NIL;
273  else
274  ft->options = untransformRelOptions(datum);
275 
276  ReleaseSysCache(tp);
277 
278  return ft;
279 }
Oid relid
Definition: foreign.h:55
List * options
Definition: foreign.h:57
Oid serverid
Definition: foreign.h:56

References 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(), create_foreign_modify(), deparseRelation(), fileGetOptions(), get_batch_size_option(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetForeignRelSize(), and postgresIsForeignRelUpdatable().

◆ GetUserMapping()

UserMapping* GetUserMapping ( Oid  userid,
Oid  serverid 
)

Definition at line 199 of file foreign.c.

200 {
201  Datum datum;
202  HeapTuple tp;
203  bool isnull;
204  UserMapping *um;
205 
206  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
207  ObjectIdGetDatum(userid),
208  ObjectIdGetDatum(serverid));
209 
210  if (!HeapTupleIsValid(tp))
211  {
212  /* Not found for the specific user -- try PUBLIC */
213  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
215  ObjectIdGetDatum(serverid));
216  }
217 
218  if (!HeapTupleIsValid(tp))
219  ereport(ERROR,
220  (errcode(ERRCODE_UNDEFINED_OBJECT),
221  errmsg("user mapping not found for \"%s\"",
222  MappingUserName(userid))));
223 
224  um = (UserMapping *) palloc(sizeof(UserMapping));
225  um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
226  um->userid = userid;
227  um->serverid = serverid;
228 
229  /* Extract the umoptions */
230  datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
231  tp,
232  Anum_pg_user_mapping_umoptions,
233  &isnull);
234  if (isnull)
235  um->options = NIL;
236  else
237  um->options = untransformRelOptions(datum);
238 
239  ReleaseSysCache(tp);
240 
241  return um;
242 }
#define MappingUserName(userid)
Definition: foreign.h:20
FormData_pg_user_mapping * Form_pg_user_mapping
#define InvalidOid
Definition: postgres_ext.h:36
Oid userid
Definition: foreign.h:48
Oid umid
Definition: foreign.h:47
Oid serverid
Definition: foreign.h:49
List * options
Definition: foreign.h:50

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

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

◆ is_conninfo_option()

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

Definition at line 586 of file foreign.c.

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

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

Referenced by postgresql_fdw_validator().

◆ IsImportableForeignTable()

bool IsImportableForeignTable ( const char *  tablename,
ImportForeignSchemaStmt stmt 
)

Definition at line 468 of file foreign.c.

470 {
471  ListCell *lc;
472 
473  switch (stmt->list_type)
474  {
476  return true;
477 
479  foreach(lc, stmt->table_list)
480  {
481  RangeVar *rv = (RangeVar *) lfirst(lc);
482 
483  if (strcmp(tablename, rv->relname) == 0)
484  return true;
485  }
486  return false;
487 
489  foreach(lc, stmt->table_list)
490  {
491  RangeVar *rv = (RangeVar *) lfirst(lc);
492 
493  if (strcmp(tablename, rv->relname) == 0)
494  return false;
495  }
496  return true;
497  }
498  return false; /* shouldn't get here */
499 }
@ FDW_IMPORT_SCHEMA_LIMIT_TO
Definition: parsenodes.h:2826
@ FDW_IMPORT_SCHEMA_ALL
Definition: parsenodes.h:2825
@ FDW_IMPORT_SCHEMA_EXCEPT
Definition: parsenodes.h:2827
ImportForeignSchemaType list_type
Definition: parsenodes.h:2836
char * relname
Definition: primnodes.h:68

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().

◆ pg_options_to_table()

Datum pg_options_to_table ( PG_FUNCTION_ARGS  )

Definition at line 508 of file foreign.c.

509 {
510  Datum array = PG_GETARG_DATUM(0);
511  ListCell *cell;
512  List *options;
513  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
514 
516  rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
517 
518  /* prepare the result set */
520 
521  foreach(cell, options)
522  {
523  DefElem *def = lfirst(cell);
524  Datum values[2];
525  bool nulls[2];
526 
528  nulls[0] = false;
529  if (def->arg)
530  {
531  values[1] = CStringGetTextDatum(strVal(def->arg));
532  nulls[1] = false;
533  }
534  else
535  {
536  values[1] = (Datum) 0;
537  nulls[1] = true;
538  }
539  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
540  values, nulls);
541  }
542 
543  return (Datum) 0;
544 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
void SetSingleFuncCall(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define SRF_SINGLE_USE_EXPECTED
Definition: funcapi.h:291
char * defname
Definition: parsenodes.h:765
Node * arg
Definition: parsenodes.h:766
TupleDesc setDesc
Definition: execnodes.h:317
Tuplestorestate * setResult
Definition: execnodes.h:316
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define strVal(v)
Definition: value.h:72

◆ postgresql_fdw_validator()

Datum postgresql_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 610 of file foreign.c.

611 {
612  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
613  Oid catalog = PG_GETARG_OID(1);
614 
615  ListCell *cell;
616 
617  foreach(cell, options_list)
618  {
619  DefElem *def = lfirst(cell);
620 
621  if (!is_conninfo_option(def->defname, catalog))
622  {
623  const struct ConnectionOption *opt;
625 
626  /*
627  * Unknown option specified, complain about it. Provide a hint
628  * with list of valid options for the object.
629  */
631  for (opt = libpq_conninfo_options; opt->optname; opt++)
632  if (catalog == opt->optcontext)
633  appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
634  opt->optname);
635 
636  ereport(ERROR,
637  (errcode(ERRCODE_SYNTAX_ERROR),
638  errmsg("invalid option \"%s\"", def->defname),
639  buf.len > 0
640  ? errhint("Valid options in this context are: %s",
641  buf.data)
642  : errhint("There are no valid options in this context.")));
643 
644  PG_RETURN_BOOL(false);
645  }
646  }
647 
648  PG_RETURN_BOOL(true);
649 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static bool is_conninfo_option(const char *option, Oid context)
Definition: foreign.c:586
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

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

Variable Documentation

◆ libpq_conninfo_options

const struct ConnectionOption libpq_conninfo_options[]
static
Initial value:
= {
{"authtype", ForeignServerRelationId},
{"service", ForeignServerRelationId},
{"user", UserMappingRelationId},
{"password", UserMappingRelationId},
{"connect_timeout", ForeignServerRelationId},
{"dbname", ForeignServerRelationId},
{"host", ForeignServerRelationId},
{"hostaddr", ForeignServerRelationId},
{"port", ForeignServerRelationId},
{"tty", ForeignServerRelationId},
{"options", ForeignServerRelationId},
{"requiressl", ForeignServerRelationId},
{"sslmode", ForeignServerRelationId},
{"gsslib", ForeignServerRelationId},
{NULL, InvalidOid}
}

Definition at line 508 of file foreign.c.

Referenced by is_conninfo_option(), and postgresql_fdw_validator().