/ Hex Artifact Content
Login

Artifact 6504bbf348b2d6dfade64a064f1050fd617e8706:


0000: 23 20 32 30 31 30 20 41 70 72 69 6c 20 31 33 0a  # 2010 April 13.
0010: 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20 64  #.# The author d
0020: 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
0030: 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
0040: 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
0050: 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e  e of.# a legal n
0060: 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
0070: 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20   blessing:.#.#  
0080: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
0090: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
00a0: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  #    May you fin
00b0: 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
00c0: 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
00d0: 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23  orgive others..#
00e0: 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
00f0: 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
0100: 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
0110: 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a   you give..#.#**
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 0a 23 20 54 68 69 73 20 66 69 6c  *****.# This fil
0170: 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 65 67  e implements reg
0180: 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 66 6f  ression tests fo
0190: 72 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  r SQLite library
01a0: 2e 20 20 54 68 65 0a 23 20 66 6f 63 75 73 20 6f  .  The.# focus o
01b0: 66 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74  f this file is t
01c0: 65 73 74 69 6e 67 20 74 68 65 20 6f 70 65 72 61  esting the opera
01d0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72  tion of the libr
01e0: 61 72 79 20 69 6e 0a 23 20 22 50 52 41 47 4d 41  ary in.# "PRAGMA
01f0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41   journal_mode=WA
0200: 4c 22 20 6d 6f 64 65 2e 0a 23 0a 0a 73 65 74 20  L" mode..#..set 
0210: 74 65 73 74 64 69 72 20 5b 66 69 6c 65 20 64 69  testdir [file di
0220: 72 6e 61 6d 65 20 24 61 72 67 76 30 5d 0a 73 6f  rname $argv0].so
0230: 75 72 63 65 20 24 74 65 73 74 64 69 72 2f 74 65  urce $testdir/te
0240: 73 74 65 72 2e 74 63 6c 0a 73 6f 75 72 63 65 20  ster.tcl.source 
0250: 24 74 65 73 74 64 69 72 2f 6c 6f 63 6b 5f 63 6f  $testdir/lock_co
0260: 6d 6d 6f 6e 2e 74 63 6c 0a 73 6f 75 72 63 65 20  mmon.tcl.source 
0270: 24 74 65 73 74 64 69 72 2f 77 61 6c 5f 63 6f 6d  $testdir/wal_com
0280: 6d 6f 6e 2e 74 63 6c 0a 73 6f 75 72 63 65 20 24  mon.tcl.source $
0290: 74 65 73 74 64 69 72 2f 6d 61 6c 6c 6f 63 5f 63  testdir/malloc_c
02a0: 6f 6d 6d 6f 6e 2e 74 63 6c 0a 69 66 63 61 70 61  ommon.tcl.ifcapa
02b0: 62 6c 65 20 21 77 61 6c 20 7b 66 69 6e 69 73 68  ble !wal {finish
02c0: 5f 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d  _test ; return }
02d0: 0a 0a 73 65 74 20 61 5f 73 74 72 69 6e 67 5f 63  ..set a_string_c
02e0: 6f 75 6e 74 65 72 20 31 0a 70 72 6f 63 20 61 5f  ounter 1.proc a_
02f0: 73 74 72 69 6e 67 20 7b 6e 7d 20 7b 0a 20 20 67  string {n} {.  g
0300: 6c 6f 62 61 6c 20 61 5f 73 74 72 69 6e 67 5f 63  lobal a_string_c
0310: 6f 75 6e 74 65 72 0a 20 20 69 6e 63 72 20 61 5f  ounter.  incr a_
0320: 73 74 72 69 6e 67 5f 63 6f 75 6e 74 65 72 0a 20  string_counter. 
0330: 20 73 74 72 69 6e 67 20 72 61 6e 67 65 20 5b 73   string range [s
0340: 74 72 69 6e 67 20 72 65 70 65 61 74 20 22 24 7b  tring repeat "${
0350: 61 5f 73 74 72 69 6e 67 5f 63 6f 75 6e 74 65 72  a_string_counter
0360: 7d 2e 22 20 24 6e 5d 20 31 20 24 6e 0a 7d 0a 64  }." $n] 1 $n.}.d
0370: 62 20 66 75 6e 63 20 61 5f 73 74 72 69 6e 67 20  b func a_string 
0380: 61 5f 73 74 72 69 6e 67 0a 0a 23 2d 2d 2d 2d 2d  a_string..#-----
0390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03d0: 2d 2d 2d 2d 0a 23 20 57 68 65 6e 20 61 20 72 6f  ----.# When a ro
03e0: 6c 6c 62 61 63 6b 20 6f 72 20 73 61 76 65 70 6f  llback or savepo
03f0: 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63  int rollback occ
0400: 75 72 73 2c 20 74 68 65 20 63 6c 69 65 6e 74 20  urs, the client 
0410: 6d 61 79 20 72 65 6d 6f 76 65 0a 23 20 65 6c 65  may remove.# ele
0420: 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6e 65 20 6f  ments from one o
0430: 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
0440: 73 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e 64  s in the wal-ind
0450: 65 78 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0a 23  ex. This block.#
0460: 20 6f 66 20 74 65 73 74 20 63 61 73 65 73 20 74   of test cases t
0470: 65 73 74 73 20 74 68 61 74 20 6e 6f 74 68 69 6e  ests that nothin
0480: 67 20 61 70 70 65 61 72 73 20 74 6f 20 67 6f 20  g appears to go 
0490: 77 72 6f 6e 67 20 77 68 65 6e 20 74 68 69 73 20  wrong when this 
04a0: 69 73 0a 23 20 64 6f 6e 65 2e 0a 23 0a 64 6f 5f  is.# done..#.do_
04b0: 74 65 73 74 20 77 61 6c 33 2d 31 2e 30 20 7b 0a  test wal3-1.0 {.
04c0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
04d0: 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a  PRAGMA cache_siz
04e0: 65 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 50 52  e = 2000;.    PR
04f0: 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65 20 3d  AGMA page_size =
0500: 20 31 30 32 34 3b 0a 20 20 20 20 50 52 41 47 4d   1024;.    PRAGM
0510: 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20  A auto_vacuum = 
0520: 6f 66 66 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  off;.    PRAGMA 
0530: 73 79 6e 63 68 72 6f 6e 6f 75 73 20 3d 20 6e 6f  synchronous = no
0540: 72 6d 61 6c 3b 0a 20 20 20 20 50 52 41 47 4d 41  rmal;.    PRAGMA
0550: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20   journal_mode = 
0560: 57 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  WAL;.    PRAGMA 
0570: 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69  wal_autocheckpoi
0580: 6e 74 20 3d 20 30 3b 0a 20 20 20 20 42 45 47 49  nt = 0;.    BEGI
0590: 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20  N;.      CREATE 
05a0: 54 41 42 4c 45 20 74 31 28 78 29 3b 0a 20 20 20  TABLE t1(x);.   
05b0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
05c0: 31 20 56 41 4c 55 45 53 28 20 61 5f 73 74 72 69  1 VALUES( a_stri
05d0: 6e 67 28 38 30 30 29 20 29 3b 20 20 20 20 20 20  ng(800) );      
05e0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
05f0: 20 20 31 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53    1 */.      INS
0600: 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45  ERT INTO t1 SELE
0610: 43 54 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29  CT a_string(800)
0620: 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20   FROM t1;       
0630: 20 20 20 20 20 20 2f 2a 20 20 20 20 32 20 2a 2f        /*    2 */
0640: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
0650: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73  TO t1 SELECT a_s
0660: 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20  tring(800) FROM 
0670: 74 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  t1;             
0680: 2f 2a 20 20 20 20 34 20 2a 2f 0a 20 20 20 20 20  /*    4 */.     
0690: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
06a0: 53 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28  SELECT a_string(
06b0: 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20  800) FROM t1;   
06c0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20            /*    
06d0: 38 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52  8 */.      INSER
06e0: 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54  T INTO t1 SELECT
06f0: 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46   a_string(800) F
0700: 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20 20 20  ROM t1;         
0710: 20 20 20 20 2f 2a 20 20 20 31 36 20 2a 2f 0a 20      /*   16 */. 
0720: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
0730: 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73 74 72   t1 SELECT a_str
0740: 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20 74 31  ing(800) FROM t1
0750: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
0760: 20 20 20 33 32 20 2a 2f 0a 20 20 20 20 20 20 49     32 */.      I
0770: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45  NSERT INTO t1 SE
0780: 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28 38 30  LECT a_string(80
0790: 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 20  0) FROM t1;     
07a0: 20 20 20 20 20 20 20 20 2f 2a 20 20 20 36 34 20          /*   64 
07b0: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
07c0: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61  INTO t1 SELECT a
07d0: 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f  _string(800) FRO
07e0: 4d 20 74 31 3b 20 20 20 20 20 20 20 20 20 20 20  M t1;           
07f0: 20 20 2f 2a 20 20 31 32 38 2a 2f 0a 20 20 20 20    /*  128*/.    
0800: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
0810: 20 53 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67   SELECT a_string
0820: 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20  (800) FROM t1;  
0830: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 32             /*  2
0840: 35 36 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  56 */.      INSE
0850: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
0860: 54 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20  T a_string(800) 
0870: 46 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20 20  FROM t1;        
0880: 20 20 20 20 20 2f 2a 20 20 35 31 32 20 2a 2f 0a       /*  512 */.
0890: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
08a0: 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73 74  O t1 SELECT a_st
08b0: 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20 74  ring(800) FROM t
08c0: 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1;             /
08d0: 2a 20 31 30 32 34 20 2a 2f 0a 20 20 20 20 20 20  * 1024 */.      
08e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53  INSERT INTO t1 S
08f0: 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28 38  ELECT a_string(8
0900: 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20  00) FROM t1;    
0910: 20 20 20 20 20 20 20 20 20 2f 2a 20 32 30 34 38           /* 2048
0920: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
0930: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
0940: 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46 52  a_string(800) FR
0950: 4f 4d 20 74 31 20 4c 49 4d 49 54 20 31 39 37 30  OM t1 LIMIT 1970
0960: 3b 20 20 2f 2a 20 34 30 31 38 20 2a 2f 0a 20 20  ;  /* 4018 */.  
0970: 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 50 52    COMMIT;.    PR
0980: 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 20  AGMA cache_size 
0990: 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 77 61 6c 5f  = 10;.  }.  wal_
09a0: 66 72 61 6d 65 5f 63 6f 75 6e 74 20 74 65 73 74  frame_count test
09b0: 2e 64 62 2d 77 61 6c 20 31 30 32 34 0a 7d 20 34  .db-wal 1024.} 4
09c0: 30 35 36 0a 0a 66 6f 72 20 7b 73 65 74 20 69 20  056..for {set i 
09d0: 31 7d 20 7b 24 69 20 3c 20 35 30 7d 20 7b 69 6e  1} {$i < 50} {in
09e0: 63 72 20 69 7d 20 7b 0a 0a 20 20 64 6f 5f 74 65  cr i} {..  do_te
09f0: 73 74 20 77 61 6c 33 2d 31 2e 24 69 2e 31 20 7b  st wal3-1.$i.1 {
0a00: 0a 20 20 20 20 73 65 74 20 73 74 72 20 5b 61 5f  .    set str [a_
0a10: 73 74 72 69 6e 67 20 38 30 30 5d 0a 20 20 20 20  string 800].    
0a20: 65 78 65 63 73 71 6c 20 7b 20 55 50 44 41 54 45  execsql { UPDATE
0a30: 20 74 31 20 53 45 54 20 78 20 3d 20 24 73 74 72   t1 SET x = $str
0a40: 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 24   WHERE rowid = $
0a50: 69 20 7d 0a 20 20 20 20 6c 61 70 70 65 6e 64 20  i }.    lappend 
0a60: 4c 20 5b 77 61 6c 5f 66 72 61 6d 65 5f 63 6f 75  L [wal_frame_cou
0a70: 6e 74 20 74 65 73 74 2e 64 62 2d 77 61 6c 20 31  nt test.db-wal 1
0a80: 30 32 34 5d 0a 20 20 20 20 65 78 65 63 73 71 6c  024].    execsql
0a90: 20 7b 0a 20 20 20 20 20 20 42 45 47 49 4e 3b 0a   {.      BEGIN;.
0aa0: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
0ab0: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f  NTO t1 SELECT a_
0ac0: 73 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d  string(800) FROM
0ad0: 20 74 31 20 4c 49 4d 49 54 20 31 30 30 3b 0a 20   t1 LIMIT 100;. 
0ae0: 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20       ROLLBACK;. 
0af0: 20 20 20 20 20 50 52 41 47 4d 41 20 69 6e 74 65       PRAGMA inte
0b00: 67 72 69 74 79 5f 63 68 65 63 6b 3b 0a 20 20 20  grity_check;.   
0b10: 20 7d 0a 20 20 7d 20 7b 6f 6b 7d 0a 0a 20 20 23   }.  } {ok}..  #
0b20: 20 43 68 65 63 6b 20 74 68 61 74 20 65 76 65 72   Check that ever
0b30: 79 74 68 69 6e 67 20 6c 6f 6f 6b 73 20 4f 4b 20  ything looks OK 
0b40: 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20 6f  from the point o
0b50: 66 20 76 69 65 77 20 6f 66 20 61 6e 20 0a 20 20  f view of an .  
0b60: 23 20 65 78 74 65 72 6e 61 6c 20 63 6f 6e 6e 65  # external conne
0b70: 63 74 69 6f 6e 2e 0a 20 20 23 0a 20 20 73 71 6c  ction..  #.  sql
0b80: 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64 62  ite3 db2 test.db
0b90: 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .  do_test wal3-
0ba0: 31 2e 24 69 2e 32 20 7b 0a 20 20 20 20 65 78 65  1.$i.2 {.    exe
0bb0: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f  csql { SELECT co
0bc0: 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 20 7d  unt(*) FROM t1 }
0bd0: 20 64 62 32 0a 20 20 7d 20 34 30 31 38 0a 20 20   db2.  } 4018.  
0be0: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 31 2e 24  do_test wal3-1.$
0bf0: 69 2e 33 20 7b 0a 20 20 20 20 65 78 65 63 73 71  i.3 {.    execsq
0c00: 6c 20 7b 20 53 45 4c 45 43 54 20 78 20 46 52 4f  l { SELECT x FRO
0c10: 4d 20 74 31 20 57 48 45 52 45 20 72 6f 77 69 64  M t1 WHERE rowid
0c20: 20 3d 20 24 69 20 7d 0a 20 20 7d 20 24 73 74 72   = $i }.  } $str
0c30: 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .  do_test wal3-
0c40: 31 2e 24 69 2e 34 20 7b 0a 20 20 20 20 65 78 65  1.$i.4 {.    exe
0c50: 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 69 6e  csql { PRAGMA in
0c60: 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 7d 20  tegrity_check } 
0c70: 64 62 32 0a 20 20 7d 20 7b 6f 6b 7d 0a 20 20 64  db2.  } {ok}.  d
0c80: 62 32 20 63 6c 6f 73 65 0a 20 20 0a 20 20 23 20  b2 close.  .  # 
0c90: 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 66  Check that the f
0ca0: 69 6c 65 2d 73 79 73 74 65 6d 20 69 6e 20 69 74  ile-system in it
0cb0: 73 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  s current state 
0cc0: 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 65 64  can be recovered
0cd0: 2e 0a 20 20 23 20 0a 20 20 66 6f 72 63 65 63 6f  ..  # .  forceco
0ce0: 70 79 20 74 65 73 74 2e 64 62 20 74 65 73 74 32  py test.db test2
0cf0: 2e 64 62 0a 20 20 66 6f 72 63 65 63 6f 70 79 20  .db.  forcecopy 
0d00: 74 65 73 74 2e 64 62 2d 77 61 6c 20 74 65 73 74  test.db-wal test
0d10: 32 2e 64 62 2d 77 61 6c 0a 20 20 66 6f 72 63 65  2.db-wal.  force
0d20: 64 65 6c 65 74 65 20 74 65 73 74 32 2e 64 62 2d  delete test2.db-
0d30: 6a 6f 75 72 6e 61 6c 0a 20 20 73 71 6c 69 74 65  journal.  sqlite
0d40: 33 20 64 62 32 20 74 65 73 74 32 2e 64 62 0a 20  3 db2 test2.db. 
0d50: 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 31 2e   do_test wal3-1.
0d60: 24 69 2e 35 20 7b 0a 20 20 20 20 65 78 65 63 73  $i.5 {.    execs
0d70: 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e  ql { SELECT coun
0d80: 74 28 2a 29 20 46 52 4f 4d 20 74 31 20 7d 20 64  t(*) FROM t1 } d
0d90: 62 32 0a 20 20 7d 20 34 30 31 38 0a 20 20 64 6f  b2.  } 4018.  do
0da0: 5f 74 65 73 74 20 77 61 6c 33 2d 31 2e 24 69 2e  _test wal3-1.$i.
0db0: 36 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  6 {.    execsql 
0dc0: 7b 20 53 45 4c 45 43 54 20 78 20 46 52 4f 4d 20  { SELECT x FROM 
0dd0: 74 31 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  t1 WHERE rowid =
0de0: 20 24 69 20 7d 0a 20 20 7d 20 24 73 74 72 0a 20   $i }.  } $str. 
0df0: 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 31 2e   do_test wal3-1.
0e00: 24 69 2e 37 20 7b 0a 20 20 20 20 65 78 65 63 73  $i.7 {.    execs
0e10: 71 6c 20 7b 20 50 52 41 47 4d 41 20 69 6e 74 65  ql { PRAGMA inte
0e20: 67 72 69 74 79 5f 63 68 65 63 6b 20 7d 20 64 62  grity_check } db
0e30: 32 0a 20 20 7d 20 7b 6f 6b 7d 0a 20 20 64 62 32  2.  } {ok}.  db2
0e40: 20 63 6c 6f 73 65 0a 7d 0a 0a 70 72 6f 63 20 62   close.}..proc b
0e50: 79 74 65 5f 69 73 5f 7a 65 72 6f 20 7b 66 69 6c  yte_is_zero {fil
0e60: 65 20 6f 66 66 73 65 74 7d 20 7b 0a 20 20 69 66  e offset} {.  if
0e70: 20 7b 5b 66 69 6c 65 20 73 69 7a 65 20 74 65 73   {[file size tes
0e80: 74 2e 64 62 5d 20 3c 3d 20 24 6f 66 66 73 65 74  t.db] <= $offset
0e90: 7d 20 7b 20 72 65 74 75 72 6e 20 31 20 7d 0a 20  } { return 1 }. 
0ea0: 20 65 78 70 72 20 7b 20 5b 68 65 78 69 6f 5f 72   expr { [hexio_r
0eb0: 65 61 64 20 24 66 69 6c 65 20 24 6f 66 66 73 65  ead $file $offse
0ec0: 74 20 31 5d 20 3d 3d 20 22 30 30 22 20 7d 0a 7d  t 1] == "00" }.}
0ed0: 0a 0a 64 6f 5f 6d 75 6c 74 69 63 6c 69 65 6e 74  ..do_multiclient
0ee0: 5f 74 65 73 74 20 69 20 7b 0a 0a 20 20 73 65 74  _test i {..  set
0ef0: 20 74 65 73 74 6e 61 6d 65 28 31 29 20 6d 75 6c   testname(1) mul
0f00: 74 69 70 72 6f 63 0a 20 20 73 65 74 20 74 65 73  tiproc.  set tes
0f10: 74 6e 61 6d 65 28 32 29 20 73 69 6e 67 6c 65 70  tname(2) singlep
0f20: 72 6f 63 0a 20 20 73 65 74 20 74 6e 20 24 74 65  roc.  set tn $te
0f30: 73 74 6e 61 6d 65 28 24 69 29 0a 0a 20 20 64 6f  stname($i)..  do
0f40: 5f 74 65 73 74 20 77 61 6c 33 2d 32 2e 24 74 6e  _test wal3-2.$tn
0f50: 2e 31 20 7b 0a 20 20 20 20 73 71 6c 31 20 7b 20  .1 {.    sql1 { 
0f60: 0a 20 20 20 20 20 20 50 52 41 47 4d 41 20 70 61  .      PRAGMA pa
0f70: 67 65 5f 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a  ge_size = 1024;.
0f80: 20 20 20 20 20 20 50 52 41 47 4d 41 20 6a 6f 75        PRAGMA jou
0f90: 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b  rnal_mode = WAL;
0fa0: 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 31 20  .    }.    sql1 
0fb0: 7b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20 54  {.      CREATE T
0fc0: 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20  ABLE t1(a, b);. 
0fd0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
0fe0: 20 74 31 20 56 41 4c 55 45 53 28 31 2c 20 27 6f   t1 VALUES(1, 'o
0ff0: 6e 65 27 29 3b 0a 20 20 20 20 20 20 42 45 47 49  ne');.      BEGI
1000: 4e 3b 0a 20 20 20 20 20 20 20 20 53 45 4c 45 43  N;.        SELEC
1010: 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20  T * FROM t1;.   
1020: 20 7d 0a 20 20 7d 20 7b 31 20 6f 6e 65 7d 0a 20   }.  } {1 one}. 
1030: 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 32 2e   do_test wal3-2.
1040: 24 74 6e 2e 32 20 7b 0a 20 20 20 20 73 71 6c 32  $tn.2 {.    sql2
1050: 20 7b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20   {.      CREATE 
1060: 54 41 42 4c 45 20 74 32 28 61 2c 20 62 29 3b 0a  TABLE t2(a, b);.
1070: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
1080: 4f 20 74 32 20 56 41 4c 55 45 53 28 32 2c 20 27  O t2 VALUES(2, '
1090: 74 77 6f 27 29 3b 0a 20 20 20 20 20 20 42 45 47  two');.      BEG
10a0: 49 4e 3b 0a 20 20 20 20 20 20 20 20 53 45 4c 45  IN;.        SELE
10b0: 43 54 20 2a 20 46 52 4f 4d 20 74 32 3b 0a 20 20  CT * FROM t2;.  
10c0: 20 20 7d 0a 20 20 7d 20 7b 32 20 74 77 6f 7d 0a    }.  } {2 two}.
10d0: 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 32    do_test wal3-2
10e0: 2e 24 74 6e 2e 33 20 7b 0a 20 20 20 20 73 71 6c  .$tn.3 {.    sql
10f0: 33 20 7b 0a 20 20 20 20 20 20 43 52 45 41 54 45  3 {.      CREATE
1100: 20 54 41 42 4c 45 20 74 33 28 61 2c 20 62 29 3b   TABLE t3(a, b);
1110: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
1120: 54 4f 20 74 33 20 56 41 4c 55 45 53 28 33 2c 20  TO t3 VALUES(3, 
1130: 27 74 68 72 65 65 27 29 3b 0a 20 20 20 20 20 20  'three');.      
1140: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 20 20 53  BEGIN;.        S
1150: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 33 3b  ELECT * FROM t3;
1160: 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 33 20 74 68  .    }.  } {3 th
1170: 72 65 65 7d 0a 0a 20 20 23 20 54 72 79 20 74 6f  ree}..  # Try to
1180: 20 63 68 65 63 6b 70 6f 69 6e 74 20 74 68 65 20   checkpoint the 
1190: 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 5b  database using [
11a0: 64 62 5d 2e 20 49 74 20 73 68 6f 75 6c 64 20 62  db]. It should b
11b0: 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20  e possible to.  
11c0: 23 20 63 68 65 63 6b 70 6f 69 6e 74 20 65 76 65  # checkpoint eve
11d0: 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 74  rything except t
11e0: 68 65 20 74 61 62 6c 65 20 61 64 64 65 64 20 62  he table added b
11f0: 79 20 5b 64 62 33 5d 20 28 63 68 65 63 6b 70 6f  y [db3] (checkpo
1200: 69 6e 74 69 6e 67 0a 20 20 23 20 74 68 65 73 65  inting.  # these
1210: 20 66 72 61 6d 65 73 20 77 6f 75 6c 64 20 63 6c   frames would cl
1220: 6f 62 62 65 72 20 74 68 65 20 73 6e 61 70 73 68  obber the snapsh
1230: 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69  ot currently bei
1240: 6e 67 20 75 73 65 64 20 62 79 20 5b 64 62 32 5d  ng used by [db2]
1250: 29 2e 0a 20 20 23 0a 20 20 23 20 41 66 74 65 72  )..  #.  # After
1260: 20 5b 64 62 32 5d 20 68 61 73 20 63 6f 6d 6d 69   [db2] has commi
1270: 74 74 65 64 2c 20 61 20 63 68 65 63 6b 70 6f 69  tted, a checkpoi
1280: 6e 74 20 63 61 6e 20 63 6f 70 79 20 74 68 65 20  nt can copy the 
1290: 65 6e 74 69 72 65 20 6c 6f 67 20 74 6f 20 74 68  entire log to th
12a0: 65 0a 20 20 23 20 64 61 74 61 62 61 73 65 20 66  e.  # database f
12b0: 69 6c 65 2e 20 43 68 65 63 6b 70 6f 69 6e 74 69  ile. Checkpointi
12c0: 6e 67 20 61 66 74 65 72 20 5b 64 62 33 5d 20 68  ng after [db3] h
12d0: 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 69 73 20  as committed is 
12e0: 74 68 65 72 65 66 6f 72 65 20 61 0a 20 20 23 20  therefore a.  # 
12f0: 6e 6f 2d 6f 70 2c 20 61 73 20 74 68 65 20 65 6e  no-op, as the en
1300: 74 69 72 65 20 6c 6f 67 20 68 61 73 20 61 6c 72  tire log has alr
1310: 65 61 64 79 20 62 65 65 6e 20 62 61 63 6b 66 69  eady been backfi
1320: 6c 6c 65 64 2e 0a 20 20 23 0a 20 20 64 6f 5f 74  lled..  #.  do_t
1330: 65 73 74 20 77 61 6c 33 2d 32 2e 24 74 6e 2e 34  est wal3-2.$tn.4
1340: 20 7b 0a 20 20 20 20 73 71 6c 31 20 7b 0a 20 20   {.    sql1 {.  
1350: 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20      COMMIT;.    
1360: 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68 65    PRAGMA wal_che
1370: 63 6b 70 6f 69 6e 74 3b 0a 20 20 20 20 7d 0a 20  ckpoint;.    }. 
1380: 20 20 20 62 79 74 65 5f 69 73 5f 7a 65 72 6f 20     byte_is_zero 
1390: 74 65 73 74 2e 64 62 20 5b 65 78 70 72 20 24 41  test.db [expr $A
13a0: 55 54 4f 56 41 43 55 55 4d 20 3f 20 34 2a 31 30  UTOVACUUM ? 4*10
13b0: 32 34 20 3a 20 33 2a 31 30 32 34 5d 0a 20 20 7d  24 : 3*1024].  }
13c0: 20 7b 31 7d 0a 20 20 64 6f 5f 74 65 73 74 20 77   {1}.  do_test w
13d0: 61 6c 33 2d 32 2e 24 74 6e 2e 35 20 7b 0a 20 20  al3-2.$tn.5 {.  
13e0: 20 20 73 71 6c 32 20 7b 0a 20 20 20 20 20 20 43    sql2 {.      C
13f0: 4f 4d 4d 49 54 3b 0a 20 20 20 20 20 20 50 52 41  OMMIT;.      PRA
1400: 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69  GMA wal_checkpoi
1410: 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 69  nt;.    }.    li
1420: 73 74 20 5b 62 79 74 65 5f 69 73 5f 7a 65 72 6f  st [byte_is_zero
1430: 20 74 65 73 74 2e 64 62 20 5b 65 78 70 72 20 24   test.db [expr $
1440: 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 34 2a 31  AUTOVACUUM ? 4*1
1450: 30 32 34 20 3a 20 33 2a 31 30 32 34 5d 5d 20 20  024 : 3*1024]]  
1460: 20 5c 0a 20 20 20 20 20 20 20 20 20 5b 62 79 74   \.         [byt
1470: 65 5f 69 73 5f 7a 65 72 6f 20 74 65 73 74 2e 64  e_is_zero test.d
1480: 62 20 5b 65 78 70 72 20 24 41 55 54 4f 56 41 43  b [expr $AUTOVAC
1490: 55 55 4d 20 3f 20 35 2a 31 30 32 34 20 3a 20 34  UUM ? 5*1024 : 4
14a0: 2a 31 30 32 34 5d 5d 0a 20 20 7d 20 7b 30 20 31  *1024]].  } {0 1
14b0: 7d 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33  }.  do_test wal3
14c0: 2d 32 2e 24 74 6e 2e 36 20 7b 0a 20 20 20 20 73  -2.$tn.6 {.    s
14d0: 71 6c 33 20 7b 0a 20 20 20 20 20 20 43 4f 4d 4d  ql3 {.      COMM
14e0: 49 54 3b 0a 20 20 20 20 20 20 50 52 41 47 4d 41  IT;.      PRAGMA
14f0: 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 3b   wal_checkpoint;
1500: 0a 20 20 20 20 7d 0a 20 20 20 20 6c 69 73 74 20  .    }.    list 
1510: 5b 62 79 74 65 5f 69 73 5f 7a 65 72 6f 20 74 65  [byte_is_zero te
1520: 73 74 2e 64 62 20 5b 65 78 70 72 20 24 41 55 54  st.db [expr $AUT
1530: 4f 56 41 43 55 55 4d 20 3f 20 34 2a 31 30 32 34  OVACUUM ? 4*1024
1540: 20 3a 20 33 2a 31 30 32 34 5d 5d 20 20 20 5c 0a   : 3*1024]]   \.
1550: 20 20 20 20 20 20 20 20 20 5b 62 79 74 65 5f 69           [byte_i
1560: 73 5f 7a 65 72 6f 20 74 65 73 74 2e 64 62 20 5b  s_zero test.db [
1570: 65 78 70 72 20 24 41 55 54 4f 56 41 43 55 55 4d  expr $AUTOVACUUM
1580: 20 3f 20 35 2a 31 30 32 34 20 3a 20 34 2a 31 30   ? 5*1024 : 4*10
1590: 32 34 5d 5d 0a 20 20 7d 20 7b 30 20 31 7d 0a 7d  24]].  } {0 1}.}
15a0: 0a 63 61 74 63 68 20 7b 64 62 20 63 6c 6f 73 65  .catch {db close
15b0: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
15c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
15f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1600: 54 65 73 74 20 74 68 61 74 20 74 68 61 74 20 66  Test that that f
1610: 6f 72 20 74 68 65 20 73 69 6d 70 6c 65 20 74 65  or the simple te
1620: 73 74 3a 0a 23 0a 23 20 20 20 43 52 45 41 54 45  st:.#.#   CREATE
1630: 20 54 41 42 4c 45 20 78 28 79 29 3b 0a 23 20 20   TABLE x(y);.#  
1640: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 20 56   INSERT INTO x V
1650: 41 4c 55 45 53 28 27 7a 27 29 3b 0a 23 20 20 20  ALUES('z');.#   
1660: 50 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b  PRAGMA wal_check
1670: 70 6f 69 6e 74 3b 0a 23 0a 23 20 69 6e 20 57 41  point;.#.# in WA
1680: 4c 20 6d 6f 64 65 20 74 68 65 20 78 53 79 6e 63  L mode the xSync
1690: 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b   method is invok
16a0: 65 64 20 61 73 20 65 78 70 65 63 74 65 64 20 66  ed as expected f
16b0: 6f 72 20 65 61 63 68 20 6f 66 0a 23 20 73 79 6e  or each of.# syn
16c0: 63 68 72 6f 6e 6f 75 73 3d 6f 66 66 2c 20 73 79  chronous=off, sy
16d0: 6e 63 68 72 6f 6e 6f 75 73 3d 6e 6f 72 6d 61 6c  nchronous=normal
16e0: 20 61 6e 64 20 73 79 6e 63 68 72 6f 6e 6f 75 73   and synchronous
16f0: 3d 66 75 6c 6c 2e 0a 23 0a 66 6f 72 65 61 63 68  =full..#.foreach
1700: 20 7b 74 6e 20 73 79 6e 63 6d 6f 64 65 20 73 79   {tn syncmode sy
1710: 6e 63 63 6f 75 6e 74 7d 20 7b 0a 20 20 31 20 6f  nccount} {.  1 o
1720: 66 66 20 20 20 20 20 0a 20 20 20 20 7b 7d 0a 20  ff     .    {}. 
1730: 20 32 20 6e 6f 72 6d 61 6c 20 20 0a 20 20 20 20   2 normal  .    
1740: 7b 74 65 73 74 2e 64 62 2d 77 61 6c 20 6e 6f 72  {test.db-wal nor
1750: 6d 61 6c 20 74 65 73 74 2e 64 62 20 6e 6f 72 6d  mal test.db norm
1760: 61 6c 7d 0a 20 20 33 20 66 75 6c 6c 20 20 20 20  al}.  3 full    
1770: 0a 20 20 20 20 7b 74 65 73 74 2e 64 62 2d 77 61  .    {test.db-wa
1780: 6c 20 6e 6f 72 6d 61 6c 20 74 65 73 74 2e 64 62  l normal test.db
1790: 2d 77 61 6c 20 6e 6f 72 6d 61 6c 20 74 65 73 74  -wal normal test
17a0: 2e 64 62 2d 77 61 6c 20 6e 6f 72 6d 61 6c 20 74  .db-wal normal t
17b0: 65 73 74 2e 64 62 20 6e 6f 72 6d 61 6c 7d 0a 7d  est.db normal}.}
17c0: 20 7b 0a 0a 20 20 70 72 6f 63 20 73 79 6e 63 5f   {..  proc sync_
17d0: 63 6f 75 6e 74 65 72 20 7b 61 72 67 73 7d 20 7b  counter {args} {
17e0: 20 0a 20 20 20 20 66 6f 72 65 61 63 68 20 7b 6d   .    foreach {m
17f0: 65 74 68 6f 64 20 66 69 6c 65 6e 61 6d 65 20 69  ethod filename i
1800: 64 20 66 6c 61 67 73 7d 20 24 61 72 67 73 20 62  d flags} $args b
1810: 72 65 61 6b 0a 20 20 20 20 6c 61 70 70 65 6e 64  reak.    lappend
1820: 20 3a 3a 73 79 6e 63 73 20 5b 66 69 6c 65 20 74   ::syncs [file t
1830: 61 69 6c 20 24 66 69 6c 65 6e 61 6d 65 5d 20 24  ail $filename] $
1840: 66 6c 61 67 73 0a 20 20 7d 0a 20 20 64 6f 5f 74  flags.  }.  do_t
1850: 65 73 74 20 77 61 6c 33 2d 33 2e 24 74 6e 20 7b  est wal3-3.$tn {
1860: 0a 20 20 20 20 66 6f 72 63 65 64 65 6c 65 74 65  .    forcedelete
1870: 20 74 65 73 74 2e 64 62 20 74 65 73 74 2e 64 62   test.db test.db
1880: 2d 77 61 6c 20 74 65 73 74 2e 64 62 2d 6a 6f 75  -wal test.db-jou
1890: 72 6e 61 6c 0a 20 20 0a 20 20 20 20 74 65 73 74  rnal.  .    test
18a0: 76 66 73 20 54 0a 20 20 20 20 54 20 66 69 6c 74  vfs T.    T filt
18b0: 65 72 20 7b 7d 20 0a 20 20 20 20 54 20 73 63 72  er {} .    T scr
18c0: 69 70 74 20 73 79 6e 63 5f 63 6f 75 6e 74 65 72  ipt sync_counter
18d0: 0a 20 20 20 20 73 71 6c 69 74 65 33 20 64 62 20  .    sqlite3 db 
18e0: 74 65 73 74 2e 64 62 20 2d 76 66 73 20 54 0a 20  test.db -vfs T. 
18f0: 20 0a 20 20 20 20 65 78 65 63 73 71 6c 20 22 50   .    execsql "P
1900: 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75  RAGMA synchronou
1910: 73 20 3d 20 24 73 79 6e 63 6d 6f 64 65 22 0a 20  s = $syncmode". 
1920: 20 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41     execsql { PRA
1930: 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  GMA journal_mode
1940: 20 3d 20 57 41 4c 20 7d 0a 20 20 20 20 65 78 65   = WAL }.    exe
1950: 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41  csql { CREATE TA
1960: 42 4c 45 20 66 69 6c 6c 65 72 28 61 2c 62 2c 63  BLE filler(a,b,c
1970: 29 3b 20 7d 0a 0a 20 20 20 20 73 65 74 20 3a 3a  ); }..    set ::
1980: 73 79 6e 63 73 20 5b 6c 69 73 74 5d 0a 20 20 20  syncs [list].   
1990: 20 54 20 66 69 6c 74 65 72 20 78 53 79 6e 63 0a   T filter xSync.
19a0: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20      execsql {.  
19b0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
19c0: 20 78 28 79 29 3b 0a 20 20 20 20 20 20 49 4e 53   x(y);.      INS
19d0: 45 52 54 20 49 4e 54 4f 20 78 20 56 41 4c 55 45  ERT INTO x VALUE
19e0: 53 28 27 7a 27 29 3b 0a 20 20 20 20 20 20 50 52  S('z');.      PR
19f0: 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f  AGMA wal_checkpo
1a00: 69 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54  int;.    }.    T
1a10: 20 66 69 6c 74 65 72 20 7b 7d 0a 20 20 20 20 73   filter {}.    s
1a20: 65 74 20 3a 3a 73 79 6e 63 73 0a 20 20 7d 20 24  et ::syncs.  } $
1a30: 73 79 6e 63 63 6f 75 6e 74 0a 0a 20 20 64 62 20  synccount..  db 
1a40: 63 6c 6f 73 65 0a 20 20 54 20 64 65 6c 65 74 65  close.  T delete
1a50: 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .}..#-----------
1a60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
1aa0: 20 57 68 65 6e 20 72 65 63 6f 76 65 72 69 6e 67   When recovering
1ab0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1ac0: 20 61 20 57 41 4c 20 66 69 6c 65 2c 20 61 20 70   a WAL file, a p
1ad0: 72 6f 63 65 73 73 20 6f 62 74 61 69 6e 73 20 74  rocess obtains t
1ae0: 68 65 20 57 52 49 54 45 52 0a 23 20 6c 6f 63 6b  he WRITER.# lock
1af0: 2c 20 74 68 65 6e 20 6c 6f 63 6b 73 20 61 6c 6c  , then locks all
1b00: 20 6f 74 68 65 72 20 62 79 74 65 73 20 62 65 66   other bytes bef
1b10: 6f 72 65 20 63 6f 6d 6d 65 6e 63 69 6e 67 20 72  ore commencing r
1b20: 65 63 6f 76 65 72 79 2e 20 49 66 20 69 74 20 66  ecovery. If it f
1b30: 61 69 6c 73 0a 23 20 74 6f 20 6c 6f 63 6b 20 61  ails.# to lock a
1b40: 6c 6c 20 6f 74 68 65 72 20 62 79 74 65 73 20 28  ll other bytes (
1b50: 62 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68  because some oth
1b60: 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f  er process is ho
1b70: 6c 64 69 6e 67 20 61 20 72 65 61 64 0a 23 20 6c  lding a read.# l
1b80: 6f 63 6b 29 20 69 74 20 73 68 6f 75 6c 64 20 72  ock) it should r
1b90: 65 74 72 79 20 75 70 20 74 6f 20 31 30 30 20 74  etry up to 100 t
1ba0: 69 6d 65 73 2e 20 54 68 65 6e 20 72 65 74 75 72  imes. Then retur
1bb0: 6e 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f  n SQLITE_PROTOCO
1bc0: 4c 20 74 6f 20 74 68 65 20 0a 23 20 63 61 6c 6c  L to the .# call
1bd0: 65 72 2e 20 54 65 73 74 20 74 68 69 73 20 28 74  er. Test this (t
1be0: 65 73 74 20 63 61 73 65 20 77 61 6c 33 2d 34 2e  est case wal3-4.
1bf0: 33 29 2e 0a 23 0a 23 20 41 6c 73 6f 20 74 65 73  3)..#.# Also tes
1c00: 74 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20  t the effect of 
1c10: 68 69 74 74 69 6e 67 20 61 6e 20 53 51 4c 49 54  hitting an SQLIT
1c20: 45 5f 42 55 53 59 20 77 68 69 6c 65 20 61 74 74  E_BUSY while att
1c30: 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69  empting to obtai
1c40: 6e 0a 23 20 74 68 65 20 57 52 49 54 45 52 20 6c  n.# the WRITER l
1c50: 6f 63 6b 20 28 73 68 6f 75 6c 64 20 62 65 20 74  ock (should be t
1c60: 68 65 20 73 61 6d 65 29 2e 20 54 65 73 74 20 63  he same). Test c
1c70: 61 73 65 20 77 61 6c 33 2d 34 2e 34 2e 0a 23 20  ase wal3-4.4..# 
1c80: 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62  .proc lock_callb
1c90: 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65  ack {method file
1ca0: 6e 61 6d 65 20 68 61 6e 64 6c 65 20 6c 6f 63 6b  name handle lock
1cb0: 7d 20 7b 0a 20 20 6c 61 70 70 65 6e 64 20 3a 3a  } {.  lappend ::
1cc0: 6c 6f 63 6b 73 20 24 6c 6f 63 6b 0a 7d 0a 64 6f  locks $lock.}.do
1cd0: 5f 74 65 73 74 20 77 61 6c 33 2d 34 2e 31 20 7b  _test wal3-4.1 {
1ce0: 0a 20 20 74 65 73 74 76 66 73 20 54 0a 20 20 54  .  testvfs T.  T
1cf0: 20 66 69 6c 74 65 72 20 78 53 68 6d 4c 6f 63 6b   filter xShmLock
1d00: 20 0a 20 20 54 20 73 63 72 69 70 74 20 6c 6f 63   .  T script loc
1d10: 6b 5f 63 61 6c 6c 62 61 63 6b 0a 20 20 73 65 74  k_callback.  set
1d20: 20 3a 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a   ::locks [list].
1d30: 20 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73    sqlite3 db tes
1d40: 74 2e 64 62 20 2d 76 66 73 20 54 0a 20 20 65 78  t.db -vfs T.  ex
1d50: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a  ecsql { SELECT *
1d60: 20 46 52 4f 4d 20 78 20 7d 0a 20 20 6c 72 61 6e   FROM x }.  lran
1d70: 67 65 20 24 3a 3a 6c 6f 63 6b 73 20 30 20 33 0a  ge $::locks 0 3.
1d80: 7d 20 5b 6c 69 73 74 20 7b 30 20 31 20 6c 6f 63  } [list {0 1 loc
1d90: 6b 20 65 78 63 6c 75 73 69 76 65 7d 20 7b 31 20  k exclusive} {1 
1da0: 37 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76 65  7 lock exclusive
1db0: 7d 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20  }      \.       
1dc0: 20 7b 31 20 37 20 75 6e 6c 6f 63 6b 20 65 78 63   {1 7 unlock exc
1dd0: 6c 75 73 69 76 65 7d 20 7b 30 20 31 20 75 6e 6c  lusive} {0 1 unl
1de0: 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 7d 20 20  ock exclusive}  
1df0: 5c 0a 5d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33  \.].do_test wal3
1e00: 2d 34 2e 32 20 7b 0a 20 20 64 62 20 63 6c 6f 73  -4.2 {.  db clos
1e10: 65 0a 20 20 73 65 74 20 3a 3a 6c 6f 63 6b 73 20  e.  set ::locks 
1e20: 5b 6c 69 73 74 5d 0a 20 20 73 71 6c 69 74 65 33  [list].  sqlite3
1e30: 20 64 62 20 74 65 73 74 2e 64 62 20 2d 76 66 73   db test.db -vfs
1e40: 20 54 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53   T.  execsql { S
1e50: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78 20 7d  ELECT * FROM x }
1e60: 0a 20 20 6c 72 61 6e 67 65 20 24 3a 3a 6c 6f 63  .  lrange $::loc
1e70: 6b 73 20 30 20 33 0a 7d 20 5b 6c 69 73 74 20 7b  ks 0 3.} [list {
1e80: 30 20 31 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69  0 1 lock exclusi
1e90: 76 65 7d 20 7b 31 20 37 20 6c 6f 63 6b 20 65 78  ve} {1 7 lock ex
1ea0: 63 6c 75 73 69 76 65 7d 20 20 20 20 20 20 5c 0a  clusive}      \.
1eb0: 20 20 20 20 20 20 20 20 7b 31 20 37 20 75 6e 6c          {1 7 unl
1ec0: 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 7d 20 7b  ock exclusive} {
1ed0: 30 20 31 20 75 6e 6c 6f 63 6b 20 65 78 63 6c 75  0 1 unlock exclu
1ee0: 73 69 76 65 7d 20 20 5c 0a 5d 0a 70 72 6f 63 20  sive}  \.].proc 
1ef0: 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d  lock_callback {m
1f00: 65 74 68 6f 64 20 66 69 6c 65 6e 61 6d 65 20 68  ethod filename h
1f10: 61 6e 64 6c 65 20 6c 6f 63 6b 7d 20 7b 0a 20 20  andle lock} {.  
1f20: 69 66 20 7b 24 6c 6f 63 6b 20 3d 3d 20 22 31 20  if {$lock == "1 
1f30: 37 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76 65  7 lock exclusive
1f40: 22 7d 20 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  "} { return SQLI
1f50: 54 45 5f 42 55 53 59 20 7d 0a 20 20 72 65 74 75  TE_BUSY }.  retu
1f60: 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 7d 0a 70  rn SQLITE_OK.}.p
1f70: 75 74 73 20 22 20 20 57 61 72 6e 69 6e 67 3a 20  uts "  Warning: 
1f80: 54 68 69 73 20 6e 65 78 74 20 74 65 73 74 20 63  This next test c
1f90: 61 73 65 20 63 61 75 73 65 73 20 53 51 4c 69 74  ase causes SQLit
1fa0: 65 20 74 6f 20 63 61 6c 6c 20 78 53 6c 65 65 70  e to call xSleep
1fb0: 28 31 29 20 31 30 30 20 74 69 6d 65 73 2e 22 0a  (1) 100 times.".
1fc0: 70 75 74 73 20 22 20 20 4e 6f 72 6d 61 6c 6c 79  puts "  Normally
1fd0: 20 74 68 69 73 20 65 71 75 61 74 65 73 20 74 6f   this equates to
1fe0: 20 61 20 31 30 30 6d 73 20 64 65 6c 61 79 2c 20   a 100ms delay, 
1ff0: 62 75 74 20 69 66 20 53 51 4c 69 74 65 20 69 73  but if SQLite is
2000: 20 62 75 69 6c 74 20 6f 6e 20 75 6e 69 78 22 0a   built on unix".
2010: 70 75 74 73 20 22 20 20 77 69 74 68 6f 75 74 20  puts "  without 
2020: 48 41 56 45 5f 55 53 4c 45 45 50 20 64 65 66 69  HAVE_USLEEP defi
2030: 6e 65 64 2c 20 69 74 20 6d 61 79 20 62 65 20 31  ned, it may be 1
2040: 30 30 20 73 65 63 6f 6e 64 73 2e 22 0a 64 6f 5f  00 seconds.".do_
2050: 74 65 73 74 20 77 61 6c 33 2d 34 2e 33 20 7b 0a  test wal3-4.3 {.
2060: 20 20 64 62 20 63 6c 6f 73 65 0a 20 20 73 65 74    db close.  set
2070: 20 3a 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a   ::locks [list].
2080: 20 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73    sqlite3 db tes
2090: 74 2e 64 62 20 2d 76 66 73 20 54 0a 20 20 63 61  t.db -vfs T.  ca
20a0: 74 63 68 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  tchsql { SELECT 
20b0: 2a 20 46 52 4f 4d 20 78 20 7d 0a 7d 20 7b 31 20  * FROM x }.} {1 
20c0: 7b 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f  {locking protoco
20d0: 6c 7d 7d 0a 0a 70 75 74 73 20 22 20 20 57 61 72  l}}..puts "  War
20e0: 6e 69 6e 67 3a 20 53 61 6d 65 20 61 67 61 69 6e  ning: Same again
20f0: 21 22 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c  !".proc lock_cal
2100: 6c 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69  lback {method fi
2110: 6c 65 6e 61 6d 65 20 68 61 6e 64 6c 65 20 6c 6f  lename handle lo
2120: 63 6b 7d 20 7b 0a 20 20 69 66 20 7b 24 6c 6f 63  ck} {.  if {$loc
2130: 6b 20 3d 3d 20 22 30 20 31 20 6c 6f 63 6b 20 65  k == "0 1 lock e
2140: 78 63 6c 75 73 69 76 65 22 7d 20 7b 20 72 65 74  xclusive"} { ret
2150: 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20  urn SQLITE_BUSY 
2160: 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
2170: 45 5f 4f 4b 0a 7d 0a 64 6f 5f 74 65 73 74 20 77  E_OK.}.do_test w
2180: 61 6c 33 2d 34 2e 34 20 7b 0a 20 20 64 62 20 63  al3-4.4 {.  db c
2190: 6c 6f 73 65 0a 20 20 73 65 74 20 3a 3a 6c 6f 63  lose.  set ::loc
21a0: 6b 73 20 5b 6c 69 73 74 5d 0a 20 20 73 71 6c 69  ks [list].  sqli
21b0: 74 65 33 20 64 62 20 74 65 73 74 2e 64 62 20 2d  te3 db test.db -
21c0: 76 66 73 20 54 0a 20 20 63 61 74 63 68 73 71 6c  vfs T.  catchsql
21d0: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
21e0: 20 78 20 7d 0a 7d 20 7b 31 20 7b 6c 6f 63 6b 69   x }.} {1 {locki
21f0: 6e 67 20 70 72 6f 74 6f 63 6f 6c 7d 7d 0a 64 62  ng protocol}}.db
2200: 20 63 6c 6f 73 65 0a 54 20 64 65 6c 65 74 65 0a   close.T delete.
2210: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
2220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4f  ------------.# O
2260: 6e 6c 79 20 6f 6e 65 20 63 6c 69 65 6e 74 20 6d  nly one client m
2270: 61 79 20 72 75 6e 20 72 65 63 6f 76 65 72 79 20  ay run recovery 
2280: 61 74 20 61 20 74 69 6d 65 2e 20 54 65 73 74 20  at a time. Test 
2290: 74 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 2e 0a  this mechanism..
22a0: 23 0a 23 20 57 68 65 6e 20 63 6c 69 65 6e 74 2d  #.# When client-
22b0: 32 20 74 72 69 65 73 20 74 6f 20 6f 70 65 6e 20  2 tries to open 
22c0: 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
22d0: 6f 6e 20 77 68 69 6c 65 20 63 6c 69 65 6e 74 2d  on while client-
22e0: 31 20 69 73 20 0a 23 20 72 75 6e 6e 69 6e 67 20  1 is .# running 
22f0: 72 65 63 6f 76 65 72 79 2c 20 69 74 20 66 61 69  recovery, it fai
2300: 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c  ls to obtain a l
2310: 6f 63 6b 20 6f 6e 20 61 6e 20 61 52 65 61 64 4d  ock on an aReadM
2320: 61 72 6b 5b 5d 20 73 6c 6f 74 0a 23 20 28 62 65  ark[] slot.# (be
2330: 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20 61  cause they are a
2340: 6c 6c 20 6c 6f 63 6b 65 64 20 62 79 20 72 65 63  ll locked by rec
2350: 6f 76 65 72 79 29 2e 20 49 74 20 74 68 65 6e 20  overy). It then 
2360: 74 72 69 65 73 20 74 6f 20 6f 62 74 61 69 6e 0a  tries to obtain.
2370: 23 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  # a shared lock 
2380: 6f 6e 20 74 68 65 20 52 45 43 4f 56 45 52 20 6c  on the RECOVER l
2390: 6f 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  ock to see if th
23a0: 65 72 65 20 72 65 61 6c 6c 79 20 69 73 20 61 0a  ere really is a.
23b0: 23 20 72 65 63 6f 76 65 72 79 20 72 75 6e 6e 69  # recovery runni
23c0: 6e 67 20 6f 72 20 6e 6f 74 2e 0a 23 0a 23 20 54  ng or not..#.# T
23d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 74 65 73  his block of tes
23e0: 74 73 20 63 68 65 63 6b 73 20 74 68 65 20 65 66  ts checks the ef
23f0: 66 65 63 74 20 6f 66 20 61 6e 20 53 51 4c 49 54  fect of an SQLIT
2400: 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54 45  E_BUSY or SQLITE
2410: 5f 49 4f 45 52 52 0a 23 20 62 65 69 6e 67 20 72  _IOERR.# being r
2420: 65 74 75 72 6e 65 64 20 77 68 65 6e 20 63 6c 69  eturned when cli
2430: 65 6e 74 2d 32 20 61 74 74 65 6d 70 74 73 20 61  ent-2 attempts a
2440: 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20   shared lock on 
2450: 74 68 65 20 52 45 43 4f 56 45 52 20 62 79 74 65  the RECOVER byte
2460: 2e 0a 23 0a 23 20 41 6e 20 53 51 4c 49 54 45 5f  ..#.# An SQLITE_
2470: 42 55 53 59 20 73 68 6f 75 6c 64 20 62 65 20 63  BUSY should be c
2480: 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 6e 20 53  onverted to an S
2490: 51 4c 49 54 45 5f 42 55 53 59 5f 52 45 43 4f 56  QLITE_BUSY_RECOV
24a0: 45 52 59 2e 20 41 6e 0a 23 20 53 51 4c 49 54 45  ERY. An.# SQLITE
24b0: 5f 49 4f 45 52 52 20 73 68 6f 75 6c 64 20 62 65  _IOERR should be
24c0: 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
24d0: 20 63 61 6c 6c 65 72 2e 0a 23 0a 64 6f 5f 74 65   caller..#.do_te
24e0: 73 74 20 77 61 6c 33 2d 35 2e 31 20 7b 0a 20 20  st wal3-5.1 {.  
24f0: 66 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f  faultsim_delete_
2500: 61 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65  and_reopen.  exe
2510: 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
2520: 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d  A journal_mode =
2530: 20 57 41 4c 3b 0a 20 20 20 20 43 52 45 41 54 45   WAL;.    CREATE
2540: 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b   TABLE t1(a, b);
2550: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2560: 20 74 31 20 56 41 4c 55 45 53 28 31 2c 20 32 29   t1 VALUES(1, 2)
2570: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2580: 4f 20 74 31 20 56 41 4c 55 45 53 28 33 2c 20 34  O t1 VALUES(3, 4
2590: 29 3b 0a 20 20 7d 0a 20 20 66 61 75 6c 74 73 69  );.  }.  faultsi
25a0: 6d 5f 73 61 76 65 5f 61 6e 64 5f 63 6c 6f 73 65  m_save_and_close
25b0: 0a 7d 20 7b 7d 0a 0a 74 65 73 74 76 66 73 20 54  .} {}..testvfs T
25c0: 20 2d 64 65 66 61 75 6c 74 20 31 0a 54 20 73 63   -default 1.T sc
25d0: 72 69 70 74 20 6d 65 74 68 6f 64 5f 63 61 6c 6c  ript method_call
25e0: 62 61 63 6b 0a 0a 70 72 6f 63 20 6d 65 74 68 6f  back..proc metho
25f0: 64 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65 74 68  d_callback {meth
2600: 6f 64 20 61 72 67 73 7d 20 7b 0a 20 20 69 66 20  od args} {.  if 
2610: 7b 24 6d 65 74 68 6f 64 20 3d 3d 20 22 78 53 68  {$method == "xSh
2620: 6d 42 61 72 72 69 65 72 22 7d 20 7b 0a 20 20 20  mBarrier"} {.   
2630: 20 69 6e 63 72 20 3a 3a 62 61 72 72 69 65 72 5f   incr ::barrier_
2640: 63 6f 75 6e 74 0a 20 20 20 20 69 66 20 7b 24 3a  count.    if {$:
2650: 3a 62 61 72 72 69 65 72 5f 63 6f 75 6e 74 20 3d  :barrier_count =
2660: 3d 20 32 7d 20 7b 0a 20 20 20 20 20 20 23 20 54  = 2} {.      # T
2670: 68 69 73 20 63 6f 64 65 20 69 73 20 65 78 65 63  his code is exec
2680: 75 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  uted within the 
2690: 78 53 68 6d 42 61 72 72 69 65 72 28 29 20 63 61  xShmBarrier() ca
26a0: 6c 6c 62 61 63 6b 20 69 6e 76 6f 6b 65 64 0a 20  llback invoked. 
26b0: 20 20 20 20 20 23 20 62 79 20 74 68 65 20 63 6c       # by the cl
26c0: 69 65 6e 74 20 72 75 6e 6e 69 6e 67 20 72 65 63  ient running rec
26d0: 6f 76 65 72 79 20 61 73 20 70 61 72 74 20 6f 66  overy as part of
26e0: 20 77 72 69 74 69 6e 67 20 74 68 65 20 72 65 63   writing the rec
26f0: 6f 76 65 72 65 64 0a 20 20 20 20 20 20 23 20 77  overed.      # w
2700: 61 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 2e  al-index header.
2710: 20 49 66 20 61 20 73 65 63 6f 6e 64 20 63 6c 69   If a second cli
2720: 65 6e 74 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ent attempts to 
2730: 61 63 63 65 73 73 20 74 68 65 20 0a 20 20 20 20  access the .    
2740: 20 20 23 20 64 61 74 61 62 61 73 65 20 6e 6f 77    # database now
2750: 2c 20 69 74 20 72 65 61 64 73 20 61 20 63 6f 72  , it reads a cor
2760: 72 75 70 74 20 28 70 61 72 74 69 61 6c 6c 79 20  rupt (partially 
2770: 77 72 69 74 74 65 6e 29 20 77 61 6c 2d 69 6e 64  written) wal-ind
2780: 65 78 0a 20 20 20 20 20 20 23 20 68 65 61 64 65  ex.      # heade
2790: 72 2e 20 42 75 74 20 69 74 20 63 61 6e 6e 6f 74  r. But it cannot
27a0: 20 65 76 65 6e 20 67 65 74 20 74 68 61 74 20 66   even get that f
27b0: 61 72 2c 20 61 73 20 74 68 65 20 66 69 72 73 74  ar, as the first
27c0: 20 63 6c 69 65 6e 74 0a 20 20 20 20 20 20 23 20   client.      # 
27d0: 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67  is still holding
27e0: 20 61 6c 6c 20 74 68 65 20 6c 6f 63 6b 73 20 28   all the locks (
27f0: 72 65 63 6f 76 65 72 79 20 74 61 6b 65 73 20 61  recovery takes a
2800: 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
2810: 0a 20 20 20 20 20 20 23 20 6f 6e 20 2a 61 6c 6c  .      # on *all
2820: 2a 20 64 62 20 6c 6f 63 6b 73 2c 20 70 72 65 76  * db locks, prev
2830: 65 6e 74 69 6e 67 20 61 63 63 65 73 73 20 62 79  enting access by
2840: 20 61 6e 79 20 6f 74 68 65 72 20 63 6c 69 65 6e   any other clien
2850: 74 29 2e 0a 20 20 20 20 20 20 23 0a 20 20 20 20  t)..      #.    
2860: 20 20 23 20 49 66 20 67 6c 6f 62 61 6c 20 76 61    # If global va
2870: 72 69 61 62 6c 65 20 3a 3a 77 61 6c 33 5f 64 6f  riable ::wal3_do
2880: 5f 6c 6f 63 6b 66 61 69 6c 75 72 65 20 69 73 20  _lockfailure is 
2890: 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 73  non-zero, then s
28a0: 65 74 0a 20 20 20 20 20 20 23 20 74 68 69 6e 67  et.      # thing
28b0: 73 20 75 70 20 73 6f 20 74 68 61 74 20 61 6e 20  s up so that an 
28c0: 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  IO error occurs 
28d0: 77 69 74 68 69 6e 20 61 6e 20 78 53 68 6d 4c 6f  within an xShmLo
28e0: 63 6b 28 29 20 63 61 6c 6c 62 61 63 6b 0a 20 20  ck() callback.  
28f0: 20 20 20 20 23 20 6d 61 64 65 20 62 79 20 74 68      # made by th
2900: 65 20 73 65 63 6f 6e 64 20 63 6c 69 65 6e 74 20  e second client 
2910: 28 61 6b 61 20 5b 64 62 32 5d 29 2e 0a 20 20 20  (aka [db2])..   
2920: 20 20 20 23 0a 20 20 20 20 20 20 73 71 6c 69 74     #.      sqlit
2930: 65 33 20 64 62 32 20 74 65 73 74 2e 64 62 0a 20  e3 db2 test.db. 
2940: 20 20 20 20 20 69 66 20 7b 20 24 3a 3a 77 61 6c       if { $::wal
2950: 33 5f 64 6f 5f 6c 6f 63 6b 66 61 69 6c 75 72 65  3_do_lockfailure
2960: 20 7d 20 7b 20 54 20 66 69 6c 74 65 72 20 78 53   } { T filter xS
2970: 68 6d 4c 6f 63 6b 20 7d 0a 20 20 20 20 20 20 73  hmLock }.      s
2980: 65 74 20 3a 3a 74 65 73 74 72 63 20 5b 20 63 61  et ::testrc [ ca
2990: 74 63 68 20 7b 20 64 62 32 20 65 76 61 6c 20 22  tch { db2 eval "
29a0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
29b0: 22 20 7d 20 3a 3a 74 65 73 74 6d 73 67 20 5d 0a  " } ::testmsg ].
29c0: 20 20 20 20 20 20 54 20 66 69 6c 74 65 72 20 7b        T filter {
29d0: 7d 0a 20 20 20 20 20 20 64 62 32 20 63 6c 6f 73  }.      db2 clos
29e0: 65 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  e.    }.  }..  i
29f0: 66 20 7b 24 6d 65 74 68 6f 64 20 3d 3d 20 22 78  f {$method == "x
2a00: 53 68 6d 4c 6f 63 6b 22 7d 20 7b 0a 20 20 20 20  ShmLock"} {.    
2a10: 66 6f 72 65 61 63 68 20 7b 66 69 6c 65 20 68 61  foreach {file ha
2a20: 6e 64 6c 65 20 73 70 65 63 7d 20 24 61 72 67 73  ndle spec} $args
2a30: 20 62 72 65 61 6b 0a 20 20 20 20 69 66 20 7b 20   break.    if { 
2a40: 24 73 70 65 63 20 3d 3d 20 22 32 20 31 20 6c 6f  $spec == "2 1 lo
2a50: 63 6b 20 73 68 61 72 65 64 22 20 7d 20 7b 0a 20  ck shared" } {. 
2a60: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2a70: 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 7d 0a 20  TE_IOERR.    }. 
2a80: 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
2a90: 49 54 45 5f 4f 4b 0a 7d 0a 0a 23 20 54 65 73 74  ITE_OK.}..# Test
2aa0: 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 49 54 45   a normal SQLITE
2ab0: 5f 42 55 53 59 20 72 65 74 75 72 6e 2e 0a 23 0a  _BUSY return..#.
2ac0: 54 20 66 69 6c 74 65 72 20 78 53 68 6d 42 61 72  T filter xShmBar
2ad0: 72 69 65 72 0a 73 65 74 20 74 65 73 74 72 63 20  rier.set testrc 
2ae0: 22 22 0a 73 65 74 20 74 65 73 74 6d 73 67 20 22  "".set testmsg "
2af0: 22 0a 73 65 74 20 62 61 72 72 69 65 72 5f 63 6f  ".set barrier_co
2b00: 75 6e 74 20 30 0a 73 65 74 20 77 61 6c 33 5f 64  unt 0.set wal3_d
2b10: 6f 5f 6c 6f 63 6b 66 61 69 6c 75 72 65 20 30 0a  o_lockfailure 0.
2b20: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 35 2e 32  do_test wal3-5.2
2b30: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65   {.  faultsim_re
2b40: 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e  store_and_reopen
2b50: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c  .  execsql { SEL
2b60: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 0a  ECT * FROM t1 }.
2b70: 7d 20 7b 31 20 32 20 33 20 34 7d 0a 64 6f 5f 74  } {1 2 3 4}.do_t
2b80: 65 73 74 20 77 61 6c 33 2d 35 2e 33 20 7b 0a 20  est wal3-5.3 {. 
2b90: 20 6c 69 73 74 20 24 3a 3a 74 65 73 74 72 63 20   list $::testrc 
2ba0: 24 3a 3a 74 65 73 74 6d 73 67 0a 7d 20 7b 31 20  $::testmsg.} {1 
2bb0: 7b 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63  {database is loc
2bc0: 6b 65 64 7d 7d 0a 64 62 20 63 6c 6f 73 65 0a 0a  ked}}.db close..
2bd0: 23 20 54 65 73 74 20 61 6e 20 53 51 4c 49 54 45  # Test an SQLITE
2be0: 5f 49 4f 45 52 52 20 72 65 74 75 72 6e 2e 0a 23  _IOERR return..#
2bf0: 0a 54 20 66 69 6c 74 65 72 20 78 53 68 6d 42 61  .T filter xShmBa
2c00: 72 72 69 65 72 0a 73 65 74 20 62 61 72 72 69 65  rrier.set barrie
2c10: 72 5f 63 6f 75 6e 74 20 30 0a 73 65 74 20 77 61  r_count 0.set wa
2c20: 6c 33 5f 64 6f 5f 6c 6f 63 6b 66 61 69 6c 75 72  l3_do_lockfailur
2c30: 65 20 31 0a 73 65 74 20 74 65 73 74 72 63 20 22  e 1.set testrc "
2c40: 22 0a 73 65 74 20 74 65 73 74 6d 73 67 20 22 22  ".set testmsg ""
2c50: 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 35 2e  .do_test wal3-5.
2c60: 34 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72  4 {.  faultsim_r
2c70: 65 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65  estore_and_reope
2c80: 6e 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  n.  execsql { SE
2c90: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d  LECT * FROM t1 }
2ca0: 0a 7d 20 7b 31 20 32 20 33 20 34 7d 0a 64 6f 5f  .} {1 2 3 4}.do_
2cb0: 74 65 73 74 20 77 61 6c 33 2d 35 2e 35 20 7b 0a  test wal3-5.5 {.
2cc0: 20 20 6c 69 73 74 20 24 3a 3a 74 65 73 74 72 63    list $::testrc
2cd0: 20 24 3a 3a 74 65 73 74 6d 73 67 0a 7d 20 7b 31   $::testmsg.} {1
2ce0: 20 7b 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72   {disk I/O error
2cf0: 7d 7d 0a 0a 64 62 20 63 6c 6f 73 65 0a 54 20 64  }}..db close.T d
2d00: 65 6c 65 74 65 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  elete..#--------
2d10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d50: 2d 0a 23 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67  -.# When opening
2d60: 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74   a read-transact
2d70: 69 6f 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ion on a databas
2d80: 65 2c 20 69 66 20 74 68 65 20 65 6e 74 69 72 65  e, if the entire
2d90: 20 6c 6f 67 20 68 61 73 0a 23 20 61 6c 72 65 61   log has.# alrea
2da0: 64 79 20 62 65 65 6e 20 63 6f 70 69 65 64 20 74  dy been copied t
2db0: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
2dc0: 69 6c 65 2c 20 74 68 65 20 72 65 61 64 65 72 20  ile, the reader 
2dd0: 67 72 61 62 73 20 61 20 73 70 65 63 69 61 6c 0a  grabs a special.
2de0: 23 20 6b 69 6e 64 20 6f 66 20 72 65 61 64 20 6c  # kind of read l
2df0: 6f 63 6b 20 28 6f 6e 20 61 52 65 61 64 4d 61 72  ock (on aReadMar
2e00: 6b 5b 30 5d 29 2e 20 54 68 69 73 20 73 65 74 20  k[0]). This set 
2e10: 6f 66 20 74 65 73 74 20 63 61 73 65 73 20 74 65  of test cases te
2e20: 73 74 73 20 74 68 65 20 0a 23 20 6f 75 74 63 6f  sts the .# outco
2e30: 6d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  me of the follow
2e40: 69 6e 67 3a 0a 23 0a 23 20 20 20 2b 20 54 68 65  ing:.#.#   + The
2e50: 20 72 65 61 64 65 72 20 64 69 73 63 6f 76 65 72   reader discover
2e60: 69 6e 67 20 74 68 61 74 20 62 65 74 77 65 65 6e  ing that between
2e70: 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 69   the time when i
2e80: 74 20 64 65 74 65 72 6d 69 6e 65 64 20 0a 23 20  t determined .# 
2e90: 20 20 20 20 74 68 61 74 20 74 68 65 20 6c 6f 67      that the log
2ea0: 20 68 61 64 20 62 65 65 6e 20 63 6f 6d 70 6c 65   had been comple
2eb0: 74 65 6c 79 20 62 61 63 6b 66 69 6c 6c 65 64 20  tely backfilled 
2ec0: 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  and the lock is 
2ed0: 6f 62 74 61 69 6e 65 64 0a 23 20 20 20 20 20 74  obtained.#     t
2ee0: 68 61 74 20 61 20 77 72 69 74 65 72 20 68 61 73  hat a writer has
2ef0: 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
2f00: 6c 6f 67 2e 20 49 6e 20 74 68 69 73 20 63 61 73  log. In this cas
2f10: 65 20 74 68 65 20 72 65 61 64 65 72 20 73 68 6f  e the reader sho
2f20: 75 6c 64 0a 23 20 20 20 20 20 61 63 71 75 69 72  uld.#     acquir
2f30: 65 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 65  e a different re
2f40: 61 64 2d 6c 6f 63 6b 20 28 6e 6f 74 20 61 52 65  ad-lock (not aRe
2f50: 61 64 4d 61 72 6b 5b 30 5d 29 20 61 6e 64 20 72  adMark[0]) and r
2f60: 65 61 64 20 74 68 65 20 6e 65 77 0a 23 20 20 20  ead the new.#   
2f70: 20 20 73 6e 61 70 73 68 6f 74 2e 0a 23 0a 23 20    snapshot..#.# 
2f80: 20 20 2b 20 54 68 65 20 61 74 74 65 6d 70 74 20    + The attempt 
2f90: 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f  to obtain the lo
2fa0: 63 6b 20 6f 6e 20 61 52 65 61 64 4d 61 72 6b 5b  ck on aReadMark[
2fb0: 30 5d 20 66 61 69 6c 73 20 77 69 74 68 20 53 51  0] fails with SQ
2fc0: 4c 49 54 45 5f 42 55 53 59 2e 0a 23 20 20 20 20  LITE_BUSY..#    
2fd0: 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e   This can happen
2fe0: 20 69 66 20 61 20 63 68 65 63 6b 70 6f 69 6e 74   if a checkpoint
2ff0: 20 69 73 20 6f 6e 67 6f 69 6e 67 2e 20 49 6e 20   is ongoing. In 
3000: 74 68 69 73 20 63 61 73 65 20 61 6c 73 6f 20 73  this case also s
3010: 69 6d 70 6c 79 0a 23 20 20 20 20 20 6f 62 74 61  imply.#     obta
3020: 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 72  in a different r
3030: 65 61 64 2d 6c 6f 63 6b 2e 0a 23 0a 63 61 74 63  ead-lock..#.catc
3040: 68 20 7b 64 62 20 63 6c 6f 73 65 7d 0a 74 65 73  h {db close}.tes
3050: 74 76 66 73 20 54 20 2d 64 65 66 61 75 6c 74 20  tvfs T -default 
3060: 31 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36  1.do_test wal3-6
3070: 2e 31 2e 31 20 7b 0a 20 20 66 6f 72 63 65 64 65  .1.1 {.  forcede
3080: 6c 65 74 65 20 74 65 73 74 2e 64 62 20 74 65 73  lete test.db tes
3090: 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20 74 65 73  t.db-journal tes
30a0: 74 2e 64 62 20 77 61 6c 0a 20 20 73 71 6c 69 74  t.db wal.  sqlit
30b0: 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20 20  e3 db test.db.  
30c0: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
30d0: 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 6f   auto_vacuum = o
30e0: 66 66 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  ff }.  execsql {
30f0: 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f   PRAGMA journal_
3100: 6d 6f 64 65 20 3d 20 57 41 4c 20 7d 0a 20 20 65  mode = WAL }.  e
3110: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
3120: 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
3130: 62 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  b);.    INSERT I
3140: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 27 6f  NTO t1 VALUES('o
3150: 27 2c 20 27 74 27 29 3b 0a 20 20 20 20 49 4e 53  ', 't');.    INS
3160: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
3170: 45 53 28 27 74 27 2c 20 27 66 27 29 3b 0a 20 20  ES('t', 'f');.  
3180: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 77  }.} {}.do_test w
3190: 61 6c 33 2d 36 2e 31 2e 32 20 7b 0a 20 20 73 71  al3-6.1.2 {.  sq
31a0: 6c 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64  lite3 db2 test.d
31b0: 62 0a 20 20 73 71 6c 69 74 65 33 20 64 62 33 20  b.  sqlite3 db3 
31c0: 74 65 73 74 2e 64 62 0a 20 20 65 78 65 63 73 71  test.db.  execsq
31d0: 6c 20 7b 20 42 45 47 49 4e 20 3b 20 53 45 4c 45  l { BEGIN ; SELE
31e0: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 20 64  CT * FROM t1 } d
31f0: 62 33 0a 7d 20 7b 6f 20 74 20 74 20 66 7d 0a 64  b3.} {o t t f}.d
3200: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31 2e  o_test wal3-6.1.
3210: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  3 {.  execsql { 
3220: 50 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b  PRAGMA wal_check
3230: 70 6f 69 6e 74 20 7d 20 64 62 32 0a 7d 20 7b 30  point } db2.} {0
3240: 20 34 20 34 7d 0a 0a 23 20 41 74 20 74 68 69 73   4 4}..# At this
3250: 20 70 6f 69 6e 74 20 74 68 65 20 6c 6f 67 20 66   point the log f
3260: 69 6c 65 20 68 61 73 20 62 65 65 6e 20 66 75 6c  ile has been ful
3270: 6c 79 20 63 68 65 63 6b 70 6f 69 6e 74 65 64 2e  ly checkpointed.
3280: 20 48 6f 77 65 76 65 72 2c 20 0a 23 20 63 6f 6e   However, .# con
3290: 6e 65 63 74 69 6f 6e 20 5b 64 62 33 5d 20 68 6f  nection [db3] ho
32a0: 6c 64 73 20 61 20 6c 6f 63 6b 20 74 68 61 74 20  lds a lock that 
32b0: 70 72 65 76 65 6e 74 73 20 74 68 65 20 6c 6f 67  prevents the log
32c0: 20 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 61 70   from being wrap
32d0: 70 65 64 2e 0a 23 20 54 65 73 74 20 63 61 73 65  ped..# Test case
32e0: 20 33 2e 36 2e 31 2e 34 20 68 61 73 20 5b 64 62   3.6.1.4 has [db
32f0: 5d 20 61 74 74 65 6d 70 74 20 61 20 72 65 61 64  ] attempt a read
3300: 2d 6c 6f 63 6b 20 6f 6e 20 61 52 65 61 64 4d 61  -lock on aReadMa
3310: 72 6b 5b 30 5d 2e 20 42 75 74 0a 23 20 61 73 20  rk[0]. But.# as 
3320: 69 74 20 69 73 20 6f 62 74 61 69 6e 69 6e 67 20  it is obtaining 
3330: 74 68 65 20 6c 6f 63 6b 2c 20 5b 64 62 32 5d 20  the lock, [db2] 
3340: 61 70 70 65 6e 64 73 20 74 6f 20 74 68 65 20 6c  appends to the l
3350: 6f 67 20 66 69 6c 65 2e 0a 23 0a 54 20 66 69 6c  og file..#.T fil
3360: 74 65 72 20 78 53 68 6d 4c 6f 63 6b 0a 54 20 73  ter xShmLock.T s
3370: 63 72 69 70 74 20 6c 6f 63 6b 5f 63 61 6c 6c 62  cript lock_callb
3380: 61 63 6b 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61  ack.proc lock_ca
3390: 6c 6c 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66  llback {method f
33a0: 69 6c 65 20 68 61 6e 64 6c 65 20 73 70 65 63 7d  ile handle spec}
33b0: 20 7b 0a 20 20 69 66 20 7b 24 73 70 65 63 20 3d   {.  if {$spec =
33c0: 3d 20 22 33 20 31 20 6c 6f 63 6b 20 73 68 61 72  = "3 1 lock shar
33d0: 65 64 22 7d 20 7b 0a 20 20 20 20 23 20 54 68 69  ed"} {.    # Thi
33e0: 73 20 69 73 20 74 68 65 20 63 61 6c 6c 62 61 63  s is the callbac
33f0: 6b 20 66 6f 72 20 5b 64 62 5d 20 74 6f 20 6f 62  k for [db] to ob
3400: 74 61 69 6e 20 74 68 65 20 72 65 61 64 20 6c 6f  tain the read lo
3410: 63 6b 20 6f 6e 20 61 52 65 61 64 4d 61 72 6b 5b  ck on aReadMark[
3420: 30 5d 2e 0a 20 20 20 20 23 20 44 69 73 61 62 6c  0]..    # Disabl
3430: 65 20 66 75 74 75 72 65 20 63 61 6c 6c 62 61 63  e future callbac
3440: 6b 73 20 75 73 69 6e 67 20 5b 54 20 66 69 6c 74  ks using [T filt
3450: 65 72 20 7b 7d 5d 20 61 6e 64 20 77 72 69 74 65  er {}] and write
3460: 20 74 6f 20 74 68 65 20 6c 6f 67 0a 20 20 20 20   to the log.    
3470: 23 20 66 69 6c 65 20 75 73 69 6e 67 20 5b 64 62  # file using [db
3480: 32 5d 2e 20 5b 64 62 33 5d 20 69 73 20 70 72 65  2]. [db3] is pre
3490: 76 65 6e 74 69 6e 67 20 5b 64 62 32 5d 20 66 72  venting [db2] fr
34a0: 6f 6d 20 77 72 61 70 70 69 6e 67 20 74 68 65 20  om wrapping the 
34b0: 6c 6f 67 0a 20 20 20 20 23 20 68 65 72 65 2c 20  log.    # here, 
34c0: 73 6f 20 74 68 69 73 20 69 73 20 61 6e 20 61 70  so this is an ap
34d0: 70 65 6e 64 2e 0a 20 20 20 20 54 20 66 69 6c 74  pend..    T filt
34e0: 65 72 20 7b 7d 0a 20 20 20 20 64 62 32 20 65 76  er {}.    db2 ev
34f0: 61 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  al { INSERT INTO
3500: 20 74 31 20 56 41 4c 55 45 53 28 27 66 27 2c 20   t1 VALUES('f', 
3510: 27 73 27 29 20 7d 0a 20 20 7d 0a 20 20 72 65 74  's') }.  }.  ret
3520: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 7d 0a  urn SQLITE_OK.}.
3530: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31  do_test wal3-6.1
3540: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
3550: 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20  .    BEGIN;.    
3560: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
3570: 3b 0a 20 20 7d 0a 7d 20 7b 6f 20 74 20 74 20 66  ;.  }.} {o t t f
3580: 20 66 20 73 7d 0a 0a 23 20 5b 64 62 5d 20 73 68   f s}..# [db] sh
3590: 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 68 6f 6c  ould be left hol
35a0: 64 69 6e 67 20 61 20 72 65 61 64 2d 6c 6f 63 6b  ding a read-lock
35b0: 20 6f 6e 20 73 6f 6d 65 20 73 6c 6f 74 20 6f 74   on some slot ot
35c0: 68 65 72 20 74 68 61 6e 20 0a 23 20 61 52 65 61  her than .# aRea
35d0: 64 4d 61 72 6b 5b 30 5d 2e 20 54 65 73 74 20 74  dMark[0]. Test t
35e0: 68 69 73 20 62 79 20 64 65 6d 6f 6e 73 74 72 61  his by demonstra
35f0: 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 72 65  ting that the re
3600: 61 64 2d 6c 6f 63 6b 20 69 73 20 70 72 65 76 65  ad-lock is preve
3610: 6e 74 69 6e 67 0a 23 20 74 68 65 20 6c 6f 67 20  nting.# the log 
3620: 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 61 70 70  from being wrapp
3630: 65 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 77 61  ed..#.do_test wa
3640: 6c 33 2d 36 2e 31 2e 35 20 7b 0a 20 20 64 62 33  l3-6.1.5 {.  db3
3650: 20 65 76 61 6c 20 43 4f 4d 4d 49 54 0a 20 20 64   eval COMMIT.  d
3660: 62 32 20 65 76 61 6c 20 7b 20 50 52 41 47 4d 41  b2 eval { PRAGMA
3670: 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20   wal_checkpoint 
3680: 7d 0a 20 20 73 65 74 20 73 7a 31 20 5b 66 69 6c  }.  set sz1 [fil
3690: 65 20 73 69 7a 65 20 74 65 73 74 2e 64 62 2d 77  e size test.db-w
36a0: 61 6c 5d 0a 20 20 64 62 32 20 65 76 61 6c 20 7b  al].  db2 eval {
36b0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
36c0: 56 41 4c 55 45 53 28 27 73 27 2c 20 27 65 27 29  VALUES('s', 'e')
36d0: 20 7d 0a 20 20 73 65 74 20 73 7a 32 20 5b 66 69   }.  set sz2 [fi
36e0: 6c 65 20 73 69 7a 65 20 74 65 73 74 2e 64 62 2d  le size test.db-
36f0: 77 61 6c 5d 0a 20 20 65 78 70 72 20 7b 24 73 7a  wal].  expr {$sz
3700: 32 3e 24 73 7a 31 7d 0a 7d 20 7b 31 7d 0a 0a 23  2>$sz1}.} {1}..#
3710: 20 54 65 73 74 20 74 68 61 74 20 69 66 20 5b 64   Test that if [d
3720: 62 32 5d 20 68 61 64 20 6e 6f 74 20 69 6e 74 65  b2] had not inte
3730: 72 66 65 72 65 64 20 77 68 65 6e 20 5b 64 62 5d  rfered when [db]
3740: 20 77 61 73 20 74 72 79 69 6e 67 20 74 6f 20 67   was trying to g
3750: 72 61 62 0a 23 20 61 52 65 61 64 4d 61 72 6b 5b  rab.# aReadMark[
3760: 30 5d 2c 20 69 74 20 77 6f 75 6c 64 20 68 61 76  0], it would hav
3770: 65 20 62 65 65 6e 20 70 6f 73 73 69 62 6c 65 20  e been possible 
3780: 74 6f 20 77 72 61 70 20 74 68 65 20 6c 6f 67 20  to wrap the log 
3790: 69 6e 20 33 2e 36 2e 31 2e 35 2e 0a 23 0a 64 6f  in 3.6.1.5..#.do
37a0: 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31 2e 36  _test wal3-6.1.6
37b0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 43   {.  execsql { C
37c0: 4f 4d 4d 49 54 20 7d 0a 20 20 65 78 65 63 73 71  OMMIT }.  execsq
37d0: 6c 20 7b 20 50 52 41 47 4d 41 20 77 61 6c 5f 63  l { PRAGMA wal_c
37e0: 68 65 63 6b 70 6f 69 6e 74 20 7d 20 64 62 32 0a  heckpoint } db2.
37f0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
3800: 42 45 47 49 4e 3b 0a 20 20 20 20 53 45 4c 45 43  BEGIN;.    SELEC
3810: 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d  T * FROM t1;.  }
3820: 0a 7d 20 7b 6f 20 74 20 74 20 66 20 66 20 73 20  .} {o t t f f s 
3830: 73 20 65 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c  s e}.do_test wal
3840: 33 2d 36 2e 31 2e 37 20 7b 0a 20 20 64 62 32 20  3-6.1.7 {.  db2 
3850: 65 76 61 6c 20 7b 20 50 52 41 47 4d 41 20 77 61  eval { PRAGMA wa
3860: 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20 7d 0a 20  l_checkpoint }. 
3870: 20 73 65 74 20 73 7a 31 20 5b 66 69 6c 65 20 73   set sz1 [file s
3880: 69 7a 65 20 74 65 73 74 2e 64 62 2d 77 61 6c 5d  ize test.db-wal]
3890: 0a 20 20 64 62 32 20 65 76 61 6c 20 7b 20 49 4e  .  db2 eval { IN
38a0: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
38b0: 55 45 53 28 27 6e 27 2c 20 27 74 27 29 20 7d 0a  UES('n', 't') }.
38c0: 20 20 73 65 74 20 73 7a 32 20 5b 66 69 6c 65 20    set sz2 [file 
38d0: 73 69 7a 65 20 74 65 73 74 2e 64 62 2d 77 61 6c  size test.db-wal
38e0: 5d 0a 20 20 65 78 70 72 20 7b 24 73 7a 32 3d 3d  ].  expr {$sz2==
38f0: 24 73 7a 31 7d 0a 7d 20 7b 31 7d 0a 0a 64 62 33  $sz1}.} {1}..db3
3900: 20 63 6c 6f 73 65 0a 64 62 32 20 63 6c 6f 73 65   close.db2 close
3910: 0a 64 62 20 63 6c 6f 73 65 0a 0a 64 6f 5f 74 65  .db close..do_te
3920: 73 74 20 77 61 6c 33 2d 36 2e 32 2e 31 20 7b 0a  st wal3-6.2.1 {.
3930: 20 20 66 6f 72 63 65 64 65 6c 65 74 65 20 74 65    forcedelete te
3940: 73 74 2e 64 62 20 74 65 73 74 2e 64 62 2d 6a 6f  st.db test.db-jo
3950: 75 72 6e 61 6c 20 74 65 73 74 2e 64 62 20 77 61  urnal test.db wa
3960: 6c 0a 20 20 73 71 6c 69 74 65 33 20 64 62 20 74  l.  sqlite3 db t
3970: 65 73 74 2e 64 62 0a 20 20 73 71 6c 69 74 65 33  est.db.  sqlite3
3980: 20 64 62 32 20 74 65 73 74 2e 64 62 0a 20 20 65   db2 test.db.  e
3990: 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20  xecsql { PRAGMA 
39a0: 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 6f 66  auto_vacuum = of
39b0: 66 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  f }.  execsql { 
39c0: 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d  PRAGMA journal_m
39d0: 6f 64 65 20 3d 20 57 41 4c 20 7d 0a 20 20 65 78  ode = WAL }.  ex
39e0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
39f0: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62  TE TABLE t1(a, b
3a00: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
3a10: 54 4f 20 74 31 20 56 41 4c 55 45 53 28 27 68 27  TO t1 VALUES('h'
3a20: 2c 20 27 68 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'h');.    INSE
3a30: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
3a40: 53 28 27 6c 27 2c 20 27 62 27 29 3b 0a 20 20 7d  S('l', 'b');.  }
3a50: 0a 7d 20 7b 7d 0a 0a 54 20 66 69 6c 74 65 72 20  .} {}..T filter 
3a60: 78 53 68 6d 4c 6f 63 6b 0a 54 20 73 63 72 69 70  xShmLock.T scrip
3a70: 74 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 0a  t lock_callback.
3a80: 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61  proc lock_callba
3a90: 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 20  ck {method file 
3aa0: 68 61 6e 64 6c 65 20 73 70 65 63 7d 20 7b 0a 20  handle spec} {. 
3ab0: 20 69 66 20 7b 24 73 70 65 63 20 3d 3d 20 22 33   if {$spec == "3
3ac0: 20 31 20 75 6e 6c 6f 63 6b 20 65 78 63 6c 75 73   1 unlock exclus
3ad0: 69 76 65 22 7d 20 7b 0a 20 20 20 20 54 20 66 69  ive"} {.    T fi
3ae0: 6c 74 65 72 20 7b 7d 0a 20 20 20 20 73 65 74 20  lter {}.    set 
3af0: 3a 3a 52 20 5b 64 62 32 20 65 76 61 6c 20 7b 0a  ::R [db2 eval {.
3b00: 20 20 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20        BEGIN;.   
3b10: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
3b20: 20 74 31 3b 0a 20 20 20 20 7d 5d 0a 20 20 7d 0a   t1;.    }].  }.
3b30: 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36  }.do_test wal3-6
3b40: 2e 32 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  .2.2 {.  execsql
3b50: 20 7b 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68   { PRAGMA wal_ch
3b60: 65 63 6b 70 6f 69 6e 74 20 7d 0a 7d 20 7b 30 20  eckpoint }.} {0 
3b70: 34 20 34 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c  4 4}.do_test wal
3b80: 33 2d 36 2e 32 2e 33 20 7b 0a 20 20 73 65 74 20  3-6.2.3 {.  set 
3b90: 3a 3a 52 0a 7d 20 7b 68 20 68 20 6c 20 62 7d 0a  ::R.} {h h l b}.
3ba0: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 32  do_test wal3-6.2
3bb0: 2e 34 20 7b 0a 20 20 73 65 74 20 73 7a 31 20 5b  .4 {.  set sz1 [
3bc0: 66 69 6c 65 20 73 69 7a 65 20 74 65 73 74 2e 64  file size test.d
3bd0: 62 2d 77 61 6c 5d 0a 20 20 65 78 65 63 73 71 6c  b-wal].  execsql
3be0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74   { INSERT INTO t
3bf0: 31 20 56 41 4c 55 45 53 28 27 62 27 2c 20 27 63  1 VALUES('b', 'c
3c00: 27 29 3b 20 7d 0a 20 20 73 65 74 20 73 7a 32 20  '); }.  set sz2 
3c10: 5b 66 69 6c 65 20 73 69 7a 65 20 74 65 73 74 2e  [file size test.
3c20: 64 62 2d 77 61 6c 5d 0a 20 20 65 78 70 72 20 7b  db-wal].  expr {
3c30: 24 73 7a 32 20 3e 20 24 73 7a 31 7d 0a 7d 20 7b  $sz2 > $sz1}.} {
3c40: 31 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  1}.do_test wal3-
3c50: 36 2e 32 2e 35 20 7b 0a 20 20 64 62 32 20 65 76  6.2.5 {.  db2 ev
3c60: 61 6c 20 7b 20 43 4f 4d 4d 49 54 20 7d 0a 20 20  al { COMMIT }.  
3c70: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
3c80: 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20   wal_checkpoint 
3c90: 7d 0a 20 20 73 65 74 20 73 7a 31 20 5b 66 69 6c  }.  set sz1 [fil
3ca0: 65 20 73 69 7a 65 20 74 65 73 74 2e 64 62 2d 77  e size test.db-w
3cb0: 61 6c 5d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  al].  execsql { 
3cc0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
3cd0: 41 4c 55 45 53 28 27 6e 27 2c 20 27 6f 27 29 3b  ALUES('n', 'o');
3ce0: 20 7d 0a 20 20 73 65 74 20 73 7a 32 20 5b 66 69   }.  set sz2 [fi
3cf0: 6c 65 20 73 69 7a 65 20 74 65 73 74 2e 64 62 2d  le size test.db-
3d00: 77 61 6c 5d 0a 20 20 65 78 70 72 20 7b 24 73 7a  wal].  expr {$sz
3d10: 32 20 3d 3d 20 24 73 7a 31 7d 0a 7d 20 7b 31 7d  2 == $sz1}.} {1}
3d20: 0a 20 0a 64 62 32 20 63 6c 6f 73 65 0a 64 62 20  . .db2 close.db 
3d30: 63 6c 6f 73 65 0a 54 20 64 65 6c 65 74 65 0a 0a  close.T delete..
3d40: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
3d50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 57 68 65  ----------.# Whe
3d90: 6e 20 6f 70 65 6e 69 6e 67 20 61 20 72 65 61 64  n opening a read
3da0: 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20  -transaction on 
3db0: 61 20 64 61 74 61 62 61 73 65 2c 20 69 66 20 74  a database, if t
3dc0: 68 65 20 65 6e 74 69 72 65 20 6c 6f 67 20 68 61  he entire log ha
3dd0: 73 0a 23 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  s.# not yet been
3de0: 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64   copied to the d
3df0: 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
3e00: 65 20 72 65 61 64 65 72 20 67 72 61 62 73 20 61  e reader grabs a
3e10: 20 72 65 61 64 0a 23 20 6c 6f 63 6b 20 6f 6e 20   read.# lock on 
3e20: 61 52 65 61 64 4d 61 72 6b 5b 78 5d 2c 20 77 68  aReadMark[x], wh
3e30: 65 72 65 20 78 3e 30 2e 20 54 68 65 20 66 6f 6c  ere x>0. The fol
3e40: 6c 6f 77 69 6e 67 20 74 65 73 74 20 63 61 73 65  lowing test case
3e50: 73 20 65 78 70 65 72 69 6d 65 6e 74 0a 23 20 77  s experiment.# w
3e60: 69 74 68 20 74 68 65 20 6f 75 74 63 6f 6d 65 20  ith the outcome 
3e70: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
3e80: 3a 0a 23 0a 23 20 20 20 2b 20 54 68 65 20 72 65  :.#.#   + The re
3e90: 61 64 65 72 20 64 69 73 63 6f 76 65 72 69 6e 67  ader discovering
3ea0: 20 74 68 61 74 20 62 65 74 77 65 65 6e 20 74 68   that between th
3eb0: 65 20 74 69 6d 65 20 77 68 65 6e 20 69 74 20 72  e time when it r
3ec0: 65 61 64 20 74 68 65 0a 23 20 20 20 20 20 77 61  ead the.#     wa
3ed0: 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 61  l-index header a
3ee0: 6e 64 20 74 68 65 20 6c 6f 63 6b 20 77 61 73 20  nd the lock was 
3ef0: 6f 62 74 61 69 6e 65 64 20 74 68 61 74 20 61 20  obtained that a 
3f00: 77 72 69 74 65 72 20 68 61 73 20 0a 23 20 20 20  writer has .#   
3f10: 20 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65    written to the
3f20: 20 6c 6f 67 2e 20 49 6e 20 74 68 69 73 20 63 61   log. In this ca
3f30: 73 65 20 74 68 65 20 72 65 61 64 65 72 20 73 68  se the reader sh
3f40: 6f 75 6c 64 20 72 65 2d 72 65 61 64 20 74 68 65  ould re-read the
3f50: 20 0a 23 20 20 20 20 20 77 61 6c 2d 69 6e 64 65   .#     wal-inde
3f60: 78 20 68 65 61 64 65 72 20 61 6e 64 20 6c 6f 63  x header and loc
3f70: 6b 20 61 20 73 6e 61 70 73 68 6f 74 20 63 6f 72  k a snapshot cor
3f80: 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
3f90: 65 20 6e 65 77 20 0a 23 20 20 20 20 20 68 65 61  e new .#     hea
3fa0: 64 65 72 2e 0a 23 0a 23 20 20 20 2b 20 54 68 65  der..#.#   + The
3fb0: 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 61 52   value in the aR
3fc0: 65 61 64 4d 61 72 6b 5b 78 5d 20 73 6c 6f 74 20  eadMark[x] slot 
3fd0: 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
3fe0: 64 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a 23  d since it was.#
3ff0: 20 20 20 20 20 72 65 61 64 2e 0a 23 0a 63 61 74       read..#.cat
4000: 63 68 20 7b 64 62 20 63 6c 6f 73 65 7d 0a 74 65  ch {db close}.te
4010: 73 74 76 66 73 20 54 20 2d 64 65 66 61 75 6c 74  stvfs T -default
4020: 20 31 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d   1.do_test wal3-
4030: 37 2e 31 2e 31 20 7b 0a 20 20 66 6f 72 63 65 64  7.1.1 {.  forced
4040: 65 6c 65 74 65 20 74 65 73 74 2e 64 62 20 74 65  elete test.db te
4050: 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20 74 65  st.db-journal te
4060: 73 74 2e 64 62 20 77 61 6c 0a 20 20 73 71 6c 69  st.db wal.  sqli
4070: 74 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a 20  te3 db test.db. 
4080: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
4090: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
40a0: 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 43 52  de = WAL;.    CR
40b0: 45 41 54 45 20 54 41 42 4c 45 20 62 6c 75 65 28  EATE TABLE blue(
40c0: 72 65 64 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  red PRIMARY KEY,
40d0: 20 67 72 65 65 6e 29 3b 0a 20 20 7d 0a 7d 20 7b   green);.  }.} {
40e0: 77 61 6c 7d 0a 0a 54 20 73 63 72 69 70 74 20 6d  wal}..T script m
40f0: 65 74 68 6f 64 5f 63 61 6c 6c 62 61 63 6b 0a 54  ethod_callback.T
4100: 20 66 69 6c 74 65 72 20 78 4f 70 65 6e 0a 70 72   filter xOpen.pr
4110: 6f 63 20 6d 65 74 68 6f 64 5f 63 61 6c 6c 62 61  oc method_callba
4120: 63 6b 20 7b 6d 65 74 68 6f 64 20 61 72 67 73 7d  ck {method args}
4130: 20 7b 0a 20 20 69 66 20 7b 24 6d 65 74 68 6f 64   {.  if {$method
4140: 20 3d 3d 20 22 78 4f 70 65 6e 22 7d 20 7b 20 72   == "xOpen"} { r
4150: 65 74 75 72 6e 20 22 72 65 61 64 65 72 22 20 7d  eturn "reader" }
4160: 0a 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .}.do_test wal3-
4170: 37 2e 31 2e 32 20 7b 0a 20 20 73 71 6c 69 74 65  7.1.2 {.  sqlite
4180: 33 20 64 62 32 20 74 65 73 74 2e 64 62 0a 20 20  3 db2 test.db.  
4190: 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
41a0: 20 2a 20 46 52 4f 4d 20 62 6c 75 65 20 7d 20 64   * FROM blue } d
41b0: 62 32 0a 7d 20 7b 7d 0a 0a 54 20 66 69 6c 74 65  b2.} {}..T filte
41c0: 72 20 78 53 68 6d 4c 6f 63 6b 0a 73 65 74 20 3a  r xShmLock.set :
41d0: 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a 70 72  :locks [list].pr
41e0: 6f 63 20 6d 65 74 68 6f 64 5f 63 61 6c 6c 62 61  oc method_callba
41f0: 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 20  ck {method file 
4200: 68 61 6e 64 6c 65 20 73 70 65 63 7d 20 7b 0a 20  handle spec} {. 
4210: 20 69 66 20 7b 24 68 61 6e 64 6c 65 20 21 3d 20   if {$handle != 
4220: 22 72 65 61 64 65 72 22 20 7d 20 7b 20 72 65 74  "reader" } { ret
4230: 75 72 6e 20 7d 0a 20 20 69 66 20 7b 24 6d 65 74  urn }.  if {$met
4240: 68 6f 64 20 3d 3d 20 22 78 53 68 6d 4c 6f 63 6b  hod == "xShmLock
4250: 22 7d 20 7b 0a 20 20 20 20 63 61 74 63 68 20 7b  "} {.    catch {
4260: 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52   execsql { INSER
4270: 54 20 49 4e 54 4f 20 62 6c 75 65 20 56 41 4c 55  T INTO blue VALU
4280: 45 53 28 31 2c 20 32 29 20 7d 20 7d 0a 20 20 20  ES(1, 2) } }.   
4290: 20 63 61 74 63 68 20 7b 20 65 78 65 63 73 71 6c   catch { execsql
42a0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 62   { INSERT INTO b
42b0: 6c 75 65 20 56 41 4c 55 45 53 28 33 2c 20 34 29  lue VALUES(3, 4)
42c0: 20 7d 20 7d 0a 20 20 7d 0a 20 20 6c 61 70 70 65   } }.  }.  lappe
42d0: 6e 64 20 3a 3a 6c 6f 63 6b 73 20 24 73 70 65 63  nd ::locks $spec
42e0: 0a 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .}.do_test wal3-
42f0: 37 2e 31 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  7.1.3 {.  execsq
4300: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
4310: 4d 20 62 6c 75 65 20 7d 20 64 62 32 0a 7d 20 7b  M blue } db2.} {
4320: 31 20 32 20 33 20 34 7d 0a 64 6f 5f 74 65 73 74  1 2 3 4}.do_test
4330: 20 77 61 6c 33 2d 37 2e 31 2e 34 20 7b 0a 20 20   wal3-7.1.4 {.  
4340: 73 65 74 20 3a 3a 6c 6f 63 6b 73 0a 7d 20 7b 7b  set ::locks.} {{
4350: 34 20 31 20 6c 6f 63 6b 20 73 68 61 72 65 64 7d  4 1 lock shared}
4360: 20 7b 34 20 31 20 75 6e 6c 6f 63 6b 20 73 68 61   {4 1 unlock sha
4370: 72 65 64 7d 20 7b 35 20 31 20 6c 6f 63 6b 20 73  red} {5 1 lock s
4380: 68 61 72 65 64 7d 20 7b 35 20 31 20 75 6e 6c 6f  hared} {5 1 unlo
4390: 63 6b 20 73 68 61 72 65 64 7d 7d 0a 0a 73 65 74  ck shared}}..set
43a0: 20 3a 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a   ::locks [list].
43b0: 70 72 6f 63 20 6d 65 74 68 6f 64 5f 63 61 6c 6c  proc method_call
43c0: 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c  back {method fil
43d0: 65 20 68 61 6e 64 6c 65 20 73 70 65 63 7d 20 7b  e handle spec} {
43e0: 0a 20 20 69 66 20 7b 24 68 61 6e 64 6c 65 20 21  .  if {$handle !
43f0: 3d 20 22 72 65 61 64 65 72 22 20 7d 20 7b 20 72  = "reader" } { r
4400: 65 74 75 72 6e 20 7d 0a 20 20 69 66 20 7b 24 6d  eturn }.  if {$m
4410: 65 74 68 6f 64 20 3d 3d 20 22 78 53 68 6d 4c 6f  ethod == "xShmLo
4420: 63 6b 22 7d 20 7b 0a 20 20 20 20 63 61 74 63 68  ck"} {.    catch
4430: 20 7b 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53   { execsql { INS
4440: 45 52 54 20 49 4e 54 4f 20 62 6c 75 65 20 56 41  ERT INTO blue VA
4450: 4c 55 45 53 28 35 2c 20 36 29 20 7d 20 7d 0a 20  LUES(5, 6) } }. 
4460: 20 7d 0a 20 20 6c 61 70 70 65 6e 64 20 3a 3a 6c   }.  lappend ::l
4470: 6f 63 6b 73 20 24 73 70 65 63 0a 7d 0a 64 6f 5f  ocks $spec.}.do_
4480: 74 65 73 74 20 77 61 6c 33 2d 37 2e 32 2e 31 20  test wal3-7.2.1 
4490: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  {.  execsql { SE
44a0: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 62 6c 75 65  LECT * FROM blue
44b0: 20 7d 20 64 62 32 0a 7d 20 7b 31 20 32 20 33 20   } db2.} {1 2 3 
44c0: 34 20 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20 77  4 5 6}.do_test w
44d0: 61 6c 33 2d 37 2e 32 2e 32 20 7b 0a 20 20 73 65  al3-7.2.2 {.  se
44e0: 74 20 3a 3a 6c 6f 63 6b 73 0a 7d 20 7b 7b 35 20  t ::locks.} {{5 
44f0: 31 20 6c 6f 63 6b 20 73 68 61 72 65 64 7d 20 7b  1 lock shared} {
4500: 35 20 31 20 75 6e 6c 6f 63 6b 20 73 68 61 72 65  5 1 unlock share
4510: 64 7d 20 7b 34 20 31 20 6c 6f 63 6b 20 73 68 61  d} {4 1 lock sha
4520: 72 65 64 7d 20 7b 34 20 31 20 75 6e 6c 6f 63 6b  red} {4 1 unlock
4530: 20 73 68 61 72 65 64 7d 7d 0a 0a 64 62 20 63 6c   shared}}..db cl
4540: 6f 73 65 0a 64 62 32 20 63 6c 6f 73 65 0a 54 20  ose.db2 close.T 
4550: 64 65 6c 65 74 65 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  delete..#-------
4560: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4570: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4580: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4590: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
45a0: 2d 2d 0a 23 20 0a 64 6f 5f 74 65 73 74 20 77 61  --.# .do_test wa
45b0: 6c 33 2d 38 2e 31 20 7b 0a 20 20 66 6f 72 63 65  l3-8.1 {.  force
45c0: 64 65 6c 65 74 65 20 74 65 73 74 2e 64 62 20 74  delete test.db t
45d0: 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20 74  est.db-journal t
45e0: 65 73 74 2e 64 62 20 77 61 6c 0a 20 20 73 71 6c  est.db wal.  sql
45f0: 69 74 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a  ite3 db test.db.
4600: 20 20 73 71 6c 69 74 65 33 20 64 62 32 20 74 65    sqlite3 db2 te
4610: 73 74 2e 64 62 0a 20 20 65 78 65 63 73 71 6c 20  st.db.  execsql 
4620: 7b 0a 20 20 20 20 50 52 41 47 4d 41 20 61 75 74  {.    PRAGMA aut
4630: 6f 5f 76 61 63 75 75 6d 20 3d 20 6f 66 66 3b 0a  o_vacuum = off;.
4640: 20 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e      PRAGMA journ
4650: 61 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20  al_mode = WAL;. 
4660: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
4670: 62 28 63 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  b(c);.    INSERT
4680: 20 49 4e 54 4f 20 62 20 56 41 4c 55 45 53 28 27   INTO b VALUES('
4690: 54 65 68 72 61 6e 27 29 3b 0a 20 20 20 20 49 4e  Tehran');.    IN
46a0: 53 45 52 54 20 49 4e 54 4f 20 62 20 56 41 4c 55  SERT INTO b VALU
46b0: 45 53 28 27 51 6f 6d 27 29 3b 0a 20 20 20 20 49  ES('Qom');.    I
46c0: 4e 53 45 52 54 20 49 4e 54 4f 20 62 20 56 41 4c  NSERT INTO b VAL
46d0: 55 45 53 28 27 4d 61 72 6b 61 7a 69 27 29 3b 0a  UES('Markazi');.
46e0: 20 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63      PRAGMA wal_c
46f0: 68 65 63 6b 70 6f 69 6e 74 3b 0a 20 20 7d 0a 7d  heckpoint;.  }.}
4700: 20 7b 77 61 6c 20 30 20 35 20 35 7d 0a 64 6f 5f   {wal 0 5 5}.do_
4710: 74 65 73 74 20 77 61 6c 33 2d 38 2e 32 20 7b 0a  test wal3-8.2 {.
4720: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
4730: 43 54 20 2a 20 46 52 4f 4d 20 62 20 7d 0a 7d 20  CT * FROM b }.} 
4740: 7b 54 65 68 72 61 6e 20 51 6f 6d 20 4d 61 72 6b  {Tehran Qom Mark
4750: 61 7a 69 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c  azi}.do_test wal
4760: 33 2d 38 2e 33 20 7b 0a 20 20 64 62 20 65 76 61  3-8.3 {.  db eva
4770: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
4780: 4d 20 62 20 7d 20 7b 0a 20 20 20 20 64 62 20 65  M b } {.    db e
4790: 76 61 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  val { INSERT INT
47a0: 4f 20 62 20 56 41 4c 55 45 53 28 27 51 61 7a 76  O b VALUES('Qazv
47b0: 69 6e 27 29 20 7d 0a 20 20 20 20 73 65 74 20 72  in') }.    set r
47c0: 20 5b 64 62 32 20 65 76 61 6c 20 7b 20 53 45 4c   [db2 eval { SEL
47d0: 45 43 54 20 2a 20 46 52 4f 4d 20 62 20 7d 5d 0a  ECT * FROM b }].
47e0: 20 20 20 20 62 72 65 61 6b 0a 20 20 7d 0a 20 20      break.  }.  
47f0: 73 65 74 20 72 0a 7d 20 7b 54 65 68 72 61 6e 20  set r.} {Tehran 
4800: 51 6f 6d 20 4d 61 72 6b 61 7a 69 20 51 61 7a 76  Qom Markazi Qazv
4810: 69 6e 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33  in}.do_test wal3
4820: 2d 38 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  -8.4 {.  execsql
4830: 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e   {.    INSERT IN
4840: 54 4f 20 62 20 56 41 4c 55 45 53 28 27 47 69 6c  TO b VALUES('Gil
4850: 61 6e 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  an');.    INSERT
4860: 20 49 4e 54 4f 20 62 20 56 41 4c 55 45 53 28 27   INTO b VALUES('
4870: 41 72 64 61 62 69 6c 27 29 3b 0a 20 20 7d 0a 7d  Ardabil');.  }.}
4880: 20 7b 7d 0a 64 62 32 20 63 6c 6f 73 65 0a 0a 66   {}.db2 close..f
4890: 61 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64  aultsim_save_and
48a0: 5f 63 6c 6f 73 65 0a 74 65 73 74 76 66 73 20 54  _close.testvfs T
48b0: 20 2d 64 65 66 61 75 6c 74 20 31 0a 66 61 75 6c   -default 1.faul
48c0: 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64  tsim_restore_and
48d0: 5f 72 65 6f 70 65 6e 0a 54 20 66 69 6c 74 65 72  _reopen.T filter
48e0: 20 78 53 68 6d 4c 6f 63 6b 0a 54 20 73 63 72 69   xShmLock.T scri
48f0: 70 74 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b  pt lock_callback
4900: 0a 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c  ..proc lock_call
4910: 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c  back {method fil
4920: 65 20 68 61 6e 64 6c 65 20 73 70 65 63 7d 20 7b  e handle spec} {
4930: 0a 20 20 69 66 20 7b 24 73 70 65 63 20 3d 3d 20  .  if {$spec == 
4940: 22 34 20 31 20 75 6e 6c 6f 63 6b 20 65 78 63 6c  "4 1 unlock excl
4950: 75 73 69 76 65 22 7d 20 7b 0a 20 20 20 20 54 20  usive"} {.    T 
4960: 66 69 6c 74 65 72 20 7b 7d 0a 20 20 20 20 73 65  filter {}.    se
4970: 74 20 3a 3a 72 20 5b 63 61 74 63 68 73 71 6c 20  t ::r [catchsql 
4980: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
4990: 62 20 7d 20 64 62 32 5d 0a 20 20 7d 0a 7d 0a 73  b } db2].  }.}.s
49a0: 71 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e 64  qlite3 db test.d
49b0: 62 0a 73 71 6c 69 74 65 33 20 64 62 32 20 74 65  b.sqlite3 db2 te
49c0: 73 74 2e 64 62 0a 64 6f 5f 74 65 73 74 20 77 61  st.db.do_test wa
49d0: 6c 33 2d 38 2e 35 20 7b 0a 20 20 65 78 65 63 73  l3-8.5 {.  execs
49e0: 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52  ql { SELECT * FR
49f0: 4f 4d 20 62 20 7d 0a 7d 20 7b 54 65 68 72 61 6e  OM b }.} {Tehran
4a00: 20 51 6f 6d 20 4d 61 72 6b 61 7a 69 20 51 61 7a   Qom Markazi Qaz
4a10: 76 69 6e 20 47 69 6c 61 6e 20 41 72 64 61 62 69  vin Gilan Ardabi
4a20: 6c 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  l}.do_test wal3-
4a30: 38 2e 36 20 7b 0a 20 20 73 65 74 20 3a 3a 72 0a  8.6 {.  set ::r.
4a40: 7d 20 7b 31 20 7b 6c 6f 63 6b 69 6e 67 20 70 72  } {1 {locking pr
4a50: 6f 74 6f 63 6f 6c 7d 7d 0a 0a 64 62 20 63 6c 6f  otocol}}..db clo
4a60: 73 65 0a 64 62 32 20 63 6c 6f 73 65 0a 0a 66 61  se.db2 close..fa
4a70: 75 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61  ultsim_restore_a
4a80: 6e 64 5f 72 65 6f 70 65 6e 0a 73 71 6c 69 74 65  nd_reopen.sqlite
4a90: 33 20 64 62 32 20 74 65 73 74 2e 64 62 0a 54 20  3 db2 test.db.T 
4aa0: 66 69 6c 74 65 72 20 78 53 68 6d 4c 6f 63 6b 0a  filter xShmLock.
4ab0: 54 20 73 63 72 69 70 74 20 6c 6f 63 6b 5f 63 61  T script lock_ca
4ac0: 6c 6c 62 61 63 6b 0a 70 72 6f 63 20 6c 6f 63 6b  llback.proc lock
4ad0: 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65 74 68 6f  _callback {metho
4ae0: 64 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 73 70  d file handle sp
4af0: 65 63 7d 20 7b 0a 20 20 69 66 20 7b 24 73 70 65  ec} {.  if {$spe
4b00: 63 20 3d 3d 20 22 31 20 37 20 75 6e 6c 6f 63 6b  c == "1 7 unlock
4b10: 20 65 78 63 6c 75 73 69 76 65 22 7d 20 7b 0a 20   exclusive"} {. 
4b20: 20 20 20 54 20 66 69 6c 74 65 72 20 7b 7d 0a 20     T filter {}. 
4b30: 20 20 20 73 65 74 20 3a 3a 72 20 5b 63 61 74 63     set ::r [catc
4b40: 68 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  hsql { SELECT * 
4b50: 46 52 4f 4d 20 62 20 7d 20 64 62 32 5d 0a 20 20  FROM b } db2].  
4b60: 7d 0a 7d 0a 75 6e 73 65 74 20 3a 3a 72 0a 64 6f  }.}.unset ::r.do
4b70: 5f 74 65 73 74 20 77 61 6c 33 2d 38 2e 35 20 7b  _test wal3-8.5 {
4b80: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c  .  execsql { SEL
4b90: 45 43 54 20 2a 20 46 52 4f 4d 20 62 20 7d 0a 7d  ECT * FROM b }.}
4ba0: 20 7b 54 65 68 72 61 6e 20 51 6f 6d 20 4d 61 72   {Tehran Qom Mar
4bb0: 6b 61 7a 69 20 51 61 7a 76 69 6e 20 47 69 6c 61  kazi Qazvin Gila
4bc0: 6e 20 41 72 64 61 62 69 6c 7d 0a 64 6f 5f 74 65  n Ardabil}.do_te
4bd0: 73 74 20 77 61 6c 33 2d 38 2e 36 20 7b 0a 20 20  st wal3-8.6 {.  
4be0: 73 65 74 20 3a 3a 72 0a 7d 20 7b 31 20 7b 6c 6f  set ::r.} {1 {lo
4bf0: 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 7d 7d  cking protocol}}
4c00: 0a 0a 64 62 20 63 6c 6f 73 65 0a 64 62 32 20 63  ..db close.db2 c
4c10: 6c 6f 73 65 0a 54 20 64 65 6c 65 74 65 0a 0a 23  lose.T delete..#
4c20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4c30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4c40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4c50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4c60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 57 68 65 6e  ---------.# When
4c70: 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70   a connection op
4c80: 65 6e 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  ens a read-lock 
4c90: 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  on the database,
4ca0: 20 69 74 20 73 65 61 72 63 68 65 73 20 66 6f 72   it searches for
4cb0: 0a 23 20 61 6e 20 61 52 65 61 64 4d 61 72 6b 5b  .# an aReadMark[
4cc0: 5d 20 73 6c 6f 74 20 74 68 61 74 20 69 73 20 61  ] slot that is a
4cd0: 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20 74 68  lready set to th
4ce0: 65 20 6d 78 46 72 61 6d 65 20 76 61 6c 75 65 20  e mxFrame value 
4cf0: 66 6f 72 20 74 68 65 0a 23 20 6e 65 77 20 74 72  for the.# new tr
4d00: 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74  ansaction. If it
4d10: 20 63 61 6e 6e 6f 74 20 66 69 6e 64 20 6f 6e 65   cannot find one
4d20: 2c 20 69 74 20 61 74 74 65 6d 70 74 73 20 74 6f  , it attempts to
4d30: 20 6f 62 74 61 69 6e 20 61 6e 20 0a 23 20 65 78   obtain an .# ex
4d40: 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20  clusive lock on 
4d50: 61 6e 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 73  an aReadMark[] s
4d60: 6c 6f 74 20 66 6f 72 20 74 68 65 20 70 75 72 70  lot for the purp
4d70: 6f 73 65 73 20 6f 66 20 6d 6f 64 69 66 79 69 6e  oses of modifyin
4d80: 67 0a 23 20 74 68 65 20 76 61 6c 75 65 2c 20 74  g.# the value, t
4d90: 68 65 6e 20 64 72 6f 70 73 20 62 61 63 6b 20 74  hen drops back t
4da0: 6f 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20  o a shared-lock 
4db0: 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e  for the duration
4dc0: 20 6f 66 20 74 68 65 0a 23 20 74 72 61 6e 73 61   of the.# transa
4dd0: 63 74 69 6f 6e 2e 0a 23 0a 23 20 54 68 69 73 20  ction..#.# This 
4de0: 74 65 73 74 20 63 61 73 65 20 76 65 72 69 66 69  test case verifi
4df0: 65 73 20 74 68 61 74 20 69 66 20 61 6e 20 65 78  es that if an ex
4e00: 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 63 61 6e  clusive lock can
4e10: 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 0a  not be obtained.
4e20: 23 20 6f 6e 20 61 6e 79 20 61 52 65 61 64 4d 61  # on any aReadMa
4e30: 72 6b 5b 5d 20 73 6c 6f 74 20 28 62 65 63 61 75  rk[] slot (becau
4e40: 73 65 20 74 68 65 72 65 20 61 72 65 20 61 6c 72  se there are alr
4e50: 65 61 64 79 20 73 65 76 65 72 61 6c 20 72 65 61  eady several rea
4e60: 64 65 72 73 29 2c 0a 23 20 74 68 65 20 63 6c 69  ders),.# the cli
4e70: 65 6e 74 20 74 61 6b 65 73 20 61 20 73 68 61 72  ent takes a shar
4e80: 65 64 2d 6c 6f 63 6b 20 6f 6e 20 61 20 73 6c 6f  ed-lock on a slo
4e90: 74 20 77 69 74 68 6f 75 74 20 6d 6f 64 69 66 79  t without modify
4ea0: 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 23 20  ing the value.# 
4eb0: 61 6e 64 20 63 6f 6e 74 69 6e 75 65 73 2e 0a 23  and continues..#
4ec0: 0a 73 65 74 20 6e 43 6f 6e 6e 20 35 30 0a 69 66  .set nConn 50.if
4ed0: 20 7b 20 5b 73 74 72 69 6e 67 20 6d 61 74 63 68   { [string match
4ee0: 20 2a 42 53 44 20 24 74 63 6c 5f 70 6c 61 74 66   *BSD $tcl_platf
4ef0: 6f 72 6d 28 6f 73 29 5d 20 7d 20 7b 20 73 65 74  orm(os)] } { set
4f00: 20 6e 43 6f 6e 6e 20 32 35 20 7d 0a 64 6f 5f 74   nConn 25 }.do_t
4f10: 65 73 74 20 77 61 6c 33 2d 39 2e 30 20 7b 0a 20  est wal3-9.0 {. 
4f20: 20 66 6f 72 63 65 64 65 6c 65 74 65 20 74 65 73   forcedelete tes
4f30: 74 2e 64 62 20 74 65 73 74 2e 64 62 2d 6a 6f 75  t.db test.db-jou
4f40: 72 6e 61 6c 20 74 65 73 74 2e 64 62 20 77 61 6c  rnal test.db wal
4f50: 0a 20 20 73 71 6c 69 74 65 33 20 64 62 20 74 65  .  sqlite3 db te
4f60: 73 74 2e 64 62 0a 20 20 65 78 65 63 73 71 6c 20  st.db.  execsql 
4f70: 7b 0a 20 20 20 20 50 52 41 47 4d 41 20 70 61 67  {.    PRAGMA pag
4f80: 65 5f 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20  e_size = 1024;. 
4f90: 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61     PRAGMA journa
4fa0: 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20  l_mode = WAL;.  
4fb0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 77    CREATE TABLE w
4fc0: 68 6f 61 6d 69 28 78 29 3b 0a 20 20 20 20 49 4e  hoami(x);.    IN
4fd0: 53 45 52 54 20 49 4e 54 4f 20 77 68 6f 61 6d 69  SERT INTO whoami
4fe0: 20 56 41 4c 55 45 53 28 27 6e 6f 62 6f 64 79 27   VALUES('nobody'
4ff0: 29 3b 0a 20 20 7d 0a 7d 20 7b 77 61 6c 7d 0a 66  );.  }.} {wal}.f
5000: 6f 72 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69  or {set i 0} {$i
5010: 20 3c 20 24 6e 43 6f 6e 6e 7d 20 7b 69 6e 63 72   < $nConn} {incr
5020: 20 69 7d 20 7b 0a 20 20 73 65 74 20 63 20 64 62   i} {.  set c db
5030: 24 69 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c  $i.  do_test wal
5040: 33 2d 39 2e 31 2e 24 69 20 7b 0a 20 20 20 20 73  3-9.1.$i {.    s
5050: 71 6c 69 74 65 33 20 24 63 20 74 65 73 74 2e 64  qlite3 $c test.d
5060: 62 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20  b.    execsql { 
5070: 55 50 44 41 54 45 20 77 68 6f 61 6d 69 20 53 45  UPDATE whoami SE
5080: 54 20 78 20 3d 20 24 63 20 7d 0a 20 20 20 20 65  T x = $c }.    e
5090: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 42  xecsql {.      B
50a0: 45 47 49 4e 3b 0a 20 20 20 20 20 20 53 45 4c 45  EGIN;.      SELE
50b0: 43 54 20 2a 20 46 52 4f 4d 20 77 68 6f 61 6d 69  CT * FROM whoami
50c0: 0a 20 20 20 20 7d 20 24 63 0a 20 20 7d 20 24 63  .    } $c.  } $c
50d0: 0a 7d 0a 66 6f 72 20 7b 73 65 74 20 69 20 30 7d  .}.for {set i 0}
50e0: 20 7b 24 69 20 3c 20 24 6e 43 6f 6e 6e 7d 20 7b   {$i < $nConn} {
50f0: 69 6e 63 72 20 69 7d 20 7b 0a 20 20 73 65 74 20  incr i} {.  set 
5100: 63 20 64 62 24 69 0a 20 20 64 6f 5f 74 65 73 74  c db$i.  do_test
5110: 20 77 61 6c 33 2d 39 2e 32 2e 24 69 20 7b 0a 20   wal3-9.2.$i {. 
5120: 20 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c     execsql { SEL
5130: 45 43 54 20 2a 20 46 52 4f 4d 20 77 68 6f 61 6d  ECT * FROM whoam
5140: 69 20 7d 20 24 63 0a 20 20 7d 20 24 63 0a 7d 0a  i } $c.  } $c.}.
5150: 0a 73 65 74 20 73 7a 20 5b 65 78 70 72 20 31 30  .set sz [expr 10
5160: 32 34 20 2a 20 28 32 2b 24 41 55 54 4f 56 41 43  24 * (2+$AUTOVAC
5170: 55 55 4d 29 5d 0a 64 6f 5f 74 65 73 74 20 77 61  UUM)].do_test wa
5180: 6c 33 2d 39 2e 33 20 7b 0a 20 20 66 6f 72 20 7b  l3-9.3 {.  for {
5190: 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 28  set i 0} {$i < (
51a0: 24 6e 43 6f 6e 6e 2d 31 29 7d 20 7b 69 6e 63 72  $nConn-1)} {incr
51b0: 20 69 7d 20 7b 20 64 62 24 69 20 63 6c 6f 73 65   i} { db$i close
51c0: 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50   }.  execsql { P
51d0: 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70  RAGMA wal_checkp
51e0: 6f 69 6e 74 20 7d 20 0a 20 20 62 79 74 65 5f 69  oint } .  byte_i
51f0: 73 5f 7a 65 72 6f 20 74 65 73 74 2e 64 62 20 5b  s_zero test.db [
5200: 65 78 70 72 20 24 73 7a 2d 31 30 32 34 5d 0a 7d  expr $sz-1024].}
5210: 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c   {1}.do_test wal
5220: 33 2d 39 2e 34 20 7b 0a 20 20 64 62 5b 65 78 70  3-9.4 {.  db[exp
5230: 72 20 24 6e 43 6f 6e 6e 2d 31 5d 20 63 6c 6f 73  r $nConn-1] clos
5240: 65 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52  e.  execsql { PR
5250: 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f  AGMA wal_checkpo
5260: 69 6e 74 20 7d 20 0a 20 20 73 65 74 20 73 7a 32  int } .  set sz2
5270: 20 5b 66 69 6c 65 20 73 69 7a 65 20 74 65 73 74   [file size test
5280: 2e 64 62 5d 0a 20 20 62 79 74 65 5f 69 73 5f 7a  .db].  byte_is_z
5290: 65 72 6f 20 74 65 73 74 2e 64 62 20 5b 65 78 70  ero test.db [exp
52a0: 72 20 24 73 7a 2d 31 30 32 34 5d 0a 7d 20 7b 30  r $sz-1024].} {0
52b0: 7d 0a 0a 64 6f 5f 6d 75 6c 74 69 63 6c 69 65 6e  }..do_multiclien
52c0: 74 5f 74 65 73 74 20 74 6e 20 7b 0a 20 20 64 6f  t_test tn {.  do
52d0: 5f 74 65 73 74 20 77 61 6c 33 2d 31 30 2e 24 74  _test wal3-10.$t
52e0: 6e 2e 31 20 7b 0a 20 20 20 20 73 71 6c 31 20 7b  n.1 {.    sql1 {
52f0: 0a 20 20 20 20 20 20 50 52 41 47 4d 41 20 70 61  .      PRAGMA pa
5300: 67 65 5f 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a  ge_size = 1024;.
5310: 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42        CREATE TAB
5320: 4c 45 20 74 31 28 78 29 3b 0a 20 20 20 20 20 20  LE t1(x);.      
5330: 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d  PRAGMA journal_m
5340: 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20 20  ode = WAL;.     
5350: 20 50 52 41 47 4d 41 20 77 61 6c 5f 61 75 74 6f   PRAGMA wal_auto
5360: 63 68 65 63 6b 70 6f 69 6e 74 20 3d 20 31 30 30  checkpoint = 100
5370: 30 30 30 3b 0a 20 20 20 20 20 20 42 45 47 49 4e  000;.      BEGIN
5380: 3b 0a 20 20 20 20 20 20 20 20 49 4e 53 45 52 54  ;.        INSERT
5390: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
53a0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 29  randomblob(800))
53b0: 3b 0a 20 20 20 20 20 20 20 20 49 4e 53 45 52 54  ;.        INSERT
53c0: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
53d0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 20  randomblob(800) 
53e0: 46 52 4f 4d 20 74 31 3b 20 20 20 2d 2d 20 32 0a  FROM t1;   -- 2.
53f0: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
5400: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61  NTO t1 SELECT ra
5410: 6e 64 6f 6d 62 6c 6f 62 28 38 30 30 29 20 46 52  ndomblob(800) FR
5420: 4f 4d 20 74 31 3b 20 20 20 2d 2d 20 34 0a 20 20  OM t1;   -- 4.  
5430: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
5440: 4f 20 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64  O t1 SELECT rand
5450: 6f 6d 62 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d  omblob(800) FROM
5460: 20 74 31 3b 20 20 20 2d 2d 20 38 0a 20 20 20 20   t1;   -- 8.    
5470: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
5480: 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  t1 SELECT random
5490: 62 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74  blob(800) FROM t
54a0: 31 3b 20 20 20 2d 2d 20 31 36 0a 20 20 20 20 20  1;   -- 16.     
54b0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
54c0: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
54d0: 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31  lob(800) FROM t1
54e0: 3b 20 20 20 2d 2d 20 33 32 0a 20 20 20 20 20 20  ;   -- 32.      
54f0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
5500: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
5510: 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b  ob(800) FROM t1;
5520: 20 20 20 2d 2d 20 36 34 0a 20 20 20 20 20 20 20     -- 64.       
5530: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
5540: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
5550: 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(800) FROM t1; 
5560: 20 20 2d 2d 20 31 32 38 0a 20 20 20 20 20 20 20    -- 128.       
5570: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
5580: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
5590: 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(800) FROM t1; 
55a0: 20 20 2d 2d 20 32 35 36 0a 20 20 20 20 20 20 20    -- 256.       
55b0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
55c0: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
55d0: 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(800) FROM t1; 
55e0: 20 20 2d 2d 20 35 31 32 0a 20 20 20 20 20 20 20    -- 512.       
55f0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
5600: 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c 6f  SELECT randomblo
5610: 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20  b(800) FROM t1; 
5620: 20 20 2d 2d 20 31 30 32 34 0a 20 20 20 20 20 20    -- 1024.      
5630: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
5640: 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62 6c   SELECT randombl
5650: 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b  ob(800) FROM t1;
5660: 20 20 20 2d 2d 20 32 30 34 38 0a 20 20 20 20 20     -- 2048.     
5670: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
5680: 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 62  1 SELECT randomb
5690: 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74 31  lob(800) FROM t1
56a0: 3b 20 20 20 2d 2d 20 34 30 39 36 0a 20 20 20 20  ;   -- 4096.    
56b0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
56c0: 74 31 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d  t1 SELECT random
56d0: 62 6c 6f 62 28 38 30 30 29 20 46 52 4f 4d 20 74  blob(800) FROM t
56e0: 31 3b 20 20 20 2d 2d 20 38 31 39 32 0a 20 20 20  1;   -- 8192.   
56f0: 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 20     COMMIT;.     
5700: 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 31   CREATE INDEX i1
5710: 20 4f 4e 20 74 31 28 78 29 3b 0a 20 20 20 20 7d   ON t1(x);.    }
5720: 0a 0a 20 20 20 20 65 78 70 72 20 7b 5b 66 69 6c  ..    expr {[fil
5730: 65 20 73 69 7a 65 20 74 65 73 74 2e 64 62 2d 77  e size test.db-w
5740: 61 6c 5d 20 3e 20 5b 65 78 70 72 20 31 30 33 32  al] > [expr 1032
5750: 2a 39 30 30 30 5d 7d 0a 20 20 7d 20 31 0a 0a 20  *9000]}.  } 1.. 
5760: 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 31 30   do_test wal3-10
5770: 2e 24 74 6e 2e 32 20 7b 0a 20 20 20 20 73 71 6c  .$tn.2 {.    sql
5780: 32 20 7b 50 52 41 47 4d 41 20 69 6e 74 65 67 72  2 {PRAGMA integr
5790: 69 74 79 5f 63 68 65 63 6b 7d 0a 20 20 7d 20 7b  ity_check}.  } {
57a0: 6f 6b 7d 0a 7d 0a 0a 66 69 6e 69 73 68 5f 74 65  ok}.}..finish_te
57b0: 73 74 0a                                         st.