sqlite发生错误时候多了 xxx.db-shm xxx.db-wal文件
shm 格式文件解释
WAL-Index Format
http://www.sqlite.org/fileformat2.html
Conceptually,the wal-index is shared memory,though the current VFS implementations use a mmapped file for the wal-index. The mmapped file is in the same directory as the database and has the same name as the database with a "-shm" suffix appended. Because the wal-index is shared memory,sqlite does not supportjournal_mode=WALon a network filesystem when clients are on different machines. All users of the database must be able to share the same memory.
The purpose of the wal-index is to answer this question quickly:
Given a page number P and a maximum WAL frame index M,return the largest WAL frame index for page P that does not exceed M,or return NULL if there are no frames for page P that do not exceed M.
TheMvalue in the prevIoUs paragraph is the "mxFrame" value defined insection 4.4that is read at the start of a transaction and which defines the maximum frame from the WAL that the reader will use.
The wal-index is transient. After a crash,the wal-index is reconstructed from the original WAL file. The VFS is required to either truncate or zero the header of the wal-index when the last connection to it closes. Because the wal-index is transient,it can use an architecture-specific format; it does not have to be cross-platform. Hence,unlike the database and WAL file formats which store all values as big endian,the wal-index stores multi-byte values in the native byte order of the host computer.
This document is concerned with the persist state of the database file,and since the wal-index is a transient structure,no further information about the format of the wal-index will be provided here. Complete details on the format of the wal-index are contained within comments in sqlite source code.
Write-Ahead Logging文件格式 (http://www.sqlite.org/wal.html)
The default method by which sqlite implementsatomic commit and rollbackis arollback journal. Beginning withversion 3.7.0,a new "Write-Ahead Log" option (hereafter referred to as "WAL") is available.
There are advantages and disadvantages to using WAL instead of a rollback journal. Advantages include:
- WAL is significantly faster in most scenarios.
- WAL provides more concurrency as readers do not block writers and a writer does not block readers. Reading and writing can proceed concurrently.
- Disk I/O operations tends to be more sequential using WAL.
- WAL uses many fewer fsync() operations and is thus less vulnerable to problems on systems where the fsync() system call is broken.
But there are also disadvantages:
- WAL normally requires that theVFSsupports shared-memory primitives. (Exception:WAL without shared memory) The built-in unix and windows VFSes support this but third-party extension VFSes for custom operating systems might not.
- All processes using a database must be on the same host computer; WAL does not work over a network filesystem.
- Transactions that involve changes against multipleATTACHeddatabases are atomic for each individual database,but are not atomic across all databases as a set.
- It is not possible to change the database page size after entering WAL mode,either on an empty database or by usingVACUUMor by restoring from a backup using thebackup API. You must be in a rollback journal mode to change the page size.
- It is not possible to openread-only WAL databases. The opening process must have write privileges for "-shm"wal-indexshared memory file associated with the database,if that file exists,or else write access on the directory containing the database file if the "-shm" file does not exist.
- WAL might be very slightly slower (perhaps 1% or 2% slower) than the traditional rollback-journal approach in applications that do mostly reads and seldom write.
- There is an additional quasi-persistent "-wal" file and "-shmshared memory file associated with each database,which can make sqlite less appealing for use as anapplication file-format.
- There is the extra operation ofcheckpointingwhich,though automatic by default,is still something that application developers need to be mindful of.
- WAL works best with smaller transactions. WAL does not work well for very large transactions. For transactions larger than about 100 megabytes,traditional rollback journal modes will likely be faster. For transactions in excess of a gigabyte,WAL mode may fail with an I/O or disk-full error. It is recommended that one of the rollback journal modes be used for transactions larger than a few dozen megabytes.
How WAL Works
The traditional rollback journal works by writing a copy of the original unchanged database content into a separate rollback journal file and then writing changes directly into the database file. In the event of a crash orROLLBACK,the original content contained in the rollback journal is played back into the database file to revert the database file to its original state. TheCOMMIToccurs when the rollback journal is deleted.
The WAL approach inverts this. The original content is preserved in the database file and the changes are appended into a separate WAL file. ACOMMIToccurs when a special record indicating a commit is appended to the WAL. Thus a COMMIT can happen without ever writing to the original database,which allows readers to continue operating from the original unaltered database while changes are simultaneously being committed into the WAL. Multiple transactions can be appended to the end of a single WAL file.
Checkpointing
Of course,one wants to eventually transfer all the transactions that are appended in the WAL file back into the original database. Moving the WAL file transactions back into the database is called a "checkpoint".
Another way to think about the difference between rollback and write-ahead log is that in the rollback-journal approach,there are two primitive operations,reading and writing,whereas with a write-ahead log there are now three primitive operations: reading,writing,and checkpointing.
By default,sqlite does a checkpoint automatically when the WAL file reaches a threshold size of 1000 pages. (TheSQLITE_DEFAULT_WAL_AUTOCHECKPOINTcompile-time option can be used to specify a different default.) Applications using WAL do not have to do anything in order to for these checkpoints to occur. But if they want to,applications can adjust the automatic checkpoint threshold. Or they can turn off the automatic checkpoints and run checkpoints during idle moments or in a separate thread or process.
Concurrency
When a read operation begins on a WAL-mode database,it first remembers the location of the last valid commit record in the WAL. Call this point the "end mark". Because the WAL can be growing and adding new commit records while varIoUs readers connect to the database,each reader can potentially have its own end mark. But for any particular reader,the end mark is unchanged for the duration of the transaction,thus ensuring that a single read transaction only sees the database content as it existed at a single point in time.
When a reader needs a page of content,it first checks the WAL to see if that page appears there,and if so it pulls in the last copy of the page that occurs in the WAL prior to the reader's end mark. If no copy of the page exists in the WAL prior to the reader's end mark,then the page is read from the original database file. Readers can exist in separate processes,so to avoid forcing every reader to scan the entire WAL looking for pages (the WAL file can grow to multiple megabytes,depending on how often checkpoints are run),a data structure called the "wal-index" is maintained in shared memory which helps readers locate pages in the WAL quickly and with a minimum of I/O. The wal-index greatly improves the performance of readers,but the use of shared memory means that all readers must exist on the same machine. This is why the write-ahead log implementation will not work on a network filesystem.
Writers merely append new content to the end of the WAL file. Because writers do nothing that would interfere with the actions of readers,writers and readers can run at the same time. However,since there is only one WAL file,there can only be one writer at a time.
A checkpoint operation takes content from the WAL file and transfers it back into the original database file. A checkpoint can run concurrently with readers,however the checkpoint must stop when it reaches a page in the WAL that is past the read mark of any current reader. The checkpoint has to stop at that point because otherwise it might overwrite part of the database file that the reader is actively using. The checkpoint remembers (in the wal-index) how far it got and will resume transferring content from the WAL to the database from where it left off on the next invocation.
Thus a long-running read transaction can prevent a checkpointer from making progress. But presumably every read transactions will eventually end and the checkpointer will be able to continue.
Whenever a write operation occurs,the writer checks how much progress the checkpointer has made,and if the entire WAL has been transferred into the database and synced and if no readers are making use of the WAL,then the writer will rewind the WAL back to the beginning and start putting new transactions at the beginning of the WAL. This mechanism prevents a WAL file from growing without bound.
Performance Considerations
Write transactions are very fast since they only involve writing the content once (versus twice for rollback-journal transactions) and because the writes are all sequential. Further,syncing the content to the disk is not required,as long as the application is willing to sacrifice durability following a power loss or hard reboot. (Writers sync the WAL on every transaction commit ifPRAGMA synchronousis set to FULL but omit this sync ifPRAGMA synchronousis set to NORMAL.)
On the other hand,read performance deteriorates as the WAL file grows in size since each reader must check the WAL file for the content and the time needed to check the WAL file is proportional to the size of the WAL file. The wal-index helps find content in the WAL file much faster,but performance still falls off with increasing WAL file size. Hence,to maintain good read performance it is important to keep the WAL file size down by running checkpoints at regular intervals.
Checkpointing does require sync operations in order to avoid the possibility of database corruption following a power loss or hard reboot. The WAL must be synced to persistent storage prior to moving content from the WAL into the database and the database file must by synced prior to resetting the WAL. Checkpoint also requires more seeking. The checkpointer makes an effort to do as many sequential page writes to the database as it can (the pages are transferred from WAL to database in ascending order) but even then there will typically be many seek operations interspersed among the page writes. These factors combine to make checkpoints slower than write transactions.
The default strategy is to allow successive write transactions to grow the WAL until the WAL becomes about 1000 pages in size,then to run a checkpoint operation for each subsequent COMMIT until the WAL is reset to be smaller than 1000 pages. By default,the checkpoint will be run automatically by the same thread that does the COMMIT that pushes the WAL over its size limit. This has the effect of causing most COMMIT operations to be very fast but an occasional COMMIT (those that trigger a checkpoint) to be much slower. If that effect is undesirable,then the application can disable automatic checkpointing and run the periodic checkpoints in a separate thread,or separate process. (Links to commands and interfaces to accomplish this areshown below.)
Note that withPRAGMA synchronousset to NORMAL,the checkpoint is the only operation to issue an I/O barrier or sync operation (fsync() on unix or FlushFileBuffers() on windows). If an application therefore runs checkpoint in a separate thread or process,the main thread or process that is doing database queries and updates will never block on a sync operation. This helps to prevent "latch-up" in applications running on a busy disk drive. The downside to this configuration is that transactions are no longer durable and might rollback following a power failure or hard reset.
Notice too that there is a tradeoff between average read performance and average write performance. To maximize the read performance,one wants to keep the WAL as small as possible and hence run checkpoints frequently,perhaps as often as every COMMIT. To maximize write performance,one wants to amortize the cost of each checkpoint over as many writes as possible,meaning that one wants to run checkpoints infrequently and let the WAL grow as large as possible before each checkpoint. The decision of how often to run checkpoints may therefore vary from one application to another depending on the relative read and write performance requirements of the application. The default strategy is to run a checkpoint once the WAL reaches 1000 pages and this strategy seems to work well in test applications on workstations,but other strategies might work better on different platforms or for different workloads.
Activating And Configuring WAL Mode
An sqlite database connection defaults tojournal_mode=DELETE. To convert to WAL mode,use the following pragma:
PRAGMA journal_mode=WAL;
The journal_mode pragma returns a string which is the new journal mode. On success,the pragma will return the string "wal". If the conversion to WAL could not be completed (for example,if the VFS does not support the necessary shared-memory primitives) then the journaling mode will be unchanged and the string returned from the primitive will be the prior journaling mode (for example "delete").
Automatic Checkpoint
By default,sqlite will automatically checkpoint whenever aCOMMIToccurs that causes the WAL file to be 1000 pages or more in size,or when the last database connection on a database file closes. The default configuration is intended to work well for most applications. But programs that want more control can force a checkpoint using thewal_checkpoint pragmaor by calling thesqlite3_wal_checkpoint()C interface. The automatic checkpoint threshold can be changed or automatic checkpointing can be completely disabled using thewal_autocheckpoint pragmaor by calling thesqlite3_wal_autocheckpoint()C interface. A program can also usesqlite3_wal_hook()to register a callback to be invoked whenever any transaction commits to the WAL. This callback can then invokesqlite3_wal_checkpoint()to for a checkpoint based on whatever criteria it thinks is appropriate. (The automatic checkpoint mechanism is implemented as a simple wrapper aroundsqlite3_wal_hook().)
Persistence of WAL mode
Unlike the other journaling modes,PRAGMA journal_mode=WALis persistent. If a process sets WAL mode,then closes and reopens the database,the database will come back in WAL mode. In contrast,if a process sets (for example) PRAGMA journal_mode=TRUNCATE and then closes and reopens the database will come back up in the default rollback mode of DELETE rather than the prevIoUs TRUNCATE setting.
The persistence of WAL mode means that applications can be converted to using sqlite in WAL mode without making any changes to the application itself. One has merely to run "PRAGMA journal_mode=WAL;" on the database file(s) using thecommand-line shellor other utility,then restart the application.
The WAL journal mode will be set on all connections to the same database file if it is set on any one connection.
Read-Only Databases
No sqlite database (regardless of whether or not it is WAL mode) is readable if it is located on read-only media and it requires recovery. So,for example,if an application crashes and leaves an sqlite database with ahot journal,that database cannot be opened unless the opening process has write privilege on the database file,the directory containing the database file,and the hot journal. This is because the incomplete transaction left over from the crash must be rolled back prior to reading the database and that rollback cannot occur without write permission on all files and the directory containing them.
A database in WAL mode cannot generally be opened from read-only media because even ordinary reads in WAL mode require recovery-like operations.
An efficient implementation of theWAL read algorithmrequires that there exist a hash table in shared memory over the content of the WAL file. This hash table is called thewal-index. The wal-index is in shared memory,and so technically it does not have to have a name in the host computer filesystem. CustomVFSimplementations are free to implement shared memory in any way they see fit,but the default unix and windows drivers that come built-in with sqlite implement shared memory usingmmapped filesnamed using the suffix "-shm" and located in the same directory as the database file. The wal-index must be rebuilt upon first access,even by readers,and so in order to open the WAL database,write access is required on the "-shm" shared memory file if the file exists,or else write access is required on the directory containing the database so that the wal-index can be created if it does not already exist. This does not preclude custom VFS implementations that implement shared memory differently from being able to access read-only WAL databases,but it does prevent the default unix and windows backends from accessing WAL databases on read-only media.
Hence,sqlite databases should always be converted toPRAGMA journal_mode=DELETEprior to being transferred to read-only media.
Also,if multiple processes are to access a WAL mode database,then all processes should run under user or group IDs that give them write access to the database files,the WAL file,the shared memory-shmfile,and the containing directory.
Implementation Of Shared-Memory For The WAL-Index
Thewal-indexis implemented using an ordinary file that is mmapped for robustness. Early (pre-release) implementations of WAL mode stored the wal-index in volatile shared-memory,such as files created in /dev/shm on Linux or /tmp on other unix systems. The problem with that approach is that processes with a different root directory (changed viachroot) will see different files and hence use different shared memory areas,leading to database corruption. Other methods for creating nameless shared memory blocks are not portable across the varIoUs flavors of unix. And we could not find any method to create nameless shared memory blocks on windows. The only way we have found to guarantee that all processes accessing the same database file use the same shared memory is to create the shared memory by mmapping a file in the same directory as the database itself.
Using an ordinary disk file to provide shared memory has the disadvantage that it might actually do unnecessary disk I/O by writing the shared memory to disk. However,the developers do not think this is a major concern since the wal-index rarely exceeds 32 KiB in size and is never synced. Furthermore,the wal-index backing file is deleted when the last database connection disconnects,which often prevents any real disk I/O from ever happening.
Specialized applications for which the default implementation of shared memory is unacceptable can devise alternative methods via a customVFS. For example,if it is known that a particular database will only be accessed by threads within a single process,the wal-index can be implemented using heap memory instead of true shared memory.
Use of WAL Without Shared-Memory
Beginning in sqlite version 3.7.4,WAL databases can be created,read,and written even if shared memory is unavailable as long as thelocking_modeis set to EXCLUSIVE before the first attempted access. In other words,a process can interact with a WAL database without using shared memory if that process is guaranteed to be the only process accessing the database. This feature allows WAL databases to be created,and written by legacyVFSesthat lack the "version 2" shared-memory methods xShmMap,xShmLock,xShmBarrier,and xShmUnmap on thesqlite3_io_methodsobject.
If EXCLUSIVE locking mode is set prior to the first WAL-mode database access,then sqlite never attempts to call any of the shared-memory methods and hence no shared-memory wal-index is ever created. In that case,the database connection remains in EXCLUSIVE mode as long as the journal mode is WAL; attempts to change the locking mode using "PRAGMA locking_mode=NORMAL;" are no-ops. The only way to change out of EXCLUSIVE locking mode is to first change out of WAL journal mode.
If NORMAL locking mode is in effect for the first WAL-mode database access,then the shared-memory wal-index is created. This means that the underlying VFS must support the "version 2" shared-memory. If the VFS does not support shared-memory methods,then the attempt to open a database that is already in WAL mode,or the attempt convert a database into WAL mode,will fail. As long a connection is using a shared-memory wal-index,the locking mode can be changed freely between NORMAL and EXCLUSIVE. It is only when the shared-memory wal-index is omitted,when the locking mode is EXCLUSIVE prior to the first WAL-mode database access,that the locking mode is stuck in EXCLUSIVE.
Backwards Compatibility
The database file format is unchanged for WAL mode. However,the WAL file and thewal-indexare new concepts and so older versions of sqlite will not know how to recover a crashed sqlite database that was operating in WAL mode when the crash occurred. To prevent older versions of sqlite from trying to recover a WAL-mode database (and making matters worse) the database file format version numbers (bytes 18 and 19 in thedatabase header) are increased from 1 to 2 in WAL mode. Thus,if an older version of sqlite attempts to connect to an sqlite database that is operating in WAL mode,it will report an error along the lines of "file is encrypted or is not a database".
One can explicitly change out of WAL mode using a pragma such as this:
PRAGMA journal_mode=DELETE;
Deliberately changing out of WAL mode changes the database file format version numbers back to 1 so that older versions of sqlite can once again access the database file.
原文链接:https://www.f2er.com/sqlite/202741.html