]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/transactions.out
Teach the system how to use hashing for UNION. (INTERSECT/EXCEPT will follow,
[postgresql] / src / test / regress / expected / transactions.out
1 --
2 -- TRANSACTIONS
3 --
4 BEGIN;
5 SELECT * 
6    INTO TABLE xacttest
7    FROM aggtest;
8 INSERT INTO xacttest (a, b) VALUES (777, 777.777);
9 END;
10 -- should retrieve one value--
11 SELECT a FROM xacttest WHERE a > 100;
12   a  
13 -----
14  777
15 (1 row)
16
17 BEGIN;
18 CREATE TABLE disappear (a int4);
19 DELETE FROM aggtest;
20 -- should be empty
21 SELECT * FROM aggtest;
22  a | b 
23 ---+---
24 (0 rows)
25
26 ABORT;
27 -- should not exist 
28 SELECT oid FROM pg_class WHERE relname = 'disappear';
29  oid 
30 -----
31 (0 rows)
32
33 -- should have members again 
34 SELECT * FROM aggtest;
35   a  |    b    
36 -----+---------
37   56 |     7.8
38  100 |  99.097
39    0 | 0.09561
40   42 |  324.78
41 (4 rows)
42
43 -- Read-only tests
44 CREATE TABLE writetest (a int);
45 CREATE TEMPORARY TABLE temptest (a int);
46 SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
47 DROP TABLE writetest; -- fail
48 ERROR:  transaction is read-only
49 INSERT INTO writetest VALUES (1); -- fail
50 ERROR:  transaction is read-only
51 SELECT * FROM writetest; -- ok
52  a 
53 ---
54 (0 rows)
55
56 DELETE FROM temptest; -- ok
57 UPDATE temptest SET a = 0 FROM writetest WHERE temptest.a = 1 AND writetest.a = temptest.a; -- ok
58 PREPARE test AS UPDATE writetest SET a = 0; -- ok
59 EXECUTE test; -- fail
60 ERROR:  transaction is read-only
61 SELECT * FROM writetest, temptest; -- ok
62  a | a 
63 ---+---
64 (0 rows)
65
66 CREATE TABLE test AS SELECT * FROM writetest; -- fail
67 ERROR:  transaction is read-only
68 START TRANSACTION READ WRITE;
69 DROP TABLE writetest; -- ok
70 COMMIT;
71 -- Subtransactions, basic tests
72 -- create & drop tables
73 SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE;
74 CREATE TABLE foobar (a int);
75 BEGIN;
76         CREATE TABLE foo (a int);
77         SAVEPOINT one;
78                 DROP TABLE foo;
79                 CREATE TABLE bar (a int);
80         ROLLBACK TO SAVEPOINT one;
81         RELEASE SAVEPOINT one;
82         SAVEPOINT two;
83                 CREATE TABLE baz (a int);
84         RELEASE SAVEPOINT two;
85         drop TABLE foobar;
86         CREATE TABLE barbaz (a int);
87 COMMIT;
88 -- should exist: barbaz, baz, foo
89 SELECT * FROM foo;              -- should be empty
90  a 
91 ---
92 (0 rows)
93
94 SELECT * FROM bar;              -- shouldn't exist
95 ERROR:  relation "bar" does not exist
96 SELECT * FROM barbaz;   -- should be empty
97  a 
98 ---
99 (0 rows)
100
101 SELECT * FROM baz;              -- should be empty
102  a 
103 ---
104 (0 rows)
105
106 -- inserts
107 BEGIN;
108         INSERT INTO foo VALUES (1);
109         SAVEPOINT one;
110                 INSERT into bar VALUES (1);
111 ERROR:  relation "bar" does not exist
112         ROLLBACK TO one;
113         RELEASE SAVEPOINT one;
114         SAVEPOINT two;
115                 INSERT into barbaz VALUES (1);
116         RELEASE two;
117         SAVEPOINT three;
118                 SAVEPOINT four;
119                         INSERT INTO foo VALUES (2);
120                 RELEASE SAVEPOINT four;
121         ROLLBACK TO SAVEPOINT three;
122         RELEASE SAVEPOINT three;
123         INSERT INTO foo VALUES (3);
124 COMMIT;
125 SELECT * FROM foo;              -- should have 1 and 3
126  a 
127 ---
128  1
129  3
130 (2 rows)
131
132 SELECT * FROM barbaz;   -- should have 1
133  a 
134 ---
135  1
136 (1 row)
137
138 -- test whole-tree commit
139 BEGIN;
140         SAVEPOINT one;
141                 SELECT foo;
142 ERROR:  column "foo" does not exist
143 LINE 1: SELECT foo;
144                ^
145         ROLLBACK TO SAVEPOINT one;
146         RELEASE SAVEPOINT one;
147         SAVEPOINT two;
148                 CREATE TABLE savepoints (a int);
149                 SAVEPOINT three;
150                         INSERT INTO savepoints VALUES (1);
151                         SAVEPOINT four;
152                                 INSERT INTO savepoints VALUES (2);
153                                 SAVEPOINT five;
154                                         INSERT INTO savepoints VALUES (3);
155                                 ROLLBACK TO SAVEPOINT five;
156 COMMIT;
157 COMMIT;         -- should not be in a transaction block
158 WARNING:  there is no transaction in progress
159 SELECT * FROM savepoints;
160  a 
161 ---
162  1
163  2
164 (2 rows)
165
166 -- test whole-tree rollback
167 BEGIN;
168         SAVEPOINT one;
169                 DELETE FROM savepoints WHERE a=1;
170         RELEASE SAVEPOINT one;
171         SAVEPOINT two;
172                 DELETE FROM savepoints WHERE a=1;
173                 SAVEPOINT three;
174                         DELETE FROM savepoints WHERE a=2;
175 ROLLBACK;
176 COMMIT;         -- should not be in a transaction block
177 WARNING:  there is no transaction in progress
178                 
179 SELECT * FROM savepoints;
180  a 
181 ---
182  1
183  2
184 (2 rows)
185
186 -- test whole-tree commit on an aborted subtransaction
187 BEGIN;
188         INSERT INTO savepoints VALUES (4);
189         SAVEPOINT one;
190                 INSERT INTO savepoints VALUES (5);
191                 SELECT foo;
192 ERROR:  column "foo" does not exist
193 LINE 1: SELECT foo;
194                ^
195 COMMIT;
196 SELECT * FROM savepoints;
197  a 
198 ---
199  1
200  2
201 (2 rows)
202
203 BEGIN;
204         INSERT INTO savepoints VALUES (6);
205         SAVEPOINT one;
206                 INSERT INTO savepoints VALUES (7);
207         RELEASE SAVEPOINT one;
208         INSERT INTO savepoints VALUES (8);
209 COMMIT;
210 -- rows 6 and 8 should have been created by the same xact
211 SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=8;
212  ?column? 
213 ----------
214  t
215 (1 row)
216
217 -- rows 6 and 7 should have been created by different xacts
218 SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=6 AND b.a=7;
219  ?column? 
220 ----------
221  f
222 (1 row)
223
224 BEGIN;
225         INSERT INTO savepoints VALUES (9);
226         SAVEPOINT one;
227                 INSERT INTO savepoints VALUES (10);
228         ROLLBACK TO SAVEPOINT one;
229                 INSERT INTO savepoints VALUES (11);
230 COMMIT;
231 SELECT a FROM savepoints WHERE a in (9, 10, 11);
232  a  
233 ----
234   9
235  11
236 (2 rows)
237
238 -- rows 9 and 11 should have been created by different xacts
239 SELECT a.xmin = b.xmin FROM savepoints a, savepoints b WHERE a.a=9 AND b.a=11;
240  ?column? 
241 ----------
242  f
243 (1 row)
244
245 BEGIN;
246         INSERT INTO savepoints VALUES (12);
247         SAVEPOINT one;
248                 INSERT INTO savepoints VALUES (13);
249                 SAVEPOINT two;
250                         INSERT INTO savepoints VALUES (14);
251         ROLLBACK TO SAVEPOINT one;
252                 INSERT INTO savepoints VALUES (15);
253                 SAVEPOINT two;
254                         INSERT INTO savepoints VALUES (16);
255                         SAVEPOINT three;
256                                 INSERT INTO savepoints VALUES (17);
257 COMMIT;
258 SELECT a FROM savepoints WHERE a BETWEEN 12 AND 17;
259  a  
260 ----
261  12
262  15
263  16
264  17
265 (4 rows)
266
267 BEGIN;
268         INSERT INTO savepoints VALUES (18);
269         SAVEPOINT one;
270                 INSERT INTO savepoints VALUES (19);
271                 SAVEPOINT two;
272                         INSERT INTO savepoints VALUES (20);
273         ROLLBACK TO SAVEPOINT one;
274                 INSERT INTO savepoints VALUES (21);
275         ROLLBACK TO SAVEPOINT one;
276                 INSERT INTO savepoints VALUES (22);
277 COMMIT;
278 SELECT a FROM savepoints WHERE a BETWEEN 18 AND 22;
279  a  
280 ----
281  18
282  22
283 (2 rows)
284
285 DROP TABLE savepoints;
286 -- only in a transaction block:
287 SAVEPOINT one;
288 ERROR:  SAVEPOINT can only be used in transaction blocks
289 ROLLBACK TO SAVEPOINT one;
290 ERROR:  ROLLBACK TO SAVEPOINT can only be used in transaction blocks
291 RELEASE SAVEPOINT one;
292 ERROR:  RELEASE SAVEPOINT can only be used in transaction blocks
293 -- Only "rollback to" allowed in aborted state
294 BEGIN;
295   SAVEPOINT one;
296   SELECT 0/0;
297 ERROR:  division by zero
298   SAVEPOINT two;    -- ignored till the end of ...
299 ERROR:  current transaction is aborted, commands ignored until end of transaction block
300   RELEASE SAVEPOINT one;      -- ignored till the end of ...
301 ERROR:  current transaction is aborted, commands ignored until end of transaction block
302   ROLLBACK TO SAVEPOINT one;
303   SELECT 1;
304  ?column? 
305 ----------
306         1
307 (1 row)
308
309 COMMIT;
310 SELECT 1;                       -- this should work
311  ?column? 
312 ----------
313         1
314 (1 row)
315
316 -- check non-transactional behavior of cursors
317 BEGIN;
318         DECLARE c CURSOR FOR SELECT unique2 FROM tenk1 ORDER BY unique2;
319         SAVEPOINT one;
320                 FETCH 10 FROM c;
321  unique2 
322 ---------
323        0
324        1
325        2
326        3
327        4
328        5
329        6
330        7
331        8
332        9
333 (10 rows)
334
335         ROLLBACK TO SAVEPOINT one;
336                 FETCH 10 FROM c;
337  unique2 
338 ---------
339       10
340       11
341       12
342       13
343       14
344       15
345       16
346       17
347       18
348       19
349 (10 rows)
350
351         RELEASE SAVEPOINT one;
352         FETCH 10 FROM c;
353  unique2 
354 ---------
355       20
356       21
357       22
358       23
359       24
360       25
361       26
362       27
363       28
364       29
365 (10 rows)
366
367         CLOSE c;
368         DECLARE c CURSOR FOR SELECT unique2/0 FROM tenk1 ORDER BY unique2;
369         SAVEPOINT two;
370                 FETCH 10 FROM c;
371 ERROR:  division by zero
372         ROLLBACK TO SAVEPOINT two;
373         -- c is now dead to the world ...
374                 FETCH 10 FROM c;
375 ERROR:  portal "c" cannot be run
376         ROLLBACK TO SAVEPOINT two;
377         RELEASE SAVEPOINT two;
378         FETCH 10 FROM c;
379 ERROR:  portal "c" cannot be run
380 COMMIT;
381 --
382 -- Check that "stable" functions are really stable.  They should not be
383 -- able to see the partial results of the calling query.  (Ideally we would
384 -- also check that they don't see commits of concurrent transactions, but
385 -- that's a mite hard to do within the limitations of pg_regress.)
386 --
387 select * from xacttest;
388   a  |    b    
389 -----+---------
390   56 |     7.8
391  100 |  99.097
392    0 | 0.09561
393   42 |  324.78
394  777 | 777.777
395 (5 rows)
396
397 create or replace function max_xacttest() returns smallint language sql as
398 'select max(a) from xacttest' stable;
399 begin;
400 update xacttest set a = max_xacttest() + 10 where a > 0;
401 select * from xacttest;
402   a  |    b    
403 -----+---------
404    0 | 0.09561
405  787 |     7.8
406  787 |  99.097
407  787 |  324.78
408  787 | 777.777
409 (5 rows)
410
411 rollback;
412 -- But a volatile function can see the partial results of the calling query
413 create or replace function max_xacttest() returns smallint language sql as
414 'select max(a) from xacttest' volatile;
415 begin;
416 update xacttest set a = max_xacttest() + 10 where a > 0;
417 select * from xacttest;
418   a  |    b    
419 -----+---------
420    0 | 0.09561
421  787 |     7.8
422  797 |  99.097
423  807 |  324.78
424  817 | 777.777
425 (5 rows)
426
427 rollback;
428 -- Now the same test with plpgsql (since it depends on SPI which is different)
429 create or replace function max_xacttest() returns smallint language plpgsql as
430 'begin return max(a) from xacttest; end' stable;
431 begin;
432 update xacttest set a = max_xacttest() + 10 where a > 0;
433 select * from xacttest;
434   a  |    b    
435 -----+---------
436    0 | 0.09561
437  787 |     7.8
438  787 |  99.097
439  787 |  324.78
440  787 | 777.777
441 (5 rows)
442
443 rollback;
444 create or replace function max_xacttest() returns smallint language plpgsql as
445 'begin return max(a) from xacttest; end' volatile;
446 begin;
447 update xacttest set a = max_xacttest() + 10 where a > 0;
448 select * from xacttest;
449   a  |    b    
450 -----+---------
451    0 | 0.09561
452  787 |     7.8
453  797 |  99.097
454  807 |  324.78
455  817 | 777.777
456 (5 rows)
457
458 rollback;
459 -- test case for problems with dropping an open relation during abort
460 BEGIN;
461         savepoint x;
462                 CREATE TABLE koju (a INT UNIQUE);
463 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "koju_a_key" for table "koju"
464                 INSERT INTO koju VALUES (1);
465                 INSERT INTO koju VALUES (1);
466 ERROR:  duplicate key value violates unique constraint "koju_a_key"
467         rollback to x;
468         CREATE TABLE koju (a INT UNIQUE);
469 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "koju_a_key" for table "koju"
470         INSERT INTO koju VALUES (1);
471         INSERT INTO koju VALUES (1);
472 ERROR:  duplicate key value violates unique constraint "koju_a_key"
473 ROLLBACK;
474 DROP TABLE foo;
475 DROP TABLE baz;
476 DROP TABLE barbaz;
477 -- verify that cursors created during an aborted subtransaction are
478 -- closed, but that we do not rollback the effect of any FETCHs
479 -- performed in the aborted subtransaction
480 begin;
481 savepoint x;
482 create table abc (a int);
483 insert into abc values (5);
484 insert into abc values (10);
485 declare foo cursor for select * from abc;
486 fetch from foo;
487  a 
488 ---
489  5
490 (1 row)
491
492 rollback to x;
493 -- should fail
494 fetch from foo;
495 ERROR:  cursor "foo" does not exist
496 commit;
497 begin;
498 create table abc (a int);
499 insert into abc values (5);
500 insert into abc values (10);
501 insert into abc values (15);
502 declare foo cursor for select * from abc;
503 fetch from foo;
504  a 
505 ---
506  5
507 (1 row)
508
509 savepoint x;
510 fetch from foo;
511  a  
512 ----
513  10
514 (1 row)
515
516 rollback to x;
517 fetch from foo;
518  a  
519 ----
520  15
521 (1 row)
522
523 abort;
524 -- tests for the "tid" type
525 SELECT '(3, 3)'::tid = '(3, 4)'::tid;
526  ?column? 
527 ----------
528  f
529 (1 row)
530
531 SELECT '(3, 3)'::tid = '(3, 3)'::tid;
532  ?column? 
533 ----------
534  t
535 (1 row)
536
537 SELECT '(3, 3)'::tid <> '(3, 3)'::tid;
538  ?column? 
539 ----------
540  f
541 (1 row)
542
543 SELECT '(3, 3)'::tid <> '(3, 4)'::tid;
544  ?column? 
545 ----------
546  t
547 (1 row)
548