PostgreSQL Source Code git master
Loading...
Searching...
No Matches
nodeBitmapAnd.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * nodeBitmapAnd.c
4 * routines to handle BitmapAnd 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/nodeBitmapAnd.c
12 *
13 *-------------------------------------------------------------------------
14 */
15/*
16 * INTERFACE ROUTINES
17 * ExecInitBitmapAnd - initialize the BitmapAnd node
18 * MultiExecBitmapAnd - retrieve the result bitmap from the node
19 * ExecEndBitmapAnd - shut down the BitmapAnd node
20 * ExecReScanBitmapAnd - rescan the BitmapAnd node
21 *
22 * NOTES
23 * BitmapAnd nodes don't make use of their left and right
24 * subtrees, rather they maintain a list of subplans,
25 * much like Append nodes. The logic is much simpler than
26 * Append, however, since we needn't cope with forward/backward
27 * execution.
28 */
29
30#include "postgres.h"
31
32#include "executor/executor.h"
33#include "executor/instrument.h"
35#include "nodes/tidbitmap.h"
36
37
38/* ----------------------------------------------------------------
39 * ExecBitmapAnd
40 *
41 * stub for pro forma compliance
42 * ----------------------------------------------------------------
43 */
44static TupleTableSlot *
46{
47 elog(ERROR, "BitmapAnd node does not support ExecProcNode call convention");
48 return NULL;
49}
50
51/* ----------------------------------------------------------------
52 * ExecInitBitmapAnd
53 *
54 * Begin all of the subscans of the BitmapAnd node.
55 * ----------------------------------------------------------------
56 */
58ExecInitBitmapAnd(BitmapAnd *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 BitmapAndState for our BitmapAnd node
79 */
80 bitmapandstate->ps.plan = (Plan *) node;
81 bitmapandstate->ps.state = estate;
82 bitmapandstate->ps.ExecProcNode = ExecBitmapAnd;
83 bitmapandstate->bitmapplans = bitmapplanstates;
84 bitmapandstate->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 * BitmapAnd 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 bitmapandstate;
106}
107
108/* ----------------------------------------------------------------
109 * MultiExecBitmapAnd
110 * ----------------------------------------------------------------
111 */
112Node *
114{
115 PlanState **bitmapplans;
116 int nplans;
117 int i;
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 AND their result bitmaps
132 */
133 for (i = 0; i < nplans; i++)
134 {
135 PlanState *subnode = bitmapplans[i];
137
139
140 if (!subresult || !IsA(subresult, TIDBitmap))
141 elog(ERROR, "unrecognized result from subplan");
142
143 if (result == NULL)
144 result = subresult; /* first subplan */
145 else
146 {
149 }
150
151 /*
152 * If at any stage we have a completely empty bitmap, we can fall out
153 * without evaluating the remaining subplans, since ANDing them can no
154 * longer change the result. (Note: the fact that indxpath.c orders
155 * the subplans by selectivity should make this case more likely to
156 * occur.)
157 */
158 if (tbm_is_empty(result))
159 break;
160 }
161
162 if (result == NULL)
163 elog(ERROR, "BitmapAnd doesn't support zero inputs");
164
165 /* must provide our own instrumentation support */
166 if (node->ps.instrument)
167 InstrStopNode(node->ps.instrument, 0 /* XXX */ );
168
169 return (Node *) result;
170}
171
172/* ----------------------------------------------------------------
173 * ExecEndBitmapAnd
174 *
175 * Shuts down the subscans of the BitmapAnd node.
176 *
177 * Returns nothing of interest.
178 * ----------------------------------------------------------------
179 */
180void
182{
183 PlanState **bitmapplans;
184 int nplans;
185 int i;
186
187 /*
188 * get information from the node
189 */
190 bitmapplans = node->bitmapplans;
191 nplans = node->nplans;
192
193 /*
194 * shut down each of the subscans (that we've initialized)
195 */
196 for (i = 0; i < nplans; i++)
197 {
198 if (bitmapplans[i])
199 ExecEndNode(bitmapplans[i]);
200 }
201}
202
203void
205{
206 int i;
207
208 for (i = 0; i < node->nplans; i++)
209 {
210 PlanState *subnode = node->bitmapplans[i];
211
212 /*
213 * ExecReScan doesn't know about my subplans, so I have to do
214 * changed-parameter signaling myself.
215 */
216 if (node->ps.chgParam != NULL)
218
219 /*
220 * If chgParam of subnode is not null then plan will be re-scanned by
221 * first ExecProcNode.
222 */
223 if (subnode->chgParam == NULL)
225 }
226}
#define Assert(condition)
Definition c.h:943
uint32 result
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
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:936
#define EXEC_FLAG_BACKWARD
Definition executor.h:70
#define EXEC_FLAG_MARK
Definition executor.h:71
void InstrStartNode(NodeInstrumentation *instr)
Definition instrument.c:132
void InstrStopNode(NodeInstrumentation *instr, double nTuples)
Definition instrument.c:139
int i
Definition isn.c:77
void * palloc0(Size size)
Definition mcxt.c:1420
BitmapAndState * ExecInitBitmapAnd(BitmapAnd *node, EState *estate, int eflags)
Node * MultiExecBitmapAnd(BitmapAndState *node)
void ExecEndBitmapAnd(BitmapAndState *node)
static TupleTableSlot * ExecBitmapAnd(PlanState *pstate)
void ExecReScanBitmapAnd(BitmapAndState *node)
#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:1622
PlanState ** bitmapplans
Definition execnodes.h:1623
List * bitmapplans
Definition plannodes.h:513
Definition nodes.h:135
NodeInstrumentation * instrument
Definition execnodes.h:1211
Bitmapset * chgParam
Definition execnodes.h:1235
void tbm_free(TIDBitmap *tbm)
Definition tidbitmap.c:312
bool tbm_is_empty(const TIDBitmap *tbm)
Definition tidbitmap.c:657
void tbm_intersect(TIDBitmap *a, const TIDBitmap *b)
Definition tidbitmap.c:528