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

Go to the source code of this file.

Data Structures

struct  ParallelAppendState
 

Macros

#define INVALID_SUBPLAN_INDEX   -1
 
#define NO_MATCHING_SUBPLANS   -2
 

Functions

static TupleTableSlotExecAppend (PlanState *pstate)
 
static bool choose_next_subplan_locally (AppendState *node)
 
static bool choose_next_subplan_for_leader (AppendState *node)
 
static bool choose_next_subplan_for_worker (AppendState *node)
 
static void mark_invalid_subplans_as_finished (AppendState *node)
 
AppendStateExecInitAppend (Append *node, EState *estate, int eflags)
 
void ExecEndAppend (AppendState *node)
 
void ExecReScanAppend (AppendState *node)
 
void ExecAppendEstimate (AppendState *node, ParallelContext *pcxt)
 
void ExecAppendInitializeDSM (AppendState *node, ParallelContext *pcxt)
 
void ExecAppendReInitializeDSM (AppendState *node, ParallelContext *pcxt)
 
void ExecAppendInitializeWorker (AppendState *node, ParallelWorkerContext *pwcxt)
 

Macro Definition Documentation

◆ INVALID_SUBPLAN_INDEX

◆ NO_MATCHING_SUBPLANS

#define NO_MATCHING_SUBPLANS   -2

Function Documentation

◆ choose_next_subplan_for_leader()

static bool choose_next_subplan_for_leader ( AppendState node)
static

Definition at line 511 of file nodeAppend.c.

References AppendState::as_first_partial_plan, AppendState::as_nplans, AppendState::as_prune_state, AppendState::as_pstate, AppendState::as_valid_subplans, AppendState::as_whichplan, Assert, EState::es_direction, ExecFindMatchingSubPlans(), INVALID_SUBPLAN_INDEX, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), mark_invalid_subplans_as_finished(), NO_MATCHING_SUBPLANS, ParallelAppendState::pa_finished, ParallelAppendState::pa_lock, ParallelAppendState::pa_next_plan, AppendState::ps, ScanDirectionIsForward, and PlanState::state.

Referenced by ExecAppendInitializeDSM().

512 {
513  ParallelAppendState *pstate = node->as_pstate;
514 
515  /* Backward scan is not supported by parallel-aware plans */
517 
518  /* We should never be called when there are no subplans */
520 
522 
523  if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
524  {
525  /* Mark just-completed subplan as finished. */
526  node->as_pstate->pa_finished[node->as_whichplan] = true;
527  }
528  else
529  {
530  /* Start with last subplan. */
531  node->as_whichplan = node->as_nplans - 1;
532 
533  /*
534  * If we've yet to determine the valid subplans for these parameters
535  * then do so now. If run-time pruning is disabled then the valid
536  * subplans will always be set to all subplans.
537  */
538  if (node->as_valid_subplans == NULL)
539  {
540  node->as_valid_subplans =
542 
543  /*
544  * Mark each invalid plan as finished to allow the loop below to
545  * select the first valid subplan.
546  */
548  }
549  }
550 
551  /* Loop until we find a subplan to execute. */
552  while (pstate->pa_finished[node->as_whichplan])
553  {
554  if (node->as_whichplan == 0)
555  {
558  LWLockRelease(&pstate->pa_lock);
559  return false;
560  }
561 
562  /*
563  * We needn't pay attention to as_valid_subplans here as all invalid
564  * plans have been marked as finished.
565  */
566  node->as_whichplan--;
567  }
568 
569  /* If non-partial, immediately mark as finished. */
570  if (node->as_whichplan < node->as_first_partial_plan)
571  node->as_pstate->pa_finished[node->as_whichplan] = true;
572 
573  LWLockRelease(&pstate->pa_lock);
574 
575  return true;
576 }
#define NO_MATCHING_SUBPLANS
Definition: nodeAppend.c:81
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
EState * state
Definition: execnodes.h:913
ScanDirection es_direction
Definition: execnodes.h:477
PlanState ps
Definition: execnodes.h:1086
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
int as_first_partial_plan
Definition: execnodes.h:1090
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
bool pa_finished[FLEXIBLE_ARRAY_MEMBER]
Definition: nodeAppend.c:77
static void mark_invalid_subplans_as_finished(AppendState *node)
Definition: nodeAppend.c:716
#define Assert(condition)
Definition: c.h:699
int as_whichplan
Definition: execnodes.h:1089
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
Bitmapset * ExecFindMatchingSubPlans(PartitionPruneState *prunestate)

