Skip to content

Commit 524cfad

Browse files
committed
Here is 4 file in tgz:
the new timetravel.c, new timetravel.README (cut from spi/README and modified), modified timetravel.sql.in and modified timetravel.example. Features: - optionally 3 parameter for insert/update/delete user name - work with CREATE UNIQUE INDEX ixxx on table xxx (unique_field,time_off); (the original version was work with unique index on 6.5.0-6.5.3, and not work on 7.3.2,7.3.3) (before 6.5.0 and between 6.5.3 and 7.3.2 I dont know) - get_timetravel(tablename) function for check timetravel-status. - timetravel trigger not change oid of the active record. (it is not a good feature, because the old version is automatice prevent the paralel update with "where oid=nnn") B?jthe Zolt?n
1 parent 38fb906 commit 524cfad

File tree

5 files changed

+432
-209
lines changed

5 files changed

+432
-209
lines changed

contrib/oid2name/oid2name.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ sql_exec_dumptable(PGconn *conn, int systables)
355355
if (systables == 1)
356356
snprintf(todo, 1024, "select relfilenode,relname from pg_class order by relname");
357357
else
358-
snprintf(todo, 1024, "select relfilenode,relname from pg_class where relname not like 'pg_%%' order by relname");
358+
snprintf(todo, 1024, "select relfilenode,relname from pg_class "
359+
"where reltype not in ('v','c') and "
360+
"relname not like 'pg_%%' order by relname");
359361

360362
sql_exec(conn, todo, 0);
361363
}

contrib/spi/README.timetravel

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_on abstime default currabstime(),
41+
date_off abstime default 'infinity'
42+
... ...
43+
/* !!! and (if have) */
44+
ins_user text /* user, who insert this record */
45+
upd_user text /* user, who updated this record */
46+
del_user text /* 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).

0 commit comments

Comments
 (0)