README.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. NOTE (2012-11-29):
  2. The functionality implemented by this extension has been superseded
  3. by WAL-mode. This module is no longer supported or maintained. The
  4. code is retained for historical reference only.
  5. ------------------------------------------------------------------------------
  6. Normally, when SQLite writes to a database file, it waits until the write
  7. operation is finished before returning control to the calling application.
  8. Since writing to the file-system is usually very slow compared with CPU
  9. bound operations, this can be a performance bottleneck. This directory
  10. contains an extension that causes SQLite to perform all write requests
  11. using a separate thread running in the background. Although this does not
  12. reduce the overall system resources (CPU, disk bandwidth etc.) at all, it
  13. allows SQLite to return control to the caller quickly even when writing to
  14. the database, eliminating the bottleneck.
  15. 1. Functionality
  16. 1.1 How it Works
  17. 1.2 Limitations
  18. 1.3 Locking and Concurrency
  19. 2. Compilation and Usage
  20. 3. Porting
  21. 1. FUNCTIONALITY
  22. With asynchronous I/O, write requests are handled by a separate thread
  23. running in the background. This means that the thread that initiates
  24. a database write does not have to wait for (sometimes slow) disk I/O
  25. to occur. The write seems to happen very quickly, though in reality
  26. it is happening at its usual slow pace in the background.
  27. Asynchronous I/O appears to give better responsiveness, but at a price.
  28. You lose the Durable property. With the default I/O backend of SQLite,
  29. once a write completes, you know that the information you wrote is
  30. safely on disk. With the asynchronous I/O, this is not the case. If
  31. your program crashes or if a power loss occurs after the database
  32. write but before the asynchronous write thread has completed, then the
  33. database change might never make it to disk and the next user of the
  34. database might not see your change.
  35. You lose Durability with asynchronous I/O, but you still retain the
  36. other parts of ACID: Atomic, Consistent, and Isolated. Many
  37. appliations get along fine without the Durablity.
  38. 1.1 How it Works
  39. Asynchronous I/O works by creating a special SQLite "vfs" structure
  40. and registering it with sqlite3_vfs_register(). When files opened via
  41. this vfs are written to (using the vfs xWrite() method), the data is not
  42. written directly to disk, but is placed in the "write-queue" to be
  43. handled by the background thread.
  44. When files opened with the asynchronous vfs are read from
  45. (using the vfs xRead() method), the data is read from the file on
  46. disk and the write-queue, so that from the point of view of
  47. the vfs reader the xWrite() appears to have already completed.
  48. The special vfs is registered (and unregistered) by calls to the
  49. API functions sqlite3async_initialize() and sqlite3async_shutdown().
  50. See section "Compilation and Usage" below for details.
  51. 1.2 Limitations
  52. In order to gain experience with the main ideas surrounding asynchronous
  53. IO, this implementation is deliberately kept simple. Additional
  54. capabilities may be added in the future.
  55. For example, as currently implemented, if writes are happening at a
  56. steady stream that exceeds the I/O capability of the background writer
  57. thread, the queue of pending write operations will grow without bound.
  58. If this goes on for long enough, the host system could run out of memory.
  59. A more sophisticated module could to keep track of the quantity of
  60. pending writes and stop accepting new write requests when the queue of
  61. pending writes grows too large.
  62. 1.3 Locking and Concurrency
  63. Multiple connections from within a single process that use this
  64. implementation of asynchronous IO may access a single database
  65. file concurrently. From the point of view of the user, if all
  66. connections are from within a single process, there is no difference
  67. between the concurrency offered by "normal" SQLite and SQLite
  68. using the asynchronous backend.
  69. If file-locking is enabled (it is enabled by default), then connections
  70. from multiple processes may also read and write the database file.
  71. However concurrency is reduced as follows:
  72. * When a connection using asynchronous IO begins a database
  73. transaction, the database is locked immediately. However the
  74. lock is not released until after all relevant operations
  75. in the write-queue have been flushed to disk. This means
  76. (for example) that the database may remain locked for some
  77. time after a "COMMIT" or "ROLLBACK" is issued.
  78. * If an application using asynchronous IO executes transactions
  79. in quick succession, other database users may be effectively
  80. locked out of the database. This is because when a BEGIN
  81. is executed, a database lock is established immediately. But
  82. when the corresponding COMMIT or ROLLBACK occurs, the lock
  83. is not released until the relevant part of the write-queue
  84. has been flushed through. As a result, if a COMMIT is followed
  85. by a BEGIN before the write-queue is flushed through, the database
  86. is never unlocked,preventing other processes from accessing
  87. the database.
  88. File-locking may be disabled at runtime using the sqlite3async_control()
  89. API (see below). This may improve performance when an NFS or other
  90. network file-system, as the synchronous round-trips to the server be
  91. required to establish file locks are avoided. However, if multiple
  92. connections attempt to access the same database file when file-locking
  93. is disabled, application crashes and database corruption is a likely
  94. outcome.
  95. 2. COMPILATION AND USAGE
  96. The asynchronous IO extension consists of a single file of C code
  97. (sqlite3async.c), and a header file (sqlite3async.h) that defines the
  98. C API used by applications to activate and control the modules
  99. functionality.
  100. To use the asynchronous IO extension, compile sqlite3async.c as
  101. part of the application that uses SQLite. Then use the API defined
  102. in sqlite3async.h to initialize and configure the module.
  103. The asynchronous IO VFS API is described in detail in comments in
  104. sqlite3async.h. Using the API usually consists of the following steps:
  105. 1. Register the asynchronous IO VFS with SQLite by calling the
  106. sqlite3async_initialize() function.
  107. 2. Create a background thread to perform write operations and call
  108. sqlite3async_run().
  109. 3. Use the normal SQLite API to read and write to databases via
  110. the asynchronous IO VFS.
  111. Refer to sqlite3async.h for details.
  112. 3. PORTING
  113. Currently the asynchronous IO extension is compatible with win32 systems
  114. and systems that support the pthreads interface, including Mac OSX, Linux,
  115. and other varieties of Unix.
  116. To port the asynchronous IO extension to another platform, the user must
  117. implement mutex and condition variable primitives for the new platform.
  118. Currently there is no externally available interface to allow this, but
  119. modifying the code within sqlite3async.c to include the new platforms
  120. concurrency primitives is relatively easy. Search within sqlite3async.c
  121. for the comment string "PORTING FUNCTIONS" for details. Then implement
  122. new versions of each of the following:
  123. static void async_mutex_enter(int eMutex);
  124. static void async_mutex_leave(int eMutex);
  125. static void async_cond_wait(int eCond, int eMutex);
  126. static void async_cond_signal(int eCond);
  127. static void async_sched_yield(void);
  128. The functionality required of each of the above functions is described
  129. in comments in sqlite3async.c.