Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Improve how truncation is controlled within the ps_thread_trx_info fu…
Browse files Browse the repository at this point in the history
…nction, by making the group_concat_max_len variable configurable via the sys_config table and a new ps_thread_trx_info.max_length variable. When truncation does happen, instead of returning NULL and ignoring the warnings, return an error JSON object instead.
  • Loading branch information
MarkLeith committed Aug 19, 2015
1 parent dd75178 commit d2cb0c5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 13 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ Note, when functions check for configuration options, they first check whether a

##### Options included

| Variable | Default Value | Description |
| ---------------------- | ------------- | ------------------------------------------------------------------------------ |
| statement_truncate_len | 64 | Sets the size to truncate statements to, for the `format_statement()` function |
| Variable | Default Value | Description |
| ------------------------------------ | ------------- | ------------------------------------------------------------------------------ |
| statement_truncate_len | 64 | Sets the size to truncate statements to, for the `format_statement()` function. |
| statement_performance_analyzer.limit | 100 | The maximum number of rows to include for the views that does not have a built-in limit (e.g. the 95th percentile view). If not set the limit is 100. |
| statement_performance_analyzer.view | NULL | Used together with the 'custom' view. If the value contains a space, it is considered a query, otherwise it must be
an existing view querying the performance_schema.events_statements_summary_by_digest table. |
| diagnostics.allow_i_s_tables | OFF | Specifies whether it is allowed to do table scan queries on information_schema.TABLES for the `diagnostics` procedure. |
| diagnostics.include_raw | OFF | Set to 'ON' to include the raw data (e.g. the original output of "SELECT * FROM sys.metrics") for the `diagnostics` procedure.|
| ps_thread_trx_info.max_length | 65535 | Sets the maximum output length for JSON object output by the `ps_thread_trx_info()` function. |

### Views

