PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeForeignscan.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeForeignscan.c
4  * Routines to support scans of foreign tables
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/nodeForeignscan.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  *
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.
22  */
23 #include "postgres.h"
24 
25 #include "executor/executor.h"
27 #include "foreign/fdwapi.h"
28 #include "utils/memutils.h"
29 #include "utils/rel.h"
30 
32 static bool ForeignRecheck(ForeignScanState *node, TupleTableSlot *slot);
33 
34 
35 /* ----------------------------------------------------------------
36  * ForeignNext
37  *
38  * This is a workhorse for ExecForeignScan
39  * ----------------------------------------------------------------
40  */
41 static TupleTableSlot *
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 }
72 
73 /*
74  * ForeignRecheck -- access method routine to recheck a tuple in EvalPlanQual
75  */
76 static bool
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);
105 }
106 
107 /* ----------------------------------------------------------------
108  * ExecForeignScan(node)
109  *
110  * Fetches the next tuple from the FDW, checks local quals, and
111  * returns it.
112  * We call the ExecScan() routine and pass it the appropriate
113  * access method functions.
114  * ----------------------------------------------------------------
115  */
118 {
119  return ExecScan((ScanState *) node,
122 }
123 
124 
125 /* ----------------------------------------------------------------
126  * ExecInitForeignScan
127  * ----------------------------------------------------------------
128  */
130 ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
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.qual =
159  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
160  scanstate->fdw_recheck_quals =
161  ExecInitQual(node->fdw_recheck_quals, (PlanState *) scanstate);
162 
163  /*
164  * tuple table initialization
165  */
166  ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
167  ExecInitScanTupleSlot(estate, &scanstate->ss);
168 
169  /*
170  * open the base relation, if any, and acquire an appropriate lock on it;
171  * also acquire function pointers from the FDW's handler
172  */
173  if (scanrelid > 0)
174  {
175  currentRelation = ExecOpenScanRelation(estate, scanrelid, eflags);
176  scanstate->ss.ss_currentRelation = currentRelation;
177  fdwroutine = GetFdwRoutineForRelation(currentRelation, true);
178  }
179  else
180  {
181  /* We can't use the relcache, so get fdwroutine the hard way */
182  fdwroutine = GetFdwRoutineByServerId(node->fs_server);
183  }
184 
185  /*
186  * Determine the scan tuple type. If the FDW provided a targetlist
187  * describing the scan tuples, use that; else use base relation's rowtype.
188  */
189  if (node->fdw_scan_tlist != NIL || currentRelation == NULL)
190  {
191  TupleDesc scan_tupdesc;
192 
193  scan_tupdesc = ExecTypeFromTL(node->fdw_scan_tlist, false);
194  ExecAssignScanType(&scanstate->ss, scan_tupdesc);
195  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
196  tlistvarno = INDEX_VAR;
197  }
198  else
199  {
200  ExecAssignScanType(&scanstate->ss, RelationGetDescr(currentRelation));
201  /* Node's targetlist will contain Vars with varno = scanrelid */
202  tlistvarno = scanrelid;
203  }
204 
205  /*
206  * Initialize result tuple type and projection info.
207  */
208  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
209  ExecAssignScanProjectionInfoWithVarno(&scanstate->ss, tlistvarno);
210 
211  /*
212  * Initialize FDW-related state.
213  */
214  scanstate->fdwroutine = fdwroutine;
215  scanstate->fdw_state = NULL;
216 
217  /* Initialize any outer plan. */
218  if (outerPlan(node))
219  outerPlanState(scanstate) =
220  ExecInitNode(outerPlan(node), estate, eflags);
221 
222  /*
223  * Tell the FDW to initialize the scan.
224  */
225  if (node->operation != CMD_SELECT)
226  fdwroutine->BeginDirectModify(scanstate, eflags);
227  else
228  fdwroutine->BeginForeignScan(scanstate, eflags);
229 
230  return scanstate;
231 }
232 
233 /* ----------------------------------------------------------------
234  * ExecEndForeignScan
235  *
236  * frees any storage allocated through C routines.
237  * ----------------------------------------------------------------
238  */
239 void
241 {
242  ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
243 
244  /* Let the FDW shut down */
245  if (plan->operation != CMD_SELECT)
246  node->fdwroutine->EndDirectModify(node);
247  else
248  node->fdwroutine->EndForeignScan(node);
249 
250  /* Shut down any outer plan. */
251  if (outerPlanState(node))
253 
254  /* Free the exprcontext */
255  ExecFreeExprContext(&node->ss.ps);
256 
257  /* clean out the tuple table */
260 
261  /* close the relation. */
262  if (node->ss.ss_currentRelation)
264 }
265 
266 /* ----------------------------------------------------------------
267  * ExecReScanForeignScan
268  *
269  * Rescans the relation.
270  * ----------------------------------------------------------------
271  */
272 void
274 {
276 
277  node->fdwroutine->ReScanForeignScan(node);
278 
279  /*
280  * If chgParam of subnode is not null then plan will be re-scanned by
281  * first ExecProcNode. outerPlan may also be NULL, in which case there is
282  * nothing to rescan at all.
283  */
284  if (outerPlan != NULL && outerPlan->chgParam == NULL)
285  ExecReScan(outerPlan);
286 
287  ExecScanReScan(&node->ss);
288 }
289 
290 /* ----------------------------------------------------------------
291  * ExecForeignScanEstimate
292  *
293  * Informs size of the parallel coordination information, if any
294  * ----------------------------------------------------------------
295  */
296 void
298 {
299  FdwRoutine *fdwroutine = node->fdwroutine;
300 
301  if (fdwroutine->EstimateDSMForeignScan)
302  {
303  node->pscan_len = fdwroutine->EstimateDSMForeignScan(node, pcxt);
305  shm_toc_estimate_keys(&pcxt->estimator, 1);
306  }
307 }
308 
309 /* ----------------------------------------------------------------
310  * ExecForeignScanInitializeDSM
311  *
312  * Initialize the parallel coordination information
313  * ----------------------------------------------------------------
314  */
315 void
317 {
318  FdwRoutine *fdwroutine = node->fdwroutine;
319 
320  if (fdwroutine->InitializeDSMForeignScan)
321  {
322  int plan_node_id = node->ss.ps.plan->plan_node_id;
323  void *coordinate;
324 
325  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
326  fdwroutine->InitializeDSMForeignScan(node, pcxt, coordinate);
327  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
328  }
329 }
330 
331 /* ----------------------------------------------------------------
332  * ExecForeignScanInitializeDSM
333  *
334  * Initialization according to the parallel coordination information
335  * ----------------------------------------------------------------
336  */
337 void
339 {
340  FdwRoutine *fdwroutine = node->fdwroutine;
341 
342  if (fdwroutine->InitializeWorkerForeignScan)
343  {
344  int plan_node_id = node->ss.ps.plan->plan_node_id;
345  void *coordinate;
346 
347  coordinate = shm_toc_lookup(toc, plan_node_id, false);
348  fdwroutine->InitializeWorkerForeignScan(node, toc, coordinate);
349  }
350 }
351 
352 /* ----------------------------------------------------------------
353  * ExecShutdownForeignScan
354  *
355  * Gives FDW chance to stop asynchronous resource consumption
356  * and release any resources still held.
357  * ----------------------------------------------------------------
358  */
359 void
361 {
362  FdwRoutine *fdwroutine = node->fdwroutine;
363 
364  if (fdwroutine->ShutdownForeignScan)
365  fdwroutine->ShutdownForeignScan(node);
366 }
BeginForeignScan_function BeginForeignScan
Definition: fdwapi.h:177
EndDirectModify_function EndDirectModify
Definition: fdwapi.h:205
#define NIL
Definition: pg_list.h:69
ScanState ss
Definition: execnodes.h:1518
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
EstimateDSMForeignScan_function EstimateDSMForeignScan
Definition: fdwapi.h:225
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:329
Oid fs_server
Definition: plannodes.h:599
#define RelationGetDescr(relation)
Definition: rel.h:428
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:654
InitializeWorkerForeignScan_function InitializeWorkerForeignScan
Definition: fdwapi.h:227
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
ExprContext * ps_ExprContext
Definition: execnodes.h:862
shm_toc_estimator estimator
Definition: parallel.h:41
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
int plan_node_id
Definition: plannodes.h:143
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static bool ForeignRecheck(ForeignScanState *node, TupleTableSlot *slot)
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition: foreign.c:330
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1078
List * fdw_scan_tlist
Definition: plannodes.h:602
Relation ss_currentRelation
Definition: execnodes.h:1076
EState * state
Definition: execnodes.h:834
ExprState * fdw_recheck_quals
Definition: execnodes.h:1519
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:516
CmdType operation
Definition: plannodes.h:598
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:440
void ExecReScanForeignScan(ForeignScanState *node)
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:389
RecheckForeignScan_function RecheckForeignScan
Definition: fdwapi.h:210
IterateDirectModify_function IterateDirectModify
Definition: fdwapi.h:204
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:247
PlanState ps
Definition: execnodes.h:1075
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:390
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:861
TupleTableSlot * ExecForeignScan(ForeignScanState *node)
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:603
void ExecEndForeignScan(ForeignScanState *node)
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:874
EndForeignScan_function EndForeignScan
Definition: fdwapi.h:180
void ExecForeignScanInitializeWorker(ForeignScanState *node, shm_toc *toc)
#define TupIsNull(slot)
Definition: tuptable.h:138
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1522
Oid t_tableOid
Definition: htup.h:66
List * fdw_recheck_quals
Definition: plannodes.h:603
Bitmapset * chgParam
Definition: execnodes.h:856
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
#define outerPlan(node)
Definition: plannodes.h:174
static TupleTableSlot * ForeignNext(ForeignScanState *node)
BeginDirectModify_function BeginDirectModify
Definition: fdwapi.h:203
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
unsigned int Index
Definition: c.h:365
InitializeDSMForeignScan_function InitializeDSMForeignScan
Definition: fdwapi.h:226
Plan * plan
Definition: execnodes.h:832
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
ReScanForeignScan_function ReScanForeignScan
Definition: fdwapi.h:179
#define EXEC_FLAG_MARK
Definition: executor.h:61
IterateForeignScan_function IterateForeignScan
Definition: fdwapi.h:178
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:418
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:661
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:197
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:725
ExprState * qual
Definition: execnodes.h:846
ShutdownForeignScan_function ShutdownForeignScan
Definition: fdwapi.h:228
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
ForeignScanState * ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
void ExecScanReScan(ScanState *node)
Definition: execScan.c:327
void ExecShutdownForeignScan(ForeignScanState *node)
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:540
#define INDEX_VAR
Definition: primnodes.h:155
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:140
#define RelationGetRelid(relation)
Definition: rel.h:416
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:222
#define ResetExprContext(econtext)
Definition: executor.h:450
shm_toc * toc
Definition: parallel.h:44
bool fsSystemCol
Definition: plannodes.h:605