Skip to content

Commit

Permalink
sql: add SHOW TRANSACTIONS
Browse files Browse the repository at this point in the history
Release note (sql change): add the SHOW TRANSACTIONS statement, similar
to SHOW SESSIONS and variants. It shows the currently active
transactions in the node or cluster, and some information about them.
  • Loading branch information
jordanlewis committed Jul 27, 2020
1 parent 1bd878c commit ebf2ec0
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/show_var.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ show_stmt ::=
| show_stats_stmt
| show_tables_stmt
| show_trace_stmt
| show_transactions_stmt
| show_users_stmt
| show_zone_stmt
6 changes: 6 additions & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ show_stmt ::=
| show_stats_stmt
| show_tables_stmt
| show_trace_stmt
| show_transactions_stmt
| show_users_stmt
| show_zone_stmt

Expand Down Expand Up @@ -581,6 +582,10 @@ show_trace_stmt ::=
'SHOW' opt_compact 'TRACE' 'FOR' 'SESSION'
| 'SHOW' opt_compact 'KV' 'TRACE' 'FOR' 'SESSION'

show_transactions_stmt ::=
'SHOW' opt_cluster 'TRANSACTIONS'
| 'SHOW' 'ALL' opt_cluster 'TRANSACTIONS'

show_users_stmt ::=
'SHOW' 'USERS'

Expand Down Expand Up @@ -878,6 +883,7 @@ unreserved_keyword ::=
| 'TIES'
| 'TRACE'
| 'TRANSACTION'
| 'TRANSACTIONS'
| 'TRIGGER'
| 'TRUNCATE'
| 'TRUSTED'
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/delegate/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func TryDelegate(
case *tree.ShowTables:
return d.delegateShowTables(t)

case *tree.ShowTransactions:
return d.delegateShowTransactions(t)

case *tree.ShowUsers:
return d.delegateShowRoles()

Expand Down
41 changes: 41 additions & 0 deletions pkg/sql/delegate/show_transactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package delegate

import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)

const _ = `
id UUID, -- the unique ID of the transaction
node_id INT, -- the ID of the node running the transaction
session_id STRING, -- the ID of the session
start TIMESTAMP, -- the start time of the transaction
txn_string STRING, -- the string representation of the transcation
application_name STRING, -- the name of the application as per SET application_name
num_stmts INT, -- the number of statements executed so far
num_retries INT, -- the number of times the transaction was restarted
num_auto_retries INT -- the number of times the transaction was automatically restarted
`