Expand Down Expand Up @@ -3948,6 +3954,14 @@ thread_stack: {"rankdir": "LR","nodesep": "0.10","stack_created": "2014-02-19 13

Returns a JSON object with info on the given thread's current transaction, and the statements it has already executed, derived from the `performance_schema.events_transactions_current` and `performance_schema.events_statements_history` tables (so the consumers for these also have to be enabled within Performance Schema to get full data in the object).
When the output exceeds the default truncation length (65535), a JSON error object is returned, such as:
`{ "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }`
Similar error objects are returned for other warnings/and exceptions raised when calling the function.
The max length of the output of this function can be controlled with the `ps_thread_trx_info.max_length` variable set via `sys_config`, or the `@sys.ps_thread_trx_info.max_length` user variable, as appropriate.
##### Parameters
* in_thread_id (BIGINT UNSIGNED): The id of the thread to return the transaction info for.
Expand Down
54 changes: 48 additions & 6 deletions functions/ps_thread_trx_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_trx_info (
for these also have to be enabled within Performance Schema to get full
data in the object).
When the output exceeds the default truncation length (65535), a JSON error
object is returned, such as:
{ "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }
Similar error objects are returned for other warnings/and exceptions raised
when calling the function.
The max length of the output of this function can be controlled with the
ps_thread_trx_info.max_length variable set via sys_config, or the
@sys.ps_thread_trx_info.max_length user variable, as appropriate.
Parameters
-----------
Expand Down Expand Up @@ -119,10 +131,39 @@ CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_trx_info (
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE v_output TEXT;
DECLARE v_output LONGTEXT DEFAULT '{}';
DECLARE v_msg_text TEXT DEFAULT '';
DECLARE v_signal_msg TEXT DEFAULT '';
DECLARE v_mysql_errno INT;
DECLARE v_max_output_len BIGINT;
-- Capture warnings/errors such as group_concat truncation
-- and report as JSON error objects
DECLARE EXIT HANDLER FOR SQLWARNING, SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
v_msg_text = MESSAGE_TEXT,
v_mysql_errno = MYSQL_ERRNO;

IF v_mysql_errno = 1260 THEN
SET v_signal_msg = CONCAT('{ "error": "Trx info truncated: ', v_msg_text, '" }');
ELSE
SET v_signal_msg = CONCAT('{ "error": "', v_msg_text, '" }');
END IF;

RETURN v_signal_msg;
END;

-- Set configuration options
IF (@sys.ps_thread_trx_info.max_length IS NULL) THEN
SET @sys.ps_thread_trx_info.max_length = sys.sys_get_config('ps_thread_trx_info.max_length', 65535);
END IF;

SET @old_ground_concat_max_len = @@session.group_concat_max_len;
SET SESSION group_concat_max_len = 1000000000;
IF (@sys.ps_thread_trx_info.max_length != @@session.group_concat_max_len) THEN
SET @old_group_concat_max_len = @@session.group_concat_max_len;
-- Convert to int value for the SET, and give some surrounding space
SET v_max_output_len = (@sys.ps_thread_trx_info.max_length - 5);
SET SESSION group_concat_max_len = v_max_output_len;
END IF;

SET v_output = (
SELECT CONCAT('[', IFNULL(GROUP_CONCAT(trx_info ORDER BY event_id), ''), '\n]') AS trx_info
Expand Down Expand Up @@ -158,7 +199,7 @@ BEGIN
GROUP_CONCAT(
IFNULL(
CONCAT('\n {\n',
' "sql_text": "', IFNULL(REPLACE(sql_text, '\\', '\\\\'), ''), '",\n',
' "sql_text": "', IFNULL(sys.format_statement(REPLACE(sql_text, '\\', '\\\\')), ''), '",\n',
' "time": "', IFNULL(sys.format_time(timer_wait), ''), '",\n',
' "schema": "', IFNULL(current_schema, ''), '",\n',
' "rows_examined": ', IFNULL(rows_examined, ''), ',\n',
Expand All @@ -182,8 +223,9 @@ BEGIN
GROUP BY thread_id
);

SET @old_ground_concat_max_len = @@session.group_concat_max_len;
SET SESSION group_concat_max_len = 1000000000;
IF (@old_group_concat_max_len IS NOT NULL) THEN
SET SESSION group_concat_max_len = @old_group_concat_max_len;
END IF;

RETURN v_output;
END$$
Expand Down
3 changes: 2 additions & 1 deletion mysql-test/suite/sysschema/include/sys_config_cleanup.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ INSERT IGNORE INTO sys.sys_config (variable, value) VALUES
('statement_performance_analyzer.limit', 100),
('statement_performance_analyzer.view', NULL),
('diagnostics.allow_i_s_tables', 'OFF'),
('diagnostics.include_raw', 'OFF');
('diagnostics.include_raw', 'OFF'),
('ps_thread_trx_info.max_length', 65535);

SET @sys.ignore_sys_config_triggers := NULL;
10 changes: 9 additions & 1 deletion mysql-test/suite/sysschema/r/fn_ps_thread_trx_info.result
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sys.ps_thread_trx_info(234623462376)
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)))
NULL
1
CREATE DATABASE trx;
CREATE TABLE trx.info (id INT PRIMARY KEY, info VARCHAR(20));
USE trx;
Expand Down Expand Up @@ -77,4 +77,12 @@ JSON_CONTAINS(@json_doc, '0', '$[0].stateme
SELECT JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text');
JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text')
1
SET @sys.ps_thread_trx_info.max_length = 100;
SELECT sys.ps_thread_trx_info(@ps_thread_id);
sys.ps_thread_trx_info(@ps_thread_id)
{ "error": "Trx info truncated: Row 12 was cut by GROUP_CONCAT()" }
SET @sys.ps_thread_trx_info.max_length = NULL;
SELECT JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id));
JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id))
1
DROP DATABASE trx;
15 changes: 14 additions & 1 deletion mysql-test/suite/sysschema/t/fn_ps_thread_trx_info.test
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ SELECT JSON_CONTAINS(@json_doc, '0', '$[0].
# Second statement in transaction should be a COMMIT
SELECT JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text');

disconnect con1;
# Simulate a truncated set of output by lowering the @sys.ps_thread_trx_info.max_length user variable
# This also tests the user variable works appropriately, incidentally

SET @sys.ps_thread_trx_info.max_length = 100;

# Should return an error JSON object
SELECT sys.ps_thread_trx_info(@ps_thread_id);

# Setting the user variable back to NULL should reset to 65535 from sys_config, and no truncation
SET @sys.ps_thread_trx_info.max_length = NULL;
SELECT JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id));

# Clean up

disconnect con1;
DROP DATABASE trx;
2 changes: 1 addition & 1 deletion sys_57.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
SOURCE ./before_setup.sql

SOURCE ./tables/sys_config.sql
SOURCE ./tables/sys_config_data.sql
SOURCE ./tables/sys_config_data_57.sql

SOURCE ./triggers/sys_config_insert_set_user.sql
SOURCE ./triggers/sys_config_update_set_user.sql
Expand Down
24 changes: 24 additions & 0 deletions tables/sys_config_data_57.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; version 2 of the License.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

-- NOTE: This needs to be replicated within the sys_config_clean.inc file

INSERT IGNORE INTO sys.sys_config (variable, value) VALUES
('statement_truncate_len', 64),
('statement_performance_analyzer.limit', 100),
('statement_performance_analyzer.view', NULL),
('diagnostics.allow_i_s_tables', 'OFF'),
('diagnostics.include_raw', 'OFF'),
('ps_thread_trx_info.max_length', 65535);

0 comments on commit d2cb0c5

Please sign in to comment.