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 225 of file foreign.c.

226{
227 MemoryContext tempContext;
229 text *volatile connection_text = NULL;
230 char *result = NULL;
231
232 /*
233 * GetForeignServer, GetForeignDataWrapper, and the connection function
234 * itself all leak memory into CurrentMemoryContext. Switch to a temporary
235 * context for easy cleanup.
236 */
238 "FDWConnectionContext",
240
241 oldcxt = MemoryContextSwitchTo(tempContext);
242
243 PG_TRY();
244 {
245 ForeignServer *server;
248
249 server = GetForeignServer(serverid);
251
252 if (!OidIsValid(fdw->fdwconnection))
255 errmsg("foreign data wrapper \"%s\" does not support subscription connections",
256 fdw->fdwname),
257 errdetail("Foreign data wrapper must be defined with CONNECTION specified.")));
258
259
260 connection_datum = OidFunctionCall3(fdw->fdwconnection,
261 ObjectIdGetDatum(userid),
262 ObjectIdGetDatum(serverid),
264
266 }
267 PG_FINALLY();
268 {
270
271 if (connection_text)
273
274 MemoryContextDelete(tempContext);
275 }
276 PG_END_TRY();
277
278 return result;
279}
#define OidIsValid(objectId)
Definition c.h:860
int errcode(int sqlerrcode)
Definition elog.c:874
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:39
ForeignServer * GetForeignServer(Oid serverid)
Definition foreign.c:114
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:778
char * text_to_cstring(const text *t)
Definition varlena.c:217

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, CurrentMemoryContext, DatumGetTextPP, ereport, errcode(), 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 185 of file foreign.c.

186{
188 char *servername;
189 HeapTuple tp;
190
192
193 if (!HeapTupleIsValid(tp))
194 elog(ERROR, "cache lookup failed for foreign server %u", serverid);
195
197
198 servername = pstrdup(NameStr(serverform->srvname));
199
200 ReleaseSysCache(tp);
201
202 return servername;
203}
#define NameStr(name)
Definition c.h:837
#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 770 of file foreign.c.

771{
772 Oid oid;
773
776 CStringGetDatum(fdwname));
777 if (!OidIsValid(oid) && !missing_ok)
780 errmsg("foreign-data wrapper \"%s\" does not exist",
781 fdwname)));
782 return oid;
783}
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 793 of file foreign.c.

794{
795 Oid oid;
796
798 CStringGetDatum(servername));
799 if (!OidIsValid(oid) && !missing_ok)
802 errmsg("server \"%s\" does not exist", servername)));
803 return oid;
804}

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 381 of file foreign.c.

382{
383 List *options;
384 HeapTuple tp;
385 Datum datum;
386 bool isnull;
387
389 ObjectIdGetDatum(relid),
391 if (!HeapTupleIsValid(tp))
392 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
393 attnum, relid);
394 datum = SysCacheGetAttr(ATTNUM,
395 tp,
397 &isnull);
398 if (isnull)
399 options = NIL;
400 else
402
403 ReleaseSysCache(tp);
404
405 return options;
406}
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 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:770

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 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 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 210 of file foreign.c.

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

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

◆ GetForeignTable()

ForeignTable * GetForeignTable ( Oid  relid)
extern

Definition at line 343 of file foreign.c.

344{
347 HeapTuple tp;
348 Datum datum;
349 bool isnull;
350
352 if (!HeapTupleIsValid(tp))
353 elog(ERROR, "cache lookup failed for foreign table %u", relid);
355
357 ft->relid = relid;
358 ft->serverid = tableform->ftserver;
359
360 /* Extract the ftoptions */
362 tp,
364 &isnull);
365 if (isnull)
366 ft->options = NIL;
367 else
368 ft->options = untransformRelOptions(datum);
369
370 ReleaseSysCache(tp);
371
372 return ft;
373}
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 289 of file foreign.c.

290{
291 Datum datum;
292 HeapTuple tp;
293 bool isnull;
295
297 ObjectIdGetDatum(userid),
298 ObjectIdGetDatum(serverid));
299
300 if (!HeapTupleIsValid(tp))
301 {
302 /* Not found for the specific user -- try PUBLIC */
305 ObjectIdGetDatum(serverid));
306 }
307
308 if (!HeapTupleIsValid(tp))
309 {
310 ForeignServer *server = GetForeignServer(serverid);
311
314 errmsg("user mapping not found for user \"%s\", server \"%s\"",
315 MappingUserName(userid), server->servername)));
316 }
317
319 um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
320 um->userid = userid;
321 um->serverid = serverid;
322
323 /* Extract the umoptions */
325 tp,
327 &isnull);
328 if (isnull)
329 um->options = NIL;
330 else
331 um->options = untransformRelOptions(datum);
332
333 ReleaseSysCache(tp);
334
335 return um;
336}
#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().