public inbox for [email protected]
help / color / mirror / Atom feedFrom: Amit Langote <[email protected]>
To: Tender Wang <[email protected]>
Cc: Alexander Lakhin <[email protected]>
Cc: Tomas Vondra <[email protected]>
Cc: Robert Haas <[email protected]>
Cc: Alvaro Herrera <[email protected]>
Cc: Andres Freund <[email protected]>
Cc: Daniel Gustafsson <[email protected]>
Cc: David Rowley <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Cc: Thom Brown <[email protected]>
Cc: Tom Lane <[email protected]>
Subject: Re: generic plans and "initial" pruning
Date: Sun, 23 Feb 2025 17:35:49 +0900
Message-ID: <CA+HiwqFp3jZGSz==QjeuV62_62F6+V6b62=Uqvy99sW_gsgWBA@mail.gmail.com> (raw)
In-Reply-To: <CAHewXNnEwVQoXBiR+nC09R20Psr2xXjOuhatC-kHfr0-B2pcVw@mail.gmail.com>
References: <CA+HiwqFpZ80UJKr4tZus4Omgg7YESzFXKSwSHRW2Ap2=XSVyUA@mail.gmail.com>
<[email protected]>
<CA+HiwqH8N-SxEB6SysEBsYNgV_KJs66k9Z2SNmqVzbBP-60yWg@mail.gmail.com>
<[email protected]>
<CA+HiwqEmG9YCQvG6uux7sO=jKFSAW6hA4Ea-ymfD+JhJAe4PWQ@mail.gmail.com>
<CA+HiwqE2FfJfH=siLiR3kJ13tmXZORAGTWsZc2r52o1_5BDv+g@mail.gmail.com>
<[email protected]>
<CA+HiwqFhkpXHAA=4NY5SqYXX08uq=nYtXcSByNZF=2MAy1UA7A@mail.gmail.com>
<CA+HiwqHCcSoYfpMjFshaU1bj6NjreiDvMSDpVSeBmqk-kbWrPw@mail.gmail.com>
<CA+HiwqHOejJk0_qMuM5g38h70hY_JvHMAKwnH3k=urfTXauPQA@mail.gmail.com>
<CA+HiwqFsGKM82oaMby3VWYXf_XFpDAMeT+6SXgj-45HpTrS1dA@mail.gmail.com>
<CA+HiwqFA5hUWYktt3VMh4zQOYMxqH-MpdX8eemfM+o-9dY-zbQ@mail.gmail.com>
<CA+HiwqEn7bbUXaXO=SmUujBjJSHfS31cwQroHRBwT0sR=66bgg@mail.gmail.com>
<CA+HiwqGGLDTd1ZTK1c0zv4La7XOVSVMqBuNtscJeh6FyUQvFvA@mail.gmail.com>
<CA+HiwqE2JFiqqrXdyJVQWY-fMGwzDkLqjXQdUKbPaCpDpxd_2g@mail.gmail.com>
<[email protected]>
<CAHewXNnEwVQoXBiR+nC09R20Psr2xXjOuhatC-kHfr0-B2pcVw@mail.gmail.com>
On Sun, Feb 23, 2025 at 2:03 AM Tender Wang <[email protected]> wrote:
> Alexander Lakhin <[email protected]> 于2025年2月22日周六 23:00写道:
>> 21.02.2025 05:40, Amit Langote wrote:
>>
>> I pushed the final piece yesterday.
>>
>>
>> Please look at new error, produced by the following script,
>> starting from 525392d57:
>> CREATE TABLE t(id int) PARTITION BY RANGE (id);
>> CREATE INDEX idx on t(id);
>> CREATE TABLE tp_1 PARTITION OF t FOR VALUES FROM (10) TO (20);
>> CREATE TABLE tp_2 PARTITION OF t FOR VALUES FROM (20) TO (30) PARTITION BY RANGE(id);
>> CREATE TABLE tp_2_1 PARTITION OF tp_2 FOR VALUES FROM (21) to (22);
>> CREATE TABLE tp_2_2 PARTITION OF tp_2 FOR VALUES FROM (22) to (23);
>> CREATE FUNCTION stable_one() RETURNS INT AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql STABLE;
>>
>> SELECT min(id) OVER (PARTITION BY id ORDER BY id) FROM t WHERE id >= stable_one();
>>
>> ERROR: XX000: trying to open a pruned relation
>> LOCATION: ExecGetRangeTableRelation, execUtils.c:830
>>
>> This issue was discovered with SQLsmith.
Thanks for the report.
> The error message was added in commit 525392d57. In this case, the estate->es_unpruned_relids only includes 1, which is the offset of table t.
> In register_partpruneinfo(), we collect glob->prunableRelids; in this case, it contains 2,3,4,5. Then we will do:
> result->unprunableRelids = bms_difference(glob->allRelids,
> glob->prunableRelids);
> so the result->unprunableRelids only contains 1.
>
> But tp_2 is also partition table, and its partpruneinfo created by create_append_plan() is put into the head of global list.
> So we first process it in ExecDoInitialPruning(). Then error reports because we only contain 1 in estate->es_unpruned_relids.
Thanks for checking.
The RT index of tp_2 should appear in PlannedStmt.unprunableRelids,
because it needs to be opened in CreatePartitionPruneState() for
setting up its PartitionPruneInfo. We use ExecGetRangeTableRelation()
to open, which expects the relation to be locked, so the error.
To ensure tp_2 appears in PlannedStmt.unprunableRelids, we should
prevent make_partitionedrel_pruneinfo() from placing the RT index into
leafpart_rti_map[], as the current condition for inclusion doesn’t
account for whether the partition is itself partitioned.
I've come up with the attached.
--
Thanks, Amit Langote
Attachments:
[application/octet-stream] v1-0001-Fix-bug-in-cbc127917-to-handle-nested-Append-corr.patch (5.4K, 2-v1-0001-Fix-bug-in-cbc127917-to-handle-nested-Append-corr.patch)
download | inline diff:
From f829cd9a679003b14030679adbd08aae70cefc55 Mon Sep 17 00:00:00 2001
From: Amit Langote <[email protected]>
Date: Sun, 23 Feb 2025 17:19:28 +0900
Subject: [PATCH v1] Fix bug in cbc127917 to handle nested Append correctly
A sub-partitioned partition with a subplan that is an Append node was
not correctly reported in PlannedStmt.unprunableRelids. This omission
led to ExecGetRangeTableRelation() reporting an error when called from
CreatePartitionPruneState() to process its PartitionPruneInfo.
Reported-by: Alexander Lakhin <[email protected]> (via sqlsmith)
Diagnosed-by: Tender Wang <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
src/backend/executor/execPartition.c | 14 +++++++++----
src/backend/partitioning/partprune.c | 8 +++++++-
src/test/regress/expected/partition_prune.out | 20 +++++++++++++++++++
src/test/regress/sql/partition_prune.sql | 7 +++++++
4 files changed, 44 insertions(+), 5 deletions(-)
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 432eeaf9034..b86fc5ea297 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -2589,9 +2589,9 @@ ExecFindMatchingSubPlans(PartitionPruneState *prunestate,
* find_matching_subplans_recurse
* Recursive worker function for ExecFindMatchingSubPlans
*
- * Adds valid (non-prunable) subplan IDs to *validsubplans and the RT indexes
- * of their corresponding leaf partitions to *validsubplan_rtis if
- * it's non-NULL.
+ * Adds valid (non-prunable) subplan IDs to *validsubplans. If
+ * *validsubplan_rtis is non-NULL, it also adds the RT indexes of their
+ * corresponding partitions, but only if they are leaf partitions.
*/
static void
find_matching_subplans_recurse(PartitionPruningData *prunedata,
@@ -2628,7 +2628,13 @@ find_matching_subplans_recurse(PartitionPruningData *prunedata,
{
*validsubplans = bms_add_member(*validsubplans,
pprune->subplan_map[i]);
- if (validsubplan_rtis)
+
+ /*
+ * Subplan might be a nested Append / MergeAppend for a
+ * sub-partitioned partition whose RT index need not be reported
+ * to the caller.
+ */
+ if (validsubplan_rtis && pprune->leafpart_rti_map[i])
*validsubplan_rtis = bms_add_member(*validsubplan_rtis,
pprune->leafpart_rti_map[i]);
}
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index ff926732f36..3faf3f8555c 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -687,7 +687,13 @@ make_partitionedrel_pruneinfo(PlannerInfo *root, RelOptInfo *parentrel,
if (subplanidx >= 0)
{
present_parts = bms_add_member(present_parts, i);
- leafpart_rti_map[i] = (int) partrel->relid;
+
+ /*
+ * Subplan might be a nested Append/MergeAppend for a
+ * sub-partitioned partition.
+ */
+ if (partrel->nparts == -1)
+ leafpart_rti_map[i] = (int) partrel->relid;
/* Record finding this subplan */
subplansfound = bms_add_member(subplansfound, subplanidx);
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out
index 6f80b62a3b8..94a499fc8e0 100644
--- a/src/test/regress/expected/partition_prune.out
+++ b/src/test/regress/expected/partition_prune.out
@@ -4590,5 +4590,25 @@ table part_abc_view;
2 | c | t
(1 row)
+-- A case with nested Append with its own PartitionPruneInfo.
+create index on part_abc (a);
+create table part_abc_3 partition of part_abc for values in (3, 4) partition by list (a);
+create table part_abc_3_3 partition of part_abc_3 for values in (3);
+create table part_abc_3_4 partition of part_abc_3 for values in (4);
+explain (costs off) select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1;
+ QUERY PLAN
+---------------------------------------------------------------------------------------
+ WindowAgg
+ -> Append
+ Subplans Removed: 1
+ -> Index Only Scan using part_abc_2_a_idx on part_abc_2 part_abc_1
+ Index Cond: (a >= (stable_one() + 1))
+ -> Append
+ -> Index Only Scan using part_abc_3_3_a_idx on part_abc_3_3 part_abc_3
+ Index Cond: (a >= (stable_one() + 1))
+ -> Index Only Scan using part_abc_3_4_a_idx on part_abc_3_4 part_abc_4
+ Index Cond: (a >= (stable_one() + 1))
+(10 rows)
+
drop view part_abc_view;
drop table part_abc;
diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql
index 86621dcec0b..48273f8d027 100644
--- a/src/test/regress/sql/partition_prune.sql
+++ b/src/test/regress/sql/partition_prune.sql
@@ -1397,5 +1397,12 @@ using (select stable_one() + 2 as pid) as q join part_abc_1 pt1 on (q.pid = pt1.
when matched then delete returning pt.a;
table part_abc_view;
+-- A case with nested Append with its own PartitionPruneInfo.
+create index on part_abc (a);
+create table part_abc_3 partition of part_abc for values in (3, 4) partition by list (a);
+create table part_abc_3_3 partition of part_abc_3 for values in (3);
+create table part_abc_3_4 partition of part_abc_3 for values in (4);
+explain (costs off) select min(a) over (partition by a order by a) from part_abc where a >= stable_one() + 1;
+
drop view part_abc_view;
drop table part_abc;
--
2.43.0
view thread (66+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: Re: generic plans and "initial" pruning
In-Reply-To: <CA+HiwqFp3jZGSz==QjeuV62_62F6+V6b62=Uqvy99sW_gsgWBA@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox