Skip to content

Commit dfbbfeb

Browse files
committed
improve partition creation callbacks subsystem (robust JSONB generation), rename some 'params'-related fuctions, remove useless code
1 parent ef2be57 commit dfbbfeb

File tree

10 files changed

+238
-257
lines changed

10 files changed

+238
-257
lines changed

expected/pg_pathman.out

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ SELECT * FROM test.hash_rel;
3535
3 | 3
3636
(3 rows)
3737

38-
SELECT pathman.disable_parent('test.hash_rel');
39-
disable_parent
40-
----------------
38+
SELECT pathman.set_enable_parent('test.hash_rel', false);
39+
set_enable_parent
40+
-------------------
4141

4242
(1 row)
4343

@@ -55,9 +55,9 @@ SELECT * FROM test.hash_rel;
5555
----+-------
5656
(0 rows)
5757

58-
SELECT pathman.enable_parent('test.hash_rel');
59-
enable_parent
60-
---------------
58+
SELECT pathman.set_enable_parent('test.hash_rel', true);
59+
set_enable_parent
60+
-------------------
6161

6262
(1 row)
6363

@@ -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.disable_auto('test.range_rel');
1264-
disable_auto
1265-
--------------
1263+
SELECT pathman.set_auto_partitioning('test.range_rel', false);
1264+
set_auto_partitioning
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.enable_auto('test.range_rel');
1272-
enable_auto
1273-
-------------
1271+
SELECT pathman.set_auto_partitioning('test.range_rel', true);
1272+
set_auto_partitioning
1273+
-----------------------
12741274

12751275
(1 row)
12761276

@@ -1770,11 +1770,11 @@ NOTICE: 100 rows copied from test_fkey_0
17701770

17711771
/* Check callbacks */
17721772
CREATE TABLE log(id serial, message text);
1773-
CREATE OR REPLACE FUNCTION abc_on_partition_created_callback(args jsonb)
1773+
CREATE OR REPLACE FUNCTION abc_on_partition_created_callback(args JSONB)
17741774
RETURNS VOID AS $$
17751775
DECLARE
1776-
start_value TEXT := args->>'start';
1777-
end_value TEXT := args::jsonb->'end';
1776+
start_value TEXT := args->>'start';
1777+
end_value TEXT := args->'end';
17781778
BEGIN
17791779
INSERT INTO log(message)
17801780
VALUES (start_value || '-' || end_value);
@@ -1788,9 +1788,9 @@ NOTICE: sequence "abc_seq" does not exist, skipping
17881788
2
17891789
(1 row)
17901790

1791-
SELECT set_callback('abc', 'abc_on_partition_created_callback');
1792-
set_callback
1793-
--------------
1791+
SELECT set_part_init_callback('abc', 'abc_on_partition_created_callback');
1792+
set_part_init_callback
1793+
------------------------
17941794

17951795
(1 row)
17961796

@@ -1814,12 +1814,12 @@ SELECT add_range_partition('abc', 401, 501);
18141814
public.abc_6
18151815
(1 row)
18161816

1817-
SELECT message FROM log;
1818-
message
1819-
---------
1820-
201-301
1821-
301-401
1822-
-99-1
1823-
401-501
1817+
SELECT message FROM log ORDER BY id;
1818+
message
1819+
-----------
1820+
201-"301"
1821+
301-"401"
1822+
-99-"1"
1823+
401-"501"
18241824
(4 rows)
18251825

hash.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ BEGIN
8484

8585
/* Copy data */
8686
IF partition_data = true THEN
87-
PERFORM @extschema@.disable_parent(parent_relid);
87+
PERFORM @extschema@.set_enable_parent(parent_relid, false);
8888
PERFORM @extschema@.partition_data(parent_relid);
8989
ELSE
90-
PERFORM @extschema@.enable_parent(parent_relid);
90+
PERFORM @extschema@.set_enable_parent(parent_relid, true);
9191
END IF;
9292

9393
RETURN partitions_count;

init.sql

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
3131
* partrel - regclass (relation type, stored as Oid)
3232
* enable_parent - add parent table to plan
3333
* auto - enable automatic partition creation
34-
* callback -
34+
* init_callback - cb to be executed on partition creation
3535
*/
3636
CREATE TABLE IF NOT EXISTS @extschema@.pathman_config_params (
3737
partrel REGCLASS NOT NULL PRIMARY KEY,
3838
enable_parent BOOLEAN NOT NULL DEFAULT TRUE,
3939
auto BOOLEAN NOT NULL DEFAULT TRUE,
40-
callback REGPROCEDURE
40+
init_callback REGPROCEDURE NOT NULL DEFAULT 0
4141
);
4242
CREATE UNIQUE INDEX i_pathman_config_params
4343
ON @extschema@.pathman_config_params(partrel);
@@ -87,7 +87,7 @@ BEGIN
8787
RETURN count(*) FROM pg_inherits WHERE inhparent = relation;
8888
END
8989
$$
90-
LANGUAGE plpgsql;
90+
LANGUAGE plpgsql STRICT;
9191

