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-2023, 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  const TupleTableSlotOps *slotOps;
33  Relation scan_rel = NULL;
34  Index scanrelid = cscan->scan.scanrelid;
35  int tlistvarno;
36 
37  /*
38  * Allocate the CustomScanState object. We let the custom scan provider
39  * do the palloc, in case it wants to make a larger object that embeds
40  * CustomScanState as the first field. It must set the node tag and the
41  * methods field correctly at this time. Other standard fields should be
42  * set to zero.
43  */
45  cscan->methods->CreateCustomScanState(cscan));
46 
47  /* ensure flags is filled correctly */
48  css->flags = cscan->flags;
49 
50  /* fill up fields of ScanState */
51  css->ss.ps.plan = &cscan->scan.plan;
52  css->ss.ps.state = estate;
54 
55  /* create expression context for node */
56  ExecAssignExprContext(estate, &css->ss.ps);
57 
58  /*
59  * open the scan relation, if any
60  */
61  if (scanrelid > 0)
62  {
63  scan_rel = ExecOpenScanRelation(estate, scanrelid, eflags);
64  css->ss.ss_currentRelation = scan_rel;
65  }
66 
67  /*
68  * Use a custom slot if specified in CustomScanState or use virtual slot
69  * otherwise.
70  */
71  slotOps = css->slotOps;
72  if (!slotOps)
73  slotOps = &TTSOpsVirtual;
74 
75  /*
76  * Determine the scan tuple type. If the custom scan provider provided a
77  * targetlist describing the scan tuples, use that; else use base
78  * relation's rowtype.
79  */
80  if (cscan->custom_scan_tlist != NIL || scan_rel == NULL)
81  {
82  TupleDesc scan_tupdesc;
83 
84  scan_tupdesc = ExecTypeFromTL(cscan->custom_scan_tlist);
85  ExecInitScanTupleSlot(estate, &css->ss, scan_tupdesc, slotOps);
86  /* Node's targetlist will contain Vars with varno = INDEX_VAR */
87  tlistvarno = INDEX_VAR;
88  }
89  else
90  {
91  ExecInitScanTupleSlot(estate, &css->ss, RelationGetDescr(scan_rel),
92  slotOps);
93  /* Node's targetlist will contain Vars with varno = scanrelid */
94  tlistvarno = scanrelid;
95  }
96 
97  /*
98  * Initialize result slot, type and projection.
99  */
101  ExecAssignScanProjectionInfoWithVarno(&css->ss, tlistvarno);
102 
103  /* initialize child expressions */
104  css->ss.ps.qual =
105  ExecInitQual(cscan->scan.plan.qual, (PlanState *) css);
106 
107  /*
108  * The callback of custom-scan provider applies the final initialization
109  * of the custom-scan-state node according to its logic.
110  */
111  css->methods->BeginCustomScan(css, estate, eflags);
112 
113  return css;
114 }
115 
116 static TupleTableSlot *
118 {
119  CustomScanState *node = castNode(CustomScanState, pstate);
120 
122 
123  Assert(node->methods->ExecCustomScan != NULL);
124  return node->methods->ExecCustomScan(node);
125 }
126 
127 void
129 {
130  Assert(node->methods->EndCustomScan != NULL);
131  node->methods->EndCustomScan(node);
132 }
133 
134 void
136 {
137  Assert(node->methods->ReScanCustomScan != NULL);
138  node->methods->ReScanCustomScan(node);
139 }
140 
141 void
143 {
144  if (!node->methods->MarkPosCustomScan)
145  ereport(ERROR,
146  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
147  errmsg("custom scan \"%s\" does not support MarkPos",
148  node->methods->CustomName)));
149  node->methods->MarkPosCustomScan(node);
150 }
151 
152 void
154 {
155  if (!node->methods->RestrPosCustomScan)
156  ereport(ERROR,
157  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
158  errmsg("custom scan \"%s\" does not support MarkPos",
159  node->methods->CustomName)));
160  node->methods->RestrPosCustomScan(node);
161 }
162 
163 void
165 {
166  const CustomExecMethods *methods = node->methods;
167 
168  if (methods->EstimateDSMCustomScan)
169  {
170  node->pscan_len = methods->EstimateDSMCustomScan(node, pcxt);
172  shm_toc_estimate_keys(&pcxt->estimator, 1);
173  }
174 }
175 
176 void
178 {
179  const CustomExecMethods *methods = node->methods;
180 
181  if (methods->InitializeDSMCustomScan)
182  {
183  int plan_node_id = node->ss.ps.plan->plan_node_id;
184  void *coordinate;
185 
186  coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
187  methods->InitializeDSMCustomScan(node, pcxt, coordinate);
188  shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
189  }
190 }
191 
192 void
194 {
195  const CustomExecMethods *methods = node->methods;
196 
197  if (methods->ReInitializeDSMCustomScan)
198  {
199  int plan_node_id = node->ss.ps.plan->plan_node_id;
200  void *coordinate;
201 
202  coordinate = shm_toc_lookup(pcxt->toc, plan_node_id, false);
203  methods->ReInitializeDSMCustomScan(node, pcxt, coordinate);
204  }
205 }
206 
207 void
209  ParallelWorkerContext *pwcxt)
210 {
211  const CustomExecMethods *methods = node->methods;
212 
213  if (methods->InitializeWorkerCustomScan)
214  {
215  int plan_node_id = node->ss.ps.plan->plan_node_id;
216  void *coordinate;
217 
218  coordinate = shm_toc_lookup(pwcxt->toc, plan_node_id, false);
219  methods->InitializeWorkerCustomScan(node, pwcxt->toc, coordinate);
220  }
221 }
222 
223 void
225 {
226  const CustomExecMethods *methods = node->methods;
227 
228  if (methods->ShutdownCustomScan)
229  methods->ShutdownCustomScan(node);
230 }
unsigned int Index
Definition: c.h:603
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:214
void ExecAssignScanProjectionInfoWithVarno(ScanState *node, int varno)
Definition: execScan.c:284
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1812
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1800
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1939
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
Definition: execUtils.c:702
Assert(fmt[strlen(fmt) - 1] !='\n')
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
void ExecCustomScanInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:177
void ExecShutdownCustomScan(CustomScanState *node)
Definition: nodeCustom.c:224
void ExecCustomScanEstimate(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:164
CustomScanState * ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
Definition: nodeCustom.c:29
void ExecCustomRestrPos(CustomScanState *node)
Definition: nodeCustom.c:153
void ExecReScanCustomScan(CustomScanState *node)
Definition: nodeCustom.c:135
void ExecCustomScanReInitializeDSM(CustomScanState *node, ParallelContext *pcxt)
Definition: nodeCustom.c:193
void ExecEndCustomScan(CustomScanState *node)
Definition: nodeCustom.c:128
void ExecCustomScanInitializeWorker(CustomScanState *node, ParallelWorkerContext *pwcxt)
Definition: nodeCustom.c:208
void ExecCustomMarkPos(CustomScanState *node)
Definition: nodeCustom.c:142
static TupleTableSlot * ExecCustomScan(PlanState *pstate)
Definition: nodeCustom.c:117
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
#define NIL
Definition: pg_list.h:68
#define INDEX_VAR
Definition: primnodes.h:216
#define RelationGetDescr(relation)
Definition: rel.h:530
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void(* BeginCustomScan)(CustomScanState *node, EState *estate, int eflags)
Definition: extensible.h:129
TupleTableSlot *(* ExecCustomScan)(CustomScanState *node)
Definition: extensible.h:132
void(* EndCustomScan)(CustomScanState *node)
Definition: extensible.h:133
void(* ShutdownCustomScan)(CustomScanState *node)
Definition: extensible.h:152
const char * CustomName
Definition: extensible.h:126
void(* ReInitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:146
void(* InitializeDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt, void *coordinate)
Definition: extensible.h:143
void(* RestrPosCustomScan)(CustomScanState *node)
Definition: extensible.h:138
Size(* EstimateDSMCustomScan)(CustomScanState *node, ParallelContext *pcxt)
Definition: extensible.h:141
void(* MarkPosCustomScan)(CustomScanState *node)
Definition: extensible.h:137
void(* InitializeWorkerCustomScan)(CustomScanState *node, shm_toc *toc, void *coordinate)
Definition: extensible.h:149
void(* ReScanCustomScan)(CustomScanState *node)
Definition: extensible.h:134
Node *(* CreateCustomScanState)(CustomScan *cscan)
Definition: extensible.h:117
const struct TupleTableSlotOps * slotOps
Definition: execnodes.h:1986
const struct CustomExecMethods * methods
Definition: execnodes.h:1985
ScanState ss
Definition: execnodes.h:1980
uint32 flags
Definition: plannodes.h:740
List * custom_scan_tlist
Definition: plannodes.h:745
Scan scan
Definition: plannodes.h:739
const struct CustomScanMethods * methods
Definition: plannodes.h:753
shm_toc_estimator estimator
Definition: parallel.h:42
shm_toc * toc
Definition: parallel.h:45
ExprState * qual
Definition: execnodes.h:1058
Plan * plan
Definition: execnodes.h:1037
EState * state
Definition: execnodes.h:1039
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1043
int plan_node_id
Definition: plannodes.h:152
Relation ss_currentRelation
Definition: execnodes.h:1475
PlanState ps
Definition: execnodes.h:1474
Index scanrelid
Definition: plannodes.h:387