PostgreSQL Source Code  git master
nodeMergeAppend.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeMergeAppend.c
4  * routines to handle MergeAppend nodes.
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  * IDENTIFICATION
11  * src/backend/executor/nodeMergeAppend.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /* INTERFACE ROUTINES
16  * ExecInitMergeAppend - initialize the MergeAppend node
17  * ExecMergeAppend - retrieve the next tuple from the node
18  * ExecEndMergeAppend - shut down the MergeAppend node
19  * ExecReScanMergeAppend - rescan the MergeAppend node
20  *
21  * NOTES
22  * A MergeAppend node contains a list of one or more subplans.
23  * These are each expected to deliver tuples that are sorted according
24  * to a common sort key. The MergeAppend node merges these streams
25  * to produce output sorted the same way.
26  *
27  * MergeAppend nodes don't make use of their left and right
28  * subtrees, rather they maintain a list of subplans so
29  * a typical MergeAppend node looks like this in the plan tree:
30  *
31  * ...
32  * /
33  * MergeAppend---+------+------+--- nil
34  * / \ | | |
35  * nil nil ... ... ...
36  * subplans
37  */
38 
39 #include "postgres.h"
40 
41 #include "executor/execdebug.h"
42 #include "executor/execPartition.h"
44 #include "lib/binaryheap.h"
45 #include "miscadmin.h"
46 
47 /*
48  * We have one slot for each item in the heap array. We use SlotNumber
49  * to store slot indexes. This doesn't actually provide any formal
50  * type-safety, but it makes the code more self-documenting.
51  */
52 typedef int32 SlotNumber;
53 
55 static int heap_compare_slots(Datum a, Datum b, void *arg);
56 
57 
58 /* ----------------------------------------------------------------
59  * ExecInitMergeAppend
60  *
61  * Begin all of the subscans of the MergeAppend node.
62  * ----------------------------------------------------------------
63  */
65 ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
66 {
68  PlanState **mergeplanstates;
69  Bitmapset *validsubplans;
70  int nplans;
71  int i,
72  j;
73 
74  /* check for unsupported flags */
75  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
76 
77  /*
78  * create new MergeAppendState for our node
79  */
80  mergestate->ps.plan = (Plan *) node;
81  mergestate->ps.state = estate;
82  mergestate->ps.ExecProcNode = ExecMergeAppend;
83 
84  /* If run-time partition pruning is enabled, then set that up now */
85  if (node->part_prune_index >= 0)
86  {
87  PartitionPruneState *prunestate;
88 
89  /*
90  * Set up pruning data structure. This also initializes the set of
91  * subplans to initialize (validsubplans) by taking into account the
92  * result of performing initial pruning if any.
93  */
94  prunestate = ExecInitPartitionPruning(&mergestate->ps,
95  list_length(node->mergeplans),
96  node->part_prune_index,
97  node->apprelids,
98  &validsubplans);
99  mergestate->ms_prune_state = prunestate;
100  nplans = bms_num_members(validsubplans);
101 
102  /*
103  * When no run-time pruning is required and there's at least one
104  * subplan, we can fill ms_valid_subplans immediately, preventing
105  * later calls to ExecFindMatchingSubPlans.
106  */
107  if (!prunestate->do_exec_prune && nplans > 0)
108  mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
109  }
110  else
111  {
112  nplans = list_length(node->mergeplans);
113 
114  /*
115  * When run-time partition pruning is not enabled we can just mark all
116  * subplans as valid; they must also all be initialized.
117  */
118  Assert(nplans > 0);
119  mergestate->ms_valid_subplans = validsubplans =
120  bms_add_range(NULL, 0, nplans - 1);
121  mergestate->ms_prune_state = NULL;
122  }
123 
124  mergeplanstates = (PlanState **) palloc(nplans * sizeof(PlanState *));
125  mergestate->mergeplans = mergeplanstates;
126  mergestate->ms_nplans = nplans;
127 
128  mergestate->ms_slots = (TupleTableSlot **) palloc0(sizeof(TupleTableSlot *) * nplans);
129  mergestate->ms_heap = binaryheap_allocate(nplans, heap_compare_slots,
130  mergestate);
131 
132  /*
133  * Miscellaneous initialization
134  *
135  * MergeAppend nodes do have Result slots, which hold pointers to tuples,
136  * so we have to initialize them. FIXME
137  */
139 
140  /* node returns slots from each of its subnodes, therefore not fixed */
141  mergestate->ps.resultopsset = true;
142  mergestate->ps.resultopsfixed = false;
143 
144  /*
145  * call ExecInitNode on each of the valid plans to be executed and save
146  * the results into the mergeplanstates array.
147  */
148  j = 0;
149  i = -1;
150  while ((i = bms_next_member(validsubplans, i)) >= 0)
151  {
152  Plan *initNode = (Plan *) list_nth(node->mergeplans, i);
153 
154  mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
155  }
156 
157  mergestate->ps.ps_ProjInfo = NULL;
158 
159  /*
160  * initialize sort-key information
161  */
162  mergestate->ms_nkeys = node->numCols;
163  mergestate->ms_sortkeys = palloc0(sizeof(SortSupportData) * node->numCols);
164 
165  for (i = 0; i < node->numCols; i++)
166  {
167  SortSupport sortKey = mergestate->ms_sortkeys + i;
168 
169  sortKey->ssup_cxt = CurrentMemoryContext;
170  sortKey->ssup_collation = node->collations[i];
171  sortKey->ssup_nulls_first = node->nullsFirst[i];
172  sortKey->ssup_attno = node->sortColIdx[i];
173 
174  /*
175  * It isn't feasible to perform abbreviated key conversion, since
176  * tuples are pulled into mergestate's binary heap as needed. It
177  * would likely be counter-productive to convert tuples into an
178  * abbreviated representation as they're pulled up, so opt out of that
179  * additional optimization entirely.
180  */
181  sortKey->abbreviate = false;
182 
183  PrepareSortSupportFromOrderingOp(node->sortOperators[i], sortKey);
184  }
185 
186  /*
187  * initialize to show we have not run the subplans yet
188  */
189  mergestate->ms_initialized = false;
190 
191  return mergestate;
192 }
193 
194 /* ----------------------------------------------------------------
195  * ExecMergeAppend
196  *
197  * Handles iteration over multiple subplans.
198  * ----------------------------------------------------------------
199  */
200 static TupleTableSlot *
202 {
203  MergeAppendState *node = castNode(MergeAppendState, pstate);
204  TupleTableSlot *result;
205  SlotNumber i;
206 
208 
209  if (!node->ms_initialized)
210  {
211  /* Nothing to do if all subplans were pruned */
212  if (node->ms_nplans == 0)
213  return ExecClearTuple(node->ps.ps_ResultTupleSlot);
214 
215  /*
216  * If we've yet to determine the valid subplans then do so now. If
217  * run-time pruning is disabled then the valid subplans will always be
218  * set to all subplans.
219  */
220  if (node->ms_valid_subplans == NULL)
221  node->ms_valid_subplans =
223 
224  /*
225  * First time through: pull the first tuple from each valid subplan,
226  * and set up the heap.
227  */
228  i = -1;
229  while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0)
230  {
231  node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
232  if (!TupIsNull(node->ms_slots[i]))
234  }
235  binaryheap_build(node->ms_heap);
236  node->ms_initialized = true;
237  }
238  else
239  {
240  /*
241  * Otherwise, pull the next tuple from whichever subplan we returned
242  * from last time, and reinsert the subplan index into the heap,
243  * because it might now compare differently against the existing
244  * elements of the heap. (We could perhaps simplify the logic a bit
245  * by doing this before returning from the prior call, but it's better
246  * to not pull tuples until necessary.)
247  */
249  node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
250  if (!TupIsNull(node->ms_slots[i]))
252  else
253  (void) binaryheap_remove_first(node->ms_heap);
254  }
255 
256  if (binaryheap_empty(node->ms_heap))
257  {
258  /* All the subplans are exhausted, and so is the heap */
259  result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
260  }
261  else
262  {
264  result = node->ms_slots[i];
265  }
266 
267  return result;
268 }
269 
270 /*
271  * Compare the tuples in the two given slots.
272  */
273 static int32
275 {
277  SlotNumber slot1 = DatumGetInt32(a);
278  SlotNumber slot2 = DatumGetInt32(b);
279 
280  TupleTableSlot *s1 = node->ms_slots[slot1];
281  TupleTableSlot *s2 = node->ms_slots[slot2];
282  int nkey;
283 
284  Assert(!TupIsNull(s1));
285  Assert(!TupIsNull(s2));
286 
287  for (nkey = 0; nkey < node->ms_nkeys; nkey++)
288  {
289  SortSupport sortKey = node->ms_sortkeys + nkey;
290  AttrNumber attno = sortKey->ssup_attno;
291  Datum datum1,
292  datum2;
293  bool isNull1,
294  isNull2;
295  int compare;
296 
297  datum1 = slot_getattr(s1, attno, &isNull1);
298  datum2 = slot_getattr(s2, attno, &isNull2);
299 
300  compare = ApplySortComparator(datum1, isNull1,
301  datum2, isNull2,
302  sortKey);
303  if (compare != 0)
304  {
306  return compare;
307  }
308  }
309  return 0;
310 }
311 
312 /* ----------------------------------------------------------------
313  * ExecEndMergeAppend
314  *
315  * Shuts down the subscans of the MergeAppend node.
316  *
317  * Returns nothing of interest.
318  * ----------------------------------------------------------------
319  */
320 void
322 {
323  PlanState **mergeplans;
324  int nplans;
325  int i;
326 
327  /*
328  * get information from the node
329  */
330  mergeplans = node->mergeplans;
331  nplans = node->ms_nplans;
332 
333  /*
334  * shut down each of the subscans
335  */
336  for (i = 0; i < nplans; i++)
337  ExecEndNode(mergeplans[i]);
338 }
339 
340 void
342 {
343  int i;
344 
345  /*
346  * If any PARAM_EXEC Params used in pruning expressions have changed, then
347  * we'd better unset the valid subplans so that they are reselected for
348  * the new parameter values.
349  */
350  if (node->ms_prune_state &&
351  bms_overlap(node->ps.chgParam,
353  {
355  node->ms_valid_subplans = NULL;
356  }
357 
358  for (i = 0; i < node->ms_nplans; i++)
359  {
360  PlanState *subnode = node->mergeplans[i];
361 
362  /*
363  * ExecReScan doesn't know about my subplans, so I have to do
364  * changed-parameter signaling myself.
365  */
366  if (node->ps.chgParam != NULL)
367  UpdateChangedParamSet(subnode, node->ps.chgParam);
368 
369  /*
370  * If chgParam of subnode is not null then plan will be re-scanned by
371  * first ExecProcNode.
372  */
373  if (subnode->chgParam == NULL)
374  ExecReScan(subnode);
375  }
376  binaryheap_reset(node->ms_heap);
377  node->ms_initialized = false;
378 }
int16 AttrNumber
Definition: attnum.h:21
void binaryheap_build(binaryheap *heap)
Definition: binaryheap.c:125
void binaryheap_add_unordered(binaryheap *heap, Datum d)
Definition: binaryheap.c:109
void binaryheap_reset(binaryheap *heap)
Definition: binaryheap.c:56
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:32
Datum binaryheap_remove_first(binaryheap *heap)
Definition: binaryheap.c:173
void binaryheap_replace_first(binaryheap *heap, Datum d)
Definition: binaryheap.c:207
Datum binaryheap_first(binaryheap *heap)
Definition: binaryheap.c:158
#define binaryheap_empty(h)
Definition: binaryheap.h:52
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
void bms_free(Bitmapset *a)
Definition: bitmapset.c:209
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:649
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:837
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1114
signed int int32
Definition: c.h:478
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
Bitmapset * ExecFindMatchingSubPlans(PartitionPruneState *prunestate, bool initial_prune)
PartitionPruneState * ExecInitPartitionPruning(PlanState *planstate, int n_total_subplans, int part_prune_index, Bitmapset *root_parent_relids, Bitmapset **initially_valid_subplans)
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1799
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:871
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define EXEC_FLAG_MARK
Definition: executor.h:59
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:257
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1241
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void * palloc(Size size)
Definition: mcxt.c:1210
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
int32 SlotNumber
static int heap_compare_slots(Datum a, Datum b, void *arg)
MergeAppendState * ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
static TupleTableSlot * ExecMergeAppend(PlanState *pstate)
void ExecReScanMergeAppend(MergeAppendState *node)
int32 SlotNumber
void ExecEndMergeAppend(MergeAppendState *node)
#define makeNode(_type_)
Definition: nodes.h:176
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
void * arg
static int list_length(const List *l)
Definition: pg_list.h:152
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
uintptr_t Datum
Definition: postgres.h:64
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
char * s1
char * s2
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:135
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200
PlanState ** mergeplans
Definition: execnodes.h:1376
SortSupport ms_sortkeys
Definition: execnodes.h:1379
Bitmapset * ms_valid_subplans
Definition: execnodes.h:1384
PlanState ps
Definition: execnodes.h:1375
struct binaryheap * ms_heap
Definition: execnodes.h:1381
TupleTableSlot ** ms_slots
Definition: execnodes.h:1380
struct PartitionPruneState * ms_prune_state
Definition: execnodes.h:1383
int part_prune_index
Definition: plannodes.h:315
Bitmapset * apprelids
Definition: plannodes.h:293
List * mergeplans
Definition: plannodes.h:295
Bitmapset * execparamids
bool resultopsset
Definition: execnodes.h:1116
Plan * plan
Definition: execnodes.h:1031
EState * state
Definition: execnodes.h:1033
Bitmapset * chgParam
Definition: execnodes.h:1063
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1069
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1071
bool resultopsfixed
Definition: execnodes.h:1112
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1037
AttrNumber ssup_attno
Definition: sortsupport.h:81
bool ssup_nulls_first
Definition: sortsupport.h:75
MemoryContext ssup_cxt
Definition: sortsupport.h:66
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:389
#define TupIsNull(slot)
Definition: tuptable.h:300