Permalink
Browse files

Improve how truncation is controlled within the ps_thread_trx_info fu…

…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...
1 parent dd75178 commit d2cb0c57e7b8b5dbaf79fc4a9b0f059c8e725b13 @MarkLeith MarkLeith committed Aug 19, 2015
View
@@ -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
@@ -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.
@@ -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
-----------
@@ -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
@@ -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',
@@ -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$$
@@ -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;
@@ -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;
@@ -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;
@@ -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;
View
@@ -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
@@ -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.