PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeForeignscan.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/nodeForeignscan.h"
#include "foreign/fdwapi.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for nodeForeignscan.c:

Go to the source code of this file.

Functions

static TupleTableSlotForeignNext (ForeignScanState *node)
 
static bool ForeignRecheck (ForeignScanState *node, TupleTableSlot *slot)
 
TupleTableSlotExecForeignScan (ForeignScanState *node)
 
ForeignScanStateExecInitForeignScan (ForeignScan *node, EState *estate, int eflags)
 
void ExecEndForeignScan (ForeignScanState *node)
 
void ExecReScanForeignScan (ForeignScanState *node)
 
void ExecForeignScanEstimate (ForeignScanState *node, ParallelContext *pcxt)
 
void ExecForeignScanInitializeDSM (ForeignScanState *node, ParallelContext *pcxt)
 
void ExecForeignScanInitializeWorker (ForeignScanState *node, shm_toc *toc)
 
void ExecShutdownForeignScan (ForeignScanState *node)
 

Function Documentation

void ExecEndForeignScan ( ForeignScanState node)

Definition at line 245 of file nodeForeignscan.c.

References CMD_SELECT, FdwRoutine::EndDirectModify, FdwRoutine::EndForeignScan, ExecClearTuple(), ExecCloseScanRelation(), ExecEndNode(), ExecFreeExprContext(), ForeignScanState::fdwroutine, ForeignScan::operation, outerPlanState, PlanState::plan, ScanState::ps, PlanState::ps_ResultTupleSlot, ForeignScanState::ss, ScanState::ss_currentRelation, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

246 {
247  ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
248 
249  /* Let the FDW shut down */
250  if (plan->operation != CMD_SELECT)
251  node->fdwroutine->EndDirectModify(node);
252  else
253  node->fdwroutine->EndForeignScan(node);
254 
255  /* Shut down any outer plan. */
256  if (outerPlanState(node))
258 
259  /* Free the exprcontext */
260  ExecFreeExprContext(&node->ss.ps);
261 
262  /* clean out the tuple table */
265 
266  /* close the relation. */
267  if (node->ss.ss_currentRelation)
269 }
EndDirectModify_function EndDirectModify
Definition: fdwapi.h:205
ScanState ss
Definition: execnodes.h:1714
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:644
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1293
Relation ss_currentRelation
Definition: execnodes.h:1291
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:686
CmdType operation
Definition: plannodes.h:575
PlanState ps
Definition: execnodes.h:1290
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1079
#define outerPlanState(node)
Definition: execnodes.h:1092
EndForeignScan_function EndForeignScan
Definition: fdwapi.h:180
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
Plan * plan
Definition: execnodes.h:1049
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:831
TupleTableSlot* ExecForeignScan ( ForeignScanState node)

Definition at line 117 of file nodeForeignscan.c.

References ExecScan(), ForeignNext(), and ForeignRecheck().

Referenced by ExecProcNode().