◆ choose_next_subplan_for_worker()

static bool choose_next_subplan_for_worker ( AppendState node)
static

Definition at line 592 of file nodeAppend.c.

References AppendState::as_first_partial_plan, AppendState::as_prune_state, AppendState::as_pstate, AppendState::as_valid_subplans, AppendState::as_whichplan, Assert, bms_next_member(), EState::es_direction, ExecFindMatchingSubPlans(), INVALID_SUBPLAN_INDEX, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), mark_invalid_subplans_as_finished(), NO_MATCHING_SUBPLANS, ParallelAppendState::pa_finished, ParallelAppendState::pa_lock, ParallelAppendState::pa_next_plan, AppendState::ps, ScanDirectionIsForward, and PlanState::state.

Referenced by ExecAppendInitializeWorker().

593 {
594  ParallelAppendState *pstate = node->as_pstate;
595 
596  /* Backward scan is not supported by parallel-aware plans */
598 
599  /* We should never be called when there are no subplans */
601 
603 
604  /* Mark just-completed subplan as finished. */
605  if (node->as_whichplan != INVALID_SUBPLAN_INDEX)
606  node->as_pstate->pa_finished[node->as_whichplan] = true;
607 
608  /*
609  * If we've yet to determine the valid subplans for these parameters then
610  * do so now. If run-time pruning is disabled then the valid subplans
611  * will always be set to all subplans.
612  */
613  else if (node->as_valid_subplans == NULL)
614  {
615  node->as_valid_subplans =
618  }
619 
620  /* If all the plans are already done, we have nothing to do */
621  if (pstate->pa_next_plan == INVALID_SUBPLAN_INDEX)
622  {
623  LWLockRelease(&pstate->pa_lock);
624  return false;
625  }
626 
627  /* Save the plan from which we are starting the search. */
628  node->as_whichplan = pstate->pa_next_plan;
629 
630  /* Loop until we find a valid subplan to execute. */
631  while (pstate->pa_finished[pstate->pa_next_plan])
632  {
633  int nextplan;
634 
635  nextplan = bms_next_member(node->as_valid_subplans,
636  pstate->pa_next_plan);
637  if (nextplan >= 0)
638  {
639  /* Advance to the next valid plan. */
640  pstate->pa_next_plan = nextplan;
641  }
642  else if (node->as_whichplan > node->as_first_partial_plan)
643  {
644  /*
645  * Try looping back to the first valid partial plan, if there is
646  * one. If there isn't, arrange to bail out below.
647  */
648  nextplan = bms_next_member(node->as_valid_subplans,
649  node->as_first_partial_plan - 1);
650  pstate->pa_next_plan =
651  nextplan < 0 ? node->as_whichplan : nextplan;
652  }
653  else
654  {
655  /*
656  * At last plan, and either there are no partial plans or we've
657  * tried them all. Arrange to bail out.
658  */
659  pstate->pa_next_plan = node->as_whichplan;
660  }
661 
662  if (pstate->pa_next_plan == node->as_whichplan)
663  {
664  /* We've tried everything! */
666  LWLockRelease(&pstate->pa_lock);
667  return false;
668  }
669  }
670 
671  /* Pick the plan we found, and advance pa_next_plan one more time. */
672  node->as_whichplan = pstate->pa_next_plan;
674  pstate->pa_next_plan);
675 
676  /*
677  * If there are no more valid plans then try setting the next plan to the
678  * first valid partial plan.
679  */
680  if (pstate->pa_next_plan < 0)
681  {
682  int nextplan = bms_next_member(node->as_valid_subplans,
683  node->as_first_partial_plan - 1);
684 
685  if (nextplan >= 0)
686  pstate->pa_next_plan = nextplan;
687  else
688  {
689  /*
690  * There are no valid partial plans, and we already chose the last
691  * non-partial plan; so flag that there's nothing more for our
692  * fellow workers to do.
693  */
695  }
696  }
697 
698  /* If non-partial, immediately mark as finished. */
699  if (node->as_whichplan < node->as_first_partial_plan)
700  node->as_pstate->pa_finished[node->as_whichplan] = true;
701 
702  LWLockRelease(&pstate->pa_lock);
703 
704  return true;
705 }
#define NO_MATCHING_SUBPLANS
Definition: nodeAppend.c:81
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1075
EState * state
Definition: execnodes.h:913
ScanDirection es_direction
Definition: execnodes.h:477
PlanState ps
Definition: execnodes.h:1086
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
int as_first_partial_plan
Definition: execnodes.h:1090
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
bool pa_finished[FLEXIBLE_ARRAY_MEMBER]
Definition: nodeAppend.c:77
static void mark_invalid_subplans_as_finished(AppendState *node)
Definition: nodeAppend.c:716
#define Assert(condition)
Definition: c.h:699
int as_whichplan
Definition: execnodes.h:1089
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
Bitmapset * ExecFindMatchingSubPlans(PartitionPruneState *prunestate)

◆ choose_next_subplan_locally()

static bool choose_next_subplan_locally ( AppendState node)
static

Definition at line 462 of file nodeAppend.c.

References AppendState::as_prune_state, AppendState::as_valid_subplans, AppendState::as_whichplan, Assert, bms_next_member(), bms_prev_member(), EState::es_direction, ExecFindMatchingSubPlans(), INVALID_SUBPLAN_INDEX, NO_MATCHING_SUBPLANS, AppendState::ps, ScanDirectionIsForward, and PlanState::state.

Referenced by ExecInitAppend().

463 {
464  int whichplan = node->as_whichplan;
465  int nextplan;
466 
467  /* We should never be called when there are no subplans */
468  Assert(whichplan != NO_MATCHING_SUBPLANS);
469 
470  /*
471  * If first call then have the bms member function choose the first valid
472  * subplan by initializing whichplan to -1. If there happen to be no
473  * valid subplans then the bms member function will handle that by
474  * returning a negative number which will allow us to exit returning a
475  * false value.
476  */
477  if (whichplan == INVALID_SUBPLAN_INDEX)
478  {
479  if (node->as_valid_subplans == NULL)
480  node->as_valid_subplans =
482 
483  whichplan = -1;
484  }
485 
486  /* Ensure whichplan is within the expected range */
487  Assert(whichplan >= -1 && whichplan <= node->as_nplans);
488 
490  nextplan = bms_next_member(node->as_valid_subplans, whichplan);
491  else
492  nextplan = bms_prev_member(node->as_valid_subplans, whichplan);
493 
494  if (nextplan < 0)
495  return false;
496 
497  node->as_whichplan = nextplan;
498 
499  return true;
500 }
#define NO_MATCHING_SUBPLANS
Definition: nodeAppend.c:81
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1075
EState * state
Definition: execnodes.h:913
ScanDirection es_direction
Definition: execnodes.h:477
PlanState ps
Definition: execnodes.h:1086
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
int bms_prev_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1139
#define Assert(condition)
Definition: c.h:699
int as_whichplan
Definition: execnodes.h:1089
Bitmapset * ExecFindMatchingSubPlans(PartitionPruneState *prunestate)

◆ ExecAppend()

static TupleTableSlot * ExecAppend ( PlanState pstate)
static

Definition at line 257 of file nodeAppend.c.

References AppendState::appendplans, AppendState::as_nplans, AppendState::as_whichplan, Assert, castNode, CHECK_FOR_INTERRUPTS, AppendState::choose_next_subplan, ExecClearTuple(), ExecProcNode(), INVALID_SUBPLAN_INDEX, NO_MATCHING_SUBPLANS, AppendState::ps, PlanState::ps_ResultTupleSlot, and TupIsNull.

Referenced by ExecInitAppend().

258 {
259  AppendState *node = castNode(AppendState, pstate);
260 
261  if (node->as_whichplan < 0)
262  {
263  /*
264  * If no subplan has been chosen, we must choose one before
265  * proceeding.
266  */
267  if (node->as_whichplan == INVALID_SUBPLAN_INDEX &&
268  !node->choose_next_subplan(node))
269  return ExecClearTuple(node->ps.ps_ResultTupleSlot);
270 
271  /* Nothing to do if there are no matching subplans */
272  else if (node->as_whichplan == NO_MATCHING_SUBPLANS)
273  return ExecClearTuple(node->ps.ps_ResultTupleSlot);
274  }
275 
276  for (;;)
277  {
278  PlanState *subnode;
279  TupleTableSlot *result;
280 
282 
283  /*
284  * figure out which subplan we are currently processing
285  */
286  Assert(node->as_whichplan >= 0 && node->as_whichplan < node->as_nplans);
287  subnode = node->appendplans[node->as_whichplan];
288 
289  /*
290  * get a tuple from the subplan
291  */
292  result = ExecProcNode(subnode);
293 
294  if (!TupIsNull(result))
295  {
296  /*
297  * If the subplan gave us something then return it as-is. We do
298  * NOT make use of the result slot that was set up in
299  * ExecInitAppend; there's no need for it.
300  */
301  return result;
302  }
303 
304  /* choose new subplan; if none, we're done */
305  if (!node->choose_next_subplan(node))
306  return ExecClearTuple(node->ps.ps_ResultTupleSlot);
307  }
308 }
#define NO_MATCHING_SUBPLANS
Definition: nodeAppend.c:81
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
PlanState ps
Definition: execnodes.h:1086
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:945
bool(* choose_next_subplan)(AppendState *)
Definition: execnodes.h:1096
#define TupIsNull(slot)
Definition: tuptable.h:146
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:233
#define Assert(condition)
Definition: c.h:699
int as_whichplan
Definition: execnodes.h:1089
PlanState ** appendplans
Definition: execnodes.h:1087
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98

◆ ExecAppendEstimate()

void ExecAppendEstimate ( AppendState node,
ParallelContext pcxt 
)

Definition at line 392 of file nodeAppend.c.

References add_size(), AppendState::as_nplans, ParallelContext::estimator, offsetof, ParallelAppendState::pa_finished, AppendState::pstate_len, shm_toc_estimate_chunk, and shm_toc_estimate_keys.

Referenced by ExecParallelEstimate().

394 {
395  node->pstate_len =
396  add_size(offsetof(ParallelAppendState, pa_finished),
397  sizeof(bool) * node->as_nplans);
398 
400  shm_toc_estimate_keys(&pcxt->estimator, 1);
401 }
Size pstate_len
Definition: execnodes.h:1093
shm_toc_estimator estimator
Definition: parallel.h:41
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:622

◆ ExecAppendInitializeDSM()

void ExecAppendInitializeDSM ( AppendState node,
ParallelContext pcxt 
)

Definition at line 411 of file nodeAppend.c.

References AppendState::as_pstate, AppendState::choose_next_subplan, choose_next_subplan_for_leader(), LWLockInitialize(), LWTRANCHE_PARALLEL_APPEND, ParallelAppendState::pa_lock, PlanState::plan, Plan::plan_node_id, AppendState::ps, AppendState::pstate_len, shm_toc_allocate(), shm_toc_insert(), and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

413 {
414  ParallelAppendState *pstate;
415 
416  pstate = shm_toc_allocate(pcxt->toc, node->pstate_len);
417  memset(pstate, 0, node->pstate_len);
419  shm_toc_insert(pcxt->toc, node->ps.plan->plan_node_id, pstate);
420 
421  node->as_pstate = pstate;
423 }
Size pstate_len
Definition: execnodes.h:1093
int plan_node_id
Definition: plannodes.h:145
PlanState ps
Definition: execnodes.h:1086
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
bool(* choose_next_subplan)(AppendState *)
Definition: execnodes.h:1096
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:677
Plan * plan
Definition: execnodes.h:911
static bool choose_next_subplan_for_leader(AppendState *node)
Definition: nodeAppend.c:511
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
shm_toc * toc
Definition: parallel.h:44

◆ ExecAppendInitializeWorker()

void ExecAppendInitializeWorker ( AppendState node,
ParallelWorkerContext pwcxt 
)

Definition at line 448 of file nodeAppend.c.

References AppendState::as_pstate, AppendState::choose_next_subplan, choose_next_subplan_for_worker(), PlanState::plan, Plan::plan_node_id, AppendState::ps, shm_toc_lookup(), and ParallelWorkerContext::toc.

Referenced by ExecParallelInitializeWorker().

