337
213
--disable_warnings
338
214
drop table if exists t1;
339
215
drop table if exists t2;
340
drop function if exists f1;
341
drop procedure if exists p_verify_status_increment;
216
drop table if exists t3;
342
217
--enable_warnings
344
# Save binlog_format in a user variable. References to system
345
# variables are "unsafe", meaning they are written as rows instead of
346
# as statements to the binlog, if the loggging mode is 'mixed'. But
347
# we don't want p_verify_status_increment to affect the logging mode.
348
# Hence, we save binlog_format in a user variable (which is not
349
# unsafe) and use that inside p_verify_status_increment.
350
set @binlog_format=@@global.binlog_format;
352
set sql_mode=no_engine_substitution;
353
create table t1 (a int unique);
354
create table t2 (a int) engine=myisam;
355
set sql_mode=default;
357
--echo # An auxiliary procedure to track Handler_prepare and Handler_commit
362
p_verify_status_increment(commit_inc_mixed int, prepare_inc_mixed int,
363
commit_inc_row int, prepare_inc_row int)
365
declare commit_inc int;
366
declare prepare_inc int;
367
declare old_commit_count int default ifnull(@commit_count, 0);
368
declare old_prepare_count int default ifnull(@prepare_count, 0);
370
# Use a cursor to have just one access to I_S instead of 2, it is very slow
371
# and amounts for over 90% of test CPU time
373
select variable_value
374
from information_schema.session_status
375
where variable_name='Handler_commit' or variable_name='Handler_prepare'
376
order by variable_name;
378
if @binlog_format = 'ROW' then
379
set commit_inc= commit_inc_row;
380
set prepare_inc= prepare_inc_row;
382
set commit_inc= commit_inc_mixed;
383
set prepare_inc= prepare_inc_mixed;
388
set @commit_count=c_res;
390
set @prepare_count=c_res;
393
if old_commit_count + commit_inc <> @commit_count then
394
select concat("Expected commit increment: ", commit_inc,
395
" actual: ", @commit_count - old_commit_count)
397
elseif old_prepare_count + prepare_inc <> @prepare_count then
398
select concat("Expected prepare increment: ", prepare_inc,
399
" actual: ", @prepare_count - old_prepare_count)
402
select '' as 'SUCCESS';
406
--echo # Reset Handler_commit and Handler_prepare counters
409
--echo # 1. Read-only statement: SELECT
412
call p_verify_status_increment(1, 0, 1, 0);
414
call p_verify_status_increment(1, 0, 1, 0);
416
--echo # 2. Read-write statement: INSERT, insert 1 row.
418
insert into t1 (a) values (1);
419
call p_verify_status_increment(2, 2, 2, 2);
421
call p_verify_status_increment(2, 2, 2, 2);
423
--echo # 3. Read-write statement: UPDATE, update 1 row.
426
call p_verify_status_increment(2, 2, 2, 2);
428
call p_verify_status_increment(2, 2, 2, 2);
430
--echo # 4. Read-write statement: UPDATE, update 0 rows, 1 row matches WHERE
433
call p_verify_status_increment(2, 2, 1, 0);
435
call p_verify_status_increment(2, 2, 1, 0);
437
--echo # 5. Read-write statement: UPDATE, update 0 rows, 0 rows match WHERE
439
--echo # In mixed replication mode, there is a read-only transaction
440
--echo # in InnoDB and also the statement is written to the binary log.
441
--echo # So we have two commits but no 2pc, since the first engine's
442
--echo # transaction is read-only.
443
--echo # In the row level replication mode, we only have the read-only
444
--echo # transaction in InnoDB and nothing is written to the binary log.
446
update t1 set a=3 where a=1;
447
call p_verify_status_increment(2, 0, 1, 0);
449
call p_verify_status_increment(2, 0, 1, 0);
451
--echo # 6. Read-write statement: DELETE, delete 0 rows.
453
delete from t1 where a=1;
454
call p_verify_status_increment(2, 0, 1, 0);
456
call p_verify_status_increment(2, 0, 1, 0);
458
--echo # 7. Read-write statement: DELETE, delete 1 row.
460
delete from t1 where a=2;
461
call p_verify_status_increment(2, 2, 2, 2);
463
call p_verify_status_increment(2, 2, 2, 2);
465
--echo # 8. Read-write statement: unqualified DELETE
467
--echo # In statement or mixed replication mode, we call
468
--echo # handler::ha_delete_all_rows() and write statement text
469
--echo # to the binary log. This results in two read-write transactions.
470
--echo # In row level replication mode, we do not call
471
--echo # handler::ha_delete_all_rows(), but delete rows one by one.
472
--echo # Since there are no rows, nothing is written to the binary log.
473
--echo # Thus we have just one read-only transaction in InnoDB.
475
call p_verify_status_increment(2, 2, 1, 0);
477
call p_verify_status_increment(2, 2, 1, 0);
479
--echo # 9. Read-write statement: REPLACE, change 1 row.
482
call p_verify_status_increment(2, 2, 2, 2);
484
call p_verify_status_increment(2, 2, 2, 2);
486
--echo # 10. Read-write statement: REPLACE, change 0 rows.
489
call p_verify_status_increment(2, 2, 1, 0);
491
call p_verify_status_increment(2, 2, 1, 0);
493
--echo # 11. Read-write statement: IODKU, change 1 row.
495
insert t1 set a=1 on duplicate key update a=a+1;
496
call p_verify_status_increment(2, 2, 2, 2);
498
call p_verify_status_increment(1, 0, 1, 0);
500
call p_verify_status_increment(2, 2, 2, 2);
502
--echo # 12. Read-write statement: IODKU, change 0 rows.
504
insert t1 set a=2 on duplicate key update a=2;
505
call p_verify_status_increment(1, 0, 1, 0);
507
call p_verify_status_increment(1, 0, 1, 0);
509
--echo # 13. Read-write statement: INSERT IGNORE, change 0 rows.
511
insert ignore t1 set a=2;
512
call p_verify_status_increment(1, 0, 1, 0);
514
call p_verify_status_increment(1, 0, 1, 0);
516
--echo # 14. Read-write statement: INSERT IGNORE, change 1 row.
518
insert ignore t1 set a=1;
519
call p_verify_status_increment(2, 2, 2, 2);
521
call p_verify_status_increment(2, 2, 2, 2);
522
--echo # 15. Read-write statement: UPDATE IGNORE, change 0 rows.
524
update ignore t1 set a=2 where a=1;
525
call p_verify_status_increment(2, 2, 1, 0);
527
call p_verify_status_increment(2, 2, 1, 0);
529
--echo # Create a stored function that modifies a
530
--echo # non-transactional table. Demonstrate that changes in
531
--echo # non-transactional tables do not affect the two phase commit
535
create function f1() returns int
541
call p_verify_status_increment(0, 0, 0, 0);
543
--echo # 16. A function changes non-trans-table.
545
--echo # For row-based logging, there is an extra commit for the
546
--echo # non-transactional changes saved in the transaction cache to
547
--echo # the binary log.
550
call p_verify_status_increment(0, 0, 1, 0);
552
call p_verify_status_increment(0, 0, 1, 0);
554
--echo # 17. Read-only statement, a function changes non-trans-table.
556
--echo # For row-based logging, there is an extra commit for the
557
--echo # non-transactional changes saved in the transaction cache to
558
--echo # the binary log.
561
call p_verify_status_increment(1, 0, 2, 0);
563
call p_verify_status_increment(1, 0, 2, 0);
565
--echo # 18. Read-write statement: UPDATE, change 0 (transactional) rows.
567
select count(*) from t2;
568
update t1 set a=2 where a=f1()+10;
569
select count(*) from t2;
570
call p_verify_status_increment(2, 0, 2, 0);
572
call p_verify_status_increment(2, 0, 2, 0);
574
--echo # Replace the non-transactional table with a temporary
575
--echo # transactional table. Demonstrate that a change to a temporary
576
--echo # transactional table does not provoke 2-phase commit, although
577
--echo # does trigger a commit and a binlog write (in statement mode).
580
set sql_mode=no_engine_substitution;
581
create temporary table t2 (a int);
582
call p_verify_status_increment(0, 0, 0, 0);
583
set sql_mode=default;
584
--echo # 19. A function changes temp-trans-table.
587
--echo # Two commits because a binary log record is written
588
call p_verify_status_increment(2, 0, 1, 0);
590
call p_verify_status_increment(2, 0, 1, 0);
592
--echo # 20. Read-only statement, a function changes non-trans-table.
595
--echo # Two commits because a binary log record is written
596
call p_verify_status_increment(2, 0, 1, 0);
598
call p_verify_status_increment(2, 0, 1, 0);
600
--echo # 21. Read-write statement: UPDATE, change 0 (transactional) rows.
602
update t1 set a=2 where a=f1()+10;
603
call p_verify_status_increment(2, 0, 1, 0);
605
call p_verify_status_increment(2, 0, 1, 0);
607
--echo # 22. DDL: ALTER TEMPORARY TABLE, should not cause a 2pc
609
alter table t2 add column b int default 5;
610
--echo # A commit is done internally by ALTER.
611
call p_verify_status_increment(2, 0, 2, 0);
613
--echo # There is nothing left to commit
614
call p_verify_status_increment(0, 0, 0, 0);
616
--echo # 23. DDL: RENAME TEMPORARY TABLE, does not start a transaction
618
--echo # No test because of Bug#8729 "rename table fails on temporary table"
620
--echo # 24. DDL: TRUNCATE TEMPORARY TABLE, does not start a transaction
623
call p_verify_status_increment(2, 0, 2, 0);
625
--echo # There is nothing left to commit
626
call p_verify_status_increment(0, 0, 0, 0);
628
--echo # 25. Read-write statement: unqualified DELETE
631
call p_verify_status_increment(2, 0, 1, 0);
633
--echo # There is nothing left to commit
634
call p_verify_status_increment(2, 0, 1, 0);
636
--echo # 25. DDL: DROP TEMPORARY TABLE, does not start a transaction
638
drop temporary table t2;
639
call p_verify_status_increment(0, 0, 0, 0);
641
call p_verify_status_increment(0, 0, 0, 0);
643
--echo # 26. Verify that SET AUTOCOMMIT issues an implicit commit
646
call p_verify_status_increment(2, 2, 2, 2);
648
call p_verify_status_increment(2, 2, 2, 2);
650
select a from t1 where a=3;
651
call p_verify_status_increment(1, 0, 1, 0);
652
delete from t1 where a=3;
653
call p_verify_status_increment(2, 2, 2, 2);
655
call p_verify_status_increment(0, 0, 0, 0);
657
call p_verify_status_increment(0, 0, 0, 0);
659
call p_verify_status_increment(2, 2, 2, 2);
660
--echo # Sic: not actually changing the value of autocommit
662
call p_verify_status_increment(0, 0, 0, 0);
664
select a from t1 where a=3;
665
call p_verify_status_increment(1, 0, 1, 0);
667
--echo # 27. Savepoint management
670
call p_verify_status_increment(2, 2, 2, 2);
672
call p_verify_status_increment(0, 0, 0, 0);
674
--echo # Sic: a bug. Binlog did not register itself this time.
675
call p_verify_status_increment(1, 0, 1, 0);
678
call p_verify_status_increment(0, 0, 0, 0);
679
select a from t1 where a=3;
680
call p_verify_status_increment(1, 0, 1, 0);
682
call p_verify_status_increment(1, 0, 1, 0);
684
--echo # 28. Read-write statement: DO
686
create table t2 (a int);
687
call p_verify_status_increment(0, 0, 0, 0);
688
do (select f1() from t1 where a=2);
689
call p_verify_status_increment(2, 2, 2, 2);
691
call p_verify_status_increment(2, 2, 2, 2);
693
--echo # 29. Read-write statement: MULTI-DELETE
695
delete t1, t2 from t1 join t2 on (t1.a=t2.a) where t1.a=2;
697
call p_verify_status_increment(4, 4, 4, 4);
699
--echo # 30. Read-write statement: INSERT-SELECT, MULTI-UPDATE, REPLACE-SELECT
701
insert into t2 select a from t1;
703
replace into t2 select a from t1;
705
call p_verify_status_increment(8, 8, 8, 8);
707
# Multi-update is one of the few remaining statements that still
708
# locks the tables at prepare step (and hence starts the transaction.
709
# Disable the PS protocol, since in this protocol we get a different
710
# number of commmits (there is an extra commit after prepare
712
--disable_ps_protocol
713
update t1, t2 set t1.a=4, t2.a=8 where t1.a=t2.a and t1.a=1;
716
call p_verify_status_increment(4, 4, 4, 4);
718
--echo # 31. DDL: various DDL with transactional tables
720
--echo # Sic: no table is created.
721
create table if not exists t2 (a int) select 6 union select 7;
722
--echo # Sic: first commits the statement, and then the transaction.
723
call p_verify_status_increment(4, 4, 4, 4);
724
create table t3 select a from t2;
725
call p_verify_status_increment(4, 4, 4, 4);
726
alter table t3 add column (b int);
727
call p_verify_status_increment(2, 0, 2, 0);
728
alter table t3 rename t4;
729
call p_verify_status_increment(1, 0, 1, 0);
730
rename table t4 to t3;
731
call p_verify_status_increment(1, 0, 1, 0);
733
call p_verify_status_increment(2, 2, 2, 2);
734
create view v1 as select * from t2;
735
call p_verify_status_increment(1, 0, 1, 0);
737
call p_verify_status_increment(3, 0, 3, 0);
738
--echo # Sic: after this bug is fixed, CHECK leaves no pending transaction
740
call p_verify_status_increment(0, 0, 0, 0);
741
check table t1, t2, t3;
742
call p_verify_status_increment(6, 0, 6, 0);
744
call p_verify_status_increment(0, 0, 0, 0);
746
call p_verify_status_increment(0, 0, 0, 0);
751
drop table t1, t2, t3;
752
drop procedure p_verify_status_increment;