Skip to content

Commit d9726d2

Browse files
committed
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
1 parent 2e90394 commit d9726d2

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

pkg/sql/exec_log.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
3131
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
3232
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
33+
"github.com/cockroachdb/errors"
3334
"github.com/cockroachdb/redact"
3435
)
3536

@@ -167,6 +168,8 @@ func (p *planner) maybeLogStatement(
167168
p.maybeLogStatementInternal(ctx, execType, isCopy, numRetries, txnCounter, rows, err, queryReceived, hasAdminRoleCache, telemetryLoggingMetrics, stmtFingerprintID, queryStats)
168169
}
169170

171+
var errTxnIsNotOpen = errors.New("txn is already committed or rolled back")
172+
170173
func (p *planner) maybeLogStatementInternal(
171174
ctx context.Context,
172175
execType executorType,
@@ -323,13 +326,19 @@ func (p *planner) maybeLogStatementInternal(
323326
mode = "rw"
324327
}
325328
tableName := ""
329+
var tn *tree.TableName
326330
// We only have a valid *table* name if the object being
327331
// audited is table-like (includes view, sequence etc). For
328332
// now, this is sufficient because the auditing feature can
329333
// only audit tables. If/when the mechanisms are extended to
330334
// audit databases and schema, we need more logic here to
331335
// extract a name to include in the logging events.
332-
tn, err := p.getQualifiedTableName(ctx, ev.desc)
336+
if p.txn != nil && p.txn.IsOpen() {
337+
// Only open txn accepts further commands.
338+
tn, err = p.getQualifiedTableName(ctx, ev.desc)
339+
} else {
340+
err = errTxnIsNotOpen
341+
}
333342
if err != nil {
334343
log.Warningf(ctx, "name for audited table ID %d not found: %v", ev.desc.GetID(), err)
335344
} else {

0 commit comments

Comments
 (0)