SHOW TRACE FOR SESSION returns details about how CockroachDB executed a statement or series of statements recorded during a session. These details include messages and timing information from all nodes involved in the execution, providing visibility into the actions taken by CockroachDB across all of its software layers.
You can use SHOW TRACE FOR SESSION to debug why a query is not performing as expected, to add more information to bug reports, or to generally learn more about how CockroachDB works.
A contains statement traces in plaintext, JSON, and format.
Usage overview
SHOW TRACE FOR SESSION returns statement traces for the most recently executed statements.
To start recording statement traces during a session, enable the tracing session variable via . To stop recording statement traces during a session, disable the tracing session variable via .
Recording statement traces during a session does not effect the logical execution of the statements. This means that errors encountered by statements during a recording are returned to clients. CockroachDB will individual statements (considered implicit transactions) and multi-statement transactions sent as a single batch when are encountered due to . Also, clients will receive retry errors required to handle . As a result, traces of all transaction retries will be captured during a recording.
Required privileges
ForSHOW TRACE FOR SESSION, no privileges are required.
Syntax
Parameters
| Parameter | Description |
|---|---|
COMPACT | If specified, fewer columns are returned in each trace. See Response for more details. |
KV | If specified, the returned messages are restricted to those describing requests to and responses from the underlying key-value , including per-result-row messages. For SHOW KV TRACE FOR SESSION, per-result-row messages are included only if the session was/is recording with SET tracing = kv;. |
Trace description
CockroachDB uses OpenTelemetry libraries for tracing, which also means that it can be easily integrated with OpenTelemetry-compatible . CockroachDB traces map to OpenTelemetry trace and span concepts as follows:| Concept | Description |
|---|---|
| trace | Information about the sub-operations performed as part of a high-level operation (a query or a transaction). This information is internally represented as a tree of “spans”, with a special “root span” representing a whole SQL transaction in the case of SHOW TRACE FOR SESSION. |
| span | A named, timed operation that describes a contiguous segment of work in a trace. Each span links to “child spans”, representing sub-operations; their children would be sub-sub-operations of the grandparent span, etc. Different spans can represent (sub-)operations that executed either sequentially or in parallel with respect to each other. (This possibly-parallel nature of execution is one of the important things that a trace is supposed to describe.) \The operations described by a trace may be distributed, that is, different spans may describe operations executed by different nodes. |
| message | A string with timing information. Each span can contain a list of these. They are produced by CockroachDB’s logging infrastructure and are the same messages that can be found in node except that a trace contains message across all severity levels, whereas log files, by default, do not. Thus, a trace is much more verbose than logs but only contains messages produced in the context of one particular traced operation. |

Response
The format of the
SHOW TRACE FOR SESSION response may change in future versions.=== SPAN START: <operation> === message) or a log message from a span. Rows are generally listed in their timestamp order (i.e., the order in which the events they represent occurred) with the exception that messages from child spans are interleaved in the parent span according to their timing. Messages from sibling spans, however, are not interleaved with respect to one another.
The following diagram shows the order in which messages from different spans would be interleaved in an example trace. Each box is a span; inner-boxes are child spans. The numbers indicate the order in which the log messages would appear in the virtual table.
| Column | Type | Description |
|---|---|---|
timestamp | TIMESTAMPTZ | The absolute time when the message occurred. |
age | INTERVAL | The age of the message relative to the beginning of the trace (i.e., the beginning of the recording in the case of SHOW TRACE FOR SESSION). |
message | STRING | The log message. |
tag | STRING | Meta-information about the message’s context. This is the same information that appears in the beginning of log file messages in between square brackets (e.g, [client=[::1]:49985,user=root,n1]). |
location | STRING | The file:line location of the line of code that produced the message. Only some of the messages have this field set; it depends on specifically how the message was logged. The --vmodule flag passed to the node producing the message also affects what rows get this field populated. Generally, if --vmodule=<file=<level is specified, messages produced by that file will have the field populated. |
operation | STRING | The name of the operation (or sub-operation) on whose behalf the message was logged. |
span | INT | The index of the span within the virtual list of all spans if they were ordered by the span’s start time. |
COMPACT parameter, only the age, message, tag, and operation columns are returned. In addition, the value of the location column is prepended to message.
Examples
Trace a session
Trace conflicting transactions
This example uses two terminals concurrently to generate conflicting transactions.-
In terminal 1, create a table:
-
In terminal 1, open a transaction and perform a write without closing the transaction:
Press enter one more time to send these statements to the server.
-
In terminal 2, turn tracing on:
-
In terminal 2, execute a conflicting read:
You’ll see that this statement is blocked until the transaction in terminal 1 finishes.
-
In terminal 1, finish the transaction:
-
In terminal 2, you’ll see the completed read:
-
In terminal 2, stop tracing and then view the completed trace:

