Commit c03911d9 authored by Dean Rasheed's avatar Dean Rasheed

Simplify the logic checking new range partition bounds.

The previous logic, whilst not actually wrong, was overly complex and
involved doing two binary searches, where only one was really
necessary. This simplifies that logic and improves the comments.

One visible change is that if the new partition overlaps multiple
existing partitions, the error message now always reports the overlap
with the first existing partition (the one with the lowest
bounds). The old code would sometimes report the clash with the first
partition and sometimes with the last one.

Original patch idea from Amit Langote, substantially rewritten by me.

Discussion: https://postgr.es/m/CAAJ_b947mowpLdxL3jo3YLKngRjrq9+Ej4ymduQTfYR+8=YAYQ@mail.gmail.com
parent ec86af91
...@@ -745,78 +745,64 @@ check_new_partition_bound(char *relname, Relation parent, ...@@ -745,78 +745,64 @@ check_new_partition_bound(char *relname, Relation parent,
if (partdesc->nparts > 0) if (partdesc->nparts > 0)
{ {
PartitionBoundInfo boundinfo = partdesc->boundinfo; PartitionBoundInfo boundinfo = partdesc->boundinfo;
int off1, int offset;
off2; bool equal;
bool equal = false;
Assert(boundinfo && boundinfo->ndatums > 0 && Assert(boundinfo && boundinfo->ndatums > 0 &&
boundinfo->strategy == PARTITION_STRATEGY_RANGE); boundinfo->strategy == PARTITION_STRATEGY_RANGE);
/* /*
* Firstly, find the greatest range bound that is less * Test whether the new lower bound (which is treated
* than or equal to the new lower bound. * inclusively as part of the new partition) lies inside an
* existing partition, or in a gap.
*
* If it's inside an existing partition, the bound at
* offset + 1 will be the upper bound of that partition,
* and its index will be >= 0.
*
* If it's in a gap, the bound at offset + 1 will be the
* lower bound of the next partition, and its index will be
* -1. This is also true if there is no next partition,
* since the index array is initialised with an extra -1 at
* the end.
*/ */
off1 = partition_bound_bsearch(key, boundinfo, lower, true, offset = partition_bound_bsearch(key, boundinfo, lower,
&equal); true, &equal);
if (boundinfo->indexes[offset + 1] < 0)
{
/* /*
* off1 == -1 means that all existing bounds are greater * Check that the new partition will fit in the gap.
* than the new lower bound. In that case and the case * For it to fit, the new upper bound must be less than
* where no partition is defined between the bounds at * or equal to the lower bound of the next partition,
* off1 and off1 + 1, we have a "gap" in the range that * if there is one.
* could be occupied by the new partition. We confirm if
* so by checking whether the new upper bound is confined
* within the gap.
*/ */
if (!equal && boundinfo->indexes[off1 + 1] < 0) if (offset + 1 < boundinfo->ndatums)
{ {
off2 = partition_bound_bsearch(key, boundinfo, upper, int32 cmpval;
true, &equal);
cmpval = partition_bound_cmp(key, boundinfo,
offset + 1, upper,
true);
if (cmpval < 0)
{
/* /*
* If the new upper bound is returned to be equal to * The new partition overlaps with the existing
* the bound at off2, the latter must be the upper * partition between offset + 1 and offset + 2.
* bound of some partition with which the new
* partition clearly overlaps.
*
* Also, if bound at off2 is not same as the one
* returned for the new lower bound (IOW, off1 !=
* off2), then the new partition overlaps at least one
* partition.
*/ */
if (equal || off1 != off2)
{
overlap = true; overlap = true;
with = boundinfo->indexes[offset + 2];
/* }
* The bound at off2 could be the lower bound of
* the partition with which the new partition
* overlaps. In that case, use the upper bound
* (that is, the bound at off2 + 1) to get the
* index of that partition.
*/
if (boundinfo->indexes[off2] < 0)
with = boundinfo->indexes[off2 + 1];
else
with = boundinfo->indexes[off2];
} }
} }
else else
{ {
/* /*
* Equal has been set to true and there is no "gap" * The new partition overlaps with the existing
* between the bound at off1 and that at off1 + 1, so * partition between offset and offset + 1.
* the new partition will overlap some partition. In
* the former case, the new lower bound is found to be
* equal to the bound at off1, which could only ever
* be true if the latter is the lower bound of some
* partition. It's clear in such a case that the new
* partition overlaps that partition, whose index we
* get using its upper bound (that is, using the bound
* at off1 + 1).
*/ */
overlap = true; overlap = true;
with = boundinfo->indexes[off1 + 1]; with = boundinfo->indexes[offset + 1];
} }
} }
......
...@@ -589,7 +589,7 @@ CREATE TABLE part3 PARTITION OF range_parted2 FOR VALUES FROM (30) TO (40); ...@@ -589,7 +589,7 @@ CREATE TABLE part3 PARTITION OF range_parted2 FOR VALUES FROM (30) TO (40);
CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (30); CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (30);
ERROR: partition "fail_part" would overlap partition "part2" ERROR: partition "fail_part" would overlap partition "part2"
CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (50); CREATE TABLE fail_part PARTITION OF range_parted2 FOR VALUES FROM (10) TO (50);
ERROR: partition "fail_part" would overlap partition "part3" ERROR: partition "fail_part" would overlap partition "part2"
-- now check for multi-column range partition key -- now check for multi-column range partition key
CREATE TABLE range_parted3 ( CREATE TABLE range_parted3 (
a int, a int,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment