diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index ac210d38b72d..5c59e634b90f 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -6417,7 +6417,8 @@ CREATE TABLE crdb_internal.%s ( last_retry_reason STRING, exec_node_ids INT[] NOT NULL, contention INTERVAL, - index_recommendations STRING[] NOT NULL + index_recommendations STRING[] NOT NULL, + implicit_txn BOOL NOT NULL )` var crdbInternalClusterExecutionInsightsTable = virtualSchemaTable{ @@ -6531,6 +6532,7 @@ func populateExecutionInsights( execNodeIDs, contentionTime, indexRecommendations, + tree.MakeDBool(tree.DBool(insight.Statement.ImplicitTxn)), )) } return diff --git a/pkg/sql/logictest/testdata/logic_test/create_statements b/pkg/sql/logictest/testdata/logic_test/create_statements index 4c22af8aaf44..11403d20ac44 100644 --- a/pkg/sql/logictest/testdata/logic_test/create_statements +++ b/pkg/sql/logictest/testdata/logic_test/create_statements @@ -271,7 +271,8 @@ CREATE TABLE crdb_internal.cluster_execution_insights ( last_retry_reason STRING NULL, exec_node_ids INT8[] NOT NULL, contention INTERVAL NULL, - index_recommendations STRING[] NOT NULL + index_recommendations STRING[] NOT NULL, + implicit_txn BOOL NOT NULL ) CREATE TABLE crdb_internal.cluster_execution_insights ( session_id STRING NOT NULL, txn_id UUID NOT NULL, @@ -296,7 +297,8 @@ CREATE TABLE crdb_internal.cluster_execution_insights ( last_retry_reason STRING NULL, exec_node_ids INT8[] NOT NULL, contention INTERVAL NULL, - index_recommendations STRING[] NOT NULL + index_recommendations STRING[] NOT NULL, + implicit_txn BOOL NOT NULL ) {} {} CREATE TABLE crdb_internal.cluster_inflight_traces ( trace_id INT8 NOT NULL, @@ -993,7 +995,8 @@ CREATE TABLE crdb_internal.node_execution_insights ( last_retry_reason STRING NULL, exec_node_ids INT8[] NOT NULL, contention INTERVAL NULL, - index_recommendations STRING[] NOT NULL + index_recommendations STRING[] NOT NULL, + implicit_txn BOOL NOT NULL ) CREATE TABLE crdb_internal.node_execution_insights ( session_id STRING NOT NULL, txn_id UUID NOT NULL, @@ -1018,7 +1021,8 @@ CREATE TABLE crdb_internal.node_execution_insights ( last_retry_reason STRING NULL, exec_node_ids INT8[] NOT NULL, contention INTERVAL NULL, - index_recommendations STRING[] NOT NULL + index_recommendations STRING[] NOT NULL, + implicit_txn BOOL NOT NULL ) {} {} CREATE TABLE crdb_internal.node_inflight_trace_spans ( trace_id INT8 NOT NULL, diff --git a/pkg/sql/sqlstats/insights/insights.proto b/pkg/sql/sqlstats/insights/insights.proto index 3e4156f7e032..4b130e60dc42 100644 --- a/pkg/sql/sqlstats/insights/insights.proto +++ b/pkg/sql/sqlstats/insights/insights.proto @@ -99,7 +99,7 @@ message Statement { repeated int64 nodes = 17; google.protobuf.Duration contention = 18 [(gogoproto.stdduration) = true]; repeated string index_recommendations = 19; - + bool implicit_txn = 20; } message Insight { diff --git a/pkg/sql/sqlstats/insights/integration/insights_test.go b/pkg/sql/sqlstats/insights/integration/insights_test.go index 2c4aa4da337d..e18e92141831 100644 --- a/pkg/sql/sqlstats/insights/integration/insights_test.go +++ b/pkg/sql/sqlstats/insights/integration/insights_test.go @@ -72,7 +72,7 @@ func TestInsightsIntegration(t *testing.T) { require.NoError(t, err) require.Equal(t, 0, count, "expect:0, actual:%d, queries:%s", count, queryText) - queryDelayInSeconds := 2 * latencyThreshold.Seconds() + queryDelayInSeconds := latencyThreshold.Seconds() // Execute a "long-running" statement, running longer than our latencyThreshold. _, err = conn.ExecContext(ctx, "SELECT pg_sleep($1)", queryDelayInSeconds) require.NoError(t, err) @@ -97,14 +97,16 @@ func TestInsightsIntegration(t *testing.T) { "status, "+ "start_time, "+ "end_time, "+ - "full_scan "+ + "full_scan, "+ + "implicit_txn "+ "FROM crdb_internal.node_execution_insights where "+ "query = $1 and app_name = $2 ", "SELECT pg_sleep($1)", appName) var query, status string var startInsights, endInsights time.Time var fullScan bool - err = row.Scan(&query, &status, &startInsights, &endInsights, &fullScan) + var implicitTxn bool + err = row.Scan(&query, &status, &startInsights, &endInsights, &fullScan, &implicitTxn) if err != nil { return err @@ -147,11 +149,29 @@ func TestInsightsPriorityIntegration(t *testing.T) { _, err = conn.Exec("CREATE TABLE t (id string, s string);") require.NoError(t, err) - queryDelayInSeconds := 2 * latencyThreshold.Seconds() - // Execute a "long-running" statement, running longer than our latencyThreshold. - _, err = conn.ExecContext(ctx, "SELECT pg_sleep($1)", queryDelayInSeconds) + // Execute a "long-running" statement, running longer than our latencyThreshold (100ms). + _, err = conn.ExecContext(ctx, "SELECT pg_sleep(.11)") require.NoError(t, err) + testutils.SucceedsWithin(t, func() error { + row := conn.QueryRowContext(ctx, "SELECT "+ + "implicit_txn "+ + "FROM crdb_internal.node_execution_insights where "+ + "app_name = $1 and query = $2 ", appName, "SELECT pg_sleep(_)") + + var implicitTxn bool + err = row.Scan(&implicitTxn) + if err != nil { + return err + } + + if implicitTxn != true { + return fmt.Errorf("expected implicit_txn '%v', but was %v", true, implicitTxn) + } + + return nil + }, 2*time.Second) + var priorities = []struct { setPriorityQuery string query string @@ -205,23 +225,29 @@ func TestInsightsPriorityIntegration(t *testing.T) { testutils.SucceedsWithin(t, func() error { row := conn.QueryRowContext(ctx, "SELECT "+ "query, "+ - "priority "+ + "priority, "+ + "implicit_txn "+ "FROM crdb_internal.node_execution_insights where "+ "app_name = $1 and query = $2 ", appName, p.queryNoValues) var query, priority string - err = row.Scan(&query, &priority) + var implicitTxn bool + err = row.Scan(&query, &priority, &implicitTxn) if err != nil { return err } if query != p.queryNoValues { - return fmt.Errorf("expected '%s', but was %s", p.queryNoValues, query) + return fmt.Errorf("expected query '%s', but was %s", p.queryNoValues, query) } if priority != p.expectedPriorityValue { - return fmt.Errorf("expected '%s', but was %s", p.expectedPriorityValue, priority) + return fmt.Errorf("expected priority '%s', but was %s", p.expectedPriorityValue, priority) + } + + if implicitTxn != false { + return fmt.Errorf("expected implicit_txn '%v', but was %v", false, implicitTxn) } return nil diff --git a/pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go b/pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go index 0ec4c82831c3..27642df2c359 100644 --- a/pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go +++ b/pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go @@ -201,6 +201,7 @@ func (s *Container) RecordStatement( Nodes: value.Nodes, Contention: contention, IndexRecommendations: value.IndexRecommendations, + ImplicitTxn: key.ImplicitTxn, }) return stats.ID, nil