|
| 1 | +--- plain --- |
| 2 | + |
| 3 | +createtableatx_test(aint); |
| 4 | +insert into atx_testvalues (1); |
| 5 | + |
| 6 | +begin; |
| 7 | +update atx_testset a=2; |
| 8 | +begin autonomous; |
| 9 | +update atx_testset a=3; |
| 10 | +commit; |
| 11 | +commit; |
| 12 | + |
| 13 | +select*from atx_test;-- you should see (2) |
| 14 | + |
| 15 | +deletefrom atx_test; |
| 16 | +begin; |
| 17 | +insert into atx_testvalues (1); |
| 18 | +begin autonomous; |
| 19 | +insert into atx_testvalues (2); |
| 20 | +begin autonomous; |
| 21 | +insert into atx_testvalues (3); |
| 22 | +begin autonomous; |
| 23 | +insert into atx_testvalues (4); |
| 24 | +begin autonomous; |
| 25 | +insert into atx_testvalues (5); |
| 26 | +begin autonomous; |
| 27 | +insert into atx_testvalues (6); |
| 28 | +begin autonomous; |
| 29 | +insert into atx_testvalues (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 | +select1; |
| 43 | +commit; |
| 44 | +commit; |
| 45 | + |
| 46 | +--- plpgsql --- |
| 47 | + |
| 48 | +create or replacelanguageplpgsql; |
| 49 | + |
| 50 | +create or replacefunctionmyatx(xint) returnsintegeras $$ |
| 51 | +begin autonomous |
| 52 | +insert into atx_testvalues (123); |
| 53 | +begin autonomous |
| 54 | +insert into atx_testvalues (124); |
| 55 | +end; |
| 56 | +begin autonomous |
| 57 | +insert into atx_testvalues (125); |
| 58 | +raise exception'hello world'; |
| 59 | +end; |
| 60 | +insert into atx_testvalues (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 | +createtableatx_actions ( |
| 72 | +tidserial, |
| 73 | +table_nametext, |
| 74 | +user_nametext, |
| 75 | +tstamptimestamp with time zone defaultcurrent_timestamp, |
| 76 | +actiontext, |
| 77 | +old_datatext, |
| 78 | +new_datatext, |
| 79 | +querytext |
| 80 | +); |
| 81 | + |
| 82 | +create or replacefunctionif_modified_func() returns triggeras $body$ |
| 83 | +declare |
| 84 | +v_old_datatext; |
| 85 | +v_new_datatext; |
| 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 | +returnnull; |
| 121 | +end if; |
| 122 | +end; |
| 123 | +$body$ language plpgsql; |
| 124 | + |
| 125 | +droptable atx_test; |
| 126 | +createtableatx_test(atext, btext); |
| 127 | + |
| 128 | +createtriggeratx_test_audit |
| 129 | +after insertorupdateordeleteon atx_test |
| 130 | +for each row execute procedure if_modified_func(); |
| 131 | + |
| 132 | +insert into atx_testvalues ('asd','bsd'); |
| 133 | +insert into atx_testvalues ('hello','world'); |
| 134 | +begin; |
| 135 | +deletefrom atx_testwhere a='asd'; |
| 136 | +update atx_testset 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, queryfrom atx_actions; |
| 142 | + |
| 143 | +droptable atx_test; |
| 144 | +droptable atx_actions; |