Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb74240

Browse files
committed
Implement an API to let foreign-data wrappers actually be functional.
This commit provides the core code and documentation needed. A contribmodule test case will follow shortly.Shigeru Hanada, Jan Urbanski, Heikki Linnakangas
1 parentd581348 commitbb74240

39 files changed

+1202
-62
lines changed

‎doc/src/sgml/ddl.sgml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,6 +2986,53 @@ ANALYZE measurement;
29862986
</sect2>
29872987
</sect1>
29882988

2989+
<sect1 id="ddl-foreign-data">
2990+
<title>Foreign Data</title>
2991+
2992+
<indexterm>
2993+
<primary>foreign data</primary>
2994+
</indexterm>
2995+
<indexterm>
2996+
<primary>foreign table</primary>
2997+
</indexterm>
2998+
2999+
<para>
3000+
<productname>PostgreSQL</productname> implements portions of the SQL/MED
3001+
specification, allowing you to access data that resides outside
3002+
PostgreSQL using regular SQL queries. Such data is referred to as
3003+
<firstterm>foreign data</>. (Note that this usage is not to be confused
3004+
with foreign keys, which are a type of constraint within the database.)
3005+
</para>
3006+
3007+
<para>
3008+
Foreign data is accessed with help from a
3009+
<firstterm>foreign data wrapper</firstterm>. A foreign data wrapper is a
3010+
library that can communicate with an external data source, hiding the
3011+
details of connecting to the data source and fetching data from it. There
3012+
are several foreign data wrappers available, which can for example read
3013+
plain data files residing on the server, or connect to another PostgreSQL
3014+
instance. If none of the existing foreign data wrappers suit your needs,
3015+
you can write your own; see <xref linkend="fdwhandler">.
3016+
</para>
3017+
3018+
<para>
3019+
To access foreign data, you need to create a <firstterm>foreign server</>
3020+
object, which defines how to connect to a particular external data source,
3021+
according to the set of options used by a particular foreign data
3022+
wrapper. Then you need to create one or more <firstterm>foreign
3023+
tables</firstterm>, which define the structure of the remote data. A
3024+
foreign table can be used in queries just like a normal table, but a
3025+
foreign table has no storage in the PostgreSQL server. Whenever it is
3026+
used, PostgreSQL asks the foreign data wrapper to fetch the data from the
3027+
external source.
3028+
</para>
3029+
3030+
<para>
3031+
Currently, foreign tables are read-only. This limitation may be fixed
3032+
in a future release.
3033+
</para>
3034+
</sect1>
3035+
29893036
<sect1 id="ddl-others">
29903037
<title>Other Database Objects</title>
29913038

‎doc/src/sgml/fdwhandler.sgml

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<!-- doc/src/sgml/fdwhandler.sgml -->
2+
3+
<chapter id="fdwhandler">
4+
<title>Writing A Foreign Data Wrapper</title>
5+
6+
<indexterm zone="fdwhandler">
7+
<primary>foreign data wrapper</primary>
8+
<secondary>handler for</secondary>
9+
</indexterm>
10+
11+
<para>
12+
All operations on a foreign table are handled through its foreign data
13+
wrapper, which consists of a set of functions that the planner and
14+
executor call. The foreign data wrapper is responsible for fetching
15+
data from the remote data source and returning it to the
16+
<productname>PostgreSQL</productname> executor. This chapter outlines how
17+
to write a new foreign data wrapper.
18+
</para>
19+
20+
<para>
21+
The FDW author needs to implement a handler function, and optionally
22+
a validator function. Both functions must be written in a compiled
23+
language such as C, using the version-1 interface.
24+
For details on C language calling conventions and dynamic loading,
25+
see <xref linkend="xfunc-c">.
26+
</para>
27+
28+
<para>
29+
The handler function simply returns a struct of function pointers to
30+
callback functions that will be called by the planner and executor.
31+
Most of the effort in writing an FDW is in implementing these callback
32+
functions.
33+
The handler function must be registered with
34+
<productname>PostgreSQL</productname> as taking no arguments and returning
35+
the special pseudo-type <type>fdw_handler</type>.
36+
The callback functions are plain C functions and are not visible or
37+
callable at the SQL level.
38+
</para>
39+
40+
<para>
41+
The validator function is responsible for validating options given in the
42+
<command>CREATE FOREIGN DATA WRAPPER</command>, <command>CREATE
43+
SERVER</command> and <command>CREATE FOREIGN TABLE</command> commands.
44+
The validator function must be registered as taking two arguments, a text
45+
array containing the options to be validated, and an OID representing the
46+
type of object the options are associated with (in the form of the OID
47+
of the system catalog the object would be stored in). If no validator
48+
function is supplied, the options are not checked at object creation time.
49+
</para>
50+
51+
<para>
52+
The foreign data wrappers included in the standard distribution are good
53+
references when trying to write your own. Look into the
54+
<filename>contrib/file_fdw</> subdirectory of the source tree.
55+
The <xref linkend="sql-createforeigndatawrapper"> reference page also has
56+
some useful details.
57+
</para>
58+
59+
<note>
60+
<para>
61+
The SQL standard specifies an interface for writing foreign data wrappers.
62+
However, PostgreSQL does not implement that API, because the effort to
63+
accommodate it into PostgreSQL would be large, and the standard API hasn't
64+
gained wide adoption anyway.
65+
</para>
66+
</note>
67+
68+
<sect1 id="fdw-routines">
69+
<title>Foreign Data Wrapper Callback Routines</title>
70+
71+
<para>
72+
The FDW handler function returns a palloc'd <structname>FdwRoutine</>
73+
struct containing pointers to the following callback functions:
74+
</para>
75+
76+
<para>
77+
<programlisting>
78+
FdwPlan *
79+
PlanForeignScan (Oid foreigntableid,
80+
PlannerInfo *root,
81+
RelOptInfo *baserel);
82+
</programlisting>
83+
84+
Plan a scan on a foreign table. This is called when a query is planned.
85+
<literal>foreigntableid</> is the <structname>pg_class</> OID of the
86+
foreign table. <literal>root</> is the planner's global information
87+
about the query, and <literal>baserel</> is the planner's information
88+
about this table.
89+
The function must return a palloc'd struct that contains cost estimates
90+
plus any FDW-private information that is needed to execute the foreign
91+
scan at a later time. (Note that the private information must be
92+
represented in a form that <function>copyObject</> knows how to copy.)
93+
</para>
94+
95+
<para>
96+
The information in <literal>root</> and <literal>baserel</> can be used
97+
to reduce the amount of information that has to be fetched from the
98+
foreign table (and therefore reduce the cost estimate).
99+
<literal>baserel-&gt;baserestrictinfo</> is particularly interesting, as
100+
it contains restriction quals (<literal>WHERE</> clauses) that can be
101+
used to filter the rows to be fetched. (The FDW is not required to
102+
enforce these quals, as the finished plan will recheck them anyway.)
103+
<literal>baserel-&gt;reltargetlist</> can be used to determine which
104+
columns need to be fetched.
105+
</para>
106+
107+
<para>
108+
In addition to returning cost estimates, the function should update
109+
<literal>baserel-&gt;rows</> to be the expected number of rows returned
110+
by the scan, after accounting for the filtering done by the restriction
111+
quals. The initial value of <literal>baserel-&gt;rows</> is just a
112+
constant default estimate, which should be replaced if at all possible.
113+
The function may also choose to update <literal>baserel-&gt;width</> if
114+
it can compute a better estimate of the average result row width.
115+
</para>
116+
117+
<para>
118+
<programlisting>
119+
void
120+
ExplainForeignScan (ForeignScanState *node,
121+
ExplainState *es);
122+
</programlisting>
123+
124+
Print additional <command>EXPLAIN</> output for a foreign table scan.
125+
This can just return if there is no need to print anything.
126+
Otherwise, it should call <function>ExplainPropertyText</> and
127+
related functions to add fields to the <command>EXPLAIN</> output.
128+
The flag fields in <literal>es</> can be used to determine what to
129+
print, and the state of the <structname>ForeignScanState</> node
130+
can be inspected to provide runtime statistics in the <command>EXPLAIN
131+
ANALYZE</> case.
132+
</para>
133+
134+
<para>
135+
<programlisting>
136+
void
137+
BeginForeignScan (ForeignScanState *node,
138+
int eflags);
139+
</programlisting>
140+
141+
Begin executing a foreign scan. This is called during executor startup.
142+
It should perform any initialization needed before the scan can start.
143+
The <structname>ForeignScanState</> node has already been created, but
144+
its <structfield>fdw_state</> field is still NULL. Information about
145+
the table to scan is accessible through the
146+
<structname>ForeignScanState</> node (in particular, from the underlying
147+
<structname>ForeignScan</> plan node, which contains a pointer to the
148+
<structname>FdwPlan</> structure returned by
149+
<function>PlanForeignScan</>).
150+
</para>
151+
152+
<para>
153+
Note that when <literal>(eflags &amp; EXEC_FLAG_EXPLAIN_ONLY)</> is
154+
true, this function should not perform any externally-visible actions;
155+
it should only do the minimum required to make the node state valid
156+
for <function>ExplainForeignScan</> and <function>EndForeignScan</>.
157+
</para>
158+
159+
<para>
160+
<programlisting>
161+
TupleTableSlot *
162+
IterateForeignScan (ForeignScanState *node);
163+
</programlisting>
164+
165+
Fetch one row from the foreign source, returning it in a tuple table slot
166+
(the node's <structfield>ScanTupleSlot</> should be used for this
167+
purpose). Return NULL if no more rows are available. The tuple table
168+
slot infrastructure allows either a physical or virtual tuple to be
169+
returned; in most cases the latter choice is preferable from a
170+
performance standpoint. Note that this is called in a short-lived memory
171+
context that will be reset between invocations. Create a memory context
172+
in <function>BeginForeignScan</> if you need longer-lived storage, or use
173+
the <structfield>es_query_cxt</> of the node's <structname>EState</>.
174+
</para>
175+
176+
<para>
177+
The rows returned must match the column signature of the foreign table
178+
being scanned. If you choose to optimize away fetching columns that
179+
are not needed, you should insert nulls in those column positions.
180+
</para>
181+
182+
<para>
183+
<programlisting>
184+
void
185+
ReScanForeignScan (ForeignScanState *node);
186+
</programlisting>
187+
188+
Restart the scan from the beginning. Note that any parameters the
189+
scan depends on may have changed value, so the new scan does not
190+
necessarily return exactly the same rows.
191+
</para>
192+
193+
<para>
194+
<programlisting>
195+
void
196+
EndForeignScan (ForeignScanState *node);
197+
</programlisting>
198+
199+
End the scan and release resources. It is normally not important
200+
to release palloc'd memory, but for example open files and connections
201+
to remote servers should be cleaned up.
202+
</para>
203+
204+
<para>
205+
The <structname>FdwRoutine</> and <structname>FdwPlan</> struct types
206+
are declared in <filename>src/include/foreign/fdwapi.h</>, which see
207+
for additional details.
208+
</para>
209+
210+
</sect1>
211+
212+
</chapter>

