Skip to content

Commit

Permalink
sql: add implicit_txn column to insights execution
Browse files Browse the repository at this point in the history
This commit adds a new column `implicit_txn` (boolean) to:
- `crdb_internal.cluster_execution_insights`
- `crdb_internal.node_execution_insights`

Part Of cockroachdb#87750

Release note (sql change): Adds a new column `implicit_txn`
(boolean) to `crdb_internal.cluster_execution_insights` and
`crdb_internal.node_execution_insights`
  • Loading branch information
maryliag committed Oct 18, 2022
1 parent c68e94a commit 7196857
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -6531,6 +6532,7 @@ func populateExecutionInsights(
execNodeIDs,
contentionTime,
indexRecommendations,
tree.MakeDBool(tree.DBool(insight.Statement.ImplicitTxn)),
))
}
return
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/create_statements
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sqlstats/insights/insights.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 36 additions & 10 deletions pkg/sql/sqlstats/insights/integration/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (s *Container) RecordStatement(
Nodes: value.Nodes,
Contention: contention,
IndexRecommendations: value.IndexRecommendations,
ImplicitTxn: key.ImplicitTxn,
})

return stats.ID, nil
Expand Down

0 comments on commit 7196857

Please sign in to comment.