@@ -84,15 +84,15 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
8484p_start_valueANYELEMENT,
8585p_intervalINTERVAL,
8686p_countINTEGER DEFAULTNULL ,
87- partition_dataBOOLEAN DEFAULTtrue )
87+ partition_dataBOOLEAN DEFAULTTRUE )
8888RETURNSINTEGER AS
8989$$
9090DECLARE
9191v_rows_countINTEGER ;
9292v_maxp_start_value%TYPE;
9393v_cur_valuep_start_value%TYPE := p_start_value;
94- v_tablespaceTEXT ;
9594iINTEGER ;
95+
9696BEGIN
9797IF partition_data= true THEN
9898/* Acquire data modification lock*/
@@ -149,9 +149,6 @@ BEGIN
149149INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
150150VALUES (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*/
156153FOR iIN 1 ..p_count
157154LOOP
@@ -162,7 +159,7 @@ BEGIN
162159parent_relid,
163160p_start_value,
164161p_start_value+ p_interval,
165- v_tablespace ;
162+ @extschema@.get_rel_tablespace_name(parent_relid) ;
166163
167164p_start_value := p_start_value+ p_interval;
168165END LOOP;
@@ -191,14 +188,13 @@ CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
191188p_start_valueANYELEMENT,
192189p_intervalANYELEMENT,
193190p_countINTEGER DEFAULTNULL ,
194- partition_dataBOOLEAN DEFAULTtrue )
191+ partition_dataBOOLEAN DEFAULTTRUE )
195192RETURNSINTEGER AS
196193$$
197194DECLARE
198195v_rows_countINTEGER ;
199196v_maxp_start_value%TYPE;
200197v_cur_valuep_start_value%TYPE := p_start_value;
201- v_tablespaceTEXT ;
202198iINTEGER ;
203199
204200BEGIN
@@ -259,17 +255,14 @@ BEGIN
259255INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
260256VALUES (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*/
266259FOR iIN 1 ..p_count
267260LOOP
268261PERFORM @extschema@.create_single_range_partition(
269262parent_relid,
270263p_start_value,
271264p_start_value+ p_interval,
272- tablespace := v_tablespace );
265+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
273266
274267p_start_value := p_start_value+ p_interval;
275268END LOOP;
@@ -298,12 +291,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
298291p_start_valueANYELEMENT,
299292p_end_valueANYELEMENT,
300293p_intervalANYELEMENT,
301- partition_dataBOOLEAN DEFAULTtrue )
294+ partition_dataBOOLEAN DEFAULTTRUE )
302295RETURNSINTEGER AS
303296$$
304297DECLARE
305298part_countINTEGER := 0 ;
306- v_tablespaceTEXT ;
307299
308300BEGIN
309301IF partition_data= true THEN
@@ -336,16 +328,13 @@ BEGIN
336328INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
337329VALUES (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-
342331WHILE p_start_value<= p_end_value
343332LOOP
344333PERFORM @extschema@.create_single_range_partition(
345334parent_relid,
346335p_start_value,
347336p_start_value+ p_interval,
348- tablespace := v_tablespace );
337+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid) );
349338
350339p_start_value := p_start_value+ p_interval;
351340part_count := part_count+ 1 ;
@@ -375,12 +364,11 @@ CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
375364p_start_valueANYELEMENT,
376365p_end_valueANYELEMENT,
377366p_intervalINTERVAL,
378- partition_dataBOOLEAN DEFAULTtrue )
367+ partition_dataBOOLEAN DEFAULTTRUE )
379368RETURNSINTEGER AS
380369$$
381370DECLARE
382371part_countINTEGER := 0 ;
383- v_tablespaceTEXT ;
384372
385373BEGIN
386374IF partition_data= true THEN
@@ -409,9 +397,6 @@ BEGIN
409397INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
410398VALUES (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-
415400WHILE p_start_value<= p_end_value
416401LOOP
417402EXECUTE
@@ -421,7 +406,7 @@ BEGIN
421406parent_relid,
422407p_start_value,
423408p_start_value+ p_interval,
424- v_tablespace ;
409+ @extschema@.get_rel_tablespace_name(parent_relid) ;
425410
426411p_start_value := p_start_value+ p_interval;
427412part_count := part_count+ 1 ;
@@ -463,7 +448,8 @@ DECLARE
463448v_plain_relnameTEXT ;
464449v_child_relname_existsBOOL;
465450v_seq_nameTEXT ;
466- v_create_table_queryTEXT ;
451+ v_init_callbackREGPROCEDURE;
452+
467453BEGIN
468454v_attname := attnameFROM @extschema@.pathman_config
469455WHERE partrel= parent_relid;
@@ -498,16 +484,15 @@ BEGIN
498484v_child_relname := partition_name;
499485END 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 ISNULL THEN
505- v_create_table_query := v_create_table_query|| ' TABLESPACE' || tablespace;
487+ IF tablespace ISNULL THEN
488+ tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
506489END 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
512497EXECUTE format(' ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)' ,
513498 v_child_relname,
@@ -518,8 +503,20 @@ BEGIN
518503 p_end_value));
519504
520505PERFORM @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_paramsAS params
512+ ON params .partrel = parent_relid
513+ INTO v_init_callback;
514+
521515PERFORM @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
524521RETURN v_child_relname;
525522END
@@ -817,11 +814,6 @@ BEGIN
817814RAISE EXCEPTION' Cannot append to empty partitions set' ;
818815END IF;
819816
820- /* If tablespace isn't specified then choose parent's tablespace*/
821- IF tablespace ISNULL THEN
822- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
823- END IF;
824-
825817p_range := @extschema@.get_range_by_idx(parent_relid,- 1 ,0 );
826818
827819IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -922,11 +914,6 @@ BEGIN
922914RAISE EXCEPTION' Cannot prepend to empty partitions set' ;
923915END IF;
924916
925- /* If tablespace isn't specified then choose parent's tablespace*/
926- IF tablespace ISNULL THEN
927- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
928- END IF;
929-
930917p_range := @extschema@.get_range_by_idx(parent_relid,0 ,0 );
931918
932919IF @extschema@.is_date_type(p_atttype::regtype) THEN
@@ -985,11 +972,6 @@ BEGIN
985972RAISE EXCEPTION' Specified range overlaps with existing partitions' ;
986973END IF;
987974
988- /* If tablespace isn't specified then choose parent's tablespace*/
989- IF tablespace ISNULL THEN
990- tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
991- END IF;
992-
993975/* Create new partition*/
994976v_part_name := @extschema@.create_single_range_partition(parent_relid,
995977 p_start_value,