Skip to content

Commit 563f40b

Browse files
committed
Eschew "RESET statement_timeout" in tests.
Instead, use transaction abort. Given an unlucky bout of latency, the timeout would cancel the RESET itself. Buildfarm members gharial, lapwing, mereswine, shearwater, and sungazer witness that. Back-patch to 9.1 (all supported versions). The query_canceled test still could timeout before entering its subtransaction; for whatever reason, that has yet to happen on the buildfarm.
1 parent 52f21c5 commit 563f40b

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

src/test/regress/expected/plpgsql.out

+29-14
Original file line numberDiff line numberDiff line change
@@ -2011,36 +2011,28 @@ NOTICE: caught numeric_value_out_of_range or cardinality_violation
20112011
(1 row)
20122012

20132013
create temp table foo (f1 int);
2014-
create function blockme() returns int as $$
2014+
create function subxact_rollback_semantics() returns int as $$
20152015
declare x int;
20162016
begin
20172017
x := 1;
20182018
insert into foo values(x);
20192019
begin
20202020
x := x + 1;
20212021
insert into foo values(x);
2022-
-- we assume this will take longer than 2 seconds:
2023-
select count(*) into x from tenk1 a, tenk1 b, tenk1 c;
2022+
raise exception 'inner';
20242023
exception
20252024
when others then
2026-
raise notice 'caught others?';
2027-
return -1;
2028-
when query_canceled then
2029-
raise notice 'nyeah nyeah, can''t stop me';
20302025
x := x * 10;
20312026
end;
20322027
insert into foo values(x);
20332028
return x;
20342029
end$$ language plpgsql;
2035-
set statement_timeout to 2000;
2036-
select blockme();
2037-
NOTICE: nyeah nyeah, can't stop me
2038-
blockme
2039-
---------
2040-
20
2030+
select subxact_rollback_semantics();
2031+
subxact_rollback_semantics
2032+
----------------------------
2033+
20
20412034
(1 row)
20422035

2043-
reset statement_timeout;
20442036
select * from foo;
20452037
f1
20462038
----
@@ -2049,6 +2041,29 @@ select * from foo;
20492041
(2 rows)
20502042

20512043
drop table foo;
2044+
create function trap_timeout() returns void as $$
2045+
begin
2046+
declare x int;
2047+
begin
2048+
-- we assume this will take longer than 2 seconds:
2049+
select count(*) into x from tenk1 a, tenk1 b, tenk1 c;
2050+
exception
2051+
when others then
2052+
raise notice 'caught others?';
2053+
when query_canceled then
2054+
raise notice 'nyeah nyeah, can''t stop me';
2055+
end;
2056+
-- Abort transaction to abandon the statement_timeout setting. Otherwise,
2057+
-- the next top-level statement would be vulnerable to the timeout.
2058+
raise exception 'end of function';
2059+
end$$ language plpgsql;
2060+
begin;
2061+
set statement_timeout to 2000;
2062+
select trap_timeout();
2063+
NOTICE: nyeah nyeah, can't stop me
2064+
ERROR: end of function
2065+
CONTEXT: PL/pgSQL function trap_timeout() line 15 at RAISE
2066+
rollback;
20522067
-- Test for pass-by-ref values being stored in proper context
20532068
create function test_variable_storage() returns text as $$
20542069
declare x text;

src/test/regress/expected/prepared_xacts.out

+4-2
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,11 @@ SELECT gid FROM pg_prepared_xacts;
194194
(2 rows)
195195

196196
-- pxtest3 should be locked because of the pending DROP
197+
begin;
197198
set statement_timeout to 2000;
198199
SELECT * FROM pxtest3;
199200
ERROR: canceling statement due to statement timeout
200-
reset statement_timeout;
201+
rollback;
201202
-- Disconnect, we will continue testing in a different backend
202203
\c -
203204
-- There should still be two prepared transactions
@@ -209,10 +210,11 @@ SELECT gid FROM pg_prepared_xacts;
209210
(2 rows)
210211

211212
-- pxtest3 should still be locked because of the pending DROP
213+
begin;
212214
set statement_timeout to 2000;
213215
SELECT * FROM pxtest3;
214216
ERROR: canceling statement due to statement timeout
215-
reset statement_timeout;
217+
rollback;
216218
-- Commit table creation
217219
COMMIT PREPARED 'regress-one';
218220
\d pxtest2

