tempesta-tech / mariadb

MariaDB System Versioning
GNU General Public License v2.0
6 stars 5 forks source link

MDEV-16226 TRX_ID-based versioned tables performance improvement #314

Closed midenok closed 4 years ago

midenok commented 6 years ago

Objectives

  1. Make index used (see comments in code).

  2. Also as commented on TR_table::query():

    Allocations on every examined row. This memory won't be freed until the query end, right? So such query on a big table will eat too much memory. I don't think this code scales well. We need to create READ_RECORD and surrounding stuff once per query and reuse. Let it be future work. For now I suggest to add a comment or open and issue on that topic.

Reproduce

create or replace table i1 (
  x int,
  row_start bigint(20) unsigned as row start invisible,
  row_end bigint(20) unsigned as row end invisible,
  period for system_time (row_start, row_end))
with system versioning
engine innodb;

insert into i1 values (1);
set @t1= now(6);
delete from i1;
insert into i1 values (2);

select *, row_start, row_end from i1 for system_time as of timestamp @t1;

Expected

1 row with x == 1;

Variations (implementation approach)

  1. Utilize rr_quick() in make_select() (see below);
  2. Access TRT via fully-processed subquery (see below).

Approach 1. shows x2 performance boost, but absolute value is anyway too low, so it was chosen to use Approach 2.

Scripts

Approach 2. TODO

Days remaining: 20 - 25 - 30.

Description

Current performance of SELECT by timestamp from TRX_ID-based tables is very low due to linear scan of TRANSACTION_REGISTRY table when doing timestamp -> TRX_ID translation. Utilize join optimizer for querying TRANSACTION_REGISTRY:

  1. For each timestamped Vers_history_point selector forge TRANSACTION_REGISTRY subquery and add it to SELECT.

Example

Query

select *, row_start, row_end from i1 for system_time as of timestamp @t1;

is transformed into

select i1.x as x,
       i1.row_start as row_start,
       i1.row_end as row_end
from i1
for SYSTEM_TIME all
join (
   select transaction_id
   from mysql.transaction_registry
   where commit_timestamp <= @t1
   order by commit_timestamp desc
   limit 1
) __trt_0
where trt_trx_sees(i1.row_end, __trt_0.transaction_id)
  and trt_trx_sees_eq(__trt_0.transaction_id, i1.row_start)
  1. subquery must be excluded from wildcard expansion, i.e.
    select transaction_id from i1 for system_time as of timestamp @t1;

    must be resolved to i1.transaction_id.

midenok commented 6 years ago

Good (previous execution)

#0  Lex_input_stream::skip_binary (this=0x7fffc12351e8, n=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.h:2204
#1  0x0000000000ab82ff in Lex_input_stream::scan_ident_middle (this=0x7fffc12351e8, thd=0x62a00009c270, str=0x7fffc121b340, introducer=0x7fffc121b340, st=0x7fffc121a680) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:2058
#2  0x0000000000ab435a in Lex_input_stream::lex_one_token (this=0x7fffc12351e8, yylval=0x7fffc121b340, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1486
#3  0x0000000000ab2d5d in Lex_input_stream::lex_token (this=0x7fffc12351e8, yylval=0x7fffc121b340, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1289
#4  0x0000000000ab2bd7 in MYSQLlex (yylval=0x7fffc121b340, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1262
#5  0x000000000121f1f5 in MYSQLparse (thd=0x62a00009c270) at sql/sql_yacc.cc:24454
#6  0x0000000000b5f000 in parse_sql (thd=0x62a00009c270, parser_state=0x7fffc12351e0, creation_ctx=0x62b000000608, do_pfs_digest=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:10034
#7  0x00000000019887f5 in sp_compile (thd=0x62a00009c270, defstr=0x7fffc1236e90, sql_mode=1411383296, parent=0x0, creation_ctx=0x62b000000608) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:853
#8  0x000000000198749f in Sp_handler::db_load_routine (this=0x53a8a80 <sp_handler_procedure>, thd=0x62a00009c270, name=0x7fffc1238190, sphp=0x7fffc1238380, sql_mode=1411383296, params=..., returns=..., body=..., chistics=..., definer=..., created=20180605220959, modified=20180605220959, parent=0x0, creation_ctx=0x62b000000608) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:977
#9  0x0000000001986949 in Sp_handler::db_find_routine (this=0x53a8a80 <sp_handler_procedure>, thd=0x62a00009c270, name=0x7fffc1238190, sphp=0x7fffc1238380) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:746
#10 0x0000000001987ec2 in Sp_handler::db_find_and_cache_routine (this=0x53a8a80 <sp_handler_procedure>, thd=0x62a00009c270, name=0x7fffc1238190, sp=0x7fffc1238380) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:767
#11 0x000000000199ad9d in Sp_handler::sp_cache_routine (this=0x53a8a80 <sp_handler_procedure>, thd=0x62a00009c270, name=0x7fffc1238190, lookup_only=false, sp=0x7fffc1238380) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:2767
#12 0x000000000199a79b in Sroutine_hash_entry::sp_cache_routine (this=0x62b000000360, thd=0x62a00009c270, lookup_only=false, sp=0x7fffc1238380) at /home/midenok/src/mariadb/midenok/src/sql/sp.cc:2720
#13 0x000000000096d7b1 in open_and_process_routine (thd=0x62a00009c270, prelocking_ctx=0x62a0000a0030, rt=0x62b000000360, prelocking_strategy=0x7fffc1239000, has_prelocking_list=false, ot_ctx=0x7fffc12386e0, need_prelocking=0x7fffc1238780, routine_modifies_data=0x7fffc1238790) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:3260
#14 0x00000000009681b0 in open_tables (thd=0x62a00009c270, options=..., start=0x7fffc1238d60, counter=0x7fffc1238d80, flags=0, prelocking_strategy=0x7fffc1239000) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4116
#15 0x0000000000973f51 in open_and_lock_tables (thd=0x62a00009c270, options=..., tables=0x0, derived=true, flags=0, prelocking_strategy=0x7fffc1239000) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4921
#16 0x00000000008a2a26 in open_and_lock_tables (thd=0x62a00009c270, tables=0x0, derived=true, flags=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.h:491
#17 0x0000000000b32c24 in Sql_cmd_call::execute (this=0x62b000000348, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3141
#18 0x0000000000b4c863 in mysql_execute_command (thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6287
#19 0x0000000000b2b764 in mysql_parse (thd=0x62a00009c270, rawbuf=0x62b000000288 "call bad", length=8, parser_state=0x7fffc123f040, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8023

0x7fffc12351e8 object repeats many times (44).

Bad

#0  Lex_input_stream::skip_binary (this=0x7fffc122d4b8, n=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.h:2204
#1  0x0000000000ab8130 in Lex_input_stream::scan_ident_middle (this=0x7fffc122d4b8, thd=0x62a00009c270, str=0x7fffc1213460, introducer=0x7fffc1213460, st=0x7fffc12127a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:2049
#2  0x0000000000ab435a in Lex_input_stream::lex_one_token (this=0x7fffc122d4b8, yylval=0x7fffc1213460, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1486
#3  0x0000000000ab2d5d in Lex_input_stream::lex_token (this=0x7fffc122d4b8, yylval=0x7fffc1213460, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1289
#4  0x0000000000ab2bd7 in MYSQLlex (yylval=0x7fffc1213460, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:1262
#5  0x000000000121f1f5 in MYSQLparse (thd=0x62a00009c270) at sql/sql_yacc.cc:24454
#6  0x0000000000b5f000 in parse_sql (thd=0x62a00009c270, parser_state=0x7fffc122d4b0, creation_ctx=0x6250000fb8e0, do_pfs_digest=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:10034
#7  0x0000000000ec0d34 in mysql_make_view (thd=0x62a00009c270, share=0x61b000067508, table=0x6250000faf68, open_view_no_parse=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_view.cc:1396
#8  0x000000000095e4d5 in open_table (thd=0x62a00009c270, table_list=0x6250000faf68, ot_ctx=0x7fffc1230240) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1841
#9  0x000000000096acf7 in open_and_process_table (thd=0x62a00009c270, lex=0x6250000fc988, tables=0x6250000faf68, counter=0x7fffc12308e0, flags=0, prelocking_strategy=0x7fffc1230b60, has_prelocking_list=false, ot_ctx=0x7fffc1230240) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:3526
#10 0x0000000000967cf9 in open_tables (thd=0x62a00009c270, options=..., start=0x7fffc12308c0, counter=0x7fffc12308e0, flags=0, prelocking_strategy=0x7fffc1230b60) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4044
#11 0x0000000000973f51 in open_and_lock_tables (thd=0x62a00009c270, options=..., tables=0x6250000faf68, derived=true, flags=0, prelocking_strategy=0x7fffc1230b60) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4921
#12 0x00000000008a2a26 in open_and_lock_tables (thd=0x62a00009c270, tables=0x6250000faf68, derived=true, flags=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.h:491
#13 0x0000000000b5094f in execute_sqlcom_select (thd=0x62a00009c270, all_tables=0x6250000faf68) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6470
#14 0x0000000000b39afd in mysql_execute_command (thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3772
#15 0x000000000088fa95 in sp_instr_stmt::exec_core (this=0x6250000fb5d8, thd=0x62a00009c270, nextp=0x7fffc12371e0) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:3601
#16 0x000000000088beb5 in sp_lex_keeper::reset_lex_and_exec_core (this=0x6250000fb628, thd=0x62a00009c270, nextp=0x7fffc12371e0, open_tables=false, instr=0x6250000fb5d8) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:3317
#17 0x000000000088d434 in sp_instr_stmt::execute (this=0x6250000fb5d8, thd=0x62a00009c270, nextp=0x7fffc12371e0) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:3504
#18 0x00000000008775c7 in sp_head::execute (this=0x6250000fa188, thd=0x62a00009c270, merge_da_on_success=true) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:1354
#19 0x000000000087d97c in sp_head::execute_procedure (this=0x6250000fa188, thd=0x62a00009c270, args=0x62a0000a0e98) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:2293
#20 0x0000000000b33f72 in do_execute_sp (thd=0x62a00009c270, sp=0x6250000fa188) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:2944
#21 0x0000000000b330a3 in Sql_cmd_call::execute (this=0x62b000000348, thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3186
#22 0x0000000000b4c863 in mysql_execute_command (thd=0x62a00009c270) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6287
#23 0x0000000000b2b764 in mysql_parse (thd=0x62a00009c270, rawbuf=0x62b000000288 "call bad", length=8, parser_state=0x7fffc123f040, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8023

Object 0x7fffc122d4b8 is used first time. Divergence is on Sql_cmd_call::execute() (sql_parse.cc:3141 vs sql_parse.cc:3186).

midenok commented 6 years ago

Info

242     /**
243       Perform initialization of Lex_input_stream instance.
244
245       Basically, a buffer for pre-processed query. This buffer should be large
246       enough to keep multi-statement query. The allocation is done once in
247       Lex_input_stream::init() in order to prevent memory pollution when
248       the server is processing large multi-statement queries.
249     */
250
251     bool Lex_input_stream::init(THD *thd,
252                                 char* buff,
253                                 size_t length)
254     {
255       DBUG_EXECUTE_IF("bug42064_simulate_oom",
256                       DBUG_SET("+d,simulate_out_of_memory"););
257
258       m_cpp_buf= (char*) thd->alloc(length + 1);
259
260       DBUG_EXECUTE_IF("bug42064_simulate_oom",
261                       DBUG_SET("-d,bug42064_simulate_oom");); 
262
263       if (m_cpp_buf == NULL)
264         return true;
265
266       m_thd= thd;
267       reset(buff, length);
268
269       return false;
270     }

Bad

(gdb) p buff
$19 = 0x6250000fb788 "select `test`.`t1`.`called_bad` AS `called_bad` from `test`.`t1` where `test`.`t1`.`called_bad` < 5"
(gdb) p length
$20 = 99

TR_table::add_subquery() is never called.

Cause

Crash is caused by ASAN itself.

midenok commented 6 years ago

Failing tests

versioning.truncate versioning.select versioning.view versioning.data versioning.select2 versioning.trx_id versioning.cte versioning.simple versioning.sysvars versioning.update versioning.partition_rotation versioning.derived versioning.optimized

versioning.sysvars

--- ./mysql-test/suite/versioning/r/sysvars.result      2018-07-19 16:08:58.912272925 +0300
+++ ./mysql-test/suite/versioning/r/sysvars.reject      2018-07-19 16:43:18.950524219 +0300
@@ -116,20 +116,15 @@
 a
 2
 select * from t for system_time as of timestamp current_timestamp(6);
-a
-2
+a      transaction_id
 select * from t for system_time all;
 a
 2
 1
 select * from t for system_time from '0-0-0' to current_timestamp(6);
-a
-2
-1
+a      transaction_id  transaction_id
 select * from t for system_time between '0-0-0' and current_timestamp(6);
-a
-2
-1
+a      transaction_id  transaction_id
 # MDEV-16026: Global system_versioning_asof must not be used if client sessions can have non-default time zone
 # changing time zone should not abuse `system_versioning_asof`
 set global time_zone = '+10:00';
midenok commented 6 years ago

Bug 4

Reproduce

create or replace table t (a int) with system versioning;
insert into t values (1);
update t set a= 2;
select * from t for system_time as of timestamp current_timestamp(6);

Result

mysqltest: At line 4: query 'select * from t for system_time as of timestamp current_timestamp(6)' failed: 1286: Unknown storage engine
 'InnoDB'

Expected

No error.

midenok commented 6 years ago

Error thrown

#0  my_error (nr=1286, MyFlags=0) at /home/midenok/src/mariadb/trunk/src/mysys/my_error.c:113
#1  0x00000000008f4997 in TABLE_SHARE::init_from_binary_frm_image (this=0x7fffd4030158, thd=0x7fffd4000d50, write=false, frm_image=0x7fffd4030750 "\376\001\n\f\022", frm_length=2618) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:1446
#2  0x00000000008f341c in open_table_def (thd=0x7fffd4000d50, share=0x7fffd4030158, flags=11) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:670
#3  0x0000000000a19bca in tdc_acquire_share (thd=0x7fffd4000d50, tl=0x7fffd40186d8, flags=3, out_table=0x7ffff075a9d8) at /home/midenok/src/mariadb/trunk/src/sql/table_cache.cc:839
#4  0x00000000007056c2 in open_table (thd=0x7fffd4000d50, table_list=0x7fffd40186d8, ot_ctx=0x7ffff075ad58) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:1797
#5  0x000000000070a118 in open_and_process_table (thd=0x7fffd4000d50, lex=0x7fffd4004af0, tables=0x7fffd40186d8, counter=0x7ffff075ae5c, flags=0, prelocking_strategy=0x7ffff075aed0, has_prelocking_list=false, ot_ctx=0x7ffff075ad58) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:3545
#6  0x0000000000708e6d in open_tables (thd=0x7fffd4000d50, options=..., start=0x7ffff075ae70, counter=0x7ffff075ae5c, flags=0, prelocking_strategy=0x7ffff075aed0) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:4063
#7  0x000000000070d455 in open_and_lock_tables (thd=0x7fffd4000d50, options=..., tables=0x7fffd4017388, derived=true, flags=0, prelocking_strategy=0x7ffff075aed0) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:4940
#8  0x00000000006baafc in open_and_lock_tables (thd=0x7fffd4000d50, tables=0x7fffd4017388, derived=true, flags=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.h:495
#9  0x00000000007c266b in execute_sqlcom_select (thd=0x7fffd4000d50, all_tables=0x7fffd4017388) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6467
#10 0x00000000007b7df1 in mysql_execute_command (thd=0x7fffd4000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3768
#11 0x00000000007b283f in mysql_parse (thd=0x7fffd4000d50, rawbuf=0x7fffd4017038 "select * from t for system_time as of timestamp current_timestamp(6)", length=68, parser_state=0x7ffff075e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8081

frame 1

1442          else if (!tmp_plugin)
1443          {
1444            /* purecov: begin inspected */
1445            ((char*) name.str)[name.length]=0;
1446            my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), name.str);
1447            goto err;
1448            /* purecov: end */
1449          }
(gdb) p name
$1 = {
  str = 0x7fffd4030f7e "InnoDB", 
  length = 6
}

frame 4

(gdb) p table_list->alias
$1 = {
  str = 0x1507557 "transaction_registry", 
  length = 20
}

Cause

TRT query is added even when table is MyISAM and InnoDB is not loaded.

midenok commented 6 years ago

TRT subquery is added

#0  TR_table::add_subquery (thd=0x7fff78000d50, p=..., cur_select=0x7fff78005338, subq_n=@0x7fffe4ece504: 0, backwards=false) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:8547
#1  0x0000000000794575 in LEX::vers_add_trt_query (this=0x7fff78004af0, thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_lex.cc:7218
#2  0x00000000007b5b5e in mysql_execute_command (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3251
#3  0x00000000007b283f in mysql_parse (thd=0x7fff78000d50, rawbuf=0x7fff78013ad8 "select * from t for system_time as of timestamp current_timestamp(6)", length=68, parser_state=0x7fffe4ed1650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8081

