Follow-up from previous patch, which introduced catalog in the GTID event.

With catalog in GTID, there is no longer a need to use the catalog in
Query_log_event or Table_map_log_event. In fact, since catalog must never
change in the middle of a transaction, it is better to _only_ set the
catalog from the GTID event and don't use any catalog info from other
events.

To save space, remove the previously introduced catalog from
Table_map_log_event. (The catalog in Query_log_event is kept as it was there
for long, so as not to change event formats).

Signed-off-by: Kristian Nielsen <kniel...@knielsen-hq.org>
---
 sql/log_event.cc        | 102 ++++++++++-----------------------------
 sql/log_event.h         |  23 ++-------
 sql/log_event_client.cc |  51 ++++++++------------
 sql/log_event_server.cc | 104 +++++++++++++++++++++++-----------------
 4 files changed, 110 insertions(+), 170 deletions(-)

diff --git a/sql/log_event.cc b/sql/log_event.cc
index 004010e9d99..e8bb22b0ecb 100644
--- a/sql/log_event.cc
+++ b/sql/log_event.cc
@@ -1376,7 +1376,7 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
                                  Log_event_type event_type)
   :Log_event(buf, description_event), data_buf(0),
    catalog(0), db(NullS), query(NullS),
-   status_vars_len(0),
+   catalog_len(0), status_vars_len(0),
    flags2_inited(0), sql_mode_inited(0), charset_inited(0), flags2(0),
    auto_increment_increment(1), auto_increment_offset(1),
    time_zone_len(0), lc_time_names_number(0), charset_database_number(0),
@@ -1387,12 +1387,11 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
   uint8 common_header_len, post_header_len;
   Log_event::Byte *start;
   const Log_event::Byte *end;
+  bool catalog_nz= 1;
   DBUG_ENTER("Query_log_event::Query_log_event(char*,...)");
 
   memset(&user, 0, sizeof(user));
   memset(&host, 0, sizeof(host));
