PostgreSQL Source Code git master
Loading...
Searching...
No Matches
foreign.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for foreign.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ForeignDataWrapper
 
struct  ForeignServer
 
struct  UserMapping
 
struct  ForeignTable
 

Macros

#define MappingUserName(userid)    (OidIsValid(userid) ? GetUserNameFromId(userid, false) : "public")
 
#define FSV_MISSING_OK   0x01
 
#define FDW_MISSING_OK   0x01
 

Typedefs

typedef struct ForeignDataWrapper ForeignDataWrapper
 
typedef struct ForeignServer ForeignServer
 
typedef struct UserMapping UserMapping
 
typedef struct ForeignTable ForeignTable
 

Functions

ForeignServerGetForeignServer (Oid serverid)
 
charForeignServerName (Oid serverid)
 
ForeignServerGetForeignServerExtended (Oid serverid, bits16 flags)
 
ForeignServerGetForeignServerByName (const char *srvname, bool missing_ok)
 
charForeignServerConnectionString (Oid userid, Oid serverid)
 
UserMappingGetUserMapping (Oid userid, Oid serverid)
 
ForeignDataWrapperGetForeignDataWrapper (Oid fdwid)
 
ForeignDataWrapperGetForeignDataWrapperExtended (Oid fdwid, bits16 flags)
 
ForeignDataWrapperGetForeignDataWrapperByName (const char *fdwname, bool missing_ok)
 
ForeignTableGetForeignTable (Oid relid)
 
ListGetForeignColumnOptions (Oid relid, AttrNumber attnum)
 
Oid get_foreign_data_wrapper_oid (const char *fdwname, bool missing_ok)
 
Oid get_foreign_server_oid (const char *servername, bool missing_ok)
 

Macro Definition Documentation

◆ FDW_MISSING_OK

#define FDW_MISSING_OK   0x01

Definition at line 65 of file foreign.h.

◆ FSV_MISSING_OK

#define FSV_MISSING_OK   0x01

Definition at line 62 of file foreign.h.

◆ MappingUserName

#define MappingUserName (   userid)     (OidIsValid(userid) ? GetUserNameFromId(userid, false) : "public")

Definition at line 20 of file foreign.h.

21 : "public")

Typedef Documentation

◆ ForeignDataWrapper

◆ ForeignServer

◆ ForeignTable

◆ UserMapping

Function Documentation

◆ ForeignServerConnectionString()

char * ForeignServerConnectionString ( Oid  userid,
Oid  serverid 
)
extern

Definition at line 224 of file foreign.c.

225{
226 MemoryContext tempContext;
228 text *volatile connection_text = NULL;
229 char *result = NULL;
230
231 /*
232 * GetForeignServer, GetForeignDataWrapper, and the connection function
233 * itself all leak memory into CurrentMemoryContext. Switch to a temporary
234 * context for easy cleanup.
235 */
237 "FDWConnectionContext",
239
240 oldcxt = MemoryContextSwitchTo(tempContext);
241
242 PG_TRY();
243 {
244 ForeignServer *server;
247
248 server = GetForeignServer(serverid);
250
251 if (!OidIsValid(fdw->fdwconnection))
253 (errmsg("foreign data wrapper \"%s\" does not support subscription connections",
254 fdw->fdwname),
255 errdetail("Foreign data wrapper must be defined with CONNECTION specified.")));
256
257
258 connection_datum = OidFunctionCall3(fdw->fdwconnection,
259 ObjectIdGetDatum(userid),
260 ObjectIdGetDatum(serverid),
262
264 }
265 PG_FINALLY();
266 {
268
269 if (connection_text)
271
272 MemoryContextDelete(tempContext);
273 }
274 PG_END_TRY();
275
276 return result;
277}
#define OidIsValid(objectId)
Definition c.h:830
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_FINALLY(...)
Definition elog.h:389
#define ereport(elevel,...)
Definition elog.h:150
#define DatumGetTextPP(X)
Definition fmgr.h:293
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition fmgr.h:726
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition foreign.c:38
ForeignServer * GetForeignServer(Oid serverid)
Definition foreign.c:113
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
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)
Definition c.h:748
char * text_to_cstring(const text *t)
Definition varlena.c:215

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, CurrentMemoryContext, DatumGetTextPP, ereport, errdetail(), errmsg, ERROR, fb(), ForeignServer::fdwid, GetForeignDataWrapper(), GetForeignServer(), MemoryContextDelete(), MemoryContextSwitchTo(), ObjectIdGetDatum(), OidFunctionCall3, OidIsValid, PG_END_TRY, PG_FINALLY, PG_TRY, PointerGetDatum(), and text_to_cstring().

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

