Debian Codegraph @

www/atomiccommit.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Atomic Commit In SQLite</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Atomic Commit In SQLite
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#_introduction">1. Introduction</a></div>
129 <div class="fancy-toc1"><a href="#_hardware_assumptions">2. Hardware Assumptions</a></div>
130 <div class="fancy-toc1"><a href="#_single_file_commit">3. Single File Commit</a></div>
131 <div class="fancy-toc2"><a href="#_initial_state">3.1. Initial State</a></div>
132 <div class="fancy-toc2"><a href="#_acquiring_a_read_lock">3.2. Acquiring A Read Lock</a></div>
133 <div class="fancy-toc2"><a href="#_reading_information_out_of_the_database">3.3. Reading Information Out Of The Database</a></div>
134 <div class="fancy-toc2"><a href="#_obtaining_a_reserved_lock">3.4. Obtaining A Reserved Lock</a></div>
135 <div class="fancy-toc2"><a href="#_creating_a_rollback_journal_file">3.5. Creating A Rollback Journal File</a></div>
136 <div class="fancy-toc2"><a href="#_changing_database_pages_in_user_space">3.6. Changing Database Pages In User Space</a></div>
137 <div class="fancy-toc2"><a href="#_flushing_the_rollback_journal_file_to_mass_storage">3.7. Flushing The Rollback Journal File To Mass Storage</a></div>
138 <div class="fancy-toc2"><a href="#_obtaining_an_exclusive_lock">3.8. Obtaining An Exclusive Lock</a></div>
139 <div class="fancy-toc2"><a href="#_writing_changes_to_the_database_file">3.9. Writing Changes To The Database File</a></div>
140 <div class="fancy-toc2"><a href="#0_flushing_changes_to_mass_storage">3.10. 0 Flushing Changes To Mass Storage</a></div>
141 <div class="fancy-toc2"><a href="#1_deleting_the_rollback_journal">3.11. 1 Deleting The Rollback Journal</a></div>
142 <div class="fancy-toc2"><a href="#2_releasing_the_lock">3.12. 2 Releasing The Lock</a></div>
143 <div class="fancy-toc1"><a href="#_rollback">4. Rollback</a></div>
144 <div class="fancy-toc2"><a href="#_when_something_goes_wrong_">4.1. When Something Goes Wrong...</a></div>
145 <div class="fancy-toc2"><a href="#_hot_rollback_journals">4.2. Hot Rollback Journals</a></div>
146 <div class="fancy-toc2"><a href="#_obtaining_an_exclusive_lock_on_the_database">4.3. Obtaining An Exclusive Lock On The Database</a></div>
147 <div class="fancy-toc2"><a href="#_rolling_back_incomplete_changes">4.4. Rolling Back Incomplete Changes</a></div>
148 <div class="fancy-toc2"><a href="#_deleting_the_hot_journal">4.5. Deleting The Hot Journal</a></div>
149 <div class="fancy-toc2"><a href="#_continue_as_if_the_uncompleted_writes_had_never_happened">4.6. Continue As If The Uncompleted Writes Had Never Happened</a></div>
150 <div class="fancy-toc1"><a href="#_multi_file_commit">5. Multi-file Commit</a></div>
151 <div class="fancy-toc2"><a href="#_separate_rollback_journals_for_each_database">5.1. Separate Rollback Journals For Each Database</a></div>
152 <div class="fancy-toc2"><a href="#_the_super_journal_file">5.2. The Super-Journal File</a></div>
153 <div class="fancy-toc2"><a href="#_updating_rollback_journal_headers">5.3. Updating Rollback Journal Headers</a></div>
154 <div class="fancy-toc2"><a href="#_updating_the_database_files">5.4. Updating The Database Files</a></div>
155 <div class="fancy-toc2"><a href="#_delete_the_super_journal_file">5.5. Delete The Super-Journal File</a></div>
156 <div class="fancy-toc2"><a href="#_clean_up_the_rollback_journals">5.6. Clean Up The Rollback Journals</a></div>
157 <div class="fancy-toc1"><a href="#_additional_details_of_the_commit_process">6. Additional Details Of The Commit Process</a></div>
158 <div class="fancy-toc2"><a href="#_always_journal_complete_sectors">6.1. Always Journal Complete Sectors</a></div>
159 <div class="fancy-toc2"><a href="#_dealing_with_garbage_written_into_journal_files">6.2. Dealing With Garbage Written Into Journal Files</a></div>
160 <div class="fancy-toc2"><a href="#_cache_spill_prior_to_commit">6.3. Cache Spill Prior To Commit</a></div>
161 <div class="fancy-toc1"><a href="#_optimizations">7. Optimizations</a></div>
162 <div class="fancy-toc2"><a href="#_cache_retained_between_transactions">7.1. Cache Retained Between Transactions</a></div>
163 <div class="fancy-toc2"><a href="#_exclusive_access_mode">7.2. Exclusive Access Mode</a></div>
164 <div class="fancy-toc2"><a href="#_do_not_journal_freelist_pages">7.3. Do Not Journal Freelist Pages</a></div>
165 <div class="fancy-toc2"><a href="#_single_page_updates_and_atomic_sector_writes">7.4. Single Page Updates And Atomic Sector Writes</a></div>
166 <div class="fancy-toc2"><a href="#_filesystems_with_safe_append_semantics">7.5. Filesystems With Safe Append Semantics</a></div>
167 <div class="fancy-toc2"><a href="#_persistent_rollback_journals">7.6. Persistent Rollback Journals</a></div>
168 <div class="fancy-toc1"><a href="#_testing_atomic_commit_behavior">8. Testing Atomic Commit Behavior</a></div>
169 <div class="fancy-toc1"><a href="#_things_that_can_go_wrong">9. Things That Can Go Wrong</a></div>
170 <div class="fancy-toc2"><a href="#_broken_locking_implementations">9.1. Broken Locking Implementations</a></div>
171 <div class="fancy-toc2"><a href="#_incomplete_disk_flushes">9.2. Incomplete Disk Flushes</a></div>
172 <div class="fancy-toc2"><a href="#_partial_file_deletions">9.3. Partial File Deletions</a></div>
173 <div class="fancy-toc2"><a href="#_garbage_written_into_files">9.4. Garbage Written Into Files</a></div>
174 <div class="fancy-toc2"><a href="#_deleting_or_renaming_a_hot_journal">9.5. Deleting Or Renaming A Hot Journal</a></div>
175 <div class="fancy-toc1"><a href="#_future_directions_and_conclusion">10. Future Directions And Conclusion</a></div>
176 </div>
177 </div>
178 <script>
179 function toggle_toc(){
180 var sub = document.getElementById("toc_sub")
181 var mk = document.getElementById("toc_mk")
182 if( sub.style.display!="block" ){
183 sub.style.display = "block";
184 mk.innerHTML = "&#x25bc;";
185 } else {
186 sub.style.display = "none";
187 mk.innerHTML = "&#x25ba;";
188 }
189 }
190 </script>
191 </div>
192
193
194
195
196 <h1 id="_introduction"><span>1. </span> Introduction</h1>
197
198 <p>An important feature of transactional databases like SQLite
199 is "atomic commit".
200 Atomic commit means that either all database changes within a single
201 transaction occur or none of them occur. With atomic commit, it
202 is as if many different writes to different sections of the database
203 file occur instantaneously and simultaneously.
204 Real hardware serializes writes to mass storage, and writing
205 a single sector takes a finite amount of time.
206 So it is impossible to truly write many different sectors of a
207 database file simultaneously and/or instantaneously.
208 But the atomic commit logic within
209 SQLite makes it appear as if the changes for a transaction
210 are all written instantaneously and simultaneously.</p>
211
212 <p>SQLite has the important property that transactions appear
213 to be atomic even if the transaction is interrupted by an
214 operating system crash or power failure.</p>
215
216 <p>This article describes the techniques used by SQLite to create the
217 illusion of atomic commit.</p>
218
219 <p>The information in this article applies only when SQLite is operating
220 in "rollback mode", or in other words when SQLite is not
221 using a <a href="wal.html">write-ahead log</a>. SQLite still supports atomic commit when
222 write-ahead logging is enabled, but it accomplishes atomic commit by
223 a different mechanism from the one described in this article. See
224 the <a href="wal.html">write-ahead log documentation</a> for additional information on how
225 SQLite supports atomic commit in that context.</p>
226
227 <a name="hardware"></a>
228
229 <h1 id="_hardware_assumptions"><span>2. </span> Hardware Assumptions</h1>
230
231 <p>Throughout this article, we will call the mass storage device "disk"
232 even though the mass storage device might really be flash memory.</p>
233
234 <p>We assume that disk is written in chunks which we call a "sector".
235 It is not possible to modify any part of the disk smaller than a sector.
236 To change a part of the disk smaller than a sector, you have to read in
237 the full sector that contains the part you want to change, make the
238 change, then write back out the complete sector.</p>
239
240 <p>On a traditional spinning disk, a sector is the minimum unit of transfer
241 in both directions, both reading and writing. On flash memory, however,
242 the minimum size of a read is typically much smaller than a minimum write.
243 SQLite is only concerned with the minimum write amount and so for the
244 purposes of this article, when we say "sector" we mean the minimum amount
245 of data that can be written to mass storage in a single go.</p>
246
247 <p>
248 Prior to SQLite version 3.3.14, a sector size of 512 bytes was
249 assumed in all cases. There was a compile-time option to change
250 this but the code had never been tested with a larger value. The
251 512 byte sector assumption seemed reasonable since until very recently
252 all disk drives used a 512 byte sector internally. However, there
253 has recently been a push to increase the sector size of disks to
254 4096 bytes. Also the sector size
255 for flash memory is usually larger than 512 bytes. For these reasons,
256 versions of SQLite beginning with 3.3.14 have a method in the OS
257 interface layer that interrogates the underlying filesystem to find
258 the true sector size. As currently implemented (version 3.5.0) this
259 method still returns a hard-coded value of 512 bytes, since there
260 is no standard way of discovering the true sector size on either
261 Unix or Windows. But the method is available for embedded device
262 manufacturers to tweak according to their own needs. And we have
263 left open the possibility of filling in a more meaningful implementation
264 on Unix and Windows in the future.</p>
265
266 <p>SQLite has traditionally assumed that a sector write is <u>not</u> atomic.
267 However, SQLite does always assume that a sector write is linear. By "linear"
268 we mean that SQLite assumes that when writing a sector, the hardware begins
269 at one end of the data and writes byte by byte until it gets to
270 the other end. The write might go from beginning to end or from
271 end to beginning. If a power failure occurs in the middle of a
272 sector write it might be that part of the sector was modified
273 and another part was left unchanged. The key assumption by SQLite
274 is that if any part of the sector gets changed, then either the
275 first or the last bytes will be changed. So the hardware will
276 never start writing a sector in the middle and work towards the
277 ends. We do not know if this assumption is always true but it
278 seems reasonable.</p>
279
280 <p>The previous paragraph states that SQLite does not assume that
281 sector writes are atomic. This is true by default. But as of
282 SQLite version 3.5.0, there is a new interface called the
283 Virtual File System (<a href="vfs.html">VFS</a>) interface. The <a href="vfs.html">VFS</a> is the only means
284 by which SQLite communicates to the underlying filesystem. The
285 code comes with default VFS implementations for Unix and Windows
286 and there is a mechanism for creating new custom VFS implementations
287 at runtime. In this new VFS interface there is a method called
288 xDeviceCharacteristics. This method interrogates the underlying
289 filesystem to discover various properties and behaviors that the
290 filesystem may or may not exhibit. The xDeviceCharacteristics
291 method might indicate that sector writes are atomic, and if it does
292 so indicate, SQLite will try to take advantage of that fact. But
293 the default xDeviceCharacteristics method for both Unix and Windows
294 does not indicate atomic sector writes and so these optimizations
295 are normally omitted.</p>
296
297 <p>SQLite assumes that the operating system will buffer writes and
298 that a write request will return before data has actually been stored
299 in the mass storage device.
300 SQLite further assumes that write operations will be reordered by
301 the operating system.
302 For this reason, SQLite does a "flush" or "fsync" operation at key
303 points. SQLite assumes that the flush or fsync will not return until
304 all pending write operations for the file that is being flushed have
305 completed. We are told that the flush and fsync primitives
306 are broken on some versions of Windows and Linux. This is unfortunate.
307 It opens SQLite up to the possibility of database corruption following
308 a power loss in the middle of a commit. However, there is nothing
309 that SQLite can do to test for or remedy the situation. SQLite
310 assumes that the operating system that it is running on works as
311 advertised. If that is not quite the case, well then hopefully you
312 will not lose power too often.</p>
313
314 <p>SQLite assumes that when a file grows in length that the new
315 file space originally contains garbage and then later is filled in
316 with the data actually written. In other words, SQLite assumes that
317 the file size is updated before the file content. This is a
318 pessimistic assumption and SQLite has to do some extra work to make
319 sure that it does not cause database corruption if power is lost
320 between the time when the file size is increased and when the
321 new content is written. The xDeviceCharacteristics method of
322 the <a href="vfs.html">VFS</a> might indicate that the filesystem will always write the
323 data before updating the file size. (This is the
324 SQLITE_IOCAP_SAFE_APPEND property for those readers who are looking
325 at the code.) When the xDeviceCharacteristics method indicates
326 that files content is written before the file size is increased,
327 SQLite can forego some of its pedantic database protection steps
328 and thereby decrease the amount of disk I/O needed to perform a
329 commit. The current implementation, however, makes no such assumptions
330 for the default VFSes for Windows and Unix.</p>
331
332 <p>SQLite assumes that a file deletion is atomic from the
333 point of view of a user process. By this we mean that if SQLite
334 requests that a file be deleted and the power is lost during the
335 delete operation, once power is restored either the file will
336 exist completely with all if its original content unaltered, or
337 else the file will not be seen in the filesystem at all. If
338 after power is restored the file is only partially deleted,
339 if some of its data has been altered or erased,
340 or the file has been truncated but not completely removed, then
341 database corruption will likely result.</p>
342
343 <p>SQLite assumes that the detection and/or correction of
344 bit errors caused by cosmic rays, thermal noise, quantum
345 fluctuations, device driver bugs, or other mechanisms, is the
346 responsibility of the underlying hardware and operating system.
347 SQLite does not add any redundancy to the database file for
348 the purpose of detecting corruption or I/O errors.
349 SQLite assumes that the data it reads is exactly the same data
350 that it previously wrote.</p>
351
352 <p>By default, SQLite assumes that an operating system call to write
353 a range of bytes will not damage or alter any bytes outside of that range
354 even if a power loss or OS crash occurs during that write. We
355 call this the "<a href="psow.html">powersafe overwrite</a>" property.
356 Prior to <a href="releaselog/3_7_9.html">version 3.7.9</a> (2011-11-01),
357 SQLite did not assume powersafe overwrite. But with the standard
358 sector size increasing from 512 to 4096 bytes on most disk drives, it
359 has become necessary to assume powersafe overwrite in order to maintain
360 historical performance levels and so powersafe overwrite is assumed by
361 default in recent versions of SQLite. The assumption of powersafe
362 overwrite property can be disabled at compile-time or a run-time if
363 desired. See the <a href="psow.html">powersafe overwrite documentation</a> for further
364 details.
365
366
367 <a name="section_3_0"></a>
368 </p><h1 id="_single_file_commit"><span>3. </span> Single File Commit</h1>
369
370 <p>We begin with an overview of the steps SQLite takes in order to
371 perform an atomic commit of a transaction against a single database
372 file. The details of file formats used to guard against damage from
373 power failures and techniques for performing an atomic commit across
374 multiple databases are discussed in later sections.</p>
375
376 <a name="initstate"></a>
377
378 <h2 id="_initial_state"><span>3.1. </span> Initial State</h2>
379
380 <img src="images/ac/commit-0.gif" align="right" hspace="15">
381
382 <p>The state of the computer when a database connection is
383 first opened is shown conceptually by the diagram at the
384 right.
385 The area of the diagram on the extreme right (labeled "Disk") represents
386 information stored on the mass storage device. Each rectangle is
387 a sector. The blue color represents that the sectors contain
388 original data.
389 The middle area is the operating systems disk cache. At the
390 onset of our example, the cache is cold and this is represented
391 by leaving the rectangles of the disk cache empty.
392 The left area of the diagram shows the content of memory for
393 the process that is using SQLite. The database connection has
394 just been opened and no information has been read yet, so the
395 user space is empty.
396 </p>
397 <br clear="both">
398
399 <a name="rdlck"></a>
400
401 <a name="section_3_2"></a>
402 <h2 id="_acquiring_a_read_lock"><span>3.2. </span> Acquiring A Read Lock</h2>
403
404 <img src="images/ac/commit-1.gif" align="right" hspace="15">
405
406 <p>Before SQLite can write to a database, it must first read
407 the database to see what is there already. Even if it is just
408 appending new data, SQLite still has to read in the database
409 schema from the "<a href="schematab.html">sqlite_schema</a>" table so that it can know
410 how to parse the INSERT statements and discover where in the
411 database file the new information should be stored.</p>
412
413 <p>The first step toward reading from the database file
414 is obtaining a shared lock on the database file. A "shared"
415 lock allows two or more database connections to read from the
416 database file at the same time. But a shared lock prevents
417 another database connection from writing to the database file
418 while we are reading it. This is necessary because if another
419 database connection were writing to the database file at the
420 same time we are reading from the database file, we might read
421 some data before the change and other data after the change.
422 This would make it appear as if the change made by the other
423 process is not atomic.</p>
424
425 <p>Notice that the shared lock is on the operating system
426 disk cache, not on the disk itself. File locks
427 really are just flags within the operating system kernel,
428 usually. (The details depend on the specific OS layer
429 interface.) Hence, the lock will instantly vanish if the
430 operating system crashes or if there is a power loss. It
431 is usually also the case that the lock will vanish if the
432 process that created the lock exits.</p>
433
434 <br clear="both">
435
436 <a name="section_3_3"></a>
437 <h2 id="_reading_information_out_of_the_database"><span>3.3. </span> Reading Information Out Of The Database</h2>
438
439 <img src="images/ac/commit-2.gif" align="right" hspace="15">
440
441 <p>After the shared lock is acquired, we can begin reading
442 information from the database file. In this scenario, we
443 are assuming a cold cache, so information must first be
444 read from mass storage into the operating system cache then
445 transferred from operating system cache into user space.
446 On subsequent reads, some or all of the information might
447 already be found in the operating system cache and so only
448 the transfer to user space would be required.</p>
449
450 <p>Usually only a subset of the pages in the database file
451 are read. In this example we are showing three
452 pages out of eight being read. In a typical application, a
453 database will have thousands of pages and a query will normally
454 only touch a small percentage of those pages.</p>
455
456 <br clear="both">
457
458 <a name="rsvdlock"></a>
459
460 <h2 id="_obtaining_a_reserved_lock"><span>3.4. </span> Obtaining A Reserved Lock</h2>
461
462 <img src="images/ac/commit-3.gif" align="right" hspace="15">
463
464 <p>Before making changes to the database, SQLite first
465 obtains a "reserved" lock on the database file. A reserved
466 lock is similar to a shared lock in that both a reserved lock
467 and shared lock allow other processes to read from the database
468 file. A single reserve lock can coexist with multiple shared
469 locks from other processes. However, there can only be a
470 single reserved lock on the database file. Hence only a
471 single process can be attempting to write to the database
472 at one time.</p>
473
474 <p>The idea behind a reserved lock is that it signals that
475 a process intends to modify the database file in the near
476 future but has not yet started to make the modifications.
477 And because the modifications have not yet started, other
478 processes can continue to read from the database. However,
479 no other process should also begin trying to write to the
480 database.</p>
481
482 <br clear="both">
483 <a name="section_3_5"></a>
484 <h2 id="_creating_a_rollback_journal_file"><span>3.5. </span> Creating A Rollback Journal File</h2>
485 <img src="images/ac/commit-4.gif" align="right" hspace="15">
486
487 <p>Prior to making any changes to the database file, SQLite first
488 creates a separate rollback journal file and writes into the
489 rollback journal the original
490 content of the database pages that are to be altered.
491 The idea behind the rollback journal is that it contains
492 all information needed to restore the database back to
493 its original state.</p>
494
495 <p>The rollback journal contains a small header (shown in green
496 in the diagram) that records the original size of the database
497 file. So if a change causes the database file to grow, we
498 will still know the original size of the database. The page
499 number is stored together with each database page that is
500 written into the rollback journal.</p>
501
502 <p>
503 When a new file is created, most desktop operating systems
504 (Windows, Linux, Mac OS X) will not actually write anything to
505 disk. The new file is created in the operating systems disk
506 cache only. The file is not created on mass storage until sometime
507 later, when the operating system has a spare moment. This creates
508 the impression to users that I/O is happening much faster than
509 is possible when doing real disk I/O. We illustrate this idea in
510 the diagram to the right by showing that the new rollback journal
511 appears in the operating system disk cache only and not on the
512 disk itself.</p>
513
514 <br clear="both">
515 <a name="section_3_6"></a>
516 <h2 id="_changing_database_pages_in_user_space"><span>3.6. </span> Changing Database Pages In User Space</h2>
517 <img src="images/ac/commit-5.gif" align="right" hspace="15">
518
519 <p>After the original page content has been saved in the rollback
520 journal, the pages can be modified in user memory. Each database
521 connection has its own private copy of user space, so the changes
522 that are made in user space are only visible to the database connection
523 that is making the changes. Other database connections still see
524 the information in operating system disk cache buffers which have
525 not yet been changed. And so even though one process is busy
526 modifying the database, other processes can continue to read their
527 own copies of the original database content.</p>
528
529 <br clear="both">
530 <a name="section_3_7"></a>
531 <h2 id="_flushing_the_rollback_journal_file_to_mass_storage"><span>3.7. </span> Flushing The Rollback Journal File To Mass Storage</h2>
532 <img src="images/ac/commit-6.gif" align="right" hspace="15">
533
534 <p>The next step is to flush the content of the rollback journal
535 file to nonvolatile storage.
536 As we will see later,
537 this is a critical step in insuring that the database can survive
538 an unexpected power loss.
539 This step also takes a lot of time, since writing to nonvolatile
540 storage is normally a slow operation.</p>
541
542 <p>This step is usually more complicated than simply flushing
543 the rollback journal to the disk. On most platforms two separate
544 flush (or fsync()) operations are required. The first flush writes
545 out the base rollback journal content. Then the header of the
546 rollback journal is modified to show the number of pages in the
547 rollback journal. Then the header is flushed to disk. The details
548 on why we do this header modification and extra flush are provided
549 in a later section of this paper.</p>
550
551 <br clear="both">
552 <a name="section_3_8"></a>
553 <h2 id="_obtaining_an_exclusive_lock"><span>3.8. </span> Obtaining An Exclusive Lock</h2>
554 <img src="images/ac/commit-7.gif" align="right" hspace="15">
555
556 <p>Prior to making changes to the database file itself, we must
557 obtain an exclusive lock on the database file. Obtaining an
558 exclusive lock is really a two-step process. First SQLite obtains
559 a "pending" lock. Then it escalates the pending lock to an
560 exclusive lock.</p>
561
562 <p>A pending lock allows other processes that already have a
563 shared lock to continue reading the database file. But it
564 prevents new shared locks from being established. The idea
565 behind a pending lock is to prevent writer starvation caused
566 by a large pool of readers. There might be dozens, even hundreds,
567 of other processes trying to read the database file. Each process
568 acquires a shared lock before it starts reading, reads what it
569 needs, then releases the shared lock. If, however, there are
570 many different processes all reading from the same database, it
571 might happen that a new process always acquires its shared lock before
572 the previous process releases its shared lock. And so there is
573 never an instant when there are no shared locks on the database
574 file and hence there is never an opportunity for the writer to
575 seize the exclusive lock. A pending lock is designed to prevent
576 that cycle by allowing existing shared locks to proceed but
577 blocking new shared locks from being established. Eventually
578 all shared locks will clear and the pending lock will then be
579 able to escalate into an exclusive lock.</p>
580
581 <br clear="both">
582 <a name="section_3_9"></a>
583 <h2 id="_writing_changes_to_the_database_file"><span>3.9. </span> Writing Changes To The Database File</h2>
584 <img src="images/ac/commit-8.gif" align="right" hspace="15">
585
586 <p>Once an exclusive lock is held, we know that no other
587 processes are reading from the database file and it is
588 safe to write changes into the database file. Usually
589 those changes only go as far as the operating systems disk
590 cache and do not make it all the way to mass storage.</p>
591
592 <br clear="both">
593 <a name="section_3_10"></a>
594 <h2 id="0_flushing_changes_to_mass_storage"><span>3.10. </span>0 Flushing Changes To Mass Storage</h2>
595 <img src="images/ac/commit-9.gif" align="right" hspace="15">
596
597 <p>Another flush must occur to make sure that all the
598 database changes are written into nonvolatile storage.
599 This is a critical step to ensure that the database will
600 survive a power loss without damage. However, because
601 of the inherent slowness of writing to disk or flash memory,
602 this step together with the rollback journal file flush in section
603 3.7 above takes up most of the time required to complete a
604 transaction commit in SQLite.</p>
605
606 <br clear="both">
607 <a name="section_3_11"></a>
608 <h2 id="1_deleting_the_rollback_journal"><span>3.11. </span>1 Deleting The Rollback Journal</h2>
609 <img src="images/ac/commit-A.gif" align="right" hspace="15">
610
611 <p>After the database changes are all safely on the mass
612 storage device, the rollback journal file is deleted.
613 This is the instant where the transaction commits.
614 If a power failure or system crash occurs prior to this
615 point, then recovery processes to be described later make
616 it appear as if no changes were ever made to the database
617 file. If a power failure or system crash occurs after
618 the rollback journal is deleted, then it appears as if
619 all changes have been written to disk. Thus, SQLite gives
620 the appearance of having made no changes to the database
621 file or having made the complete set of changes to the
622 database file depending on whether or not the rollback
623 journal file exists.</p>
624
625 <p>Deleting a file is not really an atomic operation, but
626 it appears to be from the point of view of a user process.
627 A process is always able to ask the operating system "does
628 this file exist?" and the process will get back a yes or no
629 answer. After a power failure that occurs during a
630 transaction commit, SQLite will ask the operating system
631 whether or not the rollback journal file exists. If the
632 answer is "yes" then the transaction is incomplete and is
633 rolled back. If the answer is "no" then it means the transaction
634 did commit.</p>
635
636 <p>The existence of a transaction depends on whether or
637 not the rollback journal file exists and the deletion
638 of a file appears to be an atomic operation from the point of
639 view of a user-space process. Therefore,
640 a transaction appears to be an atomic operation.</p>
641
642 <p>The act of deleting a file is expensive on many systems.
643 As an optimization, SQLite can be configured to truncate
644 the journal file to zero bytes in length
645 or overwrite the journal file header with zeros. In either
646 case, the resulting journal file is no longer capable of rolling
647 back and so the transaction still commits. Truncating a file
648 to zero length, like deleting a file, is assumed to be an atomic
649 operation from the point of view of a user process. Overwriting
650 the header of the journal with zeros is not atomic, but if any
651 part of the header is malformed the journal will not roll back.
652 Hence, one can say that the commit occurs as soon as the header
653 is sufficiently changed to make it invalid. Typically this happens
654 as soon as the first byte of the header is zeroed.</p>
655
656 <br clear="both">
657 <a name="section_3_12"></a>
658 <h2 id="2_releasing_the_lock"><span>3.12. </span>2 Releasing The Lock</h2>
659 <img src="images/ac/commit-B.gif" align="right" hspace="15">
660
661 <p>The last step in the commit process is to release the
662 exclusive lock so that other processes can once again
663 start accessing the database file.</p>
664
665 <p>In the diagram at the right, we show that the information
666 that was held in user space is cleared when the lock is released.
667 This used to be literally true for older versions of SQLite. But
668 more recent versions of SQLite keep the user space information
669 in memory in case it might be needed again at the start of the
670 next transaction. It is cheaper to reuse information that is
671 already in local memory than to transfer the information back
672 from the operating system disk cache or to read it off of the
673 disk drive again. Prior to reusing the information in user space,
674 we must first reacquire the shared lock and then we have to check
675 to make sure that no other process modified the database file while
676 we were not holding a lock. There is a counter in the first page
677 of the database that is incremented every time the database file
678 is modified. We can find out if another process has modified the
679 database by checking that counter. If the database was modified,
680 then the user space cache must be cleared and reread. But it is
681 commonly the case that no changes have been made and the user
682 space cache can be reused for a significant performance savings.</p>
683
684 <br clear="both">
685 <a name="rollback"></a>
686
687 <h1 id="_rollback"><span>4. </span> Rollback</h1>
688
689 <p>An atomic commit is supposed to happen instantaneously. But the processing
690 described above clearly takes a finite amount of time.
691 Suppose the power to the computer were cut
692 part way through the commit operation described above. In order
693 to maintain the illusion that the changes were instantaneous, we
694 have to "rollback" any partial changes and restore the database to
695 the state it was in prior to the beginning of the transaction.</p>
696
697 <a name="crisis"></a>
698
699 <h2 id="_when_something_goes_wrong_"><span>4.1. </span> When Something Goes Wrong...</h2>
700 <img src="images/ac/rollback-0.gif" align="right" hspace="15">
701
702 <p>Suppose the power loss occurred
703 during <a href="#section_3_10">step 3.10</a> above,
704 while the database changes were being written to disk.
705 After power is restored, the situation might be something
706 like what is shown to the right. We were trying to change
707 three pages of the database file but only one page was
708 successfully written. Another page was partially written
709 and a third page was not written at all.</p>
710
711 <p>The rollback journal is complete and intact on disk when
712 the power is restored. This is a key point. The reason for
713 the flush operation in <a href="#section_3_7">step 3.7</a>
714 is to make absolutely sure that
715 all of the rollback journal is safely on nonvolatile storage
716 prior to making any changes to the database file itself.</p>
717
718 <br clear="both">
719 <a name="section_4_2"></a>
720 <h2 id="_hot_rollback_journals"><span>4.2. </span> Hot Rollback Journals</h2>
721 <img src="images/ac/rollback-1.gif" align="right" hspace="15">
722
723 <p>The first time that any SQLite process attempts to access
724 the database file, it obtains a shared lock as described in
725 <a href="#section_3_2">section 3.2</a> above.
726 But then it notices that there is a
727 rollback journal file present. SQLite then checks to see if
728 the rollback journal is a "hot journal". A hot journal is
729 a rollback journal that needs to be played back in order to
730 restore the database to a sane state. A hot journal only
731 exists when an earlier process was in the middle of committing
732 a transaction when it crashed or lost power.</p>
733
734 <p>A rollback journal is a "hot" journal if all of the following
735 are true:</p>
736
737 <ul>
738 <li>The rollback journal exists.
739 </li><li>The rollback journal is not an empty file.
740 </li><li>There is no reserved lock on the main database file.
741 </li><li>The header of the rollback journal is well-formed and in particular
742 has not been zeroed out.
743 </li><li>The rollback journal does not
744 contain the name of a super-journal file (see
745 <a href="#section_5_5">section 5.5</a> below) or if does
746 contain the name of a super-journal, then that super-journal
747 file exists.
748 </li></ul>
749
750 <p>The presence of a hot journal is our indication
751 that a previous process was trying to commit a transaction but
752 it aborted for some reason prior to the completion of the
753 commit. A hot journal means that
754 the database file is in an inconsistent state and needs to
755 be repaired (by rollback) prior to being used.</p>
756
757 <br clear="both">
758 <a name="exlock"></a>
759
760 <h2 id="_obtaining_an_exclusive_lock_on_the_database"><span>4.3. </span> Obtaining An Exclusive Lock On The Database</h2>
761 <img src="images/ac/rollback-2.gif" align="right" hspace="15">
762
763 <p>The first step toward dealing with a hot journal is to
764 obtain an exclusive lock on the database file. This prevents two
765 or more processes from trying to rollback the same hot journal
766 at the same time.</p>
767
768 <br clear="both">
769 <a name="section_4_4"></a>
770 <h2 id="_rolling_back_incomplete_changes"><span>4.4. </span> Rolling Back Incomplete Changes</h2>
771 <img src="images/ac/rollback-3.gif" align="right" hspace="15">
772
773 <p>Once a process obtains an exclusive lock, it is permitted
774 to write to the database file. It then proceeds to read the
775 original content of pages out of the rollback journal and write
776 that content back to where it came from in the database file.
777 Recall that the header of the rollback journal records the original
778 size of the database file prior to the start of the aborted
779 transaction. SQLite uses this information to truncate the
780 database file back to its original size in cases where the
781 incomplete transaction caused the database to grow. At the
782 end of this step, the database should be the same size and
783 contain the same information as it did before the start of
784 the aborted transaction.</p>
785
786 <br clear="both">
787 <a name="delhotjrnl"></a>
788
789 <h2 id="_deleting_the_hot_journal"><span>4.5. </span> Deleting The Hot Journal</h2>
790 <img src="images/ac/rollback-4.gif" align="right" hspace="15">
791
792 <p>After all information in the rollback journal has been
793 played back into the database file (and flushed to disk in case
794 we encounter yet another power failure), the hot rollback journal
795 can be deleted.</p>
796
797 <p>As in <a href="#section_3_11">section 3.11</a>, the journal
798 file might be truncated to zero length or its header might
799 be overwritten with zeros as an optimization on systems where
800 deleting a file is expensive. Either way, the journal is no
801 longer hot after this step.</p>
802
803 <br clear="both">
804 <a name="cont"></a>
805
806 <h2 id="_continue_as_if_the_uncompleted_writes_had_never_happened"><span>4.6. </span> Continue As If The Uncompleted Writes Had Never Happened</h2>
807 <img src="images/ac/rollback-5.gif" align="right" hspace="15">
808
809 <p>The final recovery step is to reduce the exclusive lock back
810 to a shared lock. Once this happens, the database is back in the
811 state that it would have been if the aborted transaction had never
812 started. Since all of this recovery activity happens completely
813 automatically and transparently, it appears to the program using
814 SQLite as if the aborted transaction had never begun.</p>
815
816 <br clear="both">
817 <a name="multicommit"></a>
818
819 <h1 id="_multi_file_commit"><span>5. </span> Multi-file Commit</h1>
820
821 <p>SQLite allows a single
822 <a href="c3ref/sqlite3.html">database connection</a> to talk to
823 two or more database files simultaneously through the use of
824 the <a href="lang_attach.html">ATTACH DATABASE</a> command.
825 When multiple database files are modified within a single
826 transaction, all files are updated atomically.
827 In other words, either all of the database files are updated or
828 else none of them are.
829 Achieving an atomic commit across multiple database files is
830 more complex that doing so for a single file. This section
831 describes how SQLite works that bit of magic.</p>
832
833 <a name="multijrnl"></a>
834
835 <h2 id="_separate_rollback_journals_for_each_database"><span>5.1. </span> Separate Rollback Journals For Each Database</h2>
836 <img src="images/ac/multi-0.gif" align="right" hspace="15">
837
838 <p>When multiple database files are involved in a transaction,
839 each database has its own rollback journal and each database
840 is locked separately. The diagram at the right shows a scenario
841 where three different database files have been modified within
842 one transaction. The situation at this step is analogous to
843 the single-file transaction scenario at
844 <a href="#section_3_6">step 3.6</a>. Each database file has
845 a reserved lock. For each database, the original content of pages
846 that are being changed have been written into the rollback journal
847 for that database, but the content of the journals have not yet
848 been flushed to disk. No changes have been made to the database
849 file itself yet, though presumably there are changes being held
850 in user memory.</p>
851
852 <p>For brevity, the diagrams in this section are simplified from
853 those that came before. Blue color still signifies original content
854 and pink still signifies new content. But the individual pages
855 in the rollback journal and the database file are not shown and
856 we are not making the distinction between information in the
857 operating system cache and information that is on disk. All of
858 these factors still apply in a multi-file commit scenario. They
859 just take up a lot of space in the diagrams and they do not add
860 any new information, so they are omitted here.</p>
861
862 <br clear="both">
863 <a name="sprjrnl"></a>
864
865 <h2 id="_the_super_journal_file"><span>5.2. </span> The Super-Journal File</h2>
866 <img src="images/ac/multi-1.gif" align="right" hspace="15">
867
868 <p>The next step in a multi-file commit is the creation of a
869 "super-journal" file. The name of the super-journal file is
870 the same name as the original database filename (the database
871 that was opened using the
872 <a href="c3ref/open.html">sqlite3_open()</a> interface,
873 not one of the <a href="lang_attach.html">ATTACHed</a> auxiliary
874 databases) with the text "<b>-mj</b><i>HHHHHHHH</i>" appended where
875 <i>HHHHHHHH</i> is a random 32-bit hexadecimal number. The
876 random <i>HHHHHHHH</i> suffix changes for every new super-journal.</p>
877
878 <p><i>(Nota bene: The formula for computing the super-journal filename
879 given in the previous paragraph corresponds to the implementation as
880 of SQLite version 3.5.0. But this formula is not part of the SQLite
881 specification and is subject to change in future releases.)</i></p>
882
883 <p>Unlike the rollback journals, the super-journal does not contain
884 any original database page content. Instead, the super-journal contains
885 the full pathnames for rollback journals for every database that is
886 participating in the transaction.</p>
887
888 <p>After the super-journal is constructed, its content is flushed
889 to disk before any further actions are taken. On Unix, the directory
890 that contains the super-journal is also synced in order to make sure
891 the super-journal file will appear in the directory following a
892 power failure.</p>
893
894 <p>The purpose of the super-journal is to ensure that multi-file
895 transactions are atomic across a power-loss. But if the database files
896 have other settings that compromise integrity across a power-loss event
897 (such as <a href="pragma.html#pragma_synchronous">PRAGMA synchronous=OFF</a> or <a href="pragma.html#pragma_journal_mode">PRAGMA journal_mode=MEMORY</a>) then
898 the creation of the super-journal is omitted, as an optimization.
899
900 <br clear="both">
901 <a name="multijrnlupdate"></a>
902
903 </p><h2 id="_updating_rollback_journal_headers"><span>5.3. </span> Updating Rollback Journal Headers</h2>
904 <img src="images/ac/multi-2.gif" align="right" hspace="15">
905
906 <p>The next step is to record the full pathname of the super-journal file
907 in the header of every rollback journal. Space to hold the
908 super-journal filename was reserved at the beginning of each rollback journal
909 as the rollback journals were created.</p>
910
911 <p>The content of each rollback journal is flushed to disk both before
912 and after the super-journal filename is written into the rollback
913 journal header. It is important to do both of these flushes. Fortunately,
914 the second flush is usually inexpensive since typically only a single
915 page of the journal file (the first page) has changed.</p>
916
917 <p>This step is analogous to
918 <a href="#section_3_7">step 3.7</a> in the single-file commit
919 scenario described above.</p>
920
921 <br clear="both">
922 <a name="multidbupdate"></a>
923
924 <h2 id="_updating_the_database_files"><span>5.4. </span> Updating The Database Files</h2>
925 <img src="images/ac/multi-3.gif" align="right" hspace="15">
926
927 <p>Once all rollback journal files have been flushed to disk, it
928 is safe to begin updating database files. We have to obtain an
929 exclusive lock on all database files before writing the changes.
930 After all the changes are written, it is important to flush the
931 changes to disk so that they will be preserved in the event of
932 a power failure or operating system crash.</p>
933
934 <p>This step corresponds to steps
935 <a href="#section_3_8">3.8</a>,
936 <a href="#section_3_9">3.9</a>, and
937 <a href="#section_3_10">3.10</a> in the single-file commit
938 scenario described previously.</p>
939
940
941 <br clear="both">
942 <a name="section_5_5"></a>
943 <h2 id="_delete_the_super_journal_file"><span>5.5. </span> Delete The Super-Journal File</h2>
944 <img src="images/ac/multi-4.gif" align="right" hspace="15">
945
946 <p>The next step is to delete the super-journal file.
947 This is the point where the multi-file transaction commits.
948 This step corresponds to
949 <a href="#section_3_11">step 3.11</a> in the single-file
950 commit scenario where the rollback journal is deleted.</p>
951
952 <p>If a power failure or operating system crash occurs at this
953 point, the transaction will not rollback when the system reboots
954 even though there are rollback journals present. The
955 difference is the super-journal pathname in the header of the
956 rollback journal. Upon restart, SQLite only considers a journal
957 to be hot and will only playback the journal if there is no
958 super-journal filename in the header (which is the case for
959 a single-file commit) or if the super-journal file still
960 exists on disk.</p>
961
962 <br clear="both">
963 <a name="cleanup"></a>
964
965 <h2 id="_clean_up_the_rollback_journals"><span>5.6. </span> Clean Up The Rollback Journals</h2>
966 <img src="images/ac/multi-5.gif" align="right" hspace="15">
967
968 <p>The final step in a multi-file commit is to delete the
969 individual rollback journals and drop the exclusive locks on
970 the database files so that other processes can see the changes.
971 This corresponds to
972 <a href="#section_3_12">step 3.12</a> in the single-file
973 commit sequence.</p>
974
975 <p>The transaction has already committed at this point so timing
976 is not critical in the deletion of the rollback journals.
977 The current implementation deletes a single rollback journal
978 then unlocks the corresponding database file before proceeding
979 to the next rollback journal. But in the future we might change
980 this so that all rollback journals are deleted before any database
981 files are unlocked. As long as the rollback journal is deleted before
982 its corresponding database file is unlocked it does not matter in what
983 order the rollback journals are deleted or the database files are
984 unlocked.</p>
985
986 <a name="moredetail"></a>
987
988 <h1 id="_additional_details_of_the_commit_process"><span>6. </span> Additional Details Of The Commit Process</h1>
989
990 <p><a href="#section_3_0">Section 3.0</a> above provides an overview of
991 how atomic commit works in SQLite. But it glosses over a number of
992 important details. The following subsections will attempt to fill
993 in the gaps.</p>
994
995 <a name="completesectors"></a>
996
997 <h2 id="_always_journal_complete_sectors"><span>6.1. </span> Always Journal Complete Sectors</h2>
998
999 <p>When the original content of a database page is written into
1000 the rollback journal (as shown in <a href="#section_3_5">section 3.5</a>),
1001 SQLite always writes a complete sector of data, even if the
1002 page size of the database is smaller than the sector size.
1003 Historically, the sector size in SQLite has been hard coded to 512
1004 bytes and since the minimum page size is also 512 bytes, this has never
1005 been an issue. But beginning with SQLite version 3.3.14, it is possible
1006 for SQLite to use mass storage devices with a sector size larger than 512
1007 bytes. So, beginning with version 3.3.14, whenever any page within a
1008 sector is written into the journal file, all pages in that same sector
1009 are stored with it.</p>
1010
1011 <p>It is important to store all pages of a sector in the rollback
1012 journal in order to prevent database corruption following a power
1013 loss while writing the sector. Suppose that pages 1, 2, 3, and 4 are
1014 all stored in sector 1 and that page 2 is modified. In order to write
1015 the changes to page 2, the underlying hardware must also rewrite the
1016 content of pages 1, 3, and 4 since the hardware must write the complete
1017 sector. If this write operation is interrupted by a power outage,
1018 one or more of the pages 1, 3, or 4 might be left with incorrect data.
1019 Hence, to avoid lasting corruption to the database, the original content
1020 of all of those pages must be contained in the rollback journal.</p>
1021
1022 <a name="journalgarbage"></a>
1023
1024 <h2 id="_dealing_with_garbage_written_into_journal_files"><span>6.2. </span> Dealing With Garbage Written Into Journal Files</h2>
1025
1026 <p>When data is appended to the end of the rollback journal,
1027 SQLite normally makes the pessimistic assumption that the file
1028 is first extended with invalid "garbage" data and that afterwards
1029 the correct data replaces the garbage. In other words, SQLite assumes
1030 that the file size is increased first and then afterwards the content
1031 is written into the file. If a power failure occurs after the file
1032 size has been increased but before the file content has been written,
1033 the rollback journal can be left containing garbage data. If after
1034 power is restored, another SQLite process sees the rollback journal
1035 containing the garbage data and tries to roll it back into the original
1036 database file, it might copy some of the garbage into the database file
1037 and thus corrupt the database file.</p>
1038
1039 <p>SQLite uses two defenses against this problem. In the first place,
1040 SQLite records the number of pages in the rollback journal in the header
1041 of the rollback journal. This number is initially zero. So during an
1042 attempt to rollback an incomplete (and possibly corrupt) rollback
1043 journal, the process doing the rollback will see that the journal
1044 contains zero pages and will thus make no changes to the database. Prior
1045 to a commit, the rollback journal is flushed to disk to ensure that
1046 all content has been synced to disk and there is no "garbage" left
1047 in the file, and only then is the page count in the header changed from
1048 zero to true number of pages in the rollback journal. The rollback journal
1049 header is always kept in a separate sector from any page data so that
1050 it can be overwritten and flushed without risking damage to a data
1051 page if a power outage occurs. Notice that the rollback journal
1052 is flushed to disk twice: once to write the page content and a second
1053 time to write the page count in the header.</p>
1054
1055 <p>The previous paragraph describes what happens when the
1056 synchronous pragma setting is "full".</p>
1057
1058 <blockquote>
1059 PRAGMA synchronous=FULL;
1060 </blockquote>
1061
1062 <p>The default synchronous setting is full so the above is what usually
1063 happens. However, if the synchronous setting is lowered to "normal",
1064 SQLite only flushes the rollback journal once, after the page count has
1065 been written.
1066 This carries a risk of corruption because it might happen that the
1067 modified (non-zero) page count reaches the disk surface before all
1068 of the data does. The data will have been written first, but SQLite
1069 assumes that the underlying filesystem can reorder write requests and
1070 that the page count can be burned into oxide first even though its
1071 write request occurred last. So as a second line of defense, SQLite
1072 also uses a 32-bit checksum on every page of data in the rollback
1073 journal. This checksum is evaluated for each page during rollback
1074 while rolling back a journal as described in
1075 <a href="#section_4_4">section 4.4</a>. If an incorrect checksum
1076 is seen, the rollback is abandoned. Note that the checksum does
1077 not guarantee that the page data is correct since there is a small
1078 but finite probability that the checksum might be right even if the data is
1079 corrupt. But the checksum does at least make such an error unlikely.
1080 </p>
1081
1082 <p>Note that the checksums in the rollback journal are not necessary
1083 if the synchronous setting is FULL. We only depend on the checksums
1084 when synchronous is lowered to NORMAL. Nevertheless, the checksums
1085 never hurt and so they are included in the rollback journal regardless
1086 of the synchronous setting.</p>
1087
1088 <a name="cachespill"></a>
1089
1090 <h2 id="_cache_spill_prior_to_commit"><span>6.3. </span> Cache Spill Prior To Commit</h2>
1091
1092 <p>The commit process shown in <a href="#section_3_0">section 3.0</a>
1093 assumes that all database changes fit in memory until it is time to
1094 commit. This is the common case. But sometimes a larger change will
1095 overflow the user-space cache prior to transaction commit. In those
1096 cases, the cache must spill to the database before the transaction
1097 is complete.</p>
1098
1099 <p>At the beginning of a cache spill, the status of the database
1100 connection is as shown in <a href="#section_3_6">step 3.6</a>.
1101 Original page content has been saved in the rollback journal and
1102 modifications of the pages exist in user memory. To spill the cache,
1103 SQLite executes steps <a href="#section_3_7">3.7</a> through
1104 <a href="#section_3_9">3.9</a>. In other words, the rollback journal
1105 is flushed to disk, an exclusive lock is acquired, and changes are
1106 written into the database. But the remaining steps are deferred
1107 until the transaction really commits. A new journal header is
1108 appended to the end of the rollback journal (in its own sector)
1109 and the exclusive database lock is retained, but otherwise processing
1110 returns to <a href="#section_3_6">step 3.6</a>. When the transaction
1111 commits, or if another cache spill occurs, steps
1112 <a href="#section_3_7">3.7</a> and <a href="#section_3_9">3.9</a> are
1113 repeated. (Step <a href="#section_3_8">3.8</a> is omitted on second
1114 and subsequent passes since an exclusive database lock is already held
1115 due to the first pass.)</p>
1116
1117 <p>A cache spill causes the lock on the database file to
1118 escalate from reserved to exclusive. This reduces concurrency.
1119 A cache spill also causes extra disk flush or fsync operations to
1120 occur and these operations are slow, hence a cache spill can
1121 seriously reduce performance.
1122 For these reasons a cache spill is avoided whenever possible.</p>
1123
1124 <a name="opts"></a>
1125
1126 <h1 id="_optimizations"><span>7. </span> Optimizations</h1>
1127
1128 <p>Profiling indicates that for most systems and in most circumstances
1129 SQLite spends most of its time doing disk I/O. It follows then that
1130 anything we can do to reduce the amount of disk I/O will likely have a
1131 large positive impact on the performance of SQLite. This section
1132 describes some of the techniques used by SQLite to try to reduce the
1133 amount of disk I/O to a minimum while still preserving atomic commit.</p>
1134
1135 <a name="keepcache"></a>
1136
1137 <h2 id="_cache_retained_between_transactions"><span>7.1. </span> Cache Retained Between Transactions</h2>
1138
1139 <p><a href="#section_3_12">Step 3.12</a> of the commit process shows
1140 that once the shared lock has been released, all user-space cache
1141 images of database content must be discarded. This is done because
1142 without a shared lock, other processes are free to modify the database
1143 file content and so any user-space image of that content might become
1144 obsolete. Consequently, each new transaction would begin by rereading
1145 data which had previously been read. This is not as bad as it sounds
1146 at first since the data being read is still likely in the operating
1147 systems file cache. So the "read" is really just a copy of data
1148 from kernel space into user space. But even so, it still takes time.</p>
1149
1150 <p>Beginning with SQLite version 3.3.14 a mechanism has been added
1151 to try to reduce the needless rereading of data. In newer versions
1152 of SQLite, the data in the user-space pager cache is retained when
1153 the lock on the database file is released. Later, after the
1154 shared lock is acquired at the beginning of the next transaction,
1155 SQLite checks to see if any other process has modified the database
1156 file. If the database has been changed in any way since the lock
1157 was last released, the user-space cache is erased at that point.
1158 But commonly the database file is unchanged and the user-space cache
1159 can be retained, and some unnecessary read operations can be avoided.</p>
1160
1161 <p>In order to determine whether or not the database file has changed,
1162 SQLite uses a counter in the database header (in bytes 24 through 27)
1163 which is incremented during every change operation. SQLite saves a copy
1164 of this counter prior to releasing its database lock. Then after
1165 acquiring the next database lock it compares the saved counter value
1166 against the current counter value and erases the cache if the values
1167 are different, or reuses the cache if they are the same.</p>
1168
1169 <a name="section_7_2"></a>
1170 <h2 id="_exclusive_access_mode"><span>7.2. </span> Exclusive Access Mode</h2>
1171
1172 <p>SQLite version 3.3.14 adds the concept of "Exclusive Access Mode".
1173 In exclusive access mode, SQLite retains the exclusive
1174 database lock at the conclusion of each transaction. This prevents
1175 other processes from accessing the database, but in many deployments
1176 only a single process is using a database so this is not a
1177 serious problem. The advantage of exclusive access mode is that
1178 disk I/O can be reduced in three ways:</p>
1179
1180 <ol>
1181 <li><p>It is not necessary to increment the change counter in the
1182 database header for transactions after the first transaction. This
1183 will often save a write of page one to both the rollback
1184 journal and the main database file.</p></li>
1185
1186 <li><p>No other processes can change the database so there is never
1187 a need to check the change counter and clear the user-space cache
1188 at the beginning of a transaction.</p></li>
1189
1190 <li><p>Each transaction can be committed by overwriting the rollback
1191 journal header with zeros rather than deleting the journal file.
1192 This avoids having to modify the directory entry for the journal file
1193 and it avoids having to deallocate disk sectors associated with the
1194 journal. Furthermore, the next transaction will overwrite existing
1195 journal file content rather than append new content and on most systems
1196 overwriting is much faster than appending.</p></li>
1197 </ol>
1198
1199 <p>The third optimization, zeroing the journal file header rather than
1200 deleting the rollback journal file,
1201 does not depend on holding an exclusive lock at all times.
1202 This optimization can be set independently of exclusive lock mode
1203 using the <a href="pragma.html#pragma_journal_mode">journal_mode pragma</a>
1204 as described in <a href="#section_7_6">section 7.6</a> below.</p>
1205
1206 <a name="freelistjrnl"></a>
1207
1208 <h2 id="_do_not_journal_freelist_pages"><span>7.3. </span> Do Not Journal Freelist Pages</h2>
1209
1210 <p>When information is deleted from an SQLite database, the pages used
1211 to hold the deleted information are added to a "<a href="fileformat2.html#freelist">freelist</a>". Subsequent
1212 inserts will draw pages off of this freelist rather than expanding the
1213 database file.</p>
1214
1215 <p>Some freelist pages contain critical data; specifically the locations
1216 of other freelist pages. But most freelist pages contain nothing useful.
1217 These latter freelist pages are called "leaf" pages. We are free to
1218 modify the content of a leaf freelist page in the database without
1219 changing the meaning of the database in any way.</p>
1220
1221 <p>Because the content of leaf freelist pages is unimportant, SQLite
1222 avoids storing leaf freelist page content in the rollback journal
1223 in <a href="#section_3_5">step 3.5</a> of the commit process.
1224 If a leaf freelist page is changed and that change does not get rolled back
1225 during a transaction recovery, the database is not harmed by the omission.
1226 Similarly, the content of a new freelist page is never written back
1227 into the database at <a href="#section_3_9">step 3.9</a> nor
1228 read from the database at <a href="#section_3_3">step 3.3</a>.
1229 These optimizations can greatly reduce the amount of I/O that occurs
1230 when making changes to a database file that contains free space.</p>
1231
1232 <a name="atomicsector"></a>
1233
1234 <h2 id="_single_page_updates_and_atomic_sector_writes"><span>7.4. </span> Single Page Updates And Atomic Sector Writes</h2>
1235
1236 <p>Beginning in SQLite version 3.5.0, the new Virtual File System (VFS)
1237 interface contains a method named xDeviceCharacteristics which reports
1238 on special properties that the underlying mass storage device
1239 might have. Among the special properties that
1240 xDeviceCharacteristics might report is the ability of to do an
1241 atomic sector write.</p>
1242
1243 <p>Recall that by default SQLite assumes that sector writes are
1244 linear but not atomic. A linear write starts at one end of the
1245 sector and changes information byte by byte until it gets to the
1246 other end of the sector. If a power loss occurs in the middle of
1247 a linear write then part of the sector might be modified while the
1248 other end is unchanged. In an atomic sector write, either the entire
1249 sector is overwritten or else nothing in the sector is changed.</p>
1250
1251 <p>We believe that most modern disk drives implement atomic sector
1252 writes. When power is lost, the drive uses energy stored in capacitors
1253 and/or the angular momentum of the disk platter to provide power to
1254 complete any operation in progress. Nevertheless, there are so many
1255 layers in between the write system call and the on-board disk drive
1256 electronics that we take the safe approach in both Unix and w32 VFS
1257 implementations and assume that sector writes are not atomic. On the
1258 other hand, device
1259 manufacturers with more control over their filesystems might want
1260 to consider enabling the atomic write property of xDeviceCharacteristics
1261 if their hardware really does do atomic writes.</p>
1262
1263 <p>When sector writes are atomic and the page size of a database is
1264 the same as a sector size, and when there is a database change that
1265 only touches a single database page, then SQLite skips the whole
1266 journaling and syncing process and simply writes the modified page
1267 directly into the database file. The change counter in the first
1268 page of the database file is modified separately since no harm is
1269 done if power is lost before the change counter can be updated.</p>
1270
1271 <a name="safeappend"></a>
1272
1273 <h2 id="_filesystems_with_safe_append_semantics"><span>7.5. </span> Filesystems With Safe Append Semantics</h2>
1274
1275 <p>Another optimization introduced in SQLite version 3.5.0 makes
1276 use of "safe append" behavior of the underlying disk.
1277 Recall that SQLite assumes that when data is appended to a file
1278 (specifically to the rollback journal) that the size of the file
1279 is increased first and that the content is written second. So
1280 if power is lost after the file size is increased but before the
1281 content is written, the file is left containing invalid "garbage"
1282 data. The xDeviceCharacteristics method of the VFS might, however,
1283 indicate that the filesystem implements "safe append" semantics.
1284 This means that the content is written before the file size is
1285 increased so that it is impossible for garbage to be introduced
1286 into the rollback journal by a power loss or system crash.</p>
1287
1288 <p>When safe append semantics are indicated for a filesystem,
1289 SQLite always stores the special value of -1 for the page count
1290 in the header of the rollback journal. The -1 page count value
1291 tells any process attempting to rollback the journal that the
1292 number of pages in the journal should be computed from the journal
1293 size. This -1 value is never changed. So that when a commit
1294 occurs, we save a single flush operation and a sector write of
1295 the first page of the journal file. Furthermore, when a cache
1296 spill occurs we no longer need to append a new journal header
1297 to the end of the journal; we can simply continue appending
1298 new pages to the end of the existing journal.</p>
1299
1300 <a name="section_7_6"></a>
1301 <h2 id="_persistent_rollback_journals"><span>7.6. </span> Persistent Rollback Journals</h2>
1302
1303 <p>Deleting a file is an expensive operation on many systems.
1304 So as an optimization, SQLite can be configured to avoid the
1305 delete operation of <a href="#section_3_11">section 3.11</a>.
1306 Instead of deleting the journal file in order to commit a transaction,
1307 the file is either truncated to zero bytes in length or its
1308 header is overwritten with zeros. Truncating the file to zero
1309 length saves having to make modifications to the directory containing
1310 the file since the file is not removed from the directory.
1311 Overwriting the header has the additional savings of not having
1312 to update the length of the file (in the "inode" on many systems)
1313 and not having to deal with newly freed disk sectors. Furthermore,
1314 at the next transaction the journal will be created by overwriting
1315 existing content rather than appending new content onto the end
1316 of a file, and overwriting is often much faster than appending.</p>
1317
1318 <p>SQLite can be configured to commit transactions by overwriting
1319 the journal header with zeros instead of deleting the journal file
1320 by setting the "PERSIST" journaling mode using the
1321 <a href="pragma.html#pragma_journal_mode">journal_mode</a> PRAGMA.
1322 For example:</p>
1323
1324 <blockquote><pre>
1325 PRAGMA journal_mode=PERSIST;
1326 </pre></blockquote>
1327
1328 <p>The use of persistent journal mode provides a noticeable performance
1329 improvement on many systems. Of course, the drawback is that the
1330 journal files remain on the disk, using disk space and cluttering
1331 directories, long after the transaction commits. The only safe way
1332 to delete a persistent journal file is to commit a transaction
1333 with journaling mode set to DELETE:</p>
1334
1335 <blockquote><pre>
1336 PRAGMA journal_mode=DELETE;
1337 BEGIN EXCLUSIVE;
1338 COMMIT;
1339 </pre></blockquote>
1340
1341 <p>Beware of deleting persistent journal files by any other means
1342 since the journal file might be hot, in which case deleting it will
1343 corrupt the corresponding database file.</p>
1344
1345 <p>Beginning in SQLite <a href="releaselog/3_6_4.html">version 3.6.4</a> (2008-10-15),
1346 the TRUNCATE journal mode is
1347 also supported:</p>
1348
1349 <blockquote><pre>
1350 PRAGMA journal_mode=TRUNCATE;
1351 </pre></blockquote>
1352
1353 <p>In truncate journal mode, the transaction is committed by truncating
1354 the journal file to zero length rather than deleting the journal file
1355 (as in DELETE mode) or by zeroing the header (as in PERSIST mode).
1356 TRUNCATE mode shares the advantage of PERSIST mode that the directory
1357 that contains the journal file and database does not need to be updated.
1358 Hence truncating a file is often faster than deleting it. TRUNCATE has
1359 the additional advantage that it is not followed by a
1360 system call (ex: fsync()) to synchronize the change to disk. It might
1361 be safer if it did.
1362 But on many modern filesystems, a truncate is an atomic and
1363 synchronous operation and so we think that TRUNCATE will usually be safe
1364 in the face of power failures. If you are uncertain about whether or
1365 not TRUNCATE will be synchronous and atomic on your filesystem and it is
1366 important to you that your database survive a power loss or operating
1367 system crash that occurs during the truncation operation, then you might
1368 consider using a different journaling mode.</p>
1369
1370 <p>On embedded systems with synchronous filesystems, TRUNCATE results
1371 in slower behavior than PERSIST. The commit operation is the same speed.
1372 But subsequent transactions are slower following a TRUNCATE because it is
1373 faster to overwrite existing content than to append to the end of a file.
1374 New journal file entries will always be appended following a TRUNCATE but
1375 will usually overwrite with PERSIST.</p>
1376
1377 <a name="testing"></a>
1378
1379 <h1 id="_testing_atomic_commit_behavior"><span>8. </span> Testing Atomic Commit Behavior</h1>
1380
1381 <p>The developers of SQLite are confident that it is robust
1382 in the face of power failures and system crashes because the
1383 automatic test procedures do extensive checks on
1384 the ability of SQLite to recover from simulated power loss.
1385 We call these the "crash tests".</p>
1386
1387 <p>Crash tests in SQLite use a modified VFS that can simulate
1388 the kinds of filesystem damage that occur during a power
1389 loss or operating system crash. The crash-test VFS can simulate
1390 incomplete sector writes, pages filled with garbage data because
1391 a write has not completed, and out of order writes, all occurring
1392 at varying points during a test scenario. Crash tests execute
1393 transactions over and over, varying the time at which a simulated
1394 power loss occurs and the properties of the damage inflicted.
1395 Each test then reopens the database after the simulated crash and
1396 verifies that the transaction either occurred completely
1397 or not at all and that the database is in a completely
1398 consistent state.</p>
1399
1400 <p>The crash tests in SQLite have discovered a number of very
1401 subtle bugs (now fixed) in the recovery mechanism. Some of
1402 these bugs were very obscure and unlikely to have been found
1403 using only code inspection and analysis techniques. From this
1404 experience, the developers of SQLite feel confident that any other
1405 database system that does not use a similar crash test system
1406 likely contains undetected bugs that will lead to database
1407 corruption following a system crash or power failure.</p>
1408
1409 <a name="sect_9_0"></a>
1410
1411 <h1 id="_things_that_can_go_wrong"><span>9. </span> Things That Can Go Wrong</h1>
1412
1413 <p>The atomic commit mechanism in SQLite has proven to be robust,
1414 but it can be circumvented by a sufficiently creative
1415 adversary or a sufficiently broken operating system implementation.
1416 This section describes a few of the ways in which an SQLite database
1417 might be corrupted by a power failure or system crash.
1418 (See also: <a href="howtocorrupt.html">How To Corrupt Your Database Files</a>.)</p>
1419
1420 <a name="brokenlocks"></a>
1421
1422 <h2 id="_broken_locking_implementations"><span>9.1. </span> Broken Locking Implementations</h2>
1423
1424 <p>SQLite uses filesystem locks to make sure that only one
1425 process and database connection is trying to modify the database
1426 at a time. The filesystem locking mechanism is implemented
1427 in the VFS layer and is different for every operating system.
1428 SQLite depends on this implementation being correct. If something
1429 goes wrong and two or more processes are able to write the same
1430 database file at the same time, severe damage can result.</p>
1431
1432 <p>We have received reports of implementations of both
1433 Windows network filesystems and NFS in which locking was
1434 subtly broken. We can not verify these reports, but as
1435 locking is difficult to get right on a network filesystem
1436 we have no reason to doubt them. You are advised to
1437 avoid using SQLite on a network filesystem in the first place,
1438 since performance will be slow. But if you must use a
1439 network filesystem to store SQLite database files, consider
1440 using a secondary locking mechanism to prevent simultaneous
1441 writes to the same database even if the native filesystem
1442 locking mechanism malfunctions.</p>
1443
1444 <p>The versions of SQLite that come preinstalled on Apple
1445 Mac OS X computers contain a version of SQLite that has been
1446 extended to use alternative locking strategies that work on
1447 all network filesystems that Apple supports. These extensions
1448 used by Apple work great as long as all processes are accessing
1449 the database file in the same way. Unfortunately, the locking
1450 mechanisms do not exclude one another, so if one process is
1451 accessing a file using (for example) AFP locking and another
1452 process (perhaps on a different machine) is using dot-file locks,
1453 the two processes might collide because AFP locks do not exclude
1454 dot-file locks or vice versa.</p>
1455
1456 <a name="fsync"></a>
1457
1458 <h2 id="_incomplete_disk_flushes"><span>9.2. </span> Incomplete Disk Flushes</h2>
1459
1460 <p>SQLite uses the fsync() system call on Unix and the FlushFileBuffers()
1461 system call on w32 in order to sync the file system buffers onto disk
1462 oxide as shown in <a href="#section_3_7">step 3.7</a> and
1463 <a href="#section_3_10">step 3.10</a>. Unfortunately, we have received
1464 reports that neither of these interfaces works as advertised on many
1465 systems. We hear that FlushFileBuffers() can be completely disabled
1466 using registry settings on some Windows versions. Some historical
1467 versions of Linux contain versions of fsync() which are no-ops on
1468 some filesystems, we are told. Even on systems where
1469 FlushFileBuffers() and fsync() are said to be working, often
1470 the IDE disk control lies and says that data has reached oxide
1471 while it is still held only in the volatile control cache.</p>
1472
1473 <p>On the Mac, you can set this pragma:</p>
1474
1475 <blockquote>
1476 PRAGMA fullfsync=ON;
1477 </blockquote>
1478
1479 <p>Setting fullfsync on a Mac will guarantee that data really does
1480 get pushed out to the disk platter on a flush. But the implementation
1481 of fullfsync involves resetting the disk controller. And so not only
1482 is it profoundly slow, it also slows down other unrelated disk I/O.
1483 So its use is not recommended.</p>
1484
1485 <a name="filedel"></a>
1486
1487 <h2 id="_partial_file_deletions"><span>9.3. </span> Partial File Deletions</h2>
1488
1489 <p>SQLite assumes that file deletion is an atomic operation from the
1490 point of view of a user process. If power fails in the middle of
1491 a file deletion, then after power is restored SQLite expects to see
1492 either the entire file with all of its original data intact, or it
1493 expects not to find the file at all. Transactions may not be atomic
1494 on systems that do not work this way.</p>
1495
1496 <a name="filegarbage"></a>
1497
1498 <h2 id="_garbage_written_into_files"><span>9.4. </span> Garbage Written Into Files</h2>
1499
1500 <p>SQLite database files are ordinary disk files that can be
1501 opened and written by ordinary user processes. A rogue process
1502 can open an SQLite database and fill it with corrupt data.
1503 Corrupt data might also be introduced into an SQLite database
1504 by bugs in the operating system or disk controller; especially
1505 bugs triggered by a power failure. There is nothing SQLite can
1506 do to defend against these kinds of problems.</p>
1507
1508 <a name="mvhotjrnl"></a>
1509
1510 <h2 id="_deleting_or_renaming_a_hot_journal"><span>9.5. </span> Deleting Or Renaming A Hot Journal</h2>
1511
1512 <p>If a crash or power loss does occur and a hot journal is left on
1513 the disk, it is essential that the original database file and the hot
1514 journal remain on disk with their original names until the database
1515 file is opened by another SQLite process and rolled back.
1516 During recovery at <a href="#section_4_2">step 4.2</a> SQLite locates
1517 the hot journal by looking for a file in the same directory as the
1518 database being opened and whose name is derived from the name of the
1519 file being opened. If either the original database file or the
1520 hot journal have been moved or renamed, then the hot journal will
1521 not be seen and the database will not be rolled back.</p>
1522
1523 <p>We suspect that a common failure mode for SQLite recovery happens
1524 like this: A power failure occurs. After power is restored, a well-meaning
1525 user or system administrator begins looking around on the disk for
1526 damage. They see their database file named "important.data". This file
1527 is perhaps familiar to them. But after the crash, there is also a
1528 hot journal named "important.data-journal". The user then deletes
1529 the hot journal, thinking that they are helping to cleanup the system.
1530 We know of no way to prevent this other than user education.</p>
1531
1532 <p>If there are multiple (hard or symbolic) links to a database file,
1533 the journal will be created using the name of the link through which
1534 the file was opened. If a crash occurs and the database is opened again
1535 using a different link, the hot journal will not be located and no
1536 rollback will occur.</p>
1537
1538 <p>Sometimes a power failure will cause a filesystem to be corrupted
1539 such that recently changed filenames are forgotten and the file is
1540 moved into a "/lost+found" directory. When that happens, the hot
1541 journal will not be found and recovery will not occur.
1542 SQLite tries to prevent this
1543 by opening and syncing the directory containing the rollback journal
1544 at the same time it syncs the journal file itself. However, the
1545 movement of files into /lost+found can be caused by unrelated processes
1546 creating unrelated files in the same directory as the main database file.
1547 And since this is out from under the control of SQLite, there is nothing
1548 that SQLite can do to prevent it. If you are running on a system that
1549 is vulnerable to this kind of filesystem namespace corruption (most
1550 modern journalling filesystems are immune, we believe) then you might
1551 want to consider putting each SQLite database file in its own private
1552 subdirectory.</p>
1553
1554 <a name="future"></a>
1555
1556 <h1 id="_future_directions_and_conclusion"><span>10. </span> Future Directions And Conclusion</h1>
1557
1558 <p>Every now and then someone discovers a new failure mode for
1559 the atomic commit mechanism in SQLite and the developers have to
1560 put in a patch. This is happening less and less and the
1561 failure modes are becoming more and more obscure. But it would
1562 still be foolish to suppose that the atomic commit logic of
1563 SQLite is entirely bug-free. The developers are committed to fixing
1564 these bugs as quickly as they might be found.</p>
1565
1566 <p>
1567 The developers are also on the lookout for new ways to
1568 optimize the commit mechanism. The current VFS implementations
1569 for Unix (Linux and Mac OS X) and Windows make pessimistic assumptions about
1570 the behavior of those systems. After consultation with experts
1571 on how these systems work, we might be able to relax some of the
1572 assumptions on these systems and allow them to run faster. In
1573 particular, we suspect that most modern filesystems exhibit the
1574 safe append property and that many of them might support atomic
1575 sector writes. But until this is known for certain, SQLite will
1576 take the conservative approach and assume the worst.</p>
1577 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/atomiccommit.in?m=a727909a51">2022-12-31 21:51:03</a> UTC </small></i></p>
1578