@@ -84,15 +84,15 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
84
84
p_start_value ANYELEMENT,
85
85
p_interval INTERVAL,
86
86
p_count INTEGER DEFAULT NULL ,
87
- partition_data BOOLEAN DEFAULT true )
87
+ partition_data BOOLEAN DEFAULT TRUE )
88
88
RETURNS INTEGER AS
89
89
$$
90
90
DECLARE
91
91
v_rows_count INTEGER ;
92
92
v_max p_start_value%TYPE;
93
93
v_cur_value p_start_value%TYPE := p_start_value;
94
- v_tablespace TEXT ;
95
94
i INTEGER ;
95
+
96
96
BEGIN
97
97
IF partition_data = true THEN
98
98
/* Acquire data modification lock */
@@ -149,9 +149,6 @@ BEGIN
149
149
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
150
150
VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
151
151
152
- /* Determine tablespace of parent table */
153
- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
154
-
155
152
/* Create first partition */
156
153
FOR i IN 1 ..p_count
157
154
LOOP
@@ -162,7 +159,7 @@ BEGIN
162
159
parent_relid,
163
160
p_start_value,
164
161
p_start_value + p_interval,
165
- v_tablespace ;
162
+ @extschema@.get_rel_tablespace_name(parent_relid) ;
166
163
167
164
p_start_value := p_start_value + p_interval;
168
165
END LOOP;
@@ -191,14 +188,13 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
191
188
p_start_value ANYELEMENT,
192
189
p_interval ANYELEMENT,
193
190
p_count INTEGER DEFAULT NULL ,
194
- partition_data BOOLEAN DEFAULT true )
191
+ partition_data BOOLEAN DEFAULT TRUE )
195
192
RETURNS INTEGER AS
196
193
$$
197
194
DECLARE
198
195
v_rows_count INTEGER ;
199
196
v_max p_start_value%TYPE;
200
197
v_cur_value p_start_value%TYPE := p_start_value;
201
- v_tablespace TEXT ;
202
198
i INTEGER ;
203
199
204
200
BEGIN
@@ -259,17 +255,14 @@ BEGIN
259
255
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
260
256
VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
261
257
262
- /* Determine tablespace of parent table */
263
- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
264
-
265
258
/* create first partition */
266
259
FOR i IN 1 ..p_count
267
260
LOOP
268
261
PERFORM @extschema@.create_single_range_partition(
269
262
parent_relid,
270
263
p_start_value,
271
264
p_start_value + p_interval,
272
- tablespace := v_tablespace );
265
+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
273
266
274
267
p_start_value := p_start_value + p_interval;
275
268
END LOOP;
@@ -298,12 +291,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
298
291
p_start_value ANYELEMENT,
299
292
p_end_value ANYELEMENT,
300
293
p_interval ANYELEMENT,
301
- partition_data BOOLEAN DEFAULT true )
294
+ partition_data BOOLEAN DEFAULT TRUE )
302
295
RETURNS INTEGER AS
303
296
$$
304
297
DECLARE
305
298
part_count INTEGER := 0 ;
306
- v_tablespace TEXT ;
307
299
308
300
BEGIN
309
301
IF partition_data = true THEN
@@ -336,16 +328,13 @@ BEGIN
336
328
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
337
329
VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
338
330
339
- /* Determine tablespace of parent table */
340
- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
341
-
342
331
WHILE p_start_value <= p_end_value
343
332
LOOP
344
333
PERFORM @extschema@.create_single_range_partition(
345
334
parent_relid,
346
335
p_start_value,
347
336
p_start_value + p_interval,
348
- tablespace := v_tablespace );
337
+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
349
338
350
339
p_start_value := p_start_value + p_interval;
351
340
part_count := part_count + 1 ;
@@ -375,12 +364,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
375
364
p_start_value ANYELEMENT,
376
365
p_end_value ANYELEMENT,
377
366
p_interval INTERVAL,
378
- partition_data BOOLEAN DEFAULT true )
367
+ partition_data BOOLEAN DEFAULT TRUE )
379
368
RETURNS INTEGER AS
380
369
$$
381
370
DECLARE
382
371
part_count INTEGER := 0 ;
383
- v_tablespace TEXT ;
384
372
385
373
BEGIN
386
374
IF partition_data = true THEN
@@ -409,9 +397,6 @@ BEGIN
409
397
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
410
398
VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
411
399
412
- /* Determine tablespace of parent table */
413
- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
414
-
415
400
WHILE p_start_value <= p_end_value
416
401
LOOP
417
402
EXECUTE
@@ -421,7 +406,7 @@ BEGIN
421
406
parent_relid,
422
407
p_start_value,
423
408
p_start_value + p_interval,
424
- v_tablespace ;
409
+ @extschema@.get_rel_tablespace_name(parent_relid) ;
425
410
426
411
p_start_value := p_start_value + p_interval;
427
412
part_count := part_count + 1 ;
@@ -463,7 +448,8 @@ DECLARE
463
448
v_plain_relname TEXT ;
464
449
v_child_relname_exists BOOL;
465
450
v_seq_name TEXT ;
466
- v_create_table_query TEXT ;
451
+ v_init_callback REGPROCEDURE;
452
+
467
453
BEGIN
468
454
v_attname := attname FROM @extschema@.pathman_config
469
455
WHERE partrel = parent_relid;
@@ -498,16 +484,15 @@ BEGIN
498
484
v_child_relname := partition_name;
499
485
END IF;
500
486
501
- v_create_table_query := ' CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)' ;
502
-
503
- /* If tablespace is specified then add it to a create query */
504
- if NOT tablespace IS NULL THEN
505
- v_create_table_query := v_create_table_query || ' TABLESPACE ' || tablespace;
487
+ IF tablespace IS NULL THEN
488
+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
506
489
END IF;
507
490
508
- EXECUTE format(v_create_table_query,
491
+ EXECUTE format(' CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL)
492
+ INHERITS (%2$s) TABLESPACE %3$s' ,
509
493
v_child_relname,
510
- parent_relid::TEXT );
494
+ parent_relid::TEXT ,
495
+ tablespace);
511
496
512
497
EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
513
498
v_child_relname,
@@ -518,8 +503,20 @@ BEGIN
518
503
p_end_value));
519
504
520
505
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
506
+
507
+ /* Fetch init_callback from 'params' table */
508
+ WITH stub_callback(stub) as (values (0 ))
509
+ SELECT coalesce(init_callback, 0 ::REGPROCEDURE)
510
+ FROM stub_callback
511
+ LEFT JOIN @extschema@.pathman_config_params AS params
512
+ ON params .partrel = parent_relid
513
+ INTO v_init_callback;
514
+
521
515
PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
522
- v_child_relname::REGCLASS);
516
+ v_child_relname::REGCLASS,
517
+ v_init_callback,
518
+ p_start_value,
519
+ p_end_value);
523
520
524
521
RETURN v_child_relname;
525
522
END
@@ -817,11 +814,6 @@ BEGIN
817
814
RAISE EXCEPTION ' Cannot append to empty partitions set' ;
818
815
END IF;
819
816
820
- /* If tablespace isn't specified then choose parent's tablespace */
821
- IF tablespace IS NULL THEN
822
- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
823
- END IF;
824
-
825
817
p_range := @extschema@.get_range_by_idx(parent_relid, - 1 , 0 );
826
818
827
819
IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -922,11 +914,6 @@ BEGIN
922
914
RAISE EXCEPTION ' Cannot prepend to empty partitions set' ;
923
915
END IF;
924
916
925
- /* If tablespace isn't specified then choose parent's tablespace */
926
- IF tablespace IS NULL THEN
927
- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
928
- END IF;
929
-
930
917
p_range := @extschema@.get_range_by_idx(parent_relid, 0 , 0 );
931
918
932
919
IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -985,11 +972,6 @@ BEGIN
985
972
RAISE EXCEPTION ' Specified range overlaps with existing partitions' ;
986
973
END IF;
987
974
988
- /* If tablespace isn't specified then choose parent's tablespace */
989
- IF tablespace IS NULL THEN
990
- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
991
- END IF;
992
-
993
975
/* Create new partition */
994
976
v_part_name := @extschema@.create_single_range_partition(parent_relid,
995
977
p_start_value,
0 commit comments