Fix variant 1

TRT subquery should be added after the table is opened. This is hard, because TRT must be opened itself in single call with queried table.

Fix variant 2

Don't add TRT subquery if there is no InnoDB plugin.

Fix

Try to open TRT at server init.

Info

Tables opened at server start

midenok commented 6 years ago

versioning.derived fails

Error printed

#0  my_error (nr=1286, MyFlags=0) at /home/midenok/src/mariadb/midenok/src/mysys/my_error.c:113
#1  0x00000000008f4b17 in TABLE_SHARE::init_from_binary_frm_image (this=0x7fffd4043568, thd=0x7fffd4000d50, write=false, frm_image=0x7fffd4043b60 "\376\001\n\f\022", frm_length=2618) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:1446
#2  0x00000000008f359c in open_table_def (thd=0x7fffd4000d50, share=0x7fffd4043568, flags=11) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:670
#3  0x0000000000a19d4a in tdc_acquire_share (thd=0x7fffd4000d50, tl=0x7fffd403fac0, flags=3, out_table=0x7ffff075a748) at /home/midenok/src/mariadb/midenok/src/sql/table_cache.cc:839
#4  0x0000000000705832 in open_table (thd=0x7fffd4000d50, table_list=0x7fffd403fac0, ot_ctx=0x7ffff075aac8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1797
#5  0x000000000070a288 in open_and_process_table (thd=0x7fffd4000d50, lex=0x7fffd4038ea0, tables=0x7fffd403fac0, counter=0x7ffff075ac14, flags=512, prelocking_strategy=0x7ffff075ac18, has_prelocking_list=false, ot_ctx=0x7ffff075aac8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:3545
#6  0x0000000000708fdd in open_tables (thd=0x7fffd4000d50, options=..., start=0x7ffff075ac28, counter=0x7ffff075ac14, flags=512, prelocking_strategy=0x7ffff075ac18) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4063
#7  0x000000000070dba5 in open_tables (thd=0x7fffd4000d50, tables=0x7ffff075ac28, counter=0x7ffff075ac14, flags=512, prelocking_strategy=0x7ffff075ac18) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.h:245
#8  0x000000000070d958 in open_normal_and_derived_tables (thd=0x7fffd4000d50, tables=0x7fffd403e4c8, flags=512, dt_phases=35) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:5003
#9  0x00000000007e9a82 in mysql_test_select (stmt=0x7fffd4037560, tables=0x7fffd403e4c8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:1522
#10 0x00000000007e44f9 in check_prepared_statement (stmt=0x7fffd4037560) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2323
#11 0x00000000007de84d in Prepared_statement::prepare (this=0x7fffd4037560, packet=0x7fffd40373a0 "\nwith recursive\nancestors\nas\n(\n  select e.emp_id, e.name, e.mgr\n  from emp for system_time as of timestamp @ts as e\n  where name = 'bill'\n  union\n  select ee.emp_id, ee.name, ee.mgr\n  from emp for system_time as of timestamp @ts as ee,\n  ancestors as a\n  where ee.mgr = a.emp_id\n)\nselect * from ancestors", packet_len=304) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3986
#12 0x00000000007df248 in mysql_sql_stmt_prepare (thd=0x7fffd4000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2819
#13 0x00000000007b7fab in mysql_execute_command (thd=0x7fffd4000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3781
#14 0x00000000007b29af in mysql_parse (thd=0x7fffd4000d50, rawbuf=0x7fffd4016f98 "prepare stmt from @tmp", length=22, parser_state=0x7ffff075e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

frame 7

p tables->next_global->next_global->next_global->next_global->alias
$33 = {
  str = 0x1507a03 "transaction_registry", 
  length = 20
}
midenok commented 6 years ago

TRT subquery added

#0  TR_table::add_subquery (thd=0x7fffd4000d50, p=..., cur_select=0x7fffd403a9a0, subq_n=@0x7ffff075ad44: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8547
#1  0x00000000007946e5 in LEX::vers_add_trt_query (this=0x7fffd4038ea0, thd=0x7fffd4000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7218
#2  0x00000000007e4225 in check_prepared_statement (stmt=0x7fffd4037560) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2254
#3  0x00000000007de84d in Prepared_statement::prepare (this=0x7fffd4037560, packet=0x7fffd40373a0 "\nwith recursive\nancestors\nas\n(\n  select e.emp_id, e.name, e.mgr\n  from emp for system_time as of timestamp @ts as e\n  where name = 'bill'\n  union\n  select ee.emp_id, ee.name, ee.mgr\n  from emp for system_time as of timestamp @ts as ee,\n  ancestors as a\n  where ee.mgr = a.emp_id\n)\nselect * from ancestors", packet_len=304) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3986
#4  0x00000000007df248 in mysql_sql_stmt_prepare (thd=0x7fffd4000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2819
#5  0x00000000007b7fab in mysql_execute_command (thd=0x7fffd4000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3781
#6  0x00000000007b29af in mysql_parse (thd=0x7fffd4000d50, rawbuf=0x7fffd4016f98 "prepare stmt from @tmp", length=22, parser_state=0x7ffff075e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083
midenok commented 6 years ago

Fix

--- a/sql/sql_prepare.cc                                                                                                               
+++ b/sql/sql_prepare.cc                                                                                                               
@@ -2251,7 +2251,9 @@ static bool check_prepared_statement(Prepared_statement *stmt)                                                   
   DBUG_PRINT("enter",("command: %d  param_count: %u",
                       sql_command, stmt->param_count));

-  if (sql_command == SQLCOM_SELECT && lex->vers_add_trt_query(thd))                                                                   
+  if (TR_table::use_transaction_registry &&                                                                                           
+      sql_command == SQLCOM_SELECT &&                                                                                                 
+      lex->vers_add_trt_query(thd))                                                                                                   
     goto error;
   lex->first_lists_tables_same();
   tables= lex->query_tables;
midenok commented 6 years ago

versioning.simple fails

--- /home/midenok/src/mariadb/midenok/src/mysql-test/suite/versioning/r/simple.result   2018-04-28 13:57:40.077656740 +0300
+++ /home/midenok/src/mariadb/midenok/src/mysql-test/suite/versioning/r/simple.reject   2018-07-22 19:00:08.926491337 +0300
@@ -32,11 +32,9 @@
 emp_id dept_id name    salary
 1      10      bill    2000
 select * from emp for system_time as of timestamp @ts_2;
-emp_id dept_id name    salary
-1      10      bill    1000
+emp_id dept_id name    salary  transaction_id
 select * from emp for system_time as of timestamp @ts_3;
-emp_id dept_id name    salary
-1      10      bill    2000
+emp_id dept_id name    salary  transaction_id
 select * from emp e, dept d
 where d.dept_id = 10
 and d.dept_id = e.dept_id; 
@@ -47,7 +45,7 @@
 dept for system_time from timestamp @ts_1 to timestamp @ts_2 d
 where d.dept_id = 10
 and d.dept_id = e.dept_id; 
-emp_id dept_id name    salary  dept_id name
+emp_id dept_id name    salary  dept_id name    transaction_id  transaction_id  transaction_id  transaction_id
 set statement system_versioning_asof=@ts_0 for
 select * from emp e, dept d
 where d.dept_id = 10
midenok commented 6 years ago

Reproduce

-- source include/have_innodb.inc
create or replace table t1 (x int) with system versioning;
insert into t1 values (1);
select * from t1 for system_time as of '2020-01-01 00:00';

drop table t1;

Result

No rows.

Expected

1 row returned.

Info

No matter what type t1 is: MyISAM or InnoDB. Reproduces as test, doesn't reproduce in CLI.

midenok commented 6 years ago

Condition checked

#0  Arg_comparator::compare_datetime (this=0x7fff9801a9f8) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.cc:737
#1  0x0000000000b74cd2 in Arg_comparator::compare (this=0x7fff9801a9f8) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.h:102
#2  0x0000000000b60485 in Item_func_le::val_int (this=0x7fff9801a938) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.cc:1772
#3  0x0000000000bab867 in eval_const_cond (cond=0x7fff9801a938) at /home/midenok/src/mariadb/midenok/src/sql/item_func.cc:81
#4  0x000000000083e4af in Item_bool_func2::remove_eq_conds (this=0x7fff9801a938, thd=0x7fff98000d50, cond_value=0x7ffff01e63a4, top_level_arg=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:16247
#5  0x000000000083d8f2 in Item_cond::remove_eq_conds (this=0x7fff9801abf8, thd=0x7fff98000d50, cond_value=0x7fff98019b90, top_level_arg=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:16056
#6  0x000000000081298a in make_join_statistics (join=0x7fff98019880, tables_list=..., keyuse_array=0x7fff98019b70) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4756
#7  0x000000000080dd06 in JOIN::optimize_inner (this=0x7fff98019880) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1869
#8  0x0000000000809485 in JOIN::optimize (this=0x7fff98019880) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1444
#9  0x0000000000801f8f in mysql_select (thd=0x7fff98000d50, tables=0x7fff980172b0, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff98019860, unit=0x7fff98004bb8, select_lex=0x7fff98005338) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4194
#10 0x00000000008018b3 in handle_select (thd=0x7fff98000d50, lex=0x7fff98004af0, result=0x7fff98019860, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#11 0x00000000007c2f61 in execute_sqlcom_select (thd=0x7fff98000d50, all_tables=0x7fff980172b0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#12 0x00000000007b8051 in mysql_execute_command (thd=0x7fff98000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#13 0x00000000007b2a4f in mysql_parse (thd=0x7fff98000d50, rawbuf=0x7fff98016f98 "select * from t1 for system_time as of '2020-01-01 00:00'", length=57, parser_state=0x7ffff01ea5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083
(gdb) eval "shell unpack-datetime %lu", val1
72553160038992651 = '2018-07-23 15:33:58.992651'
(gdb) eval "shell unpack-datetime %lu", val2
72606499200000000 = '2020-01-01 00:00:00.0'

Conditions are passed as expected.

Cause

LEX::vers_add_trt_query() influences result.

midenok commented 6 years ago

Query from console or test

select t1.x as x
from t1
for SYSTEM_TIME all
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp <= timestamp'2020-01-01 00:00:00'
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __trt_0
where t1.row_end > timestamp'2020-01-01 00:00:00'
  and t1.row_start <= timestamp'2020-01-01 00:00:00'

Queries are identical.

Cause

JOIN connection is wrong because it requires mysql.transaction_registry to return at least 1 row.

Fix

Remove TRT subquery from tables that don't use it or don't add it at all.

Info

dd_from_type() can be used to read .frm without table open. Used in MDEV-15966 (4af7bbcc2e4468821bd7847b706e02ad76636d20).

Fix

Since table is opened anyway that would be strange to preopen FRM before open. The most obvious way is to add TRT to opened tables list and then add subquery (method 2).

midenok commented 6 years ago

Info

TABLE_LIST unopened vs opened

--- trtl.log    2018-07-24 15:23:14.902183412 +0300
+++ trtl-opened.log 2018-07-24 15:13:21.204986973 +0300
@@ -40,7 +40,7 @@
   is_join_columns_complete = false, 
   next_name_resolution_table = 0x0, 
   index_hints = 0x0, 
-  table = 0x0, 
+  table = 0x332e300, 
   table_id = 0, 
   derived_result = 0x0, 
   map = 0, 
@@ -155,7 +155,7 @@
   lock_type = TL_READ, 
   outer_join = 0, 
   shared = 0, 
-  updatable = false, 
+  updatable = true, 
   straight = false, 
   updating = false, 
   force_index = false, 
@@ -228,7 +228,7 @@
     duration = MDL_TRANSACTION, 
     next_in_list = 0x0, 
     prev_in_list = 0x0, 
-    ticket = 0x0, 
+    ticket = 0x7fff7c01f3f0, 
     key = {
       m_length = 28, 
       m_db_name_length = 5, 
@@ -298,6 +298,6 @@
       tr_table = 0x0
     }
   }, 
-  m_table_ref_type = TABLE_REF_NULL, 
-  m_table_ref_version = 0
+  m_table_ref_type = TABLE_REF_BASE_TABLE, 
+  m_table_ref_version = 17
 }

TABLE_LIST inited vs added

--- was inited by init_one_table() +++ was constructed by add_table_to_list()

--- trtl.log    2018-07-24 15:23:14.902183412 +0300
+++ tl.log  2018-07-24 15:15:09.083617174 +0300
@@ -55,7 +55,7 @@
   schema_select_lex = 0x0, 
   schema_table_reformed = false, 
   schema_table_param = 0x0, 
-  select_lex = 0x0, 
+  select_lex = 0x7fff7c014a68, 
   view = 0x0, 
   field_translation = 0x0, 
   field_translation_end = 0x0, 
@@ -168,7 +168,7 @@
   embedding = 0x0, 
   join_list = 0x0, 
   lifted = false, 
-  cacheable_table = false, 
+  cacheable_table = true, 
   table_in_first_from_clause = false, 
   open_type = OT_TEMPORARY_OR_BASE, 
   contain_auto_increment = false, 
@@ -180,8 +180,8 @@
   timestamp_buffer = '\000' <repeats 19 times>, 
   prelocking_placeholder = TABLE_LIST::PRELOCK_NONE, 
   open_strategy = TABLE_LIST::OPEN_NORMAL, 
-  is_alias = false, 
-  is_fqtn = false, 
+  is_alias = true, 
+  is_fqtn = true, 
   fill_me = false, 
   merged = false, 
   merged_for_insert = false, 
midenok commented 6 years ago

Good: tr_table->table assigned

#0  mysql_derived_prepare (thd=0x7fff80000d50, lex=0x7fff80004af0, derived=0x7fff80015b68) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:810
#1  0x00000000007606f7 in mysql_handle_single_derived (lex=0x7fff80004af0, derived=0x7fff80015b68, phases=2) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:197
#2  0x000000000090dcaf in TABLE_LIST::handle_derived (this=0x7fff80015b68, lex=0x7fff80004af0, phases=2) at /home/midenok/src/mariadb/review/src/sql/table.cc:7978
#3  0x0000000000787493 in st_select_lex::handle_derived (this=0x7fff80005338, lex=0x7fff80004af0, phases=2) at /home/midenok/src/mariadb/review/src/sql/sql_lex.cc:4074
#4  0x0000000000805751 in JOIN::prepare (this=0x7fff80016928, tables_init=0x7fff80013d00, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff80005338, unit_arg=0x7fff80004bb8) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:993
#5  0x00000000008024b9 in mysql_select (thd=0x7fff80000d50, tables=0x7fff80013d00, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff80016908, unit=0x7fff80004bb8, select_lex=0x7fff80005338) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:4186
#6  0x0000000000801e13 in handle_select (thd=0x7fff80000d50, lex=0x7fff80004af0, result=0x7fff80016908, setup_tables_done_option=0) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:370
#7  0x00000000007c34c0 in execute_sqlcom_select (thd=0x7fff80000d50, all_tables=0x7fff80013d00) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:6551
#8  0x00000000007b8591 in mysql_execute_command (thd=0x7fff80000d50) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:3770
#9  0x00000000007b2f8f in mysql_parse (thd=0x7fff80000d50, rawbuf=0x7fff800139d8 "select * from t1 for system_time as of timestamp now()", length=54, parser_state=0x7fffe52ae650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:8086
808       if (!derived->table)
809         derived->table= derived->derived_result->table;
(gdb) p derived->table->alias.Ptr
$4 = 0x7fff80021190 "__trt_0"

Bad: assertion failed

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x150b443 "tr_table->table", file=0x1508b1a "/home/midenok/src/mariadb/midenok/src/sql/table.cc", line=8516, function=0x150b3f5 "Item *Vers_history_point::make_trx_id(THD *, Name_resolution_context &) const") at assert.c:101
#4  0x000000000090f3f9 in Vers_history_point::make_trx_id (this=0x7fff7c0142e8, thd=0x7fff7c000d50, ctx=...) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8516
#5  0x0000000000804836 in st_select_lex::vers_setup_conds (this=0x7fff7c005338, thd=0x7fff7c000d50, tables=0x7fff7c013cd0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:907
#6  0x0000000000805602 in JOIN::prepare (this=0x7fff7c016268, tables_init=0x7fff7c013cd0, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c005338, unit_arg=0x7fff7c004bb8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1027
#7  0x0000000000802159 in mysql_select (thd=0x7fff7c000d50, tables=0x7fff7c013cd0, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c016248, unit=0x7fff7c004bb8, select_lex=0x7fff7c005338) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4186
#8  0x0000000000801ab3 in handle_select (thd=0x7fff7c000d50, lex=0x7fff7c004af0, result=0x7fff7c016248, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#9  0x00000000007c3168 in execute_sqlcom_select (thd=0x7fff7c000d50, all_tables=0x7fff7c013cd0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6554
#10 0x00000000007b8201 in mysql_execute_command (thd=0x7fff7c000d50) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#11 0x00000000007b2bff in mysql_parse (thd=0x7fff7c000d50, rawbuf=0x7fff7c0139d8 "select * from i1 for system_time as of '2020-01-01 00:00'", length=57, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8089

