@@ -24,6 +24,7 @@ import (
24
24
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
25
25
"github.com/cockroachdb/cockroach/pkg/util/log/logconfig"
26
26
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
27
+ "github.com/cockroachdb/cockroach/pkg/util/log/logtestutils"
27
28
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
28
29
"github.com/cockroachdb/errors"
29
30
"github.com/cockroachdb/redact"
@@ -115,6 +116,46 @@ func TestStructuredEventLogging(t *testing.T) {
115
116
}
116
117
}
117
118
119
+ func TestStructuredEventLogging_txnReadTimestamp (t * testing.T ) {
120
+ defer leaktest .AfterTest (t )()
121
+ defer log .Scope (t ).Close (t )
122
+
123
+ ctx := context .Background ()
124
+ appLogsSpy := logtestutils .NewStructuredLogSpy (
125
+ t ,
126
+ []logpb.Channel {logpb .Channel_SQL_SCHEMA },
127
+ []string {"create_table" },
128
+ func (entry logpb.Entry ) (eventpb.CreateTable , error ) {
129
+ var cte eventpb.CreateTable
130
+ if err := json .Unmarshal ([]byte (entry .Message [entry .StructuredStart :entry .StructuredEnd ]), & cte ); err != nil {
131
+ return cte , err
132
+ }
133
+ return cte , nil
134
+ },
135
+ )
136
+
137
+ cleanup := log .InterceptWith (ctx , appLogsSpy )
138
+ defer cleanup ()
139
+
140
+ s , conn , _ := serverutils .StartServer (t , base.TestServerArgs {})
141
+ defer s .Stopper ().Stop (ctx )
142
+
143
+ runner := sqlutils .MakeSQLRunner (conn )
144
+ runner .Exec (t , "CREATE TABLE test (id INT PRIMARY KEY)" )
145
+ runner .Exec (t , "BEGIN" )
146
+ runner .Exec (t , "SET autocommit_before_ddl = false" )
147
+ runner .Exec (t , "CREATE TABLE test1 (id INT PRIMARY KEY)" )
148
+ runner .Exec (t , "CREATE TABLE test2 (id INT PRIMARY KEY)" )
149
+ runner .Exec (t , "COMMIT" )
150
+
151
+ createTables := appLogsSpy .GetLogs (logpb .Channel_SQL_SCHEMA )
152
+ require .Len (t , createTables , 3 )
153
+ // Not created in the same transaction, so transaction read timestamps are different
154
+ require .NotEqual (t , createTables [0 ].TxnReadTimestamp , createTables [1 ].TxnReadTimestamp )
155
+ // Created in the same transaction, so transaction read timestamps are the same
156
+ require .Equal (t , createTables [1 ].TxnReadTimestamp , createTables [2 ].TxnReadTimestamp )
157
+ }
158
+
118
159
var execLogRe = regexp .MustCompile (`event_log.go` )
119
160
120
161
// Test the SQL_PERF and SQL_INTERNAL_PERF logging channels.
@@ -140,7 +181,7 @@ func TestPerfLogging(t *testing.T) {
140
181
{
141
182
query : `SELECT pg_sleep(0.256)` ,
142
183
errRe : `` ,
143
- logRe : `"EventType":"slow_query","Statement":"SELECT pg_sleep\(‹0.256›\)","Tag":"SELECT","User":"root","ExecMode":"exec","NumRows":1` ,
184
+ logRe : `"EventType":"slow_query","Statement":"SELECT pg_sleep\(‹0.256›\)","Tag":"SELECT","User":"root","TxnReadTimestamp":.*," ExecMode":"exec","NumRows":1` ,
144
185
logExpected : true ,
145
186
channel : channel .SQL_PERF ,
146
187
},
@@ -161,7 +202,7 @@ func TestPerfLogging(t *testing.T) {
161
202
{
162
203
query : `INSERT INTO t VALUES (2, pg_sleep(0.256), 'x')` ,
163
204
errRe : `` ,
164
- logRe : `"EventType":"slow_query","Statement":"INSERT INTO .*\.t VALUES \(‹2›, pg_sleep\(‹0.256›\), ‹'x'›\)","Tag":"INSERT","User":"root","ExecMode":"exec","NumRows":1` ,
205
+ logRe : `"EventType":"slow_query","Statement":"INSERT INTO .*\.t VALUES \(‹2›, pg_sleep\(‹0.256›\), ‹'x'›\)","Tag":"INSERT","User":"root","TxnReadTimestamp":.*," ExecMode":"exec","NumRows":1` ,
165
206
logExpected : true ,
166
207
channel : channel .SQL_PERF ,
167
208
},
@@ -175,7 +216,7 @@ func TestPerfLogging(t *testing.T) {
175
216
{
176
217
query : `INSERT INTO t VALUES (4, pg_sleep(0.256), repeat('x', 1024))` ,
177
218
errRe : `` ,
178
- logRe : `"EventType":"slow_query","Statement":"INSERT INTO .*\.t VALUES \(‹4›, pg_sleep\(‹0.256›\), repeat\(‹'x'›, ‹1024›\)\)","Tag":"INSERT","User":"root","ExecMode":"exec","NumRows":1` ,
219
+ logRe : `"EventType":"slow_query","Statement":"INSERT INTO .*\.t VALUES \(‹4›, pg_sleep\(‹0.256›\), repeat\(‹'x'›, ‹1024›\)\)","Tag":"INSERT","User":"root","TxnReadTimestamp":.*," ExecMode":"exec","NumRows":1` ,
179
220
logExpected : true ,
180
221
channel : channel .SQL_PERF ,
181
222
},
@@ -189,7 +230,7 @@ func TestPerfLogging(t *testing.T) {
189
230
{
190
231
query : `SELECT *, pg_sleep(0.064) FROM t` ,
191
232
errRe : `` ,
192
- logRe : `"EventType":"slow_query","Statement":"SELECT \*, pg_sleep\(‹0.064›\) FROM .*\.t","Tag":"SELECT","User":"root","ExecMode":"exec","NumRows":4` ,
233
+ logRe : `"EventType":"slow_query","Statement":"SELECT \*, pg_sleep\(‹0.064›\) FROM .*\.t","Tag":"SELECT","User":"root","TxnReadTimestamp":.*," ExecMode":"exec","NumRows":4` ,
193
234
logExpected : true ,
194
235
channel : channel .SQL_PERF ,
195
236
},
0 commit comments