From: Yuzuko Hosoya <hosoya.yuzuko@lab.ntt.co.jp>
To: 'PostgreSQL Hackers' <pgsql-hackers@lists.postgresql.org>
Cc: 'Amit Langote' <Langote_Amit_f8@lab.ntt.co.jp>
Subject: Problem with default partition pruning
Date: Fri, 22 Feb 2019 17:14:04 +0900
Message-ID: <00e601d4ca86$932b8bc0$b982a340$@lab.ntt.co.jp> (raw)
Hi,
I found the bug of default partition pruning when executing a range query.
-----
postgres=# create table test1(id int, val text) partition by range (id);
postgres=# create table test1_1 partition of test1 for values from (0) to (100);
postgres=# create table test1_2 partition of test1 for values from (150) to (200);
postgres=# create table test1_def partition of test1 default;
postgres=# explain select * from test1 where id > 0 and id < 30;
QUERY PLAN
----------------------------------------------------------------
Append (cost=0.00..11.83 rows=59 width=11)
-> Seq Scan on test1_1 (cost=0.00..5.00 rows=58 width=11)
Filter: ((id > 0) AND (id < 30))
-> Seq Scan on test1_def (cost=0.00..6.53 rows=1 width=12)
Filter: ((id > 0) AND (id < 30))
(5 rows)
There is no need to scan the default partition, but it's scanned.
-----
In the current implement, whether the default partition is scanned
or not is determined according to each condition of given WHERE
clause at get_matching_range_bounds(). In this example, scan_default
is set true according to id > 0 because id >= 200 matches the default
partition. Similarly, according to id < 30, scan_default is set true.
Then, these results are combined according to AND/OR at perform_pruning_combine_step().
In this case, final result's scan_default is set true.
The modifications I made are as follows:
- get_matching_range_bounds() determines only offsets of range bounds
according to each condition
- These results are combined at perform_pruning_combine_step()
- Whether the default partition is scanned or not is determined at
get_matching_partitions()
Attached the patch. Any feedback is greatly appreciated.
Best regards,
---
Yuzuko Hosoya
NTT Open Source Software Center
Attachments:
[application/octet-stream] default_partition_pruning.patch (12.5K, ../00e601d4ca86$932b8bc0$b982a340$@lab.ntt.co.jp/2-default_partition_pruning.patch)
download | inline diff:diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.cindex 8c97219..d46a83c 100644--- a/src/backend/partitioning/partprune.c+++ b/src/backend/partitioning/partprune.c@@ -614,6 +614,7 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
PruneStepResult **results,
*final_result;
ListCell *lc;
+ bool scan_default;
/* If there are no pruning steps then all partitions match. */
if (num_steps == 0)
@@ -665,20 +666,23 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
Assert(final_result != NULL);
i = -1;
result = NULL;
+ scan_default = final_result->scan_default;
while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
{
int partindex = context->boundinfo->indexes[i];
/*
- * In range and hash partitioning cases, some slots may contain -1,+ * In range and hash partitioning cases, some index values may be -1,
* indicating that no partition has been defined to accept a given
- * range of data or for a given remainder, respectively. The default- * partition, if any, in case of range partitioning, will be added to- * the result, because the specified range still satisfies the query's- * conditions.+ * range of values (that is, the bound at given offset is the upper+ * bound of this unassigned range of values) or for a given remainder,+ * respectively. As it's still part of the queried range of values,+ * add the default partition, if any.
*/
if (partindex >= 0)
result = bms_add_member(result, partindex);
+ else+ scan_default |= partition_bound_has_default(context->boundinfo);
}
/* Add the null and/or default partition if needed and if present. */
@@ -688,7 +692,7 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
Assert(partition_bound_accepts_nulls(context->boundinfo));
result = bms_add_member(result, context->boundinfo->null_index);
}
- if (final_result->scan_default)+ if (scan_default)
{
Assert(context->strategy == PARTITION_STRATEGY_LIST ||
context->strategy == PARTITION_STRATEGY_RANGE);
@@ -2202,6 +2206,11 @@ get_matching_hash_bounds(PartitionPruneContext *context,
* get_matching_list_bounds
* Determine the offsets of list bounds matching the specified value,
* according to the semantics of the given operator strategy
+ *+ * scan_default will be set in the returned struct, if the default partition+ * needs to be scanned, provided one exists at all. scan_null will be set if+ * the special null-accepting partition needs to be scanned.+ *
* 'opstrategy' if non-zero must be a btree strategy number.
*
* 'value' contains the value to use for pruning.
@@ -2404,8 +2413,17 @@ get_matching_list_bounds(PartitionPruneContext *context,
* Each datum whose offset is in result is to be treated as the upper bound of
* the partition that will contain the desired values.
*
- * If default partition needs to be scanned for given values, set scan_default- * in result if present.+ * scan_default will be set in the returned struct, if the default partition+ * needs to be scanned, provided one exists at all. Although note that we+ * intentionally don't set scan_default in this function if only because the+ * matching set of values, found by comparing the input values using the+ * specified opstrategy, contains unassigned portions of key space, which+ * we normally assume to belong to the default partition. We don't infer+ * that until all steps have been executed, including any combination steps,+ * because even if such unassinged portion of key space appears to be part of+ * the set of queried values based on comparisons in the individual OpExprs,+ * the final set of bounds obtained after combining multiple OpExprs may+ * exclude it.
*
* 'opstrategy' if non-zero must be a btree strategy number.
*
@@ -2431,8 +2449,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
int *partindices = boundinfo->indexes;
int off,
minoff,
- maxoff,- i;+ maxoff;
bool is_equal;
bool inclusive = false;
@@ -2496,11 +2513,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
if (nvalues == partnatts)
{
/* There can only be zero or one matching partition. */
- if (partindices[off + 1] >= 0)- result->bound_offsets = bms_make_singleton(off + 1);- else- result->scan_default =- partition_bound_has_default(boundinfo);+ result->bound_offsets = bms_make_singleton(off + 1);
return result;
}
else
@@ -2588,57 +2601,21 @@ get_matching_range_bounds(PartitionPruneContext *context,
maxoff = off + 1;
}
- /*- * Skip if minoff/maxoff are actually the upper bound of a- * un-assigned portion of values.- */- if (partindices[minoff] < 0 && minoff < boundinfo->ndatums)- minoff++;- if (partindices[maxoff] < 0 && maxoff >= 1)- maxoff--;-- /*- * There may exist a range of values unassigned to any- * non-default partition between the datums at minoff and- * maxoff. Add the default partition in that case.- */- if (partition_bound_has_default(boundinfo))- {- for (i = minoff; i <= maxoff; i++)- {- if (partindices[i] < 0)- {- result->scan_default = true;- break;- }- }- }-
Assert(minoff >= 0 && maxoff >= 0);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
}
- else if (off >= 0) /* !is_equal */+ else
{
/*
* The lookup value falls in the range between some bounds in
* boundinfo. 'off' would be the offset of the greatest bound
* that is <= lookup value, so add off + 1 to the result
* instead as the offset of the upper bound of the only
- * partition that may contain the lookup value.+ * partition that may contain the lookup value. If 'off' is+ * -1 indicating that all bounds are greater, then we simply+ * end up adding the first bound's offset, that is, 0.
*/
- if (partindices[off + 1] >= 0)- result->bound_offsets = bms_make_singleton(off + 1);- else- result->scan_default =- partition_bound_has_default(boundinfo);- }- else- {- /*- * off < 0: the lookup value is smaller than all bounds, so- * only the default partition qualifies, if there is one.- */- result->scan_default = partition_bound_has_default(boundinfo);+ result->bound_offsets = bms_make_singleton(off + 1);
}
return result;
@@ -2736,16 +2713,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
boundinfo,
nvalues, values,
&is_equal);
- if (off < 0)- {- /*- * All bounds are greater than the key, so we could only- * expect to find the lookup key in the default partition.- */- result->scan_default = partition_bound_has_default(boundinfo);- return result;- }- else+ if (off >= 0)
{
/*
* See the comment above.
@@ -2786,9 +2754,11 @@ get_matching_range_bounds(PartitionPruneContext *context,
* instead as the offset of the upper bound of the greatest
* partition that may contain lookup value. If the lookup
* value had exactly matched the bound, but it isn't
- * inclusive, no need add the adjacent partition.+ * inclusive, no need add the adjacent partition. If 'off' is+ * -1 indicating that all bounds are greater, then we simply+ * end up adding the first bound's offset, that is, 0.
*/
- else if (!is_equal || inclusive)+ else if (off < 0 || !is_equal || inclusive)
maxoff = off + 1;
else
maxoff = off;
@@ -2800,58 +2770,43 @@ get_matching_range_bounds(PartitionPruneContext *context,
break;
}
+ Assert(minoff >= 0 && minoff <= boundinfo->ndatums);+ Assert(maxoff >= 0 && maxoff <= boundinfo->ndatums);+
/*
- * Skip a gap and when doing so, check if the bound contains a finite- * value to decide if we need to add the default partition. If it's an- * infinite bound, we need not add the default partition, as having an- * infinite bound means the partition in question catches any values that- * would otherwise be in the default partition.+ * If the smallest partition to return has MINVALUE (negative infinity) as+ * its lower bound, increment it to point to the next finite bound+ * (supposedly its upper bound), so that we don't advertently end up+ * scanning the default partition.
*/
- if (partindices[minoff] < 0)+ if (minoff < boundinfo->ndatums && partindices[minoff] < 0)
{
int lastkey = nvalues - 1;
- if (minoff >= 0 &&- minoff < boundinfo->ndatums &&- boundinfo->kind[minoff][lastkey] ==- PARTITION_RANGE_DATUM_VALUE)- result->scan_default = partition_bound_has_default(boundinfo);-- minoff++;+ if (boundinfo->kind[minoff][lastkey] ==+ PARTITION_RANGE_DATUM_MINVALUE)+ {+ minoff++;+ Assert(boundinfo->indexes[minoff] >= 0);+ }
}
/*
- * Skip a gap. See the above comment about how we decide whether or not- * to scan the default partition based whether the datum that will become- * the maximum datum is finite or not.+ * If the previous greatest partition has MAXVALUE (positive infinity) as+ * its upper bound (something only possible to do with multi-column range+ * partitioning), we scan switch to it as the greatest partition to+ * return. Again, so that we don't advertently end up scanning the+ * default partition.
*/
if (maxoff >= 1 && partindices[maxoff] < 0)
{
int lastkey = nvalues - 1;
- if (maxoff >= 0 &&- maxoff <= boundinfo->ndatums &&- boundinfo->kind[maxoff - 1][lastkey] ==- PARTITION_RANGE_DATUM_VALUE)- result->scan_default = partition_bound_has_default(boundinfo);-- maxoff--;- }-- if (partition_bound_has_default(boundinfo))- {- /*- * There may exist a range of values unassigned to any non-default- * partition between the datums at minoff and maxoff. Add the default- * partition in that case.- */- for (i = minoff; i <= maxoff; i++)+ if (boundinfo->kind[maxoff - 1][lastkey] ==+ PARTITION_RANGE_DATUM_MAXVALUE)
{
- if (partindices[i] < 0)- {- result->scan_default = true;- break;- }+ maxoff--;+ Assert(boundinfo->indexes[maxoff] >= 0);
}
}
@@ -3131,14 +3086,23 @@ perform_pruning_combine_step(PartitionPruneContext *context,
/*
* A combine step without any source steps is an indication to not perform
- * any partition pruning, we just return all partitions.+ * any partition pruning, we just return all datum offsets.
*/
result = (PruneStepResult *) palloc0(sizeof(PruneStepResult));
if (list_length(cstep->source_stepids) == 0)
{
PartitionBoundInfo boundinfo = context->boundinfo;
- result->bound_offsets = bms_add_range(NULL, 0, boundinfo->ndatums - 1);+ /*+ * Add all valid offsets into the boundinfo->indexes array. For range+ * partitioning, boundinfo->indexes contains (boundinfo->ndatums + 1)+ * valid entries.+ */+ if (context->strategy == PARTITION_STRATEGY_RANGE)+ result->bound_offsets = bms_add_range(NULL, 0, boundinfo->ndatums);+ else+ result->bound_offsets = bms_add_range(NULL, 0,+ boundinfo->ndatums - 1);
result->scan_default = partition_bound_has_default(boundinfo);
result->scan_null = partition_bound_accepts_nulls(boundinfo);
return result;
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.outindex 30946f7..046e722 100644--- a/src/test/regress/expected/partition_prune.out+++ b/src/test/regress/expected/partition_prune.out@@ -517,15 +517,13 @@ explain (costs off) select * from rlp where a <= 31;
Filter: (a <= 31)
-> Seq Scan on rlp5_1
Filter: (a <= 31)
- -> Seq Scan on rlp5_default- Filter: (a <= 31)
-> Seq Scan on rlp_default_10
Filter: (a <= 31)
-> Seq Scan on rlp_default_30
Filter: (a <= 31)
-> Seq Scan on rlp_default_default
Filter: (a <= 31)
-(29 rows)+(27 rows)
explain (costs off) select * from rlp where a = 1 or a = 7;
QUERY PLAN
@@ -573,11 +571,7 @@ explain (costs off) select * from rlp where a > 20 and a < 27;
Filter: ((a > 20) AND (a < 27))
-> Seq Scan on rlp4_2
Filter: ((a > 20) AND (a < 27))
- -> Seq Scan on rlp4_default- Filter: ((a > 20) AND (a < 27))- -> Seq Scan on rlp_default_default- Filter: ((a > 20) AND (a < 27))-(9 rows)+(5 rows)
explain (costs off) select * from rlp where a = 29;
QUERY PLAN
=
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: pgsql-hackers@postgresql.org
Cc: hosoya.yuzuko@lab.ntt.co.jp, pgsql-hackers@lists.postgresql.org, Langote_Amit_f8@lab.ntt.co.jp
Subject: Re: Problem with default partition pruning
In-Reply-To: <00e601d4ca86$932b8bc0$b982a340$@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