Skip to content

Commit

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

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

Fixes #117625

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

Co-authored-by: maryliag <marylia@cockroachlabs.com>
  • Loading branch information
craig[bot] and maryliag committed Feb 6, 2024
2 parents b106b81 + ec72121 commit 47a68b5
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,7 @@ Session represents one SQL session.
| 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) |
| goroutine_id | [int64](#cockroach.server.serverpb.ListSessionsResponse-int64) | | The ID of the session's goroutine. | [reserved](#support-status) |



Expand Down Expand Up @@ -2269,6 +2270,7 @@ Session represents one SQL session.
| 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) |
| goroutine_id | [int64](#cockroach.server.serverpb.ListSessionsResponse-int64) | | The ID of the session's goroutine. | [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 ITTTTTTTTTTTTTTTI colnames
query ITTTTTTITTTIITTIII 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 trace_id
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 goroutine_id

query ITTTTTTTTTTTTTTTI colnames
query ITTTTTTITTTIITTIII 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 trace_id
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 goroutine_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 @@ -221,6 +221,7 @@ var zipInternalTablesPerCluster = DebugZipTableRegistry{
"crdb_internal.hide_sql_constants(active_queries) as active_queries",
"crdb_internal.hide_sql_constants(last_active_query) as last_active_query",
"trace_id",
"goroutine_id",
},
},
"crdb_internal.cluster_settings": {
Expand Down Expand Up @@ -817,6 +818,7 @@ var zipInternalTablesPerNode = DebugZipTableRegistry{
"crdb_internal.hide_sql_constants(active_queries) as active_queries",
"crdb_internal.hide_sql_constants(last_active_query) as last_active_query",
"trace_id",
"goroutine_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 @@ -1056,6 +1056,9 @@ message Session {

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

// The ID of the session's goroutine.
int64 goroutine_id = 21 [(gogoproto.customname) = "GoroutineID"];
}

// An error wrapper object for ListSessionsResponse.
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ go_library(
"@com_github_gogo_protobuf//types",
"@com_github_lib_pq//:pq",
"@com_github_lib_pq//oid",
"@com_github_petermattis_goid//:goid",
"@com_github_prometheus_client_model//go",
"@in_gopkg_yaml_v2//:yaml_v2",
"@io_opentelemetry_go_otel//attribute",
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import (
"github.com/cockroachdb/errors"
"github.com/cockroachdb/logtags"
"github.com/cockroachdb/redact"
"github.com/petermattis/goid"
)

var maxNumNonAdminConnections = settings.RegisterIntSetting(
Expand Down Expand Up @@ -1081,7 +1082,7 @@ func (s *Server) newConnExecutor(

// ctxHolder will be reset at the start of run(). We only define
// it here so that an early call to close() doesn't panic.
ctxHolder: ctxHolder{connCtx: ctx},
ctxHolder: ctxHolder{connCtx: ctx, goroutineID: goid.Get()},
phaseTimes: sessionphase.NewTimes(),
rng: rand.New(rand.NewSource(timeutil.Now().UnixNano())),
executorType: executorTypeExec,
Expand Down Expand Up @@ -1705,6 +1706,7 @@ type connExecutor struct {
type ctxHolder struct {
connCtx context.Context
sessionTracingCtx context.Context
goroutineID int64
}

// timeout wraps a Timer returned by time.AfterFunc. This interface
Expand Down Expand Up @@ -4190,6 +4192,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
TotalActiveTime: sessionActiveTime,
PGBackendPID: ex.planner.extendedEvalCtx.QueryCancelKey.GetPGBackendPID(),
TraceID: uint64(ex.planner.extendedEvalCtx.Tracing.connSpan.TraceID()),
GoroutineID: ex.ctxHolder.goroutineID,
}
}

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 @@ -2812,7 +2812,8 @@ CREATE TABLE crdb_internal.%s (
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
trace_id INT -- the ID of the trace of the session
trace_id INT, -- the ID of the trace of the session
goroutine_id INT -- the ID of the goroutine of the session
)
`

Expand Down Expand Up @@ -2915,6 +2916,7 @@ func populateSessionsTable(
endTSDatum,
tree.NewDInt(tree.DInt(session.PGBackendPID)),
tree.NewDInt(tree.DInt(session.TraceID)),
tree.NewDInt(tree.DInt(session.GoroutineID)),
); err != nil {
return err
}
Expand Down Expand Up @@ -2943,6 +2945,7 @@ func populateSessionsTable(
tree.DNull, // session_end
tree.DNull, // pg_backend_pid
tree.DNull, // trace_id
tree.DNull, // goroutine_id
); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/delegate/show_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (d *delegator) delegateShowSessions(n *tree.ShowSessions) (tree.Statement,
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)
columns = fmt.Sprintf("%s, trace_id, goroutine_id", columns)
}

query := fmt.Sprintf(`SELECT %s FROM crdb_internal.`, columns)
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 ITTTTTTTTTTTTTTTI colnames
query ITTTTTTITTTIITTIII 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 trace_id
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 goroutine_id

query ITTTTTTTTTTTTTTTI colnames
query ITTTTTTITTTIITTIII 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 trace_id
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 goroutine_id

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

0 comments on commit 47a68b5

Please sign in to comment.