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 *name, bool missing_ok)
 
UserMappingGetUserMapping (Oid userid, Oid serverid)
 
ForeignDataWrapperGetForeignDataWrapper (Oid fdwid)
 
ForeignDataWrapperGetForeignDataWrapperExtended (Oid fdwid, bits16 flags)
 
ForeignDataWrapperGetForeignDataWrapperByName (const char *name, 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.

Referenced by GetForeignDataWrapperExtended().

◆ FSV_MISSING_OK

#define FSV_MISSING_OK   0x01

Definition at line 61 of file foreign.h.

Referenced by GetForeignServerExtended().

◆ MappingUserName

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

Definition at line 20 of file foreign.h.

Referenced by AlterUserMapping(), CreateUserMapping(), GetUserMapping(), and RemoveUserMapping().

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

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

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

692 {
693  Oid oid;
694 
696  Anum_pg_foreign_data_wrapper_oid,
697  CStringGetDatum(fdwname));
698  if (!OidIsValid(oid) && !missing_ok)
699  ereport(ERROR,
700  (errcode(ERRCODE_UNDEFINED_OBJECT),
701  errmsg("foreign-data wrapper \"%s\" does not exist",
702  fdwname)));
703  return oid;
704 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ get_foreign_server_oid()

Oid get_foreign_server_oid ( const char *  servername,
bool  missing_ok 
)

Definition at line 714 of file foreign.c.

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

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

715 {
716  Oid oid;
717 
718  oid = GetSysCacheOid1(FOREIGNSERVERNAME, Anum_pg_foreign_server_oid,
719  CStringGetDatum(servername));
720  if (!OidIsValid(oid) && !missing_ok)
721  ereport(ERROR,
722  (errcode(ERRCODE_UNDEFINED_OBJECT),
723  errmsg("server \"%s\" does not exist", servername)));
724  return oid;
725 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ GetForeignColumnOptions()

List* GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 286 of file foreign.c.

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

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

287 {
288  List *options;
289  HeapTuple tp;
290  Datum datum;
291  bool isnull;
292 
293  tp = SearchSysCache2(ATTNUM,
294  ObjectIdGetDatum(relid),
296  if (!HeapTupleIsValid(tp))
297  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
298  attnum, relid);
299  datum = SysCacheGetAttr(ATTNUM,
300  tp,
301  Anum_pg_attribute_attfdwoptions,
302  &isnull);
303  if (isnull)
304  options = NIL;
305  else
306  options = untransformRelOptions(datum);
307 
308  ReleaseSysCache(tp);
309 
310  return options;
311 }
#define NIL
Definition: pg_list.h:65
#define Int16GetDatum(X)
Definition: postgres.h:451
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static char ** options
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
List * untransformRelOptions(Datum options)
Definition: reloptions.c:960
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
#define elog(elevel,...)
Definition: elog.h:226
Definition: pg_list.h:50

◆ GetForeignDataWrapper()

◆ GetForeignDataWrapperByName()

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

Definition at line 94 of file foreign.c.

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

95 {
96  Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok);
97 
98  if (!OidIsValid(fdwId))
99  return NULL;
100 
101  return GetForeignDataWrapper(fdwId);
102 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition: foreign.c:691
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
Definition: foreign.c:35

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper* GetForeignDataWrapperExtended ( Oid  fdwid,
bits16  flags 
)

Definition at line 47 of file foreign.c.

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

48 {
50  ForeignDataWrapper *fdw;
51  Datum datum;
52  HeapTuple tp;
53  bool isnull;
54 
56 
57  if (!HeapTupleIsValid(tp))
58  {
59  if ((flags & FDW_MISSING_OK) == 0)
60  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
61  return NULL;
62  }
63 
65 
66  fdw = (ForeignDataWrapper *) palloc(sizeof(ForeignDataWrapper));
67  fdw->fdwid = fdwid;
68  fdw->owner = fdwform->fdwowner;
69  fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
70  fdw->fdwhandler = fdwform->fdwhandler;
71  fdw->fdwvalidator = fdwform->fdwvalidator;
72 
73  /* Extract the fdwoptions */
75  tp,
76  Anum_pg_foreign_data_wrapper_fdwoptions,
77  &isnull);
78  if (isnull)
79  fdw->options = NIL;
80  else
81  fdw->options = untransformRelOptions(datum);
82 
83  ReleaseSysCache(tp);
84 
85  return fdw;
86 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * fdwname
Definition: foreign.h:28
#define FDW_MISSING_OK
Definition: foreign.h:64
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
List * options
Definition: foreign.h:31
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
List * untransformRelOptions(Datum options)
Definition: reloptions.c:960
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609

◆ GetForeignServer()

◆ GetForeignServerByName()

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

Definition at line 180 of file foreign.c.

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

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

181 {
182  Oid serverid = get_foreign_server_oid(srvname, missing_ok);
183 
184  if (!OidIsValid(serverid))
185  return NULL;
186 
187  return GetForeignServer(serverid);
188 }
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:109
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition: foreign.c:714

◆ GetForeignServerExtended()

ForeignServer* GetForeignServerExtended ( Oid  serverid,
bits16  flags 
)

Definition at line 121 of file foreign.c.

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

122 {
123  Form_pg_foreign_server serverform;
124  ForeignServer *server;
125  HeapTuple tp;
126  Datum datum;
127  bool isnull;
128 
130 
131  if (!HeapTupleIsValid(tp))
132  {
133  if ((flags & FSV_MISSING_OK) == 0)
134  elog(ERROR, "cache lookup failed for foreign server %u", serverid);
135  return NULL;
136  }
137 
138  serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
139 
140  server = (ForeignServer *) palloc(sizeof(ForeignServer));
141  server->serverid = serverid;
142  server->servername = pstrdup(NameStr(serverform->srvname));
143  server->owner = serverform->srvowner;
144  server->fdwid = serverform->srvfdw;
145 
146  /* Extract server type */
148  tp,
149  Anum_pg_foreign_server_srvtype,
150  &isnull);
151  server->servertype = isnull ? NULL : TextDatumGetCString(datum);
152 
153  /* Extract server version */
155  tp,
156  Anum_pg_foreign_server_srvversion,
157  &isnull);
158  server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
159 
160  /* Extract the srvoptions */
162  tp,
163  Anum_pg_foreign_server_srvoptions,
164  &isnull);
165  if (isnull)
166  server->options = NIL;
167  else
168  server->options = untransformRelOptions(datum);
169 
170  ReleaseSysCache(tp);
171 
172  return server;
173 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1161
char * serverversion
Definition: foreign.h:41
char * servertype
Definition: foreign.h:40
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define FSV_MISSING_OK
Definition: foreign.h:61
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
List * untransformRelOptions(Datum options)
Definition: reloptions.c:960
FormData_pg_foreign_server * Form_pg_foreign_server
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void * palloc(Size size)
Definition: mcxt.c:924
char * servername
Definition: foreign.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
List * options
Definition: foreign.h:42
Oid serverid
Definition: foreign.h:36

