PostgreSQL Source Code  git master
nodeCustom.c
Go to the documentation of this file.
1 /* ------------------------------------------------------------------------
2  *
3  * nodeCustom.c
4  * Routines to handle execution of custom scan node
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * ------------------------------------------------------------------------
10  */
11 #include "postgres.h"
12 
13 #include "access/parallel.h"
14 #include "executor/executor.h"
15 #include "executor/nodeCustom.h"
16 #include "miscadmin.h"
17 #include "nodes/execnodes.h"
18 #include "nodes/extensible.h"
19 #include "nodes/plannodes.h"
20 #include "parser/parsetree.h"
21 #include "utils/hsearch.h"
22 #include "utils/memutils.h"
23 #include "utils/rel.h"
24 
25 static TupleTableSlot *ExecCustomScan(PlanState *pstate);
26 
27 
29 ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
30 {
31  CustomScanState *css;
32  Relation scan_rel = NULL;
33  Index scanrelid = cscan->scan.scanrelid;
34  Index tlistvarno;
35 
36  /*
37  * Allocate the CustomScanState object. We let the custom scan provider
38  * do the palloc, in case it wants to make a larger object that embeds
39  * CustomScanState as the first field. It must set the node tag and the
40  * methods field correctly at this time. Other standard fields should be
41  * set to zero.
42  */
44  cscan->methods->CreateCustomScanState(cscan));
45 
46  /* ensure flags is filled correctly */
47  css->flags = cscan->flags;
48 
49  /* fill up fields of ScanState */
50  css->ss.ps.plan = &cscan->scan.plan;
51  css->ss.ps.state = estate;
53 
54  /* create expression context for node */
55  ExecAssignExprContext(estate, &css->ss.ps);
56 
57  /*
58  * open the scan relation, if any
59  */
60  if (scanrelid > 0)
61  {
62  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
63  css->ss.ss_currentRelation = scan_rel;
64  }
65 
66  /*
67  * Determine the scan tuple type. If the custom scan provider provided a
68  * targetlist describing the scan tuples, use that; else use base
69  * relation's rowtype.
70  */
71  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
72  {
73  TupleDesc scan_tupdesc;
74 
75  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist);
76  ExecInitScanTupleSlot(estate, &css->ss, scan_tupdesc, &TTSOpsVirtual);
77  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
78  tlistvarno = INDEX_VAR;
79  }
80  else
81  {
82  ExecInitScanTupleSlot(estate, &css->ss, RelationGetDescr(scan_rel),
83  &TTSOpsVirtual);
84  /* Node's targetlist will contain Vars with varno = scanrelid */
85  tlistvarno = scanrelid;
86  }
87 
88  /*
89  * Initialize result slot, type and projection.
90  */
92  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
93 
94  /* initialize child expressions */
95  css->ss.ps.qual =
96  ExecInitQual(cscan->scan.plan.qual, (PlanState *) css);
97 
98  /*
99  * The callback of custom-scan provider applies the final initialization
100  * of the custom-scan-state node according to its logic.
101  */
102  css->methods->BeginCustomScan(css, estate, eflags);
103 
104  return css;
105 }
106 
107 static TupleTableSlot *
109 {
110  CustomScanState *node = castNode(CustomScanState, pstate);
111 
113 
114  Assert(node->methods->ExecCustomScan != NULL);
115  return node->methods->ExecCustomScan(node);
116 }
117 
118 void
120 {
121  Assert(node->methods->EndCustomScan != NULL);
122  node->methods->EndCustomScan(node);
123 
124  /* Free the exprcontext */
125  ExecFreeExprContext(&node->ss.ps);
126 
127  /* Clean out the tuple table */
130 }
131 
132 void
134 {
135  Assert(node->methods->ReScanCustomScan != NULL);
136  node->methods->ReScanCustomScan(node);
137 }
138 
139 void
141 {
142  if (!node->methods->MarkPosCustomScan)
143  ereport(ERROR,
144  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
145  errmsg("custom scan \"%s\" does not support MarkPos",
146  node->methods->CustomName)));
147  node->methods->MarkPosCustomScan(node);
148 }
149 
150 void
152 {
153  if (!node->methods->RestrPosCustomScan)
154  ereport(ERROR,
155  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
156  errmsg("custom scan \"%s\" does not support MarkPos",
157  node->methods->CustomName)));
158  node->methods->RestrPosCustomScan(node);
159 }
160 
161 void
163 {
164  const CustomExecMethods *methods = node->methods;
165 
166  if (methods->EstimateDSMCustomScan)
167  {
168  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
170  shm_toc_estimate_keys(&pcxt->estimator, 1);
171  }
172 }
173 
174 void
176 {
177  const CustomExecMethods *methods = node->methods;
178 
179  if (methods->InitializeDSMCustomScan)
180  {
181  int plan_node_id = node->ss.ps.plan->plan_node_id;
182  void *coordinate;
183 
184  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
185  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
186  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
187  }
188 }
189 
190 void
192 {
193  const CustomExecMethods *methods = node->methods;
194 
195  if (methods->ReInitializeDSMCustomScan)
196  {
197  int plan_node_id = node->ss.ps.plan->plan_node_id;
198  void *coordinate;
199 
200  coordinate = shm_toc_lookup(pcxt->toc, plan_node_id, false);
201  methods->ReInitializeDSMCustomScan(node, pcxt, coordinate);
202  }
203 }
204 
205 void
207  ParallelWorkerContext *pwcxt)
208 {
209  const CustomExecMethods *methods = node->methods;
210 
211  if (methods->InitializeWorkerCustomScan)
212  {
213  int plan_node_id = node->ss.ps.plan->plan_node_id;
214  void *coordinate;
215 
216  coordinate = shm_toc_lookup(pwcxt->toc, plan_node_id, false);
217  methods->InitializeWorkerCustomScan(node, pwcxt->toc, coordinate);
218  }
219 }
220 
221 void
223 {
224  const CustomExecMethods *methods = node->methods;
225 
226  if (methods->ShutdownCustomScan)
227  methods->ShutdownCustomScan(node);
228 }
void(* EndCustomScan)(CustomScanState *node)
Definition: extensible.h:129
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:143
Plan plan
Definition: plannodes.h:344
void(* ShutdownCustomScan)(CustomScanState *node)
Definition: extensible.h:148
Index scanrelid
Definition: plannodes.h:345
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:206
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
static TupleTableSlot * ExecCustomScan(PlanState *pstate)
Definition: nodeCustom.c:108
#define RelationGetDescr(relation)
Definition: rel.h:448
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:191
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
void ExecShutdownCustomScan(CustomScanState *node)
Definition: nodeCustom.c:222
shm_toc_estimator estimator
Definition: parallel.h:41
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
int plan_node_id
Definition: plannodes.h:141
void(* ReInitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:142
Size(* EstimateDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt)
Definition: extensible.h:137
int errcode(int sqlerrcode)
Definition: elog.c:608
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1329
Relation ss_currentRelation
Definition: execnodes.h:1327
EState * state
Definition: execnodes.h:941
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:614
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:130
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
const struct CustomExecMethods * methods
Definition: execnodes.h:1810
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:128
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:285
PlanState ps
Definition: execnodes.h:1326
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:151
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
#define ERROR
Definition: elog.h:43
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:693
CustomScanState * ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
Definition: nodeCustom.c:29
ScanState ss
Definition: execnodes.h:1805
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1781
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:113
const struct CustomScanMethods * methods
Definition: plannodes.h:650
#define ereport(elevel, rest)
Definition: elog.h:141
void(* RestrPosCustomScan)(CustomScanState *node)
Definition: extensible.h:134
Scan scan
Definition: plannodes.h:642
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:945
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:145
void ExecEndCustomScan(CustomScanState *node)
Definition: nodeCustom.c:119
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:140
unsigned int Index
Definition: c.h:476
Plan * plan
Definition: execnodes.h:939
const char * CustomName
Definition: extensible.h:122
void(* InitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:139
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:175
#define Assert(condition)
Definition: c.h:739
List * custom_scan_tlist
Definition: plannodes.h:648
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:444
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1908
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1769
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
ExprState * qual
Definition: execnodes.h:960
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:822
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:162
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define INDEX_VAR
Definition: primnodes.h:159
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:133
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44
void(* MarkPosCustomScan)(CustomScanState *node)
Definition: extensible.h:133
uint32 flags
Definition: plannodes.h:643