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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/lock_common.tcl
16 source $testdir/wal_common.tcl
17 set testprefix e_walckpt
19 # The following two commands are used to determine if any of the files
20 # "test.db", "test.db2" and "test.db3" are modified by a test case.
22 # The [save_db_hashes] command saves a hash of the current contents of
23 # all three files in global variables. The [compare_db_hashes] compares
24 # the current contents with the saved hashes and returns a list of the
25 # files that have changed.
27 proc save_db_hashes {} {
29 foreach f {test.db test.db2 test.db3} {
31 catch { set H($f) [md5file $f] }
34 proc compare_db_hashes {} {
37 foreach f {test.db test.db2 test.db3} {
39 catch { set expect [md5file $f] }
40 if {$H($f) != $expect} { lappend ret $f }
45 #-------------------------------------------------------------------------
46 # All calls to the [sqlite3_wal_checkpoint_v2] command made within this
47 # file use this wrapper. It's sole purpose is to throw an error if the
48 # following requirement is violated:
50 # EVIDENCE-OF: R-60567-47780 Unless it returns SQLITE_MISUSE, the
51 # sqlite3_wal_checkpoint_v2() interface sets the error information that
52 # is queried by sqlite3_errcode() and sqlite3_errmsg().
54 proc wal_checkpoint_v2 {db args} {
56 uplevel sqlite3_wal_checkpoint_v2 $db $args
59 set errcode "SQLITE_OK"
61 set errcode [lindex [split $msg " "] 0]
62 } elseif { [lindex $msg 0] } {
63 set errcode "SQLITE_BUSY"
66 if {$errcode != "SQLITE_MISUSE" && [sqlite3_errcode $db] != $errcode} {
67 error "sqlite3_errcode mismatch! (1) $errcode!=[sqlite3_errcode $db]"
78 # The following tests are run 3 times, each using a different method of
79 # invoking a checkpoint:
81 # 1) Using sqlite3_wal_checkpoint_v2()
82 # 2) Using "PRAGMA wal_checkpoint"
83 # 3) Using sqlite3_wal_checkpoint() in place of checkpoint_v2(PASSIVE)
85 # Cases (2) and (3) are to show that the following statements are
86 # correct, respectively:
88 # EVIDENCE-OF: R-36706-10507 The PRAGMA wal_checkpoint command can be
89 # used to invoke this interface from SQL.
91 # EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is
93 # sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0).
97 proc checkpoint {db mode args} {
98 eval wal_checkpoint_v2 [list $db] [list $mode] $args
103 proc checkpoint {db mode args} {
104 set sql "PRAGMA wal_checkpoint = $mode"
105 if {[llength $args] && [lindex $args 0]!=""} {
106 set sql "PRAGMA [lindex $args 0].wal_checkpoint = $mode"
108 set rc [catch { $db eval $sql } msg]
110 regsub {database} $msg {database:} msg
111 error "[sqlite3_errcode $db] - $msg"
118 proc checkpoint {db mode args} {
119 if {$mode == "passive"} {
120 set rc [eval sqlite3_wal_checkpoint [list $db] $args]
121 if {$rc != "SQLITE_OK"} {
122 error "$rc - [sqlite3_errmsg $db]"
125 eval wal_checkpoint_v2 [list $db] [list $mode] $args
135 forcedelete test.db2 test.db3 test.db4
137 ATTACH 'test.db2' AS aux;
138 ATTACH 'test.db3' AS aux2;
139 ATTACH 'test.db4' AS aux3;
141 CREATE TABLE aux.t2(x);
142 CREATE TABLE aux2.t3(x);
143 CREATE TABLE aux3.t4(x);
144 PRAGMA main.journal_mode = WAL;
145 PRAGMA aux.journal_mode = WAL;
146 PRAGMA aux2.journal_mode = WAL;
147 /* Leave aux4 in rollback mode */
150 # EVIDENCE-OF: R-49787-09095 The sqlite3_wal_checkpoint_v2(D,X,M,L,C)
151 # interface runs a checkpoint operation on database X of database
152 # connection D in mode M. Status information is written back into
153 # integers pointed to by L and C.
155 # Tests 1, 2 and 3 below verify the "on database X" part of the
156 # above. Other parts of this requirement are tested below.
158 # EVIDENCE-OF: R-00653-06026 If parameter zDb is NULL or points to a
159 # zero length string, then the specified operation is attempted on all
160 # WAL databases attached to database connection db.
162 # Tests 4 and 5 below test this.
164 foreach {tn2 zDb dblist} {
168 4 "" {test.db test.db2 test.db3}
169 5 - {test.db test.db2 test.db3}
174 INSERT INTO t1 VALUES(1);
175 INSERT INTO t2 VALUES(2);
176 INSERT INTO t3 VALUES(3);
181 checkpoint db passive
183 checkpoint db passive $zDb
190 # EVIDENCE-OF: R-38207-48996 If zDb is not NULL (or a zero length
191 # string) and is not the name of any attached database, SQLITE_ERROR is
192 # returned to the caller.
194 list [catch { checkpoint db passive notadb } msg] $msg
195 } {1 {SQLITE_ERROR - unknown database: notadb}}
197 # EVIDENCE-OF: R-14303-42483 If database zDb is the name of an attached
198 # database that is not in WAL mode, SQLITE_OK is returned and both
199 # *pnLog and *pnCkpt set to -1.
202 # With sqlite3_wal_checkpoint() the two output variables cannot be
203 # tested. So just test that no error is returned when attempting to
204 # checkpoint a db in rollback mode.
205 do_test $tn.2.2.a { checkpoint db passive aux3 } {}
207 do_test $tn.2.2.b { checkpoint db passive aux3 } {0 -1 -1}
210 # EVIDENCE-OF: R-62028-47212 All calls obtain an exclusive "checkpoint"
211 # lock on the database file.
216 proc filelock {method file handle details} {
217 # Test for an exclusive checkpoint lock. A checkpoint lock locks a
218 # single byte starting at offset 1.
219 if {$details == "1 1 lock exclusive"} { set ::seen_checkpoint_lock 1 }
221 sqlite3 db test.db -vfs tvfs
223 execsql { INSERT INTO t1 VALUES('xyz') }
224 unset -nocomplain ::seen_checkpoint_lock
225 checkpoint db passive
226 set ::seen_checkpoint_lock
235 #-----------------------------------------------------------------------
236 # EVIDENCE-OF: R-10421-19736 If any other process is running a
237 # checkpoint operation at the same time, the lock cannot be obtained and
238 # SQLITE_BUSY is returned.
240 # EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
241 # it will not be invoked in this case.
245 sqlite3 db test.db -vfs tvfs
246 sqlite3 db2 test.db -vfs tvfs
250 PRAGMA auto_vacuum = 0;
251 PRAGMA journal_mode = WAL;
252 CREATE TABLE t1(x, y);
253 INSERT INTO t1 VALUES(1,2);
254 INSERT INTO t1 VALUES(3,4);
255 INSERT INTO t1 VALUES(5,6);
257 file size test.db-wal
258 } [wal_file_size 5 1024]
261 # Connection [db] runs a checkpoint. During this checkpoint, each
262 # time it calls xWrite() to write a page into the database file, we
263 # attempt to start a checkpoint using [db2]. According to the
264 # first requirement being tested, this should return SQLITE_BUSY. According
265 # to the second, the busy-handler belonging to [db2] should not be
269 set ::write_errors [list]
270 proc busy_callback {args} {
271 lappend ::write_errors "busy handler called!"
273 proc write_callback {args} {
274 set rc [catch {checkpoint db2 passive} msg]
275 if {0==[regexp "database is locked" $msg] && $msg!="1 -1 -1"} {
276 lappend ::write_errors "$rc $msg"
280 db2 busy busy_callback
281 tvfs script write_callback
284 db eval {SELECT * FROM sqlite_master}
297 proc busy_handler {mode busy_handler_mode n} {
298 incr ::busy_handler_counter
299 switch -- $busy_handler_mode {
301 # Do nothing. Do not block.
306 # Close first the reader, then later the writer. Give up before
307 # closing the [db6] reader.
308 if {$n==5} { catch {db2 eval commit} }
309 if {$n==10} { catch {db3 eval commit} }
310 if {$n==15} { return 1 }
315 # Close first the writer, then later the reader. And finally the
317 if {$n==5} { catch {db2 eval commit} }
318 if {$n==10} { catch {db3 eval commit} }
319 if {$n==15} { catch {db6 eval commit} }
325 foreach {mode busy_handler_mode} {
328 restart 1 restart 2 restart 3
329 truncate 1 truncate 2 truncate 3
331 set tp "$tn.$mode.$busy_handler_mode"
335 # Set up a callback function for xSync and xWrite calls made during
338 set ::checkpoint_ongoing 0
339 proc tvfs_callback {method args} {
340 if {$::checkpoint_ongoing==0} return
342 set tail [file tail [lindex $args 0]]
343 if {$method == "xSync" && $tail == "test.db"} {
346 if {$method == "xWrite" && $tail=="test.db"} {
347 if {$::write_ok < 0} {
348 set ::write_ok [expr ![catch {db5 eval { BEGIN IMMEDIATE }}]]
349 catch { db5 eval ROLLBACK }
351 if {$::read_ok < 0} {
352 set ::read_ok [expr ![catch {db5 eval { SELECT * FROM t1 }}]]
355 # If one has not already been opened, open a read-transaction using
357 catch { db6 eval { BEGIN ; SELECT * FROM sqlite_master } } msg
359 if {$method == "xShmLock" } {
360 set details [lindex $args 2]
361 if {$details == "0 1 lock exclusive"} { set ::seen_writer_lock 1 }
368 sqlite3 db test.db -vfs tvfs
370 tvfs script tvfs_callback
372 do_execsql_test $tp.0 {
373 CREATE TABLE t1(a, b);
374 CREATE TABLE t2(a, b);
375 PRAGMA journal_mode = wal;
376 INSERT INTO t1 VALUES(1, 2);
377 INSERT INTO t1 VALUES(3, 4);
378 INSERT INTO t1 VALUES(5, 6);
381 # Open a reader on the current database snapshot.
383 sqlite3 db2 test.db -vfs tvfs
386 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
390 # Open a writer. Write a transaction. Then begin, but do not commit,
391 # a second transaction.
393 sqlite3 db3 test.db -vfs tvfs
395 INSERT INTO t2 VALUES(7, 8);
397 INSERT INTO t2 VALUES(9, 10);
398 SELECT * FROM t1 UNION ALL SELECT * FROM t2;
400 } {1 2 3 4 5 6 7 8 9 10}
402 sqlite3 db5 test.db -vfs tvfs
403 sqlite3 db6 test.db -vfs tvfs
405 # Register a busy-handler with connection [db].
407 db busy [list busy_handler $mode $busy_handler_mode]
409 set ::busy_handler_counter 0
412 set ::seen_writer_lock 0
414 set ::checkpoint_ongoing 1
416 checkpoint db $mode main
419 set ::checkpoint_ongoing 0
420 set ::did_restart_blocking [expr {[catch {db6 eval commit}]}]
422 if { $mode=="passive" } {
423 # EVIDENCE-OF: R-16333-64433 Checkpoint as many frames as possible
424 # without waiting for any database readers or writers to finish, then
425 # sync the database file if all frames in the log were checkpointed.
427 # "As many frames as possible" means all but the last two transactions
428 # (the two that write to table t2, of which the scond is unfinished).
429 # So copying the db file only we see the t1 change, but not the t2
432 # The busy handler is not invoked (see below) and the db reader and
433 # writer are still active - so the checkpointer did not wait for either
434 # readers or writers. As a result the checkpoint was not finished and
435 # so the db file is not synced.
437 # EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
438 # in the SQLITE_CHECKPOINT_PASSIVE mode.
440 # It's not. Test case "$tp.6".
443 forcecopy test.db abc.db
445 db4 eval { SELECT * FROM t1 UNION ALL SELECT * FROM t2 }
447 do_test $tp.5 { set ::sync_counter } 0
448 do_test $tp.6 { set ::busy_handler_counter } 0
454 # EVIDENCE-OF: R-65499-53765 On the other hand, passive mode might leave
455 # the checkpoint unfinished if there are concurrent readers or writers.
457 # The reader and writer have now dropped their locks. And so a
458 # checkpoint now is able to checkpoint more frames. Showing that the
459 # attempt above was left "unfinished".
461 # Also, because the checkpoint finishes this time, the db is synced.
462 # Which is part of R-16333-64433 above.
464 set ::checkpoint_ongoing 1
466 checkpoint db $mode main
467 forcecopy test.db abc.db
469 db4 eval { SELECT * FROM t1 UNION ALL SELECT * FROM t2 }
470 } {1 2 3 4 5 6 7 8 9 10}
471 set ::checkpoint_ongoing 0
472 do_test $tp.7 { set ::sync_counter } 1
473 do_test $tp.8 { set ::busy_handler_counter } 0
477 if { $mode=="full" || $mode=="restart" || $mode=="truncate" } {
479 # EVIDENCE-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
480 # TRUNCATE modes also obtain the exclusive "writer" lock on the
483 # Or at least attempts to obtain.
486 set ::seen_writer_lock
489 if {$busy_handler_mode==2 || $busy_handler_mode==3} {
490 # EVIDENCE-OF: R-59171-47567 This mode blocks (it invokes the
491 # busy-handler callback) until there is no database writer and all
492 # readers are reading from the most recent database snapshot.
494 # The test below shows that both the reader and writer have
497 # Also restated by the following two. That both busy_handler_mode
498 # values 2 and 3 work show that both of the following are true - as
499 # they release the reader and writer transactions in different
502 # EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
503 # immediately, and a busy-handler is configured, it is invoked and the
504 # writer lock retried until either the busy-handler returns 0 or the
505 # lock is successfully obtained.
507 # EVIDENCE-OF: R-48107-00250 The busy-handler is also invoked while
508 # waiting for database readers as described above.
511 list [catchsql COMMIT db2] [catchsql COMMIT db3]
513 {1 {cannot commit - no transaction is active}} \
514 {1 {cannot commit - no transaction is active}} \
517 # EVIDENCE-OF: R-29177-48281 It then checkpoints all frames in the log
518 # file and syncs the database file.
521 forcecopy test.db abc.db
523 db4 eval { SELECT * FROM t1 UNION ALL SELECT * FROM t2 }
524 } {1 2 3 4 5 6 7 8 9 10}
525 do_test $tp.9 { set ::sync_counter } 1
528 # EVIDENCE-OF: R-51867-44713 This mode blocks new database writers
529 # while it is pending, but new database readers are allowed to continue
532 # EVIDENCE-OF: R-47276-58266 Like SQLITE_CHECKPOINT_FULL, this mode
533 # blocks new database writer attempts while it is pending, but does not
536 # The first of the above two refers to "full" mode. The second
540 list $::write_ok $::read_ok
543 # EVIDENCE-OF: R-12410-31217 This mode works the same way as
544 # SQLITE_CHECKPOINT_FULL with the addition that after checkpointing the
545 # log file it blocks (calls the busy-handler callback) until all
546 # readers are reading from the database file only.
548 # The stuff above passed, so the first part of this requirement
549 # is met. The second part is tested below. If the checkpoint mode
550 # was "restart" or "truncate", then the busy-handler will have
551 # been called to block on wal-file readers.
554 set ::did_restart_blocking
555 } [expr {($mode=="restart"||$mode=="truncate")&&$busy_handler_mode==3}]
557 # EVIDENCE-OF: R-44699-57140 This mode works the same way as
558 # SQLITE_CHECKPOINT_RESTART with the addition that it also truncates
559 # the log file to zero bytes just prior to a successful return.
560 if {$mode=="truncate" && $busy_handler_mode==3} {
562 file size test.db-wal
565 } elseif {$busy_handler_mode==1} {
567 # EVIDENCE-OF: R-34519-06271 SQLITE_BUSY is returned in this case.
569 # ($tn==2) is the loop that uses "PRAGMA wal_checkpoint"
570 do_test $tp.13 { sqlite3_errcode db } {SQLITE_BUSY}
573 # EVIDENCE-OF: R-49155-63541 If the busy-handler returns 0 before the
574 # writer lock is obtained or while waiting for database readers, the
575 # checkpoint operation proceeds from that point in the same way as
576 # SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
577 # without blocking any further.
579 forcecopy test.db abc.db
581 db4 eval { SELECT * FROM t1 UNION ALL SELECT * FROM t2 }
583 do_test $tp.15 { set ::sync_counter } 0
584 do_test $tp.16 { set ::busy_handler_counter } 1
599 #-----------------------------------------------------------------------
600 # EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
603 # Valid checkpoint modes are 0, 1, 2 and 3.
606 foreach {tn mode res} {
607 0 -1001 {1 {SQLITE_MISUSE - not an error}}
608 1 -1 {1 {SQLITE_MISUSE - not an error}}
613 6 4 {1 {SQLITE_MISUSE - not an error}}
614 7 114 {1 {SQLITE_MISUSE - not an error}}
615 8 1000000 {1 {SQLITE_MISUSE - not an error}}
618 list [catch "wal_checkpoint_v2 db $mode" msg] $msg
624 forcedelete test.db test.db2 test.db3
627 sqlite3 db test.db -vfs tvfs
629 ATTACH 'test.db2' AS aux2;
630 ATTACH 'test.db3' AS aux3;
631 PRAGMA main.journal_mode = WAL;
632 PRAGMA aux2.journal_mode = WAL;
633 PRAGMA aux3.journal_mode = WAL;
635 CREATE TABLE main.t1(x,y);
636 CREATE TABLE aux2.t2(x,y);
637 CREATE TABLE aux3.t3(x,y);
639 INSERT INTO t1 VALUES('a', 'b');
640 INSERT INTO t2 VALUES('a', 'b');
641 INSERT INTO t3 VALUES('a', 'b');
643 sqlite3 db2 test.db2 -vfs tvfs
647 # EVIDENCE-OF: R-41299-52117 If no error (SQLITE_BUSY or otherwise) is
648 # encountered while processing the attached databases, SQLITE_OK is
651 lindex [wal_checkpoint_v2 db truncate] 0
652 } {0} ;# 0 -> SQLITE_OK
654 list [expr [file size test.db-wal]==0] \
655 [expr [file size test.db2-wal]==0] \
656 [expr [file size test.db3-wal]==0]
661 # EVIDENCE-OF: R-38578-34175 If an SQLITE_BUSY error is encountered when
662 # processing one or more of the attached WAL databases, the operation is
663 # still attempted on any remaining attached databases and SQLITE_BUSY is
664 # returned at the end.
665 db2 eval { BEGIN; INSERT INTO t2 VALUES('d', 'e'); }
667 lindex [wal_checkpoint_v2 db truncate] 0
668 } {1} ;# 1 -> SQLITE_BUSY
670 list [expr [file size test.db-wal]==0] \
671 [expr [file size test.db2-wal]==0] \
672 [expr [file size test.db3-wal]==0]
678 # EVIDENCE-OF: R-38049-07913 If any other error occurs while processing
679 # an attached database, processing is abandoned and the error code is
680 # returned to the caller immediately.
682 tvfs script inject_ioerr
683 proc inject_ioerr {method file args} {
684 if {[file tail $file]=="test.db2"} {
685 return "SQLITE_IOERR"
690 list [catch { wal_checkpoint_v2 db truncate } msg] $msg
691 } {1 {SQLITE_IOERR - disk I/O error}}
693 list [expr [file size test.db-wal]==0] \
694 [expr [file size test.db2-wal]==0] \
695 [expr [file size test.db3-wal]==0]
710 PRAGMA auto_vacuum = 0;
711 PRAGMA journal_mode = WAL;
712 CREATE TABLE t1(a, b);
713 INSERT INTO t1 VALUES(1, 2);
715 file size test.db-wal
716 } [wal_file_size 3 1024]
719 db2 eval { BEGIN; SELECT * FROM t1; }
720 db eval { INSERT INTO t1 VALUES(3, 4) }
721 file size test.db-wal
722 } [wal_file_size 4 1024]
724 # At this point the log file contains 4 frames. 3 of which it should
725 # be possible to checkpoint.
727 # EVIDENCE-OF: R-16642-42503 If pnLog is not NULL, then *pnLog is set to
728 # the total number of frames in the log file or to -1 if the checkpoint
729 # could not run because of an error or because the database is not in
732 # EVIDENCE-OF: R-10514-25250 If pnCkpt is not NULL,then *pnCkpt is set
733 # to the total number of checkpointed frames in the log file (including
734 # any that were already checkpointed before the function was called) or
735 # to -1 if the checkpoint could not run due to an error or because the
736 # database is not in WAL mode.
739 lrange [wal_checkpoint_v2 db passive] 1 2
742 # EVIDENCE-OF: R-37257-17813 Note that upon successful completion of an
743 # SQLITE_CHECKPOINT_TRUNCATE, the log file will have been truncated to
744 # zero bytes and so both *pnLog and *pnCkpt will be set to zero.
748 wal_checkpoint_v2 db truncate