3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
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.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 source $testdir/lock_common.tcl
19 source $testdir/wal_common.tcl
20 source $testdir/malloc_common.tcl
21 ifcapable !wal {finish_test ; return }
23 set a_string_counter 1
25 global a_string_counter
27 string range [string repeat "${a_string_counter}." $n] 1 $n
29 db func a_string a_string
31 #-------------------------------------------------------------------------
32 # When a rollback or savepoint rollback occurs, the client may remove
33 # elements from one of the hash tables in the wal-index. This block
34 # of test cases tests that nothing appears to go wrong when this is
39 PRAGMA cache_size = 2000;
40 PRAGMA page_size = 1024;
41 PRAGMA auto_vacuum = off;
42 PRAGMA synchronous = normal;
43 PRAGMA journal_mode = WAL;
44 PRAGMA wal_autocheckpoint = 0;
47 INSERT INTO t1 VALUES( a_string(800) ); /* 1 */
48 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */
49 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */
50 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */
51 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */
52 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */
53 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 64 */
54 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 128*/
55 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 256 */
56 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 512 */
57 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 1024 */
58 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2048 */
59 INSERT INTO t1 SELECT a_string(800) FROM t1 LIMIT 1970; /* 4018 */
61 PRAGMA cache_size = 10;
63 set x [wal_frame_count test.db-wal 1024]
64 if {[permutation]=="memsubsys1"} {
65 if {$x==4251 || $x==4290} {set x 4056}
70 for {set i 1} {$i < 50} {incr i} {
73 set str [a_string 800]
74 execsql { UPDATE t1 SET x = $str WHERE rowid = $i }
75 lappend L [wal_frame_count test.db-wal 1024]
78 INSERT INTO t1 SELECT a_string(800) FROM t1 LIMIT 100;
80 PRAGMA integrity_check;
84 # Check that everything looks OK from the point of view of an
85 # external connection.
89 execsql { SELECT count(*) FROM t1 } db2
92 execsql { SELECT x FROM t1 WHERE rowid = $i }
95 execsql { PRAGMA integrity_check } db2
99 # Check that the file-system in its current state can be recovered.
101 forcecopy test.db test2.db
102 forcecopy test.db-wal test2.db-wal
103 forcedelete test2.db-journal
105 do_test wal3-1.$i.5 {
106 execsql { SELECT count(*) FROM t1 } db2
108 do_test wal3-1.$i.6 {
109 execsql { SELECT x FROM t1 WHERE rowid = $i }
111 do_test wal3-1.$i.7 {
112 execsql { PRAGMA integrity_check } db2
117 proc byte_is_zero {file offset} {
118 if {[file size test.db] <= $offset} { return 1 }
119 expr { [hexio_read $file $offset 1] == "00" }
122 do_multiclient_test i {
124 set testname(1) multiproc
125 set testname(2) singleproc
128 do_test wal3-2.$tn.1 {
130 PRAGMA page_size = 1024;
131 PRAGMA journal_mode = WAL;
134 CREATE TABLE t1(a, b);
135 INSERT INTO t1 VALUES(1, 'one');
140 do_test wal3-2.$tn.2 {
142 CREATE TABLE t2(a, b);
143 INSERT INTO t2 VALUES(2, 'two');
148 do_test wal3-2.$tn.3 {
150 CREATE TABLE t3(a, b);
151 INSERT INTO t3 VALUES(3, 'three');
157 # Try to checkpoint the database using [db]. It should be possible to
158 # checkpoint everything except the table added by [db3] (checkpointing
159 # these frames would clobber the snapshot currently being used by [db2]).
161 # After [db2] has committed, a checkpoint can copy the entire log to the
162 # database file. Checkpointing after [db3] has committed is therefore a
163 # no-op, as the entire log has already been backfilled.
165 do_test wal3-2.$tn.4 {
168 PRAGMA wal_checkpoint;
170 byte_is_zero test.db [expr $AUTOVACUUM ? 4*1024 : 3*1024]
172 do_test wal3-2.$tn.5 {
175 PRAGMA wal_checkpoint;
177 list [byte_is_zero test.db [expr $AUTOVACUUM ? 4*1024 : 3*1024]] \
178 [byte_is_zero test.db [expr $AUTOVACUUM ? 5*1024 : 4*1024]]
180 do_test wal3-2.$tn.6 {
183 PRAGMA wal_checkpoint;
185 list [byte_is_zero test.db [expr $AUTOVACUUM ? 4*1024 : 3*1024]] \
186 [byte_is_zero test.db [expr $AUTOVACUUM ? 5*1024 : 4*1024]]
191 #-------------------------------------------------------------------------
192 # Test that that for the simple test:
195 # INSERT INTO x VALUES('z');
196 # PRAGMA wal_checkpoint;
198 # in WAL mode the xSync method is invoked as expected for each of
199 # synchronous=off, synchronous=normal and synchronous=full.
201 foreach {tn syncmode synccount} {
205 {test.db-wal normal test.db normal}
207 {test.db-wal normal test.db-wal normal test.db-wal normal test.db normal}
210 proc sync_counter {args} {
211 foreach {method filename id flags} $args break
212 lappend ::syncs [file tail $filename] $flags
215 forcedelete test.db test.db-wal test.db-journal
219 T script sync_counter
220 sqlite3 db test.db -vfs T
222 execsql "PRAGMA synchronous = $syncmode"
223 execsql "PRAGMA checkpoint_fullfsync = 0"
224 execsql { PRAGMA journal_mode = WAL }
225 execsql { CREATE TABLE filler(a,b,c); }
231 INSERT INTO x VALUES('z');
232 PRAGMA wal_checkpoint;
243 #-------------------------------------------------------------------------
244 # Only one client may run recovery at a time. Test this mechanism.
246 # When client-2 tries to open a read transaction while client-1 is
247 # running recovery, it fails to obtain a lock on an aReadMark[] slot
248 # (because they are all locked by recovery). It then tries to obtain
249 # a shared lock on the RECOVER lock to see if there really is a
250 # recovery running or not.
252 # This block of tests checks the effect of an SQLITE_BUSY or SQLITE_IOERR
253 # being returned when client-2 attempts a shared lock on the RECOVER byte.
255 # An SQLITE_BUSY should be converted to an SQLITE_BUSY_RECOVERY. An
256 # SQLITE_IOERR should be returned to the caller.
259 faultsim_delete_and_reopen
261 PRAGMA journal_mode = WAL;
262 CREATE TABLE t1(a, b);
263 INSERT INTO t1 VALUES(1, 2);
264 INSERT INTO t1 VALUES(3, 4);
266 faultsim_save_and_close
270 T script method_callback
272 proc method_callback {method args} {
273 if {$method == "xShmBarrier"} {
275 if {$::barrier_count == 2} {
276 # This code is executed within the xShmBarrier() callback invoked
277 # by the client running recovery as part of writing the recovered
278 # wal-index header. If a second client attempts to access the
279 # database now, it reads a corrupt (partially written) wal-index
280 # header. But it cannot even get that far, as the first client
281 # is still holding all the locks (recovery takes an exclusive lock
282 # on *all* db locks, preventing access by any other client).
284 # If global variable ::wal3_do_lockfailure is non-zero, then set
285 # things up so that an IO error occurs within an xShmLock() callback
286 # made by the second client (aka [db2]).
289 if { $::wal3_do_lockfailure } { T filter xShmLock }
290 set ::testrc [ catch { db2 eval "SELECT * FROM t1" } ::testmsg ]
296 if {$method == "xShmLock"} {
297 foreach {file handle spec} $args break
298 if { $spec == "2 1 lock shared" } {
306 # Test a normal SQLITE_BUSY return.
312 set wal3_do_lockfailure 0
314 faultsim_restore_and_reopen
315 execsql { SELECT * FROM t1 }
318 list $::testrc $::testmsg
319 } {1 {database is locked}}
322 # Test an SQLITE_IOERR return.
326 set wal3_do_lockfailure 1
330 faultsim_restore_and_reopen
331 execsql { SELECT * FROM t1 }
334 list $::testrc $::testmsg
335 } {1 {disk I/O error}}
340 #-------------------------------------------------------------------------
341 # When opening a read-transaction on a database, if the entire log has
342 # already been copied to the database file, the reader grabs a special
343 # kind of read lock (on aReadMark[0]). This set of test cases tests the
344 # outcome of the following:
346 # + The reader discovering that between the time when it determined
347 # that the log had been completely backfilled and the lock is obtained
348 # that a writer has written to the log. In this case the reader should
349 # acquire a different read-lock (not aReadMark[0]) and read the new
352 # + The attempt to obtain the lock on aReadMark[0] fails with SQLITE_BUSY.
353 # This can happen if a checkpoint is ongoing. In this case also simply
354 # obtain a different read-lock.
359 forcedelete test.db test.db-journal test.db wal
361 execsql { PRAGMA auto_vacuum = off }
362 execsql { PRAGMA journal_mode = WAL }
364 CREATE TABLE t1(a, b);
365 INSERT INTO t1 VALUES('o', 't');
366 INSERT INTO t1 VALUES('t', 'f');
372 execsql { BEGIN ; SELECT * FROM t1 } db3
375 execsql { PRAGMA wal_checkpoint } db2
378 # At this point the log file has been fully checkpointed. However,
379 # connection [db3] holds a lock that prevents the log from being wrapped.
380 # Test case 3.6.1.4 has [db] attempt a read-lock on aReadMark[0]. But
381 # as it is obtaining the lock, [db2] appends to the log file.
384 T script lock_callback
385 proc lock_callback {method file handle spec} {
386 if {$spec == "3 1 lock shared"} {
387 # This is the callback for [db] to obtain the read lock on aReadMark[0].
388 # Disable future callbacks using [T filter {}] and write to the log
389 # file using [db2]. [db3] is preventing [db2] from wrapping the log
390 # here, so this is an append.
392 db2 eval { INSERT INTO t1 VALUES('f', 's') }
403 # [db] should be left holding a read-lock on some slot other than
404 # aReadMark[0]. Test this by demonstrating that the read-lock is preventing
405 # the log from being wrapped.
409 db2 eval { PRAGMA wal_checkpoint }
410 set sz1 [file size test.db-wal]
411 db2 eval { INSERT INTO t1 VALUES('s', 'e') }
412 set sz2 [file size test.db-wal]
416 # Test that if [db2] had not interfered when [db] was trying to grab
417 # aReadMark[0], it would have been possible to wrap the log in 3.6.1.5.
421 execsql { PRAGMA wal_checkpoint } db2
428 db2 eval { PRAGMA wal_checkpoint }
429 set sz1 [file size test.db-wal]
430 db2 eval { INSERT INTO t1 VALUES('n', 't') }
431 set sz2 [file size test.db-wal]
440 forcedelete test.db test.db-journal test.db wal
443 execsql { PRAGMA auto_vacuum = off }
444 execsql { PRAGMA journal_mode = WAL }
446 CREATE TABLE t1(a, b);
447 INSERT INTO t1 VALUES('h', 'h');
448 INSERT INTO t1 VALUES('l', 'b');
453 T script lock_callback
454 proc lock_callback {method file handle spec} {
455 if {$spec == "3 1 unlock exclusive"} {
464 execsql { PRAGMA wal_checkpoint }
470 set sz1 [file size test.db-wal]
471 execsql { INSERT INTO t1 VALUES('b', 'c'); }
472 set sz2 [file size test.db-wal]
477 execsql { PRAGMA wal_checkpoint }
478 set sz1 [file size test.db-wal]
479 execsql { INSERT INTO t1 VALUES('n', 'o'); }
480 set sz2 [file size test.db-wal]
488 #-------------------------------------------------------------------------
489 # When opening a read-transaction on a database, if the entire log has
490 # not yet been copied to the database file, the reader grabs a read
491 # lock on aReadMark[x], where x>0. The following test cases experiment
492 # with the outcome of the following:
494 # + The reader discovering that between the time when it read the
495 # wal-index header and the lock was obtained that a writer has
496 # written to the log. In this case the reader should re-read the
497 # wal-index header and lock a snapshot corresponding to the new
500 # + The value in the aReadMark[x] slot has been modified since it was
506 forcedelete test.db test.db-journal test.db wal
509 PRAGMA journal_mode = WAL;
510 CREATE TABLE blue(red PRIMARY KEY, green);
514 T script method_callback
516 proc method_callback {method args} {
517 if {$method == "xOpen"} { return "reader" }
521 execsql { SELECT * FROM blue } db2
526 proc method_callback {method file handle spec} {
527 if {$handle != "reader" } { return }
528 if {$method == "xShmLock"} {
529 catch { execsql { INSERT INTO blue VALUES(1, 2) } }
530 catch { execsql { INSERT INTO blue VALUES(3, 4) } }
532 lappend ::locks $spec
535 execsql { SELECT * FROM blue } db2
539 } {{4 1 lock shared} {4 1 unlock shared} {5 1 lock shared} {5 1 unlock shared}}
542 proc method_callback {method file handle spec} {
543 if {$handle != "reader" } { return }
544 if {$method == "xShmLock"} {
545 catch { execsql { INSERT INTO blue VALUES(5, 6) } }
547 lappend ::locks $spec
550 execsql { SELECT * FROM blue } db2
554 } {{5 1 lock shared} {5 1 unlock shared} {4 1 lock shared} {4 1 unlock shared}}
561 #-------------------------------------------------------------------------
562 # When a connection opens a read-lock on the database, it searches for
563 # an aReadMark[] slot that is already set to the mxFrame value for the
564 # new transaction. If it cannot find one, it attempts to obtain an
565 # exclusive lock on an aReadMark[] slot for the purposes of modifying
566 # the value, then drops back to a shared-lock for the duration of the
569 # This test case verifies that if an exclusive lock cannot be obtained
570 # on any aReadMark[] slot (because there are already several readers),
571 # the client takes a shared-lock on a slot without modifying the value
575 if { [string match *BSD $tcl_platform(os)] } { set nConn 25 }
577 forcedelete test.db test.db-journal test.db wal
580 PRAGMA page_size = 1024;
581 PRAGMA journal_mode = WAL;
582 CREATE TABLE whoami(x);
583 INSERT INTO whoami VALUES('nobody');
586 for {set i 0} {$i < $nConn} {incr i} {
588 do_test wal3-9.1.$i {
590 execsql { UPDATE whoami SET x = $c }
597 for {set i 0} {$i < $nConn} {incr i} {
599 do_test wal3-9.2.$i {
600 execsql { SELECT * FROM whoami } $c
604 set sz [expr 1024 * (2+$AUTOVACUUM)]
606 for {set i 0} {$i < ($nConn-1)} {incr i} { db$i close }
607 execsql { PRAGMA wal_checkpoint }
608 byte_is_zero test.db [expr $sz-1024]
611 db[expr $nConn-1] close
612 execsql { PRAGMA wal_checkpoint }
613 set sz2 [file size test.db]
614 byte_is_zero test.db [expr $sz-1024]
617 do_multiclient_test tn {
618 do_test wal3-10.$tn.1 {
620 PRAGMA page_size = 1024;
622 PRAGMA journal_mode = WAL;
623 PRAGMA wal_autocheckpoint = 100000;
625 INSERT INTO t1 VALUES(randomblob(800));
626 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 2
627 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 4
628 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 8
629 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 16
630 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 32
631 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 64
632 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 128
633 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 256
634 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 512
635 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 1024
636 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 2048
637 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 4096
638 INSERT INTO t1 SELECT randomblob(800) FROM t1; -- 8192
640 CREATE INDEX i1 ON t1(x);
643 expr {[file size test.db-wal] > [expr 1032*9000]}
646 do_test wal3-10.$tn.2 {
647 sql2 {PRAGMA integrity_check}