TABLE_LIST::handle_derived() was not called.

4068    bool st_select_lex::handle_derived(LEX *lex, uint phases)
4069    {
4070      for (TABLE_LIST *cursor= (TABLE_LIST*) table_list.first;
4071           cursor;
4072           cursor= cursor->next_local)
4073      {
4074        if (cursor->is_view_or_derived() && cursor->handle_derived(lex, phases))
4075          return TRUE;
4076      }
4077      return FALSE;
4078    }
(gdb) p cursor->alias
$22 = {
  str = 0x7fff7c021190 "__trt_0", 
  length = 7
}
(gdb) p cursor->is_view_or_derived()
$23 = false
midenok commented 6 years ago

Good: derived_type set

#0  TABLE_LIST::set_derived (this=0x7fff80015b68) at /home/midenok/src/mariadb/review/src/sql/table.h:2541
#1  0x000000000090df7b in TABLE_LIST::init_derived (this=0x7fff80015b68, thd=0x7fff80000d50, init_view=true) at /home/midenok/src/mariadb/review/src/sql/table.cc:8095
#2  0x000000000075ce18 in mysql_derived_init (thd=0x7fff80000d50, lex=0x7fff80004af0, derived=0x7fff80015b68) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:584
#3  0x00000000007603ad in mysql_handle_derived (lex=0x7fff80004af0, phases=1) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:121
#4  0x000000000070dc10 in open_and_lock_tables (thd=0x7fff80000d50, options=..., tables=0x7fff80013d00, derived=true, flags=0, prelocking_strategy=0x7fffe52aaf30) at /home/midenok/src/mariadb/review/src/sql/sql_base.cc:4956
#5  0x00000000006bb1ac in open_and_lock_tables (thd=0x7fff80000d50, tables=0x7fff80013d00, derived=true, flags=0) at /home/midenok/src/mariadb/review/src/sql/sql_base.h:495
#6  0x00000000007c2e2a in execute_sqlcom_select (thd=0x7fff80000d50, all_tables=0x7fff80013d00) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:6472
#7  0x00000000007b8591 in mysql_execute_command (thd=0x7fff80000d50) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:3770
#8  0x00000000007b2f8f in mysql_parse (thd=0x7fff80000d50, rawbuf=0x7fff800139d8 "select * from t1 for system_time as of timestamp now()", length=54, parser_state=0x7fffe52ae650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:8086

Cause

Chicken and egg: mysql_derived_init() occurs at open_and_lock_tables() while add_subquery() requires already opened table.

Fix

Do mysql_derived_init() manually.

midenok commented 6 years ago

Bug 5: segfault in bootstrap

Reproduce

mtr --dry-run --boot-gdb versioning.simple

Result

#0  0x000000000090b0b6 in TABLE_LIST::reinit_before_use (this=0x7fff8811d368, thd=0x31b2448) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
#1  0x00000000007e01dc in reinit_stmt_before_use (thd=0x31b2448, lex=0x7fff88118e40) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3007
#2  0x00000000006b2179 in sp_lex_keeper::reset_lex_and_exec_core (this=0x7fff88116630, thd=0x31b2448, nextp=0x7ffff4c2ff28, open_tables=false, instr=0x7fff881165e0) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:3302
#3  0x00000000006b2bfc in sp_instr_stmt::execute (this=0x7fff881165e0, thd=0x31b2448, nextp=0x7ffff4c2ff28) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:3499
#4  0x00000000006a9c45 in sp_head::execute (this=0x7fff88114e20, thd=0x31b2448, merge_da_on_success=false) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:1353
#5  0x00000000006ab13a in sp_head::execute_trigger (this=0x7fff88114e20, thd=0x31b2448, db_name=0x7fff881067a0, table_name=0x7fff881067b0, grant_info=0x7fff8810f450) at /home/midenok/src/mariadb/midenok/src/sql/sp_head.cc:1762
#6  0x00000000008cfe3f in Table_triggers_list::process_triggers (this=0x7fff880f5100, thd=0x31b2448, event=TRG_EVENT_INSERT, time_type=TRG_ACTION_BEFORE, old_row_is_record1=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_trigger.cc:2207
#7  0x000000000071647f in fill_record_n_invoke_before_triggers (thd=0x31b2448, table=0x7fff88109c68, ptr=0x7fff880eb5b0, values=..., ignore_errors=false, event=TRG_EVENT_INSERT) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8572
#8  0x000000000076ae40 in mysql_insert (thd=0x31b2448, table_list=0x7fff88004068, fields=..., values_list=..., update_fields=..., update_values=..., duplic=DUP_ERROR, ignore=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_insert.cc:1006

frame 0

#0  0x000000000090b0b6 in TABLE_LIST::reinit_before_use (this=0x7fff8811d368, thd=0x31b2448) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
7181          embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
(gdb) p embedded->prep_on_expr
$1 = (Item *) 0x8f8f8f8f8f8f8f8f

Cause

Prematurely freed prep_on_expr.

Bad commit

commit 42a321a901e406f9f6df443d5910bbc89bfe8201 (HEAD, refs/bisect/bad)                                                                
Author: Aleksey Midenkov <midenok@gmail.com>                                                                                           
Date:   Tue Jul 24 01:37:51 2018 +0300                                                                                                 

    Add TRT to global list                                                                                                             

diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc                                                                                       
index 8627bb7bdfa..6b184c0bc30 100644                                                                                                  
--- a/sql/sql_parse.cc                                                                                                                 
+++ b/sql/sql_parse.cc                                                                                                                 
@@ -6466,6 +6466,9 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)                                              
                                      (ulonglong) thd->variables.select_limit);                                                        
   }                                                                                                                                   

+  if (TR_table::add_to_lex(thd))                                                                                                      
+    return 1;                                                                                                                         
+                                                                                                                                      
   if (!(res= open_and_lock_tables(thd, all_tables, TRUE, 0)))                                                                         
   {                                                                                                                                   
     if (lex->describe) 

Fix

Exclude add_to_lex() for bootstrap.

midenok commented 6 years ago

Bug 6: versioning.select,timestamp fails

Reproduce

mtrv select,timestamp

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x14eee25 "tl->table", file=0x14ed737 "/home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc", line=7243, function=0x14eee2f "bool LEX::vers_add_trt_query2(THD *, TABLE_LIST *)") at assert.c:101
#4  0x0000000000794703 in LEX::vers_add_trt_query2 (this=0x7fff98004a88, thd=0x7fff98000ce8, trtl=0x7fff98019218) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7243
#5  0x00000000007c29a2 in execute_sqlcom_select (thd=0x7fff98000ce8, all_tables=0x7fff98018b20) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6475
#6  0x00000000007b80c1 in mysql_execute_command (thd=0x7fff98000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#7  0x00000000007b2abf in mysql_parse (thd=0x7fff98000ce8, rawbuf=0x7fff98015e20 "explain extended select * from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x)\nfor system_time as of timestamp @t0 as t", length=168, parser_state=0x7fffe835e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8089

frame 4

(gdb) p tl->alias.str
$4 = 0x7fff98018ae0 "t"

Fix

Exclude derived tables from being processed by LEX::vers_add_trt_query().

midenok commented 6 years ago

Bug 7: PS failure

Reproduce

create or replace table t1 (x int) with system versioning;
insert into t1 values (1);
prepare stmt from "select x from t1;";
execute stmt;
execute stmt;

Result

On first execute

ERROR 1615 (HY000): Prepared statement needs to be re-prepared
#0  Diagnostics_area::set_error_status (this=0x7fff7c006260, sql_errno=1615) at /home/midenok/src/mariadb/midenok/src/sql/sql_error.cc:407
#1  0x00000000007e23fe in Reprepare_observer::report_error (this=0x7fffe4ebb3e8, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3640
#2  0x00000000007069af in check_and_update_table_version (thd=0x7fff7c000ce8, tables=0x7fff7c012d08, table_share=0x3314de0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:2604
#3  0x000000000070a5c6 in open_and_process_table (thd=0x7fff7c000ce8, lex=0x7fff7c038728, tables=0x7fff7c012d08, counter=0x7fffe4eb876c, flags=0, prelocking_strategy=0x7fffe4eb87e0, has_prelocking_list=false, ot_ctx=0x7fffe4eb8668) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:3636
#4  0x0000000000708fbd in open_tables (thd=0x7fff7c000ce8, options=..., start=0x7fffe4eb8780, counter=0x7fffe4eb876c, flags=0, prelocking_strategy=0x7fffe4eb87e0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4063
#5  0x000000000070d5a5 in open_and_lock_tables (thd=0x7fff7c000ce8, options=..., tables=0x7fff7c03a0b8, derived=true, flags=0, prelocking_strategy=0x7fffe4eb87e0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4940
#6  0x00000000006bac4c in open_and_lock_tables (thd=0x7fff7c000ce8, tables=0x7fff7c03a0b8, derived=true, flags=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.h:495
#7  0x00000000007c2953 in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c03a0b8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6473
#8  0x00000000007b80a1 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#9  0x00000000007e5799 in Prepared_statement::execute (this=0x7fff7c036f88, expanded_query=0x7fffe4ebb4e0, open_cursor=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4767
#10 0x00000000007e1234 in Prepared_statement::execute_loop (this=0x7fff7c036f88, expanded_query=0x7fffe4ebb4e0, open_cursor=false, packet=0x0, packet_end=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4195
#11 0x00000000007e0eb1 in mysql_sql_stmt_execute (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3303
#12 0x00000000007b80ee in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3786
#13 0x00000000007b2a9f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "execute stmt", length=12, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8089

On second execute

Thread 33 "mysqld" received signal SIGSEGV, Segmentation fault.
0x000000000090b0b6 in TABLE_LIST::reinit_before_use (this=0x7fff7c015258, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
7181          embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
#0  0x000000000090b0b6 in TABLE_LIST::reinit_before_use (this=0x7fff7c015258, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
#1  0x00000000007e01dc in reinit_stmt_before_use (thd=0x7fff7c000ce8, lex=0x7fff7c044a08) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3007
#2  0x00000000007e567d in Prepared_statement::execute (this=0x7fff7c036f88, expanded_query=0x7fffe4ebb4e0, open_cursor=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4742
#3  0x00000000007e1234 in Prepared_statement::execute_loop (this=0x7fff7c036f88, expanded_query=0x7fffe4ebb4e0, open_cursor=false, packet=0x0, packet_end=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4195
#4  0x00000000007e0eb1 in mysql_sql_stmt_execute (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3303
#5  0x00000000007b80ee in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3786
#6  0x00000000007b2a9f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "execute stmt", length=12, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8089
7181          embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
(gdb) p embedded->prep_on_expr
$1 = (Item *) 0x8f8f8f8f8f8f8f8f

Note

Similar to Bug 5.

midenok commented 6 years ago

Bug 8: assertion on Diagnostics_area::DA_EMPTY

Reproduce

prepare stmt from "select x from t1;";

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x163463e "0", file=0x14d37c1 "/home/midenok/src/mariadb/midenok/src/sql/protocol.cc", line=588, function=0x14d3a31 "void Protocol::end_statement()") at assert.c:101
#4  0x000000000066b726 in Protocol::end_statement (this=0x7fff7c0012a8) at /home/midenok/src/mariadb/midenok/src/sql/protocol.cc:588
#5  0x00000000007b029d in dispatch_command (command=COM_QUERY, thd=0x7fff7c000ce8, packet=0x7fff7c00a8c9 "prepare stmt from \"select x from t1;\"", packet_length=37, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:2403
#6  0x00000000007b180d in do_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:1391
#7  0x000000000095a601 in do_handle_one_connection (connect=0x3124c48) at /home/midenok/src/mariadb/midenok/src/sql/sql_connect.cc:1402
#8  0x000000000095a3da in handle_one_connection (arg=0x3124c48) at /home/midenok/src/mariadb/midenok/src/sql/sql_connect.cc:1308
#9  0x00007ffff7bbd7fc in start_thread (arg=0x7fffe4ebf700) at pthread_create.c:465
#10 0x00007ffff5e81b5f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

frame 4

(gdb) p thd->get_stmt_da()->m_status
$18 = Diagnostics_area::DA_EMPTY

Good: status set to DA_OK

#0  Diagnostics_area::set_ok_status (this=0x7fff7c006260, affected_rows=0, last_insert_id=0, message=0x14f4f06 "Statement prepared") at /home/midenok/src/mariadb/midenok/src/sql/sql_error.cc:357
#1  0x00000000006fcb5f in my_ok (thd=0x7fff7c000ce8, affected_rows_arg=0, id=0, message=0x14f4f06 "Statement prepared") at /home/midenok/src/mariadb/midenok/src/sql/sql_class.h:4850
#2  0x00000000007df4dc in mysql_sql_stmt_prepare (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2829
#3  0x00000000007b80db in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3781
#4  0x00000000007b2a9f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "prepare stmt from \"select x from t1;\"", length=37, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8092
#5  0x00000000007ae31d in dispatch_command (command=COM_QUERY, thd=0x7fff7c000ce8, packet=0x7fff7c00a8c9 "prepare stmt from \"select x from t1;\"", packet_length=37, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:1846

Fix

Typo with absent '!' in this patch.

--- a/sql/sql_prepare.cc                                                                                                               
+++ b/sql/sql_prepare.cc                                                                                                               
@@ -2253,7 +2253,7 @@ static bool check_prepared_statement(Prepared_statement *stmt)                                                   

   if (TR_table::use_transaction_registry &&                                                                                           
       sql_command == SQLCOM_SELECT &&                                                                                                 
-      lex->vers_add_trt_query(thd))                                                                                                   
+      !TR_table::add_to_lex(thd, lex))                                                                                                
     goto error;                                                                                                                       
   lex->first_lists_tables_same();                                                                                                     
   tables= lex->query_tables;
midenok commented 6 years ago

Bug 9: prematurely freed TABLE_LIST

1. TABLE_LIST constructed

#0  TR_table::add_to_lex (thd=0x7fff7c000ce8, lex=0x7fff7c0380b8) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8556
#1  0x00000000007e4425 in check_prepared_statement (stmt=0x7fff7c036918) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2256
#2  0x00000000007dea3d in Prepared_statement::prepare (this=0x7fff7c036918, packet=0x7fff7c012ca8 "select x from t1;", packet_len=17) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3988
#3  0x00000000007df438 in mysql_sql_stmt_prepare (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:2821
#4  0x00000000007b80db in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3781
#5  0x00000000007b2a9f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "prepare stmt from \"select x from t1;\"", length=37, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8092
(gdb) p tl
$6 = (TABLE_LIST *) 0x7fff7c012d60

2. TABLE_LIST accessed