‎doc/src/sgml/filelist.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<!entity indexam SYSTEM "indexam.sgml">
8787
<!entity nls SYSTEM "nls.sgml">
8888
<!entity plhandler SYSTEM "plhandler.sgml">
89+
<!entity fdwhandler SYSTEM "fdwhandler.sgml">
8990
<!entity protocol SYSTEM "protocol.sgml">
9091
<!entity sources SYSTEM "sources.sgml">
9192
<!entity storage SYSTEM "storage.sgml">

‎doc/src/sgml/postgres.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
&sources;
239239
&nls;
240240
&plhandler;
241+
&fdwhandler;
241242
&geqo;
242243
&indexam;
243244
&gist;

‎doc/src/sgml/ref/create_foreign_data_wrapper.sgml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,13 @@ CREATE FOREIGN DATA WRAPPER <replaceable class="parameter">name</replaceable>
119119
<title>Notes</title>
120120

121121
<para>
122-
At the moment, the foreign-data wrapper functionality is very
123-
rudimentary. The purpose of foreign-data wrappers, foreign
124-
servers, and user mappings is to store this information in a
125-
standard way so that it can be queried by interested applications.
126-
One such application is <application>dblink</application>;
127-
see <xref linkend="dblink">. The functionality to actually query
128-
external data through a foreign-data wrapper library does not exist
129-
yet.
122+
At the moment, the foreign-data wrapper functionality is rudimentary.
123+
There is no support for updating a foreign table, and optimization of
124+
queries is primitive (and mostly left to the wrapper, too).
130125
</para>
131126

132127
<para>
133-
There iscurrentlyone foreign-data wrapper validator function
128+
There is one built-in foreign-data wrapper validator function
134129
provided:
135130
<filename>postgresql_fdw_validator</filename>, which accepts
136131
options corresponding to <application>libpq</> connection

‎doc/src/sgml/ref/create_foreign_table.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable class="PARAMETER">table_name
131131
<para>
132132
Options to be associated with the new foreign table.
133133
The allowed option names and values are specific to each foreign
134-
data wrapper and are validated using the foreign-data wrapper
135-
library. Option names must be unique.
134+
data wrapper and are validated using the foreign-data wrapper's
135+
validator function. Option names must be unique.
136136
</para>
137137
</listitem>
138138
</varlistentry>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp