@@ -84,15 +84,15 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
8484 p_start_value ANYELEMENT,
8585 p_interval INTERVAL,
8686 p_count INTEGER DEFAULT NULL ,
87- partition_data BOOLEAN DEFAULT true )
87+ partition_data BOOLEAN DEFAULT TRUE )
8888RETURNS INTEGER AS
8989$$
9090DECLARE
9191 v_rows_count INTEGER ;
9292 v_max p_start_value%TYPE;
9393 v_cur_value p_start_value%TYPE := p_start_value;
94- v_tablespace TEXT ;
9594 i INTEGER ;
95+
9696BEGIN
9797 IF partition_data = true THEN
9898 /* Acquire data modification lock */
@@ -149,9 +149,6 @@ BEGIN
149149 INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
150150 VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
151151
152- /* Determine tablespace of parent table */
153- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
154-
155152 /* Create first partition */
156153 FOR i IN 1 ..p_count
157154 LOOP
@@ -162,7 +159,7 @@ BEGIN
162159 parent_relid,
163160 p_start_value,
164161 p_start_value + p_interval,
165- v_tablespace ;
162+ @extschema@.get_rel_tablespace_name(parent_relid) ;
166163
167164 p_start_value := p_start_value + p_interval;
168165 END LOOP;
@@ -191,14 +188,13 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
191188 p_start_value ANYELEMENT,
192189 p_interval ANYELEMENT,
193190 p_count INTEGER DEFAULT NULL ,
194- partition_data BOOLEAN DEFAULT true )
191+ partition_data BOOLEAN DEFAULT TRUE )
195192RETURNS INTEGER AS
196193$$
197194DECLARE
198195 v_rows_count INTEGER ;
199196 v_max p_start_value%TYPE;
200197 v_cur_value p_start_value%TYPE := p_start_value;
201- v_tablespace TEXT ;
202198 i INTEGER ;
203199
204200BEGIN
@@ -259,17 +255,14 @@ BEGIN
259255 INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
260256 VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
261257
262- /* Determine tablespace of parent table */
263- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
264-
265258 /* create first partition */
266259 FOR i IN 1 ..p_count
267260 LOOP
268261 PERFORM @extschema@.create_single_range_partition(
269262 parent_relid,
270263 p_start_value,
271264 p_start_value + p_interval,
272- tablespace := v_tablespace );
265+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
273266
274267 p_start_value := p_start_value + p_interval;
275268 END LOOP;
@@ -298,12 +291,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
298291 p_start_value ANYELEMENT,
299292 p_end_value ANYELEMENT,
300293 p_interval ANYELEMENT,
301- partition_data BOOLEAN DEFAULT true )
294+ partition_data BOOLEAN DEFAULT TRUE )
302295RETURNS INTEGER AS
303296$$
304297DECLARE
305298 part_count INTEGER := 0 ;
306- v_tablespace TEXT ;
307299
308300BEGIN
309301 IF partition_data = true THEN
@@ -336,16 +328,13 @@ BEGIN
336328 INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
337329 VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
338330
339- /* Determine tablespace of parent table */
340- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
341-
342331 WHILE p_start_value <= p_end_value
343332 LOOP
344333 PERFORM @extschema@.create_single_range_partition(
345334 parent_relid,
346335 p_start_value,
347336 p_start_value + p_interval,
348- tablespace := v_tablespace );
337+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
349338
350339 p_start_value := p_start_value + p_interval;
351340 part_count := part_count + 1 ;
@@ -375,12 +364,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
375364 p_start_value ANYELEMENT,
376365 p_end_value ANYELEMENT,
377366 p_interval INTERVAL,
378- partition_data BOOLEAN DEFAULT true )
367+ partition_data BOOLEAN DEFAULT TRUE )
379368RETURNS INTEGER AS
380369$$
381370DECLARE
382371 part_count INTEGER := 0 ;
383- v_tablespace TEXT ;
384372
385373BEGIN
386374 IF partition_data = true THEN
@@ -409,9 +397,6 @@ BEGIN
409397 INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
410398 VALUES (parent_relid, p_attribute, 2 , p_interval::TEXT );
411399
412- /* Determine tablespace of parent table */
413- v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
414-
415400 WHILE p_start_value <= p_end_value
416401 LOOP
417402 EXECUTE
@@ -421,7 +406,7 @@ BEGIN
421406 parent_relid,
422407 p_start_value,
423408 p_start_value + p_interval,
424- v_tablespace ;
409+ @extschema@.get_rel_tablespace_name(parent_relid) ;
425410
426411 p_start_value := p_start_value + p_interval;
427412 part_count := part_count + 1 ;
@@ -463,7 +448,8 @@ DECLARE
463448 v_plain_relname TEXT ;
464449 v_child_relname_exists BOOL;
465450 v_seq_name TEXT ;
466- v_create_table_query TEXT ;
451+ v_init_callback REGPROCEDURE;
452+
467453BEGIN
468454 v_attname := attname FROM @extschema@.pathman_config
469455 WHERE partrel = parent_relid;
@@ -498,16 +484,15 @@ BEGIN
498484 v_child_relname := partition_name;
499485 END IF;
500486
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);
506489 END IF;
507490
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' ,
509493 v_child_relname,
510- parent_relid::TEXT );
494+ parent_relid::TEXT ,
495+ tablespace);
511496
512497 EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
513498 v_child_relname,
@@ -518,8 +503,20 @@ BEGIN
518503 p_end_value));
519504
520505 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+
521515 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);
523520
524521 RETURN v_child_relname;
525522END
@@ -817,11 +814,6 @@ BEGIN
817814 RAISE EXCEPTION ' Cannot append to empty partitions set' ;
818815 END IF;
819816
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-
825817 p_range := @extschema@.get_range_by_idx(parent_relid, - 1 , 0 );
826818
827819 IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -922,11 +914,6 @@ BEGIN
922914 RAISE EXCEPTION ' Cannot prepend to empty partitions set' ;
923915 END IF;
924916
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-
930917 p_range := @extschema@.get_range_by_idx(parent_relid, 0 , 0 );
931918
932919 IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -985,11 +972,6 @@ BEGIN
985972 RAISE EXCEPTION ' Specified range overlaps with existing partitions' ;
986973 END IF;
987974
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-
993975 /* Create new partition */
994976 v_part_name := @extschema@.create_single_range_partition(parent_relid,
995977 p_start_value,
0 commit comments