Skip to content

Commit 223617d

Browse files
craig[bot]yuzefovich
andcommitted
Merge #149503
149503: sql: include txn retry info into SHOW QUERIES r=yuzefovich a=yuzefovich This commit attempts to make txn retry information more visible. We already expose the number of times the txn is retried via `num_retries` and `num_auto_retries` columns of `{cluster,node}_transactions` virtual tables. However, at least for me personally, SHOW QUERIES is often the first entry point to understand what's happening on the cluster right now, and I don't always remember that there might retries going on when a given query has been running for a long time. To make that jump quicker we now include `num_txn_retries` and `num_txn_auto_retries` into the output of SHOW QUERIES (as well as the virtual table that it's powered by). Note that this might be a source of confusion in multi-stmt transactions because for each active query we'll print the number of txn retries regardless of which stmt encountered the retry. Still, it seems better than having to remember to join against the transactions virtual table on the session ID, plus we avoid possible fanout since we already have the necessary information in protos when populating the active queries. Also note that I believe we don't need to do anything special for the mixed version state since the virtual table scans prohibits the query distribution, meaning that if new columns are requested for SHOW QUERIES, then we're on a new binary and it knows how to populate them (even if the proto response came from the older node). Fixes: #148842. Release note (sql change): New columns `num_txn_retries` and `num_txn_auto_retries` are included into `crdb_internal.{cluster,node}_queries` virtual tables as well as output of SHOW QUERIES. These columns, when not NULL, have the same information as `num_retries` and `num_auto_retries` columns of `crdb_internal.{cluster,node}_transactions` virtual tables for the same transaction in which the active query is executed. Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents 34ce11a + d4bc91c commit 223617d

File tree

5 files changed

+42
-35
lines changed

5 files changed

+42
-35
lines changed

pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ SELECT * FROM crdb_internal.session_variables WHERE variable = ''
142142
----
143143
variable value hidden
144144

145-
query TTITTTTTTBTBTTT colnames
145+
query TTITTTTTTBTBTTTII colnames
146146
SELECT * FROM crdb_internal.node_queries WHERE node_id < 0
147147
----
148-
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level
148+
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level num_txn_retries num_txn_auto_retries
149149

150-
query TTITTTTTTBTBTTT colnames
150+
query TTITTTTTTBTBTTTII colnames
151151
SELECT * FROM crdb_internal.cluster_queries WHERE node_id < 0
152152
----
153-
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level
153+
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level num_txn_retries num_txn_auto_retries
154154

155155
query TITTTTIIITTTT colnames
156156
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0

pkg/cli/zip_table_registry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ var zipInternalTablesPerCluster = DebugZipTableRegistry{
198198
"phase",
199199
"full_scan",
200200
"crdb_internal.hide_sql_constants(query) as query",
201+
"num_txn_retries",
202+
"num_txn_auto_retries",
201203
},
202204
},
203205
"crdb_internal.cluster_sessions": {
@@ -921,6 +923,8 @@ var zipInternalTablesPerNode = DebugZipTableRegistry{
921923
"phase",
922924
"full_scan",
923925
"crdb_internal.hide_sql_constants(query) as query",
926+
"num_txn_retries",
927+
"num_txn_auto_retries",
924928
},
925929
},
926930
"crdb_internal.node_runtime_info": {

pkg/sql/crdb_internal.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,21 +2226,23 @@ func populateTransactionsTable(
22262226

22272227
const queriesSchemaPattern = `
22282228
CREATE TABLE crdb_internal.%s (
2229-
query_id STRING, -- the cluster-unique ID of the query
2230-
txn_id UUID, -- the unique ID of the query's transaction
2231-
node_id INT NOT NULL, -- the node on which the query is running
2232-
session_id STRING, -- the ID of the session
2233-
user_name STRING, -- the user running the query
2234-
start TIMESTAMPTZ, -- the start time of the query
2235-
query STRING, -- the SQL code of the query
2236-
client_address STRING, -- the address of the client that issued the query
2237-
application_name STRING, -- the name of the application as per SET application_name
2238-
distributed BOOL, -- whether the query is running distributed
2239-
phase STRING, -- the current execution phase
2240-
full_scan BOOL, -- whether the query contains a full table or index scan
2241-
plan_gist STRING, -- Compressed logical plan.
2242-
database STRING, -- the database the statement was executed on
2243-
isolation_level STRING -- the isolation level of the query's transaction
2229+
query_id STRING, -- the cluster-unique ID of the query
2230+
txn_id UUID, -- the unique ID of the query's transaction
2231+
node_id INT NOT NULL, -- the node on which the query is running
2232+
session_id STRING, -- the ID of the session
2233+
user_name STRING, -- the user running the query
2234+
start TIMESTAMPTZ, -- the start time of the query
2235+
query STRING, -- the SQL code of the query
2236+
client_address STRING, -- the address of the client that issued the query
2237+
application_name STRING, -- the name of the application as per SET application_name
2238+
distributed BOOL, -- whether the query is running distributed
2239+
phase STRING, -- the current execution phase
2240+
full_scan BOOL, -- whether the query contains a full table or index scan
2241+
plan_gist STRING, -- Compressed logical plan.
2242+
database STRING, -- the database the statement was executed on
2243+
isolation_level STRING, -- the isolation level of the query's transaction
2244+
num_txn_retries INT, -- number of txn retries
2245+
num_txn_auto_retries INT -- number of auto retries via SQL conn executor
22442246
)`
22452247

22462248
func (p *planner) makeSessionsRequest(
@@ -2358,6 +2360,11 @@ func populateQueriesTable(
23582360
continue
23592361
}
23602362
sessionID := getSessionID(session)
2363+
numTxnRetries, numTxnAutoRetries := tree.DNull, tree.DNull
2364+
if txn := session.ActiveTxn; txn != nil {
2365+
numTxnRetries = tree.NewDInt(tree.DInt(txn.NumRetries))
2366+
numTxnAutoRetries = tree.NewDInt(tree.DInt(txn.NumAutoRetries))
2367+
}
23612368
for _, query := range session.ActiveQueries {
23622369
isDistributedDatum := tree.DNull
23632370
isFullScanDatum := tree.DNull
@@ -2378,16 +2385,6 @@ func populateQueriesTable(
23782385
phase = fmt.Sprintf("%s (%.2f%%)", phase, query.Progress*100)
23792386
}
23802387

2381-
var txnID tree.Datum
2382-
// query.TxnID and query.TxnStart were only added in 20.1. In case this
2383-
// is a mixed cluster setting, report NULL if these values were not filled
2384-
// out by the remote session.
2385-
if query.ID == "" {
2386-
txnID = tree.DNull
2387-
} else {
2388-
txnID = tree.NewDUuid(tree.DUuid{UUID: query.TxnID})
2389-
}
2390-
23912388
ts, err := tree.MakeDTimestampTZ(query.Start, time.Microsecond)
23922389
if err != nil {
23932390
return err
@@ -2414,7 +2411,7 @@ func populateQueriesTable(
24142411

24152412
if err := addRow(
24162413
tree.NewDString(query.ID),
2417-
txnID,
2414+
tree.NewDUuid(tree.DUuid{UUID: query.TxnID}),
24182415
tree.NewDInt(tree.DInt(session.NodeID)),
24192416
sessionID,
24202417
tree.NewDString(session.Username),
@@ -2428,6 +2425,8 @@ func populateQueriesTable(
24282425
planGistDatum,
24292426
tree.NewDString(query.Database),
24302427
isolationLevelDatum,
2428+
numTxnRetries,
2429+
numTxnAutoRetries,
24312430
); err != nil {
24322431
return err
24332432
}
@@ -2455,6 +2454,8 @@ func populateQueriesTable(
24552454
tree.DNull, // plan_gist
24562455
tree.DNull, // database
24572456
tree.DNull, // isolation_level
2457+
tree.DNull, // num_txn_retries
2458+
tree.DNull, // num_txn_auto_retries
24582459
); err != nil {
24592460
return err
24602461
}

pkg/sql/delegate/show_queries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ SELECT
2626
distributed,
2727
full_scan,
2828
phase,
29-
isolation_level
29+
isolation_level,
30+
num_txn_retries,
31+
num_txn_auto_retries
3032
FROM crdb_internal.`
3133
table := `node_queries`
3234
if n.Cluster {

pkg/sql/logictest/testdata/logic_test/crdb_internal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ SELECT * FROM crdb_internal.session_variables WHERE variable = ''
276276
----
277277
variable value hidden
278278

279-
query TTITTTTTTBTBTTT colnames
279+
query TTITTTTTTBTBTTTII colnames
280280
SELECT * FROM crdb_internal.node_queries WHERE node_id < 0
281281
----
282-
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level
282+
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level num_txn_retries num_txn_auto_retries
283283

284-
query TTITTTTTTBTBTTT colnames
284+
query TTITTTTTTBTBTTTII colnames
285285
SELECT * FROM crdb_internal.cluster_queries WHERE node_id < 0
286286
----
287-
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level
287+
query_id txn_id node_id session_id user_name start query client_address application_name distributed phase full_scan plan_gist database isolation_level num_txn_retries num_txn_auto_retries
288288

289289
query TITTTTIIITTTT colnames
290290
SELECT * FROM crdb_internal.node_transactions WHERE node_id < 0

0 commit comments

Comments
 (0)