~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/test/regress/sql/transactions.sql

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--
 
2
-- TRANSACTIONS
 
3
--
 
4
 
 
5
BEGIN;
 
6
 
 
7
SELECT * 
 
8
   INTO TABLE xacttest
 
9
   FROM aggtest;
 
10
 
 
11
INSERT INTO xacttest (a, b) VALUES (777, 777.777);
 
12
 
 
13
END;
 
14
 
 
15
-- should retrieve one value--
 
16
SELECT a FROM xacttest WHERE a > 100;
 
17
 
 
18
 
 
19
BEGIN;
 
20
 
 
21
CREATE TABLE disappear (a int4);
 
22
 
 
23
DELETE FROM aggtest;
 
24
 
 
25
-- should be empty
 
26
SELECT * FROM aggtest;
 
27
 
 
28
ABORT;
 
29
 
 
30
-- should not exist 
 
31
SELECT oid FROM pg_class WHERE relname = 'disappear';
 
32
 
 
33
-- should have members again 
 
34
SELECT * FROM aggtest;
 
35
 
 
36
 
 
37
-- Read-only tests
 
38
 
 
39
CREATE TABLE writetest (a int);
 
40
CREATE TEMPORARY TABLE temptest (a int);
 
41
 
 
42
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
 
43
 
 
44
DROP TABLE writetest; -- fail
 
45
INSERT INTO writetest VALUES (1); -- fail
 
46
SELECT * FROM writetest; -- ok
 
47
DELETE FROM temptest; -- ok
 
48
UPDATE temptest SET a = 0 WHERE a = 1 AND writetest.a = temptest.a; -- ok
 
49
PREPARE test AS UPDATE writetest SET a = 0; -- ok
 
50
EXECUTE test; -- fail
 
51
SELECT * FROM writetest, temptest; -- ok
 
52
CREATE TABLE test AS SELECT * FROM writetest; -- fail
 
53
 
 
54
START TRANSACTION READ WRITE;
 
55
DROP TABLE writetest; -- ok
 
56
COMMIT;
 
57
 
 
58
-- Subtransactions, basic tests
 
59
-- create & drop tables
 
60
SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;
 
61
CREATE TABLE foobar (a int);
 
62
BEGIN;
 
63
        CREATE TABLE foo (a int);
 
64
        SAVEPOINT one;
 
65
                DROP TABLE foo;
 
66
                CREATE TABLE bar (a int);
 
67
        ROLLBACK TO SAVEPOINT one;
 
68
        RELEASE SAVEPOINT one;
 
69
        SAVEPOINT two;
 
70
                CREATE TABLE baz (a int);
 
71
        RELEASE SAVEPOINT two;
 
72
        drop TABLE foobar;
 
73
        CREATE TABLE barbaz (a int);
 
74
COMMIT;
 
75
-- should exist: barbaz, baz, foo
 
76
SELECT * FROM foo;              -- should be empty
 
77
SELECT * FROM bar;              -- shouldn't exist
 
78
SELECT * FROM barbaz;   -- should be empty
 
79
SELECT * FROM baz;              -- should be empty
 
80
 
 
81
-- inserts
 
82
BEGIN;
 
83
        INSERT INTO foo VALUES (1);
 
84
        SAVEPOINT one;
 
85
                INSERT into bar VALUES (1);
 
86
        ROLLBACK TO one;
 
87
        RELEASE SAVEPOINT one;
 
88
        SAVEPOINT two;
 
89
                INSERT into barbaz VALUES (1);
 
90
        RELEASE two;
 
91
        SAVEPOINT three;
 
92
                SAVEPOINT four;
 
93
                        INSERT INTO foo VALUES (2);
 
94
                RELEASE SAVEPOINT four;
 
95
        ROLLBACK TO SAVEPOINT three;
 
96
        RELEASE SAVEPOINT three;
 
97
        INSERT INTO foo VALUES (3);
 
98
COMMIT;
 
99
SELECT * FROM foo;              -- should have 1 and 3
 
100
SELECT * FROM barbaz;   -- should have 1
 
101
 
 
102
-- test whole-tree commit
 
103
BEGIN;
 
104
        SAVEPOINT one;
 
105
                SELECT foo;
 
