57.2. Foreign Data Wrapper Callback Routines | ||||
---|---|---|---|---|
Prev | Up | Chapter 57. Writing a Foreign Data Wrapper | Home | Next |
57.2. Foreign Data Wrapper Callback Routines#
- 57.2.1. FDW Routines for Scanning Foreign Tables
- 57.2.2. FDW Routines for Scanning Foreign Joins
- 57.2.3. FDW Routines for Planning Post-Scan/Join Processing
- 57.2.4. FDW Routines for Updating Foreign Tables
- 57.2.5. FDW Routines for
TRUNCATE
- 57.2.6. FDW Routines for Row Locking
- 57.2.7. FDW Routines for
EXPLAIN
- 57.2.8. FDW Routines for
ANALYZE
- 57.2.9. FDW Routines for
IMPORT FOREIGN SCHEMA
- 57.2.10. FDW Routines for Parallel Execution
- 57.2.11. FDW Routines for Asynchronous Execution
- 57.2.12. FDW Routines for Reparameterization of Paths
- 57.2.2. FDW Routines for Scanning Foreign Joins
The FDW handler function returns a palloc'dFdwRoutine
struct containing pointers to the callback functions described below. The scan-related functions are required, the rest are optional.
TheFdwRoutine
struct type is declared insrc/include/foreign/fdwapi.h
, which see for additional details.
57.2.1. FDW Routines for Scanning Foreign Tables#
voidGetForeignRelSize(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid);
Obtain relation size estimates for a foreign table. This is called at the beginning of planning for a query that scans a foreign table.root
is the planner's global information about the query;baserel
is the planner's information about this table; andforeigntableid
is thepg_class
OID of the foreign table. (foreigntableid
could be obtained from the planner data structures, but it's passed explicitly to save effort.)
This function should updatebaserel->rows
to be the expected number of rows returned by the table scan, after accounting for the filtering done by the restriction quals. The initial value ofbaserel->rows
is just a constant default estimate, which should be replaced if at all possible. The function may also choose to updatebaserel->width
if it can compute a better estimate of the average result row width. (The initial value is based on column data types and on column average-width values measured by the lastANALYZE
.) Also, this function may updatebaserel->tuples
if it can compute a better estimate of the foreign table's total row count. (The initial value is frompg_class
.reltuples
which represents the total row count seen by the lastANALYZE
; it will be-1
if noANALYZE
has been done on this foreign table.)
SeeSection 57.4 for additional information.
voidGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid);
Create possible access paths for a scan on a foreign table. This is called during query planning. The parameters are the same as forGetForeignRelSize
, which has already been called.
This function must generate at least one access path (ForeignPath
node) for a scan on the foreign table and must calladd_path
to add each such path tobaserel->pathlist
. It's recommended to usecreate_foreignscan_path
to build theForeignPath
nodes. The function can generate multiple access paths, e.g., a path which has validpathkeys
to represent a pre-sorted result. Each access path must contain cost estimates, and can contain any FDW-private information that is needed to identify the specific scan method intended.
SeeSection 57.4 for additional information.
ForeignScan *GetForeignPlan(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid, ForeignPath *best_path, List *tlist, List *scan_clauses, Plan *outer_plan);
Create aForeignScan
plan node from the selected foreign access path. This is called at the end of query planning. The parameters are as forGetForeignRelSize
, plus the selectedForeignPath
(previously produced byGetForeignPaths
,GetForeignJoinPaths
, orGetForeignUpperPaths
), the target list to be emitted by the plan node, the restriction clauses to be enforced by the plan node, and the outer subplan of theForeignScan
, which is used for rechecks performed byRecheckForeignScan
. (If the path is for a join rather than a base relation,foreigntableid
isInvalidOid
.)
This function must create and return aForeignScan
plan node; it's recommended to usemake_foreignscan
to build theForeignScan
node.
SeeSection 57.4 for additional information.
voidBeginForeignScan(ForeignScanState *node, int eflags);
Begin executing a foreign scan. This is called during executor startup. It should perform any initialization needed before the scan can start, but not start executing the actual scan (that should be done upon the first call toIterateForeignScan
). TheForeignScanState
node has already been created, but itsfdw_state
field is still NULL. Information about the table to scan is accessible through theForeignScanState
node (in particular, from the underlyingForeignScan
plan node, which contains any FDW-private information provided byGetForeignPlan
).eflags
contains flag bits describing the executor's operating mode for this plan node.
Note that when(eflags & EXEC_FLAG_EXPLAIN_ONLY)
is true, this function should not perform any externally-visible actions; it should only do the minimum required to make the node state valid forExplainForeignScan
andEndForeignScan
.
TupleTableSlot *IterateForeignScan(ForeignScanState *node);
Fetch one row from the foreign source, returning it in a tuple table slot (the node'sScanTupleSlot
should be used for this purpose). Return NULL if no more rows are available. The tuple table slot infrastructure allows either a physical or virtual tuple to be returned; in most cases the latter choice is preferable from a performance standpoint. Note that this is called in a short-lived memory context that will be reset between invocations. Create a memory context inBeginForeignScan
if you need longer-lived storage, or use thees_query_cxt
of the node'sEState
.
The rows returned must match thefdw_scan_tlist
target list if one was supplied, otherwise they must match the row type of the foreign table being scanned. If you choose to optimize away fetching columns that are not needed, you should insert nulls in those column positions, or else generate afdw_scan_tlist
list with those columns omitted.
Note thatPostgreSQL's executor doesn't care whether the rows returned violate any constraints that were defined on the foreign table — but the planner does care, and may optimize queries incorrectly if there are rows visible in the foreign table that do not satisfy a declared constraint. If a constraint is violated when the user has declared that the constraint should hold true, it may be appropriate to raise an error (just as you would need to do in the case of a data type mismatch).
voidReScanForeignScan(ForeignScanState *node);
Restart the scan from the beginning. Note that any parameters the scan depends on may have changed value, so the new scan does not necessarily return exactly the same rows.
voidEndForeignScan(ForeignScanState *node);
End the scan and release resources. It is normally not important to release palloc'd memory, but for example open files and connections to remote servers should be cleaned up.
57.2.2. FDW Routines for Scanning Foreign Joins#
If an FDW supports performing foreign joins remotely (rather than by fetching both tables' data and doing the join locally), it should provide this callback function:
voidGetForeignJoinPaths(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra);
Create possible access paths for a join of two (or more) foreign tables that all belong to the same foreign server. This optional function is called during query planning. As withGetForeignPaths
, this function should generateForeignPath
path(s) for the suppliedjoinrel
(usecreate_foreign_join_path
to build them), and calladd_path
to add these paths to the set of paths considered for the join. But unlikeGetForeignPaths
, it is not necessary that this function succeed in creating at least one path, since paths involving local joining are always possible.
Note that this function will be invoked repeatedly for the same join relation, with different combinations of inner and outer relations; it is the responsibility of the FDW to minimize duplicated work.
Note also that the set of join clauses to apply to the join, which is passed asextra->restrictlist
, varies depending on the combination of inner and outer relations. AForeignPath
path generated for thejoinrel
must contain the set of join clauses it uses, which will be used by the planner to convert theForeignPath
path into a plan, if it is selected by the planner as the best path for thejoinrel
.
If aForeignPath
path is chosen for the join, it will represent the entire join process; paths generated for the component tables and subsidiary joins will not be used. Subsequent processing of the join path proceeds much as it does for a path scanning a single foreign table. One difference is that thescanrelid
of the resultingForeignScan
plan node should be set to zero, since there is no single relation that it represents; instead, thefs_relids
field of theForeignScan
node represents the set of relations that were joined. (The latter field is set up automatically by the core planner code, and need not be filled by the FDW.) Another difference is that, because the column list for a remote join cannot be found from the system catalogs, the FDW must fillfdw_scan_tlist
with an appropriate list ofTargetEntry
nodes, representing the set of columns it will supply at run time in the tuples it returns.
Note
Beginning withPostgreSQL 16,fs_relids
includes the rangetable indexes of outer joins, if any were involved in this join. The new fieldfs_base_relids
includes only base relation indexes, and thus mimicsfs_relids
's old semantics.
SeeSection 57.4 for additional information.
57.2.3. FDW Routines for Planning Post-Scan/Join Processing#
If an FDW supports performing remote post-scan/join processing, such as remote aggregation, it should provide this callback function:
voidGetForeignUpperPaths(PlannerInfo *root, UpperRelationKind stage, RelOptInfo *input_rel, RelOptInfo *output_rel, void *extra);
Create possible access paths forupper relation processing, which is the planner's term for all post-scan/join query processing, such as aggregation, window functions, sorting, and table updates. This optional function is called during query planning. Currently, it is called only if all base relation(s) involved in the query belong to the same FDW. This function should generateForeignPath
path(s) for any post-scan/join processing that the FDW knows how to perform remotely (usecreate_foreign_upper_path
to build them), and calladd_path
to add these paths to the indicated upper relation. As withGetForeignJoinPaths
, it is not necessary that this function succeed in creating any paths, since paths involving local processing are always possible.
Thestage
parameter identifies which post-scan/join step is currently being considered.output_rel
is the upper relation that should receive paths representing computation of this step, andinput_rel
is the relation representing the input to this step. Theextra
parameter provides additional details, currently, it is set only forUPPERREL_PARTIAL_GROUP_AGG
orUPPERREL_GROUP_AGG
, in which case it points to aGroupPathExtraData
structure; or forUPPERREL_FINAL
, in which case it points to aFinalPathExtraData
structure. (Note thatForeignPath
paths added tooutput_rel
would typically not have any direct dependency on paths of theinput_rel
, since their processing is expected to be done externally. However, examining paths previously generated for the previous processing step can be useful to avoid redundant planning work.)
SeeSection 57.4 for additional information.
57.2.4. FDW Routines for Updating Foreign Tables#
If an FDW supports writable foreign tables, it should provide some or all of the following callback functions depending on the needs and capabilities of the FDW:
voidAddForeignUpdateTargets(PlannerInfo *root, Index rtindex, RangeTblEntry *target_rte, Relation target_relation);
UPDATE
andDELETE
operations are performed against rows previously fetched by the table-scanning functions. The FDW may need extra information, such as a row ID or the values of primary-key columns, to ensure that it can identify the exact row to update or delete. To support that, this function can add extra hidden, or“junk”, target columns to the list of columns that are to be retrieved from the foreign table during anUPDATE
orDELETE
.
To do that, construct aVar
representing an extra value you need, and pass it toadd_row_identity_var
, along with a name for the junk column. (You can do this more than once if several columns are needed.) You must choose a distinct junk column name for each differentVar
you need, except thatVar
s that are identical except for thevarno
field can and should share a column name. The core system uses the junk column namestableoid
for a table'stableoid
column,ctid
orctid
forN
ctid
,wholerow
for a whole-rowVar
marked withvartype
=RECORD
, andwholerow
for a whole-rowN
Var
withvartype
equal to the table's declared row type. Re-use these names when you can (the planner will combine duplicate requests for identical junk columns). If you need another kind of junk column besides these, it might be wise to choose a name prefixed with your extension name, to avoid conflicts against other FDWs.
If theAddForeignUpdateTargets
pointer is set toNULL
, no extra target expressions are added. (This will make it impossible to implementDELETE
operations, thoughUPDATE
may still be feasible if the FDW relies on an unchanging primary key to identify rows.)
List *PlanForeignModify(PlannerInfo *root, ModifyTable *plan, Index resultRelation, int subplan_index);
Perform any additional planning actions needed for an insert, update, or delete on a foreign table. This function generates the FDW-private information that will be attached to theModifyTable
plan node that performs the update action. This private information must have the form of aList
, and will be delivered toBeginForeignModify
during the execution stage.
root
is the planner's global information about the query.plan
is theModifyTable
plan node, which is complete except for thefdwPrivLists
field.resultRelation
identifies the target foreign table by its range table index.subplan_index
identifies which target of theModifyTable
plan node this is, counting from zero; use this if you want to index into per-target-relation substructures of theplan
node.
SeeSection 57.4 for additional information.
If thePlanForeignModify
pointer is set toNULL
, no additional plan-time actions are taken, and thefdw_private
list delivered toBeginForeignModify
will be NIL.
voidBeginForeignModify(ModifyTableState *mtstate, ResultRelInfo *rinfo, List *fdw_private, int subplan_index, int eflags);
Begin executing a foreign table modification operation. This routine is called during executor startup. It should perform any initialization needed prior to the actual table modifications. Subsequently,ExecForeignInsert/ExecForeignBatchInsert
,ExecForeignUpdate
orExecForeignDelete
will be called for tuple(s) to be inserted, updated, or deleted.
mtstate
is the overall state of theModifyTable
plan node being executed; global data about the plan and execution state is available via this structure.rinfo
is theResultRelInfo
struct describing the target foreign table. (Theri_FdwState
field ofResultRelInfo
is available for the FDW to store any private state it needs for this operation.)fdw_private
contains the private data generated byPlanForeignModify
, if any.subplan_index
identifies which target of theModifyTable
plan node this is.eflags
contains flag bits describing the executor's operating mode for this plan node.
Note that when(eflags & EXEC_FLAG_EXPLAIN_ONLY)
is true, this function should not perform any externally-visible actions; it should only do the minimum required to make the node state valid forExplainForeignModify
andEndForeignModify
.
If theBeginForeignModify
pointer is set toNULL
, no action is taken during executor startup.
TupleTableSlot *ExecForeignInsert(EState *estate, ResultRelInfo *rinfo, TupleTableSlot *slot, TupleTableSlot *planSlot);
Insert one tuple into the foreign table.estate
is global execution state for the query.rinfo
is theResultRelInfo
struct describing the target foreign table.slot
contains the tuple to be inserted; it will match the row-type definition of the foreign table.planSlot
contains the tuple that was generated by theModifyTable
plan node's subplan; it differs fromslot
in possibly containing additional“junk” columns. (TheplanSlot
is typically of little interest forINSERT
cases, but is provided for completeness.)
The return value is either a slot containing the data that was actually inserted (this might differ from the data supplied, for example as a result of trigger actions), or NULL if no row was actually inserted (again, typically as a result of triggers). The passed-inslot
can be re-used for this purpose.
The data in the returned slot is used only if theINSERT
statement has aRETURNING
clause or involves a viewWITH CHECK OPTION
; or if the foreign table has anAFTER ROW
trigger. Triggers require all columns, but the FDW could choose to optimize away returning some or all columns depending on the contents of theRETURNING
clause orWITH CHECK OPTION
constraints. Regardless, some slot must be returned to indicate success, or the query's reported row count will be wrong.
If theExecForeignInsert
pointer is set toNULL
, attempts to insert into the foreign table will fail with an error message.
Note that this function is also called when inserting routed tuples into a foreign-table partition or executingCOPY FROM
on a foreign table, in which case it is called in a different way than it is in theINSERT
case. See the callback functions described below that allow the FDW to support that.
TupleTableSlot **ExecForeignBatchInsert(EState *estate, ResultRelInfo *rinfo, TupleTableSlot **slots, TupleTableSlot **planSlots, int *numSlots);
Insert multiple tuples in bulk into the foreign table. The parameters are the same forExecForeignInsert
exceptslots
andplanSlots
contain multiple tuples and*numSlots
specifies the number of tuples in those arrays.
The return value is an array of slots containing the data that was actually inserted (this might differ from the data supplied, for example as a result of trigger actions.) The passed-inslots
can be re-used for this purpose. The number of successfully inserted tuples is returned in*numSlots
.
The data in the returned slot is used only if theINSERT
statement involves a viewWITH CHECK OPTION
; or if the foreign table has anAFTER ROW
trigger. Triggers require all columns, but the FDW could choose to optimize away returning some or all columns depending on the contents of theWITH CHECK OPTION
constraints.
If theExecForeignBatchInsert
orGetForeignModifyBatchSize
pointer is set toNULL
, attempts to insert into the foreign table will useExecForeignInsert
. This function is not used if theINSERT
has theRETURNING
clause.
Note that this function is also called when inserting routed tuples into a foreign-table partition or executingCOPY FROM
on a foreign table, in which case it is called in a different way than it is in theINSERT
case. See the callback functions described below that allow the FDW to support that.
intGetForeignModifyBatchSize(ResultRelInfo *rinfo);
Report the maximum number of tuples that a singleExecForeignBatchInsert
call can handle for the specified foreign table. The executor passes at most the given number of tuples toExecForeignBatchInsert
.rinfo
is theResultRelInfo
struct describing the target foreign table. The FDW is expected to provide a foreign server and/or foreign table option for the user to set this value, or some hard-coded value.
If theExecForeignBatchInsert
orGetForeignModifyBatchSize
pointer is set toNULL
, attempts to insert into the foreign table will useExecForeignInsert
.
TupleTableSlot *ExecForeignUpdate(EState *estate, ResultRelInfo *rinfo, TupleTableSlot *slot, TupleTableSlot *planSlot);
Update one tuple in the foreign table.estate
is global execution state for the query.rinfo
is theResultRelInfo
struct describing the target foreign table.slot
contains the new data for the tuple; it will match the row-type definition of the foreign table.planSlot
contains the tuple that was generated by theModifyTable
plan node's subplan. Unlikeslot
, this tuple contains only the new values for columns changed by the query, so do not rely on attribute numbers of the foreign table to index intoplanSlot
. Also,planSlot
typically contains additional“junk” columns. In particular, any junk columns that were requested byAddForeignUpdateTargets
will be available from this slot.
The return value is either a slot containing the row as it was actually updated (this might differ from the data supplied, for example as a result of trigger actions), or NULL if no row was actually updated (again, typically as a result of triggers). The passed-inslot
can be re-used for this purpose.
The data in the returned slot is used only if theUPDATE
statement has aRETURNING
clause or involves a viewWITH CHECK OPTION
; or if the foreign table has anAFTER ROW
trigger. Triggers require all columns, but the FDW could choose to optimize away returning some or all columns depending on the contents of theRETURNING
clause orWITH CHECK OPTION
constraints. Regardless, some slot must be returned to indicate success, or the query's reported row count will be wrong.
If theExecForeignUpdate
pointer is set toNULL
, attempts to update the foreign table will fail with an error message.
TupleTableSlot *ExecForeignDelete(EState *estate, ResultRelInfo *rinfo, TupleTableSlot *slot, TupleTableSlot *planSlot);
Delete one tuple from the foreign table.estate
is global execution state for the query.rinfo
is theResultRelInfo
struct describing the target foreign table.slot
contains nothing useful upon call, but can be used to hold the returned tuple.planSlot
contains the tuple that was generated by theModifyTable
plan node's subplan; in particular, it will carry any junk columns that were requested byAddForeignUpdateTargets
. The junk column(s) must be used to identify the tuple to be deleted.
The return value is either a slot containing the row that was deleted, or NULL if no row was deleted (typically as a result of triggers). The passed-inslot
can be used to hold the tuple to be returned.
The data in the returned slot is used only if theDELETE
query has aRETURNING
clause or the foreign table has anAFTER ROW
trigger. Triggers require all columns, but the FDW could choose to optimize away returning some or all columns depending on the contents of theRETURNING
clause. Regardless, some slot must be returned to indicate success, or the query's reported row count will be wrong.
If theExecForeignDelete
pointer is set toNULL
, attempts to delete from the foreign table will fail with an error message.
voidEndForeignModify(EState *estate, ResultRelInfo *rinfo);
End the table update and release resources. It is normally not important to release palloc'd memory, but for example open files and connections to remote servers should be cleaned up.
If theEndForeignModify
pointer is set toNULL
, no action is taken during executor shutdown.
Tuples inserted into a partitioned table byINSERT
orCOPY FROM
are routed to partitions. If an FDW supports routable foreign-table partitions, it should also provide the following callback functions. These functions are also called whenCOPY FROM
is executed on a foreign table.
voidBeginForeignInsert(ModifyTableState *mtstate, ResultRelInfo *rinfo);
Begin executing an insert operation on a foreign table. This routine is called right before the first tuple is inserted into the foreign table in both cases when it is the partition chosen for tuple routing and the target specified in aCOPY FROM
command. It should perform any initialization needed prior to the actual insertion. Subsequently,ExecForeignInsert
orExecForeignBatchInsert
will be called for tuple(s) to be inserted into the foreign table.
mtstate
is the overall state of theModifyTable
plan node being executed; global data about the plan and execution state is available via this structure.rinfo
is theResultRelInfo
struct describing the target foreign table. (Theri_FdwState
field ofResultRelInfo
is available for the FDW to store any private state it needs for this operation.)
When this is called by aCOPY FROM
command, the plan-related global data inmtstate
is not provided and theplanSlot
parameter ofExecForeignInsert
subsequently called for each inserted tuple isNULL
, whether the foreign table is the partition chosen for tuple routing or the target specified in the command.
If theBeginForeignInsert
pointer is set toNULL
, no action is taken for the initialization.
Note that if the FDW does not support routable foreign-table partitions and/or executingCOPY FROM
on foreign tables, this function orExecForeignInsert/ExecForeignBatchInsert
subsequently called must throw error as needed.
voidEndForeignInsert(EState *estate, ResultRelInfo *rinfo);
End the insert operation and release resources. It is normally not important to release palloc'd memory, but for example open files and connections to remote servers should be cleaned up.
If theEndForeignInsert
pointer is set toNULL
, no action is taken for the termination.
intIsForeignRelUpdatable(Relation rel);
Report which update operations the specified foreign table supports. The return value should be a bit mask of rule event numbers indicating which operations are supported by the foreign table, using theCmdType
enumeration; that is,(1 << CMD_UPDATE) = 4
forUPDATE
,(1 << CMD_INSERT) = 8
forINSERT
, and(1 << CMD_DELETE) = 16
forDELETE
.
If theIsForeignRelUpdatable
pointer is set toNULL
, foreign tables are assumed to be insertable, updatable, or deletable if the FDW providesExecForeignInsert
,ExecForeignUpdate
, orExecForeignDelete
respectively. This function is only needed if the FDW supports some tables that are updatable and some that are not. (Even then, it's permissible to throw an error in the execution routine instead of checking in this function. However, this function is used to determine updatability for display in theinformation_schema
views.)
Some inserts, updates, and deletes to foreign tables can be optimized by implementing an alternative set of interfaces. The ordinary interfaces for inserts, updates, and deletes fetch rows from the remote server and then modify those rows one at a time. In some cases, this row-by-row approach is necessary, but it can be inefficient. If it is possible for the foreign server to determine which rows should be modified without actually retrieving them, and if there are no local structures which would affect the operation (row-level local triggers, stored generated columns, orWITH CHECK OPTION
constraints from parent views), then it is possible to arrange things so that the entire operation is performed on the remote server. The interfaces described below make this possible.
boolPlanDirectModify(PlannerInfo *root, ModifyTable *plan, Index resultRelation, int subplan_index);
Decide whether it is safe to execute a direct modification on the remote server. If so, returntrue
after performing planning actions needed for that. Otherwise, returnfalse
. This optional function is called during query planning. If this function succeeds,BeginDirectModify
,IterateDirectModify
andEndDirectModify
will be called at the execution stage, instead. Otherwise, the table modification will be executed using the table-updating functions described above. The parameters are the same as forPlanForeignModify
.
To execute the direct modification on the remote server, this function must rewrite the target subplan with aForeignScan
plan node that executes the direct modification on the remote server. Theoperation
andresultRelation
fields of theForeignScan
must be set appropriately.operation
must be set to theCmdType
enumeration corresponding to the statement kind (that is,CMD_UPDATE
forUPDATE
,CMD_INSERT
forINSERT
, andCMD_DELETE
forDELETE
), and theresultRelation
argument must be copied to theresultRelation
field.
SeeSection 57.4 for additional information.
If thePlanDirectModify
pointer is set toNULL
, no attempts to execute a direct modification on the remote server are taken.
voidBeginDirectModify(ForeignScanState *node, int eflags);
Prepare to execute a direct modification on the remote server. This is called during executor startup. It should perform any initialization needed prior to the direct modification (that should be done upon the first call toIterateDirectModify
). TheForeignScanState
node has already been created, but itsfdw_state
field is still NULL. Information about the table to modify is accessible through theForeignScanState
node (in particular, from the underlyingForeignScan
plan node, which contains any FDW-private information provided byPlanDirectModify
).eflags
contains flag bits describing the executor's operating mode for this plan node.
Note that when(eflags & EXEC_FLAG_EXPLAIN_ONLY)
is true, this function should not perform any externally-visible actions; it should only do the minimum required to make the node state valid forExplainDirectModify
andEndDirectModify
.
If theBeginDirectModify
pointer is set toNULL
, no attempts to execute a direct modification on the remote server are taken.
TupleTableSlot *IterateDirectModify(ForeignScanState *node);
When theINSERT
,UPDATE
orDELETE
query doesn't have aRETURNING
clause, just return NULL after a direct modification on the remote server. When the query has the clause, fetch one result containing the data needed for theRETURNING
calculation, returning it in a tuple table slot (the node'sScanTupleSlot
should be used for this purpose). The data that was actually inserted, updated or deleted must be stored innode->resultRelInfo->ri_projectReturning->pi_exprContext->ecxt_scantuple
. Return NULL if no more rows are available. Note that this is called in a short-lived memory context that will be reset between invocations. Create a memory context inBeginDirectModify
if you need longer-lived storage, or use thees_query_cxt
of the node'sEState
.
The rows returned must match thefdw_scan_tlist
target list if one was supplied, otherwise they must match the row type of the foreign table being updated. If you choose to optimize away fetching columns that are not needed for theRETURNING
calculation, you should insert nulls in those column positions, or else generate afdw_scan_tlist
list with those columns omitted.
Whether the query has the clause or not, the query's reported row count must be incremented by the FDW itself. When the query doesn't have the clause, the FDW must also increment the row count for theForeignScanState
node in theEXPLAIN ANALYZE
case.
If theIterateDirectModify
pointer is set toNULL
, no attempts to execute a direct modification on the remote server are taken.
voidEndDirectModify(ForeignScanState *node);
Clean up following a direct modification on the remote server. It is normally not important to release palloc'd memory, but for example open files and connections to the remote server should be cleaned up.
If theEndDirectModify
pointer is set toNULL
, no attempts to execute a direct modification on the remote server are taken.
57.2.5. FDW Routines forTRUNCATE
#
voidExecForeignTruncate(List *rels, DropBehavior behavior, bool restart_seqs);
Truncate foreign tables. This function is called whenTRUNCATE is executed on a foreign table.rels
is a list ofRelation
data structures of foreign tables to truncate.
behavior
is eitherDROP_RESTRICT
orDROP_CASCADE
indicating that theRESTRICT
orCASCADE
option was requested in the originalTRUNCATE
command, respectively.
Ifrestart_seqs
istrue
, the originalTRUNCATE
command requested theRESTART IDENTITY
behavior, otherwise theCONTINUE IDENTITY
behavior was requested.
Note that theONLY
options specified in the originalTRUNCATE
command are not passed toExecForeignTruncate
. This behavior is similar to the callback functions ofSELECT
,UPDATE
andDELETE
on a foreign table.
ExecForeignTruncate
is invoked once per foreign server for which foreign tables are to be truncated. This means that all foreign tables included inrels
must belong to the same server.
If theExecForeignTruncate
pointer is set toNULL
, attempts to truncate foreign tables will fail with an error message.
57.2.6. FDW Routines for Row Locking#
If an FDW wishes to supportlate row locking (as described inSection 57.5), it must provide the following callback functions:
RowMarkTypeGetForeignRowMarkType(RangeTblEntry *rte, LockClauseStrength strength);
Report which row-marking option to use for a foreign table.rte
is theRangeTblEntry
node for the table andstrength
describes the lock strength requested by the relevantFOR UPDATE/SHARE
clause, if any. The result must be a member of theRowMarkType
enum type.
This function is called during query planning for each foreign table that appears in anUPDATE
,DELETE
, orSELECT FOR UPDATE/SHARE
query and is not the target ofUPDATE
orDELETE
.
If theGetForeignRowMarkType
pointer is set toNULL
, theROW_MARK_COPY
option is always used. (This implies thatRefetchForeignRow
will never be called, so it need not be provided either.)
SeeSection 57.5 for more information.
voidRefetchForeignRow(EState *estate, ExecRowMark *erm, Datum rowid, TupleTableSlot *slot, bool *updated);
Re-fetch one tuple slot from the foreign table, after locking it if required.estate
is global execution state for the query.erm
is theExecRowMark
struct describing the target foreign table and the row lock type (if any) to acquire.rowid
identifies the tuple to be fetched.slot
contains nothing useful upon call, but can be used to hold the returned tuple.updated
is an output parameter.
This function should store the tuple into the provided slot, or clear it if the row lock couldn't be obtained. The row lock type to acquire is defined byerm->markType
, which is the value previously returned byGetForeignRowMarkType
. (ROW_MARK_REFERENCE
means to just re-fetch the tuple without acquiring any lock, andROW_MARK_COPY
will never be seen by this routine.)
In addition,*updated
should be set totrue
if what was fetched was an updated version of the tuple rather than the same version previously obtained. (If the FDW cannot be sure about this, always returningtrue
is recommended.)
Note that by default, failure to acquire a row lock should result in raising an error; returning with an empty slot is only appropriate if theSKIP LOCKED
option is specified byerm->waitPolicy
.
Therowid
is thectid
value previously read for the row to be re-fetched. Although therowid
value is passed as aDatum
, it can currently only be atid
. The function API is chosen in hopes that it may be possible to allow other data types for row IDs in future.
If theRefetchForeignRow
pointer is set toNULL
, attempts to re-fetch rows will fail with an error message.
SeeSection 57.5 for more information.
boolRecheckForeignScan(ForeignScanState *node, TupleTableSlot *slot);
Recheck that a previously-returned tuple still matches the relevant scan and join qualifiers, and possibly provide a modified version of the tuple. For foreign data wrappers which do not perform join pushdown, it will typically be more convenient to set this toNULL
and instead setfdw_recheck_quals
appropriately. When outer joins are pushed down, however, it isn't sufficient to reapply the checks relevant to all the base tables to the result tuple, even if all needed attributes are present, because failure to match some qualifier might result in some attributes going to NULL, rather than in no tuple being returned.RecheckForeignScan
can recheck qualifiers and return true if they are still satisfied and false otherwise, but it can also store a replacement tuple into the supplied slot.
To implement join pushdown, a foreign data wrapper will typically construct an alternative local join plan which is used only for rechecks; this will become the outer subplan of theForeignScan
. When a recheck is required, this subplan can be executed and the resulting tuple can be stored in the slot. This plan need not be efficient since no base table will return more than one row; for example, it may implement all joins as nested loops. The functionGetExistingLocalJoinPath
may be used to search existing paths for a suitable local join path, which can be used as the alternative local join plan.GetExistingLocalJoinPath
searches for an unparameterized path in the path list of the specified join relation. (If it does not find such a path, it returns NULL, in which case a foreign data wrapper may build the local path by itself or may choose not to create access paths for that join.)
57.2.7. FDW Routines forEXPLAIN
#
voidExplainForeignScan(ForeignScanState *node, ExplainState *es);
Print additionalEXPLAIN
output for a foreign table scan. This function can callExplainPropertyText
and related functions to add fields to theEXPLAIN
output. The flag fields ines
can be used to determine what to print, and the state of theForeignScanState
node can be inspected to provide run-time statistics in theEXPLAIN ANALYZE
case.
If theExplainForeignScan
pointer is set toNULL
, no additional information is printed duringEXPLAIN
.
voidExplainForeignModify(ModifyTableState *mtstate, ResultRelInfo *rinfo, List *fdw_private, int subplan_index, struct ExplainState *es);
Print additionalEXPLAIN
output for a foreign table update. This function can callExplainPropertyText
and related functions to add fields to theEXPLAIN
output. The flag fields ines
can be used to determine what to print, and the state of theModifyTableState
node can be inspected to provide run-time statistics in theEXPLAIN ANALYZE
case. The first four arguments are the same as forBeginForeignModify
.
If theExplainForeignModify
pointer is set toNULL
, no additional information is printed duringEXPLAIN
.
voidExplainDirectModify(ForeignScanState *node, ExplainState *es);
Print additionalEXPLAIN
output for a direct modification on the remote server. This function can callExplainPropertyText
and related functions to add fields to theEXPLAIN
output. The flag fields ines
can be used to determine what to print, and the state of theForeignScanState
node can be inspected to provide run-time statistics in theEXPLAIN ANALYZE
case.
If theExplainDirectModify
pointer is set toNULL
, no additional information is printed duringEXPLAIN
.
57.2.8. FDW Routines forANALYZE
#
boolAnalyzeForeignTable(Relation relation, AcquireSampleRowsFunc *func, BlockNumber *totalpages);
This function is called whenANALYZE is executed on a foreign table. If the FDW can collect statistics for this foreign table, it should returntrue
, and provide a pointer to a function that will collect sample rows from the table infunc
, plus the estimated size of the table in pages intotalpages
. Otherwise, returnfalse
.
If the FDW does not support collecting statistics for any tables, theAnalyzeForeignTable
pointer can be set toNULL
.
If provided, the sample collection function must have the signature
intAcquireSampleRowsFunc(Relation relation, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows);
A random sample of up totargrows
rows should be collected from the table and stored into the caller-providedrows
array. The actual number of rows collected must be returned. In addition, store estimates of the total numbers of live and dead rows in the table into the output parameterstotalrows
andtotaldeadrows
. (Settotaldeadrows
to zero if the FDW does not have any concept of dead rows.)
57.2.9. FDW Routines forIMPORT FOREIGN SCHEMA
#
List *ImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid);
Obtain a list of foreign table creation commands. This function is called when executingIMPORT FOREIGN SCHEMA, and is passed the parse tree for that statement, as well as the OID of the foreign server to use. It should return a list of C strings, each of which must contain aCREATE FOREIGN TABLE command. These strings will be parsed and executed by the core server.
Within theImportForeignSchemaStmt
struct,remote_schema
is the name of the remote schema from which tables are to be imported.list_type
identifies how to filter table names:FDW_IMPORT_SCHEMA_ALL
means that all tables in the remote schema should be imported (in this casetable_list
is empty),FDW_IMPORT_SCHEMA_LIMIT_TO
means to include only tables listed intable_list
, andFDW_IMPORT_SCHEMA_EXCEPT
means to exclude the tables listed intable_list
.options
is a list of options used for the import process. The meanings of the options are up to the FDW. For example, an FDW could use an option to define whether theNOT NULL
attributes of columns should be imported. These options need not have anything to do with those supported by the FDW as database object options.
The FDW may ignore thelocal_schema
field of theImportForeignSchemaStmt
, because the core server will automatically insert that name into the parsedCREATE FOREIGN TABLE
commands.
The FDW does not have to concern itself with implementing the filtering specified bylist_type
andtable_list
, either, as the core server will automatically skip any returned commands for tables excluded according to those options. However, it's often useful to avoid the work of creating commands for excluded tables in the first place. The functionIsImportableForeignTable()
may be useful to test whether a given foreign-table name will pass the filter.
If the FDW does not support importing table definitions, theImportForeignSchema
pointer can be set toNULL
.
57.2.10. FDW Routines for Parallel Execution#
AForeignScan
node can, optionally, support parallel execution. A parallelForeignScan
will be executed in multiple processes and must return each row exactly once across all cooperating processes. To do this, processes can coordinate through fixed-size chunks of dynamic shared memory. This shared memory is not guaranteed to be mapped at the same address in every process, so it must not contain pointers. The following functions are all optional, but most are required if parallel execution is to be supported.
boolIsForeignScanParallelSafe(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte);
Test whether a scan can be performed within a parallel worker. This function will only be called when the planner believes that a parallel plan might be possible, and should return true if it is safe for that scan to run within a parallel worker. This will generally not be the case if the remote data source has transaction semantics, unless the worker's connection to the data can somehow be made to share the same transaction context as the leader.
If this function is not defined, it is assumed that the scan must take place within the parallel leader. Note that returning true does not mean that the scan itself can be done in parallel, only that the scan can be performed within a parallel worker. Therefore, it can be useful to define this method even when parallel execution is not supported.
SizeEstimateDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt);
Estimate the amount of dynamic shared memory that will be required for parallel operation. This may be higher than the amount that will actually be used, but it must not be lower. The return value is in bytes. This function is optional, and can be omitted if not needed; but if it is omitted, the next three functions must be omitted as well, because no shared memory will be allocated for the FDW's use.
voidInitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt, void *coordinate);
Initialize the dynamic shared memory that will be required for parallel operation.coordinate
points to a shared memory area of size equal to the return value ofEstimateDSMForeignScan
. This function is optional, and can be omitted if not needed.
voidReInitializeDSMForeignScan(ForeignScanState *node, ParallelContext *pcxt, void *coordinate);
Re-initialize the dynamic shared memory required for parallel operation when the foreign-scan plan node is about to be re-scanned. This function is optional, and can be omitted if not needed. Recommended practice is that this function reset only shared state, while theReScanForeignScan
function resets only local state. Currently, this function will be called beforeReScanForeignScan
, but it's best not to rely on that ordering.
voidInitializeWorkerForeignScan(ForeignScanState *node, shm_toc *toc, void *coordinate);
Initialize a parallel worker's local state based on the shared state set up by the leader duringInitializeDSMForeignScan
. This function is optional, and can be omitted if not needed.
voidShutdownForeignScan(ForeignScanState *node);
Release resources when it is anticipated the node will not be executed to completion. This is not called in all cases; sometimes,EndForeignScan
may be called without this function having been called first. Since the DSM segment used by parallel query is destroyed just after this callback is invoked, foreign data wrappers that wish to take some action before the DSM segment goes away should implement this method.
57.2.11. FDW Routines for Asynchronous Execution#
AForeignScan
node can, optionally, support asynchronous execution as described insrc/backend/executor/README
. The following functions are all optional, but are all required if asynchronous execution is to be supported.
boolIsForeignPathAsyncCapable(ForeignPath *path);
Test whether a givenForeignPath
path can scan the underlying foreign relation asynchronously. This function will only be called at the end of query planning when the given path is a direct child of anAppendPath
path and when the planner believes that asynchronous execution improves performance, and should return true if the given path is able to scan the foreign relation asynchronously.
If this function is not defined, it is assumed that the given path scans the foreign relation usingIterateForeignScan
. (This implies that the callback functions described below will never be called, so they need not be provided either.)
voidForeignAsyncRequest(AsyncRequest *areq);
Produce one tuple asynchronously from theForeignScan
node.areq
is theAsyncRequest
struct describing theForeignScan
node and the parentAppend
node that requested the tuple from it. This function should store the tuple into the slot specified byareq->result
, and setareq->request_complete
totrue
; or if it needs to wait on an event external to the core server such as network I/O, and cannot produce any tuple immediately, set the flag tofalse
, and setareq->callback_pending
totrue
for theForeignScan
node to get a callback from the callback functions described below. If no more tuples are available, set the slot to NULL or an empty slot, and theareq->request_complete
flag totrue
. It's recommended to useExecAsyncRequestDone
orExecAsyncRequestPending
to set the output parameters in theareq
.
voidForeignAsyncConfigureWait(AsyncRequest *areq);
Configure a file descriptor event for which theForeignScan
node wishes to wait. This function will only be called when theForeignScan
node has theareq->callback_pending
flag set, and should add the event to theas_eventset
of the parentAppend
node described by theareq
. See the comments forExecAsyncConfigureWait
insrc/backend/executor/execAsync.c
for additional information. When the file descriptor event occurs,ForeignAsyncNotify
will be called.
voidForeignAsyncNotify(AsyncRequest *areq);
Process a relevant event that has occurred, then produce one tuple asynchronously from theForeignScan
node. This function should set the output parameters in theareq
in the same way asForeignAsyncRequest
.
57.2.12. FDW Routines for Reparameterization of Paths#
List *ReparameterizeForeignPathByChild(PlannerInfo *root, List *fdw_private, RelOptInfo *child_rel);
This function is called while converting a path parameterized by the top-most parent of the given child relationchild_rel
to be parameterized by the child relation. The function is used to reparameterize any paths or translate any expression nodes saved in the givenfdw_private
member of aForeignPath
. The callback may usereparameterize_path_by_child
,adjust_appendrel_attrs
oradjust_appendrel_attrs_multilevel
as required.