From d9726d2ea3661b5dda1fd7ccd1a5ba6e05d8c8fc Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Wed, 2 Nov 2022 13:53:11 -0700 Subject: [PATCH] sql: do not print stack trace when logging if txn is not open After executing each statement, that statement might be logged. If there were any audit events, then we attempt to resolve the table names for which the audit events have occurred. To do the resolution we're using the current txn. Previously, if that txn has been aborted or committed, it would result in a scary-looking stack trace added to the log, and this commit fixes it. Release note: None --- pkg/sql/exec_log.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/sql/exec_log.go b/pkg/sql/exec_log.go index 4ce38aa6d11a..dfba246c1712 100644 --- a/pkg/sql/exec_log.go +++ b/pkg/sql/exec_log.go @@ -30,6 +30,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/log/eventpb" "github.com/cockroachdb/cockroach/pkg/util/log/logpb" "github.com/cockroachdb/cockroach/pkg/util/timeutil" + "github.com/cockroachdb/errors" "github.com/cockroachdb/redact" ) @@ -167,6 +168,8 @@ func (p *planner) maybeLogStatement( p.maybeLogStatementInternal(ctx, execType, isCopy, numRetries, txnCounter, rows, err, queryReceived, hasAdminRoleCache, telemetryLoggingMetrics, stmtFingerprintID, queryStats) } +var errTxnIsNotOpen = errors.New("txn is already committed or rolled back") + func (p *planner) maybeLogStatementInternal( ctx context.Context, execType executorType, @@ -323,13 +326,19 @@ func (p *planner) maybeLogStatementInternal( mode = "rw" } tableName := "" + var tn *tree.TableName // We only have a valid *table* name if the object being // audited is table-like (includes view, sequence etc). For // now, this is sufficient because the auditing feature can // only audit tables. If/when the mechanisms are extended to // audit databases and schema, we need more logic here to // extract a name to include in the logging events. - tn, err := p.getQualifiedTableName(ctx, ev.desc) + if p.txn != nil && p.txn.IsOpen() { + // Only open txn accepts further commands. + tn, err = p.getQualifiedTableName(ctx, ev.desc) + } else { + err = errTxnIsNotOpen + } if err != nil { log.Warningf(ctx, "name for audited table ID %d not found: %v", ev.desc.GetID(), err) } else {