This file is indexed.

/usr/lib/mysql-testsuite/t/sp-lock.test is in percona-xtradb-cluster-testsuite-5.5 5.5.34-25.9+dfsg-0ubuntu4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
#
# Metadata lock handling for stored procedures and
# functions.
#
--echo # 
--echo # Test coverage for changes performed by the fix
--echo # for Bug#30977 "Concurrent statement using stored function
--echo # and DROP FUNCTION breaks SBR.
--echo #

--echo #
--echo # 1) Verify that the preceding transaction is
--echo # (implicitly) committed  before CREATE/ALTER/DROP
--echo # PROCEDURE. Note, that this is already tested
--echo # in implicit_commit.test, but here we use an alternative
--echo # approach.
--echo #

--echo # Start a transaction, create a savepoint, 
--echo # then call a DDL operation on a procedure, and then check
--echo # that the savepoint is no longer present.

--disable_warnings
drop table if exists t1;
drop procedure if exists p1;
drop procedure if exists p2;
drop procedure if exists p3;
drop procedure if exists p4;
drop function if exists f1;
--enable_warnings
create table t1 (a int);
--echo #
--echo # Test 'CREATE PROCEDURE'.
--echo #
begin;
savepoint sv;
create procedure p1() begin end;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'ALTER PROCEDURE'.
--echo #
begin;
savepoint sv;
alter procedure p1 comment 'changed comment';
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'DROP PROCEDURE'.
--echo #
begin;
savepoint sv;
drop procedure p1;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'CREATE FUNCTION'.
--echo #
begin;
savepoint sv;
create function f1() returns int return 1; 
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'ALTER FUNCTION'.
--echo #
begin;
savepoint sv;
alter function f1 comment 'new comment';
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;
--echo #
--echo # Test 'DROP FUNCTION'.
--echo #
begin;
savepoint sv;
drop function f1;
--error ER_SP_DOES_NOT_EXIST
rollback to savepoint sv;

--echo #
--echo # 2) Verify that procedure DDL operations fail
--echo # under lock tables. 
--echo #
--echo # Auxiliary routines to test ALTER.
create procedure p1() begin end;
create function f1() returns int return 1;

lock table t1 write;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
lock table t1 read;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
unlock tables;
--echo #
--echo # Even if we locked a temporary table.
--echo # Todo: this is a restriction we could possibly lift.
--echo #
drop table t1;
create temporary table t1 (a int);
lock table t1 read;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create procedure p2() begin end;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter procedure p1 comment 'changed comment';
--error ER_LOCK_OR_ACTIVE_TRANSACTION
drop procedure p1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
create function f2() returns int return 1;
--error ER_LOCK_OR_ACTIVE_TRANSACTION
alter function f1 comment 'changed comment';
unlock tables;

drop function f1;
drop procedure p1;
drop temporary table t1;

--echo #
--echo # 3) Verify that CREATE/ALTER/DROP routine grab an
--echo # exclusive lock.
--echo #
--echo # For that, start a transaction, use a routine. In a concurrent
--echo # connection, try to drop or alter the routine. It should place
--echo # a pending or exclusive lock and block. In another concurrnet
--echo # connection, try to use the routine.
--echo # That should block on the pending exclusive lock.
--echo #
--echo # Establish helper connections.
connect(con1, localhost, root,,);
connect(con2, localhost, root,,);
connect(con3, localhost, root,,);

--echo #
--echo # Test DROP PROCEDURE.
--echo #
--echo # --> connection default
connection default;
create procedure p1() begin end;
delimiter |;
create function f1() returns int 
begin
  call p1();
  return 1;
end|
delimiter ;|
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'...
send drop procedure p1;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop procedure t1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and
info='drop procedure p1';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop procedure p1'...
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
--error ER_SP_DOES_NOT_EXIST
reap;
--echo # --> connection default
connection default;

--echo #
--echo # Test CREATE PROCEDURE.
--echo #
create procedure p1() begin end;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create procedure p1'...
send create procedure p1() begin end;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'create procedure t1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and
info='create procedure p1() begin end';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'create procedure p1'...
--error ER_SP_ALREADY_EXISTS
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
connection default;

--echo # 
--echo # Test ALTER PROCEDURE.
--echo #
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter procedure p1'...
send alter procedure p1 contains sql;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'alter procedure t1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and
info='alter procedure p1 contains sql';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter procedure p1'...
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
--echo # --> connection default
connection default;

--echo #
--echo # Test DROP FUNCTION.
--echo #
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'...
send drop function f1;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f1';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
--error ER_SP_DOES_NOT_EXIST
reap;
--echo # --> connection default
connection default;

--echo #
--echo # Test CREATE FUNCTION.
--echo #
create function f1() returns int return 1;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'create function f1'...
send create function f1() returns int return 2;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'create function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='create function f1() returns int return 2';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'create function f1'...
--error ER_SP_ALREADY_EXISTS
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
--echo # --> connection default
connection default;

--echo # 
--echo # Test ALTER FUNCTION.
--echo # 
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1'...
send alter function f1 contains sql;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'alter function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='alter function f1 contains sql';
--source include/wait_condition.inc
--echo # Demonstrate that there is a pending exclusive lock.
--echo # Sending 'select f1()'...
send select f1();
--echo # --> connection con3
connection con3;
--echo # Waiting for 'select f1()' to get blocked by a pending MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and info='select f1()';
--echo # --> connection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1'...
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'select f1()'
reap;
--echo # --> connection default
connection default;
drop function f1;
drop procedure p1;

--echo # 
--echo # 4) MDL lock should not be taken for 
--echo # unrolled CALL statements.
--echo # The primary goal of metadata locks is a consistent binary log.
--echo # When a call statement is unrolled, it doesn't get to the
--echo # binary log, instead the statements that are contained
--echo # in the procedure body do. This can nest to any level.
--echo #
create procedure p1() begin end;
create procedure p2() begin end;
create table t1 (a int);
delimiter |;
create procedure p3()
begin
  call p1();
  call p1();
  call p2();
end|
create procedure p4()
begin
  call p1();
  call p1();
  call p2();
  call p2();
  call p3();
end|
delimiter ;|
begin;
select * from t1;
savepoint sv;
call p4();
--echo # Prepared statement should not add any locks either.
prepare stmt from "call p4()";
execute stmt;
execute stmt;
--echo # --> connection con1
connection con1;
drop procedure p1;
drop procedure p2;
drop procedure p3;
drop procedure p4;
--echo # --> connection default
connection default;
--echo # This is to verify there was no implicit commit.
rollback to savepoint sv;
--error ER_SP_DOES_NOT_EXIST
call p4();
commit;
drop table t1;

--echo #
--echo # 5) Locks should be taken on routines
--echo # used indirectly by views or triggers.
--echo #
--echo #
--echo # A function is used from a trigger.
--echo #
create function f1() returns int return 1;
create table t1 (a int);
create table t2 (a int, b int);
create trigger t1_ai after insert on t1 for each row
  insert into t2 (a, b) values (new.a, f1());
begin;
insert into t1 (a) values (1);
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'
send drop function f1;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f1';
--source include/wait_condition.inc
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--echo #
--echo # A function is used from a view.
--echo #
create function f1() returns int return 1;
create view v1 as select f1() as a;
begin;
select * from v1;
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'
send drop function f1;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f1';
--source include/wait_condition.inc
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--echo #
--echo # A procedure is used from a function.
--echo #
delimiter |;
create function f1() returns int
begin
  declare v_out int;
  call p1(v_out);
  return v_out;
end|
delimiter ;|
create procedure p1(out v_out int) set v_out=3;
begin;
select * from v1;
--echo # --> connection con1
connection con1;
--echo # Sending 'drop procedure p1'
send drop procedure p1;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop procedure p1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored procedure metadata lock' and
info='drop procedure p1';
--source include/wait_condition.inc
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop procedure p1'...
reap;
--echo # --> connection default
connection default;

--echo #
--echo # Deep nesting: a function is used from a procedure used
--echo # from a function used from a view used in a trigger.
--echo #
create function f2() returns int return 4;
create procedure p1(out v_out int) set v_out=f2();
drop trigger t1_ai;
create trigger t1_ai after insert on t1 for each row
  insert into t2 (a, b) values (new.a, (select max(a) from v1));
begin;
insert into t1 (a) values (3);
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f2'
send drop function f2;
--echo # --> connection con2 
connection con2;
--echo # Waiting for 'drop function f2' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f2';
--source include/wait_condition.inc
--echo # --> connnection default
connection default;
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f2'...
reap;
--echo # --> connection default
connection default;

drop view v1;
drop function f1;
drop procedure p1;
drop table t1, t2;

--echo #
--echo # 6) Check that ER_LOCK_DEADLOCK is reported if 
--echo # acquisition of a shared lock fails during a transaction or
--echo # we need to back off to flush the sp cache.
--echo #
--echo # Sic: now this situation does not require a back off since we
--echo # flush the cache on the fly.
--echo #
create function f1() returns int return 7;
create table t1 (a int);
begin;
select * from t1;
# Used to have a back-off here, with optional ER_LOCK_DEADLOCK
#--error ER_LOCK_DEADLOCK
select f1();
commit;
drop table t1;
drop function f1;

--echo #
--echo # 7) Demonstrate that under LOCK TABLES we accumulate locks
--echo # on stored routines, and release metadata locks in 
--echo # ROLLBACK TO SAVEPOINT. That is done only for those stored
--echo # routines that are not part of LOCK TABLES prelocking list.
--echo # Those stored routines that are part of LOCK TABLES
--echo # prelocking list are implicitly locked when entering
--echo # LOCK TABLES, and ROLLBACK TO SAVEPOINT has no effect on
--echo # them.
--echo #
create function f1() returns varchar(20) return "f1()";
create function f2() returns varchar(20) return "f2()";
create view v1 as select f1() as a;
set @@session.autocommit=0;
lock table v1 read;
select * from v1;
savepoint sv;
select f2();
--echo # --> connection con1
connection con1;
--echo # Sending 'drop function f1'...
send drop function f1;
--echo # --> connection con2
connection con2;
--echo # Waiting for 'drop function f1' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f1';
--source include/wait_condition.inc
--echo # Sending 'drop function f2'...
send drop function f2;
--echo # --> connection default
connection default;
--echo # Waiting for 'drop function f2' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='drop function f2';
--source include/wait_condition.inc
rollback to savepoint sv;
--echo # --> connection con2
connection con2;
--echo # Reaping 'drop function f2'...
reap;
--echo # --> connection default
connection default;
unlock tables;
--echo # --> connection con1
connection con1;
--echo # Reaping 'drop function f1'...
reap;
--echo # --> connection default
connection default;
--error ER_SP_DOES_NOT_EXIST
drop function f1;
--error ER_SP_DOES_NOT_EXIST
drop function f2;
drop view v1;
set @@session.autocommit=default;

--echo # 
--echo # 8) Check the situation when we're preparing or executing a
--echo # prepared statement, and as part of that try to flush the
--echo # session sp cache. However, one of the procedures that
--echo # needs a flush is in use. Verify that there is no infinite
--echo # reprepare loop and no crash. 
--echo #
create function f1() returns int return 1;
delimiter |;
--echo # 
--echo # We just mention p1() in the body of f2() to make
--echo # sure that p1() metadata is validated when validating
--echo # 'select f2()'.
--echo # Recursion is not allowed in stored functions, so 
--echo # an attempt to just invoke p1() from f2() which is in turn
--echo # called from p1() would have given a run-time error.
--echo #
create function f2() returns int
begin
  if @var is null then
    call p1();
  end if;
  return 1;
end|
create procedure p1()
begin
  select f1() into @var;
  execute stmt;
end|
delimiter ;|
--echo # --> connection con2
connection con2;
prepare stmt from "select f2()";
--echo # --> connection default
connection default;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waiting for 'alter function f1 ...' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info like 'alter function f1 comment%';
--source include/wait_condition.inc
--echo # Sending 'call p1()'...
send call p1();
connection default;
--echo # Waiting for 'call p1()' to get blocked on MDL lock on f1...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='select f1() into @var';
--source include/wait_condition.inc
--echo # Let 'alter function f1 ...' go through...
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
reap;
deallocate prepare stmt;
--echo # --> connection default
connection default;
drop function f1;
drop function f2;
drop procedure p1;

--echo # 
--echo # 9) Check the situation when a stored function is invoked
--echo # from a stored procedure, and recursively invokes the
--echo # stored procedure that is in use. But for the second
--echo # invocation, a cache flush is requested. We can't
--echo # flush the procedure that's in use, and are forced
--echo # to use an old version. It is not a violation of
--echo # consistency, since we unroll top-level calls.
--echo # Just verify the code works.
--echo #
create function f1() returns int return 1;
begin;
select f1();
--echo # --> connection con1
connection con1;
--echo # Sending 'alter function f1 ...'...
send alter function f1 comment "comment";
--echo # --> connection con2
connection con2;
--echo # Waiting for 'alter function f1 ...' to get blocked on MDL lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info like 'alter function f1 comment%';
--source include/wait_condition.inc
delimiter |;
--echo # 
--echo # We just mention p1() in the body of f2() to make
--echo # sure that p1() is prelocked for f2().
--echo # Recursion is not allowed in stored functions, so 
--echo # an attempt to just invoke p1() from f2() which is in turn
--echo # called from p1() would have given a run-time error.
--echo #
create function f2() returns int
begin
  if @var is null then
    call p1();
  end if;
  return 1;
end|
create procedure p1()
begin
  select f1() into @var;
  select f2() into @var;
end|
delimiter ;|
--echo # Sending 'call p1()'...
send call p1();
connection default;
--echo # Waiting for 'call p1()' to get blocked on MDL lock on f1...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='Waiting for stored function metadata lock' and
info='select f1() into @var';
--source include/wait_condition.inc
--echo # Let 'alter function f1 ...' go through...
commit;
--echo # --> connection con1
connection con1;
--echo # Reaping 'alter function f1 ...'
reap;
--echo # --> connection con2
connection con2;
--echo # Reaping 'call p1()'...
reap;
--echo # --> connection default
connection default;
drop function f1;
drop function f2;
drop procedure p1;

--echo #
--echo # 10) A select from information_schema.routines now
--echo # flushes the stored routines caches. Test that this
--echo # does not remove from the cache a stored routine
--echo # that is already prelocked.
--echo #
create function f1() returns int return get_lock("30977", 100000);
create function f2() returns int return 2;
delimiter |;
create function f3() returns varchar(255) 
begin
  declare res varchar(255);
  declare c cursor for select routine_name from
    information_schema.routines where routine_name='f1';
  select f1() into @var;
  open c;
  fetch c into res;
  close c;
  select f2() into @var;
  return res;
end|
delimiter ;|
--echo # --> connection con1
connection con1;
select get_lock("30977", 0);
--echo # --> connection default
connection default;
--echo # Sending 'select f3()'...
send select f3();
--echo # --> connection con1 
connection con1;
--echo # Waiting for 'select f3()' to get blocked on the user level lock...
let $wait_condition=select count(*)=1 from information_schema.processlist
where state='User lock' and info='select f1() into @var';
--source include/wait_condition.inc
--echo # Do something to change the cache version.
create function f4() returns int return  4;
drop function f4;
select release_lock("30977");
--echo # --> connection default 
connection default;
--echo # Reaping 'select f3()'...
--echo # Routine 'f2()' should exist and get executed successfully.
reap;
select @var;
drop function f1;
drop function f2;
drop function f3;


