1 /*-------------------------------------------------------------------------
4 * Routines to support scans of foreign tables
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeForeignscan.c
13 *-------------------------------------------------------------------------
18 * ExecForeignScan scans a foreign table.
19 * ExecInitForeignScan creates and initializes state info.
20 * ExecReScanForeignScan rescans the foreign relation.
21 * ExecEndForeignScan releases any resources allocated.
25 #include "executor/executor.h"
26 #include "executor/nodeForeignscan.h"
27 #include "foreign/fdwapi.h"
28 #include "utils/rel.h"
30 static TupleTableSlot
*ForeignNext(ForeignScanState
*node
);
31 static bool ForeignRecheck(ForeignScanState
*node
, TupleTableSlot
*slot
);
34 /* ----------------------------------------------------------------
37 * This is a workhorse for ExecForeignScan
38 * ----------------------------------------------------------------
40 static TupleTableSlot
*
41 ForeignNext(ForeignScanState
*node
)
44 ForeignScan
*plan
= (ForeignScan
*) node
->ss
.ps
.plan
;
45 ExprContext
*econtext
= node
->ss
.ps
.ps_ExprContext
;
46 MemoryContext oldcontext
;
48 /* Call the Iterate function in short-lived context */
49 oldcontext
= MemoryContextSwitchTo(econtext
->ecxt_per_tuple_memory
);
50 if (plan
->operation
!= CMD_SELECT
)
53 * direct modifications cannot be re-evaluated, so shouldn't get here
54 * during EvalPlanQual processing
56 Assert(node
->ss
.ps
.state
->es_epq_active
== NULL
);
58 slot
= node
->fdwroutine
->IterateDirectModify(node
);
61 slot
= node
->fdwroutine
->IterateForeignScan(node
);
62 MemoryContextSwitchTo(oldcontext
);
65 * Insert valid value into tableoid, the only actually-useful system
68 if (plan
->fsSystemCol
&& !TupIsNull(slot
))
69 slot
->tts_tableOid
= RelationGetRelid(node
->ss
.ss_currentRelation
);
75 * ForeignRecheck -- access method routine to recheck a tuple in EvalPlanQual
78 ForeignRecheck(ForeignScanState
*node
, TupleTableSlot
*slot
)
80 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
81 ExprContext
*econtext
;
84 * extract necessary information from foreign scan node
86 econtext
= node
->ss
.ps
.ps_ExprContext
;
88 /* Does the tuple meet the remote qual condition? */
89 econtext
->ecxt_scantuple
= slot
;
91 ResetExprContext(econtext
);
94 * If an outer join is pushed down, RecheckForeignScan may need to store a
95 * different tuple in the slot, because a different set of columns may go
96 * to NULL upon recheck. Otherwise, it shouldn't need to change the slot
97 * contents, just return true or false to indicate whether the quals still
98 * pass. For simple cases, setting fdw_recheck_quals may be easier than
99 * providing this callback.
101 if (fdwroutine
->RecheckForeignScan
&&
102 !fdwroutine
->RecheckForeignScan(node
, slot
))
105 return ExecQual(node
->fdw_recheck_quals
, econtext
);
108 /* ----------------------------------------------------------------
109 * ExecForeignScan(node)
111 * Fetches the next tuple from the FDW, checks local quals, and
113 * We call the ExecScan() routine and pass it the appropriate
114 * access method functions.
115 * ----------------------------------------------------------------
117 static TupleTableSlot
*
118 ExecForeignScan(PlanState
*pstate
)
120 ForeignScanState
*node
= castNode(ForeignScanState
, pstate
);
121 ForeignScan
*plan
= (ForeignScan
*) node
->ss
.ps
.plan
;
122 EState
*estate
= node
->ss
.ps
.state
;
125 * Ignore direct modifications when EvalPlanQual is active --- they are
126 * irrelevant for EvalPlanQual rechecking
128 if (estate
->es_epq_active
!= NULL
&& plan
->operation
!= CMD_SELECT
)
131 return ExecScan(&node
->ss
,
132 (ExecScanAccessMtd
) ForeignNext
,
133 (ExecScanRecheckMtd
) ForeignRecheck
);
137 /* ----------------------------------------------------------------
138 * ExecInitForeignScan
139 * ----------------------------------------------------------------
142 ExecInitForeignScan(ForeignScan
*node
, EState
*estate
, int eflags
)
144 ForeignScanState
*scanstate
;
145 Relation currentRelation
= NULL
;
146 Index scanrelid
= node
->scan
.scanrelid
;
148 FdwRoutine
*fdwroutine
;
150 /* check for unsupported flags */
151 Assert(!(eflags
& (EXEC_FLAG_BACKWARD
| EXEC_FLAG_MARK
)));
154 * create state structure
156 scanstate
= makeNode(ForeignScanState
);
157 scanstate
->ss
.ps
.plan
= (Plan
*) node
;
158 scanstate
->ss
.ps
.state
= estate
;
159 scanstate
->ss
.ps
.ExecProcNode
= ExecForeignScan
;
162 * Miscellaneous initialization
164 * create expression context for node
166 ExecAssignExprContext(estate
, &scanstate
->ss
.ps
);
169 * open the scan relation, if any; also acquire function pointers from the
174 currentRelation
= ExecOpenScanRelation(estate
, scanrelid
, eflags
);
175 scanstate
->ss
.ss_currentRelation
= currentRelation
;
176 fdwroutine
= GetFdwRoutineForRelation(currentRelation
, true);
180 /* We can't use the relcache, so get fdwroutine the hard way */
181 fdwroutine
= GetFdwRoutineByServerId(node
->fs_server
);
185 * Determine the scan tuple type. If the FDW provided a targetlist
186 * describing the scan tuples, use that; else use base relation's rowtype.
188 if (node
->fdw_scan_tlist
!= NIL
|| currentRelation
== NULL
)
190 TupleDesc scan_tupdesc
;
192 scan_tupdesc
= ExecTypeFromTL(node
->fdw_scan_tlist
);
193 ExecInitScanTupleSlot(estate
, &scanstate
->ss
, scan_tupdesc
,
195 /* Node's targetlist will contain Vars with varno = INDEX_VAR */
196 tlistvarno
= INDEX_VAR
;
200 TupleDesc scan_tupdesc
;
202 /* don't trust FDWs to return tuples fulfilling NOT NULL constraints */
203 scan_tupdesc
= CreateTupleDescCopy(RelationGetDescr(currentRelation
));
204 ExecInitScanTupleSlot(estate
, &scanstate
->ss
, scan_tupdesc
,
206 /* Node's targetlist will contain Vars with varno = scanrelid */
207 tlistvarno
= scanrelid
;
210 /* Don't know what an FDW might return */
211 scanstate
->ss
.ps
.scanopsfixed
= false;
212 scanstate
->ss
.ps
.scanopsset
= true;
215 * Initialize result slot, type and projection.
217 ExecInitResultTypeTL(&scanstate
->ss
.ps
);
218 ExecAssignScanProjectionInfoWithVarno(&scanstate
->ss
, tlistvarno
);
221 * initialize child expressions
223 scanstate
->ss
.ps
.qual
=
224 ExecInitQual(node
->scan
.plan
.qual
, (PlanState
*) scanstate
);
225 scanstate
->fdw_recheck_quals
=
226 ExecInitQual(node
->fdw_recheck_quals
, (PlanState
*) scanstate
);
229 * Determine whether to scan the foreign relation asynchronously or not;
230 * this has to be kept in sync with the code in ExecInitAppend().
232 scanstate
->ss
.ps
.async_capable
= (((Plan
*) node
)->async_capable
&&
233 estate
->es_epq_active
== NULL
);
236 * Initialize FDW-related state.
238 scanstate
->fdwroutine
= fdwroutine
;
239 scanstate
->fdw_state
= NULL
;
242 * For the FDW's convenience, look up the modification target relation's
243 * ResultRelInfo. The ModifyTable node should have initialized it for us,
244 * see ExecInitModifyTable.
246 * Don't try to look up the ResultRelInfo when EvalPlanQual is active,
247 * though. Direct modifications cannot be re-evaluated as part of
248 * EvalPlanQual. The lookup wouldn't work anyway because during
249 * EvalPlanQual processing, EvalPlanQual only initializes the subtree
250 * under the ModifyTable, and doesn't run ExecInitModifyTable.
252 if (node
->resultRelation
> 0 && estate
->es_epq_active
== NULL
)
254 if (estate
->es_result_relations
== NULL
||
255 estate
->es_result_relations
[node
->resultRelation
- 1] == NULL
)
257 elog(ERROR
, "result relation not initialized");
259 scanstate
->resultRelInfo
= estate
->es_result_relations
[node
->resultRelation
- 1];
262 /* Initialize any outer plan. */
264 outerPlanState(scanstate
) =
265 ExecInitNode(outerPlan(node
), estate
, eflags
);
268 * Tell the FDW to initialize the scan.
270 if (node
->operation
!= CMD_SELECT
)
273 * Direct modifications cannot be re-evaluated by EvalPlanQual, so
274 * don't bother preparing the FDW.
276 * In case of an inherited UPDATE/DELETE with foreign targets there
277 * can be direct-modify ForeignScan nodes in the EvalPlanQual subtree,
278 * so we need to ignore such ForeignScan nodes during EvalPlanQual
279 * processing. See also ExecForeignScan/ExecReScanForeignScan.
281 if (estate
->es_epq_active
== NULL
)
282 fdwroutine
->BeginDirectModify(scanstate
, eflags
);
285 fdwroutine
->BeginForeignScan(scanstate
, eflags
);
290 /* ----------------------------------------------------------------
293 * frees any storage allocated through C routines.
294 * ----------------------------------------------------------------
297 ExecEndForeignScan(ForeignScanState
*node
)
299 ForeignScan
*plan
= (ForeignScan
*) node
->ss
.ps
.plan
;
300 EState
*estate
= node
->ss
.ps
.state
;
302 /* Let the FDW shut down */
303 if (plan
->operation
!= CMD_SELECT
)
305 if (estate
->es_epq_active
== NULL
)
306 node
->fdwroutine
->EndDirectModify(node
);
309 node
->fdwroutine
->EndForeignScan(node
);
311 /* Shut down any outer plan. */
312 if (outerPlanState(node
))
313 ExecEndNode(outerPlanState(node
));
316 /* ----------------------------------------------------------------
317 * ExecReScanForeignScan
319 * Rescans the relation.
320 * ----------------------------------------------------------------
323 ExecReScanForeignScan(ForeignScanState
*node
)
325 ForeignScan
*plan
= (ForeignScan
*) node
->ss
.ps
.plan
;
326 EState
*estate
= node
->ss
.ps
.state
;
327 PlanState
*outerPlan
= outerPlanState(node
);
330 * Ignore direct modifications when EvalPlanQual is active --- they are
331 * irrelevant for EvalPlanQual rechecking
333 if (estate
->es_epq_active
!= NULL
&& plan
->operation
!= CMD_SELECT
)
336 node
->fdwroutine
->ReScanForeignScan(node
);
339 * If chgParam of subnode is not null then plan will be re-scanned by
340 * first ExecProcNode. outerPlan may also be NULL, in which case there is
341 * nothing to rescan at all.
343 if (outerPlan
!= NULL
&& outerPlan
->chgParam
== NULL
)
344 ExecReScan(outerPlan
);
346 ExecScanReScan(&node
->ss
);
349 /* ----------------------------------------------------------------
350 * ExecForeignScanEstimate
352 * Informs size of the parallel coordination information, if any
353 * ----------------------------------------------------------------
356 ExecForeignScanEstimate(ForeignScanState
*node
, ParallelContext
*pcxt
)
358 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
360 if (fdwroutine
->EstimateDSMForeignScan
)
362 node
->pscan_len
= fdwroutine
->EstimateDSMForeignScan(node
, pcxt
);
363 shm_toc_estimate_chunk(&pcxt
->estimator
, node
->pscan_len
);
364 shm_toc_estimate_keys(&pcxt
->estimator
, 1);
368 /* ----------------------------------------------------------------
369 * ExecForeignScanInitializeDSM
371 * Initialize the parallel coordination information
372 * ----------------------------------------------------------------
375 ExecForeignScanInitializeDSM(ForeignScanState
*node
, ParallelContext
*pcxt
)
377 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
379 if (fdwroutine
->InitializeDSMForeignScan
)
381 int plan_node_id
= node
->ss
.ps
.plan
->plan_node_id
;
384 coordinate
= shm_toc_allocate(pcxt
->toc
, node
->pscan_len
);
385 fdwroutine
->InitializeDSMForeignScan(node
, pcxt
, coordinate
);
386 shm_toc_insert(pcxt
->toc
, plan_node_id
, coordinate
);
390 /* ----------------------------------------------------------------
391 * ExecForeignScanReInitializeDSM
393 * Reset shared state before beginning a fresh scan.
394 * ----------------------------------------------------------------
397 ExecForeignScanReInitializeDSM(ForeignScanState
*node
, ParallelContext
*pcxt
)
399 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
401 if (fdwroutine
->ReInitializeDSMForeignScan
)
403 int plan_node_id
= node
->ss
.ps
.plan
->plan_node_id
;
406 coordinate
= shm_toc_lookup(pcxt
->toc
, plan_node_id
, false);
407 fdwroutine
->ReInitializeDSMForeignScan(node
, pcxt
, coordinate
);
411 /* ----------------------------------------------------------------
412 * ExecForeignScanInitializeWorker
414 * Initialization according to the parallel coordination information
415 * ----------------------------------------------------------------
418 ExecForeignScanInitializeWorker(ForeignScanState
*node
,
419 ParallelWorkerContext
*pwcxt
)
421 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
423 if (fdwroutine
->InitializeWorkerForeignScan
)
425 int plan_node_id
= node
->ss
.ps
.plan
->plan_node_id
;
428 coordinate
= shm_toc_lookup(pwcxt
->toc
, plan_node_id
, false);
429 fdwroutine
->InitializeWorkerForeignScan(node
, pwcxt
->toc
, coordinate
);
433 /* ----------------------------------------------------------------
434 * ExecShutdownForeignScan
436 * Gives FDW chance to stop asynchronous resource consumption
437 * and release any resources still held.
438 * ----------------------------------------------------------------
441 ExecShutdownForeignScan(ForeignScanState
*node
)
443 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
445 if (fdwroutine
->ShutdownForeignScan
)
446 fdwroutine
->ShutdownForeignScan(node
);
449 /* ----------------------------------------------------------------
450 * ExecAsyncForeignScanRequest
452 * Asynchronously request a tuple from a designed async-capable node
453 * ----------------------------------------------------------------
456 ExecAsyncForeignScanRequest(AsyncRequest
*areq
)
458 ForeignScanState
*node
= (ForeignScanState
*) areq
->requestee
;
459 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
461 Assert(fdwroutine
->ForeignAsyncRequest
!= NULL
);
462 fdwroutine
->ForeignAsyncRequest(areq
);
465 /* ----------------------------------------------------------------
466 * ExecAsyncForeignScanConfigureWait
468 * In async mode, configure for a wait
469 * ----------------------------------------------------------------
472 ExecAsyncForeignScanConfigureWait(AsyncRequest
*areq
)
474 ForeignScanState
*node
= (ForeignScanState
*) areq
->requestee
;
475 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
477 Assert(fdwroutine
->ForeignAsyncConfigureWait
!= NULL
);
478 fdwroutine
->ForeignAsyncConfigureWait(areq
);
481 /* ----------------------------------------------------------------
482 * ExecAsyncForeignScanNotify
484 * Callback invoked when a relevant event has occurred
485 * ----------------------------------------------------------------
488 ExecAsyncForeignScanNotify(AsyncRequest
*areq
)
490 ForeignScanState
*node
= (ForeignScanState
*) areq
->requestee
;
491 FdwRoutine
*fdwroutine
= node
->fdwroutine
;
493 Assert(fdwroutine
->ForeignAsyncNotify
!= NULL
);
494 fdwroutine
->ForeignAsyncNotify(areq
);