The following patch adds a "SHOW LAST_BINLOG_EVENT" command to MySQL, to show you the binlog filename and event position of the last event that went into the binlog for the current thread. For example: mysql> show last_binlog_event; +---------------------------------+----------+ | Binlog | Position | +---------------------------------+----------+ | /var/log/mysql/mysql-bin.000002 | 79 | +---------------------------------+----------+ 1 row in set (0.00 sec) On initial thread creation, values are NULL: mysql> show last_binlog_event; +--------+----------+ | Binlog | Position | +--------+----------+ | NULL | NULL | +--------+----------+ 1 row in set (0.00 sec) -- Brad Fitzpatrick brad@danga.com July 20, 2004 diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/libmysqld/log.cc mysql-4.1.3-lastbinlog/libmysqld/log.cc --- mysql-4.1.3-beta/libmysqld/log.cc 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/libmysqld/log.cc 2004-07-20 13:20:22.000000000 -0700 @@ -1372,6 +1372,15 @@ if (event_info->write(file)) goto err; + /* keep track of this thread's last binlog event's file and position */ + if (thd) + { + thd->last_binlog_pos = event_info->log_pos; + memcpy(&thd->last_binlog_name, + log_file_name, + FN_REFLEN); + } + /* Write log events to reset the 'run environment' of the SQL command */ if (thd && thd->options & OPTION_NO_FOREIGN_KEY_CHECKS) diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/libmysqld/sql_class.cc mysql-4.1.3-lastbinlog/libmysqld/sql_class.cc --- mysql-4.1.3-beta/libmysqld/sql_class.cc 2004-06-27 14:56:50.000000000 -0700 +++ mysql-4.1.3-lastbinlog/libmysqld/sql_class.cc 2004-07-20 14:56:26.000000000 -0700 @@ -288,6 +288,8 @@ warn_list.empty(); bzero((char*) warn_count, sizeof(warn_count)); total_warn_count= 0; + last_binlog_name[0] = '\0'; + last_binlog_pos = 0; update_charset(); } diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/libmysqld/sql_parse.cc mysql-4.1.3-lastbinlog/libmysqld/sql_parse.cc --- mysql-4.1.3-beta/libmysqld/sql_parse.cc 2004-06-27 14:56:49.000000000 -0700 +++ mysql-4.1.3-lastbinlog/libmysqld/sql_parse.cc 2004-07-20 13:52:57.000000000 -0700 @@ -2950,6 +2950,9 @@ case SQLCOM_SHOW_OPEN_TABLES: res= mysqld_show_open_tables(thd,(lex->wild ? lex->wild->ptr() : NullS)); break; + case SQLCOM_SHOW_LAST_BINLOG_EVENT: + res= mysqld_show_last_binlog_event(thd); + break; case SQLCOM_SHOW_CHARSETS: res= mysqld_show_charsets(thd,(lex->wild ? lex->wild->ptr() : NullS)); break; diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/libmysqld/sql_show.cc mysql-4.1.3-lastbinlog/libmysqld/sql_show.cc --- mysql-4.1.3-beta/libmysqld/sql_show.cc 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/libmysqld/sql_show.cc 2004-07-20 14:57:00.000000000 -0700 @@ -130,6 +130,39 @@ DBUG_RETURN(0); } +/*************************************************************************** + List the last binlog event +***************************************************************************/ + +int mysqld_show_last_binlog_event(THD *thd) +{ + List field_list; + Protocol *protocol= thd->protocol; + DBUG_ENTER("mysqld_show_last_binlog_event"); + + field_list.push_back(new Item_empty_string("Binlog", NAME_LEN)); + field_list.push_back(new Item_return_int("Position", 1, MYSQL_TYPE_LONG)); + + if (protocol->send_fields(&field_list,1)) + DBUG_RETURN(1); + + protocol->prepare_for_resend(); + if (thd->last_binlog_name[0]) { + protocol->store(&thd->last_binlog_name[0], system_charset_info); + protocol->store_long((longlong) thd->last_binlog_pos); + } else { + protocol->store_null(); + protocol->store_null(); + } + if (protocol->write()) + { + DBUG_RETURN(-1); + } + + send_eof(thd); + DBUG_RETURN(0); +} + /*************************************************************************** ** List all tables in a database (fast version) diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/lex.h mysql-4.1.3-lastbinlog/sql/lex.h --- mysql-4.1.3-beta/sql/lex.h 2004-06-27 14:56:50.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/lex.h 2004-07-20 13:35:16.000000000 -0700 @@ -237,6 +237,7 @@ { "KEYS", SYM(KEYS)}, { "KILL", SYM(KILL_SYM)}, { "LAST", SYM(LAST_SYM)}, + { "LAST_BINLOG_EVENT",SYM(LAST_BINLOG_EVENT_SYM)}, { "LEADING", SYM(LEADING)}, { "LEAVES", SYM(LEAVES)}, { "LEFT", SYM(LEFT)}, diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/log.cc mysql-4.1.3-lastbinlog/sql/log.cc --- mysql-4.1.3-beta/sql/log.cc 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/log.cc 2004-07-20 13:20:22.000000000 -0700 @@ -1372,6 +1372,15 @@ if (event_info->write(file)) goto err; + /* keep track of this thread's last binlog event's file and position */ + if (thd) + { + thd->last_binlog_pos = event_info->log_pos; + memcpy(&thd->last_binlog_name, + log_file_name, + FN_REFLEN); + } + /* Write log events to reset the 'run environment' of the SQL command */ if (thd && thd->options & OPTION_NO_FOREIGN_KEY_CHECKS) diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/mysql_priv.h mysql-4.1.3-lastbinlog/sql/mysql_priv.h --- mysql-4.1.3-beta/sql/mysql_priv.h 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/mysql_priv.h 2004-07-20 13:53:58.000000000 -0700 @@ -616,6 +616,7 @@ /* sql_show.cc */ int mysqld_show_dbs(THD *thd,const char *wild); int mysqld_show_open_tables(THD *thd,const char *wild); +int mysqld_show_last_binlog_event(THD *thd); int mysqld_show_tables(THD *thd,const char *db,const char *wild); int mysqld_extend_show_tables(THD *thd,const char *db,const char *wild); int mysqld_show_fields(THD *thd,TABLE_LIST *table, const char *wild, diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/mysqld.cc mysql-4.1.3-lastbinlog/sql/mysqld.cc --- mysql-4.1.3-beta/sql/mysqld.cc 2004-06-27 14:56:49.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/mysqld.cc 2004-07-20 13:52:39.000000000 -0700 @@ -5174,6 +5174,7 @@ {"Select_range", (char*) &select_range_count, SHOW_LONG}, {"Select_range_check", (char*) &select_range_check_count, SHOW_LONG}, {"Select_scan", (char*) &select_scan_count, SHOW_LONG}, + {"Show_last_binlog_event", (char*) (com_stat+(uint) SQLCOM_SHOW_LAST_BINLOG_EVENT),SHOW_LONG}, {"Slave_open_temp_tables", (char*) &slave_open_temp_tables, SHOW_LONG}, {"Slave_running", (char*) 0, SHOW_SLAVE_RUNNING}, {"Slow_launch_threads", (char*) &slow_launch_threads, SHOW_LONG}, diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_class.cc mysql-4.1.3-lastbinlog/sql/sql_class.cc --- mysql-4.1.3-beta/sql/sql_class.cc 2004-06-27 14:56:50.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_class.cc 2004-07-20 14:56:26.000000000 -0700 @@ -288,6 +288,8 @@ warn_list.empty(); bzero((char*) warn_count, sizeof(warn_count)); total_warn_count= 0; + last_binlog_name[0] = '\0'; + last_binlog_pos = 0; update_charset(); } diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_class.h mysql-4.1.3-lastbinlog/sql/sql_class.h --- mysql-4.1.3-beta/sql/sql_class.h 2004-06-27 14:57:00.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_class.h 2004-07-20 13:10:22.000000000 -0700 @@ -852,6 +852,10 @@ long long_value; } sys_var_tmp; + /* keep track of this thread's last binlog event's file and position */ + char last_binlog_name[FN_REFLEN]; + my_off_t last_binlog_pos; + THD(); ~THD(); diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_lex.h mysql-4.1.3-lastbinlog/sql/sql_lex.h --- mysql-4.1.3-beta/sql/sql_lex.h 2004-06-27 14:56:48.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_lex.h 2004-07-20 13:50:35.000000000 -0700 @@ -77,6 +77,7 @@ SQLCOM_HELP, SQLCOM_DROP_USER, SQLCOM_REVOKE_ALL, SQLCOM_CHECKSUM, SQLCOM_PREPARE, SQLCOM_EXECUTE, SQLCOM_DEALLOCATE_PREPARE, + SQLCOM_SHOW_LAST_BINLOG_EVENT, /* This should be the last !!! */ SQLCOM_END }; diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_parse.cc mysql-4.1.3-lastbinlog/sql/sql_parse.cc --- mysql-4.1.3-beta/sql/sql_parse.cc 2004-06-27 14:56:49.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_parse.cc 2004-07-20 13:52:57.000000000 -0700 @@ -2950,6 +2950,9 @@ case SQLCOM_SHOW_OPEN_TABLES: res= mysqld_show_open_tables(thd,(lex->wild ? lex->wild->ptr() : NullS)); break; + case SQLCOM_SHOW_LAST_BINLOG_EVENT: + res= mysqld_show_last_binlog_event(thd); + break; case SQLCOM_SHOW_CHARSETS: res= mysqld_show_charsets(thd,(lex->wild ? lex->wild->ptr() : NullS)); break; diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_show.cc mysql-4.1.3-lastbinlog/sql/sql_show.cc --- mysql-4.1.3-beta/sql/sql_show.cc 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_show.cc 2004-07-20 14:57:00.000000000 -0700 @@ -130,6 +130,39 @@ DBUG_RETURN(0); } +/*************************************************************************** + List the last binlog event +***************************************************************************/ + +int mysqld_show_last_binlog_event(THD *thd) +{ + List field_list; + Protocol *protocol= thd->protocol; + DBUG_ENTER("mysqld_show_last_binlog_event"); + + field_list.push_back(new Item_empty_string("Binlog", NAME_LEN)); + field_list.push_back(new Item_return_int("Position", 1, MYSQL_TYPE_LONG)); + + if (protocol->send_fields(&field_list,1)) + DBUG_RETURN(1); + + protocol->prepare_for_resend(); + if (thd->last_binlog_name[0]) { + protocol->store(&thd->last_binlog_name[0], system_charset_info); + protocol->store_long((longlong) thd->last_binlog_pos); + } else { + protocol->store_null(); + protocol->store_null(); + } + if (protocol->write()) + { + DBUG_RETURN(-1); + } + + send_eof(thd); + DBUG_RETURN(0); +} + /*************************************************************************** ** List all tables in a database (fast version) diff -ur -x '*sql_yacc.cc' -x '*fill_help*' -x '*mysqlbug*' -x '*sql_yacc.h' mysql-4.1.3-beta/sql/sql_yacc.yy mysql-4.1.3-lastbinlog/sql/sql_yacc.yy --- mysql-4.1.3-beta/sql/sql_yacc.yy 2004-06-27 14:56:51.000000000 -0700 +++ mysql-4.1.3-lastbinlog/sql/sql_yacc.yy 2004-07-20 13:34:08.000000000 -0700 @@ -266,6 +266,7 @@ %token JOIN_SYM %token KEYS %token KEY_SYM +%token LAST_BINLOG_EVENT_SYM %token LEADING %token LEAST_SYM %token LEAVES @@ -4319,6 +4320,8 @@ { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); } | opt_full PROCESSLIST_SYM { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;} + | opt_full LAST_BINLOG_EVENT_SYM + { Lex->sql_command= SQLCOM_SHOW_LAST_BINLOG_EVENT;} | opt_var_type VARIABLES wild { THD *thd= YYTHD;