-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
show_commit_timestamp
345 lines (254 loc) · 6.59 KB
/
show_commit_timestamp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
statement ok
create table foo (i int primary key)
subtest basic
statement ok
begin;
insert into foo values (1)
let $commit_ts
show commit timestamp
statement ok
commit
let $commit_ts_after_txn
show commit timestamp
query B
select $commit_ts_after_txn = $commit_ts
----
true
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts
----
1
subtest after_release_cockroach_restart
statement ok
begin;
savepoint cockroach_restart;
insert into foo values (2);
release cockroach_restart
let $commit_ts
show commit timestamp
let $commit_ts_again
show commit timestamp
statement ok
commit
let $commit_ts_after_txn
show commit timestamp
query BB
select $commit_ts_after_txn = $commit_ts, $commit_ts = $commit_ts_again
----
true true
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts
----
2
subtest after_release_cockroach_restart_txn
statement ok
begin;
savepoint cockroach_restart;
insert into foo values (3);
release cockroach_restart;
commit
let $commit_ts
show commit timestamp
let $commit_ts_again
show commit timestamp
query BB
select $commit_ts_after_txn = $commit_ts, $commit_ts = $commit_ts_again
----
false true
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts
----
3
subtest implicit_txn
statement ok
insert into foo values (4);
let $commit_ts
show commit timestamp
let $commit_ts_again
show commit timestamp
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts
----
4
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts_again
----
4
query I
select * from foo where crdb_internal_mvcc_timestamp = ($commit_ts) + 0.0000000001
----
query I
select * from foo where crdb_internal_mvcc_timestamp = ($commit_ts) + 1
----
subtest invalid_transaction_state
statement ok
begin;
rollback
statement error pgcode 25000 no previous transaction
show commit timestamp
statement ok
insert into foo values (5)
statement ok
show commit timestamp
statement error pgcode 22012 division by zero
begin;
select 1/0;
statement error pgcode 25P02 current transaction is aborted, commands ignored until end of transaction block
show commit timestamp
statement ok
rollback
statement error pgcode 25000 no previous transaction
show commit timestamp
subtest multistatement_explicit_transaction
let $commit_ts
insert into foo values (6);
insert into foo values (7);
show commit timestamp
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts order by i
----
6
7
statement error pgcode 25000 cannot use SHOW COMMIT TIMESTAMP in multi-statement implicit transaction
insert into foo values (8);
show commit timestamp;
insert into foo values (9)
# Note that this will become two transaction and we'll get the commit timestamp
# of the second one.
let $commit_ts
insert into foo values(8);
insert into foo values(9);
begin;
insert into foo values(10);
commit;
insert into foo values(11);
insert into foo values (12);
show commit timestamp
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts order by i
----
11
12
# Ensure that show commit timestamp can be used in upgraded explicit
# transactions.
let $commit_ts
insert into foo values(13);
insert into foo values(14);
begin;
insert into foo values(15);
show commit timestamp;
# Despite this error, the transaction has committed at this point and we
# have the causality token.
statement error pgcode 25000 current transaction is committed, commands ignored until end of transaction block
insert into foo values(16);
statement ok
commit
query I
select * from foo where crdb_internal_mvcc_timestamp = $commit_ts order by i
----
13
14
15
subtest udf
statement error pgcode 0A000 unimplemented: SHOW COMMIT TIMESTAMP usage inside a function definition
create function f() returns decimal volatile language sql as $$ show commit timestamp $$;
subtest prepare
# You cannot prepare SHOW COMMIT TIMESTAMP because it is not preparable.
statement error pgcode 42601 at or near "commit": syntax error
prepare s as show commit timestamp;
subtest cte
# You cannot use SHOW COMMIT TIMESTAMP because it is not a row source, nor
# is it preparable, so it cannot be used in a CTE or square brackets.
statement error pgcode 42601 at or near "commit": syntax error
with committs as (show commit timestamp) select * from committs;
statement error pgcode 42601 at or near "commit": syntax error
select * from [show commit timestamp]
# Test that jobs still run and are waited for as a part of committing
# internally.
subtest ddl
statement ok
drop table foo;
create table foo (i int primary key)
statement ok
insert into foo values (1)
statement ok
begin;
alter table foo add column j int default 42
let $commit_ts
show commit timestamp
statement ok
commit;
query II
select * from foo
----
1 42
# Note that the causality token of the DDL is the timestamp when the user
# transaction commits, it does not tell you about the timestamp when the
# asynchronous schema change job[s] complete.
query I
select * from foo as of system time $commit_ts
----
1
statement ok
drop table foo;
create table foo (i int primary key);
insert into foo values (1);
statement ok
begin;
alter table foo add check (i <= 0)
# Verify that SHOW COMMIT TIMESTAMP will wait for the schema changes.
statement error pgcode XXA00 transaction committed but schema change aborted with error: \(23514\): validation of CHECK "i <= 0:::INT8" failed on row: i=1
show commit timestamp
statement ok
rollback;
drop table foo
# Above we tested that we can issue RELEASE SAVEPOINT cockroach_restart after
# SHOW COMMIT TIMESTAMP if we had set it up. Here test that it is not allowed
# if it had not been added.
subtest disallow_cockroach_restart_without_savepoint
statement ok
begin;
show commit timestamp
statement error pgcode 25000 current transaction is committed, commands ignored until end of transaction block
release savepoint cockroach_restart;
statement ok
rollback
# Test that the causality token is the same timestamp as ends up on rows,
# even if the transaction gets pushed.
subtest causality_token_equals_mvcc_timestamp
user testuser
statement ok
create table foo (i int primary key);
statement ok
begin;
insert into foo values (1), (3);
let $ts1
show commit timestamp
statement ok
commit
statement ok
begin;
insert into foo values (2), (4);
user root
statement ok
begin priority high; select * from foo; commit;
user testuser
let $ts2
show commit timestamp
statement ok
commit
query IT
SELECT i,
CASE
WHEN ts = $ts1 THEN 'ts1'
WHEN ts = $ts2 THEN 'ts2'
END
FROM (SELECT i, crdb_internal_mvcc_timestamp AS ts FROM foo)
ORDER BY i ASC;
----
1 ts1
2 ts2
3 ts1
4 ts2
statement ok
drop table foo