commit bb4b3ee2a7c82b9c619101340a710cc7602ce71e
Author: Monty <monty@mariadb.org>
Date:   Tue May 7 22:03:38 2024 +0300

    fixup! 9831521fc852f72317024778c723e28d121635e6

diff --git a/include/my_base.h b/include/my_base.h
index 9b5c00635bd..c68a72d3b31 100644
--- a/include/my_base.h
+++ b/include/my_base.h
@@ -49,7 +49,6 @@
 #define HA_OPEN_MERGE_TABLE		2048U
 #define HA_OPEN_FOR_CREATE              4096U
 #define HA_OPEN_FOR_DROP                (1U << 13) /* Open part of drop */
-#define HA_OPEN_SIZE_TRACKING           (1U << 14)
 
 /*
   Allow opening even if table is incompatible as this is for ALTER TABLE which
diff --git a/include/my_sys.h b/include/my_sys.h
index 0df12963ab9..3c733917e5f 100644
--- a/include/my_sys.h
+++ b/include/my_sys.h
@@ -184,7 +184,7 @@ void my_large_page_truncate(size_t *size);
 
 struct tmp_file_tracking
 {
-  ulonglong last_position;
+  ulonglong previous_file_size;
   ulonglong file_size;
 };
 
diff --git a/mysql-test/main/status.result b/mysql-test/main/status.result
index b0612eb44e3..7b4c475107a 100644
--- a/mysql-test/main/status.result
+++ b/mysql-test/main/status.result
@@ -183,7 +183,7 @@ show status like 'hand%write%';
 Variable_name	Value
 Handler_tmp_write	0
 Handler_write	0
-show status like '%_tmp%';
+show status like '%\_tmp%';
 Variable_name	Value
 Created_tmp_disk_tables	0
 Created_tmp_files	0
@@ -197,7 +197,7 @@ show status like 'hand%write%';
 Variable_name	Value
 Handler_tmp_write	0
 Handler_write	0
-show status like '%_tmp%';
+show status like '%\_tmp%';
 Variable_name	Value
 Created_tmp_disk_tables	0
 Created_tmp_files	0
diff --git a/mysql-test/main/status.test b/mysql-test/main/status.test
index 9ba22ab2e23..5c8d854b491 100644
--- a/mysql-test/main/status.test
+++ b/mysql-test/main/status.test
@@ -244,9 +244,9 @@ let $rnd_next = `show global status like 'handler_read_rnd_next'`;
 let $tmp_table = `show global status like 'Created_tmp_tables'`;
 show status like 'com_show_status';
 show status like 'hand%write%';
-show status like '%_tmp%';
+show status like '%\_tmp%';
 show status like 'hand%write%';
-show status like '%_tmp%';
+show status like '%\_tmp%';
 show status like 'com_show_status';
 let $rnd_next2 = `show global status like 'handler_read_rnd_next'`;
 let $tmp_table2 = `show global status like 'Created_tmp_tables'`;
diff --git a/mysys/mf_iocache.c b/mysys/mf_iocache.c
index 6214057f9e3..c0914b89ad2 100644
--- a/mysys/mf_iocache.c
+++ b/mysys/mf_iocache.c
@@ -103,6 +103,13 @@ my_bool io_cache_tmp_file_track(IO_CACHE *info, ulonglong file_size)
 }
 
 
+/**
+  End tmp space tracking for the data in the io cache
+
+  This is called when deleting or truncating the
+  cached file.
+*/
+
 void end_tracking_io_cache(IO_CACHE *info)
 {
   if ((info->myflags & (MY_TRACK | MY_TRACK_WITH_LIMIT)) &&
@@ -115,10 +122,11 @@ void end_tracking_io_cache(IO_CACHE *info)
 
 void truncate_io_cache(IO_CACHE *info)
 {
-  if (my_chsize(info->file, 0, 0, MYF(MY_WME)) <= 0)
+  if (my_chsize(info->file, 0, 0, MYF(MY_WME)) == 0)
     end_tracking_io_cache(info);
 }
 
+
 static void
 init_functions(IO_CACHE* info)
 {
@@ -214,7 +222,7 @@ int init_io_cache_ext(IO_CACHE *info, File file, size_t cachesize,
   info->buffer=0;
   info->seek_not_done= 0;
   info->next_file_user= NULL;
-  info->tracking.last_position= 0;
+  info->tracking.previous_file_size= 0;
   info->tracking.file_size= 0;
 
   if (file >= 0)
diff --git a/plugin/feedback/sender_thread.cc b/plugin/feedback/sender_thread.cc
index a9b3330d93a..1b7f4a638fd 100644
--- a/plugin/feedback/sender_thread.cc
+++ b/plugin/feedback/sender_thread.cc
@@ -258,7 +258,7 @@ static void send_report(const char *when)
     */
     server_threads.erase(thd);
     DBUG_ASSERT(thd->status_var.tmp_space_used == 0);
-    thd->set_status_var_init(clear_up_to_memory_used);
+    thd->set_status_var_init(clear_for_new_connection);
     thd->killed= KILL_CONNECTION;
     delete thd;
     thd= 0;
diff --git a/sql/filesort.cc b/sql/filesort.cc
index 5818c3de627..c30521d96cf 100644
--- a/sql/filesort.cc
+++ b/sql/filesort.cc
@@ -400,7 +400,7 @@ SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
 
   if (open_cached_file(&buffpek_pointers, mysql_tmpdir, TEMP_PREFIX,
                        DISK_CHUNK_SIZE,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
     goto err;
 
   param.local_sortorder=
@@ -453,7 +453,7 @@ SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
     /* Open cached file if it isn't open */
     if (!my_b_inited(outfile) &&
         open_cached_file(outfile, mysql_tmpdir, TEMP_PREFIX, DISK_CHUNK_SIZE,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
       goto err;
     if (reinit_io_cache(outfile,WRITE_CACHE,0L,0,0))
       goto err;
@@ -1122,7 +1122,7 @@ write_keys(Sort_param *param,  SORT_INFO *fs_info, uint count,
 
   if (!my_b_inited(tempfile) &&
       open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_CHUNK_SIZE,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
     DBUG_RETURN(1);                                /* purecov: inspected */
   /* check we won't have more buffpeks than we can possibly keep in memory */
   if (my_b_tell(buffpek_pointers) + sizeof(Merge_chunk) > (ulonglong)UINT_MAX)
@@ -1564,7 +1564,7 @@ int merge_many_buff(Sort_param *param, Sort_buffer sort_buffer,
     DBUG_RETURN(0);                             /* purecov: inspected */
   if (flush_io_cache(t_file) ||
       open_cached_file(&t_file2, mysql_tmpdir, TEMP_PREFIX, DISK_CHUNK_SIZE,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
     DBUG_RETURN(1);				/* purecov: inspected */
 
   from_file= t_file; to_file= &t_file2;
diff --git a/sql/handler.cc b/sql/handler.cc
index 81f2011128b..c559b33f9e0 100644
--- a/sql/handler.cc
+++ b/sql/handler.cc
@@ -441,7 +441,6 @@ int ha_init_errors(void)
 #define SETMSG(nr, msg) handler_errmsgs[(nr) - HA_ERR_FIRST]= (msg)
 
   /* Allocate a pointer array for the error message strings. */
-  /* Zerofill it to avoid uninitialized gaps. */
   if (! (handler_errmsgs= (const char**) my_malloc(key_memory_handler_errmsgs,
                                                    HA_ERR_ERRORS * sizeof(char*),
                                                    MYF(MY_WME))))
diff --git a/sql/json_table.cc b/sql/json_table.cc
index da5ab0c450a..09e6e63646d 100644
--- a/sql/json_table.cc
+++ b/sql/json_table.cc
@@ -734,8 +734,8 @@ bool Create_json_table::finalize(THD *thd, TABLE *table,
 
   table->db_stat= HA_OPEN_KEYFILE;
   if (unlikely(table->file->ha_open(table, table->s->path.str, O_RDWR,
-                                    HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE |
-                                    HA_OPEN_SIZE_TRACKING)))
+                                    HA_OPEN_TMP_TABLE |
+                                    HA_OPEN_INTERNAL_TABLE)))
     DBUG_RETURN(true);
 
   table->set_created();
diff --git a/sql/log.cc b/sql/log.cc
index bbcb40db461..18d6719c4ba 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -6126,10 +6126,10 @@ static binlog_cache_mngr *binlog_setup_cache_mngr(THD *thd)
   if (!cache_mngr ||
       open_cached_file(&cache_mngr->stmt_cache.cache_log, mysql_tmpdir,
                        LOG_PREFIX, (size_t)binlog_stmt_cache_size,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)) ||
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)) ||
       open_cached_file(&cache_mngr->trx_cache.cache_log, mysql_tmpdir,
                        LOG_PREFIX, (size_t)binlog_cache_size,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
   {
     my_free(cache_mngr);
     return NULL;
@@ -6281,7 +6281,7 @@ THD::binlog_start_trans_and_stmt()
       Log_event_writer writer(&tmp_io_cache, 0, BINLOG_CHECKSUM_ALG_OFF, NULL);
       if(!open_cached_file(&tmp_io_cache, mysql_tmpdir, TEMP_PREFIX,
                           128,
-                           MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                           MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
       {
         uint64 seqno= this->variables.gtid_seq_no;
         uint32 domain_id= this->variables.gtid_domain_id;
diff --git a/sql/log_event_client.cc b/sql/log_event_client.cc
index 4df6a00626f..0e983448ffc 100644
--- a/sql/log_event_client.cc
+++ b/sql/log_event_client.cc
@@ -1011,8 +1011,7 @@ Rows_log_event::print_verbose_one_row(IO_CACHE *file, table_def *td,
 
         // Using a tmp IO_CACHE to get the value output
         open_cached_file(&tmp_cache, NULL, NULL, 0,
-                         MYF(MY_WME | MY_NABP | MY_TRACK |
-                             MY_TRACK_WITH_LIMIT));
+                         MYF(MY_WME | MY_NABP | MY_TRACK_WITH_LIMIT));
         size= log_event_print_value(&tmp_cache, print_event_info,
                                     is_null ? NULL: value,
                                     td->type(i), td->field_metadata(i),
@@ -1834,7 +1833,7 @@ bool Log_event::print_base64(IO_CACHE* file,
 
         if (open_cached_file(&tmp_cache, NULL, NULL, 0,
                               MYF(MY_WME | MY_NABP |
-                                  MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                                  MY_TRACK_WITH_LIMIT)))
         {
           delete ev;
           goto err;
@@ -3729,7 +3728,7 @@ bool Ignorable_log_event::print(FILE *file,
 */
 st_print_event_info::st_print_event_info()
 {
-  myf const flags = MYF(MY_WME | MY_NABP | MY_TRACK | MY_TRACK_WITH_LIMIT);
+  myf const flags = MYF(MY_WME | MY_NABP | MY_TRACK_WITH_LIMIT);
   /*
     Currently we only use static PRINT_EVENT_INFO objects, so zeroed at
     program's startup, but these explicit bzero() is for the day someone
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 5f40e92afb2..590da51deb4 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -1951,8 +1951,9 @@ static void mysqld_exit(int exit_code)
       SAFEMALLOC_REPORT_MEMORY(0);
   }
   if (global_tmp_space_used)
-    fprintf(stderr, "Warning: tmp_space not freed: %llu\n",
-            (ulonglong) global_tmp_space_used);
+    fprintf(stderr, "Warning: Internal tmp_space accounting error of %lld "
+            "bytes\n",
+            (longlong) global_tmp_space_used);
 
   DBUG_LEAVE;
 #ifdef _WIN32
@@ -3751,21 +3752,23 @@ static int temp_file_size_cb_func(struct tmp_file_tracking *track,
 {
   THD *thd= current_thd;
   int error= 0;
-  longlong size_change= (longlong) (track->file_size - track->last_position);
+  longlong size_change= (longlong) (track->file_size -
+                                    track->previous_file_size);
   DBUG_ENTER("temp_file_size_cb_func");
   DBUG_PRINT("enter", ("last: %llu  current: %llu  diff: %lld",
-                       track->last_position,
+                       track->previous_file_size,
                        track->file_size,
-                       (longlong) (track->file_size - track->last_position)));
+                       (longlong) (track->file_size -
+                                   track->previous_file_size)));
   DBUG_ASSERT(thd);
   if (thd)
   {
     /*
       This has to be true as thd must contain all tmp space used and
       any thus must have been called before with an allocation of
-      track->last_position.
+      track->previous_file_size.
     */
-    DBUG_ASSERT(thd->status_var.tmp_space_used >= track->last_position);
+    DBUG_ASSERT(thd->status_var.tmp_space_used >= track->previous_file_size);
     if (unlikely(thd->shared_thd))
       mysql_mutex_lock(&thd->LOCK_thd_data);
 
@@ -3803,7 +3806,7 @@ static int temp_file_size_cb_func(struct tmp_file_tracking *track,
     DBUG_ASSERT((longlong) thd->status_var.tmp_space_used >= 0);
 
     /* Record that we have registered the change */
-    track->last_position= track->file_size;
+    track->previous_file_size= track->file_size;
 
 exit:
     if (unlikely(thd->shared_thd))
@@ -7654,7 +7657,7 @@ SHOW_VAR status_vars[]= {
   {"Tc_log_page_size",         (char*) &tc_log_page_size,       SHOW_LONG_NOFLUSH},
   {"Tc_log_page_waits",        (char*) &tc_log_page_waits,      SHOW_LONG},
 #endif
-  {"tmp_space_used",           (char*) offsetof(STATUS_VAR, tmp_space_used), SHOW_LONGLONG_STATUS},
+  {"Tmp_space_used",           (char*) offsetof(STATUS_VAR, tmp_space_used), SHOW_LONGLONG_STATUS},
 #ifdef HAVE_POOL_OF_THREADS
   {"Threadpool_idle_threads",  (char *) &show_threadpool_idle_threads, SHOW_SIMPLE_FUNC},
   {"Threadpool_threads",       (char *) &show_threadpool_threads, SHOW_SIMPLE_FUNC},
@@ -7875,7 +7878,7 @@ static int mysql_init_variables(void)
   prepared_stmt_count= 0;
   mysqld_unix_port= opt_mysql_tmpdir= my_bind_addr_str= NullS;
   bzero((uchar*) &mysql_tmpdir_list, sizeof(mysql_tmpdir_list));
-  bzero((char*) &global_status_var, clear_up_to_global_memory_used);
+  bzero((char*) &global_status_var, clear_for_server_start);
   opt_large_pages= 0;
   opt_super_large_pages= 0;
 #if defined(ENABLED_DEBUG_SYNC)
@@ -9197,7 +9200,7 @@ void refresh_session_status(THD *thd)
   mysql_mutex_unlock(&LOCK_status);
 
   /* Reset thread's status variables */
-  thd->set_status_var_init(clear_up_to_tmp_space_used);
+  thd->set_status_var_init(clear_for_flush_status);
   thd->status_var.global_memory_used= 0;
   bzero((uchar*) &thd->org_status_var, sizeof(thd->org_status_var)); 
   thd->start_bytes_received= 0;
@@ -9221,7 +9224,7 @@ void refresh_global_status()
     Reset accoumulated thread's status variables.
     These are the variables in 'status_vars[]' mwith the prefix _STATUS.
  */
-  bzero((char*) &global_status_var, clear_up_to_tmp_space_used);
+  bzero((char*) &global_status_var, clear_for_flush_status);
 
 #ifdef WITH_WSREP
   if (WSREP_ON)
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index cc386b0dd50..04ec2447f27 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -1316,7 +1316,7 @@ void THD::init()
   reset_current_stmt_binlog_format_row();
   reset_binlog_local_stmt_filter();
   /* local_memory_used was setup in THD::THD() */
-  set_status_var_init(clear_up_to_memory_used);
+  set_status_var_init(clear_for_new_connection);
   status_var.max_local_memory_used= status_var.local_memory_used;
   bzero((char *) &org_status_var, sizeof(org_status_var));
   status_in_global= 0;
@@ -4573,7 +4573,7 @@ void thd_increment_bytes_received(void *thd, size_t length)
 /*
   Clear status variables
 
-  @param offset How much to clear. See clear_up_to_tmp_space_used
+  @param offset How much to clear. See clear_for_flush_status
 */
 
 void THD::set_status_var_init(ulong offset)
diff --git a/sql/sql_class.h b/sql/sql_class.h
index bbf90313d8c..6ac21a69808 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -1059,6 +1059,8 @@ typedef struct system_status_var
   double last_query_cost;
   double cpu_time, busy_time;
   uint32 threads_running;
+
+  /* Following variables are not cleared by FLUSH STATUS */
   ulonglong max_tmp_space_used;
   /* Memory used for thread local storage */
   int64 max_local_memory_used;
@@ -1083,12 +1085,12 @@ typedef struct system_status_var
 
 #define STATUS_OFFSET(A) offsetof(STATUS_VAR,A)
 /* Clear as part of flush */
-#define clear_up_to_tmp_space_used      STATUS_OFFSET(tmp_space_used)
+#define clear_for_flush_status      STATUS_OFFSET(tmp_space_used)
 /* Clear as part of startup */
-#define clear_up_to_memory_used         STATUS_OFFSET(local_memory_used)
+#define clear_for_new_connection         STATUS_OFFSET(local_memory_used)
 /* Full initialization. Note that global_memory_used is updated early! */
-#define clear_up_to_global_memory_used  STATUS_OFFSET(global_memory_used)
-#define last_restored_status_var        clear_up_to_tmp_space_used
+#define clear_for_server_start  STATUS_OFFSET(global_memory_used)
+#define last_restored_status_var        clear_for_flush_status
 
 
 /** Number of contiguous global status variables */
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index d44bc16dbaa..77254f63d71 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -22278,8 +22278,7 @@ bool open_tmp_table(TABLE *table)
   int error;
   if (unlikely((error= table->file->ha_open(table, table->s->path.str, O_RDWR,
                                             HA_OPEN_TMP_TABLE |
-                                            HA_OPEN_INTERNAL_TABLE |
-                                            HA_OPEN_SIZE_TRACKING))))
+                                            HA_OPEN_INTERNAL_TABLE))))
   {
     table->file->print_error(error, MYF(0)); /* purecov: inspected */
     table->db_stat= 0;
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index 94a7edfb77c..7d8a94bd03d 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -1581,7 +1581,7 @@ class Stat_table_write_iter
 
     if (open_cached_file(&io_cache, mysql_tmpdir, TEMP_PREFIX,
                          1024,
-                         MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                         MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
       return true;
 
     handler *h= owner->stat_file;
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 0354da98ff9..53376d48b5b 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -738,7 +738,7 @@ bool Sql_cmd_update::update_single_table(THD *thd)
       IO_CACHE tempfile;
       if (open_cached_file(&tempfile, mysql_tmpdir,TEMP_PREFIX,
                            DISK_CHUNK_SIZE,
-                           MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                           MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
         goto err;
 
       /* If quick select is used, initialize it before retrieving rows. */
diff --git a/sql/uniques.cc b/sql/uniques.cc
index 44d93bb0f88..8d05efe46a7 100644
--- a/sql/uniques.cc
+++ b/sql/uniques.cc
@@ -104,7 +104,7 @@ Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg,
     max_elements= 1;
 
   (void) open_cached_file(&file, mysql_tmpdir, TEMP_PREFIX, DISK_CHUNK_SIZE,
-                          MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT));
+                          MYF(MY_WME | MY_TRACK_WITH_LIMIT));
 }
 
 
@@ -720,7 +720,7 @@ bool Unique::merge(TABLE *table, uchar *buff, size_t buff_size,
   /* Open cached file for table records if it isn't open */
   if (! my_b_inited(outfile) &&
       open_cached_file(outfile, mysql_tmpdir, TEMP_PREFIX, DISK_CHUNK_SIZE,
-                       MYF(MY_WME | MY_TRACK | MY_TRACK_WITH_LIMIT)))
+                       MYF(MY_WME | MY_TRACK_WITH_LIMIT)))
     return 1;
 
   bzero((char*) &sort_param,sizeof(sort_param));
diff --git a/storage/maria/ma_delete_all.c b/storage/maria/ma_delete_all.c
index ed2087d3091..15f3fb34223 100644
--- a/storage/maria/ma_delete_all.c
+++ b/storage/maria/ma_delete_all.c
@@ -131,9 +131,9 @@ int maria_delete_all_rows(MARIA_HA *info)
 
   if (_ma_flush_table_files(info, MARIA_FLUSH_DATA|MARIA_FLUSH_INDEX,
                             FLUSH_IGNORE_CHANGED, FLUSH_IGNORE_CHANGED) ||
-      mysql_file_chsize(info->dfile.file, 0, 0, MYF(MY_WME)) ||
+      mysql_file_chsize(info->dfile.file, 0, 0, MYF(MY_WME)) > 0 ||
       mysql_file_chsize(share->kfile.file, share->base.keystart, 0,
-                        MYF(MY_WME)))
+                        MYF(MY_WME)) > 0)
     goto err;
 
   if (info->s->tracked)
diff --git a/storage/maria/ma_open.c b/storage/maria/ma_open.c
index f9a4d7b51a3..d27eddc6f3d 100644
--- a/storage/maria/ma_open.c
+++ b/storage/maria/ma_open.c
@@ -993,7 +993,7 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags,
       share->w_locks++;			/* We don't have to update status */
       share->tot_locks++;
     }
-    share->tracked= MY_TEST(open_flags & HA_OPEN_SIZE_TRACKING);
+    share->tracked= MY_TEST(internal_table);
 
     _ma_set_index_pagecache_callbacks(&share->kfile, share);
     share->this_process=(ulong) getpid();
