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-2016, 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  */
38  css = (CustomScanState *) cscan->methods->CreateCustomScanState(cscan);
39  Assert(IsA(css, CustomScanState));
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  css->ss.ps.ps_TupFromTlist = false;
52 
53  /* initialize child expressions */
54  css->ss.ps.targetlist = (List *)
56  (PlanState *) css);
57  css->ss.ps.qual = (List *)
58  ExecInitExpr((Expr *) cscan->scan.plan.qual,
59  (PlanState *) css);
60 
61  /* tuple table initialization */
62  ExecInitScanTupleSlot(estate, &css->ss);
63  ExecInitResultTupleSlot(estate, &css->ss.ps);
64 
65  /*
66  * open the base relation, if any, and acquire an appropriate lock on it
67  */
68  if (scanrelid > 0)
69  {
70  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
71  css->ss.ss_currentRelation = scan_rel;
72  }
73 
74  /*
75  * Determine the scan tuple type. If the custom scan provider provided a
76  * targetlist describing the scan tuples, use that; else use base
77  * relation's rowtype.
78  */
79  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
80  {
81  TupleDesc scan_tupdesc;
82 
83  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist, false);
84  ExecAssignScanType(&css->ss, scan_tupdesc);
85  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
86  tlistvarno = INDEX_VAR;
87  }
88  else
89  {
90  ExecAssignScanType(&css->ss, RelationGetDescr(scan_rel));
91  /* Node's targetlist will contain Vars with varno = scanrelid */
92  tlistvarno = scanrelid;
93  }
94 
95  /*
96  * Initialize result tuple type and projection info.
97  */
99  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
100 
101  /*
102  * The callback of custom-scan provider applies the final initialization
103  * of the custom-scan-state node according to its logic.
104  */
105  css->methods->BeginCustomScan(css, estate, eflags);
106 
107  return css;
108 }
109 
112 {
113  Assert(node->methods->ExecCustomScan != NULL);
114  return node->methods->ExecCustomScan(node);
115 }
116 
117 void
119 {
120  Assert(node->methods->EndCustomScan != NULL);
121  node->methods->EndCustomScan(node);
122 
123  /* Free the exprcontext */
124  ExecFreeExprContext(&node->ss.ps);
125 
126  /* Clean out the tuple table */
129 
130  /* Close the heap relation */
131  if (node->ss.ss_currentRelation)
133 }
134 
135 void
137 {
138  Assert(node->methods->ReScanCustomScan != NULL);
139  node->methods->ReScanCustomScan(node);
140 }
141 
142 void
144 {
145  if (!node->methods->MarkPosCustomScan)
146  ereport(ERROR,
147  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
148  errmsg("custom scan \"%s\" does not support MarkPos",
149  node->methods->CustomName)));
150  node->methods->MarkPosCustomScan(node);
151 }
152 
153 void
155 {
156  if (!node->methods->RestrPosCustomScan)
157  ereport(ERROR,
158  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
159  errmsg("custom scan \"%s\" does not support MarkPos",
160  node->methods->CustomName)));
161  node->methods->RestrPosCustomScan(node);
162 }
163 
164 void
166 {
167  const CustomExecMethods *methods = node->methods;
168 
169  if (methods->EstimateDSMCustomScan)
170  {
171  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
173  shm_toc_estimate_keys(&pcxt->estimator, 1);
174  }
175 }
176 
177 void
179 {
180  const CustomExecMethods *methods = node->methods;
181 
182  if (methods->InitializeDSMCustomScan)
183  {
184  int plan_node_id = node->ss.ps.plan->plan_node_id;
185  void *coordinate;
186 
187  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
188  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
189  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
190  }
191 }
192 
193 void
195 {
196  const CustomExecMethods *methods = node->methods;
197 
198  if (methods->InitializeWorkerCustomScan)
199  {
200  int plan_node_id = node->ss.ps.plan->plan_node_id;
201  void *coordinate;
202 
203  coordinate = shm_toc_lookup(toc, plan_node_id);
204  methods->InitializeWorkerCustomScan(node, toc, coordinate);
205  }
206 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:122
Plan plan
Definition: plannodes.h:286
#define IsA(nodeptr, _type_)
Definition: nodes.h:542
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:845
Index scanrelid
Definition: plannodes.h:287
#define RelationGetDescr(relation)
Definition: rel.h:353
void ExecCustomScanInitializeWorker(CustomScanState *node, shm_toc *toc)
Definition: nodeCustom.c:194
shm_toc_estimator estimator
Definition: parallel.h:43
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:442
int plan_node_id
Definition: plannodes.h:120
List * qual
Definition: execnodes.h:1042
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:110
int errcode(int sqlerrcode)
Definition: elog.c:575
List * targetlist
Definition: execnodes.h:1041
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1251
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:127
Relation ss_currentRelation
Definition: execnodes.h:1249
EState * state
Definition: execnodes.h:1029
#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:696
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:435
const struct CustomExecMethods * methods
Definition: execnodes.h:1617
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
Definition: execScan.c:271
PlanState ps
Definition: execnodes.h:1248
TupleTableSlot * ExecCustomScan(CustomScanState *node)
Definition: nodeCustom.c:111
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:154
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1057
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4452
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:835
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:783
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:1613
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:570
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:891
Scan scan
Definition: plannodes.h:562
void ExecEndCustomScan(CustomScanState *node)
Definition: nodeCustom.c:118
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:143
unsigned int Index
Definition: c.h:361
Plan * plan
Definition: execnodes.h:1027
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:125
const char * CustomName
Definition: extensible.h:119
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:178
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:667
List * custom_scan_tlist
Definition: plannodes.h:567
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:413
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:52
void ExecCloseScanRelation(Relation scanrel)
Definition: execUtils.c:841
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
List * targetlist
Definition: plannodes.h:121
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:720
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:165
#define INDEX_VAR
Definition: primnodes.h:140
bool ps_TupFromTlist
Definition: execnodes.h:1060
Definition: pg_list.h:45
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:136
shm_toc * toc
Definition: parallel.h:46
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:139
uint32 flags
Definition: plannodes.h:563