Skip to content

Commit 3cebefb

Browse files
committed
pathman:
* append and prepend functions
1 parent af586f4 commit 3cebefb

File tree

4 files changed

+230
-292
lines changed

4 files changed

+230
-292
lines changed

contrib/pathman/pathman.c

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ PG_FUNCTION_INFO_V1( on_partitions_created );
7575
PG_FUNCTION_INFO_V1( on_partitions_updated );
7676
PG_FUNCTION_INFO_V1( on_partitions_removed );
7777
PG_FUNCTION_INFO_V1( find_range_partition );
78+
PG_FUNCTION_INFO_V1( get_range_by_idx );
7879
PG_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 is the parent relid
1167+
* second is the partition relid
11671168
* third and forth are MIN and MAX output parameters
11681169
*/
11691170
Datum
@@ -1172,16 +1173,17 @@ get_partition_range(PG_FUNCTION_ARGS)
11721173
int parent_oid = DatumGetInt32(PG_GETARG_DATUM(0));
11731174
int child_oid = DatumGetInt32(PG_GETARG_DATUM(1));
11741175
int nelems = 2;
1175-
Datum *elems = palloc(nelems * sizeof(Datum));
1176+
Datum *elems;
11761177
Oid elemtype = INT4OID;
11771178
PartRelationInfo *prel;
11781179
RangeRelation *rangerel;
11791180
RangeEntry *ranges;
1181+
TypeCacheEntry *tce;
11801182
bool found;
11811183
int i;
11821184

11831185
prel = (PartRelationInfo *)
1184-
hash_search(relations, (const void *) &parent_oid, HASH_FIND, 0);
1186+
hash_search(relations, (const void *) &parent_oid, HASH_FIND, NULL);
11851187

11861188
rangerel = (RangeRelation *)
11871189
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
@@ -1190,6 +1192,9 @@ get_partition_range(PG_FUNCTION_ARGS)
11901192
PG_RETURN_NULL();
11911193

11921194
ranges = dsm_array_get_pointer(&rangerel->ranges);
1195+
tce = lookup_type_cache(prel->atttype, 0);
1196+
1197+
/* looking for specified partition */
11931198
for(i=0; i<rangerel->ranges.length; i++)
11941199
if (ranges[i].child_oid == child_oid)
11951200
{
@@ -1199,13 +1204,60 @@ get_partition_range(PG_FUNCTION_ARGS)
11991204

12001205
if (found)
12011206
{
1207+
elems = palloc(nelems * sizeof(Datum));
12021208
elems[0] = ranges[i].min;
12031209
elems[1] = ranges[i].max;
12041210

12051211
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);
12071214
PG_RETURN_ARRAYTYPE_P(arr);
12081215
}
12091216

12101217
PG_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+
}

contrib/pathman/results/pathman.out

Lines changed: 0 additions & 239 deletions
This file was deleted.

0 commit comments

Comments
 (0)