PostgreSQL Source Code  git master
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-2020, 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  * Insert valid value into tableoid, the only actually-useful system
59  * column.
60  */
61  if (plan->fsSystemCol && !TupIsNull(slot))
63 
64  return slot;
65 }
66 
67 /*
68  * ForeignRecheck -- access method routine to recheck a tuple in EvalPlanQual
69  */
70 static bool
72 {
73  FdwRoutine *fdwroutine = node->fdwroutine;
74  ExprContext *econtext;
75 
76  /*
77  * extract necessary information from foreign scan node
78  */
79  econtext = node->ss.ps.ps_ExprContext;
80 
81  /* Does the tuple meet the remote qual condition? */
82  econtext->ecxt_scantuple = slot;
83 
84  ResetExprContext(econtext);
85 
86  /*
87  * If an outer join is pushed down, RecheckForeignScan may need to store a
88  * different tuple in the slot, because a different set of columns may go
89  * to NULL upon recheck. Otherwise, it shouldn't need to change the slot
90  * contents, just return true or false to indicate whether the quals still
91  * pass. For simple cases, setting fdw_recheck_quals may be easier than
92  * providing this callback.
93  */
94  if (fdwroutine->RecheckForeignScan &&
95  !fdwroutine->RecheckForeignScan(node, slot))
96  return false;
97 
98  return ExecQual(node->fdw_recheck_quals, econtext);
99 }
100 
101 /* ----------------------------------------------------------------
102  * ExecForeignScan(node)
103  *
104  * Fetches the next tuple from the FDW, checks local quals, and
105  * returns it.
106  * We call the ExecScan() routine and pass it the appropriate
107  * access method functions.
108  * ----------------------------------------------------------------
109  */
110 static TupleTableSlot *
112 {
113  ForeignScanState *node = castNode(ForeignScanState, pstate);
114 
115  return ExecScan(&node->ss,
118 }
119 
120 
121 /* ----------------------------------------------------------------
122  * ExecInitForeignScan
123  * ----------------------------------------------------------------
124  */
126 ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
127 {
128  ForeignScanState *scanstate;
129  Relation currentRelation = NULL;
130  Index scanrelid = node->scan.scanrelid;
131  Index tlistvarno;
132  FdwRoutine *fdwroutine;
133 
134  /* check for unsupported flags */
135  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
136 
137  /*
138  * create state structure
139  */
140  scanstate = makeNode(ForeignScanState);
141  scanstate->ss.ps.plan = (Plan *) node;
142  scanstate->ss.ps.state = estate;
143  scanstate->ss.ps.ExecProcNode = ExecForeignScan;
144 
145  /*
146  * Miscellaneous initialization
147  *
148  * create expression context for node
149  */
150  ExecAssignExprContext(estate, &scanstate->ss.ps);
151 
152  /*
153  * open the scan relation, if any; also acquire function pointers from the
154  * FDW's handler
155  */
156  if (scanrelid > 0)
157  {
158  currentRelation = ExecOpenScanRelation(estate, scanrelid, eflags);
159  scanstate->ss.ss_currentRelation = currentRelation;
160  fdwroutine = GetFdwRoutineForRelation(currentRelation, true);
161  }
162  else
163  {
164  /* We can't use the relcache, so get fdwroutine the hard way */
165  fdwroutine = GetFdwRoutineByServerId(node->fs_server);
166  }
167 
168  /*
169  * Determine the scan tuple type. If the FDW provided a targetlist
170  * describing the scan tuples, use that; else use base relation's rowtype.
171  */
172  if (node->fdw_scan_tlist != NIL || currentRelation == NULL)
173  {
174  TupleDesc scan_tupdesc;
175 
176  scan_tupdesc = ExecTypeFromTL(node->fdw_scan_tlist);
177  ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
178  &TTSOpsHeapTuple);
179  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
180  tlistvarno = INDEX_VAR;
181  }
182  else
183  {
184  TupleDesc scan_tupdesc;
185 
186  /* don't trust FDWs to return tuples fulfilling NOT NULL constraints */
187  scan_tupdesc = CreateTupleDescCopy(RelationGetDescr(currentRelation));
188  ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
189  &TTSOpsHeapTuple);
190  /* Node's targetlist will contain Vars with varno = scanrelid */
191  tlistvarno = scanrelid;
192  }
193 
194  /* Don't know what an FDW might return */
195  scanstate->ss.ps.scanopsfixed = false;
196  scanstate->ss.ps.scanopsset = true;
197 
198  /*
199  * Initialize result slot, type and projection.
200  */
201  ExecInitResultTypeTL(&scanstate->ss.ps);
202  ExecAssignScanProjectionInfoWithVarno(&scanstate->ss, tlistvarno);
203 
204  /*
205  * initialize child expressions
206  */
207  scanstate->ss.ps.qual =
208  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
209  scanstate->fdw_recheck_quals =
210  ExecInitQual(node->fdw_recheck_quals, (PlanState *) scanstate);
211 
212  /*
213  * Initialize FDW-related state.
214  */
215  scanstate->fdwroutine = fdwroutine;
216  scanstate->fdw_state = NULL;
217 
218  /*
219  * For the FDW's convenience, look up the modification target relation's.
220  * ResultRelInfo.
221  */
222  if (node->resultRelation > 0)
223  scanstate->resultRelInfo = estate->es_result_relations[node->resultRelation - 1];
224 
225  /* Initialize any outer plan. */
226  if (outerPlan(node))
227  outerPlanState(scanstate) =
228  ExecInitNode(outerPlan(node), estate, eflags);
229 
230  /*
231  * Tell the FDW to initialize the scan.
232  */
233  if (node->operation != CMD_SELECT)
234  fdwroutine->BeginDirectModify(scanstate, eflags);
235  else
236  fdwroutine->BeginForeignScan(scanstate, eflags);
237 
238  return scanstate;
239 }
240 
241 /* ----------------------------------------------------------------
242  * ExecEndForeignScan
243  *
244  * frees any storage allocated through C routines.
245  * ----------------------------------------------------------------
246  */
247 void
249 {
250  ForeignScan *plan = (ForeignScan *) node->ss.ps.plan;
251 
252  /* Let the FDW shut down */
253  if (plan->operation != CMD_SELECT)
254  node->fdwroutine->EndDirectModify(node);
255  else
256  node->fdwroutine->EndForeignScan(node);
257 
258  /* Shut down any outer plan. */
259  if (outerPlanState(node))
261 
262  /* Free the exprcontext */
263  ExecFreeExprContext(&node->ss.ps);
264 
265  /* clean out the tuple table */
266  if (node->ss.ps.ps_ResultTupleSlot)
269 }
270 
271 /* ----------------------------------------------------------------
272  * ExecReScanForeignScan
273  *
274  * Rescans the relation.
275  * ----------------------------------------------------------------
276  */
277 void
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 }
294 
295 /* ----------------------------------------------------------------
296  * ExecForeignScanEstimate
297  *
298  * Informs size of the parallel coordination information, if any
299  * ----------------------------------------------------------------
300  */
301 void
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 }
313 
314 /* ----------------------------------------------------------------
315  * ExecForeignScanInitializeDSM
316  *
317  * Initialize the parallel coordination information
318  * ----------------------------------------------------------------
319  */
320 void
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 }
335 
336 /* ----------------------------------------------------------------
337  * ExecForeignScanReInitializeDSM
338  *
339  * Reset shared state before beginning a fresh scan.
340  * ----------------------------------------------------------------
341  */
342 void
344 {
345  FdwRoutine *fdwroutine = node->fdwroutine;
346 
347  if (fdwroutine->ReInitializeDSMForeignScan)
348  {
349  int plan_node_id = node->ss.ps.plan->plan_node_id;
350  void *coordinate;
351 
352  coordinate = shm_toc_lookup(pcxt->toc, plan_node_id, false);
353  fdwroutine->ReInitializeDSMForeignScan(node, pcxt, coordinate);
354  }
355 }
356 
357 /* ----------------------------------------------------------------
358  * ExecForeignScanInitializeWorker
359  *
360  * Initialization according to the parallel coordination information
361  * ----------------------------------------------------------------
362  */
363 void
365  ParallelWorkerContext *pwcxt)
366 {
367  FdwRoutine *fdwroutine = node->fdwroutine;
368 
369  if (fdwroutine->InitializeWorkerForeignScan)
370  {
371  int plan_node_id = node->ss.ps.plan->plan_node_id;
372  void *coordinate;
373 
374  coordinate = shm_toc_lookup(pwcxt->toc, plan_node_id, false);
375  fdwroutine->InitializeWorkerForeignScan(node, pwcxt->toc, coordinate);
376  }
377 }
378 
379 /* ----------------------------------------------------------------
380  * ExecShutdownForeignScan
381  *
382  * Gives FDW chance to stop asynchronous resource consumption
383  * and release any resources still held.
384  * ----------------------------------------------------------------
385  */
386 void
388 {
389  FdwRoutine *fdwroutine = node->fdwroutine;
390 
391  if (fdwroutine->ShutdownForeignScan)
392  fdwroutine->ShutdownForeignScan(node);
393 }
void ExecForeignScanInitializeWorker(ForeignScanState *node, ParallelWorkerContext *pwcxt)
BeginForeignScan_function BeginForeignScan
Definition: fdwapi.h:191
EndDirectModify_function EndDirectModify
Definition: fdwapi.h:221
#define NIL
Definition: pg_list.h:65
Oid tts_tableOid
Definition: tuptable.h:131
ScanState ss
Definition: execnodes.h:1790
List * qual
Definition: plannodes.h:137
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
Plan plan
Definition: plannodes.h:336
EstimateDSMForeignScan_function EstimateDSMForeignScan
Definition: fdwapi.h:241
Index scanrelid
Definition: plannodes.h:337
Oid fs_server
Definition: plannodes.h:616
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:482
#define castNode(_type_, nodeptr)
Definition: nodes.h:597
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:543
InitializeWorkerForeignScan_function InitializeWorkerForeignScan
Definition: fdwapi.h:244
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:158
ExprContext * ps_ExprContext
Definition: execnodes.h:978
shm_toc_estimator estimator
Definition: parallel.h:42
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
int plan_node_id
Definition: plannodes.h:135
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static bool ForeignRecheck(ForeignScanState *node, TupleTableSlot *slot)
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition: foreign.c:362
static TupleTableSlot * ExecForeignScan(PlanState *pstate)
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1331
List * fdw_scan_tlist
Definition: plannodes.h:619
Relation ss_currentRelation
Definition: execnodes.h:1329
EState * state
Definition: execnodes.h:941
ExprState * fdw_recheck_quals
Definition: execnodes.h:1791
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:372
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:649
CmdType operation
Definition: plannodes.h:614
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:209
void ExecReScanForeignScan(ForeignScanState *node)
void ExecForeignScanReInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
RecheckForeignScan_function RecheckForeignScan
Definition: fdwapi.h:226
IterateDirectModify_function IterateDirectModify
Definition: fdwapi.h:220
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:285
PlanState ps
Definition: execnodes.h:1328
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:719
void ExecEndForeignScan(ForeignScanState *node)
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define outerPlanState(node)
Definition: execnodes.h:1033
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1781
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1725
EndForeignScan_function EndForeignScan
Definition: fdwapi.h:194
#define TupIsNull(slot)
Definition: tuptable.h:292
struct FdwRoutine * fdwroutine
Definition: execnodes.h:1795
List * fdw_recheck_quals
Definition: plannodes.h:620
ResultRelInfo ** es_result_relations
Definition: execnodes.h:534
Bitmapset * chgParam
Definition: execnodes.h:971
#define outerPlan(node)
Definition: plannodes.h:166
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:433
static TupleTableSlot * ForeignNext(ForeignScanState *node)
Index resultRelation
Definition: plannodes.h:615
BeginDirectModify_function BeginDirectModify
Definition: fdwapi.h:219
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:945
void ExecForeignScanInitializeDSM(ForeignScanState *node, ParallelContext *pcxt)
unsigned int Index
Definition: c.h:483
InitializeDSMForeignScan_function InitializeDSMForeignScan
Definition: fdwapi.h:242
Plan * plan
Definition: execnodes.h:939
bool scanopsfixed
Definition: execnodes.h:1015
#define makeNode(_type_)
Definition: nodes.h:576
#define Assert(condition)
Definition: c.h:746
ReScanForeignScan_function ReScanForeignScan
Definition: fdwapi.h:193
#define EXEC_FLAG_MARK
Definition: executor.h:59
IterateForeignScan_function IterateForeignScan
Definition: fdwapi.h:192
void ExecForeignScanEstimate(ForeignScanState *node, ParallelContext *pcxt)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:479
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1908
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:225
bool scanopsset
Definition: execnodes.h:1019
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
ExprState * qual
Definition: execnodes.h:960
ReInitializeDSMForeignScan_function ReInitializeDSMForeignScan
Definition: fdwapi.h:243
ShutdownForeignScan_function ShutdownForeignScan
Definition: fdwapi.h:245
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
ForeignScanState * ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:427
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
void ExecScanReScan(ScanState *node)
Definition: execScan.c:299
void ExecShutdownForeignScan(ForeignScanState *node)
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:432
#define INDEX_VAR
Definition: primnodes.h:173
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
#define RelationGetRelid(relation)
Definition: rel.h:456
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define ResetExprContext(econtext)
Definition: executor.h:503
shm_toc * toc
Definition: parallel.h:45
bool fsSystemCol
Definition: plannodes.h:622