Skip to content

Commit

Permalink
explain: add transaction information to EXPLAIN ANALYZE
Browse files Browse the repository at this point in the history
Add transaction isolation, priority, and quality-of-service to the
output of `EXPLAIN ANALYZE`.

Release note: None
  • Loading branch information
michae2 committed Jul 19, 2023
1 parent adbcda5 commit 43818e8
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ func (ex *connExecutor) execStmtInOpenState(
if !isPausablePortal() || portal.pauseInfo.execStmtInOpenState.ihWrapper == nil {
ctx, needFinish = ih.Setup(
ctx, ex.server.cfg, ex.statsCollector, p, ex.stmtDiagnosticsRecorder,
stmt.StmtNoConstants, os.ImplicitTxn.Get(), ex.extraTxnState.shouldCollectTxnExecutionStats,
stmt.StmtNoConstants, os.ImplicitTxn.Get(), ex.state.priority,
ex.extraTxnState.shouldCollectTxnExecutionStats,
)
} else {
ctx = portal.pauseInfo.execStmtInOpenState.ihWrapper.ctx
Expand Down Expand Up @@ -2924,7 +2925,11 @@ func (ex *connExecutor) recordTransactionFinish(
RowsWritten: ex.extraTxnState.rowsWritten,
BytesRead: ex.extraTxnState.bytesRead,
Priority: ex.state.priority,
SessionData: ex.sessionData(),
// add isolation level?
// add qos?
// add asoftime?
// add readonly?
SessionData: ex.sessionData(),
}

if ex.server.cfg.TestingKnobs.OnRecordTxnFinish != nil {
Expand Down
19 changes: 18 additions & 1 deletion pkg/sql/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
Expand All @@ -35,6 +37,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
Expand Down Expand Up @@ -83,8 +86,12 @@ type instrumentationHelper struct {

// Query fingerprint (anonymized statement).
fingerprint string

// Transaction information.
implicitTxn bool
codec keys.SQLCodec
txnPriority roachpb.UserPriority

codec keys.SQLCodec

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

Expand Down Expand Up @@ -254,10 +261,12 @@ func (ih *instrumentationHelper) Setup(
stmtDiagnosticsRecorder *stmtdiagnostics.Registry,
fingerprint string,
implicitTxn bool,
txnPriority roachpb.UserPriority,
collectTxnExecStats bool,
) (newCtx context.Context, needFinish bool) {
ih.fingerprint = fingerprint
ih.implicitTxn = implicitTxn
ih.txnPriority = txnPriority
ih.codec = cfg.Codec
ih.origCtx = ctx
ih.evalCtx = p.EvalContext()
Expand Down Expand Up @@ -590,6 +599,14 @@ func (ih *instrumentationHelper) emitExplainAnalyzePlanToOutputBuilder(
}
}

qos := sessiondatapb.Normal
iso := isolation.Serializable
if ih.evalCtx != nil {
qos = ih.evalCtx.QualityOfService()
iso = ih.evalCtx.TxnIsoLevel
}
ob.AddTxnInfo(iso, ih.txnPriority, qos)

if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
ob.AddTopLevelField("error emitting plan", fmt.Sprint(err))
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,9 @@ vectorized: <hidden>
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• values
nodes: <hidden>
Expand Down
53 changes: 53 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain_analyze
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ vectorized: <hidden>
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• scan
nodes: <hidden>
Expand Down Expand Up @@ -42,6 +45,9 @@ rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• scan
nodes: <hidden>
Expand All @@ -58,6 +64,50 @@ regions: <hidden>
table: kv@kv_pkey
spans: [/2 - ]

# Test that non-default isolation level, priority, and QoS are correct.

statement ok
SET default_transaction_quality_of_service = background

statement ok
BEGIN ISOLATION LEVEL READ COMMITTED PRIORITY LOW

query T
EXPLAIN ANALYZE (PLAN) SELECT * FROM kv WHERE k >= 2
----
planning time: 10µs
execution time: 100µs
distribution: <hidden>
vectorized: <hidden>
rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: ReadCommitted
priority: low
quality of service: background
·
• scan
nodes: <hidden>
regions: <hidden>
actual row count: 3
KV time: 0µs
KV contention time: 0µs
KV rows decoded: 3
KV pairs read: 6
KV bytes read: 24 B
KV gRPC calls: 3
estimated max memory allocated: 0 B
missing stats
table: kv@kv_pkey
spans: [/2 - ]

statement ok
ROLLBACK

statement ok
RESET default_transaction_quality_of_service

statement ok
CREATE TABLE ab (a INT PRIMARY KEY, b INT);
INSERT INTO ab VALUES (10,100), (40,400), (50,500);
Expand All @@ -73,6 +123,9 @@ rows decoded from KV: 7 (56 B, 14 KVs, 7 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• hash join (inner)
│ columns: (k, v, a, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ rows decoded from KV: 6 (48 B, 12 KVs, 6 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• sort
│ nodes: <hidden>
Expand Down Expand Up @@ -119,6 +122,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• sort
│ nodes: <hidden>
Expand Down Expand Up @@ -192,6 +198,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• sort
│ nodes: <hidden>
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• limit
│ count: 1
Expand Down Expand Up @@ -165,6 +168,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• limit
│ count: 1
Expand Down Expand Up @@ -319,6 +325,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• limit
│ count: 1
Expand Down Expand Up @@ -411,6 +420,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• limit
│ count: 2
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/prepare
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ rows decoded from KV: 1 (8 B, 2 KVs, 1 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• scan
nodes: <hidden>
Expand All @@ -145,6 +148,9 @@ vectorized: <hidden>
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• scan
nodes: <hidden>
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/vectorize_local
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ rows decoded from KV: 2,001 (16 KiB, 4,002 KVs, 2,001 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• scan
nodes: <hidden>
Expand Down Expand Up @@ -75,6 +78,9 @@ rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• lookup join (streamer)
│ nodes: <hidden>
Expand Down Expand Up @@ -156,6 +162,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: Serializable
priority: normal
quality of service: regular
·
• merge join
│ nodes: <hidden>
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/opt/exec/explain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
# Pin the dependencies used in auto-generated code.
deps = [
"//pkg/geo/geoindex",
"//pkg/kv/kvserver/concurrency/isolation",
"//pkg/roachpb",
"//pkg/sql/appstatspb",
"//pkg/sql/catalog/colinfo",
Expand All @@ -31,6 +32,7 @@ go_library(
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/sem/catid",
"//pkg/sql/sem/tree",
"//pkg/sql/sessiondatapb",
"//pkg/sql/types",
"//pkg/util",
"//pkg/util/errorutil",
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/opt/exec/explain/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import (
"strings"
"time"

"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/isolation"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -404,6 +407,16 @@ func (ob *OutputBuilder) AddRegionsStats(regions []string) {
)
}

// AddTxnInfo adds top-level fields for information about the query's
// transaction.
func (ob *OutputBuilder) AddTxnInfo(
txnIsoLevel isolation.Level, txnPriority roachpb.UserPriority, txnQoSLevel sessiondatapb.QoSLevel,
) {
ob.AddTopLevelField("isolation level", txnIsoLevel.String())
ob.AddTopLevelField("priority", txnPriority.String())
ob.AddTopLevelField("quality of service", txnQoSLevel.String())
}

// AddWarning adds the provided string to the list of warnings. Warnings will be
// appended to the end of the output produced by BuildStringRows / BuildString.
func (ob *OutputBuilder) AddWarning(warning string) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/sql/testdata/explain_tree
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• scan
columns: (oid int)
Expand Down Expand Up @@ -45,6 +48,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• scan
columns: (cid int, date date, value decimal)
Expand Down Expand Up @@ -77,6 +83,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• project
│ columns: (cid int, sum decimal)
Expand Down Expand Up @@ -156,6 +165,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• scan
columns: (value decimal)
Expand Down Expand Up @@ -188,6 +200,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• project
│ columns: (cid int, date date, value decimal)
Expand Down Expand Up @@ -277,6 +292,9 @@ distribution: local
vectorized: false
maximum memory usage: 0 B
network usage: 0 B (0 messages)
isolation level: Serializable
priority: normal
quality of service: regular

• root
│ columns: (movie_id int, title string, name string)
Expand Down

0 comments on commit 43818e8

Please sign in to comment.