9292
/*
9393
* Add a row describing the optional parameter to pathman_config_params.
@@ -108,62 +108,44 @@ $$
108108
LANGUAGE plpgsql;
109109

110110
/*
111-
* Include parent relation into query plan's for specified relation.
112-
*/
113-
CREATE OR REPLACE FUNCTION @extschema@.enable_parent(relation REGCLASS)
114-
RETURNS VOID AS
115-
$$
116-
BEGIN
117-
PERFORM @extschema@.pathman_set_param(relation, 'enable_parent', True);
118-
END
119-
$$
120-
LANGUAGE plpgsql;
121-
122-
/*
123-
* Do not include parent relation into query plan's for specified relation.
111+
* Include\exclude parent relation in query plan.
124112
*/
125-
CREATE OR REPLACE FUNCTION @extschema@.disable_parent(relation REGCLASS)
126-
RETURNS VOID AS
127-
$$
128-
BEGIN
129-
PERFORM @extschema@.pathman_set_param(relation, 'enable_parent', False);
130-
END
131-
$$
132-
LANGUAGE plpgsql;
133-
134-
/*
135-
* Enable automatic partition creation.
136-
*/
137-
CREATE OR REPLACE FUNCTION @extschema@.enable_auto(relation REGCLASS)
113+
CREATE OR REPLACE FUNCTION @extschema@.set_enable_parent(
114+
relation REGCLASS,
115+
value BOOLEAN)
138116
RETURNS VOID AS
139117
$$
140118
BEGIN
141-
PERFORM @extschema@.pathman_set_param(relation, 'auto', True);
119+
PERFORM @extschema@.pathman_set_param(relation, 'enable_parent', value);
142120
END
143121
$$
144-
LANGUAGE plpgsql;
122+
LANGUAGE plpgsql STRICT;
145123

146124
/*
147-
* Disable automatic partition creation.
125+
* Enable\disable automatic partition creation.
148126
*/
149-
CREATE OR REPLACE FUNCTION @extschema@.disable_auto(relation REGCLASS)
127+
CREATE OR REPLACE FUNCTION @extschema@.set_auto_partitioning(
128+
relation REGCLASS,
129+
value BOOLEAN)
150130
RETURNS VOID AS
151131
$$
152132
BEGIN
153-
PERFORM @extschema@.pathman_set_param(relation, 'auto', False);
133+
PERFORM @extschema@.pathman_set_param(relation, 'auto', value);
154134
END
155135
$$
156-
LANGUAGE plpgsql;
136+
LANGUAGE plpgsql STRICT;
157137

158138
/*
159139
* Set partition creation callback
160140
*/
161-
CREATE OR REPLACE FUNCTION @extschema@.set_callback(relation REGCLASS, callback REGPROC)
141+
CREATE OR REPLACE FUNCTION @extschema@.set_part_init_callback(
142+
relation REGCLASS,
143+
callback REGPROC)
162144
RETURNS VOID AS
163145
$$
164146
BEGIN
165147
PERFORM @extschema@.validate_on_partition_created_callback(callback);
166-
PERFORM @extschema@.pathman_set_param(relation, 'callback', callback);
148+
PERFORM @extschema@.pathman_set_param(relation, 'init_callback', callback);
167149
END
168150
$$
169151
LANGUAGE plpgsql;
@@ -262,7 +244,7 @@ BEGIN
262244
RETURN;
263245
END
264246
$$
265-
LANGUAGE plpgsql
247+
LANGUAGE plpgsql STRICT
266248
SET pg_pathman.enable_partitionfilter = on; /* ensures that PartitionFilter is ON */
267249

268250
/*
@@ -291,7 +273,7 @@ BEGIN
291273
RETURN;
292274
END
293275
$$
294-
LANGUAGE plpgsql
276+
LANGUAGE plpgsql STRICT
295277
SET pg_pathman.enable_partitionfilter = on; /* ensures that PartitionFilter is ON */
296278

