|
| 1 | +--- plain --- |
| 2 | +create table atx_test(a int); |
| 3 | +insert into atx_test values (1); |
| 4 | +begin; |
| 5 | +update atx_test set a = 2; |
| 6 | +begin autonomous; |
| 7 | +update atx_test set a = 3; |
| 8 | +ERROR: an ATX waiting for an ancestor transaction will cause a deadlock |
| 9 | +commit; |
| 10 | +commit; |
| 11 | +select * from atx_test; -- you should see (2) |
| 12 | + a |
| 13 | +--- |
| 14 | + 2 |
| 15 | +(1 row) |
| 16 | + |
| 17 | +delete from atx_test; |
| 18 | +begin; |
| 19 | +insert into atx_test values (1); |
| 20 | +begin autonomous; |
| 21 | +insert into atx_test values (2); |
| 22 | +begin autonomous; |
| 23 | +insert into atx_test values (3); |
| 24 | +begin autonomous; |
| 25 | +insert into atx_test values (4); |
| 26 | +begin autonomous; |
| 27 | +insert into atx_test values (5); |
| 28 | +begin autonomous; |
| 29 | +insert into atx_test values (6); |
| 30 | +begin autonomous; |
| 31 | +insert into atx_test values (7); |
| 32 | +rollback; |
| 33 | +commit; |
| 34 | +rollback; |
| 35 | +commit; |
| 36 | +rollback; |
| 37 | +commit; |
| 38 | +rollback; |
| 39 | +select * from atx_test; -- you should see (2),(4),(6) |
| 40 | + a |
| 41 | +--- |
| 42 | + 2 |
| 43 | + 4 |
| 44 | + 6 |
| 45 | +(3 rows) |
| 46 | + |
| 47 | +begin transaction isolation level repeatable read; |
| 48 | +begin autonomous transaction isolation level repeatable read; |
| 49 | +select 1; |
| 50 | + ?column? |
| 51 | +---------- |
| 52 | + 1 |
| 53 | +(1 row) |
| 54 | + |
| 55 | +commit; |
| 56 | +commit; |
| 57 | +--- plpgsql --- |
| 58 | +create or replace language plpgsql; |
| 59 | +create or replace function myatx(x int) returns integer as $$ |
| 60 | +begin autonomous |
| 61 | +insert into atx_test values (123); |
| 62 | +begin autonomous |
| 63 | +insert into atx_test values (124); |
| 64 | +end; |
| 65 | +begin autonomous |
| 66 | +insert into atx_test values (125); |
| 67 | +raise exception 'hello world'; |
| 68 | +end; |
| 69 | +insert into atx_test values (126); |
| 70 | +return x + 1; |
| 71 | +end; |
| 72 | +$$ language plpgsql; |
| 73 | +select myatx(2000); |
| 74 | +ERROR: hello world |
| 75 | +CONTEXT: PL/pgSQL function myatx(integer) line 9 at RAISE |
| 76 | +select * from atx_test; -- you should see (124) |
| 77 | + a |
| 78 | +----- |
| 79 | + 2 |
| 80 | + 4 |
| 81 | + 6 |
| 82 | + 124 |
| 83 | +(4 rows) |
| 84 | + |
| 85 | +--- audit --- |
| 86 | +create table atx_actions ( |
| 87 | +tid serial, |
| 88 | +table_name text, |
| 89 | +user_name text, |
| 90 | +tstamp timestamp with time zone default current_timestamp, |
| 91 | +action text, |
| 92 | +old_data text, |
| 93 | +new_data text, |
| 94 | +query text |
| 95 | +); |
| 96 | +create or replace function if_modified_func() returns trigger as $body$ |
| 97 | +declare |
| 98 | +v_old_data text; |
| 99 | +v_new_data text; |
| 100 | +begin |
| 101 | +if (tg_op = 'UPDATE') then |
| 102 | +v_old_data := row(old.*); |
| 103 | +v_new_data := row(new.*); |
| 104 | +begin autonomous |
| 105 | +insert |
| 106 | +into atx_actions |
| 107 | +(table_name, user_name, action, old_data, new_data, query) |
| 108 | +values |
| 109 | +(tg_table_name::text, session_user::text, tg_op, v_old_data, v_new_data, current_query()); |
| 110 | +return new; |
| 111 | +end; |
| 112 | +elsif (tg_op = 'DELETE') then |
| 113 | +v_old_data := row(old.*); |
| 114 | +begin autonomous |
| 115 | +insert |
| 116 | +into atx_actions |
| 117 | +(table_name, user_name, action, old_data, query) |
| 118 | +values |
| 119 | +(tg_table_name::text, session_user::text, tg_op, v_old_data, current_query()); |
| 120 | +return old; |
| 121 | +end; |
| 122 | +elsif (tg_op = 'INSERT') then |
| 123 | +v_new_data := row(new.*); |
| 124 | +begin autonomous |
| 125 | +insert |
| 126 | +into atx_actions |
| 127 | +(table_name, user_name, action, new_data, query) |
| 128 | +values |
| 129 | +(tg_table_name::text, session_user::text, tg_op, v_new_data, current_query()); |
| 130 | +return new; |
| 131 | +end; |
| 132 | +else |
| 133 | +raise warning 'if_modified_func - unhandled action %', tg_op; |
| 134 | +return null; |
| 135 | +end if; |
| 136 | +end; |
| 137 | +$body$ language plpgsql; |
| 138 | +drop table atx_test; |
| 139 | +create table atx_test(a text, b text); |
| 140 | +create trigger atx_test_audit |
| 141 | +after insert or update or delete on atx_test |
| 142 | +for each row execute procedure if_modified_func(); |
| 143 | +insert into atx_test values ('asd', 'bsd'); |
| 144 | +insert into atx_test values ('hello', 'world'); |
| 145 | +begin; |
| 146 | +delete from atx_test where a = 'asd'; |
| 147 | +update atx_test set a = 'goodbye' where a = 'hello'; |
| 148 | +-- atx_actions will keep the actions we performed even though we roll them back |
| 149 | +rollback; |
| 150 | +select * from atx_test; |
| 151 | + a | b |
| 152 | +-------+------- |
| 153 | + asd | bsd |
| 154 | + hello | world |
| 155 | +(2 rows) |
| 156 | + |
| 157 | +select tid, table_name, action, old_data, new_data, query from atx_actions; |
| 158 | + tid | table_name | action | old_data | new_data | query |
| 159 | +-----+------------+--------+---------------+-----------------+------------------------------------------------------ |
| 160 | + 1 | atx_test | INSERT | | (asd,bsd) | insert into atx_test values ('asd', 'bsd'); |
| 161 | + 2 | atx_test | INSERT | | (hello,world) | insert into atx_test values ('hello', 'world'); |
| 162 | + 3 | atx_test | DELETE | (asd,bsd) | | delete from atx_test where a = 'asd'; |
| 163 | + 4 | atx_test | UPDATE | (hello,world) | (goodbye,world) | update atx_test set a = 'goodbye' where a = 'hello'; |
| 164 | +(4 rows) |
| 165 | + |
| 166 | +drop table atx_test; |
| 167 | +drop table atx_actions; |