Bug#27857 (Log tables supplies the wrong value for generating AUTO_INCREMENT

numbers)

Before this patch, the code in the class Log_to_csv_event_handler, which is
used by the global LOGGER object to write to the tables mysql.slow_log and
mysql_general_log, was supporting only records of the format defined for
these tables in the database creation scripts.

Also before this patch, the server would allow, with certain limitations,
to perform ALTER TABLE on the LOG TABLES.

As implemented, the behavior of the server, with regards to LOG TABLES,
is inconsistent:
- either ALTER TABLES on LOG TABLES should be prohibited,
and the code writing to these tables can make assumptions on the record
format,
- or ALTER TABLE on LOG TABLES is permitted, in which case the code
writing a record to these tables should be more flexible and honor
new fields.

In particular, adding an AUTO_INCREMENT column to the logs,
does not work as expected (per the bug report).

Given that the ALTER TABLE on log tables statement has been explicitly
implemented to check that the log should be off to perform the operation,
and that current test cases already cover this, the user expectation is
already set that this is a "feature" and should be supported.

With this patch, the server will:
- populate AUTO INCREMENT columns if present,
- populate any additional column with it's default value
when writing a record to the LOG TABLES.

Tests are provided, that detail the precise sequence of statements
a SUPER user might want to perform to add more columns to the log tables.
parent daf0b927
......@@ -287,3 +287,71 @@ slow_log
slow_log_new
drop table slow_log_new, general_log_new;
use test;
SET GLOBAL LOG_OUTPUT = 'TABLE';
SET GLOBAL general_log = 0;
FLUSH LOGS;
TRUNCATE TABLE mysql.general_log;
ALTER TABLE mysql.general_log ENGINE = MyISAM;
ALTER TABLE mysql.general_log
ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
SET GLOBAL general_log = 1;
FLUSH LOGS;
SELECT * FROM mysql.general_log;
event_time user_host thread_id server_id command_type argument seq
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query FLUSH LOGS 1
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 2
SELECT * FROM mysql.general_log;
event_time user_host thread_id server_id command_type argument seq
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query FLUSH LOGS 1
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 2
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 3
SELECT "My own query 1";
My own query 1
My own query 1
SELECT "My own query 2";
My own query 2
My own query 2
SELECT * FROM mysql.general_log;
event_time user_host thread_id server_id command_type argument seq
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query FLUSH LOGS 1
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 2
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 3
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT "My own query 1" 4
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT "My own query 2" 5
EVENT_TIME USER_HOST THREAD_ID SERVER_ID Query SELECT * FROM mysql.general_log 6
SET GLOBAL general_log = 0;
FLUSH LOGS;
ALTER TABLE mysql.general_log DROP COLUMN seq;
ALTER TABLE mysql.general_log ENGINE = CSV;
SET @old_long_query_time:=@@long_query_time;
SET GLOBAL slow_query_log = 0;
FLUSH LOGS;
TRUNCATE TABLE mysql.slow_log;
ALTER TABLE mysql.slow_log ENGINE = MyISAM;
ALTER TABLE mysql.slow_log
ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
SET SESSION long_query_time = 1;
SET GLOBAL slow_query_log = 1;
FLUSH LOGS;
SELECT "My own slow query", sleep(2);
My own slow query sleep(2)
My own slow query 0
SELECT "My own slow query", sleep(2);
My own slow query sleep(2)
My own slow query 0
SELECT "My own slow query", sleep(2);
My own slow query sleep(2)
My own slow query 0
SELECT "My own slow query", sleep(2);
My own slow query sleep(2)
My own slow query 0
SELECT * FROM mysql.slow_log WHERE seq >= 2 LIMIT 3;
start_time user_host query_time lock_time rows_sent rows_examined db last_insert_id insert_id server_id sql_text seq
START_TIME USER_HOST QUERY_TIME 00:00:00 1 0 test NULL NULL 1 SELECT "My own slow query", sleep(2) 2
START_TIME USER_HOST QUERY_TIME 00:00:00 1 0 test NULL NULL 1 SELECT "My own slow query", sleep(2) 3
START_TIME USER_HOST QUERY_TIME 00:00:00 1 0 test NULL NULL 1 SELECT "My own slow query", sleep(2) 4
SET GLOBAL slow_query_log = 0;
SET SESSION long_query_time =@old_long_query_time;
FLUSH LOGS;
ALTER TABLE mysql.slow_log DROP COLUMN seq;
ALTER TABLE mysql.slow_log ENGINE = CSV;
......@@ -400,6 +400,76 @@ show tables like "%log%";
drop table slow_log_new, general_log_new;
use test;
#
# Bug#27857 (Log tables supplies the wrong value for generating
# AUTO_INCREMENT numbers)
#
SET GLOBAL LOG_OUTPUT = 'TABLE';
## test the general log
SET GLOBAL general_log = 0;
FLUSH LOGS;
TRUNCATE TABLE mysql.general_log;
ALTER TABLE mysql.general_log ENGINE = MyISAM;
ALTER TABLE mysql.general_log
ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
SET GLOBAL general_log = 1;
FLUSH LOGS;
--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
SELECT * FROM mysql.general_log;
--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
SELECT * FROM mysql.general_log;
SELECT "My own query 1";
SELECT "My own query 2";
--replace_column 1 EVENT_TIME 2 USER_HOST 3 THREAD_ID 4 SERVER_ID
SELECT * FROM mysql.general_log;
SET GLOBAL general_log = 0;
FLUSH LOGS;
ALTER TABLE mysql.general_log DROP COLUMN seq;
ALTER TABLE mysql.general_log ENGINE = CSV;
## test the slow query log
SET @old_long_query_time:=@@long_query_time;
SET GLOBAL slow_query_log = 0;
FLUSH LOGS;
TRUNCATE TABLE mysql.slow_log;
ALTER TABLE mysql.slow_log ENGINE = MyISAM;
ALTER TABLE mysql.slow_log
ADD COLUMN seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
SET SESSION long_query_time = 1;
SET GLOBAL slow_query_log = 1;
FLUSH LOGS;
## FLUSH LOGS above might be slow, so the following is
## logged as either seq 1-4 or seq 2-5
SELECT "My own slow query", sleep(2);
SELECT "My own slow query", sleep(2);
SELECT "My own slow query", sleep(2);
SELECT "My own slow query", sleep(2);
## So we look for seq 2-4
--replace_column 1 START_TIME 2 USER_HOST 3 QUERY_TIME
SELECT * FROM mysql.slow_log WHERE seq >= 2 LIMIT 3;
SET GLOBAL slow_query_log = 0;
SET SESSION long_query_time =@old_long_query_time;
FLUSH LOGS;
ALTER TABLE mysql.slow_log DROP COLUMN seq;
ALTER TABLE mysql.slow_log ENGINE = CSV;
# kill all connections
disconnect con1;
disconnect con2;
......
......@@ -305,6 +305,9 @@ bool Log_to_csv_event_handler::open_log_table(uint log_table_type)
table->table->use_all_columns();
table->table->locked_by_logger= TRUE;
table->table->no_replicate= TRUE;
/* Honor next number columns if present */
table->table->next_number_field= table->table->found_next_number_field;
}
/* restore thread settings */
if (curr)
......@@ -438,6 +441,7 @@ bool Log_to_csv_event_handler::
CHARSET_INFO *client_cs)
{
TABLE *table= general_log.table;
int field_index;
/*
"INSERT INTO general_log" can generate warning sometimes.
......@@ -488,6 +492,12 @@ bool Log_to_csv_event_handler::
table->field[4]->set_notnull();
table->field[5]->set_notnull();
/* Set any extra columns to their default values */
for (field_index= 6 ; field_index < table->s->fields ; field_index++)
{
table->field[field_index]->set_default();
}
/* log table entries are not replicated at the moment */
tmp_disable_binlog(current_thd);
......@@ -1329,6 +1339,7 @@ void Log_to_csv_event_handler::
/* close the table */
log_thd->store_globals();
table->table->file->ha_rnd_end();
table->table->file->ha_release_auto_increment();
/* discard logger mark before unlock*/
table->table->locked_by_logger= FALSE;
close_thread_tables(log_thd, lock_in_use);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment