Skip to content

Commit 1385218

Browse files
committed
(performance) do not fetch PartRelationInfo in invoke_on_partition_created_callback(), rename callback -> init_callback, fixes
1 parent dfbbfeb commit 1385218

File tree

9 files changed

+115
-113
lines changed

9 files changed

+115
-113
lines changed

expected/pg_pathman.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,17 +1260,17 @@ SELECT * FROM test.range_rel WHERE dt = '2015-03-15';
12601260
74 | Sun Mar 15 00:00:00 2015
12611261
(1 row)
12621262

1263-
SELECT pathman.set_auto_partitioning('test.range_rel', false);
1264-
set_auto_partitioning
1265-
-----------------------
1263+
SELECT pathman.set_auto('test.range_rel', false);
1264+
set_auto
1265+
----------
12661266

12671267
(1 row)
12681268

12691269
INSERT INTO test.range_rel (dt) VALUES ('2015-06-01');
12701270
ERROR: There is no suitable partition for key 'Mon Jun 01 00:00:00 2015'
1271-
SELECT pathman.set_auto_partitioning('test.range_rel', true);
1272-
set_auto_partitioning
1273-
-----------------------
1271+
SELECT pathman.set_auto('test.range_rel', true);
1272+
set_auto
1273+
----------
12741274

12751275
(1 row)
12761276

hash.sql

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ DECLARE
2424
v_plain_schema TEXT;
2525
v_plain_relname TEXT;
2626
v_hashfunc TEXT;
27-
v_tablespace TEXT;
27+
v_init_callback REGPROCEDURE;
2828

2929
BEGIN
3030
IF partition_data = true THEN
@@ -50,9 +50,6 @@ BEGIN
5050
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype)
5151
VALUES (parent_relid, attribute, 1);
5252

53-
/* Determine tablespace of parent table */
54-
v_tablespace := @extschema@.get_rel_tablespace_name(parent_relid);
55-
5653
/* Create partitions and update pg_pathman configuration */
5754
FOR partnum IN 0..partitions_count-1
5855
LOOP
@@ -64,7 +61,7 @@ BEGIN
6461
'CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s) TABLESPACE %s',
6562
v_child_relname,
6663
parent_relid::TEXT,
67-
v_tablespace);
64+
@extschema@.get_rel_tablespace_name(parent_relid));
6865

6966
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
7067
CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',
@@ -77,6 +74,18 @@ BEGIN
7774
partnum);
7875

7976
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
77+
78+
/* Fetch init_callback from 'params' table */
79+
WITH stub_callback(stub) as (values (0))
80+
SELECT coalesce(init_callback, 0::REGPROCEDURE)
81+
FROM stub_callback
82+
LEFT JOIN @extschema@.pathman_config_params AS params
83+
ON params.partrel = parent_relid
84+
INTO v_init_callback;
85+
86+
PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
87+
v_child_relname::REGCLASS,
88+
v_init_callback);
8089
END LOOP;
8190

8291
/* Notify backend about changes */

init.sql

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ LANGUAGE plpgsql STRICT;
124124
/*
125125
* Enable\disable automatic partition creation.
126126
*/
127-
CREATE OR REPLACE FUNCTION @extschema@.set_auto_partitioning(
127+
CREATE OR REPLACE FUNCTION @extschema@.set_auto(
128128
relation REGCLASS,
129129
value BOOLEAN)
130130
RETURNS VOID AS
@@ -713,6 +713,16 @@ LANGUAGE C STRICT;
713713
*/
714714
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
715715
parent_relid REGCLASS,
716-
partition REGCLASS)
717-
RETURNS JSONB AS 'pg_pathman', 'invoke_on_partition_created_callback'
716+
partition REGCLASS,
717+
init_callback REGPROCEDURE,
718+
start_value ANYELEMENT,
719+
end_value ANYELEMENT)
720+
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
721+
LANGUAGE C;
722+
723+
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
724+
parent_relid REGCLASS,
725+
partition REGCLASS,
726+
init_callback REGPROCEDURE)
727+
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
718728
LANGUAGE C;

range.sql

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
8888
RETURNS INTEGER AS
8989
$$
9090
DECLARE
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+
9696
BEGIN
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)
195192
RETURNS INTEGER AS
196193
$$
197194
DECLARE
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

204200
BEGIN
@@ -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)
302295
RETURNS INTEGER AS
303296
$$
304297
DECLARE
305298
part_count INTEGER := 0;
306-
v_tablespace TEXT;
307299

308300
BEGIN
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)
379368
RETURNS INTEGER AS
380369
$$
381370
DECLARE
382371
part_count INTEGER := 0;
383-
v_tablespace TEXT;
384372

385373
BEGIN
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+
467453
BEGIN
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;
525522
END
@@ -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,

sql/pg_pathman.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ SELECT * FROM test.range_rel WHERE dt = '2014-12-15';
525525
EXPLAIN (COSTS OFF) SELECT * FROM test.range_rel WHERE dt = '2015-03-15';
526526
SELECT * FROM test.range_rel WHERE dt = '2015-03-15';
527527

528-
SELECT pathman.set_auto_partitioning('test.range_rel', false);
528+
SELECT pathman.set_auto('test.range_rel', false);
529529
INSERT INTO test.range_rel (dt) VALUES ('2015-06-01');
530-
SELECT pathman.set_auto_partitioning('test.range_rel', true);
530+
SELECT pathman.set_auto('test.range_rel', true);
531531
INSERT INTO test.range_rel (dt) VALUES ('2015-06-01');
532532

533533
DROP TABLE test.range_rel CASCADE;

src/init.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,12 @@ read_pathman_params(Oid relid, Datum *values, bool *isnull)
661661
/* Extract data if necessary */
662662
heap_deform_tuple(htup, RelationGetDescr(rel), values, isnull);
663663
row_found = true;
664+
665+
/* Perform checks for non-NULL columns */
666+
Assert(!isnull[Anum_pathman_config_params_partrel - 1]);
667+
Assert(!isnull[Anum_pathman_config_params_enable_parent - 1]);
668+
Assert(!isnull[Anum_pathman_config_params_auto - 1]);
669+
Assert(!isnull[Anum_pathman_config_params_init_callback - 1]);
664670
}
665671

666672
/* Clean resources */

0 commit comments

Comments
 (0)