Skip to content

Commit 1df43cb

Browse files
committed
improve function names (and arg names too)
1 parent efa2918 commit 1df43cb

File tree

3 files changed

+71
-67
lines changed

3 files changed

+71
-67
lines changed

expected/pathman_calamity.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ SELECT create_range_partitions_internal('calamity.part_test',
110110
ERROR: 'bounds' should not be NULL
111111
SELECT create_range_partitions_internal('calamity.part_test', '{1}'::INT[],
112112
'{part_1}'::TEXT[], NULL); /* not ok */
113-
ERROR: wrong length of 'relnames' array
113+
ERROR: wrong length of 'partition_names' array
114114
SELECT create_range_partitions_internal('calamity.part_test', '{1}'::INT[],
115115
NULL, '{tblspc_1}'::TEXT[]); /* not ok */
116116
ERROR: wrong length of 'tablespaces' array

range.sql

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ $$
9898
DECLARE
9999
v_rows_count BIGINT;
100100
v_atttype REGTYPE;
101-
v_tablespace TEXT;
102101
v_max start_value%TYPE;
103102
v_cur_value start_value%TYPE := start_value;
104103
end_value start_value%TYPE;
@@ -163,10 +162,12 @@ BEGIN
163162

164163
IF p_count != 0 THEN
165164
part_count := @extschema@.create_range_partitions_internal(
166-
parent_relid,
167-
@extschema@.generate_range_bounds(start_value, p_interval, p_count),
168-
NULL,
169-
NULL);
165+
parent_relid,
166+
@extschema@.generate_range_bounds(start_value,
167+
p_interval,
168+
p_count),
169+
NULL,
170+
NULL);
170171
END IF;
171172

172173
/* Notify backend about changes */
@@ -284,49 +285,49 @@ END
284285
$$ LANGUAGE plpgsql;
285286

286287
/*
287-
* Creates RANGE partitions for specified range
288+
* Creates RANGE partitions for specified relation based on bounds array
288289
*/
289-
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
290+
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
290291
parent_relid REGCLASS,
291292
attribute TEXT,
292-
start_value ANYELEMENT,
293-
end_value ANYELEMENT,
294-
p_interval ANYELEMENT,
293+
bounds ANYARRAY,
294+
partition_names TEXT[] DEFAULT NULL,
295+
tablespaces TEXT[] DEFAULT NULL,
295296
partition_data BOOLEAN DEFAULT TRUE)
296297
RETURNS INTEGER AS
297298
$$
298299
DECLARE
299-
part_count INTEGER := 0;
300-
300+
part_count INTEGER;
301301
BEGIN
302+
IF array_ndims(bounds) > 1 THEN
303+
RAISE EXCEPTION 'Bounds array must be a one dimensional array';
304+
END IF;
305+
306+
IF array_length(bounds, 1) < 2 THEN
307+
RAISE EXCEPTION 'Bounds array must have at least two values';
308+
END IF;
309+
302310
attribute := lower(attribute);
303311
PERFORM @extschema@.prepare_for_partitioning(parent_relid, attribute, partition_data);
304312

305313
/* Check boundaries */
306314
PERFORM @extschema@.check_boundaries(parent_relid,
307315
attribute,
308-
start_value,
309-
end_value);
316+
bounds[0],
317+
bounds[array_length(bounds, 1) - 1]);
310318

311-
/* Insert new entry to pathman config */
312319
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
313-
VALUES (parent_relid, attribute, 2, p_interval::TEXT);
320+
VALUES (parent_relid, attribute, 2, NULL);
314321

315322
/* Create sequence for child partitions names */
316323
PERFORM @extschema@.create_or_replace_sequence(parent_relid)
317324
FROM @extschema@.get_plain_schema_and_relname(parent_relid);
318325

319-
WHILE start_value <= end_value
320-
LOOP
321-
PERFORM @extschema@.create_single_range_partition(
322-
parent_relid,
323-
start_value,
324-
start_value + p_interval,
325-
tablespace := @extschema@.get_tablespace(parent_relid));
326-
327-
start_value := start_value + p_interval;
328-
part_count := part_count + 1;
329-
END LOOP;
326+
/* Create partitions */
327+
part_count := @extschema@.create_range_partitions_internal(parent_relid,
328+
bounds,
329+
partition_names,
330+
tablespaces);
330331

331332
/* Notify backend about changes */
332333
PERFORM @extschema@.on_create_partitions(parent_relid);
@@ -339,19 +340,20 @@ BEGIN
339340
PERFORM @extschema@.set_enable_parent(parent_relid, true);
340341
END IF;
341342

342-
RETURN part_count; /* number of created partitions */
343+
RETURN part_count;
343344
END
344-
$$ LANGUAGE plpgsql;
345+
$$
346+
LANGUAGE plpgsql;
345347

346348
/*
347-
* Creates RANGE partitions for specified range based on datetime attribute
349+
* Creates RANGE partitions for specified range
348350
*/
349351
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
350352
parent_relid REGCLASS,
351353
attribute TEXT,
352354
start_value ANYELEMENT,
353355
end_value ANYELEMENT,
354-
p_interval INTERVAL,
356+
p_interval ANYELEMENT,
355357
partition_data BOOLEAN DEFAULT TRUE)
356358
RETURNS INTEGER AS
357359
$$
@@ -378,14 +380,11 @@ BEGIN
378380

379381
WHILE start_value <= end_value
380382
LOOP
381-
EXECUTE
382-
format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s, tablespace:=$4);',
383-
@extschema@.get_base_type(pg_typeof(start_value))::TEXT)
384-
USING
383+
PERFORM @extschema@.create_single_range_partition(
385384
parent_relid,
386385
start_value,
387386
start_value + p_interval,
388-
@extschema@.get_tablespace(parent_relid);
387+
tablespace := @extschema@.get_tablespace(parent_relid));
389388

390389
start_value := start_value + p_interval;
391390
part_count := part_count + 1;
@@ -406,48 +405,53 @@ BEGIN
406405
END
407406
$$ LANGUAGE plpgsql;
408407

409-
410-
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions2(
408+
/*
409+
* Creates RANGE partitions for specified range based on datetime attribute
410+
*/
411+
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
411412
parent_relid REGCLASS,
412413
attribute TEXT,
413-
bounds ANYARRAY,
414-
relnames TEXT[] DEFAULT NULL,
415-
tablespaces TEXT[] DEFAULT NULL,
414+
start_value ANYELEMENT,
415+
end_value ANYELEMENT,
416+
p_interval INTERVAL,
416417
partition_data BOOLEAN DEFAULT TRUE)
417418
RETURNS INTEGER AS
418419
$$
419420
DECLARE
420-
part_count INTEGER;
421-
BEGIN
422-
IF array_ndims(bounds) > 1 THEN
423-
RAISE EXCEPTION 'Bounds array must be a one dimensional array';
424-
END IF;
425-
426-
IF array_length(bounds, 1) < 2 THEN
427-
RAISE EXCEPTION 'Bounds array must have at least two values';
428-
END IF;
421+
part_count INTEGER := 0;
429422

423+
BEGIN
430424
attribute := lower(attribute);
431425
PERFORM @extschema@.prepare_for_partitioning(parent_relid, attribute, partition_data);
432426

433427
/* Check boundaries */
434428
PERFORM @extschema@.check_boundaries(parent_relid,
435429
attribute,
436-
bounds[0],
437-
bounds[array_length(bounds, 1) - 1]);
430+
start_value,
431+
end_value);
438432

433+
/* Insert new entry to pathman config */
439434
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
440-
VALUES (parent_relid, attribute, 2, NULL);
435+
VALUES (parent_relid, attribute, 2, p_interval::TEXT);
441436

442437
/* Create sequence for child partitions names */
443438
PERFORM @extschema@.create_or_replace_sequence(parent_relid)
444439
FROM @extschema@.get_plain_schema_and_relname(parent_relid);
445440

446-
/* Create partitions */
447-
part_count := @extschema@.create_range_partitions_internal(parent_relid,
448-
bounds,
449-
relnames,
450-
tablespaces);
441+
WHILE start_value <= end_value
442+
LOOP
443+
EXECUTE
444+
format('SELECT @extschema@.create_single_range_partition($1, $2, $3::%s, tablespace:=$4);',
445+
@extschema@.get_base_type(pg_typeof(start_value))::TEXT)
446+
USING
447+
parent_relid,
448+
start_value,
449+
start_value + p_interval,
450+
@extschema@.get_tablespace(parent_relid);
451+
452+
start_value := start_value + p_interval;
453+
part_count := part_count + 1;
454+
END LOOP;
451455

452456
/* Notify backend about changes */
453457
PERFORM @extschema@.on_create_partitions(parent_relid);
@@ -460,10 +464,10 @@ BEGIN
460464
PERFORM @extschema@.set_enable_parent(parent_relid, true);
461465
END IF;
462466

463-
RETURN part_count;
467+
RETURN part_count; /* number of created partitions */
464468
END
465-
$$
466-
LANGUAGE plpgsql;
469+
$$ LANGUAGE plpgsql;
470+
467471

468472
/*
469473
* Split RANGE partition
@@ -843,7 +847,7 @@ LANGUAGE plpgsql;
843847
CREATE OR REPLACE FUNCTION @extschema@.add_range_partitions(
844848
parent_relid REGCLASS,
845849
bounds ANYARRAY,
846-
relnames TEXT[] DEFAULT NULL,
850+
partition_names TEXT[] DEFAULT NULL,
847851
tablespaces TEXT[] DEFAULT NULL)
848852
RETURNS INTEGER AS
849853
$$
@@ -858,7 +862,7 @@ BEGIN
858862
/* Create partitions */
859863
part_count := @extschema@.create_range_partitions_internal(parent_relid,
860864
bounds,
861-
relnames,
865+
partition_names,
862866
tablespaces);
863867

864868
/* Notify backend about changes */
@@ -1094,7 +1098,7 @@ LANGUAGE C STRICT;
10941098
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions_internal(
10951099
parent_relid REGCLASS,
10961100
bounds ANYARRAY,
1097-
relnames TEXT[],
1101+
partition_names TEXT[],
10981102
tablespaces TEXT[])
10991103
RETURNS REGCLASS AS 'pg_pathman', 'create_range_partitions_internal'
11001104
LANGUAGE C;

src/pl_range_funcs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ create_range_partitions_internal(PG_FUNCTION_ARGS)
202202

203203
if (partnames && npartnames != ndatums - 1)
204204
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
205-
errmsg("wrong length of 'relnames' array"),
206-
errdetail("number of 'relnames' must be less than "
205+
errmsg("wrong length of 'partition_names' array"),
206+
errdetail("number of 'partition_names' must be less than "
207207
"'bounds' array length by one")));
208208

209209
if (tablespaces && ntablespaces != ndatums - 1)

0 commit comments

Comments
 (0)