#0  0x000000000090b0f6 in TABLE_LIST::reinit_before_use (this=0x7fff7c012d60, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
#1  0x00000000007e021c in reinit_stmt_before_use (thd=0x7fff7c000ce8, lex=0x7fff7c0380b8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3007
#2  0x00000000007e56bd in Prepared_statement::execute (this=0x7fff7c036918, expanded_query=0x7fffe4ebb4e0, open_cursor=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4742
#3  0x00000000007e1274 in Prepared_statement::execute_loop (this=0x7fff7c036918, expanded_query=0x7fffe4ebb4e0, open_cursor=false, packet=0x0, packet_end=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4195
#4  0x00000000007e0ef1 in mysql_sql_stmt_execute (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3303
#5  0x00000000007b80ee in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3786
#6  0x00000000007b2a9f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "execute stmt", length=12, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8092
Thread 33 "mysqld" received signal SIGSEGV, Segmentation fault.
0x000000000090b0f6 in TABLE_LIST::reinit_before_use (this=0x7fff7c012d60, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7181
7181          embedded->on_expr= embedded->prep_on_expr->copy_andor_structure(thd);
(gdb) p embedded
$7 = (TABLE_LIST *) 0x7fff7c012d60

Fix

Allocate TABLE_LIST on statement query arena.

midenok commented 6 years ago

Bug 10: subquery not added by method 2

subquery->table assigned

#0  mysql_derived_prepare (thd=0x7fff84000d50, lex=0x7fff84004af0, derived=0x7fff84015b98) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:810
#1  0x00000000007606a7 in mysql_handle_single_derived (lex=0x7fff84004af0, derived=0x7fff84015b98, phases=2) at /home/midenok/src/mariadb/review/src/sql/sql_derived.cc:197
#2  0x000000000090dc3f in TABLE_LIST::handle_derived (this=0x7fff84015b98, lex=0x7fff84004af0, phases=2) at /home/midenok/src/mariadb/review/src/sql/table.cc:7978
#3  0x0000000000787443 in st_select_lex::handle_derived (this=0x7fff84005338, lex=0x7fff84004af0, phases=2) at /home/midenok/src/mariadb/review/src/sql/sql_lex.cc:4074
#4  0x00000000008056e1 in JOIN::prepare (this=0x7fff84016a00, tables_init=0x7fff84013d30, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84005338, unit_arg=0x7fff84004bb8) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:993
#5  0x0000000000802449 in mysql_select (thd=0x7fff84000d50, tables=0x7fff84013d30, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748612, result=0x7fff840162d0, unit=0x7fff84004bb8, select_lex=0x7fff84005338) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:4186
#6  0x000000000084c2c7 in mysql_explain_union (thd=0x7fff84000d50, unit=0x7fff84004bb8, result=0x7fff840162d0) at /home/midenok/src/mariadb/review/src/sql/sql_select.cc:25693
#7  0x00000000007c2f1f in execute_sqlcom_select (thd=0x7fff84000d50, all_tables=0x7fff84013d30) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:6487
#8  0x00000000007b8541 in mysql_execute_command (thd=0x7fff84000d50) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:3770
#9  0x00000000007b2f3f in mysql_parse (thd=0x7fff84000d50, rawbuf=0x7fff840139d8 "explain extended select * from i1 for system_time as of timestamp'2018-07-20 22:06:33.197288'", length=93, parser_state=0x7fffe52ae650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/review/src/sql/sql_parse.cc:8083

Same on good and bad side.

frame 3

Good
(gdb) p dbug_print_select(this)
$3 = 0x2063c20 <dbug_item_print_buf> "select `*` AS `*` from test.i1 FOR SYSTEM_TIME AS OF TIMESTAMP TIMESTAMP'2018-07-20 22:06:33.197288' join (select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'2018-07-20 22:06:33.197288' order by `mysql`.transaction_registry.commit_timestamp desc limit 1) __trt_0"
select * as *
from test.i1
for SYSTEM_TIME as of timestamp timestamp'2018-07-20 22:06:33.197288'
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp <= timestamp'2018-07-20 22:06:33.197288'
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __trt_0
Bad
p dbug_print_select(this)
$3 = 0x2019ad0 <dbug_item_print_buf> "select `*` AS `*` from test.i1 FOR SYSTEM_TIME AS OF TIMESTAMP TIMESTAMP'2018-07-20 22:06:33.197288' join (select `mysql`.transaction_registry.transaction_id AS transaction_id from DUAL  where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'2018-07-20 22:06:33.197288' order by `mysql`.transaction_registry.commit_timestamp desc limit 1) __trt_0"
select * as *
from test.i1
for SYSTEM_TIME as of timestamp timestamp'2018-07-20 22:06:33.197288'
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from DUAL
   where mysql.transaction_registry.commit_timestamp <= timestamp'2018-07-20 22:06:33.197288'
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __trt_0

Bad

#0  TR_table::add_subquery2 (thd=0x7fff7c000ce8, trtl=0x7fff7c013618, p=..., cur_select=0x7fff7c0052d0, subq_n=@0x7fffe4ebaf34: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8773
#1  0x00000000007947cd in LEX::vers_add_trt_query2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7251
#2  0x00000000007c2b47 in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012f18) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6477
#3  0x00000000007b8231 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#4  0x00000000007b2c2f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select * from i1 for system_time as of timestamp'2018-07-20 22:06:33.197288'", length=76, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8091
(gdb) p dbug_print_select(sel)
$11 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from DUAL  where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'2018-07-20 22:06:33.197288' order by `mysql`.transaction_registry.commit_timestamp desc limit 1"

Cause

TRT table is not added properly to subquery.

Fix

--- a/sql/table.cc                                                                                                                     
+++ b/sql/table.cc                                                                                                                     
@@ -8703,6 +8703,7 @@ bool TR_table::add_subquery2(THD* thd, TABLE_LIST *trtl, Vers_history_point &p,                                  
     sel->parsing_place= NO_MATTER;
     sel->table_join_options= 0;
     trtl->select_lex= sel;
+    sel->table_list.link_in_list(trtl, &trtl->next_local);                                                                            
     sel->add_joined_table(trtl);
     sel->context.table_list= trtl;
     sel->context.first_name_resolution_table= trtl;
midenok commented 6 years ago

Bug 11: truncate,trx_id fails

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x150a65c "tr_table", file=0x1507d8a "/home/midenok/src/mariadb/midenok/src/sql/table.cc", line=8515, function=0x150a665 "Item *Vers_history_point::make_trx_id(THD *, Name_resolution_context &) const") at assert.c:101
#4  0x000000000090f435 in Vers_history_point::make_trx_id (this=0x7fff98016550, thd=0x7fff98000ce8, ctx=...) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8515
#5  0x0000000000805048 in st_select_lex::vers_setup_conds (this=0x7fff980052d0, thd=0x7fff98000ce8, tables=0x7fff98015f38) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:925
#6  0x0000000000d162b7 in mysql_delete (thd=0x7fff98000ce8, table_list=0x7fff98015f38, conds=0x0, order_list=0x7fff98005548, limit=18446744073709551615, options=0, result=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_delete.cc:331
#7  0x00000000007bc064 in mysql_execute_command (thd=0x7fff98000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:4923
#8  0x00000000007b2c2f in mysql_parse (thd=0x7fff98000ce8, rawbuf=0x7fff98015e20 "delete history from t before system_time timestamp @ts1", length=55, parser_state=0x7fffe835e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8091

Cause

Subquery is not added for DELETE.

Fix

Don't use subquery (use old method) for DELETE.

midenok commented 6 years ago

Bug 12: versioning.sysvars freezes

Bad commit

commit 1ede3e51f55493a414daeb449911eb59068389f4
Author: Aleksey Midenkov <midenok@gmail.com>
Date:   Thu Jul 19 22:03:29 2018 +0300

    Avoid adding subquery when TRT is unavailable

--- a/sql/mysqld.cc                                                                                                                    
+++ b/sql/mysqld.cc                                                                                                                    
@@ -6109,6 +6109,21 @@ int mysqld_main(int argc, char **argv)                                                                          
       exit(0);                                                                                                                        
     }                                                                                                                                 
   }                                                                                                                                   
+  else if (TR_table::use_transaction_registry)                                                                                        
+  {                                                                                                                                   
+    THD *thd= new THD(0);                                                                                                             
+    if (!thd)                                                                                                                         
+    {                                                                                                                                 
+      sql_print_error("Can't allocate memory for TRT init");                                                                          
+      unireg_abort(1);                                                                                                                
+    }                                                                                                                                 
+    thd->thread_stack= (char*) &thd;                                                                                                  
+    thd->store_globals();                                                                                                             
+    thd->set_db(&MYSQL_SCHEMA_NAME);                                                                                                  
+                                                                                                                                      
+    TR_table trt(thd);                                                                                                                
+    trt.open();                                                                                                                       
+  }                                                                                                                                   

   create_shutdown_thread();                                                                                                           
   start_handle_manager();                                                                                                             
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc 

Cause

Not freed thd.

Fix

--- a/sql/mysqld.cc                                                                                                                    
+++ b/sql/mysqld.cc                                                                                                                    
@@ -6121,9 +6121,12 @@ int mysqld_main(int argc, char **argv)                                                                          
     thd->thread_stack= (char*) &thd;
     thd->store_globals();
     thd->set_db(&MYSQL_SCHEMA_NAME);
-                                                                                                                                      
-    TR_table trt(thd);                                                                                                                
-    trt.open();                                                                                                                       
+    {                                                                                                                                 
+      TR_table trt(thd);                                                                                                              
+      trt.open();                                                                                                                     
+      ha_commit_one_phase(thd, false);                                                                                                
+    }                                                                                                                                 
+    delete thd;                                                                                                                       
   }

   create_shutdown_thread();
midenok commented 6 years ago

Tests failed so far

versioning.select versioning.select2 versioning.data versioning.trx_id
midenok commented 6 years ago

Bug 13: select,trx_id assertion

Reproduce

select x from i1 for system_time from timestamp '0-0-0 0:0:0' to timestamp '1-1-1 0:0:0';

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x16203d7 "outer_context || !*from_field || *from_field == not_found_field", file=0x161ea33 "/home/midenok/src/mariadb/midenok/src/sql/item.cc", line=5801, function=0x1620417 "int Item_field::fix_outer_field(THD *, Field **, Item **)") at assert.c:101
#4  0x0000000000b3cb36 in Item_field::fix_outer_field (this=0x7fff98017ce0, thd=0x7fff98000ce8, from_field=0x7fffe8359590, reference=0x7fff98017df8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:5800
#5  0x0000000000b3eb2f in Item_field::fix_fields (this=0x7fff98017ce0, thd=0x7fff98000ce8, reference=0x7fff98017df8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:6252
#6  0x000000000068356a in Item::fix_fields_if_needed (this=0x7fff98017ce0, thd=0x7fff98000ce8, ref=0x7fff98017df8) at /home/midenok/src/mariadb/midenok/src/sql/item.h:822
#7  0x0000000000682737 in Item::fix_fields_if_needed_for_scalar (this=0x7fff98017ce0, thd=0x7fff98000ce8, ref=0x7fff98017df8) at /home/midenok/src/mariadb/midenok/src/sql/item.h:826
#8  0x0000000000712fd8 in setup_fields (thd=0x7fff98000ce8, ref_pointer_array=..., fields=..., column_usage=MARK_COLUMNS_READ, sum_func_list=0x7fff9801aa98, pre_fix=0x7fff98017288, allow_sum_func=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:7349
#9  0x0000000000805d70 in JOIN::prepare (this=0x7fff9801a778, tables_init=0x7fff98016ab0, wild_num=0, conds_init=0x7fff98017f20, og_num=1, order_init=0x7fff98018190, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff98017148, unit_arg=0x7fff98017560) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1099
#10 0x00000000008da5bd in st_select_lex_unit::prepare_join (this=0x7fff98017560, thd_arg=0x7fff98000ce8, sl=0x7fff98017148, tmp_result=0x7fff9801a690, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#11 0x00000000008d57e2 in st_select_lex_unit::prepare (this=0x7fff98017560, derived_arg=0x7fff980182c0, sel_result=0x7fff9801a690, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#12 0x000000000075d35a in mysql_derived_prepare (thd=0x7fff98000ce8, lex=0x7fff98004a88, derived=0x7fff980182c0) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#13 0x00000000007601a7 in mysql_handle_single_derived (lex=0x7fff98004a88, derived=0x7fff980182c0, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#14 0x000000000090dc0f in TABLE_LIST::handle_derived (this=0x7fff980182c0, lex=0x7fff98004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7978
#15 0x0000000000786f43 in st_select_lex::handle_derived (this=0x7fff980052d0, lex=0x7fff98004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#16 0x0000000000805661 in JOIN::prepare (this=0x7fff9801a128, tables_init=0x7fff980163b0, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff980052d0, unit_arg=0x7fff98004b50) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:997
#17 0x0000000000802219 in mysql_select (thd=0x7fff98000ce8, tables=0x7fff980163b0, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff9801a108, unit=0x7fff98004b50, select_lex=0x7fff980052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4190
#18 0x0000000000801b73 in handle_select (thd=0x7fff98000ce8, lex=0x7fff98004a88, result=0x7fff9801a108, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#19 0x00000000007c3204 in execute_sqlcom_select (thd=0x7fff98000ce8, all_tables=0x7fff980163b0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6556
#20 0x00000000007b8261 in mysql_execute_command (thd=0x7fff98000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#21 0x00000000007b2c5f in mysql_parse (thd=0x7fff98000ce8, rawbuf=0x7fff98015e20 "select x as FROMTO_x, y from t1 for system_time from timestamp '0-0-0 0:0:0' to timestamp @t1", length=93, parser_state=0x7fffe835e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8091
5794      /*
5795        This assert is to ensure we have an outer contex when *from_field
5796        is set.
5797        If this would not be the case, we would assert in mark_as_dependent
5798        as last_checked_countex == context
5799      */
5800      DBUG_ASSERT(outer_context || !*from_field ||
5801                  *from_field == not_found_field);
(gdb) p (*from_field)->table->alias.Ptr
$11 = 0x2d70278 "transaction_registry"
(gdb) p (*from_field)->field_name.str
$12 = 0x2d81a11 "transaction_id"

frame 18

p dbug_print_select(&lex->select_lex)
$2 = 0x2019ad0 <dbug_item_print_buf> "select x AS x from test.i1 FOR SYSTEM_TIME FROM TIMESTAMP TIMESTAMP'0000-00-00 00:00:00' TO TIMESTAMP TIMESTAMP'0001-01-01 00:00:00' join (select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'0000-00-00 00:00:00' order by `mysql`.transaction_registry.commit_timestamp limit 1) __trt_0 join (select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'0001-01-01 00:00:00' order by `mysql`.transaction_registry.commit_timestamp desc limit 1) __trt_1"
select x as x
from test.i1
for SYSTEM_TIME
from timestamp timestamp'0000-00-00 00:00:00' to timestamp timestamp'0001-01-01 00:00:00'
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp >= timestamp'0000-00-00 00:00:00'
   order by mysql.transaction_registry.commit_timestamp
   limit 1) __trt_0
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp <= timestamp'0001-01-01 00:00:00'
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __trt_1

frame 4

p dbug_print_select(current_sel)
$4 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp >= TIMESTAMP'0000-00-00 00:00:00' order by `mysql`.transaction_registry.commit_timestamp limit 1"
(gdb) p current_sel->master_unit()->first_select() == current_sel
$9 = true
(gdb) p current_sel->linkage
$10 = DERIVED_TABLE_TYPE
Info
5720      @description
5721      The method resolves the column reference represented by 'this' as a column
5722      present in outer selects that contain current select.
....
5775      /*
5776        If there are outer contexts (outer selects, but current select is
5777        not derived table or view) try to resolve this reference in the
5778        outer contexts.
5779
5780        We treat each subselect as a separate namespace, so that different
5781        subselects may contain columns with the same names. The subselects
5782        are searched starting from the innermost.
5783      */
....
5789      /* Currently derived tables cannot be correlated */
5790      if (current_sel->master_unit()->first_select()->linkage !=
5791          DERIVED_TABLE_TYPE)
5792        outer_context= context->outer_context;

frame 5

6245        if (!outer_fixed && table_list && table_list->select_lex &&
6246            context->select_lex &&
6247            table_list->select_lex != context->select_lex &&
6248            !context->select_lex->is_merged_child_of(table_list->select_lex) &&
6249            is_outer_table(table_list, context->select_lex))
6250        {
6251          int ret;
6252          if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
6253            goto error;
6254          outer_fixed= 1;
6255          if (!ret)
6256            goto mark_non_agg_field;
6257        }

Cause

Outer context resolution is done for transaction_id while it's not needed at all.

Info

static bool is_outer_table(TABLE_LIST table, SELECT_LEX select) { DBUG_ASSERT(table->select_lex != select); TABLE_LIST *tl;

if (table->belong_to_view && table->belong_to_view->select_lex == select) return FALSE;

for (tl= select->master_unit()->derived; tl && tl->is_merged_derived(); select= tl->select_lex, tl= select->master_unit()->derived) { if (tl->select_lex == table->select_lex) return FALSE; } return TRUE; }

midenok commented 6 years ago

TABLE_LIST::init_derived() for subquery is called

#0  TABLE_LIST::init_derived (this=0x7fff7c014ef8, thd=0x7fff7c000ce8, init_view=true) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8062
#1  0x000000000075c8c8 in mysql_derived_init (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c014ef8) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:583
#2  0x0000000000911519 in TR_table::add_subquery2 (thd=0x7fff7c000ce8, trtl=0x7fff7c0136e8, p=..., cur_select=0x7fff7c0052d0, subq_n=@0x7fffe4ebaf34: 1, backwards=true) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8786
#3  0x0000000000794854 in LEX::vers_add_trt_query2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7256
#4  0x00000000007c2b6b in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012fe8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6477
#5  0x00000000007b8261 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#6  0x00000000007b2c5f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from i1 for system_time from timestamp '0-0-0 0:0:0' to timestamp '1-1-1 0:0:0'", length=88, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8091

Cause

This condition fails:

8101        if (!is_materialized_derived() && first_select->is_mergeable() &&
8102            optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) &&
8103            !thd->lex->can_not_use_merged() &&
8104            !(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
8105              thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
8106            !is_recursive_with_table())
8107          set_merged_derived();
8108        else
8109          set_materialized_derived();
(gdb) p dbug_print_select(first_select)
$33 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp >= TIMESTAMP'0000-00-00 00:00:00' order by `mysql`.transaction_registry.commit_timestamp limit 1"
(gdb) p first_select->is_mergeable()
$34 = false