src/test/regress/expected/prepared_xacts_1.out

+4-2
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@ SELECT gid FROM pg_prepared_xacts;
198198
(0 rows)
199199

200200
-- pxtest3 should be locked because of the pending DROP
201+
begin;
201202
set statement_timeout to 2000;
202203
SELECT * FROM pxtest3;
203204
fff
204205
-----
205206
(0 rows)
206207

207-
reset statement_timeout;
208+
rollback;
208209
-- Disconnect, we will continue testing in a different backend
209210
\c -
210211
-- There should still be two prepared transactions
@@ -214,13 +215,14 @@ SELECT gid FROM pg_prepared_xacts;
214215
(0 rows)
215216

216217
-- pxtest3 should still be locked because of the pending DROP
218+
begin;
217219
set statement_timeout to 2000;
218220
SELECT * FROM pxtest3;
219221
fff
220222
-----
221223
(0 rows)
222224

223-
reset statement_timeout;
225+
rollback;
224226
-- Commit table creation
225227
COMMIT PREPARED 'regress-one';
226228
ERROR: prepared transaction with identifier "regress-one" does not exist

src/test/regress/sql/plpgsql.sql

+24-13
Original file line numberDiff line numberDiff line change
@@ -1745,37 +1745,48 @@ select trap_matching_test(1);
17451745

17461746
create temp table foo (f1 int);
17471747

1748-
create function blockme() returns int as $$
1748+
create function subxact_rollback_semantics() returns int as $$
17491749
declare x int;
17501750
begin
17511751
x := 1;
17521752
insert into foo values(x);
17531753
begin
17541754
x := x + 1;
17551755
insert into foo values(x);
1756+
raise exception 'inner';
1757+
exception
1758+
when others then
1759+
x := x * 10;
1760+
end;
1761+
insert into foo values(x);
1762+
return x;
1763+
end$$ language plpgsql;
1764+
1765+
select subxact_rollback_semantics();
1766+
select * from foo;
1767+
drop table foo;
1768+
1769+
create function trap_timeout() returns void as $$
1770+
begin
1771+
declare x int;
1772+
begin
17561773
-- we assume this will take longer than 2 seconds:
17571774
select count(*) into x from tenk1 a, tenk1 b, tenk1 c;
17581775
exception
17591776
when others then
17601777
raise notice 'caught others?';
1761-
return -1;
17621778
when query_canceled then
17631779
raise notice 'nyeah nyeah, can''t stop me';
1764-
x := x * 10;
17651780
end;
1766-
insert into foo values(x);
1767-
return x;
1781+
-- Abort transaction to abandon the statement_timeout setting. Otherwise,
1782+
-- the next top-level statement would be vulnerable to the timeout.
1783+
raise exception 'end of function';
17681784
end$$ language plpgsql;
17691785

1786+
begin;
17701787
set statement_timeout to 2000;
1771-
1772-
select blockme();
1773-
1774-
reset statement_timeout;
1775-
1776-
select * from foo;
1777-
1778-
drop table foo;
1788+
select trap_timeout();
1789+
rollback;
17791790

17801791
-- Test for pass-by-ref values being stored in proper context
17811792
create function test_variable_storage() returns text as $$

src/test/regress/sql/prepared_xacts.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ SELECT * FROM pxtest2;
122122
SELECT gid FROM pg_prepared_xacts;
123123

124124
-- pxtest3 should be locked because of the pending DROP
125+
begin;
125126
set statement_timeout to 2000;
126127
SELECT * FROM pxtest3;
127-
reset statement_timeout;
128+
rollback;
128129

129130
-- Disconnect, we will continue testing in a different backend
130131
\c -
@@ -133,9 +134,10 @@ reset statement_timeout;
133134
SELECT gid FROM pg_prepared_xacts;
134135

135136
-- pxtest3 should still be locked because of the pending DROP
137+
begin;
136138
set statement_timeout to 2000;
137139
SELECT * FROM pxtest3;
138-
reset statement_timeout;
140+
rollback;
139141

140142
-- Commit table creation
141143
COMMIT PREPARED 'regress-one';

0 commit comments

Comments
 (0)