Skip to content

Commit 9088d1b

Browse files
committed
Add GetForeignColumnOptions() to foreign.c, and add some documentation.
GetForeignColumnOptions provides some abstraction for accessing column-specific FDW options, on a par with the access functions that were already provided here for other FDW-related information. Adjust file_fdw.c to use GetForeignColumnOptions instead of equivalent hand-rolled code. In addition, add some SGML documentation for the functions exported by foreign.c that are meant for use by FDW authors. (This is the fdw_helper portion of the proposed pgsql_fdw patch.) Hanada Shigeru, reviewed by KaiGai Kohei
1 parent cf7026b commit 9088d1b

File tree

4 files changed

+154
-39
lines changed

4 files changed

+154
-39
lines changed

contrib/file_fdw/file_fdw.c

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "optimizer/cost.h"
2828
#include "optimizer/pathnode.h"
2929
#include "utils/rel.h"
30-
#include "utils/syscache.h"
3130

3231
PG_MODULE_MAGIC;
3332

@@ -346,54 +345,30 @@ get_file_fdw_attribute_options(Oid relid)
346345
/* Retrieve FDW options for all user-defined attributes. */
347346
for (attnum = 1; attnum <= natts; attnum++)
348347
{
349-
HeapTuple tuple;
350-
Form_pg_attribute attr;
351-
Datum datum;
352-
bool isnull;
348+
Form_pg_attribute attr = tupleDesc->attrs[attnum - 1];
349+
List *options;
350+
ListCell *lc;
353351

354352
/* Skip dropped attributes. */
355-
if (tupleDesc->attrs[attnum - 1]->attisdropped)
353+
if (attr->attisdropped)
356354
continue;
357355

358-
/*
359-
* We need the whole pg_attribute tuple not just what is in the
360-
* tupleDesc, so must do a catalog lookup.
361-
*/
362-
tuple = SearchSysCache2(ATTNUM,
363-
RelationGetRelid(rel),
364-
Int16GetDatum(attnum));
365-
if (!HeapTupleIsValid(tuple))
366-
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
367-
attnum, RelationGetRelid(rel));
368-
attr = (Form_pg_attribute) GETSTRUCT(tuple);
369-
370-
datum = SysCacheGetAttr(ATTNUM,
371-
tuple,
372-
Anum_pg_attribute_attfdwoptions,
373-
&isnull);
374-
if (!isnull)
356+
options = GetForeignColumnOptions(relid, attnum);
357+
foreach(lc, options)
375358
{
376-
List *options = untransformRelOptions(datum);
377-
ListCell *lc;
359+
DefElem *def = (DefElem *) lfirst(lc);
378360

379-
foreach(lc, options)
361+
if (strcmp(def->defname, "force_not_null") == 0)
380362
{
381-
DefElem *def = (DefElem *) lfirst(lc);
382-
383-
if (strcmp(def->defname, "force_not_null") == 0)
363+
if (defGetBoolean(def))
384364
{
385-
if (defGetBoolean(def))
386-
{
387-
char *attname = pstrdup(NameStr(attr->attname));
365+
char *attname = pstrdup(NameStr(attr->attname));
388366

389-
fnncolumns = lappend(fnncolumns, makeString(attname));
390-
}
367+
fnncolumns = lappend(fnncolumns, makeString(attname));
391368
}
392-
/* maybe in future handle other options here */
393369
}
370+
/* maybe in future handle other options here */
394371
}
395-
396-
ReleaseSysCache(tuple);
397372
}
398373

399374
heap_close(rel, AccessShareLock);

doc/src/sgml/fdwhandler.sgml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,109 @@ EndForeignScan (ForeignScanState *node);
244244

245245
</sect1>
246246

247+
<sect1 id="fdw-helpers">
248+
<title>Foreign Data Wrapper Helper Functions</title>
249+
250+
<para>
251+
Several helper functions are exported from the core server so that
252+
authors of foreign data wrappers can get easy access to attributes of
253+
FDW-related objects, such as FDW options.
254+
To use any of these functions, you need to include the header file
255+
<filename>foreign/foreign.h</filename> in your source file.
256+
That header also defines the struct types that are returned by
257+
these functions.
258+
</para>
259+
260+
<para>
261+
<programlisting>
262+
ForeignDataWrapper *
263+
GetForeignDataWrapper(Oid fdwid);
264+
</programlisting>
265+
266+
This function returns a <structname>ForeignDataWrapper</structname>
267+
object for the foreign-data wrapper with the given OID. A
268+
<structname>ForeignDataWrapper</structname> object contains properties
269+
of the FDW (see <filename>foreign/foreign.h</filename> for details).
270+
</para>
271+
272+
<para>
273+
<programlisting>
274+
ForeignServer *
275+
GetForeignServer(Oid serverid);
276+
</programlisting>
277+
278+
This function returns a <structname>ForeignServer</structname> object
279+
for the foreign server with the given OID. A
280+
<structname>ForeignServer</structname> object contains properties
281+
of the server (see <filename>foreign/foreign.h</filename> for details).
282+
</para>
283+
284+
<para>
285+
<programlisting>
286+
UserMapping *
287+
GetUserMapping(Oid userid, Oid serverid);
288+
</programlisting>
289+
290+
This function returns a <structname>UserMapping</structname> object for
291+
the user mapping of the given role on the given server. (If there is no
292+
mapping for the specific user, it will return the mapping for
293+
<literal>PUBLIC</>, or throw error if there is none.) A
294+
<structname>UserMapping</structname> object contains properties of the
295+
user mapping (see <filename>foreign/foreign.h</filename> for details).
296+
</para>
297+
298+
<para>
299+
<programlisting>
300+
ForeignTable *
301+
GetForeignTable(Oid relid);
302+
</programlisting>
303+
304+
This function returns a <structname>ForeignTable</structname> object for
305+
the foreign table with the given OID. A
306+
<structname>ForeignTable</structname> object contains properties of the
307+
foreign table (see <filename>foreign/foreign.h</filename> for details).
308+
</para>
309+
310+
<para>
311+
<programlisting>
312+
List *
313+
GetForeignTableColumnOptions(Oid relid, AttrNumber attnum);
314+
</programlisting>
315+
316+
This function returns the per-column FDW options for the column with the
317+
given foreign table OID and attribute number, in the form of a list of
318+
<structname>DefElem</structname>. NIL is returned if the column has no
319+
options.
320+
</para>
321+
322+
<para>
323+
Some object types have name-based lookup functions in addition to the
324+
OID-based ones:
325+
</para>
326+
327+
<para>
328+
<programlisting>
329+
ForeignDataWrapper *
330+
GetForeignDataWrapperByName(const char *name, bool missing_ok);
331+
</programlisting>
332+
333+
This function returns a <structname>ForeignDataWrapper</structname>
334+
object for the foreign-data wrapper with the given name. If the wrapper
335+
is not found, return NULL if missing_ok is true, otherwise raise an
336+
error.
337+
</para>
338+
339+
<para>
340+
<programlisting>
341+
ForeignServer *
342+
GetForeignServerByName(const char *name, bool missing_ok);
343+
</programlisting>
344+
345+
This function returns a <structname>ForeignServer</structname> object
346+
for the foreign server with the given name. If the server is not found,
347+
return NULL if missing_ok is true, otherwise raise an error.
348+
</para>
349+
350+
</sect1>
351+
247352
</chapter>

src/backend/foreign/foreign.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ extern Datum pg_options_to_table(PG_FUNCTION_ARGS);
2828
extern Datum postgresql_fdw_validator(PG_FUNCTION_ARGS);
2929

3030

31-
3231
/*
3332
* GetForeignDataWrapper - look up the foreign-data wrapper by OID.
3433
*/
@@ -71,7 +70,6 @@ GetForeignDataWrapper(Oid fdwid)
7170
}
7271

7372

74-
7573
/*
7674
* GetForeignDataWrapperByName - look up the foreign-data wrapper
7775
* definition by name.
@@ -247,6 +245,39 @@ GetForeignTable(Oid relid)
247245
}
248246

249247

248+
/*
249+
* GetForeignColumnOptions - Get attfdwoptions of given relation/attnum
250+
* as list of DefElem.
251+
*/
252+
List *
253+
GetForeignColumnOptions(Oid relid, AttrNumber attnum)
254+
{
255+
List *options;
256+
HeapTuple tp;
257+
Datum datum;
258+
bool isnull;
259+
260+
tp = SearchSysCache2(ATTNUM,
261+
ObjectIdGetDatum(relid),
262+
Int16GetDatum(attnum));
263+
if (!HeapTupleIsValid(tp))
264+
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
265+
attnum, relid);
266+
datum = SysCacheGetAttr(ATTNUM,
267+
tp,
268+
Anum_pg_attribute_attfdwoptions,
269+
&isnull);
270+
if (isnull)
271+
options = NIL;
272+
else
273+
options = untransformRelOptions(datum);
274+
275+
ReleaseSysCache(tp);
276+
277+
return options;
278+
}
279+
280+
250281
/*
251282
* GetFdwRoutine - call the specified foreign-data wrapper handler routine
252283
* to get its FdwRoutine struct.
@@ -498,6 +529,7 @@ postgresql_fdw_validator(PG_FUNCTION_ARGS)
498529
PG_RETURN_BOOL(true);
499530
}
500531

532+
501533
/*
502534
* get_foreign_data_wrapper_oid - given a FDW name, look up the OID
503535
*
@@ -518,6 +550,7 @@ get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
518550
return oid;
519551
}
520552

553+
521554
/*
522555
* get_foreign_server_oid - given a FDW name, look up the OID
523556
*

src/include/foreign/foreign.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern ForeignDataWrapper *GetForeignDataWrapperByName(const char *name,
7676
bool missing_ok);
7777
extern ForeignTable *GetForeignTable(Oid relid);
7878

79+
extern List *GetForeignColumnOptions(Oid relid, AttrNumber attnum);
80+
7981
extern Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok);
8082
extern Oid get_foreign_server_oid(const char *servername, bool missing_ok);
8183

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy