Hi Sergey, Thanks for offering to help with the memory leak. Here are the details. Tree is here: lp:~maria-captains/maria/mariadb-5.1-knielsen This tree is the merge with MySQL-5.1.44, including your fix for the uninitialised variable in table elimination. The test case is the following, which is a simplified version of main.union (so main.union shows the same memory leak): -------------------------------- cut here -------------------------------- CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a)); INSERT INTO t1 VALUES (1),(2); CREATE TABLE t2 (b INT); INSERT INTO t2 VALUES (1),(2); EXPLAIN EXTENDED SELECT * FROM t1 UNION SELECT * FROM t1 ORDER BY (SELECT a FROM t2 WHERE b = 12); DROP TABLE t1,t2; -------------------------------- cut here -------------------------------- Here is the stack trace from Valgrind: main.knielsen [ pass ] 1604 ***Warnings generated in error logs during shutdown after running tests: main.knielsen ==11409== ==11409== 1,440 bytes in 1 blocks are definitely lost in loss record 7 of 7 ==11409== at 0x4C22FAB: malloc (vg_replace_malloc.c:207) ==11409== by 0xB3DD44: my_malloc (my_malloc.c:37) ==11409== by 0xB4D078: init_dynamic_array2 (array.c:64) ==11409== by 0x7146BA: update_ref_and_keys(THD*, st_dynamic_array*, st_join_table*, unsigned, Item*, COND_EQUAL*, unsigned long long, st_select_lex*, st_sargable_param**) (sql_select.cc:3930) ==11409== by 0x7156C5: make_join_statistics(JOIN*, TABLE_LIST*, Item*, st_dynamic_array*) (sql_select.cc:2721) ==11409== by 0x717998: JOIN::optimize() (sql_select.cc:1002) ==11409== by 0x71BC37: mysql_select(THD*, Item***, TABLE_LIST*, unsigned, List<Item>&, Item*, unsigned, st_order*, st_order*, Item*, st_order*, unsigned long long, select_result*, st_select_lex_unit*, st_select_lex*) (sql_select.cc:2473) ==11409== by 0x71C1C5: mysql_explain_union(THD*, st_select_lex_unit*, select_result*) (sql_select.cc:16946) ==11409== by 0x71E942: select_describe(JOIN*, bool, bool, bool, char const*) (sql_select.cc:16887) ==11409== by 0x71F60F: JOIN::exec() (sql_select.cc:1837) ==11409== by 0x846859: st_select_lex_unit::exec() (sql_union.cc:513) ==11409== by 0x71C0A0: mysql_explain_union(THD*, st_select_lex_unit*, select_result*) (sql_select.cc:16929) ==11409== by 0x688108: execute_sqlcom_select(THD*, TABLE_LIST*) (sql_parse.cc:5091) ==11409== by 0x68A2AC: mysql_execute_command(THD*) (sql_parse.cc:2299) ==11409== by 0x692EFF: mysql_parse(THD*, char const*, unsigned, char const**) (sql_parse.cc:6034) ==11409== by 0x693D11: dispatch_command(enum_server_command, THD*, char*, unsigned) (sql_parse.cc:1247) - Kristian.