PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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 "nodes/execnodes.h"
17 #include "nodes/plannodes.h"
18 #include "parser/parsetree.h"
19 #include "utils/hsearch.h"
20 #include "utils/memutils.h"
21 #include "utils/rel.h"
22 
24 ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
25 {
26  CustomScanState *css;
27  Relation scan_rel = NULL;
28  Index scanrelid = cscan->scan.scanrelid;
29  Index tlistvarno;
30 
31  /*
32  * Allocate the CustomScanState object. We let the custom scan provider
33  * do the palloc, in case it wants to make a larger object that embeds
34  * CustomScanState as the first field. It must set the node tag and the
35  * methods field correctly at this time. Other standard fields should be
36  * set to zero.
37  */
39  cscan->methods->CreateCustomScanState(cscan));
40 
41  /* ensure flags is filled correctly */
42  css->flags = cscan->flags;
43 
44  /* fill up fields of ScanState */
45  css->ss.ps.plan = &cscan->scan.plan;
46  css->ss.ps.state = estate;
47 
48  /* create expression context for node */
49  ExecAssignExprContext(estate, &css->ss.ps);
50 
51  /* initialize child expressions */
52  css->ss.ps.targetlist = (List *)
54  (PlanState *) css);
55  css->ss.ps.qual = (List *)
56  ExecInitExpr((Expr *) cscan->scan.plan.qual,
57  (PlanState *) css);
58 
59  /* tuple table initialization */
60  ExecInitScanTupleSlot(estate, &css->ss);
61  ExecInitResultTupleSlot(estate, &css->ss.ps);
62 
63  /*
64  * open the base relation, if any, and acquire an appropriate lock on it
65  */
66  if (scanrelid > 0)
67  {
68  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
69  css->ss.ss_currentRelation = scan_rel;
70  }
71 
72  /*
73  * Determine the scan tuple type. If the custom scan provider provided a
74  * targetlist describing the scan tuples, use that; else use base
75  * relation's rowtype.
76  */
77  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
78  {
79  TupleDesc scan_tupdesc;
80 
81  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist, false);
82  ExecAssignScanType(&css->ss, scan_tupdesc);
83  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
84  tlistvarno = INDEX_VAR;
85  }
86  else
87  {
88  ExecAssignScanType(&css->ss, RelationGetDescr(scan_rel));
89  /* Node's targetlist will contain Vars with varno = scanrelid */
90  tlistvarno = scanrelid;
91  }
92 
93  /*
94  * Initialize result tuple type and projection info.
95  */
97  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
98 
99  /*
100  * The callback of custom-scan provider applies the final initialization
101  * of the custom-scan-state node according to its logic.
102  */
103  css->methods->BeginCustomScan(css, estate, eflags);
104 
105  return css;
106 }
107 
110 {
111  Assert(node->methods->ExecCustomScan != NULL);
112  return node->methods->ExecCustomScan(node);
113 }
114 
115 void
117 {
118  Assert(node->methods->EndCustomScan != NULL);
119  node->methods->EndCustomScan(node);
120 
121  /* Free the exprcontext */
122  ExecFreeExprContext(&node->ss.ps);
123 
124  /* Clean out the tuple table */
127 
128  /* Close the heap relation */
129  if (node->ss.ss_currentRelation)
131 }
132 
133 void
135 {
136  Assert(node->methods->ReScanCustomScan != NULL);
137  node->methods->ReScanCustomScan(node);
138 }
139 
140 void
142 {
143  if (!node->methods->MarkPosCustomScan)
144  ereport(ERROR,
145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146  errmsg("custom scan \"%s\" does not support MarkPos",
147  node->methods->CustomName)));
148  node->methods->MarkPosCustomScan(node);
149 }
150 
151 void
153 {
154  if (!node->methods->RestrPosCustomScan)
155  ereport(ERROR,
156  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
157  errmsg("custom scan \"%s\" does not support MarkPos",
158  node->methods->CustomName)));
159  node->methods->RestrPosCustomScan(node);
160 }
161 
162 void
164 {
165  const CustomExecMethods *methods = node->methods;
166 
167  if (methods->EstimateDSMCustomScan)
168  {
169  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
171  shm_toc_estimate_keys(&pcxt->estimator, 1);
172  }
173 }
174 
175 void
177 {
178  const CustomExecMethods *methods = node->methods;
179 
180  if (methods->InitializeDSMCustomScan)
181  {
182  int plan_node_id = node->ss.ps.plan->plan_node_id;
183  void *coordinate;
184 
185  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
186  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
187  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
188  }
189 }
190 
191 void
193 {
194  const CustomExecMethods *methods = node->methods;
195 
196  if (methods->InitializeWorkerCustomScan)
197  {
198  int plan_node_id = node->ss.ps.plan->plan_node_id;
199  void *coordinate;
200 
201  coordinate = shm_toc_lookup(toc, plan_node_id);
202  methods->InitializeWorkerCustomScan(node, toc, coordinate);
203  }
204 }
205 
206 void
208 {
209  const CustomExecMethods *methods = node->methods;
210 
211  if (methods->ShutdownCustomScan)
212  methods->ShutdownCustomScan(node);
213 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:133
void(* ShutdownCustomScan)(CustomScanState *node)
Definition: extensible.h:142
Plan plan
Definition: plannodes.h:315
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
Index scanrelid
Definition: plannodes.h:316
#define RelationGetDescr(relation)
Definition: rel.h:425
#define castNode(_type_, nodeptr)
Definition: nodes.h:587
void ExecShutdownCustomScan(CustomScanState *node)
Definition: nodeCustom.c:207
void ExecCustomScanInitializeWorker(CustomScanState *node, shm_toc *toc)
Definition: nodeCustom.c:192
shm_toc_estimator estimator
Definition: parallel.h:42
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
int plan_node_id
Definition: plannodes.h:131
List * qual
Definition: execnodes.h:1064
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:110
int errcode(int sqlerrcode)
Definition: elog.c:575
List * targetlist
Definition: execnodes.h:1063
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1293
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:127
Relation ss_currentRelation
Definition: execnodes.h:1291
EState * state
Definition: execnodes.h:1051
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:49
Size(* EstimateDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt)
Definition: extensible.h:134
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:686
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:431
const struct CustomExecMethods * methods
Definition: execnodes.h:1744
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:247
PlanState ps
Definition: execnodes.h:1290
TupleTableSlot * ExecCustomScan(CustomScanState *node)
Definition: nodeCustom.c:109
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:152
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1079
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:773
void(* MarkPosCustomScan)(CustomScanState *node)
Definition: extensible.h:130
CustomScanState * ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
Definition: nodeCustom.c:24
ScanState ss
Definition: execnodes.h:1739
void(* EndCustomScan)(CustomScanState *node)
Definition: extensible.h:126
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
void(* BeginCustomScan)(CustomScanState *node, EState *estate, int eflags)
Definition: extensible.h:122
const struct CustomScanMethods * methods
Definition: plannodes.h:612
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
Scan scan
Definition: plannodes.h:603
void ExecEndCustomScan(CustomScanState *node)
Definition: nodeCustom.c:116
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:141
unsigned int Index
Definition: c.h:365
Plan * plan
Definition: execnodes.h:1049
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:125
const char * CustomName
Definition: extensible.h:119
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:176
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
List * custom_scan_tlist
Definition: plannodes.h:609
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:409
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:52
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:831
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
List * targetlist
Definition: plannodes.h:132
void(* InitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:136
void(* RestrPosCustomScan)(CustomScanState *node)
Definition: extensible.h:131
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:710
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:163
#define INDEX_VAR
Definition: primnodes.h:155
Definition: pg_list.h:45
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:134
shm_toc * toc
Definition: parallel.h:45
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:139
uint32 flags
Definition: plannodes.h:604