Info

  inline bool is_mergeable()
  {
    return (next_select() == 0 && group_list.elements == 0 &&
            having == 0 && with_sum_func == 0 &&
            table_list.elements >= 1 && !(options & SELECT_DISTINCT) &&
            select_limit == 0);
  }

Cause

It is not mergeable because of this condition failure: select_limit == 0.

midenok commented 6 years ago

Good

(gdb) p table_list->select_lex
$39 = (st_select_lex *) 0x7fff7c013cb0
(gdb) p context->select_lex
$40 = (st_select_lex *) 0x7fff7c013cb0
(gdb) p dbug_print_select(table_list->select_lex)
$41 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'2018-07-20 22:06:33.1972' order by `mysql`.transaction_registry.commit_timestamp desc limit 1"

Bad

(gdb) p dbug_print_select(context->select_lex)
$58 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp >= TIMESTAMP'0000-00-00 00:00:00' order by `mysql`.transaction_registry.commit_timestamp limit 1"
(gdb) p dbug_print_select(table_list->select_lex)
$59 = 0x2019ad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= TIMESTAMP'0001-01-01 00:00:00' order by `mysql`.transaction_registry.commit_timestamp desc limit 1"

Cause

Same TRT TABLE_LIST is added for both subqueries.

Fix

Open TRT as a log table after open_and_lock_tables() for each subquery.

midenok commented 6 years ago

Bug 14: assertion on reading TRT field

Reproduce

select x from i1 for system_time from timestamp '0-0-0 0:0:0' to timestamp '1-1-1 0:0:0';

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x1616183 "!table || (!table->read_set || bitmap_is_set(table->read_set, field_index))", file=0x1615d10 "/home/midenok/src/mariadb/midenok/src/sql/field.cc", line=4366, function=0x16171b4 "virtual longlong Field_longlong::val_int()") at assert.c:101
#4  0x0000000000ad476f in Field_longlong::val_int (this=0x7fff7c0432a0) at /home/midenok/src/mariadb/midenok/src/sql/field.cc:4366
#5  0x0000000000913534 in TR_table::query_sees (this=0x7fffe4eb9e40, result=@0x7fffe4eba4af: true, trx_id1=5344, trx_id0=19, commit_id1=0, iso_level1=ISO_READ_UNCOMMITTED, commit_id0=0) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:9034
#6  0x0000000000a0e0db in Item_func_trt_trx_sees::val_int (this=0x7fff7c03dbf8) at /home/midenok/src/mariadb/midenok/src/sql/item_vers.cc:177
#7  0x00000000008e9a2c in SQL_SELECT::skip_record (this=0x7fff7c046f38, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/opt_range.h:1658
#8  0x00000000009a49a4 in JOIN_CACHE::check_match (this=0x7fff7c048520, rec_ptr=0x7fff7c04b638 "\375\002") at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2449
#9  0x00000000009a0243 in JOIN_CACHE::generate_full_extensions (this=0x7fff7c048520, rec_ptr=0x7fff7c04b638 "\375\002") at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2392
#10 0x00000000009a011f in JOIN_CACHE::join_matching_records (this=0x7fff7c048520, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2292
#11 0x000000000099f963 in JOIN_CACHE::join_records (this=0x7fff7c048520, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2088
#12 0x0000000000844077 in sub_select_cache (join=0x7fff7c017418, join_tab=0x7fff7c047a10, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19037
#13 0x0000000000843a42 in sub_select (join=0x7fff7c017418, join_tab=0x7fff7c047660, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19208
#14 0x000000000082c379 in do_select (join=0x7fff7c017418, procedure=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18799
#15 0x000000000082b102 in JOIN::exec_inner (this=0x7fff7c017418) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4013
#16 0x000000000082a24e in JOIN::exec (this=0x7fff7c017418) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3807
#17 0x00000000008020cd in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c012fe8, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c0173f8, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4212
#18 0x0000000000801903 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c0173f8, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#19 0x00000000007c2fdf in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012fe8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6553
#20 0x00000000007b8071 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#21 0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from i1 for system_time from timestamp '0-0-0 0:0:0' to timestamp '1-1-1 0:0:0'", length=88, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

TABLE opened

#0  open_table (thd=0x7fff7c000ce8, table_list=0x7fffe4eb9e40, ot_ctx=0x7fffe4eb9970) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1533
#1  0x000000000070ccc7 in open_ltable (thd=0x7fff7c000ce8, table_list=0x7fffe4eb9e40, lock_type=TL_READ, lock_flags=2075) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4841
#2  0x0000000000717399 in open_log_table (thd=0x7fff7c000ce8, one_table=0x7fffe4eb9e40, backup=0x7fff7c041d70) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8894
#3  0x000000000091165d in TR_table::open (this=0x7fffe4eb9e40) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8837
#4  0x00000000009121a8 in TR_table::query (this=0x7fffe4eb9e40, trx_id=5344) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8904
#5  0x0000000000913501 in TR_table::query_sees (this=0x7fffe4eb9e40, result=@0x7fffe4eba4af: true, trx_id1=5344, trx_id0=19, commit_id1=0, iso_level1=ISO_READ_UNCOMMITTED, commit_id0=0) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:9031
#6  0x0000000000a0e0db in Item_func_trt_trx_sees::val_int (this=0x7fff7c03dbf8) at /home/midenok/src/mariadb/midenok/src/sql/item_vers.cc:177
#7  0x00000000008e9a2c in SQL_SELECT::skip_record (this=0x7fff7c046f38, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/opt_range.h:1658
#8  0x00000000009a49a4 in JOIN_CACHE::check_match (this=0x7fff7c048520, rec_ptr=0x7fff7c04b638 "\375\002") at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2449
#9  0x00000000009a0243 in JOIN_CACHE::generate_full_extensions (this=0x7fff7c048520, rec_ptr=0x7fff7c04b638 "\375\002") at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2392
#10 0x00000000009a011f in JOIN_CACHE::join_matching_records (this=0x7fff7c048520, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2292
#11 0x000000000099f963 in JOIN_CACHE::join_records (this=0x7fff7c048520, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2088
#12 0x0000000000844077 in sub_select_cache (join=0x7fff7c017418, join_tab=0x7fff7c047a10, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19037
#13 0x0000000000843a42 in sub_select (join=0x7fff7c017418, join_tab=0x7fff7c047660, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19208
#14 0x000000000082c379 in do_select (join=0x7fff7c017418, procedure=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18799
#15 0x000000000082b102 in JOIN::exec_inner (this=0x7fff7c017418) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4013
#16 0x000000000082a24e in JOIN::exec (this=0x7fff7c017418) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3807
#17 0x00000000008020cd in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c012fe8, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c0173f8, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4212
#18 0x0000000000801903 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c0173f8, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#19 0x00000000007c2fdf in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012fe8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6553
#20 0x00000000007b8071 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#21 0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from i1 for system_time from timestamp '0-0-0 0:0:0' to timestamp '1-1-1 0:0:0'", length=88, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

Fix

@@ -8884,8 +8900,13 @@ bool TR_table::update(ulonglong start_id, ulonglong end_id)                                                     

 bool TR_table::query(ulonglong trx_id)                                                                                                
 {                                                                                                                                     
-  if (!table && open())                                                                                                               
-    return false;                                                                                                                     
+  if (!table)                                                                                                                         
+  {                                                                                                                                   
+    if (open())                                                                                                                       
+      return false;                                                                                                                   
+    DBUG_ASSERT(table);                                                                                                               
+    bitmap_set_all(table->read_set);                                                                                                  
+  }                                                                                                                                   
   SQL_SELECT_auto select;                                                                                                             
   READ_RECORD info;                                                                                                                   
   int error;                                                                                                                          
midenok commented 6 years ago

Bug 15: assertion in handler::ha_reset()

Reproduce

create or replace table t1 (
  x int,
  sys_trx_start bigint unsigned as row start invisible,
  sys_trx_end bigint unsigned as row end invisible,
  period for system_time (sys_trx_start, sys_trx_end))
with system versioning;

insert into t1 (x) values (1);
select x from t1 for system_time as of timestamp now(6);

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x161d656 "bitmap_is_set_all(&table->s->all_set)", file=0x161b880 "/home/midenok/src/mariadb/midenok/src/sql/handler.cc", line=6172, function=0x161d63e "int handler::ha_reset()") at assert.c:101
#4  0x0000000000b1e618 in handler::ha_reset (this=0x3318f00) at /home/midenok/src/mariadb/midenok/src/sql/handler.cc:6172
#5  0x0000000000703458 in close_thread_table (thd=0x7fff78000ce8, table_ptr=0x7fff78000dd0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:901
#6  0x0000000000703a24 in close_thread_tables (thd=0x7fff78000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:852
#7  0x00000000007c13a6 in mysql_execute_command (thd=0x7fff78000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6369
#8  0x00000000007b2a6f in mysql_parse (thd=0x7fff78000ce8, rawbuf=0x7fff78012be0 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

frame 4

6172      DBUG_ASSERT(bitmap_is_set_all(&table->s->all_set));
(gdb) x table->s->all_set.bitmap
0x33176e8:      0x00000009
(gdb) p table->alias.Ptr
$8 = 0x32a1aa8 "transaction_registry"

TABLE_SHARE inited

#0  TABLE_SHARE::init_from_binary_frm_image (this=0x3315000, thd=0x2933eb8, write=false, frm_image=0x3315588 "\376\001\n\f\022", frm_length=2618) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:1168
#1  0x00000000008f38dc in open_table_def (thd=0x2933eb8, share=0x3315000, flags=11) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:670
#2  0x0000000000a1b2ca in tdc_acquire_share (thd=0x2933eb8, tl=0x7fffffffb780, flags=3, out_table=0x7fffffffb388) at /home/midenok/src/mariadb/midenok/src/sql/table_cache.cc:839
#3  0x00000000007057e2 in open_table (thd=0x2933eb8, table_list=0x7fffffffb780, ot_ctx=0x7fffffffb470) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1797
#4  0x000000000070ccc7 in open_ltable (thd=0x2933eb8, table_list=0x7fffffffb780, lock_type=TL_READ, lock_flags=2075) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4841
#5  0x0000000000717399 in open_log_table (thd=0x2933eb8, one_table=0x7fffffffb780, backup=0x311c650) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8894
#6  0x000000000091163d in TR_table::open (this=0x7fffffffb780) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8836
#7  0x000000000064b1be in mysqld_main (argc=32, argv=0x28a2e48) at /home/midenok/src/mariadb/midenok/src/sql/mysqld.cc:6126
#8  0x0000000000646632 in main (argc=6, argv=0x7fffffffbf58) at /home/midenok/src/mariadb/midenok/src/sql/main.cc:25

s->all_set.bitmap data written

#0  __memset_avx2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:259
#1  0x0000000000908e70 in TABLE::mark_columns_used_by_index (this=0x3317c48, index=3, bitmap=0x3315040) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:6245
#2  0x000000000080e1ce in JOIN::init_join_caches (this=0x7fff7c015b28) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1473
#3  0x000000000080c328 in JOIN::optimize_stage2 (this=0x7fff7c015b28) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:2584
#4  0x000000000080e070 in JOIN::optimize_inner (this=0x7fff7c015b28) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1899
#5  0x0000000000809685 in JOIN::optimize (this=0x7fff7c015b28) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1448
#6  0x000000000075de07 in mysql_derived_optimize (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c014e50) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:934
#7  0x0000000000760157 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c014e50, phases=4) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#8  0x000000000080d2f7 in JOIN::optimize_inner (this=0x7fff7c0154d8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1698
#9  0x0000000000809685 in JOIN::optimize (this=0x7fff7c0154d8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1448
#10 0x0000000000801fdf in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c012f18, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c0154b8, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4198
#11 0x0000000000801903 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c0154b8, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#12 0x00000000007c2fdf in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012f18) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6553
#13 0x00000000007b8071 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#14 0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

frame 2

1473            table->mark_columns_used_by_index(table->file->keyread, table->read_set);
(gdb) p table->read_set
$31 = (MY_BITMAP *) 0x3315040
(gdb) p &table->s->all_set
$32 = (MY_BITMAP *) 0x3315040

Cause

table->read_set is pointer to table->s->all_set. This is initial state after open_log_table().

midenok commented 6 years ago

table->read_set assigned to &table->s->all_set

#0  TABLE::column_bitmaps_set (this=0x3317bc8, read_set_arg=0x3314fc0, write_set_arg=0x3314fc0) at /home/midenok/src/mariadb/midenok/src/sql/table.h:1393
#1  0x00000000006fc37e in TABLE::use_all_columns (this=0x3317bc8) at /home/midenok/src/mariadb/midenok/src/sql/table.h:1423
#2  0x0000000000717436 in open_log_table (thd=0x2933eb8, one_table=0x7fffffffb780, backup=0x326c0a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8898
#3  0x000000000091163d in TR_table::open (this=0x7fffffffb780) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8836
#4  0x000000000064b1be in mysqld_main (argc=32, argv=0x28a2e48) at /home/midenok/src/mariadb/midenok/src/sql/mysqld.cc:6126
#5  0x0000000000646632 in main (argc=6, argv=0x7fffffffbf58) at /home/midenok/src/mariadb/midenok/src/sql/main.cc:25

Fix

@@ -8820,6 +8836,9 @@ bool TR_table::open()                                                                                                       
   bool error= !open_log_table(thd, this, open_tables_backup);                                                                                    
   thd->temporary_tables= temporary_tables;                                                                                                       

+  if (!error)                                                                                                                                    
+    table->default_column_bitmaps();                                                                                                             
+                                                                                                                                                 
   if (use_transaction_registry == MAYBE)                                                                                                         
   {                                                                                                                                              
     if (error)                                                                                                                                   
midenok commented 6 years ago

Bug 16: assertion in tdc_remove_table()

Reproduce

create or replace table t1 (
  x int,
  sys_trx_start bigint unsigned as row start invisible,
  sys_trx_end bigint unsigned as row end invisible,
  period for system_time (sys_trx_start, sys_trx_end))
with system versioning;
select x from t1 for system_time as of timestamp now(6);
drop table t1;

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x1534e75 "element->all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL", file=0x1534a80 "/home/midenok/src/mariadb/midenok/src/sql/table_cache.cc", line=1147, function=0x1534dcc "bool tdc_remove_table(THD *, enum_tdc_remove_table_type, const char *, const char *, bool)") at assert.c:101
#4  0x0000000000a1c9c9 in tdc_remove_table (thd=0x7fff98000ce8, remove_type=TDC_RT_REMOVE_ALL, db=0x7fff98016540 "test", table_name=0x7fff98015eb0 "t1", kill_delayed_threads=false) at /home/midenok/src/mariadb/midenok/src/sql/table_cache.cc:1147
#5  0x00000000008a99a8 in mysql_rm_table_no_locks (thd=0x7fff98000ce8, tables=0x7fff98015ee8, if_exists=false, drop_temporary=false, drop_view=false, drop_sequence=false, dont_log_query=false, dont_free_locks=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_table.cc:2502
#6  0x00000000008a8803 in mysql_rm_table (thd=0x7fff98000ce8, tables=0x7fff98015ee8, if_exists=false, drop_temporary=false, drop_sequence=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_table.cc:2127
#7  0x00000000007bc7a1 in mysql_execute_command (thd=0x7fff98000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:5053
#8  0x00000000007b2a6f in mysql_parse (thd=0x7fff98000ce8, rawbuf=0x7fff98015e20 "drop table t1", length=13, parser_state=0x7fffe835e5f0, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

frame 4

1147      DBUG_ASSERT(element->all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL);
  inline bool is_empty() const { return (m_first == NULL); }
(gdb) p element->share->table_name.str
$7 = 0x7fff98076edd "t1"
(gdb) p element->all_tables->m_first->alias.Ptr
$8 = 0x7fff98034a18 "t1"
(gdb) p element->all_tables_refs
$9 = 0

Good: all_tables->m_first cleared

#0  I_P_List<TABLE, All_share_tables, I_P_List_null_counter, I_P_List_no_push_back<TABLE> >::remove (this=0x7fff78070718, a=0x7fff78077c80) at /home/midenok/src/mariadb/trunk/src/sql/sql_plist.h:130
#1  0x0000000000a1a300 in tc_remove_all_unused_tables (element=0x7fff78070470, purge_tables=0x7fffe4ecdbd8, mark_flushed=true) at /home/midenok/src/mariadb/trunk/src/sql/table_cache.cc:288
#2  0x0000000000a19e47 in tdc_remove_table (thd=0x7fff78000d50, remove_type=TDC_RT_REMOVE_ALL, db=0x7fff780141c8 "test", table_name=0x7fff78013b48 "t1", kill_delayed_threads=false) at /home/midenok/src/mariadb/trunk/src/sql/table_cache.cc:1131

frame 1

