|
| 1 | +2. timetravel.c - functions for implementing time travel feature. |
| 2 | + |
| 3 | +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 4 | +I rewritten this, because: |
| 5 | + |
| 6 | +on original version of postgresql 7.3.2-7.3.3: |
| 7 | + |
| 8 | +the UPDATE not work on timetravel.example if I added |
| 9 | +>create unique index tttest_idx on tttest (price_id,price_off); |
| 10 | +>update tttest set price_val = 30 where price_id = 3; |
| 11 | +ERROR: Cannot insert a duplicate key into unique index tttest_idx |
| 12 | + |
| 13 | +And UPDATE not work on table tttest after |
| 14 | +>alter table tttest add column q1 text; |
| 15 | +>alter table tttest add column q2 int; |
| 16 | +>alter table tttest drop column q1; |
| 17 | +>update tttest set price_val = 30 where price_id = 3; |
| 18 | +ERROR: Parameter '$5' is out of range |
| 19 | + |
| 20 | +And I add a new optional feature: my new timetravel have +3 optional parameters: |
| 21 | +inserter_user, updater_user, deleter_user. |
| 22 | + |
| 23 | +And I add a new function: get_timetravel for get timetravel status |
| 24 | +without change it. |
| 25 | + |
| 26 | +A big difference: |
| 27 | +the old version on UPDATE changed oid on active ('infinity') record, |
| 28 | +the new version UPDATE keep oid, and the overdued record have a new oid. |
| 29 | +I sign with '!!!' my comment in this file. |
| 30 | +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 31 | + |
| 32 | + Old internally supported time-travel (TT) used insert/delete |
| 33 | +transaction commit times. To get the same feature using triggers |
| 34 | +you are to add to a table two columns of abstime type to store |
| 35 | +date when a tuple was inserted (start_date) and changed/deleted |
| 36 | +(stop_date): |
| 37 | + |
| 38 | +CREATE TABLE XXX ( |
| 39 | +...... |
| 40 | +date_onabstime default currabstime(), |
| 41 | +date_offabstime default 'infinity' |
| 42 | +...... |
| 43 | +/* !!! and (if have) */ |
| 44 | +ins_usertext/* user, who insert this record */ |
| 45 | +upd_usertext/* user, who updated this record */ |
| 46 | +del_usertext/* user, who deleted this record */ |
| 47 | +...... |
| 48 | +); |
| 49 | + |
| 50 | +!!! on INSERT my new version: |
| 51 | + ... and optionally set ins_user to current user, upd_user and del_user to null. |
| 52 | + |
| 53 | +- so, tuples being inserted with NULLs in date_on/date_off will get |
| 54 | +_current_date_ in date_on (name of start_date column in XXX) and INFINITY in |
| 55 | +date_off (name of stop_date column in XXX). |
| 56 | + |
| 57 | + Tuples with stop_date equal INFINITY are "valid now": when trigger will |
| 58 | +be fired for UPDATE/DELETE of a tuple with stop_date NOT equal INFINITY then |
| 59 | +this tuple will not be changed/deleted! |
| 60 | + |
| 61 | + If stop_date equal INFINITY then on |
| 62 | + |
| 63 | +UPDATE: |
| 64 | +original version was: |
| 65 | + only stop_date in tuple being updated will be changed to current |
| 66 | + date and new tuple with new data (coming from SET ... in UPDATE) will be |
| 67 | + inserted. Start_date in this new tuple will be setted to current date and |
| 68 | + stop_date - to INFINITY. |
| 69 | +On my new version: |
| 70 | + insert a new tuple with old values, but stop_date changed to current date; |
| 71 | + and update original tuple with new data, and update start_date to current date |
| 72 | + and optionally set upd_user to current user and clear ins_user,del_user. |
| 73 | + |
| 74 | +DELETE: new tuple will be inserted with stop_date setted to current date |
| 75 | +(and with the same data in other columns as in tuple being deleted). |
| 76 | +On my new version: |
| 77 | + ... and optionally set del_user to current user. |
| 78 | + |
| 79 | + NOTE: |
| 80 | +1. To get tuples "valid now" you are to add _stop_date_ = 'infinity' |
| 81 | + to WHERE. Internally supported TT allowed to avoid this... |
| 82 | + Fixed rewriting RULEs could help here... |
| 83 | + As work arround you may use VIEWs... |
| 84 | +2. You can't change start/stop date columns with UPDATE! |
| 85 | + Use set_timetravel (below) if you need in this. |
| 86 | + |
| 87 | + FUNCTIONs: |
| 88 | + |
| 89 | +timetravel() is general trigger function. |
| 90 | + |
| 91 | + You are to create trigger BEFORE UPDATE OR DELETE using this |
| 92 | +function on a time-traveled table. You are to specify two arguments: name of |
| 93 | +start_date column and name of stop_date column in triggered table. |
| 94 | +Or add +3 arguments: |
| 95 | + name of insert_user column, name of update_user column, name of delete_user column |
| 96 | + |
| 97 | +currabstime() may be used in DEFAULT for start_date column to get |
| 98 | +current date. |
| 99 | +!!! I deleted this function, because I newer used this. |
| 100 | + |
| 101 | +set_timetravel() allows you turn time-travel ON/OFF for a table: |
| 102 | + |
| 103 | + set_timetravel('XXX', 1) will turn TT ON for table XXX (and report |
| 104 | +old status). |
| 105 | + set_timetravel('XXX', 0) will turn TT OFF for table XXX (-"-). |
| 106 | + |
| 107 | +Turning TT OFF allows you do with a table ALL what you want. |
| 108 | + |
| 109 | +get_timetravel() reports time-travel status ON(1)/OFF(0) for a table. |
| 110 | +get_timetravel() and set_timetravel() not checking existing of table and |
| 111 | +existing of timetravel trigger on specified table. |
| 112 | + |
| 113 | + There is example in timetravel.example. |
| 114 | + |
| 115 | + To CREATE FUNCTIONs use timetravel.sql (will be made by gmake from |
| 116 | +timetravel.source). |