Reports
Run the built-in reports with the show and watch commands, call them from Python, and write your own reports with shell.register_report().
A report is a named function that collects information from the server and hands it to the shell for display. You run a report once with \show, or repeatedly with \watch, which refreshes the screen until you press Ctrl+C. MariaDB Shell ships three reports, query, thread, and threads, and you can register your own in Python.
Running Reports
The \show and \watch commands work in both SQL and Python mode and need an open global session.
\show <report> [options] [arguments]
\watch <report> [options] [arguments]Report names are case-insensitive, and
-and_are interchangeable, so\show table-sizesruns thetable_sizesreport.Put the options before the arguments. Everything after the first argument is treated as an argument.
Without a report name, both commands list the available reports.
\show <report> --helpdisplays the options and arguments of a report.
The following options apply to every report:
--help, -h
\show, \watch
Displays the help of the report.
--vertical, -E
\show, \watch
Displays the rows of a list report vertically, one line per column.
--interval=<seconds>, -i <seconds>
\watch
The time between refreshes. The default is 2 seconds, and the allowed range is 0.1 to 86400.
--nocls
\watch
Keeps the previous output instead of clearing the screen before each refresh.
For example, to watch the number of open connections every half second without clearing the screen:
MariaDB localhost:3306 ssl SQL > \watch query --interval=0.5 --nocls SHOW GLOBAL STATUS LIKE 'Threads_connected'Calling Reports from Python
Every report, built-in or registered, is also a method of the shell.reports object. The method takes the session, a list of arguments, and a dictionary of options, and returns a dictionary whose report key holds the data:
When you call a report as a function, use the long option names as dictionary keys, with underscores instead of dashes, for example {"prep_stmts": True}. The short forms such as -P work only with \show and \watch. To read the help of a report, run shell.reports.help("thread") or \? shell.reports.thread.
Built-in Reports
query
The query report runs the SQL statement that you pass as its arguments and displays the result as a table. It's most useful with \watch, to rerun a monitoring query at a fixed interval:
The report accepts --vertical and, with \watch, the refresh options.
thread
The thread report displays information about one server thread, by default the thread of the current connection. It reads performance_schema tables and sys schema functions.
--tid=<id>
-t
Reports on the thread with this Performance Schema thread ID.
--cid=<id>
-c
Reports on the thread of this connection ID, the ID that SHOW PROCESSLIST and CONNECTION_ID() return.
--general
-G
Basic information: user, host, command, state, transaction state, and the current and previous statement. This is the default when you give no other option.
--brief
-B
A one-line summary of the thread.
--client
-C
The client program, its connection attributes, the protocol, the socket, and the TLS cipher and version.
--innodb
-I
The InnoDB transaction of the thread.
--locks
-L
InnoDB and metadata locks that block the thread or that the thread blocks.
--prep-stmts
-P
The prepared statements that the thread has allocated.
--status[=<prefixes>]
-S
Session status variables, optionally only those whose names start with one of the comma-separated prefixes.
--user-vars[=<prefixes>]
-U
User-defined variables, optionally filtered by prefix.
--vars[=<prefixes>]
-V
Session system variables, optionally filtered by prefix.
--raw-locks
-R
Low-level lock information.
--all
-A
All of the above.
On MariaDB Server, --vars, --raw-locks, and --all fail, because they read the performance_schema.variables_by_thread and performance_schema.data_locks tables, which MariaDB Server doesn't have. Use the other options individually instead.
threads
The threads report lists the threads of the current user, or all foreground or background threads, with selectable columns (--format), sorted by any of them (--order-by, --desc) and limited in number (--limit). Run \show threads --help for the columns.
--where keeps the rows that match a condition. A condition compares a column with a value or another column, using =, !=, <>, <, <=, >, >=, LIKE or NOT LIKE, and combines comparisons with AND, OR, NOT and parentheses. NOT applies to the whole comparison that follows it, as in SQL. Keywords are case-insensitive, and strings are quoted with ' or ":
Other operators, such as IS, IN or BETWEEN, are refused with Disallowed operator, and a name that isn't a column of the report with Unknown column name.
Writing a Report
You register a report with shell.register_report():
name
The report name. It must be a valid Python identifier, and unique without regard to case.
type
list, report, or print. The type defines what the function returns and how \show displays it.
report
The Python function that produces the report.
description
Optional. A dictionary with the help text, options, and arguments of the report.
After registration, the report is available to \show and \watch and as shell.reports.<name>(). Registering a name that already exists raises an error.
Report Types
Type
The function returns
\show displays
list
{"report": [header, row, row, ...]}, where header is a list of column names and each row is a list of values.
A table, or vertical output with --vertical.
report
{"report": [document]}, a list with a single dictionary or list.
The document as YAML.
print
{"report": []}. The function prints its own output.
Nothing beyond what the function prints.
The Report Function
The shell calls the function with up to three positional arguments: the session, the list of arguments, and the dictionary of options. It passes the argument list only when the report accepts arguments or options, and the options dictionary only when the report defines options. To write a function that works in every case, give the last two parameters default values:
The function must return a dictionary with a report key. If it raises an exception, \show prints the exception message.
The Description Dictionary
brief
string
A one-line description, shown in the help.
details
list of strings
Paragraphs of detailed help.
argc
string
The number of arguments the report accepts: an exact number such as "1", "*" for any number, a range such as "0-2", or an open range such as "1-*". Without argc, the report accepts no arguments.
options
list of dictionaries
The options of the report. Without options, the report accepts none.
examples
list of dictionaries
Usage examples for the help. Each has a description and, optionally, args (a list of strings) and options (a dictionary of strings).
Each option dictionary accepts these keys:
name
string
Required. The long option name (--name), and the dictionary key when the report is called as a function. It must be a valid identifier.
shortcut
string
A single alphanumeric character for the short form (-n), available only with \show and \watch.
brief
string
A one-line description.
details
list of strings
Additional help paragraphs.
type
string
string (default), integer, float, or bool. A bool option is a switch: it's False unless given, and takes no value on the \show command line.
required
bool
Whether the option must be given. A bool option can't be required.
values
list of strings
The allowed values of a string option.
empty
bool
Whether a string option accepts an empty value. The default is False.
Loading Reports at Startup
To have a report in every session, register it in a plugin. Create a directory for it in the plugins directory of the user configuration directory, and put the registration code in its init.py file:
On Windows, the directory is %AppData%\MariaDB\mariadb-shell\plugins\. The shell loads the plugin at every startup, whatever the mode, so the report is also available to \show and \watch in SQL mode. A plugin directory can be shared with other users as it is, or installed from a repository with the plugins global object. See Plugins.
As an alternative, the shell runs every .py file in the init.d directory of the user configuration directory (~/.mariadb-shell/init.d/, or %AppData%\MariaDB\mariadb-shell\init.d\ on Windows) at startup, before it loads plugins. Each file runs in its own namespace, and the files also run when you start the shell with --disable-plugins. If a file fails, the shell prints a warning at startup and writes the details to the log file.
Example: Largest Tables
The following list report shows the largest tables of a schema. It accepts an optional schema name and a --limit option.
After you restart the shell, run the report:
From Python, call the same report with an options dictionary:
Example: Report and Print Types
A report report returns one document, which \show renders as YAML with the keys sorted. A print report prints its own output and returns an empty list. Neither of the following reports takes arguments or options, so the shell calls them with the session only.
This page is licensed: GPLv2
Last updated
Was this helpful?

