Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit af5c86e

Browse files
committed
General trigger functions for time-travel
1 parent f0b9b21 commit af5c86e

File tree

5 files changed

+525
-6
lines changed

5 files changed

+525
-6
lines changed

contrib/spi/Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ ifdef REFINT_VERBOSE
99
CFLAGS+= -DREFINT_VERBOSE
1010
endif
1111

12-
TARGETS= refint$(DLSUFFIX) refint.sql
12+
TARGETS= refint$(DLSUFFIX) refint.sql timetravel$(DLSUFFIX) timetravel.sql
1313

1414
CLEANFILES+= $(TARGETS)
1515

1616
all:: $(TARGETS)
17-
rm -f *.obj *.pic
1817

1918
%.sql: %.source
2019
rm -f $@; \
@@ -23,4 +22,4 @@ all:: $(TARGETS)
2322
-e "s:_DLSUFFIX_:$(DLSUFFIX):g" < $< > $@
2423

2524
clean:
26-
rm -f $(TARGETS) *.[op]*
25+
rm -f $(TARGETS)

contrib/spi/README

+70-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,77 @@ being deleted to null), triggered table column names which correspond
2828
to primary/unique key, referencing table name and column names corresponding
2929
to foreign key (, ... - as many referencing tables/keys as specified
3030
by first argument).
31-
Note, that NOT NULL constraint and unique index have to be defined
32-
by youself.
31+
Note, that NOT NULL constraint and unique index have to be defined by
32+
youself.
3333

34-
There are examples in refint.example and regression tests
34+
There are examples in refint.example and regression tests
3535
(sql/triggers.sql).
3636

37+
To CREATE FUNCTIONs use refint.sql (will be made by gmake from
38+
refint.source).
3739

40+
41+
2. timetravel.c - functions for implementing time travel feature.
42+
43+
Old internally supported time-travel (TT) used insert/delete
44+
transaction commit times. To get the same feature using triggers
45+
you are to add to a table two columns of abstime type to store
46+
date when a tuple was inserted (start_date) and changed/deleted
47+
(stop_date):
48+
49+
CREATE TABLE XXX (
50+
... ...
51+
date_on abstime default currabstime(),
52+
date_off abstime default 'infinity'
53+
... ...
54+
);
55+
56+
- so, tuples being inserted with NULLs in date_on/date_off will get
57+
_current_date_ in date_on (name of start_date column in XXX) and INFINITY in
58+
date_off (name of stop_date column in XXX).
59+
60+
Tuples with stop_date equal INFINITY are "valid now": when trigger will
61+
be fired for UPDATE/DELETE of a tuple with stop_date NOT equal INFINITY then
62+
this tuple will not be changed/deleted!
63+
64+
If stop_date equal INFINITY then on
65+
66+
UPDATE: only stop_date in tuple being updated will be changed to current
67+
date and new tuple with new data (coming from SET ... in UPDATE) will be
68+
inserted. Start_date in this new tuple will be setted to current date and
69+
stop_date - to INFINITY.
70+
71+
DELETE: new tuple will be inserted with stop_date setted to current date
72+
(and with the same data in other columns as in tuple being deleted).
73+
74+
NOTE:
75+
1. To get tuples "valid now" you are to add _stop_date_ = 'infinity'
76+
to WHERE. Internally supported TT allowed to avoid this...
77+
Fixed rewriting RULEs could help here...
78+
As work arround you may use VIEWs...
79+
2. You can't change start/stop date columns with UPDATE!
80+
Use set_timetravel (below) if you need in this.
81+
82+
FUNCTIONs:
83+
84+
timetravel() is general trigger function.
85+
86+
You are to create trigger BEFORE (!!!) UPDATE OR DELETE using this
87+
function on a time-traveled table. You are to specify two arguments: name of
88+
start_date column and name of stop_date column in triggered table.
89+
90+
currabstime() may be used in DEFAULT for start_date column to get
91+
current date.
92+
93+
set_timetravel() allows you turn time-travel ON/OFF for a table:
94+
95+
set_timetravel('XXX', 1) will turn TT ON for table XXX (and report
96+
old status).
97+
set_timetravel('XXX', 0) will turn TT OFF for table XXX (-"-).
98+
99+
Turning TT OFF allows you do with a table ALL what you want.
100+
101+
There is example in timetravel.example.
102+
103+
To CREATE FUNCTIONs use timetravel.sql (will be made by gmake from
104+
timetravel.source).

0 commit comments

Comments
 (0)