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

Commit d2cb0c5

Browse files
committed
Improve how truncation is controlled within the ps_thread_trx_info function, 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.
1 parent dd75178 commit d2cb0c5

File tree

7 files changed

+115
-13
lines changed

7 files changed

+115
-13
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ Note, when functions check for configuration options, they first check whether a
7979

8080
##### Options included
8181

82-
| Variable | Default Value | Description |
83-
| ---------------------- | ------------- | ------------------------------------------------------------------------------ |
84-
| statement_truncate_len | 64 | Sets the size to truncate statements to, for the `format_statement()` function |
82+
| Variable | Default Value | Description |
83+
| ------------------------------------ | ------------- | ------------------------------------------------------------------------------ |
84+
| statement_truncate_len | 64 | Sets the size to truncate statements to, for the `format_statement()` function. |
85+
| 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. |
86+
| 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
87+
an existing view querying the performance_schema.events_statements_summary_by_digest table. |
88+
| diagnostics.allow_i_s_tables | OFF | Specifies whether it is allowed to do table scan queries on information_schema.TABLES for the `diagnostics` procedure. |
89+
| 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.|
90+
| ps_thread_trx_info.max_length | 65535 | Sets the maximum output length for JSON object output by the `ps_thread_trx_info()` function. |
8591

8692
### Views
8793

@@ -3948,6 +3954,14 @@ thread_stack: {"rankdir": "LR","nodesep": "0.10","stack_created": "2014-02-19 13
39483954
39493955
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).
39503956
3957+
When the output exceeds the default truncation length (65535), a JSON error object is returned, such as:
3958+
3959+
`{ "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }`
3960+
3961+
Similar error objects are returned for other warnings/and exceptions raised when calling the function.
3962+
3963+
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.
3964+
39513965
##### Parameters
39523966
39533967
* in_thread_id (BIGINT UNSIGNED): The id of the thread to return the transaction info for.

functions/ps_thread_trx_info.sql

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_trx_info (
3131
for these also have to be enabled within Performance Schema to get full
3232
data in the object).
3333
34+
When the output exceeds the default truncation length (65535), a JSON error
35+
object is returned, such as:
36+
37+
{ "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }
38+
39+
Similar error objects are returned for other warnings/and exceptions raised
40+
when calling the function.
41+
42+
The max length of the output of this function can be controlled with the
43+
ps_thread_trx_info.max_length variable set via sys_config, or the
44+
@sys.ps_thread_trx_info.max_length user variable, as appropriate.
45+
3446
Parameters
3547
-----------
3648
@@ -119,10 +131,39 @@ CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_trx_info (
119131
NOT DETERMINISTIC
120132
READS SQL DATA
121133
BEGIN
122-
DECLARE v_output TEXT;
134+
DECLARE v_output LONGTEXT DEFAULT '{}';
135+
DECLARE v_msg_text TEXT DEFAULT '';
136+
DECLARE v_signal_msg TEXT DEFAULT '';
137+
DECLARE v_mysql_errno INT;
138+
DECLARE v_max_output_len BIGINT;
139+
-- Capture warnings/errors such as group_concat truncation
140+
-- and report as JSON error objects
141+
DECLARE EXIT HANDLER FOR SQLWARNING, SQLEXCEPTION
142+
BEGIN
143+
GET DIAGNOSTICS CONDITION 1
144+
v_msg_text = MESSAGE_TEXT,
145+
v_mysql_errno = MYSQL_ERRNO;
146+
147+
IF v_mysql_errno = 1260 THEN
148+
SET v_signal_msg = CONCAT('{ "error": "Trx info truncated: ', v_msg_text, '" }');
149+
ELSE
150+
SET v_signal_msg = CONCAT('{ "error": "', v_msg_text, '" }');
151+
END IF;
152+
153+
RETURN v_signal_msg;
154+
END;
155+
156+
-- Set configuration options
157+
IF (@sys.ps_thread_trx_info.max_length IS NULL) THEN
158+
SET @sys.ps_thread_trx_info.max_length = sys.sys_get_config('ps_thread_trx_info.max_length', 65535);
159+
END IF;
123160

124-
SET @old_ground_concat_max_len = @@session.group_concat_max_len;
125-
SET SESSION group_concat_max_len = 1000000000;
161+
IF (@sys.ps_thread_trx_info.max_length != @@session.group_concat_max_len) THEN
162+
SET @old_group_concat_max_len = @@session.group_concat_max_len;
163+
-- Convert to int value for the SET, and give some surrounding space
164+
SET v_max_output_len = (@sys.ps_thread_trx_info.max_length - 5);
165+
SET SESSION group_concat_max_len = v_max_output_len;
166+
END IF;
126167

127168
SET v_output = (
128169
SELECT CONCAT('[', IFNULL(GROUP_CONCAT(trx_info ORDER BY event_id), ''), '\n]') AS trx_info
@@ -158,7 +199,7 @@ BEGIN
158199
GROUP_CONCAT(
159200
IFNULL(
160201
CONCAT('\n {\n',
161-
' "sql_text": "', IFNULL(REPLACE(sql_text, '\\', '\\\\'), ''), '",\n',
202+
' "sql_text": "', IFNULL(sys.format_statement(REPLACE(sql_text, '\\', '\\\\')), ''), '",\n',
162203
' "time": "', IFNULL(sys.format_time(timer_wait), ''), '",\n',
163204
' "schema": "', IFNULL(current_schema, ''), '",\n',
164205
' "rows_examined": ', IFNULL(rows_examined, ''), ',\n',
@@ -182,8 +223,9 @@ BEGIN
182223
GROUP BY thread_id
183224
);
184225

185-
SET @old_ground_concat_max_len = @@session.group_concat_max_len;
186-
SET SESSION group_concat_max_len = 1000000000;
226+
IF (@old_group_concat_max_len IS NOT NULL) THEN
227+
SET SESSION group_concat_max_len = @old_group_concat_max_len;
228+
END IF;
187229

188230
RETURN v_output;
189231
END$$

mysql-test/suite/sysschema/include/sys_config_cleanup.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ INSERT IGNORE INTO sys.sys_config (variable, value) VALUES
1111
('statement_performance_analyzer.limit', 100),
1212
('statement_performance_analyzer.view', NULL),
1313
('diagnostics.allow_i_s_tables', 'OFF'),
14-
('diagnostics.include_raw', 'OFF');
14+
('diagnostics.include_raw', 'OFF'),
15+
('ps_thread_trx_info.max_length', 65535);
1516

1617
SET @sys.ignore_sys_config_triggers := NULL;

mysql-test/suite/sysschema/r/fn_ps_thread_trx_info.result

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sys.ps_thread_trx_info(234623462376)
33
NULL
44
SELECT JSON_VALID(sys.ps_thread_trx_info(sys.ps_thread_id(NULL)));
55
JSON_VALID(sys.ps_thread_trx_info(sys.ps_thread_id(NULL)))
6-
NULL
6+
1
77
CREATE DATABASE trx;
88
CREATE TABLE trx.info (id INT PRIMARY KEY, info VARCHAR(20));
99
USE trx;
@@ -77,4 +77,12 @@ JSON_CONTAINS(@json_doc, '0', '$[0].stateme
7777
SELECT JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text');
7878
JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text')
7979
1
80+
SET @sys.ps_thread_trx_info.max_length = 100;
81+
SELECT sys.ps_thread_trx_info(@ps_thread_id);
82+
sys.ps_thread_trx_info(@ps_thread_id)
83+
{ "error": "Trx info truncated: Row 12 was cut by GROUP_CONCAT()" }
84+
SET @sys.ps_thread_trx_info.max_length = NULL;
85+
SELECT JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id));
86+
JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id))
87+
1
8088
DROP DATABASE trx;

mysql-test/suite/sysschema/t/fn_ps_thread_trx_info.test

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ SELECT JSON_CONTAINS(@json_doc, '0', '$[0].
6767
# Second statement in transaction should be a COMMIT
6868
SELECT JSON_CONTAINS(@json_doc, '"COMMIT"', '$[0].statements_executed[1].sql_text');
6969

70-
disconnect con1;
70+
# Simulate a truncated set of output by lowering the @sys.ps_thread_trx_info.max_length user variable
71+
# This also tests the user variable works appropriately, incidentally
72+
73+
SET @sys.ps_thread_trx_info.max_length = 100;
74+
75+
# Should return an error JSON object
76+
SELECT sys.ps_thread_trx_info(@ps_thread_id);
7177

78+
# Setting the user variable back to NULL should reset to 65535 from sys_config, and no truncation
79+
SET @sys.ps_thread_trx_info.max_length = NULL;
80+
SELECT JSON_VALID(sys.ps_thread_trx_info(@ps_thread_id));
81+
82+
# Clean up
83+
84+
disconnect con1;
7285
DROP DATABASE trx;

sys_57.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SOURCE ./before_setup.sql
1717

1818
SOURCE ./tables/sys_config.sql
19-
SOURCE ./tables/sys_config_data.sql
19+
SOURCE ./tables/sys_config_data_57.sql
2020

2121
SOURCE ./triggers/sys_config_insert_set_user.sql
2222
SOURCE ./triggers/sys_config_update_set_user.sql

tables/sys_config_data_57.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
2+
--
3+
-- This program is free software; you can redistribute it and/or modify
4+
-- it under the terms of the GNU General Public License as published by
5+
-- the Free Software Foundation; version 2 of the License.
6+
--
7+
-- This program is distributed in the hope that it will be useful,
8+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
-- GNU General Public License for more details.
11+
--
12+
-- You should have received a copy of the GNU General Public License
13+
-- along with this program; if not, write to the Free Software
14+
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15+
16+
-- NOTE: This needs to be replicated within the sys_config_clean.inc file
17+
18+
INSERT IGNORE INTO sys.sys_config (variable, value) VALUES
19+
('statement_truncate_len', 64),
20+
('statement_performance_analyzer.limit', 100),
21+
('statement_performance_analyzer.view', NULL),
22+
('diagnostics.allow_i_s_tables', 'OFF'),
23+
('diagnostics.include_raw', 'OFF'),
24+
('ps_thread_trx_info.max_length', 65535);

0 commit comments

Comments
 (0)