--echo # 11) Check the situation when the connection is flushing the
--echo # SP cache which contains a procedure that is being executed.
--echo #
--echo # Function f1() calls p1(). Procedure p1() has a DROP
--echo # VIEW statement, which, we know, invalidates the routines cache.
--echo # During cache flush p1() must not be flushed since it's in
--echo # use.
--echo #
delimiter |;
create function f1() returns int
begin
  call p1();
  return 1;
end|
create procedure p1() 
begin
  create view v1 as select 1;
  drop view v1;
  select f1() into @var;
  set @exec_count=@exec_count+1;
end|
delimiter ;|
set @exec_count=0;
--error ER_SP_RECURSION_LIMIT
call p1();
select @exec_count;
set @@session.max_sp_recursion_depth=5;
set @exec_count=0;
--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
call p1();
select @exec_count;
drop procedure p1;
drop function f1;
set @@session.max_sp_recursion_depth=default;

--echo # --> connection con1
connection con1;
disconnect con1;
--source include/wait_until_disconnected.inc
--echo # --> connection con2
connection con2;
disconnect con2;
--source include/wait_until_disconnected.inc
--echo # --> connection con3
connection con3;
disconnect con3;
--source include/wait_until_disconnected.inc
--echo # --> connection default
connection default;


--echo #
--echo # SHOW CREATE PROCEDURE p1 called from p1, after p1 was altered
--echo #
--echo # We are just covering the existing behaviour with tests. The
--echo # results are not necessarily correct."
--echo #

delimiter |;
CREATE PROCEDURE p1()
BEGIN
  SELECT get_lock("test", 10);
  SHOW CREATE PROCEDURE p1;
END|
delimiter ;|

connect (con2, localhost, root);
connect (con3, localhost, root);

--echo # Connection default
connection default;
SELECT get_lock("test", 10);

--echo # Connection 2
connection con2;
--echo # Will halt before executing SHOW CREATE PROCEDURE p1
--echo # Sending:
--send CALL p1()

--echo # Connection 3
connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist 
  WHERE state='User lock' and info='SELECT get_lock("test", 10)';
--source include/wait_condition.inc
--echo # Alter p1
DROP PROCEDURE p1;
CREATE PROCEDURE p1() BEGIN END;

--echo # Connection default
connection default;
--echo # Resume CALL p1, now with new p1
SELECT release_lock("test");

--echo # Connection 2
connection con2;
--echo # Reaping: CALL p1()
--reap

--echo # Connection 3
connection con3;
disconnect con3;
--source include/wait_until_disconnected.inc
--echo # Connection 2
connection con2;
disconnect con2;
--source include/wait_until_disconnected.inc
--echo # Connection default;
connection default;
DROP PROCEDURE p1;


--echo #
--echo # Bug#57663 Concurrent statement using stored function and DROP DATABASE
--echo #           breaks SBR
--echo #

--disable_warnings
DROP DATABASE IF EXISTS db1;
DROP FUNCTION IF EXISTS f1;
--enable_warnings

connect(con1, localhost, root);
connect(con2, localhost, root);

--echo # Test 1: Check that DROP DATABASE block if a function is used
--echo #         by an active transaction.

--echo # Connection default
connection default;
CREATE DATABASE db1;
CREATE FUNCTION db1.f1() RETURNS INTEGER RETURN 1;
START TRANSACTION;
SELECT db1.f1();

--echo # Connection con1
connection con1;
--echo # Sending:
--send DROP DATABASE db1

--echo # Connection default
connection default;
--echo # Waiting for DROP DATABASE to be blocked by the lock on f1()
let $wait_condition= SELECT COUNT(*)= 1 FROM information_schema.processlist
  WHERE state= 'Waiting for stored function metadata lock'
  AND info='DROP DATABASE db1';
--source include/wait_condition.inc
COMMIT;

--echo # Connection con1
connection con1;
--echo # Reaping: DROP DATABASE db1
--reap