280       for (ulong i= 0; i < tc_instances; i++)
281       {
282         mysql_mutex_lock(&tc[i].LOCK_table_cache);
283         while ((table= element->free_tables[i].list.pop_front()))
284         {
285           tc[i].records--;
286           tc[i].free_tables.remove(table);
287           DBUG_ASSERT(element->all_tables_refs == 0);
288           element->all_tables.remove(table);
289           purge_tables->push_front(table);
290         }
291         mysql_mutex_unlock(&tc[i].LOCK_table_cache);
292       }
(gdb) p tc_instances
$4 = 8
midenok commented 6 years ago

Good

1. table->s->tdc assigned

#0  tdc_acquire_share (thd=0x7fff78000d50, tl=0x7fff78013df0, flags=3, out_table=0x7fffe4ecda48) at /home/midenok/src/mariadb/trunk/src/sql/table_cache.cc:851
#1  0x0000000000705582 in open_table (thd=0x7fff78000d50, table_list=0x7fff78013df0, ot_ctx=0x7fffe4ecddc8) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:1797
#2  0x0000000000709fd8 in open_and_process_table (thd=0x7fff78000d50, lex=0x7fff78004af0, tables=0x7fff78013df0, counter=0x7fffe4ecdecc, flags=0, prelocking_strategy=0x7fffe4ecdf40, has_prelocking_list=false, ot_ctx=0x7fffe4ecddc8) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:3545
#3  0x0000000000708d2d in open_tables (thd=0x7fff78000d50, options=..., start=0x7fffe4ecdee0, counter=0x7fffe4ecdecc, flags=0, prelocking_strategy=0x7fffe4ecdf40) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:4063
#4  0x000000000070d315 in open_and_lock_tables (thd=0x7fff78000d50, options=..., tables=0x7fff78013df0, derived=true, flags=0, prelocking_strategy=0x7fffe4ecdf40) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:4940
#5  0x00000000006ba9cc in open_and_lock_tables (thd=0x7fff78000d50, tables=0x7fff78013df0, derived=true, flags=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.h:495
#6  0x00000000007c22bb in execute_sqlcom_select (thd=0x7fff78000d50, all_tables=0x7fff78013df0) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6463
#7  0x00000000007b7a41 in mysql_execute_command (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3764
#8  0x00000000007b24ef in mysql_parse (thd=0x7fff78000d50, rawbuf=0x7fff78013ab8 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ed1650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8077

2. table->s->tdc->free_tables[0].list.m_first assigned

#0  I_P_List<TABLE, TABLE_share, I_P_List_null_counter, I_P_List_no_push_back<TABLE> >::push_front (this=0x7fff78076590, a=0x7fff7807b210) at /home/midenok/src/mariadb/trunk/src/sql/sql_plist.h:91
#1  0x0000000000a170d7 in tc_release_table (table=0x7fff7807b210) at /home/midenok/src/mariadb/trunk/src/sql/table_cache.cc:478
#2  0x0000000000703226 in close_thread_table (thd=0x7fff78000d50, table_ptr=0x7fff78000e38) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:910
#3  0x00000000007037c4 in close_thread_tables (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:852
#4  0x00000000007c0d76 in mysql_execute_command (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6363
#5  0x00000000007b24ef in mysql_parse (thd=0x7fff78000d50, rawbuf=0x7fff78013ab8 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ed1650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8077

frame 1

