Home Explore Blog CI



postgresql

27th chunk of `doc/src/sgml/fdwhandler.sgml`
95446c8e2e47e2267e96b72933e6d6200d14b7ae62cdb9bb0000000100000fa1
 This function returns the per-column FDW options for the column with the
     given foreign table OID and attribute number, in the form of a list of
     <structname>DefElem</structname>.  NIL is returned if the column has no
     options.
    </para>

    <para>
     Some object types have name-based lookup functions in addition to the
     OID-based ones:
    </para>

    <para>
<programlisting>
ForeignDataWrapper *
GetForeignDataWrapperByName(const char *name, bool missing_ok);
</programlisting>

     This function returns a <structname>ForeignDataWrapper</structname>
     object for the foreign-data wrapper with the given name.  If the wrapper
     is not found, return NULL if missing_ok is true, otherwise raise an
     error.
    </para>

    <para>
<programlisting>
ForeignServer *
GetForeignServerByName(const char *name, bool missing_ok);
</programlisting>

     This function returns a <structname>ForeignServer</structname> object
     for the foreign server with the given name.  If the server is not found,
     return NULL if missing_ok is true, otherwise raise an error.
    </para>

   </sect1>

   <sect1 id="fdw-planning">
    <title>Foreign Data Wrapper Query Planning</title>

    <para>
     The FDW callback functions <function>GetForeignRelSize</function>,
     <function>GetForeignPaths</function>, <function>GetForeignPlan</function>,
     <function>PlanForeignModify</function>, <function>GetForeignJoinPaths</function>,
     <function>GetForeignUpperPaths</function>, and <function>PlanDirectModify</function>
     must fit into the workings of the <productname>PostgreSQL</productname> planner.
     Here are some notes about what they must do.
    </para>

    <para>
     The information in <literal>root</literal> and <literal>baserel</literal> can be used
     to reduce the amount of information that has to be fetched from the
     foreign table (and therefore reduce the cost).
     <literal>baserel-&gt;baserestrictinfo</literal> is particularly interesting, as
     it contains restriction quals (<literal>WHERE</literal> clauses) that should be
     used to filter the rows to be fetched.  (The FDW itself is not required
     to enforce these quals, as the core executor can check them instead.)
     <literal>baserel-&gt;reltarget-&gt;exprs</literal> can be used to determine which
     columns need to be fetched; but note that it only lists columns that
     have to be emitted by the <structname>ForeignScan</structname> plan node, not
     columns that are used in qual evaluation but not output by the query.
    </para>

    <para>
     Various private fields are available for the FDW planning functions to
     keep information in.  Generally, whatever you store in FDW private fields
     should be palloc'd, so that it will be reclaimed at the end of planning.
    </para>

    <para>
     <literal>baserel-&gt;fdw_private</literal> is a <type>void</type> pointer that is
     available for FDW planning functions to store information relevant to
     the particular foreign table.  The core planner does not touch it except
     to initialize it to NULL when the <literal>RelOptInfo</literal> node is created.
     It is useful for passing information forward from
     <function>GetForeignRelSize</function> to <function>GetForeignPaths</function> and/or
     <function>GetForeignPaths</function> to <function>GetForeignPlan</function>, thereby
     avoiding recalculation.
    </para>

    <para>
     <function>GetForeignPaths</function> can identify the meaning of different
     access paths by storing private information in the
     <structfield>fdw_private</structfield> field of <structname>ForeignPath</structname> nodes.
     <structfield>fdw_private</structfield> is declared as a <type>List</type> pointer, but
     could actually contain anything since the core planner does not touch
     it.  However, best practice is to use a representation that's dumpable
     by <function>nodeToString</function>, for use with debugging

Title: FDW Helper Functions and Query Planning
Summary
This section covers the FDW helper functions for retrieving FDW objects by name and introduces the FDW query planning process. It details the functions `GetForeignDataWrapperByName` and `GetForeignServerByName`, which retrieve FDW objects by name, and the FDW callback functions used during query planning (`GetForeignRelSize`, `GetForeignPaths`, `GetForeignPlan`, etc.). It highlights the use of `root` and `baserel` information to optimize foreign table access and the availability of private fields (`baserel->fdw_private`, `ForeignPath->fdw_private`) for FDW planning functions to store and pass information between planning stages.