106
        ROLLBACK TO SAVEPOINT one;
 
107
        RELEASE SAVEPOINT one;
 
108
        SAVEPOINT two;
 
109
                CREATE TABLE savepoints (a int);
 
110
                SAVEPOINT three;
 
111
                        INSERT INTO savepoints VALUES (1);
 
112
                        SAVEPOINT four;
 
113
                                INSERT INTO savepoints VALUES (2);
 
114
                                SAVEPOINT five;
 
115
                                        INSERT INTO savepoints VALUES (3);
 
116
                                ROLLBACK TO SAVEPOINT five;
 
117
COMMIT;
 
118
COMMIT;         -- should not be in a transaction block
 
119
SELECT * FROM savepoints;
 
120
 
 
121
-- test whole-tree rollback
 
122
BEGIN;
 
123
        SAVEPOINT one;
 
124
                DELETE FROM savepoints WHERE a=1;
 
125
        RELEASE SAVEPOINT one;
 
126
        SAVEPOINT two;
 
127
                DELETE FROM savepoints WHERE a=1;
 
128
                SAVEPOINT three;
 
129
                        DELETE FROM savepoints WHERE a=2;
 
130
ROLLBACK;
 
131
COMMIT;         -- should not be in a transaction block
 
132
                
 
133
SELECT * FROM savepoints;
 
134
 
 
135
-- test whole-tree commit on an aborted subtransaction
 
136
BEGIN;
 
137
        INSERT INTO savepoints VALUES (4);
 
138
        SAVEPOINT one;
 
139
                INSERT INTO savepoints VALUES (5);
 
140
                SELECT foo;
 
141
COMMIT;
 
142
SELECT * FROM savepoints;
 
143
 
 
144
BEGIN;
 
145
        INSERT INTO savepoints VALUES (6);
 
146
        SAVEPOINT one;
 
147
                INSERT INTO savepoints VALUES (7);
 
148
        RELEASE SAVEPOINT one;
 
149
        INSERT INTO savepoints VALUES (8);
 
150
COMMIT;
 
151
-- rows 6 and 8 should have been created by the same xact
 
152
SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=8;
 
153
-- rows 6 and 7 should have been created by different xacts
 
154
SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=7;
 
155
 
 
156
BEGIN;
 
157
        INSERT INTO savepoints VALUES (9);
 
158
        SAVEPOINT one;
 
159
                INSERT INTO savepoints VALUES (10);
 
160
        ROLLBACK TO SAVEPOINT one;
 
161
                INSERT INTO savepoints VALUES (11);
 
162
COMMIT;
 
163
SELECT a FROM savepoints WHERE a in (9, 10, 11);
 
164
-- rows 9 and 11 should have been created by different xacts
 
165
SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=9 AND b.a=11;
 
166
 
 
167
BEGIN;
 
168
        INSERT INTO savepoints VALUES (12);
 
169
        SAVEPOINT one;
 
170
                INSERT INTO savepoints VALUES (13);
 
171
                SAVEPOINT two;
 
172
                        INSERT INTO savepoints VALUES (14);
 
173
        ROLLBACK TO SAVEPOINT one;
 
174
                INSERT INTO savepoints VALUES (15);
 
175
                SAVEPOINT two;
 
176
                        INSERT INTO savepoints VALUES (16);
 
177
                        SAVEPOINT three;
 
178
                                INSERT INTO savepoints VALUES (17);
 
179
COMMIT;
 
180
SELECT a FROM savepoints WHERE a BETWEEN 12 AND 17;
 
181
 
 
182
BEGIN;
 
183
        INSERT INTO savepoints VALUES (18);
 
184
        SAVEPOINT one;
 
185
                INSERT INTO savepoints VALUES (19);
 
186
                SAVEPOINT two;
 
187
                        INSERT INTO savepoints VALUES (20);
 
188
        ROLLBACK TO SAVEPOINT one;
 
189
                INSERT INTO savepoints VALUES (21);
 
190
        ROLLBACK TO SAVEPOINT one;
 
191
                INSERT INTO savepoints VALUES (22);
 
192
COMMIT;
 
193
SELECT a FROM savepoints WHERE a BETWEEN 18 AND 22;
 
