PostgreSQL Source Code  git master
nodeMergeAppend.c File Reference
#include "postgres.h"
#include "executor/execdebug.h"
#include "executor/execPartition.h"
#include "executor/nodeMergeAppend.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
Include dependency graph for nodeMergeAppend.c:

Go to the source code of this file.

Typedefs

typedef int32 SlotNumber
 

Functions

static TupleTableSlotExecMergeAppend (PlanState *pstate)
 
static int heap_compare_slots (Datum a, Datum b, void *arg)
 
MergeAppendStateExecInitMergeAppend (MergeAppend *node, EState *estate, int eflags)
 
void ExecEndMergeAppend (MergeAppendState *node)
 
void ExecReScanMergeAppend (MergeAppendState *node)
 

Typedef Documentation

◆ SlotNumber

typedef int32 SlotNumber

Definition at line 52 of file nodeMergeAppend.c.

Function Documentation

◆ ExecEndMergeAppend()

void ExecEndMergeAppend ( MergeAppendState node)

Definition at line 332 of file nodeMergeAppend.c.

References ExecEndNode(), i, MergeAppendState::mergeplans, and MergeAppendState::ms_nplans.

Referenced by ExecEndNode().

333 {
334  PlanState **mergeplans;
335  int nplans;
336  int i;
337 
338  /*
339  * get information from the node
340  */
341  mergeplans = node->mergeplans;
342  nplans = node->ms_nplans;
343 
344  /*
345  * shut down each of the subscans
346  */
347  for (i = 0; i < nplans; i++)
348  ExecEndNode(mergeplans[i]);
349 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:537
PlanState ** mergeplans
Definition: execnodes.h:1246
int i

◆ ExecInitMergeAppend()

MergeAppendState* ExecInitMergeAppend ( MergeAppend node,
EState estate,
int  eflags 
)

Definition at line 65 of file nodeMergeAppend.c.

References SortSupportData::abbreviate, Assert, binaryheap_allocate(), bms_add_range(), bms_next_member(), bms_num_members(), MergeAppend::collations, CurrentMemoryContext, PartitionPruneState::do_exec_prune, PartitionPruneState::do_initial_prune, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecCreatePartitionPruneState(), ExecFindInitialMatchingSubPlans(), ExecInitNode(), ExecInitResultTupleSlotTL(), ExecMergeAppend(), PlanState::ExecProcNode, heap_compare_slots(), i, list_length(), list_nth(), makeNode, MergeAppend::mergeplans, MergeAppendState::mergeplans, MergeAppendState::ms_heap, MergeAppendState::ms_initialized, MergeAppendState::ms_nkeys, MergeAppendState::ms_nplans, MergeAppendState::ms_prune_state, MergeAppendState::ms_slots, MergeAppendState::ms_sortkeys, MergeAppendState::ms_valid_subplans, MergeAppend::nullsFirst, MergeAppend::numCols, palloc(), palloc0(), MergeAppend::part_prune_info, PlanState::plan, PrepareSortSupportFromOrderingOp(), MergeAppendState::ps, PlanState::ps_ProjInfo, PlanState::resultopsfixed, PlanState::resultopsset, MergeAppend::sortColIdx, MergeAppend::sortOperators, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, PlanState::state, and TTSOpsVirtual.

Referenced by ExecInitNode().

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_info != NULL)
86  {
87  PartitionPruneState *prunestate;
88 
89  /* We may need an expression context to evaluate partition exprs */
90  ExecAssignExprContext(estate, &mergestate->ps);
91 
92  prunestate = ExecCreatePartitionPruneState(&mergestate->ps,
93  node->part_prune_info);
94  mergestate->ms_prune_state = prunestate;
95 
96  /* Perform an initial partition prune, if required. */
97  if (prunestate->do_initial_prune)
98  {
99  /* Determine which subplans survive initial pruning */
100  validsubplans = ExecFindInitialMatchingSubPlans(prunestate,
101  list_length(node->mergeplans));
102 
103  nplans = bms_num_members(validsubplans);
104  }
105  else
106  {
107  /* We'll need to initialize all subplans */
108  nplans = list_length(node->mergeplans);
109  Assert(nplans > 0);
110  validsubplans = bms_add_range(NULL, 0, nplans - 1);
111  }
112 
113  /*
114  * When no run-time pruning is required and there's at least one
115  * subplan, we can fill as_valid_subplans immediately, preventing
116  * later calls to ExecFindMatchingSubPlans.
117  */
118  if (!prunestate->do_exec_prune && nplans > 0)
119  mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
120  }
121  else
122  {
123  nplans = list_length(node->mergeplans);
124 
125  /*
126  * When run-time partition pruning is not enabled we can just mark all
127  * subplans as valid; they must also all be initialized.
128  */
129  Assert(nplans > 0);
130  mergestate->ms_valid_subplans = validsubplans =
131  bms_add_range(NULL, 0, nplans - 1);
132  mergestate->ms_prune_state = NULL;
133  }
134 
135  mergeplanstates = (PlanState **) palloc(nplans * sizeof(PlanState *));
136  mergestate->mergeplans = mergeplanstates;
137  mergestate->ms_nplans = nplans;
138 
139  mergestate->ms_slots = (TupleTableSlot **) palloc0(sizeof(TupleTableSlot *) * nplans);
140  mergestate->ms_heap = binaryheap_allocate(nplans, heap_compare_slots,
141  mergestate);
142 
143  /*
144  * Miscellaneous initialization
145  *
146  * MergeAppend nodes do have Result slots, which hold pointers to tuples,
147  * so we have to initialize them. FIXME
148  */
150 
151  /* node returns slots from each of its subnodes, therefore not fixed */
152  mergestate->ps.resultopsset = true;
153  mergestate->ps.resultopsfixed = false;
154 
155  /*
156  * call ExecInitNode on each of the valid plans to be executed and save
157  * the results into the mergeplanstates array.
158  */
159  j = 0;
160  i = -1;
161  while ((i = bms_next_member(validsubplans, i)) >= 0)
162  {
163  Plan *initNode = (Plan *) list_nth(node->mergeplans, i);
164 
165  mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
166  }
167 
168  mergestate->ps.ps_ProjInfo = NULL;
169 
170  /*
171  * initialize sort-key information
172  */
173  mergestate->ms_nkeys = node->numCols;
174  mergestate->ms_sortkeys = palloc0(sizeof(SortSupportData) * node->numCols);
175 
176  for (i = 0; i < node->numCols; i++)
177  {
178  SortSupport sortKey = mergestate->ms_sortkeys + i;
179 
180  sortKey->ssup_cxt = CurrentMemoryContext;
181  sortKey->ssup_collation = node->collations[i];
182  sortKey->ssup_nulls_first = node->nullsFirst[i];
183  sortKey->ssup_attno = node->sortColIdx[i];
184 
185  /*
186  * It isn't feasible to perform abbreviated key conversion, since
187  * tuples are pulled into mergestate's binary heap as needed. It
188  * would likely be counter-productive to convert tuples into an
189  * abbreviated representation as they're pulled up, so opt out of that
190  * additional optimization entirely.
191  */
192  sortKey->abbreviate = false;
193 
195  }
196 
197  /*
198  * initialize to show we have not run the subplans yet
199  */
200  mergestate->ms_initialized = false;
201 
202  return mergestate;
203 }
bool ssup_nulls_first
Definition: sortsupport.h:75
TupleTableSlot ** ms_slots
Definition: execnodes.h:1250
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:979
SortSupport ms_sortkeys
Definition: execnodes.h:1249
Oid * collations
Definition: plannodes.h:281
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:134
Bitmapset * ms_valid_subplans
Definition: execnodes.h:1254
struct PartitionPruneInfo * part_prune_info
Definition: plannodes.h:284
EState * state
Definition: execnodes.h:941
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:834
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
MemoryContext ssup_cxt
Definition: sortsupport.h:66
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
Bitmapset * ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubplans)
PlanState ** mergeplans
Definition: execnodes.h:1246
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool resultopsset
Definition: execnodes.h:1022
void * palloc0(Size size)
Definition: mcxt.c:980
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:945
AttrNumber ssup_attno
Definition: sortsupport.h:81
Plan * plan
Definition: execnodes.h:939
PlanState ps
Definition: execnodes.h:1245
struct binaryheap * ms_heap
Definition: execnodes.h:1251
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
#define EXEC_FLAG_MARK
Definition: executor.h:59
AttrNumber * sortColIdx
Definition: plannodes.h:279
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:444
static int list_length(const List *l)
Definition: pg_list.h:169
struct PartitionPruneState * ms_prune_state
Definition: execnodes.h:1253
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1769
bool * nullsFirst
Definition: plannodes.h:282
static int heap_compare_slots(Datum a, Datum b, void *arg)
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:33
bool resultopsfixed
Definition: execnodes.h:1018
List * mergeplans
Definition: plannodes.h:276
void * palloc(Size size)
Definition: mcxt.c:949
int i
Oid * sortOperators
Definition: plannodes.h:280
static TupleTableSlot * ExecMergeAppend(PlanState *pstate)
PartitionPruneState * ExecCreatePartitionPruneState(PlanState *planstate, PartitionPruneInfo *partitionpruneinfo)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:138

