PostgreSQL Source Code git master
Loading...
Searching...
No Matches
foreign.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "optimizer/paths.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tuplestore.h"
#include "utils/varlena.h"
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, uint16 flags)
 
ForeignDataWrapperGetForeignDataWrapperByName (const char *fdwname, bool missing_ok)
 
ForeignServerGetForeignServer (Oid serverid)
 
ForeignServerGetForeignServerExtended (Oid serverid, uint16 flags)
 
ForeignServerGetForeignServerByName (const char *srvname, bool missing_ok)
 
charForeignServerConnectionString (Oid userid, ForeignServer *server)
 
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

◆ ForeignServerConnectionString()

char * ForeignServerConnectionString ( Oid  userid,
ForeignServer server 
)

Definition at line 202 of file foreign.c.

203{
206
208
209 if (!OidIsValid(fdw->fdwconnection))
212 errmsg("foreign data wrapper \"%s\" does not support subscription connections",
213 fdw->fdwname),
214 errdetail("Foreign data wrapper must be defined with CONNECTION specified.")));
215
216 connection_datum = OidFunctionCall3(fdw->fdwconnection,
217 ObjectIdGetDatum(userid),
218 ObjectIdGetDatum(server->serverid),
220
222}
#define OidIsValid(objectId)
Definition c.h:858
int errcode(int sqlerrcode)
Definition elog.c:874
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define DatumGetTextPP(X)
Definition fmgr.h:293
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition fmgr.h:726
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition foreign.c:39
static char * errmsg
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
char * text_to_cstring(const text *t)
Definition varlena.c:217

References DatumGetTextPP, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), ForeignServer::fdwid, GetForeignDataWrapper(), ObjectIdGetDatum(), OidFunctionCall3, OidIsValid, PointerGetDatum(), ForeignServer::serverid, and text_to_cstring().

Referenced by AlterSubscription(), CreateSubscription(), DropSubscription(), and GetSubscription().

◆ get_foreign_data_wrapper_oid()

Oid get_foreign_data_wrapper_oid ( const char fdwname,
bool  missing_ok 
)

Definition at line 713 of file foreign.c.

714{
715 Oid oid;
716
719 CStringGetDatum(fdwname));
720 if (!OidIsValid(oid) && !missing_ok)
723 errmsg("foreign-data wrapper \"%s\" does not exist",
724 fdwname)));
725 return oid;
726}
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
unsigned int Oid
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition syscache.h:109

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

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

◆ get_foreign_server_oid()

Oid get_foreign_server_oid ( const char servername,
bool  missing_ok 
)

Definition at line 736 of file foreign.c.

737{
738 Oid oid;
739
741 CStringGetDatum(servername));
742 if (!OidIsValid(oid) && !missing_ok)
745 errmsg("server \"%s\" does not exist", servername)));
746 return oid;
747}

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

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

◆ GetExistingLocalJoinPath()

Path * GetExistingLocalJoinPath ( RelOptInfo joinrel)

Definition at line 773 of file foreign.c.

774{
775 ListCell *lc;
776
777 Assert(IS_JOIN_REL(joinrel));
778
779 foreach(lc, joinrel->pathlist)
780 {
781 Path *path = (Path *) lfirst(lc);
783
784 /* Skip parameterized paths. */
785 if (path->param_info != NULL)
786 continue;
787
788 switch (path->pathtype)
789 {
790 case T_HashJoin:
791 {
793
794 memcpy(hash_path, path, sizeof(HashPath));
796 }
797 break;
798
799 case T_NestLoop:
800 {
802
803 memcpy(nest_path, path, sizeof(NestPath));
805 }
806 break;
807
808 case T_MergeJoin:
809 {
811
812 memcpy(merge_path, path, sizeof(MergePath));
814 }
815 break;
816
817 default:
818
819 /*
820 * Just skip anything else. We don't know if corresponding
821 * plan would build the output row from whole-row references
822 * of base relations and execute the EPQ checks.
823 */
824 break;
825 }
826
827 /* This path isn't good for us, check next. */
828 if (!joinpath)
829 continue;
830
831 /*
832 * If either inner or outer path is a ForeignPath corresponding to a
833 * pushed down join, replace it with the fdw_outerpath, so that we
834 * maintain path for EPQ checks built entirely of local join
835 * strategies.
836 */
837 if (IsA(joinpath->outerjoinpath, ForeignPath))
838 {
840
841 foreign_path = (ForeignPath *) joinpath->outerjoinpath;
842 if (IS_JOIN_REL(foreign_path->path.parent))
843 {
844 joinpath->outerjoinpath = foreign_path->fdw_outerpath;
845
846 if (joinpath->path.pathtype == T_MergeJoin)
847 {
849
850 /*
851 * If the new outer path is already well enough ordered
852 * for the mergejoin, we can skip doing an explicit sort.
853 */
854 if (merge_path->outersortkeys &&
856 joinpath->outerjoinpath->pathkeys,
857 &merge_path->outer_presorted_keys))
859 }
860 }
861 }
862
863 if (IsA(joinpath->innerjoinpath, ForeignPath))
864 {
866
867 foreign_path = (ForeignPath *) joinpath->innerjoinpath;
868 if (IS_JOIN_REL(foreign_path->path.parent))
869 {
870 joinpath->innerjoinpath = foreign_path->fdw_outerpath;
871
872 if (joinpath->path.pathtype == T_MergeJoin)
873 {
875
876 /*
877 * If the new inner path is already well enough ordered
878 * for the mergejoin, we can skip doing an explicit sort.
879 */
880 if (merge_path->innersortkeys &&
881 pathkeys_contained_in(merge_path->innersortkeys,
882 joinpath->innerjoinpath->pathkeys))
884 }
885 }
886 }
887
888 return (Path *) joinpath;
889 }
890 return NULL;
891}
#define Assert(condition)
Definition c.h:943
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define makeNode(_type_)
Definition nodes.h:161
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition pathkeys.c:558
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition pathkeys.c:343
#define IS_JOIN_REL(rel)
Definition pathnodes.h:994
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
List * outersortkeys
Definition pathnodes.h:2470
List * innersortkeys
Definition pathnodes.h:2471
NodeTag pathtype
Definition pathnodes.h:1971
List * pathlist
Definition pathnodes.h:1050

References Assert, fb(), MergePath::innersortkeys, IS_JOIN_REL, IsA, lfirst, makeNode, memcpy(), NIL, MergePath::outersortkeys, pathkeys_contained_in(), pathkeys_count_contained_in(), RelOptInfo::pathlist, and Path::pathtype.

Referenced by postgresGetForeignJoinPaths().

◆ GetFdwRoutine()

FdwRoutine * GetFdwRoutine ( Oid  fdwhandler)

Definition at line 357 of file foreign.c.

358{
359 Datum datum;
360 FdwRoutine *routine;
361
362 /* Check if the access to foreign tables is restricted */
364 {
365 /* there must not be built-in FDW handler */
368 errmsg("access to non-system foreign table is restricted")));
369 }
370
371 datum = OidFunctionCall0(fdwhandler);
372 routine = (FdwRoutine *) DatumGetPointer(datum);
373
374 if (routine == NULL || !IsA(routine, FdwRoutine))
375 elog(ERROR, "foreign-data wrapper handler function %u did not return an FdwRoutine struct",
376 fdwhandler);
377
378 return routine;
379}
#define unlikely(x)
Definition c.h:438
#define elog(elevel,...)
Definition elog.h:228
#define OidFunctionCall0(functionId)
Definition fmgr.h:720
int restrict_nonsystem_relation_kind
Definition postgres.c:111
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
#define RESTRICT_RELKIND_FOREIGN_TABLE
Definition tcopprot.h:45

References DatumGetPointer(), elog, ereport, errcode(), errmsg, ERROR, fb(), IsA, OidFunctionCall0, restrict_nonsystem_relation_kind, RESTRICT_RELKIND_FOREIGN_TABLE, and unlikely.

Referenced by GetFdwRoutineByServerId(), and ImportForeignSchema().

◆ GetFdwRoutineByRelId()

FdwRoutine * GetFdwRoutineByRelId ( Oid  relid)

Definition at line 451 of file foreign.c.

452{
453 Oid serverid;
454
455 /* Get server OID for the foreign table. */
456 serverid = GetForeignServerIdByRelId(relid);
457
458 /* Now retrieve server's FdwRoutine struct. */
459 return GetFdwRoutineByServerId(serverid);
460}
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition foreign.c:409
Oid GetForeignServerIdByRelId(Oid relid)
Definition foreign.c:387

References GetFdwRoutineByServerId(), and GetForeignServerIdByRelId().

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

◆ GetFdwRoutineByServerId()

FdwRoutine * GetFdwRoutineByServerId ( Oid  serverid)

Definition at line 409 of file foreign.c.

410{
411 HeapTuple tp;
414 Oid fdwid;
415 Oid fdwhandler;
416
417 /* Get foreign-data wrapper OID for the server. */
419 if (!HeapTupleIsValid(tp))
420 elog(ERROR, "cache lookup failed for foreign server %u", serverid);
422 fdwid = serverform->srvfdw;
423 ReleaseSysCache(tp);
424
425 /* Get handler function OID for the FDW. */
427 if (!HeapTupleIsValid(tp))
428 elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
430 fdwhandler = fdwform->fdwhandler;
431
432 /* Complain if FDW has been set to NO HANDLER. */
433 if (!OidIsValid(fdwhandler))
436 errmsg("foreign-data wrapper \"%s\" has no handler",
437 NameStr(fdwform->fdwname))));
438
439 ReleaseSysCache(tp);
440
441 /* And finally, call the handler function. */
442 return GetFdwRoutine(fdwhandler);
443}
#define NameStr(name)
Definition c.h:835
FdwRoutine * GetFdwRoutine(Oid fdwhandler)
Definition foreign.c:357
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
END_CATALOG_STRUCT typedef FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
END_CATALOG_STRUCT typedef FormData_pg_foreign_server * Form_pg_foreign_server
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221

References elog, ereport, errcode(), errmsg, ERROR, fb(), Form_pg_foreign_data_wrapper, Form_pg_foreign_server, 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 474 of file foreign.c.

475{
476 FdwRoutine *fdwroutine;
478
479 if (relation->rd_fdwroutine == NULL)
480 {
481 /* Get the info by consulting the catalogs and the FDW code */
482 fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation));
483
484 /* Save the data for later reuse in CacheMemoryContext */
486 sizeof(FdwRoutine));
487 memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine));
488 relation->rd_fdwroutine = cfdwroutine;
489
490 /* Give back the locally palloc'd copy regardless of makecopy */
491 return fdwroutine;
492 }
493
494 /* We have valid cached data --- does the caller want a copy? */
495 if (makecopy)
496 {
497 fdwroutine = palloc_object(FdwRoutine);
498 memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine));
499 return fdwroutine;
500 }
501
502 /* Only a short-lived reference is needed, so just hand back cached copy */
503 return relation->rd_fdwroutine;
504}
#define palloc_object(type)
Definition fe_memutils.h:74
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition foreign.c:451
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext CacheMemoryContext
Definition mcxt.c:169
#define RelationGetRelid(relation)
Definition rel.h:516
struct FdwRoutine * rd_fdwroutine
Definition rel.h:240

References CacheMemoryContext, fb(), GetFdwRoutineByRelId(), memcpy(), MemoryContextAlloc(), palloc_object, 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 324 of file foreign.c.

325{
326 List *options;
327 HeapTuple tp;
328 Datum datum;
329 bool isnull;
330
332 ObjectIdGetDatum(relid),
334 if (!HeapTupleIsValid(tp))
335 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
336 attnum, relid);
337 datum = SysCacheGetAttr(ATTNUM,
338 tp,
340 &isnull);
341 if (isnull)
342 options = NIL;
343 else
345
346 ReleaseSysCache(tp);
347
348 return options;
349}
int16 attnum
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
List * untransformRelOptions(Datum options)
Definition pg_list.h:54
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:231
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596

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

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

◆ GetForeignDataWrapper()

◆ GetForeignDataWrapperByName()

ForeignDataWrapper * GetForeignDataWrapperByName ( const char fdwname,
bool  missing_ok 
)

Definition at line 99 of file foreign.c.

100{
101 Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok);
102
103 if (!OidIsValid(fdwId))
104 return NULL;
105
107}
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition foreign.c:713

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper * GetForeignDataWrapperExtended ( Oid  fdwid,
uint16  flags 
)

Definition at line 51 of file foreign.c.

52{
55 Datum datum;
56 HeapTuple tp;
57 bool isnull;
58
60
61 if (!HeapTupleIsValid(tp))
62 {
63 if ((flags & FDW_MISSING_OK) == 0)
64 elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
65 return NULL;
66 }
67
69
71 fdw->fdwid = fdwid;
72 fdw->owner = fdwform->fdwowner;
73 fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
74 fdw->fdwhandler = fdwform->fdwhandler;
75 fdw->fdwvalidator = fdwform->fdwvalidator;
76 fdw->fdwconnection = fdwform->fdwconnection;
77
78 /* Extract the fdwoptions */
80 tp,
82 &isnull);
83 if (isnull)
84 fdw->options = NIL;
85 else
86 fdw->options = untransformRelOptions(datum);
87
89
90 return fdw;
91}
#define FDW_MISSING_OK
Definition foreign.h:65
char * pstrdup(const char *in)
Definition mcxt.c:1781

References elog, ERROR, fb(), FDW_MISSING_OK, Form_pg_foreign_data_wrapper, GETSTRUCT(), HeapTupleIsValid, NameStr, NIL, ObjectIdGetDatum(), palloc_object, 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 185 of file foreign.c.

186{
187 Oid serverid = get_foreign_server_oid(srvname, missing_ok);
188
189 if (!OidIsValid(serverid))
190 return NULL;
191
192 return GetForeignServer(serverid);
193}
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition foreign.c:736
ForeignServer * GetForeignServer(Oid serverid)
Definition foreign.c:114

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

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

◆ GetForeignServerExtended()

ForeignServer * GetForeignServerExtended ( Oid  serverid,
uint16  flags 
)

Definition at line 126 of file foreign.c.

127{
129 ForeignServer *server;
130 HeapTuple tp;
131 Datum datum;
132 bool isnull;
133
135
136 if (!HeapTupleIsValid(tp))
137 {
138 if ((flags & FSV_MISSING_OK) == 0)
139 elog(ERROR, "cache lookup failed for foreign server %u", serverid);
140 return NULL;
141 }
142
144
146 server->serverid = serverid;
147 server->servername = pstrdup(NameStr(serverform->srvname));
148 server->owner = serverform->srvowner;
149 server->fdwid = serverform->srvfdw;
150
151 /* Extract server type */
153 tp,
155 &isnull);
156 server->servertype = isnull ? NULL : TextDatumGetCString(datum);
157
158 /* Extract server version */
160 tp,
162 &isnull);
163 server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
164
165 /* Extract the srvoptions */
167 tp,
169 &isnull);
170 if (isnull)
171 server->options = NIL;
172 else
173 server->options = untransformRelOptions(datum);
174
175 ReleaseSysCache(tp);
176
177 return server;
178}
#define TextDatumGetCString(d)
Definition builtins.h:99
#define FSV_MISSING_OK
Definition foreign.h:62
List * options
Definition foreign.h:43
char * serverversion
Definition foreign.h:42
char * servername
Definition foreign.h:40
char * servertype
Definition foreign.h:41

References elog, ERROR, fb(), ForeignServer::fdwid, Form_pg_foreign_server, FSV_MISSING_OK, GETSTRUCT(), HeapTupleIsValid, NameStr, NIL, ObjectIdGetDatum(), ForeignServer::options, ForeignServer::owner, palloc_object, 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_internal().

◆ GetForeignServerIdByRelId()

Oid GetForeignServerIdByRelId ( Oid  relid)

Definition at line 387 of file foreign.c.

388{
389 HeapTuple tp;
391 Oid serverid;
392
394 if (!HeapTupleIsValid(tp))
395 elog(ERROR, "cache lookup failed for foreign table %u", relid);
397 serverid = tableform->ftserver;
398 ReleaseSysCache(tp);
399
400 return serverid;
401}
END_CATALOG_STRUCT typedef FormData_pg_foreign_table * Form_pg_foreign_table

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

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

