forked from postgrespro/pg_pathman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.sql
1331 lines (1134 loc) · 35.7 KB
/
range.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ------------------------------------------------------------------------
*
* range.sql
* RANGE partitioning functions
*
* Copyright (c) 2015-2016, Postgres Professional
*
* ------------------------------------------------------------------------
*/
CREATE OR REPLACE FUNCTION @[email protected]_sequence_name(plain_schema TEXT, plain_relname TEXT)
RETURNS TEXT AS
$$
BEGIN
RETURN format('%s.%s', plain_schema, quote_ident(format('%s_seq', plain_relname)));
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION @[email protected]_or_replace_sequence(plain_schema TEXT, plain_relname TEXT, OUT seq_name TEXT)
AS $$
DECLARE
BEGIN
seq_name := @[email protected]_sequence_name(plain_schema, plain_relname);
EXECUTE format('DROP SEQUENCE IF EXISTS %s', seq_name);
EXECUTE format('CREATE SEQUENCE %s START 1', seq_name);
END
$$
LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified relation based on datetime attribute
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partitions(
p_relation REGCLASS
, p_attribute TEXT
, p_start_value ANYELEMENT
, p_interval INTERVAL
, p_count INTEGER DEFAULT NULL)
RETURNS INTEGER AS
$$
DECLARE
v_relname TEXT;
v_rows_count INTEGER;
v_max p_start_value%TYPE;
v_cur_value p_start_value%TYPE := p_start_value;
v_plain_relname TEXT;
v_plain_schema TEXT;
i INTEGER;
BEGIN
v_relname := @[email protected]_relname(p_relation);
p_attribute := lower(p_attribute);
PERFORM @[email protected]_relation_checks(v_relname, p_attribute);
/* Try to determine partitions count if not set */
IF p_count IS NULL THEN
EXECUTE format('SELECT count(*), max(%s) FROM %s'
, p_attribute, p_relation)
INTO v_rows_count, v_max;
IF v_rows_count = 0 THEN
RAISE EXCEPTION 'Cannot determine partitions count for empty table';
END IF;
p_count := 0;
WHILE v_cur_value <= v_max
LOOP
v_cur_value := v_cur_value + p_interval;
p_count := p_count + 1;
END LOOP;
END IF;
/* Check boundaries */
EXECUTE format('SELECT @[email protected]_boundaries(''%s'', ''%s'', ''%s'', ''%s''::%s)'
, v_relname
, p_attribute
, p_start_value
, p_start_value + p_interval*p_count
, pg_typeof(p_start_value));
/* Create sequence for child partitions names */
SELECT * INTO v_plain_schema, v_plain_relname FROM @[email protected]_plain_schema_and_relname(p_relation);
PERFORM @[email protected]_or_replace_sequence(v_plain_schema, v_plain_relname);
/* Insert new entry to pathman config */
INSERT INTO @[email protected]_config (relname, attname, parttype, range_interval)
VALUES (v_relname, p_attribute, 2, p_interval::text);
/* create first partition */
FOR i IN 1..p_count
LOOP
EXECUTE format('SELECT @[email protected]_single_range_partition($1, $2, $3::%s);', pg_typeof(p_start_value))
USING v_relname, p_start_value, p_start_value + p_interval;
p_start_value := p_start_value + p_interval;
END LOOP;
/* Create triggers */
PERFORM @[email protected]_range_insert_trigger(v_relname, p_attribute);
-- PERFORM create_hash_update_trigger(relation, attribute, partitions_count);
/* Notify backend about changes */
PERFORM @[email protected]_create_partitions(p_relation::oid);
/* Copy data */
PERFORM @[email protected]_data(p_relation);
RETURN p_count;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified relation based on numerical attribute
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partitions(
p_relation REGCLASS
, p_attribute TEXT
, p_start_value ANYELEMENT
, p_interval ANYELEMENT
, p_count INTEGER DEFAULT NULL)
RETURNS INTEGER AS
$$
DECLARE
v_relname TEXT;
v_rows_count INTEGER;
v_max p_start_value%TYPE;
v_cur_value p_start_value%TYPE := p_start_value;
i INTEGER;
v_plain_schema TEXT;
v_plain_relname TEXT;
BEGIN
v_relname := @[email protected]_relname(p_relation);
p_attribute := lower(p_attribute);
PERFORM @[email protected]_relation_checks(p_relation, p_attribute);
IF p_count <= 0 THEN
RAISE EXCEPTION 'Partitions count must be greater than zero';
END IF;
/* Try to determine partitions count if not set */
IF p_count IS NULL THEN
EXECUTE format('SELECT count(*), max(%s) FROM %s'
, p_attribute, p_relation)
INTO v_rows_count, v_max;
IF v_rows_count = 0 THEN
RAISE EXCEPTION 'Cannot determine partitions count for empty table';
END IF;
IF v_max IS NULL THEN
RAISE EXCEPTION '''%'' column has NULL values', p_attribute;
END IF;
p_count := 0;
WHILE v_cur_value <= v_max
LOOP
v_cur_value := v_cur_value + p_interval;
p_count := p_count + 1;
END LOOP;
END IF;
/* check boundaries */
PERFORM @[email protected]_boundaries(p_relation
, p_attribute
, p_start_value
, p_start_value + p_interval*p_count);
/* Create sequence for child partitions names */
SELECT * INTO v_plain_schema, v_plain_relname FROM @[email protected]_plain_schema_and_relname(p_relation);
PERFORM @[email protected]_or_replace_sequence(v_plain_schema, v_plain_relname);
/* Insert new entry to pathman config */
INSERT INTO @[email protected]_config (relname, attname, parttype, range_interval)
VALUES (v_relname, p_attribute, 2, p_interval::text);
/* create first partition */
FOR i IN 1..p_count
LOOP
PERFORM @[email protected]_single_range_partition(p_relation
, p_start_value
, p_start_value + p_interval);
p_start_value := p_start_value + p_interval;
END LOOP;
/* Create triggers */
PERFORM @[email protected]_range_insert_trigger(p_relation, p_attribute);
-- PERFORM create_hash_update_trigger(relation, attribute, partitions_count);
/* Notify backend about changes */
PERFORM @[email protected]_create_partitions(p_relation::regclass::oid);
/* Copy data */
PERFORM @[email protected]_data(p_relation);
RETURN p_count;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified range
*/
CREATE OR REPLACE FUNCTION @[email protected]_partitions_from_range(
p_relation REGCLASS
, p_attribute TEXT
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT
, p_interval ANYELEMENT)
RETURNS INTEGER AS
$$
DECLARE
v_relname TEXT;
v_plain_schema TEXT;
v_plain_relname TEXT;
i INTEGER := 0;
BEGIN
v_relname := @[email protected]_relname(p_relation);
p_attribute := lower(p_attribute);
PERFORM @[email protected]_relation_checks(p_relation, p_attribute);
IF p_interval <= 0 THEN
RAISE EXCEPTION 'Interval must be positive';
END IF;
/* Create sequence for child partitions names */
SELECT * INTO v_plain_schema, v_plain_relname FROM @[email protected]_plain_schema_and_relname(p_relation);
PERFORM @[email protected]_or_replace_sequence(v_plain_schema, v_plain_relname);
/* check boundaries */
PERFORM @[email protected]_boundaries(p_relation
, p_attribute
, p_start_value
, p_end_value);
/* Insert new entry to pathman config */
INSERT INTO @[email protected]_config (relname, attname, parttype, range_interval)
VALUES (v_relname, p_attribute, 2, p_interval::text);
WHILE p_start_value <= p_end_value
LOOP
PERFORM @[email protected]_single_range_partition(p_relation
, p_start_value
, p_start_value + p_interval);
p_start_value := p_start_value + p_interval;
i := i + 1;
END LOOP;
/* Create triggers */
PERFORM @[email protected]_range_insert_trigger(p_relation, p_attribute);
/* Notify backend about changes */
PERFORM @[email protected]_create_partitions(p_relation::regclass::oid);
/* Copy data */
PERFORM @[email protected]_data(p_relation);
RETURN i;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$ LANGUAGE plpgsql;
/*
* Creates RANGE partitions for specified range based on datetime attribute
*/
CREATE OR REPLACE FUNCTION @[email protected]_partitions_from_range(
p_relation REGCLASS
, p_attribute TEXT
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT
, p_interval INTERVAL)
RETURNS INTEGER AS
$$
DECLARE
v_relname TEXT;
v_plain_schema TEXT;
v_plain_relname TEXT;
i INTEGER := 0;
BEGIN
v_relname := @[email protected]_relname(p_relation);
p_attribute := lower(p_attribute);
PERFORM @[email protected]_relation_checks(p_relation, p_attribute);
/* Create sequence for child partitions names */
SELECT * INTO v_plain_schema, v_plain_relname FROM @[email protected]_plain_schema_and_relname(p_relation);
PERFORM @[email protected]_or_replace_sequence(v_plain_schema, v_plain_relname);
/* check boundaries */
PERFORM @[email protected]_boundaries(p_relation
, p_attribute
, p_start_value
, p_end_value);
/* Insert new entry to pathman config */
INSERT INTO @[email protected]_config (relname, attname, parttype, range_interval)
VALUES (v_relname, p_attribute, 2, p_interval::text);
WHILE p_start_value <= p_end_value
LOOP
EXECUTE format('SELECT @[email protected]_single_range_partition($1, $2, $3::%s);', pg_typeof(p_start_value))
USING p_relation, p_start_value, p_start_value + p_interval;
p_start_value := p_start_value + p_interval;
i := i + 1;
END LOOP;
/* Create triggers */
PERFORM @[email protected]_range_insert_trigger(p_relation, p_attribute);
/* Notify backend about changes */
PERFORM @[email protected]_create_partitions(p_relation::regclass::oid);
/* Copy data */
PERFORM @[email protected]_data(p_relation);
RETURN i;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$ LANGUAGE plpgsql;
/*
*
*/
CREATE OR REPLACE FUNCTION @[email protected]_boundaries(
p_relation REGCLASS
, p_attribute TEXT
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT)
RETURNS VOID AS
$$
DECLARE
v_min p_start_value%TYPE;
v_max p_start_value%TYPE;
v_count INTEGER;
BEGIN
/* Get min and max values */
EXECUTE format('SELECT count(*), min(%s), max(%s) FROM %s WHERE NOT %s IS NULL',
p_attribute, p_attribute, p_relation::text, p_attribute)
INTO v_count, v_min, v_max;
/* check if column has NULL values */
IF v_count > 0 AND (v_min IS NULL OR v_max IS NULL) THEN
RAISE EXCEPTION '''%'' column has NULL values', p_attribute;
END IF;
/* check lower boundary */
IF p_start_value > v_min THEN
RAISE EXCEPTION 'Start value is less than minimum value of ''%'''
, p_attribute;
END IF;
/* check upper boundary */
IF p_end_value <= v_max THEN
RAISE EXCEPTION 'Not enough partitions to fit all the values of ''%'''
, p_attribute;
END IF;
END
$$ LANGUAGE plpgsql;
/*
* Formats range condition. Utility function.
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_condition(
p_attname TEXT
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT)
RETURNS TEXT AS
$$
DECLARE
v_type REGTYPE;
v_sql TEXT;
BEGIN
/* determine the type of values */
v_type := pg_typeof(p_start_value);
/* we cannot use placeholders in DDL queries, so we are using format(...) */
IF v_type IN ('date'::regtype, 'timestamp'::regtype, 'timestamptz'::regtype) THEN
v_sql := '%s >= ''%s'' AND %s < ''%s''';
ELSE
v_sql := '%s >= %s AND %s < %s';
END IF;
v_sql := format(v_sql
, p_attname
, p_start_value
, p_attname
, p_end_value);
RETURN v_sql;
END
$$
LANGUAGE plpgsql;
/*
* Creates new RANGE partition. Returns partition name
*/
CREATE OR REPLACE FUNCTION @[email protected]_single_range_partition(
p_parent REGCLASS
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT)
RETURNS TEXT AS
$$
DECLARE
v_part_num INT;
v_child_relname TEXT;
v_plain_child_relname TEXT;
v_attname TEXT;
v_sql TEXT;
v_cond TEXT;
v_plain_schema TEXT;
v_plain_relname TEXT;
v_child_relname_exists INTEGER := 1;
v_seq_name TEXT;
BEGIN
v_attname := attname FROM @[email protected]_config
WHERE relname::regclass = p_parent;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_parent::TEXT);
END IF;
SELECT * INTO v_plain_schema, v_plain_relname
FROM @[email protected]_plain_schema_and_relname(p_parent);
v_seq_name := @[email protected]_sequence_name(v_plain_schema, v_plain_relname);
/* get next value from sequence */
LOOP
v_part_num := nextval(v_seq_name);
v_plain_child_relname := format('%s_%s', v_plain_relname, v_part_num);
v_child_relname := format('%s.%s',
v_plain_schema,
quote_ident(v_plain_child_relname));
v_child_relname_exists := count(*)
FROM pg_class
WHERE relnamespace::regnamespace || '.' || relname = v_child_relname
LIMIT 1;
EXIT WHEN v_child_relname_exists = 0;
END LOOP;
EXECUTE format('CREATE TABLE %s (LIKE %s INCLUDING ALL)'
, v_child_relname
, p_parent);
EXECUTE format('ALTER TABLE %s INHERIT %s'
, v_child_relname
, p_parent);
v_cond := @[email protected]_range_condition(v_attname, p_start_value, p_end_value);
v_sql := format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)'
, v_child_relname
, quote_ident(format('%s_%s_check', v_plain_schema, v_plain_child_relname))
, v_cond);
EXECUTE v_sql;
RETURN v_child_relname;
END
$$ LANGUAGE plpgsql;
/*
* Split RANGE partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(
p_partition REGCLASS
, p_value ANYELEMENT
, OUT p_range ANYARRAY)
RETURNS ANYARRAY AS
$$
DECLARE
v_parent_relid OID;
v_child_relid OID := p_partition::oid;
v_attname TEXT;
v_cond TEXT;
v_new_partition TEXT;
v_part_type INTEGER;
v_part_relname TEXT;
v_plain_schema TEXT;
v_plain_relname TEXT;
v_check_name TEXT;
BEGIN
v_part_relname := @[email protected]_relname(p_partition);
v_parent_relid := inhparent
FROM pg_inherits
WHERE inhrelid = v_child_relid;
SELECT attname, parttype
FROM @[email protected]_config
WHERE relname::regclass = v_parent_relid::regclass
INTO v_attname, v_part_type;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned',
quote_ident(v_parent_relid::regclass::text);
END IF;
SELECT * INTO v_plain_schema, v_plain_relname
FROM @[email protected]_plain_schema_and_relname(p_partition);
/* Check if this is RANGE partition */
IF v_part_type != 2 THEN
RAISE EXCEPTION 'Specified partition isn''t RANGE partition';
END IF;
/* Get partition values range */
p_range := @[email protected]_partition_range(v_parent_relid, v_child_relid, 0);
IF p_range IS NULL THEN
RAISE EXCEPTION 'Could not find specified partition';
END IF;
/* Check if value fit into the range */
IF p_range[1] > p_value OR p_range[2] <= p_value
THEN
RAISE EXCEPTION 'Specified value does not fit into the range [%, %)',
p_range[1], p_range[2];
END IF;
/* Create new partition */
RAISE NOTICE 'Creating new partition...';
v_new_partition := @[email protected]_single_range_partition(
@[email protected]_schema_qualified_name(v_parent_relid::regclass, '.'),
p_value,
p_range[2]);
/* Copy data */
RAISE NOTICE 'Copying data to new partition...';
v_cond := @[email protected]_range_condition(v_attname, p_value, p_range[2]);
EXECUTE format('
WITH part_data AS (
DELETE FROM %s WHERE %s RETURNING *)
INSERT INTO %s SELECT * FROM part_data'
, p_partition
, v_cond
, v_new_partition);
/* Alter original partition */
RAISE NOTICE 'Altering original partition...';
v_cond := @[email protected]_range_condition(v_attname, p_range[1], p_value);
v_check_name := quote_ident(format('%s_%s_check', v_plain_schema, v_plain_relname));
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s'
, p_partition::text
, v_check_name);
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)'
, p_partition
, v_check_name
, v_cond);
/* Tell backend to reload configuration */
PERFORM @[email protected]_update_partitions(v_parent_relid::oid);
RAISE NOTICE 'Done!';
END
$$
LANGUAGE plpgsql;
/*
* Merge RANGE partitions
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partitions(
p_partition1 REGCLASS
, p_partition2 REGCLASS)
RETURNS VOID AS
$$
DECLARE
v_parent_relid1 OID;
v_parent_relid2 OID;
v_part1_relid OID := p_partition1::oid;
v_part2_relid OID := p_partition2::oid;
v_part1_relname TEXT;
v_part2_relname TEXT;
v_attname TEXT;
v_part_type INTEGER;
v_atttype TEXT;
BEGIN
v_part1_relname := @[email protected]_relname(p_partition1);
v_part2_relname := @[email protected]_relname(p_partition2);
IF v_part1_relid = v_part2_relid THEN
RAISE EXCEPTION 'Cannot merge partition to itself';
END IF;
v_parent_relid1 := inhparent FROM pg_inherits WHERE inhrelid = v_part1_relid;
v_parent_relid2 := inhparent FROM pg_inherits WHERE inhrelid = v_part2_relid;
IF v_parent_relid1 != v_parent_relid2 THEN
RAISE EXCEPTION 'Cannot merge partitions having different parents';
END IF;
SELECT attname, parttype
FROM @[email protected]_config
WHERE relname::regclass = v_parent_relid1::regclass
INTO v_attname, v_part_type;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned',
quote_ident(v_parent_relid1::regclass::text);
END IF;
/* Check if this is RANGE partition */
IF v_part_type != 2 THEN
RAISE EXCEPTION 'Specified partitions aren''t RANGE partitions';
END IF;
v_atttype := @[email protected]_attribute_type_name(p_partition1, v_attname);
EXECUTE format('SELECT @[email protected]_range_partitions_internal($1, $2 , $3, NULL::%s)', v_atttype)
USING v_parent_relid1, p_partition1 , p_partition2;
/* Tell backend to reload configuration */
PERFORM @[email protected]_update_partitions(v_parent_relid1::oid);
RAISE NOTICE 'Done!';
END
$$
LANGUAGE plpgsql;
/*
* Merge two partitions. All data will be copied to the first one. Second
* partition will be destroyed.
*
* Notes: dummy field is used to pass the element type to the function
* (it is necessary because of pseudo-types used in function)
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partitions_internal(
p_parent_relid OID
, p_part1 REGCLASS
, p_part2 REGCLASS
, dummy ANYELEMENT
, OUT p_range ANYARRAY)
RETURNS ANYARRAY AS
$$
DECLARE
v_attname TEXT;
v_cond TEXT;
v_plain_schema TEXT;
v_plain_relname TEXT;
v_child_relname TEXT;
v_check_name TEXT;
BEGIN
SELECT attname FROM @[email protected]_config
WHERE relname::regclass = p_parent_relid::regclass
INTO v_attname;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned',
quote_ident(p_parent_relid::regclass::text);
END IF;
SELECT * INTO v_plain_schema, v_plain_relname
FROM @[email protected]_plain_schema_and_relname(p_part1);
/*
* Get ranges
* first and second elements of array are MIN and MAX of partition1
* third and forth elements are MIN and MAX of partition2
*/
p_range := @[email protected]_partition_range(p_parent_relid, p_part1::oid, 0) ||
@[email protected]_partition_range(p_parent_relid, p_part2::oid, 0);
/* Check if ranges are adjacent */
IF p_range[1] != p_range[4] AND p_range[2] != p_range[3] THEN
RAISE EXCEPTION 'Merge failed. Partitions must be adjacent';
END IF;
/* Extend first partition */
v_cond := @[email protected]_range_condition(v_attname
, least(p_range[1], p_range[3])
, greatest(p_range[2], p_range[4]));
/* Alter first partition */
RAISE NOTICE 'Altering first partition...';
v_check_name := quote_ident(v_plain_schema || '_' || v_plain_relname || '_check');
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s'
, p_part1::text
, v_check_name);
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)'
, p_part1::text
, v_check_name
, v_cond);
/* Copy data from second partition to the first one */
RAISE NOTICE 'Copying data...';
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
INSERT INTO %s SELECT * FROM part_data'
, p_part2::text
, p_part1::text);
/* Remove second partition */
RAISE NOTICE 'Dropping second partition...';
EXECUTE format('DROP TABLE %s', p_part2::text);
END
$$ LANGUAGE plpgsql;
/*
* Append new partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(
p_relation REGCLASS)
RETURNS TEXT AS
$$
DECLARE
v_attname TEXT;
v_atttype TEXT;
v_part_name TEXT;
v_interval TEXT;
BEGIN
/* Prevent concurrent partition creation */
PERFORM @[email protected]_partitions_lock();
SELECT attname, range_interval
FROM @[email protected]_config
WHERE relname::regclass = p_relation
INTO v_attname, v_interval;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
END IF;
v_atttype := @[email protected]_attribute_type_name(p_relation, v_attname);
EXECUTE format('SELECT @[email protected]_partition_internal($1, $2, $3, ARRAY[]::%s[])', v_atttype)
INTO v_part_name
USING p_relation, v_atttype, v_interval;
/* Invalidate cache */
PERFORM @[email protected]_update_partitions(p_relation::oid);
/* Release lock */
PERFORM @[email protected]_partitions_lock();
RAISE NOTICE 'Done!';
RETURN v_part_name;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION @[email protected]_partition_internal(
p_relation REGCLASS
, p_atttype TEXT
, p_interval TEXT
, p_range ANYARRAY DEFAULT NULL)
RETURNS TEXT AS
$$
DECLARE
v_part_name TEXT;
BEGIN
p_range := @[email protected]_range_by_idx(p_relation::oid, -1, 0);
RAISE NOTICE 'Appending new partition...';
IF @[email protected]_date(p_atttype::regtype) THEN
v_part_name := @[email protected]_single_range_partition(p_relation
, p_range[2]
, p_range[2] + p_interval::interval);
ELSE
EXECUTE format('SELECT @[email protected]_single_range_partition($1, $2, $2 + $3::%s)', p_atttype)
USING p_relation, p_range[2], p_interval
INTO v_part_name;
END IF;
RETURN v_part_name;
END
$$
LANGUAGE plpgsql;
/*
* Prepend new partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(p_relation REGCLASS)
RETURNS TEXT AS
$$
DECLARE
v_attname TEXT;
v_atttype TEXT;
v_part_name TEXT;
v_interval TEXT;
BEGIN
/* Prevent concurrent partition creation */
PERFORM @[email protected]_partitions_lock();
SELECT attname, range_interval
FROM @[email protected]_config
WHERE relname::regclass = p_relation
INTO v_attname, v_interval;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
END IF;
v_atttype := @[email protected]_attribute_type_name(p_relation, v_attname);
EXECUTE format('SELECT @[email protected]_partition_internal($1, $2, $3, ARRAY[]::%s[])', v_atttype)
INTO v_part_name
USING p_relation, v_atttype, v_interval;
/* Invalidate cache */
PERFORM @[email protected]_update_partitions(p_relation::oid);
/* Release lock */
PERFORM @[email protected]_partitions_lock();
RAISE NOTICE 'Done!';
RETURN v_part_name;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION @[email protected]_partition_internal(
p_relation REGCLASS
, p_atttype TEXT
, p_interval TEXT
, p_range ANYARRAY DEFAULT NULL)
RETURNS TEXT AS
$$
DECLARE
v_part_name TEXT;
BEGIN
p_range := @[email protected]_range_by_idx(p_relation::oid, 0, 0);
RAISE NOTICE 'Prepending new partition...';
IF @[email protected]_date(p_atttype::regtype) THEN
v_part_name := @[email protected]_single_range_partition(p_relation
, p_range[1] - p_interval::interval
, p_range[1]);
ELSE
EXECUTE format('SELECT @[email protected]_single_range_partition($1, $2 - $3::%s, $2)', p_atttype)
USING p_relation, p_range[1], p_interval
INTO v_part_name;
END IF;
RETURN v_part_name;
END
$$
LANGUAGE plpgsql;
/*
* Add new partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(
p_relation REGCLASS
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT)
RETURNS TEXT AS
$$
DECLARE
v_part_name TEXT;
BEGIN
/* Prevent concurrent partition creation */
PERFORM @[email protected]_partitions_lock();
/* check range overlap */
IF @[email protected]_overlap(p_relation::oid, p_start_value, p_end_value) != FALSE THEN
RAISE EXCEPTION 'Specified range overlaps with existing partitions';
END IF;
IF p_start_value >= p_end_value THEN
RAISE EXCEPTION 'Failed to create partition: p_start_value is greater than p_end_value';
END IF;
/* Create new partition */
v_part_name := @[email protected]_single_range_partition(p_relation, p_start_value, p_end_value);
PERFORM @[email protected]_update_partitions(p_relation::oid);
/* Release lock */
PERFORM @[email protected]_partitions_lock();
RAISE NOTICE 'Done!';
RETURN v_part_name;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$
LANGUAGE plpgsql;
/*
* Drop range partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(
p_partition REGCLASS)
RETURNS TEXT AS
$$
DECLARE
v_part_name TEXT := p_partition::TEXT;
v_parent TEXT;
v_count INTEGER;
BEGIN
/* Prevent concurrent partition management */
PERFORM @[email protected]_partitions_lock();
/* Parent table name */
SELECT inhparent::regclass INTO v_parent
FROM pg_inherits WHERE inhrelid::regclass = p_partition;
IF v_parent IS NULL THEN
RAISE EXCEPTION 'Partition ''%'' not found', p_partition;
END IF;
/* Drop table and update cache */
EXECUTE format('DROP TABLE %s', p_partition::TEXT);
PERFORM @[email protected]_update_partitions(v_parent::regclass::oid);
/* Release lock */
PERFORM @[email protected]_partitions_lock();
RETURN v_part_name;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END
$$
LANGUAGE plpgsql;
/*
* Attach range partition
*/
CREATE OR REPLACE FUNCTION @[email protected]_range_partition(
p_relation REGCLASS
, p_partition REGCLASS
, p_start_value ANYELEMENT
, p_end_value ANYELEMENT)
RETURNS TEXT AS
$$
DECLARE
v_attname TEXT;
v_cond TEXT;
v_plain_partname TEXT;
v_plain_schema TEXT;
rel_persistence CHAR;
BEGIN
/* Ignore temporary tables */
SELECT relpersistence FROM pg_catalog.pg_class WHERE oid = p_partition INTO rel_persistence;
IF rel_persistence = 't'::CHAR THEN
RAISE EXCEPTION 'Temporary table % cannot be used as a partition',
quote_ident(p_partition::TEXT);
END IF;
/* Prevent concurrent partition management */
PERFORM @[email protected]_partitions_lock();
IF @[email protected]_overlap(p_relation::oid, p_start_value, p_end_value) != FALSE THEN
RAISE EXCEPTION 'Specified range overlaps with existing partitions';
END IF;
IF NOT @[email protected]_relations_equality(p_relation, p_partition) THEN
RAISE EXCEPTION 'Partition must have the exact same structure as parent';
END IF;
/* Set inheritance */
EXECUTE format('ALTER TABLE %s INHERIT %s'
, p_partition
, p_relation);
/* Set check constraint */
v_attname := attname
FROM @[email protected]_config
WHERE relname::regclass = p_relation;
IF v_attname IS NULL THEN
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
END IF;
v_cond := @[email protected]_range_condition(v_attname, p_start_value, p_end_value);
/* Plain partition name and schema */
SELECT * INTO v_plain_schema, v_plain_partname FROM @[email protected]_plain_schema_and_relname(p_partition);
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)'
, p_partition
, v_plain_schema || '_' || quote_ident(v_plain_partname || '_check')
, v_cond);
/* Invalidate cache */
PERFORM @[email protected]_update_partitions(p_relation::oid);
/* Release lock */
PERFORM @[email protected]_partitions_lock();
RETURN p_partition;
EXCEPTION WHEN others THEN
RAISE EXCEPTION '%', SQLERRM;
END