Skip to content

ps_thread_id()

xiaoboluo768 edited this page Jun 9, 2020 · 2 revisions
  • 在threads表中查询给定连接ID(processlist_id)的内部线程ID,如果给定连接ID为NULL值,则返回当前连接的内部线程ID

  • 参数:

    • in_connection_id BIGINT UNSIGNED:要返回内部线程ID的连接的ID。 对应performance_schema.threads表中的PROCESSLIST_ID列或SHOW PROCESSLIST输出的Id列值
  • 返回值:一个BIGINT UNSIGNED值

  • 定义语句

DROP FUNCTION IF EXISTS ps_thread_id;

DELIMITER $$

CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_id (
        in_connection_id BIGINT UNSIGNED
    ) RETURNS BIGINT UNSIGNED
    COMMENT '
            Description
            -----------

            Return the Performance Schema THREAD_ID for the specified connection ID.

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

            in_connection_id (BIGINT UNSIGNED):
              The id of the connection to return the thread id for. If NULL, the current
              connection thread id is returned.

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

            mysql> SELECT sys.ps_thread_id(79);
            +----------------------+
            | sys.ps_thread_id(79) |
            +----------------------+
            |                  98 |
            +----------------------+
            1 row in set (0.00 sec)

            mysql> SELECT sys.ps_thread_id(CONNECTION_ID());
            +-----------------------------------+
            | sys.ps_thread_id(CONNECTION_ID()) |
            +-----------------------------------+
            |                                98 |
            +-----------------------------------+
            1 row in set (0.00 sec)
            '

    SQL SECURITY INVOKER
    NOT DETERMINISTIC
    READS SQL DATA
BEGIN
    RETURN (SELECT THREAD_ID
              FROM `performance_schema`.`threads`
            WHERE PROCESSLIST_ID = IFNULL(in_connection_id, CONNECTION_ID())
          );
END$$

DELIMITER ;

上一篇: ps_thread_account()函数 | 下一篇: ps_thread_stack()函数

Clone this wiki locally