Skip to content

Commit 94b2fa8

Browse files
committed
Merge branch 'master' into rel_future_beta
2 parents 64b78a5 + beb3471 commit 94b2fa8

File tree

5 files changed

+93
-37
lines changed

5 files changed

+93
-37
lines changed

expected/pathman_bgw.out

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,43 @@ SELECT * FROM pathman_partition_list ORDER BY partition; /* should contain 3 par
105105

106106
DROP TABLE test_bgw.test_4 CASCADE;
107107
NOTICE: drop cascades to 4 other objects
108+
/* test error handling in BGW */
109+
CREATE TABLE test_bgw.test_5(val INT4 NOT NULL);
110+
SELECT create_range_partitions('test_bgw.test_5', 'val', 1, 10, 2);
111+
create_range_partitions
112+
-------------------------
113+
2
114+
(1 row)
115+
116+
CREATE OR REPLACE FUNCTION test_bgw.abort_xact(args JSONB)
117+
RETURNS VOID AS $$
118+
BEGIN
119+
RAISE EXCEPTION 'aborting xact!';
120+
END
121+
$$ language plpgsql;
122+
SELECT set_spawn_using_bgw('test_bgw.test_5', true);
123+
set_spawn_using_bgw
124+
---------------------
125+
126+
(1 row)
127+
128+
SELECT set_init_callback('test_bgw.test_5', 'test_bgw.abort_xact(jsonb)');
129+
set_init_callback
130+
-------------------
131+
132+
(1 row)
133+
134+
INSERT INTO test_bgw.test_5 VALUES (-100);
135+
ERROR: Attempt to spawn new partitions of relation "test_5" failed
136+
SELECT * FROM pathman_partition_list ORDER BY partition; /* should contain 3 partitions */
137+
parent | partition | parttype | expr | range_min | range_max
138+
-----------------+-------------------+----------+------+-----------+-----------
139+
test_bgw.test_5 | test_bgw.test_5_1 | 2 | val | 1 | 11
140+
test_bgw.test_5 | test_bgw.test_5_2 | 2 | val | 11 | 21
141+
(2 rows)
142+
143+
DROP FUNCTION test_bgw.abort_xact(args JSONB);
144+
DROP TABLE test_bgw.test_5 CASCADE;
145+
NOTICE: drop cascades to 3 other objects
108146
DROP SCHEMA test_bgw CASCADE;
109147
DROP EXTENSION pg_pathman;

expected/pathman_callbacks.out

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
\set VERBOSITY terse
22
CREATE EXTENSION pg_pathman;
33
CREATE SCHEMA callbacks;
4-
/* Check callbacks */
4+
/* callback #1 */
55
CREATE OR REPLACE FUNCTION callbacks.abc_on_part_created_callback(args JSONB)
66
RETURNS VOID AS $$
77
BEGIN
88
RAISE WARNING 'callback arg: %', args::TEXT;
99
END
1010
$$ language plpgsql;
11-
/* callback is in public namespace, must be schema-qualified */
11+
/* callback #2 */
1212
CREATE OR REPLACE FUNCTION public.dummy_cb(args JSONB)
1313
RETURNS VOID AS $$
1414
BEGIN
@@ -184,15 +184,11 @@ WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_2",
184184

