Skip to content

Commit e39af82

Browse files
committed
pathman:
* split partition function * PL functions enchancement
1 parent c36721e commit e39af82

File tree

3 files changed

+307
-234
lines changed

3 files changed

+307
-234
lines changed

contrib/pathman/pathman.c

Lines changed: 52 additions & 0 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_partition_range );
7879

7980

8081
/*
@@ -1162,3 +1163,54 @@ find_range_partition(PG_FUNCTION_ARGS)
11621163

11631164
PG_RETURN_NULL();
11641165
}
1166+
1167+
/*
1168+
* Returns range (min, max) as output parameters
1169+
*
1170+
* first argument is parent relid
1171+
* second is partition relid
1172+
* third and forth are MIN and MAX output parameters
1173+
*/
1174+
Datum
1175+
get_partition_range(PG_FUNCTION_ARGS)
1176+
{
1177+
int parent_oid = DatumGetInt32(PG_GETARG_DATUM(0));
1178+
int child_oid = DatumGetInt32(PG_GETARG_DATUM(1));
1179+
int nelems = 2;
1180+
Datum *elems = palloc(nelems * sizeof(Datum));
1181+
Oid elemtype = INT4OID;
1182+
PartRelationInfo *prel;
1183+
RangeRelation *rangerel;
1184+
RangeEntry *ranges;
1185+
bool found;
1186+
int i;
1187+
1188+
prel = (PartRelationInfo *)
1189+
hash_search(relations, (const void *) &parent_oid, HASH_FIND, 0);
1190+
1191+
rangerel = (RangeRelation *)
1192+
hash_search(range_restrictions, (const void *) &parent_oid, HASH_FIND, NULL);
1193+
1194+
if (!prel || !rangerel)
1195+
PG_RETURN_NULL();
1196+
1197+
ranges = dsm_array_get_pointer(&rangerel->ranges);
1198+
for(i=0; i<rangerel->ranges.length; i++)
1199+
if (ranges[i].child_oid == child_oid)
1200+
{
1201+
found = true;
1202+
break;
1203+
}
1204+
1205+
if (found)
1206+
{
1207+
elems[0] = ranges[i].min;
1208+
elems[1] = ranges[i].max;
1209+
1210+
ArrayType *arr =
1211+
construct_array(elems, nelems, prel->atttype, sizeof(Oid), true, 'i');
1212+
PG_RETURN_ARRAYTYPE_P(arr);
1213+
}
1214+
1215+
PG_RETURN_NULL();
1216+
}

contrib/pathman/sql/init.sql

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,32 @@ RETURNS VOID AS 'pathman', 'on_partitions_removed' LANGUAGE C STRICT;
169169
CREATE OR REPLACE FUNCTION find_range_partition(relid OID, value ANYELEMENT)
170170
RETURNS OID AS 'pathman', 'find_range_partition' LANGUAGE C STRICT;
171171

172+
/*
173+
* Returns min and max values for specified RANGE partition.
174+
*/
175+
CREATE OR REPLACE FUNCTION get_partition_range(
176+
parent_relid ANYELEMENT,
177+
partition_relid ANYELEMENT)
178+
RETURNS ANYARRAY AS 'pathman', 'get_partition_range' LANGUAGE C STRICT;
179+
180+
/*
181+
* Copy rows to partitions
182+
*/
172183
CREATE OR REPLACE FUNCTION partition_data(p_parent text)
173184
RETURNS bigint AS
174185
$$
175186
DECLARE
176187
rec RECORD;
177188
BEGIN
178189
FOR rec IN (SELECT child.relname, pg_constraint.consrc
179-
FROM pg_pathman_rels
180-
JOIN pg_class AS parent ON parent.relname = pg_pathman_rels.relname
181-
JOIN pg_inherits ON inhparent = parent.relfilenode
182-
JOIN pg_constraint ON conrelid = inhrelid AND contype='c'
183-
JOIN pg_class AS child ON child.relfilenode = inhrelid
184-
WHERE pg_pathman_rels.relname = p_parent)
190+
FROM pg_pathman_rels
191+
JOIN pg_class AS parent ON parent.relname = pg_pathman_rels.relname
192+
JOIN pg_inherits ON inhparent = parent.relfilenode
193+
JOIN pg_constraint ON conrelid = inhrelid AND contype='c'
194+
JOIN pg_class AS child ON child.relfilenode = inhrelid
195+
WHERE pg_pathman_rels.relname = p_parent)
185196
LOOP
186-
RAISE NOTICE 'child %, condition %', rec.relname, rec.consrc;
187-
197+
RAISE NOTICE 'Copying data to % (condition: %)', rec.relname, rec.consrc;
188198
EXECUTE format('WITH part_data AS (
189199
DELETE FROM ONLY %s WHERE %s RETURNING *)
190200
INSERT INTO %s SELECT * FROM part_data'

0 commit comments

Comments
 (0)