From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Alan Jackson <ajax@tvsquared.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: inconsistent results querying table partitioned by date
Date: Fri, 10 May 2019 14:37:34 +0900
Message-ID: <49cd5235-cbe6-e686-8014-85c1e45efe56@lab.ntt.co.jp> (raw)
In-Reply-To: <24525.1557444159@sss.pgh.pa.us>
References: <FAD28A83-AC73-489E-A058-2681FA31D648@tvsquared.com>
<24525.1557444159@sss.pgh.pa.us>
On 2019/05/10 8:22, Tom Lane wrote:
> Alan Jackson <ajax@tvsquared.com> writes:
>> Im having a problem with querying a table partitioned by date.
>> Depending on the sequence of operations on a date parameter used in a where clause, I either get the expected results, or no results.
>
> Yeah, this is pretty clearly broken.
Indeed it is.
> It looks to me like the partition
> pruning code is making insupportable assumptions about a comparison to
> a stable expression. Using your example table:
>
> regression=# explain select * from dataid where id=1 and datadatetime < localtimestamp;
> QUERY PLAN
> ----------------------------------------------------------------------------------
> Bitmap Heap Scan on dataid_default (cost=4.19..11.31 rows=3 width=12)
> Recheck Cond: ((id = 1) AND (datadatetime < LOCALTIMESTAMP))
> -> Bitmap Index Scan on dataid_default_pkey (cost=0.00..4.19 rows=3 width=0)
> Index Cond: ((id = 1) AND (datadatetime < LOCALTIMESTAMP))
> (4 rows)
>
> It should absolutely not have pruned away the dataid_201902 partition,
> but it did.
>
> While I'm on about it, this behavior is also insupportable:
>
> regression=# explain select * from dataid where id=1 and datadatetime < '2018-05-09'::timestamptz ;
(likely a typo in the condition: s/2018/2019)
> QUERY PLAN
> --------------------------------------------------------------------------------------------------------
> Bitmap Heap Scan on dataid_default (cost=4.18..11.30 rows=3 width=12)
> Recheck Cond: ((id = 1) AND (datadatetime < '2018-05-09 00:00:00-04'::timestamp with time zone))
> -> Bitmap Index Scan on dataid_default_pkey (cost=0.00..4.18 rows=3 width=0)
> Index Cond: ((id = 1) AND (datadatetime < '2018-05-09 00:00:00-04'::timestamp with time zone))
> (4 rows)
>
> because timestamp-against-timestamptz comparison is inherently only
> stable; the pruning code is way exceeding its authority by supposing
> that a comparison that holds at plan time will hold at runtime,
> even with a constant comparison value.
I looked into it and the problem is not really that plan-time pruning is
comparing stable expressions against partition bounds. If it had, it
wouldn't have pruned dataid_201902 anyway, because its bounding range for
datadatetime is '2019-02-01' to '2019-03-01', which is clearly less than
'2019-05-09' (Tom's localtimestamp).
The real problem seems to be with the way range partition pruning assumes
an operator strategy to perform pruning with. Quoted query's WHERE clause
looks something like this: (firstkey = CONSTANT AND secondkey <
STABLE_EXPRESSION). From this set of clauses, a (CONSTANT,
STABLE_EXPRESSION) tuple is formed to be compared against partition bounds
using row-comparison-like semantics. As things stand today, the actual
comparison function (partprune.c: get_matching_range_bounds()) receives
only the strategy of the last expression, which in this case is that of a
LESS operator. When the tuple (CONSTANT, STABLE_EXPRESSION) passes
through the last step to extract Datum values to be passed to
get_matching_range_bounds, it's correctly determined that
STABLE_EXPRESSION cannot be computed during planning and so the tuple is
truncated to just (CONSTANT-Datum), but the strategy to assume during
pruning is still that of the LESS operator, whereas now it should really
be EQUAL. With LESS semantics, get_matching_range_bounds() concludes that
no partition bounds are smaller than 1 (extracted from id=1 in the above
query), except the default partition, so it prunes 'dataid_201902'.
I've attached a patch to fix that. Actually, I've attached two patches --
the 1st one adds a test for the misbehaving case with *wrong* output
wherein a partition is incorrectly pruned, and the 2nd actually fixes the
bug and updates the output of the test added by the 1st patch. Divided
the patch this way just to show the bug clearly.
> Seems to be equally broken in v11 and HEAD. I didn't try v10.
v10 is fine, as it uses constraint exclusion.
Attached patches apply to both v11 and HEAD.
Thanks,
Amit
Attachments:download all (2 files, .tar.gz)0001-Add-test.patch (text/plain, 2.4K) download
From 15c8ca8cd9581a1b8eac67bbccc50423803fd1e7 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 10 May 2019 13:52:41 +0900
Subject: [PATCH 1/2] Add test
---
src/test/regress/expected/partition_prune.out | 17 +++++++++++++++++
src/test/regress/sql/partition_prune.sql | 7 +++++++
2 files changed, 24 insertions(+)
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex 6f320dfff4..c8cd6a23f1 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -938,6 +938,23 @@ explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1) or (a = 10 a
Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 9)))
(9 rows)
+--+-- Check that pruning with composite range partitioning works correctly+-- correctly in the face of combination of static and runtime values being+-- passed to perform plan-time pruning with+--+explain (costs off) select * from mc3p where a = 1 and abs(b) < (select 2);+ QUERY PLAN +---------------------------------------------+ Append+ InitPlan 1 (returns $0)+ -> Result+ -> Seq Scan on mc3p0+ Filter: ((a = 1) AND (abs(b) < $0))+ -> Seq Scan on mc3p_default+ Filter: ((a = 1) AND (abs(b) < $0))+(7 rows)+
-- a simpler multi-column keys case
create table mc2p (a int, b int) partition by range (a, b);
create table mc2p_default partition of mc2p default;
diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sqlindex eafbec6f35..adfdc4d3c0 100644--- a/src/test/regress/sql/partition_prune.sql+++ b/src/test/regress/sql/partition_prune.sql@@ -122,6 +122,13 @@ explain (costs off) select * from mc3p where a = 1 or abs(b) = 1 or c = 1;
explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1) or (a = 10 and abs(b) = 10);
explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1) or (a = 10 and abs(b) = 9);
+--+-- Check that pruning with composite range partitioning works correctly+-- correctly in the face of combination of static and runtime values being+-- passed to perform plan-time pruning with+--+explain (costs off) select * from mc3p where a = 1 and abs(b) < (select 2);+
-- a simpler multi-column keys case
create table mc2p (a int, b int) partition by range (a, b);
create table mc2p_default partition of mc2p default;
--
2.11.0
0002-Bug-fix.patch (text/plain, 2.6K) download
From 1c6856b20e51c91eed0d2fd4e040d1df2ca2d21d Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 10 May 2019 13:52:59 +0900
Subject: [PATCH 2/2] Bug fix
---
src/backend/partitioning/partprune.c | 28 ++++++++++++++++++++++-----
src/test/regress/expected/partition_prune.out | 4 +++-
2 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.cindex b5c1c7d4dd..f6da2a8b2c 100644--- a/src/backend/partitioning/partprune.c+++ b/src/backend/partitioning/partprune.c@@ -3103,11 +3103,29 @@ perform_pruning_base_step(PartitionPruneContext *context,
opstep->nullkeys);
case PARTITION_STRATEGY_RANGE:
- return get_matching_range_bounds(context,- opstep->opstrategy,- values, nvalues,- partsupfunc,- opstep->nullkeys);+ {+ int opstrategy = opstep->opstrategy;++ /*+ * If we got values for only a subset (prefix) of the+ * expressions in opstep->exprs, adjust the strategy to+ * be used for pruning. Originally, it would be the strategy+ * of the operator comparing the *last* expression against the+ * corresponding partition key, but it would be wrong to use+ * the same strategy now that we'll be comparing the values of+ * only a prefix of expressions against the partition bounds.+ * Use equality strategy, because all expressions in the+ * prefix must have been extracted from clauses containing+ * equality (=) or inclusive operators (<=, >=) anyway.+ */+ if (nvalues < list_length(opstep->exprs))+ opstrategy = BTEqualStrategyNumber;+ return get_matching_range_bounds(context,+ opstrategy,+ values, nvalues,+ partsupfunc,+ opstep->nullkeys);+ }
default:
elog(ERROR, "unexpected partition strategy: %d",
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex c8cd6a23f1..c26a86472a 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -951,9 +951,11 @@ explain (costs off) select * from mc3p where a = 1 and abs(b) < (select 2);
-> Result
-> Seq Scan on mc3p0
Filter: ((a = 1) AND (abs(b) < $0))
+ -> Seq Scan on mc3p1+ Filter: ((a = 1) AND (abs(b) < $0))
-> Seq Scan on mc3p_default
Filter: ((a = 1) AND (abs(b) < $0))
-(7 rows)+(9 rows)
-- a simpler multi-column keys case
create table mc2p (a int, b int) partition by range (a, b);
--
2.11.0
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: Langote_Amit_f8@lab.ntt.co.jp, tgl@sss.pgh.pa.us, ajax@tvsquared.com
Subject: Re: inconsistent results querying table partitioned by date
In-Reply-To: <49cd5235-cbe6-e686-8014-85c1e45efe56@lab.ntt.co.jp>
References: <FAD28A83-AC73-489E-A058-2681FA31D648@tvsquared.com> <24525.1557444159@sss.pgh.pa.us> <49cd5235-cbe6-e686-8014-85c1e45efe56@lab.ntt.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox