File: system_time_periods.out

package info (click to toggle)
postgresql-periods 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,084 kB
  • sloc: sql: 9,744; ansic: 548; makefile: 30; sh: 1
file content (284 lines) | stat: -rw-r--r-- 11,304 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
SELECT setting::integer < 90600 AS pre_96
FROM pg_settings WHERE name = 'server_version_num';
 pre_96 
--------
 f
(1 row)

/* Run tests as unprivileged user */
SET ROLE TO periods_unprivileged_user;
/* SYSTEM_TIME with date */
BEGIN;
SELECT transaction_timestamp()::date AS xd,
       transaction_timestamp()::timestamp AS xts,
       transaction_timestamp() AS xtstz
\gset
CREATE TABLE sysver_date (val text, start_date date, end_date date);
SELECT periods.add_system_time_period('sysver_date', 'start_date', 'end_date');
 add_system_time_period 
------------------------
 t
(1 row)

TABLE periods.periods;
 table_name  | period_name | start_column_name | end_column_name | range_type |    bounds_check_constraint    
-------------+-------------+-------------------+-----------------+------------+-------------------------------
 sysver_date | system_time | start_date        | end_date        | daterange  | sysver_date_system_time_check
(1 row)

INSERT INTO sysver_date DEFAULT VALUES;
SELECT val, start_date = :'xd' AS start_date_eq, end_date FROM sysver_date;
 val | start_date_eq | end_date 
-----+---------------+----------
     | t             | infinity
(1 row)

DROP TABLE sysver_date;
/* SYSTEM_TIME with timestamp without time zone */
CREATE TABLE sysver_ts (val text, start_ts timestamp without time zone, end_ts timestamp without time zone);
SELECT periods.add_system_time_period('sysver_ts', 'start_ts', 'end_ts');
 add_system_time_period 
------------------------
 t
(1 row)

TABLE periods.periods;
 table_name | period_name | start_column_name | end_column_name | range_type |   bounds_check_constraint   
------------+-------------+-------------------+-----------------+------------+-----------------------------
 sysver_ts  | system_time | start_ts          | end_ts          | tsrange    | sysver_ts_system_time_check
(1 row)

INSERT INTO sysver_ts DEFAULT VALUES;
SELECT val, start_ts = :'xts' AS start_ts_eq, end_ts FROM sysver_ts;
 val | start_ts_eq |  end_ts  
-----+-------------+----------
     | t           | infinity
(1 row)

DROP TABLE sysver_ts;
/* SYSTEM_TIME with timestamp with time zone */
CREATE TABLE sysver_tstz (val text, start_tstz timestamp with time zone, end_tstz timestamp with time zone);
SELECT periods.add_system_time_period('sysver_tstz', 'start_tstz', 'end_tstz');
 add_system_time_period 
------------------------
 t
(1 row)

TABLE periods.periods;
 table_name  | period_name | start_column_name | end_column_name | range_type |    bounds_check_constraint    
-------------+-------------+-------------------+-----------------+------------+-------------------------------
 sysver_tstz | system_time | start_tstz        | end_tstz        | tstzrange  | sysver_tstz_system_time_check
(1 row)

INSERT INTO sysver_tstz DEFAULT VALUES;
SELECT val, start_tstz = :'xtstz' AS start_tstz_eq, end_tstz FROM sysver_tstz;
 val | start_tstz_eq | end_tstz 
-----+---------------+----------
     | t             | infinity
(1 row)

DROP TABLE sysver_tstz;
COMMIT;
/* Basic SYSTEM_TIME periods with CASCADE/purge */
CREATE TABLE sysver (val text);
SELECT periods.add_system_time_period('sysver', 'startname');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.drop_period('sysver', 'system_time', drop_behavior => 'CASCADE', purge => true);
 drop_period 
-------------
 t
(1 row)

SELECT periods.add_system_time_period('sysver', end_column_name => 'endname');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.drop_period('sysver', 'system_time', drop_behavior => 'CASCADE', purge => true);
 drop_period 
-------------
 t
(1 row)

SELECT periods.add_system_time_period('sysver', 'startname', 'endname');
 add_system_time_period 
------------------------
 t
(1 row)

TABLE periods.periods;
 table_name | period_name | start_column_name | end_column_name | range_type | bounds_check_constraint  
------------+-------------+-------------------+-----------------+------------+--------------------------
 sysver     | system_time | startname         | endname         | tstzrange  | sysver_system_time_check
(1 row)

TABLE periods.system_time_periods;
 table_name | period_name |   infinity_check_constraint   |      generated_always_trigger       |      write_history_trigger       | truncate_trigger | excluded_column_names 
------------+-------------+-------------------------------+-------------------------------------+----------------------------------+------------------+-----------------------
 sysver     | system_time | sysver_endname_infinity_check | sysver_system_time_generated_always | sysver_system_time_write_history | sysver_truncate  | {}
(1 row)

SELECT periods.drop_system_time_period('sysver', drop_behavior => 'CASCADE', purge => true);
 drop_system_time_period 
-------------------------
 t
(1 row)

SELECT periods.add_system_time_period('sysver', 'endname', 'startname',
        bounds_check_constraint => 'b',
        infinity_check_constraint => 'i',
        generated_always_trigger => 'g',
        write_history_trigger => 'w',
        truncate_trigger => 't');
 add_system_time_period 
------------------------
 t
(1 row)

TABLE periods.periods;
 table_name | period_name | start_column_name | end_column_name | range_type | bounds_check_constraint 
------------+-------------+-------------------+-----------------+------------+-------------------------
 sysver     | system_time | endname           | startname       | tstzrange  | b
(1 row)

