PostgreSQL Source Code git master
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)
 
ForeignServerGetForeignServerExtended (Oid serverid, bits16 flags)
 
ForeignServerGetForeignServerByName (const char *srvname, bool missing_ok)
 
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 64 of file foreign.h.

◆ FSV_MISSING_OK

#define FSV_MISSING_OK   0x01

Definition at line 61 of file foreign.h.

◆ MappingUserName

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

Definition at line 20 of file foreign.h.

Typedef Documentation

◆ ForeignDataWrapper

◆ ForeignServer

typedef struct ForeignServer ForeignServer

◆ ForeignTable

typedef struct ForeignTable ForeignTable

◆ UserMapping

typedef struct UserMapping UserMapping

Function Documentation

◆ get_foreign_data_wrapper_oid()

Oid get_foreign_data_wrapper_oid ( const char *  fdwname,
bool  missing_ok 
)

Definition at line 681 of file foreign.c.

682{
683 Oid oid;
684
685 oid = GetSysCacheOid1(FOREIGNDATAWRAPPERNAME,
686 Anum_pg_foreign_data_wrapper_oid,
687 CStringGetDatum(fdwname));
688 if (!OidIsValid(oid) && !missing_ok)
690 (errcode(ERRCODE_UNDEFINED_OBJECT),
691 errmsg("foreign-data wrapper \"%s\" does not exist",
692 fdwname)));
693 return oid;
694}
#define OidIsValid(objectId)
Definition: c.h:732
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
unsigned int Oid
Definition: postgres_ext.h:32
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:109

References CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, 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 704 of file foreign.c.

705{
706 Oid oid;
707
708 oid = GetSysCacheOid1(FOREIGNSERVERNAME, Anum_pg_foreign_server_oid,
709 CStringGetDatum(servername));
710 if (!OidIsValid(oid) && !missing_ok)
712 (errcode(ERRCODE_UNDEFINED_OBJECT),
713 errmsg("server \"%s\" does not exist", servername)));
714 return oid;
715}

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

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

◆ GetForeignColumnOptions()

List * GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 292 of file foreign.c.

293{
294 List *options;
295 HeapTuple tp;
296 Datum datum;
297 bool isnull;
298
299 tp = SearchSysCache2(ATTNUM,
300 ObjectIdGetDatum(relid),
302 if (!HeapTupleIsValid(tp))
303 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
304 attnum, relid);
305 datum = SysCacheGetAttr(ATTNUM,
306 tp,
307 Anum_pg_attribute_attfdwoptions,
308 &isnull);
309 if (isnull)
310 options = NIL;
311 else
313
314 ReleaseSysCache(tp);
315
316 return options;
317}
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int16 attnum
Definition: pg_attribute.h:74
#define NIL
Definition: pg_list.h:68
static char ** options
uintptr_t Datum
Definition: postgres.h:69
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:177
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1350
Definition: pg_list.h:54
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232

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

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

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper * GetForeignDataWrapperExtended ( Oid  fdwid,
bits16  flags 
)

Definition at line 49 of file foreign.c.

50{
53 Datum datum;
54 HeapTuple tp;
55 bool isnull;
56
57 tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid));
58
59 if (!HeapTupleIsValid(tp))
60 {
61 if ((flags & FDW_MISSING_OK) == 0)
62 elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
63 return NULL;
64 }
65
67
69 fdw->fdwid = fdwid;
70 fdw->owner = fdwform->fdwowner;
71 fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
72 fdw->fdwhandler = fdwform->fdwhandler;
73 fdw->fdwvalidator = fdwform->fdwvalidator;
74
75 /* Extract the fdwoptions */
76 datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID,
77 tp,
78 Anum_pg_foreign_data_wrapper_fdwoptions,
79 &isnull);
80 if (isnull)
81 fdw->options = NIL;
82 else
83 fdw->options = untransformRelOptions(datum);
84
86
87 return fdw;
88}
#define NameStr(name)
Definition: c.h:703
#define FDW_MISSING_OK
Definition: foreign.h:64
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc(Size size)
Definition: mcxt.c:1317
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
char * fdwname
Definition: foreign.h:28
List * options
Definition: foreign.h:31
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

References elog, ERROR, FDW_MISSING_OK, ForeignDataWrapper::fdwhandler, ForeignDataWrapper::fdwid, ForeignDataWrapper::fdwname, ForeignDataWrapper::fdwvalidator, 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 182 of file foreign.c.

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

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

Referenced by AlterUserMapping(), 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 123 of file foreign.c.

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

◆ GetForeignTable()

ForeignTable * GetForeignTable ( Oid  relid)

Definition at line 254 of file foreign.c.

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

References elog, ERROR, 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(), postgresGetAnalyzeInfoForForeignTable(), postgresGetForeignRelSize(), and postgresIsForeignRelUpdatable().

◆ GetUserMapping()

UserMapping * GetUserMapping ( Oid  userid,
Oid  serverid 
)

Definition at line 200 of file foreign.c.

201{
202 Datum datum;
203 HeapTuple tp;
204 bool isnull;
205 UserMapping *um;
206
207 tp = SearchSysCache2(USERMAPPINGUSERSERVER,
208 ObjectIdGetDatum(userid),
209 ObjectIdGetDatum(serverid));
210
211 if (!HeapTupleIsValid(tp))
212 {
213 /* Not found for the specific user -- try PUBLIC */
214 tp = SearchSysCache2(USERMAPPINGUSERSERVER,
216 ObjectIdGetDatum(serverid));
217 }
218
219 if (!HeapTupleIsValid(tp))
220 {
221 ForeignServer *server = GetForeignServer(serverid);
222
224 (errcode(ERRCODE_UNDEFINED_OBJECT),
225 errmsg("user mapping not found for user \"%s\", server \"%s\"",
226 MappingUserName(userid), server->servername)));
227 }
228
229 um = (UserMapping *) palloc(sizeof(UserMapping));
230 um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
231 um->userid = userid;
232 um->serverid = serverid;
233
234 /* Extract the umoptions */
235 datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
236 tp,
237 Anum_pg_user_mapping_umoptions,
238 &isnull);
239 if (isnull)
240 um->options = NIL;
241 else
242 um->options = untransformRelOptions(datum);
243
244 ReleaseSysCache(tp);
245
246 return um;
247}
#define MappingUserName(userid)
Definition: foreign.h:20
FormData_pg_user_mapping * Form_pg_user_mapping
#define InvalidOid
Definition: postgres_ext.h:37
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, GetForeignServer(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, MappingUserName, NIL, ObjectIdGetDatum(), UserMapping::options, palloc(), ReleaseSysCache(), SearchSysCache2(), UserMapping::serverid, ForeignServer::servername, SysCacheGetAttr(), UserMapping::umid, untransformRelOptions(), and UserMapping::userid.

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