Understanding the sys.ps_is_thread_instrumented() Function in MySQL

In MySQL, we can use the sys.ps_is_thread_instrumented() function to check whether Performance Schema instrumentation for a given connection ID is enabled.

The function returns YES or NO, depending on whether the instrumentation is enabled for the given connection. It can also return UNKNOWN if the connection ID is unknown, and NULL if the connection ID is NULL.

Syntax

The syntax goes like this:

sys.ps_is_thread_instrumented(in_connection_id)

Where in_connection_id is the connection ID to check.

Example

Here’s an example to demonstrate:

SELECT sys.ps_is_thread_instrumented( 80 );

Result:

YES

This shows that instrumentation for connection ID 80 is enabled.

Check the Current Connection

We can use the connection_id() function to check the current connection:

SELECT sys.ps_is_thread_instrumented( connection_id() );

Result:

YES

This shows that instrumentation for the current connection is enabled.

Here’s the current connection ID:

SELECT connection_id();

Result:

79

Non-Existent Connection ID

If the connection ID is unknown (or doesn’t exist), sys.ps_is_thread_instrumented() returns UNKNOWN:

SELECT sys.ps_is_thread_instrumented( 81 );

Result:

UNKNOWN

NULL Connection ID

If the argument is NULL, then NULL is returned:

SELECT sys.ps_is_thread_instrumented( NULL );

Result:

NULL

Passing the Wrong Number of Arguments

Passing the wrong number of arguments results in an error:

SELECT sys.ps_is_thread_instrumented( 79, 80 );

Result:

ERROR 1318 (42000): Incorrect number of arguments for FUNCTION sys.ps_is_thread_instrumented; expected 1, got 2

The same applies when we don’t pass any arguments:

SELECT sys.ps_is_thread_instrumented( );

Result:

ERROR 1318 (42000): Incorrect number of arguments for FUNCTION sys.ps_is_thread_instrumented; expected 1, got 0