Skip to content

Commit

Permalink
infoschema,executor: add PROCESSLIST table to INFORMMATION_SCHEMA dat…
Browse files Browse the repository at this point in the history
…abase (#7286)

Cherry-pick #7236
  • Loading branch information
coocood authored Aug 6, 2018
1 parent b13bc08 commit 59f7695
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func (s *testSuite) TestAggregation(c *C) {
result.Check(testkit.Rows("<nil>", "<nil>"))

result = tk.MustQuery("select count(*) from information_schema.columns")
// When adding new memory columns in information_schema, please update this variable.
columnCountOfAllInformationSchemaTables := "746"
// When adding new memory columns in information_schema, please update this variable.\
columnCountOfAllInformationSchemaTables := "754"
result.Check(testkit.Rows(columnCountOfAllInformationSchemaTables))

tk.MustExec("drop table if exists t1")
Expand Down
1 change: 1 addition & 0 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func (*testSuite) TestInfoTables(c *C) {
"OPTIMIZER_TRACE",
"TABLESPACES",
"COLLATION_CHARACTER_SET_APPLICABILITY",
"PROCESSLIST",
}
for _, t := range info_tables {
tb, err1 := is.TableByName(model.NewCIStr(infoschema.Name), model.NewCIStr(t))
Expand Down
44 changes: 44 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package infoschema
import (
"fmt"
"sort"
"time"

"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -63,6 +64,7 @@ const (
tableOptimizerTrace = "OPTIMIZER_TRACE"
tableTableSpaces = "TABLESPACES"
tableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY"
tableProcesslist = "PROCESSLIST"
)

type columnInfo struct {
Expand Down Expand Up @@ -515,6 +517,17 @@ var tableCollationCharacterSetApplicabilityCols = []columnInfo{
{"CHARACTER_SET_NAME", mysql.TypeVarchar, 32, mysql.NotNullFlag, nil, nil},
}

var tableProcesslistCols = []columnInfo{
{"ID", mysql.TypeLonglong, 21, mysql.NotNullFlag, 0, nil},
{"USER", mysql.TypeVarchar, 16, mysql.NotNullFlag, "", nil},
{"HOST", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil},
{"DB", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil},
{"COMMAND", mysql.TypeVarchar, 16, mysql.NotNullFlag, "", nil},
{"TIME", mysql.TypeLong, 7, mysql.NotNullFlag, 0, nil},
{"STATE", mysql.TypeVarchar, 7, 0, nil, nil},
{"Info", mysql.TypeString, 512, 0, nil, nil},
}

func dataForCharacterSets() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("ascii", "ascii_general_ci", "US ASCII", 1),
Expand Down Expand Up @@ -568,6 +581,34 @@ func dataForUserPrivileges(ctx sessionctx.Context) [][]types.Datum {
return pm.UserPrivilegesTable()
}

func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
sm := ctx.GetSessionManager()
if sm == nil {
return nil
}

var records [][]types.Datum
pl := sm.ShowProcessList()
for _, pi := range pl {
var t uint64
if len(pi.Info) != 0 {
t = uint64(time.Since(pi.Time) / time.Second)
}
record := types.MakeDatums(
pi.ID,
pi.User,
pi.Host,
pi.DB,
pi.Command,
t,
fmt.Sprintf("%d", pi.State),
pi.Info,
)
records = append(records, record)
}
return records
}

func dataForEngines() (records [][]types.Datum) {
records = append(records,
types.MakeDatums("InnoDB", "DEFAULT", "Supports transactions, row-level locking, and foreign keys", "YES", "YES", "YES"),
Expand Down Expand Up @@ -1038,6 +1079,7 @@ var tableNameToColumns = map[string][]columnInfo{
tableOptimizerTrace: tableOptimizerTraceCols,
tableTableSpaces: tableTableSpacesCols,
tableCollationCharacterSetApplicability: tableCollationCharacterSetApplicabilityCols,
tableProcesslist: tableProcesslistCols,
}

func createInfoSchemaTable(handle *Handle, meta *model.TableInfo) *infoschemaTable {
Expand Down Expand Up @@ -1124,6 +1166,8 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
case tableTableSpaces:
case tableCollationCharacterSetApplicability:
fullRows = dataForCollationCharacterSetApplicability()
case tableProcesslist:
fullRows = dataForProcesslist(ctx)
}
if err != nil {
return nil, errors.Trace(err)
Expand Down

0 comments on commit 59f7695

Please sign in to comment.