@@ -950,7 +950,8 @@ RelationGetPartitionDispatchInfo(Relation rel, int lockmode,
950
950
* parted_rels ;
951
951
ListCell * lc ;
952
952
int i ,
953
- k ;
953
+ k ,
954
+ offset ;
954
955
955
956
/*
956
957
* Lock partitions and make a list of the partitioned ones to prepare
@@ -990,11 +991,19 @@ RelationGetPartitionDispatchInfo(Relation rel, int lockmode,
990
991
*/
991
992
}
992
993
993
- /* Generate PartitionDispatch objects for all partitioned tables */
994
+ /*
995
+ * We want to create two arrays - one for leaf partitions and another for
996
+ * partitioned tables (including the root table and internal partitions).
997
+ * While we only create the latter here, leaf partition array of suitable
998
+ * objects (such as, ResultRelInfo) is created by the caller using the
999
+ * list of OIDs we return. Indexes into these arrays get assigned in a
1000
+ * breadth-first manner, whereby partitions of any given level are placed
1001
+ * consecutively in the respective arrays.
1002
+ */
994
1003
pd = (PartitionDispatchData * * ) palloc (* num_parted *
995
1004
sizeof (PartitionDispatchData * ));
996
1005
* leaf_part_oids = NIL ;
997
- i = k = 0 ;
1006
+ i = k = offset = 0 ;
998
1007
foreach (lc , parted_rels )
999
1008
{
1000
1009
Relation partrel = lfirst (lc );
@@ -1010,6 +1019,16 @@ RelationGetPartitionDispatchInfo(Relation rel, int lockmode,
1010
1019
pd [i ]-> partdesc = partdesc ;
1011
1020
pd [i ]-> indexes = (int * ) palloc (partdesc -> nparts * sizeof (int ));
1012
1021
1022
+ /*
1023
+ * Indexes corresponding to the internal partitions are multiplied by
1024
+ * -1 to distinguish them from those of leaf partitions. Encountering
1025
+ * an index >= 0 means we found a leaf partition, which is immediately
1026
+ * returned as the partition we are looking for. A negative index
1027
+ * means we found a partitioned table, whose PartitionDispatch object
1028
+ * is located at the above index multiplied back by -1. Using the
1029
+ * PartitionDispatch object, search is continued further down the
1030
+ * partition tree.
1031
+ */
1013
1032
m = 0 ;
1014
1033
for (j = 0 ; j < partdesc -> nparts ; j ++ )
1015
1034
{
@@ -1023,14 +1042,22 @@ RelationGetPartitionDispatchInfo(Relation rel, int lockmode,
1023
1042
else
1024
1043
{
1025
1044
/*
1026
- * We can assign indexes this way because of the way
1027
- * parted_rels has been generated.
1045
+ * offset denotes the number of partitioned tables of upper
1046
+ * levels including those of the current level. Any partition
1047
+ * of this table must belong to the next level and hence will
1048
+ * be placed after the last partitioned table of this level.
1028
1049
*/
1029
- pd [i ]-> indexes [j ] = - (i + 1 + m );
1050
+ pd [i ]-> indexes [j ] = - (1 + offset + m );
1030
1051
m ++ ;
1031
1052
}
1032
1053
}
1033
1054
i ++ ;
1055
+
1056
+ /*
1057
+ * This counts the number of partitioned tables at upper levels
1058
+ * including those of the current level.
1059
+ */
1060
+ offset += m ;
1034
1061
}
1035
1062
1036
1063
return pd ;
0 commit comments