◆ ForeignServerName()

char * ForeignServerName ( Oid  serverid)
extern

Definition at line 184 of file foreign.c.

185{
187 char *servername;
188 HeapTuple tp;
189
191
192 if (!HeapTupleIsValid(tp))
193 elog(ERROR, "cache lookup failed for foreign server %u", serverid);
194
196
197 servername = pstrdup(NameStr(serverform->srvname));
198
199 ReleaseSysCache(tp);
200
201 return servername;
202}
#define NameStr(name)
Definition c.h:807
#define elog(elevel,...)
Definition elog.h:226
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
char * pstrdup(const char *in)
Definition mcxt.c:1781
END_CATALOG_STRUCT typedef FormData_pg_foreign_server * Form_pg_foreign_server
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220

References elog, ERROR, fb(), Form_pg_foreign_server, GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), pstrdup(), ReleaseSysCache(), and SearchSysCache1().

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

◆ get_foreign_data_wrapper_oid()

Oid get_foreign_data_wrapper_oid ( const char fdwname,
bool  missing_ok 
)
extern

Definition at line 768 of file foreign.c.

769{
770 Oid oid;
771
774 CStringGetDatum(fdwname));
775 if (!OidIsValid(oid) && !missing_ok)
778 errmsg("foreign-data wrapper \"%s\" does not exist",
779 fdwname)));
780 return oid;
781}
int errcode(int sqlerrcode)
Definition elog.c:874
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 
)
extern

Definition at line 791 of file foreign.c.

792{
793 Oid oid;
794
796 CStringGetDatum(servername));
797 if (!OidIsValid(oid) && !missing_ok)
800 errmsg("server \"%s\" does not exist", servername)));
801 return oid;
802}

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

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

◆ GetForeignColumnOptions()

List * GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)
extern

Definition at line 379 of file foreign.c.

380{
381 List *options;
382 HeapTuple tp;
383 Datum datum;
384 bool isnull;
385
387 ObjectIdGetDatum(relid),
389 if (!HeapTupleIsValid(tp))
390 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
391 attnum, relid);
392 datum = SysCacheGetAttr(ATTNUM,
393 tp,
395 &isnull);
396 if (isnull)
397 options = NIL;
398 else
400
401 ReleaseSysCache(tp);
402
403 return options;
404}
int16 attnum
#define NIL
Definition pg_list.h:68
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:230
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595

References attnum, elog, ERROR, fb(), HeapTupleIsValid, Int16GetDatum(), NIL, ObjectIdGetDatum(), 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 
)
extern

Definition at line 98 of file foreign.c.

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

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper * GetForeignDataWrapperExtended ( Oid  fdwid,
bits16  flags 
)
extern

Definition at line 50 of file foreign.c.

51{
54 Datum datum;
55 HeapTuple tp;
56 bool isnull;
57
59
60 if (!HeapTupleIsValid(tp))
61 {
62 if ((flags & FDW_MISSING_OK) == 0)
63 elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
64 return NULL;
65 }
66
68
70 fdw->fdwid = fdwid;
71 fdw->owner = fdwform->fdwowner;
72 fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
73 fdw->fdwhandler = fdwform->fdwhandler;
74 fdw->fdwvalidator = fdwform->fdwvalidator;
75 fdw->fdwconnection = fdwform->fdwconnection;
76
77 /* Extract the fdwoptions */
79 tp,
81 &isnull);
82 if (isnull)
83 fdw->options = NIL;
84 else
85 fdw->options = untransformRelOptions(datum);
86
88
89 return fdw;
90}
#define palloc_object(type)
Definition fe_memutils.h:74
#define FDW_MISSING_OK
Definition foreign.h:65
END_CATALOG_STRUCT typedef FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper

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 
)
extern

