Skip to content

Commit

Permalink
Merge #118002
Browse files Browse the repository at this point in the history
118002: sql: add trace_id to show sessions r=maryliag a=maryliag

Add the column trace_id to the result of `SHOW SESSIONS`.
Also adding the column to the views:
`crdb_internal.node_sessions` and `crdb_internal.cluster_sessions`.

Part Of #117625

Release note (sql change): Add column `trace_id` to the response of the `SHOW SESSIONS` command.

Co-authored-by: maryliag <marylia@cockroachlabs.com>
  • Loading branch information
craig[bot] and maryliag committed Jan 23, 2024
2 parents edaa982 + 43072c2 commit 5db0b83
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,7 @@ Session represents one SQL session.
| txn_fingerprint_ids | [uint64](#cockroach.server.serverpb.ListSessionsResponse-uint64) | repeated | List of transaction fingerprint IDs in this session. | [reserved](#support-status) |
| total_active_time | [google.protobuf.Duration](#cockroach.server.serverpb.ListSessionsResponse-google.protobuf.Duration) | | The session's total active time. | [reserved](#support-status) |
| pg_backend_pid | [uint32](#cockroach.server.serverpb.ListSessionsResponse-uint32) | | The numerical ID attached to the session which is used to mimic a Postgres backend PID for compatibility with the query cancellation protocol. Unlike in Postgres, this value does not correspond to a real process ID. | [reserved](#support-status) |
| trace_id | [uint64](#cockroach.server.serverpb.ListSessionsResponse-uint64) | | The ID of the session's active trace. It will be 0 if tracing is off. | [reserved](#support-status) |



Expand Down Expand Up @@ -2267,6 +2268,7 @@ Session represents one SQL session.
| txn_fingerprint_ids | [uint64](#cockroach.server.serverpb.ListSessionsResponse-uint64) | repeated | List of transaction fingerprint IDs in this session. | [reserved](#support-status) |
| total_active_time | [google.protobuf.Duration](#cockroach.server.serverpb.ListSessionsResponse-google.protobuf.Duration) | | The session's total active time. | [reserved](#support-status) |
| pg_backend_pid | [uint32](#cockroach.server.serverpb.ListSessionsResponse-uint32) | | The numerical ID attached to the session which is used to mimic a Postgres backend PID for compatibility with the query cancellation protocol. Unlike in Postgres, this value does not correspond to a real process ID. | [reserved](#support-status) |
| trace_id | [uint64](#cockroach.server.serverpb.ListSessionsResponse-uint64) | | The ID of the session's active trace. It will be 0 if tracing is off. | [reserved](#support-status) |



Expand Down
8 changes: 4 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/crdb_internal_tenant
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ SELECT * FROM crdb_internal.cluster_transactions WHERE node_id < 0
----
id node_id session_id start txn_string application_name num_stmts num_retries num_auto_retries last_auto_retry_reason isolation_level priority quality_of_service

query ITTTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTTI colnames
SELECT * FROM crdb_internal.node_sessions WHERE node_id < 0
----
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid trace_id

query ITTTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTTI colnames
SELECT * FROM crdb_internal.cluster_sessions WHERE node_id < 0
----
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid trace_id

query IIITTTI colnames
SELECT * FROM crdb_internal.node_contention_events WHERE table_id < 0
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/zip_table_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ var zipInternalTablesPerCluster = DebugZipTableRegistry{
"session_end",
"crdb_internal.hide_sql_constants(active_queries) as active_queries",
"crdb_internal.hide_sql_constants(last_active_query) as last_active_query",
"trace_id",
},
},
"crdb_internal.cluster_settings": {
Expand Down Expand Up @@ -793,6 +794,7 @@ var zipInternalTablesPerNode = DebugZipTableRegistry{
"session_end",
"crdb_internal.hide_sql_constants(active_queries) as active_queries",
"crdb_internal.hide_sql_constants(last_active_query) as last_active_query",
"trace_id",
},
},
"crdb_internal.node_statement_statistics": {
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/serverpb/status.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,9 @@ message Session {
// backend PID for compatibility with the query cancellation protocol. Unlike
// in Postgres, this value does not correspond to a real process ID.
uint32 pg_backend_pid = 19 [ (gogoproto.customname) = "PGBackendPID" ];

// The ID of the session's active trace. It will be 0 if tracing is off.
uint64 trace_id = 20 [(gogoproto.customname) = "TraceID"];
}

// An error wrapper object for ListSessionsResponse.
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4160,6 +4160,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
Status: status,
TotalActiveTime: sessionActiveTime,
PGBackendPID: ex.planner.extendedEvalCtx.QueryCancelKey.GetPGBackendPID(),
TraceID: uint64(ex.planner.extendedEvalCtx.Tracing.connSpan.TraceID()),
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2811,7 +2811,8 @@ CREATE TABLE crdb_internal.%s (
max_alloc_bytes INT, -- the high water mark of bytes allocated by the session
status STRING, -- the status of the session (open, closed)
session_end TIMESTAMPTZ, -- the time when the session was closed
pg_backend_pid INT -- the numerical ID attached to the session which is used to mimic a Postgres backend PID
pg_backend_pid INT, -- the numerical ID attached to the session which is used to mimic a Postgres backend PID
trace_id INT -- the ID of the trace of the session
)
`

Expand Down Expand Up @@ -2913,6 +2914,7 @@ func populateSessionsTable(
tree.NewDString(session.Status.String()),
endTSDatum,
tree.NewDInt(tree.DInt(session.PGBackendPID)),
tree.NewDInt(tree.DInt(session.TraceID)),
); err != nil {
return err
}
Expand Down Expand Up @@ -2940,6 +2942,7 @@ func populateSessionsTable(
tree.DNull, // status
tree.DNull, // session_end
tree.DNull, // pg_backend_pid
tree.DNull, // trace_id
); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/delegate/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/delegate",
visibility = ["//visibility:public"],
deps = [
"//pkg/clusterversion",
"//pkg/jobs",
"//pkg/jobs/jobspb",
"//pkg/security/username",
Expand Down
11 changes: 10 additions & 1 deletion pkg/sql/delegate/show_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
package delegate

import (
"fmt"

"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)

func (d *delegator) delegateShowSessions(n *tree.ShowSessions) (tree.Statement, error) {
const query = `SELECT node_id, session_id, status, user_name, client_address, application_name, active_queries, last_active_query, session_start, active_query_start, num_txns_executed FROM crdb_internal.`
columns := `node_id, session_id, status, user_name, client_address, application_name, active_queries,
last_active_query, session_start, active_query_start, num_txns_executed`
if d.evalCtx.Settings.Version.IsActive(d.ctx, clusterversion.V24_1Start) {
columns = fmt.Sprintf("%s, trace_id", columns)
}

query := fmt.Sprintf(`SELECT %s FROM crdb_internal.`, columns)
table := `node_sessions`
if n.Cluster {
table = `cluster_sessions`
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,15 @@ user root
statement ok
REVOKE SYSTEM VIEWACTIVITYREDACTED FROM testuser

query ITTTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTTI colnames
SELECT * FROM crdb_internal.node_sessions WHERE node_id < 0
----
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid trace_id

query ITTTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTTI colnames
SELECT * FROM crdb_internal.cluster_sessions WHERE node_id < 0
----
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid
node_id session_id user_name client_address application_name active_queries last_active_query num_txns_executed session_start active_query_start kv_txn alloc_bytes max_alloc_bytes status session_end pg_backend_pid trace_id

query IIITTTI colnames
SELECT * FROM crdb_internal.node_contention_events WHERE table_id < 0
Expand Down

0 comments on commit 5db0b83

Please sign in to comment.