mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-28 08:11:50 +02:00
6310809c4a
We have an implementation restriction that PREPARE TRANSACTION can't handle cases where both session-lifespan and transaction-lifespan locks are held on the same lockable object. (That's because we'd otherwise need to acquire a new PROCLOCK entry during post-prepare cleanup, which is an operation that might fail. The situation can only arise with odd usages of advisory locks, so removing the restriction is probably not worth the amount of effort it would take.) AtPrepare_Locks attempted to enforce this, but its logic was many bricks shy of a load, because it only detected cases where the session and transaction locks had the same lockmode. Locks of different modes on the same object would lead to the rather unhelpful message "PANIC: we seem to have dropped a bit somewhere". To fix, build a transient hashtable with one entry per locktag, not one per locktag + mode, and use that to detect conflicts. Per bug #17122 from Alexander Pyhalov. This bug is ancient, so back-patch to all supported branches. Discussion: https://postgr.es/m/17122-04f3c32098a62233@postgresql.org
165 lines
3.9 KiB
PL/PgSQL
165 lines
3.9 KiB
PL/PgSQL
--
|
|
-- PREPARED TRANSACTIONS (two-phase commit)
|
|
--
|
|
-- We can't readily test persistence of prepared xacts within the
|
|
-- regression script framework, unfortunately. Note that a crash
|
|
-- isn't really needed ... stopping and starting the postmaster would
|
|
-- be enough, but we can't even do that here.
|
|
|
|
|
|
-- create a simple table that we'll use in the tests
|
|
CREATE TABLE pxtest1 (foobar VARCHAR(10));
|
|
|
|
INSERT INTO pxtest1 VALUES ('aaa');
|
|
|
|
|
|
-- Test PREPARE TRANSACTION
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
UPDATE pxtest1 SET foobar = 'bbb' WHERE foobar = 'aaa';
|
|
SELECT * FROM pxtest1;
|
|
PREPARE TRANSACTION 'foo1';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
-- Test pg_prepared_xacts system view
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- Test ROLLBACK PREPARED
|
|
ROLLBACK PREPARED 'foo1';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
|
|
-- Test COMMIT PREPARED
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
INSERT INTO pxtest1 VALUES ('ddd');
|
|
SELECT * FROM pxtest1;
|
|
PREPARE TRANSACTION 'foo2';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
COMMIT PREPARED 'foo2';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
-- Test duplicate gids
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
|
|
SELECT * FROM pxtest1;
|
|
PREPARE TRANSACTION 'foo3';
|
|
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
INSERT INTO pxtest1 VALUES ('fff');
|
|
|
|
-- This should fail, because the gid foo3 is already in use
|
|
PREPARE TRANSACTION 'foo3';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
ROLLBACK PREPARED 'foo3';
|
|
|
|
SELECT * FROM pxtest1;
|
|
|
|
-- Test serialization failure (SSI)
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd';
|
|
SELECT * FROM pxtest1;
|
|
PREPARE TRANSACTION 'foo4';
|
|
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
SELECT * FROM pxtest1;
|
|
|
|
-- This should fail, because the two transactions have a write-skew anomaly
|
|
INSERT INTO pxtest1 VALUES ('fff');
|
|
PREPARE TRANSACTION 'foo5';
|
|
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
ROLLBACK PREPARED 'foo4';
|
|
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- Clean up
|
|
DROP TABLE pxtest1;
|
|
|
|
-- Test detection of session-level and xact-level locks on same object
|
|
BEGIN;
|
|
SELECT pg_advisory_lock(1);
|
|
SELECT pg_advisory_xact_lock_shared(1);
|
|
PREPARE TRANSACTION 'foo6'; -- fails
|
|
|
|
-- Test subtransactions
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
CREATE TABLE pxtest2 (a int);
|
|
INSERT INTO pxtest2 VALUES (1);
|
|
SAVEPOINT a;
|
|
INSERT INTO pxtest2 VALUES (2);
|
|
ROLLBACK TO a;
|
|
SAVEPOINT b;
|
|
INSERT INTO pxtest2 VALUES (3);
|
|
PREPARE TRANSACTION 'regress-one';
|
|
|
|
CREATE TABLE pxtest3(fff int);
|
|
|
|
-- Test shared invalidation
|
|
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
DROP TABLE pxtest3;
|
|
CREATE TABLE pxtest4 (a int);
|
|
INSERT INTO pxtest4 VALUES (1);
|
|
INSERT INTO pxtest4 VALUES (2);
|
|
DECLARE foo CURSOR FOR SELECT * FROM pxtest4;
|
|
-- Fetch 1 tuple, keeping the cursor open
|
|
FETCH 1 FROM foo;
|
|
PREPARE TRANSACTION 'regress-two';
|
|
|
|
-- No such cursor
|
|
FETCH 1 FROM foo;
|
|
|
|
-- Table doesn't exist, the creation hasn't been committed yet
|
|
SELECT * FROM pxtest2;
|
|
|
|
-- There should be two prepared transactions
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- pxtest3 should be locked because of the pending DROP
|
|
begin;
|
|
lock table pxtest3 in access share mode nowait;
|
|
rollback;
|
|
|
|
-- Disconnect, we will continue testing in a different backend
|
|
\c -
|
|
|
|
-- There should still be two prepared transactions
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- pxtest3 should still be locked because of the pending DROP
|
|
begin;
|
|
lock table pxtest3 in access share mode nowait;
|
|
rollback;
|
|
|
|
-- Commit table creation
|
|
COMMIT PREPARED 'regress-one';
|
|
\d pxtest2
|
|
SELECT * FROM pxtest2;
|
|
|
|
-- There should be one prepared transaction
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- Commit table drop
|
|
COMMIT PREPARED 'regress-two';
|
|
SELECT * FROM pxtest3;
|
|
|
|
-- There should be no prepared transactions
|
|
SELECT gid FROM pg_prepared_xacts;
|
|
|
|
-- Clean up
|
|
DROP TABLE pxtest2;
|
|
DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled
|
|
DROP TABLE pxtest4;
|