Skip to content

Commit

Permalink
sql: add pg_backend_pid column to crdb_internal.{node, cluster}_sessions
Browse files Browse the repository at this point in the history
Fixes #98581.
Informs #95771.

This commit adds a new pg_backend_pid column to the
crdb_internal.node_sessions and crdb_internal.cluster_sessions tables.

Release note (sql change): A pg_backend_pid column was added to
crdb_internal.node_sessions and crdb_internal.cluster_sessions. This
value corresponds to the numerical ID returned from pg_backend_pid().
  • Loading branch information
nvanbenschoten committed Dec 18, 2023
1 parent 7e9aca9 commit 8a95753
Show file tree
Hide file tree
Showing 8 changed files with 25 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 @@ -2119,6 +2119,7 @@ Session represents one SQL session.
| num_txns_executed | [int32](#cockroach.server.serverpb.ListSessionsResponse-int32) | | Count of the number of transactions that have been opened on this session. This count includes transactions that are in progress. | [reserved](#support-status) |
| 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 Postgres backend PID of the session. | [reserved](#support-status) |



Expand Down Expand Up @@ -2265,6 +2266,7 @@ Session represents one SQL session.
| num_txns_executed | [int32](#cockroach.server.serverpb.ListSessionsResponse-int32) | | Count of the number of transactions that have been opened on this session. This count includes transactions that are in progress. | [reserved](#support-status) |
| 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 Postgres backend PID of the session. | [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 ITTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTT 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
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

query ITTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTT 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
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

query IIITTTI colnames
SELECT * FROM crdb_internal.node_contention_events WHERE table_id < 0
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 @@ -1048,6 +1048,9 @@ message Session {
// The session's total active time.
google.protobuf.Duration total_active_time = 18 [(gogoproto.nullable) = false,
(gogoproto.stdduration) = true];

// The Postgres backend PID of the session.
uint32 pg_backend_pid = 19 [ (gogoproto.customname) = "PGBackendPID" ];
}

// 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 @@ -4171,6 +4171,7 @@ func (ex *connExecutor) serialize() serverpb.Session {
LastActiveQueryNoConstants: lastActiveQueryNoConstants,
Status: status,
TotalActiveTime: sessionActiveTime,
PGBackendPID: ex.planner.extendedEvalCtx.QueryCancelKey.GetPGBackendPID(),
}
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,8 @@ CREATE TABLE crdb_internal.%s (
alloc_bytes INT, -- the number of bytes allocated by the session
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
session_end TIMESTAMPTZ, -- the time when the session was closed
pg_backend_pid INT -- the postgres backend PID of the session
)
`

Expand Down Expand Up @@ -2910,6 +2911,7 @@ func populateSessionsTable(
tree.NewDInt(tree.DInt(session.MaxAllocBytes)),
tree.NewDString(session.Status.String()),
endTSDatum,
tree.NewDInt(tree.DInt(session.PGBackendPID)),
); err != nil {
return err
}
Expand All @@ -2935,7 +2937,8 @@ func populateSessionsTable(
tree.DNull, // alloc_bytes
tree.DNull, // max_alloc_bytes
tree.DNull, // status
tree.DNull, // session end
tree.DNull, // session_end
tree.DNull, // pg_backend_pid
); err != nil {
return err
}
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 ITTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTT 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
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

query ITTTTTTTTTTTTTT colnames
query ITTTTTTTTTTTTTTT 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
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

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

0 comments on commit 8a95753

Please sign in to comment.