98
98
DECLARE
99
99
v_rows_count BIGINT ;
100
100
v_atttype REGTYPE;
101
- v_tablespace TEXT ;
102
101
v_max start_value%TYPE;
103
102
v_cur_value start_value%TYPE := start_value;
104
103
end_value start_value%TYPE;
@@ -163,10 +162,12 @@ BEGIN
163
162
164
163
IF p_count != 0 THEN
165
164
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 );
170
171
END IF;
171
172
172
173
/* Notify backend about changes */
@@ -284,49 +285,49 @@ END
284
285
$$ LANGUAGE plpgsql;
285
286
286
287
/*
287
- * Creates RANGE partitions for specified range
288
+ * Creates RANGE partitions for specified relation based on bounds array
288
289
*/
289
- CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range (
290
+ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions (
290
291
parent_relid REGCLASS,
291
292
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 ,
295
296
partition_data BOOLEAN DEFAULT TRUE)
296
297
RETURNS INTEGER AS
297
298
$$
298
299
DECLARE
299
- part_count INTEGER := 0 ;
300
-
300
+ part_count INTEGER ;
301
301
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
+
302
310
attribute := lower (attribute);
303
311
PERFORM @extschema@.prepare_for_partitioning(parent_relid, attribute, partition_data);
304
312
305
313
/* Check boundaries */
306
314
PERFORM @extschema@.check_boundaries(parent_relid,
307
315
attribute,
308
- start_value ,
309
- end_value );
316
+ bounds[ 0 ] ,
317
+ bounds[array_length(bounds, 1 ) - 1 ] );
310
318
311
- /* Insert new entry to pathman config */
312
319
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 );
314
321
315
322
/* Create sequence for child partitions names */
316
323
PERFORM @extschema@.create_or_replace_sequence(parent_relid)
317
324
FROM @extschema@.get_plain_schema_and_relname(parent_relid);
318
325
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);
330
331
331
332
/* Notify backend about changes */
332
333
PERFORM @extschema@.on_create_partitions(parent_relid);
@@ -339,19 +340,20 @@ BEGIN
339
340
PERFORM @extschema@.set_enable_parent(parent_relid, true);
340
341
END IF;
341
342
342
- RETURN part_count; /* number of created partitions */
343
+ RETURN part_count;
343
344
END
344
- $$ LANGUAGE plpgsql;
345
+ $$
346
+ LANGUAGE plpgsql;
345
347
346
348
/*
347
- * Creates RANGE partitions for specified range based on datetime attribute
349
+ * Creates RANGE partitions for specified range
348
350
*/
349
351
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
350
352
parent_relid REGCLASS,
351
353
attribute TEXT ,
352
354
start_value ANYELEMENT,
353
355
end_value ANYELEMENT,
354
- p_interval INTERVAL ,
356
+ p_interval ANYELEMENT ,
355
357
partition_data BOOLEAN DEFAULT TRUE)
356
358
RETURNS INTEGER AS
357
359
$$
@@ -378,14 +380,11 @@ BEGIN
378
380
379
381
WHILE start_value <= end_value
380
382
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(
385
384
parent_relid,
386
385
start_value,
387
386
start_value + p_interval,
388
- @extschema@.get_tablespace(parent_relid);
387
+ tablespace : = @extschema@.get_tablespace(parent_relid) );
389
388
390
389
start_value := start_value + p_interval;
391
390
part_count := part_count + 1 ;
@@ -406,48 +405,53 @@ BEGIN
406
405
END
407
406
$$ LANGUAGE plpgsql;
408
407
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(
411
412
parent_relid REGCLASS,
412
413
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 ,
416
417
partition_data BOOLEAN DEFAULT TRUE)
417
418
RETURNS INTEGER AS
418
419
$$
419
420
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 ;
429
422
423
+ BEGIN
430
424
attribute := lower (attribute);
431
425
PERFORM @extschema@.prepare_for_partitioning(parent_relid, attribute, partition_data);
432
426
433
427
/* Check boundaries */
434
428
PERFORM @extschema@.check_boundaries(parent_relid,
435
429
attribute,
436
- bounds[ 0 ] ,
437
- bounds[array_length(bounds, 1 ) - 1 ] );
430
+ start_value ,
431
+ end_value );
438
432
433
+ /* Insert new entry to pathman config */
439
434
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 );
441
436
442
437
/* Create sequence for child partitions names */
443
438
PERFORM @extschema@.create_or_replace_sequence(parent_relid)
444
439
FROM @extschema@.get_plain_schema_and_relname(parent_relid);
445
440
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;
451
455
452
456
/* Notify backend about changes */
453
457
PERFORM @extschema@.on_create_partitions(parent_relid);
@@ -460,10 +464,10 @@ BEGIN
460
464
PERFORM @extschema@.set_enable_parent(parent_relid, true);
461
465
END IF;
462
466
463
- RETURN part_count;
467
+ RETURN part_count; /* number of created partitions */
464
468
END
465
- $$
466
- LANGUAGE plpgsql;
469
+ $$ LANGUAGE plpgsql;
470
+
467
471
468
472
/*
469
473
* Split RANGE partition
@@ -843,7 +847,7 @@ LANGUAGE plpgsql;
843
847
CREATE OR REPLACE FUNCTION @extschema@.add_range_partitions(
844
848
parent_relid REGCLASS,
845
849
bounds ANYARRAY,
846
- relnames TEXT [] DEFAULT NULL ,
850
+ partition_names TEXT [] DEFAULT NULL ,
847
851
tablespaces TEXT [] DEFAULT NULL )
848
852
RETURNS INTEGER AS
849
853
$$
@@ -858,7 +862,7 @@ BEGIN
858
862
/* Create partitions */
859
863
part_count := @extschema@.create_range_partitions_internal(parent_relid,
860
864
bounds,
861
- relnames ,
865
+ partition_names ,
862
866
tablespaces);
863
867
864
868
/* Notify backend about changes */
@@ -1094,7 +1098,7 @@ LANGUAGE C STRICT;
1094
1098
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions_internal(
1095
1099
parent_relid REGCLASS,
1096
1100
bounds ANYARRAY,
1097
- relnames TEXT [],
1101
+ partition_names TEXT [],
1098
1102
tablespaces TEXT [])
1099
1103
RETURNS REGCLASS AS ' pg_pathman' , ' create_range_partitions_internal'
1100
1104
LANGUAGE C;
0 commit comments