1# this test needs multithreaded mysqltest
2-- source include/not_embedded.inc
3#
4# Basic log tables test
5#
6# check that CSV engine was compiled in
7--source include/have_csv.inc
8
9SET SQL_MODE="";
10SET @old_general_log_state = @@global.general_log;
11SET @old_log_output=       @@global.log_output;
12SET @old_slow_query_log=   @@global.slow_query_log;
13SET @old_general_log=      @@global.general_log;
14SET @old_long_query_time=  @@session.long_query_time;
15
16--disable_ps_protocol
17use mysql;
18
19# Capture initial settings of system variables
20# so that we can revert to old state after manipulation for testing
21# NOTE:  PLEASE USE THESE VALUES TO 'RESET' SYSTEM VARIABLES
22# Capturing old values within the tests results in loss of values
23# due to people not paying attention to previous tests' changes, captures
24# or improper cleanup
25SET @saved_long_query_time = @@long_query_time;
26SET @saved_log_output = @@log_output;
27SET @saved_general_log = @@GLOBAL.general_log;
28SET @saved_slow_query_log = @@GLOBAL.slow_query_log;
29
30#
31# Check that log tables work and we can do basic selects. This also
32# tests truncate, which works in a special mode with the log tables
33#
34
35truncate table general_log;
36--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
37select * from general_log;
38truncate table slow_log;
39--replace_column 1 TIMESTAMP 2 USER_HOST
40select * from slow_log;
41
42#
43# We want to check that a record newly written to a log table shows up for
44# the query: since log tables use concurrent insert machinery and log tables
45# are always locked by artificial THD, this feature requires additional
46# check in ha_tina::write_row. This simple test should prove that the
47# log table flag in the table handler is triggered and working.
48#
49
50truncate table general_log;
51--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
52select * from general_log where argument like '%general_log%';
53
54
55#
56# Check some basic queries interfering with the log tables.
57# In our test we'll use a table with verbose comments to the short
58# command type names, used in the tables
59#
60
61create table join_test (verbose_comment varchar (80), command_type varchar(64));
62
63insert into join_test values ("User performed a usual SQL query", "Query");
64insert into join_test values ("New DB connection was registered", "Connect");
65insert into join_test values ("Get the table info", "Field List");
66
67--replace_column 2 USER_HOST
68select verbose_comment, user_host, argument
69  from  mysql.general_log join join_test
70    on (mysql.general_log.command_type = join_test.command_type);
71
72drop table join_test;
73
74#
75# check that flush of the log table work fine
76#
77
78flush logs;
79
80#
81# check locking of the log tables
82#
83
84--error ER_CANT_LOCK_LOG_TABLE
85lock tables mysql.general_log WRITE;
86
87--error ER_CANT_LOCK_LOG_TABLE
88lock tables mysql.slow_log WRITE;
89
90#
91# This attemts to get TL_READ_NO_INSERT lock, which is incompatible with
92# TL_WRITE_CONCURRENT_INSERT. This should fail. We issue this error as log
93# tables are always opened and locked by the logger.
94#
95
96--error ER_CANT_LOCK_LOG_TABLE
97lock tables mysql.general_log READ;
98
99--error ER_CANT_LOCK_LOG_TABLE
100lock tables mysql.slow_log READ;
101
102#
103# This call should result in TL_READ lock on the log table.
104# This is not ok and should fail.
105#
106
107--error ER_CANT_LOCK_LOG_TABLE
108lock tables mysql.slow_log READ LOCAL, mysql.general_log READ LOCAL;
109
110# Misc locking tests
111
112show create table mysql.general_log;
113show fields from mysql.general_log;
114
115show create table mysql.slow_log;
116show fields from mysql.slow_log;
117
118#
119# check that FLUSH LOGS does not flush the log tables
120#
121
122flush logs;
123flush tables;
124
125SET GLOBAL GENERAL_LOG=ON;
126SET GLOBAL SLOW_QUERY_LOG=ON;
127
128show open tables;
129flush logs;
130show open tables;
131
132#
133# check that FLUSH TABLES does flush the log tables
134#
135
136flush tables;
137# Since the flush is logged, mysql.general_log will be in the cache
138show open tables;
139
140SET GLOBAL GENERAL_LOG=OFF;
141SET GLOBAL SLOW_QUERY_LOG=OFF;
142
143flush tables;
144# Here the table cache is empty
145show open tables;
146
147SET GLOBAL GENERAL_LOG=ON;
148SET GLOBAL SLOW_QUERY_LOG=ON;
149
150#
151# Bug#23924 general_log truncates queries with character set introducers.
152#
153truncate table mysql.general_log;
154set names binary;
155select _koi8r'����' as test;
156--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
157select * from mysql.general_log;
158set names utf8;
159
160#
161# Bug #16905    Log tables: unicode statements are logged incorrectly
162#
163
164truncate table mysql.general_log;
165set names utf8;
166create table bug16905 (s char(15) character set utf8 default 'пусто');
167insert into bug16905 values ('новое');
168--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
169select * from mysql.general_log;
170drop table bug16905;
171
172#
173# Bug #17600: Invalid data logged into mysql.slow_log
174#
175
176truncate table mysql.slow_log;
177set session long_query_time=1;
178select sleep(2);
179--replace_column 1 TIMESTAMP 2 USER_HOST 3 QUERY_TIME 12 THREAD_ID
180select * from mysql.slow_log;
181set @@session.long_query_time = @saved_long_query_time;
182
183#
184# Bug #18559 log tables cannot change engine, and gets deadlocked when
185# dropping w/ log on
186#
187
188# check that appropriate error messages are given when one attempts to alter
189# or drop a log tables, while corresponding logs are enabled
190--error ER_BAD_LOG_STATEMENT
191alter table mysql.general_log engine=myisam;
192--error ER_BAD_LOG_STATEMENT
193alter table mysql.slow_log engine=myisam;
194
195--error ER_BAD_LOG_STATEMENT
196drop table mysql.general_log;
197--error ER_BAD_LOG_STATEMENT
198drop table mysql.slow_log;
199
200# check that one can alter log tables to MyISAM
201set global general_log='OFF';
202
203# cannot convert another log table
204--error ER_BAD_LOG_STATEMENT
205alter table mysql.slow_log engine=myisam;
206
207# alter both tables
208set global slow_query_log='OFF';
209# check that both tables use CSV engine
210show create table mysql.general_log;
211show create table mysql.slow_log;
212
213alter table mysql.general_log engine=myisam;
214alter table mysql.slow_log engine=myisam;
215
216# check that the tables were converted
217show create table mysql.general_log;
218show create table mysql.slow_log;
219
220# enable log tables and chek that new tables indeed work
221set global general_log='ON';
222set global slow_query_log='ON';
223
224--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
225select * from mysql.general_log;
226
227# check that flush of myisam-based log tables work fine
228flush logs;
229
230# check locking of myisam-based log tables
231
232--error ER_CANT_LOCK_LOG_TABLE
233lock tables mysql.general_log WRITE;
234
235--error ER_CANT_LOCK_LOG_TABLE
236lock tables mysql.slow_log WRITE;
237
238#
239# This attemts to get TL_READ_NO_INSERT lock, which is incompatible with
240# TL_WRITE_CONCURRENT_INSERT. This should fail. We issue this error as log
241# tables are always opened and locked by the logger.
242#
243
244--error ER_CANT_LOCK_LOG_TABLE
245lock tables mysql.general_log READ;
246
247--error ER_CANT_LOCK_LOG_TABLE
248lock tables mysql.slow_log READ;
249
250# check that we can drop them
251set global general_log='OFF';
252set global slow_query_log='OFF';
253
254# check that alter table doesn't work for other engines
255set @save_storage_engine= @@session.storage_engine;
256set storage_engine= MEMORY;
257# After fixing bug#35765 the error behaivor changed:
258# If compiled in/enabled ER_UNSUPORTED_LOG_ENGINE
259# If not (i.e. not existant) it will show a warning
260# and use the current one.
261alter table mysql.slow_log engine=NonExistentEngine;
262--error ER_UNSUPORTED_LOG_ENGINE
263alter table mysql.slow_log engine=memory;
264#--error ER_UNSUPORTED_LOG_ENGINE
265#alter table mysql.slow_log engine=innodb;
266#--error ER_UNSUPORTED_LOG_ENGINE
267#alter table mysql.slow_log engine=archive;
268#--error ER_UNSUPORTED_LOG_ENGINE
269#alter table mysql.slow_log engine=blackhole;
270set storage_engine= @save_storage_engine;
271
272# Make sure only non-transactional Aria table can be used for logging
273--error ER_UNSUPORTED_LOG_ENGINE
274ALTER TABLE mysql.general_log ENGINE=Aria;
275ALTER TABLE mysql.general_log ENGINE=Aria transactional = 0;
276--error ER_UNSUPORTED_LOG_ENGINE
277ALTER TABLE mysql.slow_log ENGINE=Aria;
278ALTER TABLE mysql.slow_log ENGINE=Aria transactional = 0;
279
280drop table mysql.slow_log;
281drop table mysql.general_log;
282
283# check that table share cleanup is performed correctly (double drop)
284
285--error ER_BAD_TABLE_ERROR
286drop table mysql.general_log;
287--error ER_BAD_TABLE_ERROR
288drop table mysql.slow_log;
289
290# recreate tables and enable logs
291
292use mysql;
293
294CREATE TABLE `general_log` (
295  `event_time` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
296                         ON UPDATE CURRENT_TIMESTAMP,
297  `user_host` mediumtext NOT NULL,
298  `thread_id` BIGINT(21) UNSIGNED NOT NULL,
299  `server_id` int(10) unsigned NOT NULL,
300  `command_type` varchar(64) NOT NULL,
301  `argument` mediumtext NOT NULL
302) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log';
303
304CREATE TABLE `slow_log` (
305  `start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
306                         ON UPDATE CURRENT_TIMESTAMP,
307  `user_host` mediumtext NOT NULL,
308  `query_time` time(6) NOT NULL,
309  `lock_time` time(6) NOT NULL,
310  `rows_sent` int(11) NOT NULL,
311  `rows_examined` int(11) NOT NULL,
312  `db` varchar(512) NOT NULL,
313  `last_insert_id` int(11) NOT NULL,
314  `insert_id` int(11) NOT NULL,
315  `server_id` int(10) unsigned NOT NULL,
316  `sql_text` mediumtext NOT NULL,
317  `thread_id` BIGINT(21) UNSIGNED NOT NULL,
318  `rows_affected` int(11) NOT NULL
319) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';
320
321set global general_log='ON';
322set global slow_query_log='ON';
323use test;
324
325#
326# Bug #20139 Infinite loop after "FLUSH" and "LOCK tabX, general_log"
327#
328
329flush tables with read lock;
330unlock tables;
331use mysql;
332--error ER_CANT_LOCK_LOG_TABLE
333lock tables general_log read local, help_category read local;
334unlock tables;
335
336#
337# Bug #17544 Cannot do atomic log rotate and
338# Bug #21785 Server crashes after rename of the log table
339#
340
341SET SESSION long_query_time = 1000;
342--disable_warnings
343drop table if exists mysql.renamed_general_log;
344drop table if exists mysql.renamed_slow_log;
345drop table if exists mysql.general_log_new;
346drop table if exists mysql.slow_log_new;
347--enable_warnings
348
349use mysql;
350# Should result in error
351--error ER_CANT_RENAME_LOG_TABLE
352RENAME TABLE general_log TO renamed_general_log;
353--error ER_CANT_RENAME_LOG_TABLE
354RENAME TABLE slow_log TO renamed_slow_log;
355
356#check rotate logs
357truncate table general_log;
358--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
359select * from general_log;
360
361truncate table slow_log;
362--replace_column 1 TIMESTAMP 2 USER_HOST
363select * from slow_log;
364
365create table general_log_new like general_log;
366rename table general_log TO renamed_general_log, general_log_new TO general_log;
367
368create table slow_log_new like slow_log;
369rename table slow_log TO renamed_slow_log, slow_log_new TO slow_log;
370
371# check that rename checks more then first table in the list
372--error ER_CANT_RENAME_LOG_TABLE
373rename table general_log TO general_log_new, renamed_general_log TO general_log, slow_log to renamed_slow_log;
374
375# now check the content of tables
376--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
377select * from general_log;
378--replace_column 1 TIMESTAMP 2 USER_HOST 3 THREAD_ID
379select * from renamed_general_log;
380
381# the content of the slow log is empty, but we will try a select anyway
382--replace_column 1 TIMESTAMP 2 USER_HOST
383select * from slow_log;
384--replace_column 1 TIMESTAMP 2 USER_HOST
385select * from renamed_slow_log;
386
387# check that we can do whatever we want with disabled log
388set global general_log='OFF';
389RENAME TABLE general_log TO general_log2;
390
391set global slow_query_log='OFF';
392RENAME TABLE slow_log TO slow_log2;
393
394# this should fail
395--error ER_NO_SUCH_TABLE
396set global general_log='ON';
397--error ER_NO_SUCH_TABLE
398set global slow_query_log='ON';
399
400RENAME TABLE general_log2 TO general_log;
401RENAME TABLE slow_log2 TO slow_log;
402SET SESSION long_query_time = @saved_long_query_time;
403
404# this should work
405set global general_log='ON';
406set global slow_query_log='ON';
407# now check flush logs
408flush logs;
409flush logs;
410drop table renamed_general_log, renamed_slow_log;
411use test;
412
413#
414# Bug#27858 (Failing to log to a log table doesn't log anything to error log)
415#
416# This test works as expected, it's a negative test.
417# The message "[ERROR] Failed to write to mysql.general_log"
418# is printed to master.err when writing to the table mysql.general_log
419# failed.
420# However, this message is picked up by mysql-test-run.pl,
421# and reported as a test failure, which is a false negative.
422# There is no current way to *selectively* filter out these expected error conditions
423# (see mysql-test/lib/mtr_report.pl, mtr_report_stats()).
424# Instead of filtering all occurences of "Failed to write to
425# mysql.general_log", which could hide bugs when the error is not expected,
426# this test case is commented instead.
427# TODO: improve filtering of expected errors in master.err in
428# mysql-test-run.pl (based on the test name ?), and uncomment this test.
429
430# --disable_warnings
431# drop table if exists mysql.bad_general_log;
432# drop table if exists mysql.bad_slow_log;
433# drop table if exists mysql.general_log_hide;
434# drop table if exists mysql.slow_log_hide;
435# --enable_warnings
436#
437# create table mysql.bad_general_log (a int) engine= CSV;
438# create table mysql.bad_slow_log (a int) engine= CSV;
439#
440# # Rename does not perform checks on the table structure,
441# # exploiting this to force a failure to log
442# rename table mysql.general_log to mysql.general_log_hide, mysql.bad_general_log TO mysql.general_log;
443# rename table mysql.slow_log to mysql.slow_log_hide, mysql.bad_slow_log TO mysql.slow_log;
444#
445# # The following message should be printed in master.log:
446# # [ERROR] Failed to write to mysql.general_log
447# # TODO: See how verifying this could be automated
448#
449# flush tables;
450# select "logging this should fail";
451#
452# # Restore the log tables
453#
454# rename table mysql.general_log to mysql.bad_general_log, mysql.general_log_hide TO mysql.general_log;
455# rename table mysql.slow_log to mysql.bad_slow_log, mysql.slow_log_hide TO mysql.slow_log;
456#
457# flush tables;
458#
459# drop table mysql.bad_general_log;
460# drop table mysql.bad_slow_log;
461
462#
463# Bug #21966 Strange warnings on repair of the log tables
464#
465
466use mysql;
467# check that no warning occurs on repair of the log tables
468repair table general_log;
469repair table slow_log;
470# check that no warning occurs on "create like" for the log tables
471create table general_log_new like general_log;
472create table slow_log_new like slow_log;
473show tables like "%log%";
474drop table slow_log_new, general_log_new;
475use test;
476
477#
478# Bug#69953 / MDEV-4851
479# Log tables should be modifable on LOG_OUTPUT != TABLE
480#
481#
482
483SET GLOBAL LOG_OUTPUT = 'FILE';
484SET GLOBAL slow_query_log = 1;
485SET GLOBAL general_log = 1;
486
487ALTER TABLE mysql.slow_log ADD COLUMN comment_text TEXT NOT NULL;
488ALTER TABLE mysql.general_log ADD COLUMN comment_text TEXT NOT NULL;
489
490SET GLOBAL LOG_OUTPUT = 'NONE';
491ALTER TABLE mysql.slow_log DROP COLUMN comment_text;
492ALTER TABLE mysql.general_log DROP COLUMN comment_text;
493
494
495#
496# Bug#27857 (Log tables supplies the wrong value for generating
497#            AUTO_INCREMENT numbers)
498#
499
500SET GLOBAL LOG_OUTPUT = 'TABLE';
501
502## test the general log
503
504SET GLOBAL general_log = 0;
505FLUSH LOGS;
506
507TRUNCATE TABLE mysql.general_log;
508ALTER TABLE mysql.general_log ENGINE = MyISAM;
509ALTER TABLE mysql.general_log
510  ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
511
512SET GLOBAL general_log = 1;
513FLUSH LOGS;
514
515--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
516SELECT * FROM mysql.general_log;
517--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
518SELECT * FROM mysql.general_log;
519SELECT "My own query 1";
520SELECT "My own query 2";
521--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
522SELECT * FROM mysql.general_log;
523
524SET GLOBAL general_log = 0;
525FLUSH LOGS;
526
527ALTER TABLE mysql.general_log DROP COLUMN seq;
528ALTER TABLE mysql.general_log ENGINE = CSV;
529
530## test the slow query log
531
532SET GLOBAL slow_query_log = 0;
533FLUSH LOGS;
534
535TRUNCATE TABLE mysql.slow_log;
536ALTER TABLE mysql.slow_log ENGINE = MyISAM;
537
538ALTER TABLE mysql.slow_log
539  ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
540
541SET SESSION long_query_time = 1;
542SET GLOBAL slow_query_log = 1;
543FLUSH LOGS;
544
545## FLUSH LOGS above might be slow, so the following is
546## logged as either seq 1-4 or seq 2-5
547SELECT "My own slow query", sleep(2);
548SELECT "My own slow query", sleep(2);
549SELECT "My own slow query", sleep(2);
550SELECT "My own slow query", sleep(2);
551
552## So we look for seq 2-4
553--replace_column 1 START_TIME 2 USER_HOST 3 QUERY_TIME 12 THREAD_ID
554SELECT * FROM mysql.slow_log WHERE seq >= 2 LIMIT 3;
555
556SET GLOBAL slow_query_log = 0;
557SET SESSION long_query_time =@saved_long_query_time;
558FLUSH LOGS;
559
560ALTER TABLE mysql.slow_log DROP COLUMN seq;
561ALTER TABLE mysql.slow_log ENGINE = CSV;
562
563SET GLOBAL general_log = @old_general_log;
564SET GLOBAL slow_query_log = @old_slow_query_log;
565
566#
567# Bug#25422 (Hang with log tables)
568#
569
570--disable_warnings
571drop procedure if exists proc25422_truncate_slow;
572drop procedure if exists proc25422_truncate_general;
573drop procedure if exists proc25422_alter_slow;
574drop procedure if exists proc25422_alter_general;
575--enable_warnings
576
577delimiter //;
578
579use test//
580create procedure proc25422_truncate_slow (loops int)
581begin
582  declare v1 int default 0;
583  declare continue handler for sqlexception /* errors from truncate */
584    begin end;
585  while v1 < loops do
586    truncate mysql.slow_log;
587    set v1 = v1 + 1;
588    end while;
589end//
590
591create procedure proc25422_truncate_general (loops int)
592begin
593  declare v1 int default 0;
594  declare continue handler for sqlexception /* errors from truncate */
595    begin end;
596  while v1 < loops do
597    truncate mysql.general_log;
598    set v1 = v1 + 1;
599    end while;
600end//
601
602create procedure proc25422_alter_slow (loops int)
603begin
604  declare v1 int default 0;
605  declare ER_BAD_LOG_STATEMENT condition for 1575;
606  declare continue handler for ER_BAD_LOG_STATEMENT begin end;
607
608  while v1 < loops do
609    set @old_log_state = @@global.slow_query_log;
610    set global slow_query_log = 'OFF';
611    alter table mysql.slow_log engine = CSV;
612    set global slow_query_log = @old_log_state;
613    set v1 = v1 + 1;
614    end while;
615end//
616
617create procedure proc25422_alter_general (loops int)
618begin
619  declare v1 int default 0;
620  declare ER_BAD_LOG_STATEMENT condition for 1575;
621  declare continue handler for ER_BAD_LOG_STATEMENT begin end;
622
623  while v1 < loops do
624    set @old_log_state = @@global.general_log;
625    set global general_log = 'OFF';
626    alter table mysql.general_log engine = CSV;
627    set global general_log = @old_log_state;
628    set v1 = v1 + 1;
629    end while;
630end//
631
632delimiter ;//
633
634set @iterations=100;
635
636--echo "Serial test (proc25422_truncate_slow)"
637call proc25422_truncate_slow(@iterations);
638--echo "Serial test (proc25422_truncate_general)"
639call proc25422_truncate_general(@iterations);
640--echo "Serial test (proc25422_alter_slow)"
641call proc25422_alter_slow(@iterations);
642--echo "Serial test (proc25422_alter_general)"
643call proc25422_alter_general(@iterations);
644
645--echo "Parallel test"
646
647# ER_BAD_LOG_STATEMENT errors will occur,
648# since concurrent threads do SET GLOBAL general_log= ...
649# This is silenced by handlers and will not affect the test
650
651connect (addconroot1, localhost, root,,);
652connect (addconroot2, localhost, root,,);
653connect (addconroot3, localhost, root,,);
654connect (addconroot4, localhost, root,,);
655connect (addconroot5, localhost, root,,);
656connect (addconroot6, localhost, root,,);
657connect (addconroot7, localhost, root,,);
658connect (addconroot8, localhost, root,,);
659
660connection addconroot1;
661send call proc25422_truncate_slow(@iterations);
662connection addconroot2;
663send call proc25422_truncate_slow(@iterations);
664
665connection addconroot3;
666send call proc25422_truncate_general(@iterations);
667connection addconroot4;
668send call proc25422_truncate_general(@iterations);
669
670connection addconroot5;
671send call proc25422_alter_slow(@iterations);
672connection addconroot6;
673send call proc25422_alter_slow(@iterations);
674
675connection addconroot7;
676send call proc25422_alter_general(@iterations);
677connection addconroot8;
678send call proc25422_alter_general(@iterations);
679
680connection addconroot1;
681reap;
682connection addconroot2;
683reap;
684connection addconroot3;
685reap;
686connection addconroot4;
687reap;
688connection addconroot5;
689reap;
690connection addconroot6;
691reap;
692connection addconroot7;
693reap;
694connection addconroot8;
695reap;
696
697connection default;
698
699disconnect addconroot1;
700disconnect addconroot2;
701disconnect addconroot3;
702disconnect addconroot4;
703disconnect addconroot5;
704disconnect addconroot6;
705disconnect addconroot7;
706disconnect addconroot8;
707
708drop procedure proc25422_truncate_slow;
709drop procedure proc25422_truncate_general;
710drop procedure proc25422_alter_slow;
711drop procedure proc25422_alter_general;
712
713--enable_ps_protocol
714
715
716#
717# Bug#23044 (Warnings on flush of a log table)
718#
719
720FLUSH TABLE mysql.general_log;
721show warnings;
722
723FLUSH TABLE mysql.slow_log;
724show warnings;
725
726#
727# Bug#17876 (Truncating mysql.slow_log in a SP after using cursor locks the
728#            thread)
729#
730
731--disable_warnings
732DROP TABLE IF EXISTS `db_17876.slow_log_data`;
733DROP TABLE IF EXISTS `db_17876.general_log_data`;
734DROP PROCEDURE IF EXISTS `db_17876.archiveSlowLog`;
735DROP PROCEDURE IF EXISTS `db_17876.archiveGeneralLog`;
736DROP DATABASE IF EXISTS `db_17876`;
737--enable_warnings
738
739CREATE DATABASE db_17876;
740
741CREATE TABLE `db_17876.slow_log_data` (
742  `start_time` timestamp(6)  default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
743  `user_host` mediumtext ,
744  `query_time` time(6) ,
745  `lock_time` time(6) ,
746  `rows_sent` int(11) ,
747  `rows_examined` int(11) ,
748  `db` varchar(512) default NULL,
749  `last_insert_id` int(11) default NULL,
750  `insert_id` int(11) default NULL,
751  `server_id` int(11) default NULL,
752  `sql_text` mediumtext,
753  `thread_id` bigint(21) unsigned default NULL,
754  `rows_affected` int(11) default NULL
755);
756
757CREATE TABLE `db_17876.general_log_data` (
758  `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
759  `user_host` mediumtext,
760  `thread_id` int(11) DEFAULT NULL,
761  `server_id` int(11) DEFAULT NULL,
762  `command_type` varchar(64) DEFAULT NULL,
763  `argument` mediumtext
764);
765
766DELIMITER //;
767
768CREATE procedure `db_17876.archiveSlowLog`()
769BEGIN
770  DECLARE start_time, query_time, lock_time CHAR(28);
771  DECLARE user_host MEDIUMTEXT;
772  DECLARE rows_set, rows_examined, last_insert_id, insert_id, server_id, rows_affected INT;
773  DECLARE thread_id BIGINT UNSIGNED;
774  DECLARE dbname MEDIUMTEXT;
775  DECLARE sql_text BLOB;
776  DECLARE done INT DEFAULT 0;
777  DECLARE ER_SP_FETCH_NO_DATA CONDITION for 1329;
778
779  DECLARE cur1 CURSOR FOR SELECT * FROM mysql.slow_log;
780
781  OPEN cur1;
782
783  REPEAT
784    BEGIN
785      BEGIN
786        DECLARE CONTINUE HANDLER FOR ER_SP_FETCH_NO_DATA SET done = 1;
787
788        FETCH cur1 INTO
789           start_time, user_host, query_time, lock_time,
790           rows_set, rows_examined, dbname, last_insert_id,
791           insert_id, server_id, sql_text, thread_id, rows_affected;
792      END;
793
794      IF NOT done THEN
795        BEGIN
796          INSERT INTO
797            `db_17876.slow_log_data`
798            VALUES(start_time, user_host, query_time, lock_time, rows_set, rows_examined,
799                   dbname, last_insert_id,  insert_id, server_id, sql_text, thread_id,
800                   rows_affected);
801        END;
802      END IF;
803    END;
804  UNTIL done END REPEAT;
805
806  CLOSE cur1;
807  TRUNCATE mysql.slow_log;
808END //
809
810CREATE procedure `db_17876.archiveGeneralLog`()
811BEGIN
812  DECLARE event_time CHAR(28);
813  DECLARE user_host, argument MEDIUMTEXT;
814  DECLARE thread_id, server_id INT;
815  DECLARE sql_text BLOB;
816  DECLARE done INT DEFAULT 0;
817  DECLARE command_type VARCHAR(64);
818  DECLARE ER_SP_FETCH_NO_DATA CONDITION for 1329;
819
820  DECLARE cur1 CURSOR FOR SELECT * FROM mysql.general_log;
821
822  OPEN cur1;
823
824  REPEAT
825    BEGIN
826      BEGIN
827        DECLARE CONTINUE HANDLER FOR ER_SP_FETCH_NO_DATA SET done = 1;
828
829        FETCH cur1 INTO
830          event_time, user_host, thread_id, server_id,
831          command_type, argument;
832      END;
833
834      IF NOT done THEN
835        BEGIN
836          INSERT INTO
837            `db_17876.general_log_data`
838            VALUES(event_time, user_host, thread_id, server_id,
839            command_type, argument);
840        END;
841      END IF;
842    END;
843  UNTIL done END REPEAT;
844
845  CLOSE cur1;
846  TRUNCATE mysql.general_log;
847END //
848
849DELIMITER ;//
850
851SET GLOBAL general_log = ON;
852SET GLOBAL slow_query_log = ON;
853
854select "put something into general_log";
855select "... and something more ...";
856
857call `db_17876.archiveSlowLog`();
858call `db_17876.archiveGeneralLog`();
859
860SET GLOBAL general_log = OFF;
861SET GLOBAL slow_query_log = OFF;
862
863call `db_17876.archiveSlowLog`();
864call `db_17876.archiveGeneralLog`();
865
866DROP TABLE `db_17876.slow_log_data`;
867DROP TABLE `db_17876.general_log_data`;
868DROP PROCEDURE IF EXISTS `db_17876.archiveSlowLog`;
869DROP PROCEDURE IF EXISTS `db_17876.archiveGeneralLog`;
870DROP DATABASE IF EXISTS `db_17876`;
871
872SET GLOBAL general_log = @old_general_log;
873SET GLOBAL slow_query_log = @old_slow_query_log;
874
875#
876# Bug#21557 entries in the general query log truncated at 1000 characters.
877#
878
879select CONNECTION_ID() into @thread_id;
880--disable_ps_protocol
881truncate table mysql.general_log;
882--enable_ps_protocol
883set global general_log = on;
884--disable_result_log
885set @lparam = "000 001 002 003 004 005 006 007 008 009"
886              "010 011 012 013 014 015 016 017 018 019"
887              "020 021 022 023 024 025 026 027 028 029"
888              "030 031 032 033 034 035 036 037 038 039"
889              "040 041 042 043 044 045 046 047 048 049"
890              "050 051 052 053 054 055 056 057 058 059"
891              "060 061 062 063 064 065 066 067 068 069"
892              "070 071 072 073 074 075 076 077 078 079"
893              "080 081 082 083 084 085 086 087 088 089"
894              "090 091 092 093 094 095 096 097 098 099"
895              "100 101 102 103 104 105 106 107 108 109"
896              "110 111 112 113 114 115 116 117 118 119"
897              "120 121 122 123 124 125 126 127 128 129"
898              "130 131 132 133 134 135 136 137 138 139"
899              "140 141 142 143 144 145 146 147 148 149"
900              "150 151 152 153 154 155 156 157 158 159"
901              "160 161 162 163 164 165 166 167 168 169"
902              "170 171 172 173 174 175 176 177 178 179"
903              "180 181 182 183 184 185 186 187 188 189"
904              "190 191 192 193 194 195 196 197 198 199"
905              "200 201 202 203 204 205 206 207 208 209"
906              "210 211 212 213 214 215 216 217 218 219"
907              "220 221 222 223 224 225 226 227 228 229"
908              "230 231 232 233 234 235 236 237 238 239"
909              "240 241 242 243 244 245 246 247 248 249"
910              "250 251 252 253 254 255 256 257 258 259"
911              "260 261 262 263 264 265 266 267 268 269"
912              "270 271 272 273 274 275 276 277 278 279"
913              "280 281 282 283 284 285 286 287 288 289"
914              "290 291 292 293 294 295 296 297 298 299"
915              "300 301 302 303 304 305 306 307 308 309"
916              "310 311 312 313 314 315 316 317 318 319"
917              "320 321 322 323 324 325 326 327 328 329"
918              "330 331 332 333 334 335 336 337 338 339"
919              "340 341 342 343 344 345 346 347 348 349"
920              "350 351 352 353 354 355 356 357 358 359"
921              "360 361 362 363 364 365 366 367 368 369"
922              "370 371 372 373 374 375 376 377 378 379"
923              "380 381 382 383 384 385 386 387 388 389"
924              "390 391 392 393 394 395 396 397 398 399"
925              "400 401 402 403 404 405 406 407 408 409"
926              "410 411 412 413 414 415 416 417 418 419"
927              "420 421 422 423 424 425 426 427 428 429"
928              "430 431 432 433 434 435 436 437 438 439"
929              "440 441 442 443 444 445 446 447 448 449"
930              "450 451 452 453 454 455 456 457 458 459"
931              "460 461 462 463 464 465 466 467 468 469"
932              "470 471 472 473 474 475 476 477 478 479"
933              "480 481 482 483 484 485 486 487 488 489"
934              "490 491 492 493 494 495 496 497 498 499"
935              "500 501 502 503 504 505 506 507 508 509"
936              "510 511 512 513 514 515 516 517 518 519"
937              "520 521 522 523 524 525 526 527 528 529"
938              "530 531 532 533 534 535 536 537 538 539"
939              "540 541 542 543 544 545 546 547 548 549"
940              "550 551 552 553 554 555 556 557 558 559"
941              "560 561 562 563 564 565 566 567 568 569"
942              "570 571 572 573 574 575 576 577 578 579"
943              "580 581 582 583 584 585 586 587 588 589"
944              "590 591 592 593 594 595 596 597 598 599"
945              "600 601 602 603 604 605 606 607 608 609"
946              "610 611 612 613 614 615 616 617 618 619"
947              "620 621 622 623 624 625 626 627 628 629"
948              "630 631 632 633 634 635 636 637 638 639"
949              "640 641 642 643 644 645 646 647 648 649"
950              "650 651 652 653 654 655 656 657 658 659"
951              "660 661 662 663 664 665 666 667 668 669"
952              "670 671 672 673 674 675 676 677 678 679"
953              "680 681 682 683 684 685 686 687 688 689"
954              "690 691 692 693 694 695 696 697 698 699"
955              "700 701 702 703 704 705 706 707 708 709"
956              "710 711 712 713 714 715 716 717 718 719"
957              "720 721 722 723 724 725 726 727 728 729"
958              "730 731 732 733 734 735 736 737 738 739"
959              "740 741 742 743 744 745 746 747 748 749"
960              "750 751 752 753 754 755 756 757 758 759"
961              "760 761 762 763 764 765 766 767 768 769"
962              "770 771 772 773 774 775 776 777 778 779"
963              "780 781 782 783 784 785 786 787 788 789"
964              "790 791 792 793 794 795 796 797 798 799"
965              "800 801 802 803 804 805 806 807 808 809"
966              "810 811 812 813 814 815 816 817 818 819"
967              "820 821 822 823 824 825 826 827 828 829"
968              "830 831 832 833 834 835 836 837 838 839"
969              "840 841 842 843 844 845 846 847 848 849"
970              "850 851 852 853 854 855 856 857 858 859"
971              "860 861 862 863 864 865 866 867 868 869"
972              "870 871 872 873 874 875 876 877 878 879"
973              "880 881 882 883 884 885 886 887 888 889"
974              "890 891 892 893 894 895 896 897 898 899"
975              "900 901 902 903 904 905 906 907 908 909"
976              "910 911 912 913 914 915 916 917 918 919"
977              "920 921 922 923 924 925 926 927 928 929"
978              "930 931 932 933 934 935 936 937 938 939"
979              "940 941 942 943 944 945 946 947 948 949"
980              "950 951 952 953 954 955 956 957 958 959"
981              "960 961 962 963 964 965 966 967 968 969"
982              "970 971 972 973 974 975 976 977 978 979"
983              "980 981 982 983 984 985 986 987 988 989"
984              "990 991 992 993 994 995 996 997 998 999";
985--enable_result_log
986prepare long_query from "select ? as long_query";
987--disable_result_log
988execute long_query using @lparam;
989--enable_result_log
990set global general_log = off;
991select command_type, argument from mysql.general_log where thread_id = @thread_id;
992deallocate prepare long_query;
993set global general_log = @old_general_log;
994
995#
996# Bug#34306: Can't make copy of log tables when server binary log is enabled
997#
998
999--disable_warnings
1000DROP TABLE IF EXISTS log_count;
1001DROP TABLE IF EXISTS slow_log_copy;
1002DROP TABLE IF EXISTS general_log_copy;
1003--enable_warnings
1004
1005CREATE TABLE log_count (count BIGINT(21));
1006
1007SET GLOBAL general_log = ON;
1008SET GLOBAL slow_query_log = ON;
1009
1010CREATE TABLE slow_log_copy SELECT * FROM mysql.slow_log;
1011INSERT INTO slow_log_copy SELECT * FROM mysql.slow_log;
1012INSERT INTO log_count (count) VALUES ((SELECT count(*) FROM mysql.slow_log));
1013DROP TABLE slow_log_copy;
1014
1015CREATE TABLE general_log_copy SELECT * FROM mysql.general_log;
1016INSERT INTO general_log_copy SELECT * FROM mysql.general_log;
1017INSERT INTO log_count (count) VALUES ((SELECT count(*) FROM mysql.general_log));
1018DROP TABLE general_log_copy;
1019
1020SET GLOBAL general_log = OFF;
1021SET GLOBAL slow_query_log = OFF;
1022
1023CREATE TABLE slow_log_copy SELECT * FROM mysql.slow_log;
1024INSERT INTO slow_log_copy SELECT * FROM mysql.slow_log;
1025INSERT INTO log_count (count) VALUES ((SELECT count(*) FROM mysql.slow_log));
1026DROP TABLE slow_log_copy;
1027
1028CREATE TABLE general_log_copy SELECT * FROM mysql.general_log;
1029INSERT INTO general_log_copy SELECT * FROM mysql.general_log;
1030INSERT INTO log_count (count) VALUES ((SELECT count(*) FROM mysql.general_log));
1031DROP TABLE general_log_copy;
1032
1033SET GLOBAL general_log = @saved_general_log;
1034SET GLOBAL slow_query_log = @saved_slow_query_log;
1035
1036DROP TABLE log_count;
1037
1038#
1039# Bug #31700: thd->examined_row_count not incremented for 'const' type queries
1040#
1041
1042SET SESSION long_query_time = 0;
1043SET GLOBAL slow_query_log = ON;
1044FLUSH LOGS;
1045TRUNCATE TABLE mysql.slow_log;
1046
1047# Let there be three columns, unique, non-unique, and non-indexed:
1048CREATE TABLE t1 (f1 SERIAL,f2 INT, f3 INT, PRIMARY KEY(f1), KEY(f2));
1049INSERT INTO t1 VALUES (1,1,1);
1050INSERT INTO t1 VALUES (2,2,2);
1051INSERT INTO t1 VALUES (3,3,3);
1052INSERT INTO t1 VALUES (4,4,4);
1053
1054SELECT SQL_NO_CACHE 'Bug#31700 - SCAN',f1,f2,f3,SLEEP(1.1) FROM t1 WHERE f3=4;
1055SELECT SQL_NO_CACHE 'Bug#31700 - KEY', f1,f2,f3,SLEEP(1.1) FROM t1 WHERE f2=3;
1056SELECT SQL_NO_CACHE 'Bug#31700 - PK',  f1,f2,f3,SLEEP(1.1) FROM t1 WHERE f1=2;
1057
1058--replace_column 1 TIMESTAMP
1059SELECT start_time, rows_examined, rows_sent, sql_text FROM mysql.slow_log WHERE sql_text LIKE '%Bug#31700%' ORDER BY start_time;
1060
1061DROP TABLE t1;
1062
1063TRUNCATE TABLE mysql.slow_log;
1064
1065#
1066# Bug #47924 main.log_tables times out sporadically
1067#
1068
1069use mysql;
1070# Should result in error
1071--disable_warnings
1072drop table if exists renamed_general_log;
1073drop table if exists renamed_slow_log;
1074--enable_warnings
1075--error ER_CANT_RENAME_LOG_TABLE
1076RENAME TABLE general_log TO renamed_general_log;
1077--error ER_CANT_RENAME_LOG_TABLE
1078RENAME TABLE slow_log TO renamed_slow_log;
1079
1080use test;
1081flush tables with read lock;
1082unlock tables;
1083
1084SET @@session.long_query_time= @old_long_query_time;
1085
1086SET @@global.log_output=       @old_log_output;
1087SET @@global.slow_query_log=   @old_slow_query_log;
1088SET @@global.general_log=      @old_general_log;
1089