Stored procedures used to modify the configuration | A comprehensive understanding of the sys system library

In this series, a lot of space has been used to introduce the views of the sys system library. Using these views, we can easily and quickly query the content under performance_schema and information_schema. However, the instrument and consumer configuration information under performance_schema belongs to the content that needs to be modified. In addition to directly using the update statement to modify the configuration table, are there shortcuts similar to query views? Yes, the content of this issue begins to introduce some storage procedures related to modification and configuration confirmation.

PS: Below, if the stored procedure definition text is shorter, the definition text of some stored procedures will be listed, so that you can learn them more intuitively. Too long stored procedure definition text, please download and view by yourself according to the download path introduced in the article "Getting to know | a comprehensive understanding of sys system library" .

01

ps_setup_disable_background_threads()

Disable the performance event collection function of all background threads. The stored procedure is implemented by modifying the performance_schema.threads table, setting the instrumented field of all background threads to NO

  • The stored procedure does not require any parameters when executing, and returns a value of the number of threads that have been closed (threads that are already closed will not be counted because the ROW_COUNT() function is used as the return value, which only records the number of rows that have actually changed) , The closed thread will not collect any performance event data

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_disable_background_threads;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_disable_background_threads ()
COMMENT '
......
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.threads
  SET instrumented = 'NO'
WHERE type = 'BACKGROUND';

SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

Example (no need to pass parameters)

admin@localhost : sys 09:48:12> CALL sys.ps_setup_disable_background_threads();
+--------------------------------+
| summary                        |
+--------------------------------+
| Disabled 40 background threads |
+--------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

02

ps_setup_disable_consumer()

Disable the specified consumers. The stored procedure is implemented by modifying the performance_schema.setup_consumers table. When calling, you need to pass in a consumer name string as the parameter value, modify the enabled field of the performance_schema.setup_consumers table to NO, and return the number of closed consumers (already Consumers in the closed state will not be counted, because the ROW_COUNT() function is used as the return value, which only records the rows that have actually changed)

  • The passed-in parameter string is used internally like %consumer%; to fuzzy match the name field of the performance_schema.setup_consumers table