194
 
 
195
DROP TABLE savepoints;
 
196
 
 
197
-- only in a transaction block:
 
198
SAVEPOINT one;
 
199
ROLLBACK TO SAVEPOINT one;
 
200
RELEASE SAVEPOINT one;
 
201
 
 
202
-- Only "rollback to" allowed in aborted state
 
203
BEGIN;
 
204
  SAVEPOINT one;
 
205
  SELECT 0/0;
 
206
  SAVEPOINT two;    -- ignored till the end of ...
 
207
  RELEASE SAVEPOINT one;      -- ignored till the end of ...
 
208
  ROLLBACK TO SAVEPOINT one;
 
209
  SELECT 1;
 
210
COMMIT;
 
211
SELECT 1;                       -- this should work
 
212
 
 
213
-- check non-transactional behavior of cursors
 
214
BEGIN;
 
215
        DECLARE c CURSOR FOR SELECT unique2 FROM tenk1;
 
216
        SAVEPOINT one;
 
217
                FETCH 10 FROM c;
 
218
        ROLLBACK TO SAVEPOINT one;
 
219
                FETCH 10 FROM c;
 
220
        RELEASE SAVEPOINT one;
 
221
        FETCH 10 FROM c;
 
222
        CLOSE c;
 
223
        DECLARE c CURSOR FOR SELECT unique2/0 FROM tenk1;
 
224
        SAVEPOINT two;
 
225
                FETCH 10 FROM c;
 
226
        ROLLBACK TO SAVEPOINT two;
 
227
        -- c is now dead to the world ...
 
228
                FETCH 10 FROM c;
 
229
        ROLLBACK TO SAVEPOINT two;
 
230
        RELEASE SAVEPOINT two;
 
231
        FETCH 10 FROM c;
 
232
COMMIT;
 
233
 
 
234
--
 
235
-- Check that "stable" functions are really stable.  They should not be
 
236
-- able to see the partial results of the calling query.  (Ideally we would
 
237
-- also check that they don't see commits of concurrent transactions, but
 
238
-- that's a mite hard to do within the limitations of pg_regress.)
 
239
--
 
240
select * from xacttest;
 
241
 
 
242
create or replace function max_xacttest() returns smallint language sql as
 
243
'select max(a) from xacttest' stable;
 
244
 
 
245
begin;
 
246
update xacttest set a = max_xacttest() + 10 where a > 0;
 
247
select * from xacttest;
 
248
rollback;
 
249
 
 
250
-- But a volatile function can see the partial results of the calling query
 
251
create or replace function max_xacttest() returns smallint language sql as
 
252
'select max(a) from xacttest' volatile;
 
253
 
 
254
begin;
 
255
update xacttest set a = max_xacttest() + 10 where a > 0;
 
256
select * from xacttest;
 
257
rollback;
 
258
 
 
259
-- Now the same test with plpgsql (since it depends on SPI which is different)
 
260
create or replace function max_xacttest() returns smallint language plpgsql as
 
261
'begin return max(a) from xacttest; end' stable;
 
262
 
 
263
begin;
 
264
update xacttest set a = max_xacttest() + 10 where a > 0;
 
265
select * from xacttest;
 
266
rollback;
 
267
 
 
268
create or replace function max_xacttest() returns smallint language plpgsql as
 
269
'begin return max(a) from xacttest; end' volatile;
 
270
 
 
271
begin;
 
272
update xacttest set a = max_xacttest() + 10 where a > 0;
 
273
select * from xacttest;
 
274
rollback;
 
275
 
 
276
 
 
277
-- test case for problems with dropping an open relation during abort
 
278
BEGIN;
 
279
        savepoint x;
 
280
                CREATE TABLE koju (a INT UNIQUE);
 
281
                INSERT INTO koju VALUES (1);
 
282
                INSERT INTO koju VALUES (1);
 
283
        rollback to x;
 
284
 
 
285
        CREATE TABLE koju (a INT UNIQUE);
 
286
        INSERT INTO koju VALUES (1);
 
287
        INSERT INTO koju VALUES (1);
 
288
ROLLBACK;
 
289
 
 
290
DROP TABLE foo;
 
291
DROP TABLE baz;
 
292
DROP TABLE barbaz;