--
-- TRANSACTIONS
--
BEGIN;
SELECT * 
   INTO TABLE xacttest
   FROM aggtest;
INSERT INTO xacttest (a, b) VALUES (777, 777.777);
END;
-- should retrieve one value--
SELECT a FROM xacttest WHERE a > 100;
  a  
-----
 777
(1 row)

BEGIN;
CREATE TABLE disappear (a int4);
DELETE FROM aggtest;
-- should be empty
SELECT * FROM aggtest;
 a | b 
---+---
(0 rows)

ABORT;
-- should not exist 
SELECT oid FROM pg_class WHERE relname = 'disappear';
 oid 
-----
(0 rows)

-- should have members again 
SELECT * FROM aggtest;
  a  |    b    
-----+---------
  56 |     7.8
 100 |  99.097
   0 | 0.09561
  42 |  324.78
(4 rows)

-- Read-only tests
CREATE TABLE writetest (a int);
CREATE TEMPORARY TABLE temptest (a int);
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
DROP TABLE writetest; -- fail
ERROR:  transaction is read-only
INSERT INTO writetest VALUES (1); -- fail
ERROR:  transaction is read-only
SELECT * FROM writetest; -- ok
 a 
---
(0 rows)

DELETE FROM temptest; -- ok
UPDATE temptest SET a = 0 WHERE a = 1 AND writetest.a = temptest.a; -- ok
PREPARE test AS UPDATE writetest SET a = 0; -- ok
EXECUTE test; -- fail
ERROR:  transaction is read-only
SELECT * FROM writetest, temptest; -- ok
 a | a 
---+---
(0 rows)

CREATE TABLE test AS SELECT * FROM writetest; -- fail
ERROR:  transaction is read-only
START TRANSACTION READ WRITE;
DROP TABLE writetest; -- ok
COMMIT;
-- Subtransactions, basic tests
-- create & drop tables
SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;
CREATE TABLE foobar (a int);
BEGIN;
	CREATE TABLE foo (a int);
	BEGIN;
		DROP TABLE foo;
		CREATE TABLE bar (a int);
	ROLLBACK;
	BEGIN;
		CREATE TABLE baz (a int);
	COMMIT;
	drop TABLE foobar;
	CREATE TABLE barbaz (a int);
COMMIT;
-- should exist: barbaz, baz, foo
SELECT * FROM foo;		-- should be empty
 a 
---
(0 rows)

SELECT * FROM bar;		-- shouldn't exist
ERROR:  relation "bar" does not exist
SELECT * FROM barbaz;	-- should be empty
 a 
---
(0 rows)

SELECT * FROM baz;		-- should be empty
 a 
---
(0 rows)

-- inserts
BEGIN;
	INSERT INTO foo VALUES (1);
	BEGIN;
		INSERT into bar VALUES (1);
ERROR:  relation "bar" does not exist
	ROLLBACK;
	BEGIN;
		INSERT into barbaz VALUES (1);
	COMMIT;
	BEGIN;
		BEGIN;
			INSERT INTO foo VALUES (2);
		COMMIT;
	ROLLBACK;
	INSERT INTO foo VALUES (3);
COMMIT;
SELECT * FROM foo;		-- should have 1 and 3
 a 
---
 1
 3
(2 rows)

SELECT * FROM barbaz;	-- should have 1
 a 
---
 1
(1 row)

-- check that starting a subxact in a failed xact or subxact works
BEGIN;
	SELECT 0/0;		-- fail the outer xact
ERROR:  division by zero
	BEGIN;
		SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
	COMMIT;
	SELECT 1;		-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
	BEGIN;
		SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
	ROLLBACK;
	SELECT 1;		-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
COMMIT;
SELECT 1;			-- this should work
 ?column? 
----------
        1
(1 row)

BEGIN;
	BEGIN;
		SELECT 1;	-- this should work
 ?column? 
----------
        1
(1 row)

		SELECT 0/0;	-- fail the subxact
ERROR:  division by zero
		SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
		BEGIN;
			SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
		ROLLBACK;
		BEGIN;
			SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
		COMMIT;
		SELECT 1;	-- this should NOT work
ERROR:  current transaction is aborted, commands ignored until end of transaction block
	ROLLBACK;
	SELECT 1;		-- this should work
 ?column? 
----------
        1
(1 row)

COMMIT;
SELECT 1;			-- this should work
 ?column? 
----------
        1
(1 row)

DROP TABLE foo;
DROP TABLE baz;
DROP TABLE barbaz;
