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/* INTERFACE ROUTINES
16 * ExecInitBitmapAnd - initialize the BitmapAnd node
17 * MultiExecBitmapAnd - retrieve the result bitmap from the node
18 * ExecEndBitmapAnd - shut down the BitmapAnd node
19 * ExecReScanBitmapAnd - rescan the BitmapAnd node
20 *
21 * NOTES
22 * BitmapAnd 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
36
37/* ----------------------------------------------------------------
38 * ExecBitmapAnd
39 *
40 * stub for pro forma compliance
41 * ----------------------------------------------------------------
42 */
43static TupleTableSlot *
45{
46 elog(ERROR, "BitmapAnd node does not support ExecProcNode call convention");
47 return NULL;
48}
49
50/* ----------------------------------------------------------------
51 * ExecInitBitmapAnd
52 *
53 * Begin all of the subscans of the BitmapAnd node.
54 * ----------------------------------------------------------------
55 */
57ExecInitBitmapAnd(BitmapAnd *node, EState *estate, int eflags)
58{
61 int nplans;
62 int i;
63 ListCell *l;
65
66 /* check for unsupported flags */
68
69 /*
70 * Set up empty vector of subplan states
71 */
72 nplans = list_length(node->bitmapplans);
73
74 bitmapplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
75
76 /*
77 * create new BitmapAndState for our BitmapAnd node
78 */
79 bitmapandstate->ps.plan = (Plan *) node;
80 bitmapandstate->ps.state = estate;
81 bitmapandstate->ps.ExecProcNode = ExecBitmapAnd;
82 bitmapandstate->bitmapplans = bitmapplanstates;
83 bitmapandstate->nplans = nplans;
84
85 /*
86 * call ExecInitNode on each of the plans to be executed and save the
87 * results into the array "bitmapplanstates".
88 */
89 i = 0;
90 foreach(l, node->bitmapplans)
91 {
92 initNode = (Plan *) lfirst(l);
93 bitmapplanstates[i] = ExecInitNode(initNode, estate, eflags);
94 i++;
95 }
96
97 /*
98 * Miscellaneous initialization
99 *
100 * BitmapAnd plans don't have expression contexts because they never call
101 * ExecQual or ExecProject. They don't need any tuple slots either.
102 */
103
104 return bitmapandstate;
105}
106
107/* ----------------------------------------------------------------
108 * MultiExecBitmapAnd
109 * ----------------------------------------------------------------
110 */
111Node *
113{
114 PlanState **bitmapplans;
115 int nplans;
116 int i;
117 TIDBitmap *result = NULL;
118
119 /* must provide our own instrumentation support */
120 if (node->ps.instrument)
122
123 /*
124 * get information from the node
125 */
126 bitmapplans = node->bitmapplans;
127 nplans = node->nplans;
128
129 /*
130 * Scan all the subplans and AND their result bitmaps
131 */
132 for (i = 0; i < nplans; i++)
133 {
134 PlanState *subnode = bitmapplans[i];
136
138
139 if (!subresult || !IsA(subresult, TIDBitmap))
140 elog(ERROR, "unrecognized result from subplan");
141
142 if (result == NULL)
143 result = subresult; /* first subplan */
144 else
145 {
146 tbm_intersect(result, subresult);
148 }
149
150 /*
151 * If at any stage we have a completely empty bitmap, we can fall out
152 * without evaluating the remaining subplans, since ANDing them can no
153 * longer change the result. (Note: the fact that indxpath.c orders
154 * the subplans by selectivity should make this case more likely to
155 * occur.)
156 */
157 if (tbm_is_empty(result))
158 break;
159 }
160
161 if (result == NULL)
162 elog(ERROR, "BitmapAnd doesn't support zero inputs");
163
164 /* must provide our own instrumentation support */
165 if (node->ps.instrument)
166 InstrStopNode(node->ps.instrument, 0 /* XXX */ );
167
168 return (Node *) result;
169}
170
171/* ----------------------------------------------------------------
172 * ExecEndBitmapAnd
173 *
174 * Shuts down the subscans of the BitmapAnd node.
175 *
176 * Returns nothing of interest.
177 * ----------------------------------------------------------------
178 */
179void
181{
182 PlanState **bitmapplans;
183 int nplans;
184 int i;
185
186 /*
187 * get information from the node
188 */
189 bitmapplans = node->bitmapplans;
190 nplans = node->nplans;
191
192 /*
193 * shut down each of the subscans (that we've initialized)
194 */
195 for (i = 0; i < nplans; i++)
196 {
197 if (bitmapplans[i])
198 ExecEndNode(bitmapplans[i]);
199 }
200}
201
202void
204{
205 int i;
206
207 for (i = 0; i < node->nplans; i++)
208 {
209 PlanState *subnode = node->bitmapplans[i];
210
211 /*
212 * ExecReScan doesn't know about my subplans, so I have to do
213 * changed-parameter signaling myself.
214 */
215 if (node->ps.chgParam != NULL)
217
218 /*
219 * If chgParam of subnode is not null then plan will be re-scanned by
220 * first ExecProcNode.
221 */
222 if (subnode->chgParam == NULL)
224 }
225}
#define Assert(condition)
Definition c.h:945
#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
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
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:1596
PlanState ** bitmapplans
Definition execnodes.h:1597
List * bitmapplans
Definition plannodes.h:509
Definition nodes.h:135
Instrumentation * instrument
Definition execnodes.h:1187
Bitmapset * chgParam
Definition execnodes.h:1209
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