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

672 {
673  Oid oid;
674 
675  oid = GetSysCacheOid1(FOREIGNDATAWRAPPERNAME,
676  Anum_pg_foreign_data_wrapper_oid,
677  CStringGetDatum(fdwname));
678  if (!OidIsValid(oid) && !missing_ok)
679  ereport(ERROR,
680  (errcode(ERRCODE_UNDEFINED_OBJECT),
681  errmsg("foreign-data wrapper \"%s\" does not exist",
682  fdwname)));
683  return oid;
684 }
#define OidIsValid(objectId)
Definition: c.h:775
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
unsigned int Oid
Definition: postgres_ext.h:31
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:104

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

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

◆ get_foreign_server_oid()

Oid get_foreign_server_oid ( const char *  servername,
bool  missing_ok 
)

Definition at line 694 of file foreign.c.

695 {
696  Oid oid;
697 
698  oid = GetSysCacheOid1(FOREIGNSERVERNAME, Anum_pg_foreign_server_oid,
699  CStringGetDatum(servername));
700  if (!OidIsValid(oid) && !missing_ok)
701  ereport(ERROR,
702  (errcode(ERRCODE_UNDEFINED_OBJECT),
703  errmsg("server \"%s\" does not exist", servername)));
704  return oid;
705 }

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

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

◆ GetForeignColumnOptions()

List* GetForeignColumnOptions ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 291 of file foreign.c.

292 {
293  List *options;
294  HeapTuple tp;
295  Datum datum;
296  bool isnull;
297 
298  tp = SearchSysCache2(ATTNUM,
299  ObjectIdGetDatum(relid),
301  if (!HeapTupleIsValid(tp))
302  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
303  attnum, relid);
304  datum = SysCacheGetAttr(ATTNUM,
305  tp,
306  Anum_pg_attribute_attfdwoptions,
307  &isnull);
308  if (isnull)
309  options = NIL;
310  else
312 
313  ReleaseSysCache(tp);
314 
315  return options;
316 }
#define elog(elevel,...)
Definition: elog.h:224
#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:64
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1331
Definition: pg_list.h:54
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:479
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:229

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

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

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

Referenced by CreateForeignDataWrapper(), and CreateForeignServer().

◆ GetForeignDataWrapperExtended()

ForeignDataWrapper* GetForeignDataWrapperExtended ( Oid  fdwid,
bits16  flags 
)

Definition at line 48 of file foreign.c.

49 {
51  ForeignDataWrapper *fdw;
52  Datum datum;
53  HeapTuple tp;
54  bool isnull;
55 
56  tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid));
57 
58  if (!HeapTupleIsValid(tp))
59  {
60  if ((flags & FDW_MISSING_OK) == 0)
61  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
62  return NULL;
63  }
64 
66 
67  fdw = (ForeignDataWrapper *) palloc(sizeof(ForeignDataWrapper));
68  fdw->fdwid = fdwid;
69  fdw->owner = fdwform->fdwowner;
70  fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
71  fdw->fdwhandler = fdwform->fdwhandler;
72  fdw->fdwvalidator = fdwform->fdwvalidator;
73 
74  /* Extract the fdwoptions */
75  datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID,
76  tp,
77  Anum_pg_foreign_data_wrapper_fdwoptions,
78  &isnull);
79  if (isnull)
80  fdw->options = NIL;
81  else
82  fdw->options = untransformRelOptions(datum);
83 
84  ReleaseSysCache(tp);
85 
86  return fdw;
87 }
#define NameStr(name)
Definition: c.h:746
#define FDW_MISSING_OK
Definition: foreign.h:64
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void * palloc(Size size)
Definition: mcxt.c:1316
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:218

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

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

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

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

◆ GetForeignTable()

ForeignTable* GetForeignTable ( Oid  relid)

Definition at line 253 of file foreign.c.

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

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