@@ -741,37 +741,68 @@ check_new_partition_bound(char *relname, Relation parent, Node *bound)
741
741
boundinfo -> strategy == PARTITION_STRATEGY_RANGE );
742
742
743
743
/*
744
- * Find the greatest index of a range bound that is less
745
- * than or equal with the new lower bound.
744
+ * Firstly, find the greatest range bound that is less
745
+ * than or equal to the new lower bound.
746
746
*/
747
747
off1 = partition_bound_bsearch (key , boundinfo , lower , true,
748
748
& equal );
749
749
750
750
/*
751
- * If equal has been set to true, that means the new lower
752
- * bound is found to be equal with the bound at off1,
753
- * which clearly means an overlap with the partition at
754
- * index off1+1).
755
- *
756
- * Otherwise, check if there is a "gap" that could be
757
- * occupied by the new partition. In case of a gap, the
758
- * new upper bound should not cross past the upper
759
- * boundary of the gap, that is, off2 == off1 should be
760
- * true.
751
+ * off1 == -1 means that all existing bounds are greater
752
+ * than the new lower bound. In that case and the case
753
+ * where no partition is defined between the bounds at
754
+ * off1 and off1 + 1, we have a "gap" in the range that
755
+ * could be occupied by the new partition. We confirm if
756
+ * so by checking whether the new upper bound is confined
757
+ * within the gap.
761
758
*/
762
759
if (!equal && boundinfo -> indexes [off1 + 1 ] < 0 )
763
760
{
764
761
off2 = partition_bound_bsearch (key , boundinfo , upper ,
765
762
true, & equal );
766
763
764
+ /*
765
+ * If the new upper bound is returned to be equal to
766
+ * the bound at off2, the latter must be the upper
767
+ * bound of some partition with which the new
768
+ * partition clearly overlaps.
769
+ *
770
+ * Also, if bound at off2 is not same as the one
771
+ * returned for the new lower bound (IOW, off1 !=
772
+ * off2), then the new partition overlaps at least one
773
+ * partition.
774
+ */
767
775
if (equal || off1 != off2 )
768
776
{
769
777
overlap = true;
770
- with = boundinfo -> indexes [off2 + 1 ];
778
+
779
+ /*
780
+ * The bound at off2 could be the lower bound of
781
+ * the partition with which the new partition
782
+ * overlaps. In that case, use the upper bound
783
+ * (that is, the bound at off2 + 1) to get the
784
+ * index of that partition.
785
+ */
786
+ if (boundinfo -> indexes [off2 ] < 0 )
787
+ with = boundinfo -> indexes [off2 + 1 ];
788
+ else
789
+ with = boundinfo -> indexes [off2 ];
771
790
}
772
791
}
773
792
else
774
793
{
794
+ /*
795
+ * Equal has been set to true and there is no "gap"
796
+ * between the bound at off1 and that at off1 + 1, so
797
+ * the new partition will overlap some partition. In
798
+ * the former case, the new lower bound is found to be
799
+ * equal to the bound at off1, which could only ever
800
+ * be true if the latter is the lower bound of some
801
+ * partition. It's clear in such a case that the new
802
+ * partition overlaps that partition, whose index we
803
+ * get using its upper bound (that is, using the bound
804
+ * at off1 + 1).
805
+ */
775
806
overlap = true;
776
807
with = boundinfo -> indexes [off1 + 1 ];
777
808
}
@@ -1957,8 +1988,8 @@ partition_bound_cmp(PartitionKey key, PartitionBoundInfo boundinfo,
1957
1988
}
1958
1989
1959
1990
/*
1960
- * Binary search on a collection of partition bounds. Returns greatest index
1961
- * of bound in array boundinfo->datums which is less or equal with *probe.
1991
+ * Binary search on a collection of partition bounds. Returns greatest
1992
+ * bound in array boundinfo->datums which is less than or equal to *probe
1962
1993
* If all bounds in the array are greater than *probe, -1 is returned.
1963
1994
*
1964
1995
* *probe could either be a partition bound or a Datum array representing
@@ -1990,6 +2021,9 @@ partition_bound_bsearch(PartitionKey key, PartitionBoundInfo boundinfo,
1990
2021
{
1991
2022
lo = mid ;
1992
2023
* is_equal = (cmpval == 0 );
2024
+
2025
+ if (* is_equal )
2026
+ break ;
1993
2027
}
1994
2028
else
1995
2029
hi = mid - 1 ;
0 commit comments