<!-- doc/src/sgml/fdwhandler.sgml -->
<chapter id="fdwhandler">
<title>Writing a Foreign Data Wrapper</title>
<indexterm zone="fdwhandler">
<primary>foreign data wrapper</primary>
<secondary>handler for</secondary>
</indexterm>
<para>
All operations on a foreign table are handled through its foreign data
wrapper, which consists of a set of functions that the core server
calls. The foreign data wrapper is responsible for fetching
data from the remote data source and returning it to the
<productname>PostgreSQL</productname> executor. If updating foreign
tables is to be supported, the wrapper must handle that, too.
This chapter outlines how to write a new foreign data wrapper.
</para>
<para>
The foreign data wrappers included in the standard distribution are good
references when trying to write your own. Look into the
<filename>contrib</filename> subdirectory of the source tree.
The <xref linkend="sql-createforeigndatawrapper"/> reference page also has
some useful details.
</para>
<note>
<para>
The SQL standard specifies an interface for writing foreign data wrappers.
However, PostgreSQL does not implement that API, because the effort to
accommodate it into PostgreSQL would be large, and the standard API hasn't
gained wide adoption anyway.
</para>
</note>
<sect1 id="fdw-functions">
<title>Foreign Data Wrapper Functions</title>
<para>
The FDW author needs to implement a handler function, and optionally
a validator function. Both functions must be written in a compiled
language such as C, using the version-1 interface.
For details on C language calling conventions and dynamic loading,
see <xref linkend="xfunc-c"/>.
</para>
<para>
The handler function simply returns a struct of function pointers to
callback functions that will be called by the planner, executor, and
various maintenance commands.
Most of the effort in writing an FDW is in implementing these callback
functions.
The handler function must be registered with
<productname>PostgreSQL</productname> as taking no arguments and
returning the special pseudo-type <type>fdw_handler</type>. The
callback functions are plain C functions and are not visible or
callable at the SQL level. The callback functions are described in
<xref linkend="fdw-callbacks"/>.
</para>
<para>
The validator function is responsible for validating options given in
<command>CREATE</command> and <command>ALTER</command> commands for its
foreign data wrapper, as well as foreign servers, user mappings, and
foreign tables using the wrapper.
The validator function must be registered as taking two arguments, a
text array containing the options to be validated, and an OID
representing the type of object the options are associated with. The
latter corresponds to the OID of the system catalog the object
would be stored in, one of:
<itemizedlist spacing="compact">
<listitem><para><literal>AttributeRelationId</literal></para></listitem>
<listitem><para><literal>ForeignDataWrapperRelationId</literal></para></listitem>
<listitem><para><literal>ForeignServerRelationId</literal></para></listitem>
<listitem><para><literal>ForeignTableRelationId</literal></para></listitem>
<listitem><para><literal>UserMappingRelationId</literal></para></listitem>
</itemizedlist>
If no validator function is supplied, options are not checked at object
creation time or object alteration time.
</para>
</sect1>
<sect1 id="fdw-callbacks">
<title>Foreign Data Wrapper Callback Routines</title>
<para>
The FDW handler function returns a palloc'd <structname>FdwRoutine</structname>
struct containing pointers to the callback functions described below.
The scan-related functions are required, the