Documentation Source Text

Hex Artifact Content
Login

Artifact e2dbe7a0d26c76e94097aa53ec2ba1817552d6c4:


0000: 3c 74 69 74 6c 65 3e 41 74 6f 6d 69 63 20 43 6f  <title>Atomic Co
0010: 6d 6d 69 74 20 49 6e 20 53 51 4c 69 74 65 3c 2f  mmit In SQLite</
0020: 74 69 74 6c 65 3e 0a 3c 74 63 6c 3e 68 64 5f 6b  title>.<tcl>hd_k
0030: 65 79 77 6f 72 64 73 20 7b 61 74 6f 6d 69 63 20  eywords {atomic 
0040: 63 6f 6d 6d 69 74 7d 20 7b 2a 41 74 6f 6d 69 63  commit} {*Atomic
0050: 20 43 6f 6d 6d 69 74 7d 3c 2f 74 63 6c 3e 0a 0a   Commit}</tcl>..
0060: 3c 68 31 20 61 6c 69 67 6e 3d 22 63 65 6e 74 65  <h1 align="cente
0070: 72 22 3e 0a 41 74 6f 6d 69 63 20 43 6f 6d 6d 69  r">.Atomic Commi
0080: 74 20 49 6e 20 53 51 4c 69 74 65 0a 3c 2f 68 31  t In SQLite.</h1
0090: 3e 0a 0a 3c 68 32 3e 31 2e 30 20 49 6e 74 72 6f  >..<h2>1.0 Intro
00a0: 64 75 63 74 69 6f 6e 3c 2f 68 32 3e 0a 0a 3c 70  duction</h2>..<p
00b0: 3e 41 6e 20 69 6d 70 6f 72 74 61 6e 74 20 66 65  >An important fe
00c0: 61 74 75 72 65 20 6f 66 20 74 72 61 6e 73 61 63  ature of transac
00d0: 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 73  tional databases
00e0: 20 6c 69 6b 65 20 53 51 4c 69 74 65 0a 69 73 20   like SQLite.is 
00f0: 22 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 22 2e  "atomic commit".
0100: 20 20 0a 41 74 6f 6d 69 63 20 63 6f 6d 6d 69 74    .Atomic commit
0110: 20 6d 65 61 6e 73 20 74 68 61 74 20 65 69 74 68   means that eith
0120: 65 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  er all database 
0130: 63 68 61 6e 67 65 73 20 77 69 74 68 69 6e 20 61  changes within a
0140: 20 73 69 6e 67 6c 65 20 0a 74 72 61 6e 73 61 63   single .transac
0150: 74 69 6f 6e 20 6f 63 63 75 72 20 6f 72 20 6e 6f  tion occur or no
0160: 6e 65 20 6f 66 20 74 68 65 6d 20 6f 63 63 75 72  ne of them occur
0170: 2e 20 20 57 69 74 68 20 61 74 6f 6d 69 63 20 63  .  With atomic c
0180: 6f 6d 6d 69 74 2c 20 69 74 0a 69 73 20 61 73 20  ommit, it.is as 
0190: 69 66 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e  if many differen
01a0: 74 20 77 72 69 74 65 73 20 74 6f 20 64 69 66 66  t writes to diff
01b0: 65 72 65 6e 74 20 73 65 63 74 69 6f 6e 73 20 6f  erent sections o
01c0: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 66  f the database.f
01d0: 69 6c 65 20 6f 63 63 75 72 20 69 6e 73 74 61 6e  ile occur instan
01e0: 74 61 6e 65 6f 75 73 6c 79 20 61 6e 64 20 73 69  taneously and si
01f0: 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 52 65  multaneously..Re
0200: 61 6c 20 68 61 72 64 77 61 72 65 20 73 65 72 69  al hardware seri
0210: 61 6c 69 7a 65 73 20 77 72 69 74 65 73 20 74 6f  alizes writes to
0220: 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2c 20 61   mass storage, a
0230: 6e 64 20 77 72 69 74 69 6e 67 0a 61 20 73 69 6e  nd writing.a sin
0240: 67 6c 65 20 73 65 63 74 6f 72 20 74 61 6b 65 73  gle sector takes
0250: 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 6e 74   a finite amount
0260: 20 6f 66 20 74 69 6d 65 2e 0a 53 6f 20 69 74 20   of time..So it 
0270: 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f  is impossible to
0280: 20 74 72 75 6c 79 20 77 72 69 74 65 20 6d 61 6e   truly write man
0290: 79 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74  y different sect
02a0: 6f 72 73 20 6f 66 20 61 20 0a 64 61 74 61 62 61  ors of a .databa
02b0: 73 65 20 66 69 6c 65 20 73 69 6d 75 6c 74 61 6e  se file simultan
02c0: 65 6f 75 73 6c 79 20 61 6e 64 2f 6f 72 20 69 6e  eously and/or in
02d0: 73 74 61 6e 74 61 6e 65 6f 75 73 6c 79 2e 0a 42  stantaneously..B
02e0: 75 74 20 74 68 65 20 61 74 6f 6d 69 63 20 63 6f  ut the atomic co
02f0: 6d 6d 69 74 20 6c 6f 67 69 63 20 77 69 74 68 69  mmit logic withi
0300: 6e 0a 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69  n.SQLite makes i
0310: 74 20 61 70 70 65 61 72 20 61 73 20 69 66 20 74  t appear as if t
0320: 68 65 20 63 68 61 6e 67 65 73 20 66 6f 72 20 61  he changes for a
0330: 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 61 72 65   transaction.are
0340: 20 61 6c 6c 20 77 72 69 74 74 65 6e 20 69 6e 73   all written ins
0350: 74 61 6e 74 61 6e 65 6f 75 73 6c 79 20 61 6e 64  tantaneously and
0360: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e   simultaneously.
0370: 3c 2f 70 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20  </p>..<p>SQLite 
0380: 68 61 73 20 74 68 65 20 69 6d 70 6f 72 74 61 6e  has the importan
0390: 74 20 70 72 6f 70 65 72 74 79 20 74 68 61 74 20  t property that 
03a0: 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 70 70  transactions app
03b0: 65 61 72 0a 74 6f 20 62 65 20 61 74 6f 6d 69 63  ear.to be atomic
03c0: 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 72 61   even if the tra
03d0: 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6e 74 65  nsaction is inte
03e0: 72 72 75 70 74 65 64 20 62 79 20 61 6e 0a 6f 70  rrupted by an.op
03f0: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63  erating system c
0400: 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 66 61  rash or power fa
0410: 69 6c 75 72 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54  ilure.</p>..<p>T
0420: 68 69 73 20 61 72 74 69 63 6c 65 20 64 65 73 63  his article desc
0430: 72 69 62 65 73 20 74 68 65 20 74 65 63 68 6e 69  ribes the techni
0440: 71 75 65 73 20 75 73 65 64 20 62 79 20 53 51 4c  ques used by SQL
0450: 69 74 65 20 74 6f 20 63 72 65 61 74 65 20 74 68  ite to create th
0460: 65 0a 69 6c 6c 75 73 69 6f 6e 20 6f 66 20 61 74  e.illusion of at
0470: 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 3c 2f 70 3e  omic commit.</p>
0480: 0a 0a 3c 70 3e 54 68 65 20 69 6e 66 6f 72 6d 61  ..<p>The informa
0490: 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 61 72 74  tion in this art
04a0: 69 63 6c 65 20 61 70 70 6c 69 65 73 20 6f 6e 6c  icle applies onl
04b0: 79 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  y when SQLite is
04c0: 20 6f 70 65 72 61 74 69 6e 67 0a 69 6e 20 22 72   operating.in "r
04d0: 6f 6c 6c 62 61 63 6b 20 6d 6f 64 65 22 2c 20 6f  ollback mode", o
04e0: 72 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  r in other words
04f0: 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
0500: 6e 6f 74 20 0a 75 73 69 6e 67 20 61 20 5b 77 72  not .using a [wr
0510: 69 74 65 2d 61 68 65 61 64 20 6c 6f 67 5d 2e 20  ite-ahead log]. 
0520: 20 53 51 4c 69 74 65 20 73 74 69 6c 6c 20 73 75   SQLite still su
0530: 70 70 6f 72 74 73 20 61 74 6f 6d 69 63 20 63 6f  pports atomic co
0540: 6d 6d 69 74 20 77 68 65 6e 0a 77 72 69 74 65 2d  mmit when.write-
0550: 61 68 65 61 64 20 6c 6f 67 67 69 6e 67 20 69 73  ahead logging is
0560: 20 65 6e 61 62 6c 65 64 2c 20 62 75 74 20 69 74   enabled, but it
0570: 20 61 63 63 6f 6d 70 6c 69 73 68 65 73 20 61 74   accomplishes at
0580: 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 62 79 0a 61  omic commit by.a
0590: 20 64 69 66 66 65 72 65 6e 74 20 6d 65 63 68 61   different mecha
05a0: 6e 69 73 6d 20 66 72 6f 6d 20 74 68 65 20 6f 6e  nism from the on
05b0: 65 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74  e described in t
05c0: 68 69 73 20 61 72 74 69 63 6c 65 2e 20 20 53 65  his article.  Se
05d0: 65 0a 74 68 65 20 5b 57 41 4c 20 7c 20 77 72 69  e.the [WAL | wri
05e0: 74 65 2d 61 68 65 61 64 20 6c 6f 67 20 64 6f 63  te-ahead log doc
05f0: 75 6d 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20  umentation] for 
0600: 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
0610: 6d 61 74 69 6f 6e 20 6f 6e 20 68 6f 77 0a 53 51  mation on how.SQ
0620: 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 61 74  Lite supports at
0630: 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 69 6e 20 74  omic commit in t
0640: 68 61 74 20 63 6f 6e 74 65 78 74 2e 3c 2f 70 3e  hat context.</p>
0650: 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65  ..<tcl>hd_fragme
0660: 6e 74 20 68 61 72 64 77 61 72 65 3c 2f 74 63 6c  nt hardware</tcl
0670: 3e 0a 3c 68 32 3e 32 2e 30 20 48 61 72 64 77 61  >.<h2>2.0 Hardwa
0680: 72 65 20 41 73 73 75 6d 70 74 69 6f 6e 73 3c 2f  re Assumptions</
0690: 68 32 3e 0a 0a 3c 70 3e 54 68 72 6f 75 67 68 6f  h2>..<p>Througho
06a0: 75 74 20 74 68 69 73 20 61 72 74 69 63 6c 65 2c  ut this article,
06b0: 20 77 65 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68   we will call th
06c0: 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 20 64  e mass storage d
06d0: 65 76 69 63 65 20 22 64 69 73 6b 22 0a 65 76 65  evice "disk".eve
06e0: 6e 20 74 68 6f 75 67 68 20 74 68 65 20 6d 61 73  n though the mas
06f0: 73 20 73 74 6f 72 61 67 65 20 64 65 76 69 63 65  s storage device
0700: 20 6d 69 67 68 74 20 72 65 61 6c 6c 79 20 62 65   might really be
0710: 20 66 6c 61 73 68 20 6d 65 6d 6f 72 79 2e 3c 2f   flash memory.</
0720: 70 3e 0a 0a 3c 70 3e 57 65 20 61 73 73 75 6d 65  p>..<p>We assume
0730: 20 74 68 61 74 20 64 69 73 6b 20 69 73 20 77 72   that disk is wr
0740: 69 74 74 65 6e 20 69 6e 20 63 68 75 6e 6b 73 20  itten in chunks 
0750: 77 68 69 63 68 20 77 65 20 63 61 6c 6c 20 61 20  which we call a 
0760: 22 73 65 63 74 6f 72 22 2e 0a 49 74 20 69 73 20  "sector"..It is 
0770: 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
0780: 6d 6f 64 69 66 79 20 61 6e 79 20 70 61 72 74 20  modify any part 
0790: 6f 66 20 74 68 65 20 64 69 73 6b 20 73 6d 61 6c  of the disk smal
07a0: 6c 65 72 20 74 68 61 6e 20 61 20 73 65 63 74 6f  ler than a secto
07b0: 72 2e 0a 54 6f 20 63 68 61 6e 67 65 20 61 20 70  r..To change a p
07c0: 61 72 74 20 6f 66 20 74 68 65 20 64 69 73 6b 20  art of the disk 
07d0: 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 61 20 73  smaller than a s
07e0: 65 63 74 6f 72 2c 20 79 6f 75 20 68 61 76 65 20  ector, you have 
07f0: 74 6f 20 72 65 61 64 20 69 6e 0a 74 68 65 20 66  to read in.the f
0800: 75 6c 6c 20 73 65 63 74 6f 72 20 74 68 61 74 20  ull sector that 
0810: 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 61 72  contains the par
0820: 74 20 79 6f 75 20 77 61 6e 74 20 74 6f 20 63 68  t you want to ch
0830: 61 6e 67 65 2c 20 6d 61 6b 65 20 74 68 65 0a 63  ange, make the.c
0840: 68 61 6e 67 65 2c 20 74 68 65 6e 20 77 72 69 74  hange, then writ
0850: 65 20 62 61 63 6b 20 6f 75 74 20 74 68 65 20 63  e back out the c
0860: 6f 6d 70 6c 65 74 65 20 73 65 63 74 6f 72 2e 3c  omplete sector.<
0870: 2f 70 3e 0a 0a 3c 70 3e 4f 6e 20 61 20 74 72 61  /p>..<p>On a tra
0880: 64 69 74 69 6f 6e 61 6c 20 73 70 69 6e 6e 69 6e  ditional spinnin
0890: 67 20 64 69 73 6b 2c 20 61 20 73 65 63 74 6f 72  g disk, a sector
08a0: 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20   is the minimum 
08b0: 75 6e 69 74 20 6f 66 20 74 72 61 6e 73 66 65 72  unit of transfer
08c0: 0a 69 6e 20 62 6f 74 68 20 64 69 72 65 63 74 69  .in both directi
08d0: 6f 6e 73 2c 20 62 6f 74 68 20 72 65 61 64 69 6e  ons, both readin
08e0: 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2e 20 20  g and writing.  
08f0: 4f 6e 20 66 6c 61 73 68 20 6d 65 6d 6f 72 79 2c  On flash memory,
0900: 20 68 6f 77 65 76 65 72 2c 0a 74 68 65 20 6d 69   however,.the mi
0910: 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 20  nimum size of a 
0920: 72 65 61 64 20 69 73 20 74 79 70 69 63 61 6c 6c  read is typicall
0930: 79 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 74  y much smaller t
0940: 68 61 6e 20 61 20 6d 69 6e 69 6d 75 6d 20 77 72  han a minimum wr
0950: 69 74 65 2e 0a 53 51 4c 69 74 65 20 69 73 20 6f  ite..SQLite is o
0960: 6e 6c 79 20 63 6f 6e 63 65 72 6e 65 64 20 77 69  nly concerned wi
0970: 74 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 77  th the minimum w
0980: 72 69 74 65 20 61 6d 6f 75 6e 74 20 61 6e 64 20  rite amount and 
0990: 73 6f 20 66 6f 72 20 74 68 65 0a 70 75 72 70 6f  so for the.purpo
09a0: 73 65 73 20 6f 66 20 74 68 69 73 20 61 72 74 69  ses of this arti
09b0: 63 6c 65 2c 20 77 68 65 6e 20 77 65 20 73 61 79  cle, when we say
09c0: 20 22 73 65 63 74 6f 72 22 20 77 65 20 6d 65 61   "sector" we mea
09d0: 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d  n the minimum am
09e0: 6f 75 6e 74 0a 6f 66 20 64 61 74 61 20 74 68 61  ount.of data tha
09f0: 74 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  t can be written
0a00: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65   to mass storage
0a10: 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 67 6f 2e   in a single go.
0a20: 3c 2f 70 3e 0a 0a 3c 70 3e 0a 20 20 50 72 69 6f  </p>..<p>.  Prio
0a30: 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 72 73  r to SQLite vers
0a40: 69 6f 6e 20 33 2e 33 2e 31 34 2c 20 61 20 73 65  ion 3.3.14, a se
0a50: 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32  ctor size of 512
0a60: 20 62 79 74 65 73 20 77 61 73 0a 20 20 61 73 73   bytes was.  ass
0a70: 75 6d 65 64 20 69 6e 20 61 6c 6c 20 63 61 73 65  umed in all case
0a80: 73 2e 20 20 54 68 65 72 65 20 77 61 73 20 61 20  s.  There was a 
0a90: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74  compile-time opt
0aa0: 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0a 20 20  ion to change.  
0ab0: 74 68 69 73 20 62 75 74 20 74 68 65 20 63 6f 64  this but the cod
0ac0: 65 20 68 61 64 20 6e 65 76 65 72 20 62 65 65 6e  e had never been
0ad0: 20 74 65 73 74 65 64 20 77 69 74 68 20 61 20 6c   tested with a l
0ae0: 61 72 67 65 72 20 76 61 6c 75 65 2e 20 20 54 68  arger value.  Th
0af0: 65 0a 20 20 35 31 32 20 62 79 74 65 20 73 65 63  e.  512 byte sec
0b00: 74 6f 72 20 61 73 73 75 6d 70 74 69 6f 6e 20 73  tor assumption s
0b10: 65 65 6d 65 64 20 72 65 61 73 6f 6e 61 62 6c 65  eemed reasonable
0b20: 20 73 69 6e 63 65 20 75 6e 74 69 6c 20 76 65 72   since until ver
0b30: 79 20 72 65 63 65 6e 74 6c 79 0a 20 20 61 6c 6c  y recently.  all
0b40: 20 64 69 73 6b 20 64 72 69 76 65 73 20 75 73 65   disk drives use
0b50: 64 20 61 20 35 31 32 20 62 79 74 65 20 73 65 63  d a 512 byte sec
0b60: 74 6f 72 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20  tor internally. 
0b70: 20 48 6f 77 65 76 65 72 2c 20 74 68 65 72 65 0a   However, there.
0b80: 20 20 68 61 73 20 72 65 63 65 6e 74 6c 79 20 62    has recently b
0b90: 65 65 6e 20 61 20 70 75 73 68 20 74 6f 20 69 6e  een a push to in
0ba0: 63 72 65 61 73 65 20 74 68 65 20 73 65 63 74 6f  crease the secto
0bb0: 72 20 73 69 7a 65 20 6f 66 20 64 69 73 6b 73 20  r size of disks 
0bc0: 74 6f 0a 20 20 34 30 39 36 20 62 79 74 65 73 2e  to.  4096 bytes.
0bd0: 20 20 41 6c 73 6f 20 74 68 65 20 73 65 63 74 6f    Also the secto
0be0: 72 20 73 69 7a 65 0a 20 20 66 6f 72 20 66 6c 61  r size.  for fla
0bf0: 73 68 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 75  sh memory is usu
0c00: 61 6c 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e  ally larger than
0c10: 20 35 31 32 20 62 79 74 65 73 2e 20 20 46 6f 72   512 bytes.  For
0c20: 20 74 68 65 73 65 20 72 65 61 73 6f 6e 73 2c 0a   these reasons,.
0c30: 20 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51    versions of SQ
0c40: 4c 69 74 65 20 62 65 67 69 6e 6e 69 6e 67 20 77  Lite beginning w
0c50: 69 74 68 20 33 2e 33 2e 31 34 20 68 61 76 65 20  ith 3.3.14 have 
0c60: 61 20 6d 65 74 68 6f 64 20 69 6e 20 74 68 65 20  a method in the 
0c70: 4f 53 0a 20 20 69 6e 74 65 72 66 61 63 65 20 6c  OS.  interface l
0c80: 61 79 65 72 20 74 68 61 74 20 69 6e 74 65 72 72  ayer that interr
0c90: 6f 67 61 74 65 73 20 74 68 65 20 75 6e 64 65 72  ogates the under
0ca0: 6c 79 69 6e 67 20 66 69 6c 65 73 79 73 74 65 6d  lying filesystem
0cb0: 20 74 6f 20 66 69 6e 64 0a 20 20 74 68 65 20 74   to find.  the t
0cc0: 72 75 65 20 73 65 63 74 6f 72 20 73 69 7a 65 2e  rue sector size.
0cd0: 20 20 41 73 20 63 75 72 72 65 6e 74 6c 79 20 69    As currently i
0ce0: 6d 70 6c 65 6d 65 6e 74 65 64 20 28 76 65 72 73  mplemented (vers
0cf0: 69 6f 6e 20 33 2e 35 2e 30 29 20 74 68 69 73 0a  ion 3.5.0) this.
0d00: 20 20 6d 65 74 68 6f 64 20 73 74 69 6c 6c 20 72    method still r
0d10: 65 74 75 72 6e 73 20 61 20 68 61 72 64 2d 63 6f  eturns a hard-co
0d20: 64 65 64 20 76 61 6c 75 65 20 6f 66 20 35 31 32  ded value of 512
0d30: 20 62 79 74 65 73 2c 20 73 69 6e 63 65 20 74 68   bytes, since th
0d40: 65 72 65 0a 20 20 69 73 20 6e 6f 20 73 74 61 6e  ere.  is no stan
0d50: 64 61 72 64 20 77 61 79 20 6f 66 20 64 69 73 63  dard way of disc
0d60: 6f 76 65 72 69 6e 67 20 74 68 65 20 74 72 75 65  overing the true
0d70: 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 6e 20   sector size on 
0d80: 65 69 74 68 65 72 0a 20 20 55 6e 69 78 20 6f 72  either.  Unix or
0d90: 20 57 69 6e 64 6f 77 73 2e 20 20 42 75 74 20 74   Windows.  But t
0da0: 68 65 20 6d 65 74 68 6f 64 20 69 73 20 61 76 61  he method is ava
0db0: 69 6c 61 62 6c 65 20 66 6f 72 20 65 6d 62 65 64  ilable for embed
0dc0: 64 65 64 20 64 65 76 69 63 65 0a 20 20 6d 61 6e  ded device.  man
0dd0: 75 66 61 63 74 75 72 65 73 20 74 6f 20 74 77 65  ufactures to twe
0de0: 61 6b 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  ak according to 
0df0: 74 68 65 69 72 20 6f 77 6e 20 6e 65 65 64 73 2e  their own needs.
0e00: 20 20 41 6e 64 20 77 65 20 68 61 76 65 0a 20 20    And we have.  
0e10: 6c 65 66 74 20 6f 70 65 6e 20 74 68 65 20 70 6f  left open the po
0e20: 73 73 69 62 69 6c 69 74 79 20 6f 66 20 66 69 6c  ssibility of fil
0e30: 6c 69 6e 67 20 69 6e 20 61 20 6d 6f 72 65 20 6d  ling in a more m
0e40: 65 61 6e 69 6e 67 66 75 6c 20 69 6d 70 6c 65 6d  eaningful implem
0e50: 65 6e 74 61 74 69 6f 6e 0a 20 20 6f 6e 20 55 6e  entation.  on Un
0e60: 69 78 20 61 6e 64 20 57 69 6e 64 6f 77 73 20 69  ix and Windows i
0e70: 6e 20 74 68 65 20 66 75 74 75 72 65 2e 3c 2f 70  n the future.</p
0e80: 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 68 61 73  >..<p>SQLite has
0e90: 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 61   traditionally a
0ea0: 73 73 75 6d 65 64 20 74 68 61 74 20 61 20 73 65  ssumed that a se
0eb0: 63 74 6f 72 20 77 72 69 74 65 20 69 73 20 3c 75  ctor write is <u
0ec0: 3e 6e 6f 74 3c 2f 75 3e 20 61 74 6f 6d 69 63 2e  >not</u> atomic.
0ed0: 0a 48 6f 77 65 76 65 72 2c 20 53 51 4c 69 74 65  .However, SQLite
0ee0: 20 64 6f 65 73 20 61 6c 77 61 79 73 20 61 73 73   does always ass
0ef0: 75 6d 65 20 74 68 61 74 20 61 20 73 65 63 74 6f  ume that a secto
0f00: 72 20 77 72 69 74 65 20 69 73 20 6c 69 6e 65 61  r write is linea
0f10: 72 2e 20 20 42 79 20 22 6c 69 6e 65 61 72 22 0a  r.  By "linear".
0f20: 77 65 20 6d 65 61 6e 20 74 68 61 74 20 53 51 4c  we mean that SQL
0f30: 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74  ite assumes that
0f40: 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 61 20   when writing a 
0f50: 73 65 63 74 6f 72 2c 20 74 68 65 20 68 61 72 64  sector, the hard
0f60: 77 61 72 65 20 62 65 67 69 6e 73 0a 61 74 20 6f  ware begins.at o
0f70: 6e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61  ne end of the da
0f80: 74 61 20 61 6e 64 20 77 72 69 74 65 73 20 62 79  ta and writes by
0f90: 74 65 20 62 79 20 62 79 74 65 20 75 6e 74 69 6c  te by byte until
0fa0: 20 69 74 20 67 65 74 73 20 74 6f 0a 74 68 65 20   it gets to.the 
0fb0: 6f 74 68 65 72 20 65 6e 64 2e 20 20 54 68 65 20  other end.  The 
0fc0: 77 72 69 74 65 20 6d 69 67 68 74 20 67 6f 20 66  write might go f
0fd0: 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f  rom beginning to
0fe0: 20 65 6e 64 20 6f 72 20 66 72 6f 6d 0a 65 6e 64   end or from.end
0ff0: 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 2e 20 20   to beginning.  
1000: 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75  If a power failu
1010: 72 65 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65  re occurs in the
1020: 20 6d 69 64 64 6c 65 20 6f 66 20 61 0a 73 65 63   middle of a.sec
1030: 74 6f 72 20 77 72 69 74 65 20 69 74 20 6d 69 67  tor write it mig
1040: 68 74 20 62 65 20 74 68 61 74 20 70 61 72 74 20  ht be that part 
1050: 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 77 61  of the sector wa
1060: 73 20 6d 6f 64 69 66 69 65 64 0a 61 6e 64 20 61  s modified.and a
1070: 6e 6f 74 68 65 72 20 70 61 72 74 20 77 61 73 20  nother part was 
1080: 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 20  left unchanged. 
1090: 20 54 68 65 20 6b 65 79 20 61 73 73 75 6d 70 74   The key assumpt
10a0: 69 6f 6e 20 62 79 20 53 51 4c 69 74 65 0a 69 73  ion by SQLite.is
10b0: 20 74 68 61 74 20 69 66 20 61 6e 79 20 70 61 72   that if any par
10c0: 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20  t of the sector 
10d0: 67 65 74 73 20 63 68 61 6e 67 65 64 2c 20 74 68  gets changed, th
10e0: 65 6e 20 65 69 74 68 65 72 20 74 68 65 0a 66 69  en either the.fi
10f0: 72 73 74 20 6f 72 20 74 68 65 20 6c 61 73 74 20  rst or the last 
1100: 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 63 68  bytes will be ch
1110: 61 6e 67 65 64 2e 20 20 53 6f 20 74 68 65 20 68  anged.  So the h
1120: 61 72 64 77 61 72 65 20 77 69 6c 6c 0a 6e 65 76  ardware will.nev
1130: 65 72 20 73 74 61 72 74 20 77 72 69 74 69 6e 67  er start writing
1140: 20 61 20 73 65 63 74 6f 72 20 69 6e 20 74 68 65   a sector in the
1150: 20 6d 69 64 64 6c 65 20 61 6e 64 20 77 6f 72 6b   middle and work
1160: 20 74 6f 77 61 72 64 73 20 74 68 65 0a 65 6e 64   towards the.end
1170: 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 6b 6e  s.  We do not kn
1180: 6f 77 20 69 66 20 74 68 69 73 20 61 73 73 75 6d  ow if this assum
1190: 70 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20  ption is always 
11a0: 74 72 75 65 20 62 75 74 20 69 74 0a 73 65 65 6d  true but it.seem
11b0: 73 20 72 65 61 73 6f 6e 61 62 6c 65 2e 3c 2f 70  s reasonable.</p
11c0: 3e 0a 0a 3c 70 3e 54 68 65 20 70 72 65 76 69 6f  >..<p>The previo
11d0: 75 73 20 70 61 72 61 67 72 61 70 68 20 73 74 61  us paragraph sta
11e0: 74 65 73 20 74 68 61 74 20 53 51 4c 69 74 65 20  tes that SQLite 
11f0: 64 6f 65 73 20 6e 6f 74 20 61 73 73 75 6d 65 20  does not assume 
1200: 74 68 61 74 0a 73 65 63 74 6f 72 20 77 72 69 74  that.sector writ
1210: 65 73 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20  es are atomic.  
1220: 54 68 69 73 20 69 73 20 74 72 75 65 20 62 79 20  This is true by 
1230: 64 65 66 61 75 6c 74 2e 20 20 42 75 74 20 61 73  default.  But as
1240: 20 6f 66 0a 53 51 4c 69 74 65 20 76 65 72 73 69   of.SQLite versi
1250: 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 72 65 20  on 3.5.0, there 
1260: 69 73 20 61 20 6e 65 77 20 69 6e 74 65 72 66 61  is a new interfa
1270: 63 65 20 63 61 6c 6c 65 64 20 74 68 65 0a 56 69  ce called the.Vi
1280: 72 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65  rtual File Syste
1290: 6d 20 28 5b 56 46 53 5d 29 20 69 6e 74 65 72 66  m ([VFS]) interf
12a0: 61 63 65 2e 20 20 54 68 65 20 5b 56 46 53 5d 20  ace.  The [VFS] 
12b0: 69 73 20 74 68 65 20 6f 6e 6c 79 20 6d 65 61 6e  is the only mean
12c0: 73 0a 62 79 20 77 68 69 63 68 20 53 51 4c 69 74  s.by which SQLit
12d0: 65 20 63 6f 6d 6d 75 6e 69 63 61 74 65 73 20 74  e communicates t
12e0: 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  o the underlying
12f0: 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 54 68   filesystem.  Th
1300: 65 0a 63 6f 64 65 20 63 6f 6d 65 73 20 77 69 74  e.code comes wit
1310: 68 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 6d  h default VFS im
1320: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
1330: 72 20 55 6e 69 78 20 61 6e 64 20 57 69 6e 64 6f  r Unix and Windo
1340: 77 73 0a 61 6e 64 20 74 68 65 72 65 20 69 73 20  ws.and there is 
1350: 61 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20  a mechanism for 
1360: 63 72 65 61 74 69 6e 67 20 6e 65 77 20 63 75 73  creating new cus
1370: 74 6f 6d 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  tom VFS implemen
1380: 74 61 74 69 6f 6e 73 0a 61 74 20 72 75 6e 74 69  tations.at runti
1390: 6d 65 2e 20 20 49 6e 20 74 68 69 73 20 6e 65 77  me.  In this new
13a0: 20 56 46 53 20 69 6e 74 65 72 66 61 63 65 20 74   VFS interface t
13b0: 68 65 72 65 20 69 73 20 61 20 6d 65 74 68 6f 64  here is a method
13c0: 20 63 61 6c 6c 65 64 0a 78 44 65 76 69 63 65 43   called.xDeviceC
13d0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e 20  haracteristics. 
13e0: 20 54 68 69 73 20 6d 65 74 68 6f 64 20 69 6e 74   This method int
13f0: 65 72 72 6f 67 61 74 65 73 20 74 68 65 20 75 6e  errogates the un
1400: 64 65 72 6c 79 69 6e 67 0a 66 69 6c 65 73 79 73  derlying.filesys
1410: 74 65 6d 20 74 6f 20 64 69 73 63 6f 76 65 72 20  tem to discover 
1420: 76 61 72 69 6f 75 73 20 70 72 6f 70 65 72 74 69  various properti
1430: 65 73 20 61 6e 64 20 62 65 68 61 76 69 6f 72 73  es and behaviors
1440: 20 74 68 61 74 20 74 68 65 0a 66 69 6c 65 73 79   that the.filesy
1450: 73 74 65 6d 20 6d 61 79 20 6f 72 20 6d 61 79 20  stem may or may 
1460: 6e 6f 74 20 65 78 68 69 62 69 74 2e 20 20 54 68  not exhibit.  Th
1470: 65 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  e xDeviceCharact
1480: 65 72 69 73 74 69 63 73 0a 6d 65 74 68 6f 64 20  eristics.method 
1490: 6d 69 67 68 74 20 69 6e 64 69 63 61 74 65 20 74  might indicate t
14a0: 68 61 74 20 73 65 63 74 6f 72 20 77 72 69 74 65  hat sector write
14b0: 73 20 61 72 65 20 61 74 6f 6d 69 63 2c 20 61 6e  s are atomic, an
14c0: 64 20 69 66 20 69 74 20 64 6f 65 73 0a 73 6f 20  d if it does.so 
14d0: 69 6e 64 69 63 61 74 65 2c 20 53 51 4c 69 74 65  indicate, SQLite
14e0: 20 77 69 6c 6c 20 74 72 79 20 74 6f 20 74 61 6b   will try to tak
14f0: 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74  e advantage of t
1500: 68 61 74 20 66 61 63 74 2e 20 20 42 75 74 0a 74  hat fact.  But.t
1510: 68 65 20 64 65 66 61 75 6c 74 20 78 44 65 76 69  he default xDevi
1520: 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
1530: 73 20 6d 65 74 68 6f 64 20 66 6f 72 20 62 6f 74  s method for bot
1540: 68 20 55 6e 69 78 20 61 6e 64 20 57 69 6e 64 6f  h Unix and Windo
1550: 77 73 0a 64 6f 65 73 20 6e 6f 74 20 69 6e 64 69  ws.does not indi
1560: 63 61 74 65 20 61 74 6f 6d 69 63 20 73 65 63 74  cate atomic sect
1570: 6f 72 20 77 72 69 74 65 73 20 61 6e 64 20 73 6f  or writes and so
1580: 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74   these optimizat
1590: 69 6f 6e 73 0a 61 72 65 20 6e 6f 72 6d 61 6c 6c  ions.are normall
15a0: 79 20 6f 6d 69 74 74 65 64 2e 3c 2f 70 3e 0a 0a  y omitted.</p>..
15b0: 3c 70 3e 53 51 4c 69 74 65 20 61 73 73 75 6d 65  <p>SQLite assume
15c0: 73 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 61  s that the opera
15d0: 74 69 6e 67 20 73 79 73 74 65 6d 20 77 69 6c 6c  ting system will
15e0: 20 62 75 66 66 65 72 20 77 72 69 74 65 73 20 61   buffer writes a
15f0: 6e 64 0a 74 68 61 74 20 61 20 77 72 69 74 65 20  nd.that a write 
1600: 72 65 71 75 65 73 74 20 77 69 6c 6c 20 72 65 74  request will ret
1610: 75 72 6e 20 62 65 66 6f 72 65 20 64 61 74 61 20  urn before data 
1620: 68 61 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65  has actually bee
1630: 6e 20 73 74 6f 72 65 64 0a 69 6e 20 74 68 65 20  n stored.in the 
1640: 6d 61 73 73 20 73 74 6f 72 61 67 65 20 64 65 76  mass storage dev
1650: 69 63 65 2e 0a 53 51 4c 69 74 65 20 66 75 72 74  ice..SQLite furt
1660: 68 65 72 20 61 73 73 75 6d 65 73 20 74 68 61 74  her assumes that
1670: 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e   write operation
1680: 73 20 77 69 6c 6c 20 62 65 20 72 65 6f 72 64 65  s will be reorde
1690: 72 65 64 20 62 79 0a 74 68 65 20 6f 70 65 72 61  red by.the opera
16a0: 74 69 6e 67 20 73 79 73 74 65 6d 2e 0a 46 6f 72  ting system..For
16b0: 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 53 51   this reason, SQ
16c0: 4c 69 74 65 20 64 6f 65 73 20 61 20 22 66 6c 75  Lite does a "flu
16d0: 73 68 22 20 6f 72 20 22 66 73 79 6e 63 22 20 6f  sh" or "fsync" o
16e0: 70 65 72 61 74 69 6f 6e 20 61 74 20 6b 65 79 0a  peration at key.
16f0: 70 6f 69 6e 74 73 2e 20 20 53 51 4c 69 74 65 20  points.  SQLite 
1700: 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
1710: 20 66 6c 75 73 68 20 6f 72 20 66 73 79 6e 63 20   flush or fsync 
1720: 77 69 6c 6c 20 6e 6f 74 20 72 65 74 75 72 6e 20  will not return 
1730: 75 6e 74 69 6c 0a 61 6c 6c 20 70 65 6e 64 69 6e  until.all pendin
1740: 67 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f  g write operatio
1750: 6e 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  ns for the file 
1760: 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 66 6c  that is being fl
1770: 75 73 68 65 64 20 68 61 76 65 0a 63 6f 6d 70 6c  ushed have.compl
1780: 65 74 65 64 2e 20 20 57 65 20 61 72 65 20 74 6f  eted.  We are to
1790: 6c 64 20 74 68 61 74 20 74 68 65 20 66 6c 75 73  ld that the flus
17a0: 68 20 61 6e 64 20 66 73 79 6e 63 20 70 72 69 6d  h and fsync prim
17b0: 69 74 69 76 65 73 0a 61 72 65 20 62 72 6f 6b 65  itives.are broke
17c0: 6e 20 6f 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f  n on some versio
17d0: 6e 73 20 6f 66 20 57 69 6e 64 6f 77 73 20 61 6e  ns of Windows an
17e0: 64 20 4c 69 6e 75 78 2e 20 20 54 68 69 73 20 69  d Linux.  This i
17f0: 73 20 75 6e 66 6f 72 74 75 6e 61 74 65 2e 0a 49  s unfortunate..I
1800: 74 20 6f 70 65 6e 73 20 53 51 4c 69 74 65 20 75  t opens SQLite u
1810: 70 20 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69  p to the possibi
1820: 6c 69 74 79 20 6f 66 20 64 61 74 61 62 61 73 65  lity of database
1830: 20 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c   corruption foll
1840: 6f 77 69 6e 67 0a 61 20 70 6f 77 65 72 20 6c 6f  owing.a power lo
1850: 73 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  ss in the middle
1860: 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 20 20 48   of a commit.  H
1870: 6f 77 65 76 65 72 2c 20 74 68 65 72 65 20 69 73  owever, there is
1880: 20 6e 6f 74 68 69 6e 67 20 0a 74 68 61 74 20 53   nothing .that S
1890: 51 4c 69 74 65 20 63 61 6e 20 64 6f 20 74 6f 20  QLite can do to 
18a0: 74 65 73 74 20 66 6f 72 20 6f 72 20 72 65 6d 65  test for or reme
18b0: 64 79 20 74 68 65 20 73 69 74 75 61 74 69 6f 6e  dy the situation
18c0: 2e 20 20 53 51 4c 69 74 65 0a 61 73 73 75 6d 65  .  SQLite.assume
18d0: 73 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 61  s that the opera
18e0: 74 69 6e 67 20 73 79 73 74 65 6d 20 74 68 61 74  ting system that
18f0: 20 69 74 20 69 73 20 72 75 6e 6e 69 6e 67 20 6f   it is running o
1900: 6e 20 77 6f 72 6b 73 20 61 73 0a 61 64 76 65 72  n works as.adver
1910: 74 69 73 65 64 2e 20 20 49 66 20 74 68 61 74 20  tised.  If that 
1920: 69 73 20 6e 6f 74 20 71 75 69 74 65 20 74 68 65  is not quite the
1930: 20 63 61 73 65 2c 20 77 65 6c 6c 20 74 68 65 6e   case, well then
1940: 20 68 6f 70 65 66 75 6c 6c 79 20 79 6f 75 0a 77   hopefully you.w
1950: 69 6c 6c 20 6e 6f 74 20 6c 6f 73 65 20 70 6f 77  ill not lose pow
1960: 65 72 20 74 6f 6f 20 6f 66 74 65 6e 2e 3c 2f 70  er too often.</p
1970: 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 61 73 73  >..<p>SQLite ass
1980: 75 6d 65 73 20 74 68 61 74 20 77 68 65 6e 20 61  umes that when a
1990: 20 66 69 6c 65 20 67 72 6f 77 73 20 69 6e 20 6c   file grows in l
19a0: 65 6e 67 74 68 20 74 68 61 74 20 74 68 65 20 6e  ength that the n
19b0: 65 77 0a 66 69 6c 65 20 73 70 61 63 65 20 6f 72  ew.file space or
19c0: 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 74 61 69 6e  iginally contain
19d0: 73 20 67 61 72 62 61 67 65 20 61 6e 64 20 74 68  s garbage and th
19e0: 65 6e 20 6c 61 74 65 72 20 69 73 20 66 69 6c 6c  en later is fill
19f0: 65 64 20 69 6e 0a 77 69 74 68 20 74 68 65 20 64  ed in.with the d
1a00: 61 74 61 20 61 63 74 75 61 6c 6c 79 20 77 72 69  ata actually wri
1a10: 74 74 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  tten.  In other 
1a20: 77 6f 72 64 73 2c 20 53 51 4c 69 74 65 20 61 73  words, SQLite as
1a30: 73 75 6d 65 73 20 74 68 61 74 0a 74 68 65 20 66  sumes that.the f
1a40: 69 6c 65 20 73 69 7a 65 20 69 73 20 75 70 64 61  ile size is upda
1a50: 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 66  ted before the f
1a60: 69 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 20 54 68  ile content.  Th
1a70: 69 73 20 69 73 20 61 20 0a 70 65 73 73 69 6d 69  is is a .pessimi
1a80: 73 74 69 63 20 61 73 73 75 6d 70 74 69 6f 6e 20  stic assumption 
1a90: 61 6e 64 20 53 51 4c 69 74 65 20 68 61 73 20 74  and SQLite has t
1aa0: 6f 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20  o do some extra 
1ab0: 77 6f 72 6b 20 74 6f 20 6d 61 6b 65 0a 73 75 72  work to make.sur
1ac0: 65 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e  e that it does n
1ad0: 6f 74 20 63 61 75 73 65 20 64 61 74 61 62 61 73  ot cause databas
1ae0: 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20  e corruption if 
1af0: 70 6f 77 65 72 20 69 73 20 6c 6f 73 74 0a 62 65  power is lost.be
1b00: 74 77 65 65 6e 20 74 68 65 20 74 69 6d 65 20 77  tween the time w
1b10: 68 65 6e 20 74 68 65 20 66 69 6c 65 20 73 69 7a  hen the file siz
1b20: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 61  e is increased a
1b30: 6e 64 20 77 68 65 6e 20 74 68 65 0a 6e 65 77 20  nd when the.new 
1b40: 63 6f 6e 74 65 6e 74 20 69 73 20 77 72 69 74 74  content is writt
1b50: 65 6e 2e 20 20 54 68 65 20 78 44 65 76 69 63 65  en.  The xDevice
1b60: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
1b70: 6d 65 74 68 6f 64 20 6f 66 0a 74 68 65 20 5b 56  method of.the [V
1b80: 46 53 5d 20 6d 69 67 68 74 20 69 6e 64 69 63 61  FS] might indica
1b90: 74 65 20 74 68 61 74 20 74 68 65 20 66 69 6c 65  te that the file
1ba0: 73 79 73 74 65 6d 20 77 69 6c 6c 20 61 6c 77 61  system will alwa
1bb0: 79 73 20 77 72 69 74 65 20 74 68 65 0a 64 61 74  ys write the.dat
1bc0: 61 20 62 65 66 6f 72 65 20 75 70 64 61 74 69 6e  a before updatin
1bd0: 67 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2e  g the file size.
1be0: 20 20 28 54 68 69 73 20 69 73 20 74 68 65 20 0a    (This is the .
1bf0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
1c00: 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74  E_APPEND propert
1c10: 79 20 66 6f 72 20 74 68 6f 73 65 20 72 65 61 64  y for those read
1c20: 65 72 73 20 77 68 6f 20 61 72 65 20 6c 6f 6f 6b  ers who are look
1c30: 69 6e 67 0a 61 74 20 74 68 65 20 63 6f 64 65 2e  ing.at the code.
1c40: 29 20 20 57 68 65 6e 20 74 68 65 20 78 44 65 76  )  When the xDev
1c50: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
1c60: 63 73 20 6d 65 74 68 6f 64 20 69 6e 64 69 63 61  cs method indica
1c70: 74 65 73 0a 74 68 61 74 20 66 69 6c 65 73 20 63  tes.that files c
1c80: 6f 6e 74 65 6e 74 20 69 73 20 77 72 69 74 74 65  ontent is writte
1c90: 6e 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 6c  n before the fil
1ca0: 65 20 73 69 7a 65 20 69 73 20 69 6e 63 72 65 61  e size is increa
1cb0: 73 65 64 2c 0a 53 51 4c 69 74 65 20 63 61 6e 20  sed,.SQLite can 
1cc0: 66 6f 72 65 67 6f 20 73 6f 6d 65 20 6f 66 20 69  forego some of i
1cd0: 74 73 20 70 65 64 61 6e 74 69 63 20 64 61 74 61  ts pedantic data
1ce0: 62 61 73 65 20 70 72 6f 74 65 63 74 69 6f 6e 20  base protection 
1cf0: 73 74 65 70 73 0a 61 6e 64 20 74 68 65 72 65 62  steps.and thereb
1d00: 79 20 64 65 63 72 65 61 73 65 20 74 68 65 20 61  y decrease the a
1d10: 6d 6f 75 6e 74 20 6f 66 20 64 69 73 6b 20 49 2f  mount of disk I/
1d20: 4f 20 6e 65 65 64 65 64 20 74 6f 20 70 65 72 66  O needed to perf
1d30: 6f 72 6d 20 61 0a 63 6f 6d 6d 69 74 2e 20 20 54  orm a.commit.  T
1d40: 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65  he current imple
1d50: 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 6f 77 65 76  mentation, howev
1d60: 65 72 2c 20 6d 61 6b 65 73 20 6e 6f 20 73 75 63  er, makes no suc
1d70: 68 20 61 73 73 75 6d 70 74 69 6f 6e 73 0a 66 6f  h assumptions.fo
1d80: 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  r the default VF
1d90: 53 65 73 20 66 6f 72 20 57 69 6e 64 6f 77 73 20  Ses for Windows 
1da0: 61 6e 64 20 55 6e 69 78 2e 3c 2f 70 3e 0a 0a 3c  and Unix.</p>..<
1db0: 70 3e 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73  p>SQLite assumes
1dc0: 20 74 68 61 74 20 61 20 66 69 6c 65 20 64 65 6c   that a file del
1dd0: 65 74 69 6f 6e 20 69 73 20 61 74 6f 6d 69 63 20  etion is atomic 
1de0: 66 72 6f 6d 20 74 68 65 0a 70 6f 69 6e 74 20 6f  from the.point o
1df0: 66 20 76 69 65 77 20 6f 66 20 61 20 75 73 65 72  f view of a user
1e00: 20 70 72 6f 63 65 73 73 2e 20 20 42 79 20 74 68   process.  By th
1e10: 69 73 20 77 65 20 6d 65 61 6e 20 74 68 61 74 20  is we mean that 
1e20: 69 66 20 53 51 4c 69 74 65 0a 72 65 71 75 65 73  if SQLite.reques
1e30: 74 73 20 74 68 61 74 20 61 20 66 69 6c 65 20 62  ts that a file b
1e40: 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68  e deleted and th
1e50: 65 20 70 6f 77 65 72 20 69 73 20 6c 6f 73 74 20  e power is lost 
1e60: 64 75 72 69 6e 67 20 74 68 65 0a 64 65 6c 65 74  during the.delet
1e70: 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20 6f 6e 63  e operation, onc
1e80: 65 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f  e power is resto
1e90: 72 65 64 20 65 69 74 68 65 72 20 74 68 65 20 66  red either the f
1ea0: 69 6c 65 20 77 69 6c 6c 0a 65 78 69 73 74 20 63  ile will.exist c
1eb0: 6f 6d 70 6c 65 74 65 6c 79 20 77 69 74 68 20 61  ompletely with a
1ec0: 6c 6c 20 69 66 20 69 74 73 20 6f 72 69 67 69 6e  ll if its origin
1ed0: 61 6c 20 63 6f 6e 74 65 6e 74 20 75 6e 61 6c 74  al content unalt
1ee0: 65 72 65 64 2c 20 6f 72 0a 65 6c 73 65 20 74 68  ered, or.else th
1ef0: 65 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20  e file will not 
1f00: 62 65 20 73 65 65 6e 20 69 6e 20 74 68 65 20 66  be seen in the f
1f10: 69 6c 65 73 79 73 74 65 6d 20 61 74 20 61 6c 6c  ilesystem at all
1f20: 2e 20 20 49 66 0a 61 66 74 65 72 20 70 6f 77 65  .  If.after powe
1f30: 72 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 68  r is restored th
1f40: 65 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 70  e file is only p
1f50: 61 72 74 69 61 6c 6c 79 20 64 65 6c 65 74 65 64  artially deleted
1f60: 2c 0a 69 66 20 73 6f 6d 65 20 6f 66 20 69 74 73  ,.if some of its
1f70: 20 64 61 74 61 20 68 61 73 20 62 65 65 6e 20 61   data has been a
1f80: 6c 74 65 72 65 64 20 6f 72 20 65 72 61 73 65 64  ltered or erased
1f90: 2c 0a 6f 72 20 74 68 65 20 66 69 6c 65 20 68 61  ,.or the file ha
1fa0: 73 20 62 65 65 6e 20 74 72 75 6e 63 61 74 65 64  s been truncated
1fb0: 20 62 75 74 20 6e 6f 74 20 63 6f 6d 70 6c 65 74   but not complet
1fc0: 65 6c 79 20 72 65 6d 6f 76 65 64 2c 20 74 68 65  ely removed, the
1fd0: 6e 0a 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  n.database corru
1fe0: 70 74 69 6f 6e 20 77 69 6c 6c 20 6c 69 6b 65 6c  ption will likel
1ff0: 79 20 72 65 73 75 6c 74 2e 3c 2f 70 3e 0a 0a 3c  y result.</p>..<
2000: 70 3e 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73  p>SQLite assumes
2010: 20 74 68 61 74 20 74 68 65 20 64 65 74 65 63 74   that the detect
2020: 69 6f 6e 20 61 6e 64 2f 6f 72 20 63 6f 72 72 65  ion and/or corre
2030: 63 74 69 6f 6e 20 6f 66 20 0a 62 69 74 20 65 72  ction of .bit er
2040: 72 6f 72 73 20 63 61 75 73 65 64 20 62 79 20 63  rors caused by c
2050: 6f 73 6d 69 63 20 72 61 79 73 2c 20 74 68 65 72  osmic rays, ther
2060: 6d 61 6c 20 6e 6f 69 73 65 2c 20 71 75 61 6e 74  mal noise, quant
2070: 75 6d 0a 66 6c 75 63 74 75 61 74 69 6f 6e 73 2c  um.fluctuations,
2080: 20 64 65 76 69 63 65 20 64 72 69 76 65 72 20 62   device driver b
2090: 75 67 73 2c 20 6f 72 20 6f 74 68 65 72 20 6d 65  ugs, or other me
20a0: 63 68 61 6e 69 73 6d 73 2c 20 69 73 20 74 68 65  chanisms, is the
20b0: 20 0a 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79   .responsibility
20c0: 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69   of the underlyi
20d0: 6e 67 20 68 61 72 64 77 61 72 65 20 61 6e 64 20  ng hardware and 
20e0: 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
20f0: 2e 20 20 0a 53 51 4c 69 74 65 20 64 6f 65 73 20  .  .SQLite does 
2100: 6e 6f 74 20 61 64 64 20 61 6e 79 20 72 65 64 75  not add any redu
2110: 6e 64 61 6e 63 79 20 74 6f 20 74 68 65 20 64 61  ndancy to the da
2120: 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 0a  tabase file for.
2130: 74 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 64  the purpose of d
2140: 65 74 65 63 74 69 6e 67 20 63 6f 72 72 75 70 74  etecting corrupt
2150: 69 6f 6e 20 6f 72 20 49 2f 4f 20 65 72 72 6f 72  ion or I/O error
2160: 73 2e 0a 53 51 4c 69 74 65 20 61 73 73 75 6d 65  s..SQLite assume
2170: 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20  s that the data 
2180: 69 74 20 72 65 61 64 73 20 69 73 20 65 78 61 63  it reads is exac
2190: 74 6c 79 20 74 68 65 20 73 61 6d 65 20 64 61 74  tly the same dat
21a0: 61 20 0a 74 68 61 74 20 69 74 20 70 72 65 76 69  a .that it previ
21b0: 6f 75 73 6c 79 20 77 72 6f 74 65 2e 3c 2f 70 3e  ously wrote.</p>
21c0: 0a 0a 3c 70 3e 42 79 20 64 65 66 61 75 6c 74 2c  ..<p>By default,
21d0: 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20   SQLite assumes 
21e0: 74 68 61 74 20 61 6e 20 6f 70 65 72 61 74 69 6e  that an operatin
21f0: 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 74 6f  g system call to
2200: 20 77 72 69 74 65 0a 61 20 72 61 6e 67 65 20 6f   write.a range o
2210: 66 20 62 79 74 65 73 20 77 69 6c 6c 20 6e 6f 74  f bytes will not
2220: 20 64 61 6d 61 67 65 20 6f 72 20 61 6c 74 65 72   damage or alter
2230: 20 61 6e 79 20 62 79 74 65 73 20 6f 75 74 73 69   any bytes outsi
2240: 64 65 20 6f 66 20 74 68 61 74 20 72 61 6e 67 65  de of that range
2250: 0a 65 76 65 6e 20 69 66 20 61 20 70 6f 77 65 72  .even if a power
2260: 20 6c 6f 73 73 20 6f 72 20 4f 53 20 63 72 61 73   loss or OS cras
2270: 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  h occurs during 
2280: 74 68 61 74 20 77 72 69 74 65 2e 20 20 57 65 0a  that write.  We.
2290: 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22 5b  call this the "[
22a0: 50 53 4f 57 20 7c 20 70 6f 77 65 72 73 61 66 65  PSOW | powersafe
22b0: 20 6f 76 65 72 77 72 69 74 65 5d 22 20 70 72 6f   overwrite]" pro
22c0: 70 65 72 74 79 2e 20 20 50 72 69 6f 72 20 74 6f  perty.  Prior to
22d0: 20 76 65 72 73 69 6f 6e 20 33 2e 37 2e 39 2c 0a   version 3.7.9,.
22e0: 53 51 4c 69 74 65 20 64 69 64 20 6e 6f 74 20 61  SQLite did not a
22f0: 73 73 75 6d 65 20 70 6f 77 65 72 73 61 66 65 20  ssume powersafe 
2300: 6f 76 65 72 77 72 69 74 65 2e 20 20 42 75 74 20  overwrite.  But 
2310: 77 69 74 68 20 74 68 65 20 73 74 61 6e 64 61 72  with the standar
2320: 64 0a 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e  d.sector size in
2330: 63 72 65 61 73 69 6e 67 20 66 72 6f 6d 20 35 31  creasing from 51
2340: 32 20 74 6f 20 34 30 39 36 20 62 79 74 65 73 20  2 to 4096 bytes 
2350: 6f 6e 20 6d 6f 73 74 20 64 69 73 6b 20 64 72 69  on most disk dri
2360: 76 65 73 2c 20 69 74 0a 68 61 73 20 62 65 63 6f  ves, it.has beco
2370: 6d 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20  me necessary to 
2380: 61 73 73 75 6d 65 20 70 6f 77 65 72 73 61 66 65  assume powersafe
2390: 20 6f 76 65 72 77 72 69 74 65 20 69 6e 20 6f 72   overwrite in or
23a0: 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a  der to maintain.
23b0: 68 69 73 74 6f 72 69 63 61 6c 20 70 65 72 66 6f  historical perfo
23c0: 72 6d 61 6e 63 65 20 6c 65 76 65 6c 73 20 61 6e  rmance levels an
23d0: 64 20 73 6f 20 70 6f 77 65 72 73 61 66 65 20 6f  d so powersafe o
23e0: 76 65 72 77 72 69 74 65 20 69 73 20 61 73 73 75  verwrite is assu
23f0: 6d 65 64 20 62 79 0a 64 65 66 61 75 6c 74 20 69  med by.default i
2400: 6e 20 72 65 63 65 6e 74 20 76 65 72 73 69 6f 6e  n recent version
2410: 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68  s of SQLite.  Th
2420: 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 6f 66 20  e assumption of 
2430: 70 6f 77 65 72 73 61 66 65 20 0a 6f 76 65 72 77  powersafe .overw
2440: 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 63 61  rite property ca
2450: 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 61 74  n be disabled at
2460: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72   compile-time or
2470: 20 61 20 72 75 6e 2d 74 69 6d 65 20 69 66 0a 64   a run-time if.d
2480: 65 73 69 72 65 64 2e 20 20 53 65 65 20 74 68 65  esired.  See the
2490: 20 5b 50 53 4f 57 20 7c 20 70 6f 77 65 72 73 61   [PSOW | powersa
24a0: 66 65 20 6f 76 65 72 77 72 69 74 65 20 64 6f 63  fe overwrite doc
24b0: 75 6d 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20  umentation] for 
24c0: 66 75 72 74 68 65 72 0a 64 65 74 61 69 6c 73 2e  further.details.
24d0: 0a 0a 0a 3c 61 20 6e 61 6d 65 3d 22 73 65 63 74  ...<a name="sect
24e0: 69 6f 6e 5f 33 5f 30 22 3e 3c 2f 61 3e 0a 3c 68  ion_3_0"></a>.<h
24f0: 32 3e 33 2e 30 20 53 69 6e 67 6c 65 20 46 69 6c  2>3.0 Single Fil
2500: 65 20 43 6f 6d 6d 69 74 3c 2f 68 32 3e 0a 0a 3c  e Commit</h2>..<
2510: 70 3e 57 65 20 62 65 67 69 6e 20 77 69 74 68 20  p>We begin with 
2520: 61 6e 20 6f 76 65 72 76 69 65 77 20 6f 66 20 74  an overview of t
2530: 68 65 20 73 74 65 70 73 20 53 51 4c 69 74 65 20  he steps SQLite 
2540: 74 61 6b 65 73 20 69 6e 20 6f 72 64 65 72 20 74  takes in order t
2550: 6f 0a 70 65 72 66 6f 72 6d 20 61 6e 20 61 74 6f  o.perform an ato
2560: 6d 69 63 20 63 6f 6d 6d 69 74 20 6f 66 20 61 20  mic commit of a 
2570: 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 61 69  transaction agai
2580: 6e 73 74 20 61 20 73 69 6e 67 6c 65 20 64 61 74  nst a single dat
2590: 61 62 61 73 65 0a 66 69 6c 65 2e 20 20 54 68 65  abase.file.  The
25a0: 20 64 65 74 61 69 6c 73 20 6f 66 20 66 69 6c 65   details of file
25b0: 20 66 6f 72 6d 61 74 73 20 75 73 65 64 20 74 6f   formats used to
25c0: 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 64   guard against d
25d0: 61 6d 61 67 65 20 66 72 6f 6d 0a 70 6f 77 65 72  amage from.power
25e0: 20 66 61 69 6c 75 72 65 73 20 61 6e 64 20 74 65   failures and te
25f0: 63 68 6e 69 71 75 65 73 20 66 6f 72 20 70 65 72  chniques for per
2600: 66 6f 72 6d 69 6e 67 20 61 6e 20 61 74 6f 6d 69  forming an atomi
2610: 63 20 63 6f 6d 6d 69 74 20 61 63 72 6f 73 73 0a  c commit across.
2620: 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73  multiple databas
2630: 65 73 20 61 72 65 20 64 69 73 63 75 73 73 65 64  es are discussed
2640: 20 69 6e 20 6c 61 74 65 72 20 73 65 63 74 69 6f   in later sectio
2650: 6e 73 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64  ns.</p>..<tcl>hd
2660: 5f 66 72 61 67 6d 65 6e 74 20 69 6e 69 74 73 74  _fragment initst
2670: 61 74 65 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 33 2e  ate</tcl>.<h3>3.
2680: 31 20 49 6e 69 74 69 61 6c 20 53 74 61 74 65 3c  1 Initial State<
2690: 2f 68 33 3e 0a 0a 3c 69 6d 67 20 73 72 63 3d 22  /h3>..<img src="
26a0: 69 6d 61 67 65 73 2f 61 63 2f 63 6f 6d 6d 69 74  images/ac/commit
26b0: 2d 30 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72  -0.gif" align="r
26c0: 69 67 68 74 22 20 68 73 70 61 63 65 3d 22 31 35  ight" hspace="15
26d0: 22 3e 0a 0a 3c 70 3e 54 68 65 20 73 74 61 74 65  ">..<p>The state
26e0: 20 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 65 72   of the computer
26f0: 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
2700: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 0a 66   connection is.f
2710: 69 72 73 74 20 6f 70 65 6e 65 64 20 69 73 20 73  irst opened is s
2720: 68 6f 77 6e 20 63 6f 6e 63 65 70 74 75 61 6c 6c  hown conceptuall
2730: 79 20 62 79 20 74 68 65 20 64 69 61 67 72 61 6d  y by the diagram
2740: 20 61 74 20 74 68 65 0a 72 69 67 68 74 2e 0a 54   at the.right..T
2750: 68 65 20 61 72 65 61 20 6f 66 20 74 68 65 20 64  he area of the d
2760: 69 61 67 72 61 6d 20 6f 6e 20 74 68 65 20 65 78  iagram on the ex
2770: 74 72 65 6d 65 20 72 69 67 68 74 20 28 6c 61 62  treme right (lab
2780: 65 6c 65 64 20 22 44 69 73 6b 22 29 20 72 65 70  eled "Disk") rep
2790: 72 65 73 65 6e 74 73 0a 69 6e 66 6f 72 6d 61 74  resents.informat
27a0: 69 6f 6e 20 73 74 6f 72 65 64 20 6f 6e 20 74 68  ion stored on th
27b0: 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 20 64  e mass storage d
27c0: 65 76 69 63 65 2e 20 20 45 61 63 68 20 72 65 63  evice.  Each rec
27d0: 74 61 6e 67 6c 65 20 69 73 0a 61 20 73 65 63 74  tangle is.a sect
27e0: 6f 72 2e 20 20 54 68 65 20 62 6c 75 65 20 63 6f  or.  The blue co
27f0: 6c 6f 72 20 72 65 70 72 65 73 65 6e 74 73 20 74  lor represents t
2800: 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 73 20  hat the sectors 
2810: 63 6f 6e 74 61 69 6e 0a 6f 72 69 67 69 6e 61 6c  contain.original
2820: 20 64 61 74 61 2e 0a 54 68 65 20 6d 69 64 64 6c   data..The middl
2830: 65 20 61 72 65 61 20 69 73 20 74 68 65 20 6f 70  e area is the op
2840: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
2850: 64 69 73 6b 20 63 61 63 68 65 2e 20 20 41 74 20  disk cache.  At 
2860: 74 68 65 0a 6f 6e 73 65 74 20 6f 66 20 6f 75 72  the.onset of our
2870: 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 63 61   example, the ca
2880: 63 68 65 20 69 73 20 63 6f 6c 64 20 61 6e 64 20  che is cold and 
2890: 74 68 69 73 20 69 73 20 72 65 70 72 65 73 65 6e  this is represen
28a0: 74 65 64 0a 62 79 20 6c 65 61 76 69 6e 67 20 74  ted.by leaving t
28b0: 68 65 20 72 65 63 74 61 6e 67 6c 65 73 20 6f 66  he rectangles of
28c0: 20 74 68 65 20 64 69 73 6b 20 63 61 63 68 65 20   the disk cache 
28d0: 65 6d 70 74 79 2e 0a 54 68 65 20 6c 65 66 74 20  empty..The left 
28e0: 61 72 65 61 20 6f 66 20 74 68 65 20 64 69 61 67  area of the diag
28f0: 72 61 6d 20 73 68 6f 77 73 20 74 68 65 20 63 6f  ram shows the co
2900: 6e 74 65 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  ntent of memory 
2910: 66 6f 72 0a 74 68 65 20 70 72 6f 63 65 73 73 20  for.the process 
2920: 74 68 61 74 20 69 73 20 75 73 69 6e 67 20 53 51  that is using SQ
2930: 4c 69 74 65 2e 20 20 54 68 65 20 64 61 74 61 62  Lite.  The datab
2940: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ase connection h
2950: 61 73 0a 6a 75 73 74 20 62 65 65 6e 20 6f 70 65  as.just been ope
2960: 6e 65 64 20 61 6e 64 20 6e 6f 20 69 6e 66 6f 72  ned and no infor
2970: 6d 61 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20  mation has been 
2980: 72 65 61 64 20 79 65 74 2c 20 73 6f 20 74 68 65  read yet, so the
2990: 0a 75 73 65 72 20 73 70 61 63 65 20 69 73 20 65  .user space is e
29a0: 6d 70 74 79 2e 0a 3c 2f 70 3e 0a 3c 62 72 20 63  mpty..</p>.<br c
29b0: 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a 0a 3c 74  lear="both">..<t
29c0: 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 72  cl>hd_fragment r
29d0: 64 6c 63 6b 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 33  dlck</tcl>.<h3>3
29e0: 2e 32 20 41 63 71 75 69 72 69 6e 67 20 41 20 52  .2 Acquiring A R
29f0: 65 61 64 20 4c 6f 63 6b 3c 2f 68 33 3e 0a 0a 3c  ead Lock</h3>..<
2a00: 69 6d 67 20 73 72 63 3d 22 69 6d 61 67 65 73 2f  img src="images/
2a10: 61 63 2f 63 6f 6d 6d 69 74 2d 31 2e 67 69 66 22  ac/commit-1.gif"
2a20: 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 20 68   align="right" h
2a30: 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70 3e  space="15">..<p>
2a40: 42 65 66 6f 72 65 20 53 51 4c 69 74 65 20 63 61  Before SQLite ca
2a50: 6e 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74  n write to a dat
2a60: 61 62 61 73 65 2c 20 69 74 20 6d 75 73 74 20 66  abase, it must f
2a70: 69 72 73 74 20 72 65 61 64 0a 74 68 65 20 64 61  irst read.the da
2a80: 74 61 62 61 73 65 20 74 6f 20 73 65 65 20 77 68  tabase to see wh
2a90: 61 74 20 69 73 20 74 68 65 72 65 20 61 6c 72 65  at is there alre
2aa0: 61 64 79 2e 20 20 45 76 65 6e 20 69 66 20 69 74  ady.  Even if it
2ab0: 20 69 73 20 6a 75 73 74 0a 61 70 70 65 6e 64 69   is just.appendi
2ac0: 6e 67 20 6e 65 77 20 64 61 74 61 2c 20 53 51 4c  ng new data, SQL
2ad0: 69 74 65 20 73 74 69 6c 6c 20 68 61 73 20 74 6f  ite still has to
2ae0: 20 72 65 61 64 20 69 6e 20 74 68 65 20 64 61 74   read in the dat
2af0: 61 62 61 73 65 0a 73 63 68 65 6d 61 20 66 72 6f  abase.schema fro
2b00: 6d 20 74 68 65 20 3c 62 3e 73 71 6c 69 74 65 5f  m the <b>sqlite_
2b10: 6d 61 73 74 65 72 3c 2f 62 3e 20 74 61 62 6c 65  master</b> table
2b20: 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20   so that it can 
2b30: 6b 6e 6f 77 0a 68 6f 77 20 74 6f 20 70 61 72 73  know.how to pars
2b40: 65 20 74 68 65 20 49 4e 53 45 52 54 20 73 74 61  e the INSERT sta
2b50: 74 65 6d 65 6e 74 73 20 61 6e 64 20 64 69 73 63  tements and disc
2b60: 6f 76 65 72 20 77 68 65 72 65 20 69 6e 20 74 68  over where in th
2b70: 65 0a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e.database file 
2b80: 74 68 65 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74  the new informat
2b90: 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 73 74  ion should be st
2ba0: 6f 72 65 64 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68  ored.</p>..<p>Th
2bb0: 65 20 66 69 72 73 74 20 73 74 65 70 20 74 6f 77  e first step tow
2bc0: 61 72 64 20 72 65 61 64 69 6e 67 20 66 72 6f 6d  ard reading from
2bd0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
2be0: 6c 65 0a 69 73 20 6f 62 74 61 69 6e 69 6e 67 20  le.is obtaining 
2bf0: 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e  a shared lock on
2c00: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
2c10: 6c 65 2e 20 20 41 20 22 73 68 61 72 65 64 22 0a  le.  A "shared".
2c20: 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 74 77 6f 20  lock allows two 
2c30: 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65  or more database
2c40: 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20   connections to 
2c50: 72 65 61 64 20 66 72 6f 6d 20 74 68 65 0a 64 61  read from the.da
2c60: 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74  tabase file at t
2c70: 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 42  he same time.  B
2c80: 75 74 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  ut a shared lock
2c90: 20 70 72 65 76 65 6e 74 73 0a 61 6e 6f 74 68 65   prevents.anothe
2ca0: 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
2cb0: 63 74 69 6f 6e 20 66 72 6f 6d 20 77 72 69 74 69  ction from writi
2cc0: 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
2cd0: 73 65 20 66 69 6c 65 0a 77 68 69 6c 65 20 77 65  se file.while we
2ce0: 20 61 72 65 20 72 65 61 64 69 6e 67 20 69 74 2e   are reading it.
2cf0: 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73    This is necess
2d00: 61 72 79 20 62 65 63 61 75 73 65 20 69 66 20 61  ary because if a
2d10: 6e 6f 74 68 65 72 0a 64 61 74 61 62 61 73 65 20  nother.database 
2d20: 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 65 72 65 20  connection were 
2d30: 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64  writing to the d
2d40: 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20  atabase file at 
2d50: 74 68 65 0a 73 61 6d 65 20 74 69 6d 65 20 77 65  the.same time we
2d60: 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f   are reading fro
2d70: 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
2d80: 69 6c 65 2c 20 77 65 20 6d 69 67 68 74 20 72 65  ile, we might re
2d90: 61 64 0a 73 6f 6d 65 20 64 61 74 61 20 62 65 66  ad.some data bef
2da0: 6f 72 65 20 74 68 65 20 63 68 61 6e 67 65 20 61  ore the change a
2db0: 6e 64 20 6f 74 68 65 72 20 64 61 74 61 20 61 66  nd other data af
2dc0: 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 2e 0a  ter the change..
2dd0: 54 68 69 73 20 77 6f 75 6c 64 20 6d 61 6b 65 20  This would make 
2de0: 69 74 20 61 70 70 65 61 72 20 61 73 20 69 66 20  it appear as if 
2df0: 74 68 65 20 63 68 61 6e 67 65 20 6d 61 64 65 20  the change made 
2e00: 62 79 20 74 68 65 20 6f 74 68 65 72 0a 70 72 6f  by the other.pro
2e10: 63 65 73 73 20 69 73 20 6e 6f 74 20 61 74 6f 6d  cess is not atom
2e20: 69 63 2e 3c 2f 70 3e 0a 0a 3c 70 3e 4e 6f 74 69  ic.</p>..<p>Noti
2e30: 63 65 20 74 68 61 74 20 74 68 65 20 73 68 61 72  ce that the shar
2e40: 65 64 20 6c 6f 63 6b 20 69 73 20 6f 6e 20 74 68  ed lock is on th
2e50: 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
2e60: 65 6d 0a 64 69 73 6b 20 63 61 63 68 65 2c 20 6e  em.disk cache, n
2e70: 6f 74 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 69  ot on the disk i
2e80: 74 73 65 6c 66 2e 20 20 46 69 6c 65 20 6c 6f 63  tself.  File loc
2e90: 6b 73 0a 72 65 61 6c 6c 79 20 61 72 65 20 6a 75  ks.really are ju
2ea0: 73 74 20 66 6c 61 67 73 20 77 69 74 68 69 6e 20  st flags within 
2eb0: 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
2ec0: 73 74 65 6d 20 6b 65 72 6e 65 6c 2c 0a 75 73 75  stem kernel,.usu
2ed0: 61 6c 6c 79 2e 20 20 28 54 68 65 20 64 65 74 61  ally.  (The deta
2ee0: 69 6c 73 20 64 65 70 65 6e 64 20 6f 6e 20 74 68  ils depend on th
2ef0: 65 20 73 70 65 63 69 66 69 63 20 4f 53 20 6c 61  e specific OS la
2f00: 79 65 72 0a 69 6e 74 65 72 66 61 63 65 2e 29 20  yer.interface.) 
2f10: 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b   Hence, the lock
2f20: 20 77 69 6c 6c 20 69 6e 73 74 61 6e 74 6c 79 20   will instantly 
2f30: 76 61 6e 69 73 68 20 69 66 20 74 68 65 0a 6f 70  vanish if the.op
2f40: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63  erating system c
2f50: 72 61 73 68 65 73 20 6f 72 20 69 66 20 74 68 65  rashes or if the
2f60: 72 65 20 69 73 20 61 20 70 6f 77 65 72 20 6c 6f  re is a power lo
2f70: 73 73 2e 20 20 49 74 0a 69 73 20 75 73 75 61 6c  ss.  It.is usual
2f80: 6c 79 20 61 6c 73 6f 20 74 68 65 20 63 61 73 65  ly also the case
2f90: 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 77   that the lock w
2fa0: 69 6c 6c 20 76 61 6e 69 73 68 20 69 66 20 74 68  ill vanish if th
2fb0: 65 0a 70 72 6f 63 65 73 73 20 74 68 61 74 20 63  e.process that c
2fc0: 72 65 61 74 65 64 20 74 68 65 20 6c 6f 63 6b 20  reated the lock 
2fd0: 65 78 69 74 73 2e 3c 2f 70 3e 0a 0a 3c 62 72 20  exits.</p>..<br 
2fe0: 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a 0a 3c  clear="both">..<
2ff0: 61 20 6e 61 6d 65 3d 22 73 65 63 74 69 6f 6e 5f  a name="section_
3000: 33 5f 33 22 3e 3c 2f 61 3e 0a 3c 68 33 3e 33 2e  3_3"></a>.<h3>3.
3010: 33 20 52 65 61 64 69 6e 67 20 49 6e 66 6f 72 6d  3 Reading Inform
3020: 61 74 69 6f 6e 20 4f 75 74 20 4f 66 20 54 68 65  ation Out Of The
3030: 20 44 61 74 61 62 61 73 65 3c 2f 68 33 3e 0a 0a   Database</h3>..
3040: 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67 65 73  <img src="images
3050: 2f 61 63 2f 63 6f 6d 6d 69 74 2d 32 2e 67 69 66  /ac/commit-2.gif
3060: 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 20  " align="right" 
3070: 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70  hspace="15">..<p
3080: 3e 41 66 74 65 72 20 74 68 65 20 73 68 61 72 65  >After the share
3090: 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  d lock is acquir
30a0: 65 64 2c 20 77 65 20 63 61 6e 20 62 65 67 69 6e  ed, we can begin
30b0: 20 72 65 61 64 69 6e 67 0a 69 6e 66 6f 72 6d 61   reading.informa
30c0: 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 61  tion from the da
30d0: 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 6e  tabase file.  In
30e0: 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c 20   this scenario, 
30f0: 77 65 0a 61 72 65 20 61 73 73 75 6d 69 6e 67 20  we.are assuming 
3100: 61 20 63 6f 6c 64 20 63 61 63 68 65 2c 20 73 6f  a cold cache, so
3110: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 75 73   information mus
3120: 74 20 66 69 72 73 74 20 62 65 0a 72 65 61 64 20  t first be.read 
3130: 66 72 6f 6d 20 6d 61 73 73 20 73 74 6f 72 61 67  from mass storag
3140: 65 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 72 61  e into the opera
3150: 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 63 68  ting system cach
3160: 65 20 74 68 65 6e 0a 74 72 61 6e 73 66 65 72 72  e then.transferr
3170: 65 64 20 66 72 6f 6d 20 6f 70 65 72 61 74 69 6e  ed from operatin
3180: 67 20 73 79 73 74 65 6d 20 63 61 63 68 65 20 69  g system cache i
3190: 6e 74 6f 20 75 73 65 72 20 73 70 61 63 65 2e 0a  nto user space..
31a0: 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 72 65  On subsequent re
31b0: 61 64 73 2c 20 73 6f 6d 65 20 6f 72 20 61 6c 6c  ads, some or all
31c0: 20 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   of the informat
31d0: 69 6f 6e 20 6d 69 67 68 74 0a 61 6c 72 65 61 64  ion might.alread
31e0: 79 20 62 65 20 66 6f 75 6e 64 20 69 6e 20 74 68  y be found in th
31f0: 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
3200: 65 6d 20 63 61 63 68 65 20 61 6e 64 20 73 6f 20  em cache and so 
3210: 6f 6e 6c 79 0a 74 68 65 20 74 72 61 6e 73 66 65  only.the transfe
3220: 72 20 74 6f 20 75 73 65 72 20 73 70 61 63 65 20  r to user space 
3230: 77 6f 75 6c 64 20 62 65 20 72 65 71 75 69 72 65  would be require
3240: 64 2e 3c 2f 70 3e 0a 0a 3c 70 3e 55 73 75 61 6c  d.</p>..<p>Usual
3250: 6c 79 20 6f 6e 6c 79 20 61 20 73 75 62 73 65 74  ly only a subset
3260: 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e   of the pages in
3270: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
3280: 6c 65 0a 61 72 65 20 72 65 61 64 2e 20 20 49 6e  le.are read.  In
3290: 20 74 68 69 73 20 65 78 61 6d 70 6c 65 20 77 65   this example we
32a0: 20 61 72 65 20 73 68 6f 77 69 6e 67 20 74 68 72   are showing thr
32b0: 65 65 0a 70 61 67 65 73 20 6f 75 74 20 6f 66 20  ee.pages out of 
32c0: 65 69 67 68 74 20 62 65 69 6e 67 20 72 65 61 64  eight being read
32d0: 2e 20 20 49 6e 20 61 20 74 79 70 69 63 61 6c 20  .  In a typical 
32e0: 61 70 70 6c 69 63 61 74 69 6f 6e 2c 20 61 0a 64  application, a.d
32f0: 61 74 61 62 61 73 65 20 77 69 6c 6c 20 68 61 76  atabase will hav
3300: 65 20 74 68 6f 75 73 61 6e 64 73 20 6f 66 20 70  e thousands of p
3310: 61 67 65 73 20 61 6e 64 20 61 20 71 75 65 72 79  ages and a query
3320: 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 0a 6f   will normally.o
3330: 6e 6c 79 20 74 6f 75 63 68 20 61 20 73 6d 61 6c  nly touch a smal
3340: 6c 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66 20  l percentage of 
3350: 74 68 6f 73 65 20 70 61 67 65 73 2e 3c 2f 70 3e  those pages.</p>
3360: 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22 62 6f 74  ..<br clear="bot
3370: 68 22 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61  h">..<tcl>hd_fra
3380: 67 6d 65 6e 74 20 72 73 76 64 6c 6f 63 6b 3c 2f  gment rsvdlock</
3390: 74 63 6c 3e 0a 3c 68 33 3e 33 2e 34 20 4f 62 74  tcl>.<h3>3.4 Obt
33a0: 61 69 6e 69 6e 67 20 41 20 52 65 73 65 72 76 65  aining A Reserve
33b0: 64 20 4c 6f 63 6b 3c 2f 68 33 3e 0a 0a 3c 69 6d  d Lock</h3>..<im
33c0: 67 20 73 72 63 3d 22 69 6d 61 67 65 73 2f 61 63  g src="images/ac
33d0: 2f 63 6f 6d 6d 69 74 2d 33 2e 67 69 66 22 20 61  /commit-3.gif" a
33e0: 6c 69 67 6e 3d 22 72 69 67 68 74 22 20 68 73 70  lign="right" hsp
33f0: 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70 3e 42 65  ace="15">..<p>Be
3400: 66 6f 72 65 20 6d 61 6b 69 6e 67 20 63 68 61 6e  fore making chan
3410: 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  ges to the datab
3420: 61 73 65 2c 20 53 51 4c 69 74 65 20 66 69 72 73  ase, SQLite firs
3430: 74 0a 6f 62 74 61 69 6e 73 20 61 20 22 72 65 73  t.obtains a "res
3440: 65 72 76 65 64 22 20 6c 6f 63 6b 20 6f 6e 20 74  erved" lock on t
3450: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
3460: 2e 20 20 41 20 72 65 73 65 72 76 65 64 0a 6c 6f  .  A reserved.lo
3470: 63 6b 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f  ck is similar to
3480: 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69   a shared lock i
3490: 6e 20 74 68 61 74 20 62 6f 74 68 20 61 20 72 65  n that both a re
34a0: 73 65 72 76 65 64 20 6c 6f 63 6b 0a 61 6e 64 20  served lock.and 
34b0: 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6c 6c 6f  shared lock allo
34c0: 77 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65  w other processe
34d0: 73 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74  s to read from t
34e0: 68 65 20 64 61 74 61 62 61 73 65 0a 66 69 6c 65  he database.file
34f0: 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 65 73 65  .  A single rese
3500: 72 76 65 20 6c 6f 63 6b 20 63 61 6e 20 63 6f 65  rve lock can coe
3510: 78 69 73 74 20 77 69 74 68 20 6d 75 6c 74 69 70  xist with multip
3520: 6c 65 20 73 68 61 72 65 64 0a 6c 6f 63 6b 73 20  le shared.locks 
3530: 66 72 6f 6d 20 6f 74 68 65 72 20 70 72 6f 63 65  from other proce
3540: 73 73 65 73 2e 20 20 48 6f 77 65 76 65 72 2c 20  sses.  However, 
3550: 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62  there can only b
3560: 65 20 61 0a 73 69 6e 67 6c 65 20 72 65 73 65 72  e a.single reser
3570: 76 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ved lock on the 
3580: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
3590: 48 65 6e 63 65 20 6f 6e 6c 79 20 61 0a 73 69 6e  Hence only a.sin
35a0: 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20  gle process can 
35b0: 62 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  be attempting to
35c0: 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 61   write to the da
35d0: 74 61 62 61 73 65 0a 61 74 20 6f 6e 65 20 74 69  tabase.at one ti
35e0: 6d 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65 20  me.</p>..<p>The 
35f0: 69 64 65 61 20 62 65 68 69 6e 64 20 61 20 72 65  idea behind a re
3600: 73 65 72 76 65 64 20 6c 6f 63 6b 20 69 73 20 74  served lock is t
3610: 68 61 74 20 69 74 20 73 69 67 6e 61 6c 73 20 74  hat it signals t
3620: 68 61 74 0a 61 20 70 72 6f 63 65 73 73 20 69 6e  hat.a process in
3630: 74 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66 79 20  tends to modify 
3640: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
3650: 65 20 69 6e 20 74 68 65 20 6e 65 61 72 0a 66 75  e in the near.fu
3660: 74 75 72 65 20 62 75 74 20 68 61 73 20 6e 6f 74  ture but has not
3670: 20 79 65 74 20 73 74 61 72 74 65 64 20 74 6f 20   yet started to 
3680: 6d 61 6b 65 20 74 68 65 20 6d 6f 64 69 66 69 63  make the modific
3690: 61 74 69 6f 6e 73 2e 0a 41 6e 64 20 62 65 63 61  ations..And beca
36a0: 75 73 65 20 74 68 65 20 6d 6f 64 69 66 69 63 61  use the modifica
36b0: 74 69 6f 6e 73 20 68 61 76 65 20 6e 6f 74 20 79  tions have not y
36c0: 65 74 20 73 74 61 72 74 65 64 2c 20 6f 74 68 65  et started, othe
36d0: 72 0a 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20  r.processes can 
36e0: 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 61 64  continue to read
36f0: 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
3700: 73 65 2e 20 20 48 6f 77 65 76 65 72 2c 0a 6e 6f  se.  However,.no
3710: 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 73   other process s
3720: 68 6f 75 6c 64 20 61 6c 73 6f 20 62 65 67 69 6e  hould also begin
3730: 20 74 72 79 69 6e 67 20 74 6f 20 77 72 69 74 65   trying to write
3740: 20 74 6f 20 74 68 65 0a 64 61 74 61 62 61 73 65   to the.database
3750: 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72  .</p>..<br clear
3760: 3d 22 62 6f 74 68 22 3e 0a 3c 61 20 6e 61 6d 65  ="both">.<a name
3770: 3d 22 73 65 63 74 69 6f 6e 5f 33 5f 35 22 3e 3c  ="section_3_5"><
3780: 2f 61 3e 0a 3c 68 33 3e 33 2e 35 20 43 72 65 61  /a>.<h3>3.5 Crea
3790: 74 69 6e 67 20 41 20 52 6f 6c 6c 62 61 63 6b 20  ting A Rollback 
37a0: 4a 6f 75 72 6e 61 6c 20 46 69 6c 65 3c 2f 68 33  Journal File</h3
37b0: 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67  >.<img src="imag
37c0: 65 73 2f 61 63 2f 63 6f 6d 6d 69 74 2d 34 2e 67  es/ac/commit-4.g
37d0: 69 66 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  if" align="right
37e0: 22 20 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a  " hspace="15">..
37f0: 3c 70 3e 50 72 69 6f 72 20 74 6f 20 6d 61 6b 69  <p>Prior to maki
3800: 6e 67 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74  ng any changes t
3810: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
3820: 69 6c 65 2c 20 53 51 4c 69 74 65 20 66 69 72 73  ile, SQLite firs
3830: 74 0a 63 72 65 61 74 65 73 20 61 20 73 65 70 61  t.creates a sepa
3840: 72 61 74 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  rate rollback jo
3850: 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 77  urnal file and w
3860: 72 69 74 65 73 20 69 6e 74 6f 20 74 68 65 20 0a  rites into the .
3870: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
3880: 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 63 6f   the original.co
3890: 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 64 61 74  ntent of the dat
38a0: 61 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74  abase pages that
38b0: 20 61 72 65 20 74 6f 20 62 65 20 61 6c 74 65 72   are to be alter
38c0: 65 64 2e 0a 54 68 65 20 69 64 65 61 20 62 65 68  ed..The idea beh
38d0: 69 6e 64 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  ind the rollback
38e0: 20 6a 6f 75 72 6e 61 6c 20 69 73 20 74 68 61 74   journal is that
38f0: 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 61 6c 6c   it contains.all
3900: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65 65   information nee
3910: 64 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74  ded to restore t
3920: 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b  he database back
3930: 20 74 6f 20 0a 69 74 73 20 6f 72 69 67 69 6e 61   to .its origina
3940: 6c 20 73 74 61 74 65 2e 3c 2f 70 3e 0a 0a 3c 70  l state.</p>..<p
3950: 3e 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  >The rollback jo
3960: 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61  urnal contains a
3970: 20 73 6d 61 6c 6c 20 68 65 61 64 65 72 20 28 73   small header (s
3980: 68 6f 77 6e 20 69 6e 20 67 72 65 65 6e 0a 69 6e  hown in green.in
3990: 20 74 68 65 20 64 69 61 67 72 61 6d 29 20 74 68   the diagram) th
39a0: 61 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6f  at records the o
39b0: 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6f 66 20  riginal size of 
39c0: 74 68 65 20 64 61 74 61 62 61 73 65 0a 66 69 6c  the database.fil
39d0: 65 2e 20 20 53 6f 20 69 66 20 61 20 63 68 61 6e  e.  So if a chan
39e0: 67 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61  ge causes the da
39f0: 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67  tabase file to g
3a00: 72 6f 77 2c 20 77 65 0a 77 69 6c 6c 20 73 74 69  row, we.will sti
3a10: 6c 6c 20 6b 6e 6f 77 20 74 68 65 20 6f 72 69 67  ll know the orig
3a20: 69 6e 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65  inal size of the
3a30: 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
3a40: 70 61 67 65 0a 6e 75 6d 62 65 72 20 69 73 20 73  page.number is s
3a50: 74 6f 72 65 64 20 74 6f 67 65 74 68 65 72 20 77  tored together w
3a60: 69 74 68 20 65 61 63 68 20 64 61 74 61 62 61 73  ith each databas
3a70: 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20 0a  e page that is .
3a80: 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
3a90: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
3aa0: 6c 2e 3c 2f 70 3e 0a 0a 3c 70 3e 0a 20 20 57 68  l.</p>..<p>.  Wh
3ab0: 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 20 69 73  en a new file is
3ac0: 20 63 72 65 61 74 65 64 2c 20 6d 6f 73 74 20 64   created, most d
3ad0: 65 73 6b 74 6f 70 20 6f 70 65 72 61 74 69 6e 67  esktop operating
3ae0: 20 73 79 73 74 65 6d 73 0a 20 20 28 57 69 6e 64   systems.  (Wind
3af0: 6f 77 73 2c 20 4c 69 6e 75 78 2c 20 4d 61 63 20  ows, Linux, Mac 
3b00: 4f 53 20 58 29 20 77 69 6c 6c 20 6e 6f 74 20 61  OS X) will not a
3b10: 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 61 6e  ctually write an
3b20: 79 74 68 69 6e 67 20 74 6f 0a 20 20 64 69 73 6b  ything to.  disk
3b30: 2e 20 20 54 68 65 20 6e 65 77 20 66 69 6c 65 20  .  The new file 
3b40: 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  is created in th
3b50: 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
3b60: 65 6d 73 20 64 69 73 6b 0a 20 20 63 61 63 68 65  ems disk.  cache
3b70: 20 6f 6e 6c 79 2e 20 20 54 68 65 20 66 69 6c 65   only.  The file
3b80: 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20   is not created 
3b90: 6f 6e 20 6d 61 73 73 20 73 74 6f 72 61 67 65 20  on mass storage 
3ba0: 75 6e 74 69 6c 20 73 6f 6d 65 74 69 6d 65 0a 20  until sometime. 
3bb0: 20 6c 61 74 65 72 2c 20 77 68 65 6e 20 74 68 65   later, when the
3bc0: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
3bd0: 6d 20 68 61 73 20 61 20 73 70 61 72 65 20 6d 6f  m has a spare mo
3be0: 6d 65 6e 74 2e 20 20 54 68 69 73 20 63 72 65 61  ment.  This crea
3bf0: 74 65 73 0a 20 20 74 68 65 20 69 6d 70 72 65 73  tes.  the impres
3c00: 73 69 6f 6e 20 74 6f 20 75 73 65 72 73 20 74 68  sion to users th
3c10: 61 74 20 49 2f 4f 20 69 73 20 68 61 70 70 65 6e  at I/O is happen
3c20: 69 6e 67 20 6d 75 63 68 20 66 61 73 74 65 72 20  ing much faster 
3c30: 74 68 61 6e 0a 20 20 69 73 20 70 6f 73 73 69 62  than.  is possib
3c40: 6c 65 20 77 68 65 6e 20 64 6f 69 6e 67 20 72 65  le when doing re
3c50: 61 6c 20 64 69 73 6b 20 49 2f 4f 2e 20 20 57 65  al disk I/O.  We
3c60: 20 69 6c 6c 75 73 74 72 61 74 65 20 74 68 69 73   illustrate this
3c70: 20 69 64 65 61 20 69 6e 0a 20 20 74 68 65 20 64   idea in.  the d
3c80: 69 61 67 72 61 6d 20 74 6f 20 74 68 65 20 72 69  iagram to the ri
3c90: 67 68 74 20 62 79 20 73 68 6f 77 69 6e 67 20 74  ght by showing t
3ca0: 68 61 74 20 74 68 65 20 6e 65 77 20 72 6f 6c 6c  hat the new roll
3cb0: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 20 20 61  back journal.  a
3cc0: 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 6f 70  ppears in the op
3cd0: 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
3ce0: 69 73 6b 20 63 61 63 68 65 20 6f 6e 6c 79 20 61  isk cache only a
3cf0: 6e 64 20 6e 6f 74 20 6f 6e 20 74 68 65 0a 20 20  nd not on the.  
3d00: 64 69 73 6b 20 69 74 73 65 6c 66 2e 3c 2f 70 3e  disk itself.</p>
3d10: 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22 62 6f 74  ..<br clear="bot
3d20: 68 22 3e 0a 3c 61 20 6e 61 6d 65 3d 22 73 65 63  h">.<a name="sec
3d30: 74 69 6f 6e 5f 33 5f 36 22 3e 3c 2f 61 3e 0a 3c  tion_3_6"></a>.<
3d40: 68 33 3e 33 2e 36 20 43 68 61 6e 67 69 6e 67 20  h3>3.6 Changing 
3d50: 44 61 74 61 62 61 73 65 20 50 61 67 65 73 20 49  Database Pages I
3d60: 6e 20 55 73 65 72 20 53 70 61 63 65 3c 2f 68 33  n User Space</h3
3d70: 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67  >.<img src="imag
3d80: 65 73 2f 61 63 2f 63 6f 6d 6d 69 74 2d 35 2e 67  es/ac/commit-5.g
3d90: 69 66 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  if" align="right
3da0: 22 20 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a  " hspace="15">..
3db0: 3c 70 3e 41 66 74 65 72 20 74 68 65 20 6f 72 69  <p>After the ori
3dc0: 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65  ginal page conte
3dd0: 6e 74 20 68 61 73 20 62 65 65 6e 20 73 61 76 65  nt has been save
3de0: 64 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  d in the rollbac
3df0: 6b 0a 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 70  k.journal, the p
3e00: 61 67 65 73 20 63 61 6e 20 62 65 20 6d 6f 64 69  ages can be modi
3e10: 66 69 65 64 20 69 6e 20 75 73 65 72 20 6d 65 6d  fied in user mem
3e20: 6f 72 79 2e 20 20 45 61 63 68 20 64 61 74 61 62  ory.  Each datab
3e30: 61 73 65 0a 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  ase.connection h
3e40: 61 73 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61  as its own priva
3e50: 74 65 20 63 6f 70 79 20 6f 66 20 75 73 65 72 20  te copy of user 
3e60: 73 70 61 63 65 2c 20 73 6f 20 74 68 65 20 63 68  space, so the ch
3e70: 61 6e 67 65 73 0a 74 68 61 74 20 61 72 65 20 6d  anges.that are m
3e80: 61 64 65 20 69 6e 20 75 73 65 72 20 73 70 61 63  ade in user spac
3e90: 65 20 61 72 65 20 6f 6e 6c 79 20 76 69 73 69 62  e are only visib
3ea0: 6c 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  le to the databa
3eb0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 74 68  se connection.th
3ec0: 61 74 20 69 73 20 6d 61 6b 69 6e 67 20 74 68 65  at is making the
3ed0: 20 63 68 61 6e 67 65 73 2e 20 20 4f 74 68 65 72   changes.  Other
3ee0: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3ef0: 74 69 6f 6e 73 20 73 74 69 6c 6c 20 73 65 65 0a  tions still see.
3f00: 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
3f10: 69 6e 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  in operating sys
3f20: 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 20 62  tem disk cache b
3f30: 75 66 66 65 72 73 20 77 68 69 63 68 20 68 61 76  uffers which hav
3f40: 65 0a 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  e.not yet been c
3f50: 68 61 6e 67 65 64 2e 20 20 41 6e 64 20 73 6f 20  hanged.  And so 
3f60: 65 76 65 6e 20 74 68 6f 75 67 68 20 6f 6e 65 20  even though one 
3f70: 70 72 6f 63 65 73 73 20 69 73 20 62 75 73 79 0a  process is busy.
3f80: 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64 61  modifying the da
3f90: 74 61 62 61 73 65 2c 20 6f 74 68 65 72 20 70 72  tabase, other pr
3fa0: 6f 63 65 73 73 65 73 20 63 61 6e 20 63 6f 6e 74  ocesses can cont
3fb0: 69 6e 75 65 20 74 6f 20 72 65 61 64 20 74 68 65  inue to read the
3fc0: 69 72 0a 6f 77 6e 20 63 6f 70 69 65 73 20 6f 66  ir.own copies of
3fd0: 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
3fe0: 74 61 62 61 73 65 20 63 6f 6e 74 65 6e 74 2e 3c  tabase content.<
3ff0: 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22  /p>..<br clear="
4000: 62 6f 74 68 22 3e 0a 3c 61 20 6e 61 6d 65 3d 22  both">.<a name="
4010: 73 65 63 74 69 6f 6e 5f 33 5f 37 22 3e 3c 2f 61  section_3_7"></a
4020: 3e 0a 3c 68 33 3e 33 2e 37 20 46 6c 75 73 68 69  >.<h3>3.7 Flushi
4030: 6e 67 20 54 68 65 20 52 6f 6c 6c 62 61 63 6b 20  ng The Rollback 
4040: 4a 6f 75 72 6e 61 6c 20 46 69 6c 65 20 54 6f 20  Journal File To 
4050: 4d 61 73 73 20 53 74 6f 72 61 67 65 3c 2f 68 33  Mass Storage</h3
4060: 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67  >.<img src="imag
4070: 65 73 2f 61 63 2f 63 6f 6d 6d 69 74 2d 36 2e 67  es/ac/commit-6.g
4080: 69 66 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74  if" align="right
4090: 22 20 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a  " hspace="15">..
40a0: 3c 70 3e 54 68 65 20 6e 65 78 74 20 73 74 65 70  <p>The next step
40b0: 20 69 73 20 74 6f 20 66 6c 75 73 68 20 74 68 65   is to flush the
40c0: 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
40d0: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
40e0: 0a 66 69 6c 65 20 74 6f 20 6e 6f 6e 76 6f 6c 61  .file to nonvola
40f0: 74 69 6c 65 20 73 74 6f 72 61 67 65 2e 0a 41 73  tile storage..As
4100: 20 77 65 20 77 69 6c 6c 20 73 65 65 20 6c 61 74   we will see lat
4110: 65 72 2c 20 0a 74 68 69 73 20 69 73 20 61 20 63  er, .this is a c
4120: 72 69 74 69 63 61 6c 20 73 74 65 70 20 69 6e 20  ritical step in 
4130: 69 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68  insuring that th
4140: 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 73  e database can s
4150: 75 72 76 69 76 65 0a 61 6e 20 75 6e 65 78 70 65  urvive.an unexpe
4160: 63 74 65 64 20 70 6f 77 65 72 20 6c 6f 73 73 2e  cted power loss.
4170: 0a 54 68 69 73 20 73 74 65 70 20 61 6c 73 6f 20  .This step also 
4180: 74 61 6b 65 73 20 61 20 6c 6f 74 20 6f 66 20 74  takes a lot of t
4190: 69 6d 65 2c 20 73 69 6e 63 65 20 77 72 69 74 69  ime, since writi
41a0: 6e 67 20 74 6f 20 6e 6f 6e 76 6f 6c 61 74 69 6c  ng to nonvolatil
41b0: 65 0a 73 74 6f 72 61 67 65 20 69 73 20 6e 6f 72  e.storage is nor
41c0: 6d 61 6c 6c 79 20 61 20 73 6c 6f 77 20 6f 70 65  mally a slow ope
41d0: 72 61 74 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 70 3e  ration.</p>..<p>
41e0: 54 68 69 73 20 73 74 65 70 20 69 73 20 75 73 75  This step is usu
41f0: 61 6c 6c 79 20 6d 6f 72 65 20 63 6f 6d 70 6c 69  ally more compli
4200: 63 61 74 65 64 20 74 68 61 6e 20 73 69 6d 70 6c  cated than simpl
4210: 79 20 66 6c 75 73 68 69 6e 67 0a 74 68 65 20 72  y flushing.the r
4220: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
4230: 74 6f 20 74 68 65 20 64 69 73 6b 2e 20 20 4f 6e  to the disk.  On
4240: 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d 73 20   most platforms 
4250: 74 77 6f 20 73 65 70 61 72 61 74 65 0a 66 6c 75  two separate.flu
4260: 73 68 20 28 6f 72 20 66 73 79 6e 63 28 29 29 20  sh (or fsync()) 
4270: 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 72  operations are r
4280: 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 69  equired.  The fi
4290: 72 73 74 20 66 6c 75 73 68 20 77 72 69 74 65 73  rst flush writes
42a0: 0a 6f 75 74 20 74 68 65 20 62 61 73 65 20 72 6f  .out the base ro
42b0: 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 63  llback journal c
42c0: 6f 6e 74 65 6e 74 2e 20 20 54 68 65 6e 20 74 68  ontent.  Then th
42d0: 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 0a  e header of the.
42e0: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
42f0: 20 69 73 20 6d 6f 64 69 66 69 65 64 20 74 6f 20   is modified to 
4300: 73 68 6f 77 20 74 68 65 20 6e 75 6d 62 65 72 20  show the number 
4310: 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
4320: 0a 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61  .rollback journa
4330: 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 68 65 61  l.  Then the hea
4340: 64 65 72 20 69 73 20 66 6c 75 73 68 65 64 20 74  der is flushed t
4350: 6f 20 64 69 73 6b 2e 20 20 54 68 65 20 64 65 74  o disk.  The det
4360: 61 69 6c 73 0a 6f 6e 20 77 68 79 20 77 65 20 64  ails.on why we d
4370: 6f 20 74 68 69 73 20 68 65 61 64 65 72 20 6d 6f  o this header mo
4380: 64 69 66 69 63 61 74 69 6f 6e 20 61 6e 64 20 65  dification and e
4390: 78 74 72 61 20 66 6c 75 73 68 20 61 72 65 20 70  xtra flush are p
43a0: 72 6f 76 69 64 65 64 0a 69 6e 20 61 20 6c 61 74  rovided.in a lat
43b0: 65 72 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68  er section of th
43c0: 69 73 20 70 61 70 65 72 2e 3c 2f 70 3e 0a 0a 3c  is paper.</p>..<
43d0: 62 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e  br clear="both">
43e0: 0a 3c 61 20 6e 61 6d 65 3d 22 73 65 63 74 69 6f  .<a name="sectio
43f0: 6e 5f 33 5f 38 22 3e 3c 2f 61 3e 0a 3c 68 33 3e  n_3_8"></a>.<h3>
4400: 33 2e 38 20 4f 62 74 61 69 6e 69 6e 67 20 41 6e  3.8 Obtaining An
4410: 20 45 78 63 6c 75 73 69 76 65 20 4c 6f 63 6b 3c   Exclusive Lock<
4420: 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69  /h3>.<img src="i
4430: 6d 61 67 65 73 2f 61 63 2f 63 6f 6d 6d 69 74 2d  mages/ac/commit-
4440: 37 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72 69  7.gif" align="ri
4450: 67 68 74 22 20 68 73 70 61 63 65 3d 22 31 35 22  ght" hspace="15"
4460: 3e 0a 0a 3c 70 3e 50 72 69 6f 72 20 74 6f 20 6d  >..<p>Prior to m
4470: 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74 6f  aking changes to
4480: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
4490: 6c 65 20 69 74 73 65 6c 66 2c 20 77 65 20 6d 75  le itself, we mu
44a0: 73 74 0a 6f 62 74 61 69 6e 20 61 6e 20 65 78 63  st.obtain an exc
44b0: 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74  lusive lock on t
44c0: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
44d0: 2e 20 20 4f 62 74 61 69 6e 69 6e 67 20 61 6e 0a  .  Obtaining an.
44e0: 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69  exclusive lock i
44f0: 73 20 72 65 61 6c 6c 79 20 61 20 74 77 6f 2d 73  s really a two-s
4500: 74 65 70 20 70 72 6f 63 65 73 73 2e 20 20 46 69  tep process.  Fi
4510: 72 73 74 20 53 51 4c 69 74 65 20 6f 62 74 61 69  rst SQLite obtai
4520: 6e 73 0a 61 20 22 70 65 6e 64 69 6e 67 22 20 6c  ns.a "pending" l
4530: 6f 63 6b 2e 20 20 54 68 65 6e 20 69 74 20 65 73  ock.  Then it es
4540: 63 61 6c 61 74 65 73 20 74 68 65 20 70 65 6e 64  calates the pend
4550: 69 6e 67 20 6c 6f 63 6b 20 74 6f 20 61 6e 0a 65  ing lock to an.e
4560: 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 3c 2f  xclusive lock.</
4570: 70 3e 0a 0a 3c 70 3e 41 20 70 65 6e 64 69 6e 67  p>..<p>A pending
4580: 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68   lock allows oth
4590: 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 68 61  er processes tha
45a0: 74 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61  t already have a
45b0: 0a 73 68 61 72 65 64 20 6c 6f 63 6b 20 74 6f 20  .shared lock to 
45c0: 63 6f 6e 74 69 6e 75 65 20 72 65 61 64 69 6e 67  continue reading
45d0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
45e0: 6c 65 2e 20 20 42 75 74 20 69 74 0a 70 72 65 76  le.  But it.prev
45f0: 65 6e 74 73 20 6e 65 77 20 73 68 61 72 65 64 20  ents new shared 
4600: 6c 6f 63 6b 73 20 66 72 6f 6d 20 62 65 69 6e 67  locks from being
4610: 20 65 73 74 61 62 6c 69 73 68 65 64 2e 20 20 54   established.  T
4620: 68 65 20 69 64 65 61 0a 62 65 68 69 6e 64 20 61  he idea.behind a
4630: 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 69 73   pending lock is
4640: 20 74 6f 20 70 72 65 76 65 6e 74 20 77 72 69 74   to prevent writ
4650: 65 72 20 73 74 61 72 76 61 74 69 6f 6e 20 63 61  er starvation ca
4660: 75 73 65 64 0a 62 79 20 61 20 6c 61 72 67 65 20  used.by a large 
4670: 70 6f 6f 6c 20 6f 66 20 72 65 61 64 65 72 73 2e  pool of readers.
4680: 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62 65    There might be
4690: 20 64 6f 7a 65 6e 73 2c 20 65 76 65 6e 20 68 75   dozens, even hu
46a0: 6e 64 72 65 64 73 2c 0a 6f 66 20 6f 74 68 65 72  ndreds,.of other
46b0: 20 70 72 6f 63 65 73 73 65 73 20 74 72 79 69 6e   processes tryin
46c0: 67 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61  g to read the da
46d0: 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 45 61  tabase file.  Ea
46e0: 63 68 20 70 72 6f 63 65 73 73 0a 61 63 71 75 69  ch process.acqui
46f0: 72 65 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63  res a shared loc
4700: 6b 20 62 65 66 6f 72 65 20 69 74 20 73 74 61 72  k before it star
4710: 74 73 20 72 65 61 64 69 6e 67 2c 20 72 65 61 64  ts reading, read
4720: 73 20 77 68 61 74 20 69 74 0a 6e 65 65 64 73 2c  s what it.needs,
4730: 20 74 68 65 6e 20 72 65 6c 65 61 73 65 73 20 74   then releases t
4740: 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20  he shared lock. 
4750: 20 49 66 2c 20 68 6f 77 65 76 65 72 2c 20 74 68   If, however, th
4760: 65 72 65 20 61 72 65 0a 6d 61 6e 79 20 64 69 66  ere are.many dif
4770: 66 65 72 65 6e 74 20 70 72 6f 63 65 73 73 65 73  ferent processes
4780: 20 61 6c 6c 20 72 65 61 64 69 6e 67 20 66 72 6f   all reading fro
4790: 6d 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  m the same datab
47a0: 61 73 65 2c 20 69 74 0a 6d 69 67 68 74 20 68 61  ase, it.might ha
47b0: 70 70 65 6e 20 74 68 61 74 20 61 20 6e 65 77 20  ppen that a new 
47c0: 70 72 6f 63 65 73 73 20 61 6c 77 61 79 73 20 61  process always a
47d0: 63 71 75 69 72 65 73 20 69 74 73 20 73 68 61 72  cquires its shar
47e0: 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 74  ed lock before.t
47f0: 68 65 20 70 72 65 76 69 6f 75 73 20 70 72 6f 63  he previous proc
4800: 65 73 73 20 72 65 6c 65 61 73 65 73 20 69 74 73  ess releases its
4810: 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 41   shared lock.  A
4820: 6e 64 20 73 6f 20 74 68 65 72 65 20 69 73 0a 6e  nd so there is.n
4830: 65 76 65 72 20 61 6e 20 69 6e 73 74 61 6e 74 20  ever an instant 
4840: 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e  when there are n
4850: 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f  o shared locks o
4860: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 66  n the database.f
4870: 69 6c 65 20 61 6e 64 20 68 65 6e 63 65 20 74 68  ile and hence th
4880: 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e 20  ere is never an 
4890: 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20  opportunity for 
48a0: 74 68 65 20 77 72 69 74 65 72 20 74 6f 0a 73 65  the writer to.se
48b0: 69 7a 65 20 74 68 65 20 65 78 63 6c 75 73 69 76  ize the exclusiv
48c0: 65 20 6c 6f 63 6b 2e 20 20 41 20 70 65 6e 64 69  e lock.  A pendi
48d0: 6e 67 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 67  ng lock is desig
48e0: 6e 65 64 20 74 6f 20 70 72 65 76 65 6e 74 0a 74  ned to prevent.t
48f0: 68 61 74 20 63 79 63 6c 65 20 62 79 20 61 6c 6c  hat cycle by all
4900: 6f 77 69 6e 67 20 65 78 69 73 74 69 6e 67 20 73  owing existing s
4910: 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 70  hared locks to p
4920: 72 6f 63 65 65 64 20 62 75 74 0a 62 6c 6f 63 6b  roceed but.block
4930: 69 6e 67 20 6e 65 77 20 73 68 61 72 65 64 20 6c  ing new shared l
4940: 6f 63 6b 73 20 66 72 6f 6d 20 62 65 69 6e 67 20  ocks from being 
4950: 65 73 74 61 62 6c 69 73 68 65 64 2e 20 20 45 76  established.  Ev
4960: 65 6e 74 75 61 6c 6c 79 0a 61 6c 6c 20 73 68 61  entually.all sha
4970: 72 65 64 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 63  red locks will c
4980: 6c 65 61 72 20 61 6e 64 20 74 68 65 20 70 65 6e  lear and the pen
4990: 64 69 6e 67 20 6c 6f 63 6b 20 77 69 6c 6c 20 74  ding lock will t
49a0: 68 65 6e 20 62 65 0a 61 62 6c 65 20 74 6f 20 65  hen be.able to e
49b0: 73 63 61 6c 61 74 65 20 69 6e 74 6f 20 61 6e 20  scalate into an 
49c0: 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 3c  exclusive lock.<
49d0: 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22  /p>..<br clear="
49e0: 62 6f 74 68 22 3e 0a 3c 61 20 6e 61 6d 65 3d 22  both">.<a name="
49f0: 73 65 63 74 69 6f 6e 5f 33 5f 39 22 3e 3c 2f 61  section_3_9"></a
4a00: 3e 0a 3c 68 33 3e 33 2e 39 20 57 72 69 74 69 6e  >.<h3>3.9 Writin
4a10: 67 20 43 68 61 6e 67 65 73 20 54 6f 20 54 68 65  g Changes To The
4a20: 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 3c 2f   Database File</
4a30: 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d  h3>.<img src="im
4a40: 61 67 65 73 2f 61 63 2f 63 6f 6d 6d 69 74 2d 38  ages/ac/commit-8
4a50: 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72 69 67  .gif" align="rig
4a60: 68 74 22 20 68 73 70 61 63 65 3d 22 31 35 22 3e  ht" hspace="15">
4a70: 0a 0a 3c 70 3e 4f 6e 63 65 20 61 6e 20 65 78 63  ..<p>Once an exc
4a80: 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 68  lusive lock is h
4a90: 65 6c 64 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61  eld, we know tha
4aa0: 74 20 6e 6f 20 6f 74 68 65 72 0a 70 72 6f 63 65  t no other.proce
4ab0: 73 73 65 73 20 61 72 65 20 72 65 61 64 69 6e 67  sses are reading
4ac0: 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
4ad0: 73 65 20 66 69 6c 65 20 61 6e 64 20 69 74 20 69  se file and it i
4ae0: 73 0a 73 61 66 65 20 74 6f 20 77 72 69 74 65 20  s.safe to write 
4af0: 63 68 61 6e 67 65 73 20 69 6e 74 6f 20 74 68 65  changes into the
4b00: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
4b10: 20 55 73 75 61 6c 6c 79 0a 74 68 6f 73 65 20 63   Usually.those c
4b20: 68 61 6e 67 65 73 20 6f 6e 6c 79 20 67 6f 20 61  hanges only go a
4b30: 73 20 66 61 72 20 61 73 20 74 68 65 20 6f 70 65  s far as the ope
4b40: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 64  rating systems d
4b50: 69 73 6b 0a 63 61 63 68 65 20 61 6e 64 20 64 6f  isk.cache and do
4b60: 20 6e 6f 74 20 6d 61 6b 65 20 69 74 20 61 6c 6c   not make it all
4b70: 20 74 68 65 20 77 61 79 20 74 6f 20 6d 61 73 73   the way to mass
4b80: 20 73 74 6f 72 61 67 65 2e 3c 2f 70 3e 0a 0a 3c   storage.</p>..<
4b90: 62 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e  br clear="both">
4ba0: 0a 3c 61 20 6e 61 6d 65 3d 22 73 65 63 74 69 6f  .<a name="sectio
4bb0: 6e 5f 33 5f 31 30 22 3e 3c 2f 61 3e 0a 3c 68 33  n_3_10"></a>.<h3
4bc0: 3e 33 2e 31 30 20 46 6c 75 73 68 69 6e 67 20 43  >3.10 Flushing C
4bd0: 68 61 6e 67 65 73 20 54 6f 20 4d 61 73 73 20 53  hanges To Mass S
4be0: 74 6f 72 61 67 65 3c 2f 68 33 3e 0a 3c 69 6d 67  torage</h3>.<img
4bf0: 20 73 72 63 3d 22 69 6d 61 67 65 73 2f 61 63 2f   src="images/ac/
4c00: 63 6f 6d 6d 69 74 2d 39 2e 67 69 66 22 20 61 6c  commit-9.gif" al
4c10: 69 67 6e 3d 22 72 69 67 68 74 22 20 68 73 70 61  ign="right" hspa
4c20: 63 65 3d 22 31 35 22 3e 0a 0a 3c 70 3e 41 6e 6f  ce="15">..<p>Ano
4c30: 74 68 65 72 20 66 6c 75 73 68 20 6d 75 73 74 20  ther flush must 
4c40: 6f 63 63 75 72 20 74 6f 20 6d 61 6b 65 20 73 75  occur to make su
4c50: 72 65 20 74 68 61 74 20 61 6c 6c 20 74 68 65 0a  re that all the.
4c60: 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73  database changes
4c70: 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74   are written int
4c80: 6f 20 6e 6f 6e 76 6f 6c 61 74 69 6c 65 20 73 74  o nonvolatile st
4c90: 6f 72 61 67 65 2e 0a 54 68 69 73 20 69 73 20 61  orage..This is a
4ca0: 20 63 72 69 74 69 63 61 6c 20 73 74 65 70 20 74   critical step t
4cb0: 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
4cc0: 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 0a  e database will.
4cd0: 73 75 72 76 69 76 65 20 61 20 70 6f 77 65 72 20  survive a power 
4ce0: 6c 6f 73 73 20 77 69 74 68 6f 75 74 20 64 61 6d  loss without dam
4cf0: 61 67 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 62  age.  However, b
4d00: 65 63 61 75 73 65 0a 6f 66 20 74 68 65 20 69 6e  ecause.of the in
4d10: 68 65 72 65 6e 74 20 73 6c 6f 77 6e 65 73 73 20  herent slowness 
4d20: 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 64 69  of writing to di
4d30: 73 6b 20 6f 72 20 66 6c 61 73 68 20 6d 65 6d 6f  sk or flash memo
4d40: 72 79 2c 20 0a 74 68 69 73 20 73 74 65 70 20 74  ry, .this step t
4d50: 6f 67 65 74 68 65 72 20 77 69 74 68 20 74 68 65  ogether with the
4d60: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
4d70: 6c 20 66 69 6c 65 20 66 6c 75 73 68 20 69 6e 20  l file flush in 
4d80: 73 65 63 74 69 6f 6e 0a 33 2e 37 20 61 62 6f 76  section.3.7 abov
4d90: 65 20 74 61 6b 65 73 20 75 70 20 6d 6f 73 74 20  e takes up most 
4da0: 6f 66 20 74 68 65 20 74 69 6d 65 20 72 65 71 75  of the time requ
4db0: 69 72 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65  ired to complete
4dc0: 20 61 0a 74 72 61 6e 73 61 63 74 69 6f 6e 20 63   a.transaction c
4dd0: 6f 6d 6d 69 74 20 69 6e 20 53 51 4c 69 74 65 2e  ommit in SQLite.
4de0: 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72 3d  </p>..<br clear=
4df0: 22 62 6f 74 68 22 3e 0a 3c 61 20 6e 61 6d 65 3d  "both">.<a name=
4e00: 22 73 65 63 74 69 6f 6e 5f 33 5f 31 31 22 3e 3c  "section_3_11"><
4e10: 2f 61 3e 0a 3c 68 33 3e 33 2e 31 31 20 44 65 6c  /a>.<h3>3.11 Del
4e20: 65 74 69 6e 67 20 54 68 65 20 52 6f 6c 6c 62 61  eting The Rollba
4e30: 63 6b 20 4a 6f 75 72 6e 61 6c 3c 2f 68 33 3e 0a  ck Journal</h3>.
4e40: 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67 65 73  <img src="images
4e50: 2f 61 63 2f 63 6f 6d 6d 69 74 2d 41 2e 67 69 66  /ac/commit-A.gif
4e60: 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 20  " align="right" 
4e70: 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70  hspace="15">..<p
4e80: 3e 41 66 74 65 72 20 74 68 65 20 64 61 74 61 62  >After the datab
4e90: 61 73 65 20 63 68 61 6e 67 65 73 20 61 72 65 20  ase changes are 
4ea0: 61 6c 6c 20 73 61 66 65 6c 79 20 6f 6e 20 74 68  all safely on th
4eb0: 65 20 6d 61 73 73 0a 73 74 6f 72 61 67 65 20 64  e mass.storage d
4ec0: 65 76 69 63 65 2c 20 74 68 65 20 72 6f 6c 6c 62  evice, the rollb
4ed0: 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ack journal file
4ee0: 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 54 68 69   is deleted..Thi
4ef0: 73 20 69 73 20 74 68 65 20 69 6e 73 74 61 6e 74  s is the instant
4f00: 20 77 68 65 72 65 20 74 68 65 20 74 72 61 6e 73   where the trans
4f10: 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a  action commits..
4f20: 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75  If a power failu
4f30: 72 65 20 6f 72 20 73 79 73 74 65 6d 20 63 72 61  re or system cra
4f40: 73 68 20 6f 63 63 75 72 73 20 70 72 69 6f 72 20  sh occurs prior 
4f50: 74 6f 20 74 68 69 73 0a 70 6f 69 6e 74 2c 20 74  to this.point, t
4f60: 68 65 6e 20 72 65 63 6f 76 65 72 79 20 70 72 6f  hen recovery pro
4f70: 63 65 73 73 65 73 20 74 6f 20 62 65 20 64 65 73  cesses to be des
4f80: 63 72 69 62 65 64 20 6c 61 74 65 72 20 6d 61 6b  cribed later mak
4f90: 65 0a 69 74 20 61 70 70 65 61 72 20 61 73 20 69  e.it appear as i
4fa0: 66 20 6e 6f 20 63 68 61 6e 67 65 73 20 77 65 72  f no changes wer
4fb0: 65 20 65 76 65 72 20 6d 61 64 65 20 74 6f 20 74  e ever made to t
4fc0: 68 65 20 64 61 74 61 62 61 73 65 0a 66 69 6c 65  he database.file
4fd0: 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61  .  If a power fa
4fe0: 69 6c 75 72 65 20 6f 72 20 73 79 73 74 65 6d 20  ilure or system 
4ff0: 63 72 61 73 68 20 6f 63 63 75 72 73 20 61 66 74  crash occurs aft
5000: 65 72 0a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  er.the rollback 
5010: 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65 74  journal is delet
5020: 65 64 2c 20 74 68 65 6e 20 69 74 20 61 70 70 65  ed, then it appe
5030: 61 72 73 20 61 73 20 69 66 0a 61 6c 6c 20 63 68  ars as if.all ch
5040: 61 6e 67 65 73 20 68 61 76 65 20 62 65 65 6e 20  anges have been 
5050: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e  written to disk.
5060: 20 20 54 68 75 73 2c 20 53 51 4c 69 74 65 20 67    Thus, SQLite g
5070: 69 76 65 73 0a 74 68 65 20 61 70 70 65 61 72 61  ives.the appeara
5080: 6e 63 65 20 6f 66 20 68 61 76 69 6e 67 20 6d 61  nce of having ma
5090: 64 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f  de no changes to
50a0: 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 66 69   the database.fi
50b0: 6c 65 20 6f 72 20 68 61 76 69 6e 67 20 6d 61 64  le or having mad
50c0: 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 73  e the complete s
50d0: 65 74 20 6f 66 20 63 68 61 6e 67 65 73 20 74 6f  et of changes to
50e0: 20 74 68 65 0a 64 61 74 61 62 61 73 65 20 66 69   the.database fi
50f0: 6c 65 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  le depending on 
5100: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
5110: 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 6a 6f 75 72  he rollback.jour
5120: 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2e  nal file exists.
5130: 3c 2f 70 3e 0a 0a 3c 70 3e 44 65 6c 65 74 69 6e  </p>..<p>Deletin
5140: 67 20 61 20 66 69 6c 65 20 69 73 20 6e 6f 74 20  g a file is not 
5150: 72 65 61 6c 6c 79 20 61 6e 20 61 74 6f 6d 69 63  really an atomic
5160: 20 6f 70 65 72 61 74 69 6f 6e 2c 20 62 75 74 0a   operation, but.
5170: 69 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  it appears to be
5180: 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20   from the point 
5190: 6f 66 20 76 69 65 77 20 6f 66 20 61 20 75 73 65  of view of a use
51a0: 72 20 70 72 6f 63 65 73 73 2e 0a 41 20 70 72 6f  r process..A pro
51b0: 63 65 73 73 20 69 73 20 61 6c 77 61 79 73 20 61  cess is always a
51c0: 62 6c 65 20 74 6f 20 61 73 6b 20 74 68 65 20 6f  ble to ask the o
51d0: 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
51e0: 22 64 6f 65 73 0a 74 68 69 73 20 66 69 6c 65 20  "does.this file 
51f0: 65 78 69 73 74 3f 22 20 61 6e 64 20 74 68 65 20  exist?" and the 
5200: 70 72 6f 63 65 73 73 20 77 69 6c 6c 20 67 65 74  process will get
5210: 20 62 61 63 6b 20 61 20 79 65 73 20 6f 72 20 6e   back a yes or n
5220: 6f 0a 61 6e 73 77 65 72 2e 20 20 41 66 74 65 72  o.answer.  After
5230: 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
5240: 20 74 68 61 74 20 6f 63 63 75 72 73 20 64 75 72   that occurs dur
5250: 69 6e 67 20 61 20 0a 74 72 61 6e 73 61 63 74 69  ing a .transacti
5260: 6f 6e 20 63 6f 6d 6d 69 74 2c 20 53 51 4c 69 74  on commit, SQLit
5270: 65 20 77 69 6c 6c 20 61 73 6b 20 74 68 65 20 6f  e will ask the o
5280: 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 0a  perating system.
5290: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
52a0: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
52b0: 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2e  nal file exists.
52c0: 20 20 49 66 20 74 68 65 0a 61 6e 73 77 65 72 20    If the.answer 
52d0: 69 73 20 22 79 65 73 22 20 74 68 65 6e 20 74 68  is "yes" then th
52e0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
52f0: 20 69 6e 63 6f 6d 70 6c 65 74 65 20 61 6e 64 20   incomplete and 
5300: 69 73 0a 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  is.rolled back. 
5310: 20 49 66 20 74 68 65 20 61 6e 73 77 65 72 20 69   If the answer i
5320: 73 20 22 6e 6f 22 20 74 68 65 6e 20 69 74 20 6d  s "no" then it m
5330: 65 61 6e 73 20 74 68 65 20 74 72 61 6e 73 61 63  eans the transac
5340: 74 69 6f 6e 0a 64 69 64 20 63 6f 6d 6d 69 74 2e  tion.did commit.
5350: 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65 20 65 78 69  </p>..<p>The exi
5360: 73 74 65 6e 63 65 20 6f 66 20 61 20 74 72 61 6e  stence of a tran
5370: 73 61 63 74 69 6f 6e 20 64 65 70 65 6e 64 73 20  saction depends 
5380: 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 0a 6e 6f  on whether or.no
5390: 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  t the rollback j
53a0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73  ournal file exis
53b0: 74 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74  ts and the delet
53c0: 69 6f 6e 0a 6f 66 20 61 20 66 69 6c 65 20 61 70  ion.of a file ap
53d0: 70 65 61 72 73 20 74 6f 20 62 65 20 61 6e 20 61  pears to be an a
53e0: 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 20  tomic operation 
53f0: 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20 6f  from the point o
5400: 66 0a 76 69 65 77 20 6f 66 20 61 20 75 73 65 72  f.view of a user
5410: 2d 73 70 61 63 65 20 70 72 6f 63 65 73 73 2e 20  -space process. 
5420: 20 54 68 65 72 65 66 6f 72 65 2c 20 0a 61 20 74   Therefore, .a t
5430: 72 61 6e 73 61 63 74 69 6f 6e 20 61 70 70 65 61  ransaction appea
5440: 72 73 20 74 6f 20 62 65 20 61 6e 20 61 74 6f 6d  rs to be an atom
5450: 69 63 20 6f 70 65 72 61 74 69 6f 6e 2e 3c 2f 70  ic operation.</p
5460: 3e 0a 0a 3c 70 3e 54 68 65 20 61 63 74 20 6f 66  >..<p>The act of
5470: 20 64 65 6c 65 74 69 6e 67 20 61 20 66 69 6c 65   deleting a file
5480: 20 69 73 20 65 78 70 65 6e 73 69 76 65 20 6f 6e   is expensive on
5490: 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 2e 0a 41   many systems..A
54a0: 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
54b0: 6e 2c 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65  n, SQLite can be
54c0: 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 74   configured to t
54d0: 72 75 6e 63 61 74 65 0a 74 68 65 20 6a 6f 75 72  runcate.the jour
54e0: 6e 61 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f  nal file to zero
54f0: 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68   bytes in length
5500: 0a 6f 72 20 6f 76 65 72 77 72 69 74 65 20 74 68  .or overwrite th
5510: 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
5520: 65 61 64 65 72 20 77 69 74 68 20 7a 65 72 6f 73  eader with zeros
5530: 2e 20 20 49 6e 20 65 69 74 68 65 72 0a 63 61 73  .  In either.cas
5540: 65 2c 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  e, the resulting
5550: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
5560: 20 6e 6f 20 6c 6f 6e 67 65 72 20 63 61 70 61 62   no longer capab
5570: 6c 65 20 6f 66 20 72 6f 6c 6c 69 6e 67 0a 62 61  le of rolling.ba
5580: 63 6b 20 61 6e 64 20 73 6f 20 74 68 65 20 74 72  ck and so the tr
5590: 61 6e 73 61 63 74 69 6f 6e 20 73 74 69 6c 6c 20  ansaction still 
55a0: 63 6f 6d 6d 69 74 73 2e 20 20 54 72 75 6e 63 61  commits.  Trunca
55b0: 74 69 6e 67 20 61 20 66 69 6c 65 0a 74 6f 20 7a  ting a file.to z
55c0: 65 72 6f 20 6c 65 6e 67 74 68 2c 20 6c 69 6b 65  ero length, like
55d0: 20 64 65 6c 65 74 69 6e 67 20 61 20 66 69 6c 65   deleting a file
55e0: 2c 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  , is assumed to 
55f0: 62 65 20 61 6e 20 61 74 6f 6d 69 63 0a 6f 70 65  be an atomic.ope
5600: 72 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  ration from the 
5610: 70 6f 69 6e 74 20 6f 66 20 76 69 65 77 20 6f 66  point of view of
5620: 20 61 20 75 73 65 72 20 70 72 6f 63 65 73 73 2e   a user process.
5630: 20 20 4f 76 65 72 77 72 69 74 69 6e 67 0a 74 68    Overwriting.th
5640: 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20  e header of the 
5650: 6a 6f 75 72 6e 61 6c 20 77 69 74 68 20 7a 65 72  journal with zer
5660: 6f 73 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63  os is not atomic
5670: 2c 20 62 75 74 20 69 66 20 61 6e 79 0a 70 61 72  , but if any.par
5680: 74 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20  t of the header 
5690: 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 74 68 65  is malformed the
56a0: 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f   journal will no
56b0: 74 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 48 65 6e  t roll back..Hen
56c0: 63 65 2c 20 6f 6e 65 20 63 61 6e 20 73 61 79 20  ce, one can say 
56d0: 74 68 61 74 20 74 68 65 20 63 6f 6d 6d 69 74 20  that the commit 
56e0: 6f 63 63 75 72 73 20 61 73 20 73 6f 6f 6e 20 61  occurs as soon a
56f0: 73 20 74 68 65 20 68 65 61 64 65 72 0a 69 73 20  s the header.is 
5700: 73 75 66 66 69 63 69 65 6e 74 6c 79 20 63 68 61  sufficiently cha
5710: 6e 67 65 64 20 74 6f 20 6d 61 6b 65 20 69 74 20  nged to make it 
5720: 69 6e 76 61 6c 69 64 2e 20 20 54 79 70 69 63 61  invalid.  Typica
5730: 6c 6c 79 20 74 68 69 73 20 68 61 70 70 65 6e 73  lly this happens
5740: 0a 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20  .as soon as the 
5750: 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
5760: 65 20 68 65 61 64 65 72 20 69 73 20 7a 65 72 6f  e header is zero
5770: 65 64 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65  ed.</p>..<br cle
5780: 61 72 3d 22 62 6f 74 68 22 3e 0a 3c 61 20 6e 61  ar="both">.<a na
5790: 6d 65 3d 22 73 65 63 74 69 6f 6e 5f 33 5f 31 32  me="section_3_12
57a0: 22 3e 3c 2f 61 3e 0a 3c 68 33 3e 33 2e 31 32 20  "></a>.<h3>3.12 
57b0: 52 65 6c 65 61 73 69 6e 67 20 54 68 65 20 4c 6f  Releasing The Lo
57c0: 63 6b 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63  ck</h3>.<img src
57d0: 3d 22 69 6d 61 67 65 73 2f 61 63 2f 63 6f 6d 6d  ="images/ac/comm
57e0: 69 74 2d 42 2e 67 69 66 22 20 61 6c 69 67 6e 3d  it-B.gif" align=
57f0: 22 72 69 67 68 74 22 20 68 73 70 61 63 65 3d 22  "right" hspace="
5800: 31 35 22 3e 0a 0a 3c 70 3e 54 68 65 20 6c 61 73  15">..<p>The las
5810: 74 20 73 74 65 70 20 69 6e 20 74 68 65 20 63 6f  t step in the co
5820: 6d 6d 69 74 20 70 72 6f 63 65 73 73 20 69 73 20  mmit process is 
5830: 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 0a 65  to release the.e
5840: 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 73 6f  xclusive lock so
5850: 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 6f 63   that other proc
5860: 65 73 73 65 73 20 63 61 6e 20 6f 6e 63 65 20 61  esses can once a
5870: 67 61 69 6e 0a 73 74 61 72 74 20 61 63 63 65 73  gain.start acces
5880: 73 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  sing the databas
5890: 65 20 66 69 6c 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e  e file.</p>..<p>
58a0: 49 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20 61  In the diagram a
58b0: 74 20 74 68 65 20 72 69 67 68 74 2c 20 77 65 20  t the right, we 
58c0: 73 68 6f 77 20 74 68 61 74 20 74 68 65 20 69 6e  show that the in
58d0: 66 6f 72 6d 61 74 69 6f 6e 0a 74 68 61 74 20 77  formation.that w
58e0: 61 73 20 68 65 6c 64 20 69 6e 20 75 73 65 72 20  as held in user 
58f0: 73 70 61 63 65 20 69 73 20 63 6c 65 61 72 65 64  space is cleared
5900: 20 77 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69   when the lock i
5910: 73 20 72 65 6c 65 61 73 65 64 2e 0a 54 68 69 73  s released..This
5920: 20 75 73 65 64 20 74 6f 20 62 65 20 6c 69 74 65   used to be lite
5930: 72 61 6c 6c 79 20 74 72 75 65 20 66 6f 72 20 6f  rally true for o
5940: 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  lder versions of
5950: 20 53 51 4c 69 74 65 2e 20 20 42 75 74 0a 6d 6f   SQLite.  But.mo
5960: 72 65 20 72 65 63 65 6e 74 20 76 65 72 73 69 6f  re recent versio
5970: 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6b 65 65  ns of SQLite kee
5980: 70 20 74 68 65 20 75 73 65 72 20 73 70 61 63 65  p the user space
5990: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 69 6e 20   information.in 
59a0: 6d 65 6d 6f 72 79 20 69 6e 20 63 61 73 65 20 69  memory in case i
59b0: 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65  t might be neede
59c0: 64 20 61 67 61 69 6e 20 61 74 20 74 68 65 20 73  d again at the s
59d0: 74 61 72 74 20 6f 66 20 74 68 65 0a 6e 65 78 74  tart of the.next
59e0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49   transaction.  I
59f0: 74 20 69 73 20 63 68 65 61 70 65 72 20 74 6f 20  t is cheaper to 
5a00: 72 65 75 73 65 20 69 6e 66 6f 72 6d 61 74 69 6f  reuse informatio
5a10: 6e 20 74 68 61 74 20 69 73 0a 61 6c 72 65 61 64  n that is.alread
5a20: 79 20 69 6e 20 6c 6f 63 61 6c 20 6d 65 6d 6f 72  y in local memor
5a30: 79 20 74 68 61 6e 20 74 6f 20 74 72 61 6e 73 66  y than to transf
5a40: 65 72 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  er the informati
5a50: 6f 6e 20 62 61 63 6b 0a 66 72 6f 6d 20 74 68 65  on back.from the
5a60: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
5a70: 6d 20 64 69 73 6b 20 63 61 63 68 65 20 6f 72 20  m disk cache or 
5a80: 74 6f 20 72 65 61 64 20 69 74 20 6f 66 66 20 6f  to read it off o
5a90: 66 20 74 68 65 0a 64 69 73 6b 20 64 72 69 76 65  f the.disk drive
5aa0: 20 61 67 61 69 6e 2e 20 20 50 72 69 6f 72 20 74   again.  Prior t
5ab0: 6f 20 72 65 75 73 69 6e 67 20 74 68 65 20 69 6e  o reusing the in
5ac0: 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 75 73 65  formation in use
5ad0: 72 20 73 70 61 63 65 2c 0a 77 65 20 6d 75 73 74  r space,.we must
5ae0: 20 66 69 72 73 74 20 72 65 61 63 71 75 69 72 65   first reacquire
5af0: 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
5b00: 20 61 6e 64 20 74 68 65 6e 20 77 65 20 68 61 76   and then we hav
5b10: 65 20 74 6f 20 63 68 65 63 6b 0a 74 6f 20 6d 61  e to check.to ma
5b20: 6b 65 20 73 75 72 65 20 74 68 61 74 20 6e 6f 20  ke sure that no 
5b30: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 6f  other process mo
5b40: 64 69 66 69 65 64 20 74 68 65 20 64 61 74 61 62  dified the datab
5b50: 61 73 65 20 66 69 6c 65 20 77 68 69 6c 65 0a 77  ase file while.w
5b60: 65 20 77 65 72 65 20 6e 6f 74 20 68 6f 6c 64 69  e were not holdi
5b70: 6e 67 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65 72  ng a lock.  Ther
5b80: 65 20 69 73 20 61 20 63 6f 75 6e 74 65 72 20 69  e is a counter i
5b90: 6e 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  n the first page
5ba0: 0a 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65  .of the database
5bb0: 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65   that is increme
5bc0: 6e 74 65 64 20 65 76 65 72 79 20 74 69 6d 65 20  nted every time 
5bd0: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
5be0: 65 0a 69 73 20 6d 6f 64 69 66 69 65 64 2e 20 20  e.is modified.  
5bf0: 57 65 20 63 61 6e 20 66 69 6e 64 20 6f 75 74 20  We can find out 
5c00: 69 66 20 61 6e 6f 74 68 65 72 20 70 72 6f 63 65  if another proce
5c10: 73 73 20 68 61 73 20 6d 6f 64 69 66 69 65 64 20  ss has modified 
5c20: 74 68 65 0a 64 61 74 61 62 61 73 65 20 62 79 20  the.database by 
5c30: 63 68 65 63 6b 69 6e 67 20 74 68 61 74 20 63 6f  checking that co
5c40: 75 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 64  unter.  If the d
5c50: 61 74 61 62 61 73 65 20 77 61 73 20 6d 6f 64 69  atabase was modi
5c60: 66 69 65 64 2c 0a 74 68 65 6e 20 74 68 65 20 75  fied,.then the u
5c70: 73 65 72 20 73 70 61 63 65 20 63 61 63 68 65 20  ser space cache 
5c80: 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65 64 20  must be cleared 
5c90: 61 6e 64 20 72 65 72 65 61 64 2e 20 20 42 75 74  and reread.  But
5ca0: 20 69 74 20 69 73 0a 63 6f 6d 6d 6f 6e 6c 79 20   it is.commonly 
5cb0: 74 68 65 20 63 61 73 65 20 74 68 61 74 20 6e 6f  the case that no
5cc0: 20 63 68 61 6e 67 65 73 20 68 61 76 65 20 62 65   changes have be
5cd0: 65 6e 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20  en made and the 
5ce0: 75 73 65 72 0a 73 70 61 63 65 20 63 61 63 68 65  user.space cache
5cf0: 20 63 61 6e 20 62 65 20 72 65 75 73 65 64 20 66   can be reused f
5d00: 6f 72 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74  or a significant
5d10: 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 61 76   performance sav
5d20: 69 6e 67 73 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63  ings.</p>..<br c
5d30: 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a 3c 74 63  lear="both">.<tc
5d40: 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 72 6f  l>hd_fragment ro
5d50: 6c 6c 62 61 63 6b 3c 2f 74 63 6c 3e 0a 3c 68 32  llback</tcl>.<h2
5d60: 3e 34 2e 30 20 52 6f 6c 6c 62 61 63 6b 3c 2f 68  >4.0 Rollback</h
5d70: 32 3e 0a 0a 3c 70 3e 41 6e 20 61 74 6f 6d 69 63  2>..<p>An atomic
5d80: 20 63 6f 6d 6d 69 74 20 69 73 20 73 75 70 70 6f   commit is suppo
5d90: 73 65 64 20 74 6f 20 68 61 70 70 65 6e 20 69 6e  sed to happen in
5da0: 73 74 61 6e 74 61 6e 65 6f 75 73 6c 79 2e 20 20  stantaneously.  
5db0: 42 75 74 20 74 68 65 20 70 72 6f 63 65 73 73 69  But the processi
5dc0: 6e 67 0a 64 65 73 63 72 69 62 65 64 20 61 62 6f  ng.described abo
5dd0: 76 65 20 63 6c 65 61 72 6c 79 20 74 61 6b 65 73  ve clearly takes
5de0: 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 6e 74   a finite amount
5df0: 20 6f 66 20 74 69 6d 65 2e 0a 53 75 70 70 6f 73   of time..Suppos
5e00: 65 20 74 68 65 20 70 6f 77 65 72 20 74 6f 20 74  e the power to t
5e10: 68 65 20 63 6f 6d 70 75 74 65 72 20 77 65 72 65  he computer were
5e20: 20 63 75 74 0a 70 61 72 74 20 77 61 79 20 74 68   cut.part way th
5e30: 72 6f 75 67 68 20 74 68 65 20 63 6f 6d 6d 69 74  rough the commit
5e40: 20 6f 70 65 72 61 74 69 6f 6e 20 64 65 73 63 72   operation descr
5e50: 69 62 65 64 20 61 62 6f 76 65 2e 20 20 49 6e 20  ibed above.  In 
5e60: 6f 72 64 65 72 0a 74 6f 20 6d 61 69 6e 74 61 69  order.to maintai
5e70: 6e 20 74 68 65 20 69 6c 6c 75 73 69 6f 6e 20 74  n the illusion t
5e80: 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73 20  hat the changes 
5e90: 77 65 72 65 20 69 6e 73 74 61 6e 74 61 6e 65 6f  were instantaneo
5ea0: 75 73 2c 20 77 65 0a 68 61 76 65 20 74 6f 20 22  us, we.have to "
5eb0: 72 6f 6c 6c 62 61 63 6b 22 20 61 6e 79 20 70 61  rollback" any pa
5ec0: 72 74 69 61 6c 20 63 68 61 6e 67 65 73 20 61 6e  rtial changes an
5ed0: 64 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61  d restore the da
5ee0: 74 61 62 61 73 65 20 74 6f 0a 74 68 65 20 73 74  tabase to.the st
5ef0: 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 70 72  ate it was in pr
5f00: 69 6f 72 20 74 6f 20 74 68 65 20 62 65 67 69 6e  ior to the begin
5f10: 6e 69 6e 67 20 6f 66 20 74 68 65 20 74 72 61 6e  ning of the tran
5f20: 73 61 63 74 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 74  saction.</p>..<t
5f30: 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 63  cl>hd_fragment c
5f40: 72 69 73 69 73 3c 2f 74 63 6c 3e 0a 3c 68 33 3e  risis</tcl>.<h3>
5f50: 34 2e 31 20 57 68 65 6e 20 53 6f 6d 65 74 68 69  4.1 When Somethi
5f60: 6e 67 20 47 6f 65 73 20 57 72 6f 6e 67 2e 2e 2e  ng Goes Wrong...
5f70: 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d 22  </h3>.<img src="
5f80: 69 6d 61 67 65 73 2f 61 63 2f 72 6f 6c 6c 62 61  images/ac/rollba
5f90: 63 6b 2d 30 2e 67 69 66 22 20 61 6c 69 67 6e 3d  ck-0.gif" align=
5fa0: 22 72 69 67 68 74 22 20 68 73 70 61 63 65 3d 22  "right" hspace="
5fb0: 31 35 22 3e 0a 0a 3c 70 3e 53 75 70 70 6f 73 65  15">..<p>Suppose
5fc0: 20 74 68 65 20 70 6f 77 65 72 20 6c 6f 73 73 20   the power loss 
5fd0: 6f 63 63 75 72 72 65 64 0a 64 75 72 69 6e 67 20  occurred.during 
5fe0: 3c 61 20 68 72 65 66 3d 22 23 73 65 63 74 69 6f  <a href="#sectio
5ff0: 6e 5f 33 5f 31 30 22 3e 73 74 65 70 20 33 2e 31  n_3_10">step 3.1
6000: 30 3c 2f 61 3e 20 61 62 6f 76 65 2c 0a 77 68 69  0</a> above,.whi
6010: 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  le the database 
6020: 63 68 61 6e 67 65 73 20 77 65 72 65 20 62 65 69  changes were bei
6030: 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69  ng written to di
6040: 73 6b 2e 0a 41 66 74 65 72 20 70 6f 77 65 72 20  sk..After power 
6050: 69 73 20 72 65 73 74 6f 72 65 64 2c 20 74 68 65  is restored, the
6060: 20 73 69 74 75 61 74 69 6f 6e 20 6d 69 67 68 74   situation might
6070: 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 6c 69   be something.li
6080: 6b 65 20 77 68 61 74 20 69 73 20 73 68 6f 77 6e  ke what is shown
6090: 20 74 6f 20 74 68 65 20 72 69 67 68 74 2e 20 20   to the right.  
60a0: 57 65 20 77 65 72 65 20 74 72 79 69 6e 67 20 74  We were trying t
60b0: 6f 20 63 68 61 6e 67 65 0a 74 68 72 65 65 20 70  o change.three p
60c0: 61 67 65 73 20 6f 66 20 74 68 65 20 64 61 74 61  ages of the data
60d0: 62 61 73 65 20 66 69 6c 65 20 62 75 74 20 6f 6e  base file but on
60e0: 6c 79 20 6f 6e 65 20 70 61 67 65 20 77 61 73 0a  ly one page was.
60f0: 73 75 63 63 65 73 73 66 75 6c 6c 79 20 77 72 69  successfully wri
6100: 74 74 65 6e 2e 20 20 41 6e 6f 74 68 65 72 20 70  tten.  Another p
6110: 61 67 65 20 77 61 73 20 70 61 72 74 69 61 6c 6c  age was partiall
6120: 79 20 77 72 69 74 74 65 6e 0a 61 6e 64 20 61 20  y written.and a 
6130: 74 68 69 72 64 20 70 61 67 65 20 77 61 73 20 6e  third page was n
6140: 6f 74 20 77 72 69 74 74 65 6e 20 61 74 20 61 6c  ot written at al
6150: 6c 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65 20 72  l.</p>..<p>The r
6160: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
6170: 69 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 20  is complete and 
6180: 69 6e 74 61 63 74 20 6f 6e 20 64 69 73 6b 20 77  intact on disk w
6190: 68 65 6e 0a 74 68 65 20 70 6f 77 65 72 20 69 73  hen.the power is
61a0: 20 72 65 73 74 6f 72 65 64 2e 20 20 54 68 69 73   restored.  This
61b0: 20 69 73 20 61 20 6b 65 79 20 70 6f 69 6e 74 2e   is a key point.
61c0: 20 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72    The reason for
61d0: 0a 74 68 65 20 66 6c 75 73 68 20 6f 70 65 72 61  .the flush opera
61e0: 74 69 6f 6e 20 69 6e 20 3c 61 20 68 72 65 66 3d  tion in <a href=
61f0: 22 23 73 65 63 74 69 6f 6e 5f 33 5f 37 22 3e 73  "#section_3_7">s
6200: 74 65 70 20 33 2e 37 3c 2f 61 3e 0a 69 73 20 74  tep 3.7</a>.is t
6210: 6f 20 6d 61 6b 65 20 61 62 73 6f 6c 75 74 65 6c  o make absolutel
6220: 79 20 73 75 72 65 20 74 68 61 74 0a 61 6c 6c 20  y sure that.all 
6230: 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  of the rollback 
6240: 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c  journal is safel
6250: 79 20 6f 6e 20 6e 6f 6e 76 6f 6c 61 74 69 6c 65  y on nonvolatile
6260: 20 73 74 6f 72 61 67 65 0a 70 72 69 6f 72 20 74   storage.prior t
6270: 6f 20 6d 61 6b 69 6e 67 20 61 6e 79 20 63 68 61  o making any cha
6280: 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61  nges to the data
6290: 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66  base file itself
62a0: 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72  .</p>..<br clear
62b0: 3d 22 62 6f 74 68 22 3e 0a 3c 61 20 6e 61 6d 65  ="both">.<a name
62c0: 3d 22 73 65 63 74 69 6f 6e 5f 34 5f 32 22 3e 3c  ="section_4_2"><
62d0: 2f 61 3e 0a 3c 68 33 3e 34 2e 32 20 48 6f 74 20  /a>.<h3>4.2 Hot 
62e0: 52 6f 6c 6c 62 61 63 6b 20 4a 6f 75 72 6e 61 6c  Rollback Journal
62f0: 73 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d  s</h3>.<img src=
6300: 22 69 6d 61 67 65 73 2f 61 63 2f 72 6f 6c 6c 62  "images/ac/rollb
6310: 61 63 6b 2d 31 2e 67 69 66 22 20 61 6c 69 67 6e  ack-1.gif" align
6320: 3d 22 72 69 67 68 74 22 20 68 73 70 61 63 65 3d  ="right" hspace=
6330: 22 31 35 22 3e 0a 0a 3c 70 3e 54 68 65 20 66 69  "15">..<p>The fi
6340: 72 73 74 20 74 69 6d 65 20 74 68 61 74 20 61 6e  rst time that an
6350: 79 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73  y SQLite process
6360: 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61 63 63   attempts to acc
6370: 65 73 73 0a 74 68 65 20 64 61 74 61 62 61 73 65  ess.the database
6380: 20 66 69 6c 65 2c 20 69 74 20 6f 62 74 61 69 6e   file, it obtain
6390: 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  s a shared lock 
63a0: 61 73 20 64 65 73 63 72 69 62 65 64 20 69 6e 0a  as described in.
63b0: 3c 61 20 68 72 65 66 3d 22 73 65 63 74 69 6f 6e  <a href="section
63c0: 5f 33 5f 32 22 3e 73 65 63 74 69 6f 6e 20 33 2e  _3_2">section 3.
63d0: 32 3c 2f 61 3e 20 61 62 6f 76 65 2e 0a 42 75 74  2</a> above..But
63e0: 20 74 68 65 6e 20 69 74 20 6e 6f 74 69 63 65 73   then it notices
63f0: 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
6400: 20 0a 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e   .rollback journ
6410: 61 6c 20 66 69 6c 65 20 70 72 65 73 65 6e 74 2e  al file present.
6420: 20 20 53 51 4c 69 74 65 20 74 68 65 6e 20 63 68    SQLite then ch
6430: 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 0a 74  ecks to see if.t
6440: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
6450: 6e 61 6c 20 69 73 20 61 20 22 68 6f 74 20 6a 6f  nal is a "hot jo
6460: 75 72 6e 61 6c 22 2e 20 20 20 41 20 68 6f 74 20  urnal".   A hot 
6470: 6a 6f 75 72 6e 61 6c 20 69 73 0a 61 20 72 6f 6c  journal is.a rol
6480: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68  lback journal th
6490: 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70  at needs to be p
64a0: 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 20 6f 72  layed back in or
64b0: 64 65 72 20 74 6f 0a 72 65 73 74 6f 72 65 20 74  der to.restore t
64c0: 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 61  he database to a
64d0: 20 73 61 6e 65 20 73 74 61 74 65 2e 20 20 41 20   sane state.  A 
64e0: 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6f 6e 6c 79  hot journal only
64f0: 0a 65 78 69 73 74 73 20 77 68 65 6e 20 61 6e 20  .exists when an 
6500: 65 61 72 6c 69 65 72 20 70 72 6f 63 65 73 73 20  earlier process 
6510: 77 61 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  was in the middl
6520: 65 20 6f 66 20 63 6f 6d 6d 69 74 74 69 6e 67 0a  e of committing.
6530: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  a transaction wh
6540: 65 6e 20 69 74 20 63 72 61 73 68 65 64 20 6f 72  en it crashed or
6550: 20 6c 6f 73 74 20 70 6f 77 65 72 2e 3c 2f 70 3e   lost power.</p>
6560: 0a 0a 3c 70 3e 41 20 72 6f 6c 6c 62 61 63 6b 20  ..<p>A rollback 
6570: 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 22 68 6f  journal is a "ho
6580: 74 22 20 6a 6f 75 72 6e 61 6c 20 69 66 20 61 6c  t" journal if al
6590: 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  l of the followi
65a0: 6e 67 0a 61 72 65 20 74 72 75 65 3a 3c 2f 70 3e  ng.are true:</p>
65b0: 0a 0a 3c 75 6c 3e 0a 3c 6c 69 3e 54 68 65 20 72  ..<ul>.<li>The r
65c0: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
65d0: 65 78 69 73 74 73 2e 0a 3c 6c 69 3e 54 68 65 20  exists..<li>The 
65e0: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
65f0: 20 69 73 20 6e 6f 74 20 61 6e 20 65 6d 70 74 79   is not an empty
6600: 20 66 69 6c 65 2e 0a 3c 6c 69 3e 54 68 65 72 65   file..<li>There
6610: 20 69 73 20 6e 6f 20 72 65 73 65 72 76 65 64 20   is no reserved 
6620: 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 6e  lock on the main
6630: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
6640: 3c 6c 69 3e 54 68 65 20 68 65 61 64 65 72 20 6f  <li>The header o
6650: 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  f the rollback j
6660: 6f 75 72 6e 61 6c 20 69 73 20 77 65 6c 6c 2d 66  ournal is well-f
6670: 6f 72 6d 65 64 20 61 6e 64 20 69 6e 20 70 61 72  ormed and in par
6680: 74 69 63 75 6c 61 72 0a 20 20 20 20 68 61 73 20  ticular.    has 
6690: 6e 6f 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 20  not been zeroed 
66a0: 6f 75 74 2e 0a 3c 6c 69 3e 54 68 65 20 72 6f 6c  out..<li>The rol
66b0: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 64 6f  lback journal do
66c0: 65 73 20 6e 6f 74 0a 63 6f 6e 74 61 69 6e 20 74  es not.contain t
66d0: 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73  he name of a mas
66e0: 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
66f0: 20 28 73 65 65 0a 3c 61 20 68 72 65 66 3d 22 23   (see.<a href="#
6700: 73 65 63 74 69 6f 6e 5f 35 5f 35 22 3e 73 65 63  section_5_5">sec
6710: 74 69 6f 6e 20 35 2e 35 3c 2f 61 3e 20 62 65 6c  tion 5.5</a> bel
6720: 6f 77 29 20 6f 72 20 69 66 20 64 6f 65 73 0a 63  ow) or if does.c
6730: 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 20  ontain the name 
6740: 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  of a master jour
6750: 6e 61 6c 2c 20 74 68 65 6e 20 74 68 61 74 20 6d  nal, then that m
6760: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 66 69  aster journal.fi
6770: 6c 65 20 65 78 69 73 74 73 2e 0a 3c 2f 75 6c 3e  le exists..</ul>
6780: 0a 0a 3c 70 3e 54 68 65 20 70 72 65 73 65 6e 63  ..<p>The presenc
6790: 65 20 6f 66 20 61 20 68 6f 74 20 6a 6f 75 72 6e  e of a hot journ
67a0: 61 6c 20 69 73 20 6f 75 72 20 69 6e 64 69 63 61  al is our indica
67b0: 74 69 6f 6e 0a 74 68 61 74 20 61 20 70 72 65 76  tion.that a prev
67c0: 69 6f 75 73 20 70 72 6f 63 65 73 73 20 77 61 73  ious process was
67d0: 20 74 72 79 69 6e 67 20 74 6f 20 63 6f 6d 6d 69   trying to commi
67e0: 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  t a transaction 
67f0: 62 75 74 0a 69 74 20 61 62 6f 72 74 65 64 20 66  but.it aborted f
6800: 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20 70  or some reason p
6810: 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70  rior to the comp
6820: 6c 65 74 69 6f 6e 20 6f 66 20 74 68 65 0a 63 6f  letion of the.co
6830: 6d 6d 69 74 2e 20 20 41 20 68 6f 74 20 6a 6f 75  mmit.  A hot jou
6840: 72 6e 61 6c 20 6d 65 61 6e 73 20 74 68 61 74 0a  rnal means that.
6850: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
6860: 65 20 69 73 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  e is in an incon
6870: 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61 6e  sistent state an
6880: 64 20 6e 65 65 64 73 20 74 6f 0a 62 65 20 72 65  d needs to.be re
6890: 70 61 69 72 65 64 20 28 62 79 20 72 6f 6c 6c 62  paired (by rollb
68a0: 61 63 6b 29 20 70 72 69 6f 72 20 74 6f 20 62 65  ack) prior to be
68b0: 69 6e 67 20 75 73 65 64 2e 3c 2f 70 3e 0a 0a 3c  ing used.</p>..<
68c0: 62 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e  br clear="both">
68d0: 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e  .<tcl>hd_fragmen
68e0: 74 20 65 78 6c 6f 63 6b 3c 2f 74 63 6c 3e 0a 3c  t exlock</tcl>.<
68f0: 68 33 3e 34 2e 33 20 4f 62 74 61 69 6e 69 6e 67  h3>4.3 Obtaining
6900: 20 41 6e 20 45 78 63 6c 75 73 69 76 65 20 4c 6f   An Exclusive Lo
6910: 63 6b 20 4f 6e 20 54 68 65 20 44 61 74 61 62 61  ck On The Databa
6920: 73 65 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63  se</h3>.<img src
6930: 3d 22 69 6d 61 67 65 73 2f 61 63 2f 72 6f 6c 6c  ="images/ac/roll
6940: 62 61 63 6b 2d 32 2e 67 69 66 22 20 61 6c 69 67  back-2.gif" alig
6950: 6e 3d 22 72 69 67 68 74 22 20 68 73 70 61 63 65  n="right" hspace
6960: 3d 22 31 35 22 3e 0a 0a 3c 70 3e 54 68 65 20 66  ="15">..<p>The f
6970: 69 72 73 74 20 73 74 65 70 20 74 6f 77 61 72 64  irst step toward
6980: 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
6990: 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 74  hot journal is t
69a0: 6f 0a 6f 62 74 61 69 6e 20 61 6e 20 65 78 63 6c  o.obtain an excl
69b0: 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  usive lock on th
69c0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
69d0: 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20    This prevents 
69e0: 74 77 6f 0a 6f 72 20 6d 6f 72 65 20 70 72 6f 63  two.or more proc
69f0: 65 73 73 65 73 20 66 72 6f 6d 20 74 72 79 69 6e  esses from tryin
6a00: 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68  g to rollback th
6a10: 65 20 73 61 6d 65 20 68 6f 74 20 6a 6f 75 72 6e  e same hot journ
6a20: 61 6c 0a 61 74 20 74 68 65 20 73 61 6d 65 20 74  al.at the same t
6a30: 69 6d 65 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c  ime.</p>..<br cl
6a40: 65 61 72 3d 22 62 6f 74 68 22 3e 0a 3c 61 20 6e  ear="both">.<a n
6a50: 61 6d 65 3d 22 73 65 63 74 69 6f 6e 5f 34 5f 34  ame="section_4_4
6a60: 22 3e 3c 2f 61 3e 0a 3c 68 33 3e 34 2e 34 20 52  "></a>.<h3>4.4 R
6a70: 6f 6c 6c 69 6e 67 20 42 61 63 6b 20 49 6e 63 6f  olling Back Inco
6a80: 6d 70 6c 65 74 65 20 43 68 61 6e 67 65 73 3c 2f  mplete Changes</
6a90: 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d  h3>.<img src="im
6aa0: 61 67 65 73 2f 61 63 2f 72 6f 6c 6c 62 61 63 6b  ages/ac/rollback
6ab0: 2d 33 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72  -3.gif" align="r
6ac0: 69 67 68 74 22 20 68 73 70 61 63 65 3d 22 31 35  ight" hspace="15
6ad0: 22 3e 0a 0a 3c 70 3e 4f 6e 63 65 20 61 20 70 72  ">..<p>Once a pr
6ae0: 6f 63 65 73 73 20 6f 62 74 61 69 6e 73 20 61 6e  ocess obtains an
6af0: 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c   exclusive lock,
6b00: 20 69 74 20 69 73 20 70 65 72 6d 69 74 74 65 64   it is permitted
6b10: 0a 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65  .to write to the
6b20: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
6b30: 20 49 74 20 74 68 65 6e 20 70 72 6f 63 65 65 64   It then proceed
6b40: 73 20 74 6f 20 72 65 61 64 20 74 68 65 0a 6f 72  s to read the.or
6b50: 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 6f  iginal content o
6b60: 66 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74  f pages out of t
6b70: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
6b80: 6e 61 6c 20 61 6e 64 20 77 72 69 74 65 0a 74 68  nal and write.th
6b90: 61 74 20 63 6f 6e 74 65 6e 74 20 62 61 63 6b 20  at content back 
6ba0: 74 6f 20 77 68 65 72 65 20 69 74 20 63 61 6d 65  to where it came
6bb0: 20 66 72 6f 6d 20 69 6e 20 74 68 65 20 64 61 74   from in the dat
6bc0: 61 62 61 73 65 20 66 69 6c 65 2e 0a 52 65 63 61  abase file..Reca
6bd0: 6c 6c 20 74 68 61 74 20 74 68 65 20 68 65 61 64  ll that the head
6be0: 65 72 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61  er of the rollba
6bf0: 63 6b 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  ck journal recor
6c00: 64 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a  ds the original.
6c10: 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
6c20: 62 61 73 65 20 66 69 6c 65 20 70 72 69 6f 72 20  base file prior 
6c30: 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  to the start of 
6c40: 74 68 65 20 61 62 6f 72 74 65 64 0a 74 72 61 6e  the aborted.tran
6c50: 73 61 63 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65  saction.  SQLite
6c60: 20 75 73 65 73 20 74 68 69 73 20 69 6e 66 6f 72   uses this infor
6c70: 6d 61 74 69 6f 6e 20 74 6f 20 74 72 75 6e 63 61  mation to trunca
6c80: 74 65 20 74 68 65 0a 64 61 74 61 62 61 73 65 20  te the.database 
6c90: 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 73  file back to its
6ca0: 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 69   original size i
6cb0: 6e 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68  n cases where th
6cc0: 65 0a 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 61  e.incomplete tra
6cd0: 6e 73 61 63 74 69 6f 6e 20 63 61 75 73 65 64 20  nsaction caused 
6ce0: 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
6cf0: 67 72 6f 77 2e 20 20 41 74 20 74 68 65 0a 65 6e  grow.  At the.en
6d00: 64 20 6f 66 20 74 68 69 73 20 73 74 65 70 2c 20  d of this step, 
6d10: 74 68 65 20 64 61 74 61 62 61 73 65 20 73 68 6f  the database sho
6d20: 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20  uld be the same 
6d30: 73 69 7a 65 20 61 6e 64 0a 63 6f 6e 74 61 69 6e  size and.contain
6d40: 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d   the same inform
6d50: 61 74 69 6f 6e 20 61 73 20 69 74 20 64 69 64 20  ation as it did 
6d60: 62 65 66 6f 72 65 20 74 68 65 20 73 74 61 72 74  before the start
6d70: 20 6f 66 0a 74 68 65 20 61 62 6f 72 74 65 64 20   of.the aborted 
6d80: 74 72 61 6e 73 61 63 74 69 6f 6e 2e 3c 2f 70 3e  transaction.</p>
6d90: 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22 62 6f 74  ..<br clear="bot
6da0: 68 22 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67  h">.<tcl>hd_frag
6db0: 6d 65 6e 74 20 64 65 6c 68 6f 74 6a 72 6e 6c 3c  ment delhotjrnl<
6dc0: 2f 74 63 6c 3e 0a 3c 68 33 3e 34 2e 35 20 44 65  /tcl>.<h3>4.5 De
6dd0: 6c 65 74 69 6e 67 20 54 68 65 20 48 6f 74 20 4a  leting The Hot J
6de0: 6f 75 72 6e 61 6c 3c 2f 68 33 3e 0a 3c 69 6d 67  ournal</h3>.<img
6df0: 20 73 72 63 3d 22 69 6d 61 67 65 73 2f 61 63 2f   src="images/ac/
6e00: 72 6f 6c 6c 62 61 63 6b 2d 34 2e 67 69 66 22 20  rollback-4.gif" 
6e10: 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 20 68 73  align="right" hs
6e20: 70 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70 3e 41  pace="15">..<p>A
6e30: 66 74 65 72 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  fter all informa
6e40: 74 69 6f 6e 20 69 6e 20 74 68 65 20 72 6f 6c 6c  tion in the roll
6e50: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 68 61 73  back journal has
6e60: 20 62 65 65 6e 0a 70 6c 61 79 65 64 20 62 61 63   been.played bac
6e70: 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  k into the datab
6e80: 61 73 65 20 66 69 6c 65 20 28 61 6e 64 20 66 6c  ase file (and fl
6e90: 75 73 68 65 64 20 74 6f 20 64 69 73 6b 20 69 6e  ushed to disk in
6ea0: 20 63 61 73 65 0a 77 65 20 65 6e 63 6f 75 6e 74   case.we encount
6eb0: 65 72 20 79 65 74 20 61 6e 6f 74 68 65 72 20 70  er yet another p
6ec0: 6f 77 65 72 20 66 61 69 6c 75 72 65 29 2c 20 74  ower failure), t
6ed0: 68 65 20 68 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  he hot rollback 
6ee0: 6a 6f 75 72 6e 61 6c 0a 63 61 6e 20 62 65 20 64  journal.can be d
6ef0: 65 6c 65 74 65 64 2e 3c 2f 70 3e 0a 0a 3c 70 3e  eleted.</p>..<p>
6f00: 41 73 20 69 6e 20 3c 61 20 68 72 65 66 3d 22 23  As in <a href="#
6f10: 73 65 63 74 69 6f 6e 5f 33 5f 31 31 22 3e 73 65  section_3_11">se
6f20: 63 74 69 6f 6e 20 33 2e 31 31 3c 2f 61 3e 2c 20  ction 3.11</a>, 
6f30: 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 66 69 6c 65  the journal.file
6f40: 20 6d 69 67 68 74 20 62 65 20 74 72 75 6e 63 61   might be trunca
6f50: 74 65 64 20 74 6f 20 7a 65 72 6f 20 6c 65 6e 67  ted to zero leng
6f60: 74 68 20 6f 72 20 69 74 73 20 68 65 61 64 65 72  th or its header
6f70: 20 6d 69 67 68 74 0a 62 65 20 6f 76 65 72 77 72   might.be overwr
6f80: 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73  itten with zeros
6f90: 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74   as an optimizat
6fa0: 69 6f 6e 20 6f 6e 20 73 79 73 74 65 6d 73 20 77  ion on systems w
6fb0: 68 65 72 65 0a 64 65 6c 65 74 69 6e 67 20 61 20  here.deleting a 
6fc0: 66 69 6c 65 20 69 73 20 65 78 70 65 6e 73 69 76  file is expensiv
6fd0: 65 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20  e.  Either way, 
6fe0: 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e  the journal is n
6ff0: 6f 20 0a 6c 6f 6e 67 65 72 20 68 6f 74 20 61 66  o .longer hot af
7000: 74 65 72 20 74 68 69 73 20 73 74 65 70 2e 3c 2f  ter this step.</
7010: 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22 62  p>..<br clear="b
7020: 6f 74 68 22 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72  oth">.<tcl>hd_fr
7030: 61 67 6d 65 6e 74 20 63 6f 6e 74 3c 2f 74 63 6c  agment cont</tcl
7040: 3e 0a 3c 68 33 3e 34 2e 36 20 43 6f 6e 74 69 6e  >.<h3>4.6 Contin
7050: 75 65 20 41 73 20 49 66 20 54 68 65 20 55 6e 63  ue As If The Unc
7060: 6f 6d 70 6c 65 74 65 64 20 57 72 69 74 65 73 20  ompleted Writes 
7070: 48 61 64 20 4e 65 76 65 72 20 48 61 70 70 65 6e  Had Never Happen
7080: 65 64 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63  ed</h3>.<img src
7090: 3d 22 69 6d 61 67 65 73 2f 61 63 2f 72 6f 6c 6c  ="images/ac/roll
70a0: 62 61 63 6b 2d 35 2e 67 69 66 22 20 61 6c 69 67  back-5.gif" alig
70b0: 6e 3d 22 72 69 67 68 74 22 20 68 73 70 61 63 65  n="right" hspace
70c0: 3d 22 31 35 22 3e 0a 0a 3c 70 3e 54 68 65 20 66  ="15">..<p>The f
70d0: 69 6e 61 6c 20 72 65 63 6f 76 65 72 79 20 73 74  inal recovery st
70e0: 65 70 20 69 73 20 74 6f 20 72 65 64 75 63 65 20  ep is to reduce 
70f0: 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  the exclusive lo
7100: 63 6b 20 62 61 63 6b 0a 74 6f 20 61 20 73 68 61  ck back.to a sha
7110: 72 65 64 20 6c 6f 63 6b 2e 20 20 4f 6e 63 65 20  red lock.  Once 
7120: 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68  this happens, th
7130: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 62 61  e database is ba
7140: 63 6b 20 69 6e 20 74 68 65 0a 73 74 61 74 65 20  ck in the.state 
7150: 74 68 61 74 20 69 74 20 77 6f 75 6c 64 20 68 61  that it would ha
7160: 76 65 20 62 65 65 6e 20 69 66 20 74 68 65 20 61  ve been if the a
7170: 62 6f 72 74 65 64 20 74 72 61 6e 73 61 63 74 69  borted transacti
7180: 6f 6e 20 68 61 64 20 6e 65 76 65 72 0a 73 74 61  on had never.sta
7190: 72 74 65 64 2e 20 20 53 69 6e 63 65 20 61 6c 6c  rted.  Since all
71a0: 20 6f 66 20 74 68 69 73 20 72 65 63 6f 76 65 72   of this recover
71b0: 79 20 61 63 74 69 76 69 74 79 20 68 61 70 70 65  y activity happe
71c0: 6e 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 61 75  ns completely.au
71d0: 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 6e 64 20  tomatically and 
71e0: 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 2c 20 69  transparently, i
71f0: 74 20 61 70 70 65 61 72 73 20 74 6f 20 74 68 65  t appears to the
7200: 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 0a 53   program using.S
7210: 51 4c 69 74 65 20 61 73 20 69 66 20 74 68 65 20  QLite as if the 
7220: 61 62 6f 72 74 65 64 20 74 72 61 6e 73 61 63 74  aborted transact
7230: 69 6f 6e 20 68 61 64 20 6e 65 76 65 72 20 62 65  ion had never be
7240: 67 75 6e 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c  gun.</p>..<br cl
7250: 65 61 72 3d 22 62 6f 74 68 22 3e 0a 3c 74 63 6c  ear="both">.<tcl
7260: 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 6d 75 6c  >hd_fragment mul
7270: 74 69 63 6f 6d 6d 69 74 3c 2f 74 63 6c 3e 0a 3c  ticommit</tcl>.<
7280: 68 32 3e 35 2e 30 20 4d 75 6c 74 69 2d 66 69 6c  h2>5.0 Multi-fil
7290: 65 20 43 6f 6d 6d 69 74 3c 2f 68 32 3e 0a 0a 3c  e Commit</h2>..<
72a0: 70 3e 53 51 4c 69 74 65 20 61 6c 6c 6f 77 73 20  p>SQLite allows 
72b0: 61 20 73 69 6e 67 6c 65 20 0a 3c 61 20 68 72 65  a single .<a hre
72c0: 66 3d 22 63 33 72 65 66 2f 73 71 6c 69 74 65 33  f="c3ref/sqlite3
72d0: 2e 68 74 6d 6c 22 3e 64 61 74 61 62 61 73 65 20  .html">database 
72e0: 63 6f 6e 6e 65 63 74 69 6f 6e 3c 2f 61 3e 20 74  connection</a> t
72f0: 6f 20 74 61 6c 6b 20 74 6f 0a 74 77 6f 20 6f 72  o talk to.two or
7300: 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 66   more database f
7310: 69 6c 65 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75  iles simultaneou
7320: 73 6c 79 20 74 68 72 6f 75 67 68 20 74 68 65 20  sly through the 
7330: 75 73 65 20 6f 66 0a 74 68 65 20 3c 61 20 68 72  use of.the <a hr
7340: 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e  ef="lang_attach.
7350: 68 74 6d 6c 22 3e 41 54 54 41 43 48 20 44 41 54  html">ATTACH DAT
7360: 41 42 41 53 45 3c 2f 61 3e 20 63 6f 6d 6d 61 6e  ABASE</a> comman
7370: 64 2e 0a 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65  d..When multiple
7380: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
7390: 61 72 65 20 6d 6f 64 69 66 69 65 64 20 77 69 74  are modified wit
73a0: 68 69 6e 20 61 20 73 69 6e 67 6c 65 0a 74 72 61  hin a single.tra
73b0: 6e 73 61 63 74 69 6f 6e 2c 20 61 6c 6c 20 66 69  nsaction, all fi
73c0: 6c 65 73 20 61 72 65 20 75 70 64 61 74 65 64 20  les are updated 
73d0: 61 74 6f 6d 69 63 61 6c 6c 79 2e 20 20 0a 49 6e  atomically.  .In
73e0: 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 65 69   other words, ei
73f0: 74 68 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  ther all of the 
7400: 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 61  database files a
7410: 72 65 20 75 70 64 61 74 65 64 20 6f 72 0a 65 6c  re updated or.el
7420: 73 65 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20  se none of them 
7430: 61 72 65 2e 0a 41 63 68 69 65 76 69 6e 67 20 61  are..Achieving a
7440: 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20  n atomic commit 
7450: 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65 20  across multiple 
7460: 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 69  database files i
7470: 73 0a 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 74  s.more complex t
7480: 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 66 6f 72  hat doing so for
7490: 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 2e 20   a single file. 
74a0: 20 54 68 69 73 20 73 65 63 74 69 6f 6e 0a 64 65   This section.de
74b0: 73 63 72 69 62 65 73 20 68 6f 77 20 53 51 4c 69  scribes how SQLi
74c0: 74 65 20 77 6f 72 6b 73 20 74 68 61 74 20 62 69  te works that bi
74d0: 74 20 6f 66 20 6d 61 67 69 63 2e 3c 2f 70 3e 0a  t of magic.</p>.
74e0: 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e  .<tcl>hd_fragmen
74f0: 74 20 6d 75 6c 74 69 6a 72 6e 6c 3c 2f 74 63 6c  t multijrnl</tcl
7500: 3e 0a 3c 68 33 3e 35 2e 31 20 53 65 70 61 72 61  >.<h3>5.1 Separa
7510: 74 65 20 52 6f 6c 6c 62 61 63 6b 20 4a 6f 75 72  te Rollback Jour
7520: 6e 61 6c 73 20 46 6f 72 20 45 61 63 68 20 44 61  nals For Each Da
7530: 74 61 62 61 73 65 3c 2f 68 33 3e 0a 3c 69 6d 67  tabase</h3>.<img
7540: 20 73 72 63 3d 22 69 6d 61 67 65 73 2f 61 63 2f   src="images/ac/
7550: 6d 75 6c 74 69 2d 30 2e 67 69 66 22 20 61 6c 69  multi-0.gif" ali
7560: 67 6e 3d 22 72 69 67 68 74 22 20 68 73 70 61 63  gn="right" hspac
7570: 65 3d 22 31 35 22 3e 0a 0a 3c 70 3e 57 68 65 6e  e="15">..<p>When
7580: 20 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61   multiple databa
7590: 73 65 20 66 69 6c 65 73 20 61 72 65 20 69 6e 76  se files are inv
75a0: 6f 6c 76 65 64 20 69 6e 20 61 20 74 72 61 6e 73  olved in a trans
75b0: 61 63 74 69 6f 6e 2c 0a 65 61 63 68 20 64 61 74  action,.each dat
75c0: 61 62 61 73 65 20 68 61 73 20 69 74 73 20 6f 77  abase has its ow
75d0: 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  n rollback journ
75e0: 61 6c 20 61 6e 64 20 65 61 63 68 20 64 61 74 61  al and each data
75f0: 62 61 73 65 0a 69 73 20 6c 6f 63 6b 65 64 20 73  base.is locked s
7600: 65 70 61 72 61 74 65 6c 79 2e 20 20 54 68 65 20  eparately.  The 
7610: 64 69 61 67 72 61 6d 20 61 74 20 74 68 65 20 72  diagram at the r
7620: 69 67 68 74 20 73 68 6f 77 73 20 61 20 73 63 65  ight shows a sce
7630: 6e 61 72 69 6f 0a 77 68 65 72 65 20 74 68 72 65  nario.where thre
7640: 65 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61  e different data
7650: 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 20  base files have 
7660: 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 77 69  been modified wi
7670: 74 68 69 6e 0a 6f 6e 65 20 74 72 61 6e 73 61 63  thin.one transac
7680: 74 69 6f 6e 2e 20 20 54 68 65 20 73 69 74 75 61  tion.  The situa
7690: 74 69 6f 6e 20 61 74 20 74 68 69 73 20 73 74 65  tion at this ste
76a0: 70 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73 20 74  p is analogous t
76b0: 6f 20 0a 74 68 65 20 73 69 6e 67 6c 65 2d 66 69  o .the single-fi
76c0: 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73  le transaction s
76d0: 63 65 6e 61 72 69 6f 20 61 74 20 0a 3c 61 20 68  cenario at .<a h
76e0: 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f  ref="#section_3_
76f0: 36 22 3e 73 74 65 70 20 33 2e 36 3c 2f 61 3e 2e  6">step 3.6</a>.
7700: 20 20 45 61 63 68 20 64 61 74 61 62 61 73 65 20    Each database 
7710: 66 69 6c 65 20 68 61 73 0a 61 20 72 65 73 65 72  file has.a reser
7720: 76 65 64 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 65  ved lock.  For e
7730: 61 63 68 20 64 61 74 61 62 61 73 65 2c 20 74 68  ach database, th
7740: 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65  e original conte
7750: 6e 74 20 6f 66 20 70 61 67 65 73 20 0a 74 68 61  nt of pages .tha
7760: 74 20 61 72 65 20 62 65 69 6e 67 20 63 68 61 6e  t are being chan
7770: 67 65 64 20 68 61 76 65 20 62 65 65 6e 20 77 72  ged have been wr
7780: 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 72  itten into the r
7790: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a  ollback journal.
77a0: 66 6f 72 20 74 68 61 74 20 64 61 74 61 62 61 73  for that databas
77b0: 65 2c 20 62 75 74 20 74 68 65 20 63 6f 6e 74 65  e, but the conte
77c0: 6e 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  nt of the journa
77d0: 6c 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 0a  ls have not yet.
77e0: 62 65 65 6e 20 66 6c 75 73 68 65 64 20 74 6f 20  been flushed to 
77f0: 64 69 73 6b 2e 20 20 4e 6f 20 63 68 61 6e 67 65  disk.  No change
7800: 73 20 68 61 76 65 20 62 65 65 6e 20 6d 61 64 65  s have been made
7810: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
7820: 0a 66 69 6c 65 20 69 74 73 65 6c 66 20 79 65 74  .file itself yet
7830: 2c 20 74 68 6f 75 67 68 20 70 72 65 73 75 6d 61  , though presuma
7840: 62 6c 79 20 74 68 65 72 65 20 61 72 65 20 63 68  bly there are ch
7850: 61 6e 67 65 73 20 62 65 69 6e 67 20 68 65 6c 64  anges being held
7860: 0a 69 6e 20 75 73 65 72 20 6d 65 6d 6f 72 79 2e  .in user memory.
7870: 3c 2f 70 3e 0a 0a 3c 70 3e 46 6f 72 20 62 72 65  </p>..<p>For bre
7880: 76 69 74 79 2c 20 74 68 65 20 64 69 61 67 72 61  vity, the diagra
7890: 6d 73 20 69 6e 20 74 68 69 73 20 73 65 63 74 69  ms in this secti
78a0: 6f 6e 20 61 72 65 20 73 69 6d 70 6c 69 66 69 65  on are simplifie
78b0: 64 20 66 72 6f 6d 0a 74 68 6f 73 65 20 74 68 61  d from.those tha
78c0: 74 20 63 61 6d 65 20 62 65 66 6f 72 65 2e 20 20  t came before.  
78d0: 42 6c 75 65 20 63 6f 6c 6f 72 20 73 74 69 6c 6c  Blue color still
78e0: 20 73 69 67 6e 69 66 69 65 73 20 6f 72 69 67 69   signifies origi
78f0: 6e 61 6c 20 63 6f 6e 74 65 6e 74 0a 61 6e 64 20  nal content.and 
7900: 70 69 6e 6b 20 73 74 69 6c 6c 20 73 69 67 6e 69  pink still signi
7910: 66 69 65 73 20 6e 65 77 20 63 6f 6e 74 65 6e 74  fies new content
7920: 2e 20 20 42 75 74 20 74 68 65 20 69 6e 64 69 76  .  But the indiv
7930: 69 64 75 61 6c 20 70 61 67 65 73 0a 69 6e 20 74  idual pages.in t
7940: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
7950: 6e 61 6c 20 61 6e 64 20 74 68 65 20 64 61 74 61  nal and the data
7960: 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 6e 6f  base file are no
7970: 74 20 73 68 6f 77 6e 20 61 6e 64 0a 77 65 20 61  t shown and.we a
7980: 72 65 20 6e 6f 74 20 6d 61 6b 69 6e 67 20 74 68  re not making th
7990: 65 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65  e distinction be
79a0: 74 77 65 65 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  tween informatio
79b0: 6e 20 69 6e 20 74 68 65 0a 6f 70 65 72 61 74 69  n in the.operati
79c0: 6e 67 20 73 79 73 74 65 6d 20 63 61 63 68 65 20  ng system cache 
79d0: 61 6e 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  and information 
79e0: 74 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e  that is on disk.
79f0: 20 20 41 6c 6c 20 6f 66 0a 74 68 65 73 65 20 66    All of.these f
7a00: 61 63 74 6f 72 73 20 73 74 69 6c 6c 20 61 70 70  actors still app
7a10: 6c 79 20 69 6e 20 61 20 6d 75 6c 74 69 2d 66 69  ly in a multi-fi
7a20: 6c 65 20 63 6f 6d 6d 69 74 20 73 63 65 6e 61 72  le commit scenar
7a30: 69 6f 2e 20 20 54 68 65 79 0a 6a 75 73 74 20 74  io.  They.just t
7a40: 61 6b 65 20 75 70 20 61 20 6c 6f 74 20 6f 66 20  ake up a lot of 
7a50: 73 70 61 63 65 20 69 6e 20 74 68 65 20 64 69 61  space in the dia
7a60: 67 72 61 6d 73 20 61 6e 64 20 74 68 65 79 20 64  grams and they d
7a70: 6f 20 6e 6f 74 20 61 64 64 0a 61 6e 79 20 6e 65  o not add.any ne
7a80: 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 73  w information, s
7a90: 6f 20 74 68 65 79 20 61 72 65 20 6f 6d 69 74 74  o they are omitt
7aa0: 65 64 20 68 65 72 65 2e 3c 2f 70 3e 0a 0a 3c 62  ed here.</p>..<b
7ab0: 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a  r clear="both">.
7ac0: 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74  <tcl>hd_fragment
7ad0: 20 6d 61 73 74 65 72 6a 72 6e 6c 3c 2f 74 63 6c   masterjrnl</tcl
7ae0: 3e 0a 3c 68 33 3e 35 2e 32 20 54 68 65 20 4d 61  >.<h3>5.2 The Ma
7af0: 73 74 65 72 20 4a 6f 75 72 6e 61 6c 20 46 69 6c  ster Journal Fil
7b00: 65 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d  e</h3>.<img src=
7b10: 22 69 6d 61 67 65 73 2f 61 63 2f 6d 75 6c 74 69  "images/ac/multi
7b20: 2d 31 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72  -1.gif" align="r
7b30: 69 67 68 74 22 20 68 73 70 61 63 65 3d 22 31 35  ight" hspace="15
7b40: 22 3e 0a 0a 3c 70 3e 54 68 65 20 6e 65 78 74 20  ">..<p>The next 
7b50: 73 74 65 70 20 69 6e 20 61 20 6d 75 6c 74 69 2d  step in a multi-
7b60: 66 69 6c 65 20 63 6f 6d 6d 69 74 20 69 73 20 74  file commit is t
7b70: 68 65 20 63 72 65 61 74 69 6f 6e 20 6f 66 20 61  he creation of a
7b80: 0a 22 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  ."master journal
7b90: 22 20 66 69 6c 65 2e 20 20 54 68 65 20 6e 61 6d  " file.  The nam
7ba0: 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  e of the master 
7bb0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 0a  journal file is.
7bc0: 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 73  the same name as
7bd0: 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
7be0: 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
7bf0: 28 74 68 65 20 64 61 74 61 62 61 73 65 0a 74 68  (the database.th
7c00: 61 74 20 77 61 73 20 6f 70 65 6e 65 64 20 75 73  at was opened us
7c10: 69 6e 67 20 74 68 65 20 0a 3c 61 20 68 72 65 66  ing the .<a href
7c20: 3d 22 63 33 72 65 66 2f 6f 70 65 6e 2e 68 74 6d  ="c3ref/open.htm
7c30: 6c 22 3e 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  l">sqlite3_open(
7c40: 29 3c 2f 61 3e 20 69 6e 74 65 72 66 61 63 65 2c  )</a> interface,
7c50: 0a 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  .not one of the 
7c60: 3c 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74  <a href="lang_at
7c70: 74 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43  tach.html">ATTAC
7c80: 48 65 64 3c 2f 61 3e 20 61 75 78 69 6c 69 61 72  Hed</a> auxiliar
7c90: 79 0a 64 61 74 61 62 61 73 65 73 29 20 77 69 74  y.databases) wit
7ca0: 68 20 74 68 65 20 74 65 78 74 20 22 3c 62 3e 2d  h the text "<b>-
7cb0: 6d 6a 3c 2f 62 3e 3c 69 3e 48 48 48 48 48 48 48  mj</b><i>HHHHHHH
7cc0: 48 3c 2f 69 3e 22 20 61 70 70 65 6e 64 65 64 20  H</i>" appended 
7cd0: 77 68 65 72 65 0a 3c 69 3e 48 48 48 48 48 48 48  where.<i>HHHHHHH
7ce0: 48 3c 2f 69 3e 20 69 73 20 61 20 72 61 6e 64 6f  H</i> is a rando
7cf0: 6d 20 33 32 2d 62 69 74 20 68 65 78 61 64 65 63  m 32-bit hexadec
7d00: 69 6d 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68  imal number.  Th
7d10: 65 0a 72 61 6e 64 6f 6d 20 3c 69 3e 48 48 48 48  e.random <i>HHHH
7d20: 48 48 48 48 3c 2f 69 3e 20 73 75 66 66 69 78 20  HHHH</i> suffix 
7d30: 63 68 61 6e 67 65 73 20 66 6f 72 20 65 76 65 72  changes for ever
7d40: 79 20 6e 65 77 20 6d 61 73 74 65 72 20 6a 6f 75  y new master jou
7d50: 72 6e 61 6c 2e 3c 2f 70 3e 0a 0a 3c 70 3e 3c 69  rnal.</p>..<p><i
7d60: 3e 28 4e 6f 74 61 20 62 65 6e 65 3a 20 54 68 65  >(Nota bene: The
7d70: 20 66 6f 72 6d 75 6c 61 20 66 6f 72 20 63 6f 6d   formula for com
7d80: 70 75 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65  puting the maste
7d90: 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61  r journal filena
7da0: 6d 65 0a 67 69 76 65 6e 20 69 6e 20 74 68 65 20  me.given in the 
7db0: 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61  previous paragra
7dc0: 70 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ph corresponds t
7dd0: 6f 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  o the implementa
7de0: 74 69 6f 6e 20 61 73 0a 6f 66 20 53 51 4c 69 74  tion as.of SQLit
7df0: 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e  e version 3.5.0.
7e00: 20 20 42 75 74 20 74 68 69 73 20 66 6f 72 6d 75    But this formu
7e10: 6c 61 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f  la is not part o
7e20: 66 20 74 68 65 20 53 51 4c 69 74 65 0a 73 70 65  f the SQLite.spe
7e30: 63 69 66 69 63 61 74 69 6f 6e 20 61 6e 64 20 69  cification and i
7e40: 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
7e50: 6e 67 65 20 69 6e 20 66 75 74 75 72 65 20 72 65  nge in future re
7e60: 6c 65 61 73 65 73 2e 29 3c 2f 69 3e 3c 2f 70 3e  leases.)</i></p>
7e70: 0a 0a 3c 70 3e 55 6e 6c 69 6b 65 20 74 68 65 20  ..<p>Unlike the 
7e80: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
7e90: 73 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  s, the master jo
7ea0: 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 63  urnal does not c
7eb0: 6f 6e 74 61 69 6e 0a 61 6e 79 20 6f 72 69 67 69  ontain.any origi
7ec0: 6e 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67  nal database pag
7ed0: 65 20 63 6f 6e 74 65 6e 74 2e 20 20 49 6e 73 74  e content.  Inst
7ee0: 65 61 64 2c 20 74 68 65 20 6d 61 73 74 65 72 20  ead, the master 
7ef0: 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73  journal contains
7f00: 0a 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61  .the full pathna
7f10: 6d 65 73 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b  mes for rollback
7f20: 20 6a 6f 75 72 6e 61 6c 73 20 66 6f 72 20 65 76   journals for ev
7f30: 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68 61  ery database tha
7f40: 74 20 69 73 0a 70 61 72 74 69 63 69 70 61 74 69  t is.participati
7f50: 6e 67 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  ng in the transa
7f60: 63 74 69 6f 6e 2e 3c 2f 70 3e 0a 0a 3c 70 3e 41  ction.</p>..<p>A
7f70: 66 74 65 72 20 74 68 65 20 6d 61 73 74 65 72 20  fter the master 
7f80: 6a 6f 75 72 6e 61 6c 20 69 73 20 63 6f 6e 73 74  journal is const
7f90: 72 75 63 74 65 64 2c 20 69 74 73 20 63 6f 6e 74  ructed, its cont
7fa0: 65 6e 74 20 69 73 20 66 6c 75 73 68 65 64 0a 74  ent is flushed.t
7fb0: 6f 20 64 69 73 6b 20 62 65 66 6f 72 65 20 61 6e  o disk before an
7fc0: 79 20 66 75 72 74 68 65 72 20 61 63 74 69 6f 6e  y further action
7fd0: 73 20 61 72 65 20 74 61 6b 65 6e 2e 20 20 4f 6e  s are taken.  On
7fe0: 20 55 6e 69 78 2c 20 74 68 65 20 64 69 72 65 63   Unix, the direc
7ff0: 74 6f 72 79 0a 74 68 61 74 20 63 6f 6e 74 61 69  tory.that contai
8000: 6e 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  ns the master jo
8010: 75 72 6e 61 6c 20 69 73 20 61 6c 73 6f 20 73 79  urnal is also sy
8020: 6e 63 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  nced in order to
8030: 20 6d 61 6b 65 20 73 75 72 65 0a 74 68 65 20 6d   make sure.the m
8040: 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
8050: 6c 65 20 77 69 6c 6c 20 61 70 70 65 61 72 20 69  le will appear i
8060: 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  n the directory 
8070: 66 6f 6c 6c 6f 77 69 6e 67 20 61 0a 70 6f 77 65  following a.powe
8080: 72 20 66 61 69 6c 75 72 65 2e 3c 2f 70 3e 0a 0a  r failure.</p>..
8090: 3c 62 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22  <br clear="both"
80a0: 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65  >.<tcl>hd_fragme
80b0: 6e 74 20 6d 75 6c 74 69 6a 72 6e 6c 75 70 64 61  nt multijrnlupda
80c0: 74 65 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 35 2e 33  te</tcl>.<h3>5.3
80d0: 20 55 70 64 61 74 69 6e 67 20 52 6f 6c 6c 62 61   Updating Rollba
80e0: 63 6b 20 4a 6f 75 72 6e 61 6c 20 48 65 61 64 65  ck Journal Heade
80f0: 72 73 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63  rs</h3>.<img src
8100: 3d 22 69 6d 61 67 65 73 2f 61 63 2f 6d 75 6c 74  ="images/ac/mult
8110: 69 2d 32 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22  i-2.gif" align="
8120: 72 69 67 68 74 22 20 68 73 70 61 63 65 3d 22 31  right" hspace="1
8130: 35 22 3e 0a 0a 3c 70 3e 54 68 65 20 6e 65 78 74  5">..<p>The next
8140: 20 73 74 65 70 20 69 73 20 74 6f 20 72 65 63 6f   step is to reco
8150: 72 64 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rd the full path
8160: 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74  name of the mast
8170: 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a  er journal file.
8180: 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  in the header of
8190: 20 65 76 65 72 79 20 72 6f 6c 6c 62 61 63 6b 20   every rollback 
81a0: 6a 6f 75 72 6e 61 6c 2e 20 20 53 70 61 63 65 20  journal.  Space 
81b0: 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 73 74  to hold the mast
81c0: 65 72 0a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e  er.journal filen
81d0: 61 6d 65 20 77 61 73 20 72 65 73 65 72 76 65 64  ame was reserved
81e0: 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
81f0: 67 20 6f 66 20 65 61 63 68 20 72 6f 6c 6c 62 61  g of each rollba
8200: 63 6b 20 6a 6f 75 72 6e 61 6c 0a 61 73 20 74 68  ck journal.as th
8210: 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
8220: 61 6c 73 20 77 65 72 65 20 63 72 65 61 74 65 64  als were created
8230: 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65 20 63 6f  .</p>..<p>The co
8240: 6e 74 65 6e 74 20 6f 66 20 65 61 63 68 20 72 6f  ntent of each ro
8250: 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69  llback journal i
8260: 73 20 66 6c 75 73 68 65 64 20 74 6f 20 64 69 73  s flushed to dis
8270: 6b 20 62 6f 74 68 20 62 65 66 6f 72 65 0a 61 6e  k both before.an
8280: 64 20 61 66 74 65 72 20 74 68 65 20 6d 61 73 74  d after the mast
8290: 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e  er journal filen
82a0: 61 6d 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  ame is written i
82b0: 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  nto the rollback
82c0: 0a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e  .journal header.
82d0: 20 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e    It is importan
82e0: 74 20 74 6f 20 64 6f 20 62 6f 74 68 20 6f 66 20  t to do both of 
82f0: 74 68 65 73 65 20 66 6c 75 73 68 65 73 2e 20 20  these flushes.  
8300: 46 6f 72 74 75 6e 61 74 65 6c 79 2c 0a 74 68 65  Fortunately,.the
8310: 20 73 65 63 6f 6e 64 20 66 6c 75 73 68 20 69 73   second flush is
8320: 20 75 73 75 61 6c 6c 79 20 69 6e 65 78 70 65 6e   usually inexpen
8330: 73 69 76 65 20 73 69 6e 63 65 20 74 79 70 69 63  sive since typic
8340: 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 73 69 6e 67  ally only a sing
8350: 6c 65 0a 70 61 67 65 20 6f 66 20 74 68 65 20 6a  le.page of the j
8360: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 74 68 65  ournal file (the
8370: 20 66 69 72 73 74 20 70 61 67 65 29 20 68 61 73   first page) has
8380: 20 63 68 61 6e 67 65 64 2e 3c 2f 70 3e 0a 0a 3c   changed.</p>..<
8390: 70 3e 54 68 69 73 20 73 74 65 70 20 69 73 20 61  p>This step is a
83a0: 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 0a 3c 61 20  nalogous to .<a 
83b0: 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33  href="#section_3
83c0: 5f 37 22 3e 73 74 65 70 20 33 2e 37 3c 2f 61 3e  _7">step 3.7</a>
83d0: 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 66   in the single-f
83e0: 69 6c 65 20 63 6f 6d 6d 69 74 0a 73 63 65 6e 61  ile commit.scena
83f0: 72 69 6f 20 64 65 73 63 72 69 62 65 64 20 61 62  rio described ab
8400: 6f 76 65 2e 3c 2f 70 3e 0a 0a 3c 62 72 20 63 6c  ove.</p>..<br cl
8410: 65 61 72 3d 22 62 6f 74 68 22 3e 0a 3c 74 63 6c  ear="both">.<tcl
8420: 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 6d 75 6c  >hd_fragment mul
8430: 74 69 64 62 75 70 64 61 74 65 3c 2f 74 63 6c 3e  tidbupdate</tcl>
8440: 0a 3c 68 33 3e 35 2e 34 20 55 70 64 61 74 69 6e  .<h3>5.4 Updatin
8450: 67 20 54 68 65 20 44 61 74 61 62 61 73 65 20 46  g The Database F
8460: 69 6c 65 73 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73  iles</h3>.<img s
8470: 72 63 3d 22 69 6d 61 67 65 73 2f 61 63 2f 6d 75  rc="images/ac/mu
8480: 6c 74 69 2d 33 2e 67 69 66 22 20 61 6c 69 67 6e  lti-3.gif" align
8490: 3d 22 72 69 67 68 74 22 20 68 73 70 61 63 65 3d  ="right" hspace=
84a0: 22 31 35 22 3e 0a 0a 3c 70 3e 4f 6e 63 65 20 61  "15">..<p>Once a
84b0: 6c 6c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  ll rollback jour
84c0: 6e 61 6c 20 66 69 6c 65 73 20 68 61 76 65 20 62  nal files have b
84d0: 65 65 6e 20 66 6c 75 73 68 65 64 20 74 6f 20 64  een flushed to d
84e0: 69 73 6b 2c 20 69 74 0a 69 73 20 73 61 66 65 20  isk, it.is safe 
84f0: 74 6f 20 62 65 67 69 6e 20 75 70 64 61 74 69 6e  to begin updatin
8500: 67 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  g database files
8510: 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 6f 62  .  We have to ob
8520: 74 61 69 6e 20 61 6e 0a 65 78 63 6c 75 73 69 76  tain an.exclusiv
8530: 65 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 20 64 61  e lock on all da
8540: 74 61 62 61 73 65 20 66 69 6c 65 73 20 62 65 66  tabase files bef
8550: 6f 72 65 20 77 72 69 74 69 6e 67 20 74 68 65 20  ore writing the 
8560: 63 68 61 6e 67 65 73 2e 0a 41 66 74 65 72 20 61  changes..After a
8570: 6c 6c 20 74 68 65 20 63 68 61 6e 67 65 73 20 61  ll the changes a
8580: 72 65 20 77 72 69 74 74 65 6e 2c 20 69 74 20 69  re written, it i
8590: 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 66  s important to f
85a0: 6c 75 73 68 20 74 68 65 0a 63 68 61 6e 67 65 73  lush the.changes
85b0: 20 74 6f 20 64 69 73 6b 20 73 6f 20 74 68 61 74   to disk so that
85c0: 20 74 68 65 79 20 77 69 6c 6c 20 62 65 20 70 72   they will be pr
85d0: 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 65  eserved in the e
85e0: 76 65 6e 74 20 6f 66 0a 61 20 70 6f 77 65 72 20  vent of.a power 
85f0: 66 61 69 6c 75 72 65 20 6f 72 20 6f 70 65 72 61  failure or opera
8600: 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61 73  ting system cras
8610: 68 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 69 73 20  h.</p>..<p>This 
8620: 73 74 65 70 20 63 6f 72 72 65 73 70 6f 6e 64 73  step corresponds
8630: 20 74 6f 20 73 74 65 70 73 0a 3c 61 20 68 72 65   to steps.<a hre
8640: 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 38 22  f="#section_3_8"
8650: 3e 33 2e 38 3c 2f 61 3e 2c 0a 3c 61 20 68 72 65  >3.8</a>,.<a hre
8660: 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 39 22  f="#section_3_9"
8670: 3e 33 2e 39 3c 2f 61 3e 2c 20 61 6e 64 0a 3c 61  >3.9</a>, and.<a
8680: 20 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f   href="#section_
8690: 33 5f 31 30 22 3e 33 2e 31 30 3c 2f 61 3e 20 69  3_10">3.10</a> i
86a0: 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 66 69 6c  n the single-fil
86b0: 65 20 63 6f 6d 6d 69 74 0a 73 63 65 6e 61 72 69  e commit.scenari
86c0: 6f 20 64 65 73 63 72 69 62 65 64 20 70 72 65 76  o described prev
86d0: 69 6f 75 73 6c 79 2e 3c 2f 70 3e 0a 0a 0a 3c 62  iously.</p>...<b
86e0: 72 20 63 6c 65 61 72 3d 22 62 6f 74 68 22 3e 0a  r clear="both">.
86f0: 3c 61 20 6e 61 6d 65 3d 22 73 65 63 74 69 6f 6e  <a name="section
8700: 5f 35 5f 35 22 3e 3c 2f 61 3e 0a 3c 68 33 3e 35  _5_5"></a>.<h3>5
8710: 2e 35 20 44 65 6c 65 74 65 20 54 68 65 20 4d 61  .5 Delete The Ma
8720: 73 74 65 72 20 4a 6f 75 72 6e 61 6c 20 46 69 6c  ster Journal Fil
8730: 65 3c 2f 68 33 3e 0a 3c 69 6d 67 20 73 72 63 3d  e</h3>.<img src=
8740: 22 69 6d 61 67 65 73 2f 61 63 2f 6d 75 6c 74 69  "images/ac/multi
8750: 2d 34 2e 67 69 66 22 20 61 6c 69 67 6e 3d 22 72  -4.gif" align="r
8760: 69 67 68 74 22 20 68 73 70 61 63 65 3d 22 31 35  ight" hspace="15
8770: 22 3e 0a 0a 3c 70 3e 54 68 65 20 6e 65 78 74 20  ">..<p>The next 
8780: 73 74 65 70 20 69 73 20 74 6f 20 64 65 6c 65 74  step is to delet
8790: 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
87a0: 72 6e 61 6c 20 66 69 6c 65 2e 0a 54 68 69 73 20  rnal file..This 
87b0: 69 73 20 74 68 65 20 70 6f 69 6e 74 20 77 68 65  is the point whe
87c0: 72 65 20 74 68 65 20 6d 75 6c 74 69 2d 66 69 6c  re the multi-fil
87d0: 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
87e0: 6d 6d 69 74 73 2e 0a 54 68 69 73 20 73 74 65 70  mmits..This step
87f0: 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
8800: 0a 3c 61 20 68 72 65 66 3d 22 23 73 65 63 74 69  .<a href="#secti
8810: 6f 6e 5f 33 5f 31 31 22 3e 73 74 65 70 20 33 2e  on_3_11">step 3.
8820: 31 31 3c 2f 61 3e 20 69 6e 20 74 68 65 20 73 69  11</a> in the si
8830: 6e 67 6c 65 2d 66 69 6c 65 0a 63 6f 6d 6d 69 74  ngle-file.commit
8840: 20 73 63 65 6e 61 72 69 6f 20 77 68 65 72 65 20   scenario where 
8850: 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
8860: 72 6e 61 6c 20 69 73 20 64 65 6c 65 74 65 64 2e  rnal is deleted.
8870: 3c 2f 70 3e 0a 0a 3c 70 3e 49 66 20 61 20 70 6f  </p>..<p>If a po
8880: 77 65 72 20 66 61 69 6c 75 72 65 20 6f 72 20 6f  wer failure or o
8890: 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
88a0: 63 72 61 73 68 20 6f 63 63 75 72 73 20 61 74 20  crash occurs at 
88b0: 74 68 69 73 0a 70 6f 69 6e 74 2c 20 74 68 65 20  this.point, the 
88c0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
88d0: 20 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 77 68   not rollback wh
88e0: 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20 72 65  en the system re
88f0: 62 6f 6f 74 73 0a 65 76 65 6e 20 74 68 6f 75 67  boots.even thoug
8900: 68 20 74 68 65 72 65 20 61 72 65 20 72 6f 6c 6c  h there are roll
8910: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 73 20 70 72  back journals pr
8920: 65 73 65 6e 74 2e 20 20 54 68 65 0a 64 69 66 66  esent.  The.diff
8930: 65 72 65 6e 63 65 20 69 73 20 74 68 65 20 6d 61  erence is the ma
8940: 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 74  ster journal pat
8950: 68 6e 61 6d 65 20 69 6e 20 74 68 65 20 68 65 61  hname in the hea
8960: 64 65 72 20 6f 66 20 74 68 65 0a 72 6f 6c 6c 62  der of the.rollb
8970: 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 55 70  ack journal.  Up
8980: 6f 6e 20 72 65 73 74 61 72 74 2c 20 53 51 4c 69  on restart, SQLi
8990: 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72  te only consider
89a0: 73 20 61 20 6a 6f 75 72 6e 61 6c 0a 74 6f 20 62  s a journal.to b
89b0: 65 20 68 6f 74 20 61 6e 64 20 77 69 6c 6c 20 6f  e hot and will o
89c0: 6e 6c 79 20 70 6c 61 79 62 61 63 6b 20 74 68 65  nly playback the
89d0: 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 72   journal if ther
89e0: 65 20 69 73 20 6e 6f 0a 6d 61 73 74 65 72 20 6a  e is no.master j
89f0: 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
8a00: 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 28 77  in the header (w
8a10: 68 69 63 68 20 69 73 20 74 68 65 20 63 61 73 65  hich is the case
8a20: 20 66 6f 72 0a 61 20 73 69 6e 67 6c 65 2d 66 69   for.a single-fi
8a30: 6c 65 20 63 6f 6d 6d 69 74 29 20 6f 72 20 69 66  le commit) or if
8a40: 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
8a50: 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c 0a 65  nal file still.e
8a60: 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2e 3c 2f  xists on disk.</
8a70: 70 3e 0a 0a 3c 62 72 20 63 6c 65 61 72 3d 22 62  p>..<br clear="b
8a80: 6f 74 68 22 3e 0a 3c 74 63 6c 3e 68 64 5f 66 72  oth">.<tcl>hd_fr
8a90: 61 67 6d 65 6e 74 20 63 6c 65 61 6e 75 70 3c 2f  agment cleanup</
8aa0: 74 63 6c 3e 0a 3c 68 33 3e 35 2e 36 20 43 6c 65  tcl>.<h3>5.6 Cle
8ab0: 61 6e 20 55 70 20 54 68 65 20 52 6f 6c 6c 62 61  an Up The Rollba
8ac0: 63 6b 20 4a 6f 75 72 6e 61 6c 73 3c 2f 68 33 3e  ck Journals</h3>
8ad0: 0a 3c 69 6d 67 20 73 72 63 3d 22 69 6d 61 67 65  .<img src="image
8ae0: 73 2f 61 63 2f 6d 75 6c 74 69 2d 35 2e 67 69 66  s/ac/multi-5.gif
8af0: 22 20 61 6c 69 67 6e 3d 22 72 69 67 68 74 22 20  " align="right" 
8b00: 68 73 70 61 63 65 3d 22 31 35 22 3e 0a 0a 3c 70  hspace="15">..<p
8b10: 3e 54 68 65 20 66 69 6e 61 6c 20 73 74 65 70 20  >The final step 
8b20: 69 6e 20 61 20 6d 75 6c 74 69 2d 66 69 6c 65 20  in a multi-file 
8b30: 63 6f 6d 6d 69 74 20 69 73 20 74 6f 20 64 65 6c  commit is to del
8b40: 65 74 65 20 74 68 65 0a 69 6e 64 69 76 69 64 75  ete the.individu
8b50: 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  al rollback jour
8b60: 6e 61 6c 73 20 61 6e 64 20 64 72 6f 70 20 74 68  nals and drop th
8b70: 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
8b80: 73 20 6f 6e 0a 74 68 65 20 64 61 74 61 62 61 73  s on.the databas
8b90: 65 20 66 69 6c 65 73 20 73 6f 20 74 68 61 74 20  e files so that 
8ba0: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
8bb0: 63 61 6e 20 73 65 65 20 74 68 65 20 63 68 61 6e  can see the chan
8bc0: 67 65 73 2e 0a 54 68 69 73 20 63 6f 72 72 65 73  ges..This corres
8bd0: 70 6f 6e 64 73 20 74 6f 20 0a 3c 61 20 68 72 65  ponds to .<a hre
8be0: 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 31 32  f="#section_3_12
8bf0: 22 3e 73 74 65 70 20 33 2e 31 32 3c 2f 61 3e 20  ">step 3.12</a> 
8c00: 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 66 69  in the single-fi
8c10: 6c 65 0a 63 6f 6d 6d 69 74 20 73 65 71 75 65 6e  le.commit sequen
8c20: 63 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 54 68 65 20  ce.</p>..<p>The 
8c30: 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20  transaction has 
8c40: 61 6c 72 65 61 64 79 20 63 6f 6d 6d 69 74 74 65  already committe
8c50: 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  d at this point 
8c60: 73 6f 20 74 69 6d 69 6e 67 0a 69 73 20 6e 6f 74  so timing.is not
8c70: 20 63 72 69 74 69 63 61 6c 20 69 6e 20 74 68 65   critical in the
8c80: 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 74 68 65   deletion of the
8c90: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
8ca0: 6c 73 2e 0a 54 68 65 20 63 75 72 72 65 6e 74 20  ls..The current 
8cb0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
8cc0: 65 6c 65 74 65 73 20 61 20 73 69 6e 67 6c 65 20  eletes a single 
8cd0: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
8ce0: 0a 74 68 65 6e 20 75 6e 6c 6f 63 6b 73 20 74 68  .then unlocks th
8cf0: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
8d00: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65  database file be
8d10: 66 6f 72 65 20 70 72 6f 63 65 65 64 69 6e 67 0a  fore proceeding.
8d20: 74 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 6c 6c  to the next roll
8d30: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 42  back journal.  B
8d40: 75 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  ut in the future
8d50: 20 77 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65   we might change
8d60: 0a 74 68 69 73 20 73 6f 20 74 68 61 74 20 61 6c  .this so that al
8d70: 6c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  l rollback journ
8d80: 61 6c 73 20 61 72 65 20 64 65 6c 65 74 65 64 20  als are deleted 
8d90: 62 65 66 6f 72 65 20 61 6e 79 20 64 61 74 61 62  before any datab
8da0: 61 73 65 0a 66 69 6c 65 73 20 61 72 65 20 75 6e  ase.files are un
8db0: 6c 6f 63 6b 65 64 2e 20 20 41 73 20 6c 6f 6e 67  locked.  As long
8dc0: 20 61 73 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   as the rollback
8dd0: 20 6a 6f 75 72 6e 61 6c 20 69 73 20 64 65 6c 65   journal is dele
8de0: 74 65 64 20 62 65 66 6f 72 65 0a 69 74 73 20 63  ted before.its c
8df0: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61 74  orresponding dat
8e00: 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 6e  abase file is un
8e10: 6c 6f 63 6b 65 64 20 69 74 20 64 6f 65 73 20 6e  locked it does n
8e20: 6f 74 20 6d 61 74 74 65 72 20 69 6e 20 77 68 61  ot matter in wha
8e30: 74 0a 6f 72 64 65 72 20 74 68 65 20 72 6f 6c 6c  t.order the roll
8e40: 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 73 20 61 72  back journals ar
8e50: 65 20 64 65 6c 65 74 65 64 20 6f 72 20 74 68 65  e deleted or the
8e60: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
8e70: 61 72 65 0a 75 6e 6c 6f 63 6b 65 64 2e 3c 2f 70  are.unlocked.</p
8e80: 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d  >..<tcl>hd_fragm
8e90: 65 6e 74 20 6d 6f 72 65 64 65 74 61 69 6c 3c 2f  ent moredetail</
8ea0: 74 63 6c 3e 0a 3c 68 32 3e 36 2e 30 20 41 64 64  tcl>.<h2>6.0 Add
8eb0: 69 74 69 6f 6e 61 6c 20 44 65 74 61 69 6c 73 20  itional Details 
8ec0: 4f 66 20 54 68 65 20 43 6f 6d 6d 69 74 20 50 72  Of The Commit Pr
8ed0: 6f 63 65 73 73 3c 2f 68 32 3e 0a 0a 3c 70 3e 3c  ocess</h2>..<p><
8ee0: 61 20 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e  a href="#section
8ef0: 5f 33 5f 30 22 3e 53 65 63 74 69 6f 6e 20 33 2e  _3_0">Section 3.
8f00: 30 3c 2f 61 3e 20 61 62 6f 76 65 20 70 72 6f 76  0</a> above prov
8f10: 69 64 65 73 20 61 6e 20 6f 76 65 72 76 69 65 77  ides an overview
8f20: 20 6f 66 0a 68 6f 77 20 61 74 6f 6d 69 63 20 63   of.how atomic c
8f30: 6f 6d 6d 69 74 20 77 6f 72 6b 73 20 69 6e 20 53  ommit works in S
8f40: 51 4c 69 74 65 2e 20 20 42 75 74 20 69 74 20 67  QLite.  But it g
8f50: 6c 6f 73 73 65 73 20 6f 76 65 72 20 61 20 6e 75  losses over a nu
8f60: 6d 62 65 72 20 6f 66 0a 69 6d 70 6f 72 74 61 6e  mber of.importan
8f70: 74 20 64 65 74 61 69 6c 73 2e 20 20 54 68 65 20  t details.  The 
8f80: 66 6f 6c 6c 6f 77 69 6e 67 20 73 75 62 73 65 63  following subsec
8f90: 74 69 6f 6e 73 20 77 69 6c 6c 20 61 74 74 65 6d  tions will attem
8fa0: 70 74 20 74 6f 20 66 69 6c 6c 0a 69 6e 20 74 68  pt to fill.in th
8fb0: 65 20 67 61 70 73 2e 3c 2f 70 3e 0a 0a 3c 74 63  e gaps.</p>..<tc
8fc0: 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 63 6f  l>hd_fragment co
8fd0: 6d 70 6c 65 74 65 73 65 63 74 6f 72 73 3c 2f 74  mpletesectors</t
8fe0: 63 6c 3e 0a 3c 68 33 3e 36 2e 31 20 41 6c 77 61  cl>.<h3>6.1 Alwa
8ff0: 79 73 20 4a 6f 75 72 6e 61 6c 20 43 6f 6d 70 6c  ys Journal Compl
9000: 65 74 65 20 53 65 63 74 6f 72 73 3c 2f 68 33 3e  ete Sectors</h3>
9010: 0a 0a 3c 70 3e 57 68 65 6e 20 74 68 65 20 6f 72  ..<p>When the or
9020: 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e 74 20 6f  iginal content o
9030: 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  f a database pag
9040: 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  e is written int
9050: 6f 0a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  o.the rollback j
9060: 6f 75 72 6e 61 6c 20 28 61 73 20 73 68 6f 77 6e  ournal (as shown
9070: 20 69 6e 20 3c 61 20 68 72 65 66 3d 22 23 73 65   in <a href="#se
9080: 63 74 69 6f 6e 5f 33 5f 35 22 3e 73 65 63 74 69  ction_3_5">secti
9090: 6f 6e 20 33 2e 35 3c 2f 61 3e 29 2c 0a 53 51 4c  on 3.5</a>),.SQL
90a0: 69 74 65 20 61 6c 77 61 79 73 20 77 72 69 74 65  ite always write
90b0: 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 73 65 63  s a complete sec
90c0: 74 6f 72 73 20 77 6f 72 74 68 20 6f 66 20 64 61  tors worth of da
90d0: 74 61 2c 20 65 76 65 6e 20 69 66 20 74 68 65 0a  ta, even if the.
90e0: 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65  page size of the
90f0: 20 64 61 74 61 62 61 73 65 20 69 73 20 73 6d 61   database is sma
9100: 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 73 65  ller than the se
9110: 63 74 6f 72 20 73 69 7a 65 2e 20 20 0a 48 69 73  ctor size.  .His
9120: 74 6f 72 69 63 61 6c 6c 79 2c 20 74 68 65 20 73  torically, the s
9130: 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 53 51  ector size in SQ
9140: 4c 69 74 65 20 68 61 73 20 62 65 65 6e 20 68 61  Lite has been ha
9150: 72 64 20 63 6f 64 65 64 20 74 6f 20 35 31 32 0a  rd coded to 512.
9160: 62 79 74 65 73 20 61 6e 64 20 73 69 6e 63 65 20  bytes and since 
9170: 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65  the minimum page
9180: 20 73 69 7a 65 20 69 73 20 61 6c 73 6f 20 35 31   size is also 51
9190: 32 20 62 79 74 65 73 2c 20 74 68 69 73 20 68 61  2 bytes, this ha
91a0: 73 20 6e 65 76 65 72 0a 62 65 65 6e 20 61 6e 20  s never.been an 
91b0: 69 73 73 75 65 2e 20 20 42 75 74 20 62 65 67 69  issue.  But begi
91c0: 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69 74  nning with SQLit
91d0: 65 20 76 65 72 73 69 6f 6e 20 33 2e 33 2e 31 34  e version 3.3.14
91e0: 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  , it is possible
91f0: 0a 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75  .for SQLite to u
9200: 73 65 20 6d 61 73 73 20 73 74 6f 72 61 67 65 20  se mass storage 
9210: 64 65 76 69 63 65 73 20 77 69 74 68 20 61 20 73  devices with a s
9220: 65 63 74 6f 72 20 73 69 7a 65 20 6c 61 72 67 65  ector size large
9230: 72 20 74 68 61 6e 20 35 31 32 0a 62 79 74 65 73  r than 512.bytes
9240: 2e 20 20 53 6f 2c 20 62 65 67 69 6e 6e 69 6e 67  .  So, beginning
9250: 20 77 69 74 68 20 76 65 72 73 69 6f 6e 20 33 2e   with version 3.
9260: 33 2e 31 34 2c 20 77 68 65 6e 65 76 65 72 20 61  3.14, whenever a
9270: 6e 79 20 70 61 67 65 20 77 69 74 68 69 6e 20 61  ny page within a
9280: 0a 73 65 63 74 6f 72 20 69 73 20 77 72 69 74 74  .sector is writt
9290: 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  en into the jour
92a0: 6e 61 6c 20 66 69 6c 65 2c 20 61 6c 6c 20 70 61  nal file, all pa
92b0: 67 65 73 20 69 6e 20 74 68 61 74 20 73 61 6d 65  ges in that same
92c0: 20 73 65 63 74 6f 72 0a 61 72 65 20 73 74 6f 72   sector.are stor
92d0: 65 64 20 77 69 74 68 20 69 74 2e 3c 2f 70 3e 0a  ed with it.</p>.
92e0: 0a 3c 70 3e 49 74 20 69 73 20 69 6d 70 6f 72 74  .<p>It is import
92f0: 61 6e 74 20 74 6f 20 73 74 6f 72 65 20 61 6c 6c  ant to store all
9300: 20 70 61 67 65 73 20 6f 66 20 61 20 73 65 63 74   pages of a sect
9310: 6f 72 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61  or in the rollba
9320: 63 6b 0a 6a 6f 75 72 6e 61 6c 20 69 6e 20 6f 72  ck.journal in or
9330: 64 65 72 20 74 6f 20 70 72 65 76 65 6e 74 20 64  der to prevent d
9340: 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
9350: 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70  on following a p
9360: 6f 77 65 72 0a 6c 6f 73 73 20 77 68 69 6c 65 20  ower.loss while 
9370: 77 72 69 74 69 6e 67 20 74 68 65 20 73 65 63 74  writing the sect
9380: 6f 72 2e 20 20 53 75 70 70 6f 73 65 20 74 68 61  or.  Suppose tha
9390: 74 20 70 61 67 65 73 20 31 2c 20 32 2c 20 33 2c  t pages 1, 2, 3,
93a0: 20 61 6e 64 20 34 20 61 72 65 0a 61 6c 6c 20 73   and 4 are.all s
93b0: 74 6f 72 65 64 20 69 6e 20 73 65 63 74 6f 72 20  tored in sector 
93c0: 31 20 61 6e 64 20 74 68 61 74 20 70 61 67 65 20  1 and that page 
93d0: 32 20 69 73 20 6d 6f 64 69 66 69 65 64 2e 20 20  2 is modified.  
93e0: 49 6e 20 6f 72 64 65 72 20 74 6f 20 77 72 69 74  In order to writ
93f0: 65 0a 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f  e.the changes to
9400: 20 70 61 67 65 20 32 2c 20 74 68 65 20 75 6e 64   page 2, the und
9410: 65 72 6c 79 69 6e 67 20 68 61 72 64 77 61 72 65  erlying hardware
9420: 20 6d 75 73 74 20 61 6c 73 6f 20 72 65 77 72 69   must also rewri
9430: 74 65 20 74 68 65 0a 63 6f 6e 74 65 6e 74 20 6f  te the.content o
9440: 66 20 70 61 67 65 73 20 31 2c 20 33 2c 20 61 6e  f pages 1, 3, an
9450: 64 20 34 20 73 69 6e 63 65 20 74 68 65 20 68 61  d 4 since the ha
9460: 72 64 77 61 72 65 20 6d 75 73 74 20 77 72 69 74  rdware must writ
9470: 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 73  e the complete.s
9480: 65 63 74 6f 72 2e 20 20 49 66 20 74 68 69 73 20  ector.  If this 
9490: 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20  write operation 
94a0: 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 62  is interrupted b
94b0: 79 20 61 20 70 6f 77 65 72 20 6f 75 74 61 67 65  y a power outage
94c0: 2c 0a 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  ,.one or more of
94d0: 20 74 68 65 20 70 61 67 65 73 20 31 2c 20 33 2c   the pages 1, 3,
94e0: 20 6f 72 20 34 20 6d 69 67 68 74 20 62 65 20 6c   or 4 might be l
94f0: 65 66 74 20 77 69 74 68 20 69 6e 63 6f 72 72 65  eft with incorre
9500: 63 74 20 64 61 74 61 2e 0a 48 65 6e 63 65 2c 20  ct data..Hence, 
9510: 74 6f 20 61 76 6f 69 64 20 6c 61 73 74 69 6e 67  to avoid lasting
9520: 20 63 6f 72 72 75 70 74 69 6f 6e 20 74 6f 20 74   corruption to t
9530: 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  he database, the
9540: 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e   original conten
9550: 74 0a 6f 66 20 61 6c 6c 20 6f 66 20 74 68 6f 73  t.of all of thos
9560: 65 20 70 61 67 65 73 20 6d 75 73 74 20 62 65 20  e pages must be 
9570: 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
9580: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
9590: 6c 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f  l.</p>..<tcl>hd_
95a0: 66 72 61 67 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  fragment journal
95b0: 67 61 72 62 61 67 65 3c 2f 74 63 6c 3e 0a 3c 68  garbage</tcl>.<h
95c0: 33 3e 36 2e 32 20 44 65 61 6c 69 6e 67 20 57 69  3>6.2 Dealing Wi
95d0: 74 68 20 47 61 72 62 61 67 65 20 57 72 69 74 74  th Garbage Writt
95e0: 65 6e 20 49 6e 74 6f 20 4a 6f 75 72 6e 61 6c 20  en Into Journal 
95f0: 46 69 6c 65 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 57  Files</h3>..<p>W
9600: 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65  hen data is appe
9610: 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20  nded to the end 
9620: 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  of the rollback 
9630: 6a 6f 75 72 6e 61 6c 2c 0a 53 51 4c 69 74 65 20  journal,.SQLite 
9640: 6e 6f 72 6d 61 6c 6c 79 20 6d 61 6b 65 73 20 74  normally makes t
9650: 68 65 20 70 65 73 73 69 6d 69 73 74 69 63 20 61  he pessimistic a
9660: 73 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74  ssumption that t
9670: 68 65 20 66 69 6c 65 0a 69 73 20 66 69 72 73 74  he file.is first
9680: 20 65 78 74 65 6e 64 65 64 20 77 69 74 68 20 69   extended with i
9690: 6e 76 61 6c 69 64 20 22 67 61 72 62 61 67 65 22  nvalid "garbage"
96a0: 20 64 61 74 61 20 61 6e 64 20 74 68 61 74 20 61   data and that a
96b0: 66 74 65 72 77 61 72 64 73 0a 74 68 65 20 63 6f  fterwards.the co
96c0: 72 72 65 63 74 20 64 61 74 61 20 72 65 70 6c 61  rrect data repla
96d0: 63 65 73 20 74 68 65 20 67 61 72 62 61 67 65 2e  ces the garbage.
96e0: 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
96f0: 2c 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73  , SQLite assumes
9700: 0a 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 73  .that the file s
9710: 69 7a 65 20 69 73 20 69 6e 63 72 65 61 73 65 64  ize is increased
9720: 20 66 69 72 73 74 20 61 6e 64 20 74 68 65 6e 20   first and then 
9730: 61 66 74 65 72 77 61 72 64 73 20 74 68 65 20 63  afterwards the c
9740: 6f 6e 74 65 6e 74 0a 69 73 20 77 72 69 74 74 65  ontent.is writte
9750: 6e 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e  n into the file.
9760: 20 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69    If a power fai
9770: 6c 75 72 65 20 6f 63 63 75 72 73 20 61 66 74 65  lure occurs afte
9780: 72 20 74 68 65 20 66 69 6c 65 0a 73 69 7a 65 20  r the file.size 
9790: 68 61 73 20 62 65 65 6e 20 69 6e 63 72 65 61 73  has been increas
97a0: 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74 68  ed but before th
97b0: 65 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 20 68  e file content h
97c0: 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 2c  as been written,
97d0: 0a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  .the rollback jo
97e0: 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 6c 65 66  urnal can be lef
97f0: 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 61 72  t containing gar
9800: 62 61 67 65 20 64 61 74 61 2e 20 20 49 66 20 61  bage data.  If a
9810: 66 74 65 72 0a 70 6f 77 65 72 20 69 73 20 72 65  fter.power is re
9820: 73 74 6f 72 65 64 2c 20 61 6e 6f 74 68 65 72 20  stored, another 
9830: 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 20 73  SQLite process s
9840: 65 65 73 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  ees the rollback
9850: 20 6a 6f 75 72 6e 61 6c 0a 63 6f 6e 74 61 69 6e   journal.contain
9860: 69 6e 67 20 74 68 65 20 67 61 72 62 61 67 65 20  ing the garbage 
9870: 64 61 74 61 20 61 6e 64 20 74 72 69 65 73 20 74  data and tries t
9880: 6f 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 20 69  o roll it back i
9890: 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  nto the original
98a0: 0a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20  .database file, 
98b0: 69 74 20 6d 69 67 68 74 20 63 6f 70 79 20 73 6f  it might copy so
98c0: 6d 65 20 6f 66 20 74 68 65 20 67 61 72 62 61 67  me of the garbag
98d0: 65 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  e into the datab
98e0: 61 73 65 20 66 69 6c 65 0a 61 6e 64 20 74 68 75  ase file.and thu
98f0: 73 20 63 6f 72 72 75 70 74 20 74 68 65 20 64 61  s corrupt the da
9900: 74 61 62 61 73 65 20 66 69 6c 65 2e 3c 2f 70 3e  tabase file.</p>
9910: 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 75 73 65 73  ..<p>SQLite uses
9920: 20 74 77 6f 20 64 65 66 65 6e 73 65 73 20 61 67   two defenses ag
9930: 61 69 6e 73 74 20 74 68 69 73 20 70 72 6f 62 6c  ainst this probl
9940: 65 6d 2e 20 20 49 6e 20 74 68 65 20 66 69 72 73  em.  In the firs
9950: 74 20 70 6c 61 63 65 2c 0a 53 51 4c 69 74 65 20  t place,.SQLite 
9960: 72 65 63 6f 72 64 73 20 74 68 65 20 6e 75 6d 62  records the numb
9970: 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
9980: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
9990: 6e 61 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65  nal in the heade
99a0: 72 0a 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63  r.of the rollbac
99b0: 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 69 73  k journal.  This
99c0: 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 69 74 69   number is initi
99d0: 61 6c 6c 79 20 7a 65 72 6f 2e 20 20 53 6f 20 64  ally zero.  So d
99e0: 75 72 69 6e 67 20 61 6e 0a 61 74 74 65 6d 70 74  uring an.attempt
99f0: 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20   to rollback an 
9a00: 69 6e 63 6f 6d 70 6c 65 74 65 20 28 61 6e 64 20  incomplete (and 
9a10: 70 6f 73 73 69 62 6c 79 20 63 6f 72 72 75 70 74  possibly corrupt
9a20: 29 20 72 6f 6c 6c 62 61 63 6b 0a 6a 6f 75 72 6e  ) rollback.journ
9a30: 61 6c 2c 20 74 68 65 20 70 72 6f 63 65 73 73 20  al, the process 
9a40: 64 6f 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61  doing the rollba
9a50: 63 6b 20 77 69 6c 6c 20 73 65 65 20 74 68 61 74  ck will see that
9a60: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 63 6f 6e   the journal.con
9a70: 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65 73  tains zero pages
9a80: 20 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20 6d   and will thus m
9a90: 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74  ake no changes t
9aa0: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
9ab0: 20 50 72 69 6f 72 0a 74 6f 20 61 20 63 6f 6d 6d   Prior.to a comm
9ac0: 69 74 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  it, the rollback
9ad0: 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 6c 75 73   journal is flus
9ae0: 68 65 64 20 74 6f 20 64 69 73 6b 20 74 6f 20 65  hed to disk to e
9af0: 6e 73 75 72 65 20 74 68 61 74 0a 61 6c 6c 20 63  nsure that.all c
9b00: 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20  ontent has been 
9b10: 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 61  synced to disk a
9b20: 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22  nd there is no "
9b30: 67 61 72 62 61 67 65 22 20 6c 65 66 74 0a 69 6e  garbage" left.in
9b40: 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 20 6f   the file, and o
9b50: 6e 6c 79 20 74 68 65 6e 20 69 73 20 74 68 65 20  nly then is the 
9b60: 70 61 67 65 20 63 6f 75 6e 74 20 69 6e 20 74 68  page count in th
9b70: 65 20 68 65 61 64 65 72 20 63 68 61 6e 67 65 64  e header changed
9b80: 20 66 72 6f 6d 0a 7a 65 72 6f 20 74 6f 20 74 72   from.zero to tr
9b90: 75 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ue number of pag
9ba0: 65 73 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61  es in the rollba
9bb0: 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65  ck journal.  The
9bc0: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
9bd0: 6c 0a 68 65 61 64 65 72 20 69 73 20 61 6c 77 61  l.header is alwa
9be0: 79 73 20 6b 65 70 74 20 69 6e 20 61 20 73 65 70  ys kept in a sep
9bf0: 61 72 61 74 65 20 73 65 63 74 6f 72 20 66 72 6f  arate sector fro
9c00: 6d 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20  m any page data 
9c10: 73 6f 20 74 68 61 74 0a 69 74 20 63 61 6e 20 62  so that.it can b
9c20: 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 61 6e  e overwritten an
9c30: 64 20 66 6c 75 73 68 65 64 20 77 69 74 68 6f 75  d flushed withou
9c40: 74 20 72 69 73 6b 69 6e 67 20 64 61 6d 61 67 65  t risking damage
9c50: 20 74 6f 20 61 20 64 61 74 61 0a 70 61 67 65 20   to a data.page 
9c60: 69 66 20 61 20 70 6f 77 65 72 20 6f 75 74 61 67  if a power outag
9c70: 65 20 6f 63 63 75 72 73 2e 20 20 4e 6f 74 69 63  e occurs.  Notic
9c80: 65 20 74 68 61 74 20 74 68 65 20 72 6f 6c 6c 62  e that the rollb
9c90: 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 69 73 20 66  ack journal.is f
9ca0: 6c 75 73 68 65 64 20 74 6f 20 64 69 73 6b 20 74  lushed to disk t
9cb0: 77 69 63 65 3a 20 6f 6e 63 65 20 74 6f 20 77 72  wice: once to wr
9cc0: 69 74 65 20 74 68 65 20 70 61 67 65 20 63 6f 6e  ite the page con
9cd0: 74 65 6e 74 20 61 6e 64 20 61 20 73 65 63 6f 6e  tent and a secon
9ce0: 64 0a 74 69 6d 65 20 74 6f 20 77 72 69 74 65 20  d.time to write 
9cf0: 74 68 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69  the page count i
9d00: 6e 20 74 68 65 20 68 65 61 64 65 72 2e 3c 2f 70  n the header.</p
9d10: 3e 0a 0a 3c 70 3e 54 68 65 20 70 72 65 76 69 6f  >..<p>The previo
9d20: 75 73 20 70 61 72 61 67 72 61 70 68 20 64 65 73  us paragraph des
9d30: 63 72 69 62 65 73 20 77 68 61 74 20 68 61 70 70  cribes what happ
9d40: 65 6e 73 20 77 68 65 6e 20 74 68 65 0a 73 79 6e  ens when the.syn
9d50: 63 68 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 20  chronous pragma 
9d60: 73 65 74 74 69 6e 67 20 69 73 20 22 66 75 6c 6c  setting is "full
9d70: 22 2e 3c 2f 70 3e 0a 0a 3c 62 6c 6f 63 6b 71 75  ".</p>..<blockqu
9d80: 6f 74 65 3e 0a 50 52 41 47 4d 41 20 73 79 6e 63  ote>.PRAGMA sync
9d90: 68 72 6f 6e 6f 75 73 3d 46 55 4c 4c 3b 0a 3c 2f  hronous=FULL;.</
9da0: 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 0a 3c 70 3e  blockquote>..<p>
9db0: 54 68 65 20 64 65 66 61 75 6c 74 20 73 79 6e 63  The default sync
9dc0: 68 72 6f 6e 6f 75 73 20 73 65 74 74 69 6e 67 20  hronous setting 
9dd0: 69 73 20 66 75 6c 6c 20 73 6f 20 74 68 65 20 61  is full so the a
9de0: 62 6f 76 65 20 69 73 20 77 68 61 74 20 75 73 75  bove is what usu
9df0: 61 6c 6c 79 0a 68 61 70 70 65 6e 73 2e 20 20 48  ally.happens.  H
9e00: 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 73  owever, if the s
9e10: 79 6e 63 68 72 6f 6e 6f 75 73 20 73 65 74 74 69  ynchronous setti
9e20: 6e 67 20 69 73 20 6c 6f 77 65 72 65 64 20 74 6f  ng is lowered to
9e30: 20 22 6e 6f 72 6d 61 6c 22 2c 0a 53 51 4c 69 74   "normal",.SQLit
9e40: 65 20 6f 6e 6c 79 20 66 6c 75 73 68 65 73 20 74  e only flushes t
9e50: 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
9e60: 6e 61 6c 20 6f 6e 63 65 2c 20 61 66 74 65 72 20  nal once, after 
9e70: 74 68 65 20 70 61 67 65 20 63 6f 75 6e 74 20 68  the page count h
9e80: 61 73 0a 62 65 65 6e 20 77 72 69 74 74 65 6e 2e  as.been written.
9e90: 0a 54 68 69 73 20 63 61 72 72 69 65 73 20 61 20  .This carries a 
9ea0: 72 69 73 6b 20 6f 66 20 63 6f 72 72 75 70 74 69  risk of corrupti
9eb0: 6f 6e 20 62 65 63 61 75 73 65 20 69 74 20 6d 69  on because it mi
9ec0: 67 68 74 20 68 61 70 70 65 6e 20 74 68 61 74 20  ght happen that 
9ed0: 74 68 65 20 0a 6d 6f 64 69 66 69 65 64 20 28 6e  the .modified (n
9ee0: 6f 6e 2d 7a 65 72 6f 29 20 70 61 67 65 20 63 6f  on-zero) page co
9ef0: 75 6e 74 20 72 65 61 63 68 65 73 20 74 68 65 20  unt reaches the 
9f00: 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66  disk surface bef
9f10: 6f 72 65 20 61 6c 6c 0a 6f 66 20 74 68 65 20 64  ore all.of the d
9f20: 61 74 61 20 64 6f 65 73 2e 20 20 54 68 65 20 64  ata does.  The d
9f30: 61 74 61 20 77 69 6c 6c 20 68 61 76 65 20 62 65  ata will have be
9f40: 65 6e 20 77 72 69 74 74 65 6e 20 66 69 72 73 74  en written first
9f50: 2c 20 62 75 74 20 53 51 4c 69 74 65 0a 61 73 73  , but SQLite.ass
9f60: 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 75 6e  umes that the un
9f70: 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 73 79 73  derlying filesys
9f80: 74 65 6d 20 63 61 6e 20 72 65 6f 72 64 65 72 20  tem can reorder 
9f90: 77 72 69 74 65 20 72 65 71 75 65 73 74 73 20 61  write requests a
9fa0: 6e 64 0a 74 68 61 74 20 74 68 65 20 70 61 67 65  nd.that the page
9fb0: 20 63 6f 75 6e 74 20 63 61 6e 20 62 65 20 62 75   count can be bu
9fc0: 72 6e 65 64 20 69 6e 74 6f 20 6f 78 69 64 65 20  rned into oxide 
9fd0: 66 69 72 73 74 20 65 76 65 6e 20 74 68 6f 75 67  first even thoug
9fe0: 68 20 69 74 73 0a 77 72 69 74 65 20 72 65 71 75  h its.write requ
9ff0: 65 73 74 20 6f 63 63 75 72 72 65 64 20 6c 61 73  est occurred las
a000: 74 2e 20 20 53 6f 20 61 73 20 61 20 73 65 63 6f  t.  So as a seco
a010: 6e 64 20 6c 69 6e 65 20 6f 66 20 64 65 66 65 6e  nd line of defen
a020: 73 65 2c 20 53 51 4c 69 74 65 0a 61 6c 73 6f 20  se, SQLite.also 
a030: 75 73 65 73 20 61 20 33 32 2d 62 69 74 20 63 68  uses a 32-bit ch
a040: 65 63 6b 73 75 6d 20 6f 6e 20 65 76 65 72 79 20  ecksum on every 
a050: 70 61 67 65 20 6f 66 20 64 61 74 61 20 69 6e 20  page of data in 
a060: 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 6a 6f 75  the rollback.jou
a070: 72 6e 61 6c 2e 20 20 54 68 69 73 20 63 68 65 63  rnal.  This chec
a080: 6b 73 75 6d 20 69 73 20 65 76 61 6c 75 61 74 65  ksum is evaluate
a090: 64 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  d for each page 
a0a0: 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 0a  during rollback.
a0b0: 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 20 62 61  while rolling ba
a0c0: 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 61 73 20  ck a journal as 
a0d0: 64 65 73 63 72 69 62 65 64 20 69 6e 20 0a 3c 61  described in .<a
a0e0: 20 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f   href="#section_
a0f0: 34 5f 34 22 3e 73 65 63 74 69 6f 6e 20 34 2e 34  4_4">section 4.4
a100: 3c 2f 61 3e 2e 20 20 49 66 20 61 6e 20 69 6e 63  </a>.  If an inc
a110: 6f 72 72 65 63 74 20 63 68 65 63 6b 73 75 6d 0a  orrect checksum.
a120: 69 73 20 73 65 65 6e 2c 20 74 68 65 20 72 6f 6c  is seen, the rol
a130: 6c 62 61 63 6b 20 69 73 20 61 62 61 6e 64 6f 6e  lback is abandon
a140: 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74  ed.  Note that t
a150: 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73  he checksum does
a160: 0a 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74  .not guarantee t
a170: 68 61 74 20 74 68 65 20 70 61 67 65 20 64 61 74  hat the page dat
a180: 61 20 69 73 20 63 6f 72 72 65 63 74 20 73 69 6e  a is correct sin
a190: 63 65 20 74 68 65 72 65 20 69 73 20 61 20 73 6d  ce there is a sm
a1a0: 61 6c 6c 0a 62 75 74 20 66 69 6e 69 74 65 20 70  all.but finite p
a1b0: 72 6f 62 61 62 69 6c 69 74 79 20 74 68 61 74 20  robability that 
a1c0: 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 69 67  the checksum mig
a1d0: 68 74 20 62 65 20 72 69 67 68 74 20 65 76 65 6e  ht be right even
a1e0: 20 69 66 20 74 68 65 20 64 61 74 61 20 69 73 0a   if the data is.
a1f0: 63 6f 72 72 75 70 74 2e 20 20 42 75 74 20 74 68  corrupt.  But th
a200: 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 20  e checksum does 
a210: 61 74 20 6c 65 61 73 74 20 6d 61 6b 65 20 73 75  at least make su
a220: 63 68 20 61 6e 20 65 72 72 6f 72 20 75 6e 6c 69  ch an error unli
a230: 6b 65 6c 79 2e 0a 3c 2f 70 3e 0a 0a 3c 70 3e 4e  kely..</p>..<p>N
a240: 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 68 65  ote that the che
a250: 63 6b 73 75 6d 73 20 69 6e 20 74 68 65 20 72 6f  cksums in the ro
a260: 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61  llback journal a
a270: 72 65 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  re not necessary
a280: 0a 69 66 20 74 68 65 20 73 79 6e 63 68 72 6f 6e  .if the synchron
a290: 6f 75 73 20 73 65 74 74 69 6e 67 20 69 73 20 46  ous setting is F
a2a0: 55 4c 4c 2e 20 20 57 65 20 6f 6e 6c 79 20 64 65  ULL.  We only de
a2b0: 70 65 6e 64 20 6f 6e 20 74 68 65 20 63 68 65 63  pend on the chec
a2c0: 6b 73 75 6d 73 0a 77 68 65 6e 20 73 79 6e 63 68  ksums.when synch
a2d0: 72 6f 6e 6f 75 73 20 69 73 20 6c 6f 77 65 72 65  ronous is lowere
a2e0: 64 20 74 6f 20 4e 4f 52 4d 41 4c 2e 20 20 4e 65  d to NORMAL.  Ne
a2f0: 76 65 72 74 68 65 6c 65 73 73 2c 20 74 68 65 20  vertheless, the 
a300: 63 68 65 63 6b 73 75 6d 73 0a 6e 65 76 65 72 20  checksums.never 
a310: 68 75 72 74 20 61 6e 64 20 73 6f 20 74 68 65 79  hurt and so they
a320: 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e   are included in
a330: 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
a340: 75 72 6e 61 6c 20 72 65 67 61 72 64 6c 65 73 73  urnal regardless
a350: 0a 6f 66 20 74 68 65 20 73 79 6e 63 68 72 6f 6e  .of the synchron
a360: 6f 75 73 20 73 65 74 74 69 6e 67 2e 3c 2f 70 3e  ous setting.</p>
a370: 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65  ..<tcl>hd_fragme
a380: 6e 74 20 63 61 63 68 65 73 70 69 6c 6c 3c 2f 74  nt cachespill</t
a390: 63 6c 3e 0a 3c 68 33 3e 36 2e 33 20 43 61 63 68  cl>.<h3>6.3 Cach
a3a0: 65 20 53 70 69 6c 6c 20 50 72 69 6f 72 20 54 6f  e Spill Prior To
a3b0: 20 43 6f 6d 6d 69 74 3c 2f 68 33 3e 0a 0a 3c 70   Commit</h3>..<p
a3c0: 3e 54 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63  >The commit proc
a3d0: 65 73 73 20 73 68 6f 77 6e 20 69 6e 20 3c 61 20  ess shown in <a 
a3e0: 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33  href="#section_3
a3f0: 5f 30 22 3e 73 65 63 74 69 6f 6e 20 33 2e 30 3c  _0">section 3.0<
a400: 2f 61 3e 0a 61 73 73 75 6d 65 73 20 74 68 61 74  /a>.assumes that
a410: 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 63 68   all database ch
a420: 61 6e 67 65 73 20 66 69 74 20 69 6e 20 6d 65 6d  anges fit in mem
a430: 6f 72 79 20 75 6e 74 69 6c 20 69 74 20 69 73 20  ory until it is 
a440: 74 69 6d 65 20 74 6f 0a 63 6f 6d 6d 69 74 2e 20  time to.commit. 
a450: 20 54 68 69 73 20 69 73 20 74 68 65 20 63 6f 6d   This is the com
a460: 6d 6f 6e 20 63 61 73 65 2e 20 20 42 75 74 20 73  mon case.  But s
a470: 6f 6d 65 74 69 6d 65 73 20 61 20 6c 61 72 67 65  ometimes a large
a480: 72 20 63 68 61 6e 67 65 20 77 69 6c 6c 0a 6f 76  r change will.ov
a490: 65 72 66 6c 6f 77 20 74 68 65 20 75 73 65 72 2d  erflow the user-
a4a0: 73 70 61 63 65 20 63 61 63 68 65 20 70 72 69 6f  space cache prio
a4b0: 72 20 74 6f 20 74 72 61 6e 73 61 63 74 69 6f 6e  r to transaction
a4c0: 20 63 6f 6d 6d 69 74 2e 20 20 49 6e 20 74 68 6f   commit.  In tho
a4d0: 73 65 0a 63 61 73 65 73 2c 20 74 68 65 20 63 61  se.cases, the ca
a4e0: 63 68 65 20 6d 75 73 74 20 73 70 69 6c 6c 20 74  che must spill t
a4f0: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  o the database b
a500: 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
a510: 63 74 69 6f 6e 0a 69 73 20 63 6f 6d 70 6c 65 74  ction.is complet
a520: 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 41 74 20 74 68  e.</p>..<p>At th
a530: 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61  e beginning of a
a540: 20 63 61 63 68 65 20 73 70 69 6c 6c 2c 20 74 68   cache spill, th
a550: 65 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 20  e status of the 
a560: 64 61 74 61 62 61 73 65 0a 63 6f 6e 6e 65 63 74  database.connect
a570: 69 6f 6e 20 69 73 20 61 73 20 73 68 6f 77 6e 20  ion is as shown 
a580: 69 6e 20 3c 61 20 68 72 65 66 3d 22 23 73 65 63  in <a href="#sec
a590: 74 69 6f 6e 5f 33 5f 36 22 3e 73 74 65 70 20 33  tion_3_6">step 3
a5a0: 2e 36 3c 2f 61 3e 2e 0a 4f 72 69 67 69 6e 61 6c  .6</a>..Original
a5b0: 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 68 61   page content ha
a5c0: 73 20 62 65 65 6e 20 73 61 76 65 64 20 69 6e 20  s been saved in 
a5d0: 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
a5e0: 72 6e 61 6c 20 61 6e 64 0a 6d 6f 64 69 66 69 63  rnal and.modific
a5f0: 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 70 61  ations of the pa
a600: 67 65 73 20 65 78 69 73 74 20 69 6e 20 75 73 65  ges exist in use
a610: 72 20 6d 65 6d 6f 72 79 2e 20 20 54 6f 20 73 70  r memory.  To sp
a620: 69 6c 6c 20 74 68 65 20 63 61 63 68 65 2c 0a 53  ill the cache,.S
a630: 51 4c 69 74 65 20 65 78 65 63 75 74 65 73 20 73  QLite executes s
a640: 74 65 70 73 20 3c 61 20 68 72 65 66 3d 22 23 73  teps <a href="#s
a650: 65 63 74 69 6f 6e 5f 33 5f 37 22 3e 33 2e 37 3c  ection_3_7">3.7<
a660: 2f 61 3e 20 74 68 72 6f 75 67 68 0a 3c 61 20 68  /a> through.<a h
a670: 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f  ref="#section_3_
a680: 39 22 3e 33 2e 39 3c 2f 61 3e 2e 20 20 49 6e 20  9">3.9</a>.  In 
a690: 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
a6a0: 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
a6b0: 6c 0a 69 73 20 66 6c 75 73 68 65 64 20 74 6f 20  l.is flushed to 
a6c0: 64 69 73 6b 2c 20 61 6e 20 65 78 63 6c 75 73 69  disk, an exclusi
a6d0: 76 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69  ve lock is acqui
a6e0: 72 65 64 2c 20 61 6e 64 20 63 68 61 6e 67 65 73  red, and changes
a6f0: 20 61 72 65 0a 77 72 69 74 74 65 6e 20 69 6e 74   are.written int
a700: 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
a710: 20 42 75 74 20 74 68 65 20 72 65 6d 61 69 6e 69   But the remaini
a720: 6e 67 20 73 74 65 70 73 20 61 72 65 20 64 65 66  ng steps are def
a730: 65 72 72 65 64 0a 75 6e 74 69 6c 20 74 68 65 20  erred.until the 
a740: 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 61 6c  transaction real
a750: 6c 79 20 63 6f 6d 6d 69 74 73 2e 20 20 41 20 6e  ly commits.  A n
a760: 65 77 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  ew journal heade
a770: 72 20 69 73 0a 61 70 70 65 6e 64 65 64 20 74 6f  r is.appended to
a780: 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
a790: 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
a7a0: 20 28 69 6e 20 69 74 73 20 6f 77 6e 20 73 65 63   (in its own sec
a7b0: 74 6f 72 29 0a 61 6e 64 20 74 68 65 20 65 78 63  tor).and the exc
a7c0: 6c 75 73 69 76 65 20 64 61 74 61 62 61 73 65 20  lusive database 
a7d0: 6c 6f 63 6b 20 69 73 20 72 65 74 61 69 6e 65 64  lock is retained
a7e0: 2c 20 62 75 74 20 6f 74 68 65 72 77 69 73 65 20  , but otherwise 
a7f0: 70 72 6f 63 65 73 73 69 6e 67 0a 72 65 74 75 72  processing.retur
a800: 6e 73 20 74 6f 20 3c 61 20 68 72 65 66 3d 22 23  ns to <a href="#
a810: 73 65 63 74 69 6f 6e 5f 33 5f 36 22 3e 73 74 65  section_3_6">ste
a820: 70 20 33 2e 36 3c 2f 61 3e 2e 20 20 57 68 65 6e  p 3.6</a>.  When
a830: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
a840: 0a 63 6f 6d 6d 69 74 73 2c 20 6f 72 20 69 66 20  .commits, or if 
a850: 61 6e 6f 74 68 65 72 20 63 61 63 68 65 20 73 70  another cache sp
a860: 69 6c 6c 20 6f 63 63 75 72 73 2c 20 73 74 65 70  ill occurs, step
a870: 73 0a 3c 61 20 68 72 65 66 3d 22 23 73 65 63 74  s.<a href="#sect
a880: 69 6f 6e 5f 33 5f 37 22 3e 33 2e 37 3c 2f 61 3e  ion_3_7">3.7</a>
a890: 20 61 6e 64 20 3c 61 20 68 72 65 66 3d 22 23 73   and <a href="#s
a8a0: 65 63 74 69 6f 6e 5f 33 5f 39 22 3e 33 2e 39 3c  ection_3_9">3.9<
a8b0: 2f 61 3e 20 61 72 65 0a 72 65 70 65 61 74 65 64  /a> are.repeated
a8c0: 2e 20 20 28 53 74 65 70 20 3c 61 20 68 72 65 66  .  (Step <a href
a8d0: 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 38 22 3e  ="#section_3_8">
a8e0: 33 2e 38 3c 2f 61 3e 20 69 73 20 6f 6d 69 74 74  3.8</a> is omitt
a8f0: 65 64 20 6f 6e 20 73 65 63 6f 6e 64 0a 61 6e 64  ed on second.and
a900: 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 73 73   subsequent pass
a910: 65 73 20 73 69 6e 63 65 20 61 6e 20 65 78 63 6c  es since an excl
a920: 75 73 69 76 65 20 64 61 74 61 62 61 73 65 20 6c  usive database l
a930: 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68  ock is already h
a940: 65 6c 64 0a 64 75 65 20 74 6f 20 74 68 65 20 66  eld.due to the f
a950: 69 72 73 74 20 70 61 73 73 2e 29 3c 2f 70 3e 0a  irst pass.)</p>.
a960: 0a 3c 70 3e 41 20 63 61 63 68 65 20 73 70 69 6c  .<p>A cache spil
a970: 6c 20 63 61 75 73 65 73 20 74 68 65 20 6c 6f 63  l causes the loc
a980: 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
a990: 65 20 66 69 6c 65 20 74 6f 0a 65 73 63 61 6c 61  e file to.escala
a9a0: 74 65 20 66 72 6f 6d 20 72 65 73 65 72 76 65 64  te from reserved
a9b0: 20 74 6f 20 65 78 63 6c 75 73 69 76 65 2e 20 20   to exclusive.  
a9c0: 54 68 69 73 20 72 65 64 75 63 65 73 20 63 6f 6e  This reduces con
a9d0: 63 75 72 72 65 6e 63 79 2e 0a 41 20 63 61 63 68  currency..A cach
a9e0: 65 20 73 70 69 6c 6c 20 61 6c 73 6f 20 63 61 75  e spill also cau
a9f0: 73 65 73 20 65 78 74 72 61 20 64 69 73 6b 20 66  ses extra disk f
aa00: 6c 75 73 68 20 6f 72 20 66 73 79 6e 63 20 6f 70  lush or fsync op
aa10: 65 72 61 74 69 6f 6e 73 20 74 6f 0a 6f 63 63 75  erations to.occu
aa20: 72 20 61 6e 64 20 74 68 65 73 65 20 6f 70 65 72  r and these oper
aa30: 61 74 69 6f 6e 73 20 61 72 65 20 73 6c 6f 77 2c  ations are slow,
aa40: 20 68 65 6e 63 65 20 61 20 63 61 63 68 65 20 73   hence a cache s
aa50: 70 69 6c 6c 20 63 61 6e 0a 73 65 72 69 6f 75 73  pill can.serious
aa60: 6c 79 20 72 65 64 75 63 65 20 70 65 72 66 6f 72  ly reduce perfor
aa70: 6d 61 6e 63 65 2e 0a 46 6f 72 20 74 68 65 73 65  mance..For these
aa80: 20 72 65 61 73 6f 6e 73 20 61 20 63 61 63 68 65   reasons a cache
aa90: 20 73 70 69 6c 6c 20 69 73 20 61 76 6f 69 64 65   spill is avoide
aaa0: 64 20 77 68 65 6e 65 76 65 72 20 70 6f 73 73 69  d whenever possi
aab0: 62 6c 65 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68  ble.</p>..<tcl>h
aac0: 64 5f 66 72 61 67 6d 65 6e 74 20 6f 70 74 73 3c  d_fragment opts<
aad0: 2f 74 63 6c 3e 0a 3c 68 32 3e 37 2e 30 20 4f 70  /tcl>.<h2>7.0 Op
aae0: 74 69 6d 69 7a 61 74 69 6f 6e 73 3c 2f 68 32 3e  timizations</h2>
aaf0: 0a 0a 3c 70 3e 50 72 6f 66 69 6c 69 6e 67 20 69  ..<p>Profiling i
ab00: 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 66 6f  ndicates that fo
ab10: 72 20 6d 6f 73 74 20 73 79 73 74 65 6d 73 20 61  r most systems a
ab20: 6e 64 20 69 6e 20 6d 6f 73 74 20 63 69 72 63 75  nd in most circu
ab30: 6d 73 74 61 6e 63 65 73 0a 53 51 4c 69 74 65 20  mstances.SQLite 
ab40: 73 70 65 6e 64 73 20 6d 6f 73 74 20 6f 66 20 69  spends most of i
ab50: 74 73 20 74 69 6d 65 20 64 6f 69 6e 67 20 64 69  ts time doing di
ab60: 73 6b 20 49 2f 4f 2e 20 20 49 74 20 66 6f 6c 6c  sk I/O.  It foll
ab70: 6f 77 73 20 74 68 65 6e 20 74 68 61 74 0a 61 6e  ows then that.an
ab80: 79 74 68 69 6e 67 20 77 65 20 63 61 6e 20 64 6f  ything we can do
ab90: 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 61   to reduce the a
aba0: 6d 6f 75 6e 74 20 6f 66 20 64 69 73 6b 20 49 2f  mount of disk I/
abb0: 4f 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61  O will likely ha
abc0: 76 65 20 61 0a 6c 61 72 67 65 20 70 6f 73 69 74  ve a.large posit
abd0: 69 76 65 20 69 6d 70 61 63 74 20 6f 6e 20 74 68  ive impact on th
abe0: 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66  e performance of
abf0: 20 53 51 4c 69 74 65 2e 20 20 54 68 69 73 20 73   SQLite.  This s
ac00: 65 63 74 69 6f 6e 0a 64 65 73 63 72 69 62 65 73  ection.describes
ac10: 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 74 65 63   some of the tec
ac20: 68 6e 69 71 75 65 73 20 75 73 65 64 20 62 79 20  hniques used by 
ac30: 53 51 4c 69 74 65 20 74 6f 20 74 72 79 20 74 6f  SQLite to try to
ac40: 20 72 65 64 75 63 65 20 74 68 65 0a 61 6d 6f 75   reduce the.amou
ac50: 6e 74 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20 74  nt of disk I/O t
ac60: 6f 20 61 20 6d 69 6e 69 6d 75 6d 20 77 68 69 6c  o a minimum whil
ac70: 65 20 73 74 69 6c 6c 20 70 72 65 73 65 72 76 69  e still preservi
ac80: 6e 67 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74  ng atomic commit
ac90: 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66  .</p>..<tcl>hd_f
aca0: 72 61 67 6d 65 6e 74 20 6b 65 65 70 63 61 63 68  ragment keepcach
acb0: 65 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 37 2e 31 20  e</tcl>.<h3>7.1 
acc0: 43 61 63 68 65 20 52 65 74 61 69 6e 65 64 20 42  Cache Retained B
acd0: 65 74 77 65 65 6e 20 54 72 61 6e 73 61 63 74 69  etween Transacti
ace0: 6f 6e 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 3c 61 20  ons</h3>..<p><a 
acf0: 68 72 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33  href="#section_3
ad00: 5f 31 32 22 3e 53 74 65 70 20 33 2e 31 32 3c 2f  _12">Step 3.12</
ad10: 61 3e 20 6f 66 20 74 68 65 20 63 6f 6d 6d 69 74  a> of the commit
ad20: 20 70 72 6f 63 65 73 73 20 73 68 6f 77 73 0a 74   process shows.t
ad30: 68 61 74 20 6f 6e 63 65 20 74 68 65 20 73 68 61  hat once the sha
ad40: 72 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65  red lock has bee
ad50: 6e 20 72 65 6c 65 61 73 65 64 2c 20 61 6c 6c 20  n released, all 
ad60: 75 73 65 72 2d 73 70 61 63 65 20 63 61 63 68 65  user-space cache
ad70: 0a 69 6d 61 67 65 73 20 6f 66 20 64 61 74 61 62  .images of datab
ad80: 61 73 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74  ase content must
ad90: 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 20   be discarded.  
ada0: 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63  This is done bec
adb0: 61 75 73 65 0a 77 69 74 68 6f 75 74 20 61 20 73  ause.without a s
adc0: 68 61 72 65 64 20 6c 6f 63 6b 2c 20 6f 74 68 65  hared lock, othe
add0: 72 20 70 72 6f 63 65 73 73 65 73 20 61 72 65 20  r processes are 
ade0: 66 72 65 65 20 74 6f 20 6d 6f 64 69 66 79 20 74  free to modify t
adf0: 68 65 20 64 61 74 61 62 61 73 65 0a 66 69 6c 65  he database.file
ae00: 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 73 6f 20   content and so 
ae10: 61 6e 79 20 75 73 65 72 2d 73 70 61 63 65 20 69  any user-space i
ae20: 6d 61 67 65 20 6f 66 20 74 68 61 74 20 63 6f 6e  mage of that con
ae30: 74 65 6e 74 20 6d 69 67 68 74 20 62 65 63 6f 6d  tent might becom
ae40: 65 0a 6f 62 73 6f 6c 65 74 65 2e 20 20 43 6f 6e  e.obsolete.  Con
ae50: 73 65 71 75 65 6e 74 6c 79 2c 20 65 61 63 68 20  sequently, each 
ae60: 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  new transaction 
ae70: 77 6f 75 6c 64 20 62 65 67 69 6e 20 62 79 20 72  would begin by r
ae80: 65 72 65 61 64 69 6e 67 0a 64 61 74 61 20 77 68  ereading.data wh
ae90: 69 63 68 20 68 61 64 20 70 72 65 76 69 6f 75 73  ich had previous
aea0: 6c 79 20 62 65 65 6e 20 72 65 61 64 2e 20 20 54  ly been read.  T
aeb0: 68 69 73 20 69 73 20 6e 6f 74 20 61 73 20 62 61  his is not as ba
aec0: 64 20 61 73 20 69 74 20 73 6f 75 6e 64 73 0a 61  d as it sounds.a
aed0: 74 20 66 69 72 73 74 20 73 69 6e 63 65 20 74 68  t first since th
aee0: 65 20 64 61 74 61 20 62 65 69 6e 67 20 72 65 61  e data being rea
aef0: 64 20 69 73 20 73 74 69 6c 6c 20 6c 69 6b 65 6c  d is still likel
af00: 79 20 69 6e 20 74 68 65 20 6f 70 65 72 61 74 69  y in the operati
af10: 6e 67 0a 73 79 73 74 65 6d 73 20 66 69 6c 65 20  ng.systems file 
af20: 63 61 63 68 65 2e 20 20 53 6f 20 74 68 65 20 22  cache.  So the "
af30: 72 65 61 64 22 20 69 73 20 72 65 61 6c 6c 79 20  read" is really 
af40: 6a 75 73 74 20 61 20 63 6f 70 79 20 6f 66 20 64  just a copy of d
af50: 61 74 61 0a 66 72 6f 6d 20 6b 65 72 6e 65 6c 20  ata.from kernel 
af60: 73 70 61 63 65 20 69 6e 74 6f 20 75 73 65 72 20  space into user 
af70: 73 70 61 63 65 2e 20 20 42 75 74 20 65 76 65 6e  space.  But even
af80: 20 73 6f 2c 20 69 74 20 73 74 69 6c 6c 20 74 61   so, it still ta
af90: 6b 65 73 20 74 69 6d 65 2e 3c 2f 70 3e 0a 0a 3c  kes time.</p>..<
afa0: 70 3e 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  p>Beginning with
afb0: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
afc0: 33 2e 33 2e 31 34 20 61 20 6d 65 63 68 61 6e 69  3.3.14 a mechani
afd0: 73 6d 20 68 61 73 20 62 65 65 6e 20 61 64 64 65  sm has been adde
afe0: 64 0a 74 6f 20 74 72 79 20 74 6f 20 72 65 64 75  d.to try to redu
aff0: 63 65 20 74 68 65 20 6e 65 65 64 6c 65 73 73 20  ce the needless 
b000: 72 65 72 65 61 64 69 6e 67 20 6f 66 20 64 61 74  rereading of dat
b010: 61 2e 20 20 49 6e 20 6e 65 77 65 72 20 76 65 72  a.  In newer ver
b020: 73 69 6f 6e 73 0a 6f 66 20 53 51 4c 69 74 65 2c  sions.of SQLite,
b030: 20 74 68 65 20 64 61 74 61 20 69 6e 20 74 68 65   the data in the
b040: 20 75 73 65 72 2d 73 70 61 63 65 20 70 61 67 65   user-space page
b050: 72 20 63 61 63 68 65 20 69 73 20 72 65 74 61 69  r cache is retai
b060: 6e 65 64 20 77 68 65 6e 0a 74 68 65 20 6c 6f 63  ned when.the loc
b070: 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
b080: 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73  e file is releas
b090: 65 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74 65  ed.  Later, afte
b0a0: 72 20 74 68 65 0a 73 68 61 72 65 64 20 6c 6f 63  r the.shared loc
b0b0: 6b 20 69 73 20 61 63 71 75 69 72 65 64 20 61 74  k is acquired at
b0c0: 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
b0d0: 66 20 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73  f the next trans
b0e0: 61 63 74 69 6f 6e 2c 0a 53 51 4c 69 74 65 20 63  action,.SQLite c
b0f0: 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  hecks to see if 
b100: 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
b110: 73 20 68 61 73 20 6d 6f 64 69 66 69 65 64 20 74  s has modified t
b120: 68 65 20 64 61 74 61 62 61 73 65 0a 66 69 6c 65  he database.file
b130: 2e 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61  .  If the databa
b140: 73 65 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e  se has been chan
b150: 67 65 64 20 69 6e 20 61 6e 79 20 77 61 79 20 73  ged in any way s
b160: 69 6e 63 65 20 74 68 65 20 6c 6f 63 6b 0a 77 61  ince the lock.wa
b170: 73 20 6c 61 73 74 20 72 65 6c 65 61 73 65 64 2c  s last released,
b180: 20 74 68 65 20 75 73 65 72 2d 73 70 61 63 65 20   the user-space 
b190: 63 61 63 68 65 20 69 73 20 65 72 61 73 65 64 20  cache is erased 
b1a0: 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 42  at that point..B
b1b0: 75 74 20 63 6f 6d 6d 6f 6e 6c 79 20 74 68 65 20  ut commonly the 
b1c0: 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
b1d0: 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 74   unchanged and t
b1e0: 68 65 20 75 73 65 72 2d 73 70 61 63 65 20 63 61  he user-space ca
b1f0: 63 68 65 0a 63 61 6e 20 62 65 20 72 65 74 61 69  che.can be retai
b200: 6e 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 75 6e  ned, and some un
b210: 6e 65 63 65 73 73 61 72 79 20 72 65 61 64 20 6f  necessary read o
b220: 70 65 72 61 74 69 6f 6e 73 20 63 61 6e 20 62 65  perations can be
b230: 20 61 76 6f 69 64 65 64 2e 3c 2f 70 3e 0a 0a 3c   avoided.</p>..<
b240: 70 3e 49 6e 20 6f 72 64 65 72 20 74 6f 20 64 65  p>In order to de
b250: 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
b260: 6f 72 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62  or not the datab
b270: 61 73 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  ase file has cha
b280: 6e 67 65 64 2c 0a 53 51 4c 69 74 65 20 75 73 65  nged,.SQLite use
b290: 73 20 61 20 63 6f 75 6e 74 65 72 20 69 6e 20 74  s a counter in t
b2a0: 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64  he database head
b2b0: 65 72 20 28 69 6e 20 62 79 74 65 73 20 32 34 20  er (in bytes 24 
b2c0: 74 68 72 6f 75 67 68 20 32 37 29 0a 77 68 69 63  through 27).whic
b2d0: 68 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  h is incremented
b2e0: 20 64 75 72 69 6e 67 20 65 76 65 72 79 20 63 68   during every ch
b2f0: 61 6e 67 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ange operation. 
b300: 20 53 51 4c 69 74 65 20 73 61 76 65 73 20 61 20   SQLite saves a 
b310: 63 6f 70 79 0a 6f 66 20 74 68 69 73 20 63 6f 75  copy.of this cou
b320: 6e 74 65 72 20 70 72 69 6f 72 20 74 6f 20 72 65  nter prior to re
b330: 6c 65 61 73 69 6e 67 20 69 74 73 20 64 61 74 61  leasing its data
b340: 62 61 73 65 20 6c 6f 63 6b 2e 20 20 54 68 65 6e  base lock.  Then
b350: 20 61 66 74 65 72 0a 61 63 71 75 69 72 69 6e 67   after.acquiring
b360: 20 74 68 65 20 6e 65 78 74 20 64 61 74 61 62 61   the next databa
b370: 73 65 20 6c 6f 63 6b 20 69 74 20 63 6f 6d 70 61  se lock it compa
b380: 72 65 73 20 74 68 65 20 73 61 76 65 64 20 63 6f  res the saved co
b390: 75 6e 74 65 72 20 76 61 6c 75 65 0a 61 67 61 69  unter value.agai
b3a0: 6e 73 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  nst the current 
b3b0: 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 20 61 6e  counter value an
b3c0: 64 20 65 72 61 73 65 73 20 74 68 65 20 63 61 63  d erases the cac
b3d0: 68 65 20 69 66 20 74 68 65 20 76 61 6c 75 65 73  he if the values
b3e0: 0a 61 72 65 20 64 69 66 66 65 72 65 6e 74 2c 20  .are different, 
b3f0: 6f 72 20 72 65 75 73 65 73 20 74 68 65 20 63 61  or reuses the ca
b400: 63 68 65 20 69 66 20 74 68 65 79 20 61 72 65 20  che if they are 
b410: 74 68 65 20 73 61 6d 65 2e 3c 2f 70 3e 0a 0a 3c  the same.</p>..<
b420: 61 20 6e 61 6d 65 3d 22 73 65 63 74 69 6f 6e 5f  a name="section_
b430: 37 5f 32 22 3e 3c 2f 61 3e 0a 3c 68 33 3e 37 2e  7_2"></a>.<h3>7.
b440: 32 20 45 78 63 6c 75 73 69 76 65 20 41 63 63 65  2 Exclusive Acce
b450: 73 73 20 4d 6f 64 65 3c 2f 68 33 3e 0a 0a 3c 70  ss Mode</h3>..<p
b460: 3e 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20  >SQLite version 
b470: 33 2e 33 2e 31 34 20 61 64 64 73 20 74 68 65 20  3.3.14 adds the 
b480: 63 6f 6e 63 65 70 74 20 6f 66 20 22 45 78 63 6c  concept of "Excl
b490: 75 73 69 76 65 20 41 63 63 65 73 73 20 4d 6f 64  usive Access Mod
b4a0: 65 22 2e 0a 49 6e 20 65 78 63 6c 75 73 69 76 65  e"..In exclusive
b4b0: 20 61 63 63 65 73 73 20 6d 6f 64 65 2c 20 53 51   access mode, SQ
b4c0: 4c 69 74 65 20 72 65 74 61 69 6e 73 20 74 68 65  Lite retains the
b4d0: 20 65 78 63 6c 75 73 69 76 65 0a 64 61 74 61 62   exclusive.datab
b4e0: 61 73 65 20 6c 6f 63 6b 20 61 74 20 74 68 65 20  ase lock at the 
b4f0: 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61  conclusion of ea
b500: 63 68 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ch transaction. 
b510: 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 0a 6f   This prevents.o
b520: 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66  ther processes f
b530: 72 6f 6d 20 61 63 63 65 73 73 69 6e 67 20 74 68  rom accessing th
b540: 65 20 64 61 74 61 62 61 73 65 2c 20 62 75 74 20  e database, but 
b550: 69 6e 20 6d 61 6e 79 20 64 65 70 6c 6f 79 6d 65  in many deployme
b560: 6e 74 73 0a 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  nts.only a singl
b570: 65 20 70 72 6f 63 65 73 73 20 69 73 20 75 73 69  e process is usi
b580: 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 73 6f  ng a database so
b590: 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 0a 73   this is not a.s
b5a0: 65 72 69 6f 75 73 20 70 72 6f 62 6c 65 6d 2e 20  erious problem. 
b5b0: 20 54 68 65 20 61 64 76 61 6e 74 61 67 65 20 6f   The advantage o
b5c0: 66 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  f exclusive acce
b5d0: 73 73 20 6d 6f 64 65 20 69 73 20 74 68 61 74 0a  ss mode is that.
b5e0: 64 69 73 6b 20 49 2f 4f 20 63 61 6e 20 62 65 20  disk I/O can be 
b5f0: 72 65 64 75 63 65 64 20 69 6e 20 74 68 72 65 65  reduced in three
b600: 20 77 61 79 73 3a 3c 2f 70 3e 0a 0a 3c 6f 6c 3e   ways:</p>..<ol>
b610: 0a 3c 6c 69 3e 3c 70 3e 49 74 20 69 73 20 6e 6f  .<li><p>It is no
b620: 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69  t necessary to i
b630: 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 68 61  ncrement the cha
b640: 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 6e 20 74  nge counter in t
b650: 68 65 0a 64 61 74 61 62 61 73 65 20 68 65 61 64  he.database head
b660: 65 72 20 66 6f 72 20 74 72 61 6e 73 61 63 74 69  er for transacti
b670: 6f 6e 73 20 61 66 74 65 72 20 74 68 65 20 66 69  ons after the fi
b680: 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  rst transaction.
b690: 20 20 54 68 69 73 0a 77 69 6c 6c 20 6f 66 74 65    This.will ofte
b6a0: 6e 20 73 61 76 65 20 61 20 77 72 69 74 65 20 6f  n save a write o
b6b0: 66 20 70 61 67 65 20 6f 6e 65 20 74 6f 20 62 6f  f page one to bo
b6c0: 74 68 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a  th the rollback.
b6d0: 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20  journal and the 
b6e0: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
b6f0: 6c 65 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a 0a 3c 6c  le.</p></li>..<l
b700: 69 3e 3c 70 3e 4e 6f 20 6f 74 68 65 72 20 70 72  i><p>No other pr
b710: 6f 63 65 73 73 65 73 20 63 61 6e 20 63 68 61 6e  ocesses can chan
b720: 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ge the database 
b730: 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 65  so there is neve
b740: 72 0a 61 20 6e 65 65 64 20 74 6f 20 63 68 65 63  r.a need to chec
b750: 6b 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75  k the change cou
b760: 6e 74 65 72 20 61 6e 64 20 63 6c 65 61 72 20 74  nter and clear t
b770: 68 65 20 75 73 65 72 2d 73 70 61 63 65 20 63 61  he user-space ca
b780: 63 68 65 0a 61 74 20 74 68 65 20 62 65 67 69 6e  che.at the begin
b790: 6e 69 6e 67 20 6f 66 20 61 20 74 72 61 6e 73 61  ning of a transa
b7a0: 63 74 69 6f 6e 2e 3c 2f 70 3e 3c 2f 6c 69 3e 0a  ction.</p></li>.
b7b0: 0a 3c 6c 69 3e 3c 70 3e 45 61 63 68 20 74 72 61  .<li><p>Each tra
b7c0: 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 62 65 20  nsaction can be 
b7d0: 63 6f 6d 6d 69 74 74 65 64 20 62 79 20 6f 76 65  committed by ove
b7e0: 72 77 72 69 74 69 6e 67 20 74 68 65 20 72 6f 6c  rwriting the rol
b7f0: 6c 62 61 63 6b 0a 6a 6f 75 72 6e 61 6c 20 68 65  lback.journal he
b800: 61 64 65 72 20 77 69 74 68 20 7a 65 72 6f 73 20  ader with zeros 
b810: 72 61 74 68 65 72 20 74 68 61 6e 20 64 65 6c 65  rather than dele
b820: 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ting the journal
b830: 20 66 69 6c 65 2e 0a 54 68 69 73 20 61 76 6f 69   file..This avoi
b840: 64 73 20 68 61 76 69 6e 67 20 74 6f 20 6d 6f 64  ds having to mod
b850: 69 66 79 20 74 68 65 20 64 69 72 65 63 74 6f 72  ify the director
b860: 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  y entry for the 
b870: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 61 6e 64  journal file.and
b880: 20 69 74 20 61 76 6f 69 64 73 20 68 61 76 69 6e   it avoids havin
b890: 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
b8a0: 64 69 73 6b 20 73 65 63 74 6f 72 73 20 61 73 73  disk sectors ass
b8b0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
b8c0: 20 0a 6a 6f 75 72 6e 61 6c 2e 20 20 46 75 72 74   .journal.  Furt
b8d0: 68 65 72 6d 6f 72 65 2c 20 74 68 65 20 6e 65 78  hermore, the nex
b8e0: 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  t transaction wi
b8f0: 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 65 78 69  ll overwrite exi
b900: 73 74 69 6e 67 0a 6a 6f 75 72 6e 61 6c 20 66 69  sting.journal fi
b910: 6c 65 20 63 6f 6e 74 65 6e 74 20 72 61 74 68 65  le content rathe
b920: 72 20 74 68 61 6e 20 61 70 70 65 6e 64 20 6e 65  r than append ne
b930: 77 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 6f 6e  w content and on
b940: 20 6d 6f 73 74 20 73 79 73 74 65 6d 73 0a 6f 76   most systems.ov
b950: 65 72 77 72 69 74 69 6e 67 20 69 73 20 6d 75 63  erwriting is muc
b960: 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 61 70  h faster than ap
b970: 70 65 6e 64 69 6e 67 2e 3c 2f 70 3e 3c 2f 6c 69  pending.</p></li
b980: 3e 0a 3c 2f 6f 6c 3e 0a 0a 3c 70 3e 54 68 65 20  >.</ol>..<p>The 
b990: 74 68 69 72 64 20 6f 70 74 69 6d 69 7a 61 74 69  third optimizati
b9a0: 6f 6e 2c 20 7a 65 72 6f 69 6e 67 20 74 68 65 20  on, zeroing the 
b9b0: 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61  journal file hea
b9c0: 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 0a  der rather than.
b9d0: 64 65 6c 65 74 69 6e 67 20 74 68 65 20 72 6f 6c  deleting the rol
b9e0: 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
b9f0: 6c 65 2c 0a 64 6f 65 73 20 6e 6f 74 20 64 65 70  le,.does not dep
ba00: 65 6e 64 20 6f 6e 20 68 6f 6c 64 69 6e 67 20 61  end on holding a
ba10: 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
ba20: 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 54   at all times..T
ba30: 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
ba40: 20 63 61 6e 20 62 65 20 73 65 74 20 69 6e 64 65   can be set inde
ba50: 70 65 6e 64 65 6e 74 6c 79 20 6f 66 20 65 78 63  pendently of exc
ba60: 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6d 6f 64 65  lusive lock mode
ba70: 0a 75 73 69 6e 67 20 74 68 65 20 5b 6a 6f 75 72  .using the [jour
ba80: 6e 61 6c 5f 6d 6f 64 65 20 70 72 61 67 6d 61 5d  nal_mode pragma]
ba90: 0a 61 73 20 64 65 73 63 72 69 62 65 64 20 69 6e  .as described in
baa0: 20 3c 61 20 68 72 65 66 3d 22 23 73 65 63 74 69   <a href="#secti
bab0: 6f 6e 5f 37 5f 36 22 3e 73 65 63 74 69 6f 6e 20  on_7_6">section 
bac0: 37 2e 36 3c 2f 61 3e 20 62 65 6c 6f 77 2e 3c 2f  7.6</a> below.</
bad0: 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67  p>..<tcl>hd_frag
bae0: 6d 65 6e 74 20 66 72 65 65 6c 69 73 74 6a 72 6e  ment freelistjrn
baf0: 6c 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 37 2e 33 20  l</tcl>.<h3>7.3 
bb00: 44 6f 20 4e 6f 74 20 4a 6f 75 72 6e 61 6c 20 46  Do Not Journal F
bb10: 72 65 65 6c 69 73 74 20 50 61 67 65 73 3c 2f 68  reelist Pages</h
bb20: 33 3e 0a 0a 3c 70 3e 57 68 65 6e 20 69 6e 66 6f  3>..<p>When info
bb30: 72 6d 61 74 69 6f 6e 20 69 73 20 64 65 6c 65 74  rmation is delet
bb40: 65 64 20 66 72 6f 6d 20 61 6e 20 53 51 4c 69 74  ed from an SQLit
bb50: 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  e database, the 
bb60: 70 61 67 65 73 20 75 73 65 64 0a 74 6f 20 68 6f  pages used.to ho
bb70: 6c 64 20 74 68 65 20 64 65 6c 65 74 65 64 20 69  ld the deleted i
bb80: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 65 20 61  nformation are a
bb90: 64 64 65 64 20 74 6f 20 61 20 22 5b 66 72 65 65  dded to a "[free
bba0: 6c 69 73 74 5d 22 2e 20 20 53 75 62 73 65 71 75  list]".  Subsequ
bbb0: 65 6e 74 0a 69 6e 73 65 72 74 73 20 77 69 6c 6c  ent.inserts will
bbc0: 20 64 72 61 77 20 70 61 67 65 73 20 6f 66 66 20   draw pages off 
bbd0: 6f 66 20 74 68 69 73 20 66 72 65 65 6c 69 73 74  of this freelist
bbe0: 20 72 61 74 68 65 72 20 74 68 61 6e 20 65 78 70   rather than exp
bbf0: 61 6e 64 69 6e 67 20 74 68 65 0a 64 61 74 61 62  anding the.datab
bc00: 61 73 65 20 66 69 6c 65 2e 3c 2f 70 3e 0a 0a 3c  ase file.</p>..<
bc10: 70 3e 53 6f 6d 65 20 66 72 65 65 6c 69 73 74 20  p>Some freelist 
bc20: 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 63 72  pages contain cr
bc30: 69 74 69 63 61 6c 20 64 61 74 61 3b 20 73 70 65  itical data; spe
bc40: 63 69 66 69 63 61 6c 6c 79 20 74 68 65 20 6c 6f  cifically the lo
bc50: 63 61 74 69 6f 6e 73 0a 6f 66 20 6f 74 68 65 72  cations.of other
bc60: 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 2e   freelist pages.
bc70: 20 20 42 75 74 20 6d 6f 73 74 20 66 72 65 65 6c    But most freel
bc80: 69 73 74 20 70 61 67 65 73 20 63 6f 6e 74 61 69  ist pages contai
bc90: 6e 20 6e 6f 74 68 69 6e 67 20 75 73 65 66 75 6c  n nothing useful
bca0: 2e 0a 54 68 65 73 65 20 6c 61 74 74 65 72 20 66  ..These latter f
bcb0: 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 61 72  reelist pages ar
bcc0: 65 20 63 61 6c 6c 65 64 20 22 6c 65 61 66 22 20  e called "leaf" 
bcd0: 70 61 67 65 73 2e 20 20 57 65 20 61 72 65 20 66  pages.  We are f
bce0: 72 65 65 20 74 6f 0a 6d 6f 64 69 66 79 20 74 68  ree to.modify th
bcf0: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c  e content of a l
bd00: 65 61 66 20 66 72 65 65 6c 69 73 74 20 70 61 67  eaf freelist pag
bd10: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
bd20: 65 20 77 69 74 68 6f 75 74 0a 63 68 61 6e 67 69  e without.changi
bd30: 6e 67 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  ng the meaning o
bd40: 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
bd50: 6e 20 61 6e 79 20 77 61 79 2e 3c 2f 70 3e 0a 0a  n any way.</p>..
bd60: 3c 70 3e 42 65 63 61 75 73 65 20 74 68 65 20 63  <p>Because the c
bd70: 6f 6e 74 65 6e 74 20 6f 66 20 6c 65 61 66 20 66  ontent of leaf f
bd80: 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 73  reelist pages is
bd90: 20 75 6e 69 6d 70 6f 72 74 61 6e 74 2c 20 53 51   unimportant, SQ
bda0: 4c 69 74 65 0a 61 76 6f 69 64 73 20 73 74 6f 72  Lite.avoids stor
bdb0: 69 6e 67 20 6c 65 61 66 20 66 72 65 65 6c 69 73  ing leaf freelis
bdc0: 74 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69  t page content i
bdd0: 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
bde0: 6f 75 72 6e 61 6c 0a 69 6e 20 3c 61 20 68 72 65  ournal.in <a hre
bdf0: 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 35 22  f="#section_3_5"
be00: 3e 73 74 65 70 20 33 2e 35 3c 2f 61 3e 20 6f 66  >step 3.5</a> of
be10: 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63   the commit proc
be20: 65 73 73 2e 0a 49 66 20 61 20 6c 65 61 66 20 66  ess..If a leaf f
be30: 72 65 65 6c 69 73 74 20 70 61 67 65 20 69 73 20  reelist page is 
be40: 63 68 61 6e 67 65 64 20 61 6e 64 20 74 68 61 74  changed and that
be50: 20 63 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74   change does not
be60: 20 67 65 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b   get rolled back
be70: 0a 64 75 72 69 6e 67 20 61 20 74 72 61 6e 73 61  .during a transa
be80: 63 74 69 6f 6e 20 72 65 63 6f 76 65 72 79 2c 20  ction recovery, 
be90: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
bea0: 6e 6f 74 20 68 61 72 6d 65 64 20 62 79 20 74 68  not harmed by th
beb0: 65 20 6f 6d 69 73 73 69 6f 6e 2e 0a 53 69 6d 69  e omission..Simi
bec0: 6c 61 72 6c 79 2c 20 74 68 65 20 63 6f 6e 74 65  larly, the conte
bed0: 6e 74 20 6f 66 20 61 20 6e 65 77 20 66 72 65 65  nt of a new free
bee0: 6c 69 73 74 20 70 61 67 65 20 69 73 20 6e 65 76  list page is nev
bef0: 65 72 20 77 72 69 74 74 65 6e 20 62 61 63 6b 0a  er written back.
bf00: 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
bf10: 65 20 61 74 20 3c 61 20 68 72 65 66 3d 22 23 73  e at <a href="#s
bf20: 65 63 74 69 6f 6e 5f 33 5f 39 22 3e 73 74 65 70  ection_3_9">step
bf30: 20 33 2e 39 3c 2f 61 3e 20 6e 6f 72 0a 72 65 61   3.9</a> nor.rea
bf40: 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  d from the datab
bf50: 61 73 65 20 61 74 20 3c 61 20 68 72 65 66 3d 22  ase at <a href="
bf60: 23 73 65 63 74 69 6f 6e 5f 33 5f 33 22 3e 73 74  #section_3_3">st
bf70: 65 70 20 33 2e 33 3c 2f 61 3e 2e 0a 54 68 65 73  ep 3.3</a>..Thes
bf80: 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  e optimizations 
bf90: 63 61 6e 20 67 72 65 61 74 6c 79 20 72 65 64 75  can greatly redu
bfa0: 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  ce the amount of
bfb0: 20 49 2f 4f 20 74 68 61 74 20 6f 63 63 75 72 73   I/O that occurs
bfc0: 0a 77 68 65 6e 20 6d 61 6b 69 6e 67 20 63 68 61  .when making cha
bfd0: 6e 67 65 73 20 74 6f 20 61 20 64 61 74 61 62 61  nges to a databa
bfe0: 73 65 20 66 69 6c 65 20 74 68 61 74 20 63 6f 6e  se file that con
bff0: 74 61 69 6e 73 20 66 72 65 65 20 73 70 61 63 65  tains free space
c000: 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66  .</p>..<tcl>hd_f
c010: 72 61 67 6d 65 6e 74 20 61 74 6f 6d 69 63 73 65  ragment atomicse
c020: 63 74 6f 72 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 37  ctor</tcl>.<h3>7
c030: 2e 34 20 53 69 6e 67 6c 65 20 50 61 67 65 20 55  .4 Single Page U
c040: 70 64 61 74 65 73 20 41 6e 64 20 41 74 6f 6d 69  pdates And Atomi
c050: 63 20 53 65 63 74 6f 72 20 57 72 69 74 65 73 3c  c Sector Writes<
c060: 2f 68 33 3e 0a 0a 3c 70 3e 42 65 67 69 6e 6e 69  /h3>..<p>Beginni
c070: 6e 67 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72  ng in SQLite ver
c080: 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 20  sion 3.5.0, the 
c090: 6e 65 77 20 56 69 72 74 75 61 6c 20 46 69 6c 65  new Virtual File
c0a0: 20 53 79 73 74 65 6d 20 28 56 46 53 29 0a 69 6e   System (VFS).in
c0b0: 74 65 72 66 61 63 65 20 63 6f 6e 74 61 69 6e 73  terface contains
c0c0: 20 61 20 6d 65 74 68 6f 64 20 6e 61 6d 65 64 20   a method named 
c0d0: 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
c0e0: 69 73 74 69 63 73 20 77 68 69 63 68 20 72 65 70  istics which rep
c0f0: 6f 72 74 73 0a 6f 6e 20 73 70 65 63 69 61 6c 20  orts.on special 
c100: 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 20  properties that 
c110: 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d  the underlying m
c120: 61 73 73 20 73 74 6f 72 61 67 65 20 64 65 76 69  ass storage devi
c130: 63 65 0a 6d 69 67 68 74 20 68 61 76 65 2e 20 20  ce.might have.  
c140: 41 6d 6f 6e 67 20 74 68 65 20 73 70 65 63 69 61  Among the specia
c150: 6c 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 61  l properties tha
c160: 74 0a 78 44 65 76 69 63 65 43 68 61 72 61 63 74  t.xDeviceCharact
c170: 65 72 69 73 74 69 63 73 20 6d 69 67 68 74 20 72  eristics might r
c180: 65 70 6f 72 74 20 69 73 20 74 68 65 20 61 62 69  eport is the abi
c190: 6c 69 74 79 20 6f 66 20 74 6f 20 64 6f 20 61 6e  lity of to do an
c1a0: 0a 61 74 6f 6d 69 63 20 73 65 63 74 6f 72 20 77  .atomic sector w
c1b0: 72 69 74 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 52 65  rite.</p>..<p>Re
c1c0: 63 61 6c 6c 20 74 68 61 74 20 62 79 20 64 65 66  call that by def
c1d0: 61 75 6c 74 20 53 51 4c 69 74 65 20 61 73 73 75  ault SQLite assu
c1e0: 6d 65 73 20 74 68 61 74 20 73 65 63 74 6f 72 20  mes that sector 
c1f0: 77 72 69 74 65 73 20 61 72 65 0a 6c 69 6e 65 61  writes are.linea
c200: 72 20 62 75 74 20 6e 6f 74 20 61 74 6f 6d 69 63  r but not atomic
c210: 2e 20 20 41 20 6c 69 6e 65 61 72 20 77 72 69 74  .  A linear writ
c220: 65 20 73 74 61 72 74 73 20 61 74 20 6f 6e 65 20  e starts at one 
c230: 65 6e 64 20 6f 66 20 74 68 65 0a 73 65 63 74 6f  end of the.secto
c240: 72 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69 6e  r and changes in
c250: 66 6f 72 6d 61 74 69 6f 6e 20 62 79 74 65 20 62  formation byte b
c260: 79 20 62 79 74 65 20 75 6e 74 69 6c 20 69 74 20  y byte until it 
c270: 67 65 74 73 20 74 6f 20 74 68 65 0a 6f 74 68 65  gets to the.othe
c280: 72 20 65 6e 64 20 6f 66 20 74 68 65 20 73 65 63  r end of the sec
c290: 74 6f 72 2e 20 20 49 66 20 61 20 70 6f 77 65 72  tor.  If a power
c2a0: 20 6c 6f 73 73 20 6f 63 63 75 72 73 20 69 6e 20   loss occurs in 
c2b0: 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 61 20  the middle of.a 
c2c0: 6c 69 6e 65 61 72 20 77 72 69 74 65 20 74 68 65  linear write the
c2d0: 6e 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 65  n part of the se
c2e0: 63 74 6f 72 20 6d 69 67 68 74 20 62 65 20 6d 6f  ctor might be mo
c2f0: 64 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 65  dified while the
c300: 0a 6f 74 68 65 72 20 65 6e 64 20 69 73 20 75 6e  .other end is un
c310: 63 68 61 6e 67 65 64 2e 20 20 49 6e 20 61 6e 20  changed.  In an 
c320: 61 74 6f 6d 69 63 20 73 65 63 74 6f 72 20 77 72  atomic sector wr
c330: 69 74 65 2c 20 65 69 74 68 65 72 20 74 68 65 20  ite, either the 
c340: 65 6e 74 69 72 65 0a 73 65 63 74 6f 72 20 69 73  entire.sector is
c350: 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20   overwritten or 
c360: 65 6c 73 65 20 6e 6f 74 68 69 6e 67 20 69 6e 20  else nothing in 
c370: 74 68 65 20 73 65 63 74 6f 72 20 69 73 20 63 68  the sector is ch
c380: 61 6e 67 65 64 2e 3c 2f 70 3e 0a 0a 3c 70 3e 57  anged.</p>..<p>W
c390: 65 20 62 65 6c 69 65 76 65 20 74 68 61 74 20 6d  e believe that m
c3a0: 6f 73 74 20 6d 6f 64 65 72 6e 20 64 69 73 6b 20  ost modern disk 
c3b0: 64 72 69 76 65 73 20 69 6d 70 6c 65 6d 65 6e 74  drives implement
c3c0: 20 61 74 6f 6d 69 63 20 73 65 63 74 6f 72 0a 77   atomic sector.w
c3d0: 72 69 74 65 73 2e 20 20 57 68 65 6e 20 70 6f 77  rites.  When pow
c3e0: 65 72 20 69 73 20 6c 6f 73 74 2c 20 74 68 65 20  er is lost, the 
c3f0: 64 72 69 76 65 20 75 73 65 73 20 65 6e 65 72 67  drive uses energ
c400: 79 20 73 74 6f 72 65 64 20 69 6e 20 63 61 70 61  y stored in capa
c410: 63 69 74 6f 72 73 0a 61 6e 64 2f 6f 72 20 74 68  citors.and/or th
c420: 65 20 61 6e 67 75 6c 61 72 20 6d 6f 6d 65 6e 74  e angular moment
c430: 75 6d 20 6f 66 20 74 68 65 20 64 69 73 6b 20 70  um of the disk p
c440: 6c 61 74 74 65 72 20 74 6f 20 70 72 6f 76 69 64  latter to provid
c450: 65 20 70 6f 77 65 72 20 74 6f 20 0a 63 6f 6d 70  e power to .comp
c460: 6c 65 74 65 20 61 6e 79 20 6f 70 65 72 61 74 69  lete any operati
c470: 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 20  on in progress. 
c480: 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 74   Nevertheless, t
c490: 68 65 72 65 20 61 72 65 20 73 6f 20 6d 61 6e 79  here are so many
c4a0: 0a 6c 61 79 65 72 73 20 69 6e 20 62 65 74 77 65  .layers in betwe
c4b0: 65 6e 20 74 68 65 20 77 72 69 74 65 20 73 79 73  en the write sys
c4c0: 74 65 6d 20 63 61 6c 6c 20 61 6e 64 20 74 68 65  tem call and the
c4d0: 20 6f 6e 2d 62 6f 61 72 64 20 64 69 73 6b 20 64   on-board disk d
c4e0: 72 69 76 65 0a 65 6c 65 63 74 72 6f 6e 69 63 73  rive.electronics
c4f0: 20 74 68 61 74 20 77 65 20 74 61 6b 65 20 74 68   that we take th
c500: 65 20 73 61 66 65 20 61 70 70 72 6f 61 63 68 20  e safe approach 
c510: 69 6e 20 62 6f 74 68 20 55 6e 69 78 20 61 6e 64  in both Unix and
c520: 20 77 33 32 20 56 46 53 0a 69 6d 70 6c 65 6d 65   w32 VFS.impleme
c530: 6e 74 61 74 69 6f 6e 73 20 61 6e 64 20 61 73 73  ntations and ass
c540: 75 6d 65 20 74 68 61 74 20 73 65 63 74 6f 72 20  ume that sector 
c550: 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74 20 61  writes are not a
c560: 74 6f 6d 69 63 2e 20 20 4f 6e 20 74 68 65 0a 6f  tomic.  On the.o
c570: 74 68 65 72 20 68 61 6e 64 2c 20 64 65 76 69 63  ther hand, devic
c580: 65 0a 6d 61 6e 75 66 61 63 74 75 72 65 72 73 20  e.manufacturers 
c590: 77 69 74 68 20 6d 6f 72 65 20 63 6f 6e 74 72 6f  with more contro
c5a0: 6c 20 6f 76 65 72 20 74 68 65 69 72 20 66 69 6c  l over their fil
c5b0: 65 73 79 73 74 65 6d 73 20 6d 69 67 68 74 20 77  esystems might w
c5c0: 61 6e 74 0a 74 6f 20 63 6f 6e 73 69 64 65 72 20  ant.to consider 
c5d0: 65 6e 61 62 6c 69 6e 67 20 74 68 65 20 61 74 6f  enabling the ato
c5e0: 6d 69 63 20 77 72 69 74 65 20 70 72 6f 70 65 72  mic write proper
c5f0: 74 79 20 6f 66 20 78 44 65 76 69 63 65 43 68 61  ty of xDeviceCha
c600: 72 61 63 74 65 72 69 73 74 69 63 73 0a 69 66 20  racteristics.if 
c610: 74 68 65 69 72 20 68 61 72 64 77 61 72 65 20 72  their hardware r
c620: 65 61 6c 6c 79 20 64 6f 65 73 20 64 6f 20 61 74  eally does do at
c630: 6f 6d 69 63 20 77 72 69 74 65 73 2e 3c 2f 70 3e  omic writes.</p>
c640: 0a 0a 3c 70 3e 57 68 65 6e 20 73 65 63 74 6f 72  ..<p>When sector
c650: 20 77 72 69 74 65 73 20 61 72 65 20 61 74 6f 6d   writes are atom
c660: 69 63 20 61 6e 64 20 74 68 65 20 70 61 67 65 20  ic and the page 
c670: 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61 62 61  size of a databa
c680: 73 65 20 69 73 0a 74 68 65 20 73 61 6d 65 20 61  se is.the same a
c690: 73 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 2c  s a sector size,
c6a0: 20 61 6e 64 20 77 68 65 6e 20 74 68 65 72 65 20   and when there 
c6b0: 69 73 20 61 20 64 61 74 61 62 61 73 65 20 63 68  is a database ch
c6c0: 61 6e 67 65 20 74 68 61 74 0a 6f 6e 6c 79 20 74  ange that.only t
c6d0: 6f 75 63 68 65 73 20 61 20 73 69 6e 67 6c 65 20  ouches a single 
c6e0: 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 74  database page, t
c6f0: 68 65 6e 20 53 51 4c 69 74 65 20 73 6b 69 70 73  hen SQLite skips
c700: 20 74 68 65 20 77 68 6f 6c 65 0a 6a 6f 75 72 6e   the whole.journ
c710: 61 6c 69 6e 67 20 61 6e 64 20 73 79 6e 63 69 6e  aling and syncin
c720: 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 73 69  g process and si
c730: 6d 70 6c 79 20 77 72 69 74 65 73 20 74 68 65 20  mply writes the 
c740: 6d 6f 64 69 66 69 65 64 20 70 61 67 65 0a 64 69  modified page.di
c750: 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20  rectly into the 
c760: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
c770: 54 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  The change count
c780: 65 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 0a  er in the first.
c790: 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
c7a0: 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 6f 64  base file is mod
c7b0: 69 66 69 65 64 20 73 65 70 61 72 61 74 65 6c 79  ified separately
c7c0: 20 73 69 6e 63 65 20 6e 6f 20 68 61 72 6d 20 69   since no harm i
c7d0: 73 0a 64 6f 6e 65 20 69 66 20 70 6f 77 65 72 20  s.done if power 
c7e0: 69 73 20 6c 6f 73 74 20 62 65 66 6f 72 65 20 74  is lost before t
c7f0: 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  he change counte
c800: 72 20 63 61 6e 20 62 65 20 75 70 64 61 74 65 64  r can be updated
c810: 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66  .</p>..<tcl>hd_f
c820: 72 61 67 6d 65 6e 74 20 73 61 66 65 61 70 70 65  ragment safeappe
c830: 6e 64 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 37 2e 35  nd</tcl>.<h3>7.5
c840: 20 46 69 6c 65 73 79 73 74 65 6d 73 20 57 69 74   Filesystems Wit
c850: 68 20 53 61 66 65 20 41 70 70 65 6e 64 20 53 65  h Safe Append Se
c860: 6d 61 6e 74 69 63 73 3c 2f 68 33 3e 0a 0a 3c 70  mantics</h3>..<p
c870: 3e 41 6e 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a  >Another optimiz
c880: 61 74 69 6f 6e 20 69 6e 74 72 6f 64 75 63 65 64  ation introduced
c890: 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   in SQLite versi
c8a0: 6f 6e 20 33 2e 35 2e 30 20 6d 61 6b 65 73 0a 75  on 3.5.0 makes.u
c8b0: 73 65 20 6f 66 20 22 73 61 66 65 20 61 70 70 65  se of "safe appe
c8c0: 6e 64 22 20 62 65 68 61 76 69 6f 72 20 6f 66 20  nd" behavior of 
c8d0: 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  the underlying d
c8e0: 69 73 6b 2e 0a 52 65 63 61 6c 6c 20 74 68 61 74  isk..Recall that
c8f0: 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20   SQLite assumes 
c900: 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20 69  that when data i
c910: 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20  s appended to a 
c920: 66 69 6c 65 0a 28 73 70 65 63 69 66 69 63 61 6c  file.(specifical
c930: 6c 79 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61  ly to the rollba
c940: 63 6b 20 6a 6f 75 72 6e 61 6c 29 20 74 68 61 74  ck journal) that
c950: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
c960: 20 66 69 6c 65 0a 69 73 20 69 6e 63 72 65 61 73   file.is increas
c970: 65 64 20 66 69 72 73 74 20 61 6e 64 20 74 68 61  ed first and tha
c980: 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 73  t the content is
c990: 20 77 72 69 74 74 65 6e 20 73 65 63 6f 6e 64 2e   written second.
c9a0: 20 20 53 6f 0a 69 66 20 70 6f 77 65 72 20 69 73    So.if power is
c9b0: 20 6c 6f 73 74 20 61 66 74 65 72 20 74 68 65 20   lost after the 
c9c0: 66 69 6c 65 20 73 69 7a 65 20 69 73 20 69 6e 63  file size is inc
c9d0: 72 65 61 73 65 64 20 62 75 74 20 62 65 66 6f 72  reased but befor
c9e0: 65 20 74 68 65 0a 63 6f 6e 74 65 6e 74 20 69 73  e the.content is
c9f0: 20 77 72 69 74 74 65 6e 2c 20 74 68 65 20 66 69   written, the fi
ca00: 6c 65 20 69 73 20 6c 65 66 74 20 63 6f 6e 74 61  le is left conta
ca10: 69 6e 69 6e 67 20 69 6e 76 61 6c 69 64 20 22 67  ining invalid "g
ca20: 61 72 62 61 67 65 22 0a 64 61 74 61 2e 20 20 54  arbage".data.  T
ca30: 68 65 20 78 44 65 76 69 63 65 43 68 61 72 61 63  he xDeviceCharac
ca40: 74 65 72 69 73 74 69 63 73 20 6d 65 74 68 6f 64  teristics method
ca50: 20 6f 66 20 74 68 65 20 56 46 53 20 6d 69 67 68   of the VFS migh
ca60: 74 2c 20 68 6f 77 65 76 65 72 2c 0a 69 6e 64 69  t, however,.indi
ca70: 63 61 74 65 20 74 68 61 74 20 74 68 65 20 66 69  cate that the fi
ca80: 6c 65 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65  lesystem impleme
ca90: 6e 74 73 20 22 73 61 66 65 20 61 70 70 65 6e 64  nts "safe append
caa0: 22 20 73 65 6d 61 6e 74 69 63 73 2e 0a 54 68 69  " semantics..Thi
cab0: 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
cac0: 20 63 6f 6e 74 65 6e 74 20 69 73 20 77 72 69 74   content is writ
cad0: 74 65 6e 20 62 65 66 6f 72 65 20 74 68 65 20 66  ten before the f
cae0: 69 6c 65 20 73 69 7a 65 20 69 73 0a 69 6e 63 72  ile size is.incr
caf0: 65 61 73 65 64 20 73 6f 20 74 68 61 74 20 69 74  eased so that it
cb00: 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66   is impossible f
cb10: 6f 72 20 67 61 72 62 61 67 65 20 74 6f 20 62 65  or garbage to be
cb20: 20 69 6e 74 72 6f 64 75 63 65 64 0a 69 6e 74 6f   introduced.into
cb30: 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
cb40: 75 72 6e 61 6c 20 62 79 20 61 20 70 6f 77 65 72  urnal by a power
cb50: 20 6c 6f 73 73 20 6f 72 20 73 79 73 74 65 6d 20   loss or system 
cb60: 63 72 61 73 68 2e 3c 2f 70 3e 0a 0a 3c 70 3e 57  crash.</p>..<p>W
cb70: 68 65 6e 20 73 61 66 65 20 61 70 70 65 6e 64 20  hen safe append 
cb80: 73 65 6d 61 6e 74 69 63 73 20 61 72 65 20 69 6e  semantics are in
cb90: 64 69 63 61 74 65 64 20 66 6f 72 20 61 20 66 69  dicated for a fi
cba0: 6c 65 73 79 73 74 65 6d 2c 0a 53 51 4c 69 74 65  lesystem,.SQLite
cbb0: 20 61 6c 77 61 79 73 20 73 74 6f 72 65 73 20 74   always stores t
cbc0: 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  he special value
cbd0: 20 6f 66 20 2d 31 20 66 6f 72 20 74 68 65 20 70   of -1 for the p
cbe0: 61 67 65 20 63 6f 75 6e 74 0a 69 6e 20 74 68 65  age count.in the
cbf0: 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 72   header of the r
cc00: 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
cc10: 20 20 54 68 65 20 2d 31 20 70 61 67 65 20 63 6f    The -1 page co
cc20: 75 6e 74 20 76 61 6c 75 65 0a 74 65 6c 6c 73 20  unt value.tells 
cc30: 61 6e 79 20 70 72 6f 63 65 73 73 20 61 74 74 65  any process atte
cc40: 6d 70 74 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61  mpting to rollba
cc50: 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  ck the journal t
cc60: 68 61 74 20 74 68 65 0a 6e 75 6d 62 65 72 20 6f  hat the.number o
cc70: 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6a  f pages in the j
cc80: 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 20 62 65  ournal should be
cc90: 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 74   computed from t
cca0: 68 65 20 6a 6f 75 72 6e 61 6c 0a 73 69 7a 65 2e  he journal.size.
ccb0: 20 20 54 68 69 73 20 2d 31 20 76 61 6c 75 65 20    This -1 value 
ccc0: 69 73 20 6e 65 76 65 72 20 63 68 61 6e 67 65 64  is never changed
ccd0: 2e 20 20 53 6f 20 74 68 61 74 20 77 68 65 6e 20  .  So that when 
cce0: 61 20 63 6f 6d 6d 69 74 0a 6f 63 63 75 72 73 2c  a commit.occurs,
ccf0: 20 77 65 20 73 61 76 65 20 61 20 73 69 6e 67 6c   we save a singl
cd00: 65 20 66 6c 75 73 68 20 6f 70 65 72 61 74 69 6f  e flush operatio
cd10: 6e 20 61 6e 64 20 61 20 73 65 63 74 6f 72 20 77  n and a sector w
cd20: 72 69 74 65 20 6f 66 0a 74 68 65 20 66 69 72 73  rite of.the firs
cd30: 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6a 6f  t page of the jo
cd40: 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 46 75 72  urnal file.  Fur
cd50: 74 68 65 72 6d 6f 72 65 2c 20 77 68 65 6e 20 61  thermore, when a
cd60: 20 63 61 63 68 65 0a 73 70 69 6c 6c 20 6f 63 63   cache.spill occ
cd70: 75 72 73 20 77 65 20 6e 6f 20 6c 6f 6e 67 65 72  urs we no longer
cd80: 20 6e 65 65 64 20 74 6f 20 61 70 70 65 6e 64 20   need to append 
cd90: 61 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 68 65  a new journal he
cda0: 61 64 65 72 0a 74 6f 20 74 68 65 20 65 6e 64 20  ader.to the end 
cdb0: 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 3b 20  of the journal; 
cdc0: 77 65 20 63 61 6e 20 73 69 6d 70 6c 79 20 63 6f  we can simply co
cdd0: 6e 74 69 6e 75 65 20 61 70 70 65 6e 64 69 6e 67  ntinue appending
cde0: 0a 6e 65 77 20 70 61 67 65 73 20 74 6f 20 74 68  .new pages to th
cdf0: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 65 78 69  e end of the exi
ce00: 73 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 2e 3c 2f  sting journal.</
ce10: 70 3e 0a 0a 3c 61 20 6e 61 6d 65 3d 22 73 65 63  p>..<a name="sec
ce20: 74 69 6f 6e 5f 37 5f 36 22 3e 3c 2f 61 3e 0a 3c  tion_7_6"></a>.<
ce30: 68 33 3e 37 2e 36 20 50 65 72 73 69 73 74 65 6e  h3>7.6 Persisten
ce40: 74 20 52 6f 6c 6c 62 61 63 6b 20 4a 6f 75 72 6e  t Rollback Journ
ce50: 61 6c 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 44 65 6c  als</h3>..<p>Del
ce60: 65 74 69 6e 67 20 61 20 66 69 6c 65 20 69 73 20  eting a file is 
ce70: 61 6e 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65  an expensive ope
ce80: 72 61 74 69 6f 6e 20 6f 6e 20 6d 61 6e 79 20 73  ration on many s
ce90: 79 73 74 65 6d 73 2e 0a 53 6f 20 61 73 20 61 6e  ystems..So as an
cea0: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 53   optimization, S
ceb0: 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 6f 6e  QLite can be con
cec0: 66 69 67 75 72 65 64 20 74 6f 20 61 76 6f 69 64  figured to avoid
ced0: 20 74 68 65 0a 64 65 6c 65 74 65 20 6f 70 65 72   the.delete oper
cee0: 61 74 69 6f 6e 20 6f 66 20 3c 61 20 68 72 65 66  ation of <a href
cef0: 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 31 31 22  ="#section_3_11"
cf00: 3e 73 65 63 74 69 6f 6e 20 33 2e 31 31 3c 2f 61  >section 3.11</a
cf10: 3e 2e 0a 49 6e 73 74 65 61 64 20 6f 66 20 64 65  >..Instead of de
cf20: 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  leting the journ
cf30: 61 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72  al file in order
cf40: 20 74 6f 20 63 6f 6d 6d 69 74 20 61 20 74 72 61   to commit a tra
cf50: 6e 73 61 63 74 69 6f 6e 2c 0a 74 68 65 20 66 69  nsaction,.the fi
cf60: 6c 65 20 69 73 20 65 69 74 68 65 72 20 74 72 75  le is either tru
cf70: 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 62  ncated to zero b
cf80: 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 20 6f  ytes in length o
cf90: 72 20 69 74 73 0a 68 65 61 64 65 72 20 69 73 20  r its.header is 
cfa0: 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68  overwritten with
cfb0: 20 7a 65 72 6f 73 2e 20 20 54 72 75 6e 63 61 74   zeros.  Truncat
cfc0: 69 6e 67 20 74 68 65 20 66 69 6c 65 20 74 6f 20  ing the file to 
cfd0: 7a 65 72 6f 0a 6c 65 6e 67 74 68 20 73 61 76 65  zero.length save
cfe0: 73 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65  s having to make
cff0: 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74   modifications t
d000: 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  o the directory 
d010: 63 6f 6e 74 61 69 6e 69 6e 67 0a 74 68 65 20 66  containing.the f
d020: 69 6c 65 20 73 69 6e 63 65 20 74 68 65 20 66 69  ile since the fi
d030: 6c 65 20 69 73 20 6e 6f 74 20 72 65 6d 6f 76 65  le is not remove
d040: 64 20 66 72 6f 6d 20 74 68 65 20 64 69 72 65 63  d from the direc
d050: 74 6f 72 79 2e 20 0a 4f 76 65 72 77 72 69 74 69  tory. .Overwriti
d060: 6e 67 20 74 68 65 20 68 65 61 64 65 72 20 68 61  ng the header ha
d070: 73 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c  s the additional
d080: 20 73 61 76 69 6e 67 73 20 6f 66 20 6e 6f 74 20   savings of not 
d090: 68 61 76 69 6e 67 0a 74 6f 20 75 70 64 61 74 65  having.to update
d0a0: 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   the length of t
d0b0: 68 65 20 66 69 6c 65 20 28 69 6e 20 74 68 65 20  he file (in the 
d0c0: 22 69 6e 6f 64 65 22 20 6f 6e 20 6d 61 6e 79 20  "inode" on many 
d0d0: 73 79 73 74 65 6d 73 29 0a 61 6e 64 20 6e 6f 74  systems).and not
d0e0: 20 68 61 76 69 6e 67 20 74 6f 20 64 65 61 6c 20   having to deal 
d0f0: 77 69 74 68 20 6e 65 77 6c 79 20 66 72 65 65 64  with newly freed
d100: 20 64 69 73 6b 20 73 65 63 74 6f 72 73 2e 20 20   disk sectors.  
d110: 46 75 72 74 68 65 72 6d 6f 72 65 2c 0a 61 74 20  Furthermore,.at 
d120: 74 68 65 20 6e 65 78 74 20 74 72 61 6e 73 61 63  the next transac
d130: 74 69 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  tion the journal
d140: 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64   will be created
d150: 20 62 79 20 6f 76 65 72 77 72 69 74 69 6e 67 0a   by overwriting.
d160: 65 78 69 73 74 69 6e 67 20 63 6f 6e 74 65 6e 74  existing content
d170: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 70 70   rather than app
d180: 65 6e 64 69 6e 67 20 6e 65 77 20 63 6f 6e 74 65  ending new conte
d190: 6e 74 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 0a  nt onto the end.
d1a0: 6f 66 20 61 20 66 69 6c 65 2c 20 61 6e 64 20 6f  of a file, and o
d1b0: 76 65 72 77 72 69 74 69 6e 67 20 69 73 20 6f 66  verwriting is of
d1c0: 74 65 6e 20 6d 75 63 68 20 66 61 73 74 65 72 20  ten much faster 
d1d0: 74 68 61 6e 20 61 70 70 65 6e 64 69 6e 67 2e 3c  than appending.<
d1e0: 2f 70 3e 0a 0a 3c 70 3e 53 51 4c 69 74 65 20 63  /p>..<p>SQLite c
d1f0: 61 6e 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64  an be configured
d200: 20 74 6f 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73   to commit trans
d210: 61 63 74 69 6f 6e 73 20 62 79 20 6f 76 65 72 77  actions by overw
d220: 72 69 74 69 6e 67 0a 74 68 65 20 6a 6f 75 72 6e  riting.the journ
d230: 61 6c 20 68 65 61 64 65 72 20 77 69 74 68 20 7a  al header with z
d240: 65 72 6f 73 20 69 6e 73 74 65 61 64 20 6f 66 20  eros instead of 
d250: 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75  deleting the jou
d260: 72 6e 61 6c 20 66 69 6c 65 0a 62 79 20 73 65 74  rnal file.by set
d270: 74 69 6e 67 20 74 68 65 20 22 50 45 52 53 49 53  ting the "PERSIS
d280: 54 22 20 6a 6f 75 72 6e 61 6c 69 6e 67 20 6d 6f  T" journaling mo
d290: 64 65 20 75 73 69 6e 67 20 74 68 65 20 0a 3c 61  de using the .<a
d2a0: 20 68 72 65 66 3d 22 70 72 61 67 6d 61 2e 68 74   href="pragma.ht
d2b0: 6d 6c 23 70 72 61 67 6d 61 5f 6a 6f 75 72 6e 61  ml#pragma_journa
d2c0: 6c 5f 6d 6f 64 65 22 3e 6a 6f 75 72 6e 61 6c 5f  l_mode">journal_
d2d0: 6d 6f 64 65 3c 2f 61 3e 20 50 52 41 47 4d 41 2e  mode</a> PRAGMA.
d2e0: 0a 46 6f 72 20 65 78 61 6d 70 6c 65 3a 3c 2f 70  .For example:</p
d2f0: 3e 0a 0a 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  >..<blockquote><
d300: 70 72 65 3e 0a 50 52 41 47 4d 41 20 6a 6f 75 72  pre>.PRAGMA jour
d310: 6e 61 6c 5f 6d 6f 64 65 3d 50 45 52 53 49 53 54  nal_mode=PERSIST
d320: 3b 0a 3c 2f 70 65 72 3e 3c 2f 62 6c 6f 63 6b 71  ;.</per></blockq
d330: 75 6f 74 65 3e 0a 0a 3c 70 3e 54 68 65 20 75 73  uote>..<p>The us
d340: 65 20 6f 66 20 70 65 72 73 69 73 74 65 6e 74 20  e of persistent 
d350: 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 70 72 6f  journal mode pro
d360: 76 69 64 65 20 61 20 6e 6f 74 69 63 65 61 62 6c  vide a noticeabl
d370: 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 69 6d  e performance.im
d380: 70 72 6f 76 65 6d 65 6e 74 20 6f 6e 20 6d 61 6e  provement on man
d390: 79 20 73 79 73 74 65 6d 73 2e 20 20 4f 66 20 63  y systems.  Of c
d3a0: 6f 75 72 73 65 2c 20 74 68 65 20 64 72 61 77 62  ourse, the drawb
d3b0: 61 63 6b 20 69 73 20 74 68 61 74 20 74 68 65 20  ack is that the 
d3c0: 0a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 72  .journal files r
d3d0: 65 6d 61 69 6e 20 6f 6e 20 74 68 65 20 64 69 73  emain on the dis
d3e0: 6b 2c 20 75 73 69 6e 67 20 64 69 73 6b 20 73 70  k, using disk sp
d3f0: 61 63 65 20 61 6e 64 20 63 6c 75 74 74 65 72 69  ace and clutteri
d400: 6e 67 0a 64 69 72 65 63 74 6f 72 69 65 73 2c 20  ng.directories, 
d410: 6c 6f 6e 67 20 61 66 74 65 72 20 74 68 65 20 74  long after the t
d420: 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69  ransaction commi
d430: 74 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 73 61  ts.  The only sa
d440: 66 65 20 77 61 79 0a 74 6f 20 64 65 6c 65 74 65  fe way.to delete
d450: 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f   a persistent jo
d460: 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 6f  urnal file is to
d470: 20 63 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61   commit a transa
d480: 63 74 69 6f 6e 0a 77 69 74 68 20 6a 6f 75 72 6e  ction.with journ
d490: 61 6c 69 6e 67 20 6d 6f 64 65 20 73 65 74 20 74  aling mode set t
d4a0: 6f 20 44 45 4c 45 54 45 3a 3c 2f 70 3e 0a 0a 3c  o DELETE:</p>..<
d4b0: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
d4c0: 0a 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f  .PRAGMA journal_
d4d0: 6d 6f 64 65 3d 44 45 4c 45 54 45 3b 0a 42 45 47  mode=DELETE;.BEG
d4e0: 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 0a 43 4f  IN EXCLUSIVE;.CO
d4f0: 4d 4d 49 54 3b 0a 3c 2f 70 65 72 3e 3c 2f 62 6c  MMIT;.</per></bl
d500: 6f 63 6b 71 75 6f 74 65 3e 0a 0a 3c 70 3e 42 65  ockquote>..<p>Be
d510: 77 61 72 65 20 6f 66 20 64 65 6c 65 74 69 6e 67  ware of deleting
d520: 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72   persistent jour
d530: 6e 61 6c 20 66 69 6c 65 73 20 62 79 20 61 6e 79  nal files by any
d540: 20 6f 74 68 65 72 20 6d 65 61 6e 73 0a 73 69 6e   other means.sin
d550: 63 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ce the journal f
d560: 69 6c 65 20 6d 69 67 68 74 20 62 65 20 68 6f 74  ile might be hot
d570: 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
d580: 64 65 6c 65 74 69 6e 67 20 69 74 20 77 69 6c 6c  deleting it will
d590: 0a 63 6f 72 72 75 70 74 20 74 68 65 20 63 6f 72  .corrupt the cor
d5a0: 72 65 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 62  responding datab
d5b0: 61 73 65 20 66 69 6c 65 2e 3c 2f 70 3e 0a 0a 3c  ase file.</p>..<
d5c0: 70 3e 42 65 67 69 6e 6e 69 6e 67 20 69 6e 20 53  p>Beginning in S
d5d0: 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
d5e0: 36 2e 34 2c 20 74 68 65 20 54 52 55 4e 43 41 54  6.4, the TRUNCAT
d5f0: 45 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 69  E journal mode i
d600: 73 0a 61 6c 73 6f 20 73 75 70 70 6f 72 74 65 64  s.also supported
d610: 3a 3c 2f 70 3e 0a 0a 3c 62 6c 6f 63 6b 71 75 6f  :</p>..<blockquo
d620: 74 65 3e 3c 70 72 65 3e 0a 50 52 41 47 4d 41 20  te><pre>.PRAGMA 
d630: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 54 52 55  journal_mode=TRU
d640: 4e 43 41 54 45 3b 0a 3c 2f 70 72 65 3e 3c 2f 62  NCATE;.</pre></b
d650: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 0a 3c 70 3e 49  lockquote>..<p>I
d660: 6e 20 74 72 75 6e 63 61 74 65 20 6a 6f 75 72 6e  n truncate journ
d670: 61 6c 20 6d 6f 64 65 2c 20 74 68 65 20 74 72 61  al mode, the tra
d680: 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
d690: 69 74 74 65 64 20 62 79 20 74 72 75 6e 63 61 74  itted by truncat
d6a0: 69 6e 67 0a 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing.the journal 
d6b0: 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 6c 65 6e  file to zero len
d6c0: 67 74 68 20 72 61 74 68 65 72 20 74 68 61 6e 20  gth rather than 
d6d0: 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75  deleting the jou
d6e0: 72 6e 61 6c 20 66 69 6c 65 0a 28 61 73 20 69 6e  rnal file.(as in
d6f0: 20 44 45 4c 45 54 45 20 6d 6f 64 65 29 20 6f 72   DELETE mode) or
d700: 20 62 79 20 7a 65 72 6f 69 6e 67 20 74 68 65 20   by zeroing the 
d710: 68 65 61 64 65 72 20 28 61 73 20 69 6e 20 50 45  header (as in PE
d720: 52 53 49 53 54 20 6d 6f 64 65 29 2e 0a 54 52 55  RSIST mode)..TRU
d730: 4e 43 41 54 45 20 6d 6f 64 65 20 73 68 61 72 65  NCATE mode share
d740: 73 20 74 68 65 20 61 64 76 61 6e 74 61 67 65 20  s the advantage 
d750: 6f 66 20 50 45 52 53 49 53 54 20 6d 6f 64 65 20  of PERSIST mode 
d760: 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f  that the directo
d770: 72 79 0a 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ry.that contains
d780: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
d790: 65 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 64  e and database d
d7a0: 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
d7b0: 62 65 20 75 70 64 61 74 65 64 2e 0a 48 65 6e 63  be updated..Henc
d7c0: 65 20 74 72 75 6e 63 61 74 69 6e 67 20 61 20 66  e truncating a f
d7d0: 69 6c 65 20 69 73 20 6f 66 74 65 6e 20 66 61 73  ile is often fas
d7e0: 74 65 72 20 74 68 61 6e 20 64 65 6c 65 74 69 6e  ter than deletin
d7f0: 67 20 69 74 2e 20 20 54 52 55 4e 43 41 54 45 20  g it.  TRUNCATE 
d800: 68 61 73 0a 74 68 65 20 61 64 64 69 74 69 6f 6e  has.the addition
d810: 61 6c 20 61 64 76 61 6e 74 61 67 65 20 74 68 61  al advantage tha
d820: 74 20 69 74 20 69 73 20 6e 6f 74 20 66 6f 6c 6c  t it is not foll
d830: 6f 77 65 64 20 62 79 20 61 0a 73 79 73 74 65 6d  owed by a.system
d840: 20 63 61 6c 6c 20 28 65 78 3a 20 66 73 79 6e 63   call (ex: fsync
d850: 28 29 29 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69  ()) to synchroni
d860: 7a 65 20 74 68 65 20 63 68 61 6e 67 65 20 74 6f  ze the change to
d870: 20 64 69 73 6b 2e 20 20 49 74 20 6d 69 67 68 74   disk.  It might
d880: 0a 62 65 20 73 61 66 65 72 20 69 66 20 69 74 20  .be safer if it 
d890: 64 69 64 2e 0a 42 75 74 20 6f 6e 20 6d 61 6e 79  did..But on many
d8a0: 20 6d 6f 64 65 72 6e 20 66 69 6c 65 73 79 73 74   modern filesyst
d8b0: 65 6d 73 2c 20 61 20 74 72 75 6e 63 61 74 65 20  ems, a truncate 
d8c0: 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 61 6e 64  is an atomic and
d8d0: 0a 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6f 70 65  .synchronous ope
d8e0: 72 61 74 69 6f 6e 20 61 6e 64 20 73 6f 20 77 65  ration and so we
d8f0: 20 74 68 69 6e 6b 20 74 68 61 74 20 54 52 55 4e   think that TRUN
d900: 43 41 54 45 20 77 69 6c 6c 20 75 73 75 61 6c 6c  CATE will usuall
d910: 79 20 62 65 20 73 61 66 65 0a 69 6e 20 74 68 65  y be safe.in the
d920: 20 66 61 63 65 20 6f 66 20 70 6f 77 65 72 20 66   face of power f
d930: 61 69 6c 75 72 65 73 2e 20 20 49 66 20 79 6f 75  ailures.  If you
d940: 20 61 72 65 20 75 6e 63 65 72 74 61 69 6e 20 61   are uncertain a
d950: 62 6f 75 74 20 77 68 65 74 68 65 72 20 6f 72 0a  bout whether or.
d960: 6e 6f 74 20 54 52 55 4e 43 41 54 45 20 77 69 6c  not TRUNCATE wil
d970: 6c 20 62 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73  l be synchronous
d980: 20 61 6e 64 20 61 74 6f 6d 69 63 20 6f 6e 20 79   and atomic on y
d990: 6f 75 72 20 66 69 6c 65 73 79 73 74 65 6d 20 61  our filesystem a
d9a0: 6e 64 20 69 74 20 69 73 0a 69 6d 70 6f 72 74 61  nd it is.importa
d9b0: 6e 74 20 74 6f 20 79 6f 75 20 74 68 61 74 20 79  nt to you that y
d9c0: 6f 75 72 20 64 61 74 61 62 61 73 65 20 73 75 72  our database sur
d9d0: 76 69 76 65 20 61 20 70 6f 77 65 72 20 6c 6f 73  vive a power los
d9e0: 73 20 6f 72 20 6f 70 65 72 61 74 69 6e 67 0a 73  s or operating.s
d9f0: 79 73 74 65 6d 20 63 72 61 73 68 20 74 68 61 74  ystem crash that
da00: 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
da10: 68 65 20 74 72 75 6e 63 61 74 69 6f 6e 20 6f 70  he truncation op
da20: 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 79 6f  eration, then yo
da30: 75 20 6d 69 67 68 74 0a 63 6f 6e 73 69 64 65 72  u might.consider
da40: 20 75 73 69 6e 67 20 61 20 64 69 66 66 65 72 65   using a differe
da50: 6e 74 20 6a 6f 75 72 6e 61 6c 69 6e 67 20 6d 6f  nt journaling mo
da60: 64 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e 4f 6e 20 65  de.</p>..<p>On e
da70: 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20  mbedded systems 
da80: 77 69 74 68 20 73 79 6e 63 68 72 6f 6e 6f 75 73  with synchronous
da90: 20 66 69 6c 65 73 79 73 74 65 6d 73 2c 20 54 52   filesystems, TR
daa0: 55 4e 43 41 54 45 20 72 65 73 75 6c 74 73 0a 69  UNCATE results.i
dab0: 6e 20 73 6c 6f 77 65 72 20 62 65 68 61 76 69 6f  n slower behavio
dac0: 72 20 74 68 61 6e 20 50 45 52 53 49 53 54 2e 20  r than PERSIST. 
dad0: 20 54 68 65 20 63 6f 6d 6d 69 74 20 6f 70 65 72   The commit oper
dae0: 61 74 69 6f 6e 20 69 73 20 74 68 65 20 73 61 6d  ation is the sam
daf0: 65 20 73 70 65 65 64 2e 0a 42 75 74 20 73 75 62  e speed..But sub
db00: 73 65 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74  sequent transact
db10: 69 6f 6e 73 20 61 72 65 20 73 6c 6f 77 65 72 20  ions are slower 
db20: 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 54 52 55 4e  following a TRUN
db30: 43 41 54 45 20 62 65 63 61 75 73 65 20 69 74 20  CATE because it 
db40: 69 73 0a 66 61 73 74 65 72 20 74 6f 20 6f 76 65  is.faster to ove
db50: 72 77 72 69 74 65 20 65 78 69 73 74 69 6e 67 20  rwrite existing 
db60: 63 6f 6e 74 65 6e 74 20 74 68 61 6e 20 74 6f 20  content than to 
db70: 61 70 70 65 6e 64 20 74 6f 20 74 68 65 20 65 6e  append to the en
db80: 64 20 6f 66 20 61 20 66 69 6c 65 2e 0a 4e 65 77  d of a file..New
db90: 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 6e   journal file en
dba0: 74 72 69 65 73 20 77 69 6c 6c 20 61 6c 77 61 79  tries will alway
dbb0: 73 20 62 65 20 61 70 70 65 6e 64 65 64 20 66 6f  s be appended fo
dbc0: 6c 6c 6f 77 69 6e 67 20 61 20 54 52 55 4e 43 41  llowing a TRUNCA
dbd0: 54 45 20 62 75 74 0a 77 69 6c 6c 20 75 73 75 61  TE but.will usua
dbe0: 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 77 69  lly overwrite wi
dbf0: 74 68 20 50 45 52 53 49 53 54 2e 3c 2f 70 3e 0a  th PERSIST.</p>.
dc00: 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e  .<tcl>hd_fragmen
dc10: 74 20 20 74 65 73 74 69 6e 67 3c 2f 74 63 6c 3e  t  testing</tcl>
dc20: 0a 3c 68 32 3e 38 2e 30 20 54 65 73 74 69 6e 67  .<h2>8.0 Testing
dc30: 20 41 74 6f 6d 69 63 20 43 6f 6d 6d 69 74 20 42   Atomic Commit B
dc40: 65 68 61 76 69 6f 72 3c 2f 68 32 3e 0a 0a 3c 70  ehavior</h2>..<p
dc50: 3e 54 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20  >The developers 
dc60: 6f 66 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f  of SQLite are co
dc70: 6e 66 69 64 65 6e 74 20 74 68 61 74 20 69 74 20  nfident that it 
dc80: 69 73 20 72 6f 62 75 73 74 0a 69 6e 20 74 68 65  is robust.in the
dc90: 20 66 61 63 65 20 6f 66 20 70 6f 77 65 72 20 66   face of power f
dca0: 61 69 6c 75 72 65 73 20 61 6e 64 20 73 79 73 74  ailures and syst
dcb0: 65 6d 20 63 72 61 73 68 65 73 20 62 65 63 61 75  em crashes becau
dcc0: 73 65 20 74 68 65 0a 61 75 74 6f 6d 61 74 69 63  se the.automatic
dcd0: 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
dce0: 20 64 6f 20 65 78 74 65 6e 73 69 76 65 20 63 68   do extensive ch
dcf0: 65 63 6b 73 20 6f 6e 0a 74 68 65 20 61 62 69 6c  ecks on.the abil
dd00: 69 74 79 20 6f 66 20 53 51 4c 69 74 65 20 74 6f  ity of SQLite to
dd10: 20 72 65 63 6f 76 65 72 20 66 72 6f 6d 20 73 69   recover from si
dd20: 6d 75 6c 61 74 65 64 20 70 6f 77 65 72 20 6c 6f  mulated power lo
dd30: 73 73 2e 0a 57 65 20 63 61 6c 6c 20 74 68 65 73  ss..We call thes
dd40: 65 20 74 68 65 20 22 63 72 61 73 68 20 74 65 73  e the "crash tes
dd50: 74 73 22 2e 3c 2f 70 3e 0a 0a 3c 70 3e 43 72 61  ts".</p>..<p>Cra
dd60: 73 68 20 74 65 73 74 73 20 69 6e 20 53 51 4c 69  sh tests in SQLi
dd70: 74 65 20 75 73 65 20 61 20 6d 6f 64 69 66 69 65  te use a modifie
dd80: 64 20 56 46 53 20 74 68 61 74 20 63 61 6e 20 73  d VFS that can s
dd90: 69 6d 75 6c 61 74 65 0a 74 68 65 20 6b 69 6e 64  imulate.the kind
dda0: 73 20 6f 66 20 66 69 6c 65 73 79 73 74 65 6d 20  s of filesystem 
ddb0: 64 61 6d 61 67 65 20 74 68 61 74 20 6f 63 63 75  damage that occu
ddc0: 72 20 64 75 72 69 6e 67 20 61 20 70 6f 77 65 72  r during a power
ddd0: 0a 6c 6f 73 73 20 6f 72 20 6f 70 65 72 61 74 69  .loss or operati
dde0: 6e 67 20 73 79 73 74 65 6d 20 63 72 61 73 68 2e  ng system crash.
ddf0: 20 20 54 68 65 20 63 72 61 73 68 2d 74 65 73 74    The crash-test
de00: 20 56 46 53 20 63 61 6e 20 73 69 6d 75 6c 61 74   VFS can simulat
de10: 65 0a 69 6e 63 6f 6d 70 6c 65 74 65 20 73 65 63  e.incomplete sec
de20: 74 6f 72 20 77 72 69 74 65 73 2c 20 70 61 67 65  tor writes, page
de30: 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 67 61  s filled with ga
de40: 72 62 61 67 65 20 64 61 74 61 20 62 65 63 61 75  rbage data becau
de50: 73 65 0a 61 20 77 72 69 74 65 20 68 61 73 20 6e  se.a write has n
de60: 6f 74 20 63 6f 6d 70 6c 65 74 65 64 2c 20 61 6e  ot completed, an
de70: 64 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 20 77  d out of order w
de80: 72 69 74 65 73 2c 20 61 6c 6c 20 6f 63 63 75 72  rites, all occur
de90: 72 69 6e 67 0a 61 74 20 76 61 72 79 69 6e 67 20  ring.at varying 
dea0: 70 6f 69 6e 74 73 20 64 75 72 69 6e 67 20 61 20  points during a 
deb0: 74 65 73 74 20 73 63 65 6e 61 72 69 6f 2e 20 20  test scenario.  
dec0: 43 72 61 73 68 20 74 65 73 74 73 20 65 78 65 63  Crash tests exec
ded0: 75 74 65 0a 74 72 61 6e 73 61 63 74 69 6f 6e 73  ute.transactions
dee0: 20 6f 76 65 72 20 61 6e 64 20 6f 76 65 72 2c 20   over and over, 
def0: 76 61 72 79 69 6e 67 20 74 68 65 20 74 69 6d 65  varying the time
df00: 20 61 74 20 77 68 69 63 68 20 61 20 73 69 6d 75   at which a simu
df10: 6c 61 74 65 64 0a 70 6f 77 65 72 20 6c 6f 73 73  lated.power loss
df20: 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20   occurs and the 
df30: 70 72 6f 70 65 72 74 69 65 73 20 6f 66 20 74 68  properties of th
df40: 65 20 64 61 6d 61 67 65 20 69 6e 66 6c 69 63 74  e damage inflict
df50: 65 64 2e 0a 45 61 63 68 20 74 65 73 74 20 74 68  ed..Each test th
df60: 65 6e 20 72 65 6f 70 65 6e 73 20 74 68 65 20 64  en reopens the d
df70: 61 74 61 62 61 73 65 20 61 66 74 65 72 20 74 68  atabase after th
df80: 65 20 73 69 6d 75 6c 61 74 65 64 20 63 72 61 73  e simulated cras
df90: 68 20 61 6e 64 0a 76 65 72 69 66 69 65 73 20 74  h and.verifies t
dfa0: 68 61 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  hat the transact
dfb0: 69 6f 6e 20 65 69 74 68 65 72 20 6f 63 63 75 72  ion either occur
dfc0: 72 65 64 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 6f  red completely.o
dfd0: 72 20 6e 6f 74 20 61 74 20 61 6c 6c 20 61 6e 64  r not at all and
dfe0: 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
dff0: 73 65 20 69 73 20 69 6e 20 61 20 63 6f 6d 70 6c  se is in a compl
e000: 65 74 65 6c 79 0a 63 6f 6e 73 69 73 74 65 6e 74  etely.consistent
e010: 20 73 74 61 74 65 2e 3c 2f 70 3e 0a 0a 3c 70 3e   state.</p>..<p>
e020: 54 68 65 20 63 72 61 73 68 20 74 65 73 74 73 20  The crash tests 
e030: 69 6e 20 53 51 4c 69 74 65 20 68 61 76 65 20 64  in SQLite have d
e040: 69 73 63 6f 76 65 72 65 64 20 61 20 6e 75 6d 62  iscovered a numb
e050: 65 72 20 6f 66 20 76 65 72 79 0a 73 75 62 74 6c  er of very.subtl
e060: 65 20 62 75 67 73 20 28 6e 6f 77 20 66 69 78 65  e bugs (now fixe
e070: 64 29 20 69 6e 20 74 68 65 20 72 65 63 6f 76 65  d) in the recove
e080: 72 79 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 53  ry mechanism.  S
e090: 6f 6d 65 20 6f 66 20 0a 74 68 65 73 65 20 62 75  ome of .these bu
e0a0: 67 73 20 77 65 72 65 20 76 65 72 79 20 6f 62 73  gs were very obs
e0b0: 63 75 72 65 20 61 6e 64 20 75 6e 6c 69 6b 65 6c  cure and unlikel
e0c0: 79 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 66  y to have been f
e0d0: 6f 75 6e 64 0a 75 73 69 6e 67 20 6f 6e 6c 79 20  ound.using only 
e0e0: 63 6f 64 65 20 69 6e 73 70 65 63 74 69 6f 6e 20  code inspection 
e0f0: 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 74 65 63  and analysis tec
e100: 68 6e 69 71 75 65 73 2e 20 20 46 72 6f 6d 20 74  hniques.  From t
e110: 68 69 73 0a 65 78 70 65 72 69 65 6e 63 65 2c 20  his.experience, 
e120: 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6f  the developers o
e130: 66 20 53 51 4c 69 74 65 20 66 65 65 6c 20 63 6f  f SQLite feel co
e140: 6e 66 69 64 65 6e 74 20 74 68 61 74 20 61 6e 79  nfident that any
e150: 20 6f 74 68 65 72 0a 64 61 74 61 62 61 73 65 20   other.database 
e160: 73 79 73 74 65 6d 20 74 68 61 74 20 64 6f 65 73  system that does
e170: 20 6e 6f 74 20 75 73 65 20 61 20 73 69 6d 69 6c   not use a simil
e180: 61 72 20 63 72 61 73 68 20 74 65 73 74 20 73 79  ar crash test sy
e190: 73 74 65 6d 0a 6c 69 6b 65 6c 79 20 63 6f 6e 74  stem.likely cont
e1a0: 61 69 6e 73 20 75 6e 64 65 74 65 63 74 65 64 20  ains undetected 
e1b0: 62 75 67 73 20 74 68 61 74 20 77 69 6c 6c 20 6c  bugs that will l
e1c0: 65 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 0a  ead to database.
e1d0: 63 6f 72 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f  corruption follo
e1e0: 77 69 6e 67 20 61 20 73 79 73 74 65 6d 20 63 72  wing a system cr
e1f0: 61 73 68 20 6f 72 20 70 6f 77 65 72 20 66 61 69  ash or power fai
e200: 6c 75 72 65 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e  lure.</p>..<tcl>
e210: 68 64 5f 66 72 61 67 6d 65 6e 74 20 7b 73 65 63  hd_fragment {sec
e220: 74 5f 39 5f 30 7d 20 7b 54 68 69 6e 67 73 20 54  t_9_0} {Things T
e230: 68 61 74 20 43 61 6e 20 47 6f 20 57 72 6f 6e 67  hat Can Go Wrong
e240: 7d 3c 2f 74 63 6c 3e 0a 3c 68 32 3e 39 2e 30 20  }</tcl>.<h2>9.0 
e250: 54 68 69 6e 67 73 20 54 68 61 74 20 43 61 6e 20  Things That Can 
e260: 47 6f 20 57 72 6f 6e 67 3c 2f 68 32 3e 0a 0a 3c  Go Wrong</h2>..<
e270: 70 3e 54 68 65 20 61 74 6f 6d 69 63 20 63 6f 6d  p>The atomic com
e280: 6d 69 74 20 6d 65 63 68 61 6e 69 73 6d 20 69 6e  mit mechanism in
e290: 20 53 51 4c 69 74 65 20 68 61 73 20 70 72 6f 76   SQLite has prov
e2a0: 65 6e 20 74 6f 20 62 65 20 72 6f 62 75 73 74 2c  en to be robust,
e2b0: 0a 62 75 74 20 69 74 20 63 61 6e 20 62 65 20 63  .but it can be c
e2c0: 69 72 63 75 6d 76 65 6e 74 65 64 20 62 79 20 61  ircumvented by a
e2d0: 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20 63 72   sufficiently cr
e2e0: 65 61 74 69 76 65 0a 61 64 76 65 72 73 61 72 79  eative.adversary
e2f0: 20 6f 72 20 61 20 73 75 66 66 69 63 69 65 6e 74   or a sufficient
e300: 6c 79 20 62 72 6f 6b 65 6e 20 6f 70 65 72 61 74  ly broken operat
e310: 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65  ing system imple
e320: 6d 65 6e 74 61 74 69 6f 6e 2e 0a 54 68 69 73 20  mentation..This 
e330: 73 65 63 74 69 6f 6e 20 64 65 73 63 72 69 62 65  section describe
e340: 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 20 77  s a few of the w
e350: 61 79 73 20 69 6e 20 77 68 69 63 68 20 61 6e 20  ays in which an 
e360: 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a  SQLite database.
e370: 6d 69 67 68 74 20 62 65 20 63 6f 72 72 75 70 74  might be corrupt
e380: 65 64 20 62 79 20 61 20 70 6f 77 65 72 20 66 61  ed by a power fa
e390: 69 6c 75 72 65 20 6f 72 20 73 79 73 74 65 6d 20  ilure or system 
e3a0: 63 72 61 73 68 2e 0a 28 53 65 65 20 61 6c 73 6f  crash..(See also
e3b0: 3a 20 5b 68 6f 77 20 74 6f 20 63 6f 72 72 75 70  : [how to corrup
e3c0: 74 20 7c 20 48 6f 77 20 54 6f 20 43 6f 72 72 75  t | How To Corru
e3d0: 70 74 20 59 6f 75 72 20 44 61 74 61 62 61 73 65  pt Your Database
e3e0: 20 46 69 6c 65 73 5d 2e 29 3c 2f 70 3e 0a 0a 3c   Files].)</p>..<
e3f0: 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20  tcl>hd_fragment 
e400: 62 72 6f 6b 65 6e 6c 6f 63 6b 73 3c 2f 74 63 6c  brokenlocks</tcl
e410: 3e 0a 3c 68 33 3e 39 2e 31 20 42 72 6f 6b 65 6e  >.<h3>9.1 Broken
e420: 20 4c 6f 63 6b 69 6e 67 20 49 6d 70 6c 65 6d 65   Locking Impleme
e430: 6e 74 61 74 69 6f 6e 73 3c 2f 68 33 3e 0a 0a 3c  ntations</h3>..<
e440: 70 3e 53 51 4c 69 74 65 20 75 73 65 73 20 66 69  p>SQLite uses fi
e450: 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73 20 74  lesystem locks t
e460: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
e470: 20 6f 6e 6c 79 20 6f 6e 65 0a 70 72 6f 63 65 73   only one.proces
e480: 73 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 63  s and database c
e490: 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 74 72 79  onnection is try
e4a0: 69 6e 67 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  ing to modify th
e4b0: 65 20 64 61 74 61 62 61 73 65 0a 61 74 20 61 20  e database.at a 
e4c0: 74 69 6d 65 2e 20 20 54 68 65 20 66 69 6c 65 73  time.  The files
e4d0: 79 73 74 65 6d 20 6c 6f 63 6b 69 6e 67 20 6d 65  ystem locking me
e4e0: 63 68 61 6e 69 73 6d 20 69 73 20 69 6d 70 6c 65  chanism is imple
e4f0: 6d 65 6e 74 65 64 0a 69 6e 20 74 68 65 20 56 46  mented.in the VF
e500: 53 20 6c 61 79 65 72 20 61 6e 64 20 69 73 20 64  S layer and is d
e510: 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65  ifferent for eve
e520: 72 79 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ry operating sys
e530: 74 65 6d 2e 0a 53 51 4c 69 74 65 20 64 65 70 65  tem..SQLite depe
e540: 6e 64 73 20 6f 6e 20 74 68 69 73 20 69 6d 70 6c  nds on this impl
e550: 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 69 6e 67  ementation being
e560: 20 63 6f 72 72 65 63 74 2e 20 20 49 66 20 73 6f   correct.  If so
e570: 6d 65 74 68 69 6e 67 0a 67 6f 65 73 20 77 72 6f  mething.goes wro
e580: 6e 67 20 61 6e 64 20 74 77 6f 20 6f 72 20 6d 6f  ng and two or mo
e590: 72 65 20 70 72 6f 63 65 73 73 65 73 20 61 72 65  re processes are
e5a0: 20 61 62 6c 65 20 74 6f 20 77 72 69 74 65 20 74   able to write t
e5b0: 68 65 20 73 61 6d 65 0a 64 61 74 61 62 61 73 65  he same.database
e5c0: 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d   file at the sam
e5d0: 65 20 74 69 6d 65 2c 20 73 65 76 65 72 65 20 64  e time, severe d
e5e0: 61 6d 61 67 65 20 63 61 6e 20 72 65 73 75 6c 74  amage can result
e5f0: 2e 3c 2f 70 3e 0a 0a 3c 70 3e 57 65 20 68 61 76  .</p>..<p>We hav
e600: 65 20 72 65 63 65 69 76 65 64 20 72 65 70 6f 72  e received repor
e610: 74 73 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 61  ts of implementa
e620: 74 69 6f 6e 73 20 6f 66 20 62 6f 74 68 0a 57 69  tions of both.Wi
e630: 6e 64 6f 77 73 20 6e 65 74 77 6f 72 6b 20 66 69  ndows network fi
e640: 6c 65 73 79 73 74 65 6d 73 20 61 6e 64 20 4e 46  lesystems and NF
e650: 53 20 69 6e 20 77 68 69 63 68 20 6c 6f 63 6b 69  S in which locki
e660: 6e 67 20 77 61 73 0a 73 75 62 74 6c 79 20 62 72  ng was.subtly br
e670: 6f 6b 65 6e 2e 20 20 57 65 20 63 61 6e 20 6e 6f  oken.  We can no
e680: 74 20 76 65 72 69 66 79 20 74 68 65 73 65 20 72  t verify these r
e690: 65 70 6f 72 74 73 2c 20 62 75 74 20 61 73 0a 6c  eports, but as.l
e6a0: 6f 63 6b 69 6e 67 20 69 73 20 64 69 66 66 69 63  ocking is diffic
e6b0: 75 6c 74 20 74 6f 20 67 65 74 20 72 69 67 68 74  ult to get right
e6c0: 20 6f 6e 20 61 20 6e 65 74 77 6f 72 6b 20 66 69   on a network fi
e6d0: 6c 65 73 79 73 74 65 6d 0a 77 65 20 68 61 76 65  lesystem.we have
e6e0: 20 6e 6f 20 72 65 61 73 6f 6e 20 74 6f 20 64 6f   no reason to do
e6f0: 75 62 74 20 74 68 65 6d 2e 20 20 59 6f 75 20 61  ubt them.  You a
e700: 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 0a 61  re advised to .a
e710: 76 6f 69 64 20 75 73 69 6e 67 20 53 51 4c 69 74  void using SQLit
e720: 65 20 6f 6e 20 61 20 6e 65 74 77 6f 72 6b 20 66  e on a network f
e730: 69 6c 65 73 79 73 74 65 6d 20 69 6e 20 74 68 65  ilesystem in the
e740: 20 66 69 72 73 74 20 70 6c 61 63 65 2c 0a 73 69   first place,.si
e750: 6e 63 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  nce performance 
e760: 77 69 6c 6c 20 62 65 20 73 6c 6f 77 2e 20 20 42  will be slow.  B
e770: 75 74 20 69 66 20 79 6f 75 20 6d 75 73 74 20 75  ut if you must u
e780: 73 65 20 61 20 0a 6e 65 74 77 6f 72 6b 20 66 69  se a .network fi
e790: 6c 65 73 79 73 74 65 6d 20 74 6f 20 73 74 6f 72  lesystem to stor
e7a0: 65 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  e SQLite databas
e7b0: 65 20 66 69 6c 65 73 2c 20 63 6f 6e 73 69 64 65  e files, conside
e7c0: 72 0a 75 73 69 6e 67 20 61 20 73 65 63 6f 6e 64  r.using a second
e7d0: 61 72 79 20 6c 6f 63 6b 69 6e 67 20 6d 65 63 68  ary locking mech
e7e0: 61 6e 69 73 6d 20 74 6f 20 70 72 65 76 65 6e 74  anism to prevent
e7f0: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 0a 77 72   simultaneous.wr
e800: 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ites to the same
e810: 20 64 61 74 61 62 61 73 65 20 65 76 65 6e 20 69   database even i
e820: 66 20 74 68 65 20 6e 61 74 69 76 65 20 66 69 6c  f the native fil
e830: 65 73 79 73 74 65 6d 0a 6c 6f 63 6b 69 6e 67 20  esystem.locking 
e840: 6d 65 63 68 61 6e 69 73 6d 20 6d 61 6c 66 75 6e  mechanism malfun
e850: 63 74 69 6f 6e 73 2e 3c 2f 70 3e 0a 0a 3c 70 3e  ctions.</p>..<p>
e860: 54 68 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  The versions of 
e870: 53 51 4c 69 74 65 20 74 68 61 74 20 63 6f 6d 65  SQLite that come
e880: 20 70 72 65 69 6e 73 74 61 6c 6c 65 64 20 6f 6e   preinstalled on
e890: 20 41 70 70 6c 65 0a 4d 61 63 20 4f 53 20 58 20   Apple.Mac OS X 
e8a0: 63 6f 6d 70 75 74 65 72 73 20 63 6f 6e 74 61 69  computers contai
e8b0: 6e 20 61 20 76 65 72 73 69 6f 6e 20 6f 66 20 53  n a version of S
e8c0: 51 4c 69 74 65 20 74 68 61 74 20 68 61 73 20 62  QLite that has b
e8d0: 65 65 6e 0a 65 78 74 65 6e 64 65 64 20 74 6f 20  een.extended to 
e8e0: 75 73 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20  use alternative 
e8f0: 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 69  locking strategi
e900: 65 73 20 74 68 61 74 20 77 6f 72 6b 20 6f 6e 0a  es that work on.
e910: 61 6c 6c 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65  all network file
e920: 73 79 73 74 65 6d 73 20 74 68 61 74 20 41 70 70  systems that App
e930: 6c 65 20 73 75 70 70 6f 72 74 73 2e 20 20 54 68  le supports.  Th
e940: 65 73 65 20 65 78 74 65 6e 73 69 6f 6e 73 0a 75  ese extensions.u
e950: 73 65 64 20 62 79 20 41 70 70 6c 65 20 77 6f 72  sed by Apple wor
e960: 6b 20 67 72 65 61 74 20 61 73 20 6c 6f 6e 67 20  k great as long 
e970: 61 73 20 61 6c 6c 20 70 72 6f 63 65 73 73 65 73  as all processes
e980: 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 0a 74   are accessing.t
e990: 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
e9a0: 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79   in the same way
e9b0: 2e 20 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79  .  Unfortunately
e9c0: 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 6d 65  , the locking.me
e9d0: 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20  chanisms do not 
e9e0: 65 78 63 6c 75 64 65 20 6f 6e 65 20 61 6e 6f 74  exclude one anot
e9f0: 68 65 72 2c 20 73 6f 20 69 66 20 6f 6e 65 20 70  her, so if one p
ea00: 72 6f 63 65 73 73 20 69 73 0a 61 63 63 65 73 73  rocess is.access
ea10: 69 6e 67 20 61 20 66 69 6c 65 20 75 73 69 6e 67  ing a file using
ea20: 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 41   (for example) A
ea30: 46 50 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 61  FP locking and a
ea40: 6e 6f 74 68 65 72 0a 70 72 6f 63 65 73 73 20 28  nother.process (
ea50: 70 65 72 68 61 70 73 20 6f 6e 20 61 20 64 69 66  perhaps on a dif
ea60: 66 65 72 65 6e 74 20 6d 61 63 68 69 6e 65 29 20  ferent machine) 
ea70: 69 73 20 75 73 69 6e 67 20 64 6f 74 2d 66 69 6c  is using dot-fil
ea80: 65 20 6c 6f 63 6b 73 2c 0a 74 68 65 20 74 77 6f  e locks,.the two
ea90: 20 70 72 6f 63 65 73 73 65 73 20 6d 69 67 68 74   processes might
eaa0: 20 63 6f 6c 6c 69 64 65 20 62 65 63 61 75 73 65   collide because
eab0: 20 41 46 50 20 6c 6f 63 6b 73 20 64 6f 20 6e 6f   AFP locks do no
eac0: 74 20 65 78 63 6c 75 64 65 0a 64 6f 74 2d 66 69  t exclude.dot-fi
ead0: 6c 65 20 6c 6f 63 6b 73 20 6f 72 20 76 69 63 65  le locks or vice
eae0: 20 76 65 72 73 61 2e 3c 2f 70 3e 0a 0a 3c 74 63   versa.</p>..<tc
eaf0: 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 66 73  l>hd_fragment fs
eb00: 79 6e 63 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 39 2e  ync</tcl>.<h3>9.
eb10: 32 20 49 6e 63 6f 6d 70 6c 65 74 65 20 44 69 73  2 Incomplete Dis
eb20: 6b 20 46 6c 75 73 68 65 73 3c 2f 68 33 3e 0a 0a  k Flushes</h3>..
eb30: 3c 70 3e 53 51 4c 69 74 65 20 75 73 65 73 20 74  <p>SQLite uses t
eb40: 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65  he fsync() syste
eb50: 6d 20 63 61 6c 6c 20 6f 6e 20 55 6e 69 78 20 61  m call on Unix a
eb60: 6e 64 20 74 68 65 20 46 6c 75 73 68 46 69 6c 65  nd the FlushFile
eb70: 42 75 66 66 65 72 73 28 29 0a 73 79 73 74 65 6d  Buffers().system
eb80: 20 63 61 6c 6c 20 6f 6e 20 77 33 32 20 69 6e 20   call on w32 in 
eb90: 6f 72 64 65 72 20 74 6f 20 73 79 6e 63 20 74 68  order to sync th
eba0: 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20 62 75  e file system bu
ebb0: 66 66 65 72 73 20 6f 6e 74 6f 20 64 69 73 6b 0a  ffers onto disk.
ebc0: 6f 78 69 64 65 20 61 73 20 73 68 6f 77 6e 20 69  oxide as shown i
ebd0: 6e 20 20 3c 61 20 68 72 65 66 3d 22 23 73 65 63  n  <a href="#sec
ebe0: 74 69 6f 6e 5f 33 5f 37 22 3e 73 74 65 70 20 33  tion_3_7">step 3
ebf0: 2e 37 3c 2f 61 3e 20 61 6e 64 0a 3c 61 20 68 72  .7</a> and.<a hr
ec00: 65 66 3d 22 23 73 65 63 74 69 6f 6e 5f 33 5f 31  ef="#section_3_1
ec10: 30 22 3e 73 74 65 70 20 33 2e 31 30 3c 2f 61 3e  0">step 3.10</a>
ec20: 2e 20 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79  .  Unfortunately
ec30: 2c 20 77 65 20 68 61 76 65 20 72 65 63 65 69 76  , we have receiv
ec40: 65 64 0a 72 65 70 6f 72 74 73 20 74 68 61 74 20  ed.reports that 
ec50: 6e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65  neither of these
ec60: 20 69 6e 74 65 72 66 61 63 65 73 20 77 6f 72 6b   interfaces work
ec70: 73 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20  s as advertised 
ec80: 6f 6e 20 6d 61 6e 79 0a 73 79 73 74 65 6d 73 2e  on many.systems.
ec90: 20 20 57 65 20 68 65 61 72 20 74 68 61 74 20 46    We hear that F
eca0: 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 73 28  lushFileBuffers(
ecb0: 29 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74  ) can be complet
ecc0: 65 6c 79 20 64 69 73 61 62 6c 65 64 0a 75 73 69  ely disabled.usi
ecd0: 6e 67 20 72 65 67 69 73 74 72 79 20 73 65 74 74  ng registry sett
ece0: 69 6e 67 73 20 6f 6e 20 73 6f 6d 65 20 57 69 6e  ings on some Win
ecf0: 64 6f 77 73 20 76 65 72 73 69 6f 6e 73 2e 20 20  dows versions.  
ed00: 53 6f 6d 65 20 68 69 73 74 6f 72 69 63 61 6c 0a  Some historical.
ed10: 76 65 72 73 69 6f 6e 73 20 6f 66 20 4c 69 6e 75  versions of Linu
ed20: 78 20 63 6f 6e 74 61 69 6e 20 76 65 72 73 69 6f  x contain versio
ed30: 6e 73 20 6f 66 20 66 73 79 6e 63 28 29 20 77 68  ns of fsync() wh
ed40: 69 63 68 20 61 72 65 20 6e 6f 2d 6f 70 73 20 6f  ich are no-ops o
ed50: 6e 0a 73 6f 6d 65 20 66 69 6c 65 73 79 73 74 65  n.some filesyste
ed60: 6d 73 2c 20 77 65 20 61 72 65 20 74 6f 6c 64 2e  ms, we are told.
ed70: 20 20 45 76 65 6e 20 6f 6e 20 73 79 73 74 65 6d    Even on system
ed80: 73 20 77 68 65 72 65 20 0a 46 6c 75 73 68 46 69  s where .FlushFi
ed90: 6c 65 42 75 66 66 65 72 73 28 29 20 61 6e 64 20  leBuffers() and 
eda0: 66 73 79 6e 63 28 29 20 61 72 65 20 73 61 69 64  fsync() are said
edb0: 20 74 6f 20 62 65 20 77 6f 72 6b 69 6e 67 2c 20   to be working, 
edc0: 6f 66 74 65 6e 0a 74 68 65 20 49 44 45 20 64 69  often.the IDE di
edd0: 73 6b 20 63 6f 6e 74 72 6f 6c 20 6c 69 65 73 20  sk control lies 
ede0: 61 6e 64 20 73 61 79 73 20 74 68 61 74 20 64 61  and says that da
edf0: 74 61 20 68 61 73 20 72 65 61 63 68 65 64 20 6f  ta has reached o
ee00: 78 69 64 65 0a 77 68 69 6c 65 20 69 74 20 69 73  xide.while it is
ee10: 20 73 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 6c 79   still held only
ee20: 20 69 6e 20 74 68 65 20 76 6f 6c 61 74 69 6c 65   in the volatile
ee30: 20 63 6f 6e 74 72 6f 6c 20 63 61 63 68 65 2e 3c   control cache.<
ee40: 2f 70 3e 0a 0a 3c 70 3e 4f 6e 20 74 68 65 20 4d  /p>..<p>On the M
ee50: 61 63 2c 20 79 6f 75 20 63 61 6e 20 73 65 74 20  ac, you can set 
ee60: 74 68 69 73 20 70 72 61 67 6d 61 3a 3c 2f 70 3e  this pragma:</p>
ee70: 0a 0a 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 50  ..<blockquote>.P
ee80: 52 41 47 4d 41 20 66 75 6c 6c 66 73 79 6e 63 3d  RAGMA fullfsync=
ee90: 4f 4e 3b 0a 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  ON;.</blockquote
eea0: 3e 0a 0a 3c 70 3e 53 65 74 74 69 6e 67 20 66 75  >..<p>Setting fu
eeb0: 6c 6c 66 73 79 6e 63 20 6f 6e 20 61 20 4d 61 63  llfsync on a Mac
eec0: 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65 20   will guarantee 
eed0: 74 68 61 74 20 64 61 74 61 20 72 65 61 6c 6c 79  that data really
eee0: 20 64 6f 65 73 0a 67 65 74 20 70 75 73 68 65 64   does.get pushed
eef0: 20 6f 75 74 20 74 6f 20 74 68 65 20 64 69 73 6b   out to the disk
ef00: 20 70 6c 61 74 74 65 72 20 6f 6e 20 61 20 66 6c   platter on a fl
ef10: 75 73 68 2e 20 20 42 75 74 20 74 68 65 20 69 6d  ush.  But the im
ef20: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 6f 66 20  plementation.of 
ef30: 66 75 6c 6c 66 73 79 6e 63 20 69 6e 76 6f 6c 76  fullfsync involv
ef40: 65 73 20 72 65 73 65 74 74 69 6e 67 20 74 68 65  es resetting the
ef50: 20 64 69 73 6b 20 63 6f 6e 74 72 6f 6c 6c 65 72   disk controller
ef60: 2e 20 20 41 6e 64 20 73 6f 20 6e 6f 74 20 6f 6e  .  And so not on
ef70: 6c 79 0a 69 73 20 69 74 20 70 72 6f 66 6f 75 6e  ly.is it profoun
ef80: 64 6c 79 20 73 6c 6f 77 2c 20 69 74 20 61 6c 73  dly slow, it als
ef90: 6f 20 73 6c 6f 77 73 20 64 6f 77 6e 20 6f 74 68  o slows down oth
efa0: 65 72 20 75 6e 72 65 6c 61 74 65 64 20 64 69 73  er unrelated dis
efb0: 6b 20 49 2f 4f 2e 0a 53 6f 20 69 74 73 20 75 73  k I/O..So its us
efc0: 65 20 69 73 20 6e 6f 74 20 72 65 63 6f 6d 6d 65  e is not recomme
efd0: 6e 64 65 64 2e 3c 2f 70 3e 0a 0a 3c 74 63 6c 3e  nded.</p>..<tcl>
efe0: 68 64 5f 66 72 61 67 6d 65 6e 74 20 66 69 6c 65  hd_fragment file
eff0: 64 65 6c 3c 2f 74 63 6c 3e 0a 3c 68 33 3e 39 2e  del</tcl>.<h3>9.
f000: 33 20 50 61 72 74 69 61 6c 20 46 69 6c 65 20 44  3 Partial File D
f010: 65 6c 65 74 69 6f 6e 73 3c 2f 68 33 3e 0a 0a 3c  eletions</h3>..<
f020: 70 3e 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73  p>SQLite assumes
f030: 20 74 68 61 74 20 66 69 6c 65 20 64 65 6c 65 74   that file delet
f040: 69 6f 6e 20 69 73 20 61 6e 20 61 74 6f 6d 69 63  ion is an atomic
f050: 20 6f 70 65 72 61 74 69 6f 6e 20 66 72 6f 6d 20   operation from 
f060: 74 68 65 0a 70 6f 69 6e 74 20 6f 66 20 76 69 65  the.point of vie
f070: 77 20 6f 66 20 61 20 75 73 65 72 20 70 72 6f 63  w of a user proc
f080: 65 73 73 2e 20 20 49 66 20 70 6f 77 65 72 20 66  ess.  If power f
f090: 61 69 6c 73 20 69 6e 20 74 68 65 20 6d 69 64 64  ails in the midd
f0a0: 6c 65 20 6f 66 0a 61 20 66 69 6c 65 20 64 65 6c  le of.a file del
f0b0: 65 74 69 6f 6e 2c 20 74 68 65 6e 20 61 66 74 65  etion, then afte
f0c0: 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f  r power is resto
f0d0: 72 65 64 20 53 51 4c 69 74 65 20 65 78 70 65 63  red SQLite expec
f0e0: 74 73 20 74 6f 20 73 65 65 0a 65 69 74 68 65 72  ts to see.either
f0f0: 20 74 68 65 20 65 6e 74 69 72 65 20 66 69 6c 65   the entire file
f100: 20 77 69 74 68 20 61 6c 6c 20 6f 66 20 69 74 73   with all of its
f110: 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 20 69   original data i
f120: 6e 74 61 63 74 2c 20 6f 72 20 69 74 0a 65 78 70  ntact, or it.exp
f130: 65 63 74 73 20 6e 6f 74 20 74 6f 20 66 69 6e 64  ects not to find
f140: 20 74 68 65 20 66 69 6c 65 20 61 74 20 61 6c 6c   the file at all
f150: 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20  .  Transactions 
f160: 6d 61 79 20 6e 6f 74 20 62 65 20 61 74 6f 6d 69  may not be atomi
f170: 63 0a 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61  c.on systems tha
f180: 74 20 64 6f 20 6e 6f 74 20 77 6f 72 6b 20 74 68  t do not work th
f190: 69 73 20 77 61 79 2e 3c 2f 70 3e 0a 0a 3c 74 63  is way.</p>..<tc
f1a0: 6c 3e 68 64 5f 66 72 61 67 6d 65 6e 74 20 66 69  l>hd_fragment fi
f1b0: 6c 65 67 61 72 62 61 67 65 3c 2f 74 63 6c 3e 0a  legarbage</tcl>.
f1c0: 3c 68 33 3e 39 2e 34 20 47 61 72 62 61 67 65 20  <h3>9.4 Garbage 
f1d0: 57 72 69 74 74 65 6e 20 49 6e 74 6f 20 46 69 6c  Written Into Fil
f1e0: 65 73 3c 2f 68 33 3e 0a 0a 3c 70 3e 53 51 4c 69  es</h3>..<p>SQLi
f1f0: 74 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  te database file
f200: 73 20 61 72 65 20 6f 72 64 69 6e 61 72 79 20 64  s are ordinary d
f210: 69 73 6b 20 66 69 6c 65 73 20 74 68 61 74 20 63  isk files that c
f220: 61 6e 20 62 65 0a 6f 70 65 6e 65 64 20 61 6e 64  an be.opened and
f230: 20 77 72 69 74 74 65 6e 20 62 79 20 6f 72 64 69   written by ordi
f240: 6e 61 72 79 20 75 73 65 72 20 70 72 6f 63 65 73  nary user proces
f250: 73 65 73 2e 20 20 41 20 72 6f 67 75 65 20 70 72  ses.  A rogue pr
f260: 6f 63 65 73 73 0a 63 61 6e 20 6f 70 65 6e 20 61  ocess.can open a
f270: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
f280: 65 20 61 6e 64 20 66 69 6c 6c 20 69 74 20 77 69  e and fill it wi
f290: 74 68 20 63 6f 72 72 75 70 74 20 64 61 74 61 2e  th corrupt data.
f2a0: 20 20 0a 43 6f 72 72 75 70 74 20 64 61 74 61 20    .Corrupt data 
f2b0: 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 69 6e  might also be in
f2c0: 74 72 6f 64 75 63 65 64 20 69 6e 74 6f 20 61 6e  troduced into an
f2d0: 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
f2e0: 0a 62 79 20 62 75 67 73 20 69 6e 20 74 68 65 20  .by bugs in the 
f2f0: 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
f300: 20 6f 72 20 64 69 73 6b 20 63 6f 6e 74 72 6f 6c   or disk control
f310: 6c 65 72 3b 20 65 73 70 65 63 69 61 6c 6c 79 0a  ler; especially.
f320: 62 75 67 73 20 74 72 69 67 67 65 72 65 64 20 62  bugs triggered b
f330: 79 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  y a power failur
f340: 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 74  e.  There is not
f350: 68 69 6e 67 20 53 51 4c 69 74 65 20 63 61 6e 0a  hing SQLite can.
f360: 64 6f 20 74 6f 20 64 65 66 65 6e 64 20 61 67 61  do to defend aga
f370: 69 6e 73 74 20 74 68 65 73 65 20 6b 69 6e 64 73  inst these kinds
f380: 20 6f 66 20 70 72 6f 62 6c 65 6d 73 2e 3c 2f 70   of problems.</p
f390: 3e 0a 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d  >..<tcl>hd_fragm
f3a0: 65 6e 74 20 6d 76 68 6f 74 6a 72 6e 6c 3c 2f 74  ent mvhotjrnl</t
f3b0: 63 6c 3e 0a 3c 68 33 3e 39 2e 35 20 44 65 6c 65  cl>.<h3>9.5 Dele
f3c0: 74 69 6e 67 20 4f 72 20 52 65 6e 61 6d 69 6e 67  ting Or Renaming
f3d0: 20 41 20 48 6f 74 20 4a 6f 75 72 6e 61 6c 3c 2f   A Hot Journal</
f3e0: 68 33 3e 0a 0a 3c 70 3e 49 66 20 61 20 63 72 61  h3>..<p>If a cra
f3f0: 73 68 20 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73  sh or power loss
f400: 20 64 6f 65 73 20 6f 63 63 75 72 20 61 6e 64 20   does occur and 
f410: 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73  a hot journal is
f420: 20 6c 65 66 74 20 6f 6e 0a 74 68 65 20 64 69 73   left on.the dis
f430: 6b 2c 20 69 74 20 69 73 20 65 73 73 65 6e 74 69  k, it is essenti
f440: 61 6c 20 74 68 61 74 20 74 68 65 20 6f 72 69 67  al that the orig
f450: 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  inal database fi
f460: 6c 65 20 61 6e 64 20 74 68 65 20 68 6f 74 0a 6a  le and the hot.j
f470: 6f 75 72 6e 61 6c 20 72 65 6d 61 69 6e 20 6f 6e  ournal remain on
f480: 20 64 69 73 6b 20 77 69 74 68 20 74 68 65 69 72   disk with their
f490: 20 6f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 73 20   original names 
f4a0: 75 6e 74 69 6c 20 74 68 65 20 64 61 74 61 62 61  until the databa
f4b0: 73 65 0a 66 69 6c 65 20 69 73 20 6f 70 65 6e 65  se.file is opene
f4c0: 64 20 62 79 20 61 6e 6f 74 68 65 72 20 53 51 4c  d by another SQL
f4d0: 69 74 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20  ite process and 
f4e0: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 0a 44  rolled back.  .D
f4f0: 75 72 69 6e 67 20 72 65 63 6f 76 65 72 79 20 61  uring recovery a
f500: 74 20 3c 61 20 68 72 65 66 3d 22 23 73 65 63 74  t <a href="#sect
f510: 69 6f 6e 5f 34 5f 32 22 3e 73 74 65 70 20 34 2e  ion_4_2">step 4.
f520: 32 3c 2f 61 3e 20 53 51 4c 69 74 65 20 6c 6f 63  2</a> SQLite loc
f530: 61 74 65 73 0a 74 68 65 20 68 6f 74 20 6a 6f 75  ates.the hot jou
f540: 72 6e 61 6c 20 62 79 20 6c 6f 6f 6b 69 6e 67 20  rnal by looking 
f550: 66 6f 72 20 61 20 66 69 6c 65 20 69 6e 20 74 68  for a file in th
f560: 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f 72 79  e same directory
f570: 20 61 73 20 74 68 65 0a 64 61 74 61 62 61 73 65   as the.database
f580: 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 61 6e   being opened an
f590: 64 20 77 68 6f 73 65 20 6e 61 6d 65 20 69 73 20  d whose name is 
f5a0: 64 65 72 69 76 65 64 20 66 72 6f 6d 20 74 68 65  derived from the
f5b0: 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 66 69 6c   name of the.fil
f5c0: 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20  e being opened. 
f5d0: 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 6f   If either the o
f5e0: 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
f5f0: 20 66 69 6c 65 20 6f 72 20 74 68 65 0a 68 6f 74   file or the.hot
f600: 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 62 65   journal have be
f610: 65 6e 20 6d 6f 76 65 64 20 6f 72 20 72 65 6e 61  en moved or rena
f620: 6d 65 64 2c 20 74 68 65 6e 20 74 68 65 20 68 6f  med, then the ho
f630: 74 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 0a 6e  t journal will.n
f640: 6f 74 20 62 65 20 73 65 65 6e 20 61 6e 64 20 74  ot be seen and t
f650: 68 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  he database will
f660: 20 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62   not be rolled b
f670: 61 63 6b 2e 3c 2f 70 3e 0a 0a 3c 70 3e 57 65 20  ack.</p>..<p>We 
f680: 73 75 73 70 65 63 74 20 74 68 61 74 20 61 20 63  suspect that a c
f690: 6f 6d 6d 6f 6e 20 66 61 69 6c 75 72 65 20 6d 6f  ommon failure mo
f6a0: 64 65 20 66 6f 72 20 53 51 4c 69 74 65 20 72 65  de for SQLite re
f6b0: 63 6f 76 65 72 79 20 68 61 70 70 65 6e 73 0a 6c  covery happens.l
f6c0: 69 6b 65 20 74 68 69 73 3a 20 20 41 20 70 6f 77  ike this:  A pow
f6d0: 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  er failure occur
f6e0: 73 2e 20 20 41 66 74 65 72 20 70 6f 77 65 72 20  s.  After power 
f6f0: 69 73 20 72 65 73 74 6f 72 65 64 2c 20 61 20 77  is restored, a w
f700: 65 6c 6c 2d 6d 65 61 6e 69 6e 67 0a 75 73 65 72  ell-meaning.user
f710: 20 6f 72 20 73 79 73 74 65 6d 20 61 64 6d 69 6e   or system admin
f720: 69 73 74 72 61 74 6f 72 20 62 65 67 69 6e 73 20  istrator begins 
f730: 6c 6f 6f 6b 69 6e 67 20 61 72 6f 75 6e 64 20 6f  looking around o
f740: 6e 20 74 68 65 20 64 69 73 6b 20 66 6f 72 0a 64  n the disk for.d
f750: 61 6d 61 67 65 2e 20 20 54 68 65 79 20 73 65 65  amage.  They see
f760: 20 74 68 65 69 72 20 64 61 74 61 62 61 73 65 20   their database 
f770: 66 69 6c 65 20 6e 61 6d 65 64 20 22 69 6d 70 6f  file named "impo
f780: 72 74 61 6e 74 2e 64 61 74 61 22 2e 20 20 54 68  rtant.data".  Th
f790: 69 73 20 66 69 6c 65 0a 69 73 20 70 65 72 68 61  is file.is perha
f7a0: 70 73 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 74  ps familiar to t
f7b0: 68 65 6d 2e 20 20 42 75 74 20 61 66 74 65 72 20  hem.  But after 
f7c0: 74 68 65 20 63 72 61 73 68 2c 20 74 68 65 72 65  the crash, there
f7d0: 20 69 73 20 61 6c 73 6f 20 61 0a 68 6f 74 20 6a   is also a.hot j
f7e0: 6f 75 72 6e 61 6c 20 6e 61 6d 65 64 20 22 69 6d  ournal named "im
f7f0: 70 6f 72 74 61 6e 74 2e 64 61 74 61 2d 6a 6f 75  portant.data-jou
f800: 72 6e 61 6c 22 2e 20 20 54 68 65 20 75 73 65 72  rnal".  The user
f810: 20 74 68 65 6e 20 64 65 6c 65 74 65 73 0a 74 68   then deletes.th
f820: 65 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 74  e hot journal, t
f830: 68 69 6e 6b 69 6e 67 20 74 68 61 74 20 74 68 65  hinking that the
f840: 79 20 61 72 65 20 68 65 6c 70 69 6e 67 20 74 6f  y are helping to
f850: 20 63 6c 65 61 6e 75 70 20 74 68 65 20 73 79 73   cleanup the sys
f860: 74 65 6d 2e 0a 57 65 20 6b 6e 6f 77 20 6f 66 20  tem..We know of 
f870: 6e 6f 20 77 61 79 20 74 6f 20 70 72 65 76 65 6e  no way to preven
f880: 74 20 74 68 69 73 20 6f 74 68 65 72 20 74 68 61  t this other tha
f890: 6e 20 75 73 65 72 20 65 64 75 63 61 74 69 6f 6e  n user education
f8a0: 2e 3c 2f 70 3e 0a 0a 3c 70 3e 49 66 20 74 68 65  .</p>..<p>If the
f8b0: 72 65 20 61 72 65 20 6d 75 6c 74 69 70 6c 65 20  re are multiple 
f8c0: 28 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69  (hard or symboli
f8d0: 63 29 20 6c 69 6e 6b 73 20 74 6f 20 61 20 64 61  c) links to a da
f8e0: 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 74 68 65  tabase file,.the
f8f0: 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 62 65   journal will be
f900: 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
f910: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6c  he name of the l
f920: 69 6e 6b 20 74 68 72 6f 75 67 68 20 77 68 69 63  ink through whic
f930: 68 0a 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f  h.the file was o
f940: 70 65 6e 65 64 2e 20 20 49 66 20 61 20 63 72 61  pened.  If a cra
f950: 73 68 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68  sh occurs and th
f960: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  e database is op
f970: 65 6e 65 64 20 61 67 61 69 6e 0a 75 73 69 6e 67  ened again.using
f980: 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c 69 6e   a different lin
f990: 6b 2c 20 74 68 65 20 68 6f 74 20 6a 6f 75 72 6e  k, the hot journ
f9a0: 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c  al will not be l
f9b0: 6f 63 61 74 65 64 20 61 6e 64 20 6e 6f 0a 72 6f  ocated and no.ro
f9c0: 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6f 63 63 75  llback will occu
f9d0: 72 2e 3c 2f 70 3e 0a 0a 3c 70 3e 53 6f 6d 65 74  r.</p>..<p>Somet
f9e0: 69 6d 65 73 20 61 20 70 6f 77 65 72 20 66 61 69  imes a power fai
f9f0: 6c 75 72 65 20 77 69 6c 6c 20 63 61 75 73 65 20  lure will cause 
fa00: 61 20 66 69 6c 65 73 79 73 74 65 6d 20 74 6f 20  a filesystem to 
fa10: 62 65 20 63 6f 72 72 75 70 74 65 64 0a 73 75 63  be corrupted.suc
fa20: 68 20 74 68 61 74 20 72 65 63 65 6e 74 6c 79 20  h that recently 
fa30: 63 68 61 6e 67 65 64 20 66 69 6c 65 6e 61 6d 65  changed filename
fa40: 73 20 61 72 65 20 66 6f 72 67 6f 74 74 65 6e 20  s are forgotten 
fa50: 61 6e 64 20 74 68 65 20 66 69 6c 65 20 69 73 0a  and the file is.
fa60: 6d 6f 76 65 64 20 69 6e 74 6f 20 61 20 22 2f 6c  moved into a "/l
fa70: 6f 73 74 2b 66 6f 75 6e 64 22 20 64 69 72 65 63  ost+found" direc
fa80: 74 6f 72 79 2e 20 20 57 68 65 6e 20 74 68 61 74  tory.  When that
fa90: 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 68 6f   happens, the ho
faa0: 74 0a 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e  t.journal will n
fab0: 6f 74 20 62 65 20 66 6f 75 6e 64 20 61 6e 64 20  ot be found and 
fac0: 72 65 63 6f 76 65 72 79 20 77 69 6c 6c 20 6e 6f  recovery will no
fad0: 74 20 6f 63 63 75 72 2e 0a 53 51 4c 69 74 65 20  t occur..SQLite 
fae0: 74 72 69 65 73 20 74 6f 20 70 72 65 76 65 6e 74  tries to prevent
faf0: 20 74 68 69 73 0a 62 79 20 6f 70 65 6e 69 6e 67   this.by opening
fb00: 20 61 6e 64 20 73 79 6e 63 69 6e 67 20 74 68 65   and syncing the
fb10: 20 64 69 72 65 63 74 6f 72 79 20 63 6f 6e 74 61   directory conta
fb20: 69 6e 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61  ining the rollba
fb30: 63 6b 20 6a 6f 75 72 6e 61 6c 0a 61 74 20 74 68  ck journal.at th
fb40: 65 20 73 61 6d 65 20 74 69 6d 65 20 69 74 20 73  e same time it s
fb50: 79 6e 63 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c  yncs the journal
fb60: 20 66 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 48   file itself.  H
fb70: 6f 77 65 76 65 72 2c 20 74 68 65 0a 6d 6f 76 65  owever, the.move
fb80: 6d 65 6e 74 20 6f 66 20 66 69 6c 65 73 20 69 6e  ment of files in
fb90: 74 6f 20 2f 6c 6f 73 74 2b 66 6f 75 6e 64 20 63  to /lost+found c
fba0: 61 6e 20 62 65 20 63 61 75 73 65 64 20 62 79 20  an be caused by 
fbb0: 75 6e 72 65 6c 61 74 65 64 20 70 72 6f 63 65 73  unrelated proces
fbc0: 73 65 73 0a 63 72 65 61 74 69 6e 67 20 75 6e 72  ses.creating unr
fbd0: 65 6c 61 74 65 64 20 66 69 6c 65 73 20 69 6e 20  elated files in 
fbe0: 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 6f  the same directo
fbf0: 72 79 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64  ry as the main d
fc00: 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 41 6e  atabase file..An
fc10: 64 20 73 69 6e 63 65 20 74 68 69 73 20 69 73 20  d since this is 
fc20: 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74  out from under t
fc30: 68 65 20 63 6f 6e 74 72 6f 6c 20 6f 66 20 53 51  he control of SQ
fc40: 4c 69 74 65 2c 20 74 68 65 72 65 20 69 73 20 6e  Lite, there is n
fc50: 6f 74 68 69 6e 67 0a 74 68 61 74 20 53 51 4c 69  othing.that SQLi
fc60: 74 65 20 63 61 6e 20 64 6f 20 74 6f 20 70 72 65  te can do to pre
fc70: 76 65 6e 74 20 69 74 2e 20 20 49 66 20 79 6f 75  vent it.  If you
fc80: 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20   are running on 
fc90: 61 20 73 79 73 74 65 6d 20 74 68 61 74 0a 69 73  a system that.is
fca0: 20 76 75 6c 6e 65 72 61 62 6c 65 20 74 6f 20 74   vulnerable to t
fcb0: 68 69 73 20 6b 69 6e 64 20 6f 66 20 66 69 6c 65  his kind of file
fcc0: 73 79 73 74 65 6d 20 6e 61 6d 65 73 70 61 63 65  system namespace
fcd0: 20 63 6f 72 72 75 70 74 69 6f 6e 20 28 6d 6f 73   corruption (mos
fce0: 74 0a 6d 6f 64 65 72 6e 20 6a 6f 75 72 6e 61 6c  t.modern journal
fcf0: 6c 69 6e 67 20 66 69 6c 65 73 79 73 74 65 6d 73  ling filesystems
fd00: 20 61 72 65 20 69 6d 6d 75 6e 65 2c 20 77 65 20   are immune, we 
fd10: 62 65 6c 69 65 76 65 29 20 74 68 65 6e 20 79 6f  believe) then yo
fd20: 75 20 6d 69 67 68 74 0a 77 61 6e 74 20 74 6f 20  u might.want to 
fd30: 63 6f 6e 73 69 64 65 72 20 70 75 74 74 69 6e 67  consider putting
fd40: 20 65 61 63 68 20 53 51 4c 69 74 65 20 64 61 74   each SQLite dat
fd50: 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 69 74  abase file in it
fd60: 73 20 6f 77 6e 20 70 72 69 76 61 74 65 0a 73 75  s own private.su
fd70: 62 64 69 72 65 63 74 6f 72 79 2e 3c 2f 70 3e 0a  bdirectory.</p>.
fd80: 0a 3c 74 63 6c 3e 68 64 5f 66 72 61 67 6d 65 6e  .<tcl>hd_fragmen
fd90: 74 20 66 75 74 75 72 65 3c 2f 74 63 6c 3e 0a 3c  t future</tcl>.<
fda0: 68 32 3e 31 30 2e 30 20 46 75 74 75 72 65 20 44  h2>10.0 Future D
fdb0: 69 72 65 63 74 69 6f 6e 73 20 41 6e 64 20 43 6f  irections And Co
fdc0: 6e 63 6c 75 73 69 6f 6e 3c 2f 68 32 3e 0a 0a 3c  nclusion</h2>..<
fdd0: 70 3e 45 76 65 72 79 20 6e 6f 77 20 61 6e 64 20  p>Every now and 
fde0: 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 64 69 73  then someone dis
fdf0: 63 6f 76 65 72 73 20 61 20 6e 65 77 20 66 61 69  covers a new fai
fe00: 6c 75 72 65 20 6d 6f 64 65 20 66 6f 72 0a 74 68  lure mode for.th
fe10: 65 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20  e atomic commit 
fe20: 6d 65 63 68 61 6e 69 73 6d 20 69 6e 20 53 51 4c  mechanism in SQL
fe30: 69 74 65 20 61 6e 64 20 74 68 65 20 64 65 76 65  ite and the deve
fe40: 6c 6f 70 65 72 73 20 68 61 76 65 20 74 6f 0a 70  lopers have to.p
fe50: 75 74 20 69 6e 20 61 20 70 61 74 63 68 2e 20 20  ut in a patch.  
fe60: 54 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  This is happenin
fe70: 67 20 6c 65 73 73 20 61 6e 64 20 6c 65 73 73 20  g less and less 
fe80: 61 6e 64 20 74 68 65 0a 66 61 69 6c 75 72 65 20  and the.failure 
fe90: 6d 6f 64 65 73 20 61 72 65 20 62 65 63 6f 6d 69  modes are becomi
fea0: 6e 67 20 6d 6f 72 65 20 61 6e 64 20 6d 6f 72 65  ng more and more
feb0: 20 6f 62 73 63 75 72 65 2e 20 20 42 75 74 20 69   obscure.  But i
fec0: 74 20 77 6f 75 6c 64 0a 73 74 69 6c 6c 20 62 65  t would.still be
fed0: 20 66 6f 6f 6c 69 73 68 20 74 6f 20 73 75 70 70   foolish to supp
fee0: 6f 73 65 20 74 68 61 74 20 74 68 65 20 61 74 6f  ose that the ato
fef0: 6d 69 63 20 63 6f 6d 6d 69 74 20 6c 6f 67 69 63  mic commit logic
ff00: 20 6f 66 0a 53 51 4c 69 74 65 20 69 73 20 65 6e   of.SQLite is en
ff10: 74 69 72 65 6c 79 20 62 75 67 2d 66 72 65 65 2e  tirely bug-free.
ff20: 20 20 54 68 65 20 64 65 76 65 6c 6f 70 65 72 73    The developers
ff30: 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74   are committed t
ff40: 6f 20 66 69 78 69 6e 67 0a 74 68 65 73 65 20 62  o fixing.these b
ff50: 75 67 73 20 61 73 20 71 75 69 63 6b 6c 79 20 61  ugs as quickly a
ff60: 73 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20  s they might be 
ff70: 66 6f 75 6e 64 2e 3c 2f 70 3e 0a 0a 3c 70 3e 0a  found.</p>..<p>.
ff80: 54 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 61  The developers a
ff90: 72 65 20 61 6c 73 6f 20 6f 6e 20 74 68 65 20 6c  re also on the l
ffa0: 6f 6f 6b 6f 75 74 20 66 6f 72 20 6e 65 77 20 77  ookout for new w
ffb0: 61 79 73 20 74 6f 0a 6f 70 74 69 6d 69 7a 65 20  ays to.optimize 
ffc0: 74 68 65 20 63 6f 6d 6d 69 74 20 6d 65 63 68 61  the commit mecha
ffd0: 6e 69 73 6d 2e 20 20 54 68 65 20 63 75 72 72 65  nism.  The curre
ffe0: 6e 74 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  nt VFS implement
fff0: 61 74 69 6f 6e 73 0a 66 6f 72 20 55 6e 69 78 20  ations.for Unix 
10000 28 4c 69 6e 75 78 20 61 6e 64 20 4d 61 63 20 4f  (Linux and Mac O
10010 53 20 58 29 20 61 6e 64 20 57 69 6e 64 6f 77 73  S X) and Windows
10020 20 6d 61 6b 65 20 70 65 73 73 69 6d 69 73 74 69   make pessimisti
10030 63 20 61 73 73 75 6d 70 74 69 6f 6e 73 20 61 62  c assumptions ab
10040 6f 75 74 0a 74 68 65 20 62 65 68 61 76 69 6f 72  out.the behavior
10050 20 6f 66 20 74 68 6f 73 65 20 73 79 73 74 65 6d   of those system
10060 73 2e 20 20 41 66 74 65 72 20 63 6f 6e 73 75 6c  s.  After consul
10070 74 61 74 69 6f 6e 20 77 69 74 68 20 65 78 70 65  tation with expe
10080 72 74 73 0a 6f 6e 20 68 6f 77 20 74 68 65 73 65  rts.on how these
10090 20 73 79 73 74 65 6d 73 20 77 6f 72 6b 2c 20 77   systems work, w
100a0 65 20 6d 69 67 68 74 20 62 65 20 61 62 6c 65 20  e might be able 
100b0 74 6f 20 72 65 6c 61 78 20 73 6f 6d 65 20 6f 66  to relax some of
100c0 20 74 68 65 0a 61 73 73 75 6d 70 74 69 6f 6e 73   the.assumptions
100d0 20 6f 6e 20 74 68 65 73 65 20 73 79 73 74 65 6d   on these system
100e0 73 20 61 6e 64 20 61 6c 6c 6f 77 20 74 68 65 6d  s and allow them
100f0 20 74 6f 20 72 75 6e 20 66 61 73 74 65 72 2e 20   to run faster. 
10100 20 49 6e 0a 70 61 72 74 69 63 75 6c 61 72 2c 20   In.particular, 
10110 77 65 20 73 75 73 70 65 63 74 20 74 68 61 74 20  we suspect that 
10120 6d 6f 73 74 20 6d 6f 64 65 72 6e 20 66 69 6c 65  most modern file
10130 73 79 73 74 65 6d 73 20 65 78 68 69 62 69 74 20  systems exhibit 
10140 74 68 65 0a 73 61 66 65 20 61 70 70 65 6e 64 20  the.safe append 
10150 70 72 6f 70 65 72 74 79 20 61 6e 64 20 74 68 61  property and tha
10160 74 20 6d 61 6e 79 20 6f 66 20 74 68 65 6d 20 6d  t many of them m
10170 69 67 68 74 20 73 75 70 70 6f 72 74 20 61 74 6f  ight support ato
10180 6d 69 63 0a 73 65 63 74 6f 72 20 77 72 69 74 65  mic.sector write
10190 73 2e 20 20 42 75 74 20 75 6e 74 69 6c 20 74 68  s.  But until th
101a0 69 73 20 69 73 20 6b 6e 6f 77 6e 20 66 6f 72 20  is is known for 
101b0 63 65 72 74 61 69 6e 2c 20 53 51 4c 69 74 65 20  certain, SQLite 
101c0 77 69 6c 6c 0a 74 61 6b 65 20 74 68 65 20 63 6f  will.take the co
101d0 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 72 6f  nservative appro
101e0 61 63 68 20 61 6e 64 20 61 73 73 75 6d 65 20 74  ach and assume t
101f0 68 65 20 77 6f 72 73 74 2e 3c 2f 70 3e 0a        he worst.</p>.