118 {
119  return ExecScan((ScanState *) node,
122 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
static bool ForeignRecheck(ForeignScanState *node, TupleTableSlot *slot)
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:271
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:272
static TupleTableSlot * ForeignNext(ForeignScanState *node)
void ExecForeignScanEstimate ( ForeignScanState node,
ParallelContext pcxt 
)

Definition at line 302 of file nodeForeignscan.c.

References FdwRoutine::EstimateDSMForeignScan, ParallelContext::estimator, ForeignScanState::fdwroutine, ForeignScanState::pscan_len, shm_toc_estimate_chunk, and shm_toc_estimate_keys.

Referenced by ExecParallelEstimate().

303 {
304  FdwRoutine *fdwroutine = node->fdwroutine;
305 
306  if (fdwroutine->EstimateDSMForeignScan)
307  {
308  node->pscan_len = fdwroutine->EstimateDSMForeignScan(node, pcxt);
310  shm_toc_estimate_keys(&pcxt->estimator, 1);
311  }
312 }
EstimateDSMForeignScan_function EstimateDSMForeignScan
Definition: fdwapi.h:225
shm_toc_estimator estimator
Definition: parallel.h:42
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:49
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:52
void ExecForeignScanInitializeDSM ( ForeignScanState node,
ParallelContext pcxt 
)

Definition at line 321 of file nodeForeignscan.c.

References ForeignScanState::fdwroutine, FdwRoutine::InitializeDSMForeignScan, PlanState::plan, Plan::plan_node_id, ScanState::ps, ForeignScanState::pscan_len, shm_toc_allocate(), shm_toc_insert(), ForeignScanState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

322 {
323  FdwRoutine *fdwroutine = node->fdwroutine;
324 
325  if (fdwroutine->InitializeDSMForeignScan)
326  {
327  int plan_node_id = node->ss.ps.plan->plan_node_id;
328  void *coordinate;
329 
330  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
331  fdwroutine->InitializeDSMForeignScan(node, pcxt, coordinate);
332  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
333  }
334 }
ScanState ss
Definition: execnodes.h:1714
int plan_node_id
Definition: plannodes.h:131
PlanState ps
Definition: execnodes.h:1290
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
InitializeDSMForeignScan_function InitializeDSMForeignScan
Definition: fdwapi.h:226
Plan * plan
Definition: execnodes.h:1049
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
shm_toc * toc
Definition: parallel.h:45
void ExecForeignScanInitializeWorker ( ForeignScanState node,
shm_toc toc 
)

Definition at line 343 of file nodeForeignscan.c.

References ForeignScanState::fdwroutine, FdwRoutine::InitializeWorkerForeignScan, PlanState::plan, Plan::plan_node_id, ScanState::ps, shm_toc_lookup(), and ForeignScanState::ss.

Referenced by ExecParallelInitializeWorker().

344 {
345  FdwRoutine *fdwroutine = node->fdwroutine;
346 
347  if (fdwroutine->InitializeWorkerForeignScan)
348  {
349  int plan_node_id = node->ss.ps.plan->plan_node_id;
350  void *coordinate;
351 
352  coordinate = shm_toc_lookup(toc, plan_node_id);
353  fdwroutine->InitializeWorkerForeignScan(node, toc, coordinate);
354  }
355 }
ScanState ss
Definition: execnodes.h:1714
InitializeWorkerForeignScan_function InitializeWorkerForeignScan
Definition: fdwapi.h:227
int plan_node_id
Definition: plannodes.h:131
PlanState ps
Definition: execnodes.h:1290
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
Plan * plan
Definition: execnodes.h:1049
ForeignScanState* ExecInitForeignScan ( ForeignScan node,
EState estate,
int  eflags 
)

Definition at line 130 of file nodeForeignscan.c.

References Assert, FdwRoutine::BeginDirectModify, FdwRoutine::BeginForeignScan, CMD_SELECT, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignResultTypeFromTL(), ExecAssignScanProjectionInfoWithVarno(), ExecAssignScanType(), ExecInitExpr(), ExecInitNode(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecOpenScanRelation(), ExecTypeFromTL(), ForeignScan::fdw_recheck_quals, ForeignScanState::fdw_recheck_quals, ForeignScan::fdw_scan_tlist, ForeignScanState::fdw_state, ForeignScanState::fdwroutine, ForeignScan::fs_server, GetFdwRoutineByServerId(), GetFdwRoutineForRelation(), INDEX_VAR, makeNode, NIL, NULL, ForeignScan::operation, outerPlan, outerPlanState, Scan::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, RelationGetDescr, ForeignScan::scan, Scan::scanrelid, ForeignScanState::ss, ScanState::ss_currentRelation, PlanState::state, Plan::targetlist, and PlanState::targetlist.

Referenced by ExecInitNode().

131 {
132  ForeignScanState *scanstate;
133  Relation currentRelation = NULL;
134  Index scanrelid = node->scan.scanrelid;
135  Index tlistvarno;
136  FdwRoutine *fdwroutine;
137 
138  /* check for unsupported flags */
139  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
140 
141  /*
142  * create state structure
143  */
144  scanstate = makeNode(ForeignScanState);
145  scanstate->ss.ps.plan = (Plan *) node;
146  scanstate->ss.ps.state = estate;
147 
148  /*
149  * Miscellaneous initialization
150  *
151  * create expression context for node
152  */
153  ExecAssignExprContext(estate, &scanstate->ss.ps);
154 
155  /*
156  * initialize child expressions
157  */
158  scanstate->ss.ps.targetlist = (List *)
160  (PlanState *) scanstate);
161  scanstate->ss.ps.qual = (List *)
162  ExecInitExpr((Expr *) node->scan.plan.qual,
163  (PlanState *) scanstate);
164  scanstate->fdw_recheck_quals = (List *)
166  (PlanState *) scanstate);
167 
168  /*
169  * tuple table initialization
170  */
171  ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
172  ExecInitScanTupleSlot(estate, &scanstate->ss);
173 
174  /*
175  * open the base relation, if any, and acquire an appropriate lock on it;
176  * also acquire function pointers from the FDW's handler
177  */
178  if (scanrelid > 0)
179  {
180  currentRelation = ExecOpenScanRelation(estate, scanrelid, eflags);
181  scanstate->ss.ss_currentRelation = currentRelation;
182  fdwroutine = GetFdwRoutineForRelation(currentRelation, true);
183  }
184  else
185  {
186  /* We can't use the relcache, so get fdwroutine the hard way */
187  fdwroutine = GetFdwRoutineByServerId(node->fs_server);
188  }
189 
190  /*
191  * Determine the scan tuple type. If the FDW provided a targetlist
192  * describing the scan tuples, use that; else use base relation's rowtype.
193  */
194  if (node->fdw_scan_tlist != NIL || currentRelation == NULL)
195  {
196  TupleDesc scan_tupdesc;
197 
198  scan_tupdesc = ExecTypeFromTL(node->fdw_scan_tlist, false);
199  ExecAssignScanType(&scanstate->ss, scan_tupdesc);
200  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
201  tlistvarno = INDEX_VAR;
202  }
203  else
204  {
205  ExecAssignScanType(&scanstate->ss, RelationGetDescr(currentRelation));
206  /* Node's targetlist will contain Vars with varno = scanrelid */
207  tlistvarno = scanrelid;
208  }
209 
210  /*
211  * Initialize result tuple type and projection info.
212  */
213  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
214  ExecAssignScanProjectionInfoWithVarno(&scanstate->ss, tlistvarno);
215 
216  /*
217  * Initialize FDW-related state.
218  */
219  scanstate->fdwroutine = fdwroutine;
220  scanstate->fdw_state = NULL;
221 
222  /* Initialize any outer plan. */
223  if (outerPlan(node))
224  outerPlanState(scanstate) =
225  ExecInitNode(outerPlan(node), estate, eflags);
226 
227  /*
228  * Tell the FDW to initialize the scan.
229  */
230  if (node->operation != CMD_SELECT)
231  fdwroutine->BeginDirectModify(scanstate, eflags);
232  else
233  fdwroutine->BeginForeignScan(scanstate, eflags);
234 
235  return scanstate;
236 }
BeginForeignScan_function BeginForeignScan
Definition: fdwapi.h:177
#define NIL
Definition: pg_list.h:69
ScanState ss
Definition: execnodes.h:1714
List * qual
Definition: plannodes.h:133
Plan plan
Definition: plannodes.h:315
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:316
Oid fs_server
Definition: plannodes.h:576
#define RelationGetDescr(relation)
Definition: rel.h:425
List * qual
Definition: execnodes.h:1064
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition: foreign.c:330
List * fdw_recheck_quals
Definition: execnodes.h:1715
List * targetlist
Definition: execnodes.h:1063
List * fdw_scan_tlist
Definition: plannodes.h:579
Relation ss_currentRelation
Definition: execnodes.h:1291
EState * state
Definition: execnodes.h:1051
CmdType operation
Definition: plannodes.h:575
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:431
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:247
PlanState ps
Definition: execnodes.h:1290
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:773
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1092
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
List * fdw_recheck_quals
Definition: plannodes.h:580
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
#define outerPlan(node)
Definition: plannodes.h:162
BeginDirectModify_function BeginDirectModify
Definition: fdwapi.h:203
unsigned int Index
Definition: c.h:365
Plan * plan
Definition: execnodes.h:1049
#define makeNode(_type_)
Definition: nodes.h:566
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:409
List * targetlist
Definition: plannodes.h:132
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:710
#define INDEX_VAR
Definition: primnodes.h:155
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
void ExecReScanForeignScan ( ForeignScanState node)

Definition at line 278 of file nodeForeignscan.c.

References PlanState::chgParam, ExecReScan(), ExecScanReScan(), ForeignScanState::fdwroutine, NULL, outerPlan, outerPlanState, FdwRoutine::ReScanForeignScan, and ForeignScanState::ss.

Referenced by ExecReScan().

279 {
281 
282  node->fdwroutine->ReScanForeignScan(node);
283 
284  /*
285  * If chgParam of subnode is not null then plan will be re-scanned by
286  * first ExecProcNode. outerPlan may also be NULL, in which case there is
287  * nothing to rescan at all.
288  */
289  if (outerPlan != NULL && outerPlan->chgParam == NULL)
290  ExecReScan(outerPlan);
291 
292  ExecScanReScan(&node->ss);
293 }
ScanState ss
Definition: execnodes.h:1714
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
#define outerPlanState(node)
Definition: execnodes.h:1092
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
Bitmapset * chgParam
Definition: execnodes.h:1074
#define outerPlan(node)
Definition: plannodes.h:162
#define NULL
Definition: c.h:229
ReScanForeignScan_function ReScanForeignScan
Definition: fdwapi.h:179
void ExecScanReScan(ScanState *node)
Definition: execScan.c:327
void ExecShutdownForeignScan ( ForeignScanState node)

Definition at line 365 of file nodeForeignscan.c.

References ForeignScanState::fdwroutine, and FdwRoutine::ShutdownForeignScan.

Referenced by ExecShutdownNode().

366 {
367  FdwRoutine *fdwroutine = node->fdwroutine;
368 
369  if (fdwroutine->ShutdownForeignScan)
370  fdwroutine->ShutdownForeignScan(node);
371 }
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
ShutdownForeignScan_function ShutdownForeignScan
Definition: fdwapi.h:228
static TupleTableSlot * ForeignNext ( ForeignScanState node)
static

Definition at line 42 of file nodeForeignscan.c.

References CMD_SELECT, ExecMaterializeSlot(), ForeignScanState::fdwroutine, ForeignScan::fsSystemCol, FdwRoutine::IterateDirectModify, FdwRoutine::IterateForeignScan, MemoryContextSwitchTo(), ForeignScan::operation, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, RelationGetRelid, ForeignScanState::ss, ScanState::ss_currentRelation, HeapTupleData::t_tableOid, and TupIsNull.

Referenced by ExecForeignScan().

43 {
44  TupleTableSlot *slot;
45  ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
46  ExprContext *econtext = node->ss.ps.ps_ExprContext;
47  MemoryContext oldcontext;
48 
49  /* Call the Iterate function in short-lived context */
50  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
51  if (plan->operation != CMD_SELECT)
52  slot = node->fdwroutine->IterateDirectModify(node);
53  else
54  slot = node->fdwroutine->IterateForeignScan(node);
55  MemoryContextSwitchTo(oldcontext);
56 
57  /*
58  * If any system columns are requested, we have to force the tuple into
59  * physical-tuple form to avoid "cannot extract system attribute from
60  * virtual tuple" errors later. We also insert a valid value for
61  * tableoid, which is the only actually-useful system column.
62  */
63  if (plan->fsSystemCol && !TupIsNull(slot))
64  {
65  HeapTuple tup = ExecMaterializeSlot(slot);
66 
68  }
69 
70  return slot;
71 }
ScanState ss
Definition: execnodes.h:1714
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Relation ss_currentRelation
Definition: execnodes.h:1291
CmdType operation
Definition: plannodes.h:575
IterateDirectModify_function IterateDirectModify
Definition: fdwapi.h:204
PlanState ps
Definition: execnodes.h:1290
#define TupIsNull(slot)
Definition: tuptable.h:138
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
Oid t_tableOid
Definition: htup.h:66
Plan * plan
Definition: execnodes.h:1049
IterateForeignScan_function IterateForeignScan
Definition: fdwapi.h:178
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:725
#define RelationGetRelid(relation)
Definition: rel.h:413
bool fsSystemCol
Definition: plannodes.h:583
static bool ForeignRecheck ( ForeignScanState node,
TupleTableSlot slot 
)
static

Definition at line 77 of file nodeForeignscan.c.

References ExprContext::ecxt_scantuple, ExecQual(), ForeignScanState::fdw_recheck_quals, ForeignScanState::fdwroutine, ScanState::ps, PlanState::ps_ExprContext, FdwRoutine::RecheckForeignScan, ResetExprContext, and ForeignScanState::ss.

Referenced by ExecForeignScan().

78 {
79  FdwRoutine *fdwroutine = node->fdwroutine;
80  ExprContext *econtext;
81 
82  /*
83  * extract necessary information from foreign scan node
84  */
85  econtext = node->ss.ps.ps_ExprContext;
86 
87  /* Does the tuple meet the remote qual condition? */
88  econtext->ecxt_scantuple = slot;
89 
90  ResetExprContext(econtext);
91 
92  /*
93  * If an outer join is pushed down, RecheckForeignScan may need to store a
94  * different tuple in the slot, because a different set of columns may go
95  * to NULL upon recheck. Otherwise, it shouldn't need to change the slot
96  * contents, just return true or false to indicate whether the quals still
97  * pass. For simple cases, setting fdw_recheck_quals may be easier than
98  * providing this callback.
99  */
100  if (fdwroutine->RecheckForeignScan &&
101  !fdwroutine->RecheckForeignScan(node, slot))
102  return false;
103 
104  return ExecQual(node->fdw_recheck_quals, econtext, false);
105 }
ScanState ss
Definition: execnodes.h:1714
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
List * fdw_recheck_quals
Definition: execnodes.h:1715
RecheckForeignScan_function RecheckForeignScan
Definition: fdwapi.h:210
PlanState ps
Definition: execnodes.h:1290
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5056
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1718
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:130
#define ResetExprContext(econtext)
Definition: executor.h:332