func (d *delegator) delegateShowTransactions(n *tree.ShowTransactions) (tree.Statement, error) {
const query = `SELECT node_id, id, application_name, num_stmts, num_retries, num_auto_retries FROM `
table := `crdb_internal.node_transactions`
if n.Cluster {
table = `crdb_internal.cluster_transactions`
}
var filter string
if !n.All {
filter = " WHERE application_name NOT LIKE '" + sqlbase.InternalAppNamePrefix + "%'"
}
return parse(query + table + filter)
}
2 changes: 2 additions & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,8 @@ $function$`,

{`SHOW SESSIONS`, `SHOW CLUSTER SESSIONS`},
{`SHOW ALL SESSIONS`, `SHOW ALL CLUSTER SESSIONS`},
{`SHOW TRANSACTIONS`, `SHOW CLUSTER TRANSACTIONS`},
{`SHOW ALL TRANSACTIONS`, `SHOW ALL CLUSTER TRANSACTIONS`},
{`SHOW QUERIES`, `SHOW CLUSTER QUERIES`},
{`SHOW ALL QUERIES`, `SHOW ALL CLUSTER QUERIES`},

Expand Down
21 changes: 20 additions & 1 deletion pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ func (u *sqlSymUnion) alterTypeAddValuePlacement() *tree.AlterTypeAddValuePlacem
%token <str> SYMMETRIC SYNTAX SYSTEM SQRT SUBSCRIPTION

%token <str> TABLE TABLES TEMP TEMPLATE TEMPORARY TENANT TESTING_RELOCATE EXPERIMENTAL_RELOCATE TEXT THEN
%token <str> TIES TIME TIMETZ TIMESTAMP TIMESTAMPTZ TO THROTTLING TRAILING TRACE TRANSACTION TREAT TRIGGER TRIM TRUE
%token <str> TIES TIME TIMETZ TIMESTAMP TIMESTAMPTZ TO THROTTLING TRAILING TRACE
%token <str> TRANSACTION TRANSACTIONS TREAT TRIGGER TRIM TRUE
%token <str> TRUNCATE TRUSTED TYPE
%token <str> TRACING

Expand Down Expand Up @@ -830,6 +831,7 @@ func (u *sqlSymUnion) alterTypeAddValuePlacement() *tree.AlterTypeAddValuePlacem
%type <tree.Statement> show_tables_stmt
%type <tree.Statement> show_trace_stmt
%type <tree.Statement> show_transaction_stmt
%type <tree.Statement> show_transactions_stmt
%type <tree.Statement> show_users_stmt
%type <tree.Statement> show_zone_stmt

Expand Down Expand Up @@ -3861,6 +3863,7 @@ show_stmt:
| show_tables_stmt // EXTEND WITH HELP: SHOW TABLES
| show_trace_stmt // EXTEND WITH HELP: SHOW TRACE
| show_transaction_stmt // EXTEND WITH HELP: SHOW TRANSACTION
| show_transactions_stmt // EXTEND WITH HELP: SHOW TRANSACTIONS
| show_users_stmt // EXTEND WITH HELP: SHOW USERS
| show_zone_stmt
| SHOW error // SHOW HELP: SHOW
Expand Down Expand Up @@ -4293,6 +4296,21 @@ show_tables_stmt:
}
| SHOW TABLES error // SHOW HELP: SHOW TABLES

// %Help: SHOW TRANSACTIONS - list open client transactions
// %Category: Misc
// %Text: SHOW [ALL] [CLUSTER | LOCAL] TRANSACTIONS
show_transactions_stmt:
SHOW opt_cluster TRANSACTIONS
{
$$.val = &tree.ShowTransactions{Cluster: $2.bool()}
}
| SHOW opt_cluster TRANSACTIONS error // SHOW HELP: SHOW TRANSACTIONS
| SHOW ALL opt_cluster TRANSACTIONS
{
$$.val = &tree.ShowTransactions{All: true, Cluster: $3.bool()}
}
| SHOW ALL opt_cluster TRANSACTIONS error // SHOW HELP: SHOW TRANSACTIONS

with_comment:
WITH COMMENT { $$.val = true }
| /* EMPTY */ { $$.val = false }
Expand Down Expand Up @@ -10729,6 +10747,7 @@ unreserved_keyword:
| TIES
| TRACE
| TRANSACTION
| TRANSACTIONS
| TRIGGER
| TRUNCATE
| TRUSTED
Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/sem/tree/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,25 @@ func (node *ShowTables) Format(ctx *FmtCtx) {
}
}

// ShowTransactions represents a SHOW TRANSACTIONS statement
type ShowTransactions struct {
All bool
Cluster bool
}

// Format implements the NodeFormatter interface.
func (node *ShowTransactions) Format(ctx *FmtCtx) {
ctx.WriteString("SHOW ")
if node.All {
ctx.WriteString("ALL ")
}
if node.Cluster {
ctx.WriteString("CLUSTER TRANSACTIONS")
} else {
ctx.WriteString("LOCAL TRANSACTIONS")
}
}

// ShowConstraints represents a SHOW CONSTRAINTS statement.
type ShowConstraints struct {
Table *UnresolvedObjectName
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ func (*ShowTables) StatementType() StatementType { return Rows }
// StatementTag returns a short string identifying the type of statement.
func (*ShowTables) StatementTag() string { return "SHOW TABLES" }

// StatementType implements the Statement interface
func (*ShowTransactions) StatementType() StatementType { return Rows }

// StatementTag returns a short string identifying the type of statement.
func (*ShowTransactions) StatementTag() string { return "SHOW TRANSACTIONS" }

// StatementType implements the Statement interface.
func (*ShowSchemas) StatementType() StatementType { return Rows }

Expand Down Expand Up @@ -1027,6 +1033,7 @@ func (n *ShowTableStats) String() string { return AsString(n) }
func (n *ShowTables) String() string { return AsString(n) }
func (n *ShowTraceForSession) String() string { return AsString(n) }
func (n *ShowTransactionStatus) String() string { return AsString(n) }
func (n *ShowTransactions) String() string { return AsString(n) }
func (n *ShowLastQueryStatistics) String() string { return AsString(n) }
func (n *ShowUsers) String() string { return AsString(n) }
func (n *ShowVar) String() string { return AsString(n) }
Expand Down

0 comments on commit ebf2ec0

Please sign in to comment.