Bug #57028 Add auxiliary function to retrieve THD::query_id
Submitted: 27 Sep 2010 2:11 Modified: 27 Sep 2010 5:20
Reporter: Kieron Briggs Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Storage Engine API Severity:S4 (Feature request)
Version: OS:Any
Assigned to: CPU Architecture:Any

[27 Sep 2010 2:11] Kieron Briggs
Description:

I am instrumenting some calls in a storage engine plugin for performance analysis, and would like to be able to use the query ID to distinguish separate statements. It would be good to have an accessor function, similar to thd_get_thread_id(), to return the query ID.

I have attached a patch (against 5.1.48, but I don't expect any problems applying it to newer versions) to add such a function.

This is almost identical to Bug #30930, except for THD::query_id instead of THD::thread_id.

How to repeat:
N/A

Suggested fix:
diff -rub mysql-5.1.48.orig/include/mysql/plugin.h mysql-5.1.48/include/mysql/plugin.h
--- mysql-5.1.48.orig/include/mysql/plugin.h	2010-06-04 01:50:08.000000000 +1000
+++ mysql-5.1.48/include/mysql/plugin.h	2010-09-27 11:16:01.000000000 +1000
@@ -733,6 +733,14 @@
 */
 unsigned long thd_get_thread_id(const MYSQL_THD thd);
 
+/**
+  Return the query id of a user thread
+
+  @param thd  user thread connection handle
+  @return  query id
+*/
+unsigned long thd_get_query_id(const MYSQL_THD thd);
+
 
 /**
   Allocate memory in the connection's local memory pool
diff -rub mysql-5.1.48.orig/include/mysql/plugin.h.pp mysql-5.1.48/include/mysql/plugin.h.pp
--- mysql-5.1.48.orig/include/mysql/plugin.h.pp	2010-06-04 01:50:07.000000000 +1000
+++ mysql-5.1.48/include/mysql/plugin.h.pp	2010-09-27 11:28:04.000000000 +1000
@@ -125,6 +125,7 @@
 int mysql_tmpfile(const char *prefix);
 int thd_killed(const void* thd);
 unsigned long thd_get_thread_id(const void* thd);
+unsigned long thd_get_query_id(const void* thd);
 void *thd_alloc(void* thd, unsigned int size);
 void *thd_calloc(void* thd, unsigned int size);
 char *thd_strdup(void* thd, const char *str);
diff -rub mysql-5.1.48.orig/sql/sql_class.cc mysql-5.1.48/sql/sql_class.cc
--- mysql-5.1.48.orig/sql/sql_class.cc	2010-06-04 01:50:11.000000000 +1000
+++ mysql-5.1.48/sql/sql_class.cc	2010-09-27 11:15:11.000000000 +1000
@@ -3051,6 +3051,16 @@
   return((unsigned long)thd->thread_id);
 }
 
+/**
+  Return the query id of a user thread
+  @param thd user thread
+  @return query id
+*/
+extern "C" unsigned long thd_get_query_id(const MYSQL_THD thd)
+{
+  return((unsigned long)thd->query_id);
+}
+
 
 #ifdef INNODB_COMPATIBILITY_HOOKS
 extern "C" struct charset_info_st *thd_charset(MYSQL_THD thd)
[27 Sep 2010 5:20] Valeriy Kravchuk
Thank you for the feature request and code contributed.