-  catalog_name.str= 0;
-  catalog_name.length= 0;
   common_header_len= description_event->common_header_len;
   post_header_len= description_event->post_header_len[event_type-1];
   DBUG_PRINT("info",("event_len: %u  common_header_len: %d  post_header_len: 
%d",
@@ -1461,16 +1460,13 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
     }
     case Q_CATALOG_NZ_CODE:
     {
-      uint length;
       DBUG_PRINT("info", ("case Q_CATALOG_NZ_CODE; pos:%p; end:%p",
                           pos, end));
-      if (get_str_len_and_pointer(&pos, &catalog_name.str, &length, end))
+      if (get_str_len_and_pointer(&pos, &catalog, &catalog_len, end))
       {
         query= 0;
         DBUG_VOID_RETURN;
       }
-      catalog_name.length= length;
-      break;
     }
     break;
     case Q_AUTO_INCREMENT:
@@ -1500,10 +1496,11 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
     case Q_CATALOG_CODE: /* for 5.0.x where 0<=x<=3 masters */
     {
       CHECK_SPACE(pos, end, 1);
-      if ((catalog_name.length= *pos))
-        catalog_name.str= (char*) pos+1;    // Will be copied later
-      CHECK_SPACE(pos, end, catalog_name.length + 2);
-      pos+= catalog_name.length+2;          // leap over end 0
+      if ((catalog_len= *pos))
+        catalog= (char*) pos+1;                           // Will be copied 
later
+      CHECK_SPACE(pos, end, catalog_len + 2);
+      pos+= catalog_len+2; // leap over end 0
+      catalog_nz= 0; // catalog has end 0 in event
     }
     break;
     case Q_LC_TIME_NAMES_CODE:
@@ -1618,7 +1615,7 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
 
 #if !defined(MYSQL_CLIENT) && defined(HAVE_QUERY_CACHE)
   if (!(start= data_buf= (Log_event::Byte*) my_malloc(PSI_INSTRUMENT_ME,
-                                                       catalog_name.length + 1
+                                                       catalog_len + 1
                                                     +  time_zone_len + 1
                                                     +  user.length + 1
                                                     +  host.length + 1
@@ -1630,7 +1627,7 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
                                                        MYF(MY_WME))))
 #else
   if (!(start= data_buf= (Log_event::Byte*) my_malloc(PSI_INSTRUMENT_ME,
-                                                       catalog_name.length + 1
+                                                       catalog_len + 1
                                                     +  time_zone_len + 1
                                                     +  user.length + 1
                                                     +  host.length + 1
@@ -1638,36 +1635,22 @@ Query_log_event::Query_log_event(const uchar *buf, uint 
event_len,
                                                        MYF(MY_WME))))
 #endif
       DBUG_VOID_RETURN;
-
-  /*
-    Ensure we can support old replication clients that are using 'std' as 
catalog name
-    This is also needed to support old mtr test that uses copies of old 
replication logs that
-    still are using 'std'.
-   */
-  if (catalog_name.length == 3 and memcmp(catalog_name.str, "std", 3) == 0)
-    catalog_name.str= "def";
-
-#ifndef MYSQL_CLIENT
-  if (catalog_name.length)                      // If catalog was given
+  if (catalog_len)                                  // If catalog is given
   {
-    if (!(catalog= get_catalog(&catalog_name, 1)))
+    /**
+      @todo we should clean up and do only copy_str_and_move; it
+      works for both cases.  Then we can remove the catalog_nz
+      flag. /sven
+    */
+    if (likely(catalog_nz)) // true except if event comes from 5.0.0|1|2|3.
+      copy_str_and_move(&catalog, &start, catalog_len);
+    else
     {
-      if (!user.str)
-        user.str= "";
-      if (!host.str)
-        host.str= "";
-      my_error(ER_ACCESS_NO_SUCH_CATALOG, MYF(ME_ERROR_LOG),
-               user.str, host.str, catalog_name.length, catalog_name.str);
-      query= 0;
-      DBUG_VOID_RETURN;
+      memcpy(start, catalog, catalog_len+1); // copy end 0
+      catalog= (const char *)start;
+      start+= catalog_len+1;
     }
   }
-  else
-    catalog= default_catalog();
-#endif
-
-  copy_str_and_move(&catalog_name.str, &start, catalog_name.length);
-
   if (time_zone_len)
     copy_str_and_move(&time_zone_str, &start, time_zone_len);
 
@@ -2456,13 +2439,14 @@ Gtid_log_event::Gtid_log_event(const uchar *buf, uint 
event_len,
         return;
       }
       uint32_t cat_len= *buf++;
-      if (unlikely(cat_len > MAX_CATALOG_NAME) ||
+      if (unlikely(cat_len >= MAX_CATALOG_NAME) ||
           unlikely(buf - buf_0 + cat_len) >= event_len)
       {
         seq_no= 0;
         return;
       }
       memcpy(cat_name_buf, buf, cat_len);
+      cat_name_buf[cat_len]= '\0';
       cat_name_int.str= cat_name_buf;
       cat_name_int.length= cat_len;
       cat_name= &cat_name_int;
@@ -3341,7 +3325,6 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
 #ifndef MYSQL_CLIENT
     m_table(NULL),
 #endif
-    m_catalog(NULL),
     m_dbnam(NULL), m_dblen(0), m_tblnam(NULL), m_tbllen(0),
     m_colcnt(0), m_coltype(0),
     m_memory(NULL), m_table_id(ULONGLONG_MAX), m_flags(0),
@@ -3350,7 +3333,6 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
     m_optional_metadata_len(0), m_optional_metadata(NULL)
 {
   unsigned int bytes_read= 0;
-  LEX_CSTRING catalog= {"",0};
   uint8 common_header_len= description_event->common_header_len;
   uint8 post_header_len= description_event->post_header_len[TABLE_MAP_EVENT-1];
   DBUG_ENTER("Table_map_log_event::Table_map_log_event(const char*,uint,...)");
@@ -3359,9 +3341,6 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
                      "post_header_len: %d",
                      event_len, common_header_len, post_header_len));
 
-  m_catnam.str="";
-  m_catnam.length= 0;
-
   /*
     Don't print debug messages when running valgrind since they can
     trigger false warnings.
@@ -3396,35 +3375,7 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
   m_flags= uint2korr(post_start);
 
   /* Read the variable part of the event */
-  const uchar *vpart= buf + common_header_len + post_header_len;
-
-  if (m_flags & TM_BIT_HAS_CATALOG_F)
-  {
-    size_t catalog_len= *vpart;
-    const char *catalog_name= (const char*) vpart + 1;
-
-    catalog.str= catalog_name;
-    catalog.length= catalog_len;
-    if (vpart + catalog_len + 2 > buf + event_len)
-      DBUG_VOID_RETURN;
-
-#ifndef MYSQL_CLIENT
-    if (!(m_catalog= get_catalog(&catalog, 1)))
-    {
-      my_error(ER_ACCESS_NO_SUCH_CATALOG, MYF(ME_ERROR_LOG),
-               "replication", "localhost",
-               catalog_len, catalog_name);
-      DBUG_VOID_RETURN;
-    }
-#endif
-    vpart+= catalog_len+2;
-  }
-#ifndef MYSQL_CLIENT
-  else
-  {
-    m_catalog= default_catalog();
-  }
-#endif /* MYSQL_CLIENT */
+  const uchar *const vpart= buf + common_header_len + post_header_len;
 
   /* Extract the length of the various parts from the buffer */
   uchar const *const ptr_dblen= (uchar const*)vpart + 0;
@@ -3449,7 +3400,6 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
 
   /* Allocate mem for all fields in one go. If fails, caught in is_valid() */
   m_memory= (uchar*) my_multi_malloc(PSI_INSTRUMENT_ME, MYF(MY_WME),
-                                     &m_catnam.str, (uint) catalog.length+1,
                                      &m_dbnam, (uint) m_dblen + 1,
                                      &m_tblnam, (uint) m_tbllen + 1,
                                      &m_coltype, (uint) m_colcnt,
@@ -3458,8 +3408,6 @@ Table_map_log_event::Table_map_log_event(const uchar 
*buf, uint event_len,
   if (m_memory)
   {
     /* Copy the different parts into their memory */
-    memcpy(const_cast<char*>(m_catnam.str), catalog.str, catalog.length+1);
-    m_catnam.length= catalog.length;
     strncpy(const_cast<char*>(m_dbnam), (const char*)ptr_dblen  + 1, m_dblen + 
1);
     strncpy(const_cast<char*>(m_tblnam), (const char*)ptr_tbllen + 1, m_tbllen 
+ 1);
     memcpy(m_coltype, ptr_after_colcnt, m_colcnt);
diff --git a/sql/log_event.h b/sql/log_event.h
index 2f866efca7c..3099d693c8b 100644
--- a/sql/log_event.h
+++ b/sql/log_event.h
@@ -838,7 +838,7 @@ typedef struct st_print_event_info
     them if they are unchanged.
   */
   char db[FN_REFLEN+1]; // TODO: make this a LEX_STRING when thd->db is
-  const SQL_CATALOG *catalog;
+  char catalog_name[MAX_CATALOG_NAME];
   char charset[6]; // 3 variables, each of them storable in 2 bytes
   char time_zone_str[MAX_TIME_ZONE_NAME_LENGTH];
   char delimiter[16];
@@ -2082,8 +2082,7 @@ class Query_log_event: public Log_event
 protected:
   Log_event::Byte* data_buf;
 public:
-  const SQL_CATALOG *catalog;
-  LEX_CSTRING catalog_name;
+  const char *catalog;
   const char *db;
   const char *query;
   /*
@@ -2107,6 +2106,8 @@ class Query_log_event: public Log_event
     concerned) from here.
   */
 
+  uint catalog_len;                    // <= 255 char; 0 means uninited
+
   /*
     We want to be able to store a variable number of N-bit status vars:
     (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
@@ -3791,19 +3792,8 @@ class Annotate_rows_log_event: public Log_event
     <td>Has the following bit values</tdd>
     <td>TM_BIT_LEN_EXACT_F= 1 << 0</td>
     <td>TM_BIT_HAS_TRIGGERS=1 << 14</td>
-    <td>TM_BIT_HAS_CATALOG= 1 << 15</td>
   </tr>
 
-  <tr>
-    <td>catalog_name</td>
-    <td>one byte string length, followed by null-terminated string</td>
-    <td>The name of the catalog in which the table resides.  The name
-    is represented as a one byte unsigned integer representing the
-    number of bytes in the name, followed by length bytes containing
-    the database name, followed by a terminating 0 byte.  (Note the
-    redundancy in the representation of the length.)
-    This entry only exists if bit TM_BIT_HAS_CATALOG is set.</td>
-  </tr>
   </table>
 
   The Body has the following components:
@@ -4362,8 +4352,7 @@ class Table_map_log_event : public Log_event
     TM_NO_FLAGS = 0U,
     TM_BIT_LEN_EXACT_F = (1U << 0),
     // MariaDB flags (we starts from the other end)
-    TM_BIT_HAS_TRIGGERS_F= (1U << 14),
-    TM_BIT_HAS_CATALOG_F=  (1U << 15),
+    TM_BIT_HAS_TRIGGERS_F= (1U << 14)
   };
 
   flag_set get_flags(flag_set flag) const { return m_flags & flag; }
@@ -4468,8 +4457,6 @@ class Table_map_log_event : public Log_event
   class Default_charset_iterator;
   class Column_charset_iterator;
 #endif
-  const SQL_CATALOG   *m_catalog;
-  LEX_CSTRING m_catnam;                      // Used by mysqlbinlog
   char const    *m_dbnam;
   size_t         m_dblen;
   char const    *m_tblnam;
diff --git a/sql/log_event_client.cc b/sql/log_event_client.cc
index 9315fdcf32b..b73dcd5c02c 100644
--- a/sql/log_event_client.cc
+++ b/sql/log_event_client.cc
@@ -1849,14 +1849,6 @@ bool Query_log_event::print_query_header(IO_CACHE* file,
           goto err;
     }
   }
-  if (print_event_info->catalog != catalog)
-  {
-    print_event_info->catalog= catalog;
-    if (my_b_printf(file, "/*!110600 SET CATALOG `%.*s` */%s\n",
-                    catalog_name.length, catalog_name.str,
-                    print_event_info->delimiter))
-      goto err;
-  }
 
   end=int10_to_str((long) when, strmov(buff,"SET TIMESTAMP="),10);
   if (when_sec_part && when_sec_part <= TIME_MAX_SECOND_PART)
@@ -3017,11 +3009,9 @@ int Table_map_log_event::rewrite_db(const char *new_db, 
size_t new_len,
   // m_dbnam resides in m_memory together with m_tblnam and m_coltype
   uchar* memory= m_memory;
   char const* tblnam= m_tblnam;
-  char const* catnam= m_catnam.str;
   uchar* coltype= m_coltype;
 
   m_memory= (uchar*) my_multi_malloc(PSI_NOT_INSTRUMENTED, MYF(MY_WME),
-                                     &m_catnam.str, (uint) m_catnam.length,
                                      &m_dbnam, (uint) m_dblen + 1,
                                      &m_tblnam, (uint) m_tbllen + 1,
                                      &m_coltype, (uint) m_colcnt,
@@ -3030,13 +3020,11 @@ int Table_map_log_event::rewrite_db(const char *new_db, 
size_t new_len,
   if (!m_memory)
   {
     sql_print_error("Table_map_log_event::rewrite_db: "
-                    "failed to allocate new m_memory (%d, %d + %d + %d bytes 
required)",
-                    (int) m_catnam.length + 1,+ m_dblen + 1, m_tbllen + 1,
-                    m_colcnt);
+                    "failed to allocate new m_memory (%d + %d + %d bytes 
required)",
+                    m_dblen + 1, m_tbllen + 1, m_colcnt);
     DBUG_RETURN(-1);
   }
 
-  memcpy((void*)m_catnam.str, catnam, m_catnam.length+1);
   memcpy((void*)m_dbnam, new_db, m_dblen + 1);
   memcpy((void*)m_tblnam, tblnam, m_tbllen + 1);
   memcpy(m_coltype, coltype, m_colcnt);
@@ -3051,26 +3039,13 @@ bool Table_map_log_event::print(FILE *file, 
PRINT_EVENT_INFO *print_event_info)
   if (!print_event_info->short_form)
   {
     char llbuff[22];
-    char options[80], *ptr;
-
-    ptr= options;
-    if (m_flags & (TM_BIT_HAS_TRIGGERS_F | TM_BIT_HAS_CATALOG_F))
-    {
-      ptr= strmov(ptr, " (has ");
-      if (m_flags & TM_BIT_HAS_TRIGGERS_F)
-        ptr= strmov(ptr, "triggers,");
-      if (m_flags & TM_BIT_HAS_CATALOG_F)
-        ptr= strmov(ptr, "catalog,");
-      ptr[-1]= ')';
-      ptr++;
-    }
-    *ptr= 0;
 
     print_header(&print_event_info->head_cache, print_event_info, TRUE);
     if (my_b_printf(&print_event_info->head_cache,
                     "\tTable_map: %`s.%`s mapped to number %s%s\n",
                     m_dbnam, m_tblnam, ullstr(m_table_id, llbuff),
-                    options))
+                    ((m_flags & TM_BIT_HAS_TRIGGERS_F) ?
+                     " (has triggers)" : "")))
       goto err;
   }
   if (!print_event_info->short_form || print_event_info->print_row_count)
