blob: 234668240e14a830f78d2e360d1bc2b40531c032 [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
dan58021b22020-05-05 20:30:07 +000046 db timeout 1000
dan7c246102010-04-12 19:00:29 +000047}
48
dan3de777f2010-04-17 12:31:37 +000049proc log_deleted {logfile} {
50 return [expr [file exists $logfile]==0]
51}
52
dan7c246102010-04-12 19:00:29 +000053#
54# These are 'warm-body' tests used while developing the WAL code. They
55# serve to prove that a few really simple cases work:
56#
57# wal-1.*: Read and write the database.
58# wal-2.*: Test MVCC with one reader, one writer.
59# wal-3.*: Test transaction rollback.
60# wal-4.*: Test savepoint/statement rollback.
61# wal-5.*: Test the temp database.
62# wal-6.*: Test creating databases with different page sizes.
63#
dan998ad212010-05-07 06:59:08 +000064#
dan4bcc4982010-08-16 19:23:02 +000065#
dan7c246102010-04-12 19:00:29 +000066do_test wal-0.1 {
dan65bddc12010-05-07 12:49:22 +000067 execsql { PRAGMA auto_vacuum = 0 }
dan67032392010-04-17 15:42:43 +000068 execsql { PRAGMA synchronous = normal }
dan7c246102010-04-12 19:00:29 +000069 execsql { PRAGMA journal_mode = wal }
70} {wal}
dane04dc882010-04-20 18:53:15 +000071do_test wal-0.2 {
72 file size test.db
73} {1024}
dan7c246102010-04-12 19:00:29 +000074
75do_test wal-1.0 {
76 execsql {
77 BEGIN;
78 CREATE TABLE t1(a, b);
79 }
dane04dc882010-04-20 18:53:15 +000080 list [file exists test.db-journal] \
81 [file exists test.db-wal] \
82 [file size test.db]
83} {0 1 1024}
dan7c246102010-04-12 19:00:29 +000084do_test wal-1.1 {
85 execsql COMMIT
86 list [file exists test.db-journal] [file exists test.db-wal]
87} {0 1}
88do_test wal-1.2 {
89 # There are now two pages in the log.
90 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +000091} [wal_file_size 2 1024]
dan7c246102010-04-12 19:00:29 +000092
93do_test wal-1.3 {
94 execsql { SELECT * FROM sqlite_master }
95} {table t1 t1 2 {CREATE TABLE t1(a, b)}}
96
97do_test wal-1.4 {
98 execsql { INSERT INTO t1 VALUES(1, 2) }
99 execsql { INSERT INTO t1 VALUES(3, 4) }
100 execsql { INSERT INTO t1 VALUES(5, 6) }
101 execsql { INSERT INTO t1 VALUES(7, 8) }
102 execsql { INSERT INTO t1 VALUES(9, 10) }
103} {}
104
105do_test wal-1.5 {
106 execsql { SELECT * FROM t1 }
107} {1 2 3 4 5 6 7 8 9 10}
108
109do_test wal-2.1 {
110 sqlite3_wal db2 ./test.db
111 execsql { BEGIN; SELECT * FROM t1 } db2
112} {1 2 3 4 5 6 7 8 9 10}
113
114do_test wal-2.2 {
115 execsql { INSERT INTO t1 VALUES(11, 12) }
116 execsql { SELECT * FROM t1 }
117} {1 2 3 4 5 6 7 8 9 10 11 12}
118
119do_test wal-2.3 {
120 execsql { SELECT * FROM t1 } db2
121} {1 2 3 4 5 6 7 8 9 10}
122
123do_test wal-2.4 {
124 execsql { INSERT INTO t1 VALUES(13, 14) }
125 execsql { SELECT * FROM t1 }
126} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
127
128do_test wal-2.5 {
129 execsql { SELECT * FROM t1 } db2
130} {1 2 3 4 5 6 7 8 9 10}
131
132do_test wal-2.6 {
133 execsql { COMMIT; SELECT * FROM t1 } db2
134} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
135
136do_test wal-3.1 {
137 execsql { BEGIN; DELETE FROM t1 }
138 execsql { SELECT * FROM t1 }
139} {}
140do_test wal-3.2 {
141 execsql { SELECT * FROM t1 } db2
142} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
143do_test wal-3.3 {
144 execsql { ROLLBACK }
145 execsql { SELECT * FROM t1 }
146} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
147db2 close
148
dan74d6cd82010-04-24 18:44:05 +0000149#-------------------------------------------------------------------------
150# The following tests, wal-4.*, test that savepoints work with WAL
151# databases.
152#
dan7c246102010-04-12 19:00:29 +0000153do_test wal-4.1 {
154 execsql {
155 DELETE FROM t1;
156 BEGIN;
157 INSERT INTO t1 VALUES('a', 'b');
158 SAVEPOINT sp;
159 INSERT INTO t1 VALUES('c', 'd');
160 SELECT * FROM t1;
161 }
162} {a b c d}
163do_test wal-4.2 {
164 execsql {
165 ROLLBACK TO sp;
166 SELECT * FROM t1;
167 }
168} {a b}
169do_test wal-4.3 {
170 execsql {
171 COMMIT;
172 SELECT * FROM t1;
173 }
174} {a b}
175
dan4cd78b42010-04-26 16:57:10 +0000176do_test wal-4.4.1 {
dan74d6cd82010-04-24 18:44:05 +0000177 db close
178 sqlite3 db test.db
179 db func blob blob
180 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
181} {{a b} 0}
dan4cd78b42010-04-26 16:57:10 +0000182do_test wal-4.4.2 {
dan74d6cd82010-04-24 18:44:05 +0000183 execsql { PRAGMA cache_size = 10 }
184 execsql {
185 CREATE TABLE t2(a, b);
186 INSERT INTO t2 VALUES(blob(400), blob(400));
187 SAVEPOINT tr;
188 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
189 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
190 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
191 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
192 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
193 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
194 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
195 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
196 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
197 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
198 SELECT count(*) FROM t2;
199 }
200} {32}
dan4cd78b42010-04-26 16:57:10 +0000201do_test wal-4.4.3 {
dan74d6cd82010-04-24 18:44:05 +0000202 execsql { ROLLBACK TO tr }
203} {}
dan4cd78b42010-04-26 16:57:10 +0000204do_test wal-4.4.4 {
dan74d6cd82010-04-24 18:44:05 +0000205 set logsize [file size test.db-wal]
206 execsql {
207 INSERT INTO t1 VALUES('x', 'y');
208 RELEASE tr;
209 }
210 expr { $logsize == [file size test.db-wal] }
211} {1}
dan4cd78b42010-04-26 16:57:10 +0000212do_test wal-4.4.5 {
dan74d6cd82010-04-24 18:44:05 +0000213 execsql { SELECT count(*) FROM t2 }
214} {1}
dan4cd78b42010-04-26 16:57:10 +0000215do_test wal-4.4.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000216 forcecopy test.db test2.db
217 forcecopy test.db-wal test2.db-wal
dan74d6cd82010-04-24 18:44:05 +0000218 sqlite3 db2 test2.db
219 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
220} {1 2}
dan4cd78b42010-04-26 16:57:10 +0000221do_test wal-4.4.7 {
dan74d6cd82010-04-24 18:44:05 +0000222 execsql { PRAGMA integrity_check } db2
223} {ok}
224db2 close
225
dan4cd78b42010-04-26 16:57:10 +0000226do_test wal-4.5.1 {
227 reopen_db
228 db func blob blob
229 execsql {
230 PRAGMA journal_mode = WAL;
231 CREATE TABLE t1(a, b);
232 INSERT INTO t1 VALUES('a', 'b');
233 }
234 sqlite3 db test.db
235 db func blob blob
236 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
237} {{a b} 0}
238do_test wal-4.5.2 {
239 execsql { PRAGMA cache_size = 10 }
240 execsql {
241 CREATE TABLE t2(a, b);
242 BEGIN;
243 INSERT INTO t2 VALUES(blob(400), blob(400));
244 SAVEPOINT tr;
245 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
246 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
247 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
248 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
249 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
250 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
251 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
252 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
253 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
254 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
255 SELECT count(*) FROM t2;
256 }
257} {32}
258do_test wal-4.5.3 {
dan4cd78b42010-04-26 16:57:10 +0000259 execsql { ROLLBACK TO tr }
260} {}
261do_test wal-4.5.4 {
262 set logsize [file size test.db-wal]
dan4cd78b42010-04-26 16:57:10 +0000263 execsql {
264 INSERT INTO t1 VALUES('x', 'y');
265 RELEASE tr;
266 COMMIT;
267 }
268 expr { $logsize == [file size test.db-wal] }
269} {1}
270do_test wal-4.5.5 {
271 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 }
272} {1 2}
273do_test wal-4.5.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000274 forcecopy test.db test2.db
275 forcecopy test.db-wal test2.db-wal
dan4cd78b42010-04-26 16:57:10 +0000276 sqlite3 db2 test2.db
dan4cd78b42010-04-26 16:57:10 +0000277 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
278} {1 2}
279do_test wal-4.5.7 {
280 execsql { PRAGMA integrity_check } db2
281} {ok}
282db2 close
283
danb7d53f52010-05-06 17:28:08 +0000284do_test wal-4.6.1 {
285 execsql {
286 DELETE FROM t2;
287 PRAGMA wal_checkpoint;
288 BEGIN;
289 INSERT INTO t2 VALUES('w', 'x');
290 SAVEPOINT save;
291 INSERT INTO t2 VALUES('y', 'z');
292 ROLLBACK TO save;
293 COMMIT;
danb7d53f52010-05-06 17:28:08 +0000294 }
danbdd9af02010-11-18 16:14:24 +0000295 execsql { SELECT * FROM t2 }
danb7d53f52010-05-06 17:28:08 +0000296} {w x}
297
dan4cd78b42010-04-26 16:57:10 +0000298
dan74d6cd82010-04-24 18:44:05 +0000299reopen_db
dan7c246102010-04-12 19:00:29 +0000300do_test wal-5.1 {
301 execsql {
302 CREATE TEMP TABLE t2(a, b);
303 INSERT INTO t2 VALUES(1, 2);
304 }
305} {}
306do_test wal-5.2 {
307 execsql {
308 BEGIN;
309 INSERT INTO t2 VALUES(3, 4);
310 SELECT * FROM t2;
311 }
312} {1 2 3 4}
313do_test wal-5.3 {
314 execsql {
315 ROLLBACK;
316 SELECT * FROM t2;
317 }
318} {1 2}
319do_test wal-5.4 {
320 execsql {
321 CREATE TEMP TABLE t3(x UNIQUE);
322 BEGIN;
323 INSERT INTO t2 VALUES(3, 4);
324 INSERT INTO t3 VALUES('abc');
325 }
326 catchsql { INSERT INTO t3 VALUES('abc') }
drhf9c8ce32013-11-05 13:33:55 +0000327} {1 {UNIQUE constraint failed: t3.x}}
dan7c246102010-04-12 19:00:29 +0000328do_test wal-5.5 {
329 execsql {
330 COMMIT;
331 SELECT * FROM t2;
332 }
333} {1 2 3 4}
334db close
335
dan7c246102010-04-12 19:00:29 +0000336foreach sector {512 4096} {
337 sqlite3_simulate_device -sectorsize $sector
338 foreach pgsz {512 1024 2048 4096} {
mistachkinfda06be2011-08-02 00:57:34 +0000339 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +0000340 do_test wal-6.$sector.$pgsz.1 {
dane04dc882010-04-20 18:53:15 +0000341 sqlite3 db test.db -vfs devsym
dan7c246102010-04-12 19:00:29 +0000342 execsql "
dane04dc882010-04-20 18:53:15 +0000343 PRAGMA page_size = $pgsz;
dan65bddc12010-05-07 12:49:22 +0000344 PRAGMA auto_vacuum = 0;
dane04dc882010-04-20 18:53:15 +0000345 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000346 "
347 execsql "
348 CREATE TABLE t1(a, b);
349 INSERT INTO t1 VALUES(1, 2);
350 "
351 db close
352 file size test.db
353 } [expr $pgsz*2]
354
355 do_test wal-6.$sector.$pgsz.2 {
dan3de777f2010-04-17 12:31:37 +0000356 log_deleted test.db-wal
357 } {1}
dan7c246102010-04-12 19:00:29 +0000358 }
359}
360
361do_test wal-7.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000362 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +0000363 sqlite3_wal db test.db
364 execsql {
365 PRAGMA page_size = 1024;
366 CREATE TABLE t1(a, b);
367 INSERT INTO t1 VALUES(1, 2);
368 }
dan7c246102010-04-12 19:00:29 +0000369 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000370} [list 1024 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000371do_test wal-7.2 {
dan5a299f92010-05-03 11:05:08 +0000372 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000373 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000374} [list 2048 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000375
dan7c246102010-04-12 19:00:29 +0000376# Execute some transactions in auto-vacuum mode to test database file
377# truncation.
378#
danb9bf16b2010-04-14 11:23:30 +0000379do_test wal-8.1 {
dan7c246102010-04-12 19:00:29 +0000380 reopen_db
dane04dc882010-04-20 18:53:15 +0000381 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +0000382 forcedelete test.db test.db-wal
dane04dc882010-04-20 18:53:15 +0000383
384 sqlite3 db test.db
385 db function blob blob
dan7c246102010-04-12 19:00:29 +0000386 execsql {
387 PRAGMA auto_vacuum = 1;
dane04dc882010-04-20 18:53:15 +0000388 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000389 PRAGMA auto_vacuum;
390 }
dane04dc882010-04-20 18:53:15 +0000391} {wal 1}
danb9bf16b2010-04-14 11:23:30 +0000392do_test wal-8.2 {
dan7c246102010-04-12 19:00:29 +0000393 execsql {
394 PRAGMA page_size = 1024;
395 CREATE TABLE t1(x);
dan67032392010-04-17 15:42:43 +0000396 INSERT INTO t1 VALUES(blob(900));
397 INSERT INTO t1 VALUES(blob(900));
398 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
399 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
400 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
401 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
402 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
dan5a299f92010-05-03 11:05:08 +0000403 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000404 }
405 file size test.db
dan80a15262010-04-13 11:45:31 +0000406} [expr 68*1024]
danb9bf16b2010-04-14 11:23:30 +0000407do_test wal-8.3 {
dan7c246102010-04-12 19:00:29 +0000408 execsql {
409 DELETE FROM t1 WHERE rowid<54;
dan5a299f92010-05-03 11:05:08 +0000410 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000411 }
412 file size test.db
413} [expr 14*1024]
414
415# Run some "warm-body" tests to ensure that log-summary files with more
416# than 256 entries (log summaries that contain index blocks) work Ok.
417#
danb9bf16b2010-04-14 11:23:30 +0000418do_test wal-9.1 {
dan7c246102010-04-12 19:00:29 +0000419 reopen_db
420 execsql {
drhd5156602011-11-12 16:46:55 +0000421 PRAGMA cache_size=2000;
dan7c246102010-04-12 19:00:29 +0000422 CREATE TABLE t1(x PRIMARY KEY);
dan67032392010-04-17 15:42:43 +0000423 INSERT INTO t1 VALUES(blob(900));
424 INSERT INTO t1 VALUES(blob(900));
425 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
426 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
427 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
428 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
429 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
430 INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */
431 INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */
dan7c246102010-04-12 19:00:29 +0000432 }
433 file size test.db
dane04dc882010-04-20 18:53:15 +0000434} 1024
danb9bf16b2010-04-14 11:23:30 +0000435do_test wal-9.2 {
dan7c246102010-04-12 19:00:29 +0000436 sqlite3_wal db2 test.db
437 execsql {PRAGMA integrity_check } db2
438} {ok}
439
danb9bf16b2010-04-14 11:23:30 +0000440do_test wal-9.3 {
mistachkinfda06be2011-08-02 00:57:34 +0000441 forcedelete test2.db test2.db-wal
442 copy_file test.db test2.db
443 copy_file test.db-wal test2.db-wal
dan7c246102010-04-12 19:00:29 +0000444 sqlite3_wal db3 test2.db
445 execsql {PRAGMA integrity_check } db3
446} {ok}
447db3 close
448
danb9bf16b2010-04-14 11:23:30 +0000449do_test wal-9.4 {
dan5a299f92010-05-03 11:05:08 +0000450 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000451 db2 close
452 sqlite3_wal db2 test.db
453 execsql {PRAGMA integrity_check } db2
454} {ok}
455
dan80a15262010-04-13 11:45:31 +0000456foreach handle {db db2 db3} { catch { $handle close } }
457unset handle
458
danb9bf16b2010-04-14 11:23:30 +0000459#-------------------------------------------------------------------------
460# The following block of tests - wal-10.* - test that the WAL locking
dane264d982010-04-14 18:06:50 +0000461# scheme works in simple cases. This block of tests is run twice. Once
462# using multiple connections in the address space of the current process,
463# and once with all connections except one running in external processes.
danb9bf16b2010-04-14 11:23:30 +0000464#
dana4a90952010-06-15 19:07:42 +0000465do_multiclient_test tn {
dane264d982010-04-14 18:06:50 +0000466
467 # Initialize the database schema and contents.
468 #
469 do_test wal-10.$tn.1 {
470 execsql {
dan7fa65fb2011-04-01 19:14:40 +0000471 PRAGMA auto_vacuum = 0;
dana4a90952010-06-15 19:07:42 +0000472 PRAGMA journal_mode = wal;
dane264d982010-04-14 18:06:50 +0000473 CREATE TABLE t1(a, b);
474 INSERT INTO t1 VALUES(1, 2);
danb9bf16b2010-04-14 11:23:30 +0000475 SELECT * FROM t1;
dane264d982010-04-14 18:06:50 +0000476 }
dana4a90952010-06-15 19:07:42 +0000477 } {wal 1 2}
danb9bf16b2010-04-14 11:23:30 +0000478
dane264d982010-04-14 18:06:50 +0000479 # Open a transaction and write to the database using [db]. Check that [db2]
480 # is still able to read the snapshot before the transaction was opened.
481 #
482 do_test wal-10.$tn.2 {
483 execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); }
484 sql2 {SELECT * FROM t1}
485 } {1 2}
486
487 # Have [db] commit the transaction. Check that [db2] is now seeing the
488 # new, updated snapshot.
489 #
490 do_test wal-10.$tn.3 {
491 execsql { COMMIT }
492 sql2 {SELECT * FROM t1}
493 } {1 2 3 4}
494
495 # Have [db2] open a read transaction. Then write to the db via [db]. Check
496 # that [db2] is still seeing the original snapshot. Then read with [db3].
497 # [db3] should see the newly committed data.
498 #
499 do_test wal-10.$tn.4 {
500 sql2 { BEGIN ; SELECT * FROM t1}
501 } {1 2 3 4}
502 do_test wal-10.$tn.5 {
503 execsql { INSERT INTO t1 VALUES(5, 6); }
504 sql2 {SELECT * FROM t1}
505 } {1 2 3 4}
506 do_test wal-10.$tn.6 {
507 sql3 {SELECT * FROM t1}
508 } {1 2 3 4 5 6}
509 do_test wal-10.$tn.7 {
510 sql2 COMMIT
511 } {}
512
513 # Have [db2] open a write transaction. Then attempt to write to the
514 # database via [db]. This should fail (writer lock cannot be obtained).
515 #
516 # Then open a read-transaction with [db]. Commit the [db2] transaction
517 # to disk. Verify that [db] still cannot write to the database (because
518 # it is reading an old snapshot).
519 #
520 # Close the current [db] transaction. Open a new one. [db] can now write
521 # to the database (as it is not locked and [db] is reading the latest
522 # snapshot).
523 #
524 do_test wal-10.$tn.7 {
525 sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; }
526 catchsql { INSERT INTO t1 VALUES(9, 10) }
527 } {1 {database is locked}}
528 do_test wal-10.$tn.8 {
529 execsql { BEGIN ; SELECT * FROM t1 }
530 } {1 2 3 4 5 6}
531 do_test wal-10.$tn.9 {
532 sql2 COMMIT
533 catchsql { INSERT INTO t1 VALUES(9, 10) }
534 } {1 {database is locked}}
535 do_test wal-10.$tn.10 {
drh7e263722010-05-20 21:21:09 +0000536 execsql { COMMIT }
537 execsql { BEGIN }
538 execsql { INSERT INTO t1 VALUES(9, 10) }
539 execsql { COMMIT }
dane264d982010-04-14 18:06:50 +0000540 execsql { SELECT * FROM t1 }
541 } {1 2 3 4 5 6 7 8 9 10}
542
543 # Open a read transaction with [db2]. Check that this prevents [db] from
544 # checkpointing the database. But not from writing to it.
545 #
546 do_test wal-10.$tn.11 {
547 sql2 { BEGIN; SELECT * FROM t1 }
548 } {1 2 3 4 5 6 7 8 9 10}
549 do_test wal-10.$tn.12 {
dan5a299f92010-05-03 11:05:08 +0000550 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000551 } {0 {0 7 7}} ;# Reader no longer block checkpoints
dane264d982010-04-14 18:06:50 +0000552 do_test wal-10.$tn.13 {
553 execsql { INSERT INTO t1 VALUES(11, 12) }
554 sql2 {SELECT * FROM t1}
555 } {1 2 3 4 5 6 7 8 9 10}
556
drh34116ea2010-05-31 12:30:52 +0000557 # Writers do not block checkpoints any more either.
dane264d982010-04-14 18:06:50 +0000558 #
dane264d982010-04-14 18:06:50 +0000559 do_test wal-10.$tn.14 {
drh34116ea2010-05-31 12:30:52 +0000560 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000561 } {0 {0 8 7}}
danb9bf16b2010-04-14 11:23:30 +0000562
drha2ac9df2010-05-31 13:11:49 +0000563 # The following series of test cases used to verify another blocking
564 # case in WAL - a case which no longer blocks.
dane264d982010-04-14 18:06:50 +0000565 #
dane264d982010-04-14 18:06:50 +0000566 do_test wal-10.$tn.15 {
drh34116ea2010-05-31 12:30:52 +0000567 sql2 { COMMIT; BEGIN; SELECT * FROM t1; }
dane264d982010-04-14 18:06:50 +0000568 } {1 2 3 4 5 6 7 8 9 10 11 12}
569 do_test wal-10.$tn.16 {
dan5a299f92010-05-03 11:05:08 +0000570 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000571 } {0 {0 8 8}}
dan49320f82010-04-14 18:50:08 +0000572 do_test wal-10.$tn.17 {
dan5a299f92010-05-03 11:05:08 +0000573 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000574 } {0 8 8}
dan49320f82010-04-14 18:50:08 +0000575 do_test wal-10.$tn.18 {
drha2ac9df2010-05-31 13:11:49 +0000576 sql3 { BEGIN; SELECT * FROM t1 }
dane264d982010-04-14 18:06:50 +0000577 } {1 2 3 4 5 6 7 8 9 10 11 12}
dan49320f82010-04-14 18:50:08 +0000578 do_test wal-10.$tn.19 {
dane264d982010-04-14 18:06:50 +0000579 catchsql { INSERT INTO t1 VALUES(13, 14) }
drha2ac9df2010-05-31 13:11:49 +0000580 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000581 do_test wal-10.$tn.20 {
dane264d982010-04-14 18:06:50 +0000582 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000583 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
dan49320f82010-04-14 18:50:08 +0000584 do_test wal-10.$tn.21 {
dane264d982010-04-14 18:06:50 +0000585 sql3 COMMIT
drha2ac9df2010-05-31 13:11:49 +0000586 sql2 COMMIT
dane264d982010-04-14 18:06:50 +0000587 } {}
dan49320f82010-04-14 18:50:08 +0000588 do_test wal-10.$tn.22 {
dane264d982010-04-14 18:06:50 +0000589 execsql { SELECT * FROM t1 }
590 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
danb9bf16b2010-04-14 11:23:30 +0000591
drha2ac9df2010-05-31 13:11:49 +0000592 # Another series of tests that used to demonstrate blocking behavior
593 # but which now work.
dan49320f82010-04-14 18:50:08 +0000594 #
595 do_test wal-10.$tn.23 {
dan5a299f92010-05-03 11:05:08 +0000596 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000597 } {0 9 9}
dan49320f82010-04-14 18:50:08 +0000598 do_test wal-10.$tn.24 {
599 sql2 { BEGIN; SELECT * FROM t1; }
600 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
601 do_test wal-10.$tn.25 {
dan5a299f92010-05-03 11:05:08 +0000602 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000603 } {0 9 9}
dan49320f82010-04-14 18:50:08 +0000604 do_test wal-10.$tn.26 {
605 catchsql { INSERT INTO t1 VALUES(15, 16) }
drha2ac9df2010-05-31 13:11:49 +0000606 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000607 do_test wal-10.$tn.27 {
drha2ac9df2010-05-31 13:11:49 +0000608 sql3 { INSERT INTO t1 VALUES(17, 18) }
dan49320f82010-04-14 18:50:08 +0000609 } {}
610 do_test wal-10.$tn.28 {
611 code3 {
612 set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL]
613 sqlite3_step $::STMT
614 }
dan49320f82010-04-14 18:50:08 +0000615 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000616 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}
dan49320f82010-04-14 18:50:08 +0000617 do_test wal-10.$tn.29 {
drha2ac9df2010-05-31 13:11:49 +0000618 execsql { INSERT INTO t1 VALUES(19, 20) }
dan5a299f92010-05-03 11:05:08 +0000619 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000620 } {0 {0 3 0}}
dan49320f82010-04-14 18:50:08 +0000621 do_test wal-10.$tn.30 {
622 code3 { sqlite3_finalize $::STMT }
dan5a299f92010-05-03 11:05:08 +0000623 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000624 } {0 3 0}
dan49320f82010-04-14 18:50:08 +0000625
626 # At one point, if a reader failed to upgrade to a writer because it
627 # was reading an old snapshot, the write-locks were not being released.
628 # Test that this bug has been fixed.
629 #
630 do_test wal-10.$tn.31 {
drha2ac9df2010-05-31 13:11:49 +0000631 sql2 COMMIT
dan49320f82010-04-14 18:50:08 +0000632 execsql { BEGIN ; SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000633 sql2 { INSERT INTO t1 VALUES(21, 22) }
634 catchsql { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000635 } {1 {database is locked}}
636 do_test wal-10.$tn.32 {
637 # This statement would fail when the bug was present.
drha2ac9df2010-05-31 13:11:49 +0000638 sql2 { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000639 } {}
640 do_test wal-10.$tn.33 {
641 execsql { SELECT * FROM t1 ; COMMIT }
drha2ac9df2010-05-31 13:11:49 +0000642 } {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 +0000643 do_test wal-10.$tn.34 {
644 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000645 } {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 +0000646
dan8b348af2010-04-27 18:43:16 +0000647 # Test that if a checkpointer cannot obtain the required locks, it
648 # releases all locks before returning a busy error.
649 #
650 do_test wal-10.$tn.35 {
651 execsql {
652 DELETE FROM t1;
653 INSERT INTO t1 VALUES('a', 'b');
654 INSERT INTO t1 VALUES('c', 'd');
655 }
656 sql2 {
657 BEGIN;
658 SELECT * FROM t1;
659 }
660 } {a b c d}
dan8b348af2010-04-27 18:43:16 +0000661 do_test wal-10.$tn.36 {
dan5a299f92010-05-03 11:05:08 +0000662 catchsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000663 } {0 {0 8 8}}
dan8b348af2010-04-27 18:43:16 +0000664 do_test wal-10.$tn.36 {
665 sql3 { INSERT INTO t1 VALUES('e', 'f') }
666 sql2 { SELECT * FROM t1 }
667 } {a b c d}
668 do_test wal-10.$tn.37 {
669 sql2 COMMIT
dan5a299f92010-05-03 11:05:08 +0000670 execsql { PRAGMA wal_checkpoint }
drh1eaaf932011-12-19 00:31:09 +0000671 } {0 9 9}
dane264d982010-04-14 18:06:50 +0000672}
673
dan4cc6fb62010-04-15 16:45:34 +0000674#-------------------------------------------------------------------------
675# This block of tests, wal-11.*, test that nothing goes terribly wrong
676# if frames must be written to the log file before a transaction is
677# committed (in order to free up memory).
678#
679do_test wal-11.1 {
680 reopen_db
681 execsql {
682 PRAGMA cache_size = 10;
683 PRAGMA page_size = 1024;
684 CREATE TABLE t1(x PRIMARY KEY);
685 }
686 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
dane04dc882010-04-20 18:53:15 +0000687} {1 3}
dan4cc6fb62010-04-15 16:45:34 +0000688do_test wal-11.2 {
dan5a299f92010-05-03 11:05:08 +0000689 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000690 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000691} [list 3 [wal_file_size 3 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000692do_test wal-11.3 {
dan67032392010-04-17 15:42:43 +0000693 execsql { INSERT INTO t1 VALUES( blob(900) ) }
dan97a31352010-04-16 13:59:31 +0000694 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000695} [list 3 [wal_file_size 4 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000696
697do_test wal-11.4 {
698 execsql {
699 BEGIN;
dan67032392010-04-17 15:42:43 +0000700 INSERT INTO t1 SELECT blob(900) FROM t1; -- 2
701 INSERT INTO t1 SELECT blob(900) FROM t1; -- 4
702 INSERT INTO t1 SELECT blob(900) FROM t1; -- 8
703 INSERT INTO t1 SELECT blob(900) FROM t1; -- 16
dan4cc6fb62010-04-15 16:45:34 +0000704 }
dan97a31352010-04-16 13:59:31 +0000705 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000706} [list 3 [wal_file_size 32 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000707do_test wal-11.5 {
708 execsql {
709 SELECT count(*) FROM t1;
710 PRAGMA integrity_check;
711 }
712} {16 ok}
713do_test wal-11.6 {
714 execsql COMMIT
dan97a31352010-04-16 13:59:31 +0000715 list [expr [file size test.db]/1024] [file size test.db-wal]
dand6f7c972016-01-09 16:39:29 +0000716} [list 3 [wal_file_size 40 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000717do_test wal-11.7 {
718 execsql {
719 SELECT count(*) FROM t1;
720 PRAGMA integrity_check;
721 }
722} {16 ok}
723do_test wal-11.8 {
dan5a299f92010-05-03 11:05:08 +0000724 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000725 list [expr [file size test.db]/1024] [file size test.db-wal]
dand6f7c972016-01-09 16:39:29 +0000726} [list 37 [wal_file_size 40 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000727do_test wal-11.9 {
728 db close
dan3de777f2010-04-17 12:31:37 +0000729 list [expr [file size test.db]/1024] [log_deleted test.db-wal]
730} {37 1}
dan67032392010-04-17 15:42:43 +0000731sqlite3_wal db test.db
drhb7c2f862016-01-09 23:55:47 +0000732
733# After adding the capability of WAL to overwrite prior uncommitted
734# frame in the WAL-file with revised content, the size of the WAL file
735# following cache-spill is smaller.
736#
737#set nWal 39
738#if {[permutation]!="mmap"} {set nWal 37}
739#ifcapable !mmap {set nWal 37}
740set nWal 34
741
dan4cc6fb62010-04-15 16:45:34 +0000742do_test wal-11.10 {
743 execsql {
744 PRAGMA cache_size = 10;
745 BEGIN;
dan67032392010-04-17 15:42:43 +0000746 INSERT INTO t1 SELECT blob(900) FROM t1; -- 32
dan4cc6fb62010-04-15 16:45:34 +0000747 SELECT count(*) FROM t1;
748 }
dan97a31352010-04-16 13:59:31 +0000749 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000750} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000751do_test wal-11.11 {
752 execsql {
753 SELECT count(*) FROM t1;
754 ROLLBACK;
755 SELECT count(*) FROM t1;
756 }
757} {32 16}
758do_test wal-11.12 {
dan97a31352010-04-16 13:59:31 +0000759 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000760} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000761do_test wal-11.13 {
762 execsql {
dan67032392010-04-17 15:42:43 +0000763 INSERT INTO t1 VALUES( blob(900) );
dan4cc6fb62010-04-15 16:45:34 +0000764 SELECT count(*) FROM t1;
765 PRAGMA integrity_check;
766 }
767} {17 ok}
768do_test wal-11.14 {
dan97a31352010-04-16 13:59:31 +0000769 list [expr [file size test.db]/1024] [file size test.db-wal]
dan7909e542013-03-22 20:15:31 +0000770} [list 37 [wal_file_size $nWal 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000771
772
dan4a4b01d2010-04-16 11:30:18 +0000773#-------------------------------------------------------------------------
dan97a31352010-04-16 13:59:31 +0000774# This block of tests, wal-12.*, tests the fix for a problem that
775# could occur if a log that is a prefix of an older log is written
776# into a reused log file.
dan4a4b01d2010-04-16 11:30:18 +0000777#
778reopen_db
779do_test wal-12.1 {
780 execsql {
781 PRAGMA page_size = 1024;
782 CREATE TABLE t1(x, y);
783 CREATE TABLE t2(x, y);
784 INSERT INTO t1 VALUES('A', 1);
785 }
dan97a31352010-04-16 13:59:31 +0000786 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000787} [list 1 [wal_file_size 5 1024]]
dan4a4b01d2010-04-16 11:30:18 +0000788do_test wal-12.2 {
789 db close
dane04dc882010-04-20 18:53:15 +0000790 sqlite3 db test.db
dan4a4b01d2010-04-16 11:30:18 +0000791 execsql {
dane04dc882010-04-20 18:53:15 +0000792 PRAGMA synchronous = normal;
dan4a4b01d2010-04-16 11:30:18 +0000793 UPDATE t1 SET y = 0 WHERE x = 'A';
794 }
795 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
796} {3 1}
797do_test wal-12.3 {
798 execsql { INSERT INTO t2 VALUES('B', 1) }
799 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
800} {3 2}
dan4a4b01d2010-04-16 11:30:18 +0000801do_test wal-12.4 {
mistachkinfda06be2011-08-02 00:57:34 +0000802 forcecopy test.db test2.db
803 forcecopy test.db-wal test2.db-wal
dan4a4b01d2010-04-16 11:30:18 +0000804 sqlite3_wal db2 test2.db
dan4a4b01d2010-04-16 11:30:18 +0000805 execsql { SELECT * FROM t2 } db2
806} {B 1}
807db2 close
dan4a4b01d2010-04-16 11:30:18 +0000808do_test wal-12.5 {
809 execsql {
dan5a299f92010-05-03 11:05:08 +0000810 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000811 UPDATE t2 SET y = 2 WHERE x = 'B';
dan5a299f92010-05-03 11:05:08 +0000812 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000813 UPDATE t1 SET y = 1 WHERE x = 'A';
dan5a299f92010-05-03 11:05:08 +0000814 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000815 UPDATE t1 SET y = 0 WHERE x = 'A';
dan4a4b01d2010-04-16 11:30:18 +0000816 }
danbdd9af02010-11-18 16:14:24 +0000817 execsql { SELECT * FROM t2 }
dan4a4b01d2010-04-16 11:30:18 +0000818} {B 2}
dance4f05f2010-04-22 19:14:13 +0000819do_test wal-12.6 {
mistachkinfda06be2011-08-02 00:57:34 +0000820 forcecopy test.db test2.db
821 forcecopy test.db-wal test2.db-wal
dan4a4b01d2010-04-16 11:30:18 +0000822 sqlite3_wal db2 test2.db
823 execsql { SELECT * FROM t2 } db2
824} {B 2}
825db2 close
dance4f05f2010-04-22 19:14:13 +0000826db close
827
828#-------------------------------------------------------------------------
dan31c03902010-04-29 14:51:33 +0000829# Check a fun corruption case has been fixed.
830#
831# The problem was that after performing a checkpoint using a connection
832# that had an out-of-date pager-cache, the next time the connection was
833# used it did not realize the cache was out-of-date and proceeded to
834# operate with an inconsistent cache. Leading to corruption.
835#
dan31c03902010-04-29 14:51:33 +0000836catch { db close }
837catch { db2 close }
838catch { db3 close }
mistachkinfda06be2011-08-02 00:57:34 +0000839forcedelete test.db test.db-wal
dan31c03902010-04-29 14:51:33 +0000840sqlite3 db test.db
841sqlite3 db2 test.db
dan31c03902010-04-29 14:51:33 +0000842do_test wal-14 {
843 execsql {
844 PRAGMA journal_mode = WAL;
845 CREATE TABLE t1(a PRIMARY KEY, b);
846 INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
847 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
848 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
849 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
850 }
851
852 db2 eval {
853 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
854 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
855 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
856 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
857 }
858
dan5a299f92010-05-03 11:05:08 +0000859 # After executing the "PRAGMA wal_checkpoint", connection [db] was being
dan31c03902010-04-29 14:51:33 +0000860 # left with an inconsistent cache. Running the CREATE INDEX statement
861 # in this state led to database corruption.
862 catchsql {
dan5a299f92010-05-03 11:05:08 +0000863 PRAGMA wal_checkpoint;
dan31c03902010-04-29 14:51:33 +0000864 CREATE INDEX i1 on t1(b);
865 }
866
867 db2 eval { PRAGMA integrity_check }
868} {ok}
869
dan185cca62010-04-29 14:58:53 +0000870catch { db close }
871catch { db2 close }
dan87c1fe12010-05-03 12:14:15 +0000872
873#-------------------------------------------------------------------------
874# The following block of tests - wal-15.* - focus on testing the
875# implementation of the sqlite3_wal_checkpoint() interface.
876#
mistachkinfda06be2011-08-02 00:57:34 +0000877forcedelete test.db test.db-wal
dan87c1fe12010-05-03 12:14:15 +0000878sqlite3 db test.db
879do_test wal-15.1 {
880 execsql {
dan65bddc12010-05-07 12:49:22 +0000881 PRAGMA auto_vacuum = 0;
dan87c1fe12010-05-03 12:14:15 +0000882 PRAGMA page_size = 1024;
883 PRAGMA journal_mode = WAL;
884 }
885 execsql {
886 CREATE TABLE t1(a, b);
887 INSERT INTO t1 VALUES(1, 2);
888 }
889} {}
890
891# Test that an error is returned if the database name is not recognized
892#
893do_test wal-15.2.1 {
894 sqlite3_wal_checkpoint db aux
895} {SQLITE_ERROR}
896do_test wal-15.2.2 {
897 sqlite3_errcode db
898} {SQLITE_ERROR}
899do_test wal-15.2.3 {
900 sqlite3_errmsg db
901} {unknown database: aux}
902
903# Test that an error is returned if an attempt is made to checkpoint
904# if a transaction is open on the database.
905#
906do_test wal-15.3.1 {
907 execsql {
908 BEGIN;
909 INSERT INTO t1 VALUES(3, 4);
910 }
911 sqlite3_wal_checkpoint db main
912} {SQLITE_LOCKED}
913do_test wal-15.3.2 {
914 sqlite3_errcode db
915} {SQLITE_LOCKED}
916do_test wal-15.3.3 {
917 sqlite3_errmsg db
918} {database table is locked}
919
dandcb11692010-05-31 14:18:45 +0000920# Earlier versions returned an error is returned if the db cannot be
921# checkpointed because of locks held by another connection. Check that
922# this is no longer the case.
dan87c1fe12010-05-03 12:14:15 +0000923#
924sqlite3 db2 test.db
925do_test wal-15.4.1 {
926 execsql {
927 BEGIN;
928 SELECT * FROM t1;
929 } db2
930} {1 2}
931do_test wal-15.4.2 {
932 execsql { COMMIT }
933 sqlite3_wal_checkpoint db
dandcb11692010-05-31 14:18:45 +0000934} {SQLITE_OK}
dan87c1fe12010-05-03 12:14:15 +0000935do_test wal-15.4.3 {
936 sqlite3_errmsg db
dandcb11692010-05-31 14:18:45 +0000937} {not an error}
dan87c1fe12010-05-03 12:14:15 +0000938
939# After [db2] drops its lock, [db] may checkpoint the db.
940#
941do_test wal-15.4.4 {
942 execsql { COMMIT } db2
943 sqlite3_wal_checkpoint db
944} {SQLITE_OK}
945do_test wal-15.4.5 {
946 sqlite3_errmsg db
947} {not an error}
948do_test wal-15.4.6 {
949 file size test.db
950} [expr 1024*2]
951
952catch { db2 close }
953catch { db close }
danaf0cfd32010-05-03 15:58:50 +0000954
955#-------------------------------------------------------------------------
956# The following block of tests - wal-16.* - test that if a NULL pointer or
957# an empty string is passed as the second argument of the wal_checkpoint()
958# API, an attempt is made to checkpoint all attached databases.
959#
960foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} {
961 1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1
962 2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1
danf2b8dd52010-11-18 19:28:01 +0000963 3 {db eval "PRAGMA wal_checkpoint"} {0 10 10} 1 1
danaf0cfd32010-05-03 15:58:50 +0000964
965 4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0
966 5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1
967 6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0
danbdd9af02010-11-18 16:14:24 +0000968 7 {db eval "PRAGMA main.wal_checkpoint"} {0 10 10} 1 0
dan0774bb52011-12-19 10:07:56 +0000969 8 {db eval "PRAGMA aux.wal_checkpoint"} {0 13 13} 0 1
danbdd9af02010-11-18 16:14:24 +0000970 9 {db eval "PRAGMA temp.wal_checkpoint"} {0 -1 -1} 0 0
danaf0cfd32010-05-03 15:58:50 +0000971} {
972 do_test wal-16.$tn.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000973 forcedelete test2.db test2.db-wal test2.db-journal
974 forcedelete test.db test.db-wal test.db-journal
danaf0cfd32010-05-03 15:58:50 +0000975
976 sqlite3 db test.db
977 execsql {
978 ATTACH 'test2.db' AS aux;
dan65bddc12010-05-07 12:49:22 +0000979 PRAGMA main.auto_vacuum = 0;
980 PRAGMA aux.auto_vacuum = 0;
danaf0cfd32010-05-03 15:58:50 +0000981 PRAGMA main.journal_mode = WAL;
982 PRAGMA aux.journal_mode = WAL;
dan0774bb52011-12-19 10:07:56 +0000983 PRAGMA main.synchronous = NORMAL;
984 PRAGMA aux.synchronous = NORMAL;
danaf0cfd32010-05-03 15:58:50 +0000985 }
986 } {wal wal}
987
988 do_test wal-16.$tn.2 {
989 execsql {
990 CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b));
991 CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b));
992
993 INSERT INTO t2 VALUES(1, randomblob(1000));
994 INSERT INTO t2 VALUES(2, randomblob(1000));
995 INSERT INTO t1 SELECT * FROM t2;
996 }
997
998 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000999 } [list [expr 1*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001000 do_test wal-16.$tn.3 {
1001 list [file size test2.db] [file size test2.db-wal]
dan0774bb52011-12-19 10:07:56 +00001002 } [list [expr 1*1024] [wal_file_size 13 1024]]
danaf0cfd32010-05-03 15:58:50 +00001003
1004 do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res
1005
1006 do_test wal-16.$tn.5 {
1007 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +00001008 } [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001009
1010 do_test wal-16.$tn.6 {
1011 list [file size test2.db] [file size test2.db-wal]
dan0774bb52011-12-19 10:07:56 +00001012 } [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 13 1024]]
danaf0cfd32010-05-03 15:58:50 +00001013
1014 catch { db close }
1015}
1016
dan8d6ad1c2010-05-04 10:36:20 +00001017#-------------------------------------------------------------------------
1018# The following tests - wal-17.* - attempt to verify that the correct
1019# number of "padding" frames are appended to the log file when a transaction
1020# is committed in synchronous=FULL mode.
1021#
1022# Do this by creating a database that uses 512 byte pages. Then writing
1023# a transaction that modifies 171 pages. In synchronous=NORMAL mode, this
1024# produces a log file of:
1025#
dan10f5a502010-06-23 15:55:43 +00001026# 32 + (24+512)*171 = 90312 bytes.
dan8d6ad1c2010-05-04 10:36:20 +00001027#
1028# Slightly larger than 11*8192 = 90112 bytes.
1029#
1030# Run the test using various different sector-sizes. In each case, the
1031# WAL code should write the 90300 bytes of log file containing the
1032# transaction, then append as may frames as are required to extend the
1033# log file so that no part of the next transaction will be written into
1034# a disk-sector used by transaction just committed.
1035#
1036set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000]
1037catch { db close }
dan10f5a502010-06-23 15:55:43 +00001038foreach {tn sectorsize logsize} "
1039 1 128 [wal_file_size 172 512]
1040 2 256 [wal_file_size 172 512]
1041 3 512 [wal_file_size 172 512]
1042 4 1024 [wal_file_size 172 512]
1043 5 2048 [wal_file_size 172 512]
1044 6 4096 [wal_file_size 176 512]
1045 7 8192 [wal_file_size 184 512]
1046" {
mistachkinfda06be2011-08-02 00:57:34 +00001047 forcedelete test.db test.db-wal test.db-journal
dan8d6ad1c2010-05-04 10:36:20 +00001048 sqlite3_simulate_device -sectorsize $sectorsize
1049 sqlite3 db test.db -vfs devsym
1050
1051 do_test wal-17.$tn.1 {
1052 execsql {
1053 PRAGMA auto_vacuum = 0;
1054 PRAGMA page_size = 512;
drhd5156602011-11-12 16:46:55 +00001055 PRAGMA cache_size = -2000;
dan8d6ad1c2010-05-04 10:36:20 +00001056 PRAGMA journal_mode = WAL;
1057 PRAGMA synchronous = FULL;
1058 }
1059 execsql {
1060 BEGIN;
1061 CREATE TABLE t(x);
1062 }
1063 for {set i 0} {$i<166} {incr i} {
1064 execsql { INSERT INTO t VALUES(randomblob(400)) }
1065 }
1066 execsql COMMIT
1067
1068 file size test.db-wal
1069 } $logsize
1070
1071 do_test wal-17.$tn.2 {
1072 file size test.db
1073 } 512
1074
1075 do_test wal-17.$tn.3 {
1076 db close
1077 file size test.db
1078 } [expr 512*171]
1079}
1080sqlite3_test_control_pending_byte $old_pending_byte
1081
danb6e099a2010-05-04 14:47:39 +00001082#-------------------------------------------------------------------------
1083# This test - wal-18.* - verifies a couple of specific conditions that
1084# may be encountered while recovering a log file are handled correctly:
1085#
1086# wal-18.1.* When the first 32-bits of a frame checksum is correct but
1087# the second 32-bits are false, and
1088#
1089# wal-18.2.* When the page-size field that occurs at the start of a log
1090# file is a power of 2 greater than 16384 or smaller than 512.
1091#
mistachkinfda06be2011-08-02 00:57:34 +00001092forcedelete test.db test.db-wal test.db-journal
danb6e099a2010-05-04 14:47:39 +00001093do_test wal-18.0 {
1094 sqlite3 db test.db
1095 execsql {
1096 PRAGMA page_size = 1024;
1097 PRAGMA auto_vacuum = 0;
1098 PRAGMA journal_mode = WAL;
1099 PRAGMA synchronous = OFF;
1100
1101 CREATE TABLE t1(a, b, UNIQUE(a, b));
1102 INSERT INTO t1 VALUES(0, 0);
1103 PRAGMA wal_checkpoint;
1104
1105 INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2
1106 INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4
1107 INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6
1108 }
1109
mistachkinfda06be2011-08-02 00:57:34 +00001110 forcecopy test.db testX.db
1111 forcecopy test.db-wal testX.db-wal
danb6e099a2010-05-04 14:47:39 +00001112 db close
1113 list [file size testX.db] [file size testX.db-wal]
dan10f5a502010-06-23 15:55:43 +00001114} [list [expr 3*1024] [wal_file_size 6 1024]]
danb6e099a2010-05-04 14:47:39 +00001115
danc9e46652010-05-06 13:36:47 +00001116unset -nocomplain nFrame result
danb6e099a2010-05-04 14:47:39 +00001117foreach {nFrame result} {
1118 0 {0 0}
1119 1 {0 0}
1120 2 {0 0 1 2}
1121 3 {0 0 1 2}
1122 4 {0 0 1 2 3 4}
1123 5 {0 0 1 2 3 4}
1124 6 {0 0 1 2 3 4 5 6}
1125} {
1126 do_test wal-18.1.$nFrame {
mistachkinfda06be2011-08-02 00:57:34 +00001127 forcecopy testX.db test.db
1128 forcecopy testX.db-wal test.db-wal
danb6e099a2010-05-04 14:47:39 +00001129
drh23ea97b2010-05-20 16:45:58 +00001130 hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000
danb6e099a2010-05-04 14:47:39 +00001131
1132 sqlite3 db test.db
1133 execsql {
1134 SELECT * FROM t1;
1135 PRAGMA integrity_check;
1136 }
1137 } [concat $result ok]
1138 db close
1139}
1140
1141proc randomblob {pgsz} {
1142 sqlite3 rbdb :memory:
1143 set blob [rbdb one {SELECT randomblob($pgsz)}]
1144 rbdb close
1145 set blob
1146}
1147
1148proc logcksum {ckv1 ckv2 blob} {
1149 upvar $ckv1 c1
1150 upvar $ckv2 c2
1151
drhed1d84e2012-05-11 20:43:47 +00001152 # Since the magic number at the start of the -wal file header is
1153 # 931071618 that indicates that the content should always be read as
1154 # little-endian.
1155 #
1156 set scanpattern i*
danb6e099a2010-05-04 14:47:39 +00001157
danb8fd6c22010-05-24 10:39:36 +00001158 binary scan $blob $scanpattern values
drh4c1cb6a2010-05-19 19:09:37 +00001159 foreach {v1 v2} $values {
1160 set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
1161 set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
1162 }
danb6e099a2010-05-04 14:47:39 +00001163}
1164
mistachkinfda06be2011-08-02 00:57:34 +00001165forcecopy test.db testX.db
danb6e099a2010-05-04 14:47:39 +00001166foreach {tn pgsz works} {
1167 1 128 0
1168 2 256 0
1169 3 512 1
1170 4 1024 1
1171 5 2048 1
1172 6 4096 1
1173 7 8192 1
1174 8 16384 1
1175 9 32768 1
drhb2eced52010-08-12 02:41:12 +00001176 10 65536 1
1177 11 131072 0
drh4c1cb6a2010-05-19 19:09:37 +00001178 11 1016 0
danb6e099a2010-05-04 14:47:39 +00001179} {
1180
dan65bddc12010-05-07 12:49:22 +00001181 if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} {
1182 set works 0
1183 }
1184
danb6e099a2010-05-04 14:47:39 +00001185 for {set pg 1} {$pg <= 3} {incr pg} {
mistachkinfda06be2011-08-02 00:57:34 +00001186 forcecopy testX.db test.db
1187 forcedelete test.db-wal
danb6e099a2010-05-04 14:47:39 +00001188
1189 # Check that the database now exists and consists of three pages. And
1190 # that there is no associated wal file.
1191 #
1192 do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0
1193 do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1
1194 do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3]
1195
1196 do_test wal-18.2.$tn.$pg.4 {
1197
1198 # Create a wal file that contains a single frame (database page
1199 # number $pg) with the commit flag set. The frame checksum is
1200 # correct, but the contents of the database page are corrupt.
1201 #
1202 # The page-size in the log file header is set to $pgsz. If the
1203 # WAL code considers $pgsz to be a valid SQLite database file page-size,
1204 # the database will be corrupt (because the garbage frame contents
1205 # will be treated as valid content). If $pgsz is invalid (too small
1206 # or too large), the db will not be corrupt as the log file will
1207 # be ignored.
1208 #
drh7e263722010-05-20 21:21:09 +00001209 set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23]
danb6e099a2010-05-04 14:47:39 +00001210 set framebody [randomblob $pgsz]
drh7e263722010-05-20 21:21:09 +00001211 set framehdr [binary format IIII $pg 5 22 23]
1212 set c1 0
1213 set c2 0
dan71d89912010-05-24 13:57:42 +00001214 logcksum c1 c2 $walhdr
dan10f5a502010-06-23 15:55:43 +00001215
1216 append walhdr [binary format II $c1 $c2]
dan71d89912010-05-24 13:57:42 +00001217 logcksum c1 c2 [string range $framehdr 0 7]
danb6e099a2010-05-04 14:47:39 +00001218 logcksum c1 c2 $framebody
drh7e263722010-05-20 21:21:09 +00001219 set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2]
danb8fd6c22010-05-24 10:39:36 +00001220
danb6e099a2010-05-04 14:47:39 +00001221 set fd [open test.db-wal w]
1222 fconfigure $fd -encoding binary -translation binary
1223 puts -nonewline $fd $walhdr
1224 puts -nonewline $fd $framehdr
1225 puts -nonewline $fd $framebody
1226 close $fd
1227
1228 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +00001229 } [wal_file_size 1 $pgsz]
danb6e099a2010-05-04 14:47:39 +00001230
1231 do_test wal-18.2.$tn.$pg.5 {
1232 sqlite3 db test.db
1233 set rc [catch { db one {PRAGMA integrity_check} } msg]
1234 expr { $rc!=0 || $msg!="ok" }
1235 } $works
1236
1237 db close
1238 }
1239}
1240
danb7d53f52010-05-06 17:28:08 +00001241#-------------------------------------------------------------------------
1242# The following test - wal-19.* - fixes a bug that was present during
1243# development.
1244#
1245# When a database connection in WAL mode is closed, it attempts an
1246# EXCLUSIVE lock on the database file. If the lock is obtained, the
1247# connection knows that it is the last connection to disconnect from
1248# the database, so it runs a checkpoint operation. The bug was that
1249# the connection was not updating its private copy of the wal-index
1250# header before doing so, meaning that it could checkpoint an old
1251# snapshot.
1252#
1253do_test wal-19.1 {
mistachkinfda06be2011-08-02 00:57:34 +00001254 forcedelete test.db test.db-wal test.db-journal
danb7d53f52010-05-06 17:28:08 +00001255 sqlite3 db test.db
1256 sqlite3 db2 test.db
1257 execsql {
1258 PRAGMA journal_mode = WAL;
1259 CREATE TABLE t1(a, b);
1260 INSERT INTO t1 VALUES(1, 2);
1261 INSERT INTO t1 VALUES(3, 4);
1262 }
1263 execsql { SELECT * FROM t1 } db2
1264} {1 2 3 4}
1265do_test wal-19.2 {
1266 execsql {
1267 INSERT INTO t1 VALUES(5, 6);
1268 SELECT * FROM t1;
1269 }
1270} {1 2 3 4 5 6}
1271do_test wal-19.3 {
1272 db close
1273 db2 close
1274 file exists test.db-wal
1275} {0}
1276do_test wal-19.4 {
1277 # When the bug was present, the following was returning {1 2 3 4} only,
1278 # as [db2] had an out-of-date copy of the wal-index header when it was
1279 # closed.
1280 #
1281 sqlite3 db test.db
1282 execsql { SELECT * FROM t1 }
1283} {1 2 3 4 5 6}
1284
dan998ad212010-05-07 06:59:08 +00001285#-------------------------------------------------------------------------
1286# This test - wal-20.* - uses two connections. One in this process and
1287# the other in an external process. The procedure is:
1288#
1289# 1. Using connection 1, create the database schema.
1290#
1291# 2. Using connection 2 (in an external process), add so much
1292# data to the database without checkpointing that a wal-index
1293# larger than 64KB is required.
1294#
1295# 3. Using connection 1, checkpoint the database. Make sure all
1296# the data is present and the database is not corrupt.
1297#
1298# At one point, SQLite was failing to grow the mapping of the wal-index
1299# file in step 3 and the checkpoint was corrupting the database file.
1300#
dan94e95ea2018-12-24 15:22:47 +00001301if {[permutation]!="unix-excl"} {
1302 do_test wal-20.1 {
1303 catch {db close}
1304 forcedelete test.db test.db-wal test.db-journal
dan998ad212010-05-07 06:59:08 +00001305 sqlite3 db test.db
dan94e95ea2018-12-24 15:22:47 +00001306 execsql {
1307 PRAGMA journal_mode = WAL;
1308 CREATE TABLE t1(x);
1309 INSERT INTO t1 VALUES(randomblob(900));
1310 SELECT count(*) FROM t1;
1311 }
1312 } {wal 1}
1313 do_test wal-20.2 {
1314 set ::buddy [launch_testfixture]
1315 testfixture $::buddy {
1316 sqlite3 db test.db
1317 db transaction { db eval {
1318 PRAGMA wal_autocheckpoint = 0;
1319 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
1320 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
1321 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
1322 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
1323 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
1324 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
1325 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
1326 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
1327 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
1328 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
1329 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
1330 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
1331 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
1332 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
1333 } }
1334 }
1335 } {0}
1336 do_test wal-20.3 {
1337 close $::buddy
1338 execsql { PRAGMA wal_checkpoint }
1339 execsql { SELECT count(*) FROM t1 }
1340 } {16384}
1341 do_test wal-20.4 {
1342 db close
1343 sqlite3 db test.db
1344 execsql { SELECT count(*) FROM t1 }
1345 } {16384}
1346 integrity_check wal-20.5
1347}
dan998ad212010-05-07 06:59:08 +00001348
danaf0cfd32010-05-03 15:58:50 +00001349catch { db2 close }
1350catch { db close }
dan6e6bd562010-06-02 18:59:03 +00001351
1352do_test wal-21.1 {
dan10f5a502010-06-23 15:55:43 +00001353 faultsim_delete_and_reopen
dan6e6bd562010-06-02 18:59:03 +00001354 execsql {
1355 PRAGMA journal_mode = WAL;
1356 CREATE TABLE t1(a, b);
1357 INSERT INTO t1 VALUES(1, 2);
1358 INSERT INTO t1 VALUES(3, 4);
1359 INSERT INTO t1 VALUES(5, 6);
1360 INSERT INTO t1 VALUES(7, 8);
1361 INSERT INTO t1 VALUES(9, 10);
1362 INSERT INTO t1 VALUES(11, 12);
1363 }
1364} {wal}
1365do_test wal-21.2 {
1366 execsql {
1367 PRAGMA cache_size = 10;
1368 PRAGMA wal_checkpoint;
1369 BEGIN;
1370 SAVEPOINT s;
1371 INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1;
1372 ROLLBACK TO s;
1373 COMMIT;
dan6e6bd562010-06-02 18:59:03 +00001374 }
danbdd9af02010-11-18 16:14:24 +00001375 execsql { SELECT * FROM t1 }
dan6e6bd562010-06-02 18:59:03 +00001376} {1 2 3 4 5 6 7 8 9 10 11 12}
1377do_test wal-21.3 {
1378 execsql { PRAGMA integrity_check }
1379} {ok}
1380
dan4bcc4982010-08-16 19:23:02 +00001381#-------------------------------------------------------------------------
1382# Test reading and writing of databases with different page-sizes.
1383#
drh7da56b42016-03-14 18:34:42 +00001384incr ::do_not_use_codec
dan4bcc4982010-08-16 19:23:02 +00001385foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} {
1386 do_multiclient_test tn [string map [list %PGSZ% $pgsz] {
daneb8763d2010-08-17 14:52:22 +00001387 do_test wal-22.%PGSZ%.$tn.1 {
dan4bcc4982010-08-16 19:23:02 +00001388 sql1 {
1389 PRAGMA main.page_size = %PGSZ%;
1390 PRAGMA auto_vacuum = 0;
1391 PRAGMA journal_mode = WAL;
1392 CREATE TABLE t1(x UNIQUE);
1393 INSERT INTO t1 SELECT randomblob(800);
1394 INSERT INTO t1 SELECT randomblob(800);
1395 INSERT INTO t1 SELECT randomblob(800);
1396 }
1397 } {wal}
daneb8763d2010-08-17 14:52:22 +00001398 do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok}
1399 do_test wal-22.%PGSZ%.$tn.3 {
dan4bcc4982010-08-16 19:23:02 +00001400 sql1 {PRAGMA wal_checkpoint}
1401 expr {[file size test.db] % %PGSZ%}
1402 } {0}
1403 }]
1404}
drh7da56b42016-03-14 18:34:42 +00001405incr ::do_not_use_codec -1
dan4bcc4982010-08-16 19:23:02 +00001406
daneb8763d2010-08-17 14:52:22 +00001407#-------------------------------------------------------------------------
1408# Test that when 1 or more pages are recovered from a WAL file,
1409# sqlite3_log() is invoked to report this to the user.
1410#
mistachkinc5484652012-03-05 22:52:33 +00001411ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +00001412 set walfile [file nativename [file join [get_pwd] test.db-wal]]
mistachkinc5484652012-03-05 22:52:33 +00001413} else {
1414 set walfile test.db-wal
1415}
daneb8763d2010-08-17 14:52:22 +00001416catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +00001417forcedelete test.db
daneb8763d2010-08-17 14:52:22 +00001418do_test wal-23.1 {
1419 faultsim_delete_and_reopen
1420 execsql {
1421 CREATE TABLE t1(a, b);
1422 PRAGMA journal_mode = WAL;
1423 INSERT INTO t1 VALUES(1, 2);
1424 INSERT INTO t1 VALUES(3, 4);
1425 }
1426 faultsim_save_and_close
1427
1428 sqlite3_shutdown
1429 test_sqlite3_log [list lappend ::log]
1430 set ::log [list]
1431 sqlite3 db test.db
1432 execsql { SELECT * FROM t1 }
1433} {1 2 3 4}
1434do_test wal-23.2 { set ::log } {}
1435
1436do_test wal-23.3 {
1437 db close
1438 set ::log [list]
1439 faultsim_restore_and_reopen
1440 execsql { SELECT * FROM t1 }
1441} {1 2 3 4}
1442do_test wal-23.4 {
1443 set ::log
mistachkin56749cd2013-04-29 08:58:00 +00001444} [list SQLITE_NOTICE_RECOVER_WAL \
dan11f71d62013-05-15 18:34:17 +00001445 "recovered 2 frames from WAL file $walfile"]
daneb8763d2010-08-17 14:52:22 +00001446
dance8e5ff2011-04-05 16:09:08 +00001447
1448ifcapable autovacuum {
1449 # This block tests that if the size of a database is reduced by a
1450 # transaction (because of an incremental or auto-vacuum), that no
1451 # data is written to the WAL file for the truncated pages as part
1452 # of the commit. e.g. if a transaction reduces the size of a database
1453 # to N pages, data for page N+1 should not be written to the WAL file
1454 # when committing the transaction. At one point such data was being
1455 # written.
1456 #
1457 catch {db close}
1458 forcedelete test.db
1459 sqlite3 db test.db
1460 do_execsql_test 24.1 {
1461 PRAGMA auto_vacuum = 2;
1462 PRAGMA journal_mode = WAL;
1463 PRAGMA page_size = 1024;
1464 CREATE TABLE t1(x);
1465 INSERT INTO t1 VALUES(randomblob(5000));
1466 INSERT INTO t1 SELECT * FROM t1;
1467 INSERT INTO t1 SELECT * FROM t1;
1468 INSERT INTO t1 SELECT * FROM t1;
1469 INSERT INTO t1 SELECT * FROM t1;
1470 } {wal}
dandc5df0f2011-04-06 19:15:45 +00001471 do_test 24.2 {
1472 execsql {
1473 DELETE FROM t1;
1474 PRAGMA wal_checkpoint;
1475 }
dance8e5ff2011-04-05 16:09:08 +00001476 db close
1477 sqlite3 db test.db
1478 file exists test.db-wal
1479 } 0
dandc5df0f2011-04-06 19:15:45 +00001480 do_test 24.3 {
dance8e5ff2011-04-05 16:09:08 +00001481 file size test.db
1482 } [expr 84 * 1024]
dandc5df0f2011-04-06 19:15:45 +00001483 do_test 24.4 {
dance8e5ff2011-04-05 16:09:08 +00001484 execsql {
danb73da5b2011-04-07 05:17:32 +00001485 PRAGMA cache_size = 200;
dance8e5ff2011-04-05 16:09:08 +00001486 PRAGMA incremental_vacuum;
1487 PRAGMA wal_checkpoint;
1488 }
1489 file size test.db
1490 } [expr 3 * 1024]
dan0774bb52011-12-19 10:07:56 +00001491
1492 # WAL file now contains a single frame - the new root page for table t1.
1493 # It would be two frames (the new root page and a padding frame) if the
1494 # ZERO_DAMAGE flag were not set.
dandc5df0f2011-04-06 19:15:45 +00001495 do_test 24.5 {
dance8e5ff2011-04-05 16:09:08 +00001496 file size test.db-wal
dan0774bb52011-12-19 10:07:56 +00001497 } [wal_file_size 1 1024]
dance8e5ff2011-04-05 16:09:08 +00001498}
1499
daneb8763d2010-08-17 14:52:22 +00001500db close
1501sqlite3_shutdown
1502test_sqlite3_log
1503sqlite3_initialize
1504
drh1fb6a112014-04-04 14:12:52 +00001505# Make sure PRAGMA journal_mode=WAL works with ATTACHED databases in
1506# all journal modes.
1507#
1508foreach mode {OFF MEMORY PERSIST DELETE TRUNCATE WAL} {
1509 delete_file test.db test2.db
1510 sqlite3 db test.db
1511 do_test wal-25.$mode {
1512 db eval "PRAGMA journal_mode=$mode"
1513 db eval {ATTACH 'test2.db' AS t2; PRAGMA journal_mode=WAL;}
1514 } {wal}
1515 db close
1516}
1517
drh099b3852021-03-10 16:35:37 +00001518# 2021-03-10 forum post https://sqlite.org/forum/forumpost/a006d86f72
1519#
1520file delete test.db
1521sqlite3 db test.db
1522db eval {PRAGMA journal_mode=WAL}
1523for {set i 0} {$i<$SQLITE_MAX_ATTACHED} {incr i} {
1524 do_test wal-26.1.$i {
1525 file delete attached-$i.db
1526 db eval "ATTACH 'attached-$i.db' AS a$i;"
1527 db eval "PRAGMA a$i.journal_mode=WAL;"
1528 db eval "CREATE TABLE a$i.t$i (x);"
1529 db eval "INSERT INTO t$i VALUES(zeroblob(10000));"
1530 db eval "DELETE FROM t$i;"
1531 db eval "INSERT INTO t$i VALUES(randomblob(10000));"
1532 expr {[file size attached-$i.db-wal]>10000}
1533 } {1}
1534}
1535for {set i [expr {$SQLITE_MAX_ATTACHED-1}]} {$i>=0} {incr i -1} {
1536 do_test wal-26.2.$i {
1537 db eval "PRAGMA a$i.wal_checkpoint(TRUNCATE);"
1538 file size attached-$i.db-wal
1539 } {0}
1540 for {set j 0} {$j<$i} {incr j} {
1541 do_test wal-26.2.$i.$j {
1542 expr {[file size attached-$j.db-wal]>10000}
1543 } {1}
1544 }
1545}
1546db close
1547
1548
drh7416f2e2015-07-25 03:10:12 +00001549test_restore_config_pagecache
dan7c246102010-04-12 19:00:29 +00001550finish_test