185185
INSERT INTO callbacks.abc VALUES (201, 0); /* +1 new partition */
186186
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_3", "range_max": "301", "range_min": "201", "parent_schema": "callbacks", "partition_schema": "callbacks"}
187+
BEGIN;
187188
DROP FUNCTION callbacks.abc_on_part_created_callback(jsonb);
188189
INSERT INTO callbacks.abc VALUES (301, 0); /* +0 new partitions (ERROR) */
189190
ERROR: callback function "callbacks.abc_on_part_created_callback(jsonb)" does not exist
190-
CREATE OR REPLACE FUNCTION callbacks.abc_on_part_created_callback(args JSONB)
191-
RETURNS VOID AS $$
192-
BEGIN
193-
RAISE WARNING 'callback arg: %', args::TEXT;
194-
END
195-
$$ language plpgsql;
191+
ROLLBACK;
196192
INSERT INTO callbacks.abc VALUES (301, 0); /* +1 new partition */
197193
WARNING: callback arg: {"parent": "abc", "parttype": "2", "partition": "abc_5", "range_max": "401", "range_min": "301", "parent_schema": "callbacks", "partition_schema": "callbacks"}
198194
DROP TABLE callbacks.abc CASCADE;
@@ -211,22 +207,22 @@ CREATE OR REPLACE FUNCTION callbacks.rotation_callback(params jsonb)
211207
RETURNS VOID AS
212208
$$
213209
DECLARE
214-
relation regclass;
210+
relation regclass;
215211
parent_rel regclass;
216212
BEGIN
217213
parent_rel := concat(params->>'partition_schema', '.', params->>'parent')::regclass;
218214

