1 <!-- doc/src/sgml/fdwhandler.sgml -->
3 <chapter id=
"fdwhandler">
4 <title>Writing a Foreign Data Wrapper
</title>
6 <indexterm zone=
"fdwhandler">
7 <primary>foreign data wrapper
</primary>
8 <secondary>handler for
</secondary>
12 All operations on a foreign table are handled through its foreign data
13 wrapper, which consists of a set of functions that the core server
14 calls. 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. If updating foreign
17 tables is to be supported, the wrapper must handle that, too.
18 This chapter outlines how to write a new foreign data wrapper.
22 The foreign data wrappers included in the standard distribution are good
23 references when trying to write your own. Look into the
24 <filename>contrib
</filename> subdirectory of the source tree.
25 The
<xref linkend=
"sql-createforeigndatawrapper"/> reference page also has
31 The SQL standard specifies an interface for writing foreign data wrappers.
32 However, PostgreSQL does not implement that API, because the effort to
33 accommodate it into PostgreSQL would be large, and the standard API hasn't
34 gained wide adoption anyway.
38 <sect1 id=
"fdw-functions">
39 <title>Foreign Data Wrapper Functions
</title>
42 The FDW author needs to implement a handler function, and optionally
43 a validator function. Both functions must be written in a compiled
44 language such as C, using the version-
1 interface.
45 For details on C language calling conventions and dynamic loading,
46 see
<xref linkend=
"xfunc-c"/>.
50 The handler function simply returns a struct of function pointers to
51 callback functions that will be called by the planner, executor, and
52 various maintenance commands.
53 Most of the effort in writing an FDW is in implementing these callback
55 The handler function must be registered with
56 <productname>PostgreSQL
</productname> as taking no arguments and
57 returning the special pseudo-type
<type>fdw_handler
</type>. The
58 callback functions are plain C functions and are not visible or
59 callable at the SQL level. The callback functions are described in
60 <xref linkend=
"fdw-callbacks"/>.
64 The validator function is responsible for validating options given in
65 <command>CREATE
</command> and
<command>ALTER
</command> commands for its
66 foreign data wrapper, as well as foreign servers, user mappings, and
67 foreign tables using the wrapper.
68 The validator function must be registered as taking two arguments, a
69 text array containing the options to be validated, and an OID
70 representing the type of object the options are associated with. The
71 latter corresponds to the OID of the system catalog the object
72 would be stored in, one of:
73 <itemizedlist spacing=
"compact">
74 <listitem><para><literal>AttributeRelationId
</literal></para></listitem>
75 <listitem><para><literal>ForeignDataWrapperRelationId
</literal></para></listitem>
76 <listitem><para><literal>ForeignServerRelationId
</literal></para></listitem>
77 <listitem><para><literal>ForeignTableRelationId
</literal></para></listitem>
78 <listitem><para><literal>UserMappingRelationId
</literal></para></listitem>
80 If no validator function is supplied, options are not checked at object
81 creation time or object alteration time.
86 <sect1 id=
"fdw-callbacks">
87 <title>Foreign Data Wrapper Callback Routines
</title>
90 The FDW handler function returns a palloc'd
<structname>FdwRoutine
</structname>
91 struct containing pointers to the callback functions described below.
92 The scan-related functions are required, the rest are optional.
96 The
<structname>FdwRoutine
</structname> struct type is declared in
97 <filename>src/include/foreign/fdwapi.h
</filename>, which see for additional
101 <sect2 id=
"fdw-callbacks-scan">
102 <title>FDW Routines for Scanning Foreign Tables
</title>
107 GetForeignRelSize(PlannerInfo *root,
112 Obtain relation size estimates for a foreign table. This is called
113 at the beginning of planning for a query that scans a foreign table.
114 <literal>root
</literal> is the planner's global information about the query;
115 <literal>baserel
</literal> is the planner's information about this table; and
116 <literal>foreigntableid
</literal> is the
<structname>pg_class
</structname> OID of the
117 foreign table. (
<literal>foreigntableid
</literal> could be obtained from the
118 planner data structures, but it's passed explicitly to save effort.)
122 This function should update
<literal>baserel-
>rows
</literal> to be the
123 expected number of rows returned by the table scan, after accounting for
124 the filtering done by the restriction quals. The initial value of
125 <literal>baserel-
>rows
</literal> is just a constant default estimate, which
126 should be replaced if at all possible. The function may also choose to
127 update
<literal>baserel-
>width
</literal> if it can compute a better estimate
128 of the average result row width.
129 (The initial value is based on column data types and on column
130 average-width values measured by the last
<command>ANALYZE
</command>.)
131 Also, this function may update
<literal>baserel-
>tuples
</literal> if
132 it can compute a better estimate of the foreign table's total row count.
133 (The initial value is
134 from
<structname>pg_class
</structname>.
<structfield>reltuples
</structfield>
135 which represents the total row count seen by the
136 last
<command>ANALYZE
</command>; it will be
<literal>-
1</literal> if
137 no
<command>ANALYZE
</command> has been done on this foreign table.)
141 See
<xref linkend=
"fdw-planning"/> for additional information.
147 GetForeignPaths(PlannerInfo *root,
152 Create possible access paths for a scan on a foreign table.
153 This is called during query planning.
154 The parameters are the same as for
<function>GetForeignRelSize
</function>,
155 which has already been called.
159 This function must generate at least one access path
160 (
<structname>ForeignPath
</structname> node) for a scan on the foreign table and
161 must call
<function>add_path
</function> to add each such path to
162 <literal>baserel-
>pathlist
</literal>. It's recommended to use
163 <function>create_foreignscan_path
</function> to build the
164 <structname>ForeignPath
</structname> nodes. The function can generate multiple
165 access paths, e.g., a path which has valid
<literal>pathkeys
</literal> to
166 represent a pre-sorted result. Each access path must contain cost
167 estimates, and can contain any FDW-private information that is needed to
168 identify the specific scan method intended.
172 See
<xref linkend=
"fdw-planning"/> for additional information.
178 GetForeignPlan(PlannerInfo *root,
181 ForeignPath *best_path,
187 Create a
<structname>ForeignScan
</structname> plan node from the selected foreign
188 access path. This is called at the end of query planning.
189 The parameters are as for
<function>GetForeignRelSize
</function>, plus
190 the selected
<structname>ForeignPath
</structname> (previously produced by
191 <function>GetForeignPaths
</function>,
<function>GetForeignJoinPaths
</function>,
192 or
<function>GetForeignUpperPaths
</function>),
193 the target list to be emitted by the plan node,
194 the restriction clauses to be enforced by the plan node,
195 and the outer subplan of the
<structname>ForeignScan
</structname>,
196 which is used for rechecks performed by
<function>RecheckForeignScan
</function>.
197 (If the path is for a join rather than a base
198 relation,
<literal>foreigntableid
</literal> is
<literal>InvalidOid
</literal>.)
202 This function must create and return a
<structname>ForeignScan
</structname> plan
203 node; it's recommended to use
<function>make_foreignscan
</function> to build the
204 <structname>ForeignScan
</structname> node.
208 See
<xref linkend=
"fdw-planning"/> for additional information.
214 BeginForeignScan(ForeignScanState *node,
218 Begin executing a foreign scan. This is called during executor startup.
219 It should perform any initialization needed before the scan can start,
220 but not start executing the actual scan (that should be done upon the
221 first call to
<function>IterateForeignScan
</function>).
222 The
<structname>ForeignScanState
</structname> node has already been created, but
223 its
<structfield>fdw_state
</structfield> field is still NULL. Information about
224 the table to scan is accessible through the
225 <structname>ForeignScanState
</structname> node (in particular, from the underlying
226 <structname>ForeignScan
</structname> plan node, which contains any FDW-private
227 information provided by
<function>GetForeignPlan
</function>).
228 <literal>eflags
</literal> contains flag bits describing the executor's
229 operating mode for this plan node.
233 Note that when
<literal>(eflags
& EXEC_FLAG_EXPLAIN_ONLY)
</literal> is
234 true, this function should not perform any externally-visible actions;
235 it should only do the minimum required to make the node state valid
236 for
<function>ExplainForeignScan
</function> and
<function>EndForeignScan
</function>.
242 IterateForeignScan(ForeignScanState *node);
245 Fetch one row from the foreign source, returning it in a tuple table slot
246 (the node's
<structfield>ScanTupleSlot
</structfield> should be used for this
247 purpose). Return NULL if no more rows are available. The tuple table
248 slot infrastructure allows either a physical or virtual tuple to be
249 returned; in most cases the latter choice is preferable from a
250 performance standpoint. Note that this is called in a short-lived memory
251 context that will be reset between invocations. Create a memory context
252 in
<function>BeginForeignScan
</function> if you need longer-lived storage, or use
253 the
<structfield>es_query_cxt
</structfield> of the node's
<structname>EState
</structname>.
257 The rows returned must match the
<structfield>fdw_scan_tlist
</structfield> target
258 list if one was supplied, otherwise they must match the row type of the
259 foreign table being scanned. If you choose to optimize away fetching
260 columns that are not needed, you should insert nulls in those column
261 positions, or else generate a
<structfield>fdw_scan_tlist
</structfield> list with
262 those columns omitted.
266 Note that
<productname>PostgreSQL
</productname>'s executor doesn't care
267 whether the rows returned violate any constraints that were defined on
268 the foreign table
— but the planner does care, and may optimize
269 queries incorrectly if there are rows visible in the foreign table that
270 do not satisfy a declared constraint. If a constraint is violated when
271 the user has declared that the constraint should hold true, it may be
272 appropriate to raise an error (just as you would need to do in the case
273 of a data type mismatch).
279 ReScanForeignScan(ForeignScanState *node);
282 Restart the scan from the beginning. Note that any parameters the
283 scan depends on may have changed value, so the new scan does not
284 necessarily return exactly the same rows.
290 EndForeignScan(ForeignScanState *node);
293 End the scan and release resources. It is normally not important
294 to release palloc'd memory, but for example open files and connections
295 to remote servers should be cleaned up.
300 <sect2 id=
"fdw-callbacks-join-scan">
301 <title>FDW Routines for Scanning Foreign Joins
</title>
304 If an FDW supports performing foreign joins remotely (rather than
305 by fetching both tables' data and doing the join locally), it should
306 provide this callback function:
312 GetForeignJoinPaths(PlannerInfo *root,
314 RelOptInfo *outerrel,
315 RelOptInfo *innerrel,
317 JoinPathExtraData *extra);
319 Create possible access paths for a join of two (or more) foreign tables
320 that all belong to the same foreign server. This optional
321 function is called during query planning. As
322 with
<function>GetForeignPaths
</function>, this function should
323 generate
<structname>ForeignPath
</structname> path(s) for the
324 supplied
<literal>joinrel
</literal>
325 (use
<function>create_foreign_join_path
</function> to build them),
326 and call
<function>add_path
</function> to add these
327 paths to the set of paths considered for the join. But unlike
328 <function>GetForeignPaths
</function>, it is not necessary that this function
329 succeed in creating at least one path, since paths involving local
330 joining are always possible.
334 Note that this function will be invoked repeatedly for the same join
335 relation, with different combinations of inner and outer relations; it is
336 the responsibility of the FDW to minimize duplicated work.
340 Note also that the set of join clauses to apply to the join,
341 which is passed as
<literal>extra-
>restrictlist
</literal>, varies
342 depending on the combination of inner and outer relations. A
343 <structname>ForeignPath
</structname> path generated for the
344 <literal>joinrel
</literal> must contain the set of join clauses it uses,
345 which will be used by the planner to convert the
346 <structname>ForeignPath
</structname> path into a plan, if it is selected
347 by the planner as the best path for the
<literal>joinrel
</literal>.
351 If a
<structname>ForeignPath
</structname> path is chosen for the join, it will
352 represent the entire join process; paths generated for the component
353 tables and subsidiary joins will not be used. Subsequent processing of
354 the join path proceeds much as it does for a path scanning a single
355 foreign table. One difference is that the
<structfield>scanrelid
</structfield> of
356 the resulting
<structname>ForeignScan
</structname> plan node should be set to zero,
357 since there is no single relation that it represents; instead,
358 the
<structfield>fs_relids
</structfield> field of the
<structname>ForeignScan
</structname>
359 node represents the set of relations that were joined. (The latter field
360 is set up automatically by the core planner code, and need not be filled
361 by the FDW.) Another difference is that, because the column list for a
362 remote join cannot be found from the system catalogs, the FDW must
363 fill
<structfield>fdw_scan_tlist
</structfield> with an appropriate list
364 of
<structfield>TargetEntry
</structfield> nodes, representing the set of columns
365 it will supply at run time in the tuples it returns.
370 Beginning with
<productname>PostgreSQL
</productname> 16,
371 <structfield>fs_relids
</structfield> includes the rangetable indexes
372 of outer joins, if any were involved in this join. The new field
373 <structfield>fs_base_relids
</structfield> includes only base
374 relation indexes, and thus
375 mimics
<structfield>fs_relids
</structfield>'s old semantics.
380 See
<xref linkend=
"fdw-planning"/> for additional information.
384 <sect2 id=
"fdw-callbacks-upper-planning">
385 <title>FDW Routines for Planning Post-Scan/Join Processing
</title>
388 If an FDW supports performing remote post-scan/join processing, such as
389 remote aggregation, it should provide this callback function:
395 GetForeignUpperPaths(PlannerInfo *root,
396 UpperRelationKind stage,
397 RelOptInfo *input_rel,
398 RelOptInfo *output_rel,
401 Create possible access paths for
<firstterm>upper relation
</firstterm> processing,
402 which is the planner's term for all post-scan/join query processing, such
403 as aggregation, window functions, sorting, and table updates. This
404 optional function is called during query planning. Currently, it is
405 called only if all base relation(s) involved in the query belong to the
406 same FDW. This function should generate
<structname>ForeignPath
</structname>
407 path(s) for any post-scan/join processing that the FDW knows how to
409 (use
<function>create_foreign_upper_path
</function> to build them),
410 and call
<function>add_path
</function> to add these paths to
411 the indicated upper relation. As with
<function>GetForeignJoinPaths
</function>,
412 it is not necessary that this function succeed in creating any paths,
413 since paths involving local processing are always possible.
417 The
<literal>stage
</literal> parameter identifies which post-scan/join step is
418 currently being considered.
<literal>output_rel
</literal> is the upper relation
419 that should receive paths representing computation of this step,
420 and
<literal>input_rel
</literal> is the relation representing the input to this
421 step. The
<literal>extra
</literal> parameter provides additional details,
422 currently, it is set only for
<literal>UPPERREL_PARTIAL_GROUP_AGG
</literal>
423 or
<literal>UPPERREL_GROUP_AGG
</literal>, in which case it points to a
424 <literal>GroupPathExtraData
</literal> structure;
425 or for
<literal>UPPERREL_FINAL
</literal>, in which case it points to a
426 <literal>FinalPathExtraData
</literal> structure.
427 (Note that
<structname>ForeignPath
</structname> paths added
428 to
<literal>output_rel
</literal> would typically not have any direct dependency
429 on paths of the
<literal>input_rel
</literal>, since their processing is expected
430 to be done externally. However, examining paths previously generated for
431 the previous processing step can be useful to avoid redundant planning
436 See
<xref linkend=
"fdw-planning"/> for additional information.
440 <sect2 id=
"fdw-callbacks-update">
441 <title>FDW Routines for Updating Foreign Tables
</title>
444 If an FDW supports writable foreign tables, it should provide
445 some or all of the following callback functions depending on
446 the needs and capabilities of the FDW:
452 AddForeignUpdateTargets(PlannerInfo *root,
454 RangeTblEntry *target_rte,
455 Relation target_relation);
458 <command>UPDATE
</command> and
<command>DELETE
</command> operations are performed
459 against rows previously fetched by the table-scanning functions. The
460 FDW may need extra information, such as a row ID or the values of
461 primary-key columns, to ensure that it can identify the exact row to
462 update or delete. To support that, this function can add extra hidden,
463 or
<quote>junk
</quote>, target columns to the list of columns that are to be
464 retrieved from the foreign table during an
<command>UPDATE
</command> or
465 <command>DELETE
</command>.
469 To do that, construct a
<structname>Var
</structname> representing
470 an extra value you need, and pass it
471 to
<function>add_row_identity_var
</function>, along with a name for
472 the junk column. (You can do this more than once if several columns
473 are needed.) You must choose a distinct junk column name for each
474 different
<structname>Var
</structname> you need, except
475 that
<structname>Var
</structname>s that are identical except for
476 the
<structfield>varno
</structfield> field can and should share a
478 The core system uses the junk column names
479 <literal>tableoid
</literal> for a
480 table's
<structfield>tableoid
</structfield> column,
481 <literal>ctid
</literal>
482 or
<literal>ctid
<replaceable>N
</replaceable></literal>
483 for
<structfield>ctid
</structfield>,
484 <literal>wholerow
</literal>
485 for a whole-row
<structname>Var
</structname> marked with
486 <structfield>vartype
</structfield> =
<type>RECORD
</type>,
487 and
<literal>wholerow
<replaceable>N
</replaceable></literal>
488 for a whole-row
<structname>Var
</structname> with
489 <structfield>vartype
</structfield> equal to the table's declared row type.
490 Re-use these names when you can (the planner will combine duplicate
491 requests for identical junk columns). If you need another kind of
492 junk column besides these, it might be wise to choose a name prefixed
493 with your extension name, to avoid conflicts against other FDWs.
497 If the
<function>AddForeignUpdateTargets
</function> pointer is set to
498 <literal>NULL
</literal>, no extra target expressions are added.
499 (This will make it impossible to implement
<command>DELETE
</command>
500 operations, though
<command>UPDATE
</command> may still be feasible if the FDW
501 relies on an unchanging primary key to identify rows.)
507 PlanForeignModify(PlannerInfo *root,
509 Index resultRelation,
513 Perform any additional planning actions needed for an insert, update, or
514 delete on a foreign table. This function generates the FDW-private
515 information that will be attached to the
<structname>ModifyTable
</structname> plan
516 node that performs the update action. This private information must
517 have the form of a
<literal>List
</literal>, and will be delivered to
518 <function>BeginForeignModify
</function> during the execution stage.
522 <literal>root
</literal> is the planner's global information about the query.
523 <literal>plan
</literal> is the
<structname>ModifyTable
</structname> plan node, which is
524 complete except for the
<structfield>fdwPrivLists
</structfield> field.
525 <literal>resultRelation
</literal> identifies the target foreign table by its
526 range table index.
<literal>subplan_index
</literal> identifies which target of
527 the
<structname>ModifyTable
</structname> plan node this is, counting from zero;
528 use this if you want to index into per-target-relation substructures of the
529 <literal>plan
</literal> node.
533 See
<xref linkend=
"fdw-planning"/> for additional information.
537 If the
<function>PlanForeignModify
</function> pointer is set to
538 <literal>NULL
</literal>, no additional plan-time actions are taken, and the
539 <literal>fdw_private
</literal> list delivered to
540 <function>BeginForeignModify
</function> will be NIL.
546 BeginForeignModify(ModifyTableState *mtstate,
547 ResultRelInfo *rinfo,
553 Begin executing a foreign table modification operation. This routine is
554 called during executor startup. It should perform any initialization
555 needed prior to the actual table modifications. Subsequently,
556 <function>ExecForeignInsert/ExecForeignBatchInsert
</function>,
557 <function>ExecForeignUpdate
</function> or
558 <function>ExecForeignDelete
</function> will be called for tuple(s) to be
559 inserted, updated, or deleted.
563 <literal>mtstate
</literal> is the overall state of the
564 <structname>ModifyTable
</structname> plan node being executed; global data about
565 the plan and execution state is available via this structure.
566 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
567 the target foreign table. (The
<structfield>ri_FdwState
</structfield> field of
568 <structname>ResultRelInfo
</structname> is available for the FDW to store any
569 private state it needs for this operation.)
570 <literal>fdw_private
</literal> contains the private data generated by
571 <function>PlanForeignModify
</function>, if any.
572 <literal>subplan_index
</literal> identifies which target of
573 the
<structname>ModifyTable
</structname> plan node this is.
574 <literal>eflags
</literal> contains flag bits describing the executor's
575 operating mode for this plan node.
579 Note that when
<literal>(eflags
& EXEC_FLAG_EXPLAIN_ONLY)
</literal> is
580 true, this function should not perform any externally-visible actions;
581 it should only do the minimum required to make the node state valid
582 for
<function>ExplainForeignModify
</function> and
<function>EndForeignModify
</function>.
586 If the
<function>BeginForeignModify
</function> pointer is set to
587 <literal>NULL
</literal>, no action is taken during executor startup.
593 ExecForeignInsert(EState *estate,
594 ResultRelInfo *rinfo,
595 TupleTableSlot *slot,
596 TupleTableSlot *planSlot);
599 Insert one tuple into the foreign table.
600 <literal>estate
</literal> is global execution state for the query.
601 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
602 the target foreign table.
603 <literal>slot
</literal> contains the tuple to be inserted; it will match the
604 row-type definition of the foreign table.
605 <literal>planSlot
</literal> contains the tuple that was generated by the
606 <structname>ModifyTable
</structname> plan node's subplan; it differs from
607 <literal>slot
</literal> in possibly containing additional
<quote>junk
</quote>
608 columns. (The
<literal>planSlot
</literal> is typically of little interest
609 for
<command>INSERT
</command> cases, but is provided for completeness.)
613 The return value is either a slot containing the data that was actually
614 inserted (this might differ from the data supplied, for example as a
615 result of trigger actions), or NULL if no row was actually inserted
616 (again, typically as a result of triggers). The passed-in
617 <literal>slot
</literal> can be re-used for this purpose.
621 The data in the returned slot is used only if the
<command>INSERT
</command>
622 statement has a
<literal>RETURNING
</literal> clause or involves a view
623 <literal>WITH CHECK OPTION
</literal>; or if the foreign table has
624 an
<literal>AFTER ROW
</literal> trigger. Triggers require all columns,
625 but the FDW could choose to optimize away returning some or all columns
626 depending on the contents of the
<literal>RETURNING
</literal> clause or
627 <literal>WITH CHECK OPTION
</literal> constraints. Regardless, some slot
628 must be returned to indicate success, or the query's reported row count
633 If the
<function>ExecForeignInsert
</function> pointer is set to
634 <literal>NULL
</literal>, attempts to insert into the foreign table will fail
635 with an error message.
639 Note that this function is also called when inserting routed tuples into
640 a foreign-table partition or executing
<command>COPY FROM
</command> on
641 a foreign table, in which case it is called in a different way than it
642 is in the
<command>INSERT
</command> case. See the callback functions
643 described below that allow the FDW to support that.
649 ExecForeignBatchInsert(EState *estate,
650 ResultRelInfo *rinfo,
651 TupleTableSlot **slots,
652 TupleTableSlot **planSlots,
656 Insert multiple tuples in bulk into the foreign table.
657 The parameters are the same for
<function>ExecForeignInsert
</function>
658 except
<literal>slots
</literal> and
<literal>planSlots
</literal> contain
659 multiple tuples and
<literal>*numSlots
</literal> specifies the number of
660 tuples in those arrays.
664 The return value is an array of slots containing the data that was
665 actually inserted (this might differ from the data supplied, for
666 example as a result of trigger actions.)
667 The passed-in
<literal>slots
</literal> can be re-used for this purpose.
668 The number of successfully inserted tuples is returned in
669 <literal>*numSlots
</literal>.
673 The data in the returned slot is used only if the
<command>INSERT
</command>
674 statement involves a view
675 <literal>WITH CHECK OPTION
</literal>; or if the foreign table has
676 an
<literal>AFTER ROW
</literal> trigger. Triggers require all columns,
677 but the FDW could choose to optimize away returning some or all columns
678 depending on the contents of the
679 <literal>WITH CHECK OPTION
</literal> constraints.
683 If the
<function>ExecForeignBatchInsert
</function> or
684 <function>GetForeignModifyBatchSize
</function> pointer is set to
685 <literal>NULL
</literal>, attempts to insert into the foreign table will
686 use
<function>ExecForeignInsert
</function>.
687 This function is not used if the
<command>INSERT
</command> has the
688 <literal>RETURNING
</literal> clause.
692 Note that this function is also called when inserting routed tuples into
693 a foreign-table partition or executing
<command>COPY FROM
</command> on
694 a foreign table, in which case it is called in a different way than it
695 is in the
<command>INSERT
</command> case. See the callback functions
696 described below that allow the FDW to support that.
702 GetForeignModifyBatchSize(ResultRelInfo *rinfo);
705 Report the maximum number of tuples that a single
706 <function>ExecForeignBatchInsert
</function> call can handle for
707 the specified foreign table. The executor passes at most
708 the given number of tuples to
<function>ExecForeignBatchInsert
</function>.
709 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
710 the target foreign table.
711 The FDW is expected to provide a foreign server and/or foreign
712 table option for the user to set this value, or some hard-coded value.
716 If the
<function>ExecForeignBatchInsert
</function> or
717 <function>GetForeignModifyBatchSize
</function> pointer is set to
718 <literal>NULL
</literal>, attempts to insert into the foreign table will
719 use
<function>ExecForeignInsert
</function>.
725 ExecForeignUpdate(EState *estate,
726 ResultRelInfo *rinfo,
727 TupleTableSlot *slot,
728 TupleTableSlot *planSlot);
731 Update one tuple in the foreign table.
732 <literal>estate
</literal> is global execution state for the query.
733 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
734 the target foreign table.
735 <literal>slot
</literal> contains the new data for the tuple; it will match the
736 row-type definition of the foreign table.
737 <literal>planSlot
</literal> contains the tuple that was generated by the
738 <structname>ModifyTable
</structname> plan node's subplan. Unlike
739 <literal>slot
</literal>, this tuple contains only the new values for
740 columns changed by the query, so do not rely on attribute numbers of the
741 foreign table to index into
<literal>planSlot
</literal>.
742 Also,
<literal>planSlot
</literal> typically contains
743 additional
<quote>junk
</quote> columns. In particular, any junk columns
744 that were requested by
<function>AddForeignUpdateTargets
</function> will
745 be available from this slot.
749 The return value is either a slot containing the row as it was actually
750 updated (this might differ from the data supplied, for example as a
751 result of trigger actions), or NULL if no row was actually updated
752 (again, typically as a result of triggers). The passed-in
753 <literal>slot
</literal> can be re-used for this purpose.
757 The data in the returned slot is used only if the
<command>UPDATE
</command>
758 statement has a
<literal>RETURNING
</literal> clause or involves a view
759 <literal>WITH CHECK OPTION
</literal>; or if the foreign table has
760 an
<literal>AFTER ROW
</literal> trigger. Triggers require all columns,
761 but the FDW could choose to optimize away returning some or all columns
762 depending on the contents of the
<literal>RETURNING
</literal> clause or
763 <literal>WITH CHECK OPTION
</literal> constraints. Regardless, some slot
764 must be returned to indicate success, or the query's reported row count
769 If the
<function>ExecForeignUpdate
</function> pointer is set to
770 <literal>NULL
</literal>, attempts to update the foreign table will fail
771 with an error message.
777 ExecForeignDelete(EState *estate,
778 ResultRelInfo *rinfo,
779 TupleTableSlot *slot,
780 TupleTableSlot *planSlot);
783 Delete one tuple from the foreign table.
784 <literal>estate
</literal> is global execution state for the query.
785 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
786 the target foreign table.
787 <literal>slot
</literal> contains nothing useful upon call, but can be used to
788 hold the returned tuple.
789 <literal>planSlot
</literal> contains the tuple that was generated by the
790 <structname>ModifyTable
</structname> plan node's subplan; in particular, it will
791 carry any junk columns that were requested by
792 <function>AddForeignUpdateTargets
</function>. The junk column(s) must be used
793 to identify the tuple to be deleted.
797 The return value is either a slot containing the row that was deleted,
798 or NULL if no row was deleted (typically as a result of triggers). The
799 passed-in
<literal>slot
</literal> can be used to hold the tuple to be returned.
803 The data in the returned slot is used only if the
<command>DELETE
</command>
804 query has a
<literal>RETURNING
</literal> clause or the foreign table has
805 an
<literal>AFTER ROW
</literal> trigger. Triggers require all columns, but the
806 FDW could choose to optimize away returning some or all columns depending
807 on the contents of the
<literal>RETURNING
</literal> clause. Regardless, some
808 slot must be returned to indicate success, or the query's reported row
813 If the
<function>ExecForeignDelete
</function> pointer is set to
814 <literal>NULL
</literal>, attempts to delete from the foreign table will fail
815 with an error message.
821 EndForeignModify(EState *estate,
822 ResultRelInfo *rinfo);
825 End the table update and release resources. It is normally not important
826 to release palloc'd memory, but for example open files and connections
827 to remote servers should be cleaned up.
831 If the
<function>EndForeignModify
</function> pointer is set to
832 <literal>NULL
</literal>, no action is taken during executor shutdown.
836 Tuples inserted into a partitioned table by
<command>INSERT
</command> or
837 <command>COPY FROM
</command> are routed to partitions. If an FDW
838 supports routable foreign-table partitions, it should also provide the
839 following callback functions. These functions are also called when
840 <command>COPY FROM
</command> is executed on a foreign table.
846 BeginForeignInsert(ModifyTableState *mtstate,
847 ResultRelInfo *rinfo);
850 Begin executing an insert operation on a foreign table. This routine is
851 called right before the first tuple is inserted into the foreign table
852 in both cases when it is the partition chosen for tuple routing and the
853 target specified in a
<command>COPY FROM
</command> command. It should
854 perform any initialization needed prior to the actual insertion.
855 Subsequently,
<function>ExecForeignInsert
</function> or
856 <function>ExecForeignBatchInsert
</function> will be called for
857 tuple(s) to be inserted into the foreign table.
861 <literal>mtstate
</literal> is the overall state of the
862 <structname>ModifyTable
</structname> plan node being executed; global data about
863 the plan and execution state is available via this structure.
864 <literal>rinfo
</literal> is the
<structname>ResultRelInfo
</structname> struct describing
865 the target foreign table. (The
<structfield>ri_FdwState
</structfield> field of
866 <structname>ResultRelInfo
</structname> is available for the FDW to store any
867 private state it needs for this operation.)
871 When this is called by a
<command>COPY FROM
</command> command, the
872 plan-related global data in
<literal>mtstate
</literal> is not provided
873 and the
<literal>planSlot
</literal> parameter of
874 <function>ExecForeignInsert
</function> subsequently called for each
875 inserted tuple is
<literal>NULL
</literal>, whether the foreign table is
876 the partition chosen for tuple routing or the target specified in the
881 If the
<function>BeginForeignInsert
</function> pointer is set to
882 <literal>NULL
</literal>, no action is taken for the initialization.
886 Note that if the FDW does not support routable foreign-table partitions
887 and/or executing
<command>COPY FROM
</command> on foreign tables, this
888 function or
<function>ExecForeignInsert/ExecForeignBatchInsert
</function>
889 subsequently called must throw error as needed.
895 EndForeignInsert(EState *estate,
896 ResultRelInfo *rinfo);
899 End the insert operation and release resources. It is normally not important
900 to release palloc'd memory, but for example open files and connections
901 to remote servers should be cleaned up.
905 If the
<function>EndForeignInsert
</function> pointer is set to
906 <literal>NULL
</literal>, no action is taken for the termination.
912 IsForeignRelUpdatable(Relation rel);
915 Report which update operations the specified foreign table supports.
916 The return value should be a bit mask of rule event numbers indicating
917 which operations are supported by the foreign table, using the
918 <literal>CmdType
</literal> enumeration; that is,
919 <literal>(
1 << CMD_UPDATE) =
4</literal> for
<command>UPDATE
</command>,
920 <literal>(
1 << CMD_INSERT) =
8</literal> for
<command>INSERT
</command>, and
921 <literal>(
1 << CMD_DELETE) =
16</literal> for
<command>DELETE
</command>.
925 If the
<function>IsForeignRelUpdatable
</function> pointer is set to
926 <literal>NULL
</literal>, foreign tables are assumed to be insertable, updatable,
927 or deletable if the FDW provides
<function>ExecForeignInsert
</function>,
928 <function>ExecForeignUpdate
</function>, or
<function>ExecForeignDelete
</function>
929 respectively. This function is only needed if the FDW supports some
930 tables that are updatable and some that are not. (Even then, it's
931 permissible to throw an error in the execution routine instead of
932 checking in this function. However, this function is used to determine
933 updatability for display in the
<literal>information_schema
</literal> views.)
937 Some inserts, updates, and deletes to foreign tables can be optimized
938 by implementing an alternative set of interfaces. The ordinary
939 interfaces for inserts, updates, and deletes fetch rows from the remote
940 server and then modify those rows one at a time. In some cases, this
941 row-by-row approach is necessary, but it can be inefficient. If it is
942 possible for the foreign server to determine which rows should be
943 modified without actually retrieving them, and if there are no local
944 structures which would affect the operation (row-level local triggers,
945 stored generated columns, or
<literal>WITH CHECK OPTION
</literal>
946 constraints from parent views), then it is possible to arrange things
947 so that the entire operation is performed on the remote server. The
948 interfaces described below make this possible.
954 PlanDirectModify(PlannerInfo *root,
956 Index resultRelation,
960 Decide whether it is safe to execute a direct modification
961 on the remote server. If so, return
<literal>true
</literal> after performing
962 planning actions needed for that. Otherwise, return
<literal>false
</literal>.
963 This optional function is called during query planning.
964 If this function succeeds,
<function>BeginDirectModify
</function>,
965 <function>IterateDirectModify
</function> and
<function>EndDirectModify
</function> will
966 be called at the execution stage, instead. Otherwise, the table
967 modification will be executed using the table-updating functions
969 The parameters are the same as for
<function>PlanForeignModify
</function>.
973 To execute the direct modification on the remote server, this function
974 must rewrite the target subplan with a
<structname>ForeignScan
</structname> plan
975 node that executes the direct modification on the remote server. The
976 <structfield>operation
</structfield> and
<structfield>resultRelation
</structfield> fields
977 of the
<structname>ForeignScan
</structname> must be set appropriately.
978 <structfield>operation
</structfield> must be set to the
<literal>CmdType
</literal>
979 enumeration corresponding to the statement kind (that is,
980 <literal>CMD_UPDATE
</literal> for
<command>UPDATE
</command>,
981 <literal>CMD_INSERT
</literal> for
<command>INSERT
</command>, and
982 <literal>CMD_DELETE
</literal> for
<command>DELETE
</command>), and the
983 <literal>resultRelation
</literal> argument must be copied to the
984 <structfield>resultRelation
</structfield> field.
988 See
<xref linkend=
"fdw-planning"/> for additional information.
992 If the
<function>PlanDirectModify
</function> pointer is set to
993 <literal>NULL
</literal>, no attempts to execute a direct modification on the
994 remote server are taken.
1000 BeginDirectModify(ForeignScanState *node,
1004 Prepare to execute a direct modification on the remote server.
1005 This is called during executor startup. It should perform any
1006 initialization needed prior to the direct modification (that should be
1007 done upon the first call to
<function>IterateDirectModify
</function>).
1008 The
<structname>ForeignScanState
</structname> node has already been created, but
1009 its
<structfield>fdw_state
</structfield> field is still NULL. Information about
1010 the table to modify is accessible through the
1011 <structname>ForeignScanState
</structname> node (in particular, from the underlying
1012 <structname>ForeignScan
</structname> plan node, which contains any FDW-private
1013 information provided by
<function>PlanDirectModify
</function>).
1014 <literal>eflags
</literal> contains flag bits describing the executor's
1015 operating mode for this plan node.
1019 Note that when
<literal>(eflags
& EXEC_FLAG_EXPLAIN_ONLY)
</literal> is
1020 true, this function should not perform any externally-visible actions;
1021 it should only do the minimum required to make the node state valid
1022 for
<function>ExplainDirectModify
</function> and
<function>EndDirectModify
</function>.
1026 If the
<function>BeginDirectModify
</function> pointer is set to
1027 <literal>NULL
</literal>, no attempts to execute a direct modification on the
1028 remote server are taken.
1034 IterateDirectModify(ForeignScanState *node);
1037 When the
<command>INSERT
</command>,
<command>UPDATE
</command> or
<command>DELETE
</command>
1038 query doesn't have a
<literal>RETURNING
</literal> clause, just return NULL
1039 after a direct modification on the remote server.
1040 When the query has the clause, fetch one result containing the data
1041 needed for the
<literal>RETURNING
</literal> calculation, returning it in a
1042 tuple table slot (the node's
<structfield>ScanTupleSlot
</structfield> should be
1043 used for this purpose). The data that was actually inserted, updated
1044 or deleted must be stored in
1045 <literal>node-
>resultRelInfo-
>ri_projectReturning-
>pi_exprContext-
>ecxt_scantuple
</literal>.
1046 Return NULL if no more rows are available.
1047 Note that this is called in a short-lived memory context that will be
1048 reset between invocations. Create a memory context in
1049 <function>BeginDirectModify
</function> if you need longer-lived storage, or use
1050 the
<structfield>es_query_cxt
</structfield> of the node's
<structname>EState
</structname>.
1054 The rows returned must match the
<structfield>fdw_scan_tlist
</structfield> target
1055 list if one was supplied, otherwise they must match the row type of the
1056 foreign table being updated. If you choose to optimize away fetching
1057 columns that are not needed for the
<literal>RETURNING
</literal> calculation,
1058 you should insert nulls in those column positions, or else generate a
1059 <structfield>fdw_scan_tlist
</structfield> list with those columns omitted.
1063 Whether the query has the clause or not, the query's reported row count
1064 must be incremented by the FDW itself. When the query doesn't have the
1065 clause, the FDW must also increment the row count for the
1066 <structname>ForeignScanState
</structname> node in the
<command>EXPLAIN ANALYZE
</command>
1071 If the
<function>IterateDirectModify
</function> pointer is set to
1072 <literal>NULL
</literal>, no attempts to execute a direct modification on the
1073 remote server are taken.
1079 EndDirectModify(ForeignScanState *node);
1082 Clean up following a direct modification on the remote server. It is
1083 normally not important to release palloc'd memory, but for example open
1084 files and connections to the remote server should be cleaned up.
1088 If the
<function>EndDirectModify
</function> pointer is set to
1089 <literal>NULL
</literal>, no attempts to execute a direct modification on the
1090 remote server are taken.
1095 <sect2 id=
"fdw-callbacks-truncate">
1096 <title>FDW Routines for
<command>TRUNCATE
</command></title>
1101 ExecForeignTruncate(List *rels,
1102 DropBehavior behavior,
1106 Truncate foreign tables. This function is called when
1107 <xref linkend=
"sql-truncate"/> is executed on a foreign table.
1108 <literal>rels
</literal> is a list of
<structname>Relation
</structname>
1109 data structures of foreign tables to truncate.
1113 <literal>behavior
</literal> is either
<literal>DROP_RESTRICT
</literal>
1114 or
<literal>DROP_CASCADE
</literal> indicating that the
1115 <literal>RESTRICT
</literal> or
<literal>CASCADE
</literal> option was
1116 requested in the original
<command>TRUNCATE
</command> command,
1121 If
<literal>restart_seqs
</literal> is
<literal>true
</literal>,
1122 the original
<command>TRUNCATE
</command> command requested the
1123 <literal>RESTART IDENTITY
</literal> behavior, otherwise the
1124 <literal>CONTINUE IDENTITY
</literal> behavior was requested.
1128 Note that the
<literal>ONLY
</literal> options specified
1129 in the original
<command>TRUNCATE
</command> command are not passed to
1130 <function>ExecForeignTruncate
</function>. This behavior is similar to
1131 the callback functions of
<command>SELECT
</command>,
1132 <command>UPDATE
</command> and
<command>DELETE
</command> on
1137 <function>ExecForeignTruncate
</function> is invoked once per
1138 foreign server for which foreign tables are to be truncated.
1139 This means that all foreign tables included in
<literal>rels
</literal>
1140 must belong to the same server.
1144 If the
<function>ExecForeignTruncate
</function> pointer is set to
1145 <literal>NULL
</literal>, attempts to truncate foreign tables will
1146 fail with an error message.
1150 <sect2 id=
"fdw-callbacks-row-locking">
1151 <title>FDW Routines for Row Locking
</title>
1154 If an FDW wishes to support
<firstterm>late row locking
</firstterm> (as described
1155 in
<xref linkend=
"fdw-row-locking"/>), it must provide the following
1162 GetForeignRowMarkType(RangeTblEntry *rte,
1163 LockClauseStrength strength);
1166 Report which row-marking option to use for a foreign table.
1167 <literal>rte
</literal> is the
<structname>RangeTblEntry
</structname> node for the table
1168 and
<literal>strength
</literal> describes the lock strength requested by the
1169 relevant
<literal>FOR UPDATE/SHARE
</literal> clause, if any. The result must be
1170 a member of the
<literal>RowMarkType
</literal> enum type.
1174 This function is called during query planning for each foreign table that
1175 appears in an
<command>UPDATE
</command>,
<command>DELETE
</command>, or
<command>SELECT
1176 FOR UPDATE/SHARE
</command> query and is not the target of
<command>UPDATE
</command>
1177 or
<command>DELETE
</command>.
1181 If the
<function>GetForeignRowMarkType
</function> pointer is set to
1182 <literal>NULL
</literal>, the
<literal>ROW_MARK_COPY
</literal> option is always used.
1183 (This implies that
<function>RefetchForeignRow
</function> will never be called,
1184 so it need not be provided either.)
1188 See
<xref linkend=
"fdw-row-locking"/> for more information.
1194 RefetchForeignRow(EState *estate,
1197 TupleTableSlot *slot,
1201 Re-fetch one tuple slot from the foreign table, after locking it if required.
1202 <literal>estate
</literal> is global execution state for the query.
1203 <literal>erm
</literal> is the
<structname>ExecRowMark
</structname> struct describing
1204 the target foreign table and the row lock type (if any) to acquire.
1205 <literal>rowid
</literal> identifies the tuple to be fetched.
1206 <literal>slot
</literal> contains nothing useful upon call, but can be used to
1207 hold the returned tuple.
<literal>updated
</literal> is an output parameter.
1211 This function should store the tuple into the provided slot, or clear it if
1212 the row lock couldn't be obtained. The row lock type to acquire is
1213 defined by
<literal>erm-
>markType
</literal>, which is the value
1214 previously returned by
<function>GetForeignRowMarkType
</function>.
1215 (
<literal>ROW_MARK_REFERENCE
</literal> means to just re-fetch the tuple
1216 without acquiring any lock, and
<literal>ROW_MARK_COPY
</literal> will
1217 never be seen by this routine.)
1221 In addition,
<literal>*updated
</literal> should be set to
<literal>true
</literal>
1222 if what was fetched was an updated version of the tuple rather than
1223 the same version previously obtained. (If the FDW cannot be sure about
1224 this, always returning
<literal>true
</literal> is recommended.)
1228 Note that by default, failure to acquire a row lock should result in
1229 raising an error; returning with an empty slot is only appropriate if
1230 the
<literal>SKIP LOCKED
</literal> option is specified
1231 by
<literal>erm-
>waitPolicy
</literal>.
1235 The
<literal>rowid
</literal> is the
<structfield>ctid
</structfield> value previously read
1236 for the row to be re-fetched. Although the
<literal>rowid
</literal> value is
1237 passed as a
<type>Datum
</type>, it can currently only be a
<type>tid
</type>. The
1238 function API is chosen in hopes that it may be possible to allow other
1239 data types for row IDs in future.
1243 If the
<function>RefetchForeignRow
</function> pointer is set to
1244 <literal>NULL
</literal>, attempts to re-fetch rows will fail
1245 with an error message.
1249 See
<xref linkend=
"fdw-row-locking"/> for more information.
1255 RecheckForeignScan(ForeignScanState *node,
1256 TupleTableSlot *slot);
1258 Recheck that a previously-returned tuple still matches the relevant
1259 scan and join qualifiers, and possibly provide a modified version of
1260 the tuple. For foreign data wrappers which do not perform join pushdown,
1261 it will typically be more convenient to set this to
<literal>NULL
</literal> and
1262 instead set
<structfield>fdw_recheck_quals
</structfield> appropriately.
1263 When outer joins are pushed down, however, it isn't sufficient to
1264 reapply the checks relevant to all the base tables to the result tuple,
1265 even if all needed attributes are present, because failure to match some
1266 qualifier might result in some attributes going to NULL, rather than in
1267 no tuple being returned.
<literal>RecheckForeignScan
</literal> can recheck
1268 qualifiers and return true if they are still satisfied and false
1269 otherwise, but it can also store a replacement tuple into the supplied
1274 To implement join pushdown, a foreign data wrapper will typically
1275 construct an alternative local join plan which is used only for
1276 rechecks; this will become the outer subplan of the
1277 <literal>ForeignScan
</literal>. When a recheck is required, this subplan
1278 can be executed and the resulting tuple can be stored in the slot.
1279 This plan need not be efficient since no base table will return more
1280 than one row; for example, it may implement all joins as nested loops.
1281 The function
<literal>GetExistingLocalJoinPath
</literal> may be used to search
1282 existing paths for a suitable local join path, which can be used as the
1283 alternative local join plan.
<literal>GetExistingLocalJoinPath
</literal>
1284 searches for an unparameterized path in the path list of the specified
1285 join relation. (If it does not find such a path, it returns NULL, in
1286 which case a foreign data wrapper may build the local path by itself or
1287 may choose not to create access paths for that join.)
1291 <sect2 id=
"fdw-callbacks-explain">
1292 <title>FDW Routines for
<command>EXPLAIN
</command></title>
1297 ExplainForeignScan(ForeignScanState *node,
1301 Print additional
<command>EXPLAIN
</command> output for a foreign table scan.
1302 This function can call
<function>ExplainPropertyText
</function> and
1303 related functions to add fields to the
<command>EXPLAIN
</command> output.
1304 The flag fields in
<literal>es
</literal> can be used to determine what to
1305 print, and the state of the
<structname>ForeignScanState
</structname> node
1306 can be inspected to provide run-time statistics in the
<command>EXPLAIN
1307 ANALYZE
</command> case.
1311 If the
<function>ExplainForeignScan
</function> pointer is set to
1312 <literal>NULL
</literal>, no additional information is printed during
1313 <command>EXPLAIN
</command>.
1319 ExplainForeignModify(ModifyTableState *mtstate,
1320 ResultRelInfo *rinfo,
1323 struct ExplainState *es);
1326 Print additional
<command>EXPLAIN
</command> output for a foreign table update.
1327 This function can call
<function>ExplainPropertyText
</function> and
1328 related functions to add fields to the
<command>EXPLAIN
</command> output.
1329 The flag fields in
<literal>es
</literal> can be used to determine what to
1330 print, and the state of the
<structname>ModifyTableState
</structname> node
1331 can be inspected to provide run-time statistics in the
<command>EXPLAIN
1332 ANALYZE
</command> case. The first four arguments are the same as for
1333 <function>BeginForeignModify
</function>.
1337 If the
<function>ExplainForeignModify
</function> pointer is set to
1338 <literal>NULL
</literal>, no additional information is printed during
1339 <command>EXPLAIN
</command>.
1345 ExplainDirectModify(ForeignScanState *node,
1349 Print additional
<command>EXPLAIN
</command> output for a direct modification
1350 on the remote server.
1351 This function can call
<function>ExplainPropertyText
</function> and
1352 related functions to add fields to the
<command>EXPLAIN
</command> output.
1353 The flag fields in
<literal>es
</literal> can be used to determine what to
1354 print, and the state of the
<structname>ForeignScanState
</structname> node
1355 can be inspected to provide run-time statistics in the
<command>EXPLAIN
1356 ANALYZE
</command> case.
1360 If the
<function>ExplainDirectModify
</function> pointer is set to
1361 <literal>NULL
</literal>, no additional information is printed during
1362 <command>EXPLAIN
</command>.
1367 <sect2 id=
"fdw-callbacks-analyze">
1368 <title>FDW Routines for
<command>ANALYZE
</command></title>
1373 AnalyzeForeignTable(Relation relation,
1374 AcquireSampleRowsFunc *func,
1375 BlockNumber *totalpages);
1378 This function is called when
<xref linkend=
"sql-analyze"/> is executed on
1379 a foreign table. If the FDW can collect statistics for this
1380 foreign table, it should return
<literal>true
</literal>, and provide a pointer
1381 to a function that will collect sample rows from the table in
1382 <parameter>func
</parameter>, plus the estimated size of the table in pages in
1383 <parameter>totalpages
</parameter>. Otherwise, return
<literal>false
</literal>.
1387 If the FDW does not support collecting statistics for any tables, the
1388 <function>AnalyzeForeignTable
</function> pointer can be set to
<literal>NULL
</literal>.
1392 If provided, the sample collection function must have the signature
1395 AcquireSampleRowsFunc(Relation relation,
1400 double *totaldeadrows);
1403 A random sample of up to
<parameter>targrows
</parameter> rows should be collected
1404 from the table and stored into the caller-provided
<parameter>rows
</parameter>
1405 array. The actual number of rows collected must be returned. In
1406 addition, store estimates of the total numbers of live and dead rows in
1407 the table into the output parameters
<parameter>totalrows
</parameter> and
1408 <parameter>totaldeadrows
</parameter>. (Set
<parameter>totaldeadrows
</parameter> to zero
1409 if the FDW does not have any concept of dead rows.)
1414 <sect2 id=
"fdw-callbacks-import">
1415 <title>FDW Routines for
<command>IMPORT FOREIGN SCHEMA
</command></title>
1420 ImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid);
1423 Obtain a list of foreign table creation commands. This function is
1424 called when executing
<xref linkend=
"sql-importforeignschema"/>, and is
1425 passed the parse tree for that statement, as well as the OID of the
1426 foreign server to use. It should return a list of C strings, each of
1427 which must contain a
<xref linkend=
"sql-createforeigntable"/> command.
1428 These strings will be parsed and executed by the core server.
1432 Within the
<structname>ImportForeignSchemaStmt
</structname> struct,
1433 <structfield>remote_schema
</structfield> is the name of the remote schema from
1434 which tables are to be imported.
1435 <structfield>list_type
</structfield> identifies how to filter table names:
1436 <literal>FDW_IMPORT_SCHEMA_ALL
</literal> means that all tables in the remote
1437 schema should be imported (in this case
<structfield>table_list
</structfield> is
1438 empty),
<literal>FDW_IMPORT_SCHEMA_LIMIT_TO
</literal> means to include only
1439 tables listed in
<structfield>table_list
</structfield>,
1440 and
<literal>FDW_IMPORT_SCHEMA_EXCEPT
</literal> means to exclude the tables
1441 listed in
<structfield>table_list
</structfield>.
1442 <structfield>options
</structfield> is a list of options used for the import process.
1443 The meanings of the options are up to the FDW.
1444 For example, an FDW could use an option to define whether the
1445 <literal>NOT NULL
</literal> attributes of columns should be imported.
1446 These options need not have anything to do with those supported by the
1447 FDW as database object options.
1451 The FDW may ignore the
<structfield>local_schema
</structfield> field of
1452 the
<structname>ImportForeignSchemaStmt
</structname>, because the core server
1453 will automatically insert that name into the parsed
<command>CREATE
1454 FOREIGN TABLE
</command> commands.
1458 The FDW does not have to concern itself with implementing the filtering
1459 specified by
<structfield>list_type
</structfield> and
<structfield>table_list
</structfield>,
1460 either, as the core server will automatically skip any returned commands
1461 for tables excluded according to those options. However, it's often
1462 useful to avoid the work of creating commands for excluded tables in the
1463 first place. The function
<function>IsImportableForeignTable()
</function> may be
1464 useful to test whether a given foreign-table name will pass the filter.
1468 If the FDW does not support importing table definitions, the
1469 <function>ImportForeignSchema
</function> pointer can be set to
<literal>NULL
</literal>.
1474 <sect2 id=
"fdw-callbacks-parallel">
1475 <title>FDW Routines for Parallel Execution
</title>
1477 A
<structname>ForeignScan
</structname> node can, optionally, support parallel
1478 execution. A parallel
<structname>ForeignScan
</structname> will be executed
1479 in multiple processes and must return each row exactly once across
1480 all cooperating processes. To do this, processes can coordinate through
1481 fixed-size chunks of dynamic shared memory. This shared memory is not
1482 guaranteed to be mapped at the same address in every process, so it
1483 must not contain pointers. The following functions are all optional,
1484 but most are required if parallel execution is to be supported.
1490 IsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel,
1491 RangeTblEntry *rte);
1493 Test whether a scan can be performed within a parallel worker. This
1494 function will only be called when the planner believes that a parallel
1495 plan might be possible, and should return true if it is safe for that scan
1496 to run within a parallel worker. This will generally not be the case if
1497 the remote data source has transaction semantics, unless the worker's
1498 connection to the data can somehow be made to share the same transaction
1499 context as the leader.
1503 If this function is not defined, it is assumed that the scan must take
1504 place within the parallel leader. Note that returning true does not mean
1505 that the scan itself can be done in parallel, only that the scan can be
1506 performed within a parallel worker. Therefore, it can be useful to define
1507 this method even when parallel execution is not supported.
1513 EstimateDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt);
1515 Estimate the amount of dynamic shared memory that will be required
1516 for parallel operation. This may be higher than the amount that will
1517 actually be used, but it must not be lower. The return value is in bytes.
1518 This function is optional, and can be omitted if not needed; but if it
1519 is omitted, the next three functions must be omitted as well, because
1520 no shared memory will be allocated for the FDW's use.
1526 InitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt,
1529 Initialize the dynamic shared memory that will be required for parallel
1530 operation.
<literal>coordinate
</literal> points to a shared memory area of
1531 size equal to the return value of
<function>EstimateDSMForeignScan
</function>.
1532 This function is optional, and can be omitted if not needed.
1538 ReInitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt,
1541 Re-initialize the dynamic shared memory required for parallel operation
1542 when the foreign-scan plan node is about to be re-scanned.
1543 This function is optional, and can be omitted if not needed.
1544 Recommended practice is that this function reset only shared state,
1545 while the
<function>ReScanForeignScan
</function> function resets only local
1546 state. Currently, this function will be called
1547 before
<function>ReScanForeignScan
</function>, but it's best not to rely on
1554 InitializeWorkerForeignScan(ForeignScanState *node, shm_toc *toc,
1557 Initialize a parallel worker's local state based on the shared state
1558 set up by the leader during
<function>InitializeDSMForeignScan
</function>.
1559 This function is optional, and can be omitted if not needed.
1565 ShutdownForeignScan(ForeignScanState *node);
1567 Release resources when it is anticipated the node will not be executed
1568 to completion. This is not called in all cases; sometimes,
1569 <literal>EndForeignScan
</literal> may be called without this function having
1570 been called first. Since the DSM segment used by parallel query is
1571 destroyed just after this callback is invoked, foreign data wrappers that
1572 wish to take some action before the DSM segment goes away should implement
1577 <sect2 id=
"fdw-callbacks-async">
1578 <title>FDW Routines for Asynchronous Execution
</title>
1580 A
<structname>ForeignScan
</structname> node can, optionally, support
1581 asynchronous execution as described in
1582 <filename>src/backend/executor/README
</filename>. The following
1583 functions are all optional, but are all required if asynchronous
1584 execution is to be supported.
1590 IsForeignPathAsyncCapable(ForeignPath *path);
1592 Test whether a given
<structname>ForeignPath
</structname> path can scan
1593 the underlying foreign relation asynchronously.
1594 This function will only be called at the end of query planning when the
1595 given path is a direct child of an
<structname>AppendPath
</structname>
1596 path and when the planner believes that asynchronous execution improves
1597 performance, and should return true if the given path is able to scan the
1598 foreign relation asynchronously.
1602 If this function is not defined, it is assumed that the given path scans
1603 the foreign relation using
<function>IterateForeignScan
</function>.
1604 (This implies that the callback functions described below will never be
1605 called, so they need not be provided either.)
1611 ForeignAsyncRequest(AsyncRequest *areq);
1613 Produce one tuple asynchronously from the
1614 <structname>ForeignScan
</structname> node.
<literal>areq
</literal> is
1615 the
<structname>AsyncRequest
</structname> struct describing the
1616 <structname>ForeignScan
</structname> node and the parent
1617 <structname>Append
</structname> node that requested the tuple from it.
1618 This function should store the tuple into the slot specified by
1619 <literal>areq-
>result
</literal>, and set
1620 <literal>areq-
>request_complete
</literal> to
<literal>true
</literal>;
1621 or if it needs to wait on an event external to the core server such as
1622 network I/O, and cannot produce any tuple immediately, set the flag to
1623 <literal>false
</literal>, and set
1624 <literal>areq-
>callback_pending
</literal> to
<literal>true
</literal>
1625 for the
<structname>ForeignScan
</structname> node to get a callback from
1626 the callback functions described below. If no more tuples are available,
1627 set the slot to NULL or an empty slot, and the
1628 <literal>areq-
>request_complete
</literal> flag to
1629 <literal>true
</literal>. It's recommended to use
1630 <function>ExecAsyncRequestDone
</function> or
1631 <function>ExecAsyncRequestPending
</function> to set the output parameters
1632 in the
<literal>areq
</literal>.
1638 ForeignAsyncConfigureWait(AsyncRequest *areq);
1640 Configure a file descriptor event for which the
1641 <structname>ForeignScan
</structname> node wishes to wait.
1642 This function will only be called when the
1643 <structname>ForeignScan
</structname> node has the
1644 <literal>areq-
>callback_pending
</literal> flag set, and should add
1645 the event to the
<structfield>as_eventset
</structfield> of the parent
1646 <structname>Append
</structname> node described by the
1647 <literal>areq
</literal>. See the comments for
1648 <function>ExecAsyncConfigureWait
</function> in
1649 <filename>src/backend/executor/execAsync.c
</filename> for additional
1650 information. When the file descriptor event occurs,
1651 <function>ForeignAsyncNotify
</function> will be called.
1657 ForeignAsyncNotify(AsyncRequest *areq);
1659 Process a relevant event that has occurred, then produce one tuple
1660 asynchronously from the
<structname>ForeignScan
</structname> node.
1661 This function should set the output parameters in the
1662 <literal>areq
</literal> in the same way as
1663 <function>ForeignAsyncRequest
</function>.
1667 <sect2 id=
"fdw-callbacks-reparameterize-paths">
1668 <title>FDW Routines for Reparameterization of Paths
</title>
1673 ReparameterizeForeignPathByChild(PlannerInfo *root, List *fdw_private,
1674 RelOptInfo *child_rel);
1676 This function is called while converting a path parameterized by the
1677 top-most parent of the given child relation
<literal>child_rel
</literal> to be
1678 parameterized by the child relation. The function is used to reparameterize
1679 any paths or translate any expression nodes saved in the given
1680 <literal>fdw_private
</literal> member of a
<structname>ForeignPath
</structname>. The
1681 callback may use
<literal>reparameterize_path_by_child
</literal>,
1682 <literal>adjust_appendrel_attrs
</literal> or
1683 <literal>adjust_appendrel_attrs_multilevel
</literal> as required.
1689 <sect1 id=
"fdw-helpers">
1690 <title>Foreign Data Wrapper Helper Functions
</title>
1693 Several helper functions are exported from the core server so that
1694 authors of foreign data wrappers can get easy access to attributes of
1695 FDW-related objects, such as FDW options.
1696 To use any of these functions, you need to include the header file
1697 <filename>foreign/foreign.h
</filename> in your source file.
1698 That header also defines the struct types that are returned by
1704 ForeignDataWrapper *
1705 GetForeignDataWrapperExtended(Oid fdwid, bits16 flags);
1708 This function returns a
<structname>ForeignDataWrapper
</structname>
1709 object for the foreign-data wrapper with the given OID. A
1710 <structname>ForeignDataWrapper
</structname> object contains properties
1711 of the FDW (see
<filename>foreign/foreign.h
</filename> for details).
1712 <structfield>flags
</structfield> is a bitwise-or'd bit mask indicating
1713 an extra set of options. It can take the value
1714 <literal>FDW_MISSING_OK
</literal>, in which case a
<literal>NULL
</literal>
1715 result is returned to the caller instead of an error for an undefined
1721 ForeignDataWrapper *
1722 GetForeignDataWrapper(Oid fdwid);
1725 This function returns a
<structname>ForeignDataWrapper
</structname>
1726 object for the foreign-data wrapper with the given OID. A
1727 <structname>ForeignDataWrapper
</structname> object contains properties
1728 of the FDW (see
<filename>foreign/foreign.h
</filename> for details).
1734 GetForeignServerExtended(Oid serverid, bits16 flags);
1737 This function returns a
<structname>ForeignServer
</structname> object
1738 for the foreign server with the given OID. A
1739 <structname>ForeignServer
</structname> object contains properties
1740 of the server (see
<filename>foreign/foreign.h
</filename> for details).
1741 <structfield>flags
</structfield> is a bitwise-or'd bit mask indicating
1742 an extra set of options. It can take the value
1743 <literal>FSV_MISSING_OK
</literal>, in which case a
<literal>NULL
</literal>
1744 result is returned to the caller instead of an error for an undefined
1751 GetForeignServer(Oid serverid);
1754 This function returns a
<structname>ForeignServer
</structname> object
1755 for the foreign server with the given OID. A
1756 <structname>ForeignServer
</structname> object contains properties
1757 of the server (see
<filename>foreign/foreign.h
</filename> for details).
1763 GetUserMapping(Oid userid, Oid serverid);
1766 This function returns a
<structname>UserMapping
</structname> object for
1767 the user mapping of the given role on the given server. (If there is no
1768 mapping for the specific user, it will return the mapping for
1769 <literal>PUBLIC
</literal>, or throw error if there is none.) A
1770 <structname>UserMapping
</structname> object contains properties of the
1771 user mapping (see
<filename>foreign/foreign.h
</filename> for details).
1777 GetForeignTable(Oid relid);
1780 This function returns a
<structname>ForeignTable
</structname> object for
1781 the foreign table with the given OID. A
1782 <structname>ForeignTable
</structname> object contains properties of the
1783 foreign table (see
<filename>foreign/foreign.h
</filename> for details).
1789 GetForeignColumnOptions(Oid relid, AttrNumber attnum);
1792 This function returns the per-column FDW options for the column with the
1793 given foreign table OID and attribute number, in the form of a list of
1794 <structname>DefElem
</structname>. NIL is returned if the column has no
1799 Some object types have name-based lookup functions in addition to the
1805 ForeignDataWrapper *
1806 GetForeignDataWrapperByName(const char *name, bool missing_ok);
1809 This function returns a
<structname>ForeignDataWrapper
</structname>
1810 object for the foreign-data wrapper with the given name. If the wrapper
1811 is not found, return NULL if missing_ok is true, otherwise raise an
1818 GetForeignServerByName(const char *name, bool missing_ok);
1821 This function returns a
<structname>ForeignServer
</structname> object
1822 for the foreign server with the given name. If the server is not found,
1823 return NULL if missing_ok is true, otherwise raise an error.
1828 <sect1 id=
"fdw-planning">
1829 <title>Foreign Data Wrapper Query Planning
</title>
1832 The FDW callback functions
<function>GetForeignRelSize
</function>,
1833 <function>GetForeignPaths
</function>,
<function>GetForeignPlan
</function>,
1834 <function>PlanForeignModify
</function>,
<function>GetForeignJoinPaths
</function>,
1835 <function>GetForeignUpperPaths
</function>, and
<function>PlanDirectModify
</function>
1836 must fit into the workings of the
<productname>PostgreSQL
</productname> planner.
1837 Here are some notes about what they must do.
1841 The information in
<literal>root
</literal> and
<literal>baserel
</literal> can be used
1842 to reduce the amount of information that has to be fetched from the
1843 foreign table (and therefore reduce the cost).
1844 <literal>baserel-
>baserestrictinfo
</literal> is particularly interesting, as
1845 it contains restriction quals (
<literal>WHERE
</literal> clauses) that should be
1846 used to filter the rows to be fetched. (The FDW itself is not required
1847 to enforce these quals, as the core executor can check them instead.)
1848 <literal>baserel-
>reltarget-
>exprs
</literal> can be used to determine which
1849 columns need to be fetched; but note that it only lists columns that
1850 have to be emitted by the
<structname>ForeignScan
</structname> plan node, not
1851 columns that are used in qual evaluation but not output by the query.
1855 Various private fields are available for the FDW planning functions to
1856 keep information in. Generally, whatever you store in FDW private fields
1857 should be palloc'd, so that it will be reclaimed at the end of planning.
1861 <literal>baserel-
>fdw_private
</literal> is a
<type>void
</type> pointer that is
1862 available for FDW planning functions to store information relevant to
1863 the particular foreign table. The core planner does not touch it except
1864 to initialize it to NULL when the
<literal>RelOptInfo
</literal> node is created.
1865 It is useful for passing information forward from
1866 <function>GetForeignRelSize
</function> to
<function>GetForeignPaths
</function> and/or
1867 <function>GetForeignPaths
</function> to
<function>GetForeignPlan
</function>, thereby
1868 avoiding recalculation.
1872 <function>GetForeignPaths
</function> can identify the meaning of different
1873 access paths by storing private information in the
1874 <structfield>fdw_private
</structfield> field of
<structname>ForeignPath
</structname> nodes.
1875 <structfield>fdw_private
</structfield> is declared as a
<type>List
</type> pointer, but
1876 could actually contain anything since the core planner does not touch
1877 it. However, best practice is to use a representation that's dumpable
1878 by
<function>nodeToString
</function>, for use with debugging support available
1883 <function>GetForeignPlan
</function> can examine the
<structfield>fdw_private
</structfield>
1884 field of the selected
<structname>ForeignPath
</structname> node, and can generate
1885 <structfield>fdw_exprs
</structfield> and
<structfield>fdw_private
</structfield> lists to be
1886 placed in the
<structname>ForeignScan
</structname> plan node, where they will be
1887 available at execution time. Both of these lists must be
1888 represented in a form that
<function>copyObject
</function> knows how to copy.
1889 The
<structfield>fdw_private
</structfield> list has no other restrictions and is
1890 not interpreted by the core backend in any way. The
1891 <structfield>fdw_exprs
</structfield> list, if not NIL, is expected to contain
1892 expression trees that are intended to be executed at run time. These
1893 trees will undergo post-processing by the planner to make them fully
1898 In
<function>GetForeignPlan
</function>, generally the passed-in target list can
1899 be copied into the plan node as-is. The passed
<literal>scan_clauses
</literal> list
1900 contains the same clauses as
<literal>baserel-
>baserestrictinfo
</literal>,
1901 but may be re-ordered for better execution efficiency. In simple cases
1902 the FDW can just strip
<structname>RestrictInfo
</structname> nodes from the
1903 <literal>scan_clauses
</literal> list (using
<function>extract_actual_clauses
</function>) and put
1904 all the clauses into the plan node's qual list, which means that all the
1905 clauses will be checked by the executor at run time. More complex FDWs
1906 may be able to check some of the clauses internally, in which case those
1907 clauses can be removed from the plan node's qual list so that the
1908 executor doesn't waste time rechecking them.
1912 As an example, the FDW might identify some restriction clauses of the
1913 form
<replaceable>foreign_variable
</replaceable> <literal>=
</literal>
1914 <replaceable>sub_expression
</replaceable>, which it determines can be executed on
1915 the remote server given the locally-evaluated value of the
1916 <replaceable>sub_expression
</replaceable>. The actual identification of such a
1917 clause should happen during
<function>GetForeignPaths
</function>, since it would
1918 affect the cost estimate for the path. The path's
1919 <structfield>fdw_private
</structfield> field would probably include a pointer to
1920 the identified clause's
<structname>RestrictInfo
</structname> node. Then
1921 <function>GetForeignPlan
</function> would remove that clause from
<literal>scan_clauses
</literal>,
1922 but add the
<replaceable>sub_expression
</replaceable> to
<structfield>fdw_exprs
</structfield>
1923 to ensure that it gets massaged into executable form. It would probably
1924 also put control information into the plan node's
1925 <structfield>fdw_private
</structfield> field to tell the execution functions what
1926 to do at run time. The query transmitted to the remote server would
1927 involve something like
<literal>WHERE
<replaceable>foreign_variable
</replaceable> =
1928 $
1</literal>, with the parameter value obtained at run time from
1929 evaluation of the
<structfield>fdw_exprs
</structfield> expression tree.
1933 Any clauses removed from the plan node's qual list must instead be added
1934 to
<literal>fdw_recheck_quals
</literal> or rechecked by
1935 <literal>RecheckForeignScan
</literal> in order to ensure correct behavior
1936 at the
<literal>READ COMMITTED
</literal> isolation level. When a concurrent
1937 update occurs for some other table involved in the query, the executor
1938 may need to verify that all of the original quals are still satisfied for
1939 the tuple, possibly against a different set of parameter values. Using
1940 <literal>fdw_recheck_quals
</literal> is typically easier than implementing checks
1941 inside
<literal>RecheckForeignScan
</literal>, but this method will be
1942 insufficient when outer joins have been pushed down, since the join tuples
1943 in that case might have some fields go to NULL without rejecting the
1948 Another
<structname>ForeignScan
</structname> field that can be filled by FDWs
1949 is
<structfield>fdw_scan_tlist
</structfield>, which describes the tuples returned by
1950 the FDW for this plan node. For simple foreign table scans this can be
1951 set to
<literal>NIL
</literal>, implying that the returned tuples have the
1952 row type declared for the foreign table. A non-
<symbol>NIL
</symbol> value must be a
1953 target list (list of
<structname>TargetEntry
</structname>s) containing Vars and/or
1954 expressions representing the returned columns. This might be used, for
1955 example, to show that the FDW has omitted some columns that it noticed
1956 won't be needed for the query. Also, if the FDW can compute expressions
1957 used by the query more cheaply than can be done locally, it could add
1958 those expressions to
<structfield>fdw_scan_tlist
</structfield>. Note that join
1959 plans (created from paths made by
<function>GetForeignJoinPaths
</function>) must
1960 always supply
<structfield>fdw_scan_tlist
</structfield> to describe the set of
1961 columns they will return.
1965 The FDW should always construct at least one path that depends only on
1966 the table's restriction clauses. In join queries, it might also choose
1967 to construct path(s) that depend on join clauses, for example
1968 <replaceable>foreign_variable
</replaceable> <literal>=
</literal>
1969 <replaceable>local_variable
</replaceable>. Such clauses will not be found in
1970 <literal>baserel-
>baserestrictinfo
</literal> but must be sought in the
1971 relation's join lists. A path using such a clause is called a
1972 <quote>parameterized path
</quote>. It must identify the other relations
1973 used in the selected join clause(s) with a suitable value of
1974 <literal>param_info
</literal>; use
<function>get_baserel_parampathinfo
</function>
1975 to compute that value. In
<function>GetForeignPlan
</function>, the
1976 <replaceable>local_variable
</replaceable> portion of the join clause would be added
1977 to
<structfield>fdw_exprs
</structfield>, and then at run time the case works the
1978 same as for an ordinary restriction clause.
1982 If an FDW supports remote joins,
<function>GetForeignJoinPaths
</function> should
1983 produce
<structname>ForeignPath
</structname>s for potential remote joins in much
1984 the same way as
<function>GetForeignPaths
</function> works for base tables.
1985 Information about the intended join can be passed forward
1986 to
<function>GetForeignPlan
</function> in the same ways described above.
1987 However,
<structfield>baserestrictinfo
</structfield> is not relevant for join
1988 relations; instead, the relevant join clauses for a particular join are
1989 passed to
<function>GetForeignJoinPaths
</function> as a separate parameter
1990 (
<literal>extra-
>restrictlist
</literal>).
1994 An FDW might additionally support direct execution of some plan actions
1995 that are above the level of scans and joins, such as grouping or
1996 aggregation. To offer such options, the FDW should generate paths and
1997 insert them into the appropriate
<firstterm>upper relation
</firstterm>. For
1998 example, a path representing remote aggregation should be inserted into
1999 the
<literal>UPPERREL_GROUP_AGG
</literal> relation, using
<function>add_path
</function>.
2000 This path will be compared on a cost basis with local aggregation
2001 performed by reading a simple scan path for the foreign relation (note
2002 that such a path must also be supplied, else there will be an error at
2003 plan time). If the remote-aggregation path wins, which it usually would,
2004 it will be converted into a plan in the usual way, by
2005 calling
<function>GetForeignPlan
</function>. The recommended place to generate
2006 such paths is in the
<function>GetForeignUpperPaths
</function>
2007 callback function, which is called for each upper relation (i.e., each
2008 post-scan/join processing step), if all the base relations of the query
2009 come from the same FDW.
2013 <function>PlanForeignModify
</function> and the other callbacks described in
2014 <xref linkend=
"fdw-callbacks-update"/> are designed around the assumption
2015 that the foreign relation will be scanned in the usual way and then
2016 individual row updates will be driven by a local
<literal>ModifyTable
</literal>
2017 plan node. This approach is necessary for the general case where an
2018 update requires reading local tables as well as foreign tables.
2019 However, if the operation could be executed entirely by the foreign
2020 server, the FDW could generate a path representing that and insert it
2021 into the
<literal>UPPERREL_FINAL
</literal> upper relation, where it would
2022 compete against the
<literal>ModifyTable
</literal> approach. This approach
2023 could also be used to implement remote
<literal>SELECT FOR UPDATE
</literal>,
2024 rather than using the row locking callbacks described in
2025 <xref linkend=
"fdw-callbacks-row-locking"/>. Keep in mind that a path
2026 inserted into
<literal>UPPERREL_FINAL
</literal> is responsible for
2027 implementing
<emphasis>all
</emphasis> behavior of the query.
2031 When planning an
<command>UPDATE
</command> or
<command>DELETE
</command>,
2032 <function>PlanForeignModify
</function> and
<function>PlanDirectModify
</function>
2033 can look up the
<structname>RelOptInfo
</structname>
2034 struct for the foreign table and make use of the
2035 <literal>baserel-
>fdw_private
</literal> data previously created by the
2036 scan-planning functions. However, in
<command>INSERT
</command> the target
2037 table is not scanned so there is no
<structname>RelOptInfo
</structname> for it.
2038 The
<structname>List
</structname> returned by
<function>PlanForeignModify
</function> has
2039 the same restrictions as the
<structfield>fdw_private
</structfield> list of a
2040 <structname>ForeignScan
</structname> plan node, that is it must contain only
2041 structures that
<function>copyObject
</function> knows how to copy.
2045 <command>INSERT
</command> with an
<literal>ON CONFLICT
</literal> clause does not
2046 support specifying the conflict target, as unique constraints or
2047 exclusion constraints on remote tables are not locally known. This
2048 in turn implies that
<literal>ON CONFLICT DO UPDATE
</literal> is not supported,
2049 since the specification is mandatory there.
2054 <sect1 id=
"fdw-row-locking">
2055 <title>Row Locking in Foreign Data Wrappers
</title>
2058 If an FDW's underlying storage mechanism has a concept of locking
2059 individual rows to prevent concurrent updates of those rows, it is
2060 usually worthwhile for the FDW to perform row-level locking with as
2061 close an approximation as practical to the semantics used in
2062 ordinary
<productname>PostgreSQL
</productname> tables. There are multiple
2063 considerations involved in this.
2067 One key decision to be made is whether to perform
<firstterm>early
2068 locking
</firstterm> or
<firstterm>late locking
</firstterm>. In early locking, a row is
2069 locked when it is first retrieved from the underlying store, while in
2070 late locking, the row is locked only when it is known that it needs to
2071 be locked. (The difference arises because some rows may be discarded by
2072 locally-checked restriction or join conditions.) Early locking is much
2073 simpler and avoids extra round trips to a remote store, but it can cause
2074 locking of rows that need not have been locked, resulting in reduced
2075 concurrency or even unexpected deadlocks. Also, late locking is only
2076 possible if the row to be locked can be uniquely re-identified later.
2077 Preferably the row identifier should identify a specific version of the
2078 row, as
<productname>PostgreSQL
</productname> TIDs do.
2082 By default,
<productname>PostgreSQL
</productname> ignores locking considerations
2083 when interfacing to FDWs, but an FDW can perform early locking without
2084 any explicit support from the core code. The API functions described
2085 in
<xref linkend=
"fdw-callbacks-row-locking"/>, which were added
2086 in
<productname>PostgreSQL
</productname> 9.5, allow an FDW to use late locking if
2091 An additional consideration is that in
<literal>READ COMMITTED
</literal>
2092 isolation mode,
<productname>PostgreSQL
</productname> may need to re-check
2093 restriction and join conditions against an updated version of some
2094 target tuple. Rechecking join conditions requires re-obtaining copies
2095 of the non-target rows that were previously joined to the target tuple.
2096 When working with standard
<productname>PostgreSQL
</productname> tables, this is
2097 done by including the TIDs of the non-target tables in the column list
2098 projected through the join, and then re-fetching non-target rows when
2099 required. This approach keeps the join data set compact, but it
2100 requires inexpensive re-fetch capability, as well as a TID that can
2101 uniquely identify the row version to be re-fetched. By default,
2102 therefore, the approach used with foreign tables is to include a copy of
2103 the entire row fetched from a foreign table in the column list projected
2104 through the join. This puts no special demands on the FDW but can
2105 result in reduced performance of merge and hash joins. An FDW that is
2106 capable of meeting the re-fetch requirements can choose to do it the
2111 For an
<command>UPDATE
</command> or
<command>DELETE
</command> on a foreign table, it
2112 is recommended that the
<literal>ForeignScan
</literal> operation on the target
2113 table perform early locking on the rows that it fetches, perhaps via the
2114 equivalent of
<command>SELECT FOR UPDATE
</command>. An FDW can detect whether
2115 a table is an
<command>UPDATE
</command>/
<command>DELETE
</command> target at plan time
2116 by comparing its relid to
<literal>root-
>parse-
>resultRelation
</literal>,
2117 or at execution time by using
<function>ExecRelationIsTargetRelation()
</function>.
2118 An alternative possibility is to perform late locking within the
2119 <function>ExecForeignUpdate
</function> or
<function>ExecForeignDelete
</function>
2120 callback, but no special support is provided for this.
2124 For foreign tables that are specified to be locked by a
<command>SELECT
2125 FOR UPDATE/SHARE
</command> command, the
<literal>ForeignScan
</literal> operation can
2126 again perform early locking by fetching tuples with the equivalent
2127 of
<command>SELECT FOR UPDATE/SHARE
</command>. To perform late locking
2128 instead, provide the callback functions defined
2129 in
<xref linkend=
"fdw-callbacks-row-locking"/>.
2130 In
<function>GetForeignRowMarkType
</function>, select rowmark option
2131 <literal>ROW_MARK_EXCLUSIVE
</literal>,
<literal>ROW_MARK_NOKEYEXCLUSIVE
</literal>,
2132 <literal>ROW_MARK_SHARE
</literal>, or
<literal>ROW_MARK_KEYSHARE
</literal> depending
2133 on the requested lock strength. (The core code will act the same
2134 regardless of which of these four options you choose.)
2135 Elsewhere, you can detect whether a foreign table was specified to be
2136 locked by this type of command by using
<function>get_plan_rowmark
</function> at
2137 plan time, or
<function>ExecFindRowMark
</function> at execution time; you must
2138 check not only whether a non-null rowmark struct is returned, but that
2139 its
<structfield>strength
</structfield> field is not
<literal>LCS_NONE
</literal>.
2143 Lastly, for foreign tables that are used in an
<command>UPDATE
</command>,
2144 <command>DELETE
</command> or
<command>SELECT FOR UPDATE/SHARE
</command> command but
2145 are not specified to be row-locked, you can override the default choice
2146 to copy entire rows by having
<function>GetForeignRowMarkType
</function> select
2147 option
<literal>ROW_MARK_REFERENCE
</literal> when it sees lock strength
2148 <literal>LCS_NONE
</literal>. This will cause
<function>RefetchForeignRow
</function> to
2149 be called with that value for
<structfield>markType
</structfield>; it should then
2150 re-fetch the row without acquiring any new lock. (If you have
2151 a
<function>GetForeignRowMarkType
</function> function but don't wish to re-fetch
2152 unlocked rows, select option
<literal>ROW_MARK_COPY
</literal>
2153 for
<literal>LCS_NONE
</literal>.)
2157 See
<filename>src/include/nodes/lockoptions.h
</filename>, the comments
2158 for
<type>RowMarkType
</type> and
<type>PlanRowMark
</type>
2159 in
<filename>src/include/nodes/plannodes.h
</filename>, and the comments for
2160 <type>ExecRowMark
</type> in
<filename>src/include/nodes/execnodes.h
</filename> for
2161 additional information.