@@ -224,7 +224,6 @@ static int partition_range_bsearch(int partnatts, FmgrInfo *partsupfunc,
224
224
Oid * partcollation ,
225
225
PartitionBoundInfo boundinfo ,
226
226
PartitionRangeBound * probe , int32 * cmpval );
227
- static int get_partition_bound_num_indexes (PartitionBoundInfo b );
228
227
static Expr * make_partition_op_expr (PartitionKey key , int keynum ,
229
228
uint16 strategy , Expr * arg1 , Expr * arg2 );
230
229
static Oid get_partition_operator (PartitionKey key , int col ,
@@ -398,6 +397,7 @@ create_hash_bounds(PartitionBoundSpec **boundspecs, int nparts,
398
397
399
398
boundinfo -> ndatums = ndatums ;
400
399
boundinfo -> datums = (Datum * * ) palloc0 (ndatums * sizeof (Datum * ));
400
+ boundinfo -> nindexes = greatest_modulus ;
401
401
boundinfo -> indexes = (int * ) palloc (greatest_modulus * sizeof (int ));
402
402
for (i = 0 ; i < greatest_modulus ; i ++ )
403
403
boundinfo -> indexes [i ] = -1 ;
@@ -530,6 +530,7 @@ create_list_bounds(PartitionBoundSpec **boundspecs, int nparts,
530
530
531
531
boundinfo -> ndatums = ndatums ;
532
532
boundinfo -> datums = (Datum * * ) palloc0 (ndatums * sizeof (Datum * ));
533
+ boundinfo -> nindexes = ndatums ;
533
534
boundinfo -> indexes = (int * ) palloc (ndatums * sizeof (int ));
534
535
535
536
/*
@@ -725,8 +726,9 @@ create_range_bounds(PartitionBoundSpec **boundspecs, int nparts,
725
726
726
727
/*
727
728
* For range partitioning, an additional value of -1 is stored as the last
728
- * element.
729
+ * element of the indexes[] array .
729
730
*/
731
+ boundinfo -> nindexes = ndatums + 1 ;
730
732
boundinfo -> indexes = (int * ) palloc ((ndatums + 1 ) * sizeof (int ));
731
733
732
734
for (i = 0 ; i < ndatums ; i ++ )
@@ -807,45 +809,41 @@ partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
807
809
if (b1 -> ndatums != b2 -> ndatums )
808
810
return false;
809
811
812
+ if (b1 -> nindexes != b2 -> nindexes )
813
+ return false;
814
+
810
815
if (b1 -> null_index != b2 -> null_index )
811
816
return false;
812
817
813
818
if (b1 -> default_index != b2 -> default_index )
814
819
return false;
815
820
816
- if (b1 -> strategy == PARTITION_STRATEGY_HASH )
821
+ /* For all partition strategies, the indexes[] arrays have to match */
822
+ for (i = 0 ; i < b1 -> nindexes ; i ++ )
817
823
{
818
- int greatest_modulus = get_hash_partition_greatest_modulus (b1 );
819
-
820
- /*
821
- * If two hash partitioned tables have different greatest moduli,
822
- * their partition schemes don't match.
823
- */
824
- if (greatest_modulus != get_hash_partition_greatest_modulus (b2 ))
824
+ if (b1 -> indexes [i ] != b2 -> indexes [i ])
825
825
return false;
826
+ }
826
827
828
+ /* Finally, compare the datums[] arrays */
829
+ if (b1 -> strategy == PARTITION_STRATEGY_HASH )
830
+ {
827
831
/*
828
832
* We arrange the partitions in the ascending order of their moduli
829
833
* and remainders. Also every modulus is factor of next larger
830
834
* modulus. Therefore we can safely store index of a given partition
831
835
* in indexes array at remainder of that partition. Also entries at
832
836
* (remainder + N * modulus) positions in indexes array are all same
833
- * for (modulus, remainder) specification for any partition. Thus
834
- * datums array from both the given bounds are same, if and only if
835
- * their indexes array will be same. So, it suffices to compare
836
- * indexes array.
837
- */
838
- for (i = 0 ; i < greatest_modulus ; i ++ )
839
- if (b1 -> indexes [i ] != b2 -> indexes [i ])
840
- return false;
841
-
842
- #ifdef USE_ASSERT_CHECKING
843
-
844
- /*
845
- * Nonetheless make sure that the bounds are indeed same when the
837
+ * for (modulus, remainder) specification for any partition. Thus the
838
+ * datums arrays from the given bounds are the same, if and only if
839
+ * their indexes arrays are the same. So, it suffices to compare the
840
+ * indexes arrays.
841
+ *
842
+ * Nonetheless make sure that the bounds are indeed the same when the
846
843
* indexes match. Hash partition bound stores modulus and remainder
847
844
* at b1->datums[i][0] and b1->datums[i][1] position respectively.
848
845
*/
846
+ #ifdef USE_ASSERT_CHECKING
849
847
for (i = 0 ; i < b1 -> ndatums ; i ++ )
850
848
Assert ((b1 -> datums [i ][0 ] == b2 -> datums [i ][0 ] &&
851
849
b1 -> datums [i ][1 ] == b2 -> datums [i ][1 ]));
@@ -891,15 +889,7 @@ partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval,
891
889
parttypbyval [j ], parttyplen [j ]))
892
890
return false;
893
891
}
894
-
895
- if (b1 -> indexes [i ] != b2 -> indexes [i ])
896
- return false;
897
892
}
898
-
899
- /* There are ndatums+1 indexes in case of range partitions */
900
- if (b1 -> strategy == PARTITION_STRATEGY_RANGE &&
901
- b1 -> indexes [i ] != b2 -> indexes [i ])
902
- return false;
903
893
}
904
894
return true;
905
895
}
@@ -920,19 +910,18 @@ partition_bounds_copy(PartitionBoundInfo src,
920
910
PartitionBoundInfo dest ;
921
911
int i ;
922
912
int ndatums ;
913
+ int nindexes ;
923
914
int partnatts ;
924
- int num_indexes ;
925
915
bool hash_part ;
926
916
int natts ;
927
917
928
918
dest = (PartitionBoundInfo ) palloc (sizeof (PartitionBoundInfoData ));
929
919
930
920
dest -> strategy = src -> strategy ;
931
921
ndatums = dest -> ndatums = src -> ndatums ;
922
+ nindexes = dest -> nindexes = src -> nindexes ;
932
923
partnatts = key -> partnatts ;
933
924
934
- num_indexes = get_partition_bound_num_indexes (src );
935
-
936
925
/* List partitioned tables have only a single partition key. */
937
926
Assert (key -> strategy != PARTITION_STRATEGY_LIST || partnatts == 1 );
938
927
@@ -990,8 +979,8 @@ partition_bounds_copy(PartitionBoundInfo src,
990
979
}
991
980
}
992
981
993
- dest -> indexes = (int * ) palloc (sizeof (int ) * num_indexes );
994
- memcpy (dest -> indexes , src -> indexes , sizeof (int ) * num_indexes );
982
+ dest -> indexes = (int * ) palloc (sizeof (int ) * nindexes );
983
+ memcpy (dest -> indexes , src -> indexes , sizeof (int ) * nindexes );
995
984
996
985
dest -> null_index = src -> null_index ;
997
986
dest -> default_index = src -> default_index ;
@@ -2456,6 +2445,7 @@ build_merged_partition_bounds(char strategy, List *merged_datums,
2456
2445
}
2457
2446
2458
2447
Assert (list_length (merged_indexes ) == ndatums );
2448
+ merged_bounds -> nindexes = ndatums ;
2459
2449
merged_bounds -> indexes = (int * ) palloc (sizeof (int ) * ndatums );
2460
2450
pos = 0 ;
2461
2451
foreach (lc , merged_indexes )
@@ -2889,7 +2879,7 @@ check_new_partition_bound(char *relname, Relation parent,
2889
2879
(errcode (ERRCODE_INVALID_OBJECT_DEFINITION ),
2890
2880
errmsg ("every hash partition modulus must be a factor of the next larger modulus" )));
2891
2881
2892
- greatest_modulus = get_hash_partition_greatest_modulus ( boundinfo ) ;
2882
+ greatest_modulus = boundinfo -> nindexes ;
2893
2883
remainder = spec -> remainder ;
2894
2884
2895
2885
/*
@@ -3282,18 +3272,15 @@ check_default_partition_contents(Relation parent, Relation default_rel,
3282
3272
/*
3283
3273
* get_hash_partition_greatest_modulus
3284
3274
*
3285
- * Returns the greatest modulus of the hash partition bound. The greatest
3286
- * modulus will be at the end of the datums array because hash partitions are
3287
- * arranged in the ascending order of their moduli and remainders .
3275
+ * Returns the greatest modulus of the hash partition bound.
3276
+ * This is no longer used in the core code, but we keep it around
3277
+ * in case external modules are using it .
3288
3278
*/
3289
3279
int
3290
3280
get_hash_partition_greatest_modulus (PartitionBoundInfo bound )
3291
3281
{
3292
3282
Assert (bound && bound -> strategy == PARTITION_STRATEGY_HASH );
3293
- Assert (bound -> datums && bound -> ndatums > 0 );
3294
- Assert (DatumGetInt32 (bound -> datums [bound -> ndatums - 1 ][0 ]) > 0 );
3295
-
3296
- return DatumGetInt32 (bound -> datums [bound -> ndatums - 1 ][0 ]);
3283
+ return bound -> nindexes ;
3297
3284
}
3298
3285
3299
3286
/*
@@ -3697,46 +3684,6 @@ qsort_partition_rbound_cmp(const void *a, const void *b, void *arg)
3697
3684
b1 , b2 );
3698
3685
}
3699
3686
3700
- /*
3701
- * get_partition_bound_num_indexes
3702
- *
3703
- * Returns the number of the entries in the partition bound indexes array.
3704
- */
3705
- static int
3706
- get_partition_bound_num_indexes (PartitionBoundInfo bound )
3707
- {
3708
- int num_indexes ;
3709
-
3710
- Assert (bound );
3711
-
3712
- switch (bound -> strategy )
3713
- {
3714
- case PARTITION_STRATEGY_HASH :
3715
-
3716
- /*
3717
- * The number of the entries in the indexes array is same as the
3718
- * greatest modulus.
3719
- */
3720
- num_indexes = get_hash_partition_greatest_modulus (bound );
3721
- break ;
3722
-
3723
- case PARTITION_STRATEGY_LIST :
3724
- num_indexes = bound -> ndatums ;
3725
- break ;
3726
-
3727
- case PARTITION_STRATEGY_RANGE :
3728
- /* Range partitioned table has an extra index. */
3729
- num_indexes = bound -> ndatums + 1 ;
3730
- break ;
3731
-
3732
- default :
3733
- elog (ERROR , "unexpected partition strategy: %d" ,
3734
- (int ) bound -> strategy );
3735
- }
3736
-
3737
- return num_indexes ;
3738
- }
3739
-
3740
3687
/*
3741
3688
* get_partition_operator
3742
3689
*
0 commit comments