From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
Cc: ajax@tvsquared.com
Cc: pgsql-bugs@lists.postgresql.org
Cc: david.rowley@2ndquadrant.com
Subject: Re: inconsistent results querying table partitioned by date
Date: Mon, 13 May 2019 14:40:38 +0900
Message-ID: <a0bca4f7-b493-4d0a-8080-8dad3748c5ae@lab.ntt.co.jp> (raw)
In-Reply-To: <20307.1557522309@sss.pgh.pa.us>
References: <FAD28A83-AC73-489E-A058-2681FA31D648@tvsquared.com>
<24525.1557444159@sss.pgh.pa.us>
<49cd5235-cbe6-e686-8014-85c1e45efe56@lab.ntt.co.jp>
<20190510.153337.236910404.horiguchi.kyotaro@lab.ntt.co.jp>
<90aeaf38-270a-7e45-6b7d-af2e3d1e3719@lab.ntt.co.jp>
<20307.1557522309@sss.pgh.pa.us>
On 2019/05/11 6:05, Tom Lane wrote:
> regression=# explain select * from dataid where id=1 and datadatetime < '2018-01-01'::timestamptz;
> QUERY PLAN
> --------------------------------------------------------------------------------------------------------
> Bitmap Heap Scan on dataid_default (cost=4.18..11.30 rows=3 width=12)
> Recheck Cond: ((id = 1) AND (datadatetime < '2018-01-01 00:00:00-05'::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-01-01 00:00:00-05'::timestamp with time zone))
> (4 rows)
>
> That's not fine. What we have here is a "timestamp < timestamptz"
> operator, which is only stable, therefore it might give different
> results at runtime than at plan time. You can't make plan-time
> pruning decisions with that. What we should have gotten here was
> an Append node that could do run-time pruning.
You're right. It seems that prune_append_rel_partitions() is forgetting
to filter mutable clauses from rel->baserestrictinfo, like
relation_excluded_by_constraints() does. I fixed that in the attached
0003 patch, which also adds a test for this scenario. I needed to also
tweak run-time pruning support code a bit so that it considers the cases
involving mutable functions as requiring (startup) run-time pruning, in
addition to the cases with mutable expressions. Adding David if he wants
to comment.
0003 patch cannot be applied as-is to both REL_11_STABLE and HEAD
branches, so I've attached two files, one for each branch.
BTW, while looking at this, I came across this comment in parse_coerce.c:
coerce_type():
* XXX if the typinput function is not immutable, we really ought to
* postpone evaluation of the function call until runtime. But there
* is no way to represent a typinput function call as an expression
* tree, because C-string values are not Datums. (XXX This *is*
* possible as of 7.3, do we want to do it?)
Should something be done about that?
Thanks,
Amit
Attachments:download all (4 files, .tar.gz)pg11-v1-0003-Fix-planner-to-not-prune-partitions-with-non-immu.patch (text/plain, 5.9K) download
From 8ac87a9a8b1cc5a82946afa3c942d6353b57f847 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Mon, 13 May 2019 13:55:24 +0900
Subject: [PATCH v3] Fix planner to not prune partitions with non-immutable
operators
---
src/backend/partitioning/partprune.c | 43 ++++++++++++++++++++++-----
src/test/regress/expected/partition_prune.out | 17 +++++++++++
src/test/regress/sql/partition_prune.sql | 10 +++++++
3 files changed, 63 insertions(+), 7 deletions(-)
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.cindex 7c894fc6ad..94954c7a6d 100644--- a/src/backend/partitioning/partprune.c+++ b/src/backend/partitioning/partprune.c@@ -39,6 +39,7 @@
#include "access/nbtree.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
+#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
@@ -552,25 +553,45 @@ Relids
prune_append_rel_partitions(RelOptInfo *rel)
{
Relids result;
- List *clauses = rel->baserestrictinfo;+ List *safe_restrictions = NIL;+ ListCell *lc;
List *pruning_steps;
bool contradictory;
PartitionPruneContext context;
Bitmapset *partindexes;
int i;
- Assert(clauses != NIL);+ Assert(rel->baserestrictinfo != NIL);
Assert(rel->part_scheme != NULL);
/* If there are no partitions, return the empty set */
if (rel->nparts == 0)
return NULL;
+ /* We dare not perform pruning with non-immutable functions. */+ foreach(lc, rel->baserestrictinfo)+ {+ RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);++ if (!contain_mutable_functions((Node *) rinfo->clause))+ safe_restrictions = lappend(safe_restrictions, rinfo);+ }++ /* No clauses to perform pruning with; return all partitions. */+ if (safe_restrictions == NIL)+ {+ result = NULL;+ for (i = 0; i < rel->nparts; i++)+ result = bms_add_member(result, rel->part_rels[i]->relid);+ return result;+ }+
/*
* Process clauses. If the clauses are found to be contradictory, we can
* return the empty set.
*/
- pruning_steps = gen_partprune_steps(rel, clauses, &contradictory);+ pruning_steps = gen_partprune_steps(rel, safe_restrictions,+ &contradictory);
if (contradictory)
return NULL;
@@ -2896,8 +2917,9 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
/*
* Steps require run-time pruning if they contain EXEC_PARAM Params.
- * Otherwise, if their expressions aren't simple Consts, they require- * startup-time pruning.+ * Otherwise, if their expressions aren't simple Consts or if the+ * comparison functions are non-immutable, they require startup-time+ * pruning.
*/
pinfo->nexprs = list_length(steps) * partnatts;
pinfo->hasexecparam = (bool *) palloc0(sizeof(bool) * pinfo->nexprs);
@@ -2908,16 +2930,18 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
foreach(lc, steps)
{
PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc);
- ListCell *lc2;+ ListCell *lc2,+ *lc3;
int keyno;
if (!IsA(step, PartitionPruneStepOp))
continue;
keyno = 0;
- foreach(lc2, step->exprs)+ forboth(lc2, step->exprs, lc3, step->cmpfns)
{
Expr *expr = lfirst(lc2);
+ Oid fnoid = lfirst_oid(lc3);
if (!IsA(expr, Const))
{
@@ -2940,6 +2964,11 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
doruntimeprune = true;
}
+ else if (func_volatile(fnoid) != PROVOLATILE_IMMUTABLE)+ {+ doruntimeprune = true;+ pinfo->do_initial_prune = true;+ }
keyno++;
}
}
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex 889d4e14e2..004b3be003 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -3505,3 +3505,20 @@ explain (costs off) update listp1 set a = 1 where a = 2;
reset constraint_exclusion;
reset enable_partition_pruning;
drop table listp;
+--+-- check that stable query clauses are only used in run-time pruning+--+create table stable_qual_pruning(a timestamp) partition by range (a);+create table stable_qual_pruning1 partition of stable_qual_pruning for values from ('2019-01-01') to ('2019-02-01');+create table stable_qual_pruning2 partition of stable_qual_pruning for values from ('2019-02-01') to ('2019-03-01');+-- timestamp < timestamptz comparison is only stable, not immutable+explain (analyze, costs off, summary off, timing off) select * from stable_qual_pruning where a < '2019-02-01'::timestamptz;+ QUERY PLAN +--------------------------------------------------------------------------------+ Append (actual rows=0 loops=1)+ Subplans Removed: 1+ -> Seq Scan on stable_qual_pruning1 (actual rows=0 loops=1)+ Filter: (a < 'Fri Feb 01 00:00:00 2019 PST'::timestamp with time zone)+(4 rows)++drop table stable_qual_pruning;diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sqlindex b78b08dfc1..8c3b641a19 100644--- a/src/test/regress/sql/partition_prune.sql+++ b/src/test/regress/sql/partition_prune.sql@@ -928,3 +928,13 @@ reset constraint_exclusion;
reset enable_partition_pruning;
drop table listp;
++--+-- check that stable query clauses are only used in run-time pruning+--+create table stable_qual_pruning(a timestamp) partition by range (a);+create table stable_qual_pruning1 partition of stable_qual_pruning for values from ('2019-01-01') to ('2019-02-01');+create table stable_qual_pruning2 partition of stable_qual_pruning for values from ('2019-02-01') to ('2019-03-01');+-- timestamp < timestamptz comparison is only stable, not immutable+explain (analyze, costs off, summary off, timing off) select * from stable_qual_pruning where a < '2019-02-01'::timestamptz;+drop table stable_qual_pruning;--
2.11.0
v1-0003-Fix-planner-to-not-prune-partitions-with-non-immu.patch (text/plain, 5.8K) download
From 555e65d5fc74aa7be75d7873c2180cd93ec915ac Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Mon, 13 May 2019 13:02:47 +0900
Subject: [PATCH v3 3/3] Fix planner to not prune partitions with non-immutable
operators
---
src/backend/partitioning/partprune.c | 37 ++++++++++++++++++++++-----
src/test/regress/expected/partition_prune.out | 17 ++++++++++++
src/test/regress/sql/partition_prune.sql | 10 ++++++++
3 files changed, 57 insertions(+), 7 deletions(-)
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.cindex 92806ab9e1..e19c7b6917 100644--- a/src/backend/partitioning/partprune.c+++ b/src/backend/partitioning/partprune.c@@ -39,6 +39,7 @@
#include "access/nbtree.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
+#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
@@ -583,7 +584,8 @@ gen_partprune_steps(RelOptInfo *rel, List *clauses, bool *contradictory)
Bitmapset *
prune_append_rel_partitions(RelOptInfo *rel)
{
- List *clauses = rel->baserestrictinfo;+ List *safe_restrictions = NIL;+ ListCell *lc;
List *pruning_steps;
bool contradictory;
PartitionPruneContext context;
@@ -598,14 +600,27 @@ prune_append_rel_partitions(RelOptInfo *rel)
* If pruning is disabled or if there are no clauses to prune with, return
* all partitions.
*/
- if (!enable_partition_pruning || clauses == NIL)+ if (!enable_partition_pruning)+ return bms_add_range(NULL, 0, rel->nparts - 1);++ /* We dare not perform pruning with non-immutable functions. */+ foreach(lc, rel->baserestrictinfo)+ {+ RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);++ if (!contain_mutable_functions((Node *) rinfo->clause))+ safe_restrictions = lappend(safe_restrictions, rinfo);+ }++ if (safe_restrictions == NIL)
return bms_add_range(NULL, 0, rel->nparts - 1);
/*
* Process clauses. If the clauses are found to be contradictory, we can
* return the empty set.
*/
- pruning_steps = gen_partprune_steps(rel, clauses, &contradictory);+ pruning_steps = gen_partprune_steps(rel, safe_restrictions,+ &contradictory);
if (contradictory)
return NULL;
@@ -2926,8 +2941,9 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
/*
* Steps require run-time pruning if they contain EXEC_PARAM Params.
- * Otherwise, if their expressions aren't simple Consts, they require- * startup-time pruning.+ * Otherwise, if their expressions aren't simple Consts or if the+ * comparison functions are non-immutable, they require startup-time+ * pruning.
*/
pinfo->nexprs = list_length(steps) * partnatts;
pinfo->hasexecparam = (bool *) palloc0(sizeof(bool) * pinfo->nexprs);
@@ -2938,16 +2954,18 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
foreach(lc, steps)
{
PartitionPruneStepOp *step = (PartitionPruneStepOp *) lfirst(lc);
- ListCell *lc2;+ ListCell *lc2,+ *lc3;
int keyno;
if (!IsA(step, PartitionPruneStepOp))
continue;
keyno = 0;
- foreach(lc2, step->exprs)+ forboth(lc2, step->exprs, lc3, step->cmpfns)
{
Expr *expr = lfirst(lc2);
+ Oid fnoid = lfirst_oid(lc3);
if (!IsA(expr, Const))
{
@@ -2970,6 +2988,11 @@ analyze_partkey_exprs(PartitionedRelPruneInfo *pinfo, List *steps,
doruntimeprune = true;
}
+ else if (func_volatile(fnoid) != PROVOLATILE_IMMUTABLE)+ {+ doruntimeprune = true;+ pinfo->do_initial_prune = true;+ }
keyno++;
}
}
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex 3edbbc6dbb..ba6dd4734c 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -3727,3 +3727,20 @@ explain (costs off) update listp1 set a = 1 where a = 2;
reset constraint_exclusion;
reset enable_partition_pruning;
drop table listp;
+--+-- check that stable query clauses are only used in run-time pruning+--+create table stable_qual_pruning(a timestamp) partition by range (a);+create table stable_qual_pruning1 partition of stable_qual_pruning for values from ('2019-01-01') to ('2019-02-01');+create table stable_qual_pruning2 partition of stable_qual_pruning for values from ('2019-02-01') to ('2019-03-01');+-- timestamp < timestamptz comparison is only stable, not immutable+explain (analyze, costs off, summary off, timing off) select * from stable_qual_pruning where a < '2019-02-01'::timestamptz;+ QUERY PLAN +--------------------------------------------------------------------------------+ Append (actual rows=0 loops=1)+ Subplans Removed: 1+ -> Seq Scan on stable_qual_pruning1 (actual rows=0 loops=1)+ Filter: (a < 'Fri Feb 01 00:00:00 2019 PST'::timestamp with time zone)+(4 rows)++drop table stable_qual_pruning;diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sqlindex 66489dff63..fd3778aa18 100644--- a/src/test/regress/sql/partition_prune.sql+++ b/src/test/regress/sql/partition_prune.sql@@ -1019,3 +1019,13 @@ reset constraint_exclusion;
reset enable_partition_pruning;
drop table listp;
++--+-- check that stable query clauses are only used in run-time pruning+--+create table stable_qual_pruning(a timestamp) partition by range (a);+create table stable_qual_pruning1 partition of stable_qual_pruning for values from ('2019-01-01') to ('2019-02-01');+create table stable_qual_pruning2 partition of stable_qual_pruning for values from ('2019-02-01') to ('2019-03-01');+-- timestamp < timestamptz comparison is only stable, not immutable+explain (analyze, costs off, summary off, timing off) select * from stable_qual_pruning where a < '2019-02-01'::timestamptz;+drop table stable_qual_pruning;--
2.11.0
v2-0001-Add-test.patch (text/plain, 3.4K) download
From 345a7ca0fedb43f6a91a823469cfa9e95b36c1ae Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 10 May 2019 13:52:41 +0900
Subject: [PATCH v3 1/3] Add test
---
src/test/regress/expected/partition_prune.out | 31 +++++++++++++++++++++++++++
src/test/regress/sql/partition_prune.sql | 8 +++++++
2 files changed, 39 insertions(+)
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex bd64bed8fc..80dc700323 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -920,6 +920,37 @@ 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 (analyze, costs off, summary off, timing off) select * from mc3p where a = 1 and abs(b) < (select 2);+ QUERY PLAN +--------------------------------------------------------+ Append (actual rows=0 loops=1)+ InitPlan 1 (returns $0)+ -> Result (actual rows=1 loops=1)+ -> Seq Scan on mc3p0 (actual rows=0 loops=1)+ Filter: ((a = 1) AND (abs(b) < $0))+ -> Seq Scan on mc3p_default (actual rows=0 loops=1)+ Filter: ((a = 1) AND (abs(b) < $0))+(7 rows)++explain (analyze, costs off, summary off, timing off) select * from mc3p where a < (select 1) and abs(b) = 1;+ QUERY PLAN +--------------------------------------------------------+ Append (actual rows=0 loops=1)+ InitPlan 1 (returns $0)+ -> Result (actual rows=1 loops=1)+ -> Seq Scan on mc3p0 (actual rows=0 loops=1)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p1 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p_default (actual rows=0 loops=1)+ Filter: ((a < $0) AND (abs(b) = 1))+(9 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 246c6274af..66489dff63 100644--- a/src/test/regress/sql/partition_prune.sql+++ b/src/test/regress/sql/partition_prune.sql@@ -122,6 +122,14 @@ 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 (analyze, costs off, summary off, timing off) select * from mc3p where a = 1 and abs(b) < (select 2);+explain (analyze, costs off, summary off, timing off) select * from mc3p where a < (select 1) and abs(b) = 1;+
-- 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
v2-0002-Fix-bugs-in-pruning-with-composite-range-partitio.patch (text/plain, 7.7K) download
From c33d16f911b8e76b8b9a1eb6da756b69ba45bb48 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 10 May 2019 13:52:59 +0900
Subject: [PATCH v3 2/3] Fix bugs in pruning with composite range partition
keys
* Force using equality (= operator) semantics when comparing clause
expressions against partition bounds if the expression for the
last matched key cannot be evaluated in a given pruning context;
for example, if the expression is stable and pruning is invoked by
the planner
* Ignore keys after a given key for which we detected a clause
containing a non-inclusive operator
---
src/backend/partitioning/partprune.c | 88 +++++++++++++--------------
src/test/regress/expected/partition_prune.out | 18 +++++-
2 files changed, 58 insertions(+), 48 deletions(-)
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.cindex 59f34f5cac..92806ab9e1 100644--- a/src/backend/partitioning/partprune.c+++ b/src/backend/partitioning/partprune.c@@ -1178,17 +1178,24 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
List *opsteps = NIL;
List *btree_clauses[BTMaxStrategyNumber + 1],
*hash_clauses[HTMaxStrategyNumber + 1];
- bool need_next_less,- need_next_eq,- need_next_greater;+ bool consider_next_key;
int i;
+ consider_next_key = true;
memset(btree_clauses, 0, sizeof(btree_clauses));
memset(hash_clauses, 0, sizeof(hash_clauses));
for (i = 0; i < part_scheme->partnatts; i++)
{
List *clauselist = keyclauses[i];
- bool consider_next_key = true;++ /*+ * If the previous key was found to be compared using such an operator+ * that comparisons of subsequent key columns against partition bounds+ * that occurs during partition pruning would be redundant, then don't+ * bother generating pruning steps for them.+ */+ if (!consider_next_key)+ break;
/*
* To be useful for pruning, we must have clauses for a prefix of
@@ -1208,7 +1215,6 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
clauselist == NIL && !bms_is_member(i, nullkeys))
return NULL;
- need_next_eq = need_next_less = need_next_greater = true;
foreach(lc, clauselist)
{
PartClauseInfo *pc = (PartClauseInfo *) lfirst(lc);
@@ -1224,13 +1230,13 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
&lefttype,
&righttype);
+
switch (part_scheme->strategy)
{
case PARTITION_STRATEGY_LIST:
case PARTITION_STRATEGY_RANGE:
{
PartClauseInfo *last = NULL;
- bool inclusive = false;
/*
* Add this clause to the list of clauses to be used
@@ -1248,35 +1254,14 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
lappend(btree_clauses[pc->op_strategy], pc);
/*
- * We may not need the next clause if they're of- * certain strategy.+ * We won't need the clause for the next key if the+ * clause for the current key contains a non-inclusive+ * operator.
*/
- switch (pc->op_strategy)- {- case BTLessEqualStrategyNumber:- inclusive = true;- /* fall through */- case BTLessStrategyNumber:- if (!inclusive)- need_next_eq = need_next_less = false;- break;- case BTEqualStrategyNumber:- /* always accept clauses for the next key. */- break;- case BTGreaterEqualStrategyNumber:- inclusive = true;- /* fall through */- case BTGreaterStrategyNumber:- if (!inclusive)- need_next_eq = need_next_greater = false;- break;- }+ if (pc->op_strategy == BTLessStrategyNumber ||+ pc->op_strategy == BTGreaterStrategyNumber)+ consider_next_key = false;- /* We may want to change our mind. */- if (consider_next_key)- consider_next_key = (need_next_eq ||- need_next_less ||- need_next_greater);
break;
}
@@ -1293,13 +1278,6 @@ gen_prune_steps_from_opexps(PartitionScheme part_scheme,
break;
}
}
-- /*- * If we've decided that clauses for subsequent partition keys- * wouldn't be useful for pruning, don't search any further.- */- if (!consider_next_key)- break;
}
/*
@@ -2763,7 +2741,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
/*
* Look for the greatest bound that is < or <= lookup value and
- * set minoff to its offset.+ * set maxoff to its offset.
*/
off = partition_range_datum_bsearch(partsupfunc,
partcollation,
@@ -3133,11 +3111,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 80dc700323..3edbbc6dbb 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -933,9 +933,11 @@ explain (analyze, costs off, summary off, timing off) select * from mc3p where a
-> Result (actual rows=1 loops=1)
-> Seq Scan on mc3p0 (actual rows=0 loops=1)
Filter: ((a = 1) AND (abs(b) < $0))
+ -> Seq Scan on mc3p1 (actual rows=0 loops=1)+ Filter: ((a = 1) AND (abs(b) < $0))
-> Seq Scan on mc3p_default (actual rows=0 loops=1)
Filter: ((a = 1) AND (abs(b) < $0))
-(7 rows)+(9 rows)
explain (analyze, costs off, summary off, timing off) select * from mc3p where a < (select 1) and abs(b) = 1;
QUERY PLAN
@@ -947,9 +949,21 @@ explain (analyze, costs off, summary off, timing off) select * from mc3p where a
Filter: ((a < $0) AND (abs(b) = 1))
-> Seq Scan on mc3p1 (never executed)
Filter: ((a < $0) AND (abs(b) = 1))
+ -> Seq Scan on mc3p2 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p3 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p4 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p5 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p6 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))+ -> Seq Scan on mc3p7 (never executed)+ Filter: ((a < $0) AND (abs(b) = 1))
-> Seq Scan on mc3p_default (actual rows=0 loops=1)
Filter: ((a < $0) AND (abs(b) = 1))
-(9 rows)+(21 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, horiguchi.kyotaro@lab.ntt.co.jp, ajax@tvsquared.com, david.rowley@2ndquadrant.com
Subject: Re: inconsistent results querying table partitioned by date
In-Reply-To: <a0bca4f7-b493-4d0a-8080-8dad3748c5ae@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> <20190510.153337.236910404.horiguchi.kyotaro@lab.ntt.co.jp> <90aeaf38-270a-7e45-6b7d-af2e3d1e3719@lab.ntt.co.jp> <20307.1557522309@sss.pgh.pa.us> <a0bca4f7-b493-4d0a-8080-8dad3748c5ae@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