Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
fdw-callbacks.html1266 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>59.2. Foreign Data Wrapper Callback Routines</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="fdw-functions.html" title="59.1. Foreign Data Wrapper Functions" /><link rel="next" href="fdw-helpers.html" title="59.3. Foreign Data Wrapper Helper Functions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">59.2. Foreign Data Wrapper Callback Routines</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="fdw-functions.html" title="59.1. Foreign Data Wrapper Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="fdwhandler.html" title="Chapter 59. Writing a Foreign Data Wrapper">Up</a></td><th width="60%" align="center">Chapter 59. Writing a Foreign Data Wrapper</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="fdw-helpers.html" title="59.3. Foreign Data Wrapper Helper Functions">Next</a></td></tr></table><hr /></div><div class="sect1" id="FDW-CALLBACKS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">59.2. Foreign Data Wrapper Callback Routines <a href="#FDW-CALLBACKS" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-SCAN">59.2.1. FDW Routines for Scanning Foreign Tables</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-JOIN-SCAN">59.2.2. FDW Routines for Scanning Foreign Joins</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-UPPER-PLANNING">59.2.3. FDW Routines for Planning Post-Scan/Join Processing</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-UPDATE">59.2.4. FDW Routines for Updating Foreign Tables</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-TRUNCATE">59.2.5. FDW Routines for <code class="command">TRUNCATE</code></a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-ROW-LOCKING">59.2.6. FDW Routines for Row Locking</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-EXPLAIN">59.2.7. FDW Routines for <code class="command">EXPLAIN</code></a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-ANALYZE">59.2.8. FDW Routines for <code class="command">ANALYZE</code></a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-IMPORT">59.2.9. FDW Routines for <code class="command">IMPORT FOREIGN SCHEMA</code></a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-PARALLEL">59.2.10. FDW Routines for Parallel Execution</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-ASYNC">59.2.11. FDW Routines for Asynchronous Execution</a></span></dt><dt><span class="sect2"><a href="fdw-callbacks.html#FDW-CALLBACKS-REPARAMETERIZE-PATHS">59.2.12. FDW Routines for Reparameterization of Paths</a></span></dt></dl></div><p>3     The FDW handler function returns a palloc'd <code class="structname">FdwRoutine</code>4     struct containing pointers to the callback functions described below.5     The scan-related functions are required, the rest are optional.6    </p><p>7     The <code class="structname">FdwRoutine</code> struct type is declared in8     <code class="filename">src/include/foreign/fdwapi.h</code>, which see for additional9     details.10    </p><div class="sect2" id="FDW-CALLBACKS-SCAN"><div class="titlepage"><div><div><h3 class="title">59.2.1. FDW Routines for Scanning Foreign Tables <a href="#FDW-CALLBACKS-SCAN" class="id_link">#</a></h3></div></div></div><p>11</p><pre class="programlisting">12void13GetForeignRelSize(PlannerInfo *root,14                  RelOptInfo *baserel,15                  Oid foreigntableid);16</pre><p>17 18     Obtain relation size estimates for a foreign table.  This is called19     at the beginning of planning for a query that scans a foreign table.20     <code class="literal">root</code> is the planner's global information about the query;21     <code class="literal">baserel</code> is the planner's information about this table; and22     <code class="literal">foreigntableid</code> is the <code class="structname">pg_class</code> OID of the23     foreign table.  (<code class="literal">foreigntableid</code> could be obtained from the24     planner data structures, but it's passed explicitly to save effort.)25    </p><p>26     This function should update <code class="literal">baserel-&gt;rows</code> to be the27     expected number of rows returned by the table scan, after accounting for28     the filtering done by the restriction quals.  The initial value of29     <code class="literal">baserel-&gt;rows</code> is just a constant default estimate, which30     should be replaced if at all possible.  The function may also choose to31     update <code class="literal">baserel-&gt;width</code> if it can compute a better estimate32     of the average result row width.33     (The initial value is based on column data types and on column34     average-width values measured by the last <code class="command">ANALYZE</code>.)35     Also, this function may update <code class="literal">baserel-&gt;tuples</code> if36     it can compute a better estimate of the foreign table's total row count.37     (The initial value is38     from <code class="structname">pg_class</code>.<code class="structfield">reltuples</code>39     which represents the total row count seen by the40     last <code class="command">ANALYZE</code>; it will be <code class="literal">-1</code> if41     no <code class="command">ANALYZE</code> has been done on this foreign table.)42    </p><p>43     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.44    </p><p>45</p><pre class="programlisting">46void47GetForeignPaths(PlannerInfo *root,48                RelOptInfo *baserel,49                Oid foreigntableid);50</pre><p>51 52     Create possible access paths for a scan on a foreign table.53     This is called during query planning.54     The parameters are the same as for <code class="function">GetForeignRelSize</code>,55     which has already been called.56    </p><p>57     This function must generate at least one access path58     (<code class="structname">ForeignPath</code> node) for a scan on the foreign table and59     must call <code class="function">add_path</code> to add each such path to60     <code class="literal">baserel-&gt;pathlist</code>.  It's recommended to use61     <code class="function">create_foreignscan_path</code> to build the62     <code class="structname">ForeignPath</code> nodes.  The function can generate multiple63     access paths, e.g., a path which has valid <code class="literal">pathkeys</code> to64     represent a pre-sorted result.  Each access path must contain cost65     estimates, and can contain any FDW-private information that is needed to66     identify the specific scan method intended.67    </p><p>68     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.69    </p><p>70</p><pre class="programlisting">71ForeignScan *72GetForeignPlan(PlannerInfo *root,73               RelOptInfo *baserel,74               Oid foreigntableid,75               ForeignPath *best_path,76               List *tlist,77               List *scan_clauses,78               Plan *outer_plan);79</pre><p>80 81     Create a <code class="structname">ForeignScan</code> plan node from the selected foreign82     access path.  This is called at the end of query planning.83     The parameters are as for <code class="function">GetForeignRelSize</code>, plus84     the selected <code class="structname">ForeignPath</code> (previously produced by85     <code class="function">GetForeignPaths</code>, <code class="function">GetForeignJoinPaths</code>,86     or <code class="function">GetForeignUpperPaths</code>),87     the target list to be emitted by the plan node,88     the restriction clauses to be enforced by the plan node,89     and the outer subplan of the <code class="structname">ForeignScan</code>,90     which is used for rechecks performed by <code class="function">RecheckForeignScan</code>.91     (If the path is for a join rather than a base92     relation, <code class="literal">foreigntableid</code> is <code class="literal">InvalidOid</code>.)93    </p><p>94     This function must create and return a <code class="structname">ForeignScan</code> plan95     node; it's recommended to use <code class="function">make_foreignscan</code> to build the96     <code class="structname">ForeignScan</code> node.97    </p><p>98     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.99    </p><p>100</p><pre class="programlisting">101void102BeginForeignScan(ForeignScanState *node,103                 int eflags);104</pre><p>105 106     Begin executing a foreign scan. This is called during executor startup.107     It should perform any initialization needed before the scan can start,108     but not start executing the actual scan (that should be done upon the109     first call to <code class="function">IterateForeignScan</code>).110     The <code class="structname">ForeignScanState</code> node has already been created, but111     its <code class="structfield">fdw_state</code> field is still NULL.  Information about112     the table to scan is accessible through the113     <code class="structname">ForeignScanState</code> node (in particular, from the underlying114     <code class="structname">ForeignScan</code> plan node, which contains any FDW-private115     information provided by <code class="function">GetForeignPlan</code>).116     <code class="literal">eflags</code> contains flag bits describing the executor's117     operating mode for this plan node.118    </p><p>119     Note that when <code class="literal">(eflags &amp; EXEC_FLAG_EXPLAIN_ONLY)</code> is120     true, this function should not perform any externally-visible actions;121     it should only do the minimum required to make the node state valid122     for <code class="function">ExplainForeignScan</code> and <code class="function">EndForeignScan</code>.123    </p><p>124</p><pre class="programlisting">125TupleTableSlot *126IterateForeignScan(ForeignScanState *node);127</pre><p>128 129     Fetch one row from the foreign source, returning it in a tuple table slot130     (the node's <code class="structfield">ScanTupleSlot</code> should be used for this131     purpose).  Return NULL if no more rows are available.  The tuple table132     slot infrastructure allows either a physical or virtual tuple to be133     returned; in most cases the latter choice is preferable from a134     performance standpoint.  Note that this is called in a short-lived memory135     context that will be reset between invocations.  Create a memory context136     in <code class="function">BeginForeignScan</code> if you need longer-lived storage, or use137     the <code class="structfield">es_query_cxt</code> of the node's <code class="structname">EState</code>.138    </p><p>139     The rows returned must match the <code class="structfield">fdw_scan_tlist</code> target140     list if one was supplied, otherwise they must match the row type of the141     foreign table being scanned.  If you choose to optimize away fetching142     columns that are not needed, you should insert nulls in those column143     positions, or else generate a <code class="structfield">fdw_scan_tlist</code> list with144     those columns omitted.145    </p><p>146     Note that <span class="productname">PostgreSQL</span>'s executor doesn't care147     whether the rows returned violate any constraints that were defined on148     the foreign table — but the planner does care, and may optimize149     queries incorrectly if there are rows visible in the foreign table that150     do not satisfy a declared constraint.  If a constraint is violated when151     the user has declared that the constraint should hold true, it may be152     appropriate to raise an error (just as you would need to do in the case153     of a data type mismatch).154    </p><p>155</p><pre class="programlisting">156void157ReScanForeignScan(ForeignScanState *node);158</pre><p>159 160     Restart the scan from the beginning.  Note that any parameters the161     scan depends on may have changed value, so the new scan does not162     necessarily return exactly the same rows.163    </p><p>164</p><pre class="programlisting">165void166EndForeignScan(ForeignScanState *node);167</pre><p>168 169     End the scan and release resources.  It is normally not important170     to release palloc'd memory, but for example open files and connections171     to remote servers should be cleaned up.172    </p></div><div class="sect2" id="FDW-CALLBACKS-JOIN-SCAN"><div class="titlepage"><div><div><h3 class="title">59.2.2. FDW Routines for Scanning Foreign Joins <a href="#FDW-CALLBACKS-JOIN-SCAN" class="id_link">#</a></h3></div></div></div><p>173     If an FDW supports performing foreign joins remotely (rather than174     by fetching both tables' data and doing the join locally), it should175     provide this callback function:176    </p><p>177</p><pre class="programlisting">178void179GetForeignJoinPaths(PlannerInfo *root,180                    RelOptInfo *joinrel,181                    RelOptInfo *outerrel,182                    RelOptInfo *innerrel,183                    JoinType jointype,184                    JoinPathExtraData *extra);185</pre><p>186     Create possible access paths for a join of two (or more) foreign tables187     that all belong to the same foreign server.  This optional188     function is called during query planning.  As189     with <code class="function">GetForeignPaths</code>, this function should190     generate <code class="structname">ForeignPath</code> path(s) for the191     supplied <code class="literal">joinrel</code>192     (use <code class="function">create_foreign_join_path</code> to build them),193     and call <code class="function">add_path</code> to add these194     paths to the set of paths considered for the join.  But unlike195     <code class="function">GetForeignPaths</code>, it is not necessary that this function196     succeed in creating at least one path, since paths involving local197     joining are always possible.198    </p><p>199     Note that this function will be invoked repeatedly for the same join200     relation, with different combinations of inner and outer relations; it is201     the responsibility of the FDW to minimize duplicated work.202    </p><p>203     If a <code class="structname">ForeignPath</code> path is chosen for the join, it will204     represent the entire join process; paths generated for the component205     tables and subsidiary joins will not be used.  Subsequent processing of206     the join path proceeds much as it does for a path scanning a single207     foreign table.  One difference is that the <code class="structfield">scanrelid</code> of208     the resulting <code class="structname">ForeignScan</code> plan node should be set to zero,209     since there is no single relation that it represents; instead,210     the <code class="structfield">fs_relids</code> field of the <code class="structname">ForeignScan</code>211     node represents the set of relations that were joined.  (The latter field212     is set up automatically by the core planner code, and need not be filled213     by the FDW.)  Another difference is that, because the column list for a214     remote join cannot be found from the system catalogs, the FDW must215     fill <code class="structfield">fdw_scan_tlist</code> with an appropriate list216     of <code class="structfield">TargetEntry</code> nodes, representing the set of columns217     it will supply at run time in the tuples it returns.218    </p><div class="note"><h3 class="title">Note</h3><p>219      Beginning with <span class="productname">PostgreSQL</span> 16,220      <code class="structfield">fs_relids</code> includes the rangetable indexes221      of outer joins, if any were involved in this join.  The new field222      <code class="structfield">fs_base_relids</code> includes only base223      relation indexes, and thus224      mimics <code class="structfield">fs_relids</code>'s old semantics.225     </p></div><p>226     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.227    </p></div><div class="sect2" id="FDW-CALLBACKS-UPPER-PLANNING"><div class="titlepage"><div><div><h3 class="title">59.2.3. FDW Routines for Planning Post-Scan/Join Processing <a href="#FDW-CALLBACKS-UPPER-PLANNING" class="id_link">#</a></h3></div></div></div><p>228     If an FDW supports performing remote post-scan/join processing, such as229     remote aggregation, it should provide this callback function:230    </p><p>231</p><pre class="programlisting">232void233GetForeignUpperPaths(PlannerInfo *root,234                     UpperRelationKind stage,235                     RelOptInfo *input_rel,236                     RelOptInfo *output_rel,237                     void *extra);238</pre><p>239     Create possible access paths for <em class="firstterm">upper relation</em> processing,240     which is the planner's term for all post-scan/join query processing, such241     as aggregation, window functions, sorting, and table updates.  This242     optional function is called during query planning.  Currently, it is243     called only if all base relation(s) involved in the query belong to the244     same FDW.  This function should generate <code class="structname">ForeignPath</code>245     path(s) for any post-scan/join processing that the FDW knows how to246     perform remotely247     (use <code class="function">create_foreign_upper_path</code> to build them),248     and call <code class="function">add_path</code> to add these paths to249     the indicated upper relation.  As with <code class="function">GetForeignJoinPaths</code>,250     it is not necessary that this function succeed in creating any paths,251     since paths involving local processing are always possible.252    </p><p>253     The <code class="literal">stage</code> parameter identifies which post-scan/join step is254     currently being considered.  <code class="literal">output_rel</code> is the upper relation255     that should receive paths representing computation of this step,256     and <code class="literal">input_rel</code> is the relation representing the input to this257     step.  The <code class="literal">extra</code> parameter provides additional details,258     currently, it is set only for <code class="literal">UPPERREL_PARTIAL_GROUP_AGG</code>259     or <code class="literal">UPPERREL_GROUP_AGG</code>, in which case it points to a260     <code class="literal">GroupPathExtraData</code> structure;261     or for <code class="literal">UPPERREL_FINAL</code>, in which case it points to a262     <code class="literal">FinalPathExtraData</code> structure.263     (Note that <code class="structname">ForeignPath</code> paths added264     to <code class="literal">output_rel</code> would typically not have any direct dependency265     on paths of the <code class="literal">input_rel</code>, since their processing is expected266     to be done externally.  However, examining paths previously generated for267     the previous processing step can be useful to avoid redundant planning268     work.)269    </p><p>270     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.271    </p></div><div class="sect2" id="FDW-CALLBACKS-UPDATE"><div class="titlepage"><div><div><h3 class="title">59.2.4. FDW Routines for Updating Foreign Tables <a href="#FDW-CALLBACKS-UPDATE" class="id_link">#</a></h3></div></div></div><p>272     If an FDW supports writable foreign tables, it should provide273     some or all of the following callback functions depending on274     the needs and capabilities of the FDW:275    </p><p>276</p><pre class="programlisting">277void278AddForeignUpdateTargets(PlannerInfo *root,279                        Index rtindex,280                        RangeTblEntry *target_rte,281                        Relation target_relation);282</pre><p>283 284     <code class="command">UPDATE</code> and <code class="command">DELETE</code> operations are performed285     against rows previously fetched by the table-scanning functions.  The286     FDW may need extra information, such as a row ID or the values of287     primary-key columns, to ensure that it can identify the exact row to288     update or delete.  To support that, this function can add extra hidden,289     or <span class="quote">“<span class="quote">junk</span>”</span>, target columns to the list of columns that are to be290     retrieved from the foreign table during an <code class="command">UPDATE</code> or291     <code class="command">DELETE</code>.292    </p><p>293     To do that, construct a <code class="structname">Var</code> representing294     an extra value you need, and pass it295     to <code class="function">add_row_identity_var</code>, along with a name for296     the junk column.  (You can do this more than once if several columns297     are needed.)  You must choose a distinct junk column name for each298     different <code class="structname">Var</code> you need, except299     that <code class="structname">Var</code>s that are identical except for300     the <code class="structfield">varno</code> field can and should share a301     column name.302     The core system uses the junk column names303     <code class="literal">tableoid</code> for a304     table's <code class="structfield">tableoid</code> column,305     <code class="literal">ctid</code>306     or <code class="literal">ctid<em class="replaceable"><code>N</code></em></code>307     for <code class="structfield">ctid</code>,308     <code class="literal">wholerow</code>309     for a whole-row <code class="structname">Var</code> marked with310     <code class="structfield">vartype</code> = <code class="type">RECORD</code>,311     and <code class="literal">wholerow<em class="replaceable"><code>N</code></em></code>312     for a whole-row <code class="structname">Var</code> with313     <code class="structfield">vartype</code> equal to the table's declared row type.314     Re-use these names when you can (the planner will combine duplicate315     requests for identical junk columns).  If you need another kind of316     junk column besides these, it might be wise to choose a name prefixed317     with your extension name, to avoid conflicts against other FDWs.318    </p><p>319     If the <code class="function">AddForeignUpdateTargets</code> pointer is set to320     <code class="literal">NULL</code>, no extra target expressions are added.321     (This will make it impossible to implement <code class="command">DELETE</code>322     operations, though <code class="command">UPDATE</code> may still be feasible if the FDW323     relies on an unchanging primary key to identify rows.)324    </p><p>325</p><pre class="programlisting">326List *327PlanForeignModify(PlannerInfo *root,328                  ModifyTable *plan,329                  Index resultRelation,330                  int subplan_index);331</pre><p>332 333     Perform any additional planning actions needed for an insert, update, or334     delete on a foreign table.  This function generates the FDW-private335     information that will be attached to the <code class="structname">ModifyTable</code> plan336     node that performs the update action.  This private information must337     have the form of a <code class="literal">List</code>, and will be delivered to338     <code class="function">BeginForeignModify</code> during the execution stage.339    </p><p>340     <code class="literal">root</code> is the planner's global information about the query.341     <code class="literal">plan</code> is the <code class="structname">ModifyTable</code> plan node, which is342     complete except for the <code class="structfield">fdwPrivLists</code> field.343     <code class="literal">resultRelation</code> identifies the target foreign table by its344     range table index.  <code class="literal">subplan_index</code> identifies which target of345     the <code class="structname">ModifyTable</code> plan node this is, counting from zero;346     use this if you want to index into per-target-relation substructures of the347     <code class="literal">plan</code> node.348    </p><p>349     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.350    </p><p>351     If the <code class="function">PlanForeignModify</code> pointer is set to352     <code class="literal">NULL</code>, no additional plan-time actions are taken, and the353     <code class="literal">fdw_private</code> list delivered to354     <code class="function">BeginForeignModify</code> will be NIL.355    </p><p>356</p><pre class="programlisting">357void358BeginForeignModify(ModifyTableState *mtstate,359                   ResultRelInfo *rinfo,360                   List *fdw_private,361                   int subplan_index,362                   int eflags);363</pre><p>364 365     Begin executing a foreign table modification operation.  This routine is366     called during executor startup.  It should perform any initialization367     needed prior to the actual table modifications.  Subsequently,368     <code class="function">ExecForeignInsert/ExecForeignBatchInsert</code>,369     <code class="function">ExecForeignUpdate</code> or370     <code class="function">ExecForeignDelete</code> will be called for tuple(s) to be371     inserted, updated, or deleted.372    </p><p>373     <code class="literal">mtstate</code> is the overall state of the374     <code class="structname">ModifyTable</code> plan node being executed; global data about375     the plan and execution state is available via this structure.376     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing377     the target foreign table.  (The <code class="structfield">ri_FdwState</code> field of378     <code class="structname">ResultRelInfo</code> is available for the FDW to store any379     private state it needs for this operation.)380     <code class="literal">fdw_private</code> contains the private data generated by381     <code class="function">PlanForeignModify</code>, if any.382     <code class="literal">subplan_index</code> identifies which target of383     the <code class="structname">ModifyTable</code> plan node this is.384     <code class="literal">eflags</code> contains flag bits describing the executor's385     operating mode for this plan node.386    </p><p>387     Note that when <code class="literal">(eflags &amp; EXEC_FLAG_EXPLAIN_ONLY)</code> is388     true, this function should not perform any externally-visible actions;389     it should only do the minimum required to make the node state valid390     for <code class="function">ExplainForeignModify</code> and <code class="function">EndForeignModify</code>.391    </p><p>392     If the <code class="function">BeginForeignModify</code> pointer is set to393     <code class="literal">NULL</code>, no action is taken during executor startup.394    </p><p>395</p><pre class="programlisting">396TupleTableSlot *397ExecForeignInsert(EState *estate,398                  ResultRelInfo *rinfo,399                  TupleTableSlot *slot,400                  TupleTableSlot *planSlot);401</pre><p>402 403     Insert one tuple into the foreign table.404     <code class="literal">estate</code> is global execution state for the query.405     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing406     the target foreign table.407     <code class="literal">slot</code> contains the tuple to be inserted; it will match the408     row-type definition of the foreign table.409     <code class="literal">planSlot</code> contains the tuple that was generated by the410     <code class="structname">ModifyTable</code> plan node's subplan; it differs from411     <code class="literal">slot</code> in possibly containing additional <span class="quote">“<span class="quote">junk</span>”</span>412     columns.  (The <code class="literal">planSlot</code> is typically of little interest413     for <code class="command">INSERT</code> cases, but is provided for completeness.)414    </p><p>415     The return value is either a slot containing the data that was actually416     inserted (this might differ from the data supplied, for example as a417     result of trigger actions), or NULL if no row was actually inserted418     (again, typically as a result of triggers).  The passed-in419     <code class="literal">slot</code> can be re-used for this purpose.420    </p><p>421     The data in the returned slot is used only if the <code class="command">INSERT</code>422     statement has a <code class="literal">RETURNING</code> clause or involves a view423     <code class="literal">WITH CHECK OPTION</code>; or if the foreign table has424     an <code class="literal">AFTER ROW</code> trigger.  Triggers require all columns,425     but the FDW could choose to optimize away returning some or all columns426     depending on the contents of the <code class="literal">RETURNING</code> clause or427     <code class="literal">WITH CHECK OPTION</code> constraints.  Regardless, some slot428     must be returned to indicate success, or the query's reported row count429     will be wrong.430    </p><p>431     If the <code class="function">ExecForeignInsert</code> pointer is set to432     <code class="literal">NULL</code>, attempts to insert into the foreign table will fail433     with an error message.434    </p><p>435     Note that this function is also called when inserting routed tuples into436     a foreign-table partition or executing <code class="command">COPY FROM</code> on437     a foreign table, in which case it is called in a different way than it438     is in the <code class="command">INSERT</code> case.  See the callback functions439     described below that allow the FDW to support that.440    </p><p>441</p><pre class="programlisting">442TupleTableSlot **443ExecForeignBatchInsert(EState *estate,444                       ResultRelInfo *rinfo,445                       TupleTableSlot **slots,446                       TupleTableSlot **planSlots,447                       int *numSlots);448</pre><p>449 450     Insert multiple tuples in bulk into the foreign table.451     The parameters are the same for <code class="function">ExecForeignInsert</code>452     except <code class="literal">slots</code> and <code class="literal">planSlots</code> contain453     multiple tuples and <code class="literal">*numSlots</code> specifies the number of454     tuples in those arrays.455    </p><p>456     The return value is an array of slots containing the data that was457     actually inserted (this might differ from the data supplied, for458     example as a result of trigger actions.)459     The passed-in <code class="literal">slots</code> can be re-used for this purpose.460     The number of successfully inserted tuples is returned in461     <code class="literal">*numSlots</code>.462    </p><p>463     The data in the returned slot is used only if the <code class="command">INSERT</code>464     statement involves a view465     <code class="literal">WITH CHECK OPTION</code>; or if the foreign table has466     an <code class="literal">AFTER ROW</code> trigger.  Triggers require all columns,467     but the FDW could choose to optimize away returning some or all columns468     depending on the contents of the469     <code class="literal">WITH CHECK OPTION</code> constraints.470    </p><p>471     If the <code class="function">ExecForeignBatchInsert</code> or472     <code class="function">GetForeignModifyBatchSize</code> pointer is set to473     <code class="literal">NULL</code>, attempts to insert into the foreign table will474     use <code class="function">ExecForeignInsert</code>.475     This function is not used if the <code class="command">INSERT</code> has the476     <code class="literal">RETURNING</code> clause.477    </p><p>478     Note that this function is also called when inserting routed tuples into479     a foreign-table partition or executing <code class="command">COPY FROM</code> on480     a foreign table, in which case it is called in a different way than it481     is in the <code class="command">INSERT</code> case.  See the callback functions482     described below that allow the FDW to support that.483    </p><p>484</p><pre class="programlisting">485int486GetForeignModifyBatchSize(ResultRelInfo *rinfo);487</pre><p>488 489     Report the maximum number of tuples that a single490     <code class="function">ExecForeignBatchInsert</code> call can handle for491     the specified foreign table.  The executor passes at most492     the given number of tuples to <code class="function">ExecForeignBatchInsert</code>.493     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing494     the target foreign table.495     The FDW is expected to provide a foreign server and/or foreign496     table option for the user to set this value, or some hard-coded value.497    </p><p>498     If the <code class="function">ExecForeignBatchInsert</code> or499     <code class="function">GetForeignModifyBatchSize</code> pointer is set to500     <code class="literal">NULL</code>, attempts to insert into the foreign table will501     use <code class="function">ExecForeignInsert</code>.502    </p><p>503</p><pre class="programlisting">504TupleTableSlot *505ExecForeignUpdate(EState *estate,506                  ResultRelInfo *rinfo,507                  TupleTableSlot *slot,508                  TupleTableSlot *planSlot);509</pre><p>510 511     Update one tuple in the foreign table.512     <code class="literal">estate</code> is global execution state for the query.513     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing514     the target foreign table.515     <code class="literal">slot</code> contains the new data for the tuple; it will match the516     row-type definition of the foreign table.517     <code class="literal">planSlot</code> contains the tuple that was generated by the518     <code class="structname">ModifyTable</code> plan node's subplan.  Unlike519     <code class="literal">slot</code>, this tuple contains only the new values for520     columns changed by the query, so do not rely on attribute numbers of the521     foreign table to index into <code class="literal">planSlot</code>.522     Also, <code class="literal">planSlot</code> typically contains523     additional <span class="quote">“<span class="quote">junk</span>”</span> columns.  In particular, any junk columns524     that were requested by <code class="function">AddForeignUpdateTargets</code> will525     be available from this slot.526    </p><p>527     The return value is either a slot containing the row as it was actually528     updated (this might differ from the data supplied, for example as a529     result of trigger actions), or NULL if no row was actually updated530     (again, typically as a result of triggers).  The passed-in531     <code class="literal">slot</code> can be re-used for this purpose.532    </p><p>533     The data in the returned slot is used only if the <code class="command">UPDATE</code>534     statement has a <code class="literal">RETURNING</code> clause or involves a view535     <code class="literal">WITH CHECK OPTION</code>; or if the foreign table has536     an <code class="literal">AFTER ROW</code> trigger.  Triggers require all columns,537     but the FDW could choose to optimize away returning some or all columns538     depending on the contents of the <code class="literal">RETURNING</code> clause or539     <code class="literal">WITH CHECK OPTION</code> constraints.  Regardless, some slot540     must be returned to indicate success, or the query's reported row count541     will be wrong.542    </p><p>543     If the <code class="function">ExecForeignUpdate</code> pointer is set to544     <code class="literal">NULL</code>, attempts to update the foreign table will fail545     with an error message.546    </p><p>547</p><pre class="programlisting">548TupleTableSlot *549ExecForeignDelete(EState *estate,550                  ResultRelInfo *rinfo,551                  TupleTableSlot *slot,552                  TupleTableSlot *planSlot);553</pre><p>554 555     Delete one tuple from the foreign table.556     <code class="literal">estate</code> is global execution state for the query.557     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing558     the target foreign table.559     <code class="literal">slot</code> contains nothing useful upon call, but can be used to560     hold the returned tuple.561     <code class="literal">planSlot</code> contains the tuple that was generated by the562     <code class="structname">ModifyTable</code> plan node's subplan; in particular, it will563     carry any junk columns that were requested by564     <code class="function">AddForeignUpdateTargets</code>.  The junk column(s) must be used565     to identify the tuple to be deleted.566    </p><p>567     The return value is either a slot containing the row that was deleted,568     or NULL if no row was deleted (typically as a result of triggers).  The569     passed-in <code class="literal">slot</code> can be used to hold the tuple to be returned.570    </p><p>571     The data in the returned slot is used only if the <code class="command">DELETE</code>572     query has a <code class="literal">RETURNING</code> clause or the foreign table has573     an <code class="literal">AFTER ROW</code> trigger.  Triggers require all columns, but the574     FDW could choose to optimize away returning some or all columns depending575     on the contents of the <code class="literal">RETURNING</code> clause.  Regardless, some576     slot must be returned to indicate success, or the query's reported row577     count will be wrong.578    </p><p>579     If the <code class="function">ExecForeignDelete</code> pointer is set to580     <code class="literal">NULL</code>, attempts to delete from the foreign table will fail581     with an error message.582    </p><p>583</p><pre class="programlisting">584void585EndForeignModify(EState *estate,586                 ResultRelInfo *rinfo);587</pre><p>588 589     End the table update and release resources.  It is normally not important590     to release palloc'd memory, but for example open files and connections591     to remote servers should be cleaned up.592    </p><p>593     If the <code class="function">EndForeignModify</code> pointer is set to594     <code class="literal">NULL</code>, no action is taken during executor shutdown.595    </p><p>596     Tuples inserted into a partitioned table by <code class="command">INSERT</code> or597     <code class="command">COPY FROM</code> are routed to partitions.  If an FDW598     supports routable foreign-table partitions, it should also provide the599     following callback functions.  These functions are also called when600     <code class="command">COPY FROM</code> is executed on a foreign table.601    </p><p>602</p><pre class="programlisting">603void604BeginForeignInsert(ModifyTableState *mtstate,605                   ResultRelInfo *rinfo);606</pre><p>607 608     Begin executing an insert operation on a foreign table.  This routine is609     called right before the first tuple is inserted into the foreign table610     in both cases when it is the partition chosen for tuple routing and the611     target specified in a <code class="command">COPY FROM</code> command.  It should612     perform any initialization needed prior to the actual insertion.613     Subsequently, <code class="function">ExecForeignInsert</code> or614     <code class="function">ExecForeignBatchInsert</code> will be called for615     tuple(s) to be inserted into the foreign table.616    </p><p>617     <code class="literal">mtstate</code> is the overall state of the618     <code class="structname">ModifyTable</code> plan node being executed; global data about619     the plan and execution state is available via this structure.620     <code class="literal">rinfo</code> is the <code class="structname">ResultRelInfo</code> struct describing621     the target foreign table.  (The <code class="structfield">ri_FdwState</code> field of622     <code class="structname">ResultRelInfo</code> is available for the FDW to store any623     private state it needs for this operation.)624    </p><p>625     When this is called by a <code class="command">COPY FROM</code> command, the626     plan-related global data in <code class="literal">mtstate</code> is not provided627     and the <code class="literal">planSlot</code> parameter of628     <code class="function">ExecForeignInsert</code> subsequently called for each629     inserted tuple is <code class="literal">NULL</code>, whether the foreign table is630     the partition chosen for tuple routing or the target specified in the631     command.632    </p><p>633     If the <code class="function">BeginForeignInsert</code> pointer is set to634     <code class="literal">NULL</code>, no action is taken for the initialization.635    </p><p>636     Note that if the FDW does not support routable foreign-table partitions637     and/or executing <code class="command">COPY FROM</code> on foreign tables, this638     function or <code class="function">ExecForeignInsert/ExecForeignBatchInsert</code>639     subsequently called must throw error as needed.640    </p><p>641</p><pre class="programlisting">642void643EndForeignInsert(EState *estate,644                 ResultRelInfo *rinfo);645</pre><p>646 647     End the insert operation and release resources.  It is normally not important648     to release palloc'd memory, but for example open files and connections649     to remote servers should be cleaned up.650    </p><p>651     If the <code class="function">EndForeignInsert</code> pointer is set to652     <code class="literal">NULL</code>, no action is taken for the termination.653    </p><p>654</p><pre class="programlisting">655int656IsForeignRelUpdatable(Relation rel);657</pre><p>658 659     Report which update operations the specified foreign table supports.660     The return value should be a bit mask of rule event numbers indicating661     which operations are supported by the foreign table, using the662     <code class="literal">CmdType</code> enumeration; that is,663     <code class="literal">(1 &lt;&lt; CMD_UPDATE) = 4</code> for <code class="command">UPDATE</code>,664     <code class="literal">(1 &lt;&lt; CMD_INSERT) = 8</code> for <code class="command">INSERT</code>, and665     <code class="literal">(1 &lt;&lt; CMD_DELETE) = 16</code> for <code class="command">DELETE</code>.666    </p><p>667     If the <code class="function">IsForeignRelUpdatable</code> pointer is set to668     <code class="literal">NULL</code>, foreign tables are assumed to be insertable, updatable,669     or deletable if the FDW provides <code class="function">ExecForeignInsert</code>,670     <code class="function">ExecForeignUpdate</code>, or <code class="function">ExecForeignDelete</code>671     respectively.  This function is only needed if the FDW supports some672     tables that are updatable and some that are not.  (Even then, it's673     permissible to throw an error in the execution routine instead of674     checking in this function.  However, this function is used to determine675     updatability for display in the <code class="literal">information_schema</code> views.)676    </p><p>677     Some inserts, updates, and deletes to foreign tables can be optimized678     by implementing an alternative set of interfaces.  The ordinary679     interfaces for inserts, updates, and deletes fetch rows from the remote680     server and then modify those rows one at a time.  In some cases, this681     row-by-row approach is necessary, but it can be inefficient.  If it is682     possible for the foreign server to determine which rows should be683     modified without actually retrieving them, and if there are no local684     structures which would affect the operation (row-level local triggers,685     stored generated columns, or <code class="literal">WITH CHECK OPTION</code>686     constraints from parent views), then it is possible to arrange things687     so that the entire operation is performed on the remote server.  The688     interfaces described below make this possible.689    </p><p>690</p><pre class="programlisting">691bool692PlanDirectModify(PlannerInfo *root,693                 ModifyTable *plan,694                 Index resultRelation,695                 int subplan_index);696</pre><p>697 698     Decide whether it is safe to execute a direct modification699     on the remote server.  If so, return <code class="literal">true</code> after performing700     planning actions needed for that.  Otherwise, return <code class="literal">false</code>.701     This optional function is called during query planning.702     If this function succeeds, <code class="function">BeginDirectModify</code>,703     <code class="function">IterateDirectModify</code> and <code class="function">EndDirectModify</code> will704     be called at the execution stage, instead.  Otherwise, the table705     modification will be executed using the table-updating functions706     described above.707     The parameters are the same as for <code class="function">PlanForeignModify</code>.708    </p><p>709     To execute the direct modification on the remote server, this function710     must rewrite the target subplan with a <code class="structname">ForeignScan</code> plan711     node that executes the direct modification on the remote server.  The712     <code class="structfield">operation</code> and <code class="structfield">resultRelation</code> fields713     of the <code class="structname">ForeignScan</code> must be set appropriately.714     <code class="structfield">operation</code> must be set to the <code class="literal">CmdType</code>715     enumeration corresponding to the statement kind (that is,716     <code class="literal">CMD_UPDATE</code> for <code class="command">UPDATE</code>,717     <code class="literal">CMD_INSERT</code> for <code class="command">INSERT</code>, and718     <code class="literal">CMD_DELETE</code> for <code class="command">DELETE</code>), and the719     <code class="literal">resultRelation</code> argument must be copied to the720     <code class="structfield">resultRelation</code> field.721    </p><p>722     See <a class="xref" href="fdw-planning.html" title="59.4. Foreign Data Wrapper Query Planning">Section 59.4</a> for additional information.723    </p><p>724     If the <code class="function">PlanDirectModify</code> pointer is set to725     <code class="literal">NULL</code>, no attempts to execute a direct modification on the726     remote server are taken.727    </p><p>728</p><pre class="programlisting">729void730BeginDirectModify(ForeignScanState *node,731                  int eflags);732</pre><p>733 734     Prepare to execute a direct modification on the remote server.735     This is called during executor startup.  It should perform any736     initialization needed prior to the direct modification (that should be737     done upon the first call to <code class="function">IterateDirectModify</code>).738     The <code class="structname">ForeignScanState</code> node has already been created, but739     its <code class="structfield">fdw_state</code> field is still NULL.  Information about740     the table to modify is accessible through the741     <code class="structname">ForeignScanState</code> node (in particular, from the underlying742     <code class="structname">ForeignScan</code> plan node, which contains any FDW-private743     information provided by <code class="function">PlanDirectModify</code>).744     <code class="literal">eflags</code> contains flag bits describing the executor's745     operating mode for this plan node.746    </p><p>747     Note that when <code class="literal">(eflags &amp; EXEC_FLAG_EXPLAIN_ONLY)</code> is748     true, this function should not perform any externally-visible actions;749     it should only do the minimum required to make the node state valid750     for <code class="function">ExplainDirectModify</code> and <code class="function">EndDirectModify</code>.751    </p><p>752     If the <code class="function">BeginDirectModify</code> pointer is set to753     <code class="literal">NULL</code>, no attempts to execute a direct modification on the754     remote server are taken.755    </p><p>756</p><pre class="programlisting">757TupleTableSlot *758IterateDirectModify(ForeignScanState *node);759</pre><p>760 761     When the <code class="command">INSERT</code>, <code class="command">UPDATE</code> or <code class="command">DELETE</code>762     query doesn't have a <code class="literal">RETURNING</code> clause, just return NULL763     after a direct modification on the remote server.764     When the query has the clause, fetch one result containing the data765     needed for the <code class="literal">RETURNING</code> calculation, returning it in a766     tuple table slot (the node's <code class="structfield">ScanTupleSlot</code> should be767     used for this purpose).  The data that was actually inserted, updated768     or deleted must be stored in769     <code class="literal">node-&gt;resultRelInfo-&gt;ri_projectReturning-&gt;pi_exprContext-&gt;ecxt_scantuple</code>.770     Return NULL if no more rows are available.771     Note that this is called in a short-lived memory context that will be772     reset between invocations.  Create a memory context in773     <code class="function">BeginDirectModify</code> if you need longer-lived storage, or use774     the <code class="structfield">es_query_cxt</code> of the node's <code class="structname">EState</code>.775    </p><p>776     The rows returned must match the <code class="structfield">fdw_scan_tlist</code> target777     list if one was supplied, otherwise they must match the row type of the778     foreign table being updated.  If you choose to optimize away fetching779     columns that are not needed for the <code class="literal">RETURNING</code> calculation,780     you should insert nulls in those column positions, or else generate a781     <code class="structfield">fdw_scan_tlist</code> list with those columns omitted.782    </p><p>783     Whether the query has the clause or not, the query's reported row count784     must be incremented by the FDW itself.  When the query doesn't have the785     clause, the FDW must also increment the row count for the786     <code class="structname">ForeignScanState</code> node in the <code class="command">EXPLAIN ANALYZE</code>787     case.788    </p><p>789     If the <code class="function">IterateDirectModify</code> pointer is set to790     <code class="literal">NULL</code>, no attempts to execute a direct modification on the791     remote server are taken.792    </p><p>793</p><pre class="programlisting">794void795EndDirectModify(ForeignScanState *node);796</pre><p>797 798     Clean up following a direct modification on the remote server.  It is799     normally not important to release palloc'd memory, but for example open800     files and connections to the remote server should be cleaned up.801    </p><p>802     If the <code class="function">EndDirectModify</code> pointer is set to803     <code class="literal">NULL</code>, no attempts to execute a direct modification on the804     remote server are taken.805    </p></div><div class="sect2" id="FDW-CALLBACKS-TRUNCATE"><div class="titlepage"><div><div><h3 class="title">59.2.5. FDW Routines for <code class="command">TRUNCATE</code> <a href="#FDW-CALLBACKS-TRUNCATE" class="id_link">#</a></h3></div></div></div><p>806</p><pre class="programlisting">807void808ExecForeignTruncate(List *rels,809                    DropBehavior behavior,810                    bool restart_seqs);811</pre><p>812 813     Truncate foreign tables.  This function is called when814     <a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a> is executed on a foreign table.815     <code class="literal">rels</code> is a list of <code class="structname">Relation</code>816     data structures of foreign tables to truncate.817    </p><p>818     <code class="literal">behavior</code> is either <code class="literal">DROP_RESTRICT</code>819     or <code class="literal">DROP_CASCADE</code> indicating that the820     <code class="literal">RESTRICT</code> or <code class="literal">CASCADE</code> option was821     requested in the original <code class="command">TRUNCATE</code> command,822     respectively.823    </p><p>824     If <code class="literal">restart_seqs</code> is <code class="literal">true</code>,825     the original <code class="command">TRUNCATE</code> command requested the826     <code class="literal">RESTART IDENTITY</code> behavior, otherwise the827     <code class="literal">CONTINUE IDENTITY</code> behavior was requested.828    </p><p>829     Note that the <code class="literal">ONLY</code> options specified830     in the original <code class="command">TRUNCATE</code> command are not passed to831     <code class="function">ExecForeignTruncate</code>.  This behavior is similar to832     the callback functions of <code class="command">SELECT</code>,833     <code class="command">UPDATE</code> and <code class="command">DELETE</code> on834     a foreign table.835    </p><p>836     <code class="function">ExecForeignTruncate</code> is invoked once per837     foreign server for which foreign tables are to be truncated.838     This means that all foreign tables included in <code class="literal">rels</code>839     must belong to the same server.840    </p><p>841     If the <code class="function">ExecForeignTruncate</code> pointer is set to842     <code class="literal">NULL</code>, attempts to truncate foreign tables will843     fail with an error message.844    </p></div><div class="sect2" id="FDW-CALLBACKS-ROW-LOCKING"><div class="titlepage"><div><div><h3 class="title">59.2.6. FDW Routines for Row Locking <a href="#FDW-CALLBACKS-ROW-LOCKING" class="id_link">#</a></h3></div></div></div><p>845     If an FDW wishes to support <em class="firstterm">late row locking</em> (as described846     in <a class="xref" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers">Section 59.5</a>), it must provide the following847     callback functions:848    </p><p>849</p><pre class="programlisting">850RowMarkType851GetForeignRowMarkType(RangeTblEntry *rte,852                      LockClauseStrength strength);853</pre><p>854 855     Report which row-marking option to use for a foreign table.856     <code class="literal">rte</code> is the <code class="structname">RangeTblEntry</code> node for the table857     and <code class="literal">strength</code> describes the lock strength requested by the858     relevant <code class="literal">FOR UPDATE/SHARE</code> clause, if any.  The result must be859     a member of the <code class="literal">RowMarkType</code> enum type.860    </p><p>861     This function is called during query planning for each foreign table that862     appears in an <code class="command">UPDATE</code>, <code class="command">DELETE</code>, or <code class="command">SELECT863     FOR UPDATE/SHARE</code> query and is not the target of <code class="command">UPDATE</code>864     or <code class="command">DELETE</code>.865    </p><p>866     If the <code class="function">GetForeignRowMarkType</code> pointer is set to867     <code class="literal">NULL</code>, the <code class="literal">ROW_MARK_COPY</code> option is always used.868     (This implies that <code class="function">RefetchForeignRow</code> will never be called,869     so it need not be provided either.)870    </p><p>871     See <a class="xref" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers">Section 59.5</a> for more information.872    </p><p>873</p><pre class="programlisting">874void875RefetchForeignRow(EState *estate,876                  ExecRowMark *erm,877                  Datum rowid,878                  TupleTableSlot *slot,879                  bool *updated);880</pre><p>881 882     Re-fetch one tuple slot from the foreign table, after locking it if required.883     <code class="literal">estate</code> is global execution state for the query.884     <code class="literal">erm</code> is the <code class="structname">ExecRowMark</code> struct describing885     the target foreign table and the row lock type (if any) to acquire.886     <code class="literal">rowid</code> identifies the tuple to be fetched.887     <code class="literal">slot</code> contains nothing useful upon call, but can be used to888     hold the returned tuple. <code class="literal">updated</code> is an output parameter.889    </p><p>890     This function should store the tuple into the provided slot, or clear it if891     the row lock couldn't be obtained.  The row lock type to acquire is892     defined by <code class="literal">erm-&gt;markType</code>, which is the value893     previously returned by <code class="function">GetForeignRowMarkType</code>.894     (<code class="literal">ROW_MARK_REFERENCE</code> means to just re-fetch the tuple895     without acquiring any lock, and <code class="literal">ROW_MARK_COPY</code> will896     never be seen by this routine.)897    </p><p>898     In addition, <code class="literal">*updated</code> should be set to <code class="literal">true</code>899     if what was fetched was an updated version of the tuple rather than900     the same version previously obtained.  (If the FDW cannot be sure about901     this, always returning <code class="literal">true</code> is recommended.)902    </p><p>903     Note that by default, failure to acquire a row lock should result in904     raising an error; returning with an empty slot is only appropriate if905     the <code class="literal">SKIP LOCKED</code> option is specified906     by <code class="literal">erm-&gt;waitPolicy</code>.907    </p><p>908     The <code class="literal">rowid</code> is the <code class="structfield">ctid</code> value previously read909     for the row to be re-fetched.  Although the <code class="literal">rowid</code> value is910     passed as a <code class="type">Datum</code>, it can currently only be a <code class="type">tid</code>.  The911     function API is chosen in hopes that it may be possible to allow other912     data types for row IDs in future.913    </p><p>914     If the <code class="function">RefetchForeignRow</code> pointer is set to915     <code class="literal">NULL</code>, attempts to re-fetch rows will fail916     with an error message.917    </p><p>918     See <a class="xref" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers">Section 59.5</a> for more information.919    </p><p>920</p><pre class="programlisting">921bool922RecheckForeignScan(ForeignScanState *node,923                   TupleTableSlot *slot);924</pre><p>925     Recheck that a previously-returned tuple still matches the relevant926     scan and join qualifiers, and possibly provide a modified version of927     the tuple.  For foreign data wrappers which do not perform join pushdown,928     it will typically be more convenient to set this to <code class="literal">NULL</code> and929     instead set <code class="structfield">fdw_recheck_quals</code> appropriately.930     When outer joins are pushed down, however, it isn't sufficient to931     reapply the checks relevant to all the base tables to the result tuple,932     even if all needed attributes are present, because failure to match some933     qualifier might result in some attributes going to NULL, rather than in934     no tuple being returned.  <code class="literal">RecheckForeignScan</code> can recheck935     qualifiers and return true if they are still satisfied and false936     otherwise, but it can also store a replacement tuple into the supplied937     slot.938    </p><p>939     To implement join pushdown, a foreign data wrapper will typically940     construct an alternative local join plan which is used only for941     rechecks; this will become the outer subplan of the942     <code class="literal">ForeignScan</code>.  When a recheck is required, this subplan943     can be executed and the resulting tuple can be stored in the slot.944     This plan need not be efficient since no base table will return more945     than one row; for example, it may implement all joins as nested loops.946     The function <code class="literal">GetExistingLocalJoinPath</code> may be used to search947     existing paths for a suitable local join path, which can be used as the948     alternative local join plan.  <code class="literal">GetExistingLocalJoinPath</code>949     searches for an unparameterized path in the path list of the specified950     join relation.  (If it does not find such a path, it returns NULL, in951     which case a foreign data wrapper may build the local path by itself or952     may choose not to create access paths for that join.)953    </p></div><div class="sect2" id="FDW-CALLBACKS-EXPLAIN"><div class="titlepage"><div><div><h3 class="title">59.2.7. FDW Routines for <code class="command">EXPLAIN</code> <a href="#FDW-CALLBACKS-EXPLAIN" class="id_link">#</a></h3></div></div></div><p>954</p><pre class="programlisting">955void956ExplainForeignScan(ForeignScanState *node,957                   ExplainState *es);958</pre><p>959 960     Print additional <code class="command">EXPLAIN</code> output for a foreign table scan.961     This function can call <code class="function">ExplainPropertyText</code> and962     related functions to add fields to the <code class="command">EXPLAIN</code> output.963     The flag fields in <code class="literal">es</code> can be used to determine what to964     print, and the state of the <code class="structname">ForeignScanState</code> node965     can be inspected to provide run-time statistics in the <code class="command">EXPLAIN966     ANALYZE</code> case.967    </p><p>968     If the <code class="function">ExplainForeignScan</code> pointer is set to969     <code class="literal">NULL</code>, no additional information is printed during970     <code class="command">EXPLAIN</code>.971    </p><p>972</p><pre class="programlisting">973void974ExplainForeignModify(ModifyTableState *mtstate,975                     ResultRelInfo *rinfo,976                     List *fdw_private,977                     int subplan_index,978                     struct ExplainState *es);979</pre><p>980 981     Print additional <code class="command">EXPLAIN</code> output for a foreign table update.982     This function can call <code class="function">ExplainPropertyText</code> and983     related functions to add fields to the <code class="command">EXPLAIN</code> output.984     The flag fields in <code class="literal">es</code> can be used to determine what to985     print, and the state of the <code class="structname">ModifyTableState</code> node986     can be inspected to provide run-time statistics in the <code class="command">EXPLAIN987     ANALYZE</code> case.  The first four arguments are the same as for988     <code class="function">BeginForeignModify</code>.989    </p><p>990     If the <code class="function">ExplainForeignModify</code> pointer is set to991     <code class="literal">NULL</code>, no additional information is printed during992     <code class="command">EXPLAIN</code>.993    </p><p>994</p><pre class="programlisting">995void996ExplainDirectModify(ForeignScanState *node,997                    ExplainState *es);998</pre><p>999 1000     Print additional <code class="command">EXPLAIN</code> output for a direct modification1001     on the remote server.1002     This function can call <code class="function">ExplainPropertyText</code> and1003     related functions to add fields to the <code class="command">EXPLAIN</code> output.1004     The flag fields in <code class="literal">es</code> can be used to determine what to1005     print, and the state of the <code class="structname">ForeignScanState</code> node1006     can be inspected to provide run-time statistics in the <code class="command">EXPLAIN1007     ANALYZE</code> case.1008    </p><p>1009     If the <code class="function">ExplainDirectModify</code> pointer is set to1010     <code class="literal">NULL</code>, no additional information is printed during1011     <code class="command">EXPLAIN</code>.1012    </p></div><div class="sect2" id="FDW-CALLBACKS-ANALYZE"><div class="titlepage"><div><div><h3 class="title">59.2.8. FDW Routines for <code class="command">ANALYZE</code> <a href="#FDW-CALLBACKS-ANALYZE" class="id_link">#</a></h3></div></div></div><p>1013</p><pre class="programlisting">1014bool1015AnalyzeForeignTable(Relation relation,1016                    AcquireSampleRowsFunc *func,1017                    BlockNumber *totalpages);1018</pre><p>1019 1020     This function is called when <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> is executed on1021     a foreign table.  If the FDW can collect statistics for this1022     foreign table, it should return <code class="literal">true</code>, and provide a pointer1023     to a function that will collect sample rows from the table in1024     <em class="parameter"><code>func</code></em>, plus the estimated size of the table in pages in1025     <em class="parameter"><code>totalpages</code></em>.  Otherwise, return <code class="literal">false</code>.1026    </p><p>1027     If the FDW does not support collecting statistics for any tables, the1028     <code class="function">AnalyzeForeignTable</code> pointer can be set to <code class="literal">NULL</code>.1029    </p><p>1030     If provided, the sample collection function must have the signature1031</p><pre class="programlisting">1032int1033AcquireSampleRowsFunc(Relation relation,1034                      int elevel,1035                      HeapTuple *rows,1036                      int targrows,1037                      double *totalrows,1038                      double *totaldeadrows);1039</pre><p>1040 1041     A random sample of up to <em class="parameter"><code>targrows</code></em> rows should be collected1042     from the table and stored into the caller-provided <em class="parameter"><code>rows</code></em>1043     array.  The actual number of rows collected must be returned.  In1044     addition, store estimates of the total numbers of live and dead rows in1045     the table into the output parameters <em class="parameter"><code>totalrows</code></em> and1046     <em class="parameter"><code>totaldeadrows</code></em>.  (Set <em class="parameter"><code>totaldeadrows</code></em> to zero1047     if the FDW does not have any concept of dead rows.)1048    </p></div><div class="sect2" id="FDW-CALLBACKS-IMPORT"><div class="titlepage"><div><div><h3 class="title">59.2.9. FDW Routines for <code class="command">IMPORT FOREIGN SCHEMA</code> <a href="#FDW-CALLBACKS-IMPORT" class="id_link">#</a></h3></div></div></div><p>1049</p><pre class="programlisting">1050List *1051ImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid);1052</pre><p>1053 1054     Obtain a list of foreign table creation commands.  This function is1055     called when executing <a class="xref" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA"><span class="refentrytitle">IMPORT FOREIGN SCHEMA</span></a>, and is1056     passed the parse tree for that statement, as well as the OID of the1057     foreign server to use.  It should return a list of C strings, each of1058     which must contain a <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> command.1059     These strings will be parsed and executed by the core server.1060    </p><p>1061     Within the <code class="structname">ImportForeignSchemaStmt</code> struct,1062     <code class="structfield">remote_schema</code> is the name of the remote schema from1063     which tables are to be imported.1064     <code class="structfield">list_type</code> identifies how to filter table names:1065     <code class="literal">FDW_IMPORT_SCHEMA_ALL</code> means that all tables in the remote1066     schema should be imported (in this case <code class="structfield">table_list</code> is1067     empty), <code class="literal">FDW_IMPORT_SCHEMA_LIMIT_TO</code> means to include only1068     tables listed in <code class="structfield">table_list</code>,1069     and <code class="literal">FDW_IMPORT_SCHEMA_EXCEPT</code> means to exclude the tables1070     listed in <code class="structfield">table_list</code>.1071     <code class="structfield">options</code> is a list of options used for the import process.1072     The meanings of the options are up to the FDW.1073     For example, an FDW could use an option to define whether the1074     <code class="literal">NOT NULL</code> attributes of columns should be imported.1075     These options need not have anything to do with those supported by the1076     FDW as database object options.1077    </p><p>1078     The FDW may ignore the <code class="structfield">local_schema</code> field of1079     the <code class="structname">ImportForeignSchemaStmt</code>, because the core server1080     will automatically insert that name into the parsed <code class="command">CREATE1081     FOREIGN TABLE</code> commands.1082    </p><p>1083     The FDW does not have to concern itself with implementing the filtering1084     specified by <code class="structfield">list_type</code> and <code class="structfield">table_list</code>,1085     either, as the core server will automatically skip any returned commands1086     for tables excluded according to those options.  However, it's often1087     useful to avoid the work of creating commands for excluded tables in the1088     first place.  The function <code class="function">IsImportableForeignTable()</code> may be1089     useful to test whether a given foreign-table name will pass the filter.1090    </p><p>1091     If the FDW does not support importing table definitions, the1092     <code class="function">ImportForeignSchema</code> pointer can be set to <code class="literal">NULL</code>.1093    </p></div><div class="sect2" id="FDW-CALLBACKS-PARALLEL"><div class="titlepage"><div><div><h3 class="title">59.2.10. FDW Routines for Parallel Execution <a href="#FDW-CALLBACKS-PARALLEL" class="id_link">#</a></h3></div></div></div><p>1094     A <code class="structname">ForeignScan</code> node can, optionally, support parallel1095     execution.  A parallel <code class="structname">ForeignScan</code> will be executed1096     in multiple processes and must return each row exactly once across1097     all cooperating processes.  To do this, processes can coordinate through1098     fixed-size chunks of dynamic shared memory.  This shared memory is not1099     guaranteed to be mapped at the same address in every process, so it1100     must not contain pointers.  The following functions are all optional,1101     but most are required if parallel execution is to be supported.1102    </p><p>1103</p><pre class="programlisting">1104bool1105IsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,1106                          RangeTblEntry *rte);1107</pre><p>1108    Test whether a scan can be performed within a parallel worker.  This1109    function will only be called when the planner believes that a parallel1110    plan might be possible, and should return true if it is safe for that scan1111    to run within a parallel worker.  This will generally not be the case if1112    the remote data source has transaction semantics, unless the worker's1113    connection to the data can somehow be made to share the same transaction1114    context as the leader.1115    </p><p>1116    If this function is not defined, it is assumed that the scan must take1117    place within the parallel leader.  Note that returning true does not mean1118    that the scan itself can be done in parallel, only that the scan can be1119    performed within a parallel worker.  Therefore, it can be useful to define1120    this method even when parallel execution is not supported.1121    </p><p>1122</p><pre class="programlisting">1123Size1124EstimateDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt);1125</pre><p>1126    Estimate the amount of dynamic shared memory that will be required1127    for parallel operation.  This may be higher than the amount that will1128    actually be used, but it must not be lower.  The return value is in bytes.1129    This function is optional, and can be omitted if not needed; but if it1130    is omitted, the next three functions must be omitted as well, because1131    no shared memory will be allocated for the FDW's use.1132    </p><p>1133</p><pre class="programlisting">1134void1135InitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt,1136                         void *coordinate);1137</pre><p>1138    Initialize the dynamic shared memory that will be required for parallel1139    operation.  <code class="literal">coordinate</code> points to a shared memory area of1140    size equal to the return value of <code class="function">EstimateDSMForeignScan</code>.1141    This function is optional, and can be omitted if not needed.1142   </p><p>1143</p><pre class="programlisting">1144void1145ReInitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt,1146                           void *coordinate);1147</pre><p>1148    Re-initialize the dynamic shared memory required for parallel operation1149    when the foreign-scan plan node is about to be re-scanned.1150    This function is optional, and can be omitted if not needed.1151    Recommended practice is that this function reset only shared state,1152    while the <code class="function">ReScanForeignScan</code> function resets only local1153    state.  Currently, this function will be called1154    before <code class="function">ReScanForeignScan</code>, but it's best not to rely on1155    that ordering.1156   </p><p>1157</p><pre class="programlisting">1158void1159InitializeWorkerForeignScan(ForeignScanState *node, shm_toc *toc,1160                            void *coordinate);1161</pre><p>1162    Initialize a parallel worker's local state based on the shared state1163    set up by the leader during <code class="function">InitializeDSMForeignScan</code>.1164    This function is optional, and can be omitted if not needed.1165   </p><p>1166</p><pre class="programlisting">1167void1168ShutdownForeignScan(ForeignScanState *node);1169</pre><p>1170    Release resources when it is anticipated the node will not be executed1171    to completion.  This is not called in all cases; sometimes,1172    <code class="literal">EndForeignScan</code> may be called without this function having1173    been called first.  Since the DSM segment used by parallel query is1174    destroyed just after this callback is invoked, foreign data wrappers that1175    wish to take some action before the DSM segment goes away should implement1176    this method.1177   </p></div><div class="sect2" id="FDW-CALLBACKS-ASYNC"><div class="titlepage"><div><div><h3 class="title">59.2.11. FDW Routines for Asynchronous Execution <a href="#FDW-CALLBACKS-ASYNC" class="id_link">#</a></h3></div></div></div><p>1178     A <code class="structname">ForeignScan</code> node can, optionally, support1179     asynchronous execution as described in1180     <code class="filename">src/backend/executor/README</code>.  The following1181     functions are all optional, but are all required if asynchronous1182     execution is to be supported.1183    </p><p>1184</p><pre class="programlisting">1185bool1186IsForeignPathAsyncCapable(ForeignPath *path);1187</pre><p>1188     Test whether a given <code class="structname">ForeignPath</code> path can scan1189     the underlying foreign relation asynchronously.1190     This function will only be called at the end of query planning when the1191     given path is a direct child of an <code class="structname">AppendPath</code>1192     path and when the planner believes that asynchronous execution improves1193     performance, and should return true if the given path is able to scan the1194     foreign relation asynchronously.1195    </p><p>1196     If this function is not defined, it is assumed that the given path scans1197     the foreign relation using <code class="function">IterateForeignScan</code>.1198     (This implies that the callback functions described below will never be1199     called, so they need not be provided either.)1200    </p><p>

Showing the first 1,200 of 1266 lines. Download the file for the rest.

codekingpro/portable-devtools · Team Ai