◆ GetForeignTable()

ForeignTable* GetForeignTable ( Oid  relid)

Definition at line 248 of file foreign.c.

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(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresGetForeignRelSize(), and postgresIsForeignRelUpdatable().

249 {
250  Form_pg_foreign_table tableform;
251  ForeignTable *ft;
252  HeapTuple tp;
253  Datum datum;
254  bool isnull;
255 
257  if (!HeapTupleIsValid(tp))
258  elog(ERROR, "cache lookup failed for foreign table %u", relid);
259  tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
260 
261  ft = (ForeignTable *) palloc(sizeof(ForeignTable));
262  ft->relid = relid;
263  ft->serverid = tableform->ftserver;
264 
265  /* Extract the ftoptions */
267  tp,
268  Anum_pg_foreign_table_ftoptions,
269  &isnull);
270  if (isnull)
271  ft->options = NIL;
272  else
273  ft->options = untransformRelOptions(datum);
274 
275  ReleaseSysCache(tp);
276 
277  return ft;
278 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_foreign_table * Form_pg_foreign_table
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
Oid relid
Definition: foreign.h:55
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
List * untransformRelOptions(Datum options)
Definition: reloptions.c:960
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid serverid
Definition: foreign.h:56
List * options
Definition: foreign.h:57
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226

◆ GetUserMapping()

UserMapping* GetUserMapping ( Oid  userid,
Oid  serverid 
)

Definition at line 198 of file foreign.c.

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(), postgresGetForeignRelSize(), and postgresImportForeignSchema().

199 {
200  Datum datum;
201  HeapTuple tp;
202  bool isnull;
203  UserMapping *um;
204 
205  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
206  ObjectIdGetDatum(userid),
207  ObjectIdGetDatum(serverid));
208 
209  if (!HeapTupleIsValid(tp))
210  {
211  /* Not found for the specific user -- try PUBLIC */
212  tp = SearchSysCache2(USERMAPPINGUSERSERVER,
214  ObjectIdGetDatum(serverid));
215  }
216 
217  if (!HeapTupleIsValid(tp))
218  ereport(ERROR,
219  (errcode(ERRCODE_UNDEFINED_OBJECT),
220  errmsg("user mapping not found for \"%s\"",
221  MappingUserName(userid))));
222 
223  um = (UserMapping *) palloc(sizeof(UserMapping));
224  um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
225  um->userid = userid;
226  um->serverid = serverid;
227 
228  /* Extract the umoptions */
229  datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
230  tp,
231  Anum_pg_user_mapping_umoptions,
232  &isnull);
233  if (isnull)
234  um->options = NIL;
235  else
236  um->options = untransformRelOptions(datum);
237 
238  ReleaseSysCache(tp);
239 
240  return um;
241 }
Oid umid
Definition: foreign.h:47
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:570
Oid userid
Definition: foreign.h:48
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
FormData_pg_user_mapping * Form_pg_user_mapping
List * options
Definition: foreign.h:50
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
List * untransformRelOptions(Datum options)
Definition: reloptions.c:960
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid serverid
Definition: foreign.h:49
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1135
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define MappingUserName(userid)
Definition: foreign.h:20