PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeBitmapOr.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeBitmapOr.c
4 * routines to handle BitmapOr nodes.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/executor/nodeBitmapOr.c
12 *
13 *-------------------------------------------------------------------------
14 */
15/* INTERFACE ROUTINES
16 * ExecInitBitmapOr - initialize the BitmapOr node
17 * MultiExecBitmapOr - retrieve the result bitmap from the node
18 * ExecEndBitmapOr - shut down the BitmapOr node
19 * ExecReScanBitmapOr - rescan the BitmapOr node
20 *
21 * NOTES
22 * BitmapOr nodes don't make use of their left and right
23 * subtrees, rather they maintain a list of subplans,
24 * much like Append nodes. The logic is much simpler than
25 * Append, however, since we needn't cope with forward/backward
26 * execution.
27 */
28
29#include "postgres.h"
30
31#include "executor/executor.h"
32#include "executor/instrument.h"
34#include "nodes/tidbitmap.h"
35#include "miscadmin.h"
36
37
38/* ----------------------------------------------------------------
39 * ExecBitmapOr
40 *
41 * stub for pro forma compliance
42 * ----------------------------------------------------------------
43 */
44static TupleTableSlot *
46{
47 elog(ERROR, "BitmapOr node does not support ExecProcNode call convention");
48 return NULL;
49}
50
51/* ----------------------------------------------------------------
52 * ExecInitBitmapOr
53 *
54 * Begin all of the subscans of the BitmapOr node.
55 * ----------------------------------------------------------------
56 */
58ExecInitBitmapOr(BitmapOr *node, EState *estate, int eflags)
59{
62 int nplans;
63 int i;
64 ListCell *l;
66
67 /* check for unsupported flags */
69
70 /*
71 * Set up empty vector of subplan states
72 */
73 nplans = list_length(node->bitmapplans);
74
75 bitmapplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
76
77 /*
78 * create new BitmapOrState for our BitmapOr node
79 */
80 bitmaporstate->ps.plan = (Plan *) node;
81 bitmaporstate->ps.state = estate;
82 bitmaporstate->ps.ExecProcNode = ExecBitmapOr;
83 bitmaporstate->bitmapplans = bitmapplanstates;
84 bitmaporstate->nplans = nplans;
85
86 /*
87 * call ExecInitNode on each of the plans to be executed and save the
88 * results into the array "bitmapplanstates".
89 */
90 i = 0;
91 foreach(l, node->bitmapplans)
92 {
93 initNode = (Plan *) lfirst(l);
94 bitmapplanstates[i] = ExecInitNode(initNode, estate, eflags);
95 i++;
96 }
97
98 /*
99 * Miscellaneous initialization
100 *
101 * BitmapOr plans don't have expression contexts because they never call
102 * ExecQual or ExecProject. They don't need any tuple slots either.
103 */
104
105 return bitmaporstate;
106}
107
108/* ----------------------------------------------------------------
109 * MultiExecBitmapOr
110 * ----------------------------------------------------------------
111 */
112Node *
114{
115 PlanState **bitmapplans;
116 int nplans;
117 int i;
118 TIDBitmap *result = NULL;
119
120 /* must provide our own instrumentation support */
121 if (node->ps.instrument)
123
124 /*
125 * get information from the node
126 */
127 bitmapplans = node->bitmapplans;
128 nplans = node->nplans;
129
130 /*
131 * Scan all the subplans and OR their result bitmaps
132 */
133 for (i = 0; i < nplans; i++)
134 {
135 PlanState *subnode = bitmapplans[i];
137
138 /*
139 * We can special-case BitmapIndexScan children to avoid an explicit
140 * tbm_union step for each child: just pass down the current result
141 * bitmap and let the child OR directly into it.
142 */
144 {
145 if (result == NULL) /* first subplan */
146 {
147 /* XXX should we use less than work_mem for this? */
148 result = tbm_create(work_mem * (Size) 1024,
149 ((BitmapOr *) node->ps.plan)->isshared ?
150 node->ps.state->es_query_dsa : NULL);
151 }
152
153 ((BitmapIndexScanState *) subnode)->biss_result = result;
154
156
157 if (subresult != result)
158 elog(ERROR, "unrecognized result from subplan");
159 }
160 else
161 {
162 /* standard implementation */
164
165 if (!subresult || !IsA(subresult, TIDBitmap))
166 elog(ERROR, "unrecognized result from subplan");
167
168 if (result == NULL)
169 result = subresult; /* first subplan */
170 else
171 {
172 tbm_union(result, subresult);
174 }
175 }
176 }
177
178 /* We could return an empty result set here? */
179 if (result == NULL)
180 elog(ERROR, "BitmapOr doesn't support zero inputs");
181
182 /* must provide our own instrumentation support */
183 if (node->ps.instrument)
184 InstrStopNode(node->ps.instrument, 0 /* XXX */ );
185
186 return (Node *) result;
187}
188
189/* ----------------------------------------------------------------
190 * ExecEndBitmapOr
191 *
192 * Shuts down the subscans of the BitmapOr node.
193 *
194 * Returns nothing of interest.
195 * ----------------------------------------------------------------
196 */
197void
199{
200 PlanState **bitmapplans;
201 int nplans;
202 int i;
203
204 /*
205 * get information from the node
206 */
207 bitmapplans = node->bitmapplans;
208 nplans = node->nplans;
209
210 /*
211 * shut down each of the subscans (that we've initialized)
212 */
213 for (i = 0; i < nplans; i++)
214 {
215 if (bitmapplans[i])
216 ExecEndNode(bitmapplans[i]);
217 }
218}
219
220void
222{
223 int i;
224
225 for (i = 0; i < node->nplans; i++)
226 {
227 PlanState *subnode = node->bitmapplans[i];
228
229 /*
230 * ExecReScan doesn't know about my subplans, so I have to do
231 * changed-parameter signaling myself.
232 */
233 if (node->ps.chgParam != NULL)
235
236 /*
237 * If chgParam of subnode is not null then plan will be re-scanned by
238 * first ExecProcNode.
239 */
240 if (subnode->chgParam == NULL)
242 }
243}
#define Assert(condition)
Definition c.h:945
size_t Size
Definition c.h:691
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
void ExecReScan(PlanState *node)
Definition execAmi.c:78
Node * MultiExecProcNode(PlanState *node)
void ExecEndNode(PlanState *node)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition execUtils.c:915
#define EXEC_FLAG_BACKWARD
Definition executor.h:70
#define EXEC_FLAG_MARK
Definition executor.h:71
int work_mem
Definition globals.c:131
void InstrStartNode(Instrumentation *instr)
Definition instrument.c:68
void InstrStopNode(Instrumentation *instr, double nTuples)
Definition instrument.c:88
int i
Definition isn.c:77
void * palloc0(Size size)
Definition mcxt.c:1417
void ExecEndBitmapOr(BitmapOrState *node)
Node * MultiExecBitmapOr(BitmapOrState *node)
void ExecReScanBitmapOr(BitmapOrState *node)
BitmapOrState * ExecInitBitmapOr(BitmapOr *node, EState *estate, int eflags)
static TupleTableSlot * ExecBitmapOr(PlanState *pstate)
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define makeNode(_type_)
Definition nodes.h:161
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
static int fb(int x)
PlanState ps
Definition execnodes.h:1607
PlanState ** bitmapplans
Definition execnodes.h:1608
List * bitmapplans
Definition plannodes.h:524
struct dsa_area * es_query_dsa
Definition execnodes.h:764
Definition nodes.h:135
Instrumentation * instrument
Definition execnodes.h:1187
Plan * plan
Definition execnodes.h:1177
EState * state
Definition execnodes.h:1179
Bitmapset * chgParam
Definition execnodes.h:1209
void tbm_free(TIDBitmap *tbm)
Definition tidbitmap.c:312
void tbm_union(TIDBitmap *a, const TIDBitmap *b)
Definition tidbitmap.c:447
TIDBitmap * tbm_create(Size maxbytes, dsa_area *dsa)
Definition tidbitmap.c:256