Skip to content

Commit

Permalink
sql: add a SHOW TENANTS command
Browse files Browse the repository at this point in the history
This command prints all available tenants and their status.
To get the status for replication (c2c) tenants we need to get the
job info.

For example:

```
[email protected]:26257/defaultdb> show tenants;
  id |   name   |                               status
-----+----------+----------------------------------------------------------------------
   1 | system   | ACTIVE
   2 | src      | ACTIVE
   3 | dest     | REPLICATION PAUSED
   4 | dest2    | REPLICATION UNKNOWN (succeeded)
   6 | dest3    | REPLICATION UNKNOWN (job with ID 819888096954253313 does not exist)
  11 | dest4    | ACTIVE
  12 | dest5    | REPLICATING
  13 | dest6    | INITIALIZING REPLICATION
(13 rows)

Time: 67ms total (execution 67ms / network 0ms)
```

Fixes: #92641

Epic: CRDB-18749

Release note: None
  • Loading branch information
lidorcarmel committed Jan 7, 2023
1 parent 1c7122f commit 62c6e2b
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 77 deletions.
3 changes: 2 additions & 1 deletion docs/generated/sql/bnf/show_tenant_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
show_tenant_stmt ::=
'SHOW' 'TENANT' d_expr
'SHOW' 'TENANTS'
| 'SHOW' 'TENANT' d_expr
| 'SHOW' 'TENANT' d_expr 'WITH' 'REPLICATION' 'STATUS'
4 changes: 3 additions & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ show_tables_stmt ::=
| 'SHOW' 'TABLES' with_comment

show_tenant_stmt ::=
'SHOW' 'TENANT' d_expr
'SHOW' 'TENANTS'
| 'SHOW' 'TENANT' d_expr
| 'SHOW' 'TENANT' d_expr 'WITH' 'REPLICATION' 'STATUS'

show_trace_stmt ::=
Expand Down Expand Up @@ -1380,6 +1381,7 @@ unreserved_keyword ::=
| 'TEMPLATE'
| 'TEMPORARY'
| 'TENANT'
| 'TENANTS'
| 'TESTING_RELOCATE'
| 'TEXT'
| 'TIES'
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/streamingccl/streamingest/alter_replication_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func alterReplicationJobHook(
return err
}
if tenInfo.TenantReplicationJobID == 0 {
return errors.Newf("tenant %q does not have an active replication job", tenantName)
return errors.Newf("tenant %q is not a replicated tenant", tenantName)
}
jobRegistry := p.ExecCfg().JobRegistry
if alterTenantStmt.Cutover != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func TestAlterTenantPauseResume(t *testing.T) {

t.Run("pause-resume-tenant-with-no-replication", func(t *testing.T) {
c.DestSysSQL.Exec(t, `CREATE TENANT noreplication`)
c.DestSysSQL.ExpectErr(t, "tenant \"noreplication\" does not have an active replication job",
c.DestSysSQL.ExpectErr(t, `tenant "noreplication" is not a replicated tenant`,
`ALTER TENANT $1 PAUSE REPLICATION`, "noreplication")
c.DestSysSQL.ExpectErr(t, "tenant \"noreplication\" does not have an active replication job",
c.DestSysSQL.ExpectErr(t, `tenant "noreplication" is not a replicated tenant`,
`ALTER TENANT $1 RESUME REPLICATION`, "noreplication")
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/tenant
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ id name status
statement error tenant "seven" does not exist
SHOW TENANT seven

statement error tenant "tenant-one" does not have an active replication job
query error pq: tenant "tenant-one" is not a replicated tenant
SHOW TENANT "tenant-one" WITH REPLICATION STATUS

statement error tenant "two" does not have an active replication job
query error pq: tenant "two" is not a replicated tenant
SHOW TENANT two WITH REPLICATION STATUS

# Test creating a tenant with the same name as an existing tenant, but a unique
Expand Down
14 changes: 11 additions & 3 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -5537,13 +5537,20 @@ backup_kms:

// %Help: SHOW TENANT - display tenant information
// %Category: Misc
// %Text: SHOW TENANT <tenant_name> [WITH REPLICATION STATUS]
// %Text:
// SHOW TENANT <tenant_name> [WITH REPLICATION STATUS]
// SHOW TENANTS
show_tenant_stmt:
SHOW TENANT d_expr
SHOW TENANTS
{
$$.val = &tree.ShowTenant{
All: true,
}
}
| SHOW TENANT d_expr
{
$$.val = &tree.ShowTenant{
Name: $3.expr(),
WithReplication: false,
}
}
| SHOW TENANT d_expr WITH REPLICATION STATUS
Expand Down Expand Up @@ -16003,6 +16010,7 @@ unreserved_keyword:
| TEMPLATE
| TEMPORARY
| TENANT
| TENANTS
| TESTING_RELOCATE
| TEXT
| TIES
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/sem/tree/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,16 @@ func (node *ShowTableStats) Format(ctx *FmtCtx) {
type ShowTenant struct {
Name Expr
WithReplication bool
All bool
}

// Format implements the NodeFormatter interface.
func (node *ShowTenant) Format(ctx *FmtCtx) {
if node.All {
ctx.WriteString("SHOW TENANTS")
return
}

ctx.WriteString("SHOW TENANT ")
ctx.FormatNode(node.Name)

Expand Down
Loading

0 comments on commit 62c6e2b

Please sign in to comment.