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 "blocking-checkpoint"
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 source $testdir/lock_common.tcl
19 source $testdir/wal_common.tcl
20 ifcapable !wal {finish_test ; return }
24 proc db_page_count {{file test.db}} { expr [file size $file] / 1024 }
25 proc wal_page_count {{file test.db}} { wal_frame_count ${file}-wal 1024 }
28 # A checkpoint may be requested either using the C API or by executing
29 # an SQL PRAGMA command. To test both methods, all tests in this file are
30 # run twice - once using each method to request checkpoints.
32 foreach {testprefix do_wal_checkpoint} {
35 proc do_wal_checkpoint { dbhandle args } {
37 foreach key [array names a] {
38 if {[lsearch {-mode -db} $key]<0} { error "unknown switch: $key" }
42 if {[info exists a(-db)]} { append sql "$a(-db)." }
43 append sql "wal_checkpoint"
44 if {[info exists a(-mode)]} { append sql " = $a(-mode)" }
46 uplevel [list $dbhandle eval $sql]
51 proc do_wal_checkpoint { dbhandle args } {
54 foreach key [array names a] {
55 if {[lsearch {-mode -db} $key]<0} { error "unknown switch: $key" }
58 if {$a(-mode)!="restart" && $a(-mode)!="full"} { set a(-mode) passive }
60 set cmd [list sqlite3_wal_checkpoint_v2 $dbhandle $a(-mode)]
61 if {[info exists a(-db)]} { lappend sql $a(-db) }
68 eval $do_wal_checkpoint
70 do_multiclient_test tn {
73 set ::busy_handler_script ""
74 proc busyhandler {n} {
76 eval $::busy_handler_script
85 code1 {sqlite3 db test.db}
86 code2 {sqlite3 db2 test.db}
87 code3 {sqlite3 db3 test.db}
89 sql1 { PRAGMA synchronous = NORMAL }
90 code1 { db busy busyhandler }
96 PRAGMA page_size = 1024;
97 PRAGMA auto_vacuum = 0;
98 CREATE TABLE t1(x, y);
99 PRAGMA journal_mode = WAL;
100 INSERT INTO t1 VALUES(1, zeroblob(1200));
101 INSERT INTO t1 VALUES(2, zeroblob(1200));
102 INSERT INTO t1 VALUES(3, zeroblob(1200));
104 expr [file size test.db] / 1024
107 # Have connection 2 grab a read-lock on the current snapshot.
108 do_test 1.$tn.2 { sql2 { BEGIN; SELECT x FROM t1 } } {1 2 3}
110 # Attempt a checkpoint.
112 code1 { do_wal_checkpoint db }
113 list [db_page_count] [wal_page_count]
116 # Write to the db again. The log cannot wrap because of the lock still
117 # held by connection 2. The busy-handler has not yet been invoked.
119 sql1 { INSERT INTO t1 VALUES(4, zeroblob(1200)) }
120 list [db_page_count] [wal_page_count] $::nBusyHandler
123 # Now do a blocking-checkpoint. Set the busy-handler up so that connection
124 # 2 releases its lock on the 6th invocation. The checkpointer should then
125 # proceed to checkpoint the entire log file. Next write should go to the
126 # start of the log file.
128 set ::busy_handler_script { if {$n==5} { sql2 COMMIT } }
130 code1 { do_wal_checkpoint db -mode restart }
131 list [db_page_count] [wal_page_count] $::nBusyHandler
135 sql1 { INSERT INTO t1 VALUES(5, zeroblob(1200)) }
136 list [db_page_count] [wal_page_count] $::nBusyHandler
141 list [db_page_count] [wal_page_count] $::nBusyHandler
144 do_test 1.$tn.8 { sql2 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5}
146 sql1 { INSERT INTO t1 VALUES(6, zeroblob(1200)) }
147 list [db_page_count] [wal_page_count] $::nBusyHandler
149 do_test 1.$tn.10 { sql3 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5 6}
151 set ::busy_handler_script {
152 if {$n==5} { sql2 COMMIT }
153 if {$n==6} { set ::db_file_size [db_page_count] }
154 if {$n==7} { sql3 COMMIT }
157 code1 { do_wal_checkpoint db -mode restart }
158 list [db_page_count] [wal_page_count] $::nBusyHandler
160 do_test 1.$tn.12 { set ::db_file_size } 10
163 #-------------------------------------------------------------------------
164 # This block of tests explores checkpoint operations on more than one
167 proc setup_and_attach_aux {} {
168 sql1 { ATTACH 'test.db2' AS aux }
169 sql2 { ATTACH 'test.db2' AS aux }
170 sql3 { ATTACH 'test.db2' AS aux }
172 PRAGMA aux.auto_vacuum = 0;
173 PRAGMA main.auto_vacuum = 0;
174 PRAGMA main.page_size=1024; PRAGMA main.journal_mode=WAL;
175 PRAGMA aux.page_size=1024; PRAGMA aux.journal_mode=WAL;
179 proc file_page_counts {} {
180 list [db_page_count test.db ] \
181 [wal_page_count test.db ] \
182 [db_page_count test.db2] \
183 [wal_page_count test.db2]
186 # Test that executing "PRAGMA wal_checkpoint" checkpoints all attached
187 # databases, not just the main db. In capi mode, check that this is
188 # true if a NULL pointer is passed to wal_checkpoint_v2() in place of a
190 do_multiclient_test tn {
194 CREATE TABLE t1(a, b);
195 INSERT INTO t1 VALUES(1, 2);
196 CREATE TABLE aux.t2(a, b);
197 INSERT INTO t2 VALUES(1, 2);
200 do_test 2.2.$tn.2 { file_page_counts } {1 3 1 3}
201 do_test 2.1.$tn.3 { code1 { do_wal_checkpoint db } } {0 3 3}
202 do_test 2.1.$tn.4 { file_page_counts } {2 3 2 3}
205 do_multiclient_test tn {
209 CREATE TABLE t1(a, b);
210 INSERT INTO t1 VALUES(1, 2);
211 CREATE TABLE aux.t2(a, b);
212 INSERT INTO t2 VALUES(1, 2);
213 INSERT INTO t2 VALUES(3, 4);
216 do_test 2.2.$tn.2 { file_page_counts } {1 3 1 4}
217 do_test 2.2.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2}
218 do_test 2.2.$tn.4 { code1 { do_wal_checkpoint db -mode restart } } {1 3 3}
219 do_test 2.2.$tn.5 { file_page_counts } {2 3 2 4}
222 do_multiclient_test tn {
226 CREATE TABLE t1(a, b);
227 INSERT INTO t1 VALUES(1, 2);
228 CREATE TABLE aux.t2(a, b);
229 INSERT INTO t2 VALUES(1, 2);
232 do_test 2.3.$tn.2 { file_page_counts } {1 3 1 3}
233 do_test 2.3.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2}
234 do_test 2.3.$tn.4 { sql1 { INSERT INTO t1 VALUES(3, 4) } } {}
235 do_test 2.3.$tn.5 { sql1 { INSERT INTO t2 VALUES(3, 4) } } {}
236 do_test 2.3.$tn.6 { file_page_counts } {1 4 1 4}
237 do_test 2.3.$tn.7 { code1 { do_wal_checkpoint db -mode full } } {1 4 3}
239 # The checkpoint above only writes page 1 of the db file. The other
240 # page (page 2) is locked by the read-transaction opened by the
241 # [sql2] commmand above. So normally, the db is 1 page in size here.
242 # However, in mmap() mode, the db is pre-allocated to 2 pages at the
243 # start of the checkpoint, even though page 2 cannot be written.
245 if {[permutation]!="mmap"} {set nDb 1}
246 ifcapable !mmap {set nDb 1}
247 do_test 2.3.$tn.8 { file_page_counts } [list $nDb 4 2 4]
250 # Check that checkpoints block on the correct locks. And respond correctly
251 # if they cannot obtain those locks. There are three locks that a checkpoint
252 # may block on (in the following order):
254 # 1. The writer lock: FULL and RESTART checkpoints block until any writer
255 # process releases its lock.
257 # 2. Readers using part of the log file. FULL and RESTART checkpoints block
258 # until readers using part (but not all) of the log file have finished.
260 # 3. Readers using any of the log file. After copying data into the
261 # database file, RESTART checkpoints block until readers using any part
262 # of the log file have finished.
264 # This test case involves running a checkpoint while there exist other
265 # processes holding all three types of locks.
267 foreach {tn1 checkpoint busy_on ckpt_expected expected} {
268 1 PASSIVE - {0 3 3} -
276 7 RESTART - {0 4 4} 3
277 8 RESTART 1 {1 3 3} 1
278 9 RESTART 2 {1 4 3} 2
279 10 RESTART 3 {1 4 4} 3
282 do_multiclient_test tn {
285 proc busyhandler {x} {
286 set ::max_busyhandler $x
287 if {$::busy_on!="-" && $x==$::busy_on} { return 1 }
289 1 { sql2 "COMMIT ; BEGIN ; SELECT * FROM t1" }
295 set ::max_busyhandler -
297 do_test 2.4.$tn1.$tn.1 {
299 CREATE TABLE t1(a, b);
300 INSERT INTO t1 VALUES(1, 2);
302 sql2 { BEGIN; INSERT INTO t1 VALUES(3, 4) }
303 sql3 { BEGIN; SELECT * FROM t1 }
306 do_test 2.4.$tn1.$tn.2 {
307 code1 { db busy busyhandler }
308 code1 { do_wal_checkpoint db -mode [string tolower $checkpoint] }
310 do_test 2.4.$tn1.$tn.3 { set ::max_busyhandler } $expected
315 do_multiclient_test tn {
317 code1 $do_wal_checkpoint
318 code2 $do_wal_checkpoint
319 code3 $do_wal_checkpoint
323 PRAGMA auto_vacuum = 0;
324 PRAGMA journal_mode = WAL;
325 PRAGMA synchronous = normal;
326 CREATE TABLE t1(x, y);
329 sql2 { PRAGMA journal_mode }
330 sql3 { PRAGMA journal_mode }
333 do_test 3.$tn.2 { code2 { do_wal_checkpoint db2 } } {0 2 2}
335 do_test 3.$tn.3 { code2 { do_wal_checkpoint db2 } } {0 2 2}
337 do_test 3.$tn.4 { code3 { do_wal_checkpoint db3 } } {0 2 2}
343 code1 {sqlite3 db test.db}
344 code2 {sqlite3 db2 test.db}
345 code3 {sqlite3 db3 test.db}
347 do_test 3.$tn.5 { sql3 { PRAGMA journal_mode } } {wal}
349 do_test 3.$tn.6 { code3 { do_wal_checkpoint db3 } } {0 0 0}