mirror of
https://github.com/postgres/postgres.git
synced 2025-12-12 02:37:31 +03:00
Redesign query-snapshot timing so that volatile functions in READ COMMITTED
mode see a fresh snapshot for each command in the function, rather than using the latest interactive command's snapshot. Also, suppress fresh snapshots as well as CommandCounterIncrement inside STABLE and IMMUTABLE functions, instead using the snapshot taken for the most closely nested regular query. (This behavior is only sane for read-only functions, so the patch also enforces that such functions contain only SELECT commands.) As per my proposal of 6-Sep-2004; I note that I floated essentially the same proposal on 19-Jun-2002, but that discussion tailed off without any action. Since 8.0 seems like the right place to be taking possibly nontrivial backwards compatibility hits, let's get it done now.
This commit is contained in:
@@ -374,6 +374,84 @@ ERROR: portal "c" cannot be run
|
||||
FETCH 10 FROM c;
|
||||
ERROR: portal "c" cannot be run
|
||||
COMMIT;
|
||||
--
|
||||
-- Check that "stable" functions are really stable. They should not be
|
||||
-- able to see the partial results of the calling query. (Ideally we would
|
||||
-- also check that they don't see commits of concurrent transactions, but
|
||||
-- that's a mite hard to do within the limitations of pg_regress.)
|
||||
--
|
||||
select * from xacttest;
|
||||
a | b
|
||||
-----+---------
|
||||
56 | 7.8
|
||||
100 | 99.097
|
||||
0 | 0.09561
|
||||
42 | 324.78
|
||||
777 | 777.777
|
||||
(5 rows)
|
||||
|
||||
create or replace function max_xacttest() returns smallint language sql as
|
||||
'select max(a) from xacttest' stable;
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
a | b
|
||||
-----+---------
|
||||
0 | 0.09561
|
||||
787 | 7.8
|
||||
787 | 99.097
|
||||
787 | 324.78
|
||||
787 | 777.777
|
||||
(5 rows)
|
||||
|
||||
rollback;
|
||||
-- But a volatile function can see the partial results of the calling query
|
||||
create or replace function max_xacttest() returns smallint language sql as
|
||||
'select max(a) from xacttest' volatile;
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
a | b
|
||||
-----+---------
|
||||
0 | 0.09561
|
||||
787 | 7.8
|
||||
797 | 99.097
|
||||
807 | 324.78
|
||||
817 | 777.777
|
||||
(5 rows)
|
||||
|
||||
rollback;
|
||||
-- Now the same test with plpgsql (since it depends on SPI which is different)
|
||||
create or replace function max_xacttest() returns smallint language plpgsql as
|
||||
'begin return max(a) from xacttest; end' stable;
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
a | b
|
||||
-----+---------
|
||||
0 | 0.09561
|
||||
787 | 7.8
|
||||
787 | 99.097
|
||||
787 | 324.78
|
||||
787 | 777.777
|
||||
(5 rows)
|
||||
|
||||
rollback;
|
||||
create or replace function max_xacttest() returns smallint language plpgsql as
|
||||
'begin return max(a) from xacttest; end' volatile;
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
a | b
|
||||
-----+---------
|
||||
0 | 0.09561
|
||||
787 | 7.8
|
||||
797 | 99.097
|
||||
807 | 324.78
|
||||
817 | 777.777
|
||||
(5 rows)
|
||||
|
||||
rollback;
|
||||
-- test case for problems with dropping an open relation during abort
|
||||
BEGIN;
|
||||
savepoint x;
|
||||
|
||||
@@ -231,6 +231,49 @@ BEGIN;
|
||||
FETCH 10 FROM c;
|
||||
COMMIT;
|
||||
|
||||
--
|
||||
-- Check that "stable" functions are really stable. They should not be
|
||||
-- able to see the partial results of the calling query. (Ideally we would
|
||||
-- also check that they don't see commits of concurrent transactions, but
|
||||
-- that's a mite hard to do within the limitations of pg_regress.)
|
||||
--
|
||||
select * from xacttest;
|
||||
|
||||
create or replace function max_xacttest() returns smallint language sql as
|
||||
'select max(a) from xacttest' stable;
|
||||
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
rollback;
|
||||
|
||||
-- But a volatile function can see the partial results of the calling query
|
||||
create or replace function max_xacttest() returns smallint language sql as
|
||||
'select max(a) from xacttest' volatile;
|
||||
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
rollback;
|
||||
|
||||
-- Now the same test with plpgsql (since it depends on SPI which is different)
|
||||
create or replace function max_xacttest() returns smallint language plpgsql as
|
||||
'begin return max(a) from xacttest; end' stable;
|
||||
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
rollback;
|
||||
|
||||
create or replace function max_xacttest() returns smallint language plpgsql as
|
||||
'begin return max(a) from xacttest; end' volatile;
|
||||
|
||||
begin;
|
||||
update xacttest set a = max_xacttest() + 10 where a > 0;
|
||||
select * from xacttest;
|
||||
rollback;
|
||||
|
||||
|
||||
-- test case for problems with dropping an open relation during abort
|
||||
BEGIN;
|
||||
savepoint x;
|
||||
|
||||
Reference in New Issue
Block a user