@polycode-projects/the-mechanical-code-talker 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/corpus/LICENSES.json +19 -4
  2. package/corpus/README.md +48 -0
  3. package/corpus/generated/README.md +24 -9
  4. package/corpus/generated/ace-surface-variants.jsonl +4 -1
  5. package/corpus/generated/manifest.json +4 -4
  6. package/corpus/prose/manifest.json +512 -0
  7. package/corpus/prose/sqlite/LICENSE-NOTICE +53 -0
  8. package/corpus/prose/sqlite/arch.txt +213 -0
  9. package/corpus/prose/sqlite/atomiccommit.txt +1117 -0
  10. package/corpus/prose/sqlite/faq.txt +473 -0
  11. package/corpus/prose/sqlite/fileformat.txt +1589 -0
  12. package/corpus/prose/sqlite/lang_createtable.txt +1339 -0
  13. package/corpus/prose/sqlite/lang_insert.txt +580 -0
  14. package/corpus/prose/sqlite/lang_select.txt +3293 -0
  15. package/corpus/prose/sqlite/optoverview.txt +908 -0
  16. package/corpus/prose/sqlite/queryplanner.txt +447 -0
  17. package/corpus/prose/sqlite/transactional.txt +41 -0
  18. package/corpus/prose/sqlite/wal.txt +567 -0
  19. package/corpus/prose/sqlite/whentouse.txt +300 -0
  20. package/corpus/prose/wikipedia/Apple.txt +4 -0
  21. package/corpus/prose/wikipedia/Attempto_Controlled_English.txt +169 -0
  22. package/corpus/prose/wikipedia/Automated_planning_and_scheduling.txt +67 -0
  23. package/corpus/prose/wikipedia/Bee.txt +7 -0
  24. package/corpus/prose/wikipedia/Bird.txt +8 -0
  25. package/corpus/prose/wikipedia/Bone.txt +4 -0
  26. package/corpus/prose/wikipedia/Book.txt +7 -0
  27. package/corpus/prose/wikipedia/Bread.txt +6 -0
  28. package/corpus/prose/wikipedia/Butterfly.txt +6 -0
  29. package/corpus/prose/wikipedia/Car.txt +1 -0
  30. package/corpus/prose/wikipedia/Cat.txt +1 -0
  31. package/corpus/prose/wikipedia/Child.txt +3 -0
  32. package/corpus/prose/wikipedia/City.txt +2 -0
  33. package/corpus/prose/wikipedia/Clock.txt +2 -0
  34. package/corpus/prose/wikipedia/Cooking.txt +1 -0
  35. package/corpus/prose/wikipedia/Description_logic.txt +660 -0
  36. package/corpus/prose/wikipedia/Doctor.txt +6 -0
  37. package/corpus/prose/wikipedia/Dog.txt +4 -0
  38. package/corpus/prose/wikipedia/Eagle.txt +4 -0
  39. package/corpus/prose/wikipedia/Emotion.txt +9 -0
  40. package/corpus/prose/wikipedia/Eye.txt +5 -0
  41. package/corpus/prose/wikipedia/Family.txt +3 -0
  42. package/corpus/prose/wikipedia/Farm.txt +4 -0
  43. package/corpus/prose/wikipedia/Fear.txt +4 -0
  44. package/corpus/prose/wikipedia/First-order_logic.txt +1518 -0
  45. package/corpus/prose/wikipedia/Fish.txt +10 -0
  46. package/corpus/prose/wikipedia/Flower.txt +3 -0
  47. package/corpus/prose/wikipedia/Food.txt +10 -0
  48. package/corpus/prose/wikipedia/Grass.txt +9 -0
  49. package/corpus/prose/wikipedia/Hand.txt +2 -0
  50. package/corpus/prose/wikipedia/Happiness.txt +3 -0
  51. package/corpus/prose/wikipedia/Heart.txt +4 -0
  52. package/corpus/prose/wikipedia/Horse.txt +4 -0
  53. package/corpus/prose/wikipedia/House.txt +6 -0
  54. package/corpus/prose/wikipedia/Human.txt +4 -0
  55. package/corpus/prose/wikipedia/Insect.txt +6 -0
  56. package/corpus/prose/wikipedia/Interactive_fiction.txt +112 -0
  57. package/corpus/prose/wikipedia/Knowledge.txt +5 -0
  58. package/corpus/prose/wikipedia/Knowledge_representation_and_reasoning.txt +87 -0
  59. package/corpus/prose/wikipedia/LICENSE-NOTICE +94 -0
  60. package/corpus/prose/wikipedia/Language.txt +10 -0
  61. package/corpus/prose/wikipedia/Learning.txt +4 -0
  62. package/corpus/prose/wikipedia/Mammal.txt +3 -0
  63. package/corpus/prose/wikipedia/Memory.txt +5 -0
  64. package/corpus/prose/wikipedia/Milk.txt +1 -0
  65. package/corpus/prose/wikipedia/Mountain.txt +1 -0
  66. package/corpus/prose/wikipedia/Natural_language_processing.txt +211 -0
  67. package/corpus/prose/wikipedia/Ostrich.txt +2 -0
  68. package/corpus/prose/wikipedia/Owl.txt +2 -0
  69. package/corpus/prose/wikipedia/Penguin.txt +2 -0
  70. package/corpus/prose/wikipedia/Plant.txt +5 -0
  71. package/corpus/prose/wikipedia/Rain.txt +1 -0
  72. package/corpus/prose/wikipedia/Resource_Description_Framework.txt +184 -0
  73. package/corpus/prose/wikipedia/River.txt +1 -0
  74. package/corpus/prose/wikipedia/School.txt +8 -0
  75. package/corpus/prose/wikipedia/Sea.txt +1 -0
  76. package/corpus/prose/wikipedia/Semantic_Web.txt +114 -0
  77. package/corpus/prose/wikipedia/Semantic_reasoner.txt +29 -0
  78. package/corpus/prose/wikipedia/Snow.txt +5 -0
  79. package/corpus/prose/wikipedia/Sun.txt +5 -0
  80. package/corpus/prose/wikipedia/Teacher.txt +4 -0
  81. package/corpus/prose/wikipedia/Team.txt +3 -0
  82. package/corpus/prose/wikipedia/Text-based_game.txt +17 -0
  83. package/corpus/prose/wikipedia/Tool.txt +4 -0
  84. package/corpus/prose/wikipedia/Tree.txt +7 -0
  85. package/corpus/prose/wikipedia/Weather.txt +4 -0
  86. package/corpus/prose/wikipedia/Web_Ontology_Language.txt +133 -0
  87. package/corpus/prose/wikipedia/Wind.txt +8 -0
  88. package/corpus/prose/wikipedia/Writing.txt +5 -0
  89. package/package.json +2 -1
@@ -0,0 +1,1589 @@
1
+ Database File Format
2
+ Small. Fast. Reliable.
3
+ Choose any three.
4
+ Home
5
+ Menu
6
+ About
7
+ Documentation
8
+ Download
9
+ License
10
+ Support
11
+ Purchase
12
+ Search
13
+ About
14
+ Documentation
15
+ Download
16
+ Support
17
+ Purchase
18
+ Search Documentation
19
+ Search Changelog
20
+ Database File Format
21
+ Table Of Contents
22
+ 1. The Database File
23
+ 1.1. Hot Journals
24
+ 1.2. Pages
25
+ 1.3. The Database Header
26
+ 1.3.1. Magic Header String
27
+ 1.3.2. Page Size
28
+ 1.3.3. File format version numbers
29
+ 1.3.4. Reserved bytes per page
30
+ 1.3.5. Payload fractions
31
+ 1.3.6. File change counter
32
+ 1.3.7. In-header database size
33
+ 1.3.8. Free page list
34
+ 1.3.9. Schema cookie
35
+ 1.3.10. Schema format number
36
+ 1.3.11. Suggested cache size
37
+ 1.3.12. Incremental vacuum settings
38
+ 1.3.13. Text encoding
39
+ 1.3.14. User version number
40
+ 1.3.15. Application ID
41
+ 1.3.16. Write library version number and version-valid-for number
42
+ 1.3.17. Header space reserved for expansion
43
+ 1.4. The Lock-Byte Page
44
+ 1.5. The Freelist
45
+ 1.6. B-tree Pages
46
+ 1.7. Cell Payload Overflow Pages
47
+ 1.8. Pointer Map or Ptrmap Pages
48
+ 2. Schema Layer
49
+ 2.1. Record Format
50
+ 2.2. Record Sort Order
51
+ 2.3. Representation Of SQL Tables
52
+ 2.4. Representation of WITHOUT ROWID Tables
53
+ 2.4.1. Suppression of redundant columns in the PRIMARY KEY
54
+ of WITHOUT ROWID tables
55
+ 2.5. Representation Of SQL Indices
56
+ 2.5.1. Suppression of redundant columns in WITHOUT ROWID secondary indexes
57
+ 2.6. Storage Of The SQL Database Schema
58
+ 2.6.1. Alternative Names For The Schema Table
59
+ 2.6.2. Internal Schema Objects
60
+ 2.6.3. The sqlite_sequence table
61
+ 2.6.4. The sqlite_stat1 table
62
+ 2.6.5. The sqlite_stat2 table
63
+ 2.6.6. The sqlite_stat3 table
64
+ 2.6.7. The sqlite_stat4 table
65
+ 3. The Rollback Journal
66
+ 4. The Write-Ahead Log
67
+ 4.1. WAL File Format
68
+ 4.2. Checksum Algorithm
69
+ 4.3. Checkpoint Algorithm
70
+ 4.4. WAL Reset
71
+ 4.5. Reader Algorithm
72
+ 4.6. WAL-Index Format
73
+ This document describes and defines the on-disk database file
74
+ format used by all releases of SQLite since
75
+ version 3.0.0 (2004-06-18).
76
+ 1. The Database File
77
+ The complete state of an SQLite database is usually
78
+ contained in a single file on disk called the "main database file".
79
+ During a transaction, SQLite stores additional information
80
+ in a second file called the "rollback journal", or if SQLite is in
81
+ WAL mode , a write-ahead log file.
82
+ 1.1. Hot Journals
83
+ If the application or
84
+ host computer crashes before the transaction completes, then the rollback
85
+ journal or write-ahead log contains information needed
86
+ to restore the main database file to a consistent state. When a rollback
87
+ journal or write-ahead log contains information necessary for recovering
88
+ the state of the database, they are called a "hot journal" or "hot WAL file".
89
+ Hot journals and WAL files are only a factor during error recovery
90
+ scenarios and so are uncommon, but they are part of the state of an SQLite
91
+ database and so cannot be ignored. This document defines the format
92
+ of a rollback journal and the write-ahead log file, but the focus is
93
+ on the main database file.
94
+ 1.2. Pages
95
+ The main database file consists of one or more pages. The size of a
96
+ page is a power of two between 512 and 65536 inclusive. All pages within
97
+ the same database are the same size. The page size for a database file
98
+ is determined by the 2-byte integer located at an offset of
99
+ 16 bytes from the beginning of the database file.
100
+ Pages are numbered beginning with 1. The maximum page number is
101
+ 4294967294 (2 32 - 2). The minimum size
102
+ SQLite database is a single 512-byte page.
103
+ The maximum size database would be 4294967294 pages at 65536 bytes per
104
+ page or 281,474,976,579,584 bytes (about 281 terabytes). Usually SQLite will
105
+ hit the maximum file size limit of the underlying filesystem or disk
106
+ hardware long before it hits its own internal size limit.
107
+ In common use, SQLite databases tend to range in size from a few kilobytes
108
+ to a few gigabytes, though terabyte-size SQLite databases are known to exist
109
+ in production.
110
+ At any point in time, every page in the main database has a single
111
+ use which is one of the following:
112
+ A b-tree page
113
+ A table b-tree interior page
114
+ A table b-tree leaf page
115
+ An index b-tree interior page
116
+ An index b-tree leaf page
117
+ A freelist page
118
+ A freelist trunk page
119
+ A freelist leaf page
120
+ A payload overflow page
121
+ A pointer map page
122
+ The lock-byte page
123
+ All reads from and writes to the main database file begin at a page
124
+ boundary and all writes are an integer number of pages in size. Reads
125
+ are also usually an integer number of pages in size, with the one exception
126
+ that when the database is first opened, the first 100 bytes of the
127
+ database file (the database file header) are read as a sub-page size unit.
128
+ 1.3. The Database Header
129
+ The first 100 bytes of the database file comprise the database file
130
+ header. The database file header is divided into fields as shown by
131
+ the table below. All multibyte fields in the database file header are
132
+ stored with the most significant byte first (big-endian).
133
+ Database Header Format
134
+ Offset Size Description
135
+ 0 16
136
+ The header string: "SQLite format 3\000"
137
+ 16 2
138
+ The database page size in bytes. Must be a power of two between 512
139
+ and 32768 inclusive, or the value 1 representing a page size of 65536.
140
+ 18 1
141
+ File format write version. 1 for legacy; 2 for WAL .
142
+ 19 1
143
+ File format read version. 1 for legacy; 2 for WAL .
144
+ 20 1
145
+ Bytes of unused "reserved" space at the end of each page. Usually 0.
146
+ 21 1
147
+ Maximum embedded payload fraction. Must be 64.
148
+ 22 1
149
+ Minimum embedded payload fraction. Must be 32.
150
+ 23 1
151
+ Leaf payload fraction. Must be 32.
152
+ 24 4
153
+ File change counter.
154
+ 28 4
155
+ Size of the database file in pages. The "in-header database size".
156
+ 32 4
157
+ Page number of the first freelist trunk page.
158
+ 36 4
159
+ Total number of freelist pages.
160
+ 40 4
161
+ The schema cookie.
162
+ 44 4
163
+ The schema format number. Supported schema formats are 1, 2, 3, and 4.
164
+ 48 4
165
+ Default page cache size.
166
+ 52 4
167
+ The page number of the largest root b-tree page when in auto-vacuum or
168
+ incremental-vacuum modes, or zero otherwise.
169
+ 56 4
170
+ The database text encoding. A value of 1 means UTF-8. A value of 2
171
+ means UTF-16le. A value of 3 means UTF-16be.
172
+ 60 4
173
+ The "user version" as read and set by the user_version pragma .
174
+ 64 4
175
+ True (non-zero) for incremental-vacuum mode. False (zero) otherwise.
176
+ 68 4
177
+ The "Application ID" set by PRAGMA application_id .
178
+ 72 20
179
+ Reserved for expansion. Must be zero.
180
+ 92 4
181
+ The version-valid-for number .
182
+ 96 4
183
+ SQLITE_VERSION_NUMBER
184
+ 1.3.1. Magic Header String
185
+ Every valid SQLite database file begins with the following 16 bytes
186
+ (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00. This byte sequence
187
+ corresponds to the UTF-8 string "SQLite format 3" including the nul
188
+ terminator character at the end.
189
+ 1.3.2. Page Size
190
+ The two-byte value beginning at offset 16 determines the page size of
191
+ the database. For SQLite versions 3.7.0.1 (2010-08-04)
192
+ and earlier, this value is
193
+ interpreted as a big-endian integer and must be a power of two between
194
+ 512 and 32768, inclusive. Beginning with SQLite version 3.7.1
195
+ (2010-08-23), a page
196
+ size of 65536 bytes is supported. The value 65536 will not fit in a
197
+ two-byte integer, so to specify a 65536-byte page size, the value
198
+ at offset 16 is 0x00 0x01.
199
+ This value can be interpreted as a big-endian
200
+ 1 and thought of as a magic number to represent the 65536 page size.
201
+ Or one can view the two-byte field as a little endian number and say
202
+ that it represents the page size divided by 256. These two
203
+ interpretations of the page-size field are equivalent.
204
+ 1.3.3. File format version numbers
205
+ The file format write version and file format read version at offsets
206
+ 18 and 19 are intended to allow for enhancements of the file format
207
+ in future versions of SQLite. In current versions of SQLite, both of
208
+ these values are 1 for rollback journalling modes and 2 for WAL
209
+ journalling mode. If a version of SQLite coded to the current
210
+ file format specification encounters a database file where the read
211
+ version is 1 or 2 but the write version is greater than 2, then the database
212
+ file must be treated as read-only. If a database file with a read version
213
+ greater than 2 is encountered, then that database cannot be read or written.
214
+ 1.3.4. Reserved bytes per page
215
+ SQLite has the ability to set aside a small number of extra bytes at
216
+ the end of every page for use by extensions. These extra bytes are
217
+ used, for example, by the SQLite Encryption Extension to store a nonce
218
+ and/or cryptographic checksum associated with each page. The
219
+ "reserved space" size in the 1-byte integer at offset 20 is the number
220
+ of bytes of space at the end of each page to reserve for extensions.
221
+ This value is usually 0. The value can be odd.
222
+ The "usable size" of a database page is the page size specified by the
223
+ 2-byte integer at offset 16 in the header less the "reserved" space size
224
+ recorded in the 1-byte integer at offset 20 in the header. The usable
225
+ size of a page might be an odd number. However, the usable size is not
226
+ allowed to be less than 480. In other words, if the page size is 512,
227
+ then the reserved space size cannot exceed 32.
228
+ 1.3.5. Payload fractions
229
+ The maximum and minimum embedded payload fractions and the leaf
230
+ payload fraction values must be 64, 32, and 32. These values were
231
+ originally intended to be tunable parameters that could be used to
232
+ modify the storage format of the b-tree algorithm. However, that
233
+ functionality is not supported and there are no current plans to add
234
+ support in the future. Hence, these three bytes are fixed at the
235
+ values specified.
236
+ 1.3.6. File change counter
237
+ The file change counter is a 4-byte big-endian integer at
238
+ offset 24 that is incremented whenever the database file is unlocked
239
+ after having been modified.
240
+ When two or more processes are reading the same database file, each
241
+ process can detect database changes from other processes by monitoring
242
+ the change counter.
243
+ A process will normally want to flush its database page cache when
244
+ another process modified the database, since the cache has become stale.
245
+ The file change counter facilitates this.
246
+ In WAL mode, changes to the database are detected using the wal-index
247
+ and so the change counter is not needed. Hence, the change counter might
248
+ not be incremented on each transaction in WAL mode.
249
+ 1.3.7. In-header database size
250
+ The 4-byte big-endian integer at offset 28 into the header
251
+ stores the size of the database file in pages. If this in-header
252
+ datasize size is not valid (see the next paragraph), then the database
253
+ size is computed by looking
254
+ at the actual size of the database file. Older versions of SQLite
255
+ ignored the in-header database size and used the actual file size
256
+ exclusively. Newer versions of SQLite use the in-header database
257
+ size if it is available but fall back to the actual file size if
258
+ the in-header database size is not valid.
259
+ The in-header database size is only considered to be valid if
260
+ it is non-zero and if the 4-byte change counter at offset 24
261
+ exactly matches the 4-byte version-valid-for number at offset 92.
262
+ The in-header database size is always valid
263
+ when the database is only modified using recent versions of SQLite,
264
+ versions 3.7.0 (2010-07-21) and later.
265
+ If a legacy version of SQLite writes to the database, it will not
266
+ know to update the in-header database size and so the in-header
267
+ database size could be incorrect. But legacy versions of SQLite
268
+ will also leave the version-valid-for number at offset 92 unchanged
269
+ so it will not match the change-counter. Hence, invalid in-header
270
+ database sizes can be detected (and ignored) by observing when
271
+ the change-counter does not match the version-valid-for number.
272
+ 1.3.8. Free page list
273
+ Unused pages in the database file are stored on a freelist. The
274
+ 4-byte big-endian integer at offset 32 stores the page number of
275
+ the first page of the freelist, or zero if the freelist is empty.
276
+ The 4-byte big-endian integer at offset 36 stores the total
277
+ number of pages on the freelist.
278
+ 1.3.9. Schema cookie
279
+ The schema cookie is a 4-byte big-endian integer at offset 40
280
+ that is incremented whenever the database schema changes. A
281
+ prepared statement is compiled against a specific version of the
282
+ database schema. When the database schema changes, the statement
283
+ must be reprepared. When a prepared statement runs, it first checks
284
+ the schema cookie to ensure the value is the same as when the statement
285
+ was prepared and if the schema cookie has changed, the statement either
286
+ automatically reprepares and reruns or it aborts with an SQLITE_SCHEMA
287
+ error.
288
+ 1.3.10. Schema format number
289
+ The schema format number is a 4-byte big-endian integer at offset 44.
290
+ The schema format number is similar to the file format read and write
291
+ version numbers at offsets 18 and 19 except that the schema format number
292
+ refers to the high-level SQL formatting rather than the low-level b-tree
293
+ formatting. Four schema format numbers are currently defined:
294
+ Format 1 is understood by all versions of SQLite back to
295
+ version 3.0.0 (2004-06-18).
296
+ Format 2 adds the ability of rows within the same table
297
+ to have a varying number of columns, in order to support the
298
+ ALTER TABLE ... ADD COLUMN functionality. Support for
299
+ reading and writing format 2 was added in SQLite
300
+ version 3.1.3 on 2005-02-20.
301
+ Format 3 adds the ability of extra columns added by
302
+ ALTER TABLE ... ADD COLUMN to have non-NULL default
303
+ values. This capability was added in SQLite version 3.1.4
304
+ on 2005-03-11.
305
+ Format 4 causes SQLite to respect the
306
+ DESC keyword on
307
+ index declarations. (The DESC keyword is ignored in indexes for
308
+ formats 1, 2, and 3.)
309
+ Format 4 also adds two new boolean record type values ( serial types
310
+ 8 and 9). Support for format 4 was added in SQLite 3.3.0 on
311
+ 2006-01-10.
312
+ New database files created by SQLite use format 4 by default.
313
+ The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option for the
314
+ sqlite3_db_config() C-language interface can be used to cause SQLite
315
+ to create new database files using format 1. The format version number
316
+ can be made to default to 1 instead of 4 by setting
317
+ SQLITE_DEFAULT_FILE_FORMAT =1 at compile-time.
318
+ If the database is completely empty, if it has no schema, then the
319
+ schema format number can be zero.
320
+ 1.3.11. Suggested cache size
321
+ The 4-byte big-endian signed integer at offset 48 is the suggested
322
+ cache size in pages for the database file. The value is a suggestion
323
+ only and SQLite is under no obligation to honor it. The absolute value
324
+ of the integer is used as the suggested size. The suggested cache size
325
+ can be set using the default_cache_size pragma .
326
+ 1.3.12. Incremental vacuum settings
327
+ The two 4-byte big-endian integers at offsets 52 and 64 are used
328
+ to manage the auto_vacuum and incremental_vacuum modes. If
329
+ the integer at offset 52 is zero then pointer-map (ptrmap) pages are
330
+ omitted from the database file and neither auto_vacuum nor
331
+ incremental_vacuum are supported. If the integer at offset 52 is
332
+ non-zero then it is the page number of the largest root page in the
333
+ database file, the database file will contain ptrmap pages, and the
334
+ mode must be either auto_vacuum or incremental_vacuum. In this latter
335
+ case, the integer at offset 64 is true for incremental_vacuum and
336
+ false for auto_vacuum. If the integer at offset 52 is zero then
337
+ the integer at offset 64 must also be zero.
338
+ 1.3.13. Text encoding
339
+ The 4-byte big-endian integer at offset 56 determines the encoding
340
+ used for all text strings stored in the database.
341
+ A value of 1 means UTF-8.
342
+ A value of 2 means UTF-16le.
343
+ A value of 3 means UTF-16be.
344
+ No other values are allowed.
345
+ The sqlite3.h header file defines C-preprocessor macros SQLITE_UTF8 as 1,
346
+ SQLITE_UTF16LE as 2, and SQLITE_UTF16BE as 3, to use in place of
347
+ the numeric codes for the text encoding.
348
+ 1.3.14. User version number
349
+ The 4-byte big-endian integer at offset 60 is the user version which
350
+ is set and queried by the user_version pragma . The user version is
351
+ not used by SQLite.
352
+ 1.3.15. Application ID
353
+ The 4-byte big-endian integer at offset 68 is an "Application ID" that
354
+ can be set by the PRAGMA application_id command in order to identify the
355
+ database as belonging to or associated with a particular application.
356
+ The application ID is intended for database files used as an
357
+ application file-format . The application ID can be used by utilities
358
+ such as file(1) to determine the specific
359
+ file type rather than just reporting "SQLite3 Database". A list of
360
+ assigned application IDs can be seen by consulting the
361
+ magic.txt
362
+ file in the SQLite source repository.
363
+ 1.3.16. Write library version number and version-valid-for number
364
+ The 4-byte big-endian integer at offset 96 stores the
365
+ SQLITE_VERSION_NUMBER value for the SQLite library that most
366
+ recently modified the database file. The 4-byte big-endian integer at
367
+ offset 92 is the value of the change counter when the version number
368
+ was stored. The integer at offset 92 indicates which transaction
369
+ the version number is valid for and is sometimes called the
370
+ "version-valid-for number".
371
+ 1.3.17. Header space reserved for expansion
372
+ All other bytes of the database file header are reserved for
373
+ future expansion and must be set to zero.
374
+ 1.4. The Lock-Byte Page
375
+ The lock-byte page is the single page of the database file
376
+ that contains the bytes at offsets between 1073741824 and 1073742335,
377
+ inclusive. A database file that is less than or equal to 1073741824 bytes
378
+ in size contains no lock-byte page. A database file larger than
379
+ 1073741824 contains exactly one lock-byte page.
380
+ The lock-byte page is set aside for use by the operating-system specific
381
+ VFS implementation in implementing the database file locking primitives.
382
+ SQLite does not use the lock-byte page. The SQLite core
383
+ will never read or write the lock-byte page,
384
+ though operating-system specific VFS
385
+ implementations may choose to read or write bytes on the lock-byte
386
+ page according to the
387
+ needs and proclivities of the underlying system. The unix and win32
388
+ VFS implementations that come built into SQLite do not write to the
389
+ lock-byte page, but third-party VFS implementations for
390
+ other operating systems might.
391
+ The lock-byte page arose from the need to support Win95 which was the
392
+ predominant operating system when this file format was designed and which
393
+ only supported mandatory file locking. All modern operating systems that
394
+ we know of support advisory file locking, and so the lock-byte page is
395
+ not really needed any more, but is retained for backwards compatibility.
396
+ 1.5. The Freelist
397
+ A database file might contain one or more pages that are not in
398
+ active use. Unused pages can come about, for example, when information
399
+ is deleted from the database. Unused pages are stored on the freelist
400
+ and are reused when additional pages are required.
401
+ The freelist is organized as a linked list of freelist trunk pages
402
+ with each trunk page containing page numbers for zero or more freelist
403
+ leaf pages.
404
+ A freelist trunk page consists of an array of 4-byte big-endian integers.
405
+ The size of the array is as many integers as will fit in the usable space
406
+ of a page. The minimum usable space is 480 bytes so the array will always
407
+ be at least 120 entries in length. The first integer on a freelist trunk
408
+ page is the page number of the next freelist trunk page in the list or zero
409
+ if this is the last freelist trunk page. The second integer on a freelist
410
+ trunk page is the number of leaf page pointers to follow.
411
+ Call the second integer on a freelist trunk page L.
412
+ If L is greater than zero then integers with array indexes between 2 and
413
+ L+1 inclusive contain page numbers for freelist leaf pages.
414
+ Freelist leaf pages contain no information. SQLite avoids reading or
415
+ writing freelist leaf pages in order to reduce disk I/O.
416
+ A bug in SQLite versions prior to 3.6.0 (2008-07-16)
417
+ caused the database to be
418
+ reported as corrupt if any of the last 6 entries in the freelist trunk page
419
+ array contained non-zero values. Newer versions of SQLite do not have
420
+ this problem. However, newer versions of SQLite still avoid using the
421
+ last six entries in the freelist trunk page array in order that database
422
+ files created by newer versions of SQLite can be read by older versions
423
+ of SQLite.
424
+ The number of freelist pages is stored as a 4-byte big-endian integer
425
+ in the database header at an offset of 36 from the beginning of the file.
426
+ The database header also stores the page number of the first freelist trunk
427
+ page as a 4-byte big-endian integer at an offset of 32 from the beginning
428
+ of the file.
429
+ 1.6. B-tree Pages
430
+ The b-tree algorithm provides key/data storage with unique and
431
+ ordered keys on page-oriented storage devices.
432
+ For background information on b-trees, see
433
+ Knuth, The Art Of Computer Programming , Volume 3 "Sorting
434
+ and Searching", pages 471-479. Two variants of b-trees are used by
435
+ SQLite. "Table b-trees" use a 64-bit signed integer key and store
436
+ all data in the leaves. "Index b-trees" use arbitrary keys and store no
437
+ data at all.
438
+ A b-tree page is either an interior page or a leaf page.
439
+ A leaf page contains keys and in the case of a table b-tree each
440
+ key has associated data. An interior page contains
441
+ K keys together with K+1 pointers to child b-tree pages.
442
+ A "pointer" in an interior b-tree page is just the 32-bit
443
+ unsigned integer page number of the child page.
444
+ The number of keys on an interior b-tree page, K,
445
+ is almost always at least 2 and is usually much more than 2.
446
+ The only exception is when page 1 is an interior b-tree page.
447
+ Page 1 has 100 fewer bytes of storage space available,
448
+ due to the presence of the database header at the beginning of that page,
449
+ and so sometimes (rarely) if page 1 is an interior b-tree page, it can
450
+ end up holding just a single key. In all other cases, K is 2 or more.
451
+ The upper bound on K is as many keys as will fit on the page. Large keys
452
+ on index b-trees are split up into overflow pages so that no single key
453
+ uses more than one fourth of the available storage space on the page
454
+ and hence every internal page is able to store at least 4 keys.
455
+ The integer keys of table b-trees are never large enough to
456
+ require overflow, so key overflow only occurs on index b-trees.
457
+ Define the depth
458
+ of a leaf b-tree to be 1 and the depth of any interior b-tree to be one
459
+ more than the maximum depth of any of its children. In a well-formed
460
+ database, all children of an interior b-tree have the same depth.
461
+ In an interior b-tree page, the pointers and keys logically alternate
462
+ with a pointer on both ends. (The previous sentence is to be understood
463
+ conceptually - the actual layout of the keys and
464
+ pointers within the page is more complicated and will be described in
465
+ the sequel.) All keys within the same page are unique and are logically
466
+ organized in ascending order from left to right. (Again, this ordering
467
+ is logical, not physical. The actual location of keys within the page
468
+ is arbitrary.) For any key X, pointers to the left
469
+ of a X refer to b-tree pages on which all keys are less than or equal to X.
470
+ Pointers to the right of X refer to pages where all keys are
471
+ greater than X.
472
+ Within an interior b-tree page, each key and the pointer to its
473
+ immediate left are combined into a structure called a "cell". The
474
+ right-most pointer is held separately. A leaf b-tree page has no
475
+ pointers, but it still uses the cell structure to hold keys for
476
+ index b-trees or keys and content for table b-trees. Data is also
477
+ contained in the cell.
478
+ Every b-tree page has at most one parent b-tree page.
479
+ A b-tree page without a parent is called a root page. A root b-tree page
480
+ together with the closure of its children form a complete b-tree.
481
+ It is possible (and in fact rather common) to have a complete b-tree
482
+ that consists of a single page that is both a leaf and the root.
483
+ Because there are pointers from parents to children, every page of a
484
+ complete b-tree can be located if only the root page is known. Hence,
485
+ b-trees are identified by their root page number.
486
+ A b-tree page is either a table b-tree page or an index b-tree page.
487
+ All pages within each complete b-tree are of the same type: either table
488
+ or index. There is one table b-tree in the database file
489
+ for each rowid table in the database schema, including system tables
490
+ such as sqlite_schema . There is one index b-tree
491
+ in the database file for each index in the schema, including implied indexes
492
+ created by uniqueness constraints. There are no b-trees associated with
493
+ virtual tables . Specific virtual table implementations might make use
494
+ of shadow tables for storage, but those shadow tables will have separate
495
+ entries in the database schema. WITHOUT ROWID tables use index b-trees
496
+ rather than table b-trees, so there is one
497
+ index b-tree in the database file for each WITHOUT ROWID table.
498
+ The b-tree corresponding to the sqlite_schema table is always a table
499
+ b-tree and always has a root page of 1.
500
+ The sqlite_schema table contains the root page number for every other
501
+ table and index in the database file.
502
+ Each entry in a table b-tree consists of a 64-bit signed integer key
503
+ and up to 2147483647 bytes of arbitrary data. (The key of a table b-tree
504
+ corresponds to the rowid of the SQL table that the b-tree implements.)
505
+ Interior table b-trees hold only keys and pointers to children.
506
+ All data is contained in the table b-tree leaves.
507
+ Each entry in an index b-tree consists of an arbitrary key of up
508
+ to 2147483647 bytes in length and no data.
509
+ Define the "payload" of a cell to be the arbitrary length section
510
+ of the cell. For an index b-tree, the key is always arbitrary in length
511
+ and hence the payload is the key. There are no arbitrary length elements
512
+ in the cells of interior table b-tree pages and so those cells have no
513
+ payload. Table b-tree leaf pages contain arbitrary length content and
514
+ so for cells on those pages the payload is the content.
515
+ When the size of payload for a cell exceeds a certain threshold (to
516
+ be defined later) then only the first few bytes of the payload
517
+ are stored on the b-tree page and the balance is stored in a linked list
518
+ of content overflow pages.
519
+ A b-tree page is divided into regions in the following order:
520
+ The 100-byte database file header (found on page 1 only)
521
+ The 8 or 12 byte b-tree page header
522
+ The cell pointer array
523
+ Unallocated space
524
+ The cell content area
525
+ The reserved region
526
+ The 100-byte database file header is found only on page 1, which is
527
+ always a table b-tree page. All other b-tree pages in the database file
528
+ omit this 100-byte header.
529
+ The reserved region is an area of unused space at the end of every
530
+ page (except the locking page) that extensions can use to hold per-page
531
+ information. The size of the reserved region is determined by the one-byte
532
+ unsigned integer found at an offset of 20 into the database file header.
533
+ The size of the reserved region is usually zero.
534
+ The b-tree page header is 8 bytes in size for leaf pages and 12
535
+ bytes for interior pages. All multibyte values in the page header
536
+ are big-endian.
537
+ The b-tree page header is composed of the following fields:
538
+ B-tree Page Header Format
539
+ Offset Size Description
540
+ 0 1
541
+ The one-byte flag at offset 0 indicating the b-tree page type.
542
+ A value of 2 (0x02) means the page is an interior index b-tree page.
543
+ A value of 5 (0x05) means the page is an interior table b-tree page.
544
+ A value of 10 (0x0a) means the page is a leaf index b-tree page.
545
+ A value of 13 (0x0d) means the page is a leaf table b-tree page.
546
+ Any other value for the b-tree page type is an error.
547
+ 1 2
548
+ The two-byte integer at offset 1 gives the start of the
549
+ first freeblock on the page, or is zero if there are no freeblocks.
550
+ 3 2
551
+ The two-byte integer at offset 3 gives the number of cells on the page.
552
+ 5 2
553
+ The two-byte integer at offset 5 designates the start of the cell content
554
+ area. A zero value for this integer is interpreted as 65536.
555
+ 7 1
556
+ The one-byte integer at offset 7 gives the number of fragmented free
557
+ bytes within the cell content area.
558
+ 8 4
559
+ The four-byte page number at offset 8 is the right-most pointer. This
560
+ value appears in the header of interior b-tree pages only and is omitted from
561
+ all other pages.
562
+ The cell pointer array of a b-tree page immediately follows the b-tree
563
+ page header. Let K be the number of cells on the btree. The cell pointer
564
+ array consists of K 2-byte integer offsets to the cell contents. The
565
+ cell pointers are arranged in key order with left-most cell (the cell with the
566
+ smallest key) first and the right-most cell (the cell with the largest
567
+ key) last.
568
+ Cell content is stored in the cell content region of the b-tree page.
569
+ SQLite strives to place cells as far toward the end of the b-tree page as
570
+ it can, in order to leave space for future growth of the cell pointer array.
571
+ The area in between the last cell pointer array entry and the beginning of
572
+ the first cell is the unallocated region.
573
+ If a page contains no cells (which is only possible for a root page
574
+ of a table that contains no rows) then the offset to the
575
+ cell content area will equal the page size minus the bytes of reserved space.
576
+ If the database uses a 65536-byte page size and the reserved space is zero
577
+ (the usual value for reserved space) then the cell content offset of an
578
+ empty page wants to be 65536.
579
+ However, that integer is too large to be stored in a
580
+ 2-byte unsigned integer, so a value of 0 is used in its place.
581
+ A freeblock is a structure used to identify unallocated space within
582
+ a b-tree page. Freeblocks are organized as a chain. The first 2 bytes of
583
+ a freeblock are a big-endian integer which is the offset in the b-tree page
584
+ of the next freeblock in the chain, or zero if the freeblock is the last on
585
+ the chain. The third and fourth bytes of each freeblock form
586
+ a big-endian integer which is the size of the freeblock in bytes, including
587
+ the 4-byte header. Freeblocks are always connected in order
588
+ of increasing offset. The second field of the b-tree page header is the
589
+ offset of the first freeblock, or zero if there are no freeblocks on the
590
+ page. In a well-formed b-tree page, there will always be at least one cell
591
+ before the first freeblock.
592
+ A freeblock requires at least 4 bytes of space. If there is an isolated
593
+ group of 1, 2, or 3 unused bytes within the cell content area, those bytes
594
+ comprise a fragment. The total number of bytes in all fragments is stored
595
+ in the fifth field of the b-tree page header. In a well-formed b-tree page,
596
+ the total number of bytes in fragments may not exceed 60.
597
+ The total amount of free space on a b-tree page consists of the size
598
+ of the unallocated region plus the total size of all freeblocks plus the
599
+ number of fragmented free bytes. SQLite may from time to time reorganize
600
+ a b-tree page so that there are no freeblocks or fragment bytes, all
601
+ unused bytes are contained in the unallocated space region, and all
602
+ cells are packed tightly at the end of the page. This is called
603
+ "defragmenting" the b-tree page.
604
+ A variable-length integer or "varint" is a static Huffman encoding
605
+ of 64-bit twos-complement integers that uses less space for small positive
606
+ values.
607
+ A varint is between 1 and 9 bytes in length. The varint consists of either
608
+ zero or more bytes which have the high-order bit set followed by a single byte
609
+ with the high-order bit clear, or nine bytes, whichever is shorter.
610
+ The lower seven bits of each of the first eight bytes and all 8 bits of
611
+ the ninth byte are used to reconstruct the 64-bit twos-complement integer.
612
+ Varints are big-endian: bits taken from the earlier byte of the varint
613
+ are more significant than bits taken from the later bytes.
614
+ The format of a cell depends on which kind of b-tree page the cell
615
+ appears on. The following table shows the elements of a cell, in
616
+ order of appearance, for the various b-tree page types.
617
+ Table B-Tree Leaf Cell (header 0x0d):
618
+ A varint which is the total number of bytes of payload, including any
619
+ overflow
620
+ A varint which is the integer key, a.k.a. " rowid "
621
+ The initial portion of the payload that does not spill to overflow
622
+ pages.
623
+ A 4-byte big-endian integer page number for the first page of the
624
+ overflow page list - omitted if all payload fits on the b-tree page.
625
+ Table B-Tree Interior Cell (header 0x05):
626
+ A 4-byte big-endian page number which is the left child pointer.
627
+ A varint which is the integer key
628
+ Index B-Tree Leaf Cell (header 0x0a):
629
+ A varint which is the total number of bytes of key payload, including any
630
+ overflow
631
+ The initial portion of the payload that does not spill to overflow
632
+ pages.
633
+ A 4-byte big-endian integer page number for the first page of the
634
+ overflow page list - omitted if all payload fits on the b-tree page.
635
+ Index B-Tree Interior Cell (header 0x02):
636
+ A 4-byte big-endian page number which is the left child pointer.
637
+ A varint which is the total number of bytes of key payload, including any
638
+ overflow
639
+ The initial portion of the payload that does not spill to overflow
640
+ pages.
641
+ A 4-byte big-endian integer page number for the first page of the
642
+ overflow page list - omitted if all payload fits on the b-tree page.
643
+ The information above can be recast into a table format as follows:
644
+ B-tree Cell Format
645
+ Datatype
646
+ Appears in...
647
+ Description
648
+ Table Leaf (0x0d)
649
+ Table Interior (0x05)
650
+ Index Leaf (0x0a)
651
+ Index Interior (0x02)
652
+ 4-byte integer
653
+
654
+
655
+ Page number of left child
656
+ varint
657
+
658
+
659
+
660
+ Number of bytes of payload
661
+ varint
662
+
663
+
664
+ Rowid
665
+ byte array
666
+
667
+
668
+
669
+ Payload
670
+ 4-byte integer
671
+
672
+
673
+
674
+ Page number of first overflow page
675
+ The amount of payload that spills onto overflow pages also depends on
676
+ the page type. For the following computations, let U be the usable size
677
+ of a database page, the total page size less the reserved space at the
678
+ end of each page. And let P be the payload size. In the following,
679
+ symbol X represents the maximum amount of payload that can be stored directly
680
+ on the b-tree page without spilling onto an overflow page and symbol M
681
+ represents the minimum amount of payload that must be stored on the btree
682
+ page before spilling is allowed.
683
+ Table B-Tree Leaf Cell:
684
+ Let X be U-35. If the payload size P is less than or equal to X then
685
+ the entire payload is stored on the b-tree leaf page.
686
+ Let M be ((U-12)*32/255)-23 and let K be M+((P-M)%(U-4)).
687
+ If P is greater than X
688
+ then the number of bytes stored on the table b-tree leaf page is K
689
+ if K is less or equal to X or M otherwise.
690
+ The number of bytes stored on the leaf page is never less than M.
691
+ Table B-Tree Interior Cell:
692
+ Interior pages of table b-trees have no payload and so there is never
693
+ any payload to spill.
694
+ Index B-Tree Leaf Or Interior Cell:
695
+ Let X be ((U-12)*64/255)-23. If the payload size P is less than
696
+ or equal to X then the entire payload is stored on the b-tree page.
697
+ Let M be ((U-12)*32/255)-23 and let K be M+((P-M)%(U-4)).
698
+ If P is greater than X then the number
699
+ of bytes stored on the index b-tree page is K if K is less than or
700
+ equal to X or M otherwise.
701
+ The number of bytes stored on the index page is never less than M.
702
+ Here is an alternative description of the same computation:
703
+ X is U-35 for table btree leaf pages or
704
+ ((U-12)*64/255)-23 for index pages.
705
+ M is always ((U-12)*32/255)-23.
706
+ Let K be M+((P-M)%(U-4)).
707
+ If P<=X then all P bytes of payload are stored directly on the
708
+ btree page without overflow.
709
+ If P>X and K<=X then the first K bytes of P are stored on the
710
+ btree page and the remaining P-K bytes are stored on overflow pages.
711
+ If P>X and K>X then the first M bytes of P are stored on the
712
+ btree page and the remaining P-M bytes are stored on overflow pages.
713
+ The overflow thresholds are designed to give a minimum fanout of
714
+ 4 for index b-trees and to make sure enough of the payload
715
+ is on the b-tree page that the record header can usually be accessed
716
+ without consulting an overflow page. In hindsight, the designer of
717
+ the SQLite b-tree logic realized that these thresholds could have been
718
+ made much simpler. However, the computations cannot be changed
719
+ without resulting in an incompatible file format. And the current computations
720
+ work well, even if they are a little complex.
721
+ 1.7. Cell Payload Overflow Pages
722
+ When the payload of a b-tree cell is too large for the b-tree page,
723
+ the surplus is spilled onto overflow pages. Overflow pages form a linked
724
+ list. The first four bytes of each overflow page are a big-endian
725
+ integer which is the page number of the next page in the chain, or zero
726
+ for the final page in the chain. The fifth byte through the last usable
727
+ byte are used to hold overflow content.
728
+ 1.8. Pointer Map or Ptrmap Pages
729
+ Pointer map or ptrmap pages are extra pages inserted into the database
730
+ to make the operation of auto_vacuum and incremental_vacuum modes
731
+ more efficient. Other page types in the database typically have pointers
732
+ from parent to child. For example, an interior b-tree page contains pointers
733
+ to its child b-tree pages and an overflow chain has a pointer
734
+ from earlier to later links in the chain. A ptrmap page contains linkage
735
+ information going in the opposite direction, from child to parent.
736
+ Ptrmap pages must exist in any database file which has a non-zero
737
+ largest root b-tree page value at offset 52 in the database header.
738
+ If the largest root b-tree page value is zero, then the database must not
739
+ contain ptrmap pages.
740
+ In a database with ptrmap pages, the first ptrmap page is page 2.
741
+ A ptrmap page consists of an array of 5-byte entries. Let J be the
742
+ number of 5-byte entries that will fit in the usable space of a page.
743
+ (In other words, J=U/5.) The first ptrmap page will contain back pointer
744
+ information for pages 3 through J+2, inclusive. The second pointer map
745
+ page will be on page J+3 and that ptrmap page will provide back pointer
746
+ information for pages J+4 through 2*J+3 inclusive. And so forth for
747
+ the entire database file.
748
+ In a database that uses ptrmap pages, all pages at locations identified
749
+ by the computation in the previous paragraph must be ptrmap page and no
750
+ other page may be a ptrmap page. Except, if the byte-lock page happens to
751
+ fall on the same page number as a ptrmap page, then the ptrmap is moved
752
+ to the following page for that one case.
753
+ Each 5-byte entry on a ptrmap page provides back-link information about
754
+ one of the pages that immediately follow the pointer map. If page B is a
755
+ ptrmap page then back-link information about page B+1 is provided by
756
+ the first entry on the pointer map. Information about page B+2 is
757
+ provided by the second entry. And so forth.
758
+ Each 5-byte ptrmap entry consists of one byte of "page type" information
759
+ followed by a 4-byte big-endian page number. Five page types are recognized:
760
+ A b-tree root page. The
761
+ page number should be zero.
762
+ A freelist page. The page number should be
763
+ zero.
764
+ The first page of a
765
+ cell payload overflow chain. The page number is the b-tree page that
766
+ contains the cell whose content has overflowed.
767
+ A page in an overflow chain
768
+ other than the first page. The page number is the prior page of the
769
+ overflow chain.
770
+ A non-root b-tree page. The
771
+ page number is the parent b-tree page.
772
+ In any database file that contains ptrmap pages, all b-tree root pages
773
+ must come before any non-root b-tree page, cell payload overflow page, or
774
+ freelist page. This restriction ensures that a root page will never
775
+ be moved during an auto-vacuum or incremental-vacuum. The auto-vacuum
776
+ logic does not know how to update the root_page field of the sqlite_schema
777
+ table and so it is necessary to prevent root pages from being moved
778
+ during an auto-vacuum in order to preserve the integrity of the
779
+ sqlite_schema table. Root pages are moved to the beginning of the
780
+ database file by the CREATE TABLE, CREATE INDEX, DROP TABLE, and
781
+ DROP INDEX operations.
782
+ 2. Schema Layer
783
+ The foregoing text describes low-level aspects of the SQLite file
784
+ format. The b-tree mechanism provides a powerful and efficient means of
785
+ accessing a large data set. This section will describe how the
786
+ low-level b-tree layer is used to implement higher-level SQL
787
+ capabilities.
788
+ 2.1. Record Format
789
+ The data for a table b-tree leaf page and the key
790
+ of an index b-tree page was characterized above
791
+ as an arbitrary sequence of bytes.
792
+ The prior discussion mentioned one key being less than another, but
793
+ did not define what "less than" meant. The current section will address
794
+ these omissions.
795
+ Payload, either table b-tree data or index b-tree keys,
796
+ is always in the "record format".
797
+ The record format defines a sequence of values corresponding
798
+ to columns in a table or index. The record format specifies the number
799
+ of columns, the datatype of each column, and the content of each column.
800
+ The record format makes extensive use of the
801
+ variable-length integer or varint
802
+ representation of 64-bit signed integers defined above.
803
+ A record contains a header and a body, in that order.
804
+ The header begins with a single varint which determines the total number
805
+ of bytes in the header. The varint value is the size of the header in
806
+ bytes including the size varint itself. Following the size varint are
807
+ one or more additional varints, one per column. These additional varints
808
+ are called "serial type" numbers and
809
+ determine the datatype of each column, according to the following chart:
810
+ Serial Type Codes Of The Record Format
811
+ Serial Type Content Size Meaning
812
+ 0 0
813
+ Value is a NULL.
814
+ 1 1
815
+ Value is an 8-bit twos-complement integer.
816
+ 2 2
817
+ Value is a big-endian 16-bit twos-complement integer.
818
+ 3 3
819
+ Value is a big-endian 24-bit twos-complement integer.
820
+ 4 4
821
+ Value is a big-endian 32-bit twos-complement integer.
822
+ 5 6
823
+ Value is a big-endian 48-bit twos-complement integer.
824
+ 6 8
825
+ Value is a big-endian 64-bit twos-complement integer.
826
+ 7 8
827
+ Value is a big-endian IEEE 754-2008 64-bit floating point number.
828
+ 8 0
829
+ Value is the integer 0. (Only available for schema format 4 and higher.)
830
+ 9 0
831
+ Value is the integer 1. (Only available for schema format 4 and higher.)
832
+ 10,11
833
+ variable
834
+ Reserved for internal use. These serial type codes will
835
+ never appear in a well-formed database file, but they
836
+ might be used in transient and temporary database files
837
+ that SQLite sometimes generates for its own use.
838
+ The meanings of these codes can shift from one release
839
+ of SQLite to the next.
840
+ N≥12 and even
841
+ (N-12)/2
842
+ Value is a BLOB that is (N-12)/2 bytes in length.
843
+ N≥13 and odd
844
+ (N-13)/2
845
+ Value is a string in the text encoding and (N-13)/2 bytes in length.
846
+ The nul terminator is not stored.
847
+ The header size varint
848
+ and serial type varints will usually consist of a single byte. The
849
+ serial type varints for large strings and BLOBs might extend to two or three
850
+ byte varints, but that is the exception rather than the rule.
851
+ The varint format is very efficient at coding the record header.
852
+ The values for each column in the record immediately follow the header.
853
+ For serial types 0, 8, 9, 12, and 13, the value is zero bytes in
854
+ length. If all columns are of these types then the body section of the
855
+ record is empty.
856
+ A record might have fewer values than the number of columns in the
857
+ corresponding table. This can happen, for example, after an
858
+ ALTER TABLE ... ADD COLUMN SQL statement has increased
859
+ the number of columns in the table schema without modifying preexisting rows
860
+ in the table.
861
+ Missing values at the end of the record are filled in using the
862
+ default value for the corresponding columns defined in the table schema.
863
+ 2.2. Record Sort Order
864
+ The order of keys in an index b-tree is determined by the sort order of
865
+ the records that the keys represent. Record comparison progresses column
866
+ by column. Columns of a record are examined from left to right. The
867
+ first pair of columns that are not equal determines the relative order
868
+ of the two records. The sort order of individual columns is as
869
+ follows:
870
+ NULL values (serial type 0) sort first.
871
+ Numeric values (serial types 1 through 9) sort after NULLs
872
+ and in numeric order.
873
+ Text values (odd serial types 13 and larger) sort after numeric
874
+ values in the order determined by the columns collating function .
875
+ BLOB values (even serial types 12 and larger) sort last and in the order
876
+ determined by memcmp().
877
+ A collating function for each column is necessary in order to compute
878
+ the order of text fields.
879
+ SQLite defines three built-in collating functions:
880
+ BINARY
881
+ The built-in BINARY collation compares strings byte by byte
882
+ using the memcmp() function
883
+ from the standard C library.
884
+ NOCASE
885
+ The NOCASE collation is like BINARY except that uppercase
886
+ ASCII characters ('A' through 'Z')
887
+ are folded into their lowercase equivalents prior to running the
888
+ comparison. Only ASCII characters are case-folded.
889
+ NOCASE
890
+ does not implement a general purpose unicode caseless comparison.
891
+ RTRIM
892
+ RTRIM is like BINARY except that extra spaces at the end of either
893
+ string do not change the result. In other words, strings will
894
+ compare equal to one another as long as they
895
+ differ only in the number of spaces at the end.
896
+ Additional application-specific collating functions can be added to
897
+ SQLite using the sqlite3_create_collation() interface.
898
+ The default collating function for all strings is BINARY.
899
+ Alternative collating functions for table columns can be specified in the
900
+ CREATE TABLE statement using the COLLATE clause on the column definition .
901
+ When a column is indexed, the same collating function specified in the
902
+ CREATE TABLE statement is used for the column in the index, by default,
903
+ though this can be overridden using a COLLATE clause in the
904
+ CREATE INDEX statement.
905
+ 2.3. Representation Of SQL Tables
906
+ Each ordinary SQL table in the database schema is represented on-disk
907
+ by a table b-tree. Each entry in the table b-tree corresponds to a row
908
+ of the SQL table. The rowid of the SQL table is the 64-bit signed
909
+ integer key for each entry in the table b-tree.
910
+ The content of each SQL table row is stored in the database file by
911
+ first combining the values in the various columns into a byte array
912
+ in the record format, then storing that byte array as the payload in
913
+ an entry in the table b-tree. The order of values in the record is
914
+ the same as the order of columns in the SQL table definition.
915
+ When an SQL table includes an
916
+ INTEGER PRIMARY KEY column (which aliases the rowid ) then that
917
+ column appears in the record as a NULL value. SQLite will always use
918
+ the table b-tree key rather than the NULL value when referencing the
919
+ INTEGER PRIMARY KEY column.
920
+ If the affinity of a column is REAL and that column contains a
921
+ value that can be converted to an integer without loss of information
922
+ (if the value contains no fractional part and is not too large to be
923
+ represented as an integer) then the column may be stored in the record
924
+ as an integer. SQLite will convert the value back to floating
925
+ point when extracting it from the record.
926
+ 2.4. Representation of WITHOUT ROWID Tables
927
+ If an SQL table is created using the "WITHOUT ROWID" clause at the
928
+ end of its CREATE TABLE statement, then that table is a WITHOUT ROWID
929
+ table and uses a different on-disk representation. A WITHOUT ROWID
930
+ table uses an index b-tree rather than a table b-tree for storage.
931
+ The key for each entry in the WITHOUT ROWID b-tree is a record composed
932
+ of the columns of the PRIMARY KEY followed by all remaining columns of
933
+ the table. The primary key columns appear in the order that they were
934
+ declared in the PRIMARY KEY clause and the remaining columns appear in
935
+ the order they occur in the CREATE TABLE statement.
936
+ Hence, the content encoding for a WITHOUT ROWID table is the same
937
+ as the content encoding for an ordinary rowid table, except that the
938
+ order of the columns is rearranged so that PRIMARY KEY columns come
939
+ first, and the content is used as the key in an index b-tree rather
940
+ than as the data in a table b-tree.
941
+ The special encoding rules for columns with REAL affinity
942
+ apply to WITHOUT ROWID tables the same as they do with rowid tables.
943
+ 2.4.1. Suppression of redundant columns in the PRIMARY KEY
944
+ of WITHOUT ROWID tables
945
+ If the PRIMARY KEY of a WITHOUT ROWID tables uses the same columns
946
+ with the same collating sequence more than once, then the second and
947
+ subsequent occurrences of that column in the PRIMARY KEY definition are
948
+ ignored. For example, the following CREATE TABLE statements all specify
949
+ the same table, which will have the exact same representation on disk:
950
+ The first example above is the preferred definition of the table,
951
+ of course. All of the examples create a WITHOUT ROWID table with
952
+ two PRIMARY KEY columns, "a" and "c", in that order, followed by
953
+ two data columns "b" and "d", also in that order.
954
+ 2.5. Representation Of SQL Indices
955
+ Each SQL index, whether explicitly declared via a CREATE INDEX statement
956
+ or implied by a UNIQUE or PRIMARY KEY constraint, corresponds to an
957
+ index b-tree in the database file.
958
+ Each entry in the index b-tree corresponds to a single row in the
959
+ associated SQL table.
960
+ The key to an index b-tree is
961
+ a record composed of the columns that are being indexed followed by the
962
+ key of the corresponding table row. For ordinary tables, the row key is
963
+ the rowid , and for WITHOUT ROWID tables the row key is the PRIMARY KEY.
964
+ Because every row in the table has a unique row key,
965
+ all keys in an index are unique.
966
+ In a normal index, there is a one-to-one mapping between rows in a
967
+ table and entries in each index associated with that table.
968
+ However, in a partial index , the index b-tree only contains entries
969
+ corresponding to table rows for which the WHERE clause expression on the
970
+ CREATE INDEX statement is true.
971
+ Corresponding rows in the index and table b-trees share the same rowid
972
+ or primary key values and contain the same value for all indexed columns.
973
+ 2.5.1. Suppression of redundant columns in WITHOUT ROWID secondary indexes
974
+ In an index on a WITHOUT ROWID table, if a column of the PRIMARY KEY
975
+ is also a column in the index and has a matching collating sequence, then the
976
+ indexed column is not repeated in the table-key suffix on the
977
+ end of the index record. As an example, consider the following SQL:
978
+ Each row in the ex25ce index is a record
979
+ with these columns: c, e, d, a. The first two columns are
980
+ the columns being indexed, c and e. The remaining columns are the primary
981
+ key of the corresponding table row. Normally, the primary key would be
982
+ columns d, c, and a, but because column c already appears earlier in the
983
+ index, it is omitted from the key suffix.
984
+ In the extreme case where the columns being indexed cover all columns
985
+ of the PRIMARY KEY, the index will consist of only the columns being
986
+ indexed. The ex25acde example above demonstrates this. Each entry in
987
+ the ex25acde index consists of only the columns a, c, d, and e, in that
988
+ order.
989
+ Each row in ex25ae contains five columns: a, e, d, c, a. The "a"
990
+ column is repeated since the first occurrence of "a" has a collating
991
+ function of "nocase" and the second has a collating sequence of "binary".
992
+ If the "a" column is not repeated and if the table contains two or more
993
+ entries with the same "e" value and where "a" differs only in case, then
994
+ all of those table entries would correspond to a single entry in the
995
+ index, which would break the one-to-one correspondence between the table
996
+ and the index.
997
+ The suppression of redundant columns in the key suffix of an index
998
+ entry only occurs in WITHOUT ROWID tables. In an ordinary rowid table,
999
+ the index entry always ends with the rowid even if the INTEGER PRIMARY KEY
1000
+ column is one of the columns being indexed.
1001
+ 2.6. Storage Of The SQL Database Schema
1002
+ Page 1 of a database file is the root page of a table b-tree that
1003
+ holds a special table named " sqlite_schema ". This b-tree is known
1004
+ as the "schema table" since it stores the complete
1005
+ database schema. The structure of the sqlite_schema table is as
1006
+ if it had been created using the following SQL:
1007
+ The sqlite_schema table contains one row for each table, index, view,
1008
+ and trigger (collectively "objects") in the database schema, except there
1009
+ is no entry for the sqlite_schema table itself. The sqlite_schema table
1010
+ contains entries for internal schema objects in addition to application-
1011
+ and programmer-defined objects.
1012
+ The sqlite_schema.type column will be one
1013
+ of the following text strings: 'table', 'index', 'view', or 'trigger'
1014
+ according to the type of object defined. The 'table' string is used
1015
+ for both ordinary and virtual tables .
1016
+ The sqlite_schema.name column will hold the name of the object.
1017
+ UNIQUE and PRIMARY KEY constraints on tables cause SQLite to create
1018
+ internal indexes with names of the form "sqlite_autoindex_TABLE_N"
1019
+ where TABLE is replaced by the name of the table that contains the
1020
+ constraint and N is an integer beginning with 1 and increasing by one
1021
+ with each constraint seen in the table definition.
1022
+ In a WITHOUT ROWID table, there is no sqlite_schema entry for the
1023
+ PRIMARY KEY, but the "sqlite_autoindex_TABLE_N" name is set aside
1024
+ for the PRIMARY KEY as if the sqlite_schema entry did exist. This
1025
+ will affect the numbering of subsequent UNIQUE constraints.
1026
+ The "sqlite_autoindex_TABLE_N" name is never allocated for an
1027
+ INTEGER PRIMARY KEY , either in rowid tables or WITHOUT ROWID tables.
1028
+ The sqlite_schema.tbl_name column holds the name of a table or view
1029
+ that the object is associated with. For a table or view, the
1030
+ tbl_name column is a copy of the name column. For an index, the tbl_name
1031
+ is the name of the table that is indexed. For a trigger, the tbl_name
1032
+ column stores the name of the table or view that causes the trigger
1033
+ to fire.
1034
+ The sqlite_schema.rootpage column stores the page number of the root
1035
+ b-tree page for tables and indexes. For rows that define views, triggers,
1036
+ and virtual tables, the rootpage column is 0 or NULL.
1037
+ The sqlite_schema.sql column stores SQL text that describes the
1038
+ object. This SQL text is a CREATE TABLE , CREATE VIRTUAL TABLE ,
1039
+ CREATE INDEX ,
1040
+ CREATE VIEW , or CREATE TRIGGER statement that if evaluated against
1041
+ the database file when it is the main database of a database connection
1042
+ would recreate the object. The text is usually a copy of the original
1043
+ statement used to create the object but with normalizations applied so
1044
+ that the text conforms to the following rules:
1045
+ The CREATE, TABLE, VIEW, TRIGGER, and INDEX keywords at the beginning
1046
+ of the statement are converted to all upper case letters.
1047
+ The TEMP or TEMPORARY keyword is removed if it occurs after the
1048
+ initial CREATE keyword.
1049
+ Any database name qualifier that occurs prior to the name of the
1050
+ object being created is removed.
1051
+ Leading spaces are removed.
1052
+ All spaces following the first two keywords are converted into a single
1053
+ space.
1054
+ The text in the sqlite_schema.sql column is a copy of the original
1055
+ CREATE statement text that created the object, except normalized as
1056
+ described above and as modified by subsequent ALTER TABLE statements.
1057
+ The sqlite_schema.sql is NULL for the internal indexes that are
1058
+ automatically created by UNIQUE or PRIMARY KEY constraints.
1059
+ 2.6.1. Alternative Names For The Schema Table
1060
+ The name "sqlite_schema" does not appear anywhere in the file format.
1061
+ That name is just a convention used by the database implementation.
1062
+ Due to historical and operational considerations, the
1063
+ "sqlite_schema" table can also sometimes be called by one of the
1064
+ following aliases:
1065
+ sqlite_master
1066
+ sqlite_temp_schema
1067
+ sqlite_temp_master
1068
+ Because the name of the schema table does not appear anywhere in
1069
+ the file format, the meaning of the database file is not changed if
1070
+ the application chooses to refer to the schema table by one of
1071
+ these alternative names.
1072
+ 2.6.2. Internal Schema Objects
1073
+ In addition to the tables, indexes, views, and triggers created by
1074
+ the application and/or the developer using CREATE statements SQL, the
1075
+ sqlite_schema table may contain zero or more entries for
1076
+ internal schema objects that are created by SQLite for its
1077
+ own internal use. The names of internal schema objects
1078
+ always begin with "sqlite_" and any table, index, view, or trigger
1079
+ whose name begins with "sqlite_" is an internal schema object.
1080
+ SQLite prohibits applications from creating objects whose names begin
1081
+ with "sqlite_".
1082
+ Internal schema objects used by SQLite may include the following:
1083
+ Indices with names of the form "sqlite_autoindex_TABLE_N" that
1084
+ are used to implement UNIQUE and PRIMARY KEY constraints on
1085
+ ordinary tables.
1086
+ A table with the name "sqlite_sequence" that is used to keep track
1087
+ of the maximum historical INTEGER PRIMARY KEY for a table
1088
+ using AUTOINCREMENT .
1089
+ Tables with names of the form "sqlite_statN" where N is an integer.
1090
+ Such tables store database statistics gathered by the ANALYZE
1091
+ command and used by the query planner to help determine the best
1092
+ algorithm to use for each query.
1093
+ New internal schema objects names, always beginning with "sqlite_",
1094
+ may be added to the SQLite file format in future releases.
1095
+ 2.6.3. The sqlite_sequence table
1096
+ The sqlite_sequence table is an internal table used to help implement
1097
+ AUTOINCREMENT . The sqlite_sequence table is created automatically
1098
+ whenever any ordinary table with an AUTOINCREMENT integer primary
1099
+ key is created. Once created, the sqlite_sequence table exists in the
1100
+ sqlite_schema table forever; it cannot be dropped.
1101
+ The schema for the sqlite_sequence table is:
1102
+ There is a single row in the sqlite_sequence table for each ordinary
1103
+ table that uses AUTOINCREMENT. The name of the table (as it appears in
1104
+ sqlite_schema.name) is in the sqlite_sequence.name field and the largest
1105
+ INTEGER PRIMARY KEY ever inserted into that table is
1106
+ in the sqlite_sequence.seq field.
1107
+ New automatically generated integer primary keys for AUTOINCREMENT
1108
+ tables are guaranteed to be larger than the sqlite_sequence.seq field for
1109
+ that table.
1110
+ If the sqlite_sequence.seq field of an AUTOINCREMENT table is already at
1111
+ the largest integer value (9223372036854775807) then attempts to add new
1112
+ rows to that table with an automatically generated integer primary will fail
1113
+ with an SQLITE_FULL error.
1114
+ The sqlite_sequence.seq field is automatically updated if required when
1115
+ new entries are inserted to an AUTOINCREMENT table.
1116
+ The sqlite_sequence row for an AUTOINCREMENT table is automatically deleted
1117
+ when the table is dropped.
1118
+ If the sqlite_sequence row for an AUTOINCREMENT table does not exist when
1119
+ the AUTOINCREMENT table is updated, then a new sqlite_sequence row is created.
1120
+ If the sqlite_sequence.seq value for an AUTOINCREMENT table is manually
1121
+ set to something other than an integer and there is a subsequent attempt to
1122
+ insert the or update the AUTOINCREMENT table, then the behavior is undefined.
1123
+ Application code is allowed to modify the sqlite_sequence table, to add
1124
+ new rows, to delete rows, or to modify existing rows. However, application
1125
+ code cannot create the sqlite_sequence table if it does not already exist.
1126
+ Application code can delete all entries from the sqlite_sequence table,
1127
+ but application code cannot drop the sqlite_sequence table.
1128
+ 2.6.4. The sqlite_stat1 table
1129
+ The sqlite_stat1 is an internal table created by the ANALYZE command
1130
+ and used to hold supplemental information about tables and indexes that the
1131
+ query planner can use to help it find better ways of performing queries.
1132
+ Applications can update, delete from, insert into or drop the sqlite_stat1
1133
+ table, but may not create or alter the sqlite_stat1 table.
1134
+ The schema of the sqlite_stat1 table is as follows:
1135
+ There is normally one row per index, with the index identified by the
1136
+ name in the sqlite_stat1.idx column. The sqlite_stat1.tbl column is
1137
+ the name of the table to which the index belongs. In each such row,
1138
+ the sqlite_stat.stat column will be
1139
+ a string consisting of a list of integers followed by zero or more
1140
+ arguments. The first integer in this
1141
+ list is the approximate number of rows in the index. (The number of
1142
+ rows in the index is the same as the number of rows in the table,
1143
+ except for partial indexes .)
1144
+ The second integer is the approximate number of rows in the index
1145
+ that have the same value in the first column of the index. The third
1146
+ integer is the number of rows in the index that have
1147
+ the same value for the first two columns. The N-th integer (for N>1)
1148
+ is the estimated average number of rows in
1149
+ the index which have the same value for the first N-1 columns. For
1150
+ a K-column index, there will be K+1 integers in the stat column. If
1151
+ the index is unique, then the last integer will be 1.
1152
+ The list of integers in the stat column can optionally be followed
1153
+ by arguments, each of which is a sequence of non-space characters.
1154
+ All arguments are preceded by a single space.
1155
+ Unrecognized arguments are silently ignored.
1156
+ If the "unordered" argument is present, then the query planner assumes
1157
+ that the index is unordered and will not use the index for a range query
1158
+ or for sorting.
1159
+ The "sz=NNN" argument (where NNN represents a sequence of 1 or more digits)
1160
+ means that the average row size over all records of the table or
1161
+ index is NNN bytes per row. The SQLite query planner might use the
1162
+ estimated row size information provided by the "sz=NNN" token
1163
+ to help it choose smaller tables and indexes that require less disk I/O.
1164
+ The presence of the "noskipscan" token on the sqlite_stat1.stat field
1165
+ of an index prevents that index from being used with the
1166
+ skip-scan optimization .
1167
+ New text tokens may be added to the end of the stat column in future
1168
+ enhancements to SQLite. For compatibility, unrecognized tokens at the end
1169
+ of the stat column are silently ignored.
1170
+ If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
1171
+ column contains a single integer which is the approximate number of
1172
+ rows in the table identified by sqlite_stat1.tbl.
1173
+ If the sqlite_stat1.idx column is the same as the sqlite_stat1.tbl
1174
+ column, then the table is a WITHOUT ROWID table and the sqlite_stat1.stat
1175
+ field contains information about the index btree that implements the
1176
+ WITHOUT ROWID table.
1177
+ 2.6.5. The sqlite_stat2 table
1178
+ The sqlite_stat2 is only created and is only used if SQLite is compiled
1179
+ with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
1180
+ 3.6.18 (2009-09-11) and 3.7.8 (2011-09-19).
1181
+ The sqlite_stat2 table is neither read nor written by any
1182
+ version of SQLite before 3.6.18 nor after 3.7.8.
1183
+ The sqlite_stat2 table contains additional information
1184
+ about the distribution of keys within an index.
1185
+ The schema of the sqlite_stat2 table is as follows:
1186
+ The sqlite_stat2.idx column and the sqlite_stat2.tbl column in each
1187
+ row of the sqlite_stat2 table identify an index described by that row.
1188
+ There are usually 10 rows in the sqlite_stat2
1189
+ table for each index.
1190
+ The sqlite_stat2 entries for an index that have sqlite_stat2.sampleno
1191
+ between 0 and 9 inclusive are samples of the left-most key value in the
1192
+ index taken at evenly spaced points along the index.
1193
+ Let C be the number of rows in the index.
1194
+ Then the sampled rows are given by
1195
+ rownumber = (i*C*2 + C)/20
1196
+ The variable i in the previous expression varies between 0 and 9.
1197
+ Conceptually, the index space is divided into
1198
+ 10 uniform buckets and the samples are the middle row from each bucket.
1199
+ The format for sqlite_stat2 is recorded here for legacy reference.
1200
+ Recent versions of SQLite no longer support sqlite_stat2 and the
1201
+ sqlite_stat2 table, if is exists, is simply ignored.
1202
+ 2.6.6. The sqlite_stat3 table
1203
+ The sqlite_stat3 is only used if SQLite is compiled
1204
+ with SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4
1205
+ and if the SQLite version number is 3.7.9 (2011-11-01) or greater.
1206
+ The sqlite_stat3 table is neither read nor written by any
1207
+ version of SQLite before 3.7.9.
1208
+ If the SQLITE_ENABLE_STAT4 compile-time option is used and the
1209
+ SQLite version number is 3.8.1 (2013-10-17) or greater,
1210
+ then sqlite_stat3 might be read but not written.
1211
+ The sqlite_stat3 table contains additional information
1212
+ about the distribution of keys within an index, information that the
1213
+ query planner can use to devise better and faster query algorithms.
1214
+ The schema of the sqlite_stat3 table is as follows:
1215
+ There are usually multiple entries in the sqlite_stat3 table for each index.
1216
+ The sqlite_stat3.sample column holds the value of the left-most field of an
1217
+ index identified by sqlite_stat3.idx and sqlite_stat3.tbl.
1218
+ The sqlite_stat3.nEq column holds the approximate
1219
+ number of entries in the index whose left-most column exactly matches
1220
+ the sample.
1221
+ The sqlite_stat3.nLt holds the approximate number of entries in the
1222
+ index whose left-most column is less than the sample.
1223
+ The sqlite_stat3.nDLt column holds the approximate
1224
+ number of distinct left-most entries in the index that are less than
1225
+ the sample.
1226
+ There can be an arbitrary number of sqlite_stat3 entries per index.
1227
+ The ANALYZE command will typically generate sqlite_stat3 tables
1228
+ that contain between 10 and 40 samples that are distributed across
1229
+ the key space and with large nEq values.
1230
+ In a well-formed sqlite_stat3 table, the samples for any single
1231
+ index must appear in the same order that they occur in the index.
1232
+ In other words, if the entry with left-most column S1 is earlier in
1233
+ the index b-tree than the
1234
+ entry with left-most column S2, then in the sqlite_stat3 table,
1235
+ sample S1 must have a smaller rowid than sample S2.
1236
+ 2.6.7. The sqlite_stat4 table
1237
+ The sqlite_stat4 is only created and is only used if SQLite is compiled
1238
+ with SQLITE_ENABLE_STAT4 and if the SQLite version number is
1239
+ 3.8.1 (2013-10-17) or greater.
1240
+ The sqlite_stat4 table is neither read nor written by any
1241
+ version of SQLite before 3.8.1.
1242
+ The sqlite_stat4 table contains additional information
1243
+ about the distribution of keys within an index or the distribution of
1244
+ keys in the primary key of a WITHOUT ROWID table.
1245
+ The query planner can sometimes use the additional information in
1246
+ the sqlite_stat4 table to devise better and faster query algorithms.
1247
+ The schema of the sqlite_stat4 table is as follows:
1248
+ There are typically between 10 to 40 entries in the sqlite_stat4 table for
1249
+ each index for which statistics are available, however these limits are
1250
+ not hard bounds.
1251
+ The meanings of the columns in the sqlite_stat4 table are as follows:
1252
+ tbl:
1253
+ The sqlite_stat4.tbl column holds name of the table that owns
1254
+ the index that the row describes
1255
+ idx:
1256
+ The sqlite_stat4.idx column holds name of the index that the
1257
+ row describes, or in the case of
1258
+ an sqlite_stat4 entry for a WITHOUT ROWID table, the
1259
+ name of the table itself.
1260
+ sample:
1261
+ The sqlite_stat4.sample column holds a BLOB
1262
+ in the record format that encodes the indexed columns followed by
1263
+ the rowid for a rowid table or by the columns of the primary key
1264
+ for a WITHOUT ROWID table.
1265
+ The sqlite_stat4.sample BLOB for the WITHOUT ROWID table itself
1266
+ contains just the columns of the primary key.
1267
+ Let the number of columns encoded by the sqlite_stat4.sample blob be N.
1268
+ For indexes on an ordinary rowid table, N will be one more than the number
1269
+ of columns indexed.
1270
+ For indexes on WITHOUT ROWID tables, N will be the number of columns
1271
+ indexed plus the number of columns in the primary key.
1272
+ For a WITHOUT ROWID table, N will be the number of columns in the
1273
+ primary key.
1274
+ nEq:
1275
+ The sqlite_stat4.nEq column holds a list of N integers where
1276
+ the K-th integer is the approximate number of entries in the index
1277
+ whose left-most K columns exactly match the K left-most columns
1278
+ of the sample.
1279
+ nLt:
1280
+ The sqlite_stat4.nLt column holds a list of N integers where
1281
+ the K-th integer is the approximate number of entries in the
1282
+ index whose K left-most columns are collectively less than the
1283
+ K left-most columns of the sample.
1284
+ nDLt:
1285
+ The sqlite_stat4.nDLt column holds a list of N integers where
1286
+ the K-th integer is the approximate
1287
+ number of entries in the index that are distinct in the first K columns and
1288
+ where the left-most K columns are collectively less than the left-most
1289
+ K columns of the sample.
1290
+ The sqlite_stat4 is a generalization of the sqlite_stat3 table. The
1291
+ sqlite_stat3 table provides information about the left-most column of an
1292
+ index whereas the sqlite_stat4 table provides information about all columns
1293
+ of the index.
1294
+ There can be an arbitrary number of sqlite_stat4 entries per index.
1295
+ The ANALYZE command will typically generate sqlite_stat4 tables
1296
+ that contain between 10 and 40 samples that are distributed across
1297
+ the key space and with large nEq values.
1298
+ In a well-formed sqlite_stat4 table, the samples for any single
1299
+ index must appear in the same order that they occur in the index.
1300
+ In other words, if entry S1 is earlier in the index b-tree than
1301
+ entry S2, then in the sqlite_stat4 table, sample S1 must have a
1302
+ smaller rowid than sample S2.
1303
+ 3. The Rollback Journal
1304
+ The rollback journal is a file associated with each SQLite database
1305
+ file that holds information used to restore the database file to its initial
1306
+ state during the course of a transaction.
1307
+ The rollback journal file is always located in the same
1308
+ directory as the database
1309
+ file and has the same name as the database file but with the string
1310
+ " -journal " appended. There can only be a single rollback journal
1311
+ associated with a give database and hence there can only be one write
1312
+ transaction open against a single database at one time.
1313
+ Before any information-bearing page of the database is modified,
1314
+ the original unmodified content of that page is written into the
1315
+ rollback journal. If a transaction is interrupted and needs to be
1316
+ rolled back, the rollback journal can then be used to restore the
1317
+ database to its original state. Freelist leaf pages bear no
1318
+ information that would need to be restored on a rollback and so they
1319
+ are not written to the journal prior to modification, in order to
1320
+ reduce disk I/O.
1321
+ If a transaction is aborted due to an application crash, or a single,
1322
+ or an operating system crash, or a hardware power failure or crash, then
1323
+ the main database file might be left in an inconsistent state.
1324
+ The next time SQLite attempts to open
1325
+ the database file, the presence of the rollback journal file will be
1326
+ detected and the journal will be automatically played back to restore the
1327
+ database to its state at the start of the incomplete transaction.
1328
+ A rollback journal is only considered to be valid if it exists and
1329
+ contains a valid header. Hence a transaction can be committed in one
1330
+ of three ways:
1331
+ The rollback journal file can be deleted,
1332
+ The rollback journal file can be truncated to zero length, or
1333
+ The header of the rollback journal can be overwritten with
1334
+ invalid header text (for example, all zeros).
1335
+ These three ways of committing a transaction correspond to the DELETE,
1336
+ TRUNCATE, and PERSIST settings, respectively, of the journal_mode pragma .
1337
+ A valid rollback journal begins with a header in the following format:
1338
+ Rollback Journal Header Format
1339
+ Offset Size Description
1340
+ 0
1341
+ 8
1342
+ Header string: 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7
1343
+ 8
1344
+ 4
1345
+ The "Page Count" - The number of pages in the next segment of the
1346
+ journal, or -1 to
1347
+ mean all content to the end of the file
1348
+ 12
1349
+ 4
1350
+ A random nonce for the checksum
1351
+ 16
1352
+ 4
1353
+ Initial size of the database in pages
1354
+ 20
1355
+ 4
1356
+ Size of a disk sector assumed by the process that wrote this
1357
+ journal.
1358
+ 24
1359
+ 4
1360
+ Size of pages in this journal.
1361
+ A rollback journal header is padded with zeros out to the size of a
1362
+ single sector (as defined by the sector size integer at offset 20).
1363
+ The header is in a sector by itself so that if a power loss occurs while
1364
+ writing the sector, information that follows the header will be
1365
+ (hopefully) undamaged.
1366
+ After the header and zero padding are zero or more page records. Each
1367
+ page record stores a copy of the content of a page from the database file
1368
+ before it was changed. The same page may not appear more than once
1369
+ within a single rollback journal.
1370
+ To rollback an incomplete transaction, a process
1371
+ has merely to read the rollback journal from beginning to end and
1372
+ write pages found in the journal back into the database file at the
1373
+ appropriate location.
1374
+ Let the database page size (the value of the integer at offset 24
1375
+ in the journal header) be N.
1376
+ Then the format of a page record is as follows:
1377
+ Rollback Journal Page Record Format
1378
+ Offset Size Description
1379
+ 0
1380
+ 4
1381
+ The page number in the database file
1382
+ 4
1383
+ N
1384
+ Original content of the page prior to the start of the transaction
1385
+ N+4
1386
+ 4
1387
+ Checksum
1388
+ The checksum is an unsigned 32-bit integer computed as follows:
1389
+ Initialize the checksum to the checksum nonce value found in the
1390
+ journal header at offset 12.
1391
+ Initialize index X to be N-200 (where N is the size of a database page
1392
+ in bytes.
1393
+ Interpret the byte at offset X into the page as an 8-bit unsigned integer
1394
+ and add the value of that integer to the checksum.
1395
+ Subtract 200 from X.
1396
+ If X is greater than or equal to zero, go back to step 3.
1397
+ The checksum value is used to guard against incomplete writes of
1398
+ a journal page record following a power failure. A different random nonce
1399
+ is used each time a transaction is started in order to minimize the risk
1400
+ that unwritten sectors might by chance contain data from the same page
1401
+ that was a part of prior journals. By changing the nonce for each
1402
+ transaction, stale data on disk will still generate an incorrect checksum
1403
+ and be detected with high probability. The checksum only uses a sparse sample
1404
+ of 32-bit words from the data record for performance reasons - design studies
1405
+ during the planning phases of SQLite 3.0.0 showed
1406
+ a significant performance hit in checksumming the entire page.
1407
+ Let the page count value at offset 8 in the journal header be M.
1408
+ If M is greater than zero then after M page records the journal file
1409
+ may be zero padded out to the next multiple of the sector size and another
1410
+ journal header may be inserted. All journal headers within the same
1411
+ journal must contain the same database page size and sector size.
1412
+ If M is -1 in the initial journal header, then the number of page records
1413
+ that follow is computed by computing how many page records will fit in
1414
+ the available space of the remainder of the journal file.
1415
+ 4. The Write-Ahead Log
1416
+ Beginning with version 3.7.0 (2010-07-21),
1417
+ SQLite supports a new transaction
1418
+ control mechanism called " write-ahead log " or " WAL ".
1419
+ When a database is in WAL mode, all connections to that database must
1420
+ use the WAL. A particular database will use either a rollback journal
1421
+ or a WAL, but not both at the same time.
1422
+ The WAL is always located in the same directory as the database
1423
+ file and has the same name as the database file but with the string
1424
+ " -wal " appended.
1425
+ 4.1. WAL File Format
1426
+ A WAL file consists of a header followed by zero or more "frames".
1427
+ Each frame records the revised content of a single page from the
1428
+ database file. All changes to the database are recorded by writing
1429
+ frames into the WAL. Transactions commit when a frame is written that
1430
+ contains a commit marker. A single WAL can and usually does record
1431
+ multiple transactions. Periodically, the content of the WAL is
1432
+ transferred back into the database file in an operation called a
1433
+ "checkpoint".
1434
+ A single WAL file can be reused multiple times. In other words, the
1435
+ WAL can fill up with frames and then be checkpointed and then new
1436
+ frames can overwrite the old ones. A WAL always grows from beginning
1437
+ toward the end. Checksums and counters attached to each frame are
1438
+ used to determine which frames within the WAL are valid and which
1439
+ are leftovers from prior checkpoints.
1440
+ The WAL header is 32 bytes in size and consists of the following eight
1441
+ big-endian 32-bit unsigned integer values:
1442
+ WAL Header Format
1443
+ Offset Size Description
1444
+ 0 4
1445
+ Magic number. 0x377f0682 or 0x377f0683
1446
+ 4 4
1447
+ File format version. Currently 3007000.
1448
+ 8 4
1449
+ Database page size. Example: 1024
1450
+ 12 4
1451
+ Checkpoint sequence number
1452
+ 16 4
1453
+ Salt-1: random integer incremented with each checkpoint
1454
+ 20 4
1455
+ Salt-2: a different random number for each checkpoint
1456
+ 24 4
1457
+ Checksum-1: First part of a checksum on the first 24 bytes of header
1458
+ 28 4
1459
+ Checksum-2: Second part of the checksum on the first 24 bytes of header
1460
+ Immediately following the wal-header are zero or more frames. Each
1461
+ frame consists of a 24-byte frame-header followed by a page-size bytes
1462
+ of page data. The frame-header is six big-endian 32-bit unsigned
1463
+ integer values, as follows:
1464
+ WAL Frame Header Format
1465
+ Offset Size Description
1466
+ 0 4
1467
+ Page number
1468
+ 4 4
1469
+ For commit records, the size of the database file in pages
1470
+ after the commit. For all other records, zero.
1471
+ 8 4
1472
+ Salt-1 copied from the WAL header
1473
+ 12 4
1474
+ Salt-2 copied from the WAL header
1475
+ 16 4
1476
+ Checksum-1: Cumulative checksum up through and including this page
1477
+ 20 4
1478
+ Checksum-2: Second half of the cumulative checksum.
1479
+ A frame is considered valid if and only if the following conditions are
1480
+ true:
1481
+ The salt-1 and salt-2 values in the frame-header match
1482
+ salt values in the wal-header
1483
+ The checksum values in the final 8 bytes of the frame-header
1484
+ exactly match the checksum computed consecutively on the
1485
+ first 24 bytes of the WAL header and the first 8 bytes and
1486
+ the content of all frames
1487
+ up to and including the current frame.
1488
+ 4.2. Checksum Algorithm
1489
+ The checksum is computed by interpreting the input as
1490
+ an even number of unsigned 32-bit integers: x(0) through x(N).
1491
+ The 32-bit integers are big-endian if the
1492
+ magic number in the first 4 bytes of the WAL header is 0x377f0683 and
1493
+ the integers are little-endian if the magic number is 0x377f0682.
1494
+ The checksum values are always stored in the frame header in a
1495
+ big-endian format regardless of which byte order is used to compute
1496
+ the checksum.
1497
+ The checksum algorithm only works for content which is a multiple of
1498
+ 8 bytes in length. In other words, if the inputs are x(0) through x(N)
1499
+ then N must be odd.
1500
+ The checksum algorithm is as follows:
1501
+ The outputs s0 and s1 are both weighted checksums using Fibonacci weights
1502
+ in reverse order. (The largest Fibonacci weight occurs on the first element
1503
+ of the sequence being summed.) The s1 value spans all 32-bit integer
1504
+ terms of the sequence whereas s0 omits the final term.
1505
+ 4.3. Checkpoint Algorithm
1506
+ On a checkpoint , the WAL is first flushed to persistent storage using
1507
+ the xSync method of the VFS .
1508
+ Then valid content of the WAL is transferred into the database file.
1509
+ Finally, the database is flushed to persistent storage using another
1510
+ xSync method call.
1511
+ The xSync operations serve as write barriers - all writes launched
1512
+ before the xSync must complete before any write that launches after the
1513
+ xSync begins.
1514
+ A checkpoint need not run to completion. It might be that some
1515
+ readers are still using older transactions with data that is contained
1516
+ in the database file. In that case, transferring content for newer
1517
+ transactions from the WAL file into the database would delete the content
1518
+ out from under readers still using the older transactions. To avoid that,
1519
+ checkpoints only run to completion if all reader are using the
1520
+ last transaction in the WAL.
1521
+ 4.4. WAL Reset
1522
+ After a complete checkpoint, if no other connections are in transactions
1523
+ that use the WAL, then subsequent write transactions can
1524
+ overwrite the WAL file from the beginning. This is called "resetting the
1525
+ WAL". At the start of the first new
1526
+ write transaction, the WAL header salt-1 value is incremented
1527
+ and the salt-2 value is randomized. These changes to the salts invalidate
1528
+ old frames in the WAL that have already been checkpointed but not yet
1529
+ overwritten, and prevent them from being checkpointed again.
1530
+ The WAL file can optionally be truncated on a reset, but it need not be.
1531
+ Performance is usually a little better if the WAL is not truncated, since
1532
+ filesystems generally will overwrite an existing file faster than they
1533
+ will grow a file.
1534
+ 4.5. Reader Algorithm
1535
+ To read a page from the database (call it page number P), a reader
1536
+ first checks the WAL to see if it contains page P. If so, then the
1537
+ last valid instance of page P that is followed by a commit frame
1538
+ or is a commit frame itself becomes the value read. If the WAL
1539
+ contains no copies of page P that are valid and which are a commit
1540
+ frame or are followed by a commit frame, then page P is read from
1541
+ the database file.
1542
+ To start a read transaction, the reader records the number of value
1543
+ frames in the WAL as "mxFrame". ( More detail )
1544
+ The reader uses this recorded mxFrame value
1545
+ for all subsequent read operations. New transactions can be appended
1546
+ to the WAL, but as long as the reader uses its original mxFrame value
1547
+ and ignores subsequently appended content, the reader will see a
1548
+ consistent snapshot of the database from a single point in time.
1549
+ This technique allows multiple concurrent readers to view different
1550
+ versions of the database content simultaneously.
1551
+ The reader algorithm in the previous paragraphs works correctly, but
1552
+ because frames for page P can appear anywhere within the WAL, the
1553
+ reader has to scan the entire WAL looking for page P frames. If the
1554
+ WAL is large (multiple megabytes is typical) that scan can be slow,
1555
+ and read performance suffers. To overcome this problem, a separate
1556
+ data structure called the wal-index is maintained to expedite the
1557
+ search for frames of a particular page.
1558
+ 4.6. WAL-Index Format
1559
+ Conceptually, the wal-index is shared memory, though the current
1560
+ VFS implementations use a memory-mapped file for operating-system
1561
+ portability. The memory-mapped
1562
+ file is in the same directory as the database and has the same name
1563
+ as the database with a " -shm " suffix appended. Because
1564
+ the wal-index is shared memory, SQLite does not support
1565
+ journal_mode=WAL
1566
+ on a network filesystem when clients are on different machines, as
1567
+ all clients of the database must be able to share the same memory.
1568
+ The purpose of the wal-index is to answer this question quickly:
1569
+ Given a page number P and a maximum WAL frame index M,
1570
+ return the largest WAL frame index for page P that does not exceed M,
1571
+ or return NULL if there are no frames for page P that do not exceed M.
1572
+ The M value in the previous paragraph is the "mxFrame" value
1573
+ defined in section 4.5 that is read at the start
1574
+ of a transaction and which defines the maximum frame from the WAL that
1575
+ the reader will use.
1576
+ The wal-index is transient. After a crash, the wal-index is
1577
+ reconstructed from the original WAL file. The VFS is required
1578
+ to either truncate or zero the header of the wal-index when the last
1579
+ connection to it closes. Because the wal-index is transient, it can
1580
+ use an architecture-specific format; it does not have to be cross-platform.
1581
+ Hence, unlike the database and WAL file formats which store all values
1582
+ as big endian, the wal-index stores multi-byte values in the native
1583
+ byte order of the host computer.
1584
+ This document is concerned with the persistent state of the database
1585
+ file, and since the wal-index is a transient structure, no further
1586
+ information about the format of the wal-index will be provided here.
1587
+ Additional details on the format of the wal-index are contained in
1588
+ the separate WAL-index File Format document.
1589
+ This page was last updated on 2025-12-25 10:33:36Z