◆ ExecMergeAppend()

static TupleTableSlot * ExecMergeAppend ( PlanState pstate)
static

Definition at line 212 of file nodeMergeAppend.c.

References binaryheap_add_unordered(), binaryheap_build(), binaryheap_empty, binaryheap_first(), binaryheap_remove_first(), binaryheap_replace_first(), bms_next_member(), castNode, CHECK_FOR_INTERRUPTS, DatumGetInt32, ExecClearTuple(), ExecFindMatchingSubPlans(), ExecProcNode(), i, Int32GetDatum, MergeAppendState::mergeplans, MergeAppendState::ms_heap, MergeAppendState::ms_initialized, MergeAppendState::ms_nplans, MergeAppendState::ms_prune_state, MergeAppendState::ms_slots, MergeAppendState::ms_valid_subplans, MergeAppendState::ps, PlanState::ps_ResultTupleSlot, and TupIsNull.

Referenced by ExecInitMergeAppend().

213 {
214  MergeAppendState *node = castNode(MergeAppendState, pstate);
215  TupleTableSlot *result;
216  SlotNumber i;
217 
219 
220  if (!node->ms_initialized)
221  {
222  /* Nothing to do if all subplans were pruned */
223  if (node->ms_nplans == 0)
224  return ExecClearTuple(node->ps.ps_ResultTupleSlot);
225 
226  /*
227  * If we've yet to determine the valid subplans then do so now. If
228  * run-time pruning is disabled then the valid subplans will always be
229  * set to all subplans.
230  */
231  if (node->ms_valid_subplans == NULL)
232  node->ms_valid_subplans =
234 
235  /*
236  * First time through: pull the first tuple from each valid subplan,
237  * and set up the heap.
238  */
239  i = -1;
240  while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0)
241  {
242  node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
243  if (!TupIsNull(node->ms_slots[i]))
245  }
246  binaryheap_build(node->ms_heap);
247  node->ms_initialized = true;
248  }
249  else
250  {
251  /*
252  * Otherwise, pull the next tuple from whichever subplan we returned
253  * from last time, and reinsert the subplan index into the heap,
254  * because it might now compare differently against the existing
255  * elements of the heap. (We could perhaps simplify the logic a bit
256  * by doing this before returning from the prior call, but it's better
257  * to not pull tuples until necessary.)
258  */
260  node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
261  if (!TupIsNull(node->ms_slots[i]))
263  else
264  (void) binaryheap_remove_first(node->ms_heap);
265  }
266 
267  if (binaryheap_empty(node->ms_heap))
268  {
269  /* All the subplans are exhausted, and so is the heap */
270  result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
271  }
272  else
273  {
275  result = node->ms_slots[i];
276  }
277 
278  return result;
279 }
TupleTableSlot ** ms_slots
Definition: execnodes.h:1250
#define binaryheap_empty(h)
Definition: binaryheap.h:52
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define DatumGetInt32(X)
Definition: postgres.h:472
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * ms_valid_subplans
Definition: execnodes.h:1254
void binaryheap_replace_first(binaryheap *heap, Datum d)
Definition: binaryheap.c:204
void binaryheap_add_unordered(binaryheap *heap, Datum d)
Definition: binaryheap.c:110
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
Datum binaryheap_first(binaryheap *heap)
Definition: binaryheap.c:159
PlanState ** mergeplans
Definition: execnodes.h:1246
int32 SlotNumber
#define TupIsNull(slot)
Definition: tuptable.h:292
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:235
PlanState ps
Definition: execnodes.h:1245
void binaryheap_build(binaryheap *heap)
Definition: binaryheap.c:126
struct binaryheap * ms_heap
Definition: execnodes.h:1251
struct PartitionPruneState * ms_prune_state
Definition: execnodes.h:1253
#define Int32GetDatum(X)
Definition: postgres.h:479
int i
Datum binaryheap_remove_first(binaryheap *heap)
Definition: binaryheap.c:174
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Bitmapset * ExecFindMatchingSubPlans(PartitionPruneState *prunestate)

