1 /*-------------------------------------------------------------------------
4 * Routines to handle materialization nodes.
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
17 * ExecMaterial - materialize the result of a subplan
18 * ExecInitMaterial - initialize node and subnodes
19 * ExecEndMaterial - shutdown node and subnodes
24 #include "executor/executor.h"
25 #include "executor/nodeMaterial.h"
26 #include "miscadmin.h"
28 /* ----------------------------------------------------------------
31 * As long as we are at the end of the data collected in the tuplestore,
32 * we collect one new row from the subplan on each call, and stash it
33 * aside in the tuplestore before returning it. The tuplestore is
34 * only read if we are asked to scan backwards, rescan, or mark/restore.
36 * ----------------------------------------------------------------
38 TupleTableSlot
* /* result tuple from subplan */
39 ExecMaterial(MaterialState
*node
)
44 Tuplestorestate
*tuplestorestate
;
49 * get state info from node
51 estate
= node
->ss
.ps
.state
;
52 dir
= estate
->es_direction
;
53 forward
= ScanDirectionIsForward(dir
);
54 tuplestorestate
= node
->tuplestorestate
;
57 * If first time through, and we need a tuplestore, initialize it.
59 if (tuplestorestate
== NULL
&& node
->eflags
!= 0)
61 tuplestorestate
= tuplestore_begin_heap(true, false, work_mem
);
62 tuplestore_set_eflags(tuplestorestate
, node
->eflags
);
63 if (node
->eflags
& EXEC_FLAG_MARK
)
66 * Allocate a second read pointer to serve as the mark.
67 * We know it must have index 1, so needn't store that.
71 ptrn
= tuplestore_alloc_read_pointer(tuplestorestate
,
75 node
->tuplestorestate
= tuplestorestate
;
79 * If we are not at the end of the tuplestore, or are going backwards, try
80 * to fetch a tuple from tuplestore.
82 eof_tuplestore
= (tuplestorestate
== NULL
) ||
83 tuplestore_ateof(tuplestorestate
);
85 if (!forward
&& eof_tuplestore
)
87 if (!node
->eof_underlying
)
90 * When reversing direction at tuplestore EOF, the first
91 * gettupleslot call will fetch the last-added tuple; but we want
92 * to return the one before that, if possible. So do an extra
95 if (!tuplestore_advance(tuplestorestate
, forward
))
96 return NULL
; /* the tuplestore must be empty */
98 eof_tuplestore
= false;
102 * If we can fetch another tuple from the tuplestore, return it.
104 slot
= node
->ss
.ps
.ps_ResultTupleSlot
;
107 if (tuplestore_gettupleslot(tuplestorestate
, forward
, slot
))
110 eof_tuplestore
= true;
114 * If necessary, try to fetch another row from the subplan.
116 * Note: the eof_underlying state variable exists to short-circuit further
117 * subplan calls. It's not optional, unfortunately, because some plan
118 * node types are not robust about being called again when they've already
121 if (eof_tuplestore
&& !node
->eof_underlying
)
123 PlanState
*outerNode
;
124 TupleTableSlot
*outerslot
;
127 * We can only get here with forward==true, so no need to worry about
128 * which direction the subplan will go.
130 outerNode
= outerPlanState(node
);
131 outerslot
= ExecProcNode(outerNode
);
132 if (TupIsNull(outerslot
))
134 node
->eof_underlying
= true;
139 * Append a copy of the returned tuple to tuplestore. NOTE: because
140 * the tuplestore is certainly in EOF state, its read position will
141 * move forward over the added tuple. This is what we want.
144 tuplestore_puttupleslot(tuplestorestate
, outerslot
);
147 * We can just return the subplan's returned tuple, without copying.
155 return ExecClearTuple(slot
);
158 /* ----------------------------------------------------------------
160 * ----------------------------------------------------------------
163 ExecInitMaterial(Material
*node
, EState
*estate
, int eflags
)
165 MaterialState
*matstate
;
169 * create state structure
171 matstate
= makeNode(MaterialState
);
172 matstate
->ss
.ps
.plan
= (Plan
*) node
;
173 matstate
->ss
.ps
.state
= estate
;
176 * We must have a tuplestore buffering the subplan output to do backward
177 * scan or mark/restore. We also prefer to materialize the subplan output
178 * if we might be called on to rewind and replay it many times. However,
179 * if none of these cases apply, we can skip storing the data.
181 matstate
->eflags
= (eflags
& (EXEC_FLAG_REWIND
|
185 matstate
->eof_underlying
= false;
186 matstate
->tuplestorestate
= NULL
;
189 * Miscellaneous initialization
191 * Materialization nodes don't need ExprContexts because they never call
192 * ExecQual or ExecProject.
195 #define MATERIAL_NSLOTS 2
198 * tuple table initialization
200 * material nodes only return tuples from their materialized relation.
202 ExecInitResultTupleSlot(estate
, &matstate
->ss
.ps
);
203 ExecInitScanTupleSlot(estate
, &matstate
->ss
);
206 * initialize child nodes
208 * We shield the child node from the need to support REWIND, BACKWARD, or
211 eflags
&= ~(EXEC_FLAG_REWIND
| EXEC_FLAG_BACKWARD
| EXEC_FLAG_MARK
);
213 outerPlan
= outerPlan(node
);
214 outerPlanState(matstate
) = ExecInitNode(outerPlan
, estate
, eflags
);
217 * initialize tuple type. no need to initialize projection info because
218 * this node doesn't do projections.
220 ExecAssignResultTypeFromTL(&matstate
->ss
.ps
);
221 ExecAssignScanTypeFromOuterPlan(&matstate
->ss
);
222 matstate
->ss
.ps
.ps_ProjInfo
= NULL
;
228 ExecCountSlotsMaterial(Material
*node
)
230 return ExecCountSlotsNode(outerPlan((Plan
*) node
)) +
231 ExecCountSlotsNode(innerPlan((Plan
*) node
)) +
235 /* ----------------------------------------------------------------
237 * ----------------------------------------------------------------
240 ExecEndMaterial(MaterialState
*node
)
243 * clean out the tuple table
245 ExecClearTuple(node
->ss
.ss_ScanTupleSlot
);
248 * Release tuplestore resources
250 if (node
->tuplestorestate
!= NULL
)
251 tuplestore_end(node
->tuplestorestate
);
252 node
->tuplestorestate
= NULL
;
255 * shut down the subplan
257 ExecEndNode(outerPlanState(node
));
260 /* ----------------------------------------------------------------
261 * ExecMaterialMarkPos
263 * Calls tuplestore to save the current position in the stored file.
264 * ----------------------------------------------------------------
267 ExecMaterialMarkPos(MaterialState
*node
)
269 Assert(node
->eflags
& EXEC_FLAG_MARK
);
272 * if we haven't materialized yet, just return.
274 if (!node
->tuplestorestate
)
278 * copy the active read pointer to the mark.
280 tuplestore_copy_read_pointer(node
->tuplestorestate
, 0, 1);
283 /* ----------------------------------------------------------------
284 * ExecMaterialRestrPos
286 * Calls tuplestore to restore the last saved file position.
287 * ----------------------------------------------------------------
290 ExecMaterialRestrPos(MaterialState
*node
)
292 Assert(node
->eflags
& EXEC_FLAG_MARK
);
295 * if we haven't materialized yet, just return.
297 if (!node
->tuplestorestate
)
301 * copy the mark to the active read pointer.
303 tuplestore_copy_read_pointer(node
->tuplestorestate
, 1, 0);
306 /* ----------------------------------------------------------------
309 * Rescans the materialized relation.
310 * ----------------------------------------------------------------
313 ExecMaterialReScan(MaterialState
*node
, ExprContext
*exprCtxt
)
315 ExecClearTuple(node
->ss
.ps
.ps_ResultTupleSlot
);
317 if (node
->eflags
!= 0)
320 * If we haven't materialized yet, just return. If outerplan' chgParam
321 * is not NULL then it will be re-scanned by ExecProcNode, else - no
322 * reason to re-scan it at all.
324 if (!node
->tuplestorestate
)
328 * If subnode is to be rescanned then we forget previous stored
329 * results; we have to re-read the subplan and re-store. Also, if we
330 * told tuplestore it needn't support rescan, we lose and must
331 * re-read. (This last should not happen in common cases; else our
332 * caller lied by not passing EXEC_FLAG_REWIND to us.)
334 * Otherwise we can just rewind and rescan the stored output. The
335 * state of the subnode does not change.
337 if (((PlanState
*) node
)->lefttree
->chgParam
!= NULL
||
338 (node
->eflags
& EXEC_FLAG_REWIND
) == 0)
340 tuplestore_end(node
->tuplestorestate
);
341 node
->tuplestorestate
= NULL
;
342 if (((PlanState
*) node
)->lefttree
->chgParam
== NULL
)
343 ExecReScan(((PlanState
*) node
)->lefttree
, exprCtxt
);
344 node
->eof_underlying
= false;
347 tuplestore_rescan(node
->tuplestorestate
);
351 /* In this case we are just passing on the subquery's output */
354 * if chgParam of subnode is not null then plan will be re-scanned by
355 * first ExecProcNode.
357 if (((PlanState
*) node
)->lefttree
->chgParam
== NULL
)
358 ExecReScan(((PlanState
*) node
)->lefttree
, exprCtxt
);
359 node
->eof_underlying
= false;