TABLE periods.system_time_periods;
 table_name | period_name | infinity_check_constraint | generated_always_trigger | write_history_trigger | truncate_trigger | excluded_column_names 
------------+-------------+---------------------------+--------------------------+-----------------------+------------------+-----------------------
 sysver     | system_time | i                         | g                        | w                     | t                | {}
(1 row)

SELECT periods.drop_system_time_period('sysver', drop_behavior => 'CASCADE', purge => true);
 drop_system_time_period 
-------------------------
 t
(1 row)

SELECT periods.add_system_time_period('sysver');
 add_system_time_period 
------------------------
 t
(1 row)

DROP TABLE sysver;
TABLE periods.periods;
 table_name | period_name | start_column_name | end_column_name | range_type | bounds_check_constraint 
------------+-------------+-------------------+-----------------+------------+-------------------------
(0 rows)

TABLE periods.system_time_periods;
 table_name | period_name | infinity_check_constraint | generated_always_trigger | write_history_trigger | truncate_trigger | excluded_column_names 
------------+-------------+---------------------------+--------------------------+-----------------------+------------------+-----------------------
(0 rows)

/* Forbid UNIQUE keys on system_time columns */
CREATE TABLE no_unique (col1 timestamp with time zone, s bigint, e bigint);
SELECT periods.add_period('no_unique', 'p', 's', 'e');
 add_period 
------------
 t
(1 row)

SELECT periods.add_unique_key('no_unique', ARRAY['col1'], 'p'); -- passes
  add_unique_key  
------------------
 no_unique_col1_p
(1 row)

SELECT periods.add_system_time_period('no_unique');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.add_unique_key('no_unique', ARRAY['system_time_start'], 'p'); -- fails
ERROR:  columns in period for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_unique_key(regclass,name[],name,name,name,name) line 78 at RAISE
SELECT periods.add_unique_key('no_unique', ARRAY['system_time_end'], 'p'); -- fails
ERROR:  columns in period for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_unique_key(regclass,name[],name,name,name,name) line 78 at RAISE
SELECT periods.add_unique_key('no_unique', ARRAY['col1'], 'system_time'); -- fails
ERROR:  periods for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_unique_key(regclass,name[],name,name,name,name) line 35 at RAISE
SELECT periods.drop_system_time_period('no_unique');
 drop_system_time_period 
-------------------------
 t
(1 row)

SELECT periods.add_unique_key('no_unique', ARRAY['system_time_start'], 'p'); -- passes
        add_unique_key         
-------------------------------
 no_unique_system_time_start_p
(1 row)

SELECT periods.add_unique_key('no_unique', ARRAY['system_time_end'], 'p'); -- passes
       add_unique_key        
-----------------------------
 no_unique_system_time_end_p
(1 row)

SELECT periods.add_system_time_period('no_unique'); -- fails
ERROR:  columns in period for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_system_time_period(regclass,name,name,name,name,name,name,name,name[]) line 48 at RAISE
SELECT periods.drop_unique_key('no_unique', 'no_unique_system_time_start_p');
 drop_unique_key 
-----------------
 
(1 row)

SELECT periods.drop_unique_key('no_unique', 'no_unique_system_time_end_p');
 drop_unique_key 
-----------------
 
(1 row)

/* Forbid foreign keys on system_time columns */
CREATE TABLE no_unique_ref (LIKE no_unique);
SELECT periods.add_period('no_unique_ref', 'q', 's', 'e');
 add_period 
------------
 t
(1 row)

SELECT periods.add_system_time_period('no_unique_ref');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.add_foreign_key('no_unique_ref', ARRAY['system_time_start'], 'q', 'no_unique_col1_p'); -- fails
ERROR:  columns in period for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_foreign_key(regclass,name[],name,name,periods.fk_match_types,periods.fk_actions,periods.fk_actions,name,name,name,name,name) line 46 at RAISE
SELECT periods.add_foreign_key('no_unique_ref', ARRAY['system_time_end'], 'q', 'no_unique_col1_p'); -- fails
ERROR:  columns in period for SYSTEM_TIME are not allowed in UNIQUE keys
CONTEXT:  PL/pgSQL function periods.add_foreign_key(regclass,name[],name,name,periods.fk_match_types,periods.fk_actions,periods.fk_actions,name,name,name,name,name) line 46 at RAISE
SELECT periods.add_foreign_key('no_unique_ref', ARRAY['col1'], 'system_time', 'no_unique_col1_p'); -- fails
ERROR:  periods for SYSTEM_TIME are not allowed in foreign keys
CONTEXT:  PL/pgSQL function periods.add_foreign_key(regclass,name[],name,name,periods.fk_match_types,periods.fk_actions,periods.fk_actions,name,name,name,name,name) line 34 at RAISE
SELECT periods.drop_system_time_period('no_unique_ref');
 drop_system_time_period 
-------------------------
 t
(1 row)

SELECT periods.add_foreign_key('no_unique_ref', ARRAY['system_time_start'], 'q', 'no_unique_col1_p'); -- passes
          add_foreign_key          
-----------------------------------
 no_unique_ref_system_time_start_q
(1 row)

SELECT periods.add_foreign_key('no_unique_ref', ARRAY['system_time_end'], 'q', 'no_unique_col1_p'); -- passes
         add_foreign_key         
---------------------------------
 no_unique_ref_system_time_end_q
(1 row)

SELECT periods.add_system_time_period('no_unique_ref'); -- fails
ERROR:  columns for SYSTEM_TIME must not be part of foreign keys
CONTEXT:  PL/pgSQL function periods.add_system_time_period(regclass,name,name,name,name,name,name,name,name[]) line 168 at RAISE
DROP TABLE no_unique, no_unique_ref;