@@ -75,6 +75,7 @@ PG_FUNCTION_INFO_V1( on_partitions_created );
75
75
PG_FUNCTION_INFO_V1 (on_partitions_updated );
76
76
PG_FUNCTION_INFO_V1 (on_partitions_removed );
77
77
PG_FUNCTION_INFO_V1 (find_range_partition );
78
+ PG_FUNCTION_INFO_V1 (get_range_by_idx );
78
79
PG_FUNCTION_INFO_V1 (get_partition_range );
79
80
80
81
@@ -1162,8 +1163,8 @@ find_range_partition(PG_FUNCTION_ARGS)
1162
1163
/*
1163
1164
* Returns range (min, max) as output parameters
1164
1165
*
1165
- * first argument is parent relid
1166
- * second is partition relid
1166
+ * first argument isthe parent relid
1167
+ * second isthe partition relid
1167
1168
* third and forth are MIN and MAX output parameters
1168
1169
*/
1169
1170
Datum
@@ -1172,16 +1173,17 @@ get_partition_range(PG_FUNCTION_ARGS)
1172
1173
int parent_oid = DatumGetInt32 (PG_GETARG_DATUM (0 ));
1173
1174
int child_oid = DatumGetInt32 (PG_GETARG_DATUM (1 ));
1174
1175
int nelems = 2 ;
1175
- Datum * elems = palloc ( nelems * sizeof ( Datum )) ;
1176
+ Datum * elems ;
1176
1177
Oid elemtype = INT4OID ;
1177
1178
PartRelationInfo * prel ;
1178
1179
RangeRelation * rangerel ;
1179
1180
RangeEntry * ranges ;
1181
+ TypeCacheEntry * tce ;
1180
1182
bool found ;
1181
1183
int i ;
1182
1184
1183
1185
prel = (PartRelationInfo * )
1184
- hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,0 );
1186
+ hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,NULL );
1185
1187
1186
1188
rangerel = (RangeRelation * )
1187
1189
hash_search (range_restrictions , (const void * )& parent_oid ,HASH_FIND ,NULL );
@@ -1190,6 +1192,9 @@ get_partition_range(PG_FUNCTION_ARGS)
1190
1192
PG_RETURN_NULL ();
1191
1193
1192
1194
ranges = dsm_array_get_pointer (& rangerel -> ranges );
1195
+ tce = lookup_type_cache (prel -> atttype ,0 );
1196
+
1197
+ /* looking for specified partition */
1193
1198
for (i = 0 ;i < rangerel -> ranges .length ;i ++ )
1194
1199
if (ranges [i ].child_oid == child_oid )
1195
1200
{
@@ -1199,13 +1204,60 @@ get_partition_range(PG_FUNCTION_ARGS)
1199
1204
1200
1205
if (found )
1201
1206
{
1207
+ elems = palloc (nelems * sizeof (Datum ));
1202
1208
elems [0 ]= ranges [i ].min ;
1203
1209
elems [1 ]= ranges [i ].max ;
1204
1210
1205
1211
ArrayType * arr =
1206
- construct_array (elems ,nelems ,prel -> atttype ,sizeof (Oid ), true,'i' );
1212
+ construct_array (elems ,nelems ,prel -> atttype ,
1213
+ tce -> typlen ,tce -> typbyval ,tce -> typalign );
1207
1214
PG_RETURN_ARRAYTYPE_P (arr );
1208
1215
}
1209
1216
1210
1217
PG_RETURN_NULL ();
1211
1218
}
1219
+
1220
+
1221
+ /*
1222
+ * Returns N-th range (in form of array)
1223
+ *
1224
+ * First argument is the parent relid.
1225
+ * Second argument is the index of the range (if it is negative then the last
1226
+ * range will be returned).
1227
+ */
1228
+ Datum
1229
+ get_range_by_idx (PG_FUNCTION_ARGS )
1230
+ {
1231
+ int parent_oid = DatumGetInt32 (PG_GETARG_DATUM (0 ));
1232
+ int idx = DatumGetInt32 (PG_GETARG_DATUM (1 ));
1233
+ PartRelationInfo * prel ;
1234
+ RangeRelation * rangerel ;
1235
+ RangeEntry * ranges ;
1236
+ RangeEntry * re ;
1237
+ Datum * elems ;
1238
+ TypeCacheEntry * tce ;
1239
+
1240
+ prel = (PartRelationInfo * )
1241
+ hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,NULL );
1242
+
1243
+ rangerel = (RangeRelation * )
1244
+ hash_search (range_restrictions , (const void * )& parent_oid ,HASH_FIND ,NULL );
1245
+
1246
+ if (!prel || !rangerel || idx >= (int )rangerel -> ranges .length )
1247
+ PG_RETURN_NULL ();
1248
+
1249
+ tce = lookup_type_cache (prel -> atttype ,0 );
1250
+ ranges = dsm_array_get_pointer (& rangerel -> ranges );
1251
+ if (idx >=0 )
1252
+ re = & ranges [idx ];
1253
+ else
1254
+ re = & ranges [rangerel -> ranges .length - 1 ];
1255
+
1256
+ elems = palloc (2 * sizeof (Datum ));
1257
+ elems [0 ]= re -> min ;
1258
+ elems [1 ]= re -> max ;
1259
+
1260
+ PG_RETURN_ARRAYTYPE_P (
1261
+ construct_array (elems ,2 ,prel -> atttype ,
1262
+ tce -> typlen ,tce -> typbyval ,tce -> typalign ));
1263
+ }