Documentation Source Text

Hex Artifact Content
Login

Artifact ef030bae4c1101c850661215d7b5931064aa55f2:


0000: 3c 74 69 74 6c 65 3e 48 6f 77 20 54 6f 20 43 6f  <title>How To Co
0010: 72 72 75 70 74 20 41 6e 20 53 51 4c 69 74 65 20  rrupt An SQLite 
0020: 44 61 74 61 62 61 73 65 20 46 69 6c 65 3c 2f 74  Database File</t
0030: 69 74 6c 65 3e 0a 3c 74 63 6c 3e 68 64 5f 6b 65  itle>.<tcl>hd_ke
0040: 79 77 6f 72 64 73 20 7b 68 6f 77 20 74 6f 20 63  ywords {how to c
0050: 6f 72 72 75 70 74 7d 3c 2f 74 63 6c 3e 0a 0a 3c  orrupt}</tcl>..<
0060: 68 31 20 61 6c 69 67 6e 3d 63 65 6e 74 65 72 3e  h1 align=center>
0070: 48 6f 77 20 54 6f 20 43 6f 72 72 75 70 74 20 41  How To Corrupt A
0080: 6e 20 53 51 4c 69 74 65 20 44 61 74 61 62 61 73  n SQLite Databas
0090: 65 20 46 69 6c 65 3c 2f 68 31 3e 0a 0a 3c 70 3e  e File</h1>..<p>
00a0: 41 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  An SQLite databa
00b0: 73 65 20 69 73 20 68 69 67 68 6c 79 20 72 65 73  se is highly res
00c0: 69 73 74 61 6e 74 20 74 6f 20 63 6f 72 72 75 70  istant to corrup
00d0: 74 69 6f 6e 2e 0a 49 66 20 61 6e 20 61 70 70 6c  tion..If an appl
00e0: 69 63 61 74 69 6f 6e 20 63 72 61 73 68 2c 20 6f  ication crash, o
00f0: 72 20 61 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73  r an operating-s
0100: 79 73 74 65 6d 20 63 72 61 73 68 2c 20 6f 72 20  ystem crash, or 
0110: 65 76 65 6e 0a 61 20 70 6f 77 65 72 20 66 61 69  even.a power fai
0120: 6c 75 72 65 20 6f 63 63 75 72 73 20 69 6e 20 74  lure occurs in t
0130: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 74  he middle of a t
0140: 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20  ransaction, the 
0150: 70 61 72 74 69 61 6c 6c 79 0a 77 72 69 74 74 65  partially.writte
0160: 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 68  n transaction sh
0170: 6f 75 6c 64 20 62 65 20 61 75 74 6f 6d 61 74 69  ould be automati
0180: 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
0190: 6b 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a  k the next time.
01a0: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
01b0: 65 20 69 73 20 61 63 63 65 73 73 65 64 2e 20 20  e is accessed.  
01c0: 54 68 65 20 72 65 63 6f 76 65 72 79 20 70 72 6f  The recovery pro
01d0: 63 65 73 73 20 69 73 20 66 75 6c 6c 79 0a 61 75  cess is fully.au
01e0: 74 6f 6d 61 74 69 63 20 61 6e 64 20 64 6f 65 73  tomatic and does
01f0: 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 6e 79   not require any
0200: 20 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 70   action on the p
0210: 61 72 74 20 6f 66 20 74 68 65 20 75 73 65 72 0a  art of the user.
0220: 6f 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  or the applicati
0230: 6f 6e 2e 0a 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 6f  on..</p>..<p>Tho
0240: 75 67 68 20 53 51 4c 69 74 65 20 69 73 20 72 65  ugh SQLite is re
0250: 73 69 73 74 61 6e 74 20 74 6f 20 64 61 74 61 62  sistant to datab
0260: 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2c 20  ase corruption, 
0270: 69 74 20 69 73 20 6e 6f 74 20 69 6d 6d 75 6e 65  it is not immune
0280: 2e 0a 54 68 69 73 20 64 6f 63 75 6d 65 6e 74 20  ..This document 
0290: 64 65 73 63 72 69 62 65 73 20 74 68 65 20 76 61  describes the va
02a0: 72 69 6f 75 73 20 77 61 79 73 20 74 68 61 74 20  rious ways that 
02b0: 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  an SQLite databa
02c0: 73 65 20 6d 69 67 68 74 0a 67 6f 20 63 6f 72 72  se might.go corr
02d0: 75 70 74 2e 3c 2f 70 3e 0a 0a 3c 68 32 3e 31 2e  upt.</p>..<h2>1.
02e0: 30 20 46 69 6c 65 20 6f 76 65 72 77 72 69 74 65  0 File overwrite
02f0: 20 62 79 20 61 20 72 6f 67 75 65 20 74 68 72 65   by a rogue thre
0300: 61 64 20 6f 72 20 70 72 6f 63 65 73 73 3c 2f 68  ad or process</h
0310: 32 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 64 61  2>..<p>SQLite da
0320: 74 61 62 61 73 65 20 66 69 6c 65 73 20 61 72 65  tabase files are
0330: 20 6f 72 64 69 6e 61 72 79 20 64 69 73 6b 20 66   ordinary disk f
0340: 69 6c 65 73 2e 0a 54 68 61 74 20 6d 65 61 6e 73  iles..That means
0350: 20 74 68 61 74 20 61 6e 79 20 70 72 6f 63 65 73   that any proces
0360: 73 20 63 61 6e 20 6f 70 65 6e 20 74 68 65 20 66  s can open the f
0370: 69 6c 65 20 61 6e 64 20 0a 6f 76 65 72 77 72 69  ile and .overwri
0380: 74 65 20 69 74 20 77 69 74 68 20 67 61 72 62 61  te it with garba
0390: 67 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  ge.  There is no
03a0: 74 68 69 6e 67 20 74 68 61 74 20 74 68 65 20 53  thing that the S
03b0: 51 4c 69 74 65 0a 6c 69 62 72 61 72 79 20 63 61  QLite.library ca
03c0: 6e 20 64 6f 20 74 6f 20 64 65 66 65 6e 64 20 61  n do to defend a
03d0: 67 61 69 6e 73 74 20 74 68 69 73 2e 3c 2f 70 3e  gainst this.</p>
03e0: 0a 0a 3c 68 33 3e 31 2e 31 20 43 6f 6e 74 69 6e  ..<h3>1.1 Contin
03f0: 75 69 6e 67 20 74 6f 20 75 73 65 20 61 20 66 69  uing to use a fi
0400: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 66  le descriptor af
0410: 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ter it has been 
0420: 63 6c 6f 73 65 64 3c 2f 68 33 3e 0a 0a 3c 70 3e  closed</h3>..<p>
0430: 57 65 20 68 61 76 65 20 73 65 65 6e 20 63 61 73  We have seen cas
0440: 65 73 20 77 68 65 72 65 20 61 20 66 69 6c 65 20  es where a file 
0450: 64 65 73 63 72 69 70 74 6f 72 20 77 61 73 20 6f  descriptor was o
0460: 70 65 6e 20 6f 6e 20 61 20 6c 6f 67 20 66 69 6c  pen on a log fil
0470: 65 2c 0a 74 68 65 6e 20 74 68 61 74 20 66 69 6c  e,.then that fil
0480: 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 61 73  e descriptor was
0490: 20 63 6c 6f 73 65 64 20 61 6e 64 20 72 65 6f 70   closed and reop
04a0: 65 6e 65 64 20 6f 6e 20 61 6e 20 53 51 4c 69 74  ened on an SQLit
04b0: 65 20 64 61 74 61 62 61 73 65 2e 0a 4c 61 74 65  e database..Late
04c0: 72 2c 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68  r, some other th
04d0: 72 65 61 64 20 63 6f 6e 74 69 6e 75 65 64 20 74  read continued t
04e0: 6f 20 77 72 69 74 65 20 6c 6f 67 20 69 6e 66 6f  o write log info
04f0: 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  rmation into the
0500: 0a 6f 6c 64 20 66 69 6c 65 20 64 65 73 63 72 69  .old file descri
0510: 70 74 6f 72 2c 20 6e 6f 74 20 72 65 61 6c 69 7a  ptor, not realiz
0520: 69 6e 67 20 74 68 61 74 20 74 68 65 20 6c 6f 67  ing that the log
0530: 20 66 69 6c 65 20 68 61 64 20 62 65 65 6e 20 63   file had been c
0540: 6c 6f 73 65 64 0a 61 6c 72 65 61 64 79 2e 20 20  losed.already.  
0550: 42 75 74 20 62 65 63 61 75 73 65 20 74 68 65 20  But because the 
0560: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
0570: 68 61 64 20 62 65 65 6e 20 72 65 6f 70 65 6e 65  had been reopene
0580: 64 20 62 79 20 53 51 4c 69 74 65 2c 0a 74 68 65  d by SQLite,.the
0590: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
05a0: 74 20 77 61 73 20 69 6e 74 65 6e 64 65 64 20 74  t was intended t
05b0: 6f 20 67 6f 20 69 6e 74 6f 20 74 68 65 20 6c 6f  o go into the lo
05c0: 67 20 66 69 6c 65 20 65 6e 64 65 64 20 75 70 0a  g file ended up.
05d0: 6f 76 65 72 77 72 69 74 69 6e 67 20 70 61 72 74  overwriting part
05e0: 73 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  s of the SQLite 
05f0: 64 61 74 61 62 61 73 65 2c 20 6c 65 61 64 69 6e  database, leadin
0600: 67 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20  g to corruption 
0610: 6f 66 20 74 68 65 0a 64 61 74 61 62 61 73 65 2e  of the.database.
0620: 3c 2f 70 3e 0a 0a 3c 70 3e 4f 6e 65 20 65 78 61  </p>..<p>One exa
0630: 6d 70 6c 65 20 6f 66 20 74 68 69 73 20 6f 63 63  mple of this occ
0640: 75 72 72 65 64 20 63 69 72 63 61 20 32 30 31 33  urred circa 2013
0650: 2d 30 38 2d 33 30 20 6f 6e 20 74 68 65 20 63 61  -08-30 on the ca
0660: 6e 6f 6e 69 63 61 6c 20 72 65 70 6f 73 69 74 6f  nonical reposito
0670: 72 79 0a 66 6f 72 20 74 68 65 20 3c 61 20 68 72  ry.for the <a hr
0680: 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66  ef="http://www.f
0690: 6f 73 73 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e  ossil-scm.org/">
06a0: 46 6f 73 73 69 6c 20 44 56 43 53 3c 2f 61 3e 2e  Fossil DVCS</a>.
06b0: 20 20 49 6e 20 74 68 61 74 20 65 76 65 6e 74 2c    In that event,
06c0: 0a 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72  .file descriptor
06d0: 20 32 20 28 73 74 61 6e 64 61 72 64 20 65 72 72   2 (standard err
06e0: 6f 72 29 20 77 61 73 20 62 65 69 6e 67 20 65 72  or) was being er
06f0: 72 6f 6e 65 6f 75 73 6c 79 20 63 6c 6f 73 65 64  roneously closed
0700: 20 70 72 69 6f 72 20 74 6f 20 0a 5b 73 71 6c 69   prior to .[sqli
0710: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 73  te3_open_v2()] s
0720: 6f 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20  o that the file 
0730: 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20  descriptor used 
0740: 66 6f 72 20 74 68 65 0a 72 65 70 6f 73 69 74 6f  for the.reposito
0750: 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
0760: 20 77 61 73 20 32 2e 20 20 4c 61 74 65 72 2c 20   was 2.  Later, 
0770: 61 6e 6f 74 68 65 72 20 61 70 70 6c 69 63 61 74  another applicat
0780: 69 6f 6e 20 0a 62 75 67 20 63 61 75 73 65 64 20  ion .bug caused 
0790: 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  an assert() stat
07a0: 65 6d 65 6e 74 20 74 6f 20 65 6d 69 74 0a 61 6e  ement to emit.an
07b0: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
07c0: 79 20 69 6e 76 6f 6b 69 6e 67 20 77 72 69 74 65  y invoking write
07d0: 28 32 2c 2e 2e 2e 29 2e 20 20 42 75 74 20 73 69  (2,...).  But si
07e0: 6e 63 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  nce file descrip
07f0: 74 6f 72 20 32 20 77 61 73 20 0a 6e 6f 77 20 63  tor 2 was .now c
0800: 6f 6e 6e 65 63 74 65 64 20 74 6f 20 61 20 64 61  onnected to a da
0810: 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
0820: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 6f   error message.o
0830: 76 65 72 77 72 6f 74 65 20 70 61 72 74 20 6f 66  verwrote part of
0840: 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
0850: 54 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74  To guard against
0860: 20 74 68 69 73 20 6b 69 6e 64 20 6f 66 20 70 72   this kind of pr
0870: 6f 62 6c 65 6d 2c 0a 53 51 4c 69 74 65 20 5b 76  oblem,.SQLite [v
0880: 65 72 73 69 6f 6e 20 33 2e 38 2e 31 5d 20 61 6e  ersion 3.8.1] an
0890: 64 20 6c 61 74 65 72 20 72 65 66 75 73 65 20 74  d later refuse t
08a0: 6f 20 75 73 65 20 6c 6f 77 2d 6e 75 6d 62 65 72  o use low-number
08b0: 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
08c0: 6f 72 73 0a 66 6f 72 20 64 61 74 61 62 61 73 65  ors.for database
08d0: 20 66 69 6c 65 73 2e 20 0a 28 53 65 65 20 5b 53   files. .(See [S
08e0: 51 4c 49 54 45 5f 4d 49 4e 49 4d 55 4d 5f 46 49  QLITE_MINIMUM_FI
08f0: 4c 45 5f 44 45 53 43 52 49 50 54 4f 52 5d 20 66  LE_DESCRIPTOR] f
0900: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
0910: 66 6f 72 6d 61 74 69 6f 6e 2e 29 3c 2f 70 3e 0a  formation.)</p>.
0920: 0a 3c 68 33 3e 31 2e 32 20 42 61 63 6b 75 70 20  .<h3>1.2 Backup 
0930: 6f 72 20 72 65 73 74 6f 72 65 20 77 68 69 6c 65  or restore while
0940: 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
0950: 73 20 61 63 74 69 76 65 3c 2f 68 33 3e 0a 0a 3c  s active</h3>..<
0960: 70 3e 53 79 73 74 65 6d 73 20 74 68 61 74 20 72  p>Systems that r
0970: 75 6e 20 61 75 74 6f 6d 61 74 69 63 20 62 61 63  un automatic bac
0980: 6b 75 70 73 20 69 6e 20 74 68 65 20 62 61 63 6b  kups in the back
0990: 67 72 6f 75 6e 64 20 6d 69 67 68 74 20 74 72 79  ground might try
09a0: 20 74 6f 0a 6d 61 6b 65 20 61 20 62 61 63 6b 75   to.make a backu
09b0: 70 20 63 6f 70 79 20 6f 66 20 61 6e 20 53 51 4c  p copy of an SQL
09c0: 69 74 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ite database fil
09d0: 65 20 77 68 69 6c 65 20 69 74 20 69 73 20 69 6e  e while it is in
09e0: 20 74 68 65 20 6d 69 64 64 6c 65 0a 6f 66 20 61   the middle.of a
09f0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
0a00: 68 65 20 62 61 63 6b 75 70 20 63 6f 70 79 20 74  he backup copy t
0a10: 68 65 6e 20 6d 69 67 68 74 20 63 6f 6e 74 61 69  hen might contai
0a20: 6e 20 73 6f 6d 65 20 6f 6c 64 20 61 6e 64 20 73  n some old and s
0a30: 6f 6d 65 0a 6e 65 77 20 63 6f 6e 74 65 6e 74 2c  ome.new content,
0a40: 20 61 6e 64 20 74 68 75 73 20 62 65 20 63 6f 72   and thus be cor
0a50: 72 75 70 74 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68  rupt.</p>..<p>Th
0a60: 65 20 62 65 73 74 20 61 70 70 72 6f 61 63 68 20  e best approach 
0a70: 74 6f 20 6d 61 6b 65 20 72 65 6c 69 61 62 6c 65  to make reliable
0a80: 20 62 61 63 6b 75 70 20 63 6f 70 69 65 73 20 6f   backup copies o
0a90: 66 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  f an SQLite data
0aa0: 62 61 73 65 0a 69 73 20 74 6f 20 6d 61 6b 65 20  base.is to make 
0ab0: 75 73 65 20 6f 66 20 74 68 65 20 5b 62 61 63 6b  use of the [back
0ac0: 75 70 20 41 50 49 5d 20 74 68 61 74 20 69 73 20  up API] that is 
0ad0: 70 61 72 74 20 6f 66 20 74 68 65 20 53 51 4c 69  part of the SQLi
0ae0: 74 65 20 6c 69 62 72 61 72 79 2e 0a 46 61 69 6c  te library..Fail
0af0: 69 6e 67 20 74 68 61 74 2c 20 69 74 20 69 73 20  ing that, it is 
0b00: 73 61 66 65 20 74 6f 20 6d 61 6b 65 20 61 20 63  safe to make a c
0b10: 6f 70 79 20 6f 66 20 61 6e 20 53 51 4c 69 74 65  opy of an SQLite
0b20: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
0b30: 73 20 6c 6f 6e 67 0a 61 73 20 74 68 65 72 65 20  s long.as there 
0b40: 61 72 65 20 6e 6f 20 74 72 61 6e 73 61 63 74 69  are no transacti
0b50: 6f 6e 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20  ons in progress 
0b60: 62 79 20 61 6e 79 20 70 72 6f 63 65 73 73 2e 20  by any process. 
0b70: 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   If the previous
0b80: 0a 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 61 69  .transaction fai
0b90: 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20  led, then it is 
0ba0: 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61  important that a
0bb0: 6e 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  ny rollback jour
0bc0: 6e 61 6c 0a 28 74 68 65 20 3c 74 74 3e 2a 2d 6a  nal.(the <tt>*-j
0bd0: 6f 75 72 6e 61 6c 3c 2f 74 74 3e 20 66 69 6c 65  ournal</tt> file
0be0: 29 20 6f 72 20 77 72 69 74 65 2d 61 68 65 61 64  ) or write-ahead
0bf0: 20 6c 6f 67 20 28 74 68 65 20 3c 74 74 3e 2a 2d   log (the <tt>*-
0c00: 77 61 6c 3c 2f 74 74 3e 20 66 69 6c 65 29 0a 62  wal</tt> file).b
0c10: 65 20 63 6f 70 69 65 64 20 74 6f 67 65 74 68 65  e copied togethe
0c20: 72 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  r with the datab
0c30: 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 2e  ase file itself.
0c40: 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72  </p>..<tcl>hd_fr
0c50: 61 67 6d 65 6e 74 20 64 65 6c 68 6f 74 6a 72 6e  agment delhotjrn
0c60: 6c 20 7b 64 65 6c 65 74 69 6e 67 20 61 20 68 6f  l {deleting a ho
0c70: 74 20 6a 6f 75 72 6e 61 6c 7d 3c 2f 74 63 6c 3e  t journal}</tcl>
0c80: 0a 3c 68 33 3e 31 2e 33 20 44 65 6c 65 74 69 6e  .<h3>1.3 Deletin
0c90: 67 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 3c  g a hot journal<
0ca0: 2f 68 33 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20  /h3>..<p>SQLite 
0cb0: 6e 6f 72 6d 61 6c 6c 79 20 73 74 6f 72 65 73 20  normally stores 
0cc0: 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 61  all content in a
0cd0: 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 66 69 6c   single disk fil
0ce0: 65 2e 20 20 48 6f 77 65 76 65 72 2c 0a 77 68 69  e.  However,.whi
0cf0: 6c 65 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20  le performing a 
0d00: 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 6e 66  transaction, inf
0d10: 6f 72 6d 61 74 69 6f 6e 20 6e 65 63 65 73 73 61  ormation necessa
0d20: 72 79 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  ry to roll back 
0d30: 74 68 61 74 0a 74 72 61 6e 73 61 63 74 69 6f 6e  that.transaction
0d40: 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 72 61   following a cra
0d50: 73 68 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  sh or power fail
0d60: 75 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ure is stored in
0d70: 20 61 75 78 69 6c 69 61 72 79 0a 6a 6f 75 72 6e   auxiliary.journ
0d80: 61 6c 20 66 69 6c 65 73 2e 20 20 54 68 65 73 65  al files.  These
0d90: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 68   journal files h
0da0: 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ave the same nam
0db0: 65 20 61 73 20 74 68 65 0a 6f 72 69 67 69 6e 61  e as the.origina
0dc0: 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  l database file 
0dd0: 77 69 74 68 20 74 68 65 20 61 64 64 69 74 69 6f  with the additio
0de0: 6e 0a 6f 66 20 3c 74 74 3e 2d 6a 6f 75 72 6e 61  n.of <tt>-journa
0df0: 6c 3c 2f 74 74 3e 20 6f 72 20 3c 74 74 3e 2d 77  l</tt> or <tt>-w
0e00: 61 6c 3c 2f 74 74 3e 20 73 75 66 66 69 78 2e 3c  al</tt> suffix.<
0e10: 2f 70 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 6d  /p>..<p>SQLite m
0e20: 75 73 74 20 73 65 65 20 74 68 65 20 6a 6f 75 72  ust see the jour
0e30: 6e 61 6c 20 66 69 6c 65 73 20 69 6e 20 6f 72 64  nal files in ord
0e40: 65 72 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72  er to recover fr
0e50: 6f 6d 20 61 20 63 72 61 73 68 0a 6f 72 20 70 6f  om a crash.or po
0e60: 77 65 72 20 66 61 69 6c 75 72 65 2e 20 20 49 66  wer failure.  If
0e70: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
0e80: 65 73 20 61 72 65 20 6d 6f 76 65 64 2c 20 64 65  es are moved, de
0e90: 6c 65 74 65 64 2c 20 6f 72 20 72 65 6e 61 6d 65  leted, or rename
0ea0: 64 0a 61 66 74 65 72 20 61 20 63 72 61 73 68 20  d.after a crash 
0eb0: 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65  or power failure
0ec0: 2c 20 74 68 65 6e 20 61 75 74 6f 6d 61 74 69 63  , then automatic
0ed0: 20 72 65 63 6f 76 65 72 79 20 77 69 6c 6c 20 6e   recovery will n
0ee0: 6f 74 20 77 6f 72 6b 0a 61 6e 64 20 74 68 65 20  ot work.and the 
0ef0: 64 61 74 61 62 61 73 65 20 6d 61 79 20 67 6f 20  database may go 
0f00: 63 6f 72 72 75 70 74 2e 3c 2f 70 3e 0a 0a 3c 70  corrupt.</p>..<p
0f10: 3e 41 6e 6f 74 68 65 72 20 6d 61 6e 69 66 65 73  >Another manifes
0f20: 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 70  tation of this p
0f30: 72 6f 62 6c 65 6d 20 69 73 0a 5b 64 61 74 61 62  roblem is.[datab
0f40: 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 63  ase corruption c
0f50: 61 75 73 65 64 20 62 79 20 69 6e 63 6f 6e 73 69  aused by inconsi
0f60: 73 74 65 6e 74 20 75 73 65 20 6f 66 20 38 2b 33  stent use of 8+3
0f70: 20 66 69 6c 65 6e 61 6d 65 73 5d 2e 3c 2f 70 3e   filenames].</p>
0f80: 0a 0a 0a 3c 68 32 3e 32 2e 30 20 46 69 6c 65 20  ...<h2>2.0 File 
0f90: 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73  locking problems
0fa0: 3c 2f 68 32 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65  </h2>..<p>SQLite
0fb0: 20 75 73 65 73 20 66 69 6c 65 20 6c 6f 63 6b 73   uses file locks
0fc0: 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
0fd0: 20 66 69 6c 65 2c 20 61 6e 64 20 6f 6e 20 74 68   file, and on th
0fe0: 65 20 0a 5b 77 72 69 74 65 2d 61 68 65 61 64 20  e .[write-ahead 
0ff0: 6c 6f 67 5d 20 6f 72 20 5b 57 41 4c 5d 20 66 69  log] or [WAL] fi
1000: 6c 65 2c 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74  le, to coordinat
1010: 65 20 61 63 63 65 73 73 20 62 65 74 77 65 65 6e  e access between
1020: 20 63 6f 6e 63 75 72 72 65 6e 74 0a 70 72 6f 63   concurrent.proc
1030: 65 73 73 65 73 2e 20 20 57 69 74 68 6f 75 74 20  esses.  Without 
1040: 63 6f 6f 72 64 69 6e 61 74 69 6f 6e 2c 20 74 77  coordination, tw
1050: 6f 20 74 68 72 65 61 64 73 20 6f 72 20 70 72 6f  o threads or pro
1060: 63 65 73 73 65 73 20 6d 69 67 68 74 20 74 72 79  cesses might try
1070: 0a 74 6f 20 6d 61 6b 65 20 69 6e 63 6f 6d 70 61  .to make incompa
1080: 74 69 62 6c 65 20 63 68 61 6e 67 65 73 20 74 6f  tible changes to
1090: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
10a0: 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
10b0: 65 2c 0a 72 65 73 75 6c 74 69 6e 67 20 69 6e 20  e,.resulting in 
10c0: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
10d0: 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 68 33 3e 32 2e  ion.</p>..<h3>2.
10e0: 31 20 46 69 6c 65 73 79 73 74 65 6d 73 20 77 69  1 Filesystems wi
10f0: 74 68 20 62 72 6f 6b 65 6e 20 6f 72 20 6d 69 73  th broken or mis
1100: 73 69 6e 67 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  sing lock implem
1110: 65 6e 74 61 74 69 6f 6e 73 3c 2f 68 33 3e 0a 0a  entations</h3>..
1120: 3c 70 3e 53 51 4c 69 74 65 20 64 65 70 65 6e 64  <p>SQLite depend
1130: 73 20 6f 6e 20 74 68 65 20 75 6e 64 65 72 6c 79  s on the underly
1140: 69 6e 67 20 66 69 6c 65 73 79 73 74 65 6d 20 74  ing filesystem t
1150: 6f 20 64 6f 20 6c 6f 63 6b 69 6e 67 20 61 73 20  o do locking as 
1160: 74 68 65 0a 64 6f 63 75 6d 65 6e 74 61 74 69 6f  the.documentatio
1170: 6e 20 73 61 79 73 20 69 74 20 77 69 6c 6c 2e 20  n says it will. 
1180: 20 42 75 74 20 73 6f 6d 65 20 66 69 6c 65 73 79   But some filesy
1190: 73 74 65 6d 73 20 63 6f 6e 74 61 69 6e 20 62 75  stems contain bu
11a0: 67 73 20 69 6e 20 74 68 65 69 72 0a 6c 6f 63 6b  gs in their.lock
11b0: 69 6e 67 20 6c 6f 67 69 63 20 73 75 63 68 20 74  ing logic such t
11c0: 68 61 74 20 74 68 65 20 6c 6f 63 6b 73 20 64 6f  hat the locks do
11d0: 20 6e 6f 74 20 61 6c 77 61 79 73 20 62 65 68 61   not always beha
11e0: 76 65 20 61 73 20 61 64 76 65 72 74 69 73 65 64  ve as advertised
11f0: 2e 0a 54 68 69 73 20 69 73 20 65 73 70 65 63 69  ..This is especi
1200: 61 6c 6c 79 20 74 72 75 65 20 6f 66 20 6e 65 74  ally true of net
1210: 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 73  work filesystems
1220: 20 61 6e 64 20 4e 46 53 20 69 6e 20 70 61 72 74   and NFS in part
1230: 69 63 75 6c 61 72 2e 0a 49 66 20 53 51 4c 69 74  icular..If SQLit
1240: 65 20 69 73 20 75 73 65 64 20 6f 6e 20 61 20 66  e is used on a f
1250: 69 6c 65 73 79 73 74 65 6d 20 77 68 65 72 65 20  ilesystem where 
1260: 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d  the locking prim
1270: 69 74 69 76 65 73 20 63 6f 6e 74 61 69 6e 0a 62  itives contain.b
1280: 75 67 73 2c 20 61 6e 64 20 69 66 20 74 77 6f 20  ugs, and if two 
1290: 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20  or more threads 
12a0: 6f 72 20 70 72 6f 63 65 73 73 65 73 20 74 72 79  or processes try
12b0: 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73   to access the s
12c0: 61 6d 65 0a 64 61 74 61 62 61 73 65 20 61 74 20  ame.database at 
12d0: 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 74  the same time, t
12e0: 68 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f 72  hen database cor
12f0: 72 75 70 74 69 6f 6e 20 6d 69 67 68 74 20 72 65  ruption might re
1300: 73 75 6c 74 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e  sult.</p>..<tcl>
1310: 68 64 5f 66 72 61 67 6d 65 6e 74 20 70 6f 73 69  hd_fragment posi
1320: 78 5f 63 6c 6f 73 65 5f 62 75 67 3c 2f 74 63 6c  x_close_bug</tcl
1330: 3e 0a 3c 68 33 3e 32 2e 32 20 50 6f 73 69 78 20  >.<h3>2.2 Posix 
1340: 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 63  advisory locks c
1350: 61 6e 63 65 6c 65 64 20 62 79 20 61 20 73 65 70  anceled by a sep
1360: 61 72 61 74 65 20 74 68 72 65 61 64 20 64 6f 69  arate thread doi
1370: 6e 67 20 63 6c 6f 73 65 28 29 3c 2f 68 33 3e 0a  ng close()</h3>.
1380: 0a 3c 70 3e 54 68 65 20 64 65 66 61 75 6c 74 20  .<p>The default 
1390: 6c 6f 63 6b 69 6e 67 20 6d 65 63 68 61 6e 69 73  locking mechanis
13a0: 6d 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  m used by SQLite
13b0: 20 6f 6e 20 75 6e 69 78 20 70 6c 61 74 66 6f 72   on unix platfor
13c0: 6d 73 20 69 73 0a 50 4f 53 49 58 20 61 64 76 69  ms is.POSIX advi
13d0: 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 2e 20 20 55  sory locking.  U
13e0: 6e 66 6f 72 74 75 6e 61 74 65 6c 79 2c 20 50 4f  nfortunately, PO
13f0: 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63  SIX advisory loc
1400: 6b 69 6e 67 20 68 61 73 20 64 65 73 69 67 6e 0a  king has design.
1410: 71 75 69 72 6b 73 20 74 68 61 74 20 6d 61 6b 65  quirks that make
1420: 20 69 74 20 70 72 6f 6e 65 20 74 6f 20 6d 69 73   it prone to mis
1430: 75 73 65 20 61 6e 64 20 66 61 69 6c 75 72 65 2e  use and failure.
1440: 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20   In particular, 
1450: 61 6e 79 0a 74 68 72 65 61 64 20 69 6e 20 74 68  any.thread in th
1460: 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 77  e same process w
1470: 69 74 68 20 61 20 66 69 6c 65 20 64 65 73 63 72  ith a file descr
1480: 69 70 74 6f 72 20 74 68 61 74 20 69 73 20 68 6f  iptor that is ho
1490: 6c 64 69 6e 67 20 61 20 50 4f 53 49 58 0a 61 64  lding a POSIX.ad
14a0: 76 69 73 6f 72 79 20 6c 6f 63 6b 20 63 61 6e 20  visory lock can 
14b0: 6f 76 65 72 72 69 64 65 20 74 68 61 74 20 6c 6f  override that lo
14c0: 63 6b 20 75 73 69 6e 67 20 61 20 64 69 66 66 65  ck using a diffe
14d0: 72 65 6e 74 20 66 69 6c 65 20 64 65 73 63 72 69  rent file descri
14e0: 70 74 6f 72 2e 0a 4f 6e 65 20 70 61 72 74 69 63  ptor..One partic
14f0: 75 6c 61 72 6c 79 20 70 65 72 6e 69 63 69 6f 75  ularly perniciou
1500: 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74 68 61  s problem is tha
1510: 74 20 74 68 65 20 3c 74 74 3e 63 6c 6f 73 65 28  t the <tt>close(
1520: 29 3c 2f 74 74 3e 20 73 79 73 74 65 6d 0a 63 61  )</tt> system.ca
1530: 6c 6c 20 77 69 6c 6c 20 63 61 6e 63 65 6c 20 61  ll will cancel a
1540: 6c 6c 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72  ll POSIX advisor
1550: 79 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73  y locks on the s
1560: 61 6d 65 20 66 69 6c 65 20 66 6f 72 20 61 6c 6c  ame file for all
1570: 0a 74 68 72 65 61 64 73 20 61 6e 64 20 61 6c 6c  .threads and all
1580: 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1590: 73 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73  s in the process
15a0: 2e 3c 2f 70 3e 0a 0a 3c 70 3e 53 6f 2c 20 66 6f  .</p>..<p>So, fo
15b0: 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f  r example, suppo
15c0: 73 65 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61  se a multi-threa
15d0: 64 20 70 72 6f 63 65 73 73 20 68 61 73 0a 74 77  d process has.tw
15e0: 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64  o or more thread
15f0: 73 20 77 69 74 68 20 73 65 70 61 72 61 74 65 20  s with separate 
1600: 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
1610: 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74  connections to t
1620: 68 65 0a 73 61 6d 65 20 64 61 74 61 62 61 73 65  he.same database
1630: 20 66 69 6c 65 2e 20 20 54 68 65 6e 20 61 20 74   file.  Then a t
1640: 68 69 72 64 20 74 68 72 65 61 64 20 63 6f 6d 65  hird thread come
1650: 73 20 61 6c 6f 6e 67 20 61 6e 64 20 77 61 6e 74  s along and want
1660: 73 20 74 6f 20 72 65 61 64 0a 73 6f 6d 65 74 68  s to read.someth
1670: 69 6e 67 20 6f 75 74 20 6f 66 20 74 68 61 74 20  ing out of that 
1680: 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
1690: 6c 65 20 6f 6e 20 69 74 73 20 6f 77 6e 2c 20 77  le on its own, w
16a0: 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 68 65  ithout using the
16b0: 20 0a 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79   .SQLite library
16c0: 2e 0a 54 68 65 20 74 68 69 72 64 20 74 68 72 65  ..The third thre
16d0: 61 64 20 64 6f 65 73 20 61 6e 20 3c 74 74 3e 6f  ad does an <tt>o
16e0: 70 65 6e 28 29 3c 2f 74 74 3e 2c 20 61 20 3c 74  pen()</tt>, a <t
16f0: 74 3e 72 65 61 64 28 29 3c 2f 74 74 3e 20 61 6e  t>read()</tt> an
1700: 64 20 74 68 65 6e 0a 61 20 3c 74 74 3e 63 6c 6f  d then.a <tt>clo
1710: 73 65 28 29 3c 2f 74 74 3e 2e 0a 4f 6e 65 20 77  se()</tt>..One w
1720: 6f 75 6c 64 20 74 68 69 6e 6b 20 74 68 69 73 20  ould think this 
1730: 77 6f 75 6c 64 20 62 65 20 68 61 72 6d 6c 65 73  would be harmles
1740: 73 2e 0a 42 75 74 20 74 68 65 20 3c 74 74 3e 63  s..But the <tt>c
1750: 6c 6f 73 65 28 29 3c 2f 74 74 3e 20 73 79 73 74  lose()</tt> syst
1760: 65 6d 20 63 61 6c 6c 20 63 61 75 73 65 64 20 74  em call caused t
1770: 68 65 0a 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e  he.locks held on
1780: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79   the database by
1790: 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 74   all the other t
17a0: 68 72 65 61 64 73 20 74 6f 20 62 65 20 64 72 6f  hreads to be dro
17b0: 70 70 65 64 2e 20 20 54 68 6f 73 65 0a 6f 74 68  pped.  Those.oth
17c0: 65 72 20 74 68 72 65 61 64 73 20 68 61 76 65 20  er threads have 
17d0: 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e  no way of knowin
17e0: 67 20 74 68 61 74 20 74 68 65 69 72 20 6c 6f 63  g that their loc
17f0: 6b 73 20 68 61 76 65 20 6a 75 73 74 20 62 65 65  ks have just bee
1800: 6e 0a 74 72 61 73 68 65 64 20 28 50 4f 53 49 58  n.trashed (POSIX
1810: 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64   does not provid
1820: 65 20 61 6e 79 20 6d 65 63 68 61 6e 69 73 6d 20  e any mechanism 
1830: 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 69  to determine thi
1840: 73 29 20 61 6e 64 20 73 6f 0a 74 68 65 79 20 6b  s) and so.they k
1850: 65 65 70 20 6f 6e 20 72 75 6e 6e 69 6e 67 20 75  eep on running u
1860: 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70 74  nder the assumpt
1870: 69 6f 6e 20 74 68 61 74 20 74 68 65 69 72 20 6c  ion that their l
1880: 6f 63 6b 73 20 61 72 65 20 73 74 69 6c 6c 20 76  ocks are still v
1890: 61 6c 69 64 2e 0a 54 68 69 73 20 63 61 6e 20 6c  alid..This can l
18a0: 65 61 64 20 74 6f 20 74 77 6f 20 6f 72 20 6d 6f  ead to two or mo
18b0: 72 65 20 74 68 72 65 61 64 73 20 6f 72 20 70 72  re threads or pr
18c0: 6f 63 65 73 73 65 73 20 74 72 79 69 6e 67 20 74  ocesses trying t
18d0: 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 0a 64  o write to the.d
18e0: 61 74 61 62 61 73 65 20 61 74 20 74 68 65 20 73  atabase at the s
18f0: 61 6d 65 20 74 69 6d 65 2c 20 72 65 73 75 6c 74  ame time, result
1900: 69 6e 67 20 69 6e 20 64 61 74 61 62 61 73 65 20  ing in database 
1910: 63 6f 72 72 75 70 74 69 6f 6e 2e 3c 2f 70 3e 0a  corruption.</p>.
1920: 0a 3c 70 3e 4e 6f 74 65 20 74 68 61 74 20 69 74  .<p>Note that it
1930: 20 69 73 20 70 65 72 66 65 63 74 6c 79 20 73 61   is perfectly sa
1940: 66 65 20 66 6f 72 20 74 77 6f 20 6f 72 20 6d 6f  fe for two or mo
1950: 72 65 20 74 68 72 65 61 64 73 20 74 6f 20 61 63  re threads to ac
1960: 63 65 73 73 20 74 68 65 0a 73 61 6d 65 20 53 51  cess the.same SQ
1970: 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 66 69  Lite database fi
1980: 6c 65 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  le using the SQL
1990: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
19a0: 65 20 75 6e 69 78 20 64 72 69 76 65 72 73 20 66  e unix drivers f
19b0: 6f 72 0a 53 51 4c 69 74 65 20 6b 6e 6f 77 20 61  or.SQLite know a
19c0: 62 6f 75 74 20 74 68 65 20 50 4f 53 49 58 20 61  bout the POSIX a
19d0: 64 76 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 20  dvisory locking 
19e0: 71 75 69 72 6b 73 20 61 6e 64 20 77 6f 72 6b 20  quirks and work 
19f0: 61 72 6f 75 6e 64 20 74 68 65 6d 2e 0a 54 68 69  around them..Thi
1a00: 73 20 70 72 6f 62 6c 65 6d 20 6f 6e 6c 79 20 61  s problem only a
1a10: 72 69 73 65 73 20 77 68 65 6e 20 61 20 74 68 72  rises when a thr
1a20: 65 61 64 20 74 72 69 65 73 20 74 6f 20 62 79 70  ead tries to byp
1a30: 61 73 73 20 74 68 65 20 53 51 4c 69 74 65 20 6c  ass the SQLite l
1a40: 69 62 72 61 72 79 0a 61 6e 64 20 72 65 61 64 20  ibrary.and read 
1a50: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1a60: 65 20 64 69 72 65 63 74 6c 79 2e 3c 2f 70 3e 0a  e directly.</p>.
1a70: 0a 3c 68 34 3e 32 2e 32 2e 31 20 4d 75 6c 74 69  .<h4>2.2.1 Multi
1a80: 70 6c 65 20 63 6f 70 69 65 73 20 6f 66 20 53 51  ple copies of SQ
1a90: 4c 69 74 65 20 6c 69 6e 6b 65 64 20 69 6e 74 6f  Lite linked into
1aa0: 20 74 68 65 20 73 61 6d 65 20 61 70 70 6c 69 63   the same applic
1ab0: 61 74 69 6f 6e 3c 2f 68 34 3e 0a 0a 3c 70 3e 41  ation</h4>..<p>A
1ac0: 73 20 70 6f 69 6e 74 65 64 20 6f 75 74 20 69 6e  s pointed out in
1ad0: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
1ae0: 72 61 67 72 61 70 68 2c 20 53 51 4c 69 74 65 20  ragraph, SQLite 
1af0: 74 61 6b 65 73 20 73 74 65 70 73 20 74 6f 20 77  takes steps to w
1b00: 6f 72 6b 0a 61 72 6f 75 6e 64 20 74 68 65 20 71  ork.around the q
1b10: 75 69 72 6b 73 20 6f 66 20 50 4f 53 49 58 20 61  uirks of POSIX a
1b20: 64 76 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 2e  dvisory locking.
1b30: 20 20 50 61 72 74 20 6f 66 20 74 68 61 74 20 77    Part of that w
1b40: 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 6e 76 6f 6c  ork-around invol
1b50: 76 65 73 0a 6b 65 65 70 69 6e 67 20 61 20 67 6c  ves.keeping a gl
1b60: 6f 62 61 6c 20 6c 69 73 74 20 28 6d 75 74 65 78  obal list (mutex
1b70: 20 70 72 6f 74 65 63 74 65 64 29 20 6f 66 20 6f   protected) of o
1b80: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62  pen SQLite datab
1b90: 61 73 65 20 66 69 6c 65 73 2e 0a 42 75 74 2c 20  ase files..But, 
1ba0: 69 66 20 6d 75 6c 74 69 70 6c 65 20 63 6f 70 69  if multiple copi
1bb0: 65 73 20 6f 66 20 53 51 4c 69 74 65 20 61 72 65  es of SQLite are
1bc0: 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65   linked into the
1bd0: 20 73 61 6d 65 20 61 70 70 6c 69 63 61 74 69 6f   same applicatio
1be0: 6e 2c 0a 74 68 65 6e 20 74 68 65 72 65 20 77 69  n,.then there wi
1bf0: 6c 6c 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 69  ll be multiple i
1c00: 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 69 73  nstances of this
1c10: 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 0a 44 61   global list..Da
1c20: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c30: 6e 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20  ns opened using 
1c40: 6f 6e 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  one copy of the 
1c50: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 77  SQLite library.w
1c60: 69 6c 6c 20 62 65 20 75 6e 61 77 61 72 65 20 6f  ill be unaware o
1c70: 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  f database conne
1c80: 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64 20 75 73  ctions opened us
1c90: 69 6e 67 20 74 68 65 20 6f 74 68 65 72 20 63 6f  ing the other co
1ca0: 70 79 2c 0a 61 6e 64 20 77 69 6c 6c 20 62 65 20  py,.and will be 
1cb0: 75 6e 61 62 6c 65 20 74 6f 20 77 6f 72 6b 20 61  unable to work a
1cc0: 72 6f 75 6e 64 20 74 68 65 20 50 4f 53 49 58 20  round the POSIX 
1cd0: 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67  advisory locking
1ce0: 20 71 75 69 72 6b 73 2e 0a 41 20 3c 74 74 3e 63   quirks..A <tt>c
1cf0: 6c 6f 73 65 28 29 3c 2f 74 74 3e 20 6f 70 65 72  lose()</tt> oper
1d00: 61 74 69 6f 6e 20 6f 6e 20 6f 6e 65 20 63 6f 6e  ation on one con
1d10: 6e 65 63 74 69 6f 6e 20 6d 69 67 68 74 20 75 6e  nection might un
1d20: 6b 6e 6f 77 69 6e 67 6c 79 20 0a 63 6c 65 61 72  knowingly .clear
1d30: 20 74 68 65 20 6c 6f 63 6b 73 20 6f 6e 20 61 20   the locks on a 
1d40: 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
1d50: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6c  se connection, l
1d60: 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  eading to databa
1d70: 73 65 0a 63 6f 72 72 75 70 74 69 6f 6e 2e 3c 2f  se.corruption.</
1d80: 70 3e 0a 0a 3c 70 3e 54 68 65 20 73 63 65 6e 61  p>..<p>The scena
1d90: 72 69 6f 20 61 62 6f 76 65 20 73 6f 75 6e 64 73  rio above sounds
1da0: 20 66 61 72 2d 66 65 74 63 68 65 64 2e 0a 42 75   far-fetched..Bu
1db0: 74 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76  t the SQLite dev
1dc0: 65 6c 6f 70 65 72 73 20 61 72 65 20 61 77 61 72  elopers are awar
1dd0: 65 20 6f 66 20 61 74 20 0a 6c 65 61 73 74 20 6f  e of at .least o
1de0: 6e 65 20 63 6f 6d 6d 65 72 63 69 61 6c 20 70 72  ne commercial pr
1df0: 6f 64 75 63 74 20 74 68 61 74 20 77 61 73 20 72  oduct that was r
1e00: 65 6c 65 61 73 65 64 0a 77 69 74 68 20 65 78 61  eleased.with exa
1e10: 63 74 6c 79 20 74 68 69 73 20 62 75 67 2e 20 20  ctly this bug.  
1e20: 54 68 65 20 76 65 6e 64 6f 72 20 63 61 6d 65 20  The vendor came 
1e30: 74 6f 20 74 68 65 20 53 51 4c 69 74 65 20 64 65  to the SQLite de
1e40: 76 65 6c 6f 70 65 72 73 20 73 65 65 6b 69 6e 67  velopers seeking
1e50: 0a 68 65 6c 70 20 69 6e 20 74 72 61 63 6b 69 6e  .help in trackin
1e60: 67 20 64 6f 77 6e 20 73 6f 6d 65 20 69 6e 66 72  g down some infr
1e70: 65 71 75 65 6e 74 20 64 61 74 61 62 61 73 65 20  equent database 
1e80: 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 73 75 65  corruption issue
1e90: 73 20 74 68 65 79 20 77 65 72 65 0a 73 65 65 69  s they were.seei
1ea0: 6e 67 20 6f 6e 20 4c 69 6e 75 78 20 61 6e 64 20  ng on Linux and 
1eb0: 4d 61 63 2e 20 20 54 68 65 20 70 72 6f 62 6c 65  Mac.  The proble
1ec0: 6d 20 77 61 73 20 65 76 65 6e 74 75 61 6c 6c 79  m was eventually
1ed0: 20 74 72 61 63 65 64 20 74 6f 20 74 68 65 0a 66   traced to the.f
1ee0: 61 63 74 20 74 68 61 74 20 74 68 65 20 61 70 70  act that the app
1ef0: 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 6c 69 6e  lication was lin
1f00: 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 74 77 6f  king against two
1f10: 20 73 65 70 61 72 61 74 65 20 63 6f 70 69 65 73   separate copies
1f20: 20 6f 66 20 53 51 4c 69 74 65 2e 0a 54 68 65 20   of SQLite..The 
1f30: 73 6f 6c 75 74 69 6f 6e 20 77 61 73 20 74 6f 20  solution was to 
1f40: 63 68 61 6e 67 65 20 74 68 65 20 61 70 70 6c 69  change the appli
1f50: 63 61 74 69 6f 6e 20 62 75 69 6c 64 20 70 72 6f  cation build pro
1f60: 63 65 64 75 72 65 73 20 74 6f 20 6c 69 6e 6b 20  cedures to link 
1f70: 61 67 61 69 6e 73 74 0a 6a 75 73 74 20 6f 6e 65  against.just one
1f80: 20 63 6f 70 79 20 6f 66 20 53 51 4c 69 74 65 20   copy of SQLite 
1f90: 69 6e 73 74 65 61 64 20 6f 66 20 74 77 6f 2e 3c  instead of two.<
1fa0: 2f 70 3e 0a 0a 3c 68 33 3e 32 2e 33 20 54 77 6f  /p>..<h3>2.3 Two
1fb0: 20 70 72 6f 63 65 73 73 65 73 20 75 73 69 6e 67   processes using
1fc0: 20 64 69 66 66 65 72 65 6e 74 20 6c 6f 63 6b 69   different locki
1fd0: 6e 67 20 70 72 6f 74 6f 63 6f 6c 73 3c 2f 68 33  ng protocols</h3
1fe0: 3e 0a 0a 3c 70 3e 54 68 65 20 64 65 66 61 75 6c  >..<p>The defaul
1ff0: 74 20 6c 6f 63 6b 69 6e 67 20 6d 65 63 68 61 6e  t locking mechan
2000: 69 73 6d 20 75 73 65 64 20 62 79 20 53 51 4c 69  ism used by SQLi
2010: 74 65 20 6f 6e 20 75 6e 69 78 20 70 6c 61 74 66  te on unix platf
2020: 6f 72 6d 73 20 69 73 0a 50 4f 53 49 58 20 61 64  orms is.POSIX ad
2030: 76 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 2c 20  visory locking, 
2040: 62 75 74 20 74 68 65 72 65 20 61 72 65 20 6f 74  but there are ot
2050: 68 65 72 20 6f 70 74 69 6f 6e 73 2e 20 20 42 79  her options.  By
2060: 20 73 65 6c 65 63 74 69 6e 67 20 61 6e 0a 61 6c   selecting an.al
2070: 74 65 72 6e 61 74 69 76 65 20 5b 73 71 6c 69 74  ternative [sqlit
2080: 65 33 5f 76 66 73 5d 20 75 73 69 6e 67 20 74 68  e3_vfs] using th
2090: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  e [sqlite3_open_
20a0: 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2c  v2()] interface,
20b0: 20 61 6e 0a 61 70 70 6c 69 63 61 74 69 6f 6e 20   an.application 
20c0: 63 61 6e 20 6d 61 6b 65 20 75 73 65 20 6f 66 20  can make use of 
20d0: 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 70 72  other locking pr
20e0: 6f 74 6f 63 6f 6c 73 20 74 68 61 74 20 6d 69 67  otocols that mig
20f0: 68 74 20 62 65 20 6d 6f 72 65 0a 61 70 70 72 6f  ht be more.appro
2100: 70 72 69 61 74 65 20 74 6f 20 63 65 72 74 61 69  priate to certai
2110: 6e 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 20 20  n filesystems.  
2120: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 64 6f 74  For example, dot
2130: 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 6d 69  -file locking mi
2140: 67 68 74 0a 62 65 20 73 65 6c 65 63 74 20 66 6f  ght.be select fo
2150: 72 20 75 73 65 20 69 6e 20 61 6e 20 61 70 70 6c  r use in an appl
2160: 69 63 61 74 69 6f 6e 20 74 68 61 74 20 68 61 73  ication that has
2170: 20 74 6f 20 72 75 6e 20 6f 6e 20 61 6e 20 4e 46   to run on an NF
2180: 53 20 66 69 6c 65 73 79 73 74 65 6d 0a 74 68 61  S filesystem.tha
2190: 74 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f  t does not suppo
21a0: 72 74 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72  rt POSIX advisor
21b0: 79 20 6c 6f 63 6b 69 6e 67 2e 3c 2f 70 3e 0a 0a  y locking.</p>..
21c0: 3c 70 3e 49 74 20 69 73 20 69 6d 70 6f 72 74 61  <p>It is importa
21d0: 6e 74 20 74 68 61 74 20 61 6c 6c 20 63 6f 6e 6e  nt that all conn
21e0: 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73  ections to the s
21f0: 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ame database fil
2200: 65 20 75 73 65 20 0a 74 68 65 20 73 61 6d 65 20  e use .the same 
2210: 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c  locking protocol
2220: 2e 0a 49 66 20 6f 6e 65 20 61 70 70 6c 69 63 61  ..If one applica
2230: 74 69 6f 6e 20 69 73 20 75 73 69 6e 67 20 50 4f  tion is using PO
2240: 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63  SIX advisory loc
2250: 6b 73 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 61  ks and another a
2260: 70 70 6c 69 63 61 74 69 6f 6e 0a 69 73 20 75 73  pplication.is us
2270: 69 6e 67 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63  ing dot-file loc
2280: 6b 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 74  king, then the t
2290: 77 6f 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  wo applications 
22a0: 77 69 6c 6c 20 6e 6f 74 20 73 65 65 20 65 61 63  will not see eac
22b0: 68 0a 6f 74 68 65 72 27 73 20 6c 6f 63 6b 73 20  h.other's locks 
22c0: 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  and will not be 
22d0: 61 62 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61  able to coordina
22e0: 74 65 20 64 61 74 61 62 61 73 65 20 61 63 63 65  te database acce
22f0: 73 73 2c 20 70 6f 73 73 69 62 6c 79 0a 6c 65 61  ss, possibly.lea
2300: 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65  ding to database
2310: 20 63 6f 72 72 75 70 74 69 6f 6e 2e 3c 2f 70 3e   corruption.</p>
2320: 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65  ..<tcl>hd_fragme
2330: 6e 74 20 75 6e 6c 69 6e 6b 20 7b 75 6e 6c 69 6e  nt unlink {unlin
2340: 6b 20 63 6f 72 72 75 70 74 69 6f 6e 7d 20 7b 75  k corruption} {u
2350: 6e 6c 69 6e 6b 65 64 20 64 61 74 61 62 61 73 65  nlinked database
2360: 20 66 69 6c 65 73 7d 3c 2f 74 63 6c 3e 0a 3c 68   files}</tcl>.<h
2370: 33 3e 32 2e 34 20 55 6e 6c 69 6e 6b 69 6e 67 20  3>2.4 Unlinking 
2380: 6f 72 20 72 65 6e 61 6d 69 6e 67 20 61 20 64 61  or renaming a da
2390: 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 6c  tabase file whil
23a0: 65 20 69 6e 20 75 73 65 3c 2f 68 33 3e 0a 0a 3c  e in use</h3>..<
23b0: 70 3e 49 66 20 74 77 6f 20 70 72 6f 63 65 73 73  p>If two process
23c0: 65 73 20 68 61 76 65 20 6f 70 65 6e 20 63 6f 6e  es have open con
23d0: 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  nections to the 
23e0: 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
23f0: 6c 65 20 61 6e 64 0a 6f 6e 65 20 70 72 6f 63 65  le and.one proce
2400: 73 73 20 63 6c 6f 73 65 73 20 69 74 73 20 63 6f  ss closes its co
2410: 6e 6e 65 63 74 69 6f 6e 2c 20 75 6e 6c 69 6e 6b  nnection, unlink
2420: 73 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 6e  s the file, then
2430: 20 63 72 65 61 74 65 73 20 61 20 6e 65 77 0a 64   creates a new.d
2440: 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20  atabase file in 
2450: 69 74 73 20 70 6c 61 63 65 20 77 69 74 68 20 74  its place with t
2460: 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 6e 64  he same name and
2470: 20 72 65 6f 70 65 6e 73 20 74 68 65 20 6e 65 77   reopens the new
2480: 20 66 69 6c 65 2c 0a 74 68 65 6e 20 74 68 65 20   file,.then the 
2490: 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 77 69  two processes wi
24a0: 6c 6c 20 62 65 20 74 61 6c 6b 69 6e 67 20 74 6f  ll be talking to
24b0: 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62   different datab
24c0: 61 73 65 20 66 69 6c 65 73 20 77 69 74 68 20 0a  ase files with .
24d0: 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20  the same name.  
24e0: 28 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20  (Note that this 
24f0: 69 73 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65  is only possible
2500: 20 6f 6e 20 50 6f 73 69 78 20 61 6e 64 20 50 6f   on Posix and Po
2510: 73 69 78 2d 6c 69 6b 65 0a 73 79 73 74 65 6d 73  six-like.systems
2520: 20 74 68 61 74 20 70 65 72 6d 69 74 20 61 20 66   that permit a f
2530: 69 6c 65 20 74 6f 20 62 65 20 75 6e 6c 69 6e 6b  ile to be unlink
2540: 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20 73  ed while it is s
2550: 74 69 6c 6c 20 6f 70 65 6e 20 66 6f 72 0a 72 65  till open for.re
2560: 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e  ading and writin
2570: 67 2e 20 20 57 69 6e 64 6f 77 73 20 64 6f 65 73  g.  Windows does
2580: 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 69 73 20   not allow this 
2590: 74 6f 20 6f 63 63 75 72 2e 29 0a 53 69 6e 63 65  to occur.).Since
25a0: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
25b0: 6c 73 20 61 6e 64 20 57 41 4c 20 66 69 6c 65 73  ls and WAL files
25c0: 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20 74 68   are based on th
25d0: 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
25e0: 74 61 62 61 73 65 0a 66 69 6c 65 2c 20 74 68 65  tabase.file, the
25f0: 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20 64   two different d
2600: 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 77 69  atabase files wi
2610: 6c 6c 20 73 68 61 72 65 20 74 68 65 20 73 61 6d  ll share the sam
2620: 65 20 72 6f 6c 6c 62 61 63 6b 0a 6a 6f 75 72 6e  e rollback.journ
2630: 61 6c 20 6f 72 20 57 41 4c 20 66 69 6c 65 2e 20  al or WAL file. 
2640: 20 41 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72   A rollback or r
2650: 65 63 6f 76 65 72 79 20 66 6f 72 20 6f 6e 65 20  ecovery for one 
2660: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 73  of the databases
2670: 0a 6d 69 67 68 74 20 75 73 65 20 63 6f 6e 74 65  .might use conte
2680: 6e 74 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  nt from the othe
2690: 72 20 64 61 74 61 62 61 73 65 2c 20 72 65 73 75  r database, resu
26a0: 6c 74 69 6e 67 20 69 6e 20 63 6f 72 72 75 70 74  lting in corrupt
26b0: 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 70 3e 41 20 73  ion.</p>..<p>A s
26c0: 69 6d 69 6c 61 72 20 70 72 6f 62 6c 65 6d 20 6f  imilar problem o
26d0: 63 63 75 72 73 20 69 66 20 61 20 64 61 74 61 62  ccurs if a datab
26e0: 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 6e 61  ase file is rena
26f0: 6d 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 0a  med while it is.
2700: 6f 70 65 6e 65 64 20 61 6e 64 20 61 20 6e 65 77  opened and a new
2710: 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
2720: 20 77 69 74 68 20 74 68 65 20 6f 6c 64 20 6e 61   with the old na
2730: 6d 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 42 65 67 69  me.</p>..<p>Begi
2740: 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74  nning with SQLit
2750: 65 20 5b 76 65 72 73 69 6f 6e 20 33 2e 37 2e 31  e [version 3.7.1
2760: 37 5d 2c 20 74 68 65 20 75 6e 69 78 20 4f 53 20  7], the unix OS 
2770: 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 0a 73  interface will.s
2780: 65 6e 64 20 53 51 4c 49 54 45 5f 57 41 52 4e 49  end SQLITE_WARNI
2790: 4e 47 20 6d 65 73 73 61 67 65 73 20 74 6f 20 74  NG messages to t
27a0: 68 65 20 5b 65 72 72 6f 72 20 6c 6f 67 5d 20 69  he [error log] i
27b0: 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  f a database fil
27c0: 65 20 69 73 20 75 6e 6c 69 6e 6b 65 64 0a 77 68  e is unlinked.wh
27d0: 69 6c 65 20 69 74 20 69 73 20 73 74 69 6c 6c 20  ile it is still 
27e0: 69 6e 20 75 73 65 2e 3c 2f 70 3e 0a 0a 3c 74 63  in use.</p>..<tc
27f0: 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 61 6c  l>hd_fragment al
2800: 69 61 73 20 7b 64 61 74 61 62 61 73 65 20 66 69  ias {database fi
2810: 6c 65 6e 61 6d 65 20 61 6c 69 61 73 69 6e 67 7d  lename aliasing}
2820: 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 32 2e 35 20 4d  </tcl>.<h3>2.5 M
2830: 75 6c 74 69 70 6c 65 20 6c 69 6e 6b 73 20 74 6f  ultiple links to
2840: 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 3c 2f   the same file</
2850: 68 33 3e 0a 0a 3c 70 3e 49 66 20 61 20 73 69 6e  h3>..<p>If a sin
2860: 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  gle database fil
2870: 65 20 68 61 73 20 6d 75 6c 74 69 70 6c 65 20 6c  e has multiple l
2880: 69 6e 6b 73 20 28 65 69 74 68 65 72 20 68 61 72  inks (either har
2890: 64 20 6f 72 20 73 6f 66 74 20 6c 69 6e 6b 73 29  d or soft links)
28a0: 0a 74 68 65 6e 20 74 68 61 74 20 69 73 20 6a 75  .then that is ju
28b0: 73 74 20 61 6e 6f 74 68 65 72 20 77 61 79 20 6f  st another way o
28c0: 66 20 73 61 79 69 6e 67 20 74 68 61 74 20 74 68  f saying that th
28d0: 65 20 66 69 6c 65 20 68 61 73 20 6d 75 6c 74 69  e file has multi
28e0: 70 6c 65 20 6e 61 6d 65 73 2e 0a 49 66 20 74 77  ple names..If tw
28f0: 6f 20 6f 72 20 6d 6f 72 65 20 70 72 6f 63 65 73  o or more proces
2900: 73 65 73 20 6f 70 65 6e 20 74 68 65 20 64 61 74  ses open the dat
2910: 61 62 61 73 65 20 75 73 69 6e 67 20 64 69 66 66  abase using diff
2920: 65 72 65 6e 74 20 6e 61 6d 65 73 2c 20 74 68 65  erent names, the
2930: 6e 0a 74 68 65 79 20 77 69 6c 6c 20 75 73 65 20  n.they will use 
2940: 64 69 66 66 65 72 65 6e 74 20 72 6f 6c 6c 62 61  different rollba
2950: 63 6b 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20  ck journals and 
2960: 57 41 4c 20 66 69 6c 65 73 2e 20 20 54 68 61 74  WAL files.  That
2970: 20 6d 65 61 6e 73 20 74 68 61 74 0a 69 66 20 6f   means that.if o
2980: 6e 65 20 70 72 6f 63 65 73 73 20 63 72 61 73 68  ne process crash
2990: 65 73 2c 20 74 68 65 20 6f 74 68 65 72 20 70 72  es, the other pr
29a0: 6f 63 65 73 73 20 77 69 6c 6c 20 62 65 20 75 6e  ocess will be un
29b0: 61 62 6c 65 20 74 6f 20 72 65 63 6f 76 65 72 20  able to recover 
29c0: 74 68 65 0a 74 72 61 6e 73 61 63 74 69 6f 6e 20  the.transaction 
29d0: 69 6e 20 70 72 6f 67 72 65 73 73 20 62 65 63 61  in progress beca
29e0: 75 73 65 20 69 74 20 77 69 6c 6c 20 62 65 20 6c  use it will be l
29f0: 6f 6f 6b 69 6e 67 20 69 6e 20 74 68 65 20 77 72  ooking in the wr
2a00: 6f 6e 67 20 70 6c 61 63 65 0a 66 6f 72 20 74 68  ong place.for th
2a10: 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6a 6f  e appropriate jo
2a20: 75 72 6e 61 6c 2e 3c 2f 70 3e 0a 0a 3c 70 3e 42  urnal.</p>..<p>B
2a30: 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51  eginning with SQ
2a40: 4c 69 74 65 20 5b 76 65 72 73 69 6f 6e 20 33 2e  Lite [version 3.
2a50: 37 2e 31 37 5d 2c 20 74 68 65 20 75 6e 69 78 20  7.17], the unix 
2a60: 4f 53 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  OS interface wil
2a70: 6c 0a 73 65 6e 64 20 53 51 4c 49 54 45 5f 57 41  l.send SQLITE_WA
2a80: 52 4e 49 4e 47 20 6d 65 73 73 61 67 65 73 20 74  RNING messages t
2a90: 6f 20 74 68 65 20 5b 65 72 72 6f 72 20 6c 6f 67  o the [error log
2aa0: 5d 20 69 66 20 61 20 64 61 74 61 62 61 73 65 20  ] if a database 
2ab0: 66 69 6c 65 20 68 61 73 20 0a 6d 75 6c 74 69 70  file has .multip
2ac0: 6c 65 20 68 61 72 64 20 6c 69 6e 6b 73 2e 20 20  le hard links.  
2ad0: 41 73 20 6f 66 20 74 68 69 73 20 77 72 69 74 69  As of this writi
2ae0: 6e 67 2c 20 53 51 4c 69 74 65 20 73 74 69 6c 6c  ng, SQLite still
2af0: 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 64 65   does not yet de
2b00: 74 65 63 74 20 0a 6f 72 20 77 61 72 6e 20 61 62  tect .or warn ab
2b10: 6f 75 74 20 74 68 65 20 75 73 65 20 6f 66 20 64  out the use of d
2b20: 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 74 68  atabase files th
2b30: 72 6f 75 67 68 20 73 6f 66 74 20 6c 69 6e 6b 73  rough soft links
2b40: 2e 3c 2f 70 3e 0a 0a 0a 3c 68 32 3e 33 2e 30 20  .</p>...<h2>3.0 
2b50: 46 61 69 6c 75 72 65 20 74 6f 20 73 79 6e 63 3c  Failure to sync<
2b60: 2f 68 32 3e 0a 0a 3c 70 3e 49 6e 20 6f 72 64 65  /h2>..<p>In orde
2b70: 72 20 74 6f 20 67 75 61 72 61 6e 74 65 65 20 74  r to guarantee t
2b80: 68 61 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  hat database fil
2b90: 65 73 20 61 72 65 20 61 6c 77 61 79 73 20 63 6f  es are always co
2ba0: 6e 73 69 73 74 65 6e 74 2c 20 53 51 4c 69 74 65  nsistent, SQLite
2bb0: 0a 77 69 6c 6c 20 6f 63 63 61 73 69 6f 6e 61 6c  .will occasional
2bc0: 6c 79 20 61 73 6b 20 74 68 65 20 6f 70 65 72 61  ly ask the opera
2bd0: 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 66  ting system to f
2be0: 6c 75 73 68 20 61 6c 6c 20 70 65 6e 64 69 6e 67  lush all pending
2bf0: 20 77 72 69 74 65 73 20 74 6f 0a 70 65 72 73 69   writes to.persi
2c00: 73 74 65 6e 74 20 73 74 6f 72 61 67 65 20 74 68  stent storage th
2c10: 65 6e 20 77 61 69 74 20 66 6f 72 20 74 68 61 74  en wait for that
2c20: 20 66 6c 75 73 68 20 74 6f 20 63 6f 6d 70 6c 65   flush to comple
2c30: 74 65 2e 20 20 54 68 69 73 20 69 73 20 0a 61 63  te.  This is .ac
2c40: 63 6f 6d 70 6c 69 73 68 65 64 20 75 73 69 6e 67  complished using
2c50: 20 74 68 65 20 3c 74 74 3e 66 73 79 6e 63 28 29   the <tt>fsync()
2c60: 3c 2f 74 74 3e 20 73 79 73 74 65 6d 20 63 61 6c  </tt> system cal
2c70: 6c 20 75 6e 64 65 72 20 75 6e 69 78 20 61 6e 64  l under unix and
2c80: 0a 3c 74 74 3e 46 6c 75 73 68 46 69 6c 65 42 75  .<tt>FlushFileBu
2c90: 66 66 65 72 73 28 29 3c 2f 74 74 3e 20 75 6e 64  ffers()</tt> und
2ca0: 65 72 20 57 69 6e 64 6f 77 73 2e 20 20 57 65 20  er Windows.  We 
2cb0: 63 61 6c 6c 20 74 68 69 73 20 66 6c 75 73 68 20  call this flush 
2cc0: 6f 66 0a 70 65 6e 64 69 6e 67 20 77 72 69 74 65  of.pending write
2cd0: 73 20 61 20 22 73 79 6e 63 22 2e 3c 2f 70 3e 0a  s a "sync".</p>.
2ce0: 0a 3c 70 3e 41 63 74 75 61 6c 6c 79 2c 20 69 66  .<p>Actually, if
2cf0: 20 6f 6e 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6e   one is only con
2d00: 63 65 72 6e 65 64 20 77 69 74 68 20 61 74 6f 6d  cerned with atom
2d10: 69 63 20 61 6e 64 20 63 6f 6e 73 69 73 74 65 6e  ic and consisten
2d20: 74 20 77 72 69 74 65 73 20 61 6e 64 0a 69 73 20  t writes and.is 
2d30: 77 69 6c 6c 69 6e 67 20 74 6f 20 66 6f 72 65 67  willing to foreg
2d40: 6f 20 64 75 72 61 62 6c 65 20 77 72 69 74 65 73  o durable writes
2d50: 2c 20 74 68 65 20 73 79 6e 63 20 6f 70 65 72 61  , the sync opera
2d60: 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65  tion does not ne
2d70: 65 64 0a 74 6f 20 77 61 69 74 20 75 6e 74 69 6c  ed.to wait until
2d80: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20   the content is 
2d90: 63 6f 6d 70 6c 65 74 65 6c 79 20 73 74 6f 72 65  completely store
2da0: 64 20 6f 6e 20 70 65 72 73 69 73 74 65 6e 74 20  d on persistent 
2db0: 6d 65 64 69 61 2e 20 20 49 6e 73 74 65 61 64 2c  media.  Instead,
2dc0: 0a 74 68 65 20 73 79 6e 63 20 6f 70 65 72 61 74  .the sync operat
2dd0: 69 6f 6e 20 63 61 6e 20 62 65 20 74 68 6f 75 67  ion can be thoug
2de0: 68 74 20 6f 66 20 61 73 20 61 6e 20 49 2f 4f 20  ht of as an I/O 
2df0: 62 61 72 72 69 65 72 2e 20 20 41 73 20 6c 6f 6e  barrier.  As lon
2e00: 67 20 61 73 20 61 6c 6c 0a 77 72 69 74 65 73 20  g as all.writes 
2e10: 74 68 61 74 20 6f 63 63 75 72 20 62 65 66 6f 72  that occur befor
2e20: 65 20 74 68 65 20 73 79 6e 63 20 61 72 65 20 63  e the sync are c
2e30: 6f 6d 70 6c 65 74 65 64 20 62 65 66 6f 72 65 20  ompleted before 
2e40: 61 6e 79 20 77 72 69 74 65 20 74 68 61 74 20 68  any write that h
2e50: 61 70 70 65 6e 73 0a 61 66 74 65 72 20 74 68 65  appens.after the
2e60: 20 73 79 6e 63 2c 20 6e 6f 20 64 61 74 61 62 61   sync, no databa
2e70: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69  se corruption wi
2e80: 6c 6c 20 6f 63 63 75 72 2e 20 20 49 66 20 73 79  ll occur.  If sy
2e90: 6e 63 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20  nc is operating 
2ea0: 61 73 0a 61 6e 20 49 2f 4f 20 62 61 72 72 69 65  as.an I/O barrie
2eb0: 72 20 61 6e 64 20 6e 6f 74 20 61 73 20 61 20 74  r and not as a t
2ec0: 72 75 65 20 73 79 6e 63 2c 20 74 68 65 6e 20 61  rue sync, then a
2ed0: 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
2ee0: 72 20 73 79 73 74 65 6d 20 63 72 61 73 68 0a 6d  r system crash.m
2ef0: 69 67 68 74 20 63 61 75 73 65 20 6f 6e 65 20 6f  ight cause one o
2f00: 72 20 6d 6f 72 65 20 70 72 65 76 69 6f 75 73 6c  r more previousl
2f10: 79 20 63 6f 6d 6d 69 74 74 65 64 20 74 72 61 6e  y committed tran
2f20: 73 61 63 74 69 6f 6e 73 20 74 6f 20 72 6f 6c 6c  sactions to roll
2f30: 20 62 61 63 6b 0a 28 69 6e 20 76 69 6f 6c 61 74   back.(in violat
2f40: 69 6f 6e 20 6f 66 20 74 68 65 20 22 64 75 72 61  ion of the "dura
2f50: 62 6c 65 22 20 70 72 6f 70 65 72 74 79 20 6f 66  ble" property of
2f60: 20 22 41 43 49 44 22 29 20 62 75 74 20 74 68 65   "ACID") but the
2f70: 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 61   database will a
2f80: 74 0a 6c 65 61 73 74 20 63 6f 6e 74 69 6e 75 65  t.least continue
2f90: 20 74 6f 20 62 65 20 63 6f 6e 73 69 73 74 65 6e   to be consisten
2fa0: 74 2c 20 61 6e 64 20 74 68 61 74 20 69 73 20 77  t, and that is w
2fb0: 68 61 74 20 6d 6f 73 74 20 70 65 6f 70 6c 65 20  hat most people 
2fc0: 63 61 72 65 20 61 62 6f 75 74 2e 3c 2f 70 3e 0a  care about.</p>.
2fd0: 0a 3c 68 33 3e 33 2e 31 20 44 69 73 6b 20 64 72  .<h3>3.1 Disk dr
2fe0: 69 76 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  ives that do not
2ff0: 20 68 6f 6e 6f 72 20 73 79 6e 63 20 72 65 71 75   honor sync requ
3000: 65 73 74 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 55 6e  ests</h3>..<p>Un
3010: 66 6f 72 74 75 6e 61 74 65 6c 79 2c 20 6d 6f 73  fortunately, mos
3020: 74 20 63 6f 6e 73 75 6d 65 72 2d 67 72 61 64 65  t consumer-grade
3030: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 20 64 65   mass storage de
3040: 76 69 63 65 73 20 6c 69 65 20 61 62 6f 75 74 0a  vices lie about.
3050: 73 79 6e 63 69 6e 67 2e 20 20 44 69 73 6b 20 64  syncing.  Disk d
3060: 72 69 76 65 73 20 77 69 6c 6c 20 72 65 70 6f 72  rives will repor
3070: 74 20 74 68 61 74 20 63 6f 6e 74 65 6e 74 20 69  t that content i
3080: 73 20 73 61 66 65 6c 79 20 6f 6e 20 70 65 72 73  s safely on pers
3090: 69 73 74 65 6e 74 0a 6d 65 64 69 61 20 61 73 20  istent.media as 
30a0: 73 6f 6f 6e 20 61 73 20 69 74 20 72 65 61 63 68  soon as it reach
30b0: 65 73 20 74 68 65 20 74 72 61 63 6b 20 62 75 66  es the track buf
30c0: 66 65 72 20 61 6e 64 20 62 65 66 6f 72 65 20 61  fer and before a
30d0: 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a 77 72  ctually being.wr
30e0: 69 74 74 65 6e 20 74 6f 20 6f 78 69 64 65 2e 20  itten to oxide. 
30f0: 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20   This makes the 
3100: 64 69 73 6b 20 64 72 69 76 65 73 20 73 65 65 6d  disk drives seem
3110: 20 74 6f 20 6f 70 65 72 61 74 65 20 66 61 73 74   to operate fast
3120: 65 72 0a 28 77 68 69 63 68 20 69 73 20 76 69 74  er.(which is vit
3130: 61 6c 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 74  ally important t
3140: 6f 20 74 68 65 20 6d 61 6e 75 66 61 63 74 75 72  o the manufactur
3150: 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  er so that they 
3160: 63 61 6e 20 73 68 6f 77 0a 67 6f 6f 64 20 62 65  can show.good be
3170: 6e 63 68 6d 61 72 6b 20 6e 75 6d 62 65 72 73 20  nchmark numbers 
3180: 69 6e 20 74 72 61 64 65 20 6d 61 67 61 7a 69 6e  in trade magazin
3190: 65 73 29 2e 20 20 41 6e 64 20 69 6e 20 66 61 69  es).  And in fai
31a0: 72 6e 65 73 73 2c 20 74 68 65 20 6c 69 65 0a 6e  rness, the lie.n
31b0: 6f 72 6d 61 6c 6c 79 20 63 61 75 73 65 73 20 6e  ormally causes n
31c0: 6f 20 68 61 72 6d 2c 20 61 73 20 6c 6f 6e 67 20  o harm, as long 
31d0: 61 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  as there is no p
31e0: 6f 77 65 72 20 6c 6f 73 73 20 6f 72 20 68 61 72  ower loss or har
31f0: 64 20 72 65 73 65 74 0a 70 72 69 6f 72 20 74 6f  d reset.prior to
3200: 20 74 68 65 20 74 72 61 63 6b 20 62 75 66 66 65   the track buffe
3210: 72 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67  r actually being
3220: 20 77 72 69 74 74 65 6e 20 74 6f 20 6f 78 69 64   written to oxid
3230: 65 2e 20 20 42 75 74 20 69 66 20 61 0a 70 6f 77  e.  But if a.pow
3240: 65 72 20 6c 6f 73 73 20 6f 72 20 68 61 72 64 20  er loss or hard 
3250: 72 65 73 65 74 20 64 6f 65 73 20 6f 63 63 75 72  reset does occur
3260: 2c 20 61 6e 64 20 69 66 20 74 68 61 74 20 72 65  , and if that re
3270: 73 75 6c 74 73 20 69 6e 20 63 6f 6e 74 65 6e 74  sults in content
3280: 20 74 68 61 74 0a 77 61 73 20 77 72 69 74 74 65   that.was writte
3290: 6e 20 61 66 74 65 72 20 61 20 73 79 6e 63 20 72  n after a sync r
32a0: 65 61 63 68 69 6e 67 20 6f 78 69 64 65 20 77 68  eaching oxide wh
32b0: 69 6c 65 20 63 6f 6e 74 65 6e 74 20 77 72 69 74  ile content writ
32c0: 74 65 6e 20 62 65 66 6f 72 65 0a 74 68 65 20 73  ten before.the s
32d0: 79 6e 63 20 69 73 20 73 74 69 6c 6c 20 69 6e 20  ync is still in 
32e0: 61 20 74 72 61 63 6b 20 62 75 66 66 65 72 2c 20  a track buffer, 
32f0: 74 68 65 6e 20 64 61 74 61 62 61 73 65 20 63 6f  then database co
3300: 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 6f 63 63  rruption can occ
3310: 75 72 2e 3c 2f 70 3e 0a 0a 3c 70 3e 55 53 42 20  ur.</p>..<p>USB 
3320: 66 6c 61 73 68 20 6d 65 6d 6f 72 79 20 73 74 69  flash memory sti
3330: 63 6b 73 20 73 65 65 6d 20 74 6f 20 62 65 20 65  cks seem to be e
3340: 73 70 65 63 69 61 6c 6c 79 20 70 65 72 6e 69 63  specially pernic
3350: 69 6f 75 73 20 6c 69 61 72 73 20 0a 72 65 67 61  ious liars .rega
3360: 72 64 69 6e 67 20 73 79 6e 63 20 72 65 71 75 65  rding sync reque
3370: 73 74 73 2e 20 20 4f 6e 65 20 63 61 6e 20 65 61  sts.  One can ea
3380: 73 69 6c 79 20 73 65 65 20 74 68 69 73 20 62 79  sily see this by
3390: 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 6c 61   committing a la
33a0: 72 67 65 0a 74 72 61 6e 73 61 63 74 69 6f 6e 20  rge.transaction 
33b0: 74 6f 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74  to an SQLite dat
33c0: 61 62 61 73 65 20 6f 6e 20 61 20 55 53 42 20 6d  abase on a USB m
33d0: 65 6d 6f 72 79 20 73 74 69 63 6b 2e 20 20 54 68  emory stick.  Th
33e0: 65 20 43 4f 4d 4d 49 54 0a 63 6f 6d 6d 61 6e 64  e COMMIT.command
33f0: 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 72 65 6c   will return rel
3400: 61 74 69 76 65 6c 79 20 71 75 69 63 6b 6c 79 2c  atively quickly,
3410: 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
3420: 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 74 69 63   the memory stic
3430: 6b 0a 68 61 73 20 74 6f 6c 64 20 74 68 65 20 6f  k.has told the o
3440: 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
3450: 61 6e 64 20 74 68 65 20 6f 70 65 72 61 74 69 6e  and the operatin
3460: 67 20 73 79 73 74 65 6d 20 68 61 73 20 74 6f 6c  g system has tol
3470: 64 20 53 51 4c 69 74 65 20 74 68 61 74 0a 61 6c  d SQLite that.al
3480: 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 61 66  l content is saf
3490: 65 6c 79 20 69 6e 20 70 65 72 73 69 73 74 65 6e  ely in persisten
34a0: 74 20 73 74 6f 72 61 67 65 2c 20 61 6e 64 20 79  t storage, and y
34b0: 65 74 20 74 68 65 20 4c 45 44 20 6f 6e 20 74 68  et the LED on th
34c0: 65 20 65 6e 64 0a 6f 66 20 74 68 65 20 6d 65 6d  e end.of the mem
34d0: 6f 72 79 20 73 74 69 63 6b 20 77 69 6c 6c 20 63  ory stick will c
34e0: 6f 6e 74 69 6e 75 65 20 66 6c 61 73 68 69 6e 67  ontinue flashing
34f0: 20 66 6f 72 20 73 65 76 65 72 61 6c 20 6d 6f 72   for several mor
3500: 65 20 73 65 63 6f 6e 64 73 2e 20 0a 50 75 6c 6c  e seconds. .Pull
3510: 69 6e 67 20 6f 75 74 20 74 68 65 20 6d 65 6d 6f  ing out the memo
3520: 72 79 20 73 74 69 63 6b 20 77 68 69 6c 65 20 74  ry stick while t
3530: 68 65 20 4c 45 44 20 69 73 20 73 74 69 6c 6c 20  he LED is still 
3540: 66 6c 61 73 68 69 6e 67 20 77 69 6c 6c 20 66 72  flashing will fr
3550: 65 71 75 65 6e 74 6c 79 0a 72 65 73 75 6c 74 20  equently.result 
3560: 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  in database corr
3570: 75 70 74 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 70 3e  uption.</p>..<p>
3580: 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 65  Note that SQLite
3590: 20 6d 75 73 74 20 62 65 6c 69 65 76 65 20 77 68   must believe wh
35a0: 61 74 65 76 65 72 20 74 68 65 20 6f 70 65 72 61  atever the opera
35b0: 74 69 6e 67 20 73 79 73 74 65 6d 20 61 6e 64 20  ting system and 
35c0: 68 61 72 64 77 61 72 65 0a 74 65 6c 6c 20 69 74  hardware.tell it
35d0: 20 61 62 6f 75 74 20 74 68 65 20 73 74 61 74 75   about the statu
35e0: 73 20 6f 66 20 73 79 6e 63 20 72 65 71 75 65 73  s of sync reques
35f0: 74 73 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  ts.  There is no
3600: 20 77 61 79 20 66 6f 72 20 53 51 4c 69 74 65 20   way for SQLite 
3610: 74 6f 0a 64 65 74 65 63 74 20 74 68 61 74 20 65  to.detect that e
3620: 69 74 68 65 72 20 69 73 20 6c 79 69 6e 67 20 61  ither is lying a
3630: 6e 64 20 74 68 61 74 20 77 72 69 74 65 73 20 6d  nd that writes m
3640: 69 67 68 74 20 62 65 20 6f 63 63 75 72 72 69 6e  ight be occurrin
3650: 67 20 6f 75 74 2d 6f 66 2d 6f 72 64 65 72 2e 0a  g out-of-order..
3660: 48 6f 77 65 76 65 72 2c 20 53 51 4c 69 74 65 20  However, SQLite 
3670: 69 6e 20 5b 57 41 4c 20 7c 20 57 41 4c 20 6d 6f  in [WAL | WAL mo
3680: 64 65 5d 20 69 73 20 66 61 72 20 6d 6f 72 65 20  de] is far more 
3690: 66 6f 72 67 69 76 69 6e 67 20 6f 66 0a 6f 75 74  forgiving of.out
36a0: 2d 6f 66 2d 6f 72 64 65 72 20 77 72 69 74 65 73  -of-order writes
36b0: 20 74 68 61 6e 20 69 6e 20 74 68 65 20 64 65 66   than in the def
36c0: 61 75 6c 74 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  ault rollback jo
36d0: 75 72 6e 61 6c 20 6d 6f 64 65 73 2e 20 20 49 6e  urnal modes.  In
36e0: 20 57 41 4c 0a 6d 6f 64 65 2c 20 74 68 65 20 6f   WAL.mode, the o
36f0: 6e 6c 79 20 74 69 6d 65 20 74 68 61 74 20 61 20  nly time that a 
3700: 66 61 69 6c 65 64 20 73 79 6e 63 20 6f 70 65 72  failed sync oper
3710: 61 74 69 6f 6e 20 63 61 6e 20 63 61 75 73 65 20  ation can cause 
3720: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
3730: 69 6f 6e 0a 69 73 20 64 75 72 69 6e 67 20 61 20  ion.is during a 
3740: 5b 63 68 65 63 6b 70 6f 69 6e 74 5d 20 6f 70 65  [checkpoint] ope
3750: 72 61 74 69 6f 6e 2e 20 20 41 20 73 79 6e 63 20  ration.  A sync 
3760: 66 61 69 6c 75 72 65 20 64 75 72 69 6e 67 20 61  failure during a
3770: 20 43 4f 4d 4d 49 54 20 6d 69 67 68 74 0a 72 65   COMMIT might.re
3780: 73 75 6c 74 20 69 6e 20 6c 6f 73 73 20 6f 66 20  sult in loss of 
3790: 64 75 72 61 62 69 6c 69 74 79 20 62 75 74 20 6e  durability but n
37a0: 6f 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 20  ot in a corrupt 
37b0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
37c0: 48 65 6e 63 65 2c 0a 6f 6e 65 20 6c 69 6e 65 20  Hence,.one line 
37d0: 6f 66 20 64 65 66 65 6e 73 65 20 61 67 61 69 6e  of defense again
37e0: 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  st database corr
37f0: 75 70 74 69 6f 6e 20 64 75 65 20 74 6f 20 66 61  uption due to fa
3800: 69 6c 65 64 20 73 79 6e 63 20 6f 70 65 72 61 74  iled sync operat
3810: 69 6f 6e 73 0a 69 73 20 74 6f 20 75 73 65 20 53  ions.is to use S
3820: 51 4c 69 74 65 20 69 6e 20 57 41 4c 20 6d 6f 64  QLite in WAL mod
3830: 65 20 61 6e 64 20 74 6f 20 63 68 65 63 6b 70 6f  e and to checkpo
3840: 69 6e 74 20 61 73 20 69 6e 66 72 65 71 75 65 6e  int as infrequen
3850: 74 6c 79 20 61 73 20 70 6f 73 73 69 62 6c 65 2e  tly as possible.
3860: 3c 2f 70 3e 0a 0a 3c 68 33 3e 33 2e 32 20 44 69  </p>..<h3>3.2 Di
3870: 73 61 62 6c 69 6e 67 20 73 79 6e 63 20 75 73 69  sabling sync usi
3880: 6e 67 20 50 52 41 47 4d 41 73 3c 2f 68 33 3e 0a  ng PRAGMAs</h3>.
3890: 0a 3c 70 3e 54 68 65 20 73 79 6e 63 20 6f 70 65  .<p>The sync ope
38a0: 72 61 74 69 6f 6e 73 20 74 68 61 74 20 53 51 4c  rations that SQL
38b0: 69 74 65 20 70 65 72 66 6f 72 6d 73 20 74 6f 20  ite performs to 
38c0: 68 65 6c 70 20 65 6e 73 75 72 65 20 69 6e 74 65  help ensure inte
38d0: 67 72 69 74 79 0a 63 61 6e 20 62 65 20 64 69 73  grity.can be dis
38e0: 61 62 6c 65 64 20 61 74 20 72 75 6e 2d 74 69 6d  abled at run-tim
38f0: 65 20 75 73 69 6e 67 20 74 68 65 20 5b 73 79 6e  e using the [syn
3900: 63 68 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 5d  chronous pragma]
3910: 2e 20 20 42 79 20 73 65 74 74 69 6e 67 0a 50 52  .  By setting.PR
3920: 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f 75 73  AGMA synchronous
3930: 3d 4f 46 46 2c 20 61 6c 6c 20 73 79 6e 63 20 6f  =OFF, all sync o
3940: 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 6f 6d  perations are om
3950: 69 74 74 65 64 2e 20 20 54 68 69 73 20 6d 61 6b  itted.  This mak
3960: 65 73 0a 53 51 4c 69 74 65 20 73 65 65 6d 20 74  es.SQLite seem t
3970: 6f 20 72 75 6e 20 66 61 73 74 65 72 2c 20 62 75  o run faster, bu
3980: 74 20 69 74 20 61 6c 73 6f 20 61 6c 6c 6f 77 73  t it also allows
3990: 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
39a0: 79 73 74 65 6d 20 74 6f 20 66 72 65 65 6c 79 0a  ystem to freely.
39b0: 72 65 6f 72 64 65 72 20 77 72 69 74 65 73 2c 20  reorder writes, 
39c0: 77 68 69 63 68 20 63 6f 75 6c 64 20 72 65 73 75  which could resu
39d0: 6c 74 20 69 6e 20 64 61 74 61 62 61 73 65 20 63  lt in database c
39e0: 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 61 20 70  orruption if a p
39f0: 6f 77 65 72 20 66 61 69 6c 75 72 65 0a 6f 72 20  ower failure.or 
3a00: 68 61 72 64 20 72 65 73 65 74 20 6f 63 63 75 72  hard reset occur
3a10: 73 20 70 72 69 6f 72 20 74 6f 20 61 6c 6c 20 63  s prior to all c
3a20: 6f 6e 74 65 6e 74 20 72 65 61 63 68 69 6e 67 20  ontent reaching 
3a30: 70 65 72 73 69 73 74 65 6e 74 20 73 74 6f 72 61  persistent stora
3a40: 67 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 46 6f 72 20  ge.</p>..<p>For 
3a50: 6d 61 78 69 6d 75 6d 20 72 65 6c 69 61 62 69 6c  maximum reliabil
3a60: 69 74 79 20 61 6e 64 20 66 6f 72 20 72 6f 62 75  ity and for robu
3a70: 73 74 6e 65 73 73 20 61 67 61 69 6e 73 74 20 64  stness against d
3a80: 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
3a90: 6f 6e 2c 0a 53 51 4c 69 74 65 20 73 68 6f 75 6c  on,.SQLite shoul
3aa0: 64 20 61 6c 77 61 79 73 20 62 65 20 72 75 6e 20  d always be run 
3ab0: 77 69 74 68 20 69 74 73 20 64 65 66 61 75 6c 74  with its default
3ac0: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73 65 74   synchronous set
3ad0: 74 69 6e 67 20 6f 66 20 46 55 4c 4c 2e 3c 2f 70  ting of FULL.</p
3ae0: 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d  >..<tcl>hd_fragm
3af0: 65 6e 74 20 68 61 72 64 77 61 72 65 66 61 75 6c  ent hardwarefaul
3b00: 74 3c 2f 74 63 6c 3e 0a 3c 68 32 3e 34 2e 30 20  t</tcl>.<h2>4.0 
3b10: 44 69 73 6b 20 44 72 69 76 65 20 61 6e 64 20 46  Disk Drive and F
3b20: 6c 61 73 68 20 4d 65 6d 6f 72 79 20 46 61 69 6c  lash Memory Fail
3b30: 75 72 65 73 3c 2f 68 32 3e 0a 0a 3c 70 3e 41 6e  ures</h2>..<p>An
3b40: 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
3b50: 20 63 61 6e 20 62 65 63 6f 6d 65 20 63 6f 72 72   can become corr
3b60: 75 70 74 20 69 66 20 74 68 65 20 66 69 6c 65 20  upt if the file 
3b70: 63 6f 6e 74 65 6e 74 20 63 68 61 6e 67 65 73 20  content changes 
3b80: 0a 64 75 65 20 74 6f 20 61 20 64 69 73 6b 20 64  .due to a disk d
3b90: 72 69 76 65 20 6f 72 20 66 6c 61 73 68 20 6d 65  rive or flash me
3ba0: 6d 6f 72 79 20 66 61 69 6c 75 72 65 2e 20 20 49  mory failure.  I
3bb0: 74 20 69 73 20 76 65 72 79 20 72 61 72 65 2c 20  t is very rare, 
3bc0: 62 75 74 20 64 69 73 6b 73 20 0a 77 69 6c 6c 20  but disks .will 
3bd0: 6f 63 63 61 73 69 6f 6e 61 6c 6c 79 20 66 6c 69  occasionally fli
3be0: 70 20 61 20 62 69 74 20 69 6e 20 74 68 65 20 6d  p a bit in the m
3bf0: 69 64 64 6c 65 20 6f 66 20 61 20 73 65 63 74 6f  iddle of a secto
3c00: 72 2e 3c 2f 70 3e 0a 0a 3c 68 33 3e 34 2e 31 20  r.</p>..<h3>4.1 
3c10: 4e 6f 6e 2d 70 6f 77 65 72 73 61 66 65 20 66 6c  Non-powersafe fl
3c20: 61 73 68 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72  ash memory contr
3c30: 6f 6c 6c 65 72 73 3c 2f 68 33 3e 0a 0a 3c 70 3e  ollers</h3>..<p>
3c40: 57 65 20 61 72 65 20 74 6f 6c 64 20 74 68 61 74  We are told that
3c50: 20 69 6e 20 73 6f 6d 65 20 66 6c 61 73 68 20 6d   in some flash m
3c60: 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 72  emory controller
3c70: 73 20 74 68 65 20 77 65 61 72 2d 6c 65 76 65 6c  s the wear-level
3c80: 69 6e 67 20 6c 6f 67 69 63 0a 63 61 6e 20 63 61  ing logic.can ca
3c90: 75 73 65 20 72 61 6e 64 6f 6d 20 66 69 6c 65 73  use random files
3ca0: 79 73 74 65 6d 20 64 61 6d 61 67 65 20 69 66 20  ystem damage if 
3cb0: 70 6f 77 65 72 20 69 73 20 69 6e 74 65 72 72 75  power is interru
3cc0: 70 74 65 64 20 64 75 72 69 6e 67 20 61 20 77 72  pted during a wr
3cd0: 69 74 65 2e 0a 54 68 69 73 20 63 61 6e 20 6d 61  ite..This can ma
3ce0: 6e 69 66 65 73 74 2c 20 66 6f 72 20 65 78 61 6d  nifest, for exam
3cf0: 70 6c 65 2c 20 61 73 20 72 61 6e 64 6f 6d 20 63  ple, as random c
3d00: 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 69  hanges in the mi
3d10: 64 64 6c 65 20 6f 66 20 61 20 66 69 6c 65 0a 74  ddle of a file.t
3d20: 68 61 74 20 77 61 73 20 6e 6f 74 20 65 76 65 6e  hat was not even
3d30: 20 6f 70 65 6e 20 61 74 20 74 68 65 20 74 69 6d   open at the tim
3d40: 65 20 6f 66 20 74 68 65 20 70 6f 77 65 72 20 6c  e of the power l
3d50: 6f 73 73 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78  oss.  So, for ex
3d60: 61 6d 70 6c 65 2c 0a 61 20 64 65 76 69 63 65 20  ample,.a device 
3d70: 77 6f 75 6c 64 20 62 65 20 77 72 69 74 69 6e 67  would be writing
3d80: 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 61 6e   content into an
3d90: 20 4d 50 33 20 66 69 6c 65 20 69 6e 20 66 6c 61   MP3 file in fla
3da0: 73 68 20 6d 65 6d 6f 72 79 20 77 68 65 6e 20 61  sh memory when a
3db0: 0a 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75  .power loss occu
3dc0: 72 73 2c 20 61 6e 64 20 74 68 61 74 20 63 6f 75  rs, and that cou
3dd0: 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20  ld result in an 
3de0: 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
3df0: 62 65 69 6e 67 0a 63 6f 72 72 75 70 74 65 64 20  being.corrupted 
3e00: 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  even though the 
3e10: 64 61 74 61 62 61 73 65 20 61 73 20 6e 6f 74 20  database as not 
3e20: 65 76 65 6e 20 69 6e 20 75 73 65 20 61 74 20 74  even in use at t
3e30: 68 65 20 74 69 6d 65 20 6f 66 20 74 68 65 0a 70  he time of the.p
3e40: 6f 77 65 72 20 6c 6f 73 73 2e 3c 2f 70 3e 0a 0a  ower loss.</p>..
3e50: 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74  <tcl>hd_fragment
3e60: 20 66 61 6b 65 75 73 62 3c 2f 74 63 6c 3e 0a 3c   fakeusb</tcl>.<
3e70: 68 33 3e 34 2e 32 20 46 61 6b 65 20 63 61 70 61  h3>4.2 Fake capa
3e80: 63 69 74 79 20 55 53 42 20 73 74 69 63 6b 73 3c  city USB sticks<
3e90: 2f 68 33 3e 0a 0a 3c 70 3e 54 68 65 72 65 20 61  /h3>..<p>There a
3ea0: 72 65 20 6d 61 6e 79 20 66 72 61 75 64 75 6c 65  re many fraudule
3eb0: 6e 74 20 55 53 42 20 73 74 69 63 6b 73 20 69 6e  nt USB sticks in
3ec0: 20 63 69 72 63 75 6c 61 74 69 6f 6e 20 74 68 61   circulation tha
3ed0: 74 20 72 65 70 6f 72 74 20 74 6f 20 68 61 76 65  t report to have
3ee0: 0a 61 20 68 69 67 68 20 63 61 70 61 63 69 74 79  .a high capacity
3ef0: 20 28 65 78 3a 20 38 47 42 29 20 62 75 74 20 61   (ex: 8GB) but a
3f00: 72 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 63  re really only c
3f10: 61 70 61 62 6c 65 20 6f 66 20 73 74 6f 72 69 6e  apable of storin
3f20: 67 20 61 20 6d 75 63 68 0a 73 6d 61 6c 6c 65 72  g a much.smaller
3f30: 20 61 6d 6f 75 6e 74 20 28 65 78 3a 20 31 47 42   amount (ex: 1GB
3f40: 29 2e 20 20 20 41 74 74 65 6d 70 74 73 20 74 6f  ).   Attempts to
3f50: 20 77 72 69 74 65 20 6f 6e 20 74 68 65 73 65 20   write on these 
3f60: 64 65 76 69 63 65 73 20 77 69 6c 6c 0a 6f 66 74  devices will.oft
3f70: 65 6e 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 72  en result in unr
3f80: 65 6c 61 74 65 64 20 66 69 6c 65 73 20 62 65 69  elated files bei
3f90: 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
3fa0: 20 41 6e 79 20 75 73 65 20 6f 66 20 61 20 66 72   Any use of a fr
3fb0: 61 75 64 75 6c 65 6e 74 0a 66 6c 61 73 68 20 6d  audulent.flash m
3fc0: 65 6d 6f 72 79 20 64 65 76 69 63 65 20 63 61 6e  emory device can
3fd0: 20 65 61 73 69 6c 79 20 6c 65 61 64 20 74 6f 20   easily lead to 
3fe0: 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
3ff0: 69 6f 6e 2c 20 74 68 65 72 65 66 6f 72 65 2e 0a  ion, therefore..
4000: 49 6e 74 65 72 6e 65 74 20 73 65 61 72 63 68 65  Internet searche
4010: 73 20 73 75 63 68 20 61 73 20 22 66 61 6b 65 20  s such as "fake 
4020: 63 61 70 61 63 69 74 79 20 75 73 62 22 20 77 69  capacity usb" wi
4030: 6c 6c 20 74 75 72 6e 20 75 70 20 6c 6f 74 73 20  ll turn up lots 
4040: 6f 66 0a 64 69 73 74 75 72 62 69 6e 67 20 69 6e  of.disturbing in
4050: 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
4060: 74 68 69 73 20 70 72 6f 62 6c 65 6d 2e 0a 0a 3c  this problem...<
4070: 68 32 3e 35 2e 30 20 4d 65 6d 6f 72 79 20 63 6f  h2>5.0 Memory co
4080: 72 72 75 70 74 69 6f 6e 3c 2f 68 32 3e 0a 0a 3c  rruption</h2>..<
4090: 70 3e 53 51 4c 69 74 65 20 69 73 20 61 20 43 2d  p>SQLite is a C-
40a0: 6c 69 62 72 61 72 79 20 74 68 61 74 20 72 75 6e  library that run
40b0: 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 61 64  s in the same ad
40c0: 64 72 65 73 73 20 73 70 61 63 65 20 61 73 20 74  dress space as t
40d0: 68 65 20 0a 61 70 70 6c 69 63 61 74 69 6f 6e 20  he .application 
40e0: 74 68 61 74 20 69 74 20 73 65 72 76 65 73 2e 20  that it serves. 
40f0: 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   That means that
4100: 20 73 74 72 61 79 20 70 6f 69 6e 74 65 72 73 2c   stray pointers,
4110: 20 62 75 66 66 65 72 0a 6f 76 65 72 72 75 6e 73   buffer.overruns
4120: 2c 20 68 65 61 70 20 63 6f 72 72 75 70 74 69 6f  , heap corruptio
4130: 6e 2c 20 6f 72 20 6f 74 68 65 72 20 6d 61 6c 66  n, or other malf
4140: 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  unctions in the 
4150: 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 0a  application can.
4160: 63 6f 72 72 75 70 74 20 69 6e 74 65 72 6e 61 6c  corrupt internal
4170: 20 53 51 4c 69 74 65 20 64 61 74 61 20 73 74 72   SQLite data str
4180: 75 63 74 75 72 65 20 61 6e 64 20 75 6c 74 69 6d  ucture and ultim
4190: 61 74 65 6c 79 20 72 65 73 75 6c 74 20 69 6e 20  ately result in 
41a0: 61 0a 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a.corrupt databa
41b0: 73 65 20 66 69 6c 65 2e 20 20 4e 6f 72 6d 61 6c  se file.  Normal
41c0: 6c 79 20 74 68 65 73 65 20 6b 69 6e 64 73 20 6f  ly these kinds o
41d0: 66 20 70 72 6f 62 6c 65 6d 73 20 6d 61 6e 69 66  f problems manif
41e0: 65 73 74 20 74 68 65 6d 73 65 6c 76 65 73 0a 61  est themselves.a
41f0: 73 20 73 65 67 66 61 75 6c 74 73 20 70 72 69 6f  s segfaults prio
4200: 72 20 74 6f 20 61 6e 79 20 64 61 74 61 62 61 73  r to any databas
4210: 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63  e corruption occ
4220: 75 72 72 69 6e 67 2c 20 62 75 74 20 74 68 65 72  urring, but ther
4230: 65 20 68 61 76 65 0a 62 65 65 6e 20 69 6e 73 74  e have.been inst
4240: 61 6e 63 65 73 20 77 68 65 72 65 20 61 70 70 6c  ances where appl
4250: 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 65 72 72  ication code err
4260: 6f 72 73 20 68 61 76 65 20 63 61 75 73 65 64 20  ors have caused 
4270: 53 51 4c 69 74 65 20 74 6f 0a 6d 61 6c 66 75 6e  SQLite to.malfun
4280: 63 74 69 6f 6e 20 73 75 62 74 6c 79 20 73 6f 20  ction subtly so 
4290: 61 73 20 74 6f 20 63 6f 72 72 75 70 74 20 74 68  as to corrupt th
42a0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
42b0: 72 61 74 68 65 72 20 74 68 61 6e 0a 70 61 6e 69  rather than.pani
42c0: 63 6b 69 6e 67 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54  cking.</p>..<p>T
42d0: 68 65 20 6d 65 6d 6f 72 79 20 63 6f 72 72 75 70  he memory corrup
42e0: 74 69 6f 6e 20 70 72 6f 62 6c 65 6d 20 62 65 63  tion problem bec
42f0: 6f 6d 65 73 20 6d 6f 72 65 20 61 63 75 74 65 20  omes more acute 
4300: 77 68 65 6e 0a 75 73 69 6e 67 20 5b 6d 65 6d 6f  when.using [memo
4310: 72 79 2d 6d 61 70 70 65 64 20 49 2f 4f 5d 2e 0a  ry-mapped I/O]..
4320: 57 68 65 6e 20 61 6c 6c 20 6f 72 20 70 61 72 74  When all or part
4330: 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
4340: 20 66 69 6c 65 20 69 73 20 6d 61 70 70 65 64 20   file is mapped 
4350: 69 6e 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61  into the applica
4360: 74 69 6f 6e 27 73 0a 61 64 64 72 65 73 73 20 73  tion's.address s
4370: 70 61 63 65 2c 20 74 68 65 6e 20 61 20 73 74 72  pace, then a str
4380: 61 79 20 70 6f 69 6e 74 65 72 20 74 68 65 20 6f  ay pointer the o
4390: 76 65 72 77 72 69 74 65 73 20 61 6e 79 20 70 61  verwrites any pa
43a0: 72 74 20 6f 66 20 74 68 61 74 0a 6d 61 70 70 65  rt of that.mappe
43b0: 64 20 73 70 61 63 65 20 77 69 6c 6c 20 69 6d 6d  d space will imm
43c0: 65 64 69 61 74 65 6c 79 20 63 6f 72 72 75 70 74  ediately corrupt
43d0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
43e0: 6c 65 2c 20 77 69 74 68 6f 75 74 0a 72 65 71 75  le, without.requ
43f0: 69 72 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63  iring the applic
4400: 61 74 69 6f 6e 20 74 6f 20 64 6f 20 61 20 73 75  ation to do a su
4410: 62 73 65 71 75 65 6e 74 20 77 72 69 74 65 28 29  bsequent write()
4420: 20 73 79 73 74 65 6d 20 63 61 6c 6c 2e 3c 2f 70   system call.</p
4430: 3e 0a 0a 3c 68 32 3e 36 2e 30 20 4f 74 68 65 72  >..<h2>6.0 Other
4440: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
4450: 6d 20 70 72 6f 62 6c 65 6d 73 3c 2f 68 32 3e 0a  m problems</h2>.
4460: 0a 3c 70 3e 53 6f 6d 65 74 69 6d 65 73 20 6f 70  .<p>Sometimes op
4470: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
4480: 77 69 6c 6c 20 65 78 68 69 62 69 74 20 6e 6f 6e  will exhibit non
4490: 2d 73 74 61 6e 64 61 72 64 20 62 65 68 61 76 69  -standard behavi
44a0: 6f 72 20 77 68 69 63 68 0a 63 61 6e 20 6c 65 61  or which.can lea
44b0: 64 20 74 6f 20 70 72 6f 62 6c 65 6d 73 2e 20 20  d to problems.  
44c0: 53 6f 6d 65 74 69 6d 65 73 20 74 68 69 73 20 6e  Sometimes this n
44d0: 6f 6e 2d 73 74 61 6e 64 61 72 64 20 62 65 68 61  on-standard beha
44e0: 76 69 6f 72 20 69 73 20 64 65 6c 69 62 65 72 61  vior is delibera
44f0: 74 65 2c 0a 61 6e 64 20 73 6f 6d 65 74 69 6d 65  te,.and sometime
4500: 73 20 69 74 20 69 73 20 61 20 6d 69 73 74 61 6b  s it is a mistak
4510: 65 20 69 6e 20 74 68 65 20 69 6d 70 6c 65 6d 65  e in the impleme
4520: 6e 74 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 6e  ntation.  But in
4530: 20 61 6e 79 20 65 76 65 6e 74 2c 0a 69 66 20 74   any event,.if t
4540: 68 65 20 6f 70 65 72 61 74 69 6e 67 20 70 65 72  he operating per
4550: 66 6f 72 6d 73 20 64 69 66 66 65 72 65 6e 74 6c  forms differentl
4560: 79 20 66 72 6f 6d 20 74 68 65 79 20 77 61 79 20  y from they way 
4570: 53 51 4c 69 74 65 20 65 78 70 65 63 74 73 20 69  SQLite expects i
4580: 74 20 74 6f 0a 70 65 72 66 6f 72 6d 2c 20 74 68  t to.perform, th
4590: 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  e possibility of
45a0: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
45b0: 74 69 6f 6e 20 65 78 69 73 74 73 2e 3c 2f 70 3e  tion exists.</p>
45c0: 0a 0a 3c 68 33 3e 36 2e 31 20 4c 69 6e 75 78 20  ..<h3>6.1 Linux 
45d0: 54 68 72 65 61 64 73 3c 2f 68 33 3e 0a 0a 3c 70  Threads</h3>..<p
45e0: 3e 53 6f 6d 65 20 6f 6c 64 65 72 20 76 65 72 73  >Some older vers
45f0: 69 6f 6e 73 20 6f 66 20 4c 69 6e 75 78 20 75 73  ions of Linux us
4600: 65 64 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65  ed the LinuxThre
4610: 61 64 73 20 6c 69 62 72 61 72 79 20 66 6f 72 20  ads library for 
4620: 74 68 72 65 61 64 0a 73 75 70 70 6f 72 74 2e 20  thread.support. 
4630: 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 69 73   LinuxThreads is
4640: 20 73 69 6d 69 6c 61 72 20 74 6f 20 50 74 68 72   similar to Pthr
4650: 65 61 64 73 2c 20 62 75 74 20 69 73 20 73 75 62  eads, but is sub
4660: 74 6c 79 20 64 69 66 66 65 72 65 6e 74 0a 77 69  tly different.wi
4670: 74 68 20 72 65 73 70 65 63 74 20 74 6f 20 68 61  th respect to ha
4680: 6e 64 6c 69 6e 67 20 6f 66 20 50 4f 53 49 58 20  ndling of POSIX 
4690: 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 20  advisory locks. 
46a0: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 73   SQLite versions
46b0: 0a 32 2e 32 2e 33 20 74 68 72 6f 75 67 68 20 33  .2.2.3 through 3
46c0: 2e 36 2e 32 33 20 72 65 63 6f 67 6e 69 7a 65 64  .6.23 recognized
46d0: 20 74 68 61 74 20 4c 69 6e 75 78 54 68 72 65 61   that LinuxThrea
46e0: 64 73 20 77 68 65 72 65 20 62 65 69 6e 67 20 75  ds where being u
46f0: 73 65 64 20 61 74 0a 72 75 6e 74 69 6d 65 20 61  sed at.runtime a
4700: 6e 64 20 74 6f 6f 6b 20 61 70 70 72 6f 70 72 69  nd took appropri
4710: 61 74 65 20 61 63 74 69 6f 6e 20 74 6f 20 77 6f  ate action to wo
4720: 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 6e 6f  rk around the no
4730: 6e 2d 73 74 61 6e 64 61 72 64 0a 62 65 68 61 76  n-standard.behav
4740: 69 6f 72 20 6f 66 20 4c 69 6e 75 78 54 68 72 65  ior of LinuxThre
4750: 61 64 73 2e 20 20 42 75 74 20 6d 6f 73 74 20 6d  ads.  But most m
4760: 6f 64 65 72 6e 20 4c 69 6e 75 78 20 69 6d 70 6c  odern Linux impl
4770: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 6b 65  ementations make
4780: 0a 75 73 65 20 6f 66 20 74 68 65 20 6e 65 77 65  .use of the newe
4790: 72 2c 20 61 6e 64 20 63 6f 72 72 65 63 74 2c 20  r, and correct, 
47a0: 4e 50 54 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74  NPTL implementat
47b0: 69 6f 6e 20 6f 66 20 50 74 68 72 65 61 64 73 2e  ion of Pthreads.
47c0: 20 20 42 65 67 69 6e 6e 69 6e 67 0a 77 69 74 68    Beginning.with
47d0: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
47e0: 33 2e 37 2e 30 2c 20 74 68 65 20 75 73 65 20 6f  3.7.0, the use o
47f0: 66 20 4e 50 54 4c 20 69 73 20 61 73 73 75 6d 65  f NPTL is assume
4800: 64 2e 20 20 4e 6f 20 63 68 65 63 6b 73 20 61 72  d.  No checks ar
4810: 65 20 0a 6d 61 64 65 2e 20 20 48 65 6e 63 65 2c  e .made.  Hence,
4820: 20 72 65 63 65 6e 74 20 76 65 72 73 69 6f 6e 73   recent versions
4830: 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20   of SQLite will 
4840: 73 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69  subtly malfuncti
4850: 6f 6e 20 61 6e 64 20 6d 61 79 20 0a 63 6f 72 72  on and may .corr
4860: 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c  upt database fil
4870: 65 73 20 69 66 20 75 73 65 64 20 69 6e 20 6d 75  es if used in mu
4880: 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
4890: 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 72 75  lication that ru
48a0: 6e 0a 6f 6e 20 6f 6c 64 65 72 20 6c 69 6e 75 78  n.on older linux
48b0: 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 6d 61   systems that ma
48c0: 6b 65 20 75 73 65 20 6f 66 20 4c 69 6e 75 78 54  ke use of LinuxT
48d0: 68 72 65 61 64 73 2e 3c 2f 70 3e 0a 0a 3c 68 33  hreads.</p>..<h3
48e0: 3e 36 2e 32 20 46 61 69 6c 75 72 65 73 20 6f 66  >6.2 Failures of
48f0: 20 6d 6d 61 70 28 29 20 6f 6e 20 51 4e 58 3c 2f   mmap() on QNX</
4900: 68 33 3e 0a 0a 3c 70 3e 54 68 65 72 65 20 65 78  h3>..<p>There ex
4910: 69 73 74 73 20 73 6f 6d 65 20 73 75 62 74 6c 65  ists some subtle
4920: 20 70 72 6f 62 6c 65 6d 20 77 69 74 68 20 6d 6d   problem with mm
4930: 61 70 28 29 20 6f 6e 20 51 4e 58 20 73 75 63 68  ap() on QNX such
4940: 20 74 68 61 74 20 6d 61 6b 69 6e 67 0a 61 20 73   that making.a s
4950: 65 63 6f 6e 64 20 6d 6d 61 70 28 29 20 63 61 6c  econd mmap() cal
4960: 6c 20 61 67 61 69 6e 73 74 20 74 68 65 20 61 20  l against the a 
4970: 73 69 6e 67 6c 65 20 66 69 6c 65 20 64 65 73 63  single file desc
4980: 72 69 70 74 6f 72 20 63 61 6e 20 63 61 75 73 65  riptor can cause
4990: 0a 74 68 65 20 6d 65 6d 6f 72 79 20 6f 62 74 61  .the memory obta
49a0: 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69  ined from the fi
49b0: 72 73 74 20 6d 6d 61 70 28 29 20 63 61 6c 6c 20  rst mmap() call 
49c0: 74 6f 20 62 65 20 7a 65 72 6f 65 64 2e 20 20 53  to be zeroed.  S
49d0: 51 4c 69 74 65 20 6f 6e 0a 75 6e 69 78 20 75 73  QLite on.unix us
49e0: 65 73 20 6d 6d 61 70 28 29 20 74 6f 20 63 72 65  es mmap() to cre
49f0: 61 74 65 20 61 20 73 68 61 72 65 64 20 6d 65 6d  ate a shared mem
4a00: 6f 72 79 20 72 65 67 69 6f 6e 20 66 6f 72 20 74  ory region for t
4a10: 72 61 6e 73 61 63 74 69 6f 6e 20 0a 63 6f 6f 72  ransaction .coor
4a20: 64 69 6e 61 74 69 6f 6e 20 69 6e 20 5b 57 41 4c  dination in [WAL
4a30: 20 7c 20 57 41 4c 20 6d 6f 64 65 5d 2c 20 61 6e   | WAL mode], an
4a40: 64 20 69 74 20 77 69 6c 6c 20 63 61 6c 6c 20 6d  d it will call m
4a50: 6d 61 70 28 29 20 6d 75 6c 74 69 70 6c 65 20 74  map() multiple t
4a60: 69 6d 65 73 0a 66 6f 72 20 6c 61 72 67 65 20 74  imes.for large t
4a70: 72 61 6e 73 61 63 74 69 6f 6e 73 2e 20 20 54 68  ransactions.  Th
4a80: 65 20 51 4e 58 20 6d 6d 61 70 28 29 20 68 61 73  e QNX mmap() has
4a90: 20 62 65 65 6e 20 64 65 6d 6f 6e 73 74 72 61 74   been demonstrat
4aa0: 65 64 20 74 6f 20 63 6f 72 72 75 70 74 0a 64 61  ed to corrupt.da
4ab0: 74 61 62 61 73 65 20 66 69 6c 65 20 75 6e 64 65  tabase file unde
4ac0: 72 20 74 68 61 74 20 73 63 65 6e 61 72 69 6f 2e  r that scenario.
4ad0: 20 20 51 4e 58 20 65 6e 67 69 6e 65 65 72 73 20    QNX engineers 
4ae0: 61 72 65 20 61 77 61 72 65 20 6f 66 20 74 68 69  are aware of thi
4af0: 73 20 70 72 6f 62 6c 65 6d 0a 61 6e 64 20 61 72  s problem.and ar
4b00: 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 61 20 73  e working on a s
4b10: 6f 6c 75 74 69 6f 6e 3b 20 74 68 65 20 70 72 6f  olution; the pro
4b20: 62 6c 65 6d 20 6d 61 79 20 68 61 76 65 20 61 6c  blem may have al
4b30: 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64  ready been fixed
4b40: 20 62 79 0a 74 68 65 20 74 69 6d 65 20 79 6f 75   by.the time you
4b50: 20 72 65 61 64 20 74 68 69 73 2e 3c 2f 70 3e 0a   read this.</p>.
4b60: 0a 3c 70 3e 57 68 65 6e 20 72 75 6e 6e 69 6e 67  .<p>When running
4b70: 20 6f 6e 20 51 4e 58 2c 20 69 74 20 69 73 20 72   on QNX, it is r
4b80: 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20  ecommended that 
4b90: 5b 6d 65 6d 6f 72 79 2d 6d 61 70 70 65 64 20 49  [memory-mapped I
4ba0: 2f 4f 5d 20 6e 65 76 65 72 0a 62 65 20 75 73 65  /O] never.be use
4bb0: 64 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  d.  Furthermore,
4bc0: 20 74 6f 20 75 73 65 20 5b 57 41 4c 20 6d 6f 64   to use [WAL mod
4bd0: 65 5d 2c 20 69 74 20 69 73 20 72 65 63 6f 6d 6d  e], it is recomm
4be0: 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69  ended that appli
4bf0: 63 61 74 69 6f 6e 73 0a 65 6d 70 6c 6f 79 20 74  cations.employ t
4c00: 68 65 20 5b 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  he [locking_mode
4c10: 20 7c 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63   | exclusive loc
4c20: 6b 69 6e 67 20 6d 6f 64 65 5d 20 69 6e 20 6f 72  king mode] in or
4c30: 64 65 72 20 74 6f 20 0a 75 73 65 20 5b 57 41 4c  der to .use [WAL
4c40: 20 77 69 74 68 6f 75 74 20 73 68 61 72 65 64 20   without shared 
4c50: 6d 65 6d 6f 72 79 5d 2e 0a 0a 0a 3c 74 63 6c 3e  memory]....<tcl>
4c60: 68 64 5f 66 72 61 67 6d 65 6e 74 20 66 73 63 6f  hd_fragment fsco
4c70: 72 72 75 70 74 69 6f 6e 20 7b 66 69 6c 65 73 79  rruption {filesy
4c80: 73 74 65 6d 20 63 6f 72 72 75 70 74 69 6f 6e 7d  stem corruption}
4c90: 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 36 2e 33 20 46  </tcl>.<h3>6.3 F
4ca0: 69 6c 65 73 79 73 74 65 6d 20 43 6f 72 72 75 70  ilesystem Corrup
4cb0: 74 69 6f 6e 3c 2f 68 33 3e 0a 0a 3c 70 3e 53 69  tion</h3>..<p>Si
4cc0: 6e 63 65 20 53 51 4c 69 74 65 20 64 61 74 61 62  nce SQLite datab
4cd0: 61 73 65 73 20 61 72 65 20 6f 72 64 69 6e 61 72  ases are ordinar
4ce0: 79 20 64 69 73 6b 20 66 69 6c 65 73 2c 20 61 6e  y disk files, an
4cf0: 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 69 6e  y malfunction in
4d00: 20 74 68 65 0a 66 69 6c 65 73 79 73 74 65 6d 20   the.filesystem 
4d10: 63 61 6e 20 63 6f 72 72 75 70 74 20 74 68 65 20  can corrupt the 
4d20: 64 61 74 61 62 61 73 65 2e 20 20 46 69 6c 65 73  database.  Files
4d30: 79 73 74 65 6d 73 20 69 6e 20 6d 6f 64 65 72 6e  ystems in modern
4d40: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
4d50: 6d 73 0a 61 72 65 20 76 65 72 79 20 72 65 6c 69  ms.are very reli
4d60: 61 62 6c 65 2c 20 62 75 74 20 65 72 72 6f 72 73  able, but errors
4d70: 20 64 6f 20 73 74 69 6c 6c 20 6f 63 63 75 72 2e   do still occur.
4d80: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f    For example, o
4d90: 6e 20 32 30 31 33 2d 31 30 2d 30 31 0a 74 68 65  n 2013-10-01.the
4da0: 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
4db0: 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0a   that holds the.
4dc0: 3c 61 20 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f  <a href="http://
4dd0: 77 69 6b 69 2e 74 63 6c 2e 74 6b 2f 22 3e 57 69  wiki.tcl.tk/">Wi
4de0: 6b 69 20 66 6f 72 20 54 63 6c 2f 54 6b 3c 2f 61  ki for Tcl/Tk</a
4df0: 3e 20 77 65 6e 74 20 63 6f 72 72 75 70 74 20 61  > went corrupt a
4e00: 20 66 65 77 20 64 61 79 73 0a 61 66 74 65 72 20   few days.after 
4e10: 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65  the host compute
4e20: 72 20 77 61 73 20 6d 6f 76 65 64 20 74 6f 20 61  r was moved to a
4e30: 20 64 6f 64 67 79 20 62 75 69 6c 64 20 6f 66 20   dodgy build of 
4e40: 74 68 65 20 28 6c 69 6e 75 78 29 20 6b 65 72 6e  the (linux) kern
4e50: 65 6c 0a 74 68 61 74 20 68 61 64 20 69 73 73 75  el.that had issu
4e60: 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79  es in the filesy
4e70: 73 74 65 6d 20 6c 61 79 65 72 2e 20 20 49 6e 20  stem layer.  In 
4e80: 74 68 61 74 20 65 76 65 6e 74 2c 20 74 68 65 20  that event, the 
4e90: 66 69 6c 65 73 79 73 74 65 6d 0a 65 76 65 6e 74  filesystem.event
4ea0: 75 61 6c 6c 79 20 62 65 63 61 6d 65 20 73 6f 20  ually became so 
4eb0: 62 61 64 6c 79 20 63 6f 72 72 75 70 74 65 64 20  badly corrupted 
4ec0: 74 68 61 74 20 74 68 65 20 6d 61 63 68 69 6e 65  that the machine
4ed0: 20 77 61 73 20 75 6e 75 73 61 62 6c 65 2c 20 62   was unusable, b
4ee0: 75 74 0a 74 68 65 20 65 61 72 6c 69 65 73 74 20  ut.the earliest 
4ef0: 73 79 6d 70 74 6f 6d 20 6f 66 20 74 72 6f 75 62  symptom of troub
4f00: 6c 65 20 77 61 73 20 74 68 65 20 63 6f 72 72 75  le was the corru
4f10: 70 74 65 64 20 53 51 4c 69 74 65 20 64 61 74 61  pted SQLite data
4f20: 62 61 73 65 2e 3c 2f 70 3e 0a 0a 3c 68 32 3e 37  base.</p>..<h2>7
4f30: 2e 30 20 42 75 67 73 20 69 6e 20 53 51 4c 69 74  .0 Bugs in SQLit
4f40: 65 3c 2f 68 32 3e 0a 0a 3c 70 3e 53 51 4c 69 74  e</h2>..<p>SQLit
4f50: 65 20 69 73 20 5b 74 65 73 74 69 6e 67 20 7c 20  e is [testing | 
4f60: 76 65 72 79 20 63 61 72 65 66 75 6c 6c 79 20 74  very carefully t
4f70: 65 73 74 65 64 5d 20 74 6f 20 68 65 6c 70 20 65  ested] to help e
4f80: 6e 73 75 72 65 20 74 68 61 74 20 69 74 20 69 73  nsure that it is
4f90: 0a 61 73 20 62 75 67 2d 66 72 65 65 20 61 73 20  .as bug-free as 
4fa0: 70 6f 73 73 69 62 6c 65 2e 20 20 41 6d 6f 6e 67  possible.  Among
4fb0: 20 74 68 65 20 6d 61 6e 79 20 74 65 73 74 73 20   the many tests 
4fc0: 74 68 61 74 20 61 72 65 20 63 61 72 72 69 65 64  that are carried
4fd0: 20 6f 75 74 20 66 6f 72 0a 65 76 65 72 79 20 53   out for.every S
4fe0: 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 61 72  QLite version ar
4ff0: 65 20 74 65 73 74 73 20 74 68 61 74 20 73 69 6d  e tests that sim
5000: 75 6c 61 74 65 20 70 6f 77 65 72 20 66 61 69 6c  ulate power fail
5010: 75 72 65 73 2c 20 49 2f 4f 20 65 72 72 6f 72 73  ures, I/O errors
5020: 2c 0a 61 6e 64 20 6f 75 74 2d 6f 66 2d 6d 65 6d  ,.and out-of-mem
5030: 6f 72 79 20 28 4f 4f 4d 29 20 65 72 72 6f 72 73  ory (OOM) errors
5040: 20 61 6e 64 20 76 65 72 69 66 79 20 74 68 61 74   and verify that
5050: 20 6e 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72   no database cor
5060: 72 75 70 74 20 6f 63 63 75 72 73 0a 64 75 72 69  rupt occurs.duri
5070: 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  ng any of these 
5080: 65 76 65 6e 74 73 2e 20 20 53 51 4c 69 74 65 20  events.  SQLite 
5090: 69 73 20 61 6c 73 6f 20 66 69 65 6c 64 2d 70 72  is also field-pr
50a0: 6f 76 65 6e 20 77 69 74 68 20 61 70 70 72 6f 78  oven with approx
50b0: 69 6d 61 74 65 6c 79 0a 74 77 6f 20 62 69 6c 6c  imately.two bill
50c0: 69 6f 6e 20 61 63 74 69 76 65 20 64 65 70 6c 6f  ion active deplo
50d0: 79 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f 20 73  yments with no s
50e0: 65 72 69 6f 75 73 20 70 72 6f 62 6c 65 6d 73 2e  erious problems.
50f0: 3c 2f 70 3e 0a 0a 3c 70 3e 4e 65 76 65 72 74 68  </p>..<p>Neverth
5100: 65 6c 65 73 73 2c 20 6e 6f 20 73 6f 66 74 77 61  eless, no softwa
5110: 72 65 20 69 73 20 31 30 30 25 20 70 65 72 66 65  re is 100% perfe
5120: 63 74 2e 20 20 54 68 65 72 65 20 68 61 76 65 20  ct.  There have 
5130: 62 65 65 6e 20 61 20 66 65 77 0a 68 69 73 74 6f  been a few.histo
5140: 72 69 63 61 6c 20 62 75 67 73 20 69 6e 20 53 51  rical bugs in SQ
5150: 4c 69 74 65 20 28 6e 6f 77 20 66 69 78 65 64 29  Lite (now fixed)
5160: 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73   that could caus
5170: 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  e database corru
5180: 70 74 69 6f 6e 2e 0a 41 6e 64 20 74 68 65 72 65  ption..And there
5190: 20 6d 61 79 20 62 65 20 79 65 74 20 61 20 66 65   may be yet a fe
51a0: 77 20 6d 6f 72 65 20 74 68 61 74 20 72 65 6d 61  w more that rema
51b0: 69 6e 20 75 6e 64 69 73 63 6f 76 65 72 65 64 2e  in undiscovered.
51c0: 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65    Because of the
51d0: 0a 65 78 74 65 6e 73 69 76 65 20 74 65 73 74 69  .extensive testi
51e0: 6e 67 20 61 6e 64 20 77 69 64 65 73 70 72 65 61  ng and widesprea
51f0: 64 20 75 73 65 20 6f 66 20 53 51 4c 69 74 65 2c  d use of SQLite,
5200: 20 62 75 67 73 20 74 68 61 74 20 72 65 73 75 6c   bugs that resul
5210: 74 20 69 6e 0a 64 61 74 61 62 61 73 65 20 63 6f  t in.database co
5220: 72 72 75 70 74 69 6f 6e 20 74 65 6e 64 20 74 6f  rruption tend to
5230: 20 62 65 20 76 65 72 79 20 6f 62 73 63 75 72 65   be very obscure
5240: 2e 20 20 54 68 65 20 6c 69 6b 65 6c 69 68 6f 6f  .  The likelihoo
5250: 64 0a 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74  d.of an applicat
5260: 69 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67  ion encountering
5270: 20 61 6e 20 53 51 4c 69 74 65 20 62 75 67 20 69   an SQLite bug i
5280: 73 20 73 6d 61 6c 6c 2e 20 20 54 6f 20 69 6c 6c  s small.  To ill
5290: 75 73 74 72 61 74 65 20 74 68 69 73 2c 0a 61 6e  ustrate this,.an
52a0: 20 61 63 63 6f 75 6e 74 20 69 73 20 67 69 76 65   account is give
52b0: 6e 20 62 65 6c 6f 77 20 0a 6f 66 20 61 6c 6c 20  n below .of all 
52c0: 64 61 74 61 62 61 73 65 2d 63 6f 72 72 75 70 74  database-corrupt
52d0: 69 6f 6e 20 62 75 67 73 20 66 6f 75 6e 64 20 69  ion bugs found i
52e0: 6e 20 53 51 4c 69 74 65 20 64 75 72 69 6e 67 20  n SQLite during 
52f0: 74 68 65 0a 66 6f 75 72 2d 79 65 61 72 20 70 65  the.four-year pe
5300: 72 69 6f 64 20 66 72 6f 6d 20 32 30 30 39 2d 30  riod from 2009-0
5310: 34 2d 30 31 20 74 6f 20 32 30 31 33 2d 30 34 2d  4-01 to 2013-04-
5320: 31 35 2e 0a 54 68 69 73 20 61 63 63 6f 75 6e 74  15..This account
5330: 20 73 68 6f 75 6c 64 20 67 69 76 65 20 74 68 65   should give the
5340: 20 72 65 61 64 65 72 20 61 6e 20 69 6e 74 75 69   reader an intui
5350: 74 69 76 65 20 73 65 6e 73 65 20 6f 66 20 74 68  tive sense of th
5360: 65 0a 6b 69 6e 64 73 20 6f 66 20 62 75 67 73 20  e.kinds of bugs 
5370: 69 6e 20 53 51 4c 69 74 65 20 74 68 61 74 20 6d  in SQLite that m
5380: 61 6e 61 67 65 20 74 6f 20 73 6c 69 70 20 74 68  anage to slip th
5390: 72 6f 75 67 68 20 74 65 73 74 69 6e 67 20 70 72  rough testing pr
53a0: 6f 63 65 64 75 72 65 73 0a 61 6e 64 20 6d 61 6b  ocedures.and mak
53b0: 65 20 69 74 20 69 6e 74 6f 20 61 20 72 65 6c 65  e it into a rele
53c0: 61 73 65 2e 3c 2f 70 3e 0a 0a 0a 3c 68 33 3e 37  ase.</p>...<h3>7
53d0: 2e 31 20 46 61 6c 73 65 20 63 6f 72 72 75 70 74  .1 False corrupt
53e0: 69 6f 6e 20 72 65 70 6f 72 74 73 20 64 75 65 20  ion reports due 
53f0: 74 6f 20 64 61 74 61 62 61 73 65 20 73 68 72 69  to database shri
5400: 6e 6b 61 67 65 3c 2f 68 33 3e 0a 0a 3c 70 3e 49  nkage</h3>..<p>I
5410: 66 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20  f a database is 
5420: 77 72 69 74 74 65 6e 20 62 79 20 53 51 4c 69 74  written by SQLit
5430: 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 20  e version 3.7.0 
5440: 6f 72 20 6c 61 74 65 72 20 61 6e 64 20 74 68 65  or later and the
5450: 6e 0a 77 72 69 74 74 65 6e 20 61 67 61 69 6e 20  n.written again 
5460: 62 79 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  by SQLite versio
5470: 6e 20 33 2e 36 2e 32 33 20 6f 72 20 65 61 72 6c  n 3.6.23 or earl
5480: 69 65 72 20 69 6e 20 73 75 63 68 20 61 20 77 61  ier in such a wa
5490: 79 20 61 73 20 74 6f 0a 6d 61 6b 65 20 74 68 65  y as to.make the
54a0: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
54b0: 61 62 61 73 65 20 66 69 6c 65 20 64 65 63 72 65  abase file decre
54c0: 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65  ase, then the ne
54d0: 78 74 20 74 69 6d 65 20 74 68 61 74 0a 53 51 4c  xt time that.SQL
54e0: 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e  ite version 3.7.
54f0: 30 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74  0 access the dat
5500: 61 62 61 73 65 20 66 69 6c 65 2c 20 69 74 20 6d  abase file, it m
5510: 69 67 68 74 20 72 65 70 6f 72 74 20 74 68 61 74  ight report that
5520: 20 74 68 65 0a 64 61 74 61 62 61 73 65 20 66 69   the.database fi
5530: 6c 65 20 69 73 20 63 6f 72 72 75 70 74 2e 20 20  le is corrupt.  
5540: 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
5550: 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20  e is not really 
5560: 63 6f 72 72 75 70 74 2c 20 68 6f 77 65 76 65 72  corrupt, however
5570: 2e 0a 56 65 72 73 69 6f 6e 20 33 2e 37 2e 30 20  ..Version 3.7.0 
5580: 77 61 73 20 73 69 6d 70 6c 79 20 62 65 69 6e 67  was simply being
5590: 20 6f 76 65 72 6c 79 20 7a 65 61 6c 6f 75 73 20   overly zealous 
55a0: 69 6e 20 69 74 73 20 63 6f 72 72 75 70 74 69 6f  in its corruptio
55b0: 6e 20 64 65 74 65 63 74 69 6f 6e 2e 3c 2f 70 3e  n detection.</p>
55c0: 0a 0a 3c 70 3e 54 68 65 20 70 72 6f 62 6c 65 6d  ..<p>The problem
55d0: 20 77 61 73 20 66 69 78 65 64 20 6f 6e 20 32 30   was fixed on 20
55e0: 31 31 2d 30 32 2d 32 30 2e 20 20 54 68 65 20 66  11-02-20.  The f
55f0: 69 78 20 66 69 72 73 74 20 61 70 70 65 61 72 73  ix first appears
5600: 20 69 6e 0a 53 51 4c 69 74 65 20 76 65 72 73 69   in.SQLite versi
5610: 6f 6e 20 33 2e 37 2e 36 2e 3c 2f 70 3e 0a 0a 3c  on 3.7.6.</p>..<
5620: 68 33 3e 37 2e 32 20 43 6f 72 72 75 70 74 69 6f  h3>7.2 Corruptio
5630: 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 77 69 74  n following swit
5640: 63 68 65 73 20 62 65 74 77 65 65 6e 20 72 6f 6c  ches between rol
5650: 6c 62 61 63 6b 20 61 6e 64 20 57 41 4c 20 6d 6f  lback and WAL mo
5660: 64 65 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 52 65 70  des</h3>..<p>Rep
5670: 65 61 74 65 64 6c 79 20 73 77 69 74 63 68 69 6e  eatedly switchin
5680: 67 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  g an SQLite data
5690: 62 61 73 65 20 69 6e 20 61 6e 64 20 6f 75 74 20  base in and out 
56a0: 6f 66 20 5b 57 41 4c 20 7c 20 57 41 4c 20 6d 6f  of [WAL | WAL mo
56b0: 64 65 5d 0a 61 6e 64 20 72 75 6e 6e 69 6e 67 20  de].and running 
56c0: 74 68 65 20 5b 56 41 43 55 55 4d 5d 20 63 6f 6d  the [VACUUM] com
56d0: 6d 61 6e 64 20 69 6e 20 62 65 74 77 65 65 6e 20  mand in between 
56e0: 73 77 69 74 63 68 65 73 2c 20 69 6e 20 6f 6e 65  switches, in one
56f0: 20 70 72 6f 63 65 73 73 20 6f 72 0a 74 68 72 65   process or.thre
5700: 61 64 2c 20 63 61 6e 20 63 61 75 73 65 20 61 6e  ad, can cause an
5710: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6f 72  other process or
5720: 20 74 68 72 65 61 64 20 74 68 61 74 20 68 61 73   thread that has
5730: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
5740: 6c 65 0a 6f 70 65 6e 20 74 6f 20 6d 69 73 73 20  le.open to miss 
5750: 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
5760: 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63  e database has c
5770: 68 61 6e 67 65 64 2e 20 20 54 68 61 74 20 73 65  hanged.  That se
5780: 63 6f 6e 64 20 70 72 6f 63 65 73 73 0a 6f 72 20  cond process.or 
5790: 74 68 72 65 61 64 20 6d 69 67 68 74 20 74 68 65  thread might the
57a0: 6e 20 74 72 79 20 74 6f 20 6d 6f 64 69 66 79 20  n try to modify 
57b0: 74 68 65 20 64 61 74 61 62 61 73 65 20 75 73 69  the database usi
57c0: 6e 67 20 61 20 73 74 61 6c 65 20 63 61 63 68 65  ng a stale cache
57d0: 20 61 6e 64 0a 63 61 75 73 65 20 64 61 74 61 62   and.cause datab
57e0: 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 3c  ase corruption.<
57f0: 2f 70 3e 0a 0a 3c 70 3e 54 68 69 73 20 70 72 6f  /p>..<p>This pro
5800: 62 6c 65 6d 20 77 61 73 20 64 69 73 63 6f 76 65  blem was discove
5810: 72 65 64 20 64 75 72 69 6e 67 20 69 6e 74 65 72  red during inter
5820: 6e 61 6c 20 74 65 73 74 69 6e 67 20 61 6e 64 20  nal testing and 
5830: 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 0a 6f  has never been.o
5840: 62 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 77  bserved in the w
5850: 69 6c 64 2e 20 20 54 68 65 20 70 72 6f 62 6c 65  ild.  The proble
5860: 6d 20 77 61 73 20 66 69 78 65 64 20 6f 6e 20 32  m was fixed on 2
5870: 30 31 31 2d 30 31 2d 32 37 20 61 6e 64 20 69 6e  011-01-27 and in
5880: 20 76 65 72 73 69 6f 6e 0a 33 2e 37 2e 35 2e 3c   version.3.7.5.<
5890: 2f 70 3e 0a 0a 3c 68 33 3e 37 2e 33 20 49 2f 4f  /p>..<h3>7.3 I/O
58a0: 20 77 68 69 6c 65 20 6f 62 74 61 69 6e 69 6e 67   while obtaining
58b0: 20 61 20 6c 6f 63 6b 20 6c 65 61 64 73 20 74 6f   a lock leads to
58c0: 20 63 6f 72 72 75 70 74 69 6f 6e 3c 2f 68 33 3e   corruption</h3>
58d0: 0a 0a 3c 70 3e 49 66 20 74 68 65 20 6f 70 65 72  ..<p>If the oper
58e0: 61 74 69 6e 67 20 73 79 73 74 65 6d 20 72 65 74  ating system ret
58f0: 75 72 6e 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f  urns an I/O erro
5900: 72 20 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69  r while attempti
5910: 6e 67 20 74 6f 20 6f 62 74 61 69 6e 0a 61 20 63  ng to obtain.a c
5920: 65 72 74 61 69 6e 20 6c 6f 63 6b 20 6f 6e 20 73  ertain lock on s
5930: 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 6e 20  hared memory in 
5940: 5b 57 41 4c 20 7c 20 57 41 4c 20 6d 6f 64 65 5d  [WAL | WAL mode]
5950: 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 69 67   then SQLite mig
5960: 68 74 20 66 61 69 6c 20 0a 74 6f 20 72 65 73 65  ht fail .to rese
5970: 74 20 69 74 73 20 63 61 63 68 65 2c 0a 77 68 69  t its cache,.whi
5980: 63 68 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  ch could lead to
5990: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
59a0: 74 69 6f 6e 20 69 66 20 73 75 62 73 65 71 75 65  tion if subseque
59b0: 6e 74 20 77 72 69 74 65 73 20 61 72 65 20 61 74  nt writes are at
59c0: 74 65 6d 70 74 65 64 2e 3c 2f 70 3e 0a 0a 3c 70  tempted.</p>..<p
59d0: 3e 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20  >Note that this 
59e0: 70 72 6f 62 6c 65 6d 20 6f 6e 6c 79 20 6f 63 63  problem only occ
59f0: 75 72 73 20 69 66 20 74 68 65 20 61 74 74 65 6d  urs if the attem
5a00: 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  pt to acquire th
5a10: 65 20 6c 6f 63 6b 0a 72 65 73 75 6c 74 65 64 20  e lock.resulted 
5a20: 69 6e 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2e  in an I/O error.
5a30: 20 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73    If the lock is
5a40: 20 73 69 6d 70 6c 79 20 6e 6f 74 20 67 72 61 6e   simply not gran
5a50: 74 65 64 20 28 62 65 63 61 75 73 65 20 73 6f 6d  ted (because som
5a60: 65 0a 6f 74 68 65 72 20 74 68 72 65 61 64 20 6f  e.other thread o
5a70: 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 72  r process is alr
5a80: 65 61 64 79 20 68 6f 6c 64 69 6e 67 20 61 20 63  eady holding a c
5a90: 6f 6e 66 6c 69 63 74 69 6e 67 20 6c 6f 63 6b 29  onflicting lock)
5aa0: 20 74 68 65 6e 20 6e 6f 0a 63 6f 72 72 75 70 74   then no.corrupt
5ab0: 69 6f 6e 20 77 69 6c 6c 20 65 76 65 72 20 6f 63  ion will ever oc
5ac0: 63 75 72 2e 20 20 57 65 20 61 72 65 20 6e 6f 74  cur.  We are not
5ad0: 20 61 77 61 72 65 20 6f 66 20 61 6e 79 20 6f 70   aware of any op
5ae0: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
5af0: 74 68 61 74 0a 77 69 6c 6c 20 66 61 69 6c 20 77  that.will fail w
5b00: 69 74 68 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ith an I/O error
5b10: 20 77 68 69 6c 65 20 61 74 74 65 6d 70 74 69 6e   while attemptin
5b20: 67 20 74 6f 20 67 65 74 20 61 20 66 69 6c 65 20  g to get a file 
5b30: 6c 6f 63 6b 20 6f 6e 20 73 68 61 72 65 64 0a 6d  lock on shared.m
5b40: 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 69 73 20  emory.  So this 
5b50: 69 73 20 61 20 74 68 65 6f 72 65 74 69 63 61 6c  is a theoretical
5b60: 20 70 72 6f 62 6c 65 6d 20 72 61 74 68 65 72 20   problem rather 
5b70: 74 68 61 6e 20 61 20 72 65 61 6c 20 70 72 6f 62  than a real prob
5b80: 6c 65 6d 2e 0a 4e 65 65 64 6c 65 73 73 20 74 6f  lem..Needless to
5b90: 20 73 61 79 2c 20 74 68 69 73 20 70 72 6f 62 6c   say, this probl
5ba0: 65 6d 20 68 61 73 20 6e 65 76 65 72 20 62 65 65  em has never bee
5bb0: 6e 20 6f 62 73 65 72 76 65 64 20 69 6e 20 74 68  n observed in th
5bc0: 65 20 77 69 6c 64 2e 20 20 54 68 65 0a 70 72 6f  e wild.  The.pro
5bd0: 62 6c 65 6d 20 77 61 73 20 64 69 73 63 6f 76 65  blem was discove
5be0: 72 65 64 20 77 68 69 6c 65 20 64 6f 69 6e 67 20  red while doing 
5bf0: 73 74 72 65 73 73 20 74 65 73 74 69 6e 67 20 6f  stress testing o
5c00: 66 20 53 51 4c 69 74 65 20 69 6e 20 61 20 74 65  f SQLite in a te
5c10: 73 74 0a 68 61 72 6e 65 73 73 20 74 68 61 74 20  st.harness that 
5c20: 73 69 6d 75 6c 61 74 65 73 20 49 2f 4f 20 65 72  simulates I/O er
5c30: 72 6f 72 73 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68  rors.</p>..<p>Th
5c40: 69 73 20 70 72 6f 62 6c 65 6d 20 77 61 73 20 66  is problem was f
5c50: 69 78 65 64 20 6f 6e 20 32 30 31 30 2d 30 39 2d  ixed on 2010-09-
5c60: 32 30 20 66 6f 72 20 53 51 4c 69 74 65 20 76 65  20 for SQLite ve
5c70: 72 73 69 6f 6e 20 33 2e 37 2e 33 2e 3c 2f 70 3e  rsion 3.7.3.</p>
5c80: 0a 0a 3c 68 33 3e 37 2e 34 20 44 61 74 61 62 61  ..<h3>7.4 Databa
5c90: 73 65 20 70 61 67 65 73 20 6c 65 61 6b 20 66 72  se pages leak fr
5ca0: 6f 6d 20 74 68 65 20 66 72 65 65 20 70 61 67 65  om the free page
5cb0: 20 6c 69 73 74 3c 2f 68 33 3e 0a 0a 3c 70 3e 57   list</h3>..<p>W
5cc0: 68 65 6e 20 63 6f 6e 74 65 6e 74 20 69 73 20 64  hen content is d
5cd0: 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 53  eleted from an S
5ce0: 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2c 20  QLite database, 
5cf0: 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 6e  pages that are n
5d00: 6f 0a 6c 6f 6e 67 65 72 20 75 73 65 64 20 61 72  o.longer used ar
5d10: 65 20 61 64 64 65 64 20 74 6f 20 61 20 66 72 65  e added to a fre
5d20: 65 20 6c 69 73 74 20 61 6e 64 20 61 72 65 20 72  e list and are r
5d30: 65 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 63 6f  eused to hold co
5d40: 6e 74 65 6e 74 0a 61 64 64 65 64 20 62 75 74 20  ntent.added but 
5d50: 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72  subsequent inser
5d60: 74 73 2e 20 20 41 20 62 75 67 20 69 6e 20 53 51  ts.  A bug in SQ
5d70: 4c 69 74 65 20 74 68 61 74 20 77 61 73 20 70 72  Lite that was pr
5d80: 65 73 65 6e 74 20 69 6e 0a 76 65 72 73 69 6f 6e  esent in.version
5d90: 20 33 2e 36 2e 31 36 20 74 68 72 6f 75 67 68 20   3.6.16 through 
5da0: 33 2e 37 2e 32 20 6d 69 67 68 74 20 63 61 75 73  3.7.2 might caus
5db0: 65 20 70 61 67 65 73 20 74 6f 20 67 6f 20 6d 69  e pages to go mi
5dc0: 73 73 69 6e 67 20 6f 75 74 20 6f 66 0a 74 68 65  ssing out of.the
5dd0: 20 66 72 65 65 20 6c 69 73 74 20 77 68 65 6e 20   free list when 
5de0: 5b 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63  [incremental_vac
5df0: 75 75 6d 5d 20 77 61 73 20 75 73 65 64 2e 20 20  uum] was used.  
5e00: 54 68 69 73 20 77 6f 75 6c 64 20 6e 6f 74 20 63  This would not c
5e10: 61 75 73 65 0a 64 61 74 61 20 6c 6f 73 73 2e 20  ause.data loss. 
5e20: 20 42 75 74 20 69 74 20 77 6f 75 6c 64 20 72 65   But it would re
5e30: 73 75 6c 74 20 69 6e 20 74 68 65 20 64 61 74 61  sult in the data
5e40: 62 61 73 65 20 66 69 6c 65 20 62 65 69 6e 67 20  base file being 
5e50: 6c 61 72 67 65 72 20 74 68 61 6e 0a 6e 65 63 65  larger than.nece
5e60: 73 73 61 72 79 2e 20 20 41 6e 64 20 69 74 20 77  ssary.  And it w
5e70: 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65 20 5b  ould cause the [
5e80: 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20  integrity_check 
5e90: 70 72 61 67 6d 61 5d 20 74 6f 20 72 65 70 6f 72  pragma] to repor
5ea0: 74 0a 70 61 67 65 73 20 6d 69 73 73 69 6e 67 20  t.pages missing 
5eb0: 66 72 6f 6d 20 74 68 65 20 66 72 65 65 20 6c 69  from the free li
5ec0: 73 74 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 69 73  st.</p>..<p>This
5ed0: 20 70 72 6f 62 6c 65 6d 20 77 61 73 20 66 69 78   problem was fix
5ee0: 65 64 20 6f 6e 20 32 30 31 30 2d 30 38 2d 32 33  ed on 2010-08-23
5ef0: 20 66 6f 72 20 53 51 4c 69 74 65 20 76 65 72 73   for SQLite vers
5f00: 69 6f 6e 20 33 2e 37 2e 32 2e 3c 2f 70 3e 0a 0a  ion 3.7.2.</p>..
5f10: 3c 68 33 3e 37 2e 35 20 43 6f 72 72 75 70 74 69  <h3>7.5 Corrupti
5f20: 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 74  on following alt
5f30: 65 72 6e 61 74 69 6e 67 20 77 72 69 74 65 73 20  ernating writes 
5f40: 66 72 6f 6d 20 33 2e 36 20 61 6e 64 20 33 2e 37  from 3.6 and 3.7
5f50: 2e 3c 2f 68 33 3e 0a 0a 3c 70 3e 53 51 4c 69 74  .</h3>..<p>SQLit
5f60: 65 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 30 20  e version 3.7.0 
5f70: 69 6e 74 72 6f 64 75 63 65 64 20 61 20 6e 75 6d  introduced a num
5f80: 62 65 72 20 6f 66 20 6e 65 77 20 65 6e 68 61 6e  ber of new enhan
5f90: 63 65 6d 65 6e 74 73 20 74 6f 0a 74 68 65 20 53  cements to.the S
5fa0: 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 66  QLite database f
5fb0: 69 6c 65 20 66 6f 72 6d 61 74 20 28 73 75 63 68  ile format (such
5fc0: 20 61 73 20 62 75 74 20 6e 6f 74 20 6c 69 6d 69   as but not limi
5fd0: 74 65 64 20 74 6f 20 5b 57 41 4c 5d 29 2e 0a 54  ted to [WAL])..T
5fe0: 68 65 20 33 2e 37 2e 30 20 72 65 6c 65 61 73 65  he 3.7.0 release
5ff0: 20 77 61 73 20 61 20 73 68 61 6b 65 2d 6f 75 74   was a shake-out
6000: 20 72 65 6c 65 61 73 65 20 66 6f 72 20 74 68 65   release for the
6010: 73 65 20 6e 65 77 20 66 65 61 74 75 72 65 73 2e  se new features.
6020: 20 0a 57 65 20 65 78 70 65 63 74 65 64 20 74 6f   .We expected to
6030: 20 66 69 6e 64 20 70 72 6f 62 6c 65 6d 73 20 61   find problems a
6040: 6e 64 20 77 65 72 65 20 6e 6f 74 20 64 69 73 61  nd were not disa
6050: 70 70 6f 69 6e 74 65 64 2e 3c 2f 70 3e 0a 0a 3c  ppointed.</p>..<
6060: 70 3e 49 66 20 61 20 64 61 74 61 62 61 73 65 20  p>If a database 
6070: 77 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 20  were originally 
6080: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 51  created using SQ
6090: 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 37  Lite version 3.7
60a0: 2e 30 2c 0a 74 68 65 6e 20 77 72 69 74 74 65 6e  .0,.then written
60b0: 20 62 79 20 53 51 4c 69 74 65 20 76 65 72 73 69   by SQLite versi
60c0: 6f 6e 20 33 2e 36 2e 32 33 2e 31 20 73 75 63 68  on 3.6.23.1 such
60d0: 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 6f   that the size o
60e0: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 66  f the database.f
60f0: 69 6c 65 20 69 6e 63 72 65 61 73 65 64 2c 20 74  ile increased, t
6100: 68 65 6e 20 77 72 69 74 74 65 6e 20 61 67 61 69  hen written agai
6110: 6e 20 62 79 20 53 51 4c 69 74 65 20 76 65 72 73  n by SQLite vers
6120: 69 6f 6e 20 33 2e 37 2e 30 2c 20 74 68 65 20 64  ion 3.7.0, the d
6130: 61 74 61 62 61 73 65 0a 66 69 6c 65 20 63 6f 75  atabase.file cou
6140: 6c 64 20 67 6f 20 63 6f 72 72 75 70 74 2e 3c 2f  ld go corrupt.</
6150: 70 3e 0a 0a 3c 70 3e 54 68 69 73 20 70 72 6f 62  p>..<p>This prob
6160: 6c 65 6d 20 77 61 73 20 66 69 78 65 64 20 6f 6e  lem was fixed on
6170: 20 32 30 31 30 2d 30 38 2d 30 34 20 66 6f 72 20   2010-08-04 for 
6180: 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
6190: 2e 37 2e 31 2e 3c 2f 70 3e 0a 0a 3c 68 33 3e 37  .7.1.</p>..<h3>7
61a0: 2e 36 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f  .6 Race conditio
61b0: 6e 20 69 6e 20 72 65 63 6f 76 65 72 79 20 6f 6e  n in recovery on
61c0: 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 2e   windows system.
61d0: 3c 2f 68 33 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65  </h3>..<p>SQLite
61e0: 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 31 36 2e   version 3.7.16.
61f0: 32 20 66 69 78 65 73 20 61 20 73 75 62 74 6c 65  2 fixes a subtle
6200: 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20   race condition 
6210: 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 6c  in the locking.l
6220: 6f 67 69 63 20 6f 6e 20 57 69 6e 64 6f 77 73 20  ogic on Windows 
6230: 73 79 73 74 65 6d 73 2e 20 20 57 68 65 6e 20 61  systems.  When a
6240: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
6250: 73 20 69 6e 20 6e 65 65 64 0a 6f 66 20 72 65 63  s in need.of rec
6260: 6f 76 65 72 79 20 62 65 63 61 75 73 65 20 74 68  overy because th
6270: 65 20 70 72 65 76 69 6f 75 73 20 70 72 6f 63 65  e previous proce
6280: 73 73 20 77 72 69 74 69 6e 67 20 74 6f 20 69 74  ss writing to it
6290: 20 63 72 61 73 68 65 64 20 69 6e 20 74 68 65 0a   crashed in the.
62a0: 6d 69 64 64 6c 65 20 6f 66 20 61 20 74 72 61 6e  middle of a tran
62b0: 73 61 63 74 69 6f 6e 20 61 6e 64 20 74 77 6f 20  saction and two 
62c0: 6f 72 20 6d 6f 72 65 20 70 72 6f 63 65 73 73 65  or more processe
62d0: 73 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 74 68  s try to open th
62e0: 65 20 0a 74 68 61 74 20 64 61 74 61 62 61 73 65  e .that database
62f0: 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
6300: 65 2c 20 74 68 65 6e 20 74 68 65 20 72 61 63 65  e, then the race
6310: 20 63 6f 6e 64 69 74 69 6f 6e 20 6d 69 67 68 74   condition might
6320: 20 63 61 75 73 65 0a 6f 6e 65 20 6f 66 20 74 68   cause.one of th
6330: 6f 73 65 20 70 72 6f 63 65 73 73 65 73 20 74 6f  ose processes to
6340: 20 67 65 74 20 61 20 66 61 6c 73 65 20 69 6e 64   get a false ind
6350: 69 63 61 74 69 6f 6e 20 74 68 61 74 20 74 68 65  ication that the
6360: 20 72 65 63 6f 76 65 72 79 20 0a 68 61 73 20 61   recovery .has a
6370: 6c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 64  lready completed
6380: 2c 20 61 6c 6c 6f 77 69 6e 67 20 74 68 61 74 20  , allowing that 
6390: 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 6e 74 69  process to conti
63a0: 6e 75 65 20 75 73 69 6e 67 20 74 68 65 0a 64 61  nue using the.da
63b0: 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 74 68  tabase file with
63c0: 6f 75 74 20 72 75 6e 6e 69 6e 67 20 72 65 63 6f  out running reco
63d0: 76 65 72 79 20 66 69 72 73 74 2e 20 20 49 66 20  very first.  If 
63e0: 74 68 61 74 20 70 72 6f 63 65 73 73 20 77 72 69  that process wri
63f0: 74 65 73 0a 74 6f 20 74 68 65 20 66 69 6c 65 2c  tes.to the file,
6400: 20 74 68 65 6e 20 74 68 65 20 66 69 6c 65 20 6d   then the file m
6410: 69 67 68 74 20 67 6f 20 63 6f 72 72 75 70 74 2e  ight go corrupt.
6420: 20 20 54 68 69 73 20 72 61 63 65 20 63 6f 6e 64    This race cond
6430: 69 74 69 6f 6e 0a 68 61 64 20 61 70 70 61 72 65  ition.had appare
6440: 6e 74 6c 79 20 65 78 69 73 74 69 6e 67 20 69 6e  ntly existing in
6450: 20 61 6c 6c 20 70 72 69 6f 72 20 76 65 72 73 69   all prior versi
6460: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 66 6f  ons of SQLite fo
6470: 72 20 57 69 6e 64 6f 77 73 20 67 6f 69 6e 67 0a  r Windows going.
6480: 62 61 63 6b 20 74 6f 20 32 30 30 34 2e 20 20 42  back to 2004.  B
6490: 75 74 20 74 68 65 20 72 61 63 65 20 77 61 73 20  ut the race was 
64a0: 76 65 72 79 20 74 69 67 68 74 2e 20 20 50 72 61  very tight.  Pra
64b0: 63 74 69 63 61 6c 6c 79 20 73 70 65 61 6b 69 6e  ctically speakin
64c0: 67 2c 20 79 6f 75 0a 6e 65 65 64 20 61 20 66 61  g, you.need a fa
64d0: 73 74 20 6d 75 6c 74 69 2d 63 6f 72 65 20 6d 61  st multi-core ma
64e0: 63 68 69 6e 65 20 69 6e 20 77 68 69 63 68 20 79  chine in which y
64f0: 6f 75 20 6c 61 75 6e 63 68 20 74 77 6f 20 70 72  ou launch two pr
6500: 6f 63 65 73 73 65 73 20 74 6f 20 72 75 6e 0a 72  ocesses to run.r
6510: 65 63 6f 76 65 72 79 20 61 74 20 74 68 65 20 73  ecovery at the s
6520: 61 6d 65 20 6d 6f 6d 65 6e 74 20 6f 6e 20 74 77  ame moment on tw
6530: 6f 20 73 65 70 61 72 61 74 65 20 63 6f 72 65 73  o separate cores
6540: 2e 20 20 54 68 69 73 20 64 65 66 65 63 74 20 77  .  This defect w
6550: 61 73 0a 6f 6e 20 57 69 6e 64 6f 77 73 20 73 79  as.on Windows sy
6560: 73 74 65 6d 73 20 6f 6e 6c 79 20 61 6e 64 20 64  stems only and d
6570: 69 64 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68  id not affect th
6580: 65 20 70 6f 73 69 78 20 4f 53 20 69 6e 74 65 72  e posix OS inter
6590: 66 61 63 65 2e 3c 2f 70 3e 0a                    face.</p>.