◆ GetForeignTable()

ForeignTable * GetForeignTable ( Oid  relid)

◆ GetUserMapping()

UserMapping * GetUserMapping ( Oid  userid,
Oid  serverid 
)

Definition at line 232 of file foreign.c.

233{
234 Datum datum;
235 HeapTuple tp;
236 bool isnull;
238
240 ObjectIdGetDatum(userid),
241 ObjectIdGetDatum(serverid));
242
243 if (!HeapTupleIsValid(tp))
244 {
245 /* Not found for the specific user -- try PUBLIC */
248 ObjectIdGetDatum(serverid));
249 }
250
251 if (!HeapTupleIsValid(tp))
252 {
253 ForeignServer *server = GetForeignServer(serverid);
254
257 errmsg("user mapping not found for user \"%s\", server \"%s\"",
258 MappingUserName(userid), server->servername)));
259 }
260
262 um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
263 um->userid = userid;
264 um->serverid = serverid;
265
266 /* Extract the umoptions */
268 tp,
270 &isnull);
271 if (isnull)
272 um->options = NIL;
273 else
274 um->options = untransformRelOptions(datum);
275
276 ReleaseSysCache(tp);
277
278 return um;
279}
#define MappingUserName(userid)
Definition foreign.h:20
END_CATALOG_STRUCT typedef FormData_pg_user_mapping * Form_pg_user_mapping
#define InvalidOid

