From 1ae13448ad2c463ed004b4713183b43d4ae41337 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Mon, 23 Nov 2026 19:14:42 -0300 Subject: [PATCH v1 1/2] Defer changed-parameter signaling of Append/MergeAppend children When an Append or MergeAppend node is rescanned, it has to do the changed-parameter signaling for its children itself, and until now it did that eagerly by calling UpdateChangedParamSet() or ExecReScan() on every child. That costs O(number of children) per rescan, even when run-time partition pruning means that only one or a few of them will be executed before the next rescan. This is the common case on the inner side of a parameterized nested loop over a partitioned table, where the per-rescan loop over all partitions can dominate the execution time with many partitions. Instead, record each rescan in a generation counter, along with the chgParam set, and deliver the pending signaling to each child only just before it is next executed. A rescan with a different chgParam set than the one pending first brings all lagging children up to date, so that the signaling each child receives is exactly what it would have received before. This is only done for nodes that do run-time pruning. Without it, every child is executed after each rescan anyway, so deferring would just add overhead; such nodes keep signaling all of their children right away, now through the new ExecReScanChildren(). search_plan_tree() in execCurrent.c relied on Append children having been rescanned, so teach it to treat a child with deferred signaling as having a pending rescan. --- src/backend/executor/execAmi.c | 126 +++++++++++++ src/backend/executor/execCurrent.c | 8 + src/backend/executor/nodeAppend.c | 52 ++++-- src/backend/executor/nodeMergeAppend.c | 43 +++-- src/include/executor/executor.h | 24 +++ src/include/nodes/execnodes.h | 44 +++++ src/test/regress/expected/partition_prune.out | 174 ++++++++++++++++++ src/test/regress/sql/partition_prune.sql | 66 +++++++ src/tools/pgindent/typedefs.list | 1 + 9 files changed, 499 insertions(+), 39 deletions(-) diff --git a/src/backend/executor/execAmi.c b/src/backend/executor/execAmi.c index 41f9bce70a7..4a684f4cf91 100644 --- a/src/backend/executor/execAmi.c +++ b/src/backend/executor/execAmi.c @@ -318,6 +318,132 @@ ExecReScan(PlanState *node) } } +/* + * ExecInitDeferredChildRescan + * Initialize a DeferredChildRescan for a node with 'nchildren' children. + * + * All children start out up to date, and stay so as long as the node never + * calls ExecDeferChildRescan(), so ExecPrepareDeferredChild() is then a + * no-op. + */ +void +ExecInitDeferredChildRescan(DeferredChildRescan *dcr, int nchildren) +{ + dcr->generation = 0; + dcr->child_generation = palloc0_array(uint64, Max(nchildren, 1)); + dcr->pending_chgparam = NULL; +} + +/* + * rescan_child + * Do the changed-parameter signaling for a child that a node with an + * arbitrary list of children would traditionally do in its ReScan + * function. + */ +static inline void +rescan_child(PlanState *child, Bitmapset *chgParam) +{ + if (chgParam != NULL) + UpdateChangedParamSet(child, chgParam); + + /* + * If chgParam of child is not null then the plan will be re-scanned by + * the first ExecProcNode or by the first ExecAsyncRequest. + */ + if (child->chgParam == NULL) + ExecReScan(child); +} + +/* + * ExecReScanChildren + * Do the changed-parameter signaling for all of a node's children right + * away. + * + * ExecReScan() doesn't know about the children of nodes like Append, so such + * nodes must do changed-parameter signaling for them in their ReScan + * function. This is the plain way of doing it; see ExecDeferChildRescan() + * for an alternative that pays off when only a few of the children are + * executed after each rescan. + */ +void +ExecReScanChildren(PlanState **children, int nchildren, Bitmapset *chgParam) +{ + for (int i = 0; i < nchildren; i++) + rescan_child(children[i], chgParam); +} + +/* + * ExecDeferChildRescan + * Called from a node's ReScan function, in place of doing the + * changed-parameter signaling for each of its children itself. + * + * ExecReScan() doesn't know about the children of nodes like Append, so such + * nodes must do changed-parameter signaling for them. Rather than doing it + * for all children right away, we just record that a rescan happened, by + * incrementing the generation counter, which makes every child "lagging" + * (see DeferredChildRescan). ExecPrepareDeferredChild() then does the + * signaling for a lagging child just before that child is next executed, + * making it "up to date" again. This matters when only a few children are + * executed per rescan, for example because of run-time partition pruning on + * the inner side of a parameterized nested loop: the rescan itself is O(1), + * and children that are never executed are never signaled. + * + * To be able to tell exactly which parameters a lagging child needs to be + * told have changed, we require that all rescans that a child may have + * missed were done with the same chgParam set, which we remember in + * pending_chgparam. When a rescan has a different set than the previous + * ones, we first bring all lagging children up to date using the old set. + * That's rare in practice: the inner side of a nested loop is always + * rescanned with the same set of parameters. + */ +void +ExecDeferChildRescan(DeferredChildRescan *dcr, PlanState **children, + int nchildren, Bitmapset *chgParam) +{ + if (!bms_equal(chgParam, dcr->pending_chgparam)) + { + /* + * The set of changed parameters differs from that of the rescans that + * lagging children have missed, so we can't keep deferring their + * signaling with a single pending set. Bring them up to date now, + * with the old set, and start over with the new one. + */ + for (int i = 0; i < nchildren; i++) + { + if (dcr->child_generation[i] != dcr->generation) + { + rescan_child(children[i], dcr->pending_chgparam); + dcr->child_generation[i] = dcr->generation; + } + } + + bms_free(dcr->pending_chgparam); + dcr->pending_chgparam = bms_copy(chgParam); + } + + /* Advance the clock; every child is now lagging */ + dcr->generation++; +} + +/* + * ExecCatchUpChildRescan + * Deliver changed-parameter signaling deferred by ExecDeferChildRescan() + * to the given child. Use ExecPrepareDeferredChild() instead of + * calling this directly. + */ +void +ExecCatchUpChildRescan(DeferredChildRescan *dcr, PlanState *child, + int childno) +{ + Assert(dcr->child_generation[childno] != dcr->generation); + + /* Apply the signaling of the missed rescans, once */ + rescan_child(child, dcr->pending_chgparam); + + /* The child is up to date now */ + dcr->child_generation[childno] = dcr->generation; +} + /* * ExecMarkPos * diff --git a/src/backend/executor/execCurrent.c b/src/backend/executor/execCurrent.c index 9058de2e998..418b696d698 100644 --- a/src/backend/executor/execCurrent.c +++ b/src/backend/executor/execCurrent.c @@ -360,6 +360,11 @@ search_plan_tree(PlanState *node, Oid table_oid, * We do need to watch out for multiple matches (possible if * Append was from UNION ALL rather than an inheritance tree). * + * Also, an input whose rescan has been deferred (see + * ExecDeferChildRescan) may still appear to be positioned on a + * tuple from before the rescan, so treat that like a pending + * rescan. + * * Note: we can NOT descend through MergeAppend similarly, since * its inputs are likely all active, and we don't know which one * returned the current output tuple. (Perhaps that could be @@ -385,6 +390,9 @@ search_plan_tree(PlanState *node, Oid table_oid, if (result) return NULL; /* multiple matches */ result = elem; + if (astate->as_child_rescan.child_generation[i] != + astate->as_child_rescan.generation) + *pending_rescan = true; } break; } diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c index f7ce2f17409..a2a82ea67e4 100644 --- a/src/backend/executor/nodeAppend.c +++ b/src/backend/executor/nodeAppend.c @@ -220,6 +220,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags) appendstate->as_first_partial_plan = firstvalid; appendstate->appendplans = appendplanstates; appendstate->as_nplans = nplans; + ExecInitDeferredChildRescan(&appendstate->as_child_rescan, nplans); /* * Initialize Append's result tuple type and slot. If the child plans all @@ -420,7 +421,6 @@ void ExecReScanAppend(AppendState *node) { int nasyncplans = node->as_nasyncplans; - int i; /* If there are any async subplans, reset async requests made for them. */ if (nasyncplans > 0) @@ -442,24 +442,20 @@ ExecReScanAppend(AppendState *node) node->as_valid_asyncplans = NULL; } - for (i = 0; i < node->as_nplans; i++) - { - PlanState *subnode = node->appendplans[i]; - - /* - * ExecReScan doesn't know about my subplans, so I have to do - * changed-parameter signaling myself. - */ - if (node->ps.chgParam != NULL) - UpdateChangedParamSet(subnode, node->ps.chgParam); - - /* - * If chgParam of subnode is not null then plan will be re-scanned by - * first ExecProcNode or by first ExecAsyncRequest. - */ - if (subnode->chgParam == NULL) - ExecReScan(subnode); - } + /* + * ExecReScan doesn't know about my subplans, so I have to do + * changed-parameter signaling myself. With run-time pruning, most of + * them might not be executed before the next rescan, so defer the + * signaling until each subplan is next chosen; see choose_next_subplan_* + * and ExecAppendAsyncBegin. Otherwise all of them will be executed, so + * just do it right away. + */ + if (node->as_prune_state && node->as_prune_state->do_exec_prune) + ExecDeferChildRescan(&node->as_child_rescan, node->appendplans, + node->as_nplans, node->ps.chgParam); + else + ExecReScanChildren(node->appendplans, node->as_nplans, + node->ps.chgParam); /* Let choose_next_subplan_* function handle setting the first subplan */ node->as_whichplan = INVALID_SUBPLAN_INDEX; @@ -604,6 +600,10 @@ choose_next_subplan_locally(AppendState *node) node->as_whichplan = nextplan; + /* Deliver any deferred rescan signaling before it gets executed */ + ExecPrepareDeferredChild(&node->as_child_rescan, + node->appendplans[nextplan], nextplan); + return true; } @@ -681,6 +681,11 @@ choose_next_subplan_for_leader(AppendState *node) LWLockRelease(&pstate->pa_lock); + /* Deliver any deferred rescan signaling before it gets executed */ + ExecPrepareDeferredChild(&node->as_child_rescan, + node->appendplans[node->as_whichplan], + node->as_whichplan); + return true; } @@ -812,6 +817,11 @@ choose_next_subplan_for_worker(AppendState *node) LWLockRelease(&pstate->pa_lock); + /* Deliver any deferred rescan signaling before it gets executed */ + ExecPrepareDeferredChild(&node->as_child_rescan, + node->appendplans[node->as_whichplan], + node->as_whichplan); + return true; } @@ -898,6 +908,10 @@ ExecAppendAsyncBegin(AppendState *node) Assert(areq->request_index == i); Assert(!areq->callback_pending); + /* Deliver any deferred rescan signaling before it gets executed */ + ExecPrepareDeferredChild(&node->as_child_rescan, + node->appendplans[i], i); + /* Do the actual work. */ ExecAsyncRequest(areq); } diff --git a/src/backend/executor/nodeMergeAppend.c b/src/backend/executor/nodeMergeAppend.c index 236be9599a4..235e623c340 100644 --- a/src/backend/executor/nodeMergeAppend.c +++ b/src/backend/executor/nodeMergeAppend.c @@ -135,6 +135,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags) mergeplanstates = palloc_array(PlanState *, nplans); mergestate->mergeplans = mergeplanstates; mergestate->ms_nplans = nplans; + ExecInitDeferredChildRescan(&mergestate->ms_child_rescan, nplans); mergestate->ms_slots = palloc0_array(TupleTableSlot *, nplans); mergestate->ms_heap = binaryheap_allocate(nplans, heap_compare_slots, @@ -287,6 +288,15 @@ ExecMergeAppend(PlanState *pstate) node->ms_valid_subplans = ExecFindMatchingSubPlans(node->ms_prune_state, false, NULL); + /* + * Deliver any deferred rescan signaling to the subplans we're about + * to execute. + */ + i = -1; + while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0) + ExecPrepareDeferredChild(&node->ms_child_rescan, + node->mergeplans[i], i); + /* * First time through: pull the first tuple from each valid subplan, * and set up the heap. @@ -462,8 +472,6 @@ ExecEndMergeAppend(MergeAppendState *node) void ExecReScanMergeAppend(MergeAppendState *node) { - int i; - /* If there are any async subplans, reset async requests made for them. */ if (node->ms_nasyncplans > 0) ExecMergeAppendAsyncReset(node); @@ -481,24 +489,19 @@ ExecReScanMergeAppend(MergeAppendState *node) node->ms_valid_subplans = NULL; } - for (i = 0; i < node->ms_nplans; i++) - { - PlanState *subnode = node->mergeplans[i]; - - /* - * ExecReScan doesn't know about my subplans, so I have to do - * changed-parameter signaling myself. - */ - if (node->ps.chgParam != NULL) - UpdateChangedParamSet(subnode, node->ps.chgParam); - - /* - * If chgParam of subnode is not null then plan will be re-scanned by - * first ExecProcNode or by first ExecAsyncRequest. - */ - if (subnode->chgParam == NULL) - ExecReScan(subnode); - } + /* + * ExecReScan doesn't know about my subplans, so I have to do + * changed-parameter signaling myself. With run-time pruning, most of + * them might not be executed before the next rescan, so defer the + * signaling until each subplan is next executed; see ExecMergeAppend. + * Otherwise all of them will be executed, so just do it right away. + */ + if (node->ms_prune_state && node->ms_prune_state->do_exec_prune) + ExecDeferChildRescan(&node->ms_child_rescan, node->mergeplans, + node->ms_nplans, node->ps.chgParam); + else + ExecReScanChildren(node->mergeplans, node->ms_nplans, + node->ps.chgParam); binaryheap_reset(node->ms_heap); node->ms_initialized = false; diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 95498600aba..c5de7778786 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -106,6 +106,15 @@ extern PGDLLIMPORT ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook; typedef struct Path Path; /* avoid including pathnodes.h here */ extern void ExecReScan(PlanState *node); +extern void ExecReScanChildren(PlanState **children, int nchildren, + Bitmapset *chgParam); +extern void ExecInitDeferredChildRescan(DeferredChildRescan *dcr, + int nchildren); +extern void ExecDeferChildRescan(DeferredChildRescan *dcr, + PlanState **children, int nchildren, + Bitmapset *chgParam); +extern void ExecCatchUpChildRescan(DeferredChildRescan *dcr, + PlanState *child, int childno); extern void ExecMarkPos(PlanState *node); extern void ExecRestrPos(PlanState *node); extern bool ExecSupportsMarkRestore(Path *pathnode); @@ -328,6 +337,21 @@ ExecProcNode(PlanState *node) return node->ExecProcNode(node); } + +/* + * ExecPrepareDeferredChild + * + * Must be called before executing the childno'th child of a node that uses + * ExecDeferChildRescan(), to deliver any rescan signaling that was deferred. + */ +static inline void +ExecPrepareDeferredChild(DeferredChildRescan *dcr, PlanState *child, + int childno) +{ + /* Is the child lagging, i.e. has it missed any rescans of its parent? */ + if (dcr->child_generation[childno] != dcr->generation) + ExecCatchUpChildRescan(dcr, child, childno); +} #endif /* diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index e4fd53c383b..68f19356de3 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1505,6 +1505,48 @@ typedef struct ModifyTableState */ struct AppendState; + +/* ---------------- + * DeferredChildRescan information + * + * Append and MergeAppend have to do changed-parameter signaling to + * their child plans themselves when they are rescanned. Doing that + * eagerly costs O(number of children) per rescan, which is wasteful + * when run-time partition pruning means that only a few of the + * children will actually be executed before the next rescan, such as + * on the inner side of a parameterized nested loop. This struct lets + * such nodes defer the signaling of each child until just before that + * child is next executed; see ExecDeferChildRescan(). + * + * This works because the signaling is idempotent: a child that misses + * several rescans in a row, all with the same chgParam set, ends up in + * the same state whether it is signaled once per rescan or just once + * before it next runs. So for each child we only need to know whether + * it has missed any rescans, and which chgParam set they had. + * + * generation a clock, incremented on every rescan of the + * parent. + * child_generation per child, the value of generation when that + * child was last signaled. A child is "up to + * date" when this equals generation, and + * "lagging" (it has missed at least one rescan) + * otherwise. Incrementing generation thus makes + * all children lagging without touching any of + * them. + * pending_chgparam the chgParam set of the rescans that lagging + * children have missed. One set suffices because + * all of those rescans used the same set: a rescan + * with a different set first brings all lagging + * children up to date using the old one. + * ---------------- + */ +typedef struct DeferredChildRescan +{ + uint64 generation; + uint64 *child_generation; + Bitmapset *pending_chgparam; +} DeferredChildRescan; + typedef struct AppendState AppendState; struct ParallelAppendState; typedef struct ParallelAppendState ParallelAppendState; @@ -1536,6 +1578,7 @@ struct AppendState bool as_valid_subplans_identified; /* is as_valid_subplans valid? */ Bitmapset *as_valid_subplans; Bitmapset *as_valid_asyncplans; /* valid asynchronous plans indexes */ + DeferredChildRescan as_child_rescan; /* deferred rescan of subplans */ bool (*choose_next_subplan) (AppendState *); }; @@ -1572,6 +1615,7 @@ typedef struct MergeAppendState Bitmapset *ms_asyncpending; /* async plans with an outstanding request */ struct WaitEventSet *ms_eventset; /* WaitEventSet used to configure file * descriptor wait events */ + DeferredChildRescan ms_child_rescan; /* deferred rescan of subplans */ } MergeAppendState; /* ---------------- diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out index 3506ecec477..a05fecee7a2 100644 --- a/src/test/regress/expected/partition_prune.out +++ b/src/test/regress/expected/partition_prune.out @@ -3456,6 +3456,180 @@ order by tbl1.col1, tprt.col1; (0 rows) drop table tbl1, tprt; +-- Run-time pruning on the inner side of a nested loop whose outer side has +-- runs of repeated values, NULLs, and changing values, so that the result of +-- pruning is sometimes reused and sometimes recomputed. +create table nlp_outer (id int, a int, b text); +insert into nlp_outer values + (1, 1, 'a'), (2, 1, 'a'), (3, 1, 'a'), (4, 2, 'b'), (5, 2, 'b'), + (6, null, null), (7, null, null), (8, 1, 'b'), (9, 1, 'b'), (10, 3, 'c'); +create table nlp (a int, b text) partition by list (a); +create table nlp_1 partition of nlp for values in (1) partition by list (b); +create table nlp_1_a partition of nlp_1 for values in ('a'); +create table nlp_1_b partition of nlp_1 for values in ('b'); +create table nlp_2 partition of nlp for values in (2); +create table nlp_3 partition of nlp for values in (3); +create table nlp_null partition of nlp for values in (null); +insert into nlp values (1, 'a'), (1, 'b'), (2, 'b'), (3, 'c'), (null, null); +create index on nlp (a, b); +create index on nlp_outer (id); +create table nlph (a int, b text) partition by hash (a); +create table nlph_0 partition of nlph for values with (modulus 4, remainder 0); +create table nlph_1 partition of nlph for values with (modulus 4, remainder 1); +create table nlph_2 partition of nlph for values with (modulus 4, remainder 2); +create table nlph_3 partition of nlph for values with (modulus 4, remainder 3); +insert into nlph select a, b from nlp; +create index on nlph (b); +analyze nlp_outer, nlp, nlph; +set enable_hashjoin = off; +set enable_mergejoin = off; +set enable_material = off; +set enable_memoize = off; +set enable_seqscan = off; +set enable_bitmapscan = off; +set enable_sort = off; +-- The outer scan's order must be stable for the expected loop counts. +explain (analyze, costs off, summary off, timing off, buffers off) +select * from (select * from nlp_outer order by id) o + join nlp n on n.a = o.a and n.b = o.b; + QUERY PLAN +---------------------------------------------------------------------------------------- + Nested Loop (actual rows=8.00 loops=1) + -> Index Scan using nlp_outer_id_idx on nlp_outer (actual rows=10.00 loops=1) + Index Searches: 1 + -> Append (actual rows=0.80 loops=10) + -> Index Scan using nlp_1_a_a_b_idx on nlp_1_a n_1 (actual rows=1.00 loops=3) + Index Cond: ((a = nlp_outer.a) AND (b = nlp_outer.b)) + Index Searches: 3 + -> Index Scan using nlp_1_b_a_b_idx on nlp_1_b n_2 (actual rows=1.00 loops=2) + Index Cond: ((a = nlp_outer.a) AND (b = nlp_outer.b)) + Index Searches: 2 + -> Index Scan using nlp_2_a_b_idx on nlp_2 n_3 (actual rows=1.00 loops=2) + Index Cond: ((a = nlp_outer.a) AND (b = nlp_outer.b)) + Index Searches: 2 + -> Index Scan using nlp_3_a_b_idx on nlp_3 n_4 (actual rows=1.00 loops=1) + Index Cond: ((a = nlp_outer.a) AND (b = nlp_outer.b)) + Index Searches: 1 + -> Index Scan using nlp_null_a_b_idx on nlp_null n_5 (never executed) + Index Cond: ((a = nlp_outer.a) AND (b = nlp_outer.b)) + Index Searches: 0 +(19 rows) + +select o.id, n.* from (select * from nlp_outer order by id) o + join nlp n on n.a = o.a and n.b = o.b; + id | a | b +----+---+--- + 1 | 1 | a + 2 | 1 | a + 3 | 1 | a + 4 | 2 | b + 5 | 2 | b + 8 | 1 | b + 9 | 1 | b + 10 | 3 | c +(8 rows) + +-- Same, through a MergeAppend. +explain (analyze, costs off, summary off, timing off, buffers off) +select * from (select * from nlp_outer order by id) o, + lateral (select * from nlph n where n.a = o.a order by n.b limit 1) s; + QUERY PLAN +------------------------------------------------------------------------------------------ + Nested Loop (actual rows=8.00 loops=1) + -> Index Scan using nlp_outer_id_idx on nlp_outer (actual rows=10.00 loops=1) + Index Searches: 1 + -> Limit (actual rows=0.80 loops=10) + -> Merge Append (actual rows=0.80 loops=10) + Sort Key: n.b + -> Index Scan using nlph_0_b_idx on nlph_0 n_1 (actual rows=1.00 loops=5) + Filter: (a = nlp_outer.a) + Index Searches: 5 + -> Index Scan using nlph_1_b_idx on nlph_1 n_2 (actual rows=1.00 loops=1) + Filter: (a = nlp_outer.a) + Index Searches: 1 + -> Index Scan using nlph_2_b_idx on nlph_2 n_3 (actual rows=1.00 loops=2) + Filter: (a = nlp_outer.a) + Index Searches: 2 + -> Index Scan using nlph_3_b_idx on nlph_3 n_4 (never executed) + Filter: (a = nlp_outer.a) + Index Searches: 0 +(18 rows) + +select o.id, s.* from (select * from nlp_outer order by id) o, + lateral (select * from nlph n where n.a = o.a order by n.b limit 1) s; + id | a | b +----+---+--- + 1 | 1 | a + 2 | 1 | a + 3 | 1 | a + 4 | 2 | b + 5 | 2 | b + 8 | 1 | a + 9 | 1 | a + 10 | 3 | c +(8 rows) + +-- Here the inner Append is rescanned alternately because of a change in o1.a +-- and o2.b, so it doesn't always see the same set of changed Params. +select o1.id, o2.id, n.* from nlp_outer o1, nlp_outer o2, + lateral (select * from nlp n where n.a = o1.a and n.b = o2.b offset 0) n +order by 1, 2; + id | id | a | b +----+----+---+--- + 1 | 1 | 1 | a + 1 | 2 | 1 | a + 1 | 3 | 1 | a + 1 | 4 | 1 | b + 1 | 5 | 1 | b + 1 | 8 | 1 | b + 1 | 9 | 1 | b + 2 | 1 | 1 | a + 2 | 2 | 1 | a + 2 | 3 | 1 | a + 2 | 4 | 1 | b + 2 | 5 | 1 | b + 2 | 8 | 1 | b + 2 | 9 | 1 | b + 3 | 1 | 1 | a + 3 | 2 | 1 | a + 3 | 3 | 1 | a + 3 | 4 | 1 | b + 3 | 5 | 1 | b + 3 | 8 | 1 | b + 3 | 9 | 1 | b + 4 | 4 | 2 | b + 4 | 5 | 2 | b + 4 | 8 | 2 | b + 4 | 9 | 2 | b + 5 | 4 | 2 | b + 5 | 5 | 2 | b + 5 | 8 | 2 | b + 5 | 9 | 2 | b + 8 | 1 | 1 | a + 8 | 2 | 1 | a + 8 | 3 | 1 | a + 8 | 4 | 1 | b + 8 | 5 | 1 | b + 8 | 8 | 1 | b + 8 | 9 | 1 | b + 9 | 1 | 1 | a + 9 | 2 | 1 | a + 9 | 3 | 1 | a + 9 | 4 | 1 | b + 9 | 5 | 1 | b + 9 | 8 | 1 | b + 9 | 9 | 1 | b + 10 | 10 | 3 | c +(44 rows) + +reset enable_hashjoin; +reset enable_mergejoin; +reset enable_material; +reset enable_memoize; +reset enable_seqscan; +reset enable_bitmapscan; +reset enable_sort; +drop table nlp, nlph, nlp_outer; -- Test with columns defined in varying orders between each level create table part_abc (a int not null, b int not null, c int not null) partition by list (a); create table part_bac (b int not null, a int not null, c int not null) partition by list (b); diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql index d6e096dc565..997e2338b91 100644 --- a/src/test/regress/sql/partition_prune.sql +++ b/src/test/regress/sql/partition_prune.sql @@ -897,6 +897,72 @@ order by tbl1.col1, tprt.col1; drop table tbl1, tprt; +-- Run-time pruning on the inner side of a nested loop whose outer side has +-- runs of repeated values, NULLs, and changing values, so that the result of +-- pruning is sometimes reused and sometimes recomputed. +create table nlp_outer (id int, a int, b text); +insert into nlp_outer values + (1, 1, 'a'), (2, 1, 'a'), (3, 1, 'a'), (4, 2, 'b'), (5, 2, 'b'), + (6, null, null), (7, null, null), (8, 1, 'b'), (9, 1, 'b'), (10, 3, 'c'); +create table nlp (a int, b text) partition by list (a); +create table nlp_1 partition of nlp for values in (1) partition by list (b); +create table nlp_1_a partition of nlp_1 for values in ('a'); +create table nlp_1_b partition of nlp_1 for values in ('b'); +create table nlp_2 partition of nlp for values in (2); +create table nlp_3 partition of nlp for values in (3); +create table nlp_null partition of nlp for values in (null); +insert into nlp values (1, 'a'), (1, 'b'), (2, 'b'), (3, 'c'), (null, null); +create index on nlp (a, b); +create index on nlp_outer (id); +create table nlph (a int, b text) partition by hash (a); +create table nlph_0 partition of nlph for values with (modulus 4, remainder 0); +create table nlph_1 partition of nlph for values with (modulus 4, remainder 1); +create table nlph_2 partition of nlph for values with (modulus 4, remainder 2); +create table nlph_3 partition of nlph for values with (modulus 4, remainder 3); +insert into nlph select a, b from nlp; +create index on nlph (b); +analyze nlp_outer, nlp, nlph; + +set enable_hashjoin = off; +set enable_mergejoin = off; +set enable_material = off; +set enable_memoize = off; +set enable_seqscan = off; +set enable_bitmapscan = off; +set enable_sort = off; + +-- The outer scan's order must be stable for the expected loop counts. +explain (analyze, costs off, summary off, timing off, buffers off) +select * from (select * from nlp_outer order by id) o + join nlp n on n.a = o.a and n.b = o.b; + +select o.id, n.* from (select * from nlp_outer order by id) o + join nlp n on n.a = o.a and n.b = o.b; + +-- Same, through a MergeAppend. +explain (analyze, costs off, summary off, timing off, buffers off) +select * from (select * from nlp_outer order by id) o, + lateral (select * from nlph n where n.a = o.a order by n.b limit 1) s; + +select o.id, s.* from (select * from nlp_outer order by id) o, + lateral (select * from nlph n where n.a = o.a order by n.b limit 1) s; + +-- Here the inner Append is rescanned alternately because of a change in o1.a +-- and o2.b, so it doesn't always see the same set of changed Params. +select o1.id, o2.id, n.* from nlp_outer o1, nlp_outer o2, + lateral (select * from nlp n where n.a = o1.a and n.b = o2.b offset 0) n +order by 1, 2; + +reset enable_hashjoin; +reset enable_mergejoin; +reset enable_material; +reset enable_memoize; +reset enable_seqscan; +reset enable_bitmapscan; +reset enable_sort; + +drop table nlp, nlph, nlp_outer; + -- Test with columns defined in varying orders between each level create table part_abc (a int not null, b int not null, c int not null) partition by list (a); create table part_bac (b int not null, a int not null, c int not null) partition by list (b); diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index f10eb469266..b0bb9123ffd 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -663,6 +663,7 @@ DecodingWorkerShared DefElem DefElemAction DefaultACLInfo +DeferredChildRescan DefineStmt DefnDumperPtr DeleteStmt -- 2.50.1 (Apple Git-155)