Skip to content

Commit d1556b4

Browse files
committed
refactoring, add column 'spawn_using_bgw' to PATHMAN_CONFIG_PARAMS
1 parent 2a31969 commit d1556b4

17 files changed

+201
-131
lines changed

expected/pathman_callbacks.out

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,66 @@ SELECT set_init_callback('callbacks.abc',
2626
(1 row)
2727

2828
INSERT INTO callbacks.abc VALUES (123, 1);
29-
INSERT INTO callbacks.abc VALUES (223, 1);
29+
INSERT INTO callbacks.abc VALUES (223, 1); /* show warning */
30+
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_3", "range_max": "301", "range_min": "201"}
31+
SELECT set_spawn_using_bgw('callbacks.abc', true);
32+
set_spawn_using_bgw
33+
---------------------
34+
35+
(1 row)
36+
37+
SELECT get_number_of_partitions('callbacks.abc');
38+
get_number_of_partitions
39+
--------------------------
40+
3
41+
(1 row)
42+
43+
INSERT INTO callbacks.abc VALUES (323, 1);
44+
SELECT get_number_of_partitions('callbacks.abc'); /* +1 partition (created by BGW) */
45+
get_number_of_partitions
46+
--------------------------
47+
4
48+
(1 row)
49+
50+
SELECT set_spawn_using_bgw('callbacks.abc', false);
51+
set_spawn_using_bgw
52+
---------------------
53+
54+
(1 row)
55+
3056
SELECT append_range_partition('callbacks.abc');
31-
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_4", "range_max": "401", "range_min": "301"}
57+
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_5", "range_max": "501", "range_min": "401"}
3258
append_range_partition
3359
------------------------
34-
callbacks.abc_4
60+
callbacks.abc_5
3561
(1 row)
3662

3763
SELECT prepend_range_partition('callbacks.abc');
38-
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_5", "range_max": "1", "range_min": "-99"}
64+
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_6", "range_max": "1", "range_min": "-99"}
3965
prepend_range_partition
4066
-------------------------
41-
callbacks.abc_5
67+
callbacks.abc_6
4268
(1 row)
4369

44-
SELECT add_range_partition('callbacks.abc', 401, 502);
45-
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_6", "range_max": "502", "range_min": "401"}
70+
SELECT add_range_partition('callbacks.abc', 501, 602);
71+
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_7", "range_max": "602", "range_min": "501"}
4672
add_range_partition
4773
---------------------
48-
callbacks.abc_6
74+
callbacks.abc_7
4975
(1 row)
5076

5177
SELECT drop_partitions('callbacks.abc');
5278
NOTICE: function callbacks.abc_upd_trig_func() does not exist, skipping
5379
NOTICE: 0 rows copied from callbacks.abc_1
5480
NOTICE: 1 rows copied from callbacks.abc_2
5581
NOTICE: 1 rows copied from callbacks.abc_3
56-
NOTICE: 0 rows copied from callbacks.abc_4
82+
NOTICE: 1 rows copied from callbacks.abc_4
5783
NOTICE: 0 rows copied from callbacks.abc_5
5884
NOTICE: 0 rows copied from callbacks.abc_6
85+
NOTICE: 0 rows copied from callbacks.abc_7
5986
drop_partitions
6087
-----------------
61-
6
88+
7
6289
(1 row)
6390

6491
/* set callback to be called on HASH partitions */

expected/pathman_permissions.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ SELECT * FROM pathman_config;
3939
(1 row)
4040

4141
SELECT * FROM pathman_config_params;
42-
partrel | enable_parent | auto | init_callback
43-
-------------------------+---------------+------+---------------
44-
permissions.user1_table | f | t | -
42+
partrel | enable_parent | auto | init_callback | spawn_using_bgw
43+
-------------------------+---------------+------+---------------+-----------------
44+
permissions.user1_table | f | t | - | f
4545
(1 row)
4646

4747
/* Should fail */

init.sql

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ CREATE TABLE IF NOT EXISTS @[email protected]_config (
2626
CHECK (parttype IN (1, 2)) /* check for allowed part types */
2727
);
2828

29+
30+
/*
31+
* Checks that callback function meets specific requirements.
32+
* Particularly it must have the only JSONB argument and VOID return type.
33+
*
34+
* NOTE: this function is used in CHECK CONSTRAINT.
35+
*/
36+
CREATE OR REPLACE FUNCTION @[email protected]_part_callback(
37+
callback REGPROC,
38+
raise_error BOOL DEFAULT FALSE)
39+
RETURNS BOOL AS 'pg_pathman', 'validate_part_callback_pl'
40+
LANGUAGE C STRICT;
41+
42+
2943
/*
3044
* Optional parameters for partitioned tables.
3145
* partrel - regclass (relation type, stored as Oid)
@@ -37,10 +51,11 @@ CREATE TABLE IF NOT EXISTS @[email protected]_config_params (
3751
partrel REGCLASS NOT NULL PRIMARY KEY,
3852
enable_parent BOOLEAN NOT NULL DEFAULT FALSE,
3953
auto BOOLEAN NOT NULL DEFAULT TRUE,
40-
init_callback REGPROCEDURE NOT NULL DEFAULT 0
54+
init_callback REGPROCEDURE NOT NULL DEFAULT 0,
55+
spawn_using_bgw BOOLEAN NOT NULL DEFAULT FALSE
56+
57+
CHECK (@[email protected]_part_callback(init_callback)) /* check signature */
4158
);
42-
CREATE UNIQUE INDEX i_pathman_config_params
43-
ON @[email protected]_config_params(partrel);
4459

4560
GRANT SELECT, INSERT, UPDATE, DELETE
4661
ON @[email protected]_config, @[email protected]_config_params
@@ -120,7 +135,7 @@ BEGIN
120135
USING relation, value;
121136
END
122137
$$
123-
LANGUAGE plpgsql;
138+
LANGUAGE plpgsql STRICT;
124139

125140
/*
126141
* Include\exclude parent relation in query plan.
@@ -159,11 +174,25 @@ CREATE OR REPLACE FUNCTION @[email protected]_init_callback(
159174
RETURNS VOID AS
160175
$$
161176
BEGIN
162-
PERFORM @[email protected]_on_partition_created_callback(callback);
163177
PERFORM @[email protected]_set_param(relation, 'init_callback', callback);
164178
END
165179
$$
166-
LANGUAGE plpgsql;
180+
LANGUAGE plpgsql STRICT;
181+
182+
/*
183+
* Set 'spawn using BGW' option
184+
*/
185+
CREATE OR REPLACE FUNCTION @[email protected]_spawn_using_bgw(
186+
relation REGCLASS,
187+
value BOOLEAN)
188+
RETURNS VOID AS
189+
$$
190+
BEGIN
191+
PERFORM @[email protected]_set_param(relation, 'spawn_using_bgw', value);
192+
END
193+
$$
194+
LANGUAGE plpgsql STRICT;
195+
167196

168197
/*
169198
* Show all existing parents and partitions.
@@ -752,15 +781,6 @@ CREATE OR REPLACE FUNCTION @[email protected]_capture()
752781
RETURNS VOID AS 'pg_pathman', 'debug_capture'
753782
LANGUAGE C STRICT;
754783

755-
/*
756-
* Checks that callback function meets specific requirements. Particularly it
757-
* must have the only JSONB argument and VOID return type.
758-
*/
759-
CREATE OR REPLACE FUNCTION @[email protected]_on_partition_created_callback(
760-
callback REGPROC)
761-
RETURNS VOID AS 'pg_pathman', 'validate_on_part_init_callback_pl'
762-
LANGUAGE C STRICT;
763-
764784

765785
/*
766786
* Invoke init_callback on RANGE partition.

sql/pathman_callbacks.sql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ SELECT set_init_callback('callbacks.abc',
2222
'callbacks.abc_on_part_created_callback');
2323

2424
INSERT INTO callbacks.abc VALUES (123, 1);
25-
INSERT INTO callbacks.abc VALUES (223, 1);
25+
INSERT INTO callbacks.abc VALUES (223, 1); /* show warning */
26+
27+
SELECT set_spawn_using_bgw('callbacks.abc', true);
28+
SELECT get_number_of_partitions('callbacks.abc');
29+
INSERT INTO callbacks.abc VALUES (323, 1);
30+
SELECT get_number_of_partitions('callbacks.abc'); /* +1 partition (created by BGW) */
31+
SELECT set_spawn_using_bgw('callbacks.abc', false);
32+
2633

2734
SELECT append_range_partition('callbacks.abc');
2835
SELECT prepend_range_partition('callbacks.abc');
29-
SELECT add_range_partition('callbacks.abc', 401, 502);
36+
SELECT add_range_partition('callbacks.abc', 501, 602);
3037

3138
SELECT drop_partitions('callbacks.abc');
3239

src/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ init_main_pathman_toggles(void)
128128
"Enables automatic partition creation",
129129
NULL,
130130
&pg_pathman_init_state.auto_partition,
131-
true,
131+
DEFAULT_AUTO,
132132
PGC_SUSET,
133133
0,
134134
NULL,
@@ -730,6 +730,7 @@ read_pathman_params(Oid relid, Datum *values, bool *isnull)
730730
Assert(!isnull[Anum_pathman_config_params_enable_parent - 1]);
731731
Assert(!isnull[Anum_pathman_config_params_auto - 1]);
732732
Assert(!isnull[Anum_pathman_config_params_init_callback - 1]);
733+
Assert(!isnull[Anum_pathman_config_params_spawn_using_bgw - 1]);
733734
}
734735

735736
/* Clean resources */

src/init.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ extern PathmanInitState pg_pathman_init_state;
8787
} while (0)
8888

8989

90+
/* Default column values for PATHMAN_CONFIG_PARAMS */
91+
#define DEFAULT_ENABLE_PARENT false
92+
#define DEFAULT_AUTO true
93+
#define DEFAULT_INIT_CALLBACK InvalidOid
94+
#define DEFAULT_SPAWN_USING_BGW false
95+
96+
9097
/*
9198
* Save and restore PathmanInitState.
9299
*/

0 commit comments

Comments
 (0)