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

Commit 601b909

Browse files
Modifying ps_trace_statement_digest and ps_trace_thread so:
* sql_log_bin is set to OFF already in the topmost procedures * restore INSTRUMENTED for the thread executing the procedure and fix that in_auto_enable didn't actually do anything for ps_trace_statement_digest.sql
1 parent b2b7f3a commit 601b909

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

procedures/ps_trace_statement_digest.sql

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ CREATE DEFINER='root'@'localhost' PROCEDURE ps_trace_statement_digest (
4343
Performance Schema truncates long SQL_TEXT values (and hence the
4444
EXPLAIN will fail due to parse errors).
4545
46+
Requires the SUPER privilege for "SET sql_log_bin = 0;".
47+
4648
Parameters
4749
-----------
4850
@@ -141,11 +143,16 @@ BEGIN
141143

142144
DECLARE v_start_fresh BOOLEAN DEFAULT false;
143145
DECLARE v_auto_enable BOOLEAN DEFAULT false;
146+
DECLARE v_this_thread_enabed ENUM('YES', 'NO');
144147
DECLARE v_runtime INT DEFAULT 0;
145148
DECLARE v_start INT DEFAULT 0;
146149
DECLARE v_found_stmts INT;
147150

151+
SET @log_bin := @@sql_log_bin;
152+
SET sql_log_bin = 0;
153+
148154
/* Do not track the current thread, it will kill the stack */
155+
SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
149156
CALL sys.ps_setup_disable_thread(CONNECTION_ID());
150157

151158
DROP TEMPORARY TABLE IF EXISTS stmt_trace;
@@ -185,6 +192,22 @@ BEGIN
185192
SET v_auto_enable = in_auto_enable;
186193
IF v_auto_enable THEN
187194
CALL sys.ps_setup_save(0);
195+
196+
UPDATE performance_schema.threads
197+
SET INSTRUMENTED = IF(PROCESSLIST_ID IS NOT NULL, 'YES', 'NO');
198+
199+
-- Only the events_statements_history_long and events_stages_history_long tables and their ancestors are needed
200+
UPDATE performance_schema.setup_consumers
201+
SET ENABLED = 'YES'
202+
WHERE NAME NOT LIKE '%\_history'
203+
AND NAME NOT LIKE 'events_wait%'
204+
AND NAME NOT LIKE 'events_transactions%'
205+
AND NAME <> 'statements_digest';
206+
207+
UPDATE performance_schema.setup_instruments
208+
SET ENABLED = 'YES',
209+
TIMED = 'YES'
210+
WHERE NAME LIKE 'statement/%' OR NAME LIKE 'stage/%';
188211
END IF;
189212

190213
WHILE v_runtime < in_runtime DO
@@ -261,10 +284,14 @@ BEGIN
261284
DEALLOCATE PREPARE explain_stmt;
262285

263286
IF v_auto_enable THEN
264-
CALL sys.ps_reload_saved();
287+
CALL sys.ps_setup_reload_saved();
288+
END IF;
289+
/* Restore INSTRUMENTED for this thread */
290+
IF (v_this_thread_enabed = 'YES') THEN
265291
CALL sys.ps_setup_enable_thread(CONNECTION_ID());
266292
END IF;
267293

294+
SET sql_log_bin = @log_bin;
268295
END$$
269296

270297
DELIMITER ;

procedures/ps_trace_thread.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CREATE DEFINER='root'@'localhost' PROCEDURE ps_trace_thread (
3434
3535
Each resultset returned from the procedure should be used for a complete graph
3636
37+
Requires the SUPER privilege for "SET sql_log_bin = 0;".
38+
3739
Parameters
3840
-----------
3941
@@ -108,6 +110,7 @@ BEGIN
108110
DECLARE v_done bool DEFAULT FALSE;
109111
DECLARE v_start, v_runtime DECIMAL(20,2) DEFAULT 0.0;
110112
DECLARE v_min_event_id bigint unsigned DEFAULT 0;
113+
DECLARE v_this_thread_enabed ENUM('YES', 'NO');
111114
DECLARE v_event longtext;
112115
DECLARE c_stack CURSOR FOR
113116
SELECT CONCAT(IF(nesting_event_id IS NOT NULL, CONCAT(nesting_event_id, ' -> '), ''),
@@ -216,7 +219,11 @@ BEGIN
216219
ORDER BY event_id;
217220
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
218221

222+
SET @log_bin := @@sql_log_bin;
223+
SET sql_log_bin = 0;
224+
219225
/* Do not track the current thread, it will kill the stack */
226+
SELECT INSTRUMENTED INTO v_this_thread_enabed FROM performance_schema.threads WHERE PROCESSLIST_ID = CONNECTION_ID();
220227
CALL sys.ps_setup_disable_thread(CONNECTION_ID());
221228

222229
IF (in_auto_setup) THEN
@@ -302,8 +309,13 @@ BEGIN
302309
/* Reset the settings for the performance schema */
303310
IF (in_auto_setup) THEN
304311
CALL sys.ps_setup_reload_saved();
312+
END IF;
313+
/* Restore INSTRUMENTED for this thread */
314+
IF (v_this_thread_enabed = 'YES') THEN
305315
CALL sys.ps_setup_enable_thread(CONNECTION_ID());
306316
END IF;
317+
318+
SET sql_log_bin = @log_bin;
307319
END$$
308320

309321
DELIMITER ;

0 commit comments

Comments
 (0)