@@ -3653,8 +3628,8 @@ st_print_event_info::st_print_event_info()
   bzero(db, sizeof(db));
   bzero(charset, sizeof(charset));
   bzero(time_zone_str, sizeof(time_zone_str));
-  // Set catalog to impossible value to ensure that catalog is updated later!
-  catalog= (SQL_CATALOG *) 1;
+  catalog_name[0]= '\0';
+  catalog_length= 0;
   delimiter[0]= ';';
   delimiter[1]= 0;
   flags2_inited= 0;
@@ -3725,6 +3700,7 @@ Gtid_log_event::print(FILE *file, PRINT_EVENT_INFO 
*print_event_info)
                                Write_on_release_cache::FLUSH_F, this);
   char buf[21];
   char buf2[21];
+  bool different;
 
   if (!print_event_info->short_form && !is_flashback)
   {
@@ -3797,6 +3773,19 @@ Gtid_log_event::print(FILE *file, PRINT_EVENT_INFO 
*print_event_info)
                       buf, print_event_info->delimiter))
         goto err;
   }
+
+  different= print_event_info->catalog_length != cat_name->length ||
+    memcmp(print_event_info->catalog_name, cat_name->str, cat_name->length);
+  if (different)
+  {
+    print_event_info->catalog_length= cat_name->length;
+    memcpy(print_event_info->catalog_name, cat_name->str, cat_name->length);
+    if (my_b_printf(&cache, "/*!110600 SET CATALOG `%.*s` */%s\n",
+                    cat_name->length, cat_name->str,
+                    print_event_info->delimiter))
+      goto err;
+  }
+
   if ((flags2 & FL_PREPARED_XA) && !is_flashback)
   {
     my_b_write_string(&cache, "XA START ");
diff --git a/sql/log_event_server.cc b/sql/log_event_server.cc
index 22ad0a8b4a6..d3a5f616476 100644
--- a/sql/log_event_server.cc
+++ b/sql/log_event_server.cc
@@ -995,21 +995,13 @@ void Query_log_event::pack_info(Protocol *protocol)
 {
   char buf_mem[1024];
   String buf(buf_mem, sizeof(buf_mem), system_charset_info);
-  buf.real_alloc(30 + ((catalog ? catalog->name.length : 0 )+ db_len)*2 + 
q_len);
-  if (!(flags & LOG_EVENT_SUPPRESS_USE_F))
+  buf.real_alloc(9 + db_len + q_len);
+  if (!(flags & LOG_EVENT_SUPPRESS_USE_F)
+      && db && db_len)
   {
-    if (catalog)
-    {
-      buf.append(STRING_WITH_LEN("SET CATALOG "));
-      append_identifier(protocol->thd, &buf, catalog->name.str, 
catalog->name.length);
-      buf.append(STRING_WITH_LEN("; "));
-    }
-    if (db && db_len)
-    {
-      buf.append(STRING_WITH_LEN("use "));
-      append_identifier(protocol->thd, &buf, db, db_len);
-      buf.append(STRING_WITH_LEN("; "));
-    }
+    buf.append(STRING_WITH_LEN("use "));
+    append_identifier(protocol->thd, &buf, db, db_len);
+    buf.append(STRING_WITH_LEN("; "));
   }
 
   DBUG_ASSERT(!flags2 || flags2_inited);
@@ -1139,7 +1131,7 @@ bool Query_log_event::write()
     int8store(start, (ulonglong)sql_mode);
     start+= 8;
   }
-  store_str_with_code_and_len(&start, catalog->name.str, catalog->name.length,
+  store_str_with_code_and_len(&start, catalog, catalog_len,
                               (uint) Q_CATALOG_NZ_CODE);
   /*
     In 5.0.x where x<4 masters we used to store the end zero here. This was
@@ -1383,7 +1375,7 @@ Query_log_event::Query_log_event(THD* thd_arg, const 
char* query_arg,
               ? LOG_EVENT_THREAD_SPECIFIC_F : 0) |
              (suppress_use ? LOG_EVENT_SUPPRESS_USE_F : 0),
             using_trans),
-   data_buf(0),  catalog(thd_arg->catalog), query(query_arg),
+   data_buf(0),  catalog(thd_arg->catalog->name.str), query(query_arg),
    q_len((uint32) query_length),
    thread_id(thd_arg->thread_id),
    /* save the original thread id; we already know the server id */
@@ -1422,6 +1414,11 @@ Query_log_event::Query_log_event(THD* thd_arg, const 
char* query_arg,
 
   end_time= my_time(0);
   exec_time = (ulong) (end_time  - thd_arg->start_time);
+  /**
+    @todo this means that if we have no catalog, then it is replicated
+    as an existing catalog of length zero. is that safe? /sven
+  */
+  catalog_len = (catalog) ? (uint32) strlen(catalog) : 0;
 
   if (!(db= thd->db.str))
     db= "";
@@ -1855,9 +1852,13 @@ int Query_log_event::do_apply_event(rpl_group_info *rgi,
   bool skip_error_check= false;
   DBUG_ENTER("Query_log_event::do_apply_event");
 
-  DBUG_ASSERT(catalog);
-  if (thd->catalog != catalog)
-    thd->change_catalog(const_cast<SQL_CATALOG*>(catalog));
+  /*
+    Colleagues: please never free(thd->catalog) in MySQL. This would
+    lead to bugs as here thd->catalog is a part of an alloced block,
+    not an entire alloced block (see
+    Query_log_event::do_apply_event()). Same for thd->db.  Thank
+    you.
+  */
   rgi->start_alter_ev= this;
 
   size_t valid_len= Well_formed_prefix(system_charset_info,
@@ -2348,11 +2349,6 @@ Query_log_event::do_shall_skip(rpl_group_info *rgi)
   DBUG_ASSERT(query && q_len > 0);
   DBUG_ASSERT(thd == rgi->thd);
 
-  /* Set thd to point to the current catalog */
-  DBUG_ASSERT(catalog);
-  if (thd->catalog != catalog)
-    thd->change_catalog(const_cast<SQL_CATALOG*>(catalog));
-
   /*
     An event skipped due to @@skip_replication must not be counted towards the
     number of events to be skipped due to @@sql_slave_skip_counter.
@@ -3157,9 +3153,22 @@ Gtid_log_event::make_compatible_event(String *packet, 
bool *need_dummy_event,
 void
 Gtid_log_event::pack_info(Protocol *protocol)
 {
-  char buf[6+5+10+1+10+1+20+1+4+20+1+ ser_buf_size+5 /* sprintf */];
-  char *p;
-  p = strmov(buf, (flags2 & FL_STANDALONE  ? "GTID " :
+  static constexpr int catalog_needed= 12 + 2*MAX_CATALOG_NAME+2 + 2;
+  char buf[catalog_needed+6+5+10+1+10+1+20+1+4+20+1+
+           ser_buf_size+5 /* sprintf */];
+  char *p= buf;
+  if (flags_extra & FL_CATALOG)
+  {
+    char buf_mem[2*MAX_CATALOG_NAME+2];
+    String buf2(buf_mem, sizeof(buf_mem), system_charset_info);
+    append_identifier(protocol->thd, &buf2, cat_name->str, cat_name->length);
+    size_t buf2_len= std::min(buf2.length(), (uint32)(2*MAX_CATALOG_NAME+2));
+    DBUG_ASSERT(buf2_len == buf2.length());
+    p= strmov(p, "SET CATALOG ");
+    memcpy(p, buf2.ptr(), buf2_len);
+    p= strmov(p + buf2_len, "; ");
+  }
+  p = strmov(p, (flags2 & FL_STANDALONE  ? "GTID " :
                    flags2 & FL_PREPARED_XA ? "XA START " : "BEGIN GTID "));
   if (flags2 & FL_PREPARED_XA)
   {
@@ -3215,6 +3224,29 @@ Gtid_log_event::do_apply_event(rpl_group_info *rgi)
       return 1;
   }
 
+  if (using_catalogs)
+  {
+    if (flags_extra & FL_CATALOG)
+    {
+      if (!thd->catalog || cmp(cat_name, &thd->catalog->name))
+      {
+        SQL_CATALOG *catalog= get_catalog(cat_name, 1);
+        if (!catalog)
+        {
+          my_error(ER_NO_SUCH_CATALOG, MYF(ME_ERROR_LOG),
+                   cat_name->length, cat_name->str);
+          return 1;
+        }
+        thd->change_catalog(catalog);
+      }
+    }
+    else
+    {
+      if (thd->catalog != default_catalog())
+        thd->change_catalog(default_catalog());
+    }
+  }
+
   DBUG_ASSERT((bits & OPTION_GTID_BEGIN) == 0);
 
   Master_info *mi=rgi->rli->mi;
@@ -5689,8 +5721,6 @@ Table_map_log_event::Table_map_log_event(THD *thd, TABLE 
*tbl, ulong tid,
                                          bool is_transactional)
   : Log_event(thd, 0, is_transactional),
     m_table(tbl),
-    m_catalog(thd->catalog),
-    m_catnam(empty_clex_str),
     m_dbnam(tbl->s->db.str),
     m_dblen(m_dbnam ? tbl->s->db.length : 0),
     m_tblnam(tbl->s->table_name.str),
@@ -5736,11 +5766,6 @@ Table_map_log_event::Table_map_log_event(THD *thd, TABLE 
*tbl, ulong tid,
 
   if (tbl->triggers)
     m_flags|= TM_BIT_HAS_TRIGGERS_F;
-  if (m_catalog != default_catalog())
-  {
-    m_flags|= TM_BIT_HAS_CATALOG_F;
-    m_data_size+= m_catalog->name.length+2;
-  }
 
   /* If malloc fails, caught in is_valid() */
   if ((m_memory= (uchar*) my_malloc(PSI_INSTRUMENT_ME, m_colcnt, MYF(MY_WME))))
@@ -5910,8 +5935,6 @@ int Table_map_log_event::do_apply_event(rpl_group_info 
*rgi)
 
   /* Step the query id to mark what columns that are actually used. */
   thd->set_query_id(next_query_id());
-  if (thd->catalog != m_catalog)
-    thd->change_catalog(const_cast<SQL_CATALOG*>(m_catalog));
 
   if (!(memory= my_multi_malloc(PSI_INSTRUMENT_ME, MYF(MY_WME),
                                 &table_list, (uint) sizeof(RPL_TABLE_LIST),
@@ -5938,7 +5961,7 @@ int Table_map_log_event::do_apply_event(rpl_group_info 
*rgi)
   LEX_CSTRING tmp_db_name=  {db_mem, db_mem_length };
   LEX_CSTRING tmp_tbl_name= {tname_mem, tname_mem_length };
 
-  table_list->init_one_table(m_catalog, &tmp_db_name, &tmp_tbl_name, 0,
+  table_list->init_one_table(thd->catalog, &tmp_db_name, &tmp_tbl_name, 0,
                              TL_WRITE);
   table_list->table_id= DBUG_IF("inject_tblmap_same_id_maps_diff_table") ? 0 : 
m_table_id;
   table_list->updating= 1;
@@ -6079,13 +6102,6 @@ bool Table_map_log_event::write_data_body()
   uchar mbuf[MAX_INT_WIDTH];
   uchar *const mbuf_end= net_store_length(mbuf, m_field_metadata_size);
 
-  if (m_flags & TM_BIT_HAS_CATALOG_F)
-  {
-    uchar const len[]= { (uchar) m_catalog->name.length };
-    if (write_data(len, 1) ||
-        write_data(m_catalog->name.str, m_catalog->name.length+1))
-      return 1;
-  }
   return write_data(dbuf,      sizeof(dbuf)) ||
          write_data(m_dbnam,   m_dblen+1) ||
          write_data(tbuf,      sizeof(tbuf)) ||
-- 
2.39.2

_______________________________________________
commits mailing list -- commits@lists.mariadb.org
To unsubscribe send an email to commits-le...@lists.mariadb.org

Reply via email to