297279
/*
@@ -311,7 +293,7 @@ BEGIN
311293
PERFORM @extschema@.on_remove_partitions(parent_relid);
312294
END
313295
$$
314-
LANGUAGE plpgsql;
296+
LANGUAGE plpgsql STRICT;
315297

316298
/*
317299
* Aggregates several common relation checks before partitioning.
@@ -380,7 +362,7 @@ BEGIN
380362
INTO schema, relname;
381363
END
382364
$$
383-
LANGUAGE plpgsql;
365+
LANGUAGE plpgsql STRICT;
384366

385367
/*
386368
* Returns schema-qualified name for table
@@ -399,7 +381,7 @@ BEGIN
399381
WHERE oid = cls::oid);
400382
END
401383
$$
402-
LANGUAGE plpgsql;
384+
LANGUAGE plpgsql STRICT;
403385

404386
/*
405387
* Validates relation name. It must be schema qualified
@@ -499,7 +481,7 @@ BEGIN
499481
EXECUTE format('DROP FUNCTION IF EXISTS %s() CASCADE',
500482
@extschema@.build_update_trigger_func_name(parent_relid));
501483
END
502-
$$ LANGUAGE plpgsql;
484+
$$ LANGUAGE plpgsql STRICT;
503485

504486
/*
505487
* Drop partitions
@@ -584,7 +566,7 @@ BEGIN
584566
pg_get_constraintdef(rec.conid));
585567
END LOOP;
586568
END
587-
$$ LANGUAGE plpgsql;
569+
$$ LANGUAGE plpgsql STRICT;
588570

589571

590572
/*
@@ -712,28 +694,25 @@ RETURNS VOID AS 'pg_pathman', 'debug_capture'
712694
LANGUAGE C STRICT;
713695

714696
/*
715-
* Return tablespace name for specified relation
697+
* Return tablespace name for specified relation.
716698
*/
717699
CREATE OR REPLACE FUNCTION @extschema@.get_rel_tablespace_name(relation REGCLASS)
718700
RETURNS TEXT AS 'pg_pathman', 'get_rel_tablespace_name'
719701
LANGUAGE C STRICT;
720702

721703
/*
722704
* Checks that callback function meets specific requirements. Particularly it
723-
* must have the only JSONB argument and VOID return type
705+
* must have the only JSONB argument and VOID return type.
724706
*/
725707
CREATE OR REPLACE FUNCTION @extschema@.validate_on_partition_created_callback(callback REGPROC)
726-
RETURNS VOID AS 'pg_pathman', 'validate_on_partition_created_callback'
708+
RETURNS VOID AS 'pg_pathman', 'validate_on_part_init_callback_pl'
727709
LANGUAGE C STRICT;
728710

729711
/*
730-
* Builds JSONB object containing new partition parameters and invoke the
731-
* callback
712+
* Builds JSONB object containing new partition parameters and invoke the callback.
732713
*/
733714
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
734-
parent REGCLASS,
735-
partition REGCLASS,
736-
start_value ANYELEMENT,
737-
end_value ANYELEMENT)
738-
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
739-
LANGUAGE C STRICT;
715+
parent_relid REGCLASS,
716+
partition REGCLASS)
717+
RETURNS JSONB AS 'pg_pathman', 'invoke_on_partition_created_callback'
718+
LANGUAGE C;

range.sql

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ BEGIN
172172

173173
/* Relocate data if asked to */
174174
IF partition_data = true THEN
175-
PERFORM @extschema@.disable_parent(parent_relid);
175+
PERFORM @extschema@.set_enable_parent(parent_relid, false);
176176
PERFORM @extschema@.partition_data(parent_relid);
177177
ELSE
178-
PERFORM @extschema@.enable_parent(parent_relid);
178+
PERFORM @extschema@.set_enable_parent(parent_relid, true);
179179
END IF;
180180

181181
RETURN p_count;
@@ -279,10 +279,10 @@ BEGIN
279279

280280
/* Relocate data if asked to */
281281
IF partition_data = true THEN
282-
PERFORM @extschema@.disable_parent(parent_relid);
282+
PERFORM @extschema@.set_enable_parent(parent_relid, false);
283283
PERFORM @extschema@.partition_data(parent_relid);
284284
ELSE
285-
PERFORM @extschema@.enable_parent(parent_relid);
285+
PERFORM @extschema@.set_enable_parent(parent_relid, true);
286286
END IF;
287287

288288
RETURN p_count;
@@ -356,10 +356,10 @@ BEGIN
356356

357357
/* Relocate data if asked to */
358358
IF partition_data = true THEN
359-
PERFORM @extschema@.disable_parent(parent_relid);
359+
PERFORM @extschema@.set_enable_parent(parent_relid, false);
360360
PERFORM @extschema@.partition_data(parent_relid);
361361
ELSE
362-
PERFORM @extschema@.enable_parent(parent_relid);
362+
PERFORM @extschema@.set_enable_parent(parent_relid, true);
363363
END IF;
364364

365365
RETURN part_count; /* number of created partitions */
@@ -432,10 +432,10 @@ BEGIN
432432

433433
/* Relocate data if asked to */
434434
IF partition_data = true THEN
435-
PERFORM @extschema@.disable_parent(parent_relid);
435+
PERFORM @extschema@.set_enable_parent(parent_relid, false);
436436
PERFORM @extschema@.partition_data(parent_relid);
437437
ELSE
438-
PERFORM @extschema@.enable_parent(parent_relid);
438+
PERFORM @extschema@.set_enable_parent(parent_relid, true);
439439
END IF;
440440

441441
RETURN part_count; /* number of created partitions */
@@ -519,9 +519,7 @@ BEGIN
519519

520520
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
521521
PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
522-
v_child_relname,
523-
p_start_value,
524-
p_end_value);
522+
v_child_relname::REGCLASS);
525523

526524
RETURN v_child_relname;
527525
END

0 commit comments

Comments
 (0)