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.
34/* ----------------------------------------------------------------
37 * This is a workhorse for ExecForeignScan
38 * ----------------------------------------------------------------
48 /* Call the Iterate function in short-lived context */
53 * direct modifications cannot be re-evaluated, so shouldn't get here
54 * during EvalPlanQual processing
65 * Insert valid value into tableoid, the only actually-useful system
75 * ForeignRecheck -- access method routine to recheck a tuple in EvalPlanQual
84 * extract necessary information from foreign scan node
88 /* Does the tuple meet the remote qual condition? */
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.
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 * ----------------------------------------------------------------
125 * Ignore direct modifications when EvalPlanQual is active --- they are
126 * irrelevant for EvalPlanQual rechecking
137/* ----------------------------------------------------------------
138 * ExecInitForeignScan
139 * ----------------------------------------------------------------
150 /* check for unsupported flags */
154 * create state structure
162 * Miscellaneous initialization
164 * create expression context for node
169 * open the scan relation, if any; also acquire function pointers from the
180 /* We can't use the relcache, so get fdwroutine the hard way */
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.
195 /* Node's targetlist will contain Vars with varno = INDEX_VAR */
202 /* don't trust FDWs to return tuples fulfilling NOT NULL constraints */
206 /* Node's targetlist will contain Vars with varno = scanrelid */
207 tlistvarno = scanrelid;
210 /* Don't know what an FDW might return */
215 * Initialize result slot, type and projection.
221 * initialize child expressions
229 * Determine whether to scan the foreign relation asynchronously or not;
230 * this has to be kept in sync with the code in ExecInitAppend().
236 * Initialize FDW-related state.
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.
257 elog(
ERROR,
"result relation not initialized");
262 /* Initialize any outer plan. */
268 * Tell the FDW to initialize the scan.
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.
290/* ----------------------------------------------------------------
293 * frees any storage allocated through C routines.
294 * ----------------------------------------------------------------
302 /* Let the FDW shut down */
305 if (estate->es_epq_active == NULL)
311 /* Shut down any outer plan. */
316/* ----------------------------------------------------------------
317 * ExecReScanForeignScan
319 * Rescans the relation.
320 * ----------------------------------------------------------------
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)
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.
349/* ----------------------------------------------------------------
350 * ExecForeignScanEstimate
352 * Informs size of the parallel coordination information, if any
353 * ----------------------------------------------------------------
368/* ----------------------------------------------------------------
369 * ExecForeignScanInitializeDSM
371 * Initialize the parallel coordination information
372 * ----------------------------------------------------------------
390/* ----------------------------------------------------------------
391 * ExecForeignScanReInitializeDSM
393 * Reset shared state before beginning a fresh scan.
394 * ----------------------------------------------------------------
411/* ----------------------------------------------------------------
412 * ExecForeignScanInitializeWorker
414 * Initialization according to the parallel coordination information
415 * ----------------------------------------------------------------
433/* ----------------------------------------------------------------
434 * ExecShutdownForeignScan
436 * Gives FDW chance to stop asynchronous resource consumption
437 * and release any resources still held.
438 * ----------------------------------------------------------------
449/* ----------------------------------------------------------------
450 * ExecAsyncForeignScanRequest
452 * Asynchronously request a tuple from a designed async-capable node
453 * ----------------------------------------------------------------
461 Assert(fdwroutine->ForeignAsyncRequest != NULL);
462 fdwroutine->ForeignAsyncRequest(areq);
465/* ----------------------------------------------------------------
466 * ExecAsyncForeignScanConfigureWait
468 * In async mode, configure for a wait
469 * ----------------------------------------------------------------
477 Assert(fdwroutine->ForeignAsyncConfigureWait != NULL);
478 fdwroutine->ForeignAsyncConfigureWait(areq);
481/* ----------------------------------------------------------------
482 * ExecAsyncForeignScanNotify
484 * Callback invoked when a relevant event has occurred
485 * ----------------------------------------------------------------
493 Assert(fdwroutine->ForeignAsyncNotify != NULL);
494 fdwroutine->ForeignAsyncNotify(areq);
void ExecReScan(PlanState *node)
ExprState * ExecInitQual(List *qual, PlanState *parent)
void ExecEndNode(PlanState *node)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
void ExecScanReScan(ScanState *node)
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
void ExecInitResultTypeTL(PlanState *planstate)
const TupleTableSlotOps TTSOpsHeapTuple
TupleDesc ExecTypeFromTL(List *targetList)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
#define outerPlanState(node)
#define EXEC_FLAG_BACKWARD
#define ResetExprContext(econtext)
static bool ExecQual(ExprState *state, ExprContext *econtext)
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Assert(PointerIsAligned(start, uint64))
if(TABLE==NULL||TABLE_index==NULL)
ForeignScanState * ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecShutdownForeignScan(ForeignScanState *node)
void ExecAsyncForeignScanNotify(AsyncRequest *areq)
void ExecAsyncForeignScanConfigureWait(AsyncRequest *areq)
static TupleTableSlot * ExecForeignScan(PlanState *pstate)
void ExecAsyncForeignScanRequest(AsyncRequest *areq)
void ExecReScanForeignScan(ForeignScanState *node)
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
void ExecEndForeignScan(ForeignScanState *node)
static bool ForeignRecheck(ForeignScanState *node, TupleTableSlot *slot)
static TupleTableSlot * ForeignNext(ForeignScanState *node)
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
#define castNode(_type_, nodeptr)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
#define RelationGetRelid(relation)
#define RelationGetDescr(relation)
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
#define shm_toc_estimate_chunk(e, sz)
#define shm_toc_estimate_keys(e, cnt)
ResultRelInfo ** es_result_relations
struct EPQState * es_epq_active
TupleTableSlot * ecxt_scantuple
EstimateDSMForeignScan_function EstimateDSMForeignScan
ReScanForeignScan_function ReScanForeignScan
RecheckForeignScan_function RecheckForeignScan
InitializeDSMForeignScan_function InitializeDSMForeignScan
BeginDirectModify_function BeginDirectModify
BeginForeignScan_function BeginForeignScan
IterateDirectModify_function IterateDirectModify
EndForeignScan_function EndForeignScan
EndDirectModify_function EndDirectModify
ReInitializeDSMForeignScan_function ReInitializeDSMForeignScan
IterateForeignScan_function IterateForeignScan
InitializeWorkerForeignScan_function InitializeWorkerForeignScan
ShutdownForeignScan_function ShutdownForeignScan
struct FdwRoutine * fdwroutine
ExprState * fdw_recheck_quals
ResultRelInfo * resultRelInfo
shm_toc_estimator estimator
ExprContext * ps_ExprContext
ExecProcNodeMtd ExecProcNode
Relation ss_currentRelation
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)