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