diff --git a/pkg/sql/conn_executor_exec.go b/pkg/sql/conn_executor_exec.go index 2e0ad5ee9993..7ff5ba50e3cd 100644 --- a/pkg/sql/conn_executor_exec.go +++ b/pkg/sql/conn_executor_exec.go @@ -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 @@ -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 { diff --git a/pkg/sql/instrumentation.go b/pkg/sql/instrumentation.go index edfc84627b36..70e14bc98ef0 100644 --- a/pkg/sql/instrumentation.go +++ b/pkg/sql/instrumentation.go @@ -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" @@ -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" @@ -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() -- @@ -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() @@ -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)) } diff --git a/pkg/sql/opt/exec/execbuilder/testdata/explain b/pkg/sql/opt/exec/execbuilder/testdata/explain index 86c22ca7fe65..92fcafbe9c6a 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/explain +++ b/pkg/sql/opt/exec/execbuilder/testdata/explain @@ -2227,6 +2227,9 @@ vectorized: maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • values nodes: diff --git a/pkg/sql/opt/exec/execbuilder/testdata/explain_analyze b/pkg/sql/opt/exec/execbuilder/testdata/explain_analyze index f73fa8e4fa16..adfc76a428c9 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/explain_analyze +++ b/pkg/sql/opt/exec/execbuilder/testdata/explain_analyze @@ -13,6 +13,9 @@ vectorized: maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • scan nodes: @@ -42,6 +45,9 @@ rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • scan nodes: @@ -58,6 +64,50 @@ regions: 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: +vectorized: +rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls) +maximum memory usage: +network usage: +regions: +isolation level: ReadCommitted +priority: low +quality of service: background +· +• scan + nodes: + regions: + 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); @@ -73,6 +123,9 @@ rows decoded from KV: 7 (56 B, 14 KVs, 7 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • hash join (inner) │ columns: (k, v, a, b) diff --git a/pkg/sql/opt/exec/execbuilder/testdata/inverted_index_geospatial b/pkg/sql/opt/exec/execbuilder/testdata/inverted_index_geospatial index e66e75585f0f..3c9cd45553c0 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/inverted_index_geospatial +++ b/pkg/sql/opt/exec/execbuilder/testdata/inverted_index_geospatial @@ -30,6 +30,9 @@ rows decoded from KV: 6 (48 B, 12 KVs, 6 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • sort │ nodes: @@ -119,6 +122,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • sort │ nodes: @@ -192,6 +198,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • sort │ nodes: diff --git a/pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit b/pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit index 772feb554c88..f30fc01be451 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit +++ b/pkg/sql/opt/exec/execbuilder/testdata/lookup_join_limit @@ -84,6 +84,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • limit │ count: 1 @@ -165,6 +168,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • limit │ count: 1 @@ -319,6 +325,9 @@ rows decoded from KV: 2 (16 B, 4 KVs, 2 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • limit │ count: 1 @@ -411,6 +420,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • limit │ count: 2 diff --git a/pkg/sql/opt/exec/execbuilder/testdata/prepare b/pkg/sql/opt/exec/execbuilder/testdata/prepare index 43cf4b877698..1c6a3bd20a66 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/prepare +++ b/pkg/sql/opt/exec/execbuilder/testdata/prepare @@ -119,6 +119,9 @@ rows decoded from KV: 1 (8 B, 2 KVs, 1 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • scan nodes: @@ -145,6 +148,9 @@ vectorized: maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • scan nodes: diff --git a/pkg/sql/opt/exec/execbuilder/testdata/vectorize_local b/pkg/sql/opt/exec/execbuilder/testdata/vectorize_local index 2b3627e1d8e3..279a3adc5f2c 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/vectorize_local +++ b/pkg/sql/opt/exec/execbuilder/testdata/vectorize_local @@ -46,6 +46,9 @@ rows decoded from KV: 2,001 (16 KiB, 4,002 KVs, 2,001 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • scan nodes: @@ -75,6 +78,9 @@ rows decoded from KV: 3 (24 B, 6 KVs, 3 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • lookup join (streamer) │ nodes: @@ -156,6 +162,9 @@ rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls) maximum memory usage: network usage: regions: +isolation level: Serializable +priority: normal +quality of service: regular · • merge join │ nodes: diff --git a/pkg/sql/opt/exec/explain/BUILD.bazel b/pkg/sql/opt/exec/explain/BUILD.bazel index 68f906c23879..809b45748ea2 100644 --- a/pkg/sql/opt/exec/explain/BUILD.bazel +++ b/pkg/sql/opt/exec/explain/BUILD.bazel @@ -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", @@ -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", diff --git a/pkg/sql/opt/exec/explain/output.go b/pkg/sql/opt/exec/explain/output.go index 274bb44c8434..bf4f5a50907c 100644 --- a/pkg/sql/opt/exec/explain/output.go +++ b/pkg/sql/opt/exec/explain/output.go @@ -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" @@ -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) { diff --git a/pkg/sql/testdata/explain_tree b/pkg/sql/testdata/explain_tree index 5432733903f9..d0b5dd1ace96 100644 --- a/pkg/sql/testdata/explain_tree +++ b/pkg/sql/testdata/explain_tree @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)