Skip to content

Commit 30e194f

Browse files
committed
explain: add transaction information to EXPLAIN ANALYZE
Add transaction isolation, priority, and quality-of-service to the output of `EXPLAIN ANALYZE`. Release note (sql change): `EXPLAIN ANALYZE` output now includes: - the isolation level of the statement's transaction - the priority of the statement's transaction - the quality of service level of the statement's transaction
1 parent 1d3c11e commit 30e194f

File tree

14 files changed

+177
-3
lines changed

14 files changed

+177
-3
lines changed

pkg/sql/conn_executor_exec.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ func (ex *connExecutor) execStmtInOpenState(
627627
if !isPausablePortal() || portal.pauseInfo.execStmtInOpenState.ihWrapper == nil {
628628
ctx, needFinish = ih.Setup(
629629
ctx, ex.server.cfg, ex.statsCollector, p, ex.stmtDiagnosticsRecorder,
630-
stmt.StmtNoConstants, os.ImplicitTxn.Get(), ex.extraTxnState.shouldCollectTxnExecutionStats,
630+
stmt.StmtNoConstants, os.ImplicitTxn.Get(), ex.state.priority,
631+
ex.extraTxnState.shouldCollectTxnExecutionStats,
631632
)
632633
} else {
633634
ctx = portal.pauseInfo.execStmtInOpenState.ihWrapper.ctx
@@ -2926,7 +2927,11 @@ func (ex *connExecutor) recordTransactionFinish(
29262927
RowsWritten: ex.extraTxnState.rowsWritten,
29272928
BytesRead: ex.extraTxnState.bytesRead,
29282929
Priority: ex.state.priority,
2929-
SessionData: ex.sessionData(),
2930+
// TODO(107318): add isolation level
2931+
// TODO(107318): add qos
2932+
// TODO(107318): add asoftime or ishistorical
2933+
// TODO(107318): add readonly
2934+
SessionData: ex.sessionData(),
29302935
}
29312936

29322937
if ex.server.cfg.TestingKnobs.OnRecordTxnFinish != nil {

pkg/sql/instrumentation.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"time"
1919

2020
"github.com/cockroachdb/cockroach/pkg/keys"
21+
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation"
2122
"github.com/cockroachdb/cockroach/pkg/multitenant"
23+
"github.com/cockroachdb/cockroach/pkg/roachpb"
2224
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
2325
"github.com/cockroachdb/cockroach/pkg/settings"
2426
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
@@ -35,6 +37,7 @@ import (
3537
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
3638
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
3739
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
40+
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
3841
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
3942
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
4043
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
@@ -83,8 +86,12 @@ type instrumentationHelper struct {
8386

8487
// Query fingerprint (anonymized statement).
8588
fingerprint string
89+
90+
// Transaction information.
8691
implicitTxn bool
87-
codec keys.SQLCodec
92+
txnPriority roachpb.UserPriority
93+
94+
codec keys.SQLCodec
8895

8996
// -- The following fields are initialized by Setup() --
9097

@@ -254,10 +261,12 @@ func (ih *instrumentationHelper) Setup(
254261
stmtDiagnosticsRecorder *stmtdiagnostics.Registry,
255262
fingerprint string,
256263
implicitTxn bool,
264+
txnPriority roachpb.UserPriority,
257265
collectTxnExecStats bool,
258266
) (newCtx context.Context, needFinish bool) {
259267
ih.fingerprint = fingerprint
260268
ih.implicitTxn = implicitTxn
269+
ih.txnPriority = txnPriority
261270
ih.codec = cfg.Codec
262271
ih.origCtx = ctx
263272
ih.evalCtx = p.EvalContext()
@@ -590,6 +599,14 @@ func (ih *instrumentationHelper) emitExplainAnalyzePlanToOutputBuilder(
590599
}
591600
}
592601

602+
qos := sessiondatapb.Normal
603+
iso := isolation.Serializable
604+
if ih.evalCtx != nil {
605+
qos = ih.evalCtx.QualityOfService()
606+
iso = ih.evalCtx.TxnIsoLevel
607+
}
608+
ob.AddTxnInfo(iso, ih.txnPriority, qos)
609+
593610
if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
594611
ob.AddTopLevelField("error emitting plan", fmt.Sprint(err))
595612
}

pkg/sql/opt/exec/execbuilder/testdata/dist_vectorize

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ rows decoded from KV: 5 (40 B, 10 KVs, 5 gRPC calls)
6060
maximum memory usage: <hidden>
6161
network usage: <hidden>
6262
regions: <hidden>
63+
isolation level: serializable
64+
priority: normal
65+
quality of service: regular
6366
·
6467
• group (scalar)
6568
│ nodes: <hidden>
@@ -94,6 +97,9 @@ rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
9497
maximum memory usage: <hidden>
9598
network usage: <hidden>
9699
regions: <hidden>
100+
isolation level: serializable
101+
priority: normal
102+
quality of service: regular
97103
·
98104
• merge join
99105
│ nodes: <hidden>

pkg/sql/opt/exec/execbuilder/testdata/distsql_misc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ rows decoded from KV: 1,000 (7.8 KiB, 2,000 KVs, 1,000 gRPC calls)
109109
maximum memory usage: <hidden>
110110
network usage: <hidden>
111111
regions: <hidden>
112+
isolation level: serializable
113+
priority: normal
114+
quality of service: regular
112115
·
113116
• create statistics
114117
·

pkg/sql/opt/exec/execbuilder/testdata/explain

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,9 @@ vectorized: <hidden>
22272227
maximum memory usage: <hidden>
22282228
network usage: <hidden>
22292229
regions: <hidden>
2230+
isolation level: serializable
2231+
priority: normal
2232+
quality of service: regular
22302233
·
22312234
• values
22322235
nodes: <hidden>

pkg/sql/opt/exec/execbuilder/testdata/explain_analyze

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ vectorized: <hidden>
1313
maximum memory usage: <hidden>
1414
network usage: <hidden>
1515
regions: <hidden>
16+
isolation level: serializable
17+
priority: normal
18+
quality of service: regular
1619
·
1720
• scan
1821
nodes: <hidden>
@@ -42,6 +45,9 @@ rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls)
4245
maximum memory usage: <hidden>
4346
network usage: <hidden>
4447
regions: <hidden>
48+
isolation level: serializable
49+
priority: normal
50+
quality of service: regular
4551
·
4652
• scan
4753
nodes: <hidden>
@@ -58,6 +64,50 @@ regions: <hidden>
5864
table: kv@kv_pkey
5965
spans: [/2 - ]
6066

67+
# Test that non-default isolation level, priority, and QoS are correct.
68+
69+
statement ok
70+
SET default_transaction_quality_of_service = background
71+
72+
statement ok
73+
BEGIN ISOLATION LEVEL READ COMMITTED PRIORITY LOW
74+
75+
query T
76+
EXPLAIN ANALYZE (PLAN) SELECT * FROM kv WHERE k >= 2
77+
----
78+
planning time: 10µs
79+
execution time: 100µs
80+
distribution: <hidden>
81+
vectorized: <hidden>
82+
rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls)
83+
maximum memory usage: <hidden>
84+
network usage: <hidden>
85+
regions: <hidden>
86+
isolation level: read committed
87+
priority: low
88+
quality of service: background
89+
·
90+
• scan
91+
nodes: <hidden>
92+
regions: <hidden>
93+
actual row count: 3
94+
KV time: 0µs
95+
KV contention time: 0µs
96+
KV rows decoded: 3
97+
KV pairs read: 6
98+
KV bytes read: 24 B
99+
KV gRPC calls: 3
100+
estimated max memory allocated: 0 B
101+
missing stats
102+
table: kv@kv_pkey
103+
spans: [/2 - ]
104+
105+
statement ok
106+
ROLLBACK
107+
108+
statement ok
109+
RESET default_transaction_quality_of_service
110+
61111
statement ok
62112
CREATE TABLE ab (a INT PRIMARY KEY, b INT);
63113
INSERT INTO ab VALUES (10,100), (40,400), (50,500);
@@ -73,6 +123,9 @@ rows decoded from KV: 7 (56 B, 14 KVs, 7 gRPC calls)
73123
maximum memory usage: <hidden>
74124
network usage: <hidden>
75125
regions: <hidden>
126+
isolation level: serializable
127+
priority: normal
128+
quality of service: regular
76129
·
77130
• hash join (inner)
78131
│ columns: (k, v, a, b)

pkg/sql/opt/exec/execbuilder/testdata/explain_analyze_plans

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
6969
maximum memory usage: <hidden>
7070
network usage: <hidden>
7171
regions: <hidden>
72+
isolation level: serializable
73+
priority: normal
74+
quality of service: regular
7275
·
7376
• group (streaming)
7477
│ nodes: <hidden>
@@ -131,6 +134,9 @@ rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
131134
maximum memory usage: <hidden>
132135
network usage: <hidden>
133136
regions: <hidden>
137+
isolation level: serializable
138+
priority: normal
139+
quality of service: regular
134140
·
135141
• sort
136142
│ nodes: <hidden>
@@ -201,6 +207,9 @@ rows decoded from KV: 10 (80 B, 20 KVs, 10 gRPC calls)
201207
maximum memory usage: <hidden>
202208
network usage: <hidden>
203209
regions: <hidden>
210+
isolation level: serializable
211+
priority: normal
212+
quality of service: regular
204213
·
205214
• cross join
206215
│ nodes: <hidden>
@@ -284,6 +293,9 @@ rows decoded from KV: 5 (40 B, 10 KVs, 5 gRPC calls)
284293
maximum memory usage: <hidden>
285294
network usage: <hidden>
286295
regions: <hidden>
296+
isolation level: serializable
297+
priority: normal
298+
quality of service: regular
287299
·
288300
• window
289301
│ nodes: <hidden>
@@ -321,6 +333,9 @@ vectorized: <hidden>
321333
maximum memory usage: <hidden>
322334
network usage: <hidden>
323335
regions: <hidden>
336+
isolation level: serializable
337+
priority: normal
338+
quality of service: regular
324339
·
325340
• scan
326341
nodes: <hidden>
@@ -355,6 +370,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
355370
maximum memory usage: <hidden>
356371
network usage: <hidden>
357372
regions: <hidden>
373+
isolation level: serializable
374+
priority: normal
375+
quality of service: regular
358376
·
359377
• root
360378

pkg/sql/opt/exec/execbuilder/testdata/inverted_index_geospatial

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ rows decoded from KV: 6 (48 B, 12 KVs, 6 gRPC calls)
3030
maximum memory usage: <hidden>
3131
network usage: <hidden>
3232
regions: <hidden>
33+
isolation level: serializable
34+
priority: normal
35+
quality of service: regular
3336
·
3437
• sort
3538
│ nodes: <hidden>
@@ -119,6 +122,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
119122
maximum memory usage: <hidden>
120123
network usage: <hidden>
121124
regions: <hidden>
125+
isolation level: serializable
126+
priority: normal
127+
quality of service: regular
122128
·
123129
• sort
124130
│ nodes: <hidden>
@@ -192,6 +198,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
192198
maximum memory usage: <hidden>
193199
network usage: <hidden>
194200
regions: <hidden>
201+
isolation level: serializable
202+
priority: normal
203+
quality of service: regular
195204
·
196205
• sort
197206
│ nodes: <hidden>

pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
8484
maximum memory usage: <hidden>
8585
network usage: <hidden>
8686
regions: <hidden>
87+
isolation level: serializable
88+
priority: normal
89+
quality of service: regular
8790
·
8891
• limit
8992
│ count: 1
@@ -165,6 +168,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
165168
maximum memory usage: <hidden>
166169
network usage: <hidden>
167170
regions: <hidden>
171+
isolation level: serializable
172+
priority: normal
173+
quality of service: regular
168174
·
169175
• limit
170176
│ count: 1
@@ -319,6 +325,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
319325
maximum memory usage: <hidden>
320326
network usage: <hidden>
321327
regions: <hidden>
328+
isolation level: serializable
329+
priority: normal
330+
quality of service: regular
322331
·
323332
• limit
324333
│ count: 1
@@ -411,6 +420,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
411420
maximum memory usage: <hidden>
412421
network usage: <hidden>
413422
regions: <hidden>
423+
isolation level: serializable
424+
priority: normal
425+
quality of service: regular
414426
·
415427
• limit
416428
│ count: 2

pkg/sql/opt/exec/execbuilder/testdata/prepare

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ rows decoded from KV: 1 (8 B, 2 KVs, 1 gRPC calls)
119119
maximum memory usage: <hidden>
120120
network usage: <hidden>
121121
regions: <hidden>
122+
isolation level: serializable
123+
priority: normal
124+
quality of service: regular
122125
·
123126
• scan
124127
nodes: <hidden>
@@ -145,6 +148,9 @@ vectorized: <hidden>
145148
maximum memory usage: <hidden>
146149
network usage: <hidden>
147150
regions: <hidden>
151+
isolation level: serializable
152+
priority: normal
153+
quality of service: regular
148154
·
149155
• scan
150156
nodes: <hidden>

0 commit comments

Comments
 (0)