@@ -75,6 +75,7 @@ PG_FUNCTION_INFO_V1( on_partitions_created );
7575PG_FUNCTION_INFO_V1 (on_partitions_updated );
7676PG_FUNCTION_INFO_V1 (on_partitions_removed );
7777PG_FUNCTION_INFO_V1 (find_range_partition );
78+ PG_FUNCTION_INFO_V1 (get_range_by_idx );
7879PG_FUNCTION_INFO_V1 (get_partition_range );
7980
8081
@@ -1162,8 +1163,8 @@ find_range_partition(PG_FUNCTION_ARGS)
11621163/*
11631164 * Returns range (min, max) as output parameters
11641165 *
1165- * first argument is parent relid
1166- * second is partition relid
1166+ * first argument isthe parent relid
1167+ * second isthe partition relid
11671168 * third and forth are MIN and MAX output parameters
11681169 */
11691170Datum
@@ -1172,16 +1173,17 @@ get_partition_range(PG_FUNCTION_ARGS)
11721173int parent_oid = DatumGetInt32 (PG_GETARG_DATUM (0 ));
11731174int child_oid = DatumGetInt32 (PG_GETARG_DATUM (1 ));
11741175int nelems = 2 ;
1175- Datum * elems = palloc ( nelems * sizeof ( Datum )) ;
1176+ Datum * elems ;
11761177Oid elemtype = INT4OID ;
11771178PartRelationInfo * prel ;
11781179RangeRelation * rangerel ;
11791180RangeEntry * ranges ;
1181+ TypeCacheEntry * tce ;
11801182bool found ;
11811183int i ;
11821184
11831185prel = (PartRelationInfo * )
1184- hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,0 );
1186+ hash_search (relations , (const void * )& parent_oid ,HASH_FIND ,NULL );
11851187
11861188rangerel = (RangeRelation * )
11871189hash_search (range_restrictions , (const void * )& parent_oid ,HASH_FIND ,NULL );
@@ -1190,6 +1192,9 @@ get_partition_range(PG_FUNCTION_ARGS)
11901192PG_RETURN_NULL ();
11911193
11921194ranges = dsm_array_get_pointer (& rangerel -> ranges );
1195+ tce = lookup_type_cache (prel -> atttype ,0 );
1196+
1197+ /* looking for specified partition */
11931198for (i = 0 ;i < rangerel -> ranges .length ;i ++ )
11941199if (ranges [i ].child_oid == child_oid )
11951200{
@@ -1199,13 +1204,60 @@ get_partition_range(PG_FUNCTION_ARGS)
11991204
12001205if (found )
12011206{
1207+ elems = palloc (nelems * sizeof (Datum ));
12021208elems [0 ]= ranges [i ].min ;
12031209elems [1 ]= ranges [i ].max ;
12041210
12051211ArrayType * 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 );
12071214PG_RETURN_ARRAYTYPE_P (arr );
12081215}
12091216
12101217PG_RETURN_NULL ();
12111218}
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+ }