parameter:

  • consumer VARCHAR(128): Match the value of the consumer name, by using the form like %consumer%; to fuzzy match the name field of the setup_consumers table to perform the UPDATE operation. Note that if the value passed in is a null value, it will match all consumers

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_disable_consumer;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_disable_consumer (
    IN consumer VARCHAR(128)
)
COMMENT '
        Description
        -----------

        Disables consumers within Performance Schema 
        matching the input pattern.

        Parameters
        -----------

        consumer (VARCHAR(128)):
          A LIKE pattern match (using "%consumer%") of consumers to disable

        Example
        -----------

        To disable all consumers:

        mysql> CALL sys.ps_setup_disable_consumer(\'\');
        +--------------------------+
        | summary                  |
        +--------------------------+
        | Disabled 15 consumers    |
        +--------------------------+
        1 row in set (0.02 sec)

        To disable just the event_stage consumers:

        mysql> CALL sys.ps_setup_disable_comsumers(\'stage\');
        +------------------------+
        | summary                |
        +------------------------+
        | Disabled 3 consumers  |
        +------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.setup_consumers
  SET enabled = 'NO'
WHERE name LIKE CONCAT('%', consumer, '%');

SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

Example (see the comment section of the stored procedure, all examples of the use of the stored procedure definition statement with comment in the following will refer to the comment section and will not be described again)

03

ps_setup_disable_instrument()

Disable the specified instruments by modifying the performance_schema.setup_instruments table. The value passed in during the call is used as the value of the name field of the internal update statement of the stored procedure, and the enabled and timed fields are modified to NO, and a number of closed instruments (that is already in the closed state) is returned. Instruments will not count, because it uses the ROW_COUNT() function as the return value, which only records the rows that actually changed)

  • The incoming parameter string uses the form like %in_pattern%; to fuzzy match the name field of the setup_instruments table

parameter:

  • in_pattern VARCHAR(128): match the value of the instrument name, by using the form like %in_pattern%; to fuzzy match the name field of the setup_instrumentss table to perform the UPDATE operation. Note that if the value passed in is a null value, it will match all instruments

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_disable_instrument;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_disable_instrument (
    IN in_pattern VARCHAR(128)
)
COMMENT '
        Description
        -----------

        Disables instruments within Performance Schema 
        matching the input pattern.

        Parameters
        -----------

        in_pattern (VARCHAR(128)):
          A LIKE pattern match (using "%in_pattern%") of events to disable

        Example
        -----------

        To disable all mutex instruments:

        mysql> CALL sys.ps_setup_disable_instrument(\'wait/synch/mutex\');
        +--------------------------+
        | summary                  |
        +--------------------------+
        | Disabled 155 instruments |
        +--------------------------+
        1 row in set (0.02 sec)

        To disable just a specific TCP/IP based network IO instrument:

        mysql> CALL sys.ps_setup_disable_instrument(\'wait/io/socket/sql/server_tcpip_socket\');
        +------------------------+
        | summary                |
        +------------------------+
        | Disabled 1 instruments |
        +------------------------+
        1 row in set (0.00 sec)

        To disable all instruments:

        mysql> CALL sys.ps_setup_disable_instrument(\'\');
        +--------------------------+
        | summary                  |
        +--------------------------+
        | Disabled 547 instruments |
        +--------------------------+
        1 row in set (0.01 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.setup_instruments
  SET enabled = 'NO', timed = 'NO'
WHERE name LIKE CONCAT('%', in_pattern, '%');

SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

04

ps_setup_disable_thread()

Disable the performance event collection function of the specified thread by modifying the performance_schema.threads table. The value passed in when calling is used as the processlist_id field value of the internal statement of the stored procedure (or show processlist; output id field value), and the instrumented field is changed to NO, Returns the number of threads that have been closed (threads that are already in the closed state will not be counted because the ROW_COUNT() function is used as the return value, which only records the rows that have actually changed)

parameter:

  • in_connection_id BIGINT: Connection ID (process ID), which is the value of the PROCESSLIST_ID column of the performance_schema.theads table or the id column output by SHOW PROCESSLIST

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_disable_thread;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_disable_thread (
    IN in_connection_id BIGINT
)
COMMENT '
        Description
        -----------

        Disable the given connection/thread in Performance Schema.

        Parameters
        -----------

        in_connection_id (BIGINT):
          The connection ID (PROCESSLIST_ID from performance_schema.threads
          or the ID shown within SHOW PROCESSLIST)

        Example
        -----------

        mysql> CALL sys.ps_setup_disable_thread(3);
        +-------------------+
        | summary          |
        +-------------------+
        | Disabled 1 thread |
        +-------------------+
        1 row in set (0.01 sec)

        To disable the current connection:

        mysql> CALL sys.ps_setup_disable_thread(CONNECTION_ID());
        +-------------------+
        | summary          |
        +-------------------+
        | Disabled 1 thread |
        +-------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.threads
  SET instrumented = 'NO'
WHERE processlist_id = in_connection_id;

SELECT CONCAT('Disabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

05

ps_setup_enable_background_threads()

Enabling the performance event collection function of all background threads is achieved by modifying the performance_schema.threads table, setting the instrumented field of all background threads to YES, and returning the number of threads with the performance event collection function enabled (threads that are already in the enabled state are not counted) Count, because the ROW_COUNT() function is used as the return value, this function only records the rows that actually changed)

  • No parameters need to be given when the stored procedure is executed

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_enable_background_threads;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_enable_background_threads ()
COMMENT '
        Description
        -----------

        Enable all background thread instrumentation within Performance Schema.

        Parameters
        -----------

        None.

        Example
        -----------

        mysql> CALL sys.ps_setup_enable_background_threads();
        +-------------------------------+
        | summary                      |
        +-------------------------------+
        | Enabled 18 background threads |
        +-------------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.threads
  SET instrumented = 'YES'
WHERE type = 'BACKGROUND';

SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' background thread', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

06

ps_setup_enable_consumer()

Enable specified consumers by modifying the performance_schema.setup_consumers table. The value passed in when calling is used as the value of the name field of the internal statement of the stored procedure, and the enabled field is modified to YES, and the number of enabled consumers is returned (consumers that are already in the enabled state are not counted) Count, because the ROW_COUNT() function is used as the return value, this function only records the rows that actually changed)

  • The incoming parameter string uses the form like %consumer%; to fuzzy match the name field of the setup_consumers table

parameter:

  • consumer VARCHAR(128): Match the value of the consumer name, by using the form like %consumer%; to fuzzy match the name field of the setup_consumers table to perform the UPDATE operation. Note that if the value passed in is a null value, it will match all consumers

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_enable_consumer;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_enable_consumer (
    IN consumer VARCHAR(128)
)
COMMENT '
        Description
        -----------

        Enables consumers within Performance Schema 
        matching the input pattern.

        Parameters
        -----------

        consumer (VARCHAR(128)):
          A LIKE pattern match (using "%consumer%") of consumers to enable

        Example
        -----------

        To enable all consumers:

        mysql> CALL sys.ps_setup_enable_consumer(\'\');
        +-------------------------+
        | summary                |
        +-------------------------+
        | Enabled 10 consumers    |
        +-------------------------+
        1 row in set (0.02 sec)

        Query OK, 0 rows affected (0.02 sec)

        To enable just "waits" consumers:

        mysql> CALL sys.ps_setup_enable_consumer(\'waits\');
        +-----------------------+
        | summary              |
        +-----------------------+
        | Enabled 3 consumers  |
        +-----------------------+
        1 row in set (0.00 sec)

        Query OK, 0 rows affected (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.setup_consumers
  SET enabled = 'YES'
WHERE name LIKE CONCAT('%', consumer, '%');

SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' consumer', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

07

ps_setup_enable_instrument()

The specified instruments are enabled by modifying the performance_schema.setup_instruments table. The value passed in during the call is used as the name field value of the internal statement of the stored procedure, and the enabled and timed fields are modified to YES, and a number of enabled instruments is returned (instruments that are already in the enabled state It will not count, because it uses the ROW_COUNT() function as the return value, which only records the rows that actually changed)

  • The incoming parameter string uses the form like %in_pattern%; to fuzzy match the name field of the setup_instruments table

parameter:

  • in_pattern VARCHAR(128): match the value of the instrument name, by using the form like %in_pattern%; to fuzzy match the name field of the setup_instrumentss table to perform the UPDATE operation. Note that if the value passed in is a null value, it will match all instruments

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_enable_instrument;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_enable_instrument (
    IN in_pattern VARCHAR(128)
)
COMMENT '
        Description
        -----------

        Enables instruments within Performance Schema 
        matching the input pattern.

        Parameters
        -----------

        in_pattern (VARCHAR(128)):
          A LIKE pattern match (using "%in_pattern%") of events to enable

        Example
        -----------

        To enable all mutex instruments:

        mysql> CALL sys.ps_setup_enable_instrument(\'wait/synch/mutex\');
        +-------------------------+
        | summary                |
        +-------------------------+
        | Enabled 155 instruments |
        +-------------------------+
        1 row in set (0.02 sec)

        Query OK, 0 rows affected (0.02 sec)

        To enable just a specific TCP/IP based network IO instrument:

        mysql> CALL sys.ps_setup_enable_instrument(\'wait/io/socket/sql/server_tcpip_socket\');
        +-----------------------+
        | summary              |
        +-----------------------+
        | Enabled 1 instruments |
        +-----------------------+
        1 row in set (0.00 sec)

        Query OK, 0 rows affected (0.00 sec)

        To enable all instruments:

        mysql> CALL sys.ps_setup_enable_instrument(\'\');
        +-------------------------+
        | summary                |
        +-------------------------+
        | Enabled 547 instruments |
        +-------------------------+
        1 row in set (0.01 sec)

        Query OK, 0 rows affected (0.01 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.setup_instruments
  SET enabled = 'YES', timed = 'YES'
WHERE name LIKE CONCAT('%', in_pattern, '%');

SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' instrument', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

08

ps_setup_enable_thread()

The specified thread performance event collection function is enabled by modifying the performance_schema.threads table. The value passed in when calling is used as the processlist_id field value of the internal statement of the stored procedure. The instrumented field is modified to YES, and the number of threads with the performance event collection function enabled ( Threads that are already enabled will not be counted because the ROW_COUNT() function is used as the return value, which only records the rows that actually changed)

parameter:

  • in_connection_id BIGINT: Connection ID (process ID), which is the value of the PROCESSLIST_ID column of the performance_schema.theads table or the id column output by SHOW PROCESSLIST

Stored procedure definition statement text

DROP PROCEDURE IF EXISTS ps_setup_enable_thread;
DELIMITER $$
CREATE DEFINER='root'@'localhost' PROCEDURE ps_setup_enable_thread (
    IN in_connection_id BIGINT
)
COMMENT '
        Description
        -----------

        Enable the given connection/thread in Performance Schema.

        Parameters
        -----------

        in_connection_id (BIGINT):
          The connection ID (PROCESSLIST_ID from performance_schema.threads
          or the ID shown within SHOW PROCESSLIST)

        Example
        -----------

        mysql> CALL sys.ps_setup_enable_thread(3);
        +------------------+
        | summary          |
        +------------------+
        | Enabled 1 thread |
        +------------------+
        1 row in set (0.01 sec)

        To enable the current connection:

        mysql> CALL sys.ps_setup_enable_thread(CONNECTION_ID());
        +------------------+
        | summary          |
        +------------------+
        | Enabled 1 thread |
        +------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
UPDATE performance_schema.threads
  SET instrumented = 'YES'
WHERE processlist_id = in_connection_id;

SELECT CONCAT('Enabled ', @rows := ROW_COUNT(), ' thread', IF(@rows != 1, 's', '')) AS summary;
END$$
DELIMITER ;

09

ps_setup_reload_saved()

When the stored procedure is called, the performance_schema.threads, performance_schema.setup_consumers, performance_schema.setup_instruments, performance_schema.setup_actors configuration information saved in the temporary table when the ps_setup_save() stored procedure was called will be reloaded. The calling of the stored procedure depends on the same session In the configuration backup temporary table created by calling the ps_setup_save() stored procedure before, if the ps_setup_save() stored procedure has not been called before, the stored procedure cannot be executed

  • The execution of this stored procedure requires SUPER permission, because the sql_log_bin system variable will be modified during execution to disable the binary logging function

Example

mysql> CALL sys.ps_setup_save();
Query OK, 0 rows affected (0.08 sec)

mysql> UPDATE performance_schema.setup_instruments SET enabled = 'YES', timed = 'YES';
Query OK, 547 rows affected (0.40 sec)
Rows matched: 784 Changed: 547 Warnings: 0

/* Run some tests that need more detailed instrumentation here */

mysql> CALL sys.ps_setup_reload_saved();
Query OK, 0 rows affected (0.32 sec)

10

ps_setup_reset_to_default()

Reset the performance_schema configuration to the default value. For the performance_schema.setup_actors table, directly empty and reinsert (using delete, INSERT IGNORE INTO statements). For the performance_schema.setup_instruments table, return by querying the sys.ps_is_instrument_default_enabled(NAME) and sys.ps_is_instrument_default_timed(NAME) functions The default value of event_name is used to update the configuration table (using the update statement), for the performance_schema.setup_consumers table directly use IF (NAME IN (``xxx'',...), ``YES'', ``NO'') The function return value updates the configuration table (using the update statement), for the setup_objects table delete ... where not in ('xxx'...) to delete configuration lines other than the default configuration line, and then follow the default configuration line field values Re-update these default configuration lines (using delete, INSERT IGNORE INTO statement), for the threads table, update the INSTRUMENTED field of all threads to YES (using update statement)

parameter:

  • in_verbose BOOLEAN: Whether to display the stage information of each configuration table restoration during the execution of the stored procedure, including the execution of the SQL statement to update the configuration table

Example

mysql> CALL sys.ps_setup_reset_to_default(true)\G
*************************** 1. row ***************************
status: Resetting: setup_actors
DELETE
FROM performance_schema.setup_actors
WHERE NOT (HOST = '%' AND USER = '%' AND ROLE = '%')
1 row in set (0.00 sec)

*************************** 1. row ***************************
status: Resetting: setup_actors
INSERT IGNORE INTO performance_schema.setup_actors
...

mysql> CALL sys.ps_setup_reset_to_default(false)\G
Query OK, 0 rows affected (0.00 sec)

11

ps_setup_save()

Save the current configuration table of performance_schema, create a temporary table with the same structure for threads, setup_actors, setup_consumers, setup_instruments tables under performance_schema, and copy the current configuration data to the temporary table through insert...select... The sql_log_bin parameter is turned off to prevent the operation from being written to the binlog (SUPER permission is required). After the operation, change it back to the default value. Restore the configuration by calling the sys.ps_setup_reload_saved() stored procedure. For the sys.ps_setup_reload_saved() stored procedure, see sys. ps_setup_reload_saved() explanation part

  • In order to prevent other sessions from simultaneously calling the stored procedure to perform the save configuration operation, the sys.ps_setup_save() stored procedure internally obtains an advisory lock named "sys.ps_setup_save" by calling the GET_LOCK() function to prevent other processes from executing sys.ps_setup_save( ) Stored procedure. The sys.ps_setup_save() stored procedure accepts a timeout parameter, which is used by the GET_LOCK() function to obtain the timeout time of the advisory lock named "sys.ps_setup_save" (if the advisory lock named "sys.ps_setup_save" already exists, it will Wait for the number of seconds specified by the timeout parameter). If other sessions have not released the advisory lock after the number of seconds specified by the timeout parameter, the GET_LOCK() function returns 0, otherwise it returns 1 (the function is captured in the ps_setup_save() stored procedure If the return value is judged, the operation of saving the configuration will be continued with a return value of 1, and an error will be reported if a return value of 0:'Could not lock the sys.ps_setup_save user lock, another thread has a saved configuration')

  • The sys.ps_setup_reload_saved() stored procedure and the sys.ps_setup_save() stored procedure need to be executed in the same session, because the configuration is saved in the TEMPORARY table. In addition, if the sys.ps_setup_save() stored procedure is executed, sys is not called. The ps_setup_reload_saved() stored procedure restores the configuration and directly disconnects the session. The temporary table created by the sys.ps_setup_save() stored procedure and the advisory lock acquired will be automatically deleted

parameter:

  • in_timeout INT: the timeout seconds to wait for acquiring the sys.ps_setup_save lock, note: if the value is negative, it will cause infinite waiting

Example

mysql> CALL sys.ps_setup_save(1);
Query OK, 0 rows affected (0.08 sec)

mysql> UPDATE performance_schema.setup_instruments 
-> SET enabled = 'YES', timed = 'YES';
Query OK, 547 rows affected (0.40 sec)
Rows matched: 784 Changed: 547 Warnings: 0

/* Run some tests that need more detailed instrumentation here */

mysql> CALL sys.ps_setup_reload_saved();
Query OK, 0 rows affected (0.32 sec)

The content of this issue is introduced here, and the reference link for the content of this issue is as follows:

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-save.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-disable-consumer.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-disable-instrument.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-disable-thread.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-enable-background-threads.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-enable-consumer.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-enable-instrument.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-enable-thread.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-reload-saved.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-reset-to-default.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-setup-disable-background-threads.html

| About the author

Luo Xiaobo·Database Technology Expert

One of the authors of "A Thousand Gold Recipes-MySQL Performance Optimization Pyramid Rule", "Data Ecology: MySQL Replication Technology and Production Practice". Familiar with MySQL architecture, good at overall database tuning, like to specialize in open source technology, and keen on the promotion of open source technology, have done many public database topic sharing online and offline, and published nearly 100 database-related research articles.

The full text is over.

Enjoy MySQL :)

Teacher Ye's "MySQL Core Optimization" class has been upgraded to MySQL 8.0, scan the code to start the journey of MySQL 8.0 practice

Guess you like

Origin blog.csdn.net/n88Lpo/article/details/110411934