219-
-- drop "old" partitions
220-
FOR relation IN (SELECT partition FROM
215+
-- drop "old" partitions
216+
FOR relation IN (SELECT partition FROM
221217
(SELECT partition, range_min::INT4 FROM pathman_partition_list
222218
WHERE parent = parent_rel
223219
ORDER BY range_min::INT4 DESC
224220
OFFSET 4) t -- remain 4 last partitions
225221
ORDER BY range_min)
226-
LOOP
227-
RAISE NOTICE 'dropping partition %', relation;
228-
PERFORM drop_range_partition(relation);
229-
END LOOP;
222+
LOOP
223+
RAISE NOTICE 'dropping partition %', relation;
224+
PERFORM drop_range_partition(relation);
225+
END LOOP;
230226
END
231227
$$ LANGUAGE plpgsql;
232228
SELECT * FROM pathman_partition_list

sql/pathman_bgw.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ SELECT * FROM pathman_partition_list ORDER BY partition; /* should contain 3 par
5353
DROP TABLE test_bgw.test_4 CASCADE;
5454

5555

56+
/* test error handling in BGW */
57+
CREATE TABLE test_bgw.test_5(val INT4 NOT NULL);
58+
SELECT create_range_partitions('test_bgw.test_5', 'val', 1, 10, 2);
59+
60+
CREATE OR REPLACE FUNCTION test_bgw.abort_xact(args JSONB)
61+
RETURNS VOID AS $$
62+
BEGIN
63+
RAISE EXCEPTION 'aborting xact!';
64+
END
65+
$$ language plpgsql;
66+
67+
SELECT set_spawn_using_bgw('test_bgw.test_5', true);
68+
SELECT set_init_callback('test_bgw.test_5', 'test_bgw.abort_xact(jsonb)');
69+
INSERT INTO test_bgw.test_5 VALUES (-100);
70+
SELECT * FROM pathman_partition_list ORDER BY partition; /* should contain 3 partitions */
71+
72+
DROP FUNCTION test_bgw.abort_xact(args JSONB);
73+
DROP TABLE test_bgw.test_5 CASCADE;
74+
75+
5676

5777
DROP SCHEMA test_bgw CASCADE;
5878
DROP EXTENSION pg_pathman;

sql/pathman_callbacks.sql

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
CREATE EXTENSION pg_pathman;
44
CREATE SCHEMA callbacks;
55

6-
/* Check callbacks */
76

7+
8+
/* callback #1 */
89
CREATE OR REPLACE FUNCTION callbacks.abc_on_part_created_callback(args JSONB)
910
RETURNS VOID AS $$
1011
BEGIN
1112
RAISE WARNING 'callback arg: %', args::TEXT;
1213
END
1314
$$ language plpgsql;
1415

15-
16-
17-
/* callback is in public namespace, must be schema-qualified */
16+
/* callback #2 */
1817
CREATE OR REPLACE FUNCTION public.dummy_cb(args JSONB)
1918
RETURNS VOID AS $$
2019
BEGIN
2120
END
2221
$$ language plpgsql;
2322

23+
24+
2425
CREATE TABLE callbacks.abc(a serial, b int);
2526
SELECT create_range_partitions('callbacks.abc', 'a', 1, 100, 2);
2627

@@ -78,25 +79,25 @@ SELECT create_hash_partitions('callbacks.abc', 'a', 5);
7879

7980
DROP TABLE callbacks.abc CASCADE;
8081

82+
8183
/* test the temprary deletion of callback function */
8284
CREATE TABLE callbacks.abc(a serial, b int);
8385
SELECT set_init_callback('callbacks.abc',
8486
'callbacks.abc_on_part_created_callback(jsonb)');
8587
SELECT create_range_partitions('callbacks.abc', 'a', 1, 100, 2);
8688

8789
INSERT INTO callbacks.abc VALUES (201, 0); /* +1 new partition */
90+
91+
BEGIN;
8892
DROP FUNCTION callbacks.abc_on_part_created_callback(jsonb);
8993
INSERT INTO callbacks.abc VALUES (301, 0); /* +0 new partitions (ERROR) */
90-
CREATE OR REPLACE FUNCTION callbacks.abc_on_part_created_callback(args JSONB)
91-
RETURNS VOID AS $$
92-
BEGIN
93-
RAISE WARNING 'callback arg: %', args::TEXT;
94-
END
95-
$$ language plpgsql;
94+
ROLLBACK;
95+
9696
INSERT INTO callbacks.abc VALUES (301, 0); /* +1 new partition */
9797

9898
DROP TABLE callbacks.abc CASCADE;
9999

100+
100101
/* more complex test using rotation of tables */
101102
CREATE TABLE callbacks.abc(a INT4 NOT NULL);
102103
INSERT INTO callbacks.abc
@@ -107,22 +108,22 @@ CREATE OR REPLACE FUNCTION callbacks.rotation_callback(params jsonb)
107108
RETURNS VOID AS
108109
$$
109110
DECLARE
110-
relation regclass;
111+
relation regclass;
111112
parent_rel regclass;
112113
BEGIN
113114
parent_rel := concat(params->>'partition_schema', '.', params->>'parent')::regclass;
114115

115-
-- drop "old" partitions
116-
FOR relation IN (SELECT partition FROM
116+
-- drop "old" partitions
117+
FOR relation IN (SELECT partition FROM
117118
(SELECT partition, range_min::INT4 FROM pathman_partition_list
118119
WHERE parent = parent_rel
119120
ORDER BY range_min::INT4 DESC
120121
OFFSET 4) t -- remain 4 last partitions
121122
ORDER BY range_min)
122-
LOOP
123-
RAISE NOTICE 'dropping partition %', relation;
124-
PERFORM drop_range_partition(relation);
125-
END LOOP;
123+
LOOP
124+
RAISE NOTICE 'dropping partition %', relation;
125+
PERFORM drop_range_partition(relation);
126+
END LOOP;
126127
END
127128
$$ LANGUAGE plpgsql;
128129

@@ -140,6 +141,8 @@ SELECT * FROM pathman_partition_list
140141
WHERE parent = 'callbacks.abc'::REGCLASS
141142
ORDER BY range_min::INT4;
142143

144+
145+
143146
DROP TABLE callbacks.abc CASCADE;
144147
DROP SCHEMA callbacks CASCADE;
145148
DROP EXTENSION pg_pathman CASCADE;

src/partition_creation.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,10 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,
452452
FlushErrorState();
453453

454454
/* Produce log message if we're in BGW */
455-
error->elevel = LOG;
456-
error->message = psprintf(CppAsString(create_partitions_for_value_internal)
457-
": %s [%u]", error->message, MyProcPid);
458-
459-
ReThrowError(error);
455+
elog(LOG,
456+
CppAsString(create_partitions_for_value_internal) ": %s [%u]",
457+
error->message,
458+
MyProcPid);
460459

461460
/* Reset 'partid' in case of error */
462461
partid = InvalidOid;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy