Skip to content

Commit 73cc422

Browse files
committed
In query-behavior-troubleshooting.md, backported section for Log traces for transactions.
1 parent fd7fa65 commit 73cc422

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/current/v25.2/query-behavior-troubleshooting.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You can identify high-latency SQL statements on the [**Insights**]({% link {{ pa
2929

3030
You can also enable the [slow query log]({% link {{ page.version.version }}/logging-use-cases.md %}#sql_perf) to log all queries whose latency exceeds a configured threshold, as well as queries that perform a full table or index scan.
3131

32-
You can collect richer diagnostics of a high-latency statement by creating a [diagnostics bundle]({% link {{ page.version.version }}/ui-statements-page.md %}#diagnostics) when a statement fingerprint exceeds a certain latency.
32+
You can collect richer diagnostics of a high-latency statement by creating a [diagnostics bundle]({% link {{ page.version.version }}/ui-statements-page.md %}#diagnostics) when a statement fingerprint exceeds a certain latency. To identify slow transactions in an active workload, you can [selectively log traces of transactions](#log-traces-for-transactions) that exceed a configured latency threshold.
3333

3434
{{site.data.alerts.callout_info}}
3535
{% include {{ page.version.version }}/prod-deployment/resolution-untuned-query.md %}
@@ -109,6 +109,43 @@ docker run -d --name jaeger \
109109
-p 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest
110110
~~~
111111

112+
### Log traces for transactions
113+
114+
CockroachDB allows you to trace [transactions]({% link {{ page.version.version }}/transactions.md %}) to help troubleshoot performance issues. [Tracing]({% link {{ page.version.version }}/show-trace.md %}#trace-description) is controlled through the following cluster settings that govern when a transaction trace is captured and emitted.
115+
116+
#### Trace sampling and emission
117+
118+
To enable tracing for a subset of transactions and emit relevant traces to the [`SQL_EXEC` logging channel]({% link {{ page.version.version }}/logging.md %}#sql_exec), configure the following cluster settings:
119+
120+
- {% include_cached new-in.html version="v25.2.8" %}[`sql.trace.txn.sample_rate`]({% link {{ page.version.version }}/cluster-settings.md %}#setting-sql-trace-txn-sample-rate): Specifies the probability (between `0.0` and `1.0`) that a given transaction will have tracing enabled. A value of `0.01` means that approximately 1% of transactions are traced. The default is `1`, which means 100% of transactions are sampled.
121+
- [`sql.trace.txn.enable_threshold`]({% link {{ page.version.version }}/cluster-settings.md %}#setting-sql-trace-txn-enable-threshold): Specifies a duration threshold. A trace is emitted only if a sampled transaction's execution time exceeds this value. When set to `0` (default), tracing is disabled regardless of whether the value of `sql.trace.txn.sample_rate` is greater than `0`.
122+
123+
To emit a trace to the logs, the following conditions must be met:
124+
125+
1. The transaction is selected based on the sampling probability.
126+
1. Its execution duration exceeds the configured threshold.
127+
128+
This approach minimizes overhead by tracing a fraction of the workload and emitting traces only for potentially relevant transactions.
129+
130+
#### Trace configuration example
131+
132+
To configure the trace sampling probability and duration, set the following cluster settings:
133+
134+
{% include_cached copy-clipboard.html %}
135+
~~~ sql
136+
-- Enable trace sampling at a rate of 1%
137+
SET CLUSTER SETTING sql.trace.txn.sample_rate = 0.01;
138+
139+
-- Emit traces for sampled transactions that run longer than 1s
140+
SET CLUSTER SETTING sql.trace.txn.enable_threshold = '1s';
141+
~~~
142+
143+
With this configuration, approximately 1% of transactions are traced, and only those running longer than 1s will have their traces written to the logs. In the [`SQL_EXEC` log]({% link {{ page.version.version }}/logging.md %}#sql_exec), a line similar to the following precedes the trace:
144+
145+
~~~
146+
SQL txn took 2.004362083s, exceeding threshold of 1s:
147+
~~~
148+
112149
<a id="query-is-always-slow"></a>
113150

114151
### Queries are always slow

0 commit comments

Comments
 (0)