Documentation Source Text

Hex Artifact Content
Login

Artifact c54f77614e092beb37196c08e3f84513618d15ac4538c434741640072b299962:


0000: 3c 74 69 74 6c 65 3e 46 69 6c 65 20 4c 6f 63 6b  <title>File Lock
0010: 69 6e 67 20 41 6e 64 20 43 6f 6e 63 75 72 72 65  ing And Concurre
0020: 6e 63 79 20 49 6e 20 53 51 4c 69 74 65 20 56 65  ncy In SQLite Ve
0030: 72 73 69 6f 6e 20 33 3c 2f 74 69 74 6c 65 3e 0a  rsion 3</title>.
0040: 0a 3c 66 6f 6e 74 20 63 6f 6c 6f 72 3d 22 23 37  .<font color="#7
0050: 66 30 30 31 66 22 3e 3c 70 3e 0a 3c 70 3e 54 68  f001f"><p>.<p>Th
0060: 69 73 20 64 6f 63 75 6d 65 6e 74 20 77 61 73 20  is document was 
0070: 6f 72 69 67 69 6e 61 6c 6c 79 20 63 72 65 61 74  originally creat
0080: 65 64 20 69 6e 20 65 61 72 6c 79 20 32 30 30 34  ed in early 2004
0090: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 76 65 72   when SQLite ver
00a0: 73 69 6f 6e 20 32 0a 77 61 73 20 73 74 69 6c 6c  sion 2.was still
00b0: 20 69 6e 20 77 69 64 65 73 70 72 65 61 64 20 75   in widespread u
00c0: 73 65 20 61 6e 64 20 77 61 73 20 77 72 69 74 74  se and was writt
00d0: 65 6e 20 74 6f 20 69 6e 74 72 6f 64 75 63 65 0a  en to introduce.
00e0: 74 68 65 20 6e 65 77 20 63 6f 6e 63 65 70 74 73  the new concepts
00f0: 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72 73 69   of SQLite versi
0100: 6f 6e 20 33 20 74 6f 20 72 65 61 64 65 72 73 20  on 3 to readers 
0110: 77 68 6f 20 77 65 72 65 20 61 6c 72 65 61 64 79  who were already
0120: 20 66 61 6d 69 6c 69 61 72 0a 77 69 74 68 20 53   familiar.with S
0130: 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 2e  QLite version 2.
0140: 20 20 42 75 74 20 74 68 65 73 65 20 64 61 79 73    But these days
0150: 2c 20 6d 6f 73 74 20 72 65 61 64 65 72 73 20 6f  , most readers o
0160: 66 20 74 68 69 73 20 64 6f 63 75 6d 65 6e 74 20  f this document 
0170: 68 61 76 65 0a 70 72 6f 62 61 62 6c 79 20 6e 65  have.probably ne
0180: 76 65 72 20 73 65 65 6e 20 53 51 4c 69 74 65 20  ver seen SQLite 
0190: 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 61 72  version 2 and ar
01a0: 65 20 6f 6e 6c 79 20 66 61 6d 69 6c 69 61 72 20  e only familiar 
01b0: 77 69 74 68 20 53 51 4c 69 74 65 0a 76 65 72 73  with SQLite.vers
01c0: 69 6f 6e 20 33 2e 20 20 4e 65 76 65 72 74 68 65  ion 3.  Neverthe
01d0: 6c 65 73 73 2c 20 74 68 69 73 20 64 6f 63 75 6d  less, this docum
01e0: 65 6e 74 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f  ent continues to
01f0: 20 73 65 72 76 65 20 61 73 20 61 6e 0a 61 75 74   serve as an.aut
0200: 68 6f 72 69 74 61 74 69 76 65 20 72 65 66 65 72  horitative refer
0210: 65 6e 63 65 20 74 6f 20 68 6f 77 20 64 61 74 61  ence to how data
0220: 62 61 73 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  base file lockin
0230: 67 20 77 6f 72 6b 73 20 69 6e 20 53 51 4c 69 74  g works in SQLit
0240: 65 20 0a 76 65 72 73 69 6f 6e 20 33 2e 3c 2f 70  e .version 3.</p
0250: 3e 0a 0a 3c 70 3e 54 68 65 20 64 6f 63 75 6d 65  >..<p>The docume
0260: 6e 74 20 6f 6e 6c 79 20 64 65 73 63 72 69 62 65  nt only describe
0270: 73 20 6c 6f 63 6b 69 6e 67 20 66 6f 72 20 74 68  s locking for th
0280: 65 20 6f 6c 64 65 72 20 72 6f 6c 6c 62 61 63 6b  e older rollback
0290: 2d 6d 6f 64 65 0a 74 72 61 6e 73 61 63 74 69 6f  -mode.transactio
02a0: 6e 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 4c 6f  n mechanism.  Lo
02b0: 63 6b 69 6e 67 20 66 6f 72 20 74 68 65 20 6e 65  cking for the ne
02c0: 77 65 72 20 5b 77 72 69 74 65 2d 61 68 65 61 64  wer [write-ahead
02d0: 20 6c 6f 67 5d 20 6f 72 20 5b 57 41 4c 20 6d 6f   log] or [WAL mo
02e0: 64 65 5d 0a 69 73 20 64 65 73 63 72 69 62 65 64  de].is described
02f0: 20 73 65 70 61 72 61 74 65 6c 79 2e 3c 2f 70 3e   separately.</p>
0300: 0a 3c 2f 66 6f 6e 74 3e 0a 0a 3c 74 63 6c 3e 0a  .</font>..<tcl>.
0310: 68 64 5f 6b 65 79 77 6f 72 64 73 20 7b 66 69 6c  hd_keywords {fil
0320: 65 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 63 6f  e locking and co
0330: 6e 63 75 72 72 65 6e 63 79 20 63 6f 6e 74 72 6f  ncurrency contro
0340: 6c 7d 0a 70 72 6f 63 20 48 45 41 44 49 4e 47 20  l}.proc HEADING 
0350: 7b 6c 65 76 65 6c 20 74 69 74 6c 65 20 61 72 67  {level title arg
0360: 73 7d 20 7b 0a 20 20 67 6c 6f 62 61 6c 20 70 6e  s} {.  global pn
0370: 75 6d 0a 20 20 69 6e 63 72 20 70 6e 75 6d 28 24  um.  incr pnum($
0380: 6c 65 76 65 6c 29 0a 20 20 66 6f 72 65 61 63 68  level).  foreach
0390: 20 69 20 5b 61 72 72 61 79 20 6e 61 6d 65 73 20   i [array names 
03a0: 70 6e 75 6d 5d 20 7b 0a 20 20 20 20 69 66 20 7b  pnum] {.    if {
03b0: 24 69 3e 24 6c 65 76 65 6c 7d 20 7b 73 65 74 20  $i>$level} {set 
03c0: 70 6e 75 6d 28 24 69 29 20 30 7d 0a 20 20 7d 0a  pnum($i) 0}.  }.
03d0: 20 20 73 65 74 20 68 20 5b 65 78 70 72 20 7b 24    set h [expr {$
03e0: 6c 65 76 65 6c 2b 31 7d 5d 0a 20 20 69 66 20 7b  level+1}].  if {
03f0: 24 68 3e 36 7d 20 7b 73 65 74 20 68 20 36 7d 0a  $h>6} {set h 6}.
0400: 20 20 73 65 74 20 6e 20 24 70 6e 75 6d 28 31 29    set n $pnum(1)
0410: 2e 24 70 6e 75 6d 28 32 29 0a 20 20 66 6f 72 20  .$pnum(2).  for 
0420: 7b 73 65 74 20 69 20 33 7d 20 7b 24 69 3c 3d 24  {set i 3} {$i<=$
0430: 6c 65 76 65 6c 7d 20 7b 69 6e 63 72 20 69 7d 20  level} {incr i} 
0440: 7b 0a 20 20 20 20 61 70 70 65 6e 64 20 6e 20 2e  {.    append n .
0450: 24 70 6e 75 6d 28 24 69 29 0a 20 20 7d 0a 20 20  $pnum($i).  }.  
0460: 69 66 20 7b 5b 6c 6c 65 6e 67 74 68 20 24 61 72  if {[llength $ar
0470: 67 73 5d 3e 30 7d 20 7b 0a 20 20 20 20 65 76 61  gs]>0} {.    eva
0480: 6c 20 68 64 5f 66 72 61 67 6d 65 6e 74 20 24 61  l hd_fragment $a
0490: 72 67 73 0a 20 20 7d 0a 20 20 68 64 5f 70 75 74  rgs.  }.  hd_put
04a0: 73 20 22 3c 68 24 68 3e 24 6e 20 24 74 69 74 6c  s "<h$h>$n $titl
04b0: 65 3c 2f 68 24 68 3e 22 0a 7d 0a 73 65 74 20 70  e</h$h>".}.set p
04c0: 6e 75 6d 28 31 29 20 30 0a 73 65 74 20 70 6e 75  num(1) 0.set pnu
04d0: 6d 28 32 29 20 30 0a 73 65 74 20 70 6e 75 6d 28  m(2) 0.set pnum(
04e0: 33 29 20 30 0a 73 65 74 20 70 6e 75 6d 28 34 29  3) 0.set pnum(4)
04f0: 20 30 0a 73 65 74 20 70 6e 75 6d 28 35 29 20 30   0.set pnum(5) 0
0500: 0a 73 65 74 20 70 6e 75 6d 28 36 29 20 30 0a 73  .set pnum(6) 0.s
0510: 65 74 20 70 6e 75 6d 28 37 29 20 30 0a 73 65 74  et pnum(7) 0.set
0520: 20 70 6e 75 6d 28 38 29 20 30 0a 0a 48 45 41 44   pnum(8) 0..HEAD
0530: 49 4e 47 20 31 20 7b 46 69 6c 65 20 4c 6f 63 6b  ING 1 {File Lock
0540: 69 6e 67 20 41 6e 64 20 43 6f 6e 63 75 72 72 65  ing And Concurre
0550: 6e 63 79 20 49 6e 20 53 51 4c 69 74 65 20 56 65  ncy In SQLite Ve
0560: 72 73 69 6f 6e 20 33 7d 0a 3c 2f 74 63 6c 3e 0a  rsion 3}.</tcl>.
0570: 0a 3c 70 3e 53 51 4c 69 74 65 20 5b 56 65 72 73  .<p>SQLite [Vers
0580: 69 6f 6e 20 33 2e 30 2e 30 5d 20 69 6e 74 72 6f  ion 3.0.0] intro
0590: 64 75 63 65 64 20 61 20 6e 65 77 20 6c 6f 63 6b  duced a new lock
05a0: 69 6e 67 20 61 6e 64 20 6a 6f 75 72 6e 61 6c 69  ing and journali
05b0: 6e 67 20 0a 6d 65 63 68 61 6e 69 73 6d 20 64 65  ng .mechanism de
05c0: 73 69 67 6e 65 64 20 74 6f 20 69 6d 70 72 6f 76  signed to improv
05d0: 65 20 63 6f 6e 63 75 72 72 65 6e 63 79 20 6f 76  e concurrency ov
05e0: 65 72 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f  er SQLite versio
05f0: 6e 20 32 0a 61 6e 64 20 74 6f 20 72 65 64 75 63  n 2.and to reduc
0600: 65 20 74 68 65 20 77 72 69 74 65 72 20 73 74 61  e the writer sta
0610: 72 76 61 74 69 6f 6e 20 0a 70 72 6f 62 6c 65 6d  rvation .problem
0620: 2e 20 20 54 68 65 20 6e 65 77 20 6d 65 63 68 61  .  The new mecha
0630: 6e 69 73 6d 20 61 6c 73 6f 20 61 6c 6c 6f 77 73  nism also allows
0640: 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 73 20   atomic commits 
0650: 6f 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 0a  of transactions.
0660: 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 69 70  involving multip
0670: 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
0680: 73 2e 0a 54 68 69 73 20 64 6f 63 75 6d 65 6e 74  s..This document
0690: 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 6e   describes the n
06a0: 65 77 20 6c 6f 63 6b 69 6e 67 20 6d 65 63 68 61  ew locking mecha
06b0: 6e 69 73 6d 2e 0a 54 68 65 20 69 6e 74 65 6e 64  nism..The intend
06c0: 65 64 20 61 75 64 69 65 6e 63 65 20 69 73 20 70  ed audience is p
06d0: 72 6f 67 72 61 6d 6d 65 72 73 20 77 68 6f 20 77  rogrammers who w
06e0: 61 6e 74 20 74 6f 20 75 6e 64 65 72 73 74 61 6e  ant to understan
06f0: 64 20 61 6e 64 2f 6f 72 20 6d 6f 64 69 66 79 0a  d and/or modify.
0700: 74 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 61  the pager code a
0710: 6e 64 20 72 65 76 69 65 77 65 72 73 20 77 6f 72  nd reviewers wor
0720: 6b 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74  king to verify t
0730: 68 65 20 64 65 73 69 67 6e 0a 6f 66 20 53 51 4c  he design.of SQL
0740: 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 0a 3c  ite version 3..<
0750: 2f 70 3e 0a 0a 3c 74 63 6c 3e 48 45 41 44 49 4e  /p>..<tcl>HEADIN
0760: 47 20 31 20 7b 4f 76 65 72 76 69 65 77 7d 20 6f  G 1 {Overview} o
0770: 76 65 72 76 69 65 77 3c 2f 74 63 6c 3e 0a 0a 3c  verview</tcl>..<
0780: 70 3e 0a 4c 6f 63 6b 69 6e 67 20 61 6e 64 20 63  p>.Locking and c
0790: 6f 6e 63 75 72 72 65 6e 63 79 20 63 6f 6e 74 72  oncurrency contr
07a0: 6f 6c 20 61 72 65 20 68 61 6e 64 6c 65 64 20 62  ol are handled b
07b0: 79 20 74 68 65 20 0a 3c 61 20 68 72 65 66 3d 22  y the .<a href="
07c0: 68 74 74 70 3a 2f 2f 77 77 77 2e 73 71 6c 69 74  http://www.sqlit
07d0: 65 2e 6f 72 67 2f 73 72 63 2f 66 69 6e 66 6f 3f  e.org/src/finfo?
07e0: 6e 61 6d 65 3d 73 72 63 2f 70 61 67 65 72 2e 63  name=src/pager.c
07f0: 22 3e 0a 70 61 67 65 72 20 6d 6f 64 75 6c 65 3c  ">.pager module<
0800: 2f 61 3e 2e 0a 54 68 65 20 70 61 67 65 72 20 6d  /a>..The pager m
0810: 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73  odule is respons
0820: 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20  ible for making 
0830: 53 51 4c 69 74 65 20 22 41 43 49 44 22 20 28 41  SQLite "ACID" (A
0840: 74 6f 6d 69 63 2c 0a 43 6f 6e 73 69 73 74 65 6e  tomic,.Consisten
0850: 74 2c 20 49 73 6f 6c 61 74 65 64 2c 20 61 6e 64  t, Isolated, and
0860: 20 44 75 72 61 62 6c 65 29 2e 20 20 54 68 65 20   Durable).  The 
0870: 70 61 67 65 72 20 6d 6f 64 75 6c 65 20 6d 61 6b  pager module mak
0880: 65 73 20 73 75 72 65 20 63 68 61 6e 67 65 73 0a  es sure changes.
0890: 68 61 70 70 65 6e 20 61 6c 6c 20 61 74 20 6f 6e  happen all at on
08a0: 63 65 2c 20 74 68 61 74 20 65 69 74 68 65 72 20  ce, that either 
08b0: 61 6c 6c 20 63 68 61 6e 67 65 73 20 6f 63 63 75  all changes occu
08c0: 72 20 6f 72 20 6e 6f 6e 65 20 6f 66 20 74 68 65  r or none of the
08d0: 6d 20 64 6f 2c 0a 74 68 61 74 20 74 77 6f 20 6f  m do,.that two o
08e0: 72 20 6d 6f 72 65 20 70 72 6f 63 65 73 73 65 73  r more processes
08f0: 20 64 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61   do not try to a
0900: 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61  ccess the databa
0910: 73 65 0a 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62  se.in incompatib
0920: 6c 65 20 77 61 79 73 20 61 74 20 74 68 65 20 73  le ways at the s
0930: 61 6d 65 20 74 69 6d 65 2c 20 61 6e 64 20 74 68  ame time, and th
0940: 61 74 20 6f 6e 63 65 20 63 68 61 6e 67 65 73 20  at once changes 
0950: 68 61 76 65 20 62 65 65 6e 0a 77 72 69 74 74 65  have been.writte
0960: 6e 20 74 68 65 79 20 70 65 72 73 69 73 74 20 75  n they persist u
0970: 6e 74 69 6c 20 65 78 70 6c 69 63 69 74 6c 79 20  ntil explicitly 
0980: 64 65 6c 65 74 65 64 2e 20 20 54 68 65 20 70 61  deleted.  The pa
0990: 67 65 72 20 61 6c 73 6f 20 70 72 6f 76 69 64 65  ger also provide
09a0: 73 0a 61 20 6d 65 6d 6f 72 79 20 63 61 63 68 65  s.a memory cache
09b0: 20 6f 66 20 73 6f 6d 65 20 6f 66 20 74 68 65 20   of some of the 
09c0: 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
09d0: 64 69 73 6b 20 66 69 6c 65 2e 3c 2f 70 3e 0a 0a  disk file.</p>..
09e0: 3c 70 3e 54 68 65 20 70 61 67 65 72 20 69 73 20  <p>The pager is 
09f0: 75 6e 63 6f 6e 63 65 72 6e 65 64 0a 77 69 74 68  unconcerned.with
0a00: 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20   the details of 
0a10: 42 2d 54 72 65 65 73 2c 20 74 65 78 74 20 65 6e  B-Trees, text en
0a20: 63 6f 64 69 6e 67 73 2c 20 69 6e 64 69 63 65 73  codings, indices
0a30: 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a  , and so forth..
0a40: 46 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20 6f  From the point o
0a50: 66 20 76 69 65 77 20 6f 66 20 74 68 65 20 70 61  f view of the pa
0a60: 67 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ger the database
0a70: 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 61 20 73   consists of.a s
0a80: 69 6e 67 6c 65 20 66 69 6c 65 20 6f 66 20 75 6e  ingle file of un
0a90: 69 66 6f 72 6d 2d 73 69 7a 65 64 20 62 6c 6f 63  iform-sized bloc
0aa0: 6b 73 2e 20 20 45 61 63 68 20 62 6c 6f 63 6b 20  ks.  Each block 
0ab0: 69 73 20 63 61 6c 6c 65 64 20 61 0a 22 70 61 67  is called a."pag
0ac0: 65 22 20 61 6e 64 20 69 73 20 75 73 75 61 6c 6c  e" and is usuall
0ad0: 79 20 31 30 32 34 20 62 79 74 65 73 20 69 6e 20  y 1024 bytes in 
0ae0: 73 69 7a 65 2e 20 20 20 54 68 65 20 70 61 67 65  size.   The page
0af0: 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 0a 62  s are numbered.b
0b00: 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 31 2e  eginning with 1.
0b10: 20 20 53 6f 20 74 68 65 20 66 69 72 73 74 20 31    So the first 1
0b20: 30 32 34 20 62 79 74 65 73 20 6f 66 20 74 68 65  024 bytes of the
0b30: 20 64 61 74 61 62 61 73 65 20 61 72 65 20 63 61   database are ca
0b40: 6c 6c 65 64 0a 22 70 61 67 65 20 31 22 20 61 6e  lled."page 1" an
0b50: 64 20 74 68 65 20 73 65 63 6f 6e 64 20 31 30 32  d the second 102
0b60: 34 20 62 79 74 65 73 20 61 72 65 20 63 61 6c 6c  4 bytes are call
0b70: 20 22 70 61 67 65 20 32 22 20 61 6e 64 20 73 6f   "page 2" and so
0b80: 20 66 6f 72 74 68 2e 20 41 6c 6c 20 0a 6f 74 68   forth. All .oth
0b90: 65 72 20 65 6e 63 6f 64 69 6e 67 20 64 65 74 61  er encoding deta
0ba0: 69 6c 73 20 61 72 65 20 68 61 6e 64 6c 65 64 20  ils are handled 
0bb0: 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 72 73  by higher layers
0bc0: 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
0bd0: 20 20 0a 54 68 65 20 70 61 67 65 72 20 63 6f 6d    .The pager com
0be0: 6d 75 6e 69 63 61 74 65 73 20 77 69 74 68 20 74  municates with t
0bf0: 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
0c00: 74 65 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66  tem using one of
0c10: 20 73 65 76 65 72 61 6c 0a 6d 6f 64 75 6c 65 73   several.modules
0c20: 20 0a 28 45 78 61 6d 70 6c 65 73 3a 0a 3c 61 20   .(Examples:.<a 
0c30: 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77  href="http://www
0c40: 2e 73 71 6c 69 74 65 2e 6f 72 67 2f 73 72 63 2f  .sqlite.org/src/
0c50: 66 69 6e 66 6f 3f 6e 61 6d 65 3d 73 72 63 2f 6f  finfo?name=src/o
0c60: 73 5f 75 6e 69 78 2e 63 22 3e 0a 6f 73 5f 75 6e  s_unix.c">.os_un
0c70: 69 78 2e 63 3c 2f 61 3e 2c 0a 3c 61 20 68 72 65  ix.c</a>,.<a hre
0c80: 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 73 71  f="http://www.sq
0c90: 6c 69 74 65 2e 6f 72 67 2f 73 72 63 2f 66 69 6e  lite.org/src/fin
0ca0: 66 6f 3f 6e 61 6d 65 3d 73 72 63 2f 6f 73 5f 77  fo?name=src/os_w
0cb0: 69 6e 2e 63 22 3e 0a 6f 73 5f 77 69 6e 2e 63 3c  in.c">.os_win.c<
0cc0: 2f 61 3e 29 0a 74 68 61 74 20 70 72 6f 76 69 64  /a>).that provid
0cd0: 65 73 20 61 20 75 6e 69 66 6f 72 6d 20 61 62 73  es a uniform abs
0ce0: 74 72 61 63 74 69 6f 6e 20 66 6f 72 20 6f 70 65  traction for ope
0cf0: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 65  rating system se
0d00: 72 76 69 63 65 73 2e 0a 3c 2f 70 3e 0a 0a 3c 70  rvices..</p>..<p
0d10: 3e 54 68 65 20 70 61 67 65 72 20 6d 6f 64 75 6c  >The pager modul
0d20: 65 20 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f  e effectively co
0d30: 6e 74 72 6f 6c 73 20 61 63 63 65 73 73 20 66 6f  ntrols access fo
0d40: 72 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61  r separate threa
0d50: 64 73 2c 20 6f 72 0a 73 65 70 61 72 61 74 65 20  ds, or.separate 
0d60: 70 72 6f 63 65 73 73 65 73 2c 20 6f 72 20 62 6f  processes, or bo
0d70: 74 68 2e 20 20 54 68 72 6f 75 67 68 6f 75 74 20  th.  Throughout 
0d80: 74 68 69 73 20 64 6f 63 75 6d 65 6e 74 20 77 68  this document wh
0d90: 65 6e 65 76 65 72 20 74 68 65 0a 77 6f 72 64 20  enever the.word 
0da0: 22 70 72 6f 63 65 73 73 22 20 69 73 20 77 72 69  "process" is wri
0db0: 74 74 65 6e 20 79 6f 75 20 6d 61 79 20 73 75 62  tten you may sub
0dc0: 73 74 69 74 75 74 65 20 74 68 65 20 77 6f 72 64  stitute the word
0dd0: 20 22 74 68 72 65 61 64 22 20 77 69 74 68 6f 75   "thread" withou
0de0: 74 0a 63 68 61 6e 67 69 6e 67 20 74 68 65 20 74  t.changing the t
0df0: 72 75 74 68 20 6f 66 20 74 68 65 20 73 74 61 74  ruth of the stat
0e00: 65 6d 65 6e 74 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c  ement.</p>..<tcl
0e10: 3e 48 45 41 44 49 4e 47 20 31 20 7b 4c 6f 63 6b  >HEADING 1 {Lock
0e20: 69 6e 67 7d 20 6c 6f 63 6b 69 6e 67 20 7b 66 69  ing} locking {fi
0e30: 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65  le locking state
0e40: 73 7d 3c 2f 74 63 6c 3e 0a 0a 3c 70 3e 0a 46 72  s}</tcl>..<p>.Fr
0e50: 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20  om the point of 
0e60: 76 69 65 77 20 6f 66 20 61 20 73 69 6e 67 6c 65  view of a single
0e70: 20 70 72 6f 63 65 73 73 2c 20 61 20 64 61 74 61   process, a data
0e80: 62 61 73 65 20 66 69 6c 65 0a 63 61 6e 20 62 65  base file.can be
0e90: 20 69 6e 20 6f 6e 65 20 6f 66 20 66 69 76 65 20   in one of five 
0ea0: 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65 73 3a 0a  locking states:.
0eb0: 3c 2f 70 3e 0a 0a 3c 70 3e 0a 3c 74 61 62 6c 65  </p>..<p>.<table
0ec0: 20 63 65 6c 6c 70 61 64 64 69 6e 67 3d 22 32 30   cellpadding="20
0ed0: 22 3e 0a 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67  ">.<tr><td valig
0ee0: 6e 3d 22 74 6f 70 22 3e 55 4e 4c 4f 43 4b 45 44  n="top">UNLOCKED
0ef0: 3c 2f 74 64 3e 0a 3c 74 64 20 76 61 6c 69 67 6e  </td>.<td valign
0f00: 3d 22 74 6f 70 22 3e 0a 4e 6f 20 6c 6f 63 6b 73  ="top">.No locks
0f10: 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65   are held on the
0f20: 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
0f30: 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 20  database may be 
0f40: 6e 65 69 74 68 65 72 20 72 65 61 64 20 6e 6f 72  neither read nor
0f50: 0a 77 72 69 74 74 65 6e 2e 20 20 41 6e 79 20 69  .written.  Any i
0f60: 6e 74 65 72 6e 61 6c 6c 79 20 63 61 63 68 65 64  nternally cached
0f70: 20 64 61 74 61 20 69 73 20 63 6f 6e 73 69 64 65   data is conside
0f80: 72 65 64 20 73 75 73 70 65 63 74 20 61 6e 64 20  red suspect and 
0f90: 73 75 62 6a 65 63 74 20 74 6f 0a 76 65 72 69 66  subject to.verif
0fa0: 69 63 61 74 69 6f 6e 20 61 67 61 69 6e 73 74 20  ication against 
0fb0: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
0fc0: 65 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 75  e before being u
0fd0: 73 65 64 2e 20 20 4f 74 68 65 72 20 0a 70 72 6f  sed.  Other .pro
0fe0: 63 65 73 73 65 73 20 63 61 6e 20 72 65 61 64 20  cesses can read 
0ff0: 6f 72 20 77 72 69 74 65 20 74 68 65 20 64 61 74  or write the dat
1000: 61 62 61 73 65 20 61 73 20 74 68 65 69 72 20 6f  abase as their o
1010: 77 6e 20 6c 6f 63 6b 69 6e 67 20 73 74 61 74 65  wn locking state
1020: 73 0a 70 65 72 6d 69 74 2e 20 20 54 68 69 73 20  s.permit.  This 
1030: 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 73  is the default s
1040: 74 61 74 65 2e 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  tate..</td></tr>
1050: 0a 0a 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67 6e  ..<tr><td valign
1060: 3d 22 74 6f 70 22 3e 53 48 41 52 45 44 3c 2f 74  ="top">SHARED</t
1070: 64 3e 0a 3c 74 64 20 76 61 6c 69 67 6e 3d 22 74  d>.<td valign="t
1080: 6f 70 22 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72 61  op">.<tcl>hd_fra
1090: 67 6d 65 6e 74 20 73 68 61 72 65 64 5f 6c 6f 63  gment shared_loc
10a0: 6b 20 7b 2a 73 68 61 72 65 64 20 6c 6f 63 6b 7d  k {*shared lock}
10b0: 20 2a 53 48 41 52 45 44 20 7b 53 48 41 52 45 44   *SHARED {SHARED
10c0: 20 6c 6f 63 6b 7d 3c 2f 74 63 6c 3e 0a 54 68 65   lock}</tcl>.The
10d0: 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65   database may be
10e0: 20 72 65 61 64 20 62 75 74 20 6e 6f 74 20 77 72   read but not wr
10f0: 69 74 74 65 6e 2e 20 20 41 6e 79 20 6e 75 6d 62  itten.  Any numb
1100: 65 72 20 6f 66 20 0a 70 72 6f 63 65 73 73 65 73  er of .processes
1110: 20 63 61 6e 20 68 6f 6c 64 20 53 48 41 52 45 44   can hold SHARED
1120: 20 6c 6f 63 6b 73 20 61 74 20 74 68 65 20 73 61   locks at the sa
1130: 6d 65 20 74 69 6d 65 2c 20 68 65 6e 63 65 20 74  me time, hence t
1140: 68 65 72 65 20 63 61 6e 20 62 65 0a 6d 61 6e 79  here can be.many
1150: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 72 65   simultaneous re
1160: 61 64 65 72 73 2e 20 20 42 75 74 20 6e 6f 20 6f  aders.  But no o
1170: 74 68 65 72 20 74 68 72 65 61 64 20 6f 72 20 70  ther thread or p
1180: 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65  rocess is allowe
1190: 64 0a 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d.to write to th
11a0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
11b0: 77 68 69 6c 65 20 6f 6e 65 20 6f 72 20 6d 6f 72  while one or mor
11c0: 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61  e SHARED locks a
11d0: 72 65 20 61 63 74 69 76 65 2e 0a 3c 2f 74 64 3e  re active..</td>
11e0: 3c 2f 74 72 3e 0a 0a 3c 74 72 3e 3c 74 64 20 76  </tr>..<tr><td v
11f0: 61 6c 69 67 6e 3d 22 74 6f 70 22 3e 52 45 53 45  align="top">RESE
1200: 52 56 45 44 3c 2f 74 64 3e 0a 3c 74 64 20 76 61  RVED</td>.<td va
1210: 6c 69 67 6e 3d 22 74 6f 70 22 3e 0a 3c 74 63 6c  lign="top">.<tcl
1220: 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 72 65 73  >hd_fragment res
1230: 65 72 76 65 64 5f 6c 6f 63 6b 20 7b 2a 72 65 73  erved_lock {*res
1240: 65 72 76 65 64 20 6c 6f 63 6b 7d 20 2a 52 45 53  erved lock} *RES
1250: 45 52 56 45 44 20 7b 52 45 53 45 52 56 45 44 20  ERVED {RESERVED 
1260: 6c 6f 63 6b 7d 3c 2f 74 63 6c 3e 0a 41 20 52 45  lock}</tcl>.A RE
1270: 53 45 52 56 45 44 20 6c 6f 63 6b 20 6d 65 61 6e  SERVED lock mean
1280: 73 20 74 68 61 74 20 74 68 65 20 70 72 6f 63 65  s that the proce
1290: 73 73 20 69 73 20 70 6c 61 6e 6e 69 6e 67 20 6f  ss is planning o
12a0: 6e 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65  n writing to the
12b0: 0a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61  .database file a
12c0: 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20  t some point in 
12d0: 74 68 65 20 66 75 74 75 72 65 20 62 75 74 20 74  the future but t
12e0: 68 61 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  hat it is curren
12f0: 74 6c 79 20 6a 75 73 74 0a 72 65 61 64 69 6e 67  tly just.reading
1300: 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 20   from the file. 
1310: 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 52   Only a single R
1320: 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6d 61 79  ESERVED lock may
1330: 20 62 65 20 61 63 74 69 76 65 20 61 74 20 6f 6e   be active at on
1340: 65 0a 74 69 6d 65 2c 20 74 68 6f 75 67 68 20 6d  e.time, though m
1350: 75 6c 74 69 70 6c 65 20 53 48 41 52 45 44 20 6c  ultiple SHARED l
1360: 6f 63 6b 73 20 63 61 6e 20 63 6f 65 78 69 73 74  ocks can coexist
1370: 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 52   with a single R
1380: 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 52 45  ESERVED lock..RE
1390: 53 45 52 56 45 44 20 64 69 66 66 65 72 73 20 66  SERVED differs f
13a0: 72 6f 6d 20 50 45 4e 44 49 4e 47 20 69 6e 20 74  rom PENDING in t
13b0: 68 61 74 20 6e 65 77 20 53 48 41 52 45 44 20 6c  hat new SHARED l
13c0: 6f 63 6b 73 20 63 61 6e 20 62 65 20 61 63 71 75  ocks can be acqu
13d0: 69 72 65 64 0a 77 68 69 6c 65 20 74 68 65 72 65  ired.while there
13e0: 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
13f0: 6f 63 6b 2e 0a 3c 2f 74 64 3e 3c 2f 74 72 3e 0a  ock..</td></tr>.
1400: 0a 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67 6e 3d  .<tr><td valign=
1410: 22 74 6f 70 22 3e 50 45 4e 44 49 4e 47 3c 2f 74  "top">PENDING</t
1420: 64 3e 0a 3c 74 64 20 76 61 6c 69 67 6e 3d 22 74  d>.<td valign="t
1430: 6f 70 22 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72 61  op">.<tcl>hd_fra
1440: 67 6d 65 6e 74 20 70 65 6e 64 69 6e 67 5f 6c 6f  gment pending_lo
1450: 63 6b 20 7b 2a 70 65 6e 64 69 6e 67 20 6c 6f 63  ck {*pending loc
1460: 6b 7d 20 2a 50 45 4e 44 49 4e 47 20 7b 50 45 4e  k} *PENDING {PEN
1470: 44 49 4e 47 20 6c 6f 63 6b 7d 3c 2f 74 63 6c 3e  DING lock}</tcl>
1480: 0a 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20  .A PENDING lock 
1490: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70  means that the p
14a0: 72 6f 63 65 73 73 20 68 6f 6c 64 69 6e 67 20 74  rocess holding t
14b0: 68 65 20 6c 6f 63 6b 20 77 61 6e 74 73 20 74 6f  he lock wants to
14c0: 20 77 72 69 74 65 0a 74 6f 20 74 68 65 20 64 61   write.to the da
14d0: 74 61 62 61 73 65 20 61 73 20 73 6f 6f 6e 20 61  tabase as soon a
14e0: 73 20 70 6f 73 73 69 62 6c 65 20 61 6e 64 20 69  s possible and i
14f0: 73 20 6a 75 73 74 20 77 61 69 74 69 6e 67 20 6f  s just waiting o
1500: 6e 20 61 6c 6c 20 63 75 72 72 65 6e 74 0a 53 48  n all current.SH
1510: 41 52 45 44 20 6c 6f 63 6b 73 20 74 6f 20 63 6c  ARED locks to cl
1520: 65 61 72 20 73 6f 20 74 68 61 74 20 69 74 20 63  ear so that it c
1530: 61 6e 20 67 65 74 20 61 6e 20 45 58 43 4c 55 53  an get an EXCLUS
1540: 49 56 45 20 6c 6f 63 6b 2e 20 20 4e 6f 20 6e 65  IVE lock.  No ne
1550: 77 20 0a 53 48 41 52 45 44 20 6c 6f 63 6b 73 20  w .SHARED locks 
1560: 61 72 65 20 70 65 72 6d 69 74 74 65 64 20 61 67  are permitted ag
1570: 61 69 6e 73 74 20 74 68 65 20 64 61 74 61 62 61  ainst the databa
1580: 73 65 20 69 66 0a 61 20 50 45 4e 44 49 4e 47 20  se if.a PENDING 
1590: 6c 6f 63 6b 20 69 73 20 61 63 74 69 76 65 2c 20  lock is active, 
15a0: 74 68 6f 75 67 68 20 65 78 69 73 74 69 6e 67 20  though existing 
15b0: 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65  SHARED locks are
15c0: 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 63 6f 6e 74   allowed to.cont
15d0: 69 6e 75 65 2e 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  inue..</td></tr>
15e0: 0a 0a 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67 6e  ..<tr><td valign
15f0: 3d 22 74 6f 70 22 3e 45 58 43 4c 55 53 49 56 45  ="top">EXCLUSIVE
1600: 3c 2f 74 64 3e 0a 3c 74 64 20 76 61 6c 69 67 6e  </td>.<td valign
1610: 3d 22 74 6f 70 22 3e 0a 3c 74 63 6c 3e 68 64 5f  ="top">.<tcl>hd_
1620: 66 72 61 67 6d 65 6e 74 20 65 78 63 6c 5f 6c 6f  fragment excl_lo
1630: 63 6b 20 7b 2a 65 78 63 6c 75 73 69 76 65 20 6c  ck {*exclusive l
1640: 6f 63 6b 7d 20 2a 45 58 43 4c 55 53 49 56 45 20  ock} *EXCLUSIVE 
1650: 7b 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 7d  {EXCLUSIVE lock}
1660: 3c 2f 74 63 6c 3e 0a 41 6e 20 45 58 43 4c 55 53  </tcl>.An EXCLUS
1670: 49 56 45 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64  IVE lock is need
1680: 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 77  ed in order to w
1690: 72 69 74 65 20 74 6f 20 74 68 65 20 64 61 74 61  rite to the data
16a0: 62 61 73 65 20 66 69 6c 65 2e 0a 4f 6e 6c 79 20  base file..Only 
16b0: 6f 6e 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f  one EXCLUSIVE lo
16c0: 63 6b 20 69 73 20 61 6c 6c 6f 77 65 64 20 6f 6e  ck is allowed on
16d0: 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 6e 6f   the file and no
16e0: 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 6f 66 0a   other locks of.
16f0: 61 6e 79 20 6b 69 6e 64 20 61 72 65 20 61 6c 6c  any kind are all
1700: 6f 77 65 64 20 74 6f 20 63 6f 65 78 69 73 74 20  owed to coexist 
1710: 77 69 74 68 20 61 6e 20 45 58 43 4c 55 53 49 56  with an EXCLUSIV
1720: 45 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 72 64 65  E lock.  In orde
1730: 72 20 74 6f 0a 6d 61 78 69 6d 69 7a 65 20 63 6f  r to.maximize co
1740: 6e 63 75 72 72 65 6e 63 79 2c 20 53 51 4c 69 74  ncurrency, SQLit
1750: 65 20 77 6f 72 6b 73 20 74 6f 20 6d 69 6e 69 6d  e works to minim
1760: 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  ize the amount o
1770: 66 20 74 69 6d 65 20 74 68 61 74 0a 45 58 43 4c  f time that.EXCL
1780: 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 72 65 20  USIVE locks are 
1790: 68 65 6c 64 2e 0a 3c 2f 74 64 3e 3c 2f 74 72 3e  held..</td></tr>
17a0: 0a 3c 2f 74 61 62 6c 65 3e 0a 3c 2f 70 3e 0a 0a  .</table>.</p>..
17b0: 3c 70 3e 0a 54 68 65 20 6f 70 65 72 61 74 69 6e  <p>.The operatin
17c0: 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
17d0: 63 65 20 6c 61 79 65 72 20 75 6e 64 65 72 73 74  ce layer underst
17e0: 61 6e 64 73 20 61 6e 64 20 74 72 61 63 6b 73 20  ands and tracks 
17f0: 61 6c 6c 20 66 69 76 65 0a 6c 6f 63 6b 69 6e 67  all five.locking
1800: 20 73 74 61 74 65 73 20 64 65 73 63 72 69 62 65   states describe
1810: 64 20 61 62 6f 76 65 2e 20 20 0a 54 68 65 20 70  d above.  .The p
1820: 61 67 65 72 20 6d 6f 64 75 6c 65 20 6f 6e 6c 79  ager module only
1830: 20 74 72 61 63 6b 73 20 66 6f 75 72 20 6f 66 20   tracks four of 
1840: 74 68 65 20 66 69 76 65 20 6c 6f 63 6b 69 6e 67  the five locking
1850: 20 73 74 61 74 65 73 2e 0a 41 20 50 45 4e 44 49   states..A PENDI
1860: 4e 47 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79  NG lock is alway
1870: 73 20 6a 75 73 74 20 61 20 74 65 6d 70 6f 72 61  s just a tempora
1880: 72 79 0a 73 74 65 70 70 69 6e 67 20 73 74 6f 6e  ry.stepping ston
1890: 65 20 6f 6e 20 74 68 65 20 70 61 74 68 20 74 6f  e on the path to
18a0: 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
18b0: 63 6b 20 61 6e 64 20 73 6f 20 74 68 65 20 70 61  ck and so the pa
18c0: 67 65 72 20 6d 6f 64 75 6c 65 0a 64 6f 65 73 20  ger module.does 
18d0: 6e 6f 74 20 74 72 61 63 6b 20 50 45 4e 44 49 4e  not track PENDIN
18e0: 47 20 6c 6f 63 6b 73 2e 0a 3c 2f 70 3e 0a 0a 3c  G locks..</p>..<
18f0: 74 63 6c 3e 48 45 41 44 49 4e 47 20 31 20 7b 54  tcl>HEADING 1 {T
1900: 68 65 20 52 6f 6c 6c 62 61 63 6b 20 4a 6f 75 72  he Rollback Jour
1910: 6e 61 6c 7d 20 72 6f 6c 6c 62 61 63 6b 20 7b 72  nal} rollback {r
1920: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 7d  ollback journal}
1930: 3c 2f 74 63 6c 3e 0a 0a 3c 70 3e 57 68 65 6e 20  </tcl>..<p>When 
1940: 61 20 70 72 6f 63 65 73 73 20 77 61 6e 74 73 20  a process wants 
1950: 74 6f 20 63 68 61 6e 67 65 20 61 20 64 61 74 61  to change a data
1960: 62 61 73 65 20 66 69 6c 65 20 28 61 6e 64 20 69  base file (and i
1970: 74 20 69 73 20 6e 6f 74 0a 69 6e 20 5b 57 41 4c  t is not.in [WAL
1980: 5d 20 6d 6f 64 65 29 2c 20 69 74 0a 66 69 72 73  ] mode), it.firs
1990: 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6f 72  t records the or
19a0: 69 67 69 6e 61 6c 20 75 6e 63 68 61 6e 67 65 64  iginal unchanged
19b0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 65 6e   database conten
19c0: 74 0a 69 6e 20 61 20 3c 65 6d 3e 72 6f 6c 6c 62  t.in a <em>rollb
19d0: 61 63 6b 20 6a 6f 75 72 6e 61 6c 3c 2f 65 6d 3e  ack journal</em>
19e0: 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  .  The rollback 
19f0: 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 6f 72  journal is an or
1a00: 64 69 6e 61 72 79 0a 64 69 73 6b 20 66 69 6c 65  dinary.disk file
1a10: 20 74 68 61 74 20 69 73 20 61 6c 77 61 79 73 20   that is always 
1a20: 6c 6f 63 61 74 65 64 0a 69 6e 20 74 68 65 20 73  located.in the s
1a30: 61 6d 65 20 64 69 72 65 63 74 6f 72 79 20 6f 72  ame directory or
1a40: 20 66 6f 6c 64 65 72 20 61 73 20 74 68 65 20 64   folder as the d
1a50: 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1a60: 20 68 61 73 20 74 68 65 0a 73 61 6d 65 20 6e 61   has the.same na
1a70: 6d 65 20 61 73 20 74 68 65 20 64 61 74 61 62 61  me as the databa
1a80: 73 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  se file with the
1a90: 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 61 20 3c   addition of a <
1aa0: 74 74 3e 2d 6a 6f 75 72 6e 61 6c 3c 2f 74 74 3e  tt>-journal</tt>
1ab0: 0a 73 75 66 66 69 78 2e 20 54 68 65 20 72 6f 6c  .suffix. The rol
1ac0: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6c  lback journal al
1ad0: 73 6f 20 72 65 63 6f 72 64 73 20 74 68 65 20 69  so records the i
1ae0: 6e 69 74 69 61 6c 0a 73 69 7a 65 20 6f 66 20 74  nitial.size of t
1af0: 68 65 20 64 61 74 61 62 61 73 65 20 73 6f 20 74  he database so t
1b00: 68 61 74 20 69 66 20 74 68 65 20 64 61 74 61 62  hat if the datab
1b10: 61 73 65 20 66 69 6c 65 20 67 72 6f 77 73 20 69  ase file grows i
1b20: 74 20 63 61 6e 20 62 65 20 74 72 75 6e 63 61 74  t can be truncat
1b30: 65 64 0a 62 61 63 6b 20 74 6f 20 69 74 73 20 6f  ed.back to its o
1b40: 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6f 6e 20  riginal size on 
1b50: 61 20 72 6f 6c 6c 62 61 63 6b 2e 3c 2f 70 3e 0a  a rollback.</p>.
1b60: 0a 3c 70 3e 49 66 20 53 51 4c 69 74 65 20 69 73  .<p>If SQLite is
1b70: 20 77 6f 72 6b 69 6e 67 20 77 69 74 68 20 6d 75   working with mu
1b80: 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 73  ltiple databases
1b90: 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
1ba0: 65 0a 28 75 73 69 6e 67 20 74 68 65 20 5b 41 54  e.(using the [AT
1bb0: 54 41 43 48 5d 20 63 6f 6d 6d 61 6e 64 29 20 74  TACH] command) t
1bc0: 68 65 6e 20 65 61 63 68 20 64 61 74 61 62 61 73  hen each databas
1bd0: 65 20 68 61 73 20 69 74 73 20 6f 77 6e 20 72 6f  e has its own ro
1be0: 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a  llback journal..
1bf0: 42 75 74 20 74 68 65 72 65 20 69 73 20 61 6c 73  But there is als
1c00: 6f 20 61 20 73 65 70 61 72 61 74 65 20 61 67 67  o a separate agg
1c10: 72 65 67 61 74 65 20 6a 6f 75 72 6e 61 6c 0a 63  regate journal.c
1c20: 61 6c 6c 65 64 20 74 68 65 20 3c 65 6d 3e 6d 61  alled the <em>ma
1c30: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 3c 2f 65 6d  ster journal</em
1c40: 3e 2e 0a 54 68 65 20 6d 61 73 74 65 72 20 6a 6f  >..The master jo
1c50: 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 63  urnal does not c
1c60: 6f 6e 74 61 69 6e 20 70 61 67 65 20 64 61 74 61  ontain page data
1c70: 20 75 73 65 64 20 66 6f 72 20 72 6f 6c 6c 69 6e   used for rollin
1c80: 67 20 62 61 63 6b 0a 63 68 61 6e 67 65 73 2e 20  g back.changes. 
1c90: 20 49 6e 73 74 65 61 64 20 74 68 65 20 6d 61 73   Instead the mas
1ca0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74  ter journal cont
1cb0: 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 73 20 6f  ains the names o
1cc0: 66 20 74 68 65 0a 69 6e 64 69 76 69 64 75 61 6c  f the.individual
1cd0: 20 64 61 74 61 62 61 73 65 20 72 6f 6c 6c 62 61   database rollba
1ce0: 63 6b 20 6a 6f 75 72 6e 61 6c 73 20 66 6f 72 20  ck journals for 
1cf0: 65 61 63 68 20 6f 66 20 74 68 65 20 41 54 54 41  each of the ATTA
1d00: 43 48 65 64 20 64 61 74 61 62 61 73 65 73 2e 20  CHed databases. 
1d10: 0a 45 61 63 68 20 6f 66 20 74 68 65 20 69 6e 64  .Each of the ind
1d20: 69 76 69 64 75 61 6c 20 64 61 74 61 62 61 73 65  ividual database
1d30: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1d40: 6c 73 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20  ls also contain 
1d50: 74 68 65 20 6e 61 6d 65 20 0a 6f 66 20 74 68 65  the name .of the
1d60: 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
1d70: 0a 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f  .If there are no
1d80: 20 41 54 54 41 43 48 65 64 20 64 61 74 61 62 61   ATTACHed databa
1d90: 73 65 73 20 28 6f 72 20 69 66 20 6e 6f 6e 65 20  ses (or if none 
1da0: 6f 66 20 74 68 65 20 41 54 54 41 43 48 65 64 20  of the ATTACHed 
1db0: 64 61 74 61 62 61 73 65 0a 69 73 20 70 61 72 74  database.is part
1dc0: 69 63 69 70 61 74 69 6e 67 20 69 6e 20 74 68 65  icipating in the
1dd0: 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
1de0: 74 69 6f 6e 29 20 6e 6f 20 6d 61 73 74 65 72 20  tion) no master 
1df0: 6a 6f 75 72 6e 61 6c 20 69 73 0a 63 72 65 61 74  journal is.creat
1e00: 65 64 20 61 6e 64 20 74 68 65 20 6e 6f 72 6d 61  ed and the norma
1e10: 6c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  l rollback journ
1e20: 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65  al contains an e
1e30: 6d 70 74 79 20 73 74 72 69 6e 67 0a 69 6e 20 74  mpty string.in t
1e40: 68 65 20 70 6c 61 63 65 20 6e 6f 72 6d 61 6c 6c  he place normall
1e50: 79 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 72  y reserved for r
1e60: 65 63 6f 72 64 69 6e 67 20 74 68 65 20 6e 61 6d  ecording the nam
1e70: 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 0a  e of the master.
1e80: 6a 6f 75 72 6e 61 6c 2e 3c 2f 70 3e 0a 0a 3c 70  journal.</p>..<p
1e90: 3e 41 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  >A rollback jour
1ea0: 6e 61 6c 20 69 73 20 73 61 69 64 20 74 6f 20 62  nal is said to b
1eb0: 65 20 5b 68 6f 74 20 6a 6f 75 72 6e 61 6c 7c 68  e [hot journal|h
1ec0: 6f 74 5d 0a 69 66 20 69 74 20 6e 65 65 64 73 20  ot].if it needs 
1ed0: 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
1ee0: 6b 0a 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65  k.in order to re
1ef0: 73 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 72  store the integr
1f00: 69 74 79 20 6f 66 20 69 74 73 20 64 61 74 61 62  ity of its datab
1f10: 61 73 65 2e 20 20 0a 41 20 68 6f 74 20 6a 6f 75  ase.  .A hot jou
1f20: 72 6e 61 6c 20 69 73 20 63 72 65 61 74 65 64 20  rnal is created 
1f30: 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 69  when a process i
1f40: 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
1f50: 6f 66 20 61 20 64 61 74 61 62 61 73 65 0a 75 70  of a database.up
1f60: 64 61 74 65 20 61 6e 64 20 61 20 70 72 6f 67 72  date and a progr
1f70: 61 6d 20 6f 72 20 6f 70 65 72 61 74 69 6e 67 20  am or operating 
1f80: 73 79 73 74 65 6d 20 63 72 61 73 68 20 6f 72 20  system crash or 
1f90: 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 70 72  power failure pr
1fa0: 65 76 65 6e 74 73 20 0a 74 68 65 20 75 70 64 61  events .the upda
1fb0: 74 65 20 66 72 6f 6d 20 63 6f 6d 70 6c 65 74 69  te from completi
1fc0: 6e 67 2e 0a 48 6f 74 20 6a 6f 75 72 6e 61 6c 73  ng..Hot journals
1fd0: 20 61 72 65 20 61 6e 20 65 78 63 65 70 74 69 6f   are an exceptio
1fe0: 6e 20 63 6f 6e 64 69 74 69 6f 6e 2e 20 0a 48 6f  n condition. .Ho
1ff0: 74 20 6a 6f 75 72 6e 61 6c 73 20 65 78 69 73 74  t journals exist
2000: 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72 6f 6d   to recover from
2010: 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77   crashes and pow
2020: 65 72 20 66 61 69 6c 75 72 65 73 2e 0a 49 66 20  er failures..If 
2030: 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 77 6f  everything is wo
2040: 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 20  rking correctly 
2050: 0a 28 74 68 61 74 20 69 73 2c 20 69 66 20 74 68  .(that is, if th
2060: 65 72 65 20 61 72 65 20 6e 6f 20 63 72 61 73 68  ere are no crash
2070: 65 73 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c  es or power fail
2080: 75 72 65 73 29 0a 79 6f 75 20 77 69 6c 6c 20 6e  ures).you will n
2090: 65 76 65 72 20 67 65 74 20 61 20 68 6f 74 20 6a  ever get a hot j
20a0: 6f 75 72 6e 61 6c 2e 0a 3c 2f 70 3e 0a 0a 3c 70  ournal..</p>..<p
20b0: 3e 0a 49 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a  >.If no master j
20c0: 6f 75 72 6e 61 6c 20 69 73 20 69 6e 76 6f 6c 76  ournal is involv
20d0: 65 64 2c 20 74 68 65 6e 0a 61 20 6a 6f 75 72 6e  ed, then.a journ
20e0: 61 6c 20 69 73 20 68 6f 74 20 69 66 20 69 74 20  al is hot if it 
20f0: 65 78 69 73 74 73 20 61 6e 64 20 68 61 73 20 61  exists and has a
2100: 20 6e 6f 6e 2d 7a 65 72 6f 20 68 65 61 64 65 72   non-zero header
2110: 0a 61 6e 64 20 69 74 73 20 63 6f 72 72 65 73 70  .and its corresp
2120: 6f 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20  onding database 
2130: 66 69 6c 65 0a 64 6f 65 73 20 6e 6f 74 20 68 61  file.does not ha
2140: 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ve a RESERVED lo
2150: 63 6b 2e 0a 49 66 20 61 20 6d 61 73 74 65 72 20  ck..If a master 
2160: 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 61 6d 65 64  journal is named
2170: 20 69 6e 20 74 68 65 20 66 69 6c 65 20 6a 6f 75   in the file jou
2180: 72 6e 61 6c 2c 20 74 68 65 6e 20 74 68 65 20 66  rnal, then the f
2190: 69 6c 65 20 6a 6f 75 72 6e 61 6c 0a 69 73 20 68  ile journal.is h
21a0: 6f 74 20 69 66 20 69 74 73 20 6d 61 73 74 65 72  ot if its master
21b0: 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 20   journal exists 
21c0: 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20  and there is no 
21d0: 52 45 53 45 52 56 45 44 0a 6c 6f 63 6b 20 6f 6e  RESERVED.lock on
21e0: 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
21f0: 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ng database file
2200: 2e 0a 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  ..It is importan
2210: 74 20 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20  t to understand 
2220: 77 68 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 69  when a journal i
2230: 73 20 68 6f 74 20 73 6f 20 74 68 65 0a 70 72 65  s hot so the.pre
2240: 63 65 64 69 6e 67 20 72 75 6c 65 73 20 77 69 6c  ceding rules wil
2250: 6c 20 62 65 20 72 65 70 65 61 74 65 64 20 69 6e  l be repeated in
2260: 20 62 75 6c 6c 65 74 73 3a 0a 3c 2f 70 3e 0a 0a   bullets:.</p>..
2270: 3c 75 6c 3e 0a 3c 6c 69 3e 41 20 6a 6f 75 72 6e  <ul>.<li>A journ
2280: 61 6c 20 69 73 20 68 6f 74 20 69 66 2e 2e 2e 0a  al is hot if....
2290: 20 20 20 20 3c 75 6c 3e 0a 20 20 20 20 3c 6c 69      <ul>.    <li
22a0: 3e 49 74 20 65 78 69 73 74 73 2c 20 61 6e 64 3c  >It exists, and<
22b0: 2f 6c 69 3e 0a 20 20 20 20 3c 6c 69 3e 49 74 73  /li>.    <li>Its
22c0: 20 73 69 7a 65 20 69 73 20 67 72 65 61 74 65 72   size is greater
22d0: 20 74 68 61 6e 20 35 31 32 20 62 79 74 65 73 2c   than 512 bytes,
22e0: 20 61 6e 64 3c 2f 6c 69 3e 0a 20 20 20 20 3c 6c   and</li>.    <l
22f0: 69 3e 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  i>The journal he
2300: 61 64 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  ader is non-zero
2310: 20 61 6e 64 20 77 65 6c 6c 2d 66 6f 72 6d 65 64   and well-formed
2320: 2c 20 61 6e 64 3c 2f 6c 69 3e 0a 20 20 20 20 3c  , and</li>.    <
2330: 6c 69 3e 49 74 73 20 6d 61 73 74 65 72 20 6a 6f  li>Its master jo
2340: 75 72 6e 61 6c 20 65 78 69 73 74 73 20 6f 72 20  urnal exists or 
2350: 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
2360: 61 6c 20 6e 61 6d 65 20 69 73 20 61 6e 0a 20 20  al name is an.  
2370: 20 20 20 20 20 20 65 6d 70 74 79 20 73 74 72 69        empty stri
2380: 6e 67 2c 20 61 6e 64 3c 2f 6c 69 3e 0a 20 20 20  ng, and</li>.   
2390: 20 3c 6c 69 3e 54 68 65 72 65 20 69 73 20 6e 6f   <li>There is no
23a0: 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f   RESERVED lock o
23b0: 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
23c0: 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 6c  ing database fil
23d0: 65 2e 3c 2f 6c 69 3e 0a 20 20 20 20 3c 2f 75 6c  e.</li>.    </ul
23e0: 3e 0a 3c 2f 6c 69 3e 0a 3c 2f 75 6c 3e 0a 0a 3c  >.</li>.</ul>..<
23f0: 74 63 6c 3e 48 45 41 44 49 4e 47 20 32 20 7b 44  tcl>HEADING 2 {D
2400: 65 61 6c 69 6e 67 20 77 69 74 68 20 68 6f 74 20  ealing with hot 
2410: 6a 6f 75 72 6e 61 6c 73 7d 20 68 6f 74 5f 6a 6f  journals} hot_jo
2420: 75 72 6e 61 6c 73 3c 2f 74 63 6c 3e 0a 0a 3c 70  urnals</tcl>..<p
2430: 3e 0a 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67  >.Before reading
2440: 20 66 72 6f 6d 20 61 20 64 61 74 61 62 61 73 65   from a database
2450: 20 66 69 6c 65 2c 20 53 51 4c 69 74 65 20 61 6c   file, SQLite al
2460: 77 61 79 73 20 63 68 65 63 6b 73 20 74 6f 20 73  ways checks to s
2470: 65 65 20 69 66 20 74 68 61 74 0a 64 61 74 61 62  ee if that.datab
2480: 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 20 68  ase file has a h
2490: 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20  ot journal.  If 
24a0: 74 68 65 20 66 69 6c 65 20 64 6f 65 73 20 68 61  the file does ha
24b0: 76 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  ve a hot journal
24c0: 2c 20 74 68 65 6e 0a 74 68 65 20 6a 6f 75 72 6e  , then.the journ
24d0: 61 6c 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  al is rolled bac
24e0: 6b 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 6c  k before the fil
24f0: 65 20 69 73 20 72 65 61 64 2e 20 20 49 6e 20 74  e is read.  In t
2500: 68 69 73 20 77 61 79 2c 20 77 65 20 65 6e 73 75  his way, we ensu
2510: 72 65 0a 74 68 61 74 20 74 68 65 20 64 61 74 61  re.that the data
2520: 62 61 73 65 20 66 69 6c 65 20 69 73 20 69 6e 20  base file is in 
2530: 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61  a consistent sta
2540: 74 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  te before it is 
2550: 72 65 61 64 2e 0a 3c 2f 70 3e 0a 0a 3c 70 3e 57  read..</p>..<p>W
2560: 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 77 61  hen a process wa
2570: 6e 74 73 20 74 6f 20 72 65 61 64 20 66 72 6f 6d  nts to read from
2580: 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
2590: 2c 20 69 74 20 66 6f 6c 6c 6f 77 65 64 0a 74 68  , it followed.th
25a0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75  e following sequ
25b0: 65 6e 63 65 20 6f 66 20 73 74 65 70 73 3a 0a 3c  ence of steps:.<
25c0: 2f 70 3e 0a 0a 3c 6f 6c 3e 0a 3c 6c 69 3e 4f 70  /p>..<ol>.<li>Op
25d0: 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
25e0: 66 69 6c 65 20 61 6e 64 20 6f 62 74 61 69 6e 20  file and obtain 
25f0: 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20  a SHARED lock.  
2600: 49 66 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f  If the SHARED lo
2610: 63 6b 0a 20 20 20 20 63 61 6e 6e 6f 74 20 62 65  ck.    cannot be
2620: 20 6f 62 74 61 69 6e 65 64 2c 20 66 61 69 6c 20   obtained, fail 
2630: 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 6e 64 20  immediately and 
2640: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
2650: 53 59 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e 43 68 65  SY.</li>.<li>Che
2660: 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2670: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
2680: 61 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  as a hot journal
2690: 2e 20 20 20 49 66 20 74 68 65 20 66 69 6c 65 0a  .   If the file.
26a0: 20 20 20 20 64 6f 65 73 20 6e 6f 74 20 68 61 76      does not hav
26b0: 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c  e a hot journal,
26c0: 20 77 65 20 61 72 65 20 64 6f 6e 65 2e 20 20 52   we are done.  R
26d0: 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c  eturn immediatel
26e0: 79 2e 0a 20 20 20 20 49 66 20 74 68 65 72 65 20  y..    If there 
26f0: 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  is a hot journal
2700: 2c 20 74 68 61 74 20 6a 6f 75 72 6e 61 6c 20 6d  , that journal m
2710: 75 73 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ust be rolled ba
2720: 63 6b 20 62 79 0a 20 20 20 20 74 68 65 20 73 75  ck by.    the su
2730: 62 73 65 71 75 65 6e 74 20 73 74 65 70 73 20 6f  bsequent steps o
2740: 66 20 74 68 69 73 20 61 6c 67 6f 72 69 74 68 6d  f this algorithm
2750: 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e 41 63 71 75 69  .</li>.<li>Acqui
2760: 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
2770: 6b 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53  k then an EXCLUS
2780: 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
2790: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
27a0: 20 20 20 28 4e 6f 74 65 3a 20 44 6f 20 6e 6f 74     (Note: Do not
27b0: 20 61 63 71 75 69 72 65 20 61 20 52 45 53 45 52   acquire a RESER
27c0: 56 45 44 20 6c 6f 63 6b 20 62 65 63 61 75 73 65  VED lock because
27d0: 20 74 68 61 74 20 77 6f 75 6c 64 20 6d 61 6b 65   that would make
27e0: 0a 20 20 20 20 6f 74 68 65 72 20 70 72 6f 63 65  .    other proce
27f0: 73 73 65 73 20 74 68 69 6e 6b 20 74 68 65 20 6a  sses think the j
2800: 6f 75 72 6e 61 6c 20 77 61 73 20 6e 6f 20 6c 6f  ournal was no lo
2810: 6e 67 65 72 20 68 6f 74 2e 29 20 20 49 66 20 77  nger hot.)  If w
2820: 65 0a 20 20 20 20 66 61 69 6c 20 74 6f 20 61 63  e.    fail to ac
2830: 71 75 69 72 65 20 74 68 65 73 65 20 6c 6f 63 6b  quire these lock
2840: 73 20 69 74 20 6d 65 61 6e 73 20 61 6e 6f 74 68  s it means anoth
2850: 65 72 20 70 72 6f 63 65 73 73 0a 20 20 20 20 69  er process.    i
2860: 73 20 61 6c 72 65 61 64 79 20 74 72 79 69 6e 67  s already trying
2870: 20 74 6f 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62   to do the rollb
2880: 61 63 6b 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ack.  In that ca
2890: 73 65 2c 0a 20 20 20 20 64 72 6f 70 20 61 6c 6c  se,.    drop all
28a0: 20 6c 6f 63 6b 73 2c 20 63 6c 6f 73 65 20 74 68   locks, close th
28b0: 65 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 20  e database, and 
28c0: 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
28d0: 53 59 2e 20 3c 2f 6c 69 3e 0a 3c 6c 69 3e 52 65  SY. </li>.<li>Re
28e0: 61 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ad the journal f
28f0: 69 6c 65 20 61 6e 64 20 72 6f 6c 6c 20 62 61 63  ile and roll bac
2900: 6b 20 74 68 65 20 63 68 61 6e 67 65 73 2e 3c 2f  k the changes.</
2910: 6c 69 3e 0a 3c 6c 69 3e 57 61 69 74 20 66 6f 72  li>.<li>Wait for
2920: 20 74 68 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b   the rolled back
2930: 20 63 68 61 6e 67 65 73 20 74 6f 20 62 65 20 77   changes to be w
2940: 72 69 74 74 65 6e 20 6f 6e 74 6f 20 0a 20 20 20  ritten onto .   
2950: 20 70 65 72 73 69 73 74 65 6e 74 20 73 74 6f 72   persistent stor
2960: 61 67 65 2e 20 20 54 68 69 73 20 70 72 6f 74 65  age.  This prote
2970: 63 74 73 20 74 68 65 20 69 6e 74 65 67 72 69 74  cts the integrit
2980: 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
2990: 65 0a 20 20 20 20 69 6e 20 63 61 73 65 20 61 6e  e.    in case an
29a0: 6f 74 68 65 72 20 70 6f 77 65 72 20 66 61 69 6c  other power fail
29b0: 75 72 65 20 6f 72 20 63 72 61 73 68 20 6f 63 63  ure or crash occ
29c0: 75 72 73 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e 44 65  urs.</li>.<li>De
29d0: 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
29e0: 20 66 69 6c 65 20 28 6f 72 20 74 72 75 6e 63 61   file (or trunca
29f0: 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  te the journal t
2a00: 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 0a  o zero bytes in.
2a10: 20 20 20 20 6c 65 6e 67 74 68 20 69 66 20 5b 50      length if [P
2a20: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
2a30: 64 65 20 7c 20 50 52 41 47 4d 41 20 6a 6f 75 72  de | PRAGMA jour
2a40: 6e 61 6c 5f 6d 6f 64 65 3d 54 52 55 4e 43 41 54  nal_mode=TRUNCAT
2a50: 45 5d 20 69 73 0a 20 20 20 20 73 65 74 2c 20 6f  E] is.    set, o
2a60: 72 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75 72 6e  r zero the journ
2a70: 61 6c 20 68 65 61 64 65 72 20 69 66 0a 20 20 20  al header if.   
2a80: 20 5b 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c   [PRAGMA journal
2a90: 5f 6d 6f 64 65 20 7c 20 50 52 41 47 4d 41 20 6a  _mode | PRAGMA j
2aa0: 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 50 45 52 53  ournal_mode=PERS
2ab0: 49 53 54 5d 20 69 73 20 73 65 74 29 2e 3c 2f 6c  IST] is set).</l
2ac0: 69 3e 0a 3c 6c 69 3e 44 65 6c 65 74 65 20 74 68  i>.<li>Delete th
2ad0: 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
2ae0: 20 66 69 6c 65 20 69 66 20 69 74 20 69 73 20 73   file if it is s
2af0: 61 66 65 20 74 6f 20 64 6f 20 73 6f 2e 0a 20 20  afe to do so..  
2b00: 20 20 54 68 69 73 20 73 74 65 70 20 69 73 20 6f    This step is o
2b10: 70 74 69 6f 6e 61 6c 2e 20 20 49 74 20 69 73 20  ptional.  It is 
2b20: 68 65 72 65 20 6f 6e 6c 79 20 74 6f 20 70 72 65  here only to pre
2b30: 76 65 6e 74 20 73 74 61 6c 65 0a 20 20 20 20 6d  vent stale.    m
2b40: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 73 20 66  aster journals f
2b50: 72 6f 6d 20 63 6c 75 74 74 65 72 69 6e 67 20 75  rom cluttering u
2b60: 70 20 74 68 65 20 64 69 73 6b 20 64 72 69 76 65  p the disk drive
2b70: 2e 0a 20 20 20 20 53 65 65 20 74 68 65 20 64 69  ..    See the di
2b80: 73 63 75 73 73 69 6f 6e 20 62 65 6c 6f 77 20 66  scussion below f
2b90: 6f 72 20 64 65 74 61 69 6c 73 2e 3c 2f 6c 69 3e  or details.</li>
2ba0: 0a 3c 6c 69 3e 44 72 6f 70 20 74 68 65 20 45 58  .<li>Drop the EX
2bb0: 43 4c 55 53 49 56 45 20 61 6e 64 20 50 45 4e 44  CLUSIVE and PEND
2bc0: 49 4e 47 20 6c 6f 63 6b 73 20 62 75 74 20 72 65  ING locks but re
2bd0: 74 61 69 6e 20 74 68 65 20 53 48 41 52 45 44 20  tain the SHARED 
2be0: 6c 6f 63 6b 2e 3c 2f 6c 69 3e 0a 3c 2f 6f 6c 3e  lock.</li>.</ol>
2bf0: 0a 0a 3c 70 3e 41 66 74 65 72 20 74 68 65 20 61  ..<p>After the a
2c00: 6c 67 6f 72 69 74 68 6d 20 61 62 6f 76 65 20 63  lgorithm above c
2c10: 6f 6d 70 6c 65 74 65 73 20 73 75 63 63 65 73 73  ompletes success
2c20: 66 75 6c 6c 79 2c 20 69 74 20 69 73 20 73 61 66  fully, it is saf
2c30: 65 20 74 6f 20 0a 72 65 61 64 20 66 72 6f 6d 20  e to .read from 
2c40: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
2c50: 65 2e 20 20 4f 6e 63 65 20 61 6c 6c 20 72 65 61  e.  Once all rea
2c60: 64 69 6e 67 20 68 61 73 20 63 6f 6d 70 6c 65 74  ding has complet
2c70: 65 64 2c 20 74 68 65 0a 53 48 41 52 45 44 20 6c  ed, the.SHARED l
2c80: 6f 63 6b 20 69 73 20 64 72 6f 70 70 65 64 2e 3c  ock is dropped.<
2c90: 2f 70 3e 0a 0a 3c 74 63 6c 3e 48 45 41 44 49 4e  /p>..<tcl>HEADIN
2ca0: 47 20 32 20 7b 44 65 6c 65 74 69 6e 67 20 73 74  G 2 {Deleting st
2cb0: 61 6c 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ale master journ
2cc0: 61 6c 73 7d 20 73 74 61 6c 65 5f 6d 61 73 74 65  als} stale_maste
2cd0: 72 5f 6a 6f 75 72 6e 61 6c 73 3c 2f 74 63 6c 3e  r_journals</tcl>
2ce0: 0a 0a 3c 70 3e 41 20 73 74 61 6c 65 20 6d 61 73  ..<p>A stale mas
2cf0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  ter journal is a
2d00: 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
2d10: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  that is no longe
2d20: 72 20 62 65 69 6e 67 0a 75 73 65 64 20 66 6f 72  r being.used for
2d30: 20 61 6e 79 74 68 69 6e 67 2e 20 20 54 68 65 72   anything.  Ther
2d40: 65 20 69 73 20 6e 6f 20 72 65 71 75 69 72 65 6d  e is no requirem
2d50: 65 6e 74 20 74 68 61 74 20 73 74 61 6c 65 20 6d  ent that stale m
2d60: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 73 0a 62  aster journals.b
2d70: 65 20 64 65 6c 65 74 65 64 2e 20 20 54 68 65 20  e deleted.  The 
2d80: 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 66 6f 72 20  only reason for 
2d90: 64 6f 69 6e 67 20 73 6f 20 69 73 20 74 6f 20 66  doing so is to f
2da0: 72 65 65 20 75 70 20 64 69 73 6b 20 73 70 61 63  ree up disk spac
2db0: 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 41 20 6d 61 73  e.</p>..<p>A mas
2dc0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  ter journal is s
2dd0: 74 61 6c 65 20 69 66 20 6e 6f 20 69 6e 64 69 76  tale if no indiv
2de0: 69 64 75 61 6c 20 66 69 6c 65 20 6a 6f 75 72 6e  idual file journ
2df0: 61 6c 73 20 61 72 65 20 70 6f 69 6e 74 69 6e 67  als are pointing
2e00: 0a 74 6f 20 69 74 2e 20 20 54 6f 20 66 69 67 75  .to it.  To figu
2e10: 72 65 20 6f 75 74 20 69 66 20 61 20 6d 61 73 74  re out if a mast
2e20: 65 72 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 74  er journal is st
2e30: 61 6c 65 2c 20 77 65 20 66 69 72 73 74 20 72 65  ale, we first re
2e40: 61 64 20 74 68 65 0a 6d 61 73 74 65 72 20 6a 6f  ad the.master jo
2e50: 75 72 6e 61 6c 20 74 6f 20 6f 62 74 61 69 6e 20  urnal to obtain 
2e60: 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c  the names of all
2e70: 20 6f 66 20 69 74 73 20 66 69 6c 65 20 6a 6f 75   of its file jou
2e80: 72 6e 61 6c 73 2e 20 20 54 68 65 6e 0a 77 65 20  rnals.  Then.we 
2e90: 63 68 65 63 6b 20 65 61 63 68 20 6f 66 20 74 68  check each of th
2ea0: 6f 73 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c  ose file journal
2eb0: 73 2e 20 20 49 66 20 61 6e 79 20 6f 66 20 74 68  s.  If any of th
2ec0: 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 73 20  e file journals 
2ed0: 6e 61 6d 65 64 0a 69 6e 20 74 68 65 20 6d 61 73  named.in the mas
2ee0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73  ter journal exis
2ef0: 74 73 20 61 6e 64 20 70 6f 69 6e 74 73 20 62 61  ts and points ba
2f00: 63 6b 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72  ck to the master
2f10: 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 0a 74   journal, then.t
2f20: 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
2f30: 6c 20 69 73 20 6e 6f 74 20 73 74 61 6c 65 2e 20  l is not stale. 
2f40: 20 49 66 20 61 6c 6c 20 66 69 6c 65 20 6a 6f 75   If all file jou
2f50: 72 6e 61 6c 73 20 61 72 65 20 65 69 74 68 65 72  rnals are either
2f60: 20 6d 69 73 73 69 6e 67 0a 6f 72 20 72 65 66 65   missing.or refe
2f70: 72 20 74 6f 20 6f 74 68 65 72 20 6d 61 73 74 65  r to other maste
2f80: 72 20 6a 6f 75 72 6e 61 6c 73 20 6f 72 20 6e 6f  r journals or no
2f90: 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
2fa0: 61 74 20 61 6c 6c 2c 20 74 68 65 6e 20 74 68 65  at all, then the
2fb0: 0a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20  .master journal 
2fc0: 77 65 20 61 72 65 20 74 65 73 74 69 6e 67 20 69  we are testing i
2fd0: 73 20 73 74 61 6c 65 20 61 6e 64 20 63 61 6e 20  s stale and can 
2fe0: 62 65 20 73 61 66 65 6c 79 20 64 65 6c 65 74 65  be safely delete
2ff0: 64 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 48 45 41  d.</p>..<tcl>HEA
3000: 44 49 4e 47 20 31 20 7b 57 72 69 74 69 6e 67 20  DING 1 {Writing 
3010: 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69  to a database fi
3020: 6c 65 7d 20 77 72 69 74 69 6e 67 3c 2f 74 63 6c  le} writing</tcl
3030: 3e 0a 0a 3c 70 3e 54 6f 20 77 72 69 74 65 20 74  >..<p>To write t
3040: 6f 20 61 20 64 61 74 61 62 61 73 65 2c 20 61 20  o a database, a 
3050: 70 72 6f 63 65 73 73 20 6d 75 73 74 20 66 69 72  process must fir
3060: 73 74 20 61 63 71 75 69 72 65 20 61 20 53 48 41  st acquire a SHA
3070: 52 45 44 20 6c 6f 63 6b 0a 61 73 20 64 65 73 63  RED lock.as desc
3080: 72 69 62 65 64 20 61 62 6f 76 65 20 28 70 6f 73  ribed above (pos
3090: 73 69 62 6c 79 20 72 6f 6c 6c 69 6e 67 20 62 61  sibly rolling ba
30a0: 63 6b 20 69 6e 63 6f 6d 70 6c 65 74 65 20 63 68  ck incomplete ch
30b0: 61 6e 67 65 73 20 69 66 20 74 68 65 72 65 0a 69  anges if there.i
30c0: 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 29  s a hot journal)
30d0: 2e 20 0a 41 66 74 65 72 20 61 20 53 48 41 52 45  . .After a SHARE
30e0: 44 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  D lock is obtain
30f0: 65 64 2c 20 61 20 52 45 53 45 52 56 45 44 20 6c  ed, a RESERVED l
3100: 6f 63 6b 20 6d 75 73 74 20 62 65 20 61 63 71 75  ock must be acqu
3110: 69 72 65 64 2e 0a 54 68 65 20 52 45 53 45 52 56  ired..The RESERV
3120: 45 44 20 6c 6f 63 6b 20 73 69 67 6e 61 6c 73 20  ED lock signals 
3130: 74 68 61 74 20 74 68 65 20 70 72 6f 63 65 73 73  that the process
3140: 20 69 6e 74 65 6e 64 73 20 74 6f 20 77 72 69 74   intends to writ
3150: 65 20 74 6f 20 74 68 65 0a 64 61 74 61 62 61 73  e to the.databas
3160: 65 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20  e at some point 
3170: 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 20 20  in the future.  
3180: 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73  Only one process
3190: 20 61 74 20 61 20 74 69 6d 65 0a 63 61 6e 20 68   at a time.can h
31a0: 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c  old a RESERVED l
31b0: 6f 63 6b 2e 20 20 42 75 74 20 6f 74 68 65 72 20  ock.  But other 
31c0: 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20 63 6f  processes can co
31d0: 6e 74 69 6e 75 65 20 74 6f 20 72 65 61 64 0a 74  ntinue to read.t
31e0: 68 65 20 64 61 74 61 62 61 73 65 20 77 68 69 6c  he database whil
31f0: 65 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c  e the RESERVED l
3200: 6f 63 6b 20 69 73 20 68 65 6c 64 2e 0a 3c 2f 70  ock is held..</p
3210: 3e 0a 0a 3c 70 3e 49 66 20 74 68 65 20 70 72 6f  >..<p>If the pro
3220: 63 65 73 73 20 74 68 61 74 20 77 61 6e 74 73 20  cess that wants 
3230: 74 6f 20 77 72 69 74 65 20 69 73 20 75 6e 61 62  to write is unab
3240: 6c 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 52  le to obtain a R
3250: 45 53 45 52 56 45 44 0a 6c 6f 63 6b 2c 20 69 74  ESERVED.lock, it
3260: 20 6d 75 73 74 20 6d 65 61 6e 20 74 68 61 74 20   must mean that 
3270: 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  another process 
3280: 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 52 45  already has a RE
3290: 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 49 6e 20  SERVED lock..In 
32a0: 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 77  that case, the w
32b0: 72 69 74 65 20 61 74 74 65 6d 70 74 20 66 61 69  rite attempt fai
32c0: 6c 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 53  ls and returns S
32d0: 51 4c 49 54 45 5f 42 55 53 59 2e 3c 2f 70 3e 0a  QLITE_BUSY.</p>.
32e0: 0a 3c 70 3e 41 66 74 65 72 20 6f 62 74 61 69 6e  .<p>After obtain
32f0: 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 20 6c  ing a RESERVED l
3300: 6f 63 6b 2c 20 74 68 65 20 70 72 6f 63 65 73 73  ock, the process
3310: 20 74 68 61 74 20 77 61 6e 74 73 20 74 6f 20 77   that wants to w
3320: 72 69 74 65 0a 63 72 65 61 74 65 73 20 61 20 72  rite.creates a r
3330: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
3340: 20 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20    The header of 
3350: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 69  the journal is i
3360: 6e 69 74 69 61 6c 69 7a 65 64 0a 77 69 74 68 20  nitialized.with 
3370: 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  the original siz
3380: 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
3390: 65 20 66 69 6c 65 2e 20 20 53 70 61 63 65 20 69  e file.  Space i
33a0: 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  n the journal he
33b0: 61 64 65 72 0a 69 73 20 61 6c 73 6f 20 72 65 73  ader.is also res
33c0: 65 72 76 65 64 20 66 6f 72 20 61 20 6d 61 73 74  erved for a mast
33d0: 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c  er journal name,
33e0: 20 74 68 6f 75 67 68 20 74 68 65 20 6d 61 73 74   though the mast
33f0: 65 72 20 6a 6f 75 72 6e 61 6c 0a 6e 61 6d 65 20  er journal.name 
3400: 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70  is initially emp
3410: 74 79 2e 3c 2f 70 3e 0a 0a 3c 70 3e 42 65 66 6f  ty.</p>..<p>Befo
3420: 72 65 20 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65  re making change
3430: 73 20 74 6f 20 61 6e 79 20 70 61 67 65 20 6f 66  s to any page of
3440: 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
3450: 68 65 20 70 72 6f 63 65 73 73 20 77 72 69 74 65  he process write
3460: 73 0a 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63  s.the original c
3470: 6f 6e 74 65 6e 74 20 6f 66 20 74 68 61 74 20 70  ontent of that p
3480: 61 67 65 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c  age into the rol
3490: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20  lback journal.  
34a0: 43 68 61 6e 67 65 73 0a 74 6f 20 70 61 67 65 73  Changes.to pages
34b0: 20 61 72 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d   are held in mem
34c0: 6f 72 79 20 61 74 20 66 69 72 73 74 20 61 6e 64  ory at first and
34d0: 20 61 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e   are not written
34e0: 20 74 6f 20 74 68 65 20 64 69 73 6b 2e 0a 54 68   to the disk..Th
34f0: 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
3500: 61 73 65 20 66 69 6c 65 20 72 65 6d 61 69 6e 73  ase file remains
3510: 20 75 6e 61 6c 74 65 72 65 64 2c 20 77 68 69 63   unaltered, whic
3520: 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 74 68  h means that oth
3530: 65 72 0a 70 72 6f 63 65 73 73 65 73 20 63 61 6e  er.processes can
3540: 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 61   continue to rea
3550: 64 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 3c  d the database.<
3560: 2f 70 3e 0a 0a 3c 70 3e 45 76 65 6e 74 75 61 6c  /p>..<p>Eventual
3570: 6c 79 2c 20 74 68 65 20 77 72 69 74 69 6e 67 20  ly, the writing 
3580: 70 72 6f 63 65 73 73 20 77 69 6c 6c 20 77 61 6e  process will wan
3590: 74 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  t to update the 
35a0: 64 61 74 61 62 61 73 65 0a 66 69 6c 65 2c 20 65  database.file, e
35b0: 69 74 68 65 72 20 62 65 63 61 75 73 65 20 69 74  ither because it
35c0: 73 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 68  s memory cache h
35d0: 61 73 20 66 69 6c 6c 65 64 20 75 70 20 6f 72 20  as filled up or 
35e0: 62 65 63 61 75 73 65 20 69 74 20 69 73 0a 72 65  because it is.re
35f0: 61 64 79 20 74 6f 20 63 6f 6d 6d 69 74 20 69 74  ady to commit it
3600: 73 20 63 68 61 6e 67 65 73 2e 20 20 42 65 66 6f  s changes.  Befo
3610: 72 65 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c  re this happens,
3620: 20 74 68 65 20 77 72 69 74 65 72 20 6d 75 73 74   the writer must
3630: 0a 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6f 74  .make sure no ot
3640: 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 72  her process is r
3650: 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62  eading the datab
3660: 61 73 65 20 61 6e 64 20 74 68 61 74 20 74 68 65  ase and that the
3670: 20 72 6f 6c 6c 62 61 63 6b 0a 6a 6f 75 72 6e 61   rollback.journa
3680: 6c 20 64 61 74 61 20 69 73 20 73 61 66 65 6c 79  l data is safely
3690: 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 73 75 72   on the disk sur
36a0: 66 61 63 65 20 73 6f 20 74 68 61 74 20 69 74 20  face so that it 
36b0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 72  can be used to.r
36c0: 6f 6c 6c 62 61 63 6b 20 69 6e 63 6f 6d 70 6c 65  ollback incomple
36d0: 74 65 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  te changes in th
36e0: 65 20 65 76 65 6e 74 20 6f 66 20 61 20 70 6f 77  e event of a pow
36f0: 65 72 20 66 61 69 6c 75 72 65 2e 0a 54 68 65 20  er failure..The 
3700: 73 74 65 70 73 20 61 72 65 20 61 73 20 66 6f 6c  steps are as fol
3710: 6c 6f 77 73 3a 3c 2f 70 3e 0a 0a 3c 6f 6c 3e 0a  lows:</p>..<ol>.
3720: 3c 6c 69 3e 4d 61 6b 65 20 73 75 72 65 20 61 6c  <li>Make sure al
3730: 6c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  l rollback journ
3740: 61 6c 20 64 61 74 61 20 68 61 73 20 61 63 74 75  al data has actu
3750: 61 6c 6c 79 20 62 65 65 6e 20 77 72 69 74 74 65  ally been writte
3760: 6e 20 74 6f 0a 20 20 20 20 74 68 65 20 73 75 72  n to.    the sur
3770: 66 61 63 65 20 6f 66 20 74 68 65 20 64 69 73 6b  face of the disk
3780: 20 28 61 6e 64 20 69 73 20 6e 6f 74 20 6a 75 73   (and is not jus
3790: 74 20 62 65 69 6e 67 20 68 65 6c 64 20 69 6e 20  t being held in 
37a0: 74 68 65 20 6f 70 65 72 61 74 69 6e 67 0a 20 20  the operating.  
37b0: 20 20 73 79 73 74 65 6d 27 73 20 20 6f 72 20 64    system's  or d
37c0: 69 73 6b 20 63 6f 6e 74 72 6f 6c 6c 65 72 73 20  isk controllers 
37d0: 63 61 63 68 65 29 20 73 6f 20 74 68 61 74 20 69  cache) so that i
37e0: 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
37f0: 65 20 6f 63 63 75 72 73 0a 20 20 20 20 74 68 65  e occurs.    the
3800: 20 64 61 74 61 20 77 69 6c 6c 20 73 74 69 6c 6c   data will still
3810: 20 62 65 20 74 68 65 72 65 20 61 66 74 65 72 20   be there after 
3820: 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65  power is restore
3830: 64 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e 4f 62 74 61  d.</li>.<li>Obta
3840: 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  in a PENDING loc
3850: 6b 20 61 6e 64 20 74 68 65 6e 20 61 6e 20 45 58  k and then an EX
3860: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
3870: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
3880: 65 2e 0a 20 20 20 20 49 66 20 6f 74 68 65 72 20  e..    If other 
3890: 70 72 6f 63 65 73 73 65 73 20 73 74 69 6c 6c 20  processes still 
38a0: 68 61 76 65 20 53 48 41 52 45 44 20 6c 6f 63 6b  have SHARED lock
38b0: 73 2c 20 74 68 65 20 77 72 69 74 65 72 20 6d 69  s, the writer mi
38c0: 67 68 74 20 68 61 76 65 0a 20 20 20 20 74 6f 20  ght have.    to 
38d0: 77 61 69 74 20 75 6e 74 69 6c 20 74 68 6f 73 65  wait until those
38e0: 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 63 6c   SHARED locks cl
38f0: 65 61 72 20 62 65 66 6f 72 65 20 69 74 20 69 73  ear before it is
3900: 20 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e 0a   able to obtain.
3910: 20 20 20 20 61 6e 20 45 58 43 4c 55 53 49 56 45      an EXCLUSIVE
3920: 20 6c 6f 63 6b 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e   lock.</li>.<li>
3930: 57 72 69 74 65 20 61 6c 6c 20 70 61 67 65 20 6d  Write all page m
3940: 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 63 75 72  odifications cur
3950: 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d  rently held in m
3960: 65 6d 6f 72 79 20 6f 75 74 20 74 6f 20 74 68 65  emory out to the
3970: 0a 20 20 20 20 6f 72 69 67 69 6e 61 6c 20 64 61  .    original da
3980: 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 6c 65  tabase disk file
3990: 2e 3c 2f 6c 69 3e 0a 3c 2f 6f 6c 3e 0a 0a 3c 70  .</li>.</ol>..<p
39a0: 3e 0a 49 66 20 74 68 65 20 72 65 61 73 6f 6e 20  >.If the reason 
39b0: 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 20 74  for writing to t
39c0: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
39d0: 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20   is because the 
39e0: 6d 65 6d 6f 72 79 0a 63 61 63 68 65 20 77 61 73  memory.cache was
39f0: 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20   full, then the 
3a00: 77 72 69 74 65 72 20 77 69 6c 6c 20 6e 6f 74 20  writer will not 
3a10: 63 6f 6d 6d 69 74 20 72 69 67 68 74 20 61 77 61  commit right awa
3a20: 79 2e 20 20 49 6e 73 74 65 61 64 2c 0a 74 68 65  y.  Instead,.the
3a30: 20 77 72 69 74 65 72 20 6d 69 67 68 74 20 63 6f   writer might co
3a40: 6e 74 69 6e 75 65 20 74 6f 20 6d 61 6b 65 20 63  ntinue to make c
3a50: 68 61 6e 67 65 73 20 74 6f 20 6f 74 68 65 72 20  hanges to other 
3a60: 70 61 67 65 73 2e 20 20 42 65 66 6f 72 65 20 0a  pages.  Before .
3a70: 73 75 62 73 65 71 75 65 6e 74 20 63 68 61 6e 67  subsequent chang
3a80: 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 74  es are written t
3a90: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
3aa0: 69 6c 65 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63  ile, the rollbac
3ab0: 6b 0a 6a 6f 75 72 6e 61 6c 20 6d 75 73 74 20 62  k.journal must b
3ac0: 65 20 66 6c 75 73 68 65 64 20 74 6f 20 64 69 73  e flushed to dis
3ad0: 6b 20 61 67 61 69 6e 2e 20 20 4e 6f 74 65 20 61  k again.  Note a
3ae0: 6c 73 6f 20 74 68 61 74 20 74 68 65 20 45 58 43  lso that the EXC
3af0: 4c 55 53 49 56 45 0a 6c 6f 63 6b 20 74 68 61 74  LUSIVE.lock that
3b00: 20 74 68 65 20 77 72 69 74 65 72 20 6f 62 74 61   the writer obta
3b10: 69 6e 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ined in order to
3b20: 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 61   write to the da
3b30: 74 61 62 61 73 65 20 69 6e 69 74 69 61 6c 6c 79  tabase initially
3b40: 0a 6d 75 73 74 20 62 65 20 68 65 6c 64 20 75 6e  .must be held un
3b50: 74 69 6c 20 61 6c 6c 20 63 68 61 6e 67 65 73 20  til all changes 
3b60: 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20  are committed.  
3b70: 54 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  That means that 
3b80: 6e 6f 20 6f 74 68 65 72 0a 70 72 6f 63 65 73 73  no other.process
3b90: 65 73 20 61 72 65 20 61 62 6c 65 20 74 6f 20 61  es are able to a
3ba0: 63 63 65 73 73 20 74 68 65 20 64 61 74 61 62 61  ccess the databa
3bb0: 73 65 20 66 72 6f 6d 20 74 68 65 0a 74 69 6d 65  se from the.time
3bc0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 61 63 68   the memory cach
3bd0: 65 20 66 69 72 73 74 20 73 70 69 6c 6c 73 20 74  e first spills t
3be0: 6f 20 64 69 73 6b 20 75 6e 74 69 6c 20 74 68 65  o disk until the
3bf0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 63 6f 6d   transaction.com
3c00: 6d 69 74 73 2e 0a 3c 2f 70 3e 0a 0a 3c 70 3e 0a  mits..</p>..<p>.
3c10: 57 68 65 6e 20 61 20 77 72 69 74 65 72 20 69 73  When a writer is
3c20: 20 72 65 61 64 79 20 74 6f 20 63 6f 6d 6d 69 74   ready to commit
3c30: 20 69 74 73 20 63 68 61 6e 67 65 73 2c 20 69 74   its changes, it
3c40: 20 65 78 65 63 75 74 65 73 20 74 68 65 20 66 6f   executes the fo
3c50: 6c 6c 6f 77 69 6e 67 0a 73 74 65 70 73 3a 0a 3c  llowing.steps:.<
3c60: 2f 70 3e 0a 0a 3c 6f 6c 3e 0a 3c 6c 69 20 76 61  /p>..<ol>.<li va
3c70: 6c 75 65 3d 22 34 22 3e 0a 20 20 20 4f 62 74 61  lue="4">.   Obta
3c80: 69 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  in an EXCLUSIVE 
3c90: 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
3ca0: 62 61 73 65 20 66 69 6c 65 20 61 6e 64 0a 20 20  base file and.  
3cb0: 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6d   make sure all m
3cc0: 65 6d 6f 72 79 20 63 68 61 6e 67 65 73 20 68 61  emory changes ha
3cd0: 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
3ce0: 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
3cf0: 66 69 6c 65 0a 20 20 20 75 73 69 6e 67 20 74 68  file.   using th
3d00: 65 20 61 6c 67 6f 72 69 74 68 6d 20 6f 66 20 73  e algorithm of s
3d10: 74 65 70 73 20 31 2d 33 20 61 62 6f 76 65 2e 3c  teps 1-3 above.<
3d20: 2f 6c 69 3e 0a 3c 6c 69 3e 46 6c 75 73 68 20 61  /li>.<li>Flush a
3d30: 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
3d40: 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
3d50: 64 69 73 6b 2e 20 20 57 61 69 74 20 66 6f 72 20  disk.  Wait for 
3d60: 74 68 6f 73 65 20 63 68 61 6e 67 65 73 0a 20 20  those changes.  
3d70: 20 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 62 65    to actually be
3d80: 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68   written onto th
3d90: 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 2e 3c  e disk surface.<
3da0: 2f 6c 69 3e 0a 3c 6c 69 3e 44 65 6c 65 74 65 20  /li>.<li>Delete 
3db0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
3dc0: 2e 20 20 28 4f 72 20 69 66 20 74 68 65 20 5b 50  .  (Or if the [P
3dd0: 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
3de0: 64 65 5d 20 69 73 20 54 52 55 4e 43 41 54 45 20  de] is TRUNCATE 
3df0: 6f 72 0a 20 20 20 20 50 45 52 53 49 53 54 2c 20  or.    PERSIST, 
3e00: 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75  truncate the jou
3e10: 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 7a 65 72  rnal file or zer
3e20: 6f 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20  o the header of 
3e30: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
3e40: 2c 0a 20 20 20 20 72 65 73 70 65 63 74 69 76 65  ,.    respective
3e50: 6c 79 2e 29 20 20 54 68 69 73 20 69 73 20 74 68  ly.)  This is th
3e60: 65 20 69 6e 73 74 61 6e 74 20 77 68 65 6e 20 74  e instant when t
3e70: 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 0a 20  he changes are. 
3e80: 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 50     committed.  P
3e90: 72 69 6f 72 20 74 6f 20 64 65 6c 65 74 69 6e 67  rior to deleting
3ea0: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
3eb0: 65 2c 20 69 66 20 61 20 70 6f 77 65 72 20 66 61  e, if a power fa
3ec0: 69 6c 75 72 65 0a 20 20 20 20 6f 72 20 63 72 61  ilure.    or cra
3ed0: 73 68 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6e  sh occurs, the n
3ee0: 65 78 74 20 70 72 6f 63 65 73 73 20 74 6f 20 6f  ext process to o
3ef0: 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
3f00: 20 77 69 6c 6c 20 73 65 65 20 74 68 61 74 0a 20   will see that. 
3f10: 20 20 20 69 74 20 68 61 73 20 61 20 68 6f 74 20     it has a hot 
3f20: 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 77 69 6c 6c  journal and will
3f30: 20 72 6f 6c 6c 20 74 68 65 20 63 68 61 6e 67 65   roll the change
3f40: 73 20 62 61 63 6b 2e 0a 20 20 20 20 41 66 74 65  s back..    Afte
3f50: 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  r the journal is
3f60: 20 64 65 6c 65 74 65 64 2c 20 74 68 65 72 65 20   deleted, there 
3f70: 77 69 6c 6c 20 6e 6f 20 6c 6f 6e 67 65 72 20 62  will no longer b
3f80: 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 0a  e a hot journal.
3f90: 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 61 6e      and the chan
3fa0: 67 65 73 20 77 69 6c 6c 20 70 65 72 73 69 73 74  ges will persist
3fb0: 2e 0a 20 20 20 20 3c 2f 6c 69 3e 0a 3c 6c 69 3e  ..    </li>.<li>
3fc0: 44 72 6f 70 20 74 68 65 20 45 58 43 4c 55 53 49  Drop the EXCLUSI
3fd0: 56 45 20 61 6e 64 20 50 45 4e 44 49 4e 47 20 6c  VE and PENDING l
3fe0: 6f 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 64 61  ocks from the da
3ff0: 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
4000: 20 3c 2f 6c 69 3e 0a 3c 2f 6f 6c 3e 0a 0a 3c 70   </li>.</ol>..<p
4010: 3e 41 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20  >As soon as the 
4020: 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20  PENDING lock is 
4030: 72 65 6c 65 61 73 65 64 20 66 72 6f 6d 20 74 68  released from th
4040: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
4050: 20 6f 74 68 65 72 0a 70 72 6f 63 65 73 73 65 73   other.processes
4060: 20 63 61 6e 20 62 65 67 69 6e 20 72 65 61 64 69   can begin readi
4070: 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
4080: 61 67 61 69 6e 2e 20 20 49 6e 20 74 68 65 20 63  again.  In the c
4090: 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74  urrent implement
40a0: 61 74 69 6f 6e 2c 0a 74 68 65 20 52 45 53 45 52  ation,.the RESER
40b0: 56 45 44 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f  VED lock is also
40c0: 20 72 65 6c 65 61 73 65 64 2c 20 62 75 74 20 74   released, but t
40d0: 68 61 74 20 69 73 20 6e 6f 74 20 65 73 73 65 6e  hat is not essen
40e0: 74 69 61 6c 20 66 6f 72 0a 63 6f 72 72 65 63 74  tial for.correct
40f0: 20 6f 70 65 72 61 74 69 6f 6e 2e 3c 2f 70 3e 0a   operation.</p>.
4100: 0a 3c 70 3e 49 66 20 61 20 74 72 61 6e 73 61 63  .<p>If a transac
4110: 74 69 6f 6e 20 69 6e 76 6f 6c 76 65 73 20 6d 75  tion involves mu
4120: 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 73  ltiple databases
4130: 2c 20 74 68 65 6e 20 61 20 6d 6f 72 65 20 63 6f  , then a more co
4140: 6d 70 6c 65 78 0a 63 6f 6d 6d 69 74 20 73 65 71  mplex.commit seq
4150: 75 65 6e 63 65 20 69 73 20 75 73 65 64 2c 20 61  uence is used, a
4160: 73 20 66 6f 6c 6c 6f 77 73 3a 3c 2f 70 3e 0a 0a  s follows:</p>..
4170: 3c 6f 6c 3e 0a 3c 6c 69 20 76 61 6c 75 65 3d 22  <ol>.<li value="
4180: 34 22 3e 0a 20 20 20 4d 61 6b 65 20 73 75 72 65  4">.   Make sure
4190: 20 61 6c 6c 20 69 6e 64 69 76 69 64 75 61 6c 20   all individual 
41a0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68  database files h
41b0: 61 76 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ave an EXCLUSIVE
41c0: 20 6c 6f 63 6b 20 61 6e 64 20 61 0a 20 20 20 76   lock and a.   v
41d0: 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 2e 0a 3c 6c  alid journal..<l
41e0: 69 3e 43 72 65 61 74 65 20 61 20 6d 61 73 74 65  i>Create a maste
41f0: 72 2d 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  r-journal.  The 
4200: 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74  name of the mast
4210: 65 72 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 72  er-journal is ar
4220: 62 69 74 72 61 72 79 2e 0a 20 20 20 20 28 54 68  bitrary..    (Th
4230: 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d  e current implem
4240: 65 6e 74 61 74 69 6f 6e 20 61 70 70 65 6e 64 73  entation appends
4250: 20 72 61 6e 64 6f 6d 20 73 75 66 66 69 78 65 73   random suffixes
4260: 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   to the name of 
4270: 74 68 65 0a 20 20 20 20 6d 61 69 6e 20 64 61 74  the.    main dat
4280: 61 62 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c  abase file until
4290: 20 69 74 20 66 69 6e 64 73 20 61 20 6e 61 6d 65   it finds a name
42a0: 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 70   that does not p
42b0: 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74 2e  reviously exist.
42c0: 29 0a 20 20 20 20 46 69 6c 6c 20 74 68 65 20 6d  ).    Fill the m
42d0: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 77 69  aster journal wi
42e0: 74 68 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  th the names of 
42f0: 61 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75  all the individu
4300: 61 6c 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20  al journals.    
4310: 61 6e 64 20 66 6c 75 73 68 20 69 74 73 20 63 6f  and flush its co
4320: 6e 74 65 6e 74 73 20 74 6f 20 64 69 73 6b 2e 0a  ntents to disk..
4330: 3c 6c 69 3e 57 72 69 74 65 20 74 68 65 20 6e 61  <li>Write the na
4340: 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  me of the master
4350: 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 0a 20 20   journal into.  
4360: 20 20 61 6c 6c 20 69 6e 64 69 76 69 64 75 61 6c    all individual
4370: 20 6a 6f 75 72 6e 61 6c 73 20 28 69 6e 20 73 70   journals (in sp
4380: 61 63 65 20 73 65 74 20 61 73 69 64 65 20 66 6f  ace set aside fo
4390: 72 20 74 68 61 74 20 70 75 72 70 6f 73 65 20 69  r that purpose i
43a0: 6e 20 74 68 65 0a 20 20 20 20 68 65 61 64 65 72  n the.    header
43b0: 73 20 6f 66 20 74 68 65 20 69 6e 64 69 76 69 64  s of the individ
43c0: 75 61 6c 20 6a 6f 75 72 6e 61 6c 73 29 20 61 6e  ual journals) an
43d0: 64 20 66 6c 75 73 68 20 74 68 65 20 63 6f 6e 74  d flush the cont
43e0: 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20  ents of the.    
43f0: 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e  individual journ
4400: 61 6c 73 20 74 6f 20 64 69 73 6b 20 61 6e 64 20  als to disk and 
4410: 77 61 69 74 20 66 6f 72 20 74 68 6f 73 65 20 63  wait for those c
4420: 68 61 6e 67 65 73 20 74 6f 20 72 65 61 63 68 20  hanges to reach 
4430: 74 68 65 0a 20 20 20 20 64 69 73 6b 20 73 75 72  the.    disk sur
4440: 66 61 63 65 2e 0a 3c 6c 69 3e 46 6c 75 73 68 20  face..<li>Flush 
4450: 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
4460: 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65  e changes to the
4470: 20 64 69 73 6b 2e 20 20 57 61 69 74 20 66 6f 72   disk.  Wait for
4480: 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 0a 20   those changes. 
4490: 20 20 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 62     to actually b
44a0: 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74  e written onto t
44b0: 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 2e  he disk surface.
44c0: 3c 2f 6c 69 3e 0a 3c 6c 69 3e 44 65 6c 65 74 65  </li>.<li>Delete
44d0: 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
44e0: 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68 69 73 20  nal file.  This 
44f0: 69 73 20 74 68 65 20 69 6e 73 74 61 6e 74 20 77  is the instant w
4500: 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 73 20  hen the changes 
4510: 61 72 65 0a 20 20 20 20 63 6f 6d 6d 69 74 74 65  are.    committe
4520: 64 2e 20 20 50 72 69 6f 72 20 74 6f 20 64 65 6c  d.  Prior to del
4530: 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72  eting the master
4540: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 69   journal file, i
4550: 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
4560: 65 0a 20 20 20 20 6f 72 20 63 72 61 73 68 20 6f  e.    or crash o
4570: 63 63 75 72 73 2c 20 74 68 65 20 69 6e 64 69 76  ccurs, the indiv
4580: 69 64 75 61 6c 20 66 69 6c 65 20 6a 6f 75 72 6e  idual file journ
4590: 61 6c 73 20 77 69 6c 6c 20 62 65 20 63 6f 6e 73  als will be cons
45a0: 69 64 65 72 65 64 20 68 6f 74 0a 20 20 20 20 61  idered hot.    a
45b0: 6e 64 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65  nd will be rolle
45c0: 64 20 62 61 63 6b 20 62 79 20 74 68 65 20 6e 65  d back by the ne
45d0: 78 74 20 70 72 6f 63 65 73 73 20 74 68 61 74 0a  xt process that.
45e0: 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f 20      attempts to 
45f0: 72 65 61 64 20 74 68 65 6d 2e 20 20 41 66 74 65  read them.  Afte
4600: 72 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  r the master jou
4610: 72 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 64 65  rnal has been de
4620: 6c 65 74 65 64 2c 0a 20 20 20 20 74 68 65 20 66  leted,.    the f
4630: 69 6c 65 20 6a 6f 75 72 6e 61 6c 73 20 77 69 6c  ile journals wil
4640: 6c 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63  l no longer be c
4650: 6f 6e 73 69 64 65 72 65 64 20 68 6f 74 20 61 6e  onsidered hot an
4660: 64 20 74 68 65 20 63 68 61 6e 67 65 73 0a 20 20  d the changes.  
4670: 20 20 77 69 6c 6c 20 70 65 72 73 69 73 74 2e 0a    will persist..
4680: 20 20 20 20 3c 2f 6c 69 3e 0a 3c 6c 69 3e 44 65      </li>.<li>De
4690: 6c 65 74 65 20 61 6c 6c 20 69 6e 64 69 76 69 64  lete all individ
46a0: 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ual journal file
46b0: 73 2e 0a 3c 6c 69 3e 44 72 6f 70 20 74 68 65 20  s..<li>Drop the 
46c0: 45 58 43 4c 55 53 49 56 45 20 61 6e 64 20 50 45  EXCLUSIVE and PE
46d0: 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 66 72 6f 6d  NDING locks from
46e0: 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69   all database fi
46f0: 6c 65 73 2e 0a 20 20 20 20 3c 2f 6c 69 3e 0a 3c  les..    </li>.<
4700: 2f 6f 6c 3e 0a 0a 3c 74 63 6c 3e 48 45 41 44 49  /ol>..<tcl>HEADI
4710: 4e 47 20 32 20 7b 57 72 69 74 65 72 20 73 74 61  NG 2 {Writer sta
4720: 72 76 61 74 69 6f 6e 7d 20 77 72 69 74 65 72 5f  rvation} writer_
4730: 73 74 61 72 76 61 74 69 6f 6e 20 7b 77 72 69 74  starvation {writ
4740: 65 72 20 73 74 61 72 76 61 74 69 6f 6e 7d 3c 2f  er starvation}</
4750: 74 63 6c 3e 0a 0a 3c 70 3e 49 6e 20 53 51 4c 69  tcl>..<p>In SQLi
4760: 74 65 20 76 65 72 73 69 6f 6e 20 32 2c 20 69 66  te version 2, if
4770: 20 6d 61 6e 79 20 70 72 6f 63 65 73 73 65 73 20   many processes 
4780: 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d  are reading from
4790: 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 69   the database,.i
47a0: 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
47b0: 61 73 65 20 74 68 61 74 20 74 68 65 72 65 20 69  ase that there i
47c0: 73 20 6e 65 76 65 72 20 61 20 74 69 6d 65 20 77  s never a time w
47d0: 68 65 6e 20 74 68 65 72 65 20 61 72 65 0a 6e 6f  hen there are.no
47e0: 20 61 63 74 69 76 65 20 72 65 61 64 65 72 73 2e   active readers.
47f0: 20 20 41 6e 64 20 69 66 20 74 68 65 72 65 20 69    And if there i
4800: 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73  s always at leas
4810: 74 20 6f 6e 65 20 72 65 61 64 20 6c 6f 63 6b 20  t one read lock 
4820: 6f 6e 20 74 68 65 0a 64 61 74 61 62 61 73 65 2c  on the.database,
4830: 20 6e 6f 20 70 72 6f 63 65 73 73 20 77 6f 75 6c   no process woul
4840: 64 20 65 76 65 72 20 62 65 20 61 62 6c 65 20 74  d ever be able t
4850: 6f 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74  o make changes t
4860: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 62  o the database.b
4870: 65 63 61 75 73 65 20 69 74 20 77 6f 75 6c 64 20  ecause it would 
4880: 62 65 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f  be impossible to
4890: 20 61 63 71 75 69 72 65 20 61 20 77 72 69 74 65   acquire a write
48a0: 20 6c 6f 63 6b 2e 20 20 54 68 69 73 20 73 69 74   lock.  This sit
48b0: 75 61 74 69 6f 6e 0a 69 73 20 63 61 6c 6c 65 64  uation.is called
48c0: 20 3c 65 6d 3e 77 72 69 74 65 72 20 73 74 61 72   <em>writer star
48d0: 76 61 74 69 6f 6e 3c 2f 65 6d 3e 2e 3c 2f 70 3e  vation</em>.</p>
48e0: 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 76 65 72 73  ..<p>SQLite vers
48f0: 69 6f 6e 20 33 20 73 65 65 6b 73 20 74 6f 20 61  ion 3 seeks to a
4900: 76 6f 69 64 20 77 72 69 74 65 72 20 73 74 61 72  void writer star
4910: 76 61 74 69 6f 6e 20 74 68 72 6f 75 67 68 20 74  vation through t
4920: 68 65 20 75 73 65 20 6f 66 0a 74 68 65 20 50 45  he use of.the PE
4930: 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 20 54 68 65  NDING lock.  The
4940: 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 6c   PENDING lock al
4950: 6c 6f 77 73 20 65 78 69 73 74 69 6e 67 20 72 65  lows existing re
4960: 61 64 65 72 73 20 74 6f 20 63 6f 6e 74 69 6e 75  aders to continu
4970: 65 0a 62 75 74 20 70 72 65 76 65 6e 74 73 20 6e  e.but prevents n
4980: 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20  ew readers from 
4990: 63 6f 6e 6e 65 63 74 69 6e 67 20 74 6f 20 74 68  connecting to th
49a0: 65 20 64 61 74 61 62 61 73 65 2e 20 20 53 6f 20  e database.  So 
49b0: 77 68 65 6e 20 61 0a 70 72 6f 63 65 73 73 20 77  when a.process w
49c0: 61 6e 74 73 20 74 6f 20 77 72 69 74 65 20 61 20  ants to write a 
49d0: 62 75 73 79 20 64 61 74 61 62 61 73 65 2c 20 69  busy database, i
49e0: 74 20 63 61 6e 20 73 65 74 20 61 20 50 45 4e 44  t can set a PEND
49f0: 49 4e 47 20 6c 6f 63 6b 20 77 68 69 63 68 0a 77  ING lock which.w
4a00: 69 6c 6c 20 70 72 65 76 65 6e 74 20 6e 65 77 20  ill prevent new 
4a10: 72 65 61 64 65 72 73 20 66 72 6f 6d 20 63 6f 6d  readers from com
4a20: 69 6e 67 20 69 6e 2e 20 20 41 73 73 75 6d 69 6e  ing in.  Assumin
4a30: 67 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65  g existing reade
4a40: 72 73 20 64 6f 0a 65 76 65 6e 74 75 61 6c 6c 79  rs do.eventually
4a50: 20 63 6f 6d 70 6c 65 74 65 2c 20 61 6c 6c 20 53   complete, all S
4a60: 48 41 52 45 44 20 6c 6f 63 6b 73 20 77 69 6c 6c  HARED locks will
4a70: 20 65 76 65 6e 74 75 61 6c 6c 79 20 63 6c 65 61   eventually clea
4a80: 72 20 61 6e 64 20 74 68 65 0a 77 72 69 74 65 72  r and the.writer
4a90: 20 77 69 6c 6c 20 62 65 20 67 69 76 65 6e 20 61   will be given a
4aa0: 20 63 68 61 6e 63 65 20 74 6f 20 6d 61 6b 65 20   chance to make 
4ab0: 69 74 73 20 63 68 61 6e 67 65 73 2e 3c 2f 70 3e  its changes.</p>
4ac0: 0a 0a 3c 74 63 6c 3e 48 45 41 44 49 4e 47 20 31  ..<tcl>HEADING 1
4ad0: 20 7b 48 6f 77 20 54 6f 20 43 6f 72 72 75 70 74   {How To Corrupt
4ae0: 20 59 6f 75 72 20 44 61 74 61 62 61 73 65 20 46   Your Database F
4af0: 69 6c 65 73 7d 20 68 6f 77 5f 74 6f 5f 63 6f 72  iles} how_to_cor
4b00: 72 75 70 74 20 5c 0a 7b 48 6f 77 20 54 6f 20 43  rupt \.{How To C
4b10: 6f 72 72 75 70 74 20 59 6f 75 72 20 44 61 74 61  orrupt Your Data
4b20: 62 61 73 65 20 46 69 6c 65 73 7d 3c 2f 74 63 6c  base Files}</tcl
4b30: 3e 0a 0a 3c 70 3e 54 68 65 20 70 61 67 65 72 20  >..<p>The pager 
4b40: 6d 6f 64 75 6c 65 20 69 73 20 76 65 72 79 20 72  module is very r
4b50: 6f 62 75 73 74 20 62 75 74 20 69 74 20 63 61 6e  obust but it can
4b60: 20 62 65 20 73 75 62 76 65 72 74 65 64 2e 20 20   be subverted.  
4b70: 0a 54 68 69 73 20 73 65 63 74 69 6f 6e 20 61 74  .This section at
4b80: 74 65 6d 70 74 73 20 74 6f 20 69 64 65 6e 74 69  tempts to identi
4b90: 66 79 20 61 6e 64 20 65 78 70 6c 61 69 6e 20 74  fy and explain t
4ba0: 68 65 20 72 69 73 6b 73 2e 0a 28 53 65 65 20 61  he risks..(See a
4bb0: 6c 73 6f 20 74 68 65 20 5b 54 68 69 6e 67 73 20  lso the [Things 
4bc0: 54 68 61 74 20 43 61 6e 20 47 6f 20 57 72 6f 6e  That Can Go Wron
4bd0: 67 5d 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68  g] section of th
4be0: 65 20 61 72 74 69 63 6c 65 0a 6f 6e 20 5b 41 74  e article.on [At
4bf0: 6f 6d 69 63 20 43 6f 6d 6d 69 74 5d 2e 3c 2f 70  omic Commit].</p
4c00: 3e 0a 0a 3c 70 3e 0a 43 6c 65 61 72 6c 79 2c 20  >..<p>.Clearly, 
4c10: 61 20 68 61 72 64 77 61 72 65 20 6f 72 20 6f 70  a hardware or op
4c20: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 66  erating system f
4c30: 61 75 6c 74 20 74 68 61 74 20 69 6e 74 72 6f 64  ault that introd
4c40: 75 63 65 73 20 69 6e 63 6f 72 72 65 63 74 20 64  uces incorrect d
4c50: 61 74 61 0a 69 6e 74 6f 20 74 68 65 20 6d 69 64  ata.into the mid
4c60: 64 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  dle of the datab
4c70: 61 73 65 20 66 69 6c 65 20 6f 72 20 6a 6f 75 72  ase file or jour
4c80: 6e 61 6c 20 77 69 6c 6c 20 63 61 75 73 65 20 70  nal will cause p
4c90: 72 6f 62 6c 65 6d 73 2e 0a 4c 69 6b 65 77 69 73  roblems..Likewis
4ca0: 65 2c 20 0a 69 66 20 61 20 72 6f 67 75 65 20 70  e, .if a rogue p
4cb0: 72 6f 63 65 73 73 20 6f 70 65 6e 73 20 61 20 64  rocess opens a d
4cc0: 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 72 20  atabase file or 
4cd0: 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 77 72 69 74  journal and writ
4ce0: 65 73 20 6d 61 6c 66 6f 72 6d 65 64 0a 64 61 74  es malformed.dat
4cf0: 61 20 69 6e 74 6f 20 74 68 65 20 6d 69 64 64 6c  a into the middl
4d00: 65 20 6f 66 20 69 74 2c 20 74 68 65 6e 20 74 68  e of it, then th
4d10: 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20  e database will 
4d20: 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a  become corrupt..
4d30: 54 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75 63  There is not muc
4d40: 68 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 6f  h that can be do
4d50: 6e 65 20 61 62 6f 75 74 20 74 68 65 73 65 20 6b  ne about these k
4d60: 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
4d70: 0a 73 6f 20 74 68 65 79 20 61 72 65 20 67 69 76  .so they are giv
4d80: 65 6e 20 6e 6f 20 66 75 72 74 68 65 72 20 61 74  en no further at
4d90: 74 65 6e 74 69 6f 6e 2e 0a 3c 2f 70 3e 0a 0a 3c  tention..</p>..<
4da0: 70 3e 0a 53 51 4c 69 74 65 20 75 73 65 73 20 50  p>.SQLite uses P
4db0: 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f  OSIX advisory lo
4dc0: 63 6b 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  cks to implement
4dd0: 20 6c 6f 63 6b 69 6e 67 20 6f 6e 20 55 6e 69 78   locking on Unix
4de0: 2e 20 20 4f 6e 0a 57 69 6e 64 6f 77 73 20 69 74  .  On.Windows it
4df0: 20 75 73 65 73 20 74 68 65 20 4c 6f 63 6b 46 69   uses the LockFi
4e00: 6c 65 28 29 2c 20 4c 6f 63 6b 46 69 6c 65 45 78  le(), LockFileEx
4e10: 28 29 2c 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69  (), and UnlockFi
4e20: 6c 65 28 29 20 73 79 73 74 65 6d 0a 63 61 6c 6c  le() system.call
4e30: 73 2e 20 20 53 51 4c 69 74 65 20 61 73 73 75 6d  s.  SQLite assum
4e40: 65 73 20 74 68 61 74 20 74 68 65 73 65 20 73 79  es that these sy
4e50: 73 74 65 6d 20 63 61 6c 6c 73 20 61 6c 6c 20 77  stem calls all w
4e60: 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65  ork as advertise
4e70: 64 2e 20 20 49 66 0a 74 68 61 74 20 69 73 20 6e  d.  If.that is n
4e80: 6f 74 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  ot the case, the
4e90: 6e 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  n database corru
4ea0: 70 74 69 6f 6e 20 63 61 6e 20 72 65 73 75 6c 74  ption can result
4eb0: 2e 20 20 4f 6e 65 20 73 68 6f 75 6c 64 0a 6e 6f  .  One should.no
4ec0: 74 65 20 74 68 61 74 20 50 4f 53 49 58 20 61 64  te that POSIX ad
4ed0: 76 69 73 6f 72 79 20 6c 6f 63 6b 69 6e 67 20 69  visory locking i
4ee0: 73 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 62 75  s known to be bu
4ef0: 67 67 79 20 6f 72 20 65 76 65 6e 20 75 6e 69 6d  ggy or even unim
4f00: 70 6c 65 6d 65 6e 74 65 64 0a 6f 6e 20 6d 61 6e  plemented.on man
4f10: 79 20 4e 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  y NFS implementa
4f20: 74 69 6f 6e 73 20 28 69 6e 63 6c 75 64 69 6e 67  tions (including
4f30: 20 72 65 63 65 6e 74 20 76 65 72 73 69 6f 6e 73   recent versions
4f40: 20 6f 66 20 4d 61 63 20 4f 53 20 58 29 0a 61 6e   of Mac OS X).an
4f50: 64 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  d that there are
4f60: 20 72 65 70 6f 72 74 73 20 6f 66 20 6c 6f 63 6b   reports of lock
4f70: 69 6e 67 20 70 72 6f 62 6c 65 6d 73 0a 66 6f 72  ing problems.for
4f80: 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73   network filesys
4f90: 74 65 6d 73 20 75 6e 64 65 72 20 57 69 6e 64 6f  tems under Windo
4fa0: 77 73 2e 20 20 59 6f 75 72 20 62 65 73 74 20 64  ws.  Your best d
4fb0: 65 66 65 6e 73 65 20 69 73 20 74 6f 20 6e 6f 74  efense is to not
4fc0: 0a 75 73 65 20 53 51 4c 69 74 65 20 66 6f 72 20  .use SQLite for 
4fd0: 66 69 6c 65 73 20 6f 6e 20 61 20 6e 65 74 77 6f  files on a netwo
4fe0: 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 2e 0a 3c  rk filesystem..<
4ff0: 2f 70 3e 0a 0a 3c 70 3e 0a 53 51 4c 69 74 65 20  /p>..<p>.SQLite 
5000: 75 73 65 73 20 74 68 65 20 66 73 79 6e 63 28 29  uses the fsync()
5010: 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f 20   system call to 
5020: 66 6c 75 73 68 20 64 61 74 61 20 74 6f 20 74 68  flush data to th
5030: 65 20 64 69 73 6b 20 75 6e 64 65 72 20 55 6e 69  e disk under Uni
5040: 78 20 61 6e 64 0a 69 74 20 75 73 65 73 20 74 68  x and.it uses th
5050: 65 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65  e FlushFileBuffe
5060: 72 73 28 29 20 74 6f 20 64 6f 20 74 68 65 20 73  rs() to do the s
5070: 61 6d 65 20 75 6e 64 65 72 20 57 69 6e 64 6f 77  ame under Window
5080: 73 2e 20 20 4f 6e 63 65 20 61 67 61 69 6e 2c 0a  s.  Once again,.
5090: 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20 74  SQLite assumes t
50a0: 68 61 74 20 74 68 65 73 65 20 6f 70 65 72 61 74  hat these operat
50b0: 69 6e 67 20 73 79 73 74 65 6d 20 73 65 72 76 69  ing system servi
50c0: 63 65 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 20  ces function as 
50d0: 61 64 76 65 72 74 69 73 65 64 2e 0a 42 75 74 20  advertised..But 
50e0: 69 74 20 68 61 73 20 62 65 65 6e 20 72 65 70 6f  it has been repo
50f0: 72 74 65 64 20 74 68 61 74 20 66 73 79 6e 63 28  rted that fsync(
5100: 29 20 61 6e 64 20 46 6c 75 73 68 46 69 6c 65 42  ) and FlushFileB
5110: 75 66 66 65 72 73 28 29 20 64 6f 20 6e 6f 74 20  uffers() do not 
5120: 61 6c 77 61 79 73 0a 77 6f 72 6b 20 63 6f 72 72  always.work corr
5130: 65 63 74 6c 79 2c 20 65 73 70 65 63 69 61 6c 6c  ectly, especiall
5140: 79 20 77 69 74 68 20 69 6e 65 78 70 65 6e 73 69  y with inexpensi
5150: 76 65 20 49 44 45 20 64 69 73 6b 73 2e 20 20 41  ve IDE disks.  A
5160: 70 70 61 72 65 6e 74 6c 79 20 73 6f 6d 65 0a 6d  pparently some.m
5170: 61 6e 75 66 61 63 74 75 72 65 73 20 6f 66 20 49  anufactures of I
5180: 44 45 20 64 69 73 6b 73 20 68 61 76 65 20 63 6f  DE disks have co
5190: 6e 74 72 6f 6c 6c 65 72 20 63 68 69 70 73 20 74  ntroller chips t
51a0: 68 61 74 20 72 65 70 6f 72 74 0a 74 68 61 74 20  hat report.that 
51b0: 64 61 74 61 20 68 61 73 20 72 65 61 63 68 65 64  data has reached
51c0: 20 74 68 65 20 64 69 73 6b 20 73 75 72 66 61 63   the disk surfac
51d0: 65 20 77 68 65 6e 20 69 6e 20 66 61 63 74 20 74  e when in fact t
51e0: 68 65 20 64 61 74 61 20 69 73 20 73 74 69 6c 6c  he data is still
51f0: 0a 69 6e 20 76 6f 6c 61 74 69 6c 65 20 63 61 63  .in volatile cac
5200: 68 65 20 6d 65 6d 6f 72 79 20 69 6e 20 74 68 65  he memory in the
5210: 20 64 69 73 6b 20 64 72 69 76 65 20 65 6c 65 63   disk drive elec
5220: 74 72 6f 6e 69 63 73 2e 20 20 54 68 65 72 65 20  tronics.  There 
5230: 61 72 65 20 61 6c 73 6f 0a 72 65 70 6f 72 74 73  are also.reports
5240: 20 74 68 61 74 20 57 69 6e 64 6f 77 73 20 73 6f   that Windows so
5250: 6d 65 74 69 6d 65 73 20 63 68 6f 6f 73 65 73 20  metimes chooses 
5260: 74 6f 20 69 67 6e 6f 72 65 20 46 6c 75 73 68 46  to ignore FlushF
5270: 69 6c 65 42 75 66 66 65 72 73 28 29 20 66 6f 72  ileBuffers() for
5280: 0a 75 6e 73 70 65 63 69 66 69 65 64 20 72 65 61  .unspecified rea
5290: 73 6f 6e 73 2e 20 20 54 68 65 20 61 75 74 68 6f  sons.  The autho
52a0: 72 20 63 61 6e 6e 6f 74 20 76 65 72 69 66 79 20  r cannot verify 
52b0: 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 65 70  any of these rep
52c0: 6f 72 74 73 2e 0a 42 75 74 20 69 66 20 74 68 65  orts..But if the
52d0: 79 20 61 72 65 20 74 72 75 65 2c 20 69 74 20 6d  y are true, it m
52e0: 65 61 6e 73 20 74 68 61 74 20 64 61 74 61 62 61  eans that databa
52f0: 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73  se corruption is
5300: 20 61 20 70 6f 73 73 69 62 69 6c 69 74 79 0a 66   a possibility.f
5310: 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 75 6e 65 78  ollowing an unex
5320: 70 65 63 74 65 64 20 70 6f 77 65 72 20 6c 6f 73  pected power los
5330: 73 2e 20 20 54 68 65 73 65 20 61 72 65 20 68 61  s.  These are ha
5340: 72 64 77 61 72 65 20 61 6e 64 2f 6f 72 20 6f 70  rdware and/or op
5350: 65 72 61 74 69 6e 67 0a 73 79 73 74 65 6d 20 62  erating.system b
5360: 75 67 73 20 74 68 61 74 20 53 51 4c 69 74 65 20  ugs that SQLite 
5370: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 66  is unable to def
5380: 65 6e 64 20 61 67 61 69 6e 73 74 2e 0a 3c 2f 70  end against..</p
5390: 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d  >..<tcl>hd_fragm
53a0: 65 6e 74 20 7b 65 78 74 33 2d 62 61 72 72 69 65  ent {ext3-barrie
53b0: 72 2d 70 72 6f 62 6c 65 6d 7d 20 7b 74 68 65 20  r-problem} {the 
53c0: 65 78 74 33 20 62 61 72 72 69 65 72 20 70 72 6f  ext3 barrier pro
53d0: 62 6c 65 6d 7d 3c 2f 74 63 6c 3e 0a 3c 70 3e 49  blem}</tcl>.<p>I
53e0: 66 20 61 20 4c 69 6e 75 78 20 3c 61 20 68 72 65  f a Linux <a hre
53f0: 66 3d 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69 6b  f="http://en.wik
5400: 69 70 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69 2f  ipedia.org/wiki/
5410: 45 78 74 33 22 3e 65 78 74 33 3c 2f 61 3e 0a 66  Ext3">ext3</a>.f
5420: 69 6c 65 73 79 73 74 65 6d 20 69 73 20 6d 6f 75  ilesystem is mou
5430: 6e 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65  nted without the
5440: 20 22 62 61 72 72 69 65 72 3d 31 22 20 6f 70 74   "barrier=1" opt
5450: 69 6f 6e 0a 69 6e 20 74 68 65 20 3c 61 20 68 72  ion.in the <a hr
5460: 65 66 3d 22 68 74 74 70 3a 2f 2f 65 6e 2e 77 69  ef="http://en.wi
5470: 6b 69 70 65 64 69 61 2e 6f 72 67 2f 77 69 6b 69  kipedia.org/wiki
5480: 2f 66 73 74 61 62 22 3e 2f 65 74 63 2f 66 73 74  /fstab">/etc/fst
5490: 61 62 3c 2f 61 3e 0a 61 6e 64 20 74 68 65 20 64  ab</a>.and the d
54a0: 69 73 6b 20 64 72 69 76 65 20 77 72 69 74 65 20  isk drive write 
54b0: 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
54c0: 0a 74 68 65 6e 20 66 69 6c 65 73 79 73 74 65 6d  .then filesystem
54d0: 20 63 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20   corruption can 
54e0: 6f 63 63 75 72 20 66 6f 6c 6c 6f 77 69 6e 67 20  occur following 
54f0: 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 72 20  a power loss or 
5500: 4f 53 20 63 72 61 73 68 2e 0a 57 68 65 74 68 65  OS crash..Whethe
5510: 72 20 6f 72 20 6e 6f 74 20 63 6f 72 72 75 70 74  r or not corrupt
5520: 69 6f 6e 20 63 61 6e 20 6f 63 63 75 72 20 64 65  ion can occur de
5530: 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 64 65 74  pends on the det
5540: 61 69 6c 73 20 6f 66 20 74 68 65 20 64 69 73 6b  ails of the disk
5550: 20 63 6f 6e 74 72 6f 6c 0a 68 61 72 64 77 61 72   control.hardwar
5560: 65 3b 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73  e; corruption is
5570: 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 77 69 74   more likely wit
5580: 68 20 69 6e 65 78 70 65 6e 73 69 76 65 20 63 6f  h inexpensive co
5590: 6e 73 75 6d 65 72 2d 67 72 61 64 65 20 64 69 73  nsumer-grade dis
55a0: 6b 73 0a 61 6e 64 20 6c 65 73 73 20 6f 66 20 61  ks.and less of a
55b0: 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 65 6e 74   problem for ent
55c0: 65 72 70 72 69 73 65 2d 63 6c 61 73 73 20 73 74  erprise-class st
55d0: 6f 72 61 67 65 20 64 65 76 69 63 65 73 20 77 69  orage devices wi
55e0: 74 68 20 61 64 76 61 6e 63 65 64 0a 66 65 61 74  th advanced.feat
55f0: 75 72 65 73 20 73 75 63 68 20 61 73 20 6e 6f 6e  ures such as non
5600: 2d 76 6f 6c 61 74 69 6c 65 20 77 72 69 74 65 20  -volatile write 
5610: 63 61 63 68 65 73 2e 0a 56 61 72 69 6f 75 73 20  caches..Various 
5620: 65 78 74 33 20 65 78 70 65 72 74 73 0a 3c 61 20  ext3 experts.<a 
5630: 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77  href="http://www
5640: 2e 72 65 64 68 61 74 2e 63 6f 6d 2f 61 72 63 68  .redhat.com/arch
5650: 69 76 65 73 2f 65 78 74 33 2d 75 73 65 72 73 2f  ives/ext3-users/
5660: 32 30 31 30 2d 4a 75 6c 79 2f 6d 73 67 30 30 30  2010-July/msg000
5670: 30 31 2e 68 74 6d 6c 22 3e 0a 63 6f 6e 66 69 72  01.html">.confir
5680: 6d 20 74 68 69 73 20 62 65 68 61 76 69 6f 72 3c  m this behavior<
5690: 2f 61 3e 2e 0a 57 65 20 61 72 65 20 74 6f 6c 64  /a>..We are told
56a0: 20 74 68 61 74 20 6d 6f 73 74 20 4c 69 6e 75 78   that most Linux
56b0: 20 64 69 73 74 72 69 62 75 74 69 6f 6e 73 20 64   distributions d
56c0: 6f 20 6e 6f 74 20 75 73 65 20 62 61 72 72 69 65  o not use barrie
56d0: 72 3d 31 20 61 6e 64 20 64 6f 0a 6e 6f 74 20 64  r=1 and do.not d
56e0: 69 73 61 62 6c 65 20 74 68 65 20 77 72 69 74 65  isable the write
56f0: 20 63 61 63 68 65 20 73 6f 20 6d 6f 73 74 0a 4c   cache so most.L
5700: 69 6e 75 78 20 64 69 73 74 72 69 62 75 74 69 6f  inux distributio
5710: 6e 73 20 61 72 65 20 76 75 6c 6e 65 72 61 62 6c  ns are vulnerabl
5720: 65 20 74 6f 20 74 68 69 73 20 70 72 6f 62 6c 65  e to this proble
5730: 6d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  m.  Note that th
5740: 69 73 20 69 73 20 61 6e 0a 6f 70 65 72 61 74 69  is is an.operati
5750: 6e 67 20 73 79 73 74 65 6d 20 61 6e 64 20 68 61  ng system and ha
5760: 72 64 77 61 72 65 20 69 73 73 75 65 20 61 6e 64  rdware issue and
5770: 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
5780: 6f 74 68 69 6e 67 20 74 68 61 74 20 53 51 4c 69  othing that SQLi
5790: 74 65 0a 63 61 6e 20 64 6f 20 74 6f 20 77 6f 72  te.can do to wor
57a0: 6b 20 61 72 6f 75 6e 64 20 69 74 2e 20 20 0a 3c  k around it.  .<
57b0: 61 20 68 72 65 66 3d 22 68 74 74 70 3a 2f 2f 6f  a href="http://o
57c0: 7a 6c 61 62 73 2e 6f 72 67 2f 7e 72 75 73 74 79  zlabs.org/~rusty
57d0: 2f 69 6e 64 65 78 2e 63 67 69 2f 74 65 63 68 2f  /index.cgi/tech/
57e0: 32 30 30 39 2d 31 30 2d 32 30 2e 68 74 6d 6c 22  2009-10-20.html"
57f0: 3e 0a 4f 74 68 65 72 20 64 61 74 61 62 61 73 65  >.Other database
5800: 20 65 6e 67 69 6e 65 73 3c 2f 61 3e 20 68 61 76   engines</a> hav
5810: 65 20 61 6c 73 6f 20 72 75 6e 20 69 6e 74 6f 20  e also run into 
5820: 74 68 69 73 20 73 61 6d 65 20 70 72 6f 62 6c 65  this same proble
5830: 6d 2e 3c 2f 70 3e 0a 0a 3c 70 3e 0a 49 66 20 61  m.</p>..<p>.If a
5840: 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20   crash or power 
5850: 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61  failure occurs a
5860: 6e 64 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20  nd results in a 
5870: 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 62 75 74 20  hot journal but 
5880: 74 68 61 74 0a 6a 6f 75 72 6e 61 6c 20 69 73 20  that.journal is 
5890: 64 65 6c 65 74 65 64 2c 20 74 68 65 20 6e 65 78  deleted, the nex
58a0: 74 20 70 72 6f 63 65 73 73 20 74 6f 20 6f 70 65  t process to ope
58b0: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  n the database w
58c0: 69 6c 6c 20 6e 6f 74 0a 6b 6e 6f 77 20 74 68 61  ill not.know tha
58d0: 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 63 68  t it contains ch
58e0: 61 6e 67 65 73 20 74 68 61 74 20 6e 65 65 64 20  anges that need 
58f0: 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  to be rolled bac
5900: 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  k.  The rollback
5910: 0a 77 69 6c 6c 20 6e 6f 74 20 6f 63 63 75 72 20  .will not occur 
5920: 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
5930: 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e   will be left in
5940: 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
5950: 20 73 74 61 74 65 2e 0a 52 6f 6c 6c 62 61 63 6b   state..Rollback
5960: 20 6a 6f 75 72 6e 61 6c 73 20 6d 69 67 68 74 20   journals might 
5970: 62 65 20 64 65 6c 65 74 65 64 20 66 6f 72 20 61  be deleted for a
5980: 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 61  ny number of rea
5990: 73 6f 6e 73 3a 0a 3c 2f 70 3e 0a 0a 3c 75 6c 3e  sons:.</p>..<ul>
59a0: 0a 3c 6c 69 3e 41 6e 20 61 64 6d 69 6e 69 73 74  .<li>An administ
59b0: 72 61 74 6f 72 20 6d 69 67 68 74 20 62 65 20 63  rator might be c
59c0: 6c 65 61 6e 69 6e 67 20 75 70 20 61 66 74 65 72  leaning up after
59d0: 20 61 6e 20 4f 53 20 63 72 61 73 68 20 6f 72 20   an OS crash or 
59e0: 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 0a 20  power failure,. 
59f0: 20 20 20 73 65 65 20 74 68 65 20 6a 6f 75 72 6e     see the journ
5a00: 61 6c 20 66 69 6c 65 2c 20 74 68 69 6e 6b 20 69  al file, think i
5a10: 74 20 69 73 20 6a 75 6e 6b 2c 20 61 6e 64 20 64  t is junk, and d
5a20: 65 6c 65 74 65 20 69 74 2e 3c 2f 6c 69 3e 0a 3c  elete it.</li>.<
5a30: 6c 69 3e 53 6f 6d 65 6f 6e 65 20 28 6f 72 20 73  li>Someone (or s
5a40: 6f 6d 65 20 70 72 6f 63 65 73 73 29 20 6d 69 67  ome process) mig
5a50: 68 74 20 72 65 6e 61 6d 65 20 74 68 65 20 64 61  ht rename the da
5a60: 74 61 62 61 73 65 20 66 69 6c 65 20 62 75 74 20  tabase file but 
5a70: 66 61 69 6c 20 74 6f 0a 20 20 20 20 61 6c 73 6f  fail to.    also
5a80: 20 72 65 6e 61 6d 65 20 69 74 73 20 61 73 73 6f   rename its asso
5a90: 63 69 61 74 65 64 20 6a 6f 75 72 6e 61 6c 2e 3c  ciated journal.<
5aa0: 2f 6c 69 3e 0a 3c 6c 69 3e 49 66 20 74 68 65 20  /li>.<li>If the 
5ab0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
5ac0: 73 20 61 6c 69 61 73 65 73 20 28 68 61 72 64 20  s aliases (hard 
5ad0: 6f 72 20 73 6f 66 74 20 6c 69 6e 6b 73 29 20 61  or soft links) a
5ae0: 6e 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  nd the file.    
5af0: 69 73 20 6f 70 65 6e 65 64 20 62 79 20 61 20 64  is opened by a d
5b00: 69 66 66 65 72 65 6e 74 20 61 6c 69 61 73 20 74  ifferent alias t
5b10: 68 61 6e 20 74 68 65 20 6f 6e 65 20 75 73 65 64  han the one used
5b20: 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6a   to create the j
5b30: 6f 75 72 6e 61 6c 2c 0a 20 20 20 20 74 68 65 6e  ournal,.    then
5b40: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c   the journal wil
5b50: 6c 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 20  l not be found. 
5b60: 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73 20 70   To avoid this p
5b70: 72 6f 62 6c 65 6d 2c 20 79 6f 75 20 73 68 6f 75  roblem, you shou
5b80: 6c 64 0a 20 20 20 20 6e 6f 74 20 63 72 65 61 74  ld.    not creat
5b90: 65 20 6c 69 6e 6b 73 20 74 6f 20 53 51 4c 69 74  e links to SQLit
5ba0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  e database files
5bb0: 2e 3c 2f 6c 69 3e 0a 3c 6c 69 3e 46 69 6c 65 73  .</li>.<li>Files
5bc0: 79 73 74 65 6d 20 63 6f 72 72 75 70 74 69 6f 6e  ystem corruption
5bd0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77   following a pow
5be0: 65 72 20 66 61 69 6c 75 72 65 20 6d 69 67 68 74  er failure might
5bf0: 20 63 61 75 73 65 20 74 68 65 0a 20 20 20 20 6a   cause the.    j
5c00: 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 72 65 6e  ournal to be ren
5c10: 61 6d 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e  amed or deleted.
5c20: 3c 2f 6c 69 3e 0a 3c 2f 75 6c 3e 0a 0a 3c 70 3e  </li>.</ul>..<p>
5c30: 0a 54 68 65 20 6c 61 73 74 20 28 66 6f 75 72 74  .The last (fourt
5c40: 68 29 20 62 75 6c 6c 65 74 20 61 62 6f 76 65 20  h) bullet above 
5c50: 6d 65 72 69 74 73 20 61 64 64 69 74 69 6f 6e 61  merits additiona
5c60: 6c 20 63 6f 6d 6d 65 6e 74 2e 20 20 57 68 65 6e  l comment.  When
5c70: 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 73 0a   SQLite creates.
5c80: 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  a journal file o
5c90: 6e 20 55 6e 69 78 2c 20 69 74 20 6f 70 65 6e 73  n Unix, it opens
5ca0: 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74   the directory t
5cb0: 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 61  hat contains tha
5cc0: 74 20 66 69 6c 65 20 61 6e 64 0a 63 61 6c 6c 73  t file and.calls
5cd0: 20 66 73 79 6e 63 28 29 20 6f 6e 20 74 68 65 20   fsync() on the 
5ce0: 64 69 72 65 63 74 6f 72 79 2c 20 69 6e 20 61 6e  directory, in an
5cf0: 20 65 66 66 6f 72 74 20 74 6f 20 70 75 73 68 20   effort to push 
5d00: 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69 6e  the directory in
5d10: 66 6f 72 6d 61 74 69 6f 6e 0a 74 6f 20 64 69 73  formation.to dis
5d20: 6b 2e 20 20 42 75 74 20 73 75 70 70 6f 73 65 20  k.  But suppose 
5d30: 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
5d40: 73 73 20 69 73 20 61 64 64 69 6e 67 20 6f 72 20  ss is adding or 
5d50: 72 65 6d 6f 76 69 6e 67 20 75 6e 72 65 6c 61 74  removing unrelat
5d60: 65 64 0a 66 69 6c 65 73 20 74 6f 20 74 68 65 20  ed.files to the 
5d70: 64 69 72 65 63 74 6f 72 79 20 74 68 61 74 20 63  directory that c
5d80: 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 61  ontains the data
5d90: 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e 61 6c  base and journal
5da0: 20 61 74 20 74 68 65 0a 6d 6f 6d 65 6e 74 20 6f   at the.moment o
5db0: 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
5dc0: 65 2e 20 20 54 68 65 20 73 75 70 70 6f 73 65 64  e.  The supposed
5dd0: 6c 79 20 75 6e 72 65 6c 61 74 65 64 20 61 63 74  ly unrelated act
5de0: 69 6f 6e 73 20 6f 66 20 74 68 69 73 20 6f 74 68  ions of this oth
5df0: 65 72 0a 70 72 6f 63 65 73 73 20 6d 69 67 68 74  er.process might
5e00: 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 6a   result in the j
5e10: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 65 69 6e  ournal file bein
5e20: 67 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20 74  g dropped from t
5e30: 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e 64  he directory and
5e40: 0a 6d 6f 76 65 64 20 69 6e 74 6f 20 22 6c 6f 73  .moved into "los
5e50: 74 2b 66 6f 75 6e 64 22 2e 20 20 54 68 69 73 20  t+found".  This 
5e60: 69 73 20 61 6e 20 75 6e 6c 69 6b 65 6c 79 20 73  is an unlikely s
5e70: 63 65 6e 61 72 69 6f 2c 20 62 75 74 20 69 74 20  cenario, but it 
5e80: 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 0a 54 68  could happen..Th
5e90: 65 20 62 65 73 74 20 64 65 66 65 6e 73 65 73 20  e best defenses 
5ea0: 61 72 65 20 74 6f 20 75 73 65 20 61 20 6a 6f 75  are to use a jou
5eb0: 72 6e 61 6c 69 6e 67 20 66 69 6c 65 73 79 73 74  rnaling filesyst
5ec0: 65 6d 20 6f 72 20 74 6f 20 6b 65 65 70 20 74 68  em or to keep th
5ed0: 65 0a 64 61 74 61 62 61 73 65 20 61 6e 64 20 6a  e.database and j
5ee0: 6f 75 72 6e 61 6c 20 69 6e 20 61 20 64 69 72 65  ournal in a dire
5ef0: 63 74 6f 72 79 20 62 79 20 74 68 65 6d 73 65 6c  ctory by themsel
5f00: 76 65 73 2e 0a 3c 2f 70 3e 0a 0a 3c 70 3e 0a 46  ves..</p>..<p>.F
5f10: 6f 72 20 61 20 63 6f 6d 6d 69 74 20 69 6e 76 6f  or a commit invo
5f20: 6c 76 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 64  lving multiple d
5f30: 61 74 61 62 61 73 65 73 20 61 6e 64 20 61 20 6d  atabases and a m
5f40: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2c 20 69  aster journal, i
5f50: 66 20 74 68 65 0a 76 61 72 69 6f 75 73 20 64 61  f the.various da
5f60: 74 61 62 61 73 65 73 20 77 65 72 65 20 6f 6e 20  tabases were on 
5f70: 64 69 66 66 65 72 65 6e 74 20 64 69 73 6b 20 76  different disk v
5f80: 6f 6c 75 6d 65 73 20 61 6e 64 20 61 20 70 6f 77  olumes and a pow
5f90: 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  er failure occur
5fa0: 73 0a 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d  s.during the com
5fb0: 6d 69 74 2c 20 74 68 65 6e 20 77 68 65 6e 20 74  mit, then when t
5fc0: 68 65 20 6d 61 63 68 69 6e 65 20 63 6f 6d 65 73  he machine comes
5fd0: 20 62 61 63 6b 20 75 70 20 74 68 65 20 64 69 73   back up the dis
5fe0: 6b 73 20 6d 69 67 68 74 0a 62 65 20 72 65 6d 6f  ks might.be remo
5ff0: 75 6e 74 65 64 20 77 69 74 68 20 64 69 66 66 65  unted with diffe
6000: 72 65 6e 74 20 6e 61 6d 65 73 2e 20 20 4f 72 20  rent names.  Or 
6010: 73 6f 6d 65 20 64 69 73 6b 73 20 6d 69 67 68 74  some disks might
6020: 20 6e 6f 74 20 62 65 20 6d 6f 75 6e 74 65 64 0a   not be mounted.
6030: 61 74 20 61 6c 6c 2e 20 20 20 57 68 65 6e 20 74  at all.   When t
6040: 68 69 73 20 68 61 70 70 65 6e 73 20 74 68 65 20  his happens the 
6050: 69 6e 64 69 76 69 64 75 61 6c 20 66 69 6c 65 20  individual file 
6060: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 74 68 65  journals and the
6070: 20 6d 61 73 74 65 72 0a 6a 6f 75 72 6e 61 6c 20   master.journal 
6080: 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c  might not be abl
6090: 65 20 74 6f 20 66 69 6e 64 20 65 61 63 68 20 6f  e to find each o
60a0: 74 68 65 72 2e 20 54 68 65 20 77 6f 72 73 74 20  ther. The worst 
60b0: 6f 75 74 63 6f 6d 65 20 66 72 6f 6d 0a 74 68 69  outcome from.thi
60c0: 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68  s scenario is th
60d0: 61 74 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 65  at the commit ce
60e0: 61 73 65 73 20 74 6f 20 62 65 20 61 74 6f 6d 69  ases to be atomi
60f0: 63 2e 20 20 0a 53 6f 6d 65 20 64 61 74 61 62 61  c.  .Some databa
6100: 73 65 73 20 6d 69 67 68 74 20 62 65 20 72 6f 6c  ses might be rol
6110: 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 6f 74 68  led back and oth
6120: 65 72 73 20 6d 69 67 68 74 20 6e 6f 74 2e 20 0a  ers might not. .
6130: 41 6c 6c 20 64 61 74 61 62 61 73 65 73 20 77 69  All databases wi
6140: 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62  ll continue to b
6150: 65 20 73 65 6c 66 2d 63 6f 6e 73 69 73 74 65 6e  e self-consisten
6160: 74 2e 0a 54 6f 20 64 65 66 65 6e 64 20 61 67 61  t..To defend aga
6170: 69 6e 73 74 20 74 68 69 73 20 70 72 6f 62 6c 65  inst this proble
6180: 6d 2c 20 6b 65 65 70 20 61 6c 6c 20 64 61 74 61  m, keep all data
6190: 62 61 73 65 73 0a 6f 6e 20 74 68 65 20 73 61 6d  bases.on the sam
61a0: 65 20 64 69 73 6b 20 76 6f 6c 75 6d 65 20 61 6e  e disk volume an
61b0: 64 2f 6f 72 20 72 65 6d 6f 75 6e 74 20 64 69 73  d/or remount dis
61c0: 6b 73 20 75 73 69 6e 67 20 65 78 61 63 74 6c 79  ks using exactly
61d0: 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 73 0a   the same names.
61e0: 61 66 74 65 72 20 61 20 70 6f 77 65 72 20 66 61  after a power fa
61f0: 69 6c 75 72 65 2e 0a 3c 2f 70 3e 0a 0a 3c 74 63  ilure..</p>..<tc
6200: 6c 3e 48 45 41 44 49 4e 47 20 31 20 7b 54 72 61  l>HEADING 1 {Tra
6210: 6e 73 61 63 74 69 6f 6e 20 43 6f 6e 74 72 6f 6c  nsaction Control
6220: 20 41 74 20 54 68 65 20 53 51 4c 20 4c 65 76 65   At The SQL Leve
6230: 6c 7d 20 74 72 61 6e 73 61 63 74 69 6f 6e 5f 63  l} transaction_c
6240: 6f 6e 74 72 6f 6c 3c 2f 74 63 6c 3e 0a 0a 3c 70  ontrol</tcl>..<p
6250: 3e 0a 54 68 65 20 63 68 61 6e 67 65 73 20 74 6f  >.The changes to
6260: 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 63 6f 6e   locking and con
6270: 63 75 72 72 65 6e 63 79 20 63 6f 6e 74 72 6f 6c  currency control
6280: 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   in SQLite versi
6290: 6f 6e 20 33 20 61 6c 73 6f 0a 69 6e 74 72 6f 64  on 3 also.introd
62a0: 75 63 65 20 73 6f 6d 65 20 73 75 62 74 6c 65 20  uce some subtle 
62b0: 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 77  changes in the w
62c0: 61 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  ay transactions 
62d0: 77 6f 72 6b 20 61 74 20 74 68 65 20 53 51 4c 0a  work at the SQL.
62e0: 6c 61 6e 67 75 61 67 65 20 6c 65 76 65 6c 2e 0a  language level..
62f0: 42 79 20 64 65 66 61 75 6c 74 2c 20 53 51 4c 69  By default, SQLi
6300: 74 65 20 76 65 72 73 69 6f 6e 20 33 20 6f 70 65  te version 3 ope
6310: 72 61 74 65 73 20 69 6e 20 3c 65 6d 3e 61 75 74  rates in <em>aut
6320: 6f 63 6f 6d 6d 69 74 3c 2f 65 6d 3e 20 6d 6f 64  ocommit</em> mod
6330: 65 2e 0a 49 6e 20 61 75 74 6f 63 6f 6d 6d 69 74  e..In autocommit
6340: 20 6d 6f 64 65 2c 0a 61 6c 6c 20 63 68 61 6e 67   mode,.all chang
6350: 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
6360: 73 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64  se are committed
6370: 20 61 73 20 73 6f 6f 6e 20 61 73 20 61 6c 6c 20   as soon as all 
6380: 6f 70 65 72 61 74 69 6f 6e 73 20 61 73 73 6f 63  operations assoc
6390: 69 61 74 65 64 0a 77 69 74 68 20 74 68 65 20 63  iated.with the c
63a0: 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 20  urrent database 
63b0: 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6d 70 6c  connection compl
63c0: 65 74 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65  ete.</p>..<p>The
63d0: 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 22 42 45   SQL command "BE
63e0: 47 49 4e 20 54 52 41 4e 53 41 43 54 49 4f 4e 22  GIN TRANSACTION"
63f0: 20 28 74 68 65 20 54 52 41 4e 53 41 43 54 49 4f   (the TRANSACTIO
6400: 4e 20 6b 65 79 77 6f 72 64 0a 69 73 20 6f 70 74  N keyword.is opt
6410: 69 6f 6e 61 6c 29 20 69 73 20 75 73 65 64 20 74  ional) is used t
6420: 6f 20 74 61 6b 65 20 53 51 4c 69 74 65 20 6f 75  o take SQLite ou
6430: 74 20 6f 66 20 61 75 74 6f 63 6f 6d 6d 69 74 20  t of autocommit 
6440: 6d 6f 64 65 2e 0a 4e 6f 74 65 20 74 68 61 74 20  mode..Note that 
6450: 74 68 65 20 42 45 47 49 4e 20 63 6f 6d 6d 61 6e  the BEGIN comman
6460: 64 20 64 6f 65 73 20 6e 6f 74 20 61 63 71 75 69  d does not acqui
6470: 72 65 20 61 6e 79 20 6c 6f 63 6b 73 20 6f 6e 20  re any locks on 
6480: 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 41 66  the database..Af
6490: 74 65 72 20 61 20 42 45 47 49 4e 20 63 6f 6d 6d  ter a BEGIN comm
64a0: 61 6e 64 2c 20 61 20 53 48 41 52 45 44 20 6c 6f  and, a SHARED lo
64b0: 63 6b 20 77 69 6c 6c 20 62 65 20 61 63 71 75 69  ck will be acqui
64c0: 72 65 64 20 77 68 65 6e 20 74 68 65 20 66 69 72  red when the fir
64d0: 73 74 0a 53 45 4c 45 43 54 20 73 74 61 74 65 6d  st.SELECT statem
64e0: 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2e  ent is executed.
64f0: 20 20 41 20 52 45 53 45 52 56 45 44 20 6c 6f 63    A RESERVED loc
6500: 6b 20 77 69 6c 6c 20 62 65 20 61 63 71 75 69 72  k will be acquir
6510: 65 64 20 77 68 65 6e 0a 74 68 65 20 66 69 72 73  ed when.the firs
6520: 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  t INSERT, UPDATE
6530: 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  , or DELETE stat
6540: 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65  ement is execute
6550: 64 2e 20 20 4e 6f 20 45 58 43 4c 55 53 49 56 45  d.  No EXCLUSIVE
6560: 0a 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65  .lock is acquire
6570: 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 74  d until either t
6580: 68 65 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 20  he memory cache 
6590: 66 69 6c 6c 73 20 75 70 20 61 6e 64 20 6d 75 73  fills up and mus
65a0: 74 0a 62 65 20 73 70 69 6c 6c 65 64 20 74 6f 20  t.be spilled to 
65b0: 64 69 73 6b 20 6f 72 20 75 6e 74 69 6c 20 74 68  disk or until th
65c0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
65d0: 6d 6d 69 74 73 2e 20 20 49 6e 20 74 68 69 73 20  mmits.  In this 
65e0: 77 61 79 2c 0a 74 68 65 20 73 79 73 74 65 6d 20  way,.the system 
65f0: 64 65 6c 61 79 73 20 62 6c 6f 63 6b 69 6e 67 20  delays blocking 
6600: 72 65 61 64 20 61 63 63 65 73 73 20 74 6f 20 74  read access to t
6610: 68 65 20 66 69 6c 65 20 66 69 6c 65 20 75 6e 74  he file file unt
6620: 69 6c 20 74 68 65 0a 6c 61 73 74 20 70 6f 73 73  il the.last poss
6630: 69 62 6c 65 20 6d 6f 6d 65 6e 74 2e 0a 3c 2f 70  ible moment..</p
6640: 3e 0a 0a 3c 70 3e 54 68 65 20 53 51 4c 20 63 6f  >..<p>The SQL co
6650: 6d 6d 61 6e 64 20 22 43 4f 4d 4d 49 54 22 20 20  mmand "COMMIT"  
6660: 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c  does not actuall
6670: 79 20 63 6f 6d 6d 69 74 20 74 68 65 20 63 68 61  y commit the cha
6680: 6e 67 65 73 20 74 6f 0a 64 69 73 6b 2e 20 20 49  nges to.disk.  I
6690: 74 20 6a 75 73 74 20 74 75 72 6e 73 20 61 75 74  t just turns aut
66a0: 6f 63 6f 6d 6d 69 74 20 62 61 63 6b 20 6f 6e 2e  ocommit back on.
66b0: 20 20 54 68 65 6e 2c 20 61 74 20 74 68 65 20 63    Then, at the c
66c0: 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a 74 68 65  onclusion of.the
66d0: 20 63 6f 6d 6d 61 6e 64 2c 20 74 68 65 20 72 65   command, the re
66e0: 67 75 6c 61 72 20 61 75 74 6f 63 6f 6d 6d 69 74  gular autocommit
66f0: 20 6c 6f 67 69 63 20 74 61 6b 65 73 20 6f 76 65   logic takes ove
6700: 72 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65  r and causes the
6710: 0a 61 63 74 75 61 6c 20 63 6f 6d 6d 69 74 20 74  .actual commit t
6720: 6f 20 64 69 73 6b 20 74 6f 20 6f 63 63 75 72 2e  o disk to occur.
6730: 0a 54 68 65 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  .The SQL command
6740: 20 22 52 4f 4c 4c 42 41 43 4b 22 20 61 6c 73 6f   "ROLLBACK" also
6750: 20 6f 70 65 72 61 74 65 73 20 62 79 20 74 75 72   operates by tur
6760: 6e 69 6e 67 20 61 75 74 6f 63 6f 6d 6d 69 74 20  ning autocommit 
6770: 62 61 63 6b 20 6f 6e 2c 0a 62 75 74 20 69 74 20  back on,.but it 
6780: 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67  also sets a flag
6790: 20 74 68 61 74 20 74 65 6c 6c 73 20 74 68 65 20   that tells the 
67a0: 61 75 74 6f 63 6f 6d 6d 69 74 20 6c 6f 67 69 63  autocommit logic
67b0: 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 72 61 74   to rollback rat
67c0: 68 65 72 0a 74 68 61 6e 20 63 6f 6d 6d 69 74 2e  her.than commit.
67d0: 3c 2f 70 3e 0a 0a 3c 70 3e 49 66 20 74 68 65 20  </p>..<p>If the 
67e0: 53 51 4c 20 43 4f 4d 4d 49 54 20 63 6f 6d 6d 61  SQL COMMIT comma
67f0: 6e 64 20 74 75 72 6e 73 20 61 75 74 6f 63 6f 6d  nd turns autocom
6800: 6d 69 74 20 6f 6e 20 61 6e 64 20 74 68 65 20 61  mit on and the a
6810: 75 74 6f 63 6f 6d 6d 69 74 20 6c 6f 67 69 63 0a  utocommit logic.
6820: 74 68 65 6e 20 74 72 69 65 73 20 74 6f 20 63 6f  then tries to co
6830: 6d 6d 69 74 20 63 68 61 6e 67 65 20 62 75 74 20  mmit change but 
6840: 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 73 6f  fails because so
6850: 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
6860: 20 69 73 20 68 6f 6c 64 69 6e 67 0a 61 20 53 48   is holding.a SH
6870: 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20  ARED lock, then 
6880: 61 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20 74 75  autocommit is tu
6890: 72 6e 65 64 20 62 61 63 6b 20 6f 66 66 20 61 75  rned back off au
68a0: 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68  tomatically.  Th
68b0: 69 73 0a 61 6c 6c 6f 77 73 20 74 68 65 20 75 73  is.allows the us
68c0: 65 72 20 74 6f 20 72 65 74 72 79 20 74 68 65 20  er to retry the 
68d0: 43 4f 4d 4d 49 54 20 61 74 20 61 20 6c 61 74 65  COMMIT at a late
68e0: 72 20 74 69 6d 65 20 61 66 74 65 72 20 74 68 65  r time after the
68f0: 20 53 48 41 52 45 44 20 6c 6f 63 6b 0a 68 61 73   SHARED lock.has
6900: 20 68 61 64 20 61 6e 20 6f 70 70 6f 72 74 75 6e   had an opportun
6910: 69 74 79 20 74 6f 20 63 6c 65 61 72 2e 3c 2f 70  ity to clear.</p
6920: 3e 0a 0a 3c 70 3e 49 66 20 6d 75 6c 74 69 70 6c  >..<p>If multipl
6930: 65 20 63 6f 6d 6d 61 6e 64 73 20 61 72 65 20 62  e commands are b
6940: 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 61 67  eing executed ag
6950: 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20 53  ainst the same S
6960: 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 63  QLite database.c
6970: 6f 6e 6e 65 63 74 69 6f 6e 20 61 74 20 74 68 65  onnection at the
6980: 20 73 61 6d 65 20 74 69 6d 65 2c 20 74 68 65 20   same time, the 
6990: 61 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20 64 65  autocommit is de
69a0: 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65  ferred until the
69b0: 20 76 65 72 79 0a 6c 61 73 74 20 63 6f 6d 6d 61   very.last comma
69c0: 6e 64 20 63 6f 6d 70 6c 65 74 65 73 2e 20 20 46  nd completes.  F
69d0: 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61  or example, if a
69e0: 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
69f0: 74 20 69 73 20 62 65 69 6e 67 0a 65 78 65 63 75  t is being.execu
6a00: 74 65 64 2c 20 74 68 65 20 65 78 65 63 75 74 69  ted, the executi
6a10: 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6d 6d 61 6e  on of the comman
6a20: 64 20 77 69 6c 6c 20 70 61 75 73 65 20 61 73 20  d will pause as 
6a30: 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 0a  each row of the.
6a40: 72 65 73 75 6c 74 20 69 73 20 72 65 74 75 72 6e  result is return
6a50: 65 64 2e 20 20 44 75 72 69 6e 67 20 74 68 69 73  ed.  During this
6a60: 20 70 61 75 73 65 20 6f 74 68 65 72 20 49 4e 53   pause other INS
6a70: 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
6a80: 44 45 4c 45 54 45 0a 63 6f 6d 6d 61 6e 64 73 20  DELETE.commands 
6a90: 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20  can be executed 
6aa0: 61 67 61 69 6e 73 74 20 6f 74 68 65 72 20 74 61  against other ta
6ab0: 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  bles in the data
6ac0: 62 61 73 65 2e 20 20 42 75 74 20 6e 6f 6e 65 0a  base.  But none.
6ad0: 6f 66 20 74 68 65 73 65 20 63 68 61 6e 67 65 73  of these changes
6ae0: 20 77 69 6c 6c 20 63 6f 6d 6d 69 74 20 75 6e 74   will commit unt
6af0: 69 6c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  il the original 
6b00: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
6b10: 20 66 69 6e 69 73 68 65 73 2e 0a 3c 2f 70 3e 0a   finishes..</p>.