Definition at line 209 of file foreign.c.

210{
211 Oid serverid = get_foreign_server_oid(srvname, missing_ok);
212
213 if (!OidIsValid(serverid))
214 return NULL;
215
216 return GetForeignServer(serverid);
217}
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition foreign.c:791

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,
bits16  flags 
)
extern

Definition at line 125 of file foreign.c.

126{
128 ForeignServer *server;
129 HeapTuple tp;
130 Datum datum;
131 bool isnull;
132
134
135 if (!HeapTupleIsValid(tp))
136 {
137 if ((flags & FSV_MISSING_OK) == 0)
138 elog(ERROR, "cache lookup failed for foreign server %u", serverid);
139 return NULL;
140 }
141
143
145 server->serverid = serverid;
146 server->servername = pstrdup(NameStr(serverform->srvname));
147 server->owner = serverform->srvowner;
148 server->fdwid = serverform->srvfdw;
149
150 /* Extract server type */
152 tp,
154 &isnull);
155 server->servertype = isnull ? NULL : TextDatumGetCString(datum);
156
157 /* Extract server version */
159 tp,
161 &isnull);
162 server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
163
164 /* Extract the srvoptions */
166 tp,
168 &isnull);
169 if (isnull)
170 server->options = NIL;
171 else
172 server->options = untransformRelOptions(datum);
173
174 ReleaseSysCache(tp);
175
176 return server;
177}
#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().

◆ GetForeignTable()

ForeignTable * GetForeignTable ( Oid  relid)
extern

Definition at line 341 of file foreign.c.

342{
345 HeapTuple tp;
346 Datum datum;
347 bool isnull;
348
350 if (!HeapTupleIsValid(tp))
351 elog(ERROR, "cache lookup failed for foreign table %u", relid);
353
355 ft->relid = relid;
356 ft->serverid = tableform->ftserver;
357
358 /* Extract the ftoptions */
360 tp,
362 &isnull);
363 if (isnull)
364 ft->options = NIL;
365 else
366 ft->options = untransformRelOptions(datum);
367
368 ReleaseSysCache(tp);
369
370 return ft;
371}
END_CATALOG_STRUCT typedef FormData_pg_foreign_table * Form_pg_foreign_table

References elog, ERROR, fb(), Form_pg_foreign_table, GETSTRUCT(), HeapTupleIsValid, NIL, ObjectIdGetDatum(), palloc_object, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and untransformRelOptions().

Referenced by check_selective_binary_conversion(), create_foreign_modify(), deparseRelation(), fileGetOptions(), get_batch_size_option(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresGetForeignRelSize(), and postgresIsForeignRelUpdatable().

◆ GetUserMapping()

UserMapping * GetUserMapping ( Oid  userid,
Oid  serverid 
)
extern

Definition at line 287 of file foreign.c.

288{
289 Datum datum;
290 HeapTuple tp;
291 bool isnull;
293
295 ObjectIdGetDatum(userid),
296 ObjectIdGetDatum(serverid));
297
298 if (!HeapTupleIsValid(tp))
299 {
300 /* Not found for the specific user -- try PUBLIC */
303 ObjectIdGetDatum(serverid));
304 }
305
306 if (!HeapTupleIsValid(tp))
307 {
308 ForeignServer *server = GetForeignServer(serverid);
309
312 errmsg("user mapping not found for user \"%s\", server \"%s\"",
313 MappingUserName(userid), server->servername)));
314 }
315
317 um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
318 um->userid = userid;
319 um->serverid = serverid;
320
321 /* Extract the umoptions */
323 tp,
325 &isnull);
326 if (isnull)
327 um->options = NIL;
328 else
329 um->options = untransformRelOptions(datum);
330
331 ReleaseSysCache(tp);
332
333 return um;
334}
#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(), get_connect_string(), postgres_fdw_connection(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresGetForeignRelSize(), and postgresImportForeignSchema().