Skip to content

Commit

Permalink
sql: do not print stack trace when logging if txn is not open
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yuzefovich committed Nov 8, 2022
1 parent 2e90394 commit d9726d2
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/sql/exec_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d9726d2

Please sign in to comment.