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 28, 2022
1 parent 775d97c commit e2cc21c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
4 changes: 3 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6371,7 +6371,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 @@ -6485,6 +6486,7 @@ func populateExecutionInsights(
execNodeIDs,
contentionTime,
indexRecommendations,
tree.MakeDBool(tree.DBool(insight.Transaction.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 @@ -991,7 +993,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 @@ -1016,7 +1019,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 @@ -68,6 +68,7 @@ message Transaction {
(gogoproto.customtype) = "github.com/cockroachdb/cockroach/pkg/roachpb.TransactionFingerprintID",
(gogoproto.nullable) = false];
string user_priority = 3;
bool implicit_txn = 4;
}

message Statement {
Expand Down Expand Up @@ -99,7 +100,6 @@ message Statement {
repeated int64 nodes = 17;
google.protobuf.Duration contention = 18 [(gogoproto.stdduration) = true];
repeated string index_recommendations = 19;

}

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
3 changes: 2 additions & 1 deletion pkg/sql/sqlstats/ssmemstorage/ss_mem_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ func (s *Container) RecordTransaction(
s.insights.ObserveTransaction(value.SessionID, &insights.Transaction{
ID: value.TransactionID,
FingerprintID: key,
UserPriority: value.Priority.String()})
UserPriority: value.Priority.String(),
ImplicitTxn: value.ImplicitTxn})

return nil
}
Expand Down

0 comments on commit e2cc21c

Please sign in to comment.