468       if (table->needs_reopen() || table->s->tdc->flushed ||
469           tc[i].records > tc_size)
470       {
...
475       else
476       {
477         table->in_use= 0;
478         table->s->tdc->free_tables[i].list.push_front(table);
479         tc[i].free_tables.push_back(table);
480         mysql_mutex_unlock(&tc[i].LOCK_table_cache);
481       }

frame 3

851       while (thd->open_tables)
852         (void) close_thread_table(thd, &thd->open_tables);
midenok commented 6 years ago

Good: thd->open_tables changed

#0  close_thread_table (thd=0x7fff78000d50, table_ptr=0x7fff78000e38) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:893
#1  0x00000000007037c4 in close_thread_tables (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_base.cc:852
#2  0x00000000007c0d76 in mysql_execute_command (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6363
#3  0x00000000007b24ef in mysql_parse (thd=0x7fff78000d50, rawbuf=0x7fff78013ab8 "select x from t1, t2", length=20, parser_state=0x7fffe4ed1650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8077
891       mysql_mutex_lock(&thd->LOCK_thd_data);
892       *table_ptr=table->next;
893       mysql_mutex_unlock(&thd->LOCK_thd_data);

Bad

#0  close_thread_tables (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:852
#1  0x00000000007c13a6 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6369
#2  0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sq
(gdb) p thd->open_tables->alias.Ptr
$35 = 0x3294768 "transaction_registry"
(gdb) p thd->open_tables->next
$36 = (TABLE *) 0x0

Cause

No next link for separately opened TRT.

midenok commented 6 years ago

Bad

1. thd->open_tables is cleared

#0  Open_tables_state::reset_open_tables_state (this=0x7fff7c000dc8, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_class.h:1583
#1  0x0000000000744c0c in THD::reset_n_backup_open_tables_state (this=0x7fff7c000ce8, backup=0x7fff7c01c1a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_class.cc:4496
#2  0x000000000071737b in open_log_table (thd=0x7fff7c000ce8, one_table=0x7fff7c013648, backup=0x7fff7c01c1a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8892
#3  0x000000000091163d in TR_table::open (this=0x7fff7c013648) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8836
#4  0x0000000000910698 in LEX::vers_add_subquery2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8, p=..., cur_select=0x7fff7c0052d0, subq_n=@0x7fffe4ebaf34: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8684
#5  0x000000000079479d in LEX::vers_add_trt_query2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7251
#6  0x00000000007c2946 in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012f18) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6474
#7  0x00000000007b8071 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#8  0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

2. thd->open_tables set

#0  THD::set_open_tables (this=0x7fff7c000ce8, open_tables_arg=0x3317c98) at /home/midenok/src/mariadb/midenok/src/sql/sql_class.h:4411
#1  0x000000000070601a in open_table (thd=0x7fff7c000ce8, table_list=0x7fff7c013648, ot_ctx=0x7fffe4eba9f0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1974
#2  0x000000000070ccc7 in open_ltable (thd=0x7fff7c000ce8, table_list=0x7fff7c013648, lock_type=TL_READ, lock_flags=2075) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4841
#3  0x0000000000717399 in open_log_table (thd=0x7fff7c000ce8, one_table=0x7fff7c013648, backup=0x7fff7c01c1a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8894
#4  0x000000000091163d in TR_table::open (this=0x7fff7c013648) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8836
#5  0x0000000000910698 in LEX::vers_add_subquery2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8, p=..., cur_select=0x7fff7c0052d0, subq_n=@0x7fffe4ebaf34: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8684
#6  0x000000000079479d in LEX::vers_add_trt_query2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7251
#7  0x00000000007c2946 in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012f18) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6474
#8  0x00000000007b8071 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#9  0x00000000007b2a6f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select x from t1 for system_time as of timestamp now(6)", length=55, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

Fix

Don't use open_log_table(), use directly open_ltable().

midenok commented 6 years ago

Tests failed so far

versioning.select versioning.select2 versioning.trx_id
midenok commented 6 years ago

Bug 17: ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION instead of ER_ILLEGAL_PARAMETER_DATA_TYPE_FOR_OPERATION

Reproduce

select * from t1 for system_time as of (1,1);

Result

#0  my_error (nr=4078, MyFlags=0) at /home/midenok/src/mariadb/midenok/src/mysys/my_error.c:113
#1  0x0000000000badb13 in Item_func::check_argument_types_like_args0 (this=0x7fff7c014ba0) at /home/midenok/src/mariadb/midenok/src/sql/item_func.cc:166
#2  0x0000000000b785b5 in Item_bool_rowready_func2::check_arguments (this=0x7fff7c014ba0) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.h:502
#3  0x0000000000bae828 in Item_func::fix_fields (this=0x7fff7c014ba0, thd=0x7fff7c000ce8, ref=0x7fff7c016020) at /home/midenok/src/mariadb/midenok/src/sql/item_func.cc:378
#4  0x00000000006835aa in Item::fix_fields_if_needed (this=0x7fff7c014ba0, thd=0x7fff7c000ce8, ref=0x7fff7c016020) at /home/midenok/src/mariadb/midenok/src/sql/item.h:822
#5  0x0000000000682777 in Item::fix_fields_if_needed_for_scalar (this=0x7fff7c014ba0, thd=0x7fff7c000ce8, ref=0x7fff7c016020) at /home/midenok/src/mariadb/midenok/src/sql/item.h:826
#6  0x000000000071bd35 in Item::fix_fields_if_needed_for_bool (this=0x7fff7c014ba0, thd=0x7fff7c000ce8, ref=0x7fff7c016020) at /home/midenok/src/mariadb/midenok/src/sql/item.h:830
#7  0x0000000000715397 in setup_conds (thd=0x7fff7c000ce8, tables=0x7fff7c013760, leaves=..., conds=0x7fff7c016020) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8191
#8  0x0000000000807bec in setup_without_group (thd=0x7fff7c000ce8, ref_pointer_array=..., tables=0x7fff7c013760, leaves=..., fields=..., all_fields=..., conds=0x7fff7c016020, order=0x7fff7c014e10, group=0x0, win_specs=..., win_funcs=..., hidden_group_fields=0x7fff7c015eff, reserved=0x7fff7c0140a4) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:646
#9  0x00000000008061c5 in JOIN::prepare (this=0x7fff7c015c18, tables_init=0x7fff7c013760, wild_num=0, conds_init=0x7fff7c014ba0, og_num=1, order_init=0x7fff7c014e10, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c013dc8, unit_arg=0x7fff7c0141e0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1104
#10 0x00000000008da8dd in st_select_lex_unit::prepare_join (this=0x7fff7c0141e0, thd_arg=0x7fff7c000ce8, sl=0x7fff7c013dc8, tmp_result=0x7fff7c015b30, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#11 0x00000000008d5b02 in st_select_lex_unit::prepare (this=0x7fff7c0141e0, derived_arg=0x7fff7c014f40, sel_result=0x7fff7c015b30, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#12 0x000000000075d89a in mysql_derived_prepare (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c014f40) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#13 0x00000000007606e7 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c014f40, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#14 0x000000000090df2f in TABLE_LIST::handle_derived (this=0x7fff7c014f40, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#15 0x0000000000787483 in st_select_lex::handle_derived (this=0x7fff7c0052d0, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#16 0x0000000000805981 in JOIN::prepare (this=0x7fff7c0155c8, tables_init=0x7fff7c013030, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c0052d0, unit_arg=0x7fff7c004b50) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:997
#17 0x0000000000802539 in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c013030, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c0155a8, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4190
#18 0x0000000000801e93 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c0155a8, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#19 0x00000000007c356f in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c013030) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6553
#20 0x00000000007b8601 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#21 0x00000000007b2fff in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select * from i2 for system_time as of (1,1)", length=44, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

Expected

#0  my_error (nr=4079, MyFlags=0) at /home/midenok/src/mariadb/trunk/src/mysys/my_error.c:113
#1  0x0000000000911188 in Vers_history_point::bad_expression_data_type_error (this=0x7fff78014520, type=0x1639e0a "row") at /home/midenok/src/mariadb/trunk/src/sql/table.cc:8886
#2  0x00000000009f2225 in Type_handler::Vers_history_point_resolve_unit (this=0x2015770 <type_handler_row>, thd=0x7fff78000d50, point=0x7fff78014520) at /home/midenok/src/mariadb/trunk/src/sql/sql_type.cc:5798
#3  0x00000000009110b1 in Vers_history_point::resolve_unit (this=0x7fff78014520, thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:8880
#4  0x0000000000910fdf in vers_select_conds_t::resolve_units (this=0x7fff78014518, thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/table.cc:8848
#5  0x000000000080305a in st_select_lex::vers_setup_conds (this=0x7fff78005328, thd=0x7fff78000d50, tables=0x7fff78013f08) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:848
#6  0x0000000000805182 in JOIN::prepare (this=0x7fff78014648, tables_init=0x7fff78013f08, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff78005328, unit_arg=0x7fff78004bb8) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1035
#7  0x0000000000801939 in mysql_select (thd=0x7fff78000d50, tables=0x7fff78013f08, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff78014628, unit=0x7fff78004bb8, select_lex=0x7fff78005328) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:4194
#8  0x0000000000801293 in handle_select (thd=0x7fff78000d50, lex=0x7fff78004af0, result=0x7fff78014628, setup_tables_done_option=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:370
#9  0x00000000007c2951 in execute_sqlcom_select (thd=0x7fff78000d50, all_tables=0x7fff78013f08) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:6542
#10 0x00000000007b7a41 in mysql_execute_command (thd=0x7fff78000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3764
#11 0x00000000007b24ef in mysql_parse (thd=0x7fff78000d50, rawbuf=0x7fff78013ab8 "select * from t1 for system_time as of (1,1)", length=44, parser_state=0x7fffe4ed1650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:8077

Cause

Subquery is checked first, now it have following expression:

"select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= (1,1) order by `mysql`.transaction_registry.commit_timestamp desc limit 1"

Fix

It's not a big issue and can be left as is.

midenok commented 6 years ago

Bug 18: error 'mysql.transaction_registry' doesn't exist

Reproduce

create or replace table ttx
(
  x int,
  start_timestamp bigint unsigned generated always as row start,
  end_timestamp   bigint unsigned generated always as row end,
  period for system_time(start_timestamp, end_timestamp)
) engine=innodb with system versioning;
create or replace function fts() returns datetime return '2001-01-01 10:20:30';

select * from ttx for system_time as of fts();

Result

#0  my_error (nr=1146, MyFlags=0) at /home/midenok/src/mariadb/midenok/src/mysys/my_error.c:113
#1  0x0000000000705388 in open_table (thd=0x7fff7c000ce8, table_list=0x7fff7c014c50, ot_ctx=0x7fffe4ebaa78) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1694
#2  0x000000000070d6a5 in open_ltable2 (thd=0x7fff7c000ce8, table_list=0x7fff7c014c50, lock_type=TL_READ, lock_flags=18459) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4927
#3  0x0000000000911ba8 in TR_table::open2 (this=0x7fff7c014c50) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8881
#4  0x0000000000910bf8 in LEX::vers_add_subquery2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8, p=..., cur_select=0x7fff7c0052d0, subq_n=@0x7fffe4ebaf34: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8685
#5  0x0000000000794cfd in LEX::vers_add_trt_query2 (this=0x7fff7c004a88, thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7251
#6  0x00000000007c2ea6 in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c0142a0) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6474
#7  0x00000000007b85d1 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#8  0x00000000007b2fcf in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select * from ttx for system_time as of fts()", length=45, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

Fix

Allow LTM_PRELOCKED by MYSQL_OPEN_GET_NEW_TABLE.

midenok commented 6 years ago

Bug 19: as of transaction is treated as timestamp

Reproduce

create or replace table t1 (
  x int,
  sys_start bigint(20) unsigned as row start invisible,
  sys_end bigint(20) unsigned as row end invisible,
  period for system_time (sys_start, sys_end)
) with system versioning;

insert into t1 values (1);
select sys_start from t1 where x = 1 into @trx_id1;
select * from t1 for system_time as of transaction @trx_id1;

Result

+---------+------+----------------------------------+
| Level   | Code | Message                          |
+---------+------+----------------------------------+
| Warning | 1292 | Incorrect datetime value: '5734' |
+---------+------+----------------------------------+
#0  push_warning (thd=0x7fff7c000ce8, level=Sql_state_errno_level::WARN_LEVEL_WARN, code=1292, msg=0x7fffe4eb9450 "Incorrect datetime value: '5734'") at /home/midenok/src/mariadb/midenok/src/sql/sql_error.cc:702
#1  0x0000000000918199 in make_truncated_value_warning (thd=0x7fff7c000ce8, level=Sql_state_errno_level::WARN_LEVEL_WARN, sval=0x7fffe4eb9740, time_type=MYSQL_TIMESTAMP_ERROR, field_name=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_time.cc:966
#2  0x0000000000918a79 in number_to_time_with_warn (neg=false, nr=5734, sec_part=0, ltime=0x7fffe4eb9af8, fuzzydate=33554433, str=0x7fffe4eb9740, field_name=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_time.cc:460
#3  0x0000000000918c4b in int_to_datetime_with_warn (neg=false, value=5734, ltime=0x7fffe4eb9af8, fuzzydate=33554433, field_name=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_time.cc:504
#4  0x0000000000b2e00e in Item::get_date_from_int (this=0x7fff7c012e28, ltime=0x7fffe4eb9af8, fuzzydate=33554433) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:1438
#5  0x00000000009edb59 in Type_handler_int_result::Item_get_date (this=0x20187a0 <type_handler_longlong>, item=0x7fff7c012e28, ltime=0x7fffe4eb9af8, fuzzydate=33554433) at /home/midenok/src/mariadb/midenok/src/sql/sql_type.cc:3239
#6  0x0000000000bd16d2 in Item_func_user_var::get_date (this=0x7fff7c012e28, ltime=0x7fffe4eb9af8, fuzzydate=33554433) at /home/midenok/src/mariadb/midenok/src/sql/item_func.h:2368
#7  0x0000000000673785 in Item::get_date_result (this=0x7fff7c012e28, ltime=0x7fffe4eb9af8, fuzzydate=33554433) at /home/midenok/src/mariadb/midenok/src/sql/item.h:1530
#8  0x0000000000b28e18 in Item::val_datetime_packed_result (this=0x7fff7c012e28) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:121
#9  0x0000000000b4cc72 in Item_cache_temporal::cache_value (this=0x7fff7c0161a8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:10072
#10 0x0000000000b4c75e in Item_cache_temporal::val_datetime_packed (this=0x7fff7c0161a8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:9996
#11 0x0000000000b5e755 in Arg_comparator::compare_datetime (this=0x7fff7c014b50) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.cc:739
#12 0x0000000000b76ab2 in Arg_comparator::compare (this=0x7fff7c014b50) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.h:102
#13 0x0000000000b62265 in Item_func_le::val_int (this=0x7fff7c014a90) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.cc:1772
#14 0x0000000000844a82 in evaluate_join_record (join=0x7fff7c015b08, join_tab=0x7fff7c018050, error=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19352
#15 0x0000000000844241 in sub_select (join=0x7fff7c015b08, join_tab=0x7fff7c018050, end_of_records=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19257
#16 0x000000000082c879 in do_select (join=0x7fff7c015b08, procedure=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18797
#17 0x000000000082b662 in JOIN::exec_inner (this=0x7fff7c015b08) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4013
#18 0x000000000082a7ae in JOIN::exec (this=0x7fff7c015b08) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3807
#19 0x000000000080262d in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c013650, wild_num=0, fields=..., conds=0x7fff7c014a90, og_num=1, order=0x7fff7c014d00, group=0x0, having=0x0, proc_param=0x0, select_options=2416184064, result=0x7fff7c015a20, unit=0x7fff7c0140d0, select_lex=0x7fff7c013cb8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4212
#20 0x000000000075fbcb in mysql_derived_fill (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c014e30) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:1151
#21 0x00000000007606b7 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c014e30, phases=96) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#22 0x0000000000839370 in st_join_table::preread_init (this=0x7fff7c0747e8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:12439
#23 0x0000000000828aa7 in join_init_read_record (tab=0x7fff7c0747e8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:20184
#24 0x00000000009a2848 in JOIN_TAB_SCAN::open (this=0x7fff7c075678) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:3348
#25 0x00000000009a0983 in JOIN_CACHE::join_matching_records (this=0x7fff7c075548, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2251
#26 0x00000000009a03b3 in JOIN_CACHE::join_records (this=0x7fff7c075548, skip_last=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_join_cache.cc:2088
#27 0x00000000008445d7 in sub_select_cache (join=0x7fff7c0154b8, join_tab=0x7fff7c0747e8, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19037
#28 0x0000000000843fa2 in sub_select (join=0x7fff7c0154b8, join_tab=0x7fff7c074438, end_of_records=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:19208
#29 0x000000000082c8d9 in do_select (join=0x7fff7c0154b8, procedure=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:18799
#30 0x000000000082b662 in JOIN::exec_inner (this=0x7fff7c0154b8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4013
#31 0x000000000082a7ae in JOIN::exec (this=0x7fff7c0154b8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:3807
#32 0x000000000080262d in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c012f20, wild_num=1, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c015498, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4212
#33 0x0000000000801e63 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c015498, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#34 0x00000000007c353f in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c012f20) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6553
#35 0x00000000007b85d1 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3770
#36 0x00000000007b2fcf in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select * from t1 for system_time as of transaction @trx_id1", length=59, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8088

frame 19

 p dbug_print_select(join->select_lex)
$14 = 0x201aad0 <dbug_item_print_buf> "select `mysql`.transaction_registry.transaction_id AS transaction_id from `mysql`.transaction_registry where `mysql`.transaction_registry.commit_timestamp <= @trx_id1 order by `mysql`.transaction_registry.commit_timestamp desc limit 1"

Cause

transaction specifier is ignored.

Fix

--- a/sql/sql_lex.cc                                                                                                                   
+++ b/sql/sql_lex.cc                                                                                                                   
@@ -7248,14 +7248,17 @@ bool LEX::vers_add_trt_query2(THD *thd)                                                                        
     {                                                                                                                                 
     case SYSTEM_TIME_AS_OF:                                                                                                           
     case SYSTEM_TIME_BEFORE:                                                                                                          
-      if (vers_add_subquery2(thd, tl->vers_conditions.start, select_lex, subq_n))                                                     
+      if (tl->vers_conditions.start.unit != VERS_TRX_ID &&                                                                            
+        vers_add_subquery2(thd, tl->vers_conditions.start, select_lex, subq_n))                                                       
         return true;                                                                                                                  
       break;                                                                                                                          
     case SYSTEM_TIME_FROM_TO:                                                                                                         
     case SYSTEM_TIME_BETWEEN:                                                                                                         
-      if (vers_add_subquery2(thd, tl->vers_conditions.start, select_lex, subq_n, true))                                               
+      if (tl->vers_conditions.start.unit != VERS_TRX_ID &&                                                                            
+        vers_add_subquery2(thd, tl->vers_conditions.start, select_lex, subq_n, true))                                                 
         return true;                                                                                                                  
-      if (vers_add_subquery2(thd, tl->vers_conditions.end, select_lex, subq_n))                                                       
+      if (tl->vers_conditions.end.unit != VERS_TRX_ID &&                                                                              
+        vers_add_subquery2(thd, tl->vers_conditions.end, select_lex, subq_n))                                                         
         return true;                                                                                                                  
       break;                                                                                                                          
     default:;
midenok commented 6 years ago

Bug 20: assertion in derived

Reproduce

From versioning.select2:

select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x)
for system_time as of timestamp @t0 as t;

Result

#3  0x00007ffff5d9bfc2 in __GI___assert_fail (assertion=0x150b2bc "tr_subquery", file=0x15089ea "/home/midenok/src/mariadb/midenok/src/sql/table.cc", line=8516, function=0x150b2c8 "Item *Vers_history_point::make_trx_id(THD *, Name_resolution_context &) const") at assert.c:101
#4  0x000000000090f7e5 in Vers_history_point::make_trx_id (this=0x7fff7c014928, thd=0x7fff7c000ce8, ctx=...) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8516
#5  0x0000000000804aec in st_select_lex::vers_setup_conds (this=0x7fff7c013240, thd=0x7fff7c000ce8, tables=0x7fff7c014310) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:908
#6  0x0000000000805a02 in JOIN::prepare (this=0x7fff7c016a00, tables_init=0x7fff7c014310, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c013240, unit_arg=0x7fff7c013658) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1031
#7  0x00000000008da74d in st_select_lex_unit::prepare_join (this=0x7fff7c013658, thd_arg=0x7fff7c000ce8, sl=0x7fff7c013240, tmp_result=0x7fff7c016918, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#8  0x00000000008d5972 in st_select_lex_unit::prepare (this=0x7fff7c013658, derived_arg=0x7fff7c015c58, sel_result=0x7fff7c016918, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#9  0x000000000075d77a in mysql_derived_prepare (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c015c58) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#10 0x00000000007605c7 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c015c58, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#11 0x000000000090dd9f in TABLE_LIST::handle_derived (this=0x7fff7c015c58, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#12 0x0000000000787363 in st_select_lex::handle_derived (this=0x7fff7c0052d0, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#13 0x00000000008057f1 in JOIN::prepare (this=0x7fff7c0163b0, tables_init=0x7fff7c015c58, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c0052d0, unit_arg=0x7fff7c004b50) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:997
#14 0x00000000008023a9 in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c015c58, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c016390, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4190
#15 0x0000000000801d03 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c016390, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#16 0x00000000007c33df in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c015c58) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#17 0x00000000007b8471 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#18 0x00000000007b2edf in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t", length=165, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

frame 4

#4  0x000000000090f7e5 in Vers_history_point::make_trx_id (this=0x7fff7c014928, thd=0x7fff7c000ce8, ctx=...) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8516
8516      DBUG_ASSERT(tr_subquery);

Cause

vers_add_trt_query2() was not called.

midenok commented 6 years ago

Info

mysql_test_select() is used for PS

#0  st_select_lex::vers_setup_conds (this=0x7fff800ac588, thd=0x7fff80000d50, tables=0x7fff800ad6a8, where_expr=0x7fff80016018) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:724
#1  0x000000000079500c in JOIN::prepare (this=0x7fff80015c10, tables_init=0x7fff800ad6a8, wild_num=1, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff800ac588, unit_arg=0x7fff800abe28) at /home/midenok/src/mariadb/trunk/src/sql/sql_select.cc:1189
#2  0x00000000008699c8 in st_select_lex_unit::prepare_join (this=0x7fff800abe28, thd_arg=0x7fff80000d50, sl=0x7fff800ac588, tmp_result=0x0, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:661
#3  0x0000000000864d49 in st_select_lex_unit::prepare (this=0x7fff800abe28, thd_arg=0x7fff80000d50, sel_result=0x0, additional_options=0) at /home/midenok/src/mariadb/trunk/src/sql/sql_union.cc:946
#4  0x0000000000778b94 in mysql_test_select (stmt=0x7fff800ab900, tables=0x7fff800ad6a8) at /home/midenok/src/mariadb/trunk/src/sql/sql_prepare.cc:1547
#5  0x00000000007739cf in check_prepared_statement (stmt=0x7fff800ab900) at /home/midenok/src/mariadb/trunk/src/sql/sql_prepare.cc:2338
#6  0x000000000076e085 in Prepared_statement::prepare (this=0x7fff800ab900, packet=0x7fff80015b38 "select * from t1 for system_time all", packet_len=36) at /home/midenok/src/mariadb/trunk/src/sql/sql_prepare.cc:3901
#7  0x000000000076ea69 in mysql_sql_stmt_prepare (thd=0x7fff80000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_prepare.cc:2779
#8  0x0000000000746ef0 in mysql_execute_command (thd=0x7fff80000d50) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:3760
#9  0x0000000000741a9f in mysql_parse (thd=0x7fff80000d50, rawbuf=0x7fff80015a48 "prepare stmt from 'select * from t1 for system_time all'", length=56, parser_state=0x7fffe526d650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/trunk/src/sql/sql_parse.cc:7988

Derived: table t1 opened

#0  open_table (thd=0x7fff7c000ce8, table_list=0x7fff7c013ab0, ot_ctx=0x7fffe4ebadb8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:1533
#1  0x000000000070a1d8 in open_and_process_table (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, tables=0x7fff7c013ab0, counter=0x7fffe4ebaebc, flags=0, prelocking_strategy=0x7fffe4ebaf30, has_prelocking_list=false, ot_ctx=0x7fffe4ebadb8) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:3545
#2  0x0000000000708f2d in open_tables (thd=0x7fff7c000ce8, options=..., start=0x7fffe4ebaed0, counter=0x7fffe4ebaebc, flags=0, prelocking_strategy=0x7fffe4ebaf30) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:4063
#3  0x000000000070d9e5 in open_and_lock_tables (thd=0x7fff7c000ce8, options=..., tables=0x7fff7c014160, derived=true, flags=0, prelocking_strategy=0x7fffe4ebaf30) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:5011
#4  0x00000000006babbc in open_and_lock_tables (thd=0x7fff7c000ce8, tables=0x7fff7c014160, derived=true, flags=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.h:498
#5  0x00000000007c2ceb in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c014160) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6464
#6  0x00000000007b8471 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#7  0x00000000007b2edf in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select * from (select * from t1) f", length=34, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

Tables from derived queries are in same global list.

midenok commented 6 years ago

vers_conditions are propagated from derived

807         if (outer_table && !vers_conditions.is_set())
808         {
809           // propagate system_time from nearest outer SELECT_LEX
810           vers_conditions= outer_table->vers_conditions;
811           outer_table->vers_conditions.used= true;
812         }
#0  st_select_lex::vers_setup_conds (this=0x7fff7c013240, thd=0x7fff7c000ce8, tables=0x7fff7c014310) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:810
#1  0x0000000000805a02 in JOIN::prepare (this=0x7fff7c016a00, tables_init=0x7fff7c014310, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c013240, unit_arg=0x7fff7c013658) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1031
#2  0x00000000008da74d in st_select_lex_unit::prepare_join (this=0x7fff7c013658, thd_arg=0x7fff7c000ce8, sl=0x7fff7c013240, tmp_result=0x7fff7c016918, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#3  0x00000000008d5972 in st_select_lex_unit::prepare (this=0x7fff7c013658, derived_arg=0x7fff7c015c58, sel_result=0x7fff7c016918, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#4  0x000000000075d77a in mysql_derived_prepare (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c015c58) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#5  0x00000000007605c7 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c015c58, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#6  0x000000000090dd9f in TABLE_LIST::handle_derived (this=0x7fff7c015c58, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#7  0x0000000000787363 in st_select_lex::handle_derived (this=0x7fff7c0052d0, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#8  0x00000000008057f1 in JOIN::prepare (this=0x7fff7c0163b0, tables_init=0x7fff7c015c58, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c0052d0, unit_arg=0x7fff7c004b50) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:997
#9  0x00000000008023a9 in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c015c58, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c016390, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4190
#10 0x0000000000801d03 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c016390, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#11 0x00000000007c33df in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c015c58) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#12 0x00000000007b8471 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#13 0x00000000007b2edf in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t", length=165, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

Cause

Propagation of SYSTEM_TIME is too late.

Fix

Move propagation to vers_add_trt_query2() or check if table is part of derived query with SYSTEM_TIME specifier.

midenok commented 6 years ago

After fix

select IJ2_x1 as IJ2_x1,
       y1 as y1,
       x2 as x2,
       y2 as y2
from
  (select t1.x as IJ2_x1,
          t1.y as y1,
          t2.x as x2,
          t2.y as y2
   from (test.t1
         join test.t2 on(t1.x = t2.x))
   join
     (select mysql.transaction_registry.transaction_id as transaction_id
      from mysql.transaction_registry
      where mysql.transaction_registry.commit_timestamp <= @t0
      order by mysql.transaction_registry.commit_timestamp desc
      limit 1) __trt_0) t

Tests failed so far

midenok commented 6 years ago

Bug 21: transaction_id field is not hidden in FROM .. TO

Reproduce

select * from t1 for system_time from '2000-01-01 00:00' to '2020-01-01 00:00';

Result

+------+------+----------------+
| x    | y    | transaction_id |
+------+------+----------------+

Expected

No transaction_id in resultset.

Fix

--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -7658,15 +7658,17 @@ bool setup_tables(THD *thd, Name_resolution_context *context,
     if (table_list->dont_resolve)
     {
       if (prev_tl)
-        prev_tl->next_name_resolution_table= table_list->next_name_resolution_table;
+        prev_tl->next_name_resolution_table= NULL;
       else
-      {
-        context->first_name_resolution_table= table_list->next_name_resolution_table;
-        continue;
-      }
+        context->first_name_resolution_table= NULL;
+      continue;
     }
+    if (prev_tl)
+      prev_tl->next_name_resolution_table= table_list;
+    else
+      context->first_name_resolution_table= table_list;
     prev_tl= table_list;
-  }
+}
midenok commented 6 years ago

Bug 22: prepare SELECT from derived fails

Adding subquery to open_normal_and_derived_tables() fails (as well as mysql_test_select() of course), because derived is not yet inited. It is done in mysql_handle_derived(). find_vers_conditions() also fails because st_select_lex_unit::derived is not yet assigned.

@@ -5071,8 +5071,16 @@ bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags,
   MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
   DBUG_ENTER("open_normal_and_derived_tables");
   DBUG_ASSERT(!thd->fill_derived_tables());
-  if (open_tables(thd, &tables, &counter, flags, &prelocking_strategy) ||
-      mysql_handle_derived(thd->lex, dt_phases))
+  if (open_tables(thd, &tables, &counter, flags, &prelocking_strategy))
+    goto end;
+
+  if (TR_table::use_transaction_registry &&
+      thd->lex->sql_command == SQLCOM_SELECT &&
+      thd->stmt_arena->is_stmt_prepare() &&
+      thd->lex->vers_add_tr_queries(thd))
+    return 1;
+
+  if (mysql_handle_derived(thd->lex, dt_phases))
     goto end;

   DBUG_RETURN(0);
midenok commented 6 years ago

Good: subquery->table assigned

