Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mysql-test/main/mysqld--help.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ perl;
wsrep file-key-management cracklib-password-check user-variables
provider-bzip2 provider-lzma provider-lzo caching-sha2-password
thread-pool-groups thread-pool-queues thread-pool-stats
thread-pool-waits hashicorp provider gssapi parsec videx/;
thread-pool-waits hashicorp provider gssapi parsec videx
ps-thread-id ps-current-thread-id/;

# And substitute the content some environment variables with their
# names:
Expand Down
7 changes: 5 additions & 2 deletions mysql-test/mariadb-test-run.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1846,15 +1846,18 @@ sub collect_mysqld_features {
# Enable or disable ARCHIVE plugin. Possible values are ON, OFF,
# FORCE (don't start if the plugin fails to load),
# FORCE_PLUS_PERMANENT (like FORCE, but the plugin can not be uninstalled).
# For Innodb I_S plugins that are referenced in sys schema
# do not make them optional, to prevent diffs in tests.
# For Innodb I_S plugins and the performance schema functions
# that are referenced in sys schema do not make them optional,
# to prevent diffs in tests.
push @optional_plugins, $1
if /^ --([-a-z0-9]+)\[=name\] +Enable or disable \w+ plugin. One of: ON, OFF, FORCE/
and $1 ne "innodb-metrics"
and $1 ne "innodb-buffer-page"
and $1 ne "innodb-lock-waits"
and $1 ne "innodb-locks"
and $1 ne "innodb-trx"
and $1 ne "ps-thread-id"
and $1 ne "ps-current-thread-id"
and $1 ne "gssapi";
next;
}
Expand Down
95 changes: 95 additions & 0 deletions mysql-test/suite/perfschema/r/native_func_thread_id.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# MDEV-19629 Implement MySQL 8.0 native functions:
# PS_THREAD_ID() and PS_CURRENT_THREAD_ID()
#
# Function plugins of the performance schema library
SELECT PLUGIN_NAME, PLUGIN_STATUS, PLUGIN_TYPE, PLUGIN_LIBRARY
FROM information_schema.PLUGINS
WHERE PLUGIN_NAME IN ('ps_thread_id', 'ps_current_thread_id')
ORDER BY PLUGIN_NAME;
PLUGIN_NAME PLUGIN_STATUS PLUGIN_TYPE PLUGIN_LIBRARY
ps_current_thread_id ACTIVE FUNCTION NULL
ps_thread_id ACTIVE FUNCTION NULL
SELECT PS_CURRENT_THREAD_ID() = @tid;
PS_CURRENT_THREAD_ID() = @tid
1
SELECT PS_THREAD_ID(CONNECTION_ID()) = @tid;
PS_THREAD_ID(CONNECTION_ID()) = @tid
1
SELECT PS_THREAD_ID(CONNECTION_ID()) = PS_CURRENT_THREAD_ID();
PS_THREAD_ID(CONNECTION_ID()) = PS_CURRENT_THREAD_ID()
1
# Another connection
connect con1,localhost,root,,;
connection default;
con1_matches
1
# Same answer as performance_schema.threads for every connection
SELECT COUNT(*) >= 2 AS have_two,
COUNT(*) = SUM(PS_THREAD_ID(PROCESSLIST_ID) <=> THREAD_ID) AS all_match
FROM performance_schema.threads
WHERE PROCESSLIST_ID IS NOT NULL;
have_two all_match
1 1
disconnect con1;
gone_after_disconnect
1
# NULL for unknown, NULL, zero, negative and non-integer arguments
SELECT PS_THREAD_ID(234623462376), PS_THREAD_ID(NULL), PS_THREAD_ID(0),
PS_THREAD_ID(-1), PS_THREAD_ID(1.9), PS_THREAD_ID(18446744073709551615);
PS_THREAD_ID(234623462376) PS_THREAD_ID(NULL) PS_THREAD_ID(0) PS_THREAD_ID(-1) PS_THREAD_ID(1.9) PS_THREAD_ID(18446744073709551615)
NULL NULL NULL NULL NULL NULL
SELECT PS_THREAD_ID('abc');
PS_THREAD_ID('abc')
NULL
Warnings:
Warning 1292 Truncated incorrect INTEGER value: 'abc'
# Prepared statements
PREPARE s FROM 'SELECT PS_THREAD_ID(?) = PS_CURRENT_THREAD_ID()';
SET @cid= CONNECTION_ID();
EXECUTE s USING @cid;
PS_THREAD_ID(?) = PS_CURRENT_THREAD_ID()
1
EXECUTE s USING NULL;
PS_THREAD_ID(?) = PS_CURRENT_THREAD_ID()
NULL
DEALLOCATE PREPARE s;
# Result type
CREATE TABLE t1 AS SELECT PS_THREAD_ID(1) AS a, PS_CURRENT_THREAD_ID() AS b;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` bigint(21) unsigned DEFAULT NULL,
`b` bigint(21) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
# Wrong argument count
SELECT PS_THREAD_ID();
ERROR 42000: Incorrect parameter count in the call to native function 'PS_THREAD_ID'
SELECT PS_THREAD_ID(1, 2);
ERROR 42000: Incorrect parameter count in the call to native function 'PS_THREAD_ID'
SELECT PS_CURRENT_THREAD_ID(1);
ERROR 42000: Incorrect parameter count in the call to native function 'PS_CURRENT_THREAD_ID'
# Allowed in VIRTUAL columns, not in PERSISTENT ones (like CONNECTION_ID())
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_THREAD_ID(a)) VIRTUAL,
c BIGINT UNSIGNED AS (PS_CURRENT_THREAD_ID()) VIRTUAL);
DROP TABLE t1;
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_THREAD_ID(a)) PERSISTENT);
ERROR HY000: Function or expression 'ps_thread_id()' cannot be used in the GENERATED ALWAYS AS clause of `b`
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_CURRENT_THREAD_ID()) PERSISTENT);
ERROR HY000: Function or expression 'ps_current_thread_id()' cannot be used in the GENERATED ALWAYS AS clause of `b`
# sys.ps_thread_id() now uses the native functions
SELECT sys.ps_thread_id(NULL) = PS_CURRENT_THREAD_ID();
sys.ps_thread_id(NULL) = PS_CURRENT_THREAD_ID()
1
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
SELECT sys.ps_thread_id(CONNECTION_ID()) = PS_CURRENT_THREAD_ID();
sys.ps_thread_id(CONNECTION_ID()) = PS_CURRENT_THREAD_ID()
1
SELECT sys.ps_thread_id(234623462376);
sys.ps_thread_id(234623462376)
NULL
#
# End of 13.2 tests
#
22 changes: 22 additions & 0 deletions mysql-test/suite/perfschema/r/native_func_thread_id_off.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# MDEV-19629 PS_THREAD_ID() and PS_CURRENT_THREAD_ID() with the
# Performance Schema disabled
#
SELECT @@performance_schema;
@@performance_schema
0
SELECT PS_CURRENT_THREAD_ID();
ERROR HY000: 'ps_current_thread_id': The Performance Schema is not enabled.
SELECT PS_THREAD_ID(CONNECTION_ID());
ERROR HY000: 'ps_thread_id': The Performance Schema is not enabled.
SELECT PS_THREAD_ID(NULL);
ERROR HY000: 'ps_thread_id': The Performance Schema is not enabled.
# sys.ps_thread_id() keeps returning NULL
SELECT sys.ps_thread_id(NULL), sys.ps_thread_id(CONNECTION_ID());
sys.ps_thread_id(NULL) sys.ps_thread_id(CONNECTION_ID())
NULL NULL
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
#
# End of 13.2 tests
#
97 changes: 97 additions & 0 deletions mysql-test/suite/perfschema/t/native_func_thread_id.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
--source include/not_embedded.inc
--source include/have_perfschema.inc

--echo #
--echo # MDEV-19629 Implement MySQL 8.0 native functions:
--echo # PS_THREAD_ID() and PS_CURRENT_THREAD_ID()
--echo #

--echo # Function plugins of the performance schema library
SELECT PLUGIN_NAME, PLUGIN_STATUS, PLUGIN_TYPE, PLUGIN_LIBRARY
FROM information_schema.PLUGINS
WHERE PLUGIN_NAME IN ('ps_thread_id', 'ps_current_thread_id')
ORDER BY PLUGIN_NAME;

let $tid= `SELECT THREAD_ID FROM performance_schema.threads
WHERE PROCESSLIST_ID = CONNECTION_ID()`;
--disable_query_log
eval SET @tid= $tid;
--enable_query_log

SELECT PS_CURRENT_THREAD_ID() = @tid;
SELECT PS_THREAD_ID(CONNECTION_ID()) = @tid;
SELECT PS_THREAD_ID(CONNECTION_ID()) = PS_CURRENT_THREAD_ID();

--echo # Another connection
connect (con1,localhost,root,,);
let $con1_id= `SELECT CONNECTION_ID()`;
let $con1_tid= `SELECT PS_CURRENT_THREAD_ID()`;
connection default;
--disable_query_log
eval SELECT PS_THREAD_ID($con1_id) = $con1_tid AS con1_matches;
--enable_query_log

--echo # Same answer as performance_schema.threads for every connection
SELECT COUNT(*) >= 2 AS have_two,
COUNT(*) = SUM(PS_THREAD_ID(PROCESSLIST_ID) <=> THREAD_ID) AS all_match
FROM performance_schema.threads
WHERE PROCESSLIST_ID IS NOT NULL;

disconnect con1;
--disable_query_log
let $wait_condition= SELECT COUNT(*) = 0 FROM information_schema.processlist
WHERE id = $con1_id;
--source include/wait_condition.inc
--disable_query_log
eval SELECT PS_THREAD_ID($con1_id) IS NULL AS gone_after_disconnect;
--enable_query_log

--echo # NULL for unknown, NULL, zero, negative and non-integer arguments
SELECT PS_THREAD_ID(234623462376), PS_THREAD_ID(NULL), PS_THREAD_ID(0),
PS_THREAD_ID(-1), PS_THREAD_ID(1.9), PS_THREAD_ID(18446744073709551615);
SELECT PS_THREAD_ID('abc');

--echo # Prepared statements
PREPARE s FROM 'SELECT PS_THREAD_ID(?) = PS_CURRENT_THREAD_ID()';
SET @cid= CONNECTION_ID();
EXECUTE s USING @cid;
EXECUTE s USING NULL;
DEALLOCATE PREPARE s;

--echo # Result type
CREATE TABLE t1 AS SELECT PS_THREAD_ID(1) AS a, PS_CURRENT_THREAD_ID() AS b;
SHOW CREATE TABLE t1;
DROP TABLE t1;

--echo # Wrong argument count
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT PS_THREAD_ID();
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT PS_THREAD_ID(1, 2);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT PS_CURRENT_THREAD_ID(1);

--echo # Allowed in VIRTUAL columns, not in PERSISTENT ones (like CONNECTION_ID())
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_THREAD_ID(a)) VIRTUAL,
c BIGINT UNSIGNED AS (PS_CURRENT_THREAD_ID()) VIRTUAL);
DROP TABLE t1;
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_THREAD_ID(a)) PERSISTENT);
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
CREATE TABLE t1 (a INT, b BIGINT UNSIGNED AS (PS_CURRENT_THREAD_ID()) PERSISTENT);

--echo # sys.ps_thread_id() now uses the native functions
# Various protocols display ER_NATIVE_FCT_NAME_COLLISION very differently.
--disable_ps_protocol
--disable_cursor_protocol
--disable_view_protocol
SELECT sys.ps_thread_id(NULL) = PS_CURRENT_THREAD_ID();
SELECT sys.ps_thread_id(CONNECTION_ID()) = PS_CURRENT_THREAD_ID();
SELECT sys.ps_thread_id(234623462376);
--enable_view_protocol
--enable_cursor_protocol
--enable_ps_protocol

--echo #
--echo # End of 13.2 tests
--echo #
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--loose-performance-schema=OFF
30 changes: 30 additions & 0 deletions mysql-test/suite/perfschema/t/native_func_thread_id_off.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--source include/not_embedded.inc
--source include/have_perfschema.inc

--echo #
--echo # MDEV-19629 PS_THREAD_ID() and PS_CURRENT_THREAD_ID() with the
--echo # Performance Schema disabled
--echo #

SELECT @@performance_schema;

--error ER_WRONG_PERFSCHEMA_USAGE
SELECT PS_CURRENT_THREAD_ID();
--error ER_WRONG_PERFSCHEMA_USAGE
SELECT PS_THREAD_ID(CONNECTION_ID());
--error ER_WRONG_PERFSCHEMA_USAGE
SELECT PS_THREAD_ID(NULL);

--echo # sys.ps_thread_id() keeps returning NULL
# Various protocols display ER_NATIVE_FCT_NAME_COLLISION very differently.
--disable_ps_protocol
--disable_cursor_protocol
--disable_view_protocol
SELECT sys.ps_thread_id(NULL), sys.ps_thread_id(CONNECTION_ID());
--enable_view_protocol
--enable_cursor_protocol
--enable_ps_protocol

--echo #
--echo # End of 13.2 tests
--echo #
2 changes: 2 additions & 0 deletions mysql-test/suite/sysschema/r/fn_ps_thread_account.result
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ NULL
SELECT sys.ps_thread_account(sys.ps_thread_id(NULL));
sys.ps_thread_account(sys.ps_thread_id(NULL))
root@localhost
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
2 changes: 2 additions & 0 deletions mysql-test/suite/sysschema/r/fn_ps_thread_id.result
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SELECT sys.ps_thread_id(234623462376);
sys.ps_thread_id(234623462376)
NULL
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
SELECT sys.ps_thread_id(CONNECTION_ID()) = @ps_thread_id;
sys.ps_thread_id(CONNECTION_ID()) = @ps_thread_id
1
Expand Down
2 changes: 2 additions & 0 deletions mysql-test/suite/sysschema/r/fn_ps_thread_trx_info.result
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NULL
SELECT JSON_VALID(sys.ps_thread_trx_info(sys.ps_thread_id(NULL)));
JSON_VALID(sys.ps_thread_trx_info(sys.ps_thread_id(NULL)))
1
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
CREATE DATABASE trx;
CREATE TABLE trx.info (id INT PRIMARY KEY, info VARCHAR(20));
connect con1,localhost,root,,;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use test;
SET @threadid = sys.ps_thread_id(NULL);
Warnings:
Note 1585 This function 'ps_thread_id' has the same name as a native function
CREATE TABLE t1 (id INT PRIMARY KEY, val int) ENGINE=MEMORY;
INSERT INTO test.t1 VALUES (1, 9);
SET @digest.insert = (SELECT DIGEST FROM performance_schema.events_statements_history WHERE THREAD_ID = @threadid AND SQL_TEXT LIKE 'INSERT INTO test.t1 VALUES (1, 9)');
Expand Down
7 changes: 7 additions & 0 deletions mysql-test/suite/sysschema/t/fn_ps_thread_account.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ SELECT sys.ps_thread_account(234623462376);


# Check result
# Various protocols display ER_NATIVE_FCT_NAME_COLLISION very differently.
--disable_ps_protocol
--disable_cursor_protocol
--disable_view_protocol
SELECT sys.ps_thread_account(sys.ps_thread_id(NULL));
--enable_view_protocol
--enable_cursor_protocol
--enable_ps_protocol
11 changes: 11 additions & 0 deletions mysql-test/suite/sysschema/t/fn_ps_thread_id.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
-- source include/not_embedded.inc

# Various protocols display ER_NATIVE_FCT_NAME_COLLISION very differently.
# Disable all of them.

--disable_ps_protocol
--disable_cursor_protocol
--disable_view_protocol
# Tests for sys schema
# Verify the sys.ps_thread_id() function perfoms as expected

Expand All @@ -16,3 +23,7 @@ SELECT sys.ps_thread_id(CONNECTION_ID()) = @ps_thread_id;

# Passing NULL should also return the current connection thread id
SELECT sys.ps_thread_id(NULL) = @ps_thread_id;

--enable_view_protocol
--enable_cursor_protocol
--enable_ps_protocol
12 changes: 8 additions & 4 deletions scripts/sys_schema/functions/ps_thread_id.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ CREATE OR REPLACE DEFINER='mariadb.sys'@'localhost' FUNCTION ps_thread_id (
NOT DETERMINISTIC
READS SQL DATA
BEGIN
RETURN (SELECT THREAD_ID
FROM `performance_schema`.`threads`
WHERE PROCESSLIST_ID = IFNULL(in_connection_id, CONNECTION_ID())
);
-- The native functions raise an error when the Performance Schema
-- is disabled; keep returning NULL, as this function always did.
IF NOT @@performance_schema THEN
RETURN NULL;
END IF;
RETURN IF(in_connection_id IS NULL,
PS_CURRENT_THREAD_ID(),
PS_THREAD_ID(in_connection_id));
END$$

DELIMITER ;
2 changes: 2 additions & 0 deletions storage/perfschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pfs_instr.h
pfs_instr_class.h
pfs_lock.h
pfs_memory.h
pfs_native_func.h
pfs_server.h
pfs_setup_actor.h
pfs_setup_object.h
Expand Down Expand Up @@ -187,6 +188,7 @@ pfs_host.cc
pfs_instr.cc
pfs_instr_class.cc
pfs_memory.cc
pfs_native_func.cc
pfs_server.cc
pfs_setup_actor.cc
pfs_setup_object.cc
Expand Down
Loading