449 {
450  node->as_pstate = shm_toc_lookup(pwcxt->toc, node->ps.plan->plan_node_id, false);
452 }
int plan_node_id
Definition: plannodes.h:145
PlanState ps
Definition: execnodes.h:1086
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
bool(* choose_next_subplan)(AppendState *)
Definition: execnodes.h:1096
static bool choose_next_subplan_for_worker(AppendState *node)
Definition: nodeAppend.c:592
Plan * plan
Definition: execnodes.h:911
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecAppendReInitializeDSM()

void ExecAppendReInitializeDSM ( AppendState node,
ParallelContext pcxt 
)

Definition at line 432 of file nodeAppend.c.

References AppendState::as_nplans, AppendState::as_pstate, ParallelAppendState::pa_finished, and ParallelAppendState::pa_next_plan.

Referenced by ExecParallelReInitializeDSM().

433 {
434  ParallelAppendState *pstate = node->as_pstate;
435 
436  pstate->pa_next_plan = 0;
437  memset(pstate->pa_finished, 0, sizeof(bool) * node->as_nplans);
438 }
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
bool pa_finished[FLEXIBLE_ARRAY_MEMBER]
Definition: nodeAppend.c:77

◆ ExecEndAppend()

void ExecEndAppend ( AppendState node)

Definition at line 319 of file nodeAppend.c.

References AppendState::appendplans, AppendState::as_nplans, ExecEndNode(), and i.

Referenced by ExecEndNode().

320 {
321  PlanState **appendplans;
322  int nplans;
323  int i;
324 
325  /*
326  * get information from the node
327  */
328  appendplans = node->appendplans;
329  nplans = node->as_nplans;
330 
331  /*
332  * shut down each of the subscans
333  */
334  for (i = 0; i < nplans; i++)
335  ExecEndNode(appendplans[i]);
336 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:539
PlanState ** appendplans
Definition: execnodes.h:1087
int i

◆ ExecInitAppend()

AppendState* ExecInitAppend ( Append node,
EState estate,
int  eflags 
)

Definition at line 101 of file nodeAppend.c.

References Append::appendplans, AppendState::appendplans, AppendState::as_first_partial_plan, AppendState::as_nplans, AppendState::as_prune_state, AppendState::as_valid_subplans, AppendState::as_whichplan, Assert, bms_add_range(), bms_is_empty(), bms_is_member(), bms_make_singleton(), bms_num_members(), AppendState::choose_next_subplan, choose_next_subplan_locally(), EXEC_FLAG_MARK, ExecAppend(), ExecAssignExprContext(), ExecFindInitialMatchingSubPlans(), ExecInitNode(), ExecInitResultTupleSlotTL(), ExecLockNonLeafAppendTables(), PartitionPruneState::execparams, PlanState::ExecProcNode, ExecSetupPartitionPruneState(), PartitionPruneState::extparams, Append::first_partial_plan, i, INVALID_SUBPLAN_INDEX, lfirst, list_length(), makeNode, NIL, NO_MATCHING_SUBPLANS, palloc(), Append::part_prune_infos, Append::partitioned_rels, PlanState::plan, AppendState::ps, PlanState::ps_ProjInfo, and PlanState::state.

Referenced by ExecInitNode().

102 {
103  AppendState *appendstate = makeNode(AppendState);
104  PlanState **appendplanstates;
105  Bitmapset *validsubplans;
106  int nplans;
107  int firstvalid;
108  int i,
109  j;
110  ListCell *lc;
111 
112  /* check for unsupported flags */
113  Assert(!(eflags & EXEC_FLAG_MARK));
114 
115  /*
116  * Lock the non-leaf tables in the partition tree controlled by this node.
117  * It's a no-op for non-partitioned parent tables.
118  */
120 
121  /*
122  * create new AppendState for our append node
123  */
124  appendstate->ps.plan = (Plan *) node;
125  appendstate->ps.state = estate;
126  appendstate->ps.ExecProcNode = ExecAppend;
127 
128  /* Let choose_next_subplan_* function handle setting the first subplan */
129  appendstate->as_whichplan = INVALID_SUBPLAN_INDEX;
130 
131  /* If run-time partition pruning is enabled, then set that up now */
132  if (node->part_prune_infos != NIL)
133  {
134  PartitionPruneState *prunestate;
135 
136  ExecAssignExprContext(estate, &appendstate->ps);
137 
138  prunestate = ExecSetupPartitionPruneState(&appendstate->ps,
139  node->part_prune_infos);
140 
141  /*
142  * When there are external params matching the partition key we may be
143  * able to prune away Append subplans now.
144  */
145  if (!bms_is_empty(prunestate->extparams))
146  {
147  /* Determine which subplans match the external params */
148  validsubplans = ExecFindInitialMatchingSubPlans(prunestate,
149  list_length(node->appendplans));
150 
151  /*
152  * If no subplans match the given parameters then we must handle
153  * this case in a special way. The problem here is that code in
154  * explain.c requires an Append to have at least one subplan in
155  * order for it to properly determine the Vars in that subplan's
156  * targetlist. We sidestep this issue by just initializing the
157  * first subplan and setting as_whichplan to NO_MATCHING_SUBPLANS
158  * to indicate that we don't need to scan any subnodes.
159  */
160  if (bms_is_empty(validsubplans))
161  {
162  appendstate->as_whichplan = NO_MATCHING_SUBPLANS;
163 
164  /* Mark the first as valid so that it's initialized below */
165  validsubplans = bms_make_singleton(0);
166  }
167 
168  nplans = bms_num_members(validsubplans);
169  }
170  else
171  {
172  /* We'll need to initialize all subplans */
173  nplans = list_length(node->appendplans);
174  validsubplans = bms_add_range(NULL, 0, nplans - 1);
175  }
176 
177  /*
178  * If there's no exec params then no further pruning can be done, we
179  * can just set the valid subplans to all remaining subplans.
180  */
181  if (bms_is_empty(prunestate->execparams))
182  appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
183 
184  appendstate->as_prune_state = prunestate;
185 
186  }
187  else
188  {
189  nplans = list_length(node->appendplans);
190 
191  /*
192  * When run-time partition pruning is not enabled we can just mark all
193  * subplans as valid, they must also all be initialized.
194  */
195  appendstate->as_valid_subplans = validsubplans =
196  bms_add_range(NULL, 0, nplans - 1);
197  appendstate->as_prune_state = NULL;
198  }
199 
200  /*
201  * Initialize result tuple type and slot.
202  */
203  ExecInitResultTupleSlotTL(estate, &appendstate->ps);
204 
205  appendplanstates = (PlanState **) palloc(nplans *
206  sizeof(PlanState *));
207 
208  /*
209  * call ExecInitNode on each of the valid plans to be executed and save
210  * the results into the appendplanstates array.
211  *
212  * While at it, find out the first valid partial plan.
213  */
214  j = i = 0;
215  firstvalid = nplans;
216  foreach(lc, node->appendplans)
217  {
218  if (bms_is_member(i, validsubplans))
219  {
220  Plan *initNode = (Plan *) lfirst(lc);
221 
222  /*
223  * Record the lowest appendplans index which is a valid partial
224  * plan.
225  */
226  if (i >= node->first_partial_plan && j < firstvalid)
227  firstvalid = j;
228 
229  appendplanstates[j++] = ExecInitNode(initNode, estate, eflags);
230  }
231  i++;
232  }
233 
234  appendstate->as_first_partial_plan = firstvalid;
235  appendstate->appendplans = appendplanstates;
236  appendstate->as_nplans = nplans;
237 
238  /*
239  * Miscellaneous initialization
240  */
241 
242  appendstate->ps.ps_ProjInfo = NULL;
243 
244  /* For parallel query, this will be overridden later. */
246 
247  return appendstate;
248 }
#define NIL
Definition: pg_list.h:69
void ExecLockNonLeafAppendTables(List *partitioned_rels, EState *estate)
Definition: execUtils.c:866
static TupleTableSlot * ExecAppend(PlanState *pstate)
Definition: nodeAppend.c:257
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:947
#define NO_MATCHING_SUBPLANS
Definition: nodeAppend.c:81
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
static bool choose_next_subplan_locally(AppendState *node)
Definition: nodeAppend.c:462
EState * state
Definition: execnodes.h:913
PlanState ps
Definition: execnodes.h:1086
List * appendplans
Definition: plannodes.h:253
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:862
int first_partial_plan
Definition: plannodes.h:259
bool(* choose_next_subplan)(AppendState *)
Definition: execnodes.h:1096
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:671
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:245
int as_first_partial_plan
Definition: execnodes.h:1090
List * partitioned_rels
Definition: plannodes.h:252
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
List * part_prune_infos
Definition: plannodes.h:264
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:917
Bitmapset * execparams
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:890
Plan * plan
Definition: execnodes.h:911
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:61
int as_whichplan
Definition: execnodes.h:1089
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:428
static int list_length(const List *l)
Definition: pg_list.h:89
Bitmapset * ExecFindInitialMatchingSubPlans(PartitionPruneState *prunestate, int nsubnodes)
Bitmapset * extparams
void * palloc(Size size)
Definition: mcxt.c:924
PlanState ** appendplans
Definition: execnodes.h:1087
int i
PartitionPruneState * ExecSetupPartitionPruneState(PlanState *planstate, List *partitionpruneinfo)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486

◆ ExecReScanAppend()

void ExecReScanAppend ( AppendState node)

Definition at line 339 of file nodeAppend.c.

References AppendState::appendplans, AppendState::as_nplans, AppendState::as_prune_state, AppendState::as_valid_subplans, AppendState::as_whichplan, bms_free(), bms_overlap(), PlanState::chgParam, PartitionPruneState::execparams, ExecReScan(), i, INVALID_SUBPLAN_INDEX, AppendState::ps, and UpdateChangedParamSet().

Referenced by ExecReScan().

340 {
341  int i;
342 
343  /*
344  * If any of the parameters being used for partition pruning have changed,
345  * then we'd better unset the valid subplans so that they are reselected
346  * for the new parameter values.
347  */
348  if (node->as_prune_state &&
349  bms_overlap(node->ps.chgParam,
350  node->as_prune_state->execparams))
351  {
353  node->as_valid_subplans = NULL;
354  }
355 
356  for (i = 0; i < node->as_nplans; i++)
357  {
358  PlanState *subnode = node->appendplans[i];
359 
360  /*
361  * ExecReScan doesn't know about my subplans, so I have to do
362  * changed-parameter signaling myself.
363  */
364  if (node->ps.chgParam != NULL)
365  UpdateChangedParamSet(subnode, node->ps.chgParam);
366 
367  /*
368  * If chgParam of subnode is not null then plan will be re-scanned by
369  * first ExecProcNode.
370  */
371  if (subnode->chgParam == NULL)
372  ExecReScan(subnode);
373  }
374 
375  /* Let choose_next_subplan_* function handle setting the first subplan */
377 }
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
PlanState ps
Definition: execnodes.h:1086
Bitmapset * chgParam
Definition: execnodes.h:940
#define INVALID_SUBPLAN_INDEX
Definition: nodeAppend.c:80
Bitmapset * execparams
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
Definition: execUtils.c:711
void bms_free(Bitmapset *a)
Definition: bitmapset.c:267
int as_whichplan
Definition: execnodes.h:1089
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
PlanState ** appendplans
Definition: execnodes.h:1087
int i

◆ mark_invalid_subplans_as_finished()

static void mark_invalid_subplans_as_finished ( AppendState node)
static

Definition at line 716 of file nodeAppend.c.

References AppendState::as_nplans, AppendState::as_prune_state, AppendState::as_pstate, AppendState::as_valid_subplans, Assert, bms_is_member(), bms_num_members(), i, and ParallelAppendState::pa_finished.

Referenced by choose_next_subplan_for_leader(), and choose_next_subplan_for_worker().

717 {
718  int i;
719 
720  /* Only valid to call this while in parallel Append mode */
721  Assert(node->as_pstate);
722 
723  /* Shouldn't have been called when run-time pruning is not enabled */
724  Assert(node->as_prune_state);
725 
726  /* Nothing to do if all plans are valid */
727  if (bms_num_members(node->as_valid_subplans) == node->as_nplans)
728  return;
729 
730  /* Mark all non-valid plans as finished */
731  for (i = 0; i < node->as_nplans; i++)
732  {
733  if (!bms_is_member(i, node->as_valid_subplans))
734  node->as_pstate->pa_finished[i] = true;
735  }
736 }
struct PartitionPruneState * as_prune_state
Definition: execnodes.h:1094
ParallelAppendState * as_pstate
Definition: execnodes.h:1092
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:671
Bitmapset * as_valid_subplans
Definition: execnodes.h:1095
bool pa_finished[FLEXIBLE_ARRAY_MEMBER]
Definition: nodeAppend.c:77
#define Assert(condition)
Definition: c.h:699
int i
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486