@@ -92,7 +92,6 @@ static int partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
92
92
Oid * partcollation ,
93
93
PartitionBoundInfo boundinfo ,
94
94
PartitionRangeBound * probe , bool * is_equal );
95
- static int get_partition_bound_num_indexes (PartitionBoundInfo b );
96
95
static Expr * make_partition_op_expr (PartitionKey key , int keynum ,
97
96
uint16 strategy , Expr * arg1 , Expr * arg2 );
98
97
static Oid get_partition_operator (PartitionKey key , int col ,
@@ -266,6 +265,7 @@ create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts,
266
265
267
266
boundinfo -> ndatums = ndatums ;
268
267
boundinfo -> datums = (Datum * * ) palloc0 (ndatums * sizeof (Datum * ));
268
+ boundinfo -> nindexes = greatest_modulus ;
269
269
boundinfo -> indexes = (int * ) palloc (greatest_modulus * sizeof (int ));
270
270
for (i = 0 ; i < greatest_modulus ; i ++ )
271
271
boundinfo -> indexes [i ] = -1 ;
@@ -398,6 +398,7 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts,
398
398
399
399
boundinfo -> ndatums = ndatums ;
400
400
boundinfo -> datums = (Datum * * ) palloc0 (ndatums * sizeof (Datum * ));
401
+ boundinfo -> nindexes = ndatums ;
401
402
boundinfo -> indexes = (int * ) palloc (ndatums * sizeof (int ));
402
403
403
404
/*
@@ -593,8 +594,9 @@ create_range_bounds(PartitionBoundSpec **boundspecs, int nparts,
593
594
594
595
/*
595
596
* For range partitioning, an additional value of -1 is stored as the last
596
- * element.
597
+ * element of the indexes[] array .
597
598
*/
599
+ boundinfo -> nindexes = ndatums + 1 ;
598
600
boundinfo -> indexes = (int * ) palloc ((ndatums + 1 ) * sizeof (int ));
599
601
600
602
for (i = 0 ; i < ndatums ; i ++ )
@@ -675,45 +677,41 @@ partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
675
677
if (b1 -> ndatums != b2 -> ndatums )
676
678
return false;
677
679
680
+ if (b1 -> nindexes != b2 -> nindexes )
681
+ return false;
682
+
678
683
if (b1 -> null_index != b2 -> null_index )
679
684
return false;
680
685
681
686
if (b1 -> default_index != b2 -> default_index )
682
687
return false;
683
688
684
- if (b1 -> strategy == PARTITION_STRATEGY_HASH )
689
+ /* For all partition strategies, the indexes[] arrays have to match */
690
+ for (i = 0 ; i < b1 -> nindexes ; i ++ )
685
691
{
686
- int greatest_modulus = get_hash_partition_greatest_modulus (b1 );
687
-
688
- /*
689
- * If two hash partitioned tables have different greatest moduli,
690
- * their partition schemes don't match.
691
- */
692
- if (greatest_modulus != get_hash_partition_greatest_modulus (b2 ))
692
+ if (b1 -> indexes [i ] != b2 -> indexes [i ])
693
693
return false;
694
+ }
694
695
696
+ /* Finally, compare the datums[] arrays */
697
+ if (b1 -> strategy == PARTITION_STRATEGY_HASH )
698
+ {
695
699
/*
696
700
* We arrange the partitions in the ascending order of their moduli
697
701
* and remainders. Also every modulus is factor of next larger
698
702
* modulus. Therefore we can safely store index of a given partition
699
703
* in indexes array at remainder of that partition. Also entries at
700
704
* (remainder + N * modulus) positions in indexes array are all same
701
- * for (modulus, remainder) specification for any partition. Thus
702
- * datums array from both the given bounds are same, if and only if
703
- * their indexes array will be same. So, it suffices to compare
704
- * indexes array.
705
- */
706
- for (i = 0 ; i < greatest_modulus ; i ++ )
707
- if (b1 -> indexes [i ] != b2 -> indexes [i ])
708
- return false;
709
-
710
- #ifdef USE_ASSERT_CHECKING
711
-
712
- /*
713
- * Nonetheless make sure that the bounds are indeed same when the
705
+ * for (modulus, remainder) specification for any partition. Thus the
706
+ * datums arrays from the given bounds are the same, if and only if
707
+ * their indexes arrays are the same. So, it suffices to compare the
708
+ * indexes arrays.
709
+ *
710
+ * Nonetheless make sure that the bounds are indeed the same when the
714
711
* indexes match. Hash partition bound stores modulus and remainder
715
712
* at b1->datums[i][0] and b1->datums[i][1] position respectively.
716
713
*/
714
+ #ifdef USE_ASSERT_CHECKING
717
715
for (i = 0 ; i < b1 -> ndatums ; i ++ )
718
716
Assert ((b1 -> datums [i ][0 ] == b2 -> datums [i ][0 ] &&
719
717
b1 -> datums [i ][1 ] == b2 -> datums [i ][1 ]));
@@ -759,15 +757,7 @@ partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
759
757
parttypbyval [j ], parttyplen [j ]))
760
758
return false;
761
759
}
762
-
763
- if (b1 -> indexes [i ] != b2 -> indexes [i ])
764
- return false;
765
760
}
766
-
767
- /* There are ndatums+1 indexes in case of range partitions */
768
- if (b1 -> strategy == PARTITION_STRATEGY_RANGE &&
769
- b1 -> indexes [i ] != b2 -> indexes [i ])
770
- return false;
771
761
}
772
762
return true;
773
763
}
@@ -783,17 +773,16 @@ partition_bounds_copy(PartitionBoundInfo src,
783
773
PartitionBoundInfo dest ;
784
774
int i ;
785
775
int ndatums ;
776
+ int nindexes ;
786
777
int partnatts ;
787
- int num_indexes ;
788
778
789
779
dest = (PartitionBoundInfo ) palloc (sizeof (PartitionBoundInfoData ));
790
780
791
781
dest -> strategy = src -> strategy ;
792
782
ndatums = dest -> ndatums = src -> ndatums ;
783
+ nindexes = dest -> nindexes = src -> nindexes ;
793
784
partnatts = key -> partnatts ;
794
785
795
- num_indexes = get_partition_bound_num_indexes (src );
796
-
797
786
/* List partitioned tables have only a single partition key. */
798
787
Assert (key -> strategy != PARTITION_STRATEGY_LIST || partnatts == 1 );
799
788
@@ -851,8 +840,8 @@ partition_bounds_copy(PartitionBoundInfo src,
851
840
}
852
841
}
853
842
854
- dest -> indexes = (int * ) palloc (sizeof (int ) * num_indexes );
855
- memcpy (dest -> indexes , src -> indexes , sizeof (int ) * num_indexes );
843
+ dest -> indexes = (int * ) palloc (sizeof (int ) * nindexes );
844
+ memcpy (dest -> indexes , src -> indexes , sizeof (int ) * nindexes );
856
845
857
846
dest -> null_index = src -> null_index ;
858
847
dest -> default_index = src -> default_index ;
@@ -1016,7 +1005,7 @@ check_new_partition_bound(char *relname, Relation parent,
1016
1005
(errcode (ERRCODE_INVALID_OBJECT_DEFINITION ),
1017
1006
errmsg ("every hash partition modulus must be a factor of the next larger modulus" )));
1018
1007
1019
- greatest_modulus = get_hash_partition_greatest_modulus ( boundinfo ) ;
1008
+ greatest_modulus = boundinfo -> nindexes ;
1020
1009
remainder = spec -> remainder ;
1021
1010
1022
1011
/*
@@ -1380,18 +1369,15 @@ check_default_partition_contents(Relation parent, Relation default_rel,
1380
1369
/*
1381
1370
* get_hash_partition_greatest_modulus
1382
1371
*
1383
- * Returns the greatest modulus of the hash partition bound. The greatest
1384
- * modulus will be at the end of the datums array because hash partitions are
1385
- * arranged in the ascending order of their moduli and remainders .
1372
+ * Returns the greatest modulus of the hash partition bound.
1373
+ * This is no longer used in the core code, but we keep it around
1374
+ * in case external modules are using it .
1386
1375
*/
1387
1376
int
1388
1377
get_hash_partition_greatest_modulus (PartitionBoundInfo bound )
1389
1378
{
1390
1379
Assert (bound && bound -> strategy == PARTITION_STRATEGY_HASH );
1391
- Assert (bound -> datums && bound -> ndatums > 0 );
1392
- Assert (DatumGetInt32 (bound -> datums [bound -> ndatums - 1 ][0 ]) > 0 );
1393
-
1394
- return DatumGetInt32 (bound -> datums [bound -> ndatums - 1 ][0 ]);
1380
+ return bound -> nindexes ;
1395
1381
}
1396
1382
1397
1383
/*
@@ -1788,46 +1774,6 @@ qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
1788
1774
b1 -> lower , b2 );
1789
1775
}
1790
1776
1791
- /*
1792
- * get_partition_bound_num_indexes
1793
- *
1794
- * Returns the number of the entries in the partition bound indexes array.
1795
- */
1796
- static int
1797
- get_partition_bound_num_indexes (PartitionBoundInfo bound )
1798
- {
1799
- int num_indexes ;
1800
-
1801
- Assert (bound );
1802
-
1803
- switch (bound -> strategy )
1804
- {
1805
- case PARTITION_STRATEGY_HASH :
1806
-
1807
- /*
1808
- * The number of the entries in the indexes array is same as the
1809
- * greatest modulus.
1810
- */
1811
- num_indexes = get_hash_partition_greatest_modulus (bound );
1812
- break ;
1813
-
1814
- case PARTITION_STRATEGY_LIST :
1815
- num_indexes = bound -> ndatums ;
1816
- break ;
1817
-
1818
- case PARTITION_STRATEGY_RANGE :
1819
- /* Range partitioned table has an extra index. */
1820
- num_indexes = bound -> ndatums + 1 ;
1821
- break ;
1822
-
1823
- default :
1824
- elog (ERROR , "unexpected partition strategy: %d" ,
1825
- (int ) bound -> strategy );
1826
- }
1827
-
1828
- return num_indexes ;
1829
- }
1830
-
1831
1777
/*
1832
1778
* get_partition_operator
1833
1779
*
0 commit comments