◆ ExecReScanMergeAppend()

void ExecReScanMergeAppend ( MergeAppendState node)

Definition at line 352 of file nodeMergeAppend.c.

References binaryheap_reset(), bms_free(), bms_overlap(), PlanState::chgParam, PartitionPruneState::execparamids, ExecReScan(), i, MergeAppendState::mergeplans, MergeAppendState::ms_heap, MergeAppendState::ms_initialized, MergeAppendState::ms_nplans, MergeAppendState::ms_prune_state, MergeAppendState::ms_valid_subplans, MergeAppendState::ps, and UpdateChangedParamSet().

Referenced by ExecReScan().

353 {
354  int i;
355 
356  /*
357  * If any PARAM_EXEC Params used in pruning expressions have changed, then
358  * we'd better unset the valid subplans so that they are reselected for
359  * the new parameter values.
360  */
361  if (node->ms_prune_state &&
362  bms_overlap(node->ps.chgParam,
364  {
366  node->ms_valid_subplans = NULL;
367  }
368 
369  for (i = 0; i < node->ms_nplans; i++)
370  {
371  PlanState *subnode = node->mergeplans[i];
372 
373  /*
374  * ExecReScan doesn't know about my subplans, so I have to do
375  * changed-parameter signaling myself.
376  */
377  if (node->ps.chgParam != NULL)
378  UpdateChangedParamSet(subnode, node->ps.chgParam);
379 
380  /*
381  * If chgParam of subnode is not null then plan will be re-scanned by
382  * first ExecProcNode.
383  */
384  if (subnode->chgParam == NULL)
385  ExecReScan(subnode);
386  }
387  binaryheap_reset(node->ms_heap);
388  node->ms_initialized = false;
389 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:75
Bitmapset * ms_valid_subplans
Definition: execnodes.h:1254
PlanState ** mergeplans
Definition: execnodes.h:1246
Bitmapset * execparamids
void binaryheap_reset(binaryheap *heap)
Definition: binaryheap.c:57
Bitmapset * chgParam
Definition: execnodes.h:971
PlanState ps
Definition: execnodes.h:1245
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:802
struct binaryheap * ms_heap
Definition: execnodes.h:1251
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
struct PartitionPruneState * ms_prune_state
Definition: execnodes.h:1253
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i

◆ heap_compare_slots()

static int32 heap_compare_slots ( Datum  a,
Datum  b,
void *  arg 
)
static

Definition at line 285 of file nodeMergeAppend.c.

References ApplySortComparator(), Assert, compare(), DatumGetInt32, INVERT_COMPARE_RESULT, MergeAppendState::ms_nkeys, MergeAppendState::ms_slots, MergeAppendState::ms_sortkeys, s1, s2, slot_getattr(), SortSupportData::ssup_attno, and TupIsNull.

Referenced by ExecInitMergeAppend().

286 {
288  SlotNumber slot1 = DatumGetInt32(a);
289  SlotNumber slot2 = DatumGetInt32(b);
290 
291  TupleTableSlot *s1 = node->ms_slots[slot1];
292  TupleTableSlot *s2 = node->ms_slots[slot2];
293  int nkey;
294 
295  Assert(!TupIsNull(s1));
296  Assert(!TupIsNull(s2));
297 
298  for (nkey = 0; nkey < node->ms_nkeys; nkey++)
299  {
300  SortSupport sortKey = node->ms_sortkeys + nkey;
301  AttrNumber attno = sortKey->ssup_attno;
302  Datum datum1,
303  datum2;
304  bool isNull1,
305  isNull2;
306  int compare;
307 
308  datum1 = slot_getattr(s1, attno, &isNull1);
309  datum2 = slot_getattr(s2, attno, &isNull2);
310 
311  compare = ApplySortComparator(datum1, isNull1,
312  datum2, isNull2,
313  sortKey);
314  if (compare != 0)
315  {
316  INVERT_COMPARE_RESULT(compare);
317  return compare;
318  }
319  }
320  return 0;
321 }
TupleTableSlot ** ms_slots
Definition: execnodes.h:1250
SortSupport ms_sortkeys
Definition: execnodes.h:1249
#define DatumGetInt32(X)
Definition: postgres.h:472
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
char * s1
int32 SlotNumber
#define TupIsNull(slot)
Definition: tuptable.h:292
char * s2
uintptr_t Datum
Definition: postgres.h:367
AttrNumber ssup_attno
Definition: sortsupport.h:81
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define Assert(condition)
Definition: c.h:739
void * arg
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200
int16 AttrNumber
Definition: attnum.h:21
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1075