#0  mysql_derived_prepare (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, derived=0x7fff7c017b70) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:809
#1  0x0000000000760687 in mysql_handle_single_derived (lex=0x7fff7c004a88, derived=0x7fff7c017b70, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#2  0x000000000090e06f in TABLE_LIST::handle_derived (this=0x7fff7c017b70, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#3  0x0000000000787423 in st_select_lex::handle_derived (this=0x7fff7c013240, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#4  0x000000000090dff6 in TABLE_LIST::handle_derived (this=0x7fff7c015c58, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7976
#5  0x0000000000787423 in st_select_lex::handle_derived (this=0x7fff7c0052d0, lex=0x7fff7c004a88, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#6  0x0000000000805ac1 in JOIN::prepare (this=0x7fff7c04f358, tables_init=0x7fff7c015c58, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff7c0052d0, unit_arg=0x7fff7c004b50) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1001
#7  0x0000000000802679 in mysql_select (thd=0x7fff7c000ce8, tables=0x7fff7c015c58, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff7c04f338, unit=0x7fff7c004b50, select_lex=0x7fff7c0052d0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4194
#8  0x0000000000801fd3 in handle_select (thd=0x7fff7c000ce8, lex=0x7fff7c004a88, result=0x7fff7c04f338, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#9  0x00000000007c368f in execute_sqlcom_select (thd=0x7fff7c000ce8, all_tables=0x7fff7c015c58) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#10 0x00000000007b8721 in mysql_execute_command (thd=0x7fff7c000ce8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#11 0x00000000007b318f in mysql_parse (thd=0x7fff7c000ce8, rawbuf=0x7fff7c012be0 "select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t", length=165, parser_state=0x7fffe4ebe650, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083
807       if (!derived->table)
808         derived->table= derived->derived_result->table;

Subquery must be added before TABLE_LIST::handle_derived().

midenok commented 5 years ago

FYI: how multi-update runs SELECT

1758      res= mysql_select(thd,
1759                        table_list, select_lex->with_wild, total_list, conds,
1760                        select_lex->order_list.elements, select_lex->order_list.first,
1761                        (ORDER *)NULL, (Item *) NULL, (ORDER *)NULL,
1762                        options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1763                        OPTION_SETUP_TABLES_DONE,
1764                        *result, unit, select_lex);
midenok commented 5 years ago

1. __tr_0 added

#0  LEX::vers_add_tr_subquery (this=0x7fff84004a98, thd=0x7fff84000cf8, p=..., cur_select=0x7fff84013250, subq_n=@0x7ffff4ab1764: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8793
#1  0x0000000000794e5c in LEX::vers_add_tr_queries (this=0x7fff84004a98, thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7249
#2  0x00000000008059d1 in JOIN::prepare (this=0x7fff840163c0, tables_init=0x7fff84015c68, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff840052e0, unit_arg=0x7fff84004b60) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:999
#3  0x0000000000802599 in mysql_select (thd=0x7fff84000cf8, tables=0x7fff84015c68, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff840163a0, unit=0x7fff84004b60, select_lex=0x7fff840052e0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4198
#4  0x0000000000801ef3 in handle_select (thd=0x7fff84000cf8, lex=0x7fff84004a98, result=0x7fff840163a0, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#5  0x00000000007c35d1 in execute_sqlcom_select (thd=0x7fff84000cf8, all_tables=0x7fff84015c68) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#6  0x00000000007b86c1 in mysql_execute_command (thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#7  0x00000000007b312f in mysql_parse (thd=0x7fff84000cf8, rawbuf=0x7fff84012bf0 "select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t", length=165, parser_state=0x7ffff4ab5640, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

2. __tr_0 added

#0  LEX::vers_add_tr_subquery (this=0x7fff84004a98, thd=0x7fff84000cf8, p=..., cur_select=0x7fff84013250, subq_n=@0x7ffff4ab0784: 0, backwards=false) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:8793
#1  0x0000000000794e5c in LEX::vers_add_tr_queries (this=0x7fff84004a98, thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:7249
#2  0x00000000008059d1 in JOIN::prepare (this=0x7fff84055620, tables_init=0x7fff84016928, wild_num=0, conds_init=0x7fff84017d68, og_num=1, order_init=0x7fff84017fd8, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff84016f90, unit_arg=0x7fff840173a8) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:999
#3  0x00000000008da9ad in st_select_lex_unit::prepare_join (this=0x7fff840173a8, thd_arg=0x7fff84000cf8, sl=0x7fff84016f90, tmp_result=0x7fff84055538, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#4  0x00000000008d5bd2 in st_select_lex_unit::prepare (this=0x7fff840173a8, derived_arg=0x7fff84018108, sel_result=0x7fff84055538, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#5  0x000000000075d7da in mysql_derived_prepare (thd=0x7fff84000cf8, lex=0x7fff84004a98, derived=0x7fff84018108) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#6  0x0000000000760627 in mysql_handle_single_derived (lex=0x7fff84004a98, derived=0x7fff84018108, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#7  0x000000000090dfff in TABLE_LIST::handle_derived (this=0x7fff84018108, lex=0x7fff84004a98, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#8  0x00000000007873c3 in st_select_lex::handle_derived (this=0x7fff84013250, lex=0x7fff84004a98, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#9  0x000000000090df86 in TABLE_LIST::handle_derived (this=0x7fff84015c68, lex=0x7fff84004a98, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7976
#10 0x00000000007873c3 in st_select_lex::handle_derived (this=0x7fff840052e0, lex=0x7fff84004a98, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#11 0x0000000000805a56 in JOIN::prepare (this=0x7fff840163c0, tables_init=0x7fff84015c68, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff840052e0, unit_arg=0x7fff84004b60) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1005
#12 0x0000000000802599 in mysql_select (thd=0x7fff84000cf8, tables=0x7fff84015c68, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2147748608, result=0x7fff840163a0, unit=0x7fff84004b60, select_lex=0x7fff840052e0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4198
#13 0x0000000000801ef3 in handle_select (thd=0x7fff84000cf8, lex=0x7fff84004a98, result=0x7fff840163a0, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#14 0x00000000007c35d1 in execute_sqlcom_select (thd=0x7fff84000cf8, all_tables=0x7fff84015c68) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6548
#15 0x00000000007b86c1 in mysql_execute_command (thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#16 0x00000000007b312f in mysql_parse (thd=0x7fff84000cf8, rawbuf=0x7fff84012bf0 "select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t", length=165, parser_state=0x7ffff4ab5640, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8083

Fix

Use local table list of JOIN.

midenok commented 5 years ago

Bug 23: prepared exec from derived error "Unknown column"

Reproduce

prepare stmt from
'select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x)
for system_time as of timestamp @t0 as t';
execute stmt;

Result

ERROR 1054 (42S22): Unknown column '__tr_1.transaction_id' in 'on clause'
#0  my_error (nr=1054, MyFlags=0) at /home/midenok/src/mariadb/midenok/src/mysys/my_error.c:113
#1  0x00000000006e1ace in find_field_in_tables (thd=0x7fff84000cf8, item=0x7fff84057738, first_table=0x7fff84050e40, last_table=0x0, ref=0x7fff840579c8, report_error=REPORT_ALL_ERRORS, check_privileges=true, register_tree_change=true) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:6308
#2  0x0000000000af759c in Item_field::fix_outer_field (this=0x7fff84057738, thd=0x7fff84000cf8, from_field=0x7ffff4aad7d0, reference=0x7fff840579c8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:5972
#3  0x0000000000af882d in Item_field::fix_fields (this=0x7fff84057738, thd=0x7fff84000cf8, reference=0x7fff840579c8) at /home/midenok/src/mariadb/midenok/src/sql/item.cc:6226
#4  0x0000000000657967 in Item::fix_fields_if_needed (this=0x7fff84057738, thd=0x7fff84000cf8, ref=0x7fff840579c8) at /home/midenok/src/mariadb/midenok/src/sql/item.h:822
#5  0x0000000000b5e965 in Item_func::fix_fields (this=0x7fff84057930, thd=0x7fff84000cf8, ref=0x7fff84012df0) at /home/midenok/src/mariadb/midenok/src/sql/item_func.cc:363
#6  0x0000000000657967 in Item::fix_fields_if_needed (this=0x7fff84057930, thd=0x7fff84000cf8, ref=0x7fff84012df0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:822
#7  0x0000000000656db7 in Item::fix_fields_if_needed_for_scalar (this=0x7fff84057930, thd=0x7fff84000cf8, ref=0x7fff84012df0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:826
#8  0x00000000006ec265 in Item::fix_fields_if_needed_for_bool (this=0x7fff84057930, thd=0x7fff84000cf8, ref=0x7fff84012df0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:830
#9  0x0000000000b24315 in Item_cond::fix_fields (this=0x7fff84012cd8, thd=0x7fff84000cf8, ref=0x7fff84050ea0) at /home/midenok/src/mariadb/midenok/src/sql/item_cmpfunc.cc:4589
#10 0x0000000000657967 in Item::fix_fields_if_needed (this=0x7fff84012cd8, thd=0x7fff84000cf8, ref=0x7fff84050ea0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:822
#11 0x0000000000656db7 in Item::fix_fields_if_needed_for_scalar (this=0x7fff84012cd8, thd=0x7fff84000cf8, ref=0x7fff84050ea0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:826
#12 0x00000000006ec265 in Item::fix_fields_if_needed_for_bool (this=0x7fff84012cd8, thd=0x7fff84000cf8, ref=0x7fff84050ea0) at /home/midenok/src/mariadb/midenok/src/sql/item.h:830
#13 0x00000000006e5567 in setup_on_expr (thd=0x7fff84000cf8, table=0x7fff84050e40, is_update=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8081
#14 0x00000000006e5a85 in setup_conds (thd=0x7fff84000cf8, tables=0x7fff84050e40, leaves=..., conds=0x7fff840149f0) at /home/midenok/src/mariadb/midenok/src/sql/sql_base.cc:8198
#15 0x00000000007d3986 in setup_without_group (thd=0x7fff84000cf8, ref_pointer_array=..., tables=0x7fff84050e40, leaves=..., fields=..., all_fields=..., conds=0x7fff840149f0, order=0x0, group=0x0, win_specs=..., win_funcs=..., hidden_group_fields=0x7fff840148cf, reserved=0x7fff8405004c) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:646
#16 0x00000000007d1fb4 in JOIN::prepare (this=0x7fff840145e8, tables_init=0x7fff84050e40, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff8404fd70, unit_arg=0x7fff84050188) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1107
#17 0x00000000008a22f3 in st_select_lex_unit::prepare_join (this=0x7fff84050188, thd_arg=0x7fff84000cf8, sl=0x7fff8404fd70, tmp_result=0x7fff84014500, additional_options=0, is_union_select=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:654
#18 0x000000000089d606 in st_select_lex_unit::prepare (this=0x7fff84050188, derived_arg=0x7fff84052788, sel_result=0x7fff84014500, additional_options=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_union.cc:977
#19 0x000000000072c707 in mysql_derived_prepare (thd=0x7fff84000cf8, lex=0x7fff8404def8, derived=0x7fff84052788) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:765
#20 0x000000000072f557 in mysql_handle_single_derived (lex=0x7fff8404def8, derived=0x7fff84052788, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_derived.cc:196
#21 0x00000000008d4acf in TABLE_LIST::handle_derived (this=0x7fff84052788, lex=0x7fff8404def8, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/table.cc:7979
#22 0x0000000000755a63 in st_select_lex::handle_derived (this=0x7fff8404e740, lex=0x7fff8404def8, phases=2) at /home/midenok/src/mariadb/midenok/src/sql/sql_lex.cc:4074
#23 0x00000000007d1782 in JOIN::prepare (this=0x7fff84013038, tables_init=0x7fff84052788, wild_num=0, conds_init=0x0, og_num=0, order_init=0x0, skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, select_lex_arg=0x7fff8404e740, unit_arg=0x7fff8404dfc0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:1000
#24 0x00000000007ce439 in mysql_select (thd=0x7fff84000cf8, tables=0x7fff84052788, wild_num=0, fields=..., conds=0x0, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_options=2416184064, result=0x7fff84052df0, unit=0x7fff8404dfc0, select_lex=0x7fff8404e740) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:4193
#25 0x00000000007cddc3 in handle_select (thd=0x7fff84000cf8, lex=0x7fff8404def8, result=0x7fff84052df0, setup_tables_done_option=0) at /home/midenok/src/mariadb/midenok/src/sql/sql_select.cc:370
#26 0x0000000000790224 in execute_sqlcom_select (thd=0x7fff84000cf8, all_tables=0x7fff84052788) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:6543
#27 0x000000000078540c in mysql_execute_command (thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3765
#28 0x00000000007b2176 in Prepared_statement::execute (this=0x7fff84047728, expanded_query=0x7ffff4ab2480, open_cursor=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4763
#29 0x00000000007addd4 in Prepared_statement::execute_loop (this=0x7fff84047728, expanded_query=0x7ffff4ab2480, open_cursor=false, packet=0x0, packet_end=0x0) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:4191
#30 0x00000000007ada61 in mysql_sql_stmt_execute (thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_prepare.cc:3299
#31 0x0000000000785459 in mysql_execute_command (thd=0x7fff84000cf8) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:3781
#32 0x0000000000780030 in mysql_parse (thd=0x7fff84000cf8, rawbuf=0x7fff84012bf0 "execute stmt", length=12, parser_state=0x7ffff4ab5640, is_com_multi=false, is_next_command=false) at /home/midenok/src/mariadb/midenok/src/sql/sql_parse.cc:8078

frame 19

p dbug_print_select(derived->select_lex)
select ij2_x1 as ij2_x1,
       y1 as y1,
       x2 as x2,
       y2 as y2
from
  (select t1.x as ij2_x1,
          t1.y as y1,
          t2.x as x2,
          t2.y as y2
   from (test.t1
         for system_time all
         join test.t2
         for system_time all on(t1.x = t2.x
                                and trt_trx_sees(t2.sys_end, __tr_1.transaction_id)
                                and trt_trx_sees_eq(__tr_1.transaction_id, t2.sys_start)))
   join
     (select mysql.transaction_registry.transaction_id as transaction_id
      from mysql.transaction_registry
      where mysql.transaction_registry.commit_timestamp <= @t0
      order by mysql.transaction_registry.commit_timestamp desc
      limit 1) __tr_0
   join
     (select mysql.transaction_registry.transaction_id as transaction_id
      from mysql.transaction_registry
      where mysql.transaction_registry.commit_timestamp <= @t0
      order by mysql.transaction_registry.commit_timestamp desc
      limit 1) __tr_1) t

non-prepared

select t1.x as ij2_x1,
       t1.y as y1,
       t2.x as x2,
       t2.y as y2
from t1
for system_time all
join t2
for system_time all
where t1.x = t2.x
  and t2.sys_end = 18446744073709551615
  and t1.sys_end = 18446744073709551615

original

select ij2_x1,
       y1,
       x2,
       y2
from
  (select t1.x as ij2_x1,
          t1.y as y1,
          t2.x as x2,
          t2.y as y2
   from t1
   inner join t2 on t1.x = t2.x)

Cause

Wrong place for __tr_1.

midenok commented 5 years ago

Good: no prepare

select IJ2_x1,y1,x2,y2 from (select t1.x as IJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2 from t1 inner join t2 on t1.x = t2.x) for system_time as of timestamp @t0 as t;

expands to:

select t1.x as ij2_x1,
       t1.y as y1,
       t2.x as x2,
       t2.y as y2
from t1
for system_time all
join t2
for system_time all
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp <= @t0
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __tr_0
join
  (select mysql.transaction_registry.transaction_id as transaction_id
   from mysql.transaction_registry
   where mysql.transaction_registry.commit_timestamp <= @t0
   order by mysql.transaction_registry.commit_timestamp desc
   limit 1) __tr_1
where t1.x = t2.x
  and trt_trx_sees(t2.sys_end, __tr_1.transaction_id)
  and trt_trx_sees_eq(__tr_1.transaction_id, t2.sys_start)
  and trt_trx_sees(t1.sys_end, __tr_1.transaction_id)
  and trt_trx_sees_eq(__tr_1.transaction_id, t1.sys_start)

Fix

Push the subqueries at the start of join list?

Bad: after duplicate subquery fix

select IJ2_x1 as IJ2_x1,
       y1 as y1,
       x2 as x2,
       y2 as y2
from
  (select t1.x as IJ2_x1,
          t1.y as y1,
          t2.x as x2,
          t2.y as y2
   from (test.t1
         for SYSTEM_TIME all
         join test.t2
         for SYSTEM_TIME all on(t1.x = t2.x
                                and trt_trx_sees(t2.sys_end, __tr_0.transaction_id)
                                and trt_trx_sees_eq(__tr_0.transaction_id, t2.sys_start)))
   join
     (select `mysql`.transaction_registry.transaction_id as transaction_id
      from `mysql`.transaction_registry
      where `mysql`.transaction_registry.commit_timestamp <= @t0
      order by `mysql`.transaction_registry.commit_timestamp desc
      limit 1) __tr_0) t;