blob: bfe3634577b753272fe28b4aebc58b696e1c3593 [file] [log] [blame]
dand3f8f942010-04-13 11:35:01 +00001# 2010 April 13
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the operation of the library in
13# "PRAGMA journal_mode=WAL" mode.
14#
dan7c246102010-04-12 19:00:29 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
dane264d982010-04-14 18:06:50 +000018source $testdir/lock_common.tcl
dana4a90952010-06-15 19:07:42 +000019source $testdir/malloc_common.tcl
dan10f5a502010-06-23 15:55:43 +000020source $testdir/wal_common.tcl
dan7c246102010-04-12 19:00:29 +000021
dance8e5ff2011-04-05 16:09:08 +000022set testprefix wal
23
dan5cf53532010-05-01 16:40:20 +000024ifcapable !wal {finish_test ; return }
drh7416f2e2015-07-25 03:10:12 +000025test_set_config_pagecache 0 0
dan5cf53532010-05-01 16:40:20 +000026
dan7c246102010-04-12 19:00:29 +000027proc reopen_db {} {
danb9bf16b2010-04-14 11:23:30 +000028 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +000029 forcedelete test.db test.db-wal test.db-wal-summary
dan7c246102010-04-12 19:00:29 +000030 sqlite3_wal db test.db
dan7c246102010-04-12 19:00:29 +000031}
32
dan67032392010-04-17 15:42:43 +000033set ::blobcnt 0
34proc blob {nByte} {
35 incr ::blobcnt
36 return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte]
37}
38
dan7c246102010-04-12 19:00:29 +000039proc sqlite3_wal {args} {
40 eval sqlite3 $args
dan65bddc12010-05-07 12:49:22 +000041 [lindex $args 0] eval { PRAGMA auto_vacuum = 0 }
dane04dc882010-04-20 18:53:15 +000042 [lindex $args 0] eval { PRAGMA page_size = 1024 }
dan7c246102010-04-12 19:00:29 +000043 [lindex $args 0] eval { PRAGMA journal_mode = wal }
dan67032392010-04-17 15:42:43 +000044 [lindex $args 0] eval { PRAGMA synchronous = normal }
45 [lindex $args 0] function blob blob
dan7c246102010-04-12 19:00:29 +000046}
47
dan3de777f2010-04-17 12:31:37 +000048proc log_deleted {logfile} {
49 return [expr [file exists $logfile]==0]
50}
51
dan7c246102010-04-12 19:00:29 +000052#
53# These are 'warm-body' tests used while developing the WAL code. They
54# serve to prove that a few really simple cases work:
55#
56# wal-1.*: Read and write the database.
57# wal-2.*: Test MVCC with one reader, one writer.
58# wal-3.*: Test transaction rollback.
59# wal-4.*: Test savepoint/statement rollback.
60# wal-5.*: Test the temp database.
61# wal-6.*: Test creating databases with different page sizes.
62#
dan998ad212010-05-07 06:59:08 +000063#
dan4bcc4982010-08-16 19:23:02 +000064#
dan7c246102010-04-12 19:00:29 +000065do_test wal-0.1 {
dan65bddc12010-05-07 12:49:22 +000066 execsql { PRAGMA auto_vacuum = 0 }
dan67032392010-04-17 15:42:43 +000067 execsql { PRAGMA synchronous = normal }
dan7c246102010-04-12 19:00:29 +000068 execsql { PRAGMA journal_mode = wal }
69} {wal}
dane04dc882010-04-20 18:53:15 +000070do_test wal-0.2 {
71 file size test.db
72} {1024}
dan7c246102010-04-12 19:00:29 +000073
74do_test wal-1.0 {
75 execsql {
76 BEGIN;
77 CREATE TABLE t1(a, b);
78 }
dane04dc882010-04-20 18:53:15 +000079 list [file exists test.db-journal] \
80 [file exists test.db-wal] \
81 [file size test.db]
82} {0 1 1024}
dan7c246102010-04-12 19:00:29 +000083do_test wal-1.1 {
84 execsql COMMIT
85 list [file exists test.db-journal] [file exists test.db-wal]
86} {0 1}
87do_test wal-1.2 {
88 # There are now two pages in the log.
89 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +000090} [wal_file_size 2 1024]
dan7c246102010-04-12 19:00:29 +000091
92do_test wal-1.3 {
93 execsql { SELECT * FROM sqlite_master }
94} {table t1 t1 2 {CREATE TABLE t1(a, b)}}
95
96do_test wal-1.4 {
97 execsql { INSERT INTO t1 VALUES(1, 2) }
98 execsql { INSERT INTO t1 VALUES(3, 4) }
99 execsql { INSERT INTO t1 VALUES(5, 6) }
100 execsql { INSERT INTO t1 VALUES(7, 8) }
101 execsql { INSERT INTO t1 VALUES(9, 10) }
102} {}
103
104do_test wal-1.5 {
105 execsql { SELECT * FROM t1 }
106} {1 2 3 4 5 6 7 8 9 10}
107
108do_test wal-2.1 {
109 sqlite3_wal db2 ./test.db
110 execsql { BEGIN; SELECT * FROM t1 } db2
111} {1 2 3 4 5 6 7 8 9 10}
112
113do_test wal-2.2 {
114 execsql { INSERT INTO t1 VALUES(11, 12) }
115 execsql { SELECT * FROM t1 }
116} {1 2 3 4 5 6 7 8 9 10 11 12}
117
118do_test wal-2.3 {
119 execsql { SELECT * FROM t1 } db2
120} {1 2 3 4 5 6 7 8 9 10}
121
122do_test wal-2.4 {
123 execsql { INSERT INTO t1 VALUES(13, 14) }
124 execsql { SELECT * FROM t1 }
125} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
126
127do_test wal-2.5 {
128 execsql { SELECT * FROM t1 } db2
129} {1 2 3 4 5 6 7 8 9 10}
130
131do_test wal-2.6 {
132 execsql { COMMIT; SELECT * FROM t1 } db2
133} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
134
135do_test wal-3.1 {
136 execsql { BEGIN; DELETE FROM t1 }
137 execsql { SELECT * FROM t1 }
138} {}
139do_test wal-3.2 {
140 execsql { SELECT * FROM t1 } db2
141} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
142do_test wal-3.3 {
143 execsql { ROLLBACK }
144 execsql { SELECT * FROM t1 }
145} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
146db2 close
147
dan74d6cd82010-04-24 18:44:05 +0000148#-------------------------------------------------------------------------
149# The following tests, wal-4.*, test that savepoints work with WAL
150# databases.
151#
dan7c246102010-04-12 19:00:29 +0000152do_test wal-4.1 {
153 execsql {
154 DELETE FROM t1;
155 BEGIN;
156 INSERT INTO t1 VALUES('a', 'b');
157 SAVEPOINT sp;
158 INSERT INTO t1 VALUES('c', 'd');
159 SELECT * FROM t1;
160 }
161} {a b c d}
162do_test wal-4.2 {
163 execsql {
164 ROLLBACK TO sp;
165 SELECT * FROM t1;
166 }
167} {a b}
168do_test wal-4.3 {
169 execsql {
170 COMMIT;
171 SELECT * FROM t1;
172 }
173} {a b}
174
dan4cd78b42010-04-26 16:57:10 +0000175do_test wal-4.4.1 {
dan74d6cd82010-04-24 18:44:05 +0000176 db close
177 sqlite3 db test.db
178 db func blob blob
179 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
180} {{a b} 0}
dan4cd78b42010-04-26 16:57:10 +0000181do_test wal-4.4.2 {
dan74d6cd82010-04-24 18:44:05 +0000182 execsql { PRAGMA cache_size = 10 }
183 execsql {
184 CREATE TABLE t2(a, b);
185 INSERT INTO t2 VALUES(blob(400), blob(400));
186 SAVEPOINT tr;
187 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
188 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
189 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
190 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
191 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
192 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
193 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
194 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
195 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
196 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
197 SELECT count(*) FROM t2;
198 }
199} {32}
dan4cd78b42010-04-26 16:57:10 +0000200do_test wal-4.4.3 {
dan74d6cd82010-04-24 18:44:05 +0000201 execsql { ROLLBACK TO tr }
202} {}
dan4cd78b42010-04-26 16:57:10 +0000203do_test wal-4.4.4 {
dan74d6cd82010-04-24 18:44:05 +0000204 set logsize [file size test.db-wal]
205 execsql {
206 INSERT INTO t1 VALUES('x', 'y');
207 RELEASE tr;
208 }
209 expr { $logsize == [file size test.db-wal] }
210} {1}
dan4cd78b42010-04-26 16:57:10 +0000211do_test wal-4.4.5 {
dan74d6cd82010-04-24 18:44:05 +0000212 execsql { SELECT count(*) FROM t2 }
213} {1}
dan4cd78b42010-04-26 16:57:10 +0000214do_test wal-4.4.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000215 forcecopy test.db test2.db
216 forcecopy test.db-wal test2.db-wal
dan74d6cd82010-04-24 18:44:05 +0000217 sqlite3 db2 test2.db
218 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
219} {1 2}
dan4cd78b42010-04-26 16:57:10 +0000220do_test wal-4.4.7 {
dan74d6cd82010-04-24 18:44:05 +0000221 execsql { PRAGMA integrity_check } db2
222} {ok}
223db2 close
224
dan4cd78b42010-04-26 16:57:10 +0000225do_test wal-4.5.1 {
226 reopen_db
227 db func blob blob
228 execsql {
229 PRAGMA journal_mode = WAL;
230 CREATE TABLE t1(a, b);
231 INSERT INTO t1 VALUES('a', 'b');
232 }
233 sqlite3 db test.db
234 db func blob blob
235 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
236} {{a b} 0}
237do_test wal-4.5.2 {
238 execsql { PRAGMA cache_size = 10 }
239 execsql {
240 CREATE TABLE t2(a, b);
241 BEGIN;
242 INSERT INTO t2 VALUES(blob(400), blob(400));
243 SAVEPOINT tr;
244 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
245 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
246 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
247 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
248 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
249 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
250 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
251 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
252 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
253 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
254 SELECT count(*) FROM t2;
255 }
256} {32}
257do_test wal-4.5.3 {
dan4cd78b42010-04-26 16:57:10 +0000258 execsql { ROLLBACK TO tr }
259} {}
260do_test wal-4.5.4 {
261 set logsize [file size test.db-wal]
dan4cd78b42010-04-26 16:57:10 +0000262 execsql {
263 INSERT INTO t1 VALUES('x', 'y');
264 RELEASE tr;
265 COMMIT;
266 }
267 expr { $logsize == [file size test.db-wal] }
268} {1}
269do_test wal-4.5.5 {
270 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 }
271} {1 2}
272do_test wal-4.5.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000273 forcecopy test.db test2.db
274 forcecopy test.db-wal test2.db-wal
dan4cd78b42010-04-26 16:57:10 +0000275 sqlite3 db2 test2.db
dan4cd78b42010-04-26 16:57:10 +0000276 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
277} {1 2}
278do_test wal-4.5.7 {
279 execsql { PRAGMA integrity_check } db2
280} {ok}
281db2 close
282
danb7d53f52010-05-06 17:28:08 +0000283do_test wal-4.6.1 {
284 execsql {
285 DELETE FROM t2;
286 PRAGMA wal_checkpoint;
287 BEGIN;
288 INSERT INTO t2 VALUES('w', 'x');
289 SAVEPOINT save;
290 INSERT INTO t2 VALUES('y', 'z');
291 ROLLBACK TO save;
292 COMMIT;
danb7d53f52010-05-06 17:28:08 +0000293 }
danbdd9af02010-11-18 16:14:24 +0000294 execsql { SELECT * FROM t2 }
danb7d53f52010-05-06 17:28:08 +0000295} {w x}
296
dan4cd78b42010-04-26 16:57:10 +0000297
dan74d6cd82010-04-24 18:44:05 +0000298reopen_db
dan7c246102010-04-12 19:00:29 +0000299do_test wal-5.1 {
300 execsql {
301 CREATE TEMP TABLE t2(a, b);
302 INSERT INTO t2 VALUES(1, 2);
303 }
304} {}
305do_test wal-5.2 {
306 execsql {
307 BEGIN;
308 INSERT INTO t2 VALUES(3, 4);
309 SELECT * FROM t2;
310 }
311} {1 2 3 4}
312do_test wal-5.3 {
313 execsql {
314 ROLLBACK;
315 SELECT * FROM t2;
316 }
317} {1 2}
318do_test wal-5.4 {
319 execsql {
320 CREATE TEMP TABLE t3(x UNIQUE);
321 BEGIN;
322 INSERT INTO t2 VALUES(3, 4);
323 INSERT INTO t3 VALUES('abc');
324 }
325 catchsql { INSERT INTO t3 VALUES('abc') }
drhf9c8ce32013-11-05 13:33:55 +0000326} {1 {UNIQUE constraint failed: t3.x}}
dan7c246102010-04-12 19:00:29 +0000327do_test wal-5.5 {
328 execsql {
329 COMMIT;
330 SELECT * FROM t2;
331 }
332} {1 2 3 4}
333db close
334
dan7c246102010-04-12 19:00:29 +0000335foreach sector {512 4096} {
336 sqlite3_simulate_device -sectorsize $sector
337 foreach pgsz {512 1024 2048 4096} {
mistachkinfda06be2011-08-02 00:57:34 +0000338 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +0000339 do_test wal-6.$sector.$pgsz.1 {
dane04dc882010-04-20 18:53:15 +0000340 sqlite3 db test.db -vfs devsym
dan7c246102010-04-12 19:00:29 +0000341 execsql "
dane04dc882010-04-20 18:53:15 +0000342 PRAGMA page_size = $pgsz;
dan65bddc12010-05-07 12:49:22 +0000343 PRAGMA auto_vacuum = 0;
dane04dc882010-04-20 18:53:15 +0000344 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000345 "
346 execsql "
347 CREATE TABLE t1(a, b);
348 INSERT INTO t1 VALUES(1, 2);
349 "
350 db close
351 file size test.db
352 } [expr $pgsz*2]
353
354 do_test wal-6.$sector.$pgsz.2 {
dan3de777f2010-04-17 12:31:37 +0000355 log_deleted test.db-wal
356 } {1}
dan7c246102010-04-12 19:00:29 +0000357 }
358}
359
360do_test wal-7.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000361 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +0000362 sqlite3_wal db test.db
363 execsql {
364 PRAGMA page_size = 1024;
365 CREATE TABLE t1(a, b);
366 INSERT INTO t1 VALUES(1, 2);
367 }
dan7c246102010-04-12 19:00:29 +0000368 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000369} [list 1024 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000370do_test wal-7.2 {
dan5a299f92010-05-03 11:05:08 +0000371 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000372 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000373} [list 2048 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000374
dan7c246102010-04-12 19:00:29 +0000375# Execute some transactions in auto-vacuum mode to test database file
376# truncation.
377#
danb9bf16b2010-04-14 11:23:30 +0000378do_test wal-8.1 {
dan7c246102010-04-12 19:00:29 +0000379 reopen_db
dane04dc882010-04-20 18:53:15 +0000380 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +0000381 forcedelete test.db test.db-wal
dane04dc882010-04-20 18:53:15 +0000382
383 sqlite3 db test.db
384 db function blob blob
dan7c246102010-04-12 19:00:29 +0000385 execsql {
386 PRAGMA auto_vacuum = 1;
dane04dc882010-04-20 18:53:15 +0000387 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000388 PRAGMA auto_vacuum;
389 }
dane04dc882010-04-20 18:53:15 +0000390} {wal 1}
danb9bf16b2010-04-14 11:23:30 +0000391do_test wal-8.2 {
dan7c246102010-04-12 19:00:29 +0000392 execsql {
393 PRAGMA page_size = 1024;
394 CREATE TABLE t1(x);
dan67032392010-04-17 15:42:43 +0000395 INSERT INTO t1 VALUES(blob(900));
396 INSERT INTO t1 VALUES(blob(900));
397 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
398 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
399 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
400 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
401 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
dan5a299f92010-05-03 11:05:08 +0000402 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000403 }
404 file size test.db
dan80a15262010-04-13 11:45:31 +0000405} [expr 68*1024]
danb9bf16b2010-04-14 11:23:30 +0000406do_test wal-8.3 {
dan7c246102010-04-12 19:00:29 +0000407 execsql {
408 DELETE FROM t1 WHERE rowid<54;
dan5a299f92010-05-03 11:05:08 +0000409 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000410 }
411 file size test.db
412} [expr 14*1024]
413
414# Run some "warm-body" tests to ensure that log-summary files with more
415# than 256 entries (log summaries that contain index blocks) work Ok.
416#
danb9bf16b2010-04-14 11:23:30 +0000417do_test wal-9.1 {
dan7c246102010-04-12 19:00:29 +0000418 reopen_db
419 execsql {
drhd5156602011-11-12 16:46:55 +0000420 PRAGMA cache_size=2000;
dan7c246102010-04-12 19:00:29 +0000421 CREATE TABLE t1(x PRIMARY KEY);
dan67032392010-04-17 15:42:43 +0000422 INSERT INTO t1 VALUES(blob(900));
423 INSERT INTO t1 VALUES(blob(900));
424 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
425 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
426 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
427 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
428 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
429 INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */
430 INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */
dan7c246102010-04-12 19:00:29 +0000431 }
432 file size test.db
dane04dc882010-04-20 18:53:15 +0000433} 1024
danb9bf16b2010-04-14 11:23:30 +0000434do_test wal-9.2 {
dan7c246102010-04-12 19:00:29 +0000435 sqlite3_wal db2 test.db
436 execsql {PRAGMA integrity_check } db2
437} {ok}
438
danb9bf16b2010-04-14 11:23:30 +0000439do_test wal-9.3 {
mistachkinfda06be2011-08-02 00:57:34 +0000440 forcedelete test2.db test2.db-wal
441 copy_file test.db test2.db
442 copy_file test.db-wal test2.db-wal
dan7c246102010-04-12 19:00:29 +0000443 sqlite3_wal db3 test2.db
444 execsql {PRAGMA integrity_check } db3
445} {ok}
446db3 close
447
danb9bf16b2010-04-14 11:23:30 +0000448do_test wal-9.4 {
dan5a299f92010-05-03 11:05:08 +0000449 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000450 db2 close
451 sqlite3_wal db2 test.db
452 execsql {PRAGMA integrity_check } db2
453} {ok}
454
dan80a15262010-04-13 11:45:31 +0000455foreach handle {db db2 db3} { catch { $handle close } }
456unset handle
457
danb9bf16b2010-04-14 11:23:30 +0000458#-------------------------------------------------------------------------
459# The following block of tests - wal-10.* - test that the WAL locking
dane264d982010-04-14 18:06:50 +0000460# scheme works in simple cases. This block of tests is run twice. Once
461# using multiple connections in the address space of the current process,
462# and once with all connections except one running in external processes.
danb9bf16b2010-04-14 11:23:30 +0000463#
dana4a90952010-06-15 19:07:42 +0000464do_multiclient_test tn {
dane264d982010-04-14 18:06:50 +0000465
466 # Initialize the database schema and contents.
467 #
468 do_test wal-10.$tn.1 {
469 execsql {
dan7fa65fb2011-04-01 19:14:40 +0000470 PRAGMA auto_vacuum = 0;
dana4a90952010-06-15 19:07:42 +0000471 PRAGMA journal_mode = wal;
dane264d982010-04-14 18:06:50 +0000472 CREATE TABLE t1(a, b);
473 INSERT INTO t1 VALUES(1, 2);
danb9bf16b2010-04-14 11:23:30 +0000474 SELECT * FROM t1;
dane264d982010-04-14 18:06:50 +0000475 }
dana4a90952010-06-15 19:07:42 +0000476 } {wal 1 2}
danb9bf16b2010-04-14 11:23:30 +0000477
dane264d982010-04-14 18:06:50 +0000478 # Open a transaction and write to the database using [db]. Check that [db2]
479 # is still able to read the snapshot before the transaction was opened.
480 #
481 do_test wal-10.$tn.2 {
482 execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); }
483 sql2 {SELECT * FROM t1}
484 } {1 2}
485
486 # Have [db] commit the transaction. Check that [db2] is now seeing the
487 # new, updated snapshot.
488 #
489 do_test wal-10.$tn.3 {
490 execsql { COMMIT }
491 sql2 {SELECT * FROM t1}
492 } {1 2 3 4}
493
494 # Have [db2] open a read transaction. Then write to the db via [db]. Check
495 # that [db2] is still seeing the original snapshot. Then read with [db3].
496 # [db3] should see the newly committed data.
497 #
498 do_test wal-10.$tn.4 {
499 sql2 { BEGIN ; SELECT * FROM t1}
500 } {1 2 3 4}
501 do_test wal-10.$tn.5 {
502 execsql { INSERT INTO t1 VALUES(5, 6); }
503 sql2 {SELECT * FROM t1}
504 } {1 2 3 4}
505 do_test wal-10.$tn.6 {
506 sql3 {SELECT * FROM t1}
507 } {1 2 3 4 5 6}
508 do_test wal-10.$tn.7 {
509 sql2 COMMIT
510 } {}
511
512 # Have [db2] open a write transaction. Then attempt to write to the
513 # database via [db]. This should fail (writer lock cannot be obtained).
514 #
515 # Then open a read-transaction with [db]. Commit the [db2] transaction
516 # to disk. Verify that [db] still cannot write to the database (because
517 # it is reading an old snapshot).
518 #
519 # Close the current [db] transaction. Open a new one. [db] can now write
520 # to the database (as it is not locked and [db] is reading the latest
521 # snapshot).
522 #
523 do_test wal-10.$tn.7 {
524 sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; }
525 catchsql { INSERT INTO t1 VALUES(9, 10) }
526 } {1 {database is locked}}
527 do_test wal-10.$tn.8 {
528 execsql { BEGIN ; SELECT * FROM t1 }
529 } {1 2 3 4 5 6}
530 do_test wal-10.$tn.9 {
531 sql2 COMMIT
532 catchsql { INSERT INTO t1 VALUES(9, 10) }
533 } {1 {database is locked}}
534 do_test wal-10.$tn.10 {
drh7e263722010-05-20 21:21:09 +0000535 execsql { COMMIT }
536 execsql { BEGIN }
537 execsql { INSERT INTO t1 VALUES(9, 10) }
538 execsql { COMMIT }
dane264d982010-04-14 18:06:50 +0000539 execsql { SELECT * FROM t1 }
540 } {1 2 3 4 5 6 7 8 9 10}
541
542 # Open a read transaction with [db2]. Check that this prevents [db] from
543 # checkpointing the database. But not from writing to it.
544 #
545 do_test wal-10.$tn.11 {
546 sql2 { BEGIN; SELECT * FROM t1 }
547 } {1 2 3 4 5 6 7 8 9 10}
548 do_test wal-10.$tn.12 {
dan5a299f92010-05-03 11:05:08 +0000549 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000550 } {0 {0 7 7}} ;# Reader no longer block checkpoints
dane264d982010-04-14 18:06:50 +0000551 do_test wal-10.$tn.13 {
552 execsql { INSERT INTO t1 VALUES(11, 12) }
553 sql2 {SELECT * FROM t1}
554 } {1 2 3 4 5 6 7 8 9 10}
555
drh34116ea2010-05-31 12:30:52 +0000556 # Writers do not block checkpoints any more either.
dane264d982010-04-14 18:06:50 +0000557 #
dane264d982010-04-14 18:06:50 +0000558 do_test wal-10.$tn.14 {
drh34116ea2010-05-31 12:30:52 +0000559 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000560 } {0 {0 8 7}}
danb9bf16b2010-04-14 11:23:30 +0000561
drha2ac9df2010-05-31 13:11:49 +0000562 # The following series of test cases used to verify another blocking
563 # case in WAL - a case which no longer blocks.
dane264d982010-04-14 18:06:50 +0000564 #
dane264d982010-04-14 18:06:50 +0000565 do_test wal-10.$tn.15 {
drh34116ea2010-05-31 12:30:52 +0000566 sql2 { COMMIT; BEGIN; SELECT * FROM t1; }
dane264d982010-04-14 18:06:50 +0000567 } {1 2 3 4 5 6 7 8 9 10 11 12}
568 do_test wal-10.$tn.16 {
dan5a299f92010-05-03 11:05:08 +0000569 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000570 } {0 {0 8 8}}
dan49320f82010-04-14 18:50:08 +0000571 do_test wal-10.$tn.17 {
dan5a299f92010-05-03 11:05:08 +0000572 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000573 } {0 8 8}
dan49320f82010-04-14 18:50:08 +0000574 do_test wal-10.$tn.18 {
drha2ac9df2010-05-31 13:11:49 +0000575 sql3 { BEGIN; SELECT * FROM t1 }
dane264d982010-04-14 18:06:50 +0000576 } {1 2 3 4 5 6 7 8 9 10 11 12}
dan49320f82010-04-14 18:50:08 +0000577 do_test wal-10.$tn.19 {
dane264d982010-04-14 18:06:50 +0000578 catchsql { INSERT INTO t1 VALUES(13, 14) }
drha2ac9df2010-05-31 13:11:49 +0000579 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000580 do_test wal-10.$tn.20 {
dane264d982010-04-14 18:06:50 +0000581 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000582 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
dan49320f82010-04-14 18:50:08 +0000583 do_test wal-10.$tn.21 {
dane264d982010-04-14 18:06:50 +0000584 sql3 COMMIT
drha2ac9df2010-05-31 13:11:49 +0000585 sql2 COMMIT
dane264d982010-04-14 18:06:50 +0000586 } {}
dan49320f82010-04-14 18:50:08 +0000587 do_test wal-10.$tn.22 {
dane264d982010-04-14 18:06:50 +0000588 execsql { SELECT * FROM t1 }
589 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
danb9bf16b2010-04-14 11:23:30 +0000590
drha2ac9df2010-05-31 13:11:49 +0000591 # Another series of tests that used to demonstrate blocking behavior
592 # but which now work.
dan49320f82010-04-14 18:50:08 +0000593 #
594 do_test wal-10.$tn.23 {
dan5a299f92010-05-03 11:05:08 +0000595 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000596 } {0 9 9}
dan49320f82010-04-14 18:50:08 +0000597 do_test wal-10.$tn.24 {
598 sql2 { BEGIN; SELECT * FROM t1; }
599 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
600 do_test wal-10.$tn.25 {
dan5a299f92010-05-03 11:05:08 +0000601 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000602 } {0 9 9}
dan49320f82010-04-14 18:50:08 +0000603 do_test wal-10.$tn.26 {
604 catchsql { INSERT INTO t1 VALUES(15, 16) }
drha2ac9df2010-05-31 13:11:49 +0000605 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000606 do_test wal-10.$tn.27 {
drha2ac9df2010-05-31 13:11:49 +0000607 sql3 { INSERT INTO t1 VALUES(17, 18) }
dan49320f82010-04-14 18:50:08 +0000608 } {}
609 do_test wal-10.$tn.28 {
610 code3 {
611 set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL]
612 sqlite3_step $::STMT
613 }
dan49320f82010-04-14 18:50:08 +0000614 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000615 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}
dan49320f82010-04-14 18:50:08 +0000616 do_test wal-10.$tn.29 {
drha2ac9df2010-05-31 13:11:49 +0000617 execsql { INSERT INTO t1 VALUES(19, 20) }
dan5a299f92010-05-03 11:05:08 +0000618 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000619 } {0 {0 3 0}}
dan49320f82010-04-14 18:50:08 +0000620 do_test wal-10.$tn.30 {
621 code3 { sqlite3_finalize $::STMT }
dan5a299f92010-05-03 11:05:08 +0000622 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000623 } {0 3 0}
dan49320f82010-04-14 18:50:08 +0000624
625 # At one point, if a reader failed to upgrade to a writer because it
626 # was reading an old snapshot, the write-locks were not being released.
627 # Test that this bug has been fixed.
628 #
629 do_test wal-10.$tn.31 {
drha2ac9df2010-05-31 13:11:49 +0000630 sql2 COMMIT
dan49320f82010-04-14 18:50:08 +0000631 execsql { BEGIN ; SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000632 sql2 { INSERT INTO t1 VALUES(21, 22) }
633 catchsql { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000634 } {1 {database is locked}}
635 do_test wal-10.$tn.32 {
636 # This statement would fail when the bug was present.
drha2ac9df2010-05-31 13:11:49 +0000637 sql2 { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000638 } {}
639 do_test wal-10.$tn.33 {
640 execsql { SELECT * FROM t1 ; COMMIT }
drha2ac9df2010-05-31 13:11:49 +0000641 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
dan49320f82010-04-14 18:50:08 +0000642 do_test wal-10.$tn.34 {
643 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000644 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
dan49320f82010-04-14 18:50:08 +0000645
dan8b348af2010-04-27 18:43:16 +0000646 # Test that if a checkpointer cannot obtain the required locks, it
647 # releases all locks before returning a busy error.
648 #
649 do_test wal-10.$tn.35 {
650 execsql {
651 DELETE FROM t1;
652 INSERT INTO t1 VALUES('a', 'b');
653 INSERT INTO t1 VALUES('c', 'd');
654 }
655 sql2 {
656 BEGIN;
657 SELECT * FROM t1;
658 }
659 } {a b c d}
dan8b348af2010-04-27 18:43:16 +0000660 do_test wal-10.$tn.36 {
dan5a299f92010-05-03 11:05:08 +0000661 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000662 } {0 {0 8 8}}
dan8b348af2010-04-27 18:43:16 +0000663 do_test wal-10.$tn.36 {
664 sql3 { INSERT INTO t1 VALUES('e', 'f') }
665 sql2 { SELECT * FROM t1 }
666 } {a b c d}
667 do_test wal-10.$tn.37 {
668 sql2 COMMIT
dan5a299f92010-05-03 11:05:08 +0000669 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000670 } {0 9 9}
dane264d982010-04-14 18:06:50 +0000671}
672
dan4cc6fb62010-04-15 16:45:34 +0000673#-------------------------------------------------------------------------
674# This block of tests, wal-11.*, test that nothing goes terribly wrong
675# if frames must be written to the log file before a transaction is
676# committed (in order to free up memory).
677#
678do_test wal-11.1 {
679 reopen_db
680 execsql {
681 PRAGMA cache_size = 10;
682 PRAGMA page_size = 1024;
683 CREATE TABLE t1(x PRIMARY KEY);
684 }
685 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
dane04dc882010-04-20 18:53:15 +0000686} {1 3}
dan4cc6fb62010-04-15 16:45:34 +0000687do_test wal-11.2 {
dan5a299f92010-05-03 11:05:08 +0000688 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000689 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000690} [list 3 [wal_file_size 3 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000691do_test wal-11.3 {
dan67032392010-04-17 15:42:43 +0000692 execsql { INSERT INTO t1 VALUES( blob(900) ) }
dan97a31352010-04-16 13:59:31 +0000693 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000694} [list 3 [wal_file_size 4 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000695
696do_test wal-11.4 {
697 execsql {
698 BEGIN;
dan67032392010-04-17 15:42:43 +0000699 INSERT INTO t1 SELECT blob(900) FROM t1; -- 2
700 INSERT INTO t1 SELECT blob(900) FROM t1; -- 4
701 INSERT INTO t1 SELECT blob(900) FROM t1; -- 8
702 INSERT INTO t1 SELECT blob(900) FROM t1; -- 16
dan4cc6fb62010-04-15 16:45:34 +0000703 }
dan97a31352010-04-16 13:59:31 +0000704 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000705} [list 3 [wal_file_size 32 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000706do_test wal-11.5 {
707 execsql {
708 SELECT count(*) FROM t1;
709 PRAGMA integrity_check;
710 }
711} {16 ok}
712do_test wal-11.6 {
713 execsql COMMIT
dan97a31352010-04-16 13:59:31 +0000714 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000715} [list 3 [wal_file_size 41 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000716do_test wal-11.7 {
717 execsql {
718 SELECT count(*) FROM t1;
719 PRAGMA integrity_check;
720 }
721} {16 ok}
722do_test wal-11.8 {
dan5a299f92010-05-03 11:05:08 +0000723 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000724 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000725} [list 37 [wal_file_size 41 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000726do_test wal-11.9 {
727 db close
dan3de777f2010-04-17 12:31:37 +0000728 list [expr [file size test.db]/1024] [log_deleted test.db-wal]
729} {37 1}
dan67032392010-04-17 15:42:43 +0000730sqlite3_wal db test.db
drh41f89cc2013-03-26 01:07:50 +0000731set nWal 39
drh9b4c59f2013-04-15 17:03:42 +0000732if {[permutation]!="mmap"} {set nWal 37}
drh188d4882013-04-08 20:47:49 +0000733ifcapable !mmap {set nWal 37}
dan4cc6fb62010-04-15 16:45:34 +0000734do_test wal-11.10 {
735 execsql {
736 PRAGMA cache_size = 10;
737 BEGIN;
dan67032392010-04-17 15:42:43 +0000738 INSERT INTO t1 SELECT blob(900) FROM t1; -- 32
dan4cc6fb62010-04-15 16:45:34 +0000739 SELECT count(*) FROM t1;
740 }
dan97a31352010-04-16 13:59:31 +0000741 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000742} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000743do_test wal-11.11 {
744 execsql {
745 SELECT count(*) FROM t1;
746 ROLLBACK;
747 SELECT count(*) FROM t1;
748 }
749} {32 16}
750do_test wal-11.12 {
dan97a31352010-04-16 13:59:31 +0000751 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000752} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000753do_test wal-11.13 {
754 execsql {
dan67032392010-04-17 15:42:43 +0000755 INSERT INTO t1 VALUES( blob(900) );
dan4cc6fb62010-04-15 16:45:34 +0000756 SELECT count(*) FROM t1;
757 PRAGMA integrity_check;
758 }
759} {17 ok}
760do_test wal-11.14 {
dan97a31352010-04-16 13:59:31 +0000761 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000762} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000763
764
dan4a4b01d2010-04-16 11:30:18 +0000765#-------------------------------------------------------------------------
dan97a31352010-04-16 13:59:31 +0000766# This block of tests, wal-12.*, tests the fix for a problem that
767# could occur if a log that is a prefix of an older log is written
768# into a reused log file.
dan4a4b01d2010-04-16 11:30:18 +0000769#
770reopen_db
771do_test wal-12.1 {
772 execsql {
773 PRAGMA page_size = 1024;
774 CREATE TABLE t1(x, y);
775 CREATE TABLE t2(x, y);
776 INSERT INTO t1 VALUES('A', 1);
777 }
dan97a31352010-04-16 13:59:31 +0000778 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000779} [list 1 [wal_file_size 5 1024]]
dan4a4b01d2010-04-16 11:30:18 +0000780do_test wal-12.2 {
781 db close
dane04dc882010-04-20 18:53:15 +0000782 sqlite3 db test.db
dan4a4b01d2010-04-16 11:30:18 +0000783 execsql {
dane04dc882010-04-20 18:53:15 +0000784 PRAGMA synchronous = normal;
dan4a4b01d2010-04-16 11:30:18 +0000785 UPDATE t1 SET y = 0 WHERE x = 'A';
786 }
787 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
788} {3 1}
789do_test wal-12.3 {
790 execsql { INSERT INTO t2 VALUES('B', 1) }
791 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
792} {3 2}
dan4a4b01d2010-04-16 11:30:18 +0000793do_test wal-12.4 {
mistachkinfda06be2011-08-02 00:57:34 +0000794 forcecopy test.db test2.db
795 forcecopy test.db-wal test2.db-wal
dan4a4b01d2010-04-16 11:30:18 +0000796 sqlite3_wal db2 test2.db
dan4a4b01d2010-04-16 11:30:18 +0000797 execsql { SELECT * FROM t2 } db2
798} {B 1}
799db2 close
dan4a4b01d2010-04-16 11:30:18 +0000800do_test wal-12.5 {
801 execsql {
dan5a299f92010-05-03 11:05:08 +0000802 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000803 UPDATE t2 SET y = 2 WHERE x = 'B';
dan5a299f92010-05-03 11:05:08 +0000804 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000805 UPDATE t1 SET y = 1 WHERE x = 'A';
dan5a299f92010-05-03 11:05:08 +0000806 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000807 UPDATE t1 SET y = 0 WHERE x = 'A';
dan4a4b01d2010-04-16 11:30:18 +0000808 }
danbdd9af02010-11-18 16:14:24 +0000809 execsql { SELECT * FROM t2 }
dan4a4b01d2010-04-16 11:30:18 +0000810} {B 2}
dance4f05f2010-04-22 19:14:13 +0000811do_test wal-12.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000812 forcecopy test.db test2.db
813 forcecopy test.db-wal test2.db-wal
dan4a4b01d2010-04-16 11:30:18 +0000814 sqlite3_wal db2 test2.db
815 execsql { SELECT * FROM t2 } db2
816} {B 2}
817db2 close
dance4f05f2010-04-22 19:14:13 +0000818db close
819
820#-------------------------------------------------------------------------
821# Test large log summaries.
822#
dan8d6ad1c2010-05-04 10:36:20 +0000823# In this case "large" usually means a log file that requires a wal-index
824# mapping larger than 64KB (the default initial allocation). A 64KB wal-index
825# is large enough for a log file that contains approximately 13100 frames.
826# So the following tests create logs containing at least this many frames.
827#
828# wal-13.1.*: This test case creates a very large log file within the
829# file-system (around 200MB). The log file does not contain
830# any valid frames. Test that the database file can still be
831# opened and queried, and that the invalid log file causes no
832# problems.
833#
834# wal-13.2.*: Test that a process may create a large log file and query
835# the database (including the log file that it itself created).
836#
837# wal-13.3.*: Test that if a very large log file is created, and then a
838# second connection is opened on the database file, it is possible
839# to query the database (and the very large log) using the
840# second connection.
841#
842# wal-13.4.*: Same test as wal-13.3.*. Except in this case the second
843# connection is opened by an external process.
844#
dan31f98fc2010-04-27 05:42:32 +0000845do_test wal-13.1.1 {
dance4f05f2010-04-22 19:14:13 +0000846 list [file exists test.db] [file exists test.db-wal]
847} {1 0}
dan31f98fc2010-04-27 05:42:32 +0000848do_test wal-13.1.2 {
dance4f05f2010-04-22 19:14:13 +0000849 set fd [open test.db-wal w]
850 seek $fd [expr 200*1024*1024]
851 puts $fd ""
852 close $fd
853 sqlite3 db test.db
854 execsql { SELECT * FROM t2 }
855} {B 2}
dan31f98fc2010-04-27 05:42:32 +0000856do_test wal-13.1.3 {
dance4f05f2010-04-22 19:14:13 +0000857 db close
858 file exists test.db-wal
859} {0}
dan8d6ad1c2010-05-04 10:36:20 +0000860
861do_test wal-13.2.1 {
dance4f05f2010-04-22 19:14:13 +0000862 sqlite3 db test.db
863 execsql { SELECT count(*) FROM t2 }
864} {1}
dan8d6ad1c2010-05-04 10:36:20 +0000865do_test wal-13.2.2 {
dan65bddc12010-05-07 12:49:22 +0000866 db function blob blob
dan8d6ad1c2010-05-04 10:36:20 +0000867 for {set i 0} {$i < 16} {incr i} {
dan65bddc12010-05-07 12:49:22 +0000868 execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 }
dance4f05f2010-04-22 19:14:13 +0000869 }
870 execsql { SELECT count(*) FROM t2 }
dan8d6ad1c2010-05-04 10:36:20 +0000871} [expr int(pow(2, 16))]
dan65bddc12010-05-07 12:49:22 +0000872do_test wal-13.2.3 {
dan10f5a502010-06-23 15:55:43 +0000873 expr [file size test.db-wal] > [wal_file_size 33000 1024]
danc6315a42010-05-07 13:52:42 +0000874} 1
dance4f05f2010-04-22 19:14:13 +0000875
dana4a90952010-06-15 19:07:42 +0000876do_multiclient_test tn {
877 incr tn 2
dan31f98fc2010-04-27 05:42:32 +0000878
879 do_test wal-13.$tn.0 {
dana4a90952010-06-15 19:07:42 +0000880 sql1 {
dan31f98fc2010-04-27 05:42:32 +0000881 PRAGMA journal_mode = WAL;
882 CREATE TABLE t1(x);
dan8d6ad1c2010-05-04 10:36:20 +0000883 INSERT INTO t1 SELECT randomblob(800);
dan31f98fc2010-04-27 05:42:32 +0000884 }
dana4a90952010-06-15 19:07:42 +0000885 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000886 } {1}
887
888 for {set ii 1} {$ii<16} {incr ii} {
889 do_test wal-13.$tn.$ii.a {
dana4a90952010-06-15 19:07:42 +0000890 sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 }
891 sql2 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000892 } [expr (1<<$ii)]
893 do_test wal-13.$tn.$ii.b {
dana4a90952010-06-15 19:07:42 +0000894 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000895 } [expr (1<<$ii)]
896 do_test wal-13.$tn.$ii.c {
dana4a90952010-06-15 19:07:42 +0000897 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000898 } [expr (1<<$ii)]
drh1b48aa42010-04-30 17:28:35 +0000899 do_test wal-13.$tn.$ii.d {
dana4a90952010-06-15 19:07:42 +0000900 sql1 { PRAGMA integrity_check }
drh1b48aa42010-04-30 17:28:35 +0000901 } {ok}
dan31f98fc2010-04-27 05:42:32 +0000902 }
dan31f98fc2010-04-27 05:42:32 +0000903}
dan4a4b01d2010-04-16 11:30:18 +0000904
dan31c03902010-04-29 14:51:33 +0000905#-------------------------------------------------------------------------
906# Check a fun corruption case has been fixed.
907#
908# The problem was that after performing a checkpoint using a connection
909# that had an out-of-date pager-cache, the next time the connection was
910# used it did not realize the cache was out-of-date and proceeded to
911# operate with an inconsistent cache. Leading to corruption.
912#
dan31c03902010-04-29 14:51:33 +0000913catch { db close }
914catch { db2 close }
915catch { db3 close }
mistachkinfda06be2011-08-02 00:57:34 +0000916forcedelete test.db test.db-wal
dan31c03902010-04-29 14:51:33 +0000917sqlite3 db test.db
918sqlite3 db2 test.db
dan31c03902010-04-29 14:51:33 +0000919do_test wal-14 {
920 execsql {
921 PRAGMA journal_mode = WAL;
922 CREATE TABLE t1(a PRIMARY KEY, b);
923 INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
924 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
925 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
926 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
927 }
928
929 db2 eval {
930 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
931 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
932 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
933 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
934 }
935
dan5a299f92010-05-03 11:05:08 +0000936 # After executing the "PRAGMA wal_checkpoint", connection [db] was being
dan31c03902010-04-29 14:51:33 +0000937 # left with an inconsistent cache. Running the CREATE INDEX statement
938 # in this state led to database corruption.
939 catchsql {
dan5a299f92010-05-03 11:05:08 +0000940 PRAGMA wal_checkpoint;
dan31c03902010-04-29 14:51:33 +0000941 CREATE INDEX i1 on t1(b);
942 }
943
944 db2 eval { PRAGMA integrity_check }
945} {ok}
946
dan185cca62010-04-29 14:58:53 +0000947catch { db close }
948catch { db2 close }
dan87c1fe12010-05-03 12:14:15 +0000949
950#-------------------------------------------------------------------------
951# The following block of tests - wal-15.* - focus on testing the
952# implementation of the sqlite3_wal_checkpoint() interface.
953#
mistachkinfda06be2011-08-02 00:57:34 +0000954forcedelete test.db test.db-wal
dan87c1fe12010-05-03 12:14:15 +0000955sqlite3 db test.db
956do_test wal-15.1 {
957 execsql {
dan65bddc12010-05-07 12:49:22 +0000958 PRAGMA auto_vacuum = 0;
dan87c1fe12010-05-03 12:14:15 +0000959 PRAGMA page_size = 1024;
960 PRAGMA journal_mode = WAL;
961 }
962 execsql {
963 CREATE TABLE t1(a, b);
964 INSERT INTO t1 VALUES(1, 2);
965 }
966} {}
967
968# Test that an error is returned if the database name is not recognized
969#
970do_test wal-15.2.1 {
971 sqlite3_wal_checkpoint db aux
972} {SQLITE_ERROR}
973do_test wal-15.2.2 {
974 sqlite3_errcode db
975} {SQLITE_ERROR}
976do_test wal-15.2.3 {
977 sqlite3_errmsg db
978} {unknown database: aux}
979
980# Test that an error is returned if an attempt is made to checkpoint
981# if a transaction is open on the database.
982#
983do_test wal-15.3.1 {
984 execsql {
985 BEGIN;
986 INSERT INTO t1 VALUES(3, 4);
987 }
988 sqlite3_wal_checkpoint db main
989} {SQLITE_LOCKED}
990do_test wal-15.3.2 {
991 sqlite3_errcode db
992} {SQLITE_LOCKED}
993do_test wal-15.3.3 {
994 sqlite3_errmsg db
995} {database table is locked}
996
dandcb11692010-05-31 14:18:45 +0000997# Earlier versions returned an error is returned if the db cannot be
998# checkpointed because of locks held by another connection. Check that
999# this is no longer the case.
dan87c1fe12010-05-03 12:14:15 +00001000#
1001sqlite3 db2 test.db
1002do_test wal-15.4.1 {
1003 execsql {
1004 BEGIN;
1005 SELECT * FROM t1;
1006 } db2
1007} {1 2}
1008do_test wal-15.4.2 {
1009 execsql { COMMIT }
1010 sqlite3_wal_checkpoint db
dandcb11692010-05-31 14:18:45 +00001011} {SQLITE_OK}
dan87c1fe12010-05-03 12:14:15 +00001012do_test wal-15.4.3 {
1013 sqlite3_errmsg db
dandcb11692010-05-31 14:18:45 +00001014} {not an error}
dan87c1fe12010-05-03 12:14:15 +00001015
1016# After [db2] drops its lock, [db] may checkpoint the db.
1017#
1018do_test wal-15.4.4 {
1019 execsql { COMMIT } db2
1020 sqlite3_wal_checkpoint db
1021} {SQLITE_OK}
1022do_test wal-15.4.5 {
1023 sqlite3_errmsg db
1024} {not an error}
1025do_test wal-15.4.6 {
1026 file size test.db
1027} [expr 1024*2]
1028
1029catch { db2 close }
1030catch { db close }
danaf0cfd32010-05-03 15:58:50 +00001031
1032#-------------------------------------------------------------------------
1033# The following block of tests - wal-16.* - test that if a NULL pointer or
1034# an empty string is passed as the second argument of the wal_checkpoint()
1035# API, an attempt is made to checkpoint all attached databases.
1036#
1037foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} {
1038 1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1
1039 2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1
danf2b8dd52010-11-18 19:28:01 +00001040 3 {db eval "PRAGMA wal_checkpoint"} {0 10 10} 1 1
danaf0cfd32010-05-03 15:58:50 +00001041
1042 4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0
1043 5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1
1044 6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0
danbdd9af02010-11-18 16:14:24 +00001045 7 {db eval "PRAGMA main.wal_checkpoint"} {0 10 10} 1 0
dan0774bb52011-12-19 10:07:56 +00001046 8 {db eval "PRAGMA aux.wal_checkpoint"} {0 13 13} 0 1
danbdd9af02010-11-18 16:14:24 +00001047 9 {db eval "PRAGMA temp.wal_checkpoint"} {0 -1 -1} 0 0
danaf0cfd32010-05-03 15:58:50 +00001048} {
1049 do_test wal-16.$tn.1 {
mistachkinfda06be2011-08-02 00:57:34 +00001050 forcedelete test2.db test2.db-wal test2.db-journal
1051 forcedelete test.db test.db-wal test.db-journal
danaf0cfd32010-05-03 15:58:50 +00001052
1053 sqlite3 db test.db
1054 execsql {
1055 ATTACH 'test2.db' AS aux;
dan65bddc12010-05-07 12:49:22 +00001056 PRAGMA main.auto_vacuum = 0;
1057 PRAGMA aux.auto_vacuum = 0;
danaf0cfd32010-05-03 15:58:50 +00001058 PRAGMA main.journal_mode = WAL;
1059 PRAGMA aux.journal_mode = WAL;
dan0774bb52011-12-19 10:07:56 +00001060 PRAGMA main.synchronous = NORMAL;
1061 PRAGMA aux.synchronous = NORMAL;
danaf0cfd32010-05-03 15:58:50 +00001062 }
1063 } {wal wal}
1064
1065 do_test wal-16.$tn.2 {
1066 execsql {
1067 CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b));
1068 CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b));
1069
1070 INSERT INTO t2 VALUES(1, randomblob(1000));
1071 INSERT INTO t2 VALUES(2, randomblob(1000));
1072 INSERT INTO t1 SELECT * FROM t2;
1073 }
1074
1075 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +00001076 } [list [expr 1*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001077 do_test wal-16.$tn.3 {
1078 list [file size test2.db] [file size test2.db-wal]
dan0774bb52011-12-19 10:07:56 +00001079 } [list [expr 1*1024] [wal_file_size 13 1024]]
danaf0cfd32010-05-03 15:58:50 +00001080
1081 do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res
1082
1083 do_test wal-16.$tn.5 {
1084 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +00001085 } [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001086
1087 do_test wal-16.$tn.6 {
1088 list [file size test2.db] [file size test2.db-wal]
dan0774bb52011-12-19 10:07:56 +00001089 } [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 13 1024]]
danaf0cfd32010-05-03 15:58:50 +00001090
1091 catch { db close }
1092}
1093
dan8d6ad1c2010-05-04 10:36:20 +00001094#-------------------------------------------------------------------------
1095# The following tests - wal-17.* - attempt to verify that the correct
1096# number of "padding" frames are appended to the log file when a transaction
1097# is committed in synchronous=FULL mode.
1098#
1099# Do this by creating a database that uses 512 byte pages. Then writing
1100# a transaction that modifies 171 pages. In synchronous=NORMAL mode, this
1101# produces a log file of:
1102#
dan10f5a502010-06-23 15:55:43 +00001103# 32 + (24+512)*171 = 90312 bytes.
dan8d6ad1c2010-05-04 10:36:20 +00001104#
1105# Slightly larger than 11*8192 = 90112 bytes.
1106#
1107# Run the test using various different sector-sizes. In each case, the
1108# WAL code should write the 90300 bytes of log file containing the
1109# transaction, then append as may frames as are required to extend the
1110# log file so that no part of the next transaction will be written into
1111# a disk-sector used by transaction just committed.
1112#
1113set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000]
1114catch { db close }
dan10f5a502010-06-23 15:55:43 +00001115foreach {tn sectorsize logsize} "
1116 1 128 [wal_file_size 172 512]
1117 2 256 [wal_file_size 172 512]
1118 3 512 [wal_file_size 172 512]
1119 4 1024 [wal_file_size 172 512]
1120 5 2048 [wal_file_size 172 512]
1121 6 4096 [wal_file_size 176 512]
1122 7 8192 [wal_file_size 184 512]
1123" {
mistachkinfda06be2011-08-02 00:57:34 +00001124 forcedelete test.db test.db-wal test.db-journal
dan8d6ad1c2010-05-04 10:36:20 +00001125 sqlite3_simulate_device -sectorsize $sectorsize
1126 sqlite3 db test.db -vfs devsym
1127
1128 do_test wal-17.$tn.1 {
1129 execsql {
1130 PRAGMA auto_vacuum = 0;
1131 PRAGMA page_size = 512;
drhd5156602011-11-12 16:46:55 +00001132 PRAGMA cache_size = -2000;
dan8d6ad1c2010-05-04 10:36:20 +00001133 PRAGMA journal_mode = WAL;
1134 PRAGMA synchronous = FULL;
1135 }
1136 execsql {
1137 BEGIN;
1138 CREATE TABLE t(x);
1139 }
1140 for {set i 0} {$i<166} {incr i} {
1141 execsql { INSERT INTO t VALUES(randomblob(400)) }
1142 }
1143 execsql COMMIT
1144
1145 file size test.db-wal
1146 } $logsize
1147
1148 do_test wal-17.$tn.2 {
1149 file size test.db
1150 } 512
1151
1152 do_test wal-17.$tn.3 {
1153 db close
1154 file size test.db
1155 } [expr 512*171]
1156}
1157sqlite3_test_control_pending_byte $old_pending_byte
1158
danb6e099a2010-05-04 14:47:39 +00001159#-------------------------------------------------------------------------
1160# This test - wal-18.* - verifies a couple of specific conditions that
1161# may be encountered while recovering a log file are handled correctly:
1162#
1163# wal-18.1.* When the first 32-bits of a frame checksum is correct but
1164# the second 32-bits are false, and
1165#
1166# wal-18.2.* When the page-size field that occurs at the start of a log
1167# file is a power of 2 greater than 16384 or smaller than 512.
1168#
mistachkinfda06be2011-08-02 00:57:34 +00001169forcedelete test.db test.db-wal test.db-journal
danb6e099a2010-05-04 14:47:39 +00001170do_test wal-18.0 {
1171 sqlite3 db test.db
1172 execsql {
1173 PRAGMA page_size = 1024;
1174 PRAGMA auto_vacuum = 0;
1175 PRAGMA journal_mode = WAL;
1176 PRAGMA synchronous = OFF;
1177
1178 CREATE TABLE t1(a, b, UNIQUE(a, b));
1179 INSERT INTO t1 VALUES(0, 0);
1180 PRAGMA wal_checkpoint;
1181
1182 INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2
1183 INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4
1184 INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6
1185 }
1186
mistachkinfda06be2011-08-02 00:57:34 +00001187 forcecopy test.db testX.db
1188 forcecopy test.db-wal testX.db-wal
danb6e099a2010-05-04 14:47:39 +00001189 db close
1190 list [file size testX.db] [file size testX.db-wal]
dan10f5a502010-06-23 15:55:43 +00001191} [list [expr 3*1024] [wal_file_size 6 1024]]
danb6e099a2010-05-04 14:47:39 +00001192
danc9e46652010-05-06 13:36:47 +00001193unset -nocomplain nFrame result
danb6e099a2010-05-04 14:47:39 +00001194foreach {nFrame result} {
1195 0 {0 0}
1196 1 {0 0}
1197 2 {0 0 1 2}
1198 3 {0 0 1 2}
1199 4 {0 0 1 2 3 4}
1200 5 {0 0 1 2 3 4}
1201 6 {0 0 1 2 3 4 5 6}
1202} {
1203 do_test wal-18.1.$nFrame {
mistachkinfda06be2011-08-02 00:57:34 +00001204 forcecopy testX.db test.db
1205 forcecopy testX.db-wal test.db-wal
danb6e099a2010-05-04 14:47:39 +00001206
drh23ea97b2010-05-20 16:45:58 +00001207 hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000
danb6e099a2010-05-04 14:47:39 +00001208
1209 sqlite3 db test.db
1210 execsql {
1211 SELECT * FROM t1;
1212 PRAGMA integrity_check;
1213 }
1214 } [concat $result ok]
1215 db close
1216}
1217
1218proc randomblob {pgsz} {
1219 sqlite3 rbdb :memory:
1220 set blob [rbdb one {SELECT randomblob($pgsz)}]
1221 rbdb close
1222 set blob
1223}
1224
1225proc logcksum {ckv1 ckv2 blob} {
1226 upvar $ckv1 c1
1227 upvar $ckv2 c2
1228
drhed1d84e2012-05-11 20:43:47 +00001229 # Since the magic number at the start of the -wal file header is
1230 # 931071618 that indicates that the content should always be read as
1231 # little-endian.
1232 #
1233 set scanpattern i*
danb6e099a2010-05-04 14:47:39 +00001234
danb8fd6c22010-05-24 10:39:36 +00001235 binary scan $blob $scanpattern values
drh4c1cb6a2010-05-19 19:09:37 +00001236 foreach {v1 v2} $values {
1237 set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
1238 set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
1239 }
danb6e099a2010-05-04 14:47:39 +00001240}
1241
mistachkinfda06be2011-08-02 00:57:34 +00001242forcecopy test.db testX.db
danb6e099a2010-05-04 14:47:39 +00001243foreach {tn pgsz works} {
1244 1 128 0
1245 2 256 0
1246 3 512 1
1247 4 1024 1
1248 5 2048 1
1249 6 4096 1
1250 7 8192 1
1251 8 16384 1
1252 9 32768 1
drhb2eced52010-08-12 02:41:12 +00001253 10 65536 1
1254 11 131072 0
drh4c1cb6a2010-05-19 19:09:37 +00001255 11 1016 0
danb6e099a2010-05-04 14:47:39 +00001256} {
1257
dan65bddc12010-05-07 12:49:22 +00001258 if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} {
1259 set works 0
1260 }
1261
danb6e099a2010-05-04 14:47:39 +00001262 for {set pg 1} {$pg <= 3} {incr pg} {
mistachkinfda06be2011-08-02 00:57:34 +00001263 forcecopy testX.db test.db
1264 forcedelete test.db-wal
danb6e099a2010-05-04 14:47:39 +00001265
1266 # Check that the database now exists and consists of three pages. And
1267 # that there is no associated wal file.
1268 #
1269 do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0
1270 do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1
1271 do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3]
1272
1273 do_test wal-18.2.$tn.$pg.4 {
1274
1275 # Create a wal file that contains a single frame (database page
1276 # number $pg) with the commit flag set. The frame checksum is
1277 # correct, but the contents of the database page are corrupt.
1278 #
1279 # The page-size in the log file header is set to $pgsz. If the
1280 # WAL code considers $pgsz to be a valid SQLite database file page-size,
1281 # the database will be corrupt (because the garbage frame contents
1282 # will be treated as valid content). If $pgsz is invalid (too small
1283 # or too large), the db will not be corrupt as the log file will
1284 # be ignored.
1285 #
drh7e263722010-05-20 21:21:09 +00001286 set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23]
danb6e099a2010-05-04 14:47:39 +00001287 set framebody [randomblob $pgsz]
drh7e263722010-05-20 21:21:09 +00001288 set framehdr [binary format IIII $pg 5 22 23]
1289 set c1 0
1290 set c2 0
dan71d89912010-05-24 13:57:42 +00001291 logcksum c1 c2 $walhdr
dan10f5a502010-06-23 15:55:43 +00001292
1293 append walhdr [binary format II $c1 $c2]
dan71d89912010-05-24 13:57:42 +00001294 logcksum c1 c2 [string range $framehdr 0 7]
danb6e099a2010-05-04 14:47:39 +00001295 logcksum c1 c2 $framebody
drh7e263722010-05-20 21:21:09 +00001296 set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2]
danb8fd6c22010-05-24 10:39:36 +00001297
danb6e099a2010-05-04 14:47:39 +00001298 set fd [open test.db-wal w]
1299 fconfigure $fd -encoding binary -translation binary
1300 puts -nonewline $fd $walhdr
1301 puts -nonewline $fd $framehdr
1302 puts -nonewline $fd $framebody
1303 close $fd
1304
1305 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +00001306 } [wal_file_size 1 $pgsz]
danb6e099a2010-05-04 14:47:39 +00001307
1308 do_test wal-18.2.$tn.$pg.5 {
1309 sqlite3 db test.db
1310 set rc [catch { db one {PRAGMA integrity_check} } msg]
1311 expr { $rc!=0 || $msg!="ok" }
1312 } $works
1313
1314 db close
1315 }
1316}
1317
danb7d53f52010-05-06 17:28:08 +00001318#-------------------------------------------------------------------------
1319# The following test - wal-19.* - fixes a bug that was present during
1320# development.
1321#
1322# When a database connection in WAL mode is closed, it attempts an
1323# EXCLUSIVE lock on the database file. If the lock is obtained, the
1324# connection knows that it is the last connection to disconnect from
1325# the database, so it runs a checkpoint operation. The bug was that
1326# the connection was not updating its private copy of the wal-index
1327# header before doing so, meaning that it could checkpoint an old
1328# snapshot.
1329#
1330do_test wal-19.1 {
mistachkinfda06be2011-08-02 00:57:34 +00001331 forcedelete test.db test.db-wal test.db-journal
danb7d53f52010-05-06 17:28:08 +00001332 sqlite3 db test.db
1333 sqlite3 db2 test.db
1334 execsql {
1335 PRAGMA journal_mode = WAL;
1336 CREATE TABLE t1(a, b);
1337 INSERT INTO t1 VALUES(1, 2);
1338 INSERT INTO t1 VALUES(3, 4);
1339 }
1340 execsql { SELECT * FROM t1 } db2
1341} {1 2 3 4}
1342do_test wal-19.2 {
1343 execsql {
1344 INSERT INTO t1 VALUES(5, 6);
1345 SELECT * FROM t1;
1346 }
1347} {1 2 3 4 5 6}
1348do_test wal-19.3 {
1349 db close
1350 db2 close
1351 file exists test.db-wal
1352} {0}
1353do_test wal-19.4 {
1354 # When the bug was present, the following was returning {1 2 3 4} only,
1355 # as [db2] had an out-of-date copy of the wal-index header when it was
1356 # closed.
1357 #
1358 sqlite3 db test.db
1359 execsql { SELECT * FROM t1 }
1360} {1 2 3 4 5 6}
1361
dan998ad212010-05-07 06:59:08 +00001362#-------------------------------------------------------------------------
1363# This test - wal-20.* - uses two connections. One in this process and
1364# the other in an external process. The procedure is:
1365#
1366# 1. Using connection 1, create the database schema.
1367#
1368# 2. Using connection 2 (in an external process), add so much
1369# data to the database without checkpointing that a wal-index
1370# larger than 64KB is required.
1371#
1372# 3. Using connection 1, checkpoint the database. Make sure all
1373# the data is present and the database is not corrupt.
1374#
1375# At one point, SQLite was failing to grow the mapping of the wal-index
1376# file in step 3 and the checkpoint was corrupting the database file.
1377#
1378do_test wal-20.1 {
shaneha10069d2010-05-11 02:46:16 +00001379 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +00001380 forcedelete test.db test.db-wal test.db-journal
dan998ad212010-05-07 06:59:08 +00001381 sqlite3 db test.db
1382 execsql {
1383 PRAGMA journal_mode = WAL;
1384 CREATE TABLE t1(x);
1385 INSERT INTO t1 VALUES(randomblob(900));
1386 SELECT count(*) FROM t1;
1387 }
1388} {wal 1}
1389do_test wal-20.2 {
1390 set ::buddy [launch_testfixture]
1391 testfixture $::buddy {
1392 sqlite3 db test.db
1393 db transaction { db eval {
1394 PRAGMA wal_autocheckpoint = 0;
1395 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
1396 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
1397 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
1398 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
1399 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
1400 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
1401 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
1402 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
1403 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
1404 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
1405 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
1406 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
1407 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
1408 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
1409 } }
1410 }
1411} {0}
1412do_test wal-20.3 {
1413 close $::buddy
danbdd9af02010-11-18 16:14:24 +00001414 execsql { PRAGMA wal_checkpoint }
1415 execsql { SELECT count(*) FROM t1 }
dan998ad212010-05-07 06:59:08 +00001416} {16384}
1417do_test wal-20.4 {
1418 db close
1419 sqlite3 db test.db
1420 execsql { SELECT count(*) FROM t1 }
1421} {16384}
1422integrity_check wal-20.5
1423
danaf0cfd32010-05-03 15:58:50 +00001424catch { db2 close }
1425catch { db close }
dan6e6bd562010-06-02 18:59:03 +00001426
1427do_test wal-21.1 {
dan10f5a502010-06-23 15:55:43 +00001428 faultsim_delete_and_reopen
dan6e6bd562010-06-02 18:59:03 +00001429 execsql {
1430 PRAGMA journal_mode = WAL;
1431 CREATE TABLE t1(a, b);
1432 INSERT INTO t1 VALUES(1, 2);
1433 INSERT INTO t1 VALUES(3, 4);
1434 INSERT INTO t1 VALUES(5, 6);
1435 INSERT INTO t1 VALUES(7, 8);
1436 INSERT INTO t1 VALUES(9, 10);
1437 INSERT INTO t1 VALUES(11, 12);
1438 }
1439} {wal}
1440do_test wal-21.2 {
1441 execsql {
1442 PRAGMA cache_size = 10;
1443 PRAGMA wal_checkpoint;
1444 BEGIN;
1445 SAVEPOINT s;
1446 INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1;
1447 ROLLBACK TO s;
1448 COMMIT;
dan6e6bd562010-06-02 18:59:03 +00001449 }
danbdd9af02010-11-18 16:14:24 +00001450 execsql { SELECT * FROM t1 }
dan6e6bd562010-06-02 18:59:03 +00001451} {1 2 3 4 5 6 7 8 9 10 11 12}
1452do_test wal-21.3 {
1453 execsql { PRAGMA integrity_check }
1454} {ok}
1455
dan4bcc4982010-08-16 19:23:02 +00001456#-------------------------------------------------------------------------
1457# Test reading and writing of databases with different page-sizes.
1458#
1459foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} {
1460 do_multiclient_test tn [string map [list %PGSZ% $pgsz] {
daneb8763d2010-08-17 14:52:22 +00001461 do_test wal-22.%PGSZ%.$tn.1 {
dan4bcc4982010-08-16 19:23:02 +00001462 sql1 {
1463 PRAGMA main.page_size = %PGSZ%;
1464 PRAGMA auto_vacuum = 0;
1465 PRAGMA journal_mode = WAL;
1466 CREATE TABLE t1(x UNIQUE);
1467 INSERT INTO t1 SELECT randomblob(800);
1468 INSERT INTO t1 SELECT randomblob(800);
1469 INSERT INTO t1 SELECT randomblob(800);
1470 }
1471 } {wal}
daneb8763d2010-08-17 14:52:22 +00001472 do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok}
1473 do_test wal-22.%PGSZ%.$tn.3 {
dan4bcc4982010-08-16 19:23:02 +00001474 sql1 {PRAGMA wal_checkpoint}
1475 expr {[file size test.db] % %PGSZ%}
1476 } {0}
1477 }]
1478}
1479
daneb8763d2010-08-17 14:52:22 +00001480#-------------------------------------------------------------------------
1481# Test that when 1 or more pages are recovered from a WAL file,
1482# sqlite3_log() is invoked to report this to the user.
1483#
mistachkinc5484652012-03-05 22:52:33 +00001484ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +00001485 set walfile [file nativename [file join [get_pwd] test.db-wal]]
mistachkinc5484652012-03-05 22:52:33 +00001486} else {
1487 set walfile test.db-wal
1488}
daneb8763d2010-08-17 14:52:22 +00001489catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +00001490forcedelete test.db
daneb8763d2010-08-17 14:52:22 +00001491do_test wal-23.1 {
1492 faultsim_delete_and_reopen
1493 execsql {
1494 CREATE TABLE t1(a, b);
1495 PRAGMA journal_mode = WAL;
1496 INSERT INTO t1 VALUES(1, 2);
1497 INSERT INTO t1 VALUES(3, 4);
1498 }
1499 faultsim_save_and_close
1500
1501 sqlite3_shutdown
1502 test_sqlite3_log [list lappend ::log]
1503 set ::log [list]
1504 sqlite3 db test.db
1505 execsql { SELECT * FROM t1 }
1506} {1 2 3 4}
1507do_test wal-23.2 { set ::log } {}
1508
1509do_test wal-23.3 {
1510 db close
1511 set ::log [list]
1512 faultsim_restore_and_reopen
1513 execsql { SELECT * FROM t1 }
1514} {1 2 3 4}
1515do_test wal-23.4 {
1516 set ::log
mistachkin56749cd2013-04-29 08:58:00 +00001517} [list SQLITE_NOTICE_RECOVER_WAL \
dan11f71d62013-05-15 18:34:17 +00001518 "recovered 2 frames from WAL file $walfile"]
daneb8763d2010-08-17 14:52:22 +00001519
dance8e5ff2011-04-05 16:09:08 +00001520
1521ifcapable autovacuum {
1522 # This block tests that if the size of a database is reduced by a
1523 # transaction (because of an incremental or auto-vacuum), that no
1524 # data is written to the WAL file for the truncated pages as part
1525 # of the commit. e.g. if a transaction reduces the size of a database
1526 # to N pages, data for page N+1 should not be written to the WAL file
1527 # when committing the transaction. At one point such data was being
1528 # written.
1529 #
1530 catch {db close}
1531 forcedelete test.db
1532 sqlite3 db test.db
1533 do_execsql_test 24.1 {
1534 PRAGMA auto_vacuum = 2;
1535 PRAGMA journal_mode = WAL;
1536 PRAGMA page_size = 1024;
1537 CREATE TABLE t1(x);
1538 INSERT INTO t1 VALUES(randomblob(5000));
1539 INSERT INTO t1 SELECT * FROM t1;
1540 INSERT INTO t1 SELECT * FROM t1;
1541 INSERT INTO t1 SELECT * FROM t1;
1542 INSERT INTO t1 SELECT * FROM t1;
1543 } {wal}
dandc5df0f2011-04-06 19:15:45 +00001544 do_test 24.2 {
1545 execsql {
1546 DELETE FROM t1;
1547 PRAGMA wal_checkpoint;
1548 }
dance8e5ff2011-04-05 16:09:08 +00001549 db close
1550 sqlite3 db test.db
1551 file exists test.db-wal
1552 } 0
dandc5df0f2011-04-06 19:15:45 +00001553 do_test 24.3 {
dance8e5ff2011-04-05 16:09:08 +00001554 file size test.db
1555 } [expr 84 * 1024]
dandc5df0f2011-04-06 19:15:45 +00001556 do_test 24.4 {
dance8e5ff2011-04-05 16:09:08 +00001557 execsql {
danb73da5b2011-04-07 05:17:32 +00001558 PRAGMA cache_size = 200;
dance8e5ff2011-04-05 16:09:08 +00001559 PRAGMA incremental_vacuum;
1560 PRAGMA wal_checkpoint;
1561 }
1562 file size test.db
1563 } [expr 3 * 1024]
dan0774bb52011-12-19 10:07:56 +00001564
1565 # WAL file now contains a single frame - the new root page for table t1.
1566 # It would be two frames (the new root page and a padding frame) if the
1567 # ZERO_DAMAGE flag were not set.
dandc5df0f2011-04-06 19:15:45 +00001568 do_test 24.5 {
dance8e5ff2011-04-05 16:09:08 +00001569 file size test.db-wal
dan0774bb52011-12-19 10:07:56 +00001570 } [wal_file_size 1 1024]
dance8e5ff2011-04-05 16:09:08 +00001571}
1572
daneb8763d2010-08-17 14:52:22 +00001573db close
1574sqlite3_shutdown
1575test_sqlite3_log
1576sqlite3_initialize
1577
drh1fb6a112014-04-04 14:12:52 +00001578# Make sure PRAGMA journal_mode=WAL works with ATTACHED databases in
1579# all journal modes.
1580#
1581foreach mode {OFF MEMORY PERSIST DELETE TRUNCATE WAL} {
1582 delete_file test.db test2.db
1583 sqlite3 db test.db
1584 do_test wal-25.$mode {
1585 db eval "PRAGMA journal_mode=$mode"
1586 db eval {ATTACH 'test2.db' AS t2; PRAGMA journal_mode=WAL;}
1587 } {wal}
1588 db close
1589}
1590
drh7416f2e2015-07-25 03:10:12 +00001591test_restore_config_pagecache
dan7c246102010-04-12 19:00:29 +00001592finish_test