References ereport, errcode(), errmsg, ERROR, fb(), Form_pg_user_mapping, GetForeignServer(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, MappingUserName, NIL, ObjectIdGetDatum(), palloc_object, ReleaseSysCache(), SearchSysCache2(), ForeignServer::servername, SysCacheGetAttr(), and untransformRelOptions().

Referenced by AlterSubscription(), AlterSubscriptionOwner_internal(), create_foreign_modify(), CreateSubscription(), fetch_remote_statistics(), get_connect_string(), postgres_fdw_connection(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresGetForeignRelSize(), and postgresImportForeignSchema().

◆ is_conninfo_option()

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

Definition at line 633 of file foreign.c.

634{
635 const struct ConnectionOption *opt;
636
637 for (opt = libpq_conninfo_options; opt->optname; opt++)
638 if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
639 return true;
640 return false;
641}
static const struct ConnectionOption libpq_conninfo_options[]
Definition foreign.c:607
const char * optname
Definition foreign.c:598

References fb(), libpq_conninfo_options, ConnectionOption::optcontext, and ConnectionOption::optname.

Referenced by postgresql_fdw_validator().

◆ IsImportableForeignTable()

bool IsImportableForeignTable ( const char tablename,
ImportForeignSchemaStmt stmt 
)

Definition at line 514 of file foreign.c.

516{
517 ListCell *lc;
518
519 switch (stmt->list_type)
520 {
522 return true;
523
525 foreach(lc, stmt->table_list)
526 {
527 RangeVar *rv = (RangeVar *) lfirst(lc);
528
529 if (strcmp(tablename, rv->relname) == 0)
530 return true;
531 }
532 return false;
533
535 foreach(lc, stmt->table_list)
536 {
537 RangeVar *rv = (RangeVar *) lfirst(lc);
538
539 if (strcmp(tablename, rv->relname) == 0)
540 return false;
541 }
542 return true;
543 }
544 return false; /* shouldn't get here */
545}
#define stmt
@ FDW_IMPORT_SCHEMA_LIMIT_TO
@ FDW_IMPORT_SCHEMA_ALL
@ FDW_IMPORT_SCHEMA_EXCEPT
char * relname
Definition primnodes.h:84

References fb(), FDW_IMPORT_SCHEMA_ALL, FDW_IMPORT_SCHEMA_EXCEPT, FDW_IMPORT_SCHEMA_LIMIT_TO, lfirst, RangeVar::relname, and stmt.

Referenced by ImportForeignSchema().

◆ pg_options_to_table()

Datum pg_options_to_table ( PG_FUNCTION_ARGS  )

Definition at line 554 of file foreign.c.

555{
556 Datum array = PG_GETARG_DATUM(0);
557 ListCell *cell;
558 List *options;
560
562 rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
563
564 /* prepare the result set */
566
567 foreach(cell, options)
568 {
569 DefElem *def = lfirst(cell);
570 Datum values[2];
571 bool nulls[2];
572
574 nulls[0] = false;
575 if (def->arg)
576 {
578 nulls[1] = false;
579 }
580 else
581 {
582 values[1] = (Datum) 0;
583 nulls[1] = true;
584 }
585 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
586 values, nulls);
587 }
588
589 return (Datum) 0;
590}
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
void InitMaterializedSRF(FunctionCallInfo fcinfo, uint32 flags)
Definition funcapi.c:76
#define MAT_SRF_USE_EXPECTED_DESC
Definition funcapi.h:296
char * defname
Definition parsenodes.h:860
Node * arg
Definition parsenodes.h:861
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition tuplestore.c:785
#define strVal(v)
Definition value.h:82

References DefElem::arg, CStringGetTextDatum, DefElem::defname, fb(), InitMaterializedSRF(), lfirst, MAT_SRF_USE_EXPECTED_DESC, PG_GETARG_DATUM, strVal, tuplestore_putvalues(), untransformRelOptions(), and values.

◆ postgresql_fdw_validator()

Datum postgresql_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 657 of file foreign.c.

658{
661
662 ListCell *cell;
663
664 foreach(cell, options_list)
665 {
666 DefElem *def = lfirst(cell);
667
669 {
670 const struct ConnectionOption *opt;
671 const char *closest_match;
673 bool has_valid_options = false;
674
675 /*
676 * Unknown option specified, complain about it. Provide a hint
677 * with a valid option that looks similar, if there is one.
678 */
680 for (opt = libpq_conninfo_options; opt->optname; opt++)
681 {
682 if (catalog == opt->optcontext)
683 {
684 has_valid_options = true;
686 }
687 }
688
692 errmsg("invalid option \"%s\"", def->defname),
694 errhint("Perhaps you meant the option \"%s\".",
695 closest_match) : 0 :
696 errhint("There are no valid options in this context.")));
697
698 PG_RETURN_BOOL(false);
699 }
700 }
701
702 PG_RETURN_BOOL(true);
703}
int errhint(const char *fmt,...) pg_attribute_printf(1
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
static bool is_conninfo_option(const char *option, Oid context)
Definition foreign.c:633
struct parser_state match_state[5]
const char * getClosestMatch(ClosestMatchState *state)
Definition varlena.c:5386
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition varlena.c:5331
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition varlena.c:5351

References DefElem::defname, ereport, errcode(), errhint(), errmsg, ERROR, fb(), getClosestMatch(), initClosestMatch(), is_conninfo_option(), lfirst, libpq_conninfo_options, match_state, ConnectionOption::optcontext, ConnectionOption::optname, PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_BOOL, untransformRelOptions(), and updateClosestMatch().

Variable Documentation

◆ libpq_conninfo_options

const struct ConnectionOption libpq_conninfo_options[]
static
Initial value:
= {
{"authtype", ForeignServerRelationId},
{"password", UserMappingRelationId},
{"connect_timeout", ForeignServerRelationId},
{"hostaddr", ForeignServerRelationId},
{"requiressl", ForeignServerRelationId},
{"gssdelegation", ForeignServerRelationId},
}

Definition at line 607 of file foreign.c.

607 {
608 {"authtype", ForeignServerRelationId},
609 {"service", ForeignServerRelationId},
610 {"user", UserMappingRelationId},
611 {"password", UserMappingRelationId},
612 {"connect_timeout", ForeignServerRelationId},
613 {"dbname", ForeignServerRelationId},
614 {"host", ForeignServerRelationId},
615 {"hostaddr", ForeignServerRelationId},
616 {"port", ForeignServerRelationId},
618 {"options", ForeignServerRelationId},
619 {"requiressl", ForeignServerRelationId},
620 {"sslmode", ForeignServerRelationId},
621 {"gsslib", ForeignServerRelationId},
622 {"gssdelegation", ForeignServerRelationId},
624};

Referenced by is_conninfo_option(), and postgresql_fdw_validator().