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/malloc_common.tcl
20
source $testdir/wal_common.tcl
24
ifcapable !wal {finish_test ; return }
28
file delete -force test.db test.db-wal test.db-wal-summary
29
sqlite3_wal db test.db
35
return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte]
38
proc sqlite3_wal {args} {
40
[lindex $args 0] eval { PRAGMA auto_vacuum = 0 }
41
[lindex $args 0] eval { PRAGMA page_size = 1024 }
42
[lindex $args 0] eval { PRAGMA journal_mode = wal }
43
[lindex $args 0] eval { PRAGMA synchronous = normal }
44
[lindex $args 0] function blob blob
47
proc log_deleted {logfile} {
48
return [expr [file exists $logfile]==0]
52
# These are 'warm-body' tests used while developing the WAL code. They
53
# serve to prove that a few really simple cases work:
55
# wal-1.*: Read and write the database.
56
# wal-2.*: Test MVCC with one reader, one writer.
57
# wal-3.*: Test transaction rollback.
58
# wal-4.*: Test savepoint/statement rollback.
59
# wal-5.*: Test the temp database.
60
# wal-6.*: Test creating databases with different page sizes.
65
execsql { PRAGMA auto_vacuum = 0 }
66
execsql { PRAGMA synchronous = normal }
67
execsql { PRAGMA journal_mode = wal }
76
CREATE TABLE t1(a, b);
78
list [file exists test.db-journal] \
79
[file exists test.db-wal] \
84
list [file exists test.db-journal] [file exists test.db-wal]
87
# There are now two pages in the log.
89
} [wal_file_size 2 1024]
92
execsql { SELECT * FROM sqlite_master }
93
} {table t1 t1 2 {CREATE TABLE t1(a, b)}}
96
execsql { INSERT INTO t1 VALUES(1, 2) }
97
execsql { INSERT INTO t1 VALUES(3, 4) }
98
execsql { INSERT INTO t1 VALUES(5, 6) }
99
execsql { INSERT INTO t1 VALUES(7, 8) }
100
execsql { INSERT INTO t1 VALUES(9, 10) }
104
execsql { SELECT * FROM t1 }
105
} {1 2 3 4 5 6 7 8 9 10}
108
sqlite3_wal db2 ./test.db
109
execsql { BEGIN; SELECT * FROM t1 } db2
110
} {1 2 3 4 5 6 7 8 9 10}
113
execsql { INSERT INTO t1 VALUES(11, 12) }
114
execsql { SELECT * FROM t1 }
115
} {1 2 3 4 5 6 7 8 9 10 11 12}
118
execsql { SELECT * FROM t1 } db2
119
} {1 2 3 4 5 6 7 8 9 10}
122
execsql { INSERT INTO t1 VALUES(13, 14) }
123
execsql { SELECT * FROM t1 }
124
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
127
execsql { SELECT * FROM t1 } db2
128
} {1 2 3 4 5 6 7 8 9 10}
131
execsql { COMMIT; SELECT * FROM t1 } db2
132
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
135
execsql { BEGIN; DELETE FROM t1 }
136
execsql { SELECT * FROM t1 }
139
execsql { SELECT * FROM t1 } db2
140
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
143
execsql { SELECT * FROM t1 }
144
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
147
#-------------------------------------------------------------------------
148
# The following tests, wal-4.*, test that savepoints work with WAL
155
INSERT INTO t1 VALUES('a', 'b');
157
INSERT INTO t1 VALUES('c', 'd');
178
list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
181
execsql { PRAGMA cache_size = 10 }
183
CREATE TABLE t2(a, b);
184
INSERT INTO t2 VALUES(blob(400), blob(400));
186
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
187
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
188
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
189
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
190
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
191
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
192
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
193
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
194
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
195
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
196
SELECT count(*) FROM t2;
200
execsql { ROLLBACK TO tr }
203
set logsize [file size test.db-wal]
205
INSERT INTO t1 VALUES('x', 'y');
208
expr { $logsize == [file size test.db-wal] }
211
execsql { SELECT count(*) FROM t2 }
214
file copy -force test.db test2.db
215
file copy -force test.db-wal test2.db-wal
217
execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
220
execsql { PRAGMA integrity_check } db2
228
PRAGMA journal_mode = WAL;
229
CREATE TABLE t1(a, b);
230
INSERT INTO t1 VALUES('a', 'b');
234
list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
237
execsql { PRAGMA cache_size = 10 }
239
CREATE TABLE t2(a, b);
241
INSERT INTO t2 VALUES(blob(400), blob(400));
243
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
244
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
245
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
246
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
247
INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
248
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
249
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
250
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
251
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
252
INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
253
SELECT count(*) FROM t2;
257
execsql { ROLLBACK TO tr }
260
set logsize [file size test.db-wal]
262
INSERT INTO t1 VALUES('x', 'y');
266
expr { $logsize == [file size test.db-wal] }
269
execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 }
272
file copy -force test.db test2.db
273
file copy -force test.db-wal test2.db-wal
275
execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
278
execsql { PRAGMA integrity_check } db2
285
PRAGMA wal_checkpoint;
287
INSERT INTO t2 VALUES('w', 'x');
289
INSERT INTO t2 VALUES('y', 'z');
293
execsql { SELECT * FROM t2 }
300
CREATE TEMP TABLE t2(a, b);
301
INSERT INTO t2 VALUES(1, 2);
307
INSERT INTO t2 VALUES(3, 4);
319
CREATE TEMP TABLE t3(x UNIQUE);
321
INSERT INTO t2 VALUES(3, 4);
322
INSERT INTO t3 VALUES('abc');
324
catchsql { INSERT INTO t3 VALUES('abc') }
325
} {1 {column x is not unique}}
334
foreach sector {512 4096} {
335
sqlite3_simulate_device -sectorsize $sector
336
foreach pgsz {512 1024 2048 4096} {
337
file delete -force test.db test.db-wal
338
do_test wal-6.$sector.$pgsz.1 {
339
sqlite3 db test.db -vfs devsym
341
PRAGMA page_size = $pgsz;
342
PRAGMA auto_vacuum = 0;
343
PRAGMA journal_mode = wal;
346
CREATE TABLE t1(a, b);
347
INSERT INTO t1 VALUES(1, 2);
353
do_test wal-6.$sector.$pgsz.2 {
354
log_deleted test.db-wal
360
file delete -force test.db test.db-wal
361
sqlite3_wal db test.db
363
PRAGMA page_size = 1024;
364
CREATE TABLE t1(a, b);
365
INSERT INTO t1 VALUES(1, 2);
367
list [file size test.db] [file size test.db-wal]
368
} [list 1024 [wal_file_size 3 1024]]
370
execsql { PRAGMA wal_checkpoint }
371
list [file size test.db] [file size test.db-wal]
372
} [list 2048 [wal_file_size 3 1024]]
374
# Execute some transactions in auto-vacuum mode to test database file
380
file delete -force test.db test.db-wal
383
db function blob blob
385
PRAGMA auto_vacuum = 1;
386
PRAGMA journal_mode = wal;
392
PRAGMA page_size = 1024;
394
INSERT INTO t1 VALUES(blob(900));
395
INSERT INTO t1 VALUES(blob(900));
396
INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
397
INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
398
INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
399
INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
400
INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
401
PRAGMA wal_checkpoint;
407
DELETE FROM t1 WHERE rowid<54;
408
PRAGMA wal_checkpoint;
413
# Run some "warm-body" tests to ensure that log-summary files with more
414
# than 256 entries (log summaries that contain index blocks) work Ok.
419
CREATE TABLE t1(x PRIMARY KEY);
420
INSERT INTO t1 VALUES(blob(900));
421
INSERT INTO t1 VALUES(blob(900));
422
INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
423
INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
424
INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
425
INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
426
INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
427
INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */
428
INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */
433
sqlite3_wal db2 test.db
434
execsql {PRAGMA integrity_check } db2
438
file delete -force test2.db test2.db-wal
439
file copy test.db test2.db
440
file copy test.db-wal test2.db-wal
441
sqlite3_wal db3 test2.db
442
execsql {PRAGMA integrity_check } db3
447
execsql { PRAGMA wal_checkpoint }
449
sqlite3_wal db2 test.db
450
execsql {PRAGMA integrity_check } db2
453
foreach handle {db db2 db3} { catch { $handle close } }
456
#-------------------------------------------------------------------------
457
# The following block of tests - wal-10.* - test that the WAL locking
458
# scheme works in simple cases. This block of tests is run twice. Once
459
# using multiple connections in the address space of the current process,
460
# and once with all connections except one running in external processes.
462
do_multiclient_test tn {
464
# Initialize the database schema and contents.
466
do_test wal-10.$tn.1 {
468
PRAGMA auto_vacuum = 0;
469
PRAGMA journal_mode = wal;
470
CREATE TABLE t1(a, b);
471
INSERT INTO t1 VALUES(1, 2);
476
# Open a transaction and write to the database using [db]. Check that [db2]
477
# is still able to read the snapshot before the transaction was opened.
479
do_test wal-10.$tn.2 {
480
execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); }
481
sql2 {SELECT * FROM t1}
484
# Have [db] commit the transaction. Check that [db2] is now seeing the
485
# new, updated snapshot.
487
do_test wal-10.$tn.3 {
489
sql2 {SELECT * FROM t1}
492
# Have [db2] open a read transaction. Then write to the db via [db]. Check
493
# that [db2] is still seeing the original snapshot. Then read with [db3].
494
# [db3] should see the newly committed data.
496
do_test wal-10.$tn.4 {
497
sql2 { BEGIN ; SELECT * FROM t1}
499
do_test wal-10.$tn.5 {
500
execsql { INSERT INTO t1 VALUES(5, 6); }
501
sql2 {SELECT * FROM t1}
503
do_test wal-10.$tn.6 {
504
sql3 {SELECT * FROM t1}
506
do_test wal-10.$tn.7 {
510
# Have [db2] open a write transaction. Then attempt to write to the
511
# database via [db]. This should fail (writer lock cannot be obtained).
513
# Then open a read-transaction with [db]. Commit the [db2] transaction
514
# to disk. Verify that [db] still cannot write to the database (because
515
# it is reading an old snapshot).
517
# Close the current [db] transaction. Open a new one. [db] can now write
518
# to the database (as it is not locked and [db] is reading the latest
521
do_test wal-10.$tn.7 {
522
sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; }
523
catchsql { INSERT INTO t1 VALUES(9, 10) }
524
} {1 {database is locked}}
525
do_test wal-10.$tn.8 {
526
execsql { BEGIN ; SELECT * FROM t1 }
528
do_test wal-10.$tn.9 {
530
catchsql { INSERT INTO t1 VALUES(9, 10) }
531
} {1 {database is locked}}
532
do_test wal-10.$tn.10 {
535
execsql { INSERT INTO t1 VALUES(9, 10) }
537
execsql { SELECT * FROM t1 }
538
} {1 2 3 4 5 6 7 8 9 10}
540
# Open a read transaction with [db2]. Check that this prevents [db] from
541
# checkpointing the database. But not from writing to it.
543
do_test wal-10.$tn.11 {
544
sql2 { BEGIN; SELECT * FROM t1 }
545
} {1 2 3 4 5 6 7 8 9 10}
546
do_test wal-10.$tn.12 {
547
catchsql { PRAGMA wal_checkpoint }
548
} {0 {0 13 13}} ;# Reader no longer block checkpoints
549
do_test wal-10.$tn.13 {
550
execsql { INSERT INTO t1 VALUES(11, 12) }
551
sql2 {SELECT * FROM t1}
552
} {1 2 3 4 5 6 7 8 9 10}
554
# Writers do not block checkpoints any more either.
556
do_test wal-10.$tn.14 {
557
catchsql { PRAGMA wal_checkpoint }
560
# The following series of test cases used to verify another blocking
561
# case in WAL - a case which no longer blocks.
563
do_test wal-10.$tn.15 {
564
sql2 { COMMIT; BEGIN; SELECT * FROM t1; }
565
} {1 2 3 4 5 6 7 8 9 10 11 12}
566
do_test wal-10.$tn.16 {
567
catchsql { PRAGMA wal_checkpoint }
569
do_test wal-10.$tn.17 {
570
execsql { PRAGMA wal_checkpoint }
572
do_test wal-10.$tn.18 {
573
sql3 { BEGIN; SELECT * FROM t1 }
574
} {1 2 3 4 5 6 7 8 9 10 11 12}
575
do_test wal-10.$tn.19 {
576
catchsql { INSERT INTO t1 VALUES(13, 14) }
578
do_test wal-10.$tn.20 {
579
execsql { SELECT * FROM t1 }
580
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
581
do_test wal-10.$tn.21 {
585
do_test wal-10.$tn.22 {
586
execsql { SELECT * FROM t1 }
587
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
589
# Another series of tests that used to demonstrate blocking behavior
590
# but which now work.
592
do_test wal-10.$tn.23 {
593
execsql { PRAGMA wal_checkpoint }
595
do_test wal-10.$tn.24 {
596
sql2 { BEGIN; SELECT * FROM t1; }
597
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
598
do_test wal-10.$tn.25 {
599
execsql { PRAGMA wal_checkpoint }
601
do_test wal-10.$tn.26 {
602
catchsql { INSERT INTO t1 VALUES(15, 16) }
604
do_test wal-10.$tn.27 {
605
sql3 { INSERT INTO t1 VALUES(17, 18) }
607
do_test wal-10.$tn.28 {
609
set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL]
612
execsql { SELECT * FROM t1 }
613
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}
614
do_test wal-10.$tn.29 {
615
execsql { INSERT INTO t1 VALUES(19, 20) }
616
catchsql { PRAGMA wal_checkpoint }
618
do_test wal-10.$tn.30 {
619
code3 { sqlite3_finalize $::STMT }
620
execsql { PRAGMA wal_checkpoint }
623
# At one point, if a reader failed to upgrade to a writer because it
624
# was reading an old snapshot, the write-locks were not being released.
625
# Test that this bug has been fixed.
627
do_test wal-10.$tn.31 {
629
execsql { BEGIN ; SELECT * FROM t1 }
630
sql2 { INSERT INTO t1 VALUES(21, 22) }
631
catchsql { INSERT INTO t1 VALUES(23, 24) }
632
} {1 {database is locked}}
633
do_test wal-10.$tn.32 {
634
# This statement would fail when the bug was present.
635
sql2 { INSERT INTO t1 VALUES(23, 24) }
637
do_test wal-10.$tn.33 {
638
execsql { SELECT * FROM t1 ; COMMIT }
639
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
640
do_test wal-10.$tn.34 {
641
execsql { SELECT * FROM t1 }
642
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
644
# Test that if a checkpointer cannot obtain the required locks, it
645
# releases all locks before returning a busy error.
647
do_test wal-10.$tn.35 {
650
INSERT INTO t1 VALUES('a', 'b');
651
INSERT INTO t1 VALUES('c', 'd');
658
do_test wal-10.$tn.36 {
659
catchsql { PRAGMA wal_checkpoint }
661
do_test wal-10.$tn.36 {
662
sql3 { INSERT INTO t1 VALUES('e', 'f') }
663
sql2 { SELECT * FROM t1 }
665
do_test wal-10.$tn.37 {
667
execsql { PRAGMA wal_checkpoint }
671
#-------------------------------------------------------------------------
672
# This block of tests, wal-11.*, test that nothing goes terribly wrong
673
# if frames must be written to the log file before a transaction is
674
# committed (in order to free up memory).
679
PRAGMA cache_size = 10;
680
PRAGMA page_size = 1024;
681
CREATE TABLE t1(x PRIMARY KEY);
683
list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
686
execsql { PRAGMA wal_checkpoint }
687
list [expr [file size test.db]/1024] [file size test.db-wal]
688
} [list 3 [wal_file_size 3 1024]]
690
execsql { INSERT INTO t1 VALUES( blob(900) ) }
691
list [expr [file size test.db]/1024] [file size test.db-wal]
692
} [list 3 [wal_file_size 4 1024]]
697
INSERT INTO t1 SELECT blob(900) FROM t1; -- 2
698
INSERT INTO t1 SELECT blob(900) FROM t1; -- 4
699
INSERT INTO t1 SELECT blob(900) FROM t1; -- 8
700
INSERT INTO t1 SELECT blob(900) FROM t1; -- 16
702
list [expr [file size test.db]/1024] [file size test.db-wal]
703
} [list 3 [wal_file_size 32 1024]]
706
SELECT count(*) FROM t1;
707
PRAGMA integrity_check;
712
list [expr [file size test.db]/1024] [file size test.db-wal]
713
} [list 3 [wal_file_size 41 1024]]
716
SELECT count(*) FROM t1;
717
PRAGMA integrity_check;
721
execsql { PRAGMA wal_checkpoint }
722
list [expr [file size test.db]/1024] [file size test.db-wal]
723
} [list 37 [wal_file_size 41 1024]]
726
list [expr [file size test.db]/1024] [log_deleted test.db-wal]
728
sqlite3_wal db test.db
731
PRAGMA cache_size = 10;
733
INSERT INTO t1 SELECT blob(900) FROM t1; -- 32
734
SELECT count(*) FROM t1;
736
list [expr [file size test.db]/1024] [file size test.db-wal]
737
} [list 37 [wal_file_size 37 1024]]
740
SELECT count(*) FROM t1;
742
SELECT count(*) FROM t1;
746
list [expr [file size test.db]/1024] [file size test.db-wal]
747
} [list 37 [wal_file_size 37 1024]]
750
INSERT INTO t1 VALUES( blob(900) );
751
SELECT count(*) FROM t1;
752
PRAGMA integrity_check;
756
list [expr [file size test.db]/1024] [file size test.db-wal]
757
} [list 37 [wal_file_size 37 1024]]
760
#-------------------------------------------------------------------------
761
# This block of tests, wal-12.*, tests the fix for a problem that
762
# could occur if a log that is a prefix of an older log is written
763
# into a reused log file.
768
PRAGMA page_size = 1024;
769
CREATE TABLE t1(x, y);
770
CREATE TABLE t2(x, y);
771
INSERT INTO t1 VALUES('A', 1);
773
list [expr [file size test.db]/1024] [file size test.db-wal]
774
} [list 1 [wal_file_size 5 1024]]
779
PRAGMA synchronous = normal;
780
UPDATE t1 SET y = 0 WHERE x = 'A';
782
list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
785
execsql { INSERT INTO t2 VALUES('B', 1) }
786
list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
789
file copy -force test.db test2.db
790
file copy -force test.db-wal test2.db-wal
791
sqlite3_wal db2 test2.db
792
execsql { SELECT * FROM t2 } db2
797
PRAGMA wal_checkpoint;
798
UPDATE t2 SET y = 2 WHERE x = 'B';
799
PRAGMA wal_checkpoint;
800
UPDATE t1 SET y = 1 WHERE x = 'A';
801
PRAGMA wal_checkpoint;
802
UPDATE t1 SET y = 0 WHERE x = 'A';
804
execsql { SELECT * FROM t2 }
807
file copy -force test.db test2.db
808
file copy -force test.db-wal test2.db-wal
809
sqlite3_wal db2 test2.db
810
execsql { SELECT * FROM t2 } db2
815
#-------------------------------------------------------------------------
816
# Test large log summaries.
818
# In this case "large" usually means a log file that requires a wal-index
819
# mapping larger than 64KB (the default initial allocation). A 64KB wal-index
820
# is large enough for a log file that contains approximately 13100 frames.
821
# So the following tests create logs containing at least this many frames.
823
# wal-13.1.*: This test case creates a very large log file within the
824
# file-system (around 200MB). The log file does not contain
825
# any valid frames. Test that the database file can still be
826
# opened and queried, and that the invalid log file causes no
829
# wal-13.2.*: Test that a process may create a large log file and query
830
# the database (including the log file that it itself created).
832
# wal-13.3.*: Test that if a very large log file is created, and then a
833
# second connection is opened on the database file, it is possible
834
# to query the database (and the very large log) using the
837
# wal-13.4.*: Same test as wal-13.3.*. Except in this case the second
838
# connection is opened by an external process.
841
list [file exists test.db] [file exists test.db-wal]
844
set fd [open test.db-wal w]
845
seek $fd [expr 200*1024*1024]
849
execsql { SELECT * FROM t2 }
854
file exists test.db-wal
859
execsql { SELECT count(*) FROM t2 }
862
db function blob blob
863
for {set i 0} {$i < 16} {incr i} {
864
execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 }
866
execsql { SELECT count(*) FROM t2 }
867
} [expr int(pow(2, 16))]
869
expr [file size test.db-wal] > [wal_file_size 33000 1024]
872
do_multiclient_test tn {
875
do_test wal-13.$tn.0 {
877
PRAGMA journal_mode = WAL;
879
INSERT INTO t1 SELECT randomblob(800);
881
sql1 { SELECT count(*) FROM t1 }
884
for {set ii 1} {$ii<16} {incr ii} {
885
do_test wal-13.$tn.$ii.a {
886
sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 }
887
sql2 { SELECT count(*) FROM t1 }
889
do_test wal-13.$tn.$ii.b {
890
sql1 { SELECT count(*) FROM t1 }
892
do_test wal-13.$tn.$ii.c {
893
sql1 { SELECT count(*) FROM t1 }
895
do_test wal-13.$tn.$ii.d {
896
sql1 { PRAGMA integrity_check }
901
#-------------------------------------------------------------------------
902
# Check a fun corruption case has been fixed.
904
# The problem was that after performing a checkpoint using a connection
905
# that had an out-of-date pager-cache, the next time the connection was
906
# used it did not realize the cache was out-of-date and proceeded to
907
# operate with an inconsistent cache. Leading to corruption.
912
file delete -force test.db test.db-wal
917
PRAGMA journal_mode = WAL;
918
CREATE TABLE t1(a PRIMARY KEY, b);
919
INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
920
INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
921
INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
922
INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
926
INSERT INTO t1 SELECT randomblob(10), randomblob(100);
927
INSERT INTO t1 SELECT randomblob(10), randomblob(100);
928
INSERT INTO t1 SELECT randomblob(10), randomblob(100);
929
INSERT INTO t1 SELECT randomblob(10), randomblob(100);
932
# After executing the "PRAGMA wal_checkpoint", connection [db] was being
933
# left with an inconsistent cache. Running the CREATE INDEX statement
934
# in this state led to database corruption.
936
PRAGMA wal_checkpoint;
937
CREATE INDEX i1 on t1(b);
940
db2 eval { PRAGMA integrity_check }
946
#-------------------------------------------------------------------------
947
# The following block of tests - wal-15.* - focus on testing the
948
# implementation of the sqlite3_wal_checkpoint() interface.
950
file delete -force test.db test.db-wal
954
PRAGMA auto_vacuum = 0;
955
PRAGMA page_size = 1024;
956
PRAGMA journal_mode = WAL;
959
CREATE TABLE t1(a, b);
960
INSERT INTO t1 VALUES(1, 2);
964
# Test that an error is returned if the database name is not recognized
967
sqlite3_wal_checkpoint db aux
974
} {unknown database: aux}
976
# Test that an error is returned if an attempt is made to checkpoint
977
# if a transaction is open on the database.
982
INSERT INTO t1 VALUES(3, 4);
984
sqlite3_wal_checkpoint db main
991
} {database table is locked}
993
# Earlier versions returned an error is returned if the db cannot be
994
# checkpointed because of locks held by another connection. Check that
995
# this is no longer the case.
1004
do_test wal-15.4.2 {
1006
sqlite3_wal_checkpoint db
1008
do_test wal-15.4.3 {
1012
# After [db2] drops its lock, [db] may checkpoint the db.
1014
do_test wal-15.4.4 {
1015
execsql { COMMIT } db2
1016
sqlite3_wal_checkpoint db
1018
do_test wal-15.4.5 {
1021
do_test wal-15.4.6 {
1028
#-------------------------------------------------------------------------
1029
# The following block of tests - wal-16.* - test that if a NULL pointer or
1030
# an empty string is passed as the second argument of the wal_checkpoint()
1031
# API, an attempt is made to checkpoint all attached databases.
1033
foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} {
1034
1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1
1035
2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1
1036
3 {db eval "PRAGMA wal_checkpoint"} {0 10 10} 1 1
1038
4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0
1039
5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1
1040
6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0
1041
7 {db eval "PRAGMA main.wal_checkpoint"} {0 10 10} 1 0
1042
8 {db eval "PRAGMA aux.wal_checkpoint"} {0 16 16} 0 1
1043
9 {db eval "PRAGMA temp.wal_checkpoint"} {0 -1 -1} 0 0
1045
do_test wal-16.$tn.1 {
1046
file delete -force test2.db test2.db-wal test2.db-journal
1047
file delete -force test.db test.db-wal test.db-journal
1051
ATTACH 'test2.db' AS aux;
1052
PRAGMA main.auto_vacuum = 0;
1053
PRAGMA aux.auto_vacuum = 0;
1054
PRAGMA main.journal_mode = WAL;
1055
PRAGMA aux.journal_mode = WAL;
1056
PRAGMA synchronous = NORMAL;
1060
do_test wal-16.$tn.2 {
1062
CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b));
1063
CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b));
1065
INSERT INTO t2 VALUES(1, randomblob(1000));
1066
INSERT INTO t2 VALUES(2, randomblob(1000));
1067
INSERT INTO t1 SELECT * FROM t2;
1070
list [file size test.db] [file size test.db-wal]
1071
} [list [expr 1*1024] [wal_file_size 10 1024]]
1072
do_test wal-16.$tn.3 {
1073
list [file size test2.db] [file size test2.db-wal]
1074
} [list [expr 1*1024] [wal_file_size 16 1024]]
1076
do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res
1078
do_test wal-16.$tn.5 {
1079
list [file size test.db] [file size test.db-wal]
1080
} [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]]
1082
do_test wal-16.$tn.6 {
1083
list [file size test2.db] [file size test2.db-wal]
1084
} [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 16 1024]]
1089
#-------------------------------------------------------------------------
1090
# The following tests - wal-17.* - attempt to verify that the correct
1091
# number of "padding" frames are appended to the log file when a transaction
1092
# is committed in synchronous=FULL mode.
1094
# Do this by creating a database that uses 512 byte pages. Then writing
1095
# a transaction that modifies 171 pages. In synchronous=NORMAL mode, this
1096
# produces a log file of:
1098
# 32 + (24+512)*171 = 90312 bytes.
1100
# Slightly larger than 11*8192 = 90112 bytes.
1102
# Run the test using various different sector-sizes. In each case, the
1103
# WAL code should write the 90300 bytes of log file containing the
1104
# transaction, then append as may frames as are required to extend the
1105
# log file so that no part of the next transaction will be written into
1106
# a disk-sector used by transaction just committed.
1108
set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000]
1110
foreach {tn sectorsize logsize} "
1111
1 128 [wal_file_size 172 512]
1112
2 256 [wal_file_size 172 512]
1113
3 512 [wal_file_size 172 512]
1114
4 1024 [wal_file_size 172 512]
1115
5 2048 [wal_file_size 172 512]
1116
6 4096 [wal_file_size 176 512]
1117
7 8192 [wal_file_size 184 512]
1119
file delete -force test.db test.db-wal test.db-journal
1120
sqlite3_simulate_device -sectorsize $sectorsize
1121
sqlite3 db test.db -vfs devsym
1123
do_test wal-17.$tn.1 {
1125
PRAGMA auto_vacuum = 0;
1126
PRAGMA page_size = 512;
1127
PRAGMA journal_mode = WAL;
1128
PRAGMA synchronous = FULL;
1134
for {set i 0} {$i<166} {incr i} {
1135
execsql { INSERT INTO t VALUES(randomblob(400)) }
1139
file size test.db-wal
1142
do_test wal-17.$tn.2 {
1146
do_test wal-17.$tn.3 {
1151
sqlite3_test_control_pending_byte $old_pending_byte
1153
#-------------------------------------------------------------------------
1154
# This test - wal-18.* - verifies a couple of specific conditions that
1155
# may be encountered while recovering a log file are handled correctly:
1157
# wal-18.1.* When the first 32-bits of a frame checksum is correct but
1158
# the second 32-bits are false, and
1160
# wal-18.2.* When the page-size field that occurs at the start of a log
1161
# file is a power of 2 greater than 16384 or smaller than 512.
1163
file delete -force test.db test.db-wal test.db-journal
1167
PRAGMA page_size = 1024;
1168
PRAGMA auto_vacuum = 0;
1169
PRAGMA journal_mode = WAL;
1170
PRAGMA synchronous = OFF;
1172
CREATE TABLE t1(a, b, UNIQUE(a, b));
1173
INSERT INTO t1 VALUES(0, 0);
1174
PRAGMA wal_checkpoint;
1176
INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2
1177
INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4
1178
INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6
1181
file copy -force test.db testX.db
1182
file copy -force test.db-wal testX.db-wal
1184
list [file size testX.db] [file size testX.db-wal]
1185
} [list [expr 3*1024] [wal_file_size 6 1024]]
1187
unset -nocomplain nFrame result
1188
foreach {nFrame result} {
1197
do_test wal-18.1.$nFrame {
1198
file copy -force testX.db test.db
1199
file copy -force testX.db-wal test.db-wal
1201
hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000
1206
PRAGMA integrity_check;
1208
} [concat $result ok]
1212
proc randomblob {pgsz} {
1213
sqlite3 rbdb :memory:
1214
set blob [rbdb one {SELECT randomblob($pgsz)}]
1219
proc logcksum {ckv1 ckv2 blob} {
1224
if {$::tcl_platform(byteOrder) eq "littleEndian"} {
1228
binary scan $blob $scanpattern values
1229
foreach {v1 v2} $values {
1230
set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
1231
set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
1235
file copy -force test.db testX.db
1236
foreach {tn pgsz works} {
1251
if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} {
1255
for {set pg 1} {$pg <= 3} {incr pg} {
1256
file copy -force testX.db test.db
1257
file delete -force test.db-wal
1259
# Check that the database now exists and consists of three pages. And
1260
# that there is no associated wal file.
1262
do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0
1263
do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1
1264
do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3]
1266
do_test wal-18.2.$tn.$pg.4 {
1268
# Create a wal file that contains a single frame (database page
1269
# number $pg) with the commit flag set. The frame checksum is
1270
# correct, but the contents of the database page are corrupt.
1272
# The page-size in the log file header is set to $pgsz. If the
1273
# WAL code considers $pgsz to be a valid SQLite database file page-size,
1274
# the database will be corrupt (because the garbage frame contents
1275
# will be treated as valid content). If $pgsz is invalid (too small
1276
# or too large), the db will not be corrupt as the log file will
1279
set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23]
1280
set framebody [randomblob $pgsz]
1281
set framehdr [binary format IIII $pg 5 22 23]
1284
logcksum c1 c2 $walhdr
1286
append walhdr [binary format II $c1 $c2]
1287
logcksum c1 c2 [string range $framehdr 0 7]
1288
logcksum c1 c2 $framebody
1289
set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2]
1291
set fd [open test.db-wal w]
1292
fconfigure $fd -encoding binary -translation binary
1293
puts -nonewline $fd $walhdr
1294
puts -nonewline $fd $framehdr
1295
puts -nonewline $fd $framebody
1298
file size test.db-wal
1299
} [wal_file_size 1 $pgsz]
1301
do_test wal-18.2.$tn.$pg.5 {
1303
set rc [catch { db one {PRAGMA integrity_check} } msg]
1304
expr { $rc!=0 || $msg!="ok" }
1311
#-------------------------------------------------------------------------
1312
# The following test - wal-19.* - fixes a bug that was present during
1315
# When a database connection in WAL mode is closed, it attempts an
1316
# EXCLUSIVE lock on the database file. If the lock is obtained, the
1317
# connection knows that it is the last connection to disconnect from
1318
# the database, so it runs a checkpoint operation. The bug was that
1319
# the connection was not updating its private copy of the wal-index
1320
# header before doing so, meaning that it could checkpoint an old
1324
file delete -force test.db test.db-wal test.db-journal
1328
PRAGMA journal_mode = WAL;
1329
CREATE TABLE t1(a, b);
1330
INSERT INTO t1 VALUES(1, 2);
1331
INSERT INTO t1 VALUES(3, 4);
1333
execsql { SELECT * FROM t1 } db2
1337
INSERT INTO t1 VALUES(5, 6);
1344
file exists test.db-wal
1347
# When the bug was present, the following was returning {1 2 3 4} only,
1348
# as [db2] had an out-of-date copy of the wal-index header when it was
1352
execsql { SELECT * FROM t1 }
1355
#-------------------------------------------------------------------------
1356
# This test - wal-20.* - uses two connections. One in this process and
1357
# the other in an external process. The procedure is:
1359
# 1. Using connection 1, create the database schema.
1361
# 2. Using connection 2 (in an external process), add so much
1362
# data to the database without checkpointing that a wal-index
1363
# larger than 64KB is required.
1365
# 3. Using connection 1, checkpoint the database. Make sure all
1366
# the data is present and the database is not corrupt.
1368
# At one point, SQLite was failing to grow the mapping of the wal-index
1369
# file in step 3 and the checkpoint was corrupting the database file.
1373
file delete -force test.db test.db-wal test.db-journal
1376
PRAGMA journal_mode = WAL;
1378
INSERT INTO t1 VALUES(randomblob(900));
1379
SELECT count(*) FROM t1;
1383
set ::buddy [launch_testfixture]
1384
testfixture $::buddy {
1386
db transaction { db eval {
1387
PRAGMA wal_autocheckpoint = 0;
1388
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
1389
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
1390
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
1391
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
1392
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
1393
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
1394
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
1395
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
1396
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
1397
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
1398
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
1399
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
1400
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
1401
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
1407
execsql { PRAGMA wal_checkpoint }
1408
execsql { SELECT count(*) FROM t1 }
1413
execsql { SELECT count(*) FROM t1 }
1415
integrity_check wal-20.5
1421
faultsim_delete_and_reopen
1423
PRAGMA journal_mode = WAL;
1424
CREATE TABLE t1(a, b);
1425
INSERT INTO t1 VALUES(1, 2);
1426
INSERT INTO t1 VALUES(3, 4);
1427
INSERT INTO t1 VALUES(5, 6);
1428
INSERT INTO t1 VALUES(7, 8);
1429
INSERT INTO t1 VALUES(9, 10);
1430
INSERT INTO t1 VALUES(11, 12);
1435
PRAGMA cache_size = 10;
1436
PRAGMA wal_checkpoint;
1439
INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1;
1443
execsql { SELECT * FROM t1 }
1444
} {1 2 3 4 5 6 7 8 9 10 11 12}
1446
execsql { PRAGMA integrity_check }
1449
#-------------------------------------------------------------------------
1450
# Test reading and writing of databases with different page-sizes.
1452
foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} {
1453
do_multiclient_test tn [string map [list %PGSZ% $pgsz] {
1454
do_test wal-22.%PGSZ%.$tn.1 {
1456
PRAGMA main.page_size = %PGSZ%;
1457
PRAGMA auto_vacuum = 0;
1458
PRAGMA journal_mode = WAL;
1459
CREATE TABLE t1(x UNIQUE);
1460
INSERT INTO t1 SELECT randomblob(800);
1461
INSERT INTO t1 SELECT randomblob(800);
1462
INSERT INTO t1 SELECT randomblob(800);
1465
do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok}
1466
do_test wal-22.%PGSZ%.$tn.3 {
1467
sql1 {PRAGMA wal_checkpoint}
1468
expr {[file size test.db] % %PGSZ%}
1473
#-------------------------------------------------------------------------
1474
# Test that when 1 or more pages are recovered from a WAL file,
1475
# sqlite3_log() is invoked to report this to the user.
1477
set walfile [file nativename [file join [pwd] test.db-wal]]
1479
file delete -force test.db
1481
faultsim_delete_and_reopen
1483
CREATE TABLE t1(a, b);
1484
PRAGMA journal_mode = WAL;
1485
INSERT INTO t1 VALUES(1, 2);
1486
INSERT INTO t1 VALUES(3, 4);
1488
faultsim_save_and_close
1491
test_sqlite3_log [list lappend ::log]
1494
execsql { SELECT * FROM t1 }
1496
do_test wal-23.2 { set ::log } {}
1501
faultsim_restore_and_reopen
1502
execsql { SELECT * FROM t1 }
1504
set nPage [expr 2+$AUTOVACUUM]
1507
} [list SQLITE_OK "Recovered $nPage frames from WAL file $walfile"]
1510
ifcapable autovacuum {
1511
# This block tests that if the size of a database is reduced by a
1512
# transaction (because of an incremental or auto-vacuum), that no
1513
# data is written to the WAL file for the truncated pages as part
1514
# of the commit. e.g. if a transaction reduces the size of a database
1515
# to N pages, data for page N+1 should not be written to the WAL file
1516
# when committing the transaction. At one point such data was being
1522
do_execsql_test 24.1 {
1523
PRAGMA auto_vacuum = 2;
1524
PRAGMA journal_mode = WAL;
1525
PRAGMA page_size = 1024;
1527
INSERT INTO t1 VALUES(randomblob(5000));
1528
INSERT INTO t1 SELECT * FROM t1;
1529
INSERT INTO t1 SELECT * FROM t1;
1530
INSERT INTO t1 SELECT * FROM t1;
1531
INSERT INTO t1 SELECT * FROM t1;
1536
PRAGMA wal_checkpoint;
1540
file exists test.db-wal
1547
PRAGMA cache_size = 200;
1548
PRAGMA incremental_vacuum;
1549
PRAGMA wal_checkpoint;
1554
file size test.db-wal