Skip to content

Commit 0a3a203

Browse files
craig[bot]kyle-a-wong
andcommitted
Merge #153289
153289: stmtdiagnostics: write transaction diagnostics to new system tables r=kyle-a-wong a=kyle-a-wong Updates `TxnRegistry.InsertTxnRequest` and `TxnRegistry.InsertTxnDiagnostic` to persist data to the new `system.transaction_diagnostics_requests` and `system.transaction_diagnostics` tables. `InsertTxnDiagnostic` will also update the `system.statement_diagnostics` table to update the `transaction_diagnostics_id` column and update the `system.transaction_diagnostics_requests` table to mark the request as complete. It will also insert its own bundle into `system.statement_bundle_chunks`. For now, this bundle will only contain trace recordings, but in the future it can be updated to have any data related to the transaction. Part-of: [CRDB-53546](https://cockroachlabs.atlassian.net/browse/CRDB-53546) Epic: [CRDB-53541](https://cockroachlabs.atlassian.net/browse/CRDB-53541) Release note: None ---- Note: this is a stacked PR, only the last commit needs to be reviewed Co-authored-by: Kyle Wong <[email protected]>
2 parents 1e2b120 + 4e397fa commit 0a3a203

File tree

7 files changed

+553
-144
lines changed

7 files changed

+553
-144
lines changed

pkg/sql/stmtdiagnostics/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"//pkg/sql/isql",
1616
"//pkg/sql/sem/tree",
1717
"//pkg/sql/sessiondata",
18+
"//pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil",
1819
"//pkg/sql/types",
1920
"//pkg/util/intsets",
2021
"//pkg/util/log",
@@ -32,6 +33,7 @@ go_test(
3233
"main_test.go",
3334
"statement_diagnostics_helpers_test.go",
3435
"statement_diagnostics_test.go",
36+
"txn_diagnostics_helpers_test.go",
3537
"txn_diagnostics_test.go",
3638
],
3739
embed = [":stmtdiagnostics"],
@@ -56,12 +58,14 @@ go_test(
5658
"//pkg/testutils/skip",
5759
"//pkg/testutils/sqlutils",
5860
"//pkg/testutils/testcluster",
61+
"//pkg/util/encoding",
5962
"//pkg/util/leaktest",
6063
"//pkg/util/log",
6164
"//pkg/util/syncutil",
6265
"//pkg/util/timeutil",
6366
"//pkg/util/uuid",
6467
"@com_github_cockroachdb_errors//:errors",
68+
"@com_github_lib_pq//:pq",
6569
"@com_github_stretchr_testify//require",
6670
],
6771
)

pkg/sql/stmtdiagnostics/statement_diagnostics.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ func (r *Registry) InsertStatementDiagnostics(
580580
var diagID CollectedInstanceID
581581
err := r.db.Txn(ctx, func(ctx context.Context, txn isql.Txn) error {
582582
txn.KV().SetDebugName("stmt-diag-insert-bundle")
583-
id, err := r.innerInsertStatementDiagnostics(ctx, NewStmtDiagnostic(requestID, req, stmtFingerprint, stmt, bundle, collectionErr), txn)
583+
id, err := r.innerInsertStatementDiagnostics(ctx, NewStmtDiagnostic(requestID, req, stmtFingerprint, stmt, bundle, collectionErr), txn, CollectedInstanceID(0))
584584
if err != nil {
585585
return err
586586
}
@@ -627,7 +627,7 @@ func (r *Registry) insertBundleChunks(
627627
}
628628

629629
func (r *Registry) innerInsertStatementDiagnostics(
630-
ctx context.Context, diagnostic StmtDiagnostic, txn isql.Txn,
630+
ctx context.Context, diagnostic StmtDiagnostic, txn isql.Txn, txnDiagnosticId CollectedInstanceID,
631631
) (CollectedInstanceID, error) {
632632
var diagID CollectedInstanceID
633633
if diagnostic.requestID != 0 {
@@ -663,14 +663,22 @@ func (r *Registry) innerInsertStatementDiagnostics(
663663

664664
collectionTime := timeutil.Now()
665665

666+
insertCols := "statement_fingerprint, statement, collected_at, bundle_chunks, error"
667+
insertVals := "$1, $2, $3, $4, $5"
668+
vals := []interface{}{diagnostic.stmtFingerprint, diagnostic.stmt, collectionTime, bundleChunksVal, errorVal}
669+
if txnDiagnosticId != 0 {
670+
insertCols += ", transaction_diagnostics_id"
671+
insertVals += ", $6"
672+
vals = append(vals, txnDiagnosticId)
673+
}
666674
// Insert the collection metadata into system.statement_diagnostics.
667675
row, err := txn.QueryRowEx(
668676
ctx, "stmt-diag-insert", txn.KV(),
669677
sessiondata.NodeUserSessionDataOverride,
670678
"INSERT INTO system.statement_diagnostics "+
671-
"(statement_fingerprint, statement, collected_at, bundle_chunks, error) "+
672-
"VALUES ($1, $2, $3, $4, $5) RETURNING id",
673-
diagnostic.stmtFingerprint, diagnostic.stmt, collectionTime, bundleChunksVal, errorVal,
679+
"("+insertCols+") "+
680+
"VALUES ("+insertVals+") RETURNING id",
681+
vals...,
674682
)
675683
if err != nil {
676684
return diagID, err

0 commit comments

Comments
 (0)