Skip to content

Commit

Permalink
sql: record error code for failed executions to execution insights
Browse files Browse the repository at this point in the history
tables

Part of: cockroachdb#87785.

Previously, the error codes for failed executions were not written to
the execution insights tables. This commit adds an `error_code` column
to the `crdb_internal.[cluster|node]_execution_insights` virtual tables
to keep track of the error code for a failed stmt execution. This commit
also adds a also adds a `last_error_code` column to to the
`crdb_internal.[cluster|node_txn_execution_insights` virtual tables to
keep track of the error code for the last failed statement for that
transaction.

Release note (sql change): Adds `error_code` column to the
`crdb_internal.[cluster|node]_execution_insights` virtual tables, which
contains the error code for a failed execution. Adds `last_error_code`
column to the `crdb_internal.[cluster|node]_txn_execution_insights`
virtual tables, which is the error code of the last failed statement in
that transaction.
  • Loading branch information
gtr committed Feb 13, 2023
1 parent 2d5449d commit 03b9c07
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 18 deletions.
18 changes: 14 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,25 @@ SELECT * FROM crdb_internal.node_inflight_trace_spans WHERE span_id < 0
----
trace_id parent_span_id span_id goroutine_id finished start_time duration operation

query TTTBTTTTTIITITTTTTI colnames
query TTTBTTTTTIITITTTTTTTTTTTTTIT colnames
SELECT * FROM crdb_internal.cluster_execution_insights WHERE query = ''
----
session_id txn_id txn_fingerprint_id stmt_id stmt_fingerprint_id problem causes query status start_time end_time full_scan user_name app_name database_name plan_gist rows_read rows_written priority retries last_retry_reason exec_node_ids contention contention_events index_recommendations implicit_txn cpu_sql_nanos error_code

query TTTBTTTTTIITITTTTTTTTTTTTTIT colnames
SELECT * FROM crdb_internal.node_execution_insights WHERE query = ''
----
session_id txn_id txn_fingerprint_id stmt_id stmt_fingerprint_id problem causes query status start_time end_time full_scan user_name app_name database_name plan_gist rows_read rows_written priority retries last_retry_reason exec_node_ids contention contention_events index_recommendations implicit_txn cpu_sql_nanos error_code

query TTTBTTTTTIITITTTTTIT colnames
SELECT * FROM crdb_internal.cluster_txn_execution_insights WHERE query = ''
----
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos last_error_code

query TTTBTTTTTIITITTTTTI colnames
query TTTBTTTTTIITITTTTTIT colnames
SELECT * FROM crdb_internal.node_txn_execution_insights WHERE query = ''
----
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos last_error_code

query ITTI
SELECT range_id, start_pretty, end_pretty, lease_holder FROM crdb_internal.ranges
Expand Down
15 changes: 13 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7042,7 +7042,8 @@ CREATE TABLE crdb_internal.%s (
problems STRING[] NOT NULL,
causes STRING[] NOT NULL,
stmt_execution_ids STRING[] NOT NULL,
cpu_sql_nanos INT8
cpu_sql_nanos INT8,
last_error_code STRING
)`

var crdbInternalClusterTxnExecutionInsightsTable = virtualSchemaTable{
Expand Down Expand Up @@ -7151,6 +7152,13 @@ func populateTxnExecutionInsights(
}
}

errorCode := tree.DNull
for _, stmt := range insight.Statements {
if stmt.ErrorCode != "" {
errorCode = tree.NewDString(stmt.ErrorCode)
}
}

err = errors.CombineErrors(err, addRow(
tree.NewDUuid(tree.DUuid{UUID: insight.Transaction.ID}),
tree.NewDBytes(tree.DBytes(sqlstatsutil.EncodeUint64ToBytes(uint64(insight.Transaction.FingerprintID)))),
Expand All @@ -7171,6 +7179,7 @@ func populateTxnExecutionInsights(
causes,
stmtIDs,
tree.NewDInt(tree.DInt(insight.Transaction.CPUSQLNanos)),
errorCode,
))

if err != nil {
Expand Down Expand Up @@ -7210,7 +7219,8 @@ CREATE TABLE crdb_internal.%s (
contention_events JSONB,
index_recommendations STRING[] NOT NULL,
implicit_txn BOOL NOT NULL,
cpu_sql_nanos INT8
cpu_sql_nanos INT8,
error_code STRING
)`

var crdbInternalClusterExecutionInsightsTable = virtualSchemaTable{
Expand Down Expand Up @@ -7341,6 +7351,7 @@ func populateStmtInsights(
indexRecommendations,
tree.MakeDBool(tree.DBool(insight.Transaction.ImplicitTxn)),
tree.NewDInt(tree.DInt(s.CPUSQLNanos)),
tree.NewDString(s.ErrorCode),
))
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,16 @@ SELECT * FROM crdb_internal.ranges_no_leases WHERE range_id < 0
----
range_id start_key start_pretty end_key end_pretty replicas replica_localities voting_replicas non_voting_replicas learner_replicas split_enforced_until

query TTTBTTTTTIITITTTTTI colnames
query TTTBTTTTTIITITTTTTIT colnames
SELECT * FROM crdb_internal.cluster_txn_execution_insights WHERE query = ''
----
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos last_error_code


query TTTBTTTTTIITITTTTTI colnames
query TTTBTTTTTIITITTTTTIT colnames
SELECT * FROM crdb_internal.node_txn_execution_insights WHERE query = ''
----
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos
txn_id txn_fingerprint_id query implicit_txn session_id start_time end_time user_name app_name rows_read rows_written priority retries last_retry_reason contention problems causes stmt_execution_ids cpu_sql_nanos last_error_code


statement ok
Expand Down
Loading

0 comments on commit 03b9c07

Please sign in to comment.