--echo # Test 2: Check that DROP DATABASE blocks if a procedure is
--echo #         used by an active transaction.

--echo # Connection default
connection default;
CREATE DATABASE db1;
CREATE PROCEDURE db1.p1() BEGIN END;
delimiter |;
CREATE FUNCTION f1() RETURNS INTEGER
BEGIN
  CALL db1.p1();
  RETURN 1;
END|
delimiter ;|
START TRANSACTION;
SELECT f1();

--echo # Connection con1
connection con1;
--echo # Sending:
--send DROP DATABASE db1

--echo # Connection default
connection default;
--echo # Waiting for DROP DATABASE to be blocked by the lock on p1()
let $wait_condition= SELECT COUNT(*)= 1 FROM information_schema.processlist
  WHERE state= 'Waiting for stored procedure metadata lock'
  AND info='DROP DATABASE db1';
--source include/wait_condition.inc
COMMIT;

--echo # Connection con1
connection con1;
--echo # Reaping: DROP DATABASE db1
--reap

--echo # Test 3: Check that DROP DATABASE is not selected as a victim if a
--echo #         deadlock is discovered with DML statements.

--echo # Connection default
connection default;
CREATE DATABASE db1;
CREATE TABLE db1.t1 (a INT);
CREATE FUNCTION db1.f1() RETURNS INTEGER RETURN 1;
START TRANSACTION;
# DROP DATABASE will lock tables (t1) before functions (f1)
SELECT db1.f1();

--echo # Connection con1
connection con1;
--echo # Sending:
--send DROP DATABASE db1

--echo # Connection default
connection default;
--echo # Waiting for DROP DATABASE to be blocked by the lock on f1()
let $wait_condition= SELECT COUNT(*)= 1 FROM information_schema.processlist
  WHERE state= 'Waiting for stored function metadata lock'
  AND info='DROP DATABASE db1';
--source include/wait_condition.inc
--error ER_LOCK_DEADLOCK
SELECT * FROM db1.t1;
COMMIT;

--echo # Connection con1
connection con1;
--echo # Reaping: DROP DATABASE db1
--reap

--echo # Test 4: Check that active DROP DATABASE blocks stored routine DDL.

--echo # Connection default
connection default;
CREATE DATABASE db1;
CREATE FUNCTION db1.f1() RETURNS INTEGER RETURN 1;
CREATE FUNCTION db1.f2() RETURNS INTEGER RETURN 2;
START TRANSACTION;
SELECT db1.f2();

--echo # Connection con1
connection con1;
--echo # Sending:
--send DROP DATABASE db1

--echo # Connection con2
connection con2;
--echo # Waiting for DROP DATABASE to be blocked by the lock on f2()
let $wait_condition= SELECT COUNT(*)= 1 FROM information_schema.processlist
  WHERE state= 'Waiting for stored function metadata lock'
  AND info='DROP DATABASE db1';
--source include/wait_condition.inc
--echo # Sending:
--send ALTER FUNCTION db1.f1 COMMENT "test"

--echo # Connection default
connection default;
--echo # Waiting for ALTER FUNCTION to be blocked by the schema lock on db1
let $wait_condition= SELECT COUNT(*)= 1 FROM information_schema.processlist
  WHERE state= 'Waiting for schema metadata lock'
  AND info='ALTER FUNCTION db1.f1 COMMENT "test"';
--source include/wait_condition.inc
COMMIT;

--echo # Connection con1
connection con1;
--echo # Reaping: DROP DATABASE db1
--reap
disconnect con1;
--source include/wait_until_disconnected.inc

--echo # Connection con2
connection con2;
--echo # Reaping: ALTER FUNCTION f1 COMMENT 'test'
--error ER_SP_DOES_NOT_EXIST
--reap
disconnect con2;
--source include/wait_until_disconnected.inc

--echo # Connection default
connection default;
DROP FUNCTION f1;


--echo #
--echo # End of 5.5 tests
--echo #