Debian Codegraph @

src/pl/tcl/expected/pltcl_subxact.out

raw
1 --
2 -- Test explicit subtransactions
3 --
4 CREATE TABLE subtransaction_tbl (
5 i integer
6 );
7 --
8 -- We use this wrapper to catch errors and return errormsg only,
9 -- because values of $::errorinfo variable contain procedure name which
10 -- includes OID, so it's not stable
11 --
12 CREATE FUNCTION pltcl_wrapper(statement text) RETURNS text
13 AS $$
14 if [catch {spi_exec $1} msg] {
15 return "ERROR: $msg"
16 } else {
17 return "SUCCESS: $msg"
18 }
19 $$ LANGUAGE pltcl;
20 -- Test subtransaction successfully committed
21 CREATE FUNCTION subtransaction_ctx_success() RETURNS void
22 AS $$
23 spi_exec "INSERT INTO subtransaction_tbl VALUES(1)"
24 subtransaction {
25 spi_exec "INSERT INTO subtransaction_tbl VALUES(2)"
26 }
27 $$ LANGUAGE pltcl;
28 BEGIN;
29 INSERT INTO subtransaction_tbl VALUES(0);
30 SELECT subtransaction_ctx_success();
31 subtransaction_ctx_success
32 ----------------------------
33
34 (1 row)
35
36 COMMIT;
37 SELECT * FROM subtransaction_tbl;
38 i
39 ---
40 0
41 1
42 2
43 (3 rows)
44
45 TRUNCATE subtransaction_tbl;
46 -- Test subtransaction rollback
47 CREATE FUNCTION subtransaction_ctx_test(what_error text = NULL) RETURNS void
48 AS $$
49 spi_exec "INSERT INTO subtransaction_tbl VALUES (1)"
50 subtransaction {
51 spi_exec "INSERT INTO subtransaction_tbl VALUES (2)"
52 if {$1 == "SPI"} {
53 spi_exec "INSERT INTO subtransaction_tbl VALUES ('oops')"
54 } elseif { $1 == "Tcl"} {
55 elog ERROR "Tcl error"
56 }
57 }
58 $$ LANGUAGE pltcl;
59 SELECT pltcl_wrapper('SELECT subtransaction_ctx_test()');
60 pltcl_wrapper
61 ---------------
62 SUCCESS: 1
63 (1 row)
64
65 SELECT * FROM subtransaction_tbl;
66 i
67 ---
68 1
69 2
70 (2 rows)
71
72 TRUNCATE subtransaction_tbl;
73 SELECT pltcl_wrapper('SELECT subtransaction_ctx_test(''SPI'')');
74 pltcl_wrapper
75 ------------------------------------------------------
76 ERROR: invalid input syntax for type integer: "oops"
77 (1 row)
78
79 SELECT * FROM subtransaction_tbl;
80 i
81 ---
82 (0 rows)
83
84 TRUNCATE subtransaction_tbl;
85 SELECT pltcl_wrapper('SELECT subtransaction_ctx_test(''Tcl'')');
86 pltcl_wrapper
87 ------------------
88 ERROR: Tcl error
89 (1 row)
90
91 SELECT * FROM subtransaction_tbl;
92 i
93 ---
94 (0 rows)
95
96 TRUNCATE subtransaction_tbl;
97 -- Nested subtransactions
98 CREATE FUNCTION subtransaction_nested_test(swallow boolean = 'f') RETURNS text
99 AS $$
100 spi_exec "INSERT INTO subtransaction_tbl VALUES (1)"
101 subtransaction {
102 spi_exec "INSERT INTO subtransaction_tbl VALUES (2)"
103 if [catch {
104 subtransaction {
105 spi_exec "INSERT INTO subtransaction_tbl VALUES (3)"
106 spi_exec "error"
107 }
108 } errormsg] {
109 if {$1 != "t"} {
110 error $errormsg $::errorInfo $::errorCode
111 }
112 elog NOTICE "Swallowed $errormsg"
113 }
114 }
115 return "ok"
116 $$ LANGUAGE pltcl;
117 SELECT pltcl_wrapper('SELECT subtransaction_nested_test()');
118 pltcl_wrapper
119 ----------------------------------------
120 ERROR: syntax error at or near "error"
121 (1 row)
122
123 SELECT * FROM subtransaction_tbl;
124 i
125 ---
126 (0 rows)
127
128 TRUNCATE subtransaction_tbl;
129 SELECT pltcl_wrapper('SELECT subtransaction_nested_test(''t'')');
130 NOTICE: Swallowed syntax error at or near "error"
131 pltcl_wrapper
132 ---------------
133 SUCCESS: 1
134 (1 row)
135
136 SELECT * FROM subtransaction_tbl;
137 i
138 ---
139 1
140 2
141 (2 rows)
142
143 TRUNCATE subtransaction_tbl;