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/malloc_common.tcl
17 source $testdir/wal_common.tcl
20 if {[atomic_batch_write test.db]} {
25 # Do not use a codec for tests in this file, as the database file is
26 # manipulated directly using tcl scripts (using the [hexio_write] command).
31 # pager1-1.*: Test inter-process locking (clients in multiple processes).
33 # pager1-2.*: Test intra-process locking (multiple clients in this process).
35 # pager1-3.*: Savepoint related tests.
37 # pager1-4.*: Hot-journal related tests.
39 # pager1-5.*: Cases related to multi-file commits.
41 # pager1-6.*: Cases related to "PRAGMA max_page_count"
43 # pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
45 # pager1-8.*: Cases using temporary and in-memory databases.
47 # pager1-9.*: Tests related to the backup API.
49 # pager1-10.*: Test that the assumed file-system sector-size is limited to
52 # pager1-12.*: Tests involving "PRAGMA page_size"
54 # pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST"
56 # pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF"
58 # pager1-15.*: Varying sqlite3_vfs.szOsFile
60 # pager1-16.*: Varying sqlite3_vfs.mxPathname
62 # pager1-17.*: Tests related to "PRAGMA omit_readlock"
63 # (The omit_readlock pragma has been removed and so have
66 # pager1-18.*: Test that the pager layer responds correctly if the b-tree
67 # requests an invalid page number (due to db corruption).
70 proc recursive_select {id table {script {}}} {
72 db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" {
73 recursive_select $rowid $table $script
76 if {$cnt==0} { eval $script }
79 set a_string_counter 1
81 global a_string_counter
83 string range [string repeat "${a_string_counter}." $n] 1 $n
85 db func a_string a_string
87 do_multiclient_test tn {
89 # Create and populate a database table using connection [db]. Check
90 # that connections [db2] and [db3] can see the schema and content.
92 do_test pager1-$tn.1 {
94 CREATE TABLE t1(a PRIMARY KEY, b);
95 CREATE INDEX i1 ON t1(b);
96 INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two');
99 do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
100 do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two}
102 # Open a transaction and add a row using [db]. This puts [db] in
103 # RESERVED state. Check that connections [db2] and [db3] can still
104 # read the database content as it was before the transaction was
105 # opened. [db] should see the inserted row.
107 do_test pager1-$tn.4 {
110 INSERT INTO t1 VALUES(3, 'three');
113 do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
114 do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
116 # [db] still has an open write transaction. Check that this prevents
117 # other connections (specifically [db2]) from writing to the database.
119 # Even if [db2] opens a transaction first, it may not write to the
120 # database. After the attempt to write the db within a transaction,
121 # [db2] is left with an open transaction, but not a read-lock on
122 # the main database. So it does not prevent [db] from committing.
124 do_test pager1-$tn.8 {
125 csql2 { UPDATE t1 SET a = a + 10 }
126 } {1 {database is locked}}
127 do_test pager1-$tn.9 {
130 UPDATE t1 SET a = a + 10;
132 } {1 {database is locked}}
134 # Have [db] commit its transactions. Check the other connections can
135 # now see the new database content.
137 do_test pager1-$tn.10 { sql1 { COMMIT } } {}
138 do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
139 do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
140 do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
142 # Check that, as noted above, [db2] really did keep an open transaction
143 # after the attempt to write the database failed.
145 do_test pager1-$tn.14 {
147 } {1 {cannot start a transaction within a transaction}}
148 do_test pager1-$tn.15 { sql2 { ROLLBACK } } {}
150 # Have [db2] open a transaction and take a read-lock on the database.
151 # Check that this prevents [db] from writing to the database (outside
152 # of any transaction). After this fails, check that [db3] can read
153 # the db (showing that [db] did not take a PENDING lock etc.)
155 do_test pager1-$tn.15 {
156 sql2 { BEGIN; SELECT * FROM t1; }
157 } {1 one 2 two 3 three}
158 do_test pager1-$tn.16 {
159 csql1 { UPDATE t1 SET a = a + 10 }
160 } {1 {database is locked}}
161 do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
163 # This time, have [db] open a transaction before writing the database.
164 # This works - [db] gets a RESERVED lock which does not conflict with
165 # the SHARED lock [db2] is holding.
167 do_test pager1-$tn.18 {
170 UPDATE t1 SET a = a + 10;
173 do_test pager1-$tn-19 {
174 sql1 { PRAGMA lock_status }
175 } {main reserved temp closed}
176 do_test pager1-$tn-20 {
177 sql2 { PRAGMA lock_status }
178 } {main shared temp closed}
180 # Check that all connections can still read the database. Only [db] sees
181 # the updated content (as the transaction has not been committed yet).
183 do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
184 do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
185 do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
187 # Because [db2] still has the SHARED lock, [db] is unable to commit the
188 # transaction. If it tries, an error is returned and the connection
189 # upgrades to a PENDING lock.
191 # Once this happens, [db] can read the database and see the new content,
192 # [db2] (still holding SHARED) can still read the old content, but [db3]
193 # (not holding any lock) is prevented by [db]'s PENDING from reading
196 do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}}
197 do_test pager1-$tn-25 {
198 sql1 { PRAGMA lock_status }
199 } {main pending temp closed}
200 do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
201 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
202 do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
204 # Have [db2] commit its read transaction, releasing the SHARED lock it
205 # is holding. Now, neither [db2] nor [db3] may read the database (as [db]
206 # is still holding a PENDING).
208 do_test pager1-$tn.29 { sql2 { COMMIT } } {}
209 do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
210 do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
212 # [db] is now able to commit the transaction. Once the transaction is
213 # committed, all three connections can read the new content.
215 do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {}
216 do_test pager1-$tn.26 { sql1 { COMMIT } } {}
217 do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three}
218 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three}
219 do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three}
221 # Install a busy-handler for connection [db].
226 if {$n>5} { sql2 COMMIT }
231 do_test pager1-$tn.29 {
232 sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') }
234 do_test pager1-$tn.30 {
235 sql2 { BEGIN ; SELECT * FROM t1 }
236 } {21 one 22 two 23 three}
237 do_test pager1-$tn.31 { sql1 COMMIT } {}
238 do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6}
241 #-------------------------------------------------------------------------
242 # Savepoint related test cases.
244 # pager1-3.1.2.*: Force a savepoint rollback to cause the database file
247 # pager1-3.1.3.*: Use a journal created in synchronous=off mode as part
248 # of a savepoint rollback.
250 do_test pager1-3.1.1 {
251 faultsim_delete_and_reopen
253 CREATE TABLE t1(a PRIMARY KEY, b);
254 CREATE TABLE counter(
258 INSERT INTO counter VALUES(0, 0);
259 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
260 UPDATE counter SET i = i+1;
262 CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN
263 UPDATE counter SET u = u+1;
266 execsql { SELECT * FROM counter }
269 do_execsql_test pager1-3.1.2 {
270 PRAGMA cache_size = 10;
272 INSERT INTO t1 VALUES(1, randomblob(1500));
273 INSERT INTO t1 VALUES(2, randomblob(1500));
274 INSERT INTO t1 VALUES(3, randomblob(1500));
275 SELECT * FROM counter;
277 do_catchsql_test pager1-3.1.3 {
278 INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1
279 } {1 {CHECK constraint failed: counter}}
280 do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
281 do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
282 do_execsql_test pager1-3.6 { COMMIT } {}
284 foreach {tn sql tcl} {
285 7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } {
286 testvfs tv -default 1
287 tv devchar safe_append
289 8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } {
290 testvfs tv -default 1
291 tv devchar sequential
293 9 { PRAGMA synchronous = FULL } { }
294 10 { PRAGMA synchronous = NORMAL } { }
295 11 { PRAGMA synchronous = OFF } { }
296 12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { }
297 13 { PRAGMA synchronous = FULL } {
298 testvfs tv -default 1
299 tv devchar sequential
301 14 { PRAGMA locking_mode = EXCLUSIVE } {
304 do_test pager1-3.$tn.1 {
306 faultsim_delete_and_reopen
307 db func a_string a_string
310 PRAGMA auto_vacuum = 2;
311 PRAGMA cache_size = 10;
312 CREATE TABLE z(x INTEGER PRIMARY KEY, y);
314 INSERT INTO z VALUES(NULL, a_string(800));
315 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2
316 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4
317 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8
318 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16
319 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32
320 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64
321 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128
322 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256
325 execsql { PRAGMA auto_vacuum }
327 do_execsql_test pager1-3.$tn.2 {
329 INSERT INTO z VALUES(NULL, a_string(800));
330 INSERT INTO z VALUES(NULL, a_string(800));
332 UPDATE z SET y = NULL WHERE x>256;
333 PRAGMA incremental_vacuum;
334 SELECT count(*) FROM z WHERE x < 100;
339 do_execsql_test pager1-3.$tn.3 {
342 UPDATE z SET y = y||x;
345 SELECT count(*) FROM z;
348 do_execsql_test pager1-3.$tn.4 {
350 UPDATE z SET y = y||x;
353 do_execsql_test pager1-3.$tn.5 {
354 SELECT count(*) FROM z;
356 PRAGMA integrity_check;
359 do_execsql_test pager1-3.$tn.6 {
368 #-------------------------------------------------------------------------
369 # Hot journal rollback related test cases.
371 # pager1.4.1.*: Test that the pager module deletes very small invalid
374 # pager1.4.2.*: Test that if the master journal pointer at the end of a
375 # hot-journal file appears to be corrupt (checksum does not
376 # compute) the associated journal is rolled back (and no
377 # xAccess() call to check for the presence of any master
378 # journal file is made).
380 # pager1.4.3.*: Test that the contents of a hot-journal are ignored if the
381 # page-size or sector-size in the journal header appear to
382 # be invalid (too large, too small or not a power of 2).
384 # pager1.4.4.*: Test hot-journal rollback of journal file with a master
385 # journal pointer generated in various "PRAGMA synchronous"
388 # pager1.4.5.*: Test that hot-journal rollback stops if it encounters a
389 # journal-record for which the checksum fails.
391 # pager1.4.6.*: Test that when rolling back a hot-journal that contains a
392 # master journal pointer, the master journal file is deleted
393 # after all the hot-journals that refer to it are deleted.
395 # pager1.4.7.*: Test that if a hot-journal file exists but a client can
396 # open it for reading only, the database cannot be accessed and
397 # SQLITE_CANTOPEN is returned.
399 do_test pager1.4.1.1 {
400 faultsim_delete_and_reopen
402 CREATE TABLE x(y, z);
403 INSERT INTO x VALUES(1, 2);
405 set fd [open test.db-journal w]
406 puts -nonewline $fd "helloworld"
408 file exists test.db-journal
410 do_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2}
411 do_test pager1.4.1.3 { file exists test.db-journal } {0}
413 # Set up a [testvfs] to snapshot the file-system just before SQLite
414 # deletes the master-journal to commit a multi-file transaction.
416 # In subsequent test cases, invoking [faultsim_restore_and_reopen] sets
417 # up the file system to contain two databases, two hot-journal files and
420 do_test pager1.4.2.1 {
421 testvfs tstvfs -default 1
422 tstvfs filter xDelete
423 tstvfs script xDeleteCallback
424 proc xDeleteCallback {method file args} {
425 set file [file tail $file]
426 if { [string match *mj* $file] } { faultsim_save }
428 faultsim_delete_and_reopen
429 db func a_string a_string
431 ATTACH 'test.db2' AS aux;
432 PRAGMA journal_mode = DELETE;
433 PRAGMA main.cache_size = 10;
434 PRAGMA aux.cache_size = 10;
435 CREATE TABLE t1(a UNIQUE, b UNIQUE);
436 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
437 INSERT INTO t1 VALUES(a_string(200), a_string(300));
438 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
439 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
440 INSERT INTO t2 SELECT * FROM t1;
442 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
443 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
444 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
445 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
446 REPLACE INTO t2 SELECT * FROM t1;
453 if {$::tcl_platform(platform)!="windows"} {
454 do_test pager1.4.2.2 {
455 faultsim_restore_and_reopen
457 SELECT count(*) FROM t1;
458 PRAGMA integrity_check;
461 do_test pager1.4.2.3 {
462 faultsim_restore_and_reopen
463 foreach f [glob test.db-mj*] { forcedelete $f }
465 SELECT count(*) FROM t1;
466 PRAGMA integrity_check;
469 do_test pager1.4.2.4 {
470 faultsim_restore_and_reopen
471 hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
473 SELECT count(*) FROM t1;
474 PRAGMA integrity_check;
477 do_test pager1.4.2.5 {
478 faultsim_restore_and_reopen
479 hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
480 foreach f [glob test.db-mj*] { forcedelete $f }
482 SELECT count(*) FROM t1;
483 PRAGMA integrity_check;
488 do_test pager1.4.3.1 {
489 testvfs tstvfs -default 1
491 tstvfs script xSyncCallback
492 proc xSyncCallback {method file args} {
493 set file [file tail $file]
494 if { 0==[string match *journal $file] } { faultsim_save }
496 faultsim_delete_and_reopen
498 PRAGMA journal_mode = DELETE;
499 CREATE TABLE t1(a, b);
500 INSERT INTO t1 VALUES(1, 2);
501 INSERT INTO t1 VALUES(3, 4);
507 foreach {tn ofst value result} {
512 6 20 131072 {1 2 3 4}
516 9 24 131072 {1 2 3 4}
520 do_test pager1.4.3.$tn {
521 faultsim_restore_and_reopen
522 hexio_write test.db-journal $ofst [format %.8x $value]
523 execsql { SELECT * FROM t1 }
528 # Set up a VFS that snapshots the file-system just before a master journal
529 # file is deleted to commit a multi-file transaction. Specifically, the
530 # file-system is saved just before the xDelete() call to remove the
531 # master journal file from the file-system.
534 testvfs tv -default 1
535 tv script copy_on_mj_delete
536 set ::mj_filename_length 0
537 set ::mj_delete_cnt 0
538 proc copy_on_mj_delete {method filename args} {
539 if {[string match *mj* [file tail $filename]]} {
541 # NOTE: Is the file name relative? If so, add the length of the current
544 if {[is_relative_file $filename]} {
545 set ::mj_filename_length \
546 [expr {[string length $filename] + [string length $::pwd]}]
548 set ::mj_filename_length [string length $filename]
557 1 { set prefix "test.db" }
559 # This test depends on the underlying VFS being able to open paths
560 # 512 bytes in length. The idea is to create a hot-journal file that
561 # contains a master-journal pointer so large that it could contain
562 # a valid page record (if the file page-size is 512 bytes). So as to
563 # make sure SQLite doesn't get confused by this.
565 set nPadding [expr 511 - $::mj_filename_length]
566 if {$tcl_platform(platform)=="windows"} {
567 # TBD need to figure out how to do this correctly for Windows!!!
568 set nPadding [expr 255 - $::mj_filename_length]
571 # We cannot just create a really long database file name to open, as
572 # Linux limits a single component of a path to 255 bytes by default
573 # (and presumably other systems have limits too). So create a directory
574 # hierarchy to work in.
576 set dirname "d123456789012345678901234567890/"
577 set nDir [expr $nPadding / 32]
579 set p [string repeat $dirname $nDir]
584 set padding [string repeat x [expr $nPadding %32]]
585 set prefix "test.db${padding}"
589 foreach {tn2 sql usesMJ} {
591 PRAGMA main.synchronous=OFF;
592 PRAGMA aux.synchronous=OFF;
593 PRAGMA journal_mode = DELETE;
596 PRAGMA main.synchronous=OFF;
597 PRAGMA aux.synchronous=OFF;
598 PRAGMA main.page_size = 512;
599 PRAGMA aux.page_size = 512;
600 PRAGMA journal_mode = DELETE;
603 PRAGMA main.synchronous=NORMAL;
604 PRAGMA aux.synchronous=NORMAL;
605 PRAGMA journal_mode = DELETE;
608 PRAGMA main.synchronous=FULL;
609 PRAGMA aux.synchronous=FULL;
610 PRAGMA journal_mode = DELETE;
613 PRAGMA main.synchronous=NORMAL;
614 PRAGMA aux.synchronous=NORMAL;
615 PRAGMA journal_mode = WAL;
618 PRAGMA main.synchronous=NORMAL;
619 PRAGMA aux.synchronous=NORMAL;
620 PRAGMA main.journal_mode=DELETE;
621 PRAGMA aux.journal_mode=WAL;
624 PRAGMA main.synchronous=FULL;
625 PRAGMA aux.synchronous=OFF;
626 PRAGMA journal_mode=DELETE;
629 PRAGMA main.synchronous=OFF;
630 PRAGMA aux.synchronous=NORMAL;
631 PRAGMA journal_mode=DELETE;
634 PRAGMA main.synchronous=NORMAL;
635 PRAGMA aux.synchronous=NORMAL;
636 PRAGMA main.journal_mode=DELETE;
637 PRAGMA aux.journal_mode = MEMORY;
640 PRAGMA main.synchronous=NORMAL;
641 PRAGMA aux.synchronous=NORMAL;
642 PRAGMA main.journal_mode=DELETE;
643 PRAGMA aux.journal_mode = TRUNCATE;
646 PRAGMA main.synchronous=NORMAL;
647 PRAGMA aux.synchronous=NORMAL;
648 PRAGMA main.journal_mode=DELETE;
649 PRAGMA aux.journal_mode = PERSIST;
653 set tn "${tn1}.${tn2}"
655 # Set up a connection to have two databases, test.db (main) and
656 # test.db2 (aux). Then run a multi-file transaction on them. The
657 # VFS will snapshot the file-system just before the master-journal
658 # file is deleted to commit the transaction.
661 do_test pager1-4.4.$tn.1 {
662 set ::mj_delete_cnt 0
663 faultsim_delete_and_reopen $prefix
665 ATTACH '${prefix}2' AS aux;
668 CREATE TABLE aux.b(x);
669 INSERT INTO a VALUES('double-you');
670 INSERT INTO a VALUES('why');
671 INSERT INTO a VALUES('zed');
672 INSERT INTO b VALUES('won');
673 INSERT INTO b VALUES('too');
674 INSERT INTO b VALUES('free');
678 INSERT INTO a SELECT * FROM b WHERE rowid<=3;
679 INSERT INTO b SELECT * FROM a WHERE rowid<=3;
685 # Verify that a master journal was deleted only for those cases where
686 # master journals really ought to be used
688 do_test pager1-4.4.$tn.1b {
692 # Check that the transaction was committed successfully.
694 do_execsql_test pager1-4.4.$tn.2 {
696 } {double-you why zed won too free}
697 do_execsql_test pager1-4.4.$tn.3 {
699 } {won too free double-you why zed}
702 # Restore the file-system and reopen the databases. Check that it now
703 # appears that the transaction was not committed (because the file-system
704 # was restored to the state where it had not been).
706 do_test pager1-4.4.$tn.4 {
707 faultsim_restore_and_reopen $prefix
708 execsql "ATTACH '${prefix}2' AS aux"
710 do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed}
711 do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free}
714 # Restore the file-system again. This time, before reopening the databases,
715 # delete the master-journal file from the file-system. It now appears that
716 # the transaction was committed (no master-journal file == no rollback).
718 do_test pager1-4.4.$tn.7 {
719 if {$::mj_delete_cnt>0} {
720 faultsim_restore_and_reopen $prefix
721 foreach f [glob ${prefix}-mj*] { forcedelete $f }
726 execsql "ATTACH '${prefix}2' AS aux"
727 glob -nocomplain ${prefix}-mj*
729 do_execsql_test pager1-4.4.$tn.8 {
731 } {double-you why zed won too free}
732 do_execsql_test pager1-4.4.$tn.9 {
734 } {won too free double-you why zed}
743 # Set up a VFS to make a copy of the file-system just before deleting a
744 # journal file to commit a transaction. The transaction modifies exactly
745 # two database pages (and page 1 - the change counter).
747 testvfs tv -default 1
749 tv script copy_on_journal_delete
751 proc copy_on_journal_delete {method filename args} {
752 if {[string match *journal $filename]} faultsim_save
755 faultsim_delete_and_reopen
756 do_execsql_test pager1.4.5.1 {
757 PRAGMA journal_mode = DELETE;
758 PRAGMA page_size = 1024;
759 CREATE TABLE t1(a, b);
760 CREATE TABLE t2(a, b);
761 INSERT INTO t1 VALUES('I', 'II');
762 INSERT INTO t2 VALUES('III', 'IV');
764 INSERT INTO t1 VALUES(1, 2);
765 INSERT INTO t2 VALUES(3, 4);
770 # Check the transaction was committed:
772 do_execsql_test pager1.4.5.2 {
775 } {I II 1 2 III IV 3 4}
777 # Now try four tests:
779 # pager1-4.5.3: Restore the file-system. Check that the whole transaction
782 # pager1-4.5.4: Restore the file-system. Corrupt the first record in the
783 # journal. Check the transaction is not rolled back.
785 # pager1-4.5.5: Restore the file-system. Corrupt the second record in the
786 # journal. Check that the first record in the transaction is
787 # played back, but not the second.
789 # pager1-4.5.6: Restore the file-system. Try to open the database with a
790 # readonly connection. This should fail, as a read-only
791 # connection cannot roll back the database file.
793 faultsim_restore_and_reopen
794 do_execsql_test pager1.4.5.3 {
798 faultsim_restore_and_reopen
799 hexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF
800 do_execsql_test pager1.4.5.4 {
803 } {I II 1 2 III IV 3 4}
804 faultsim_restore_and_reopen
805 hexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF
806 do_execsql_test pager1.4.5.5 {
811 faultsim_restore_and_reopen
813 sqlite3 db test.db -readonly 1
814 do_catchsql_test pager1.4.5.6 {
817 } {1 {attempt to write a readonly database}}
820 # Snapshot the file-system just before multi-file commit. Save the name
821 # of the master journal file in $::mj_filename.
823 tv script copy_on_mj_delete
825 proc copy_on_mj_delete {method filename args} {
826 if {[string match *mj* [file tail $filename]]} {
827 set ::mj_filename $filename
832 do_test pager1.4.6.1 {
833 faultsim_delete_and_reopen
835 PRAGMA journal_mode = DELETE;
836 ATTACH 'test.db2' AS two;
837 CREATE TABLE t1(a, b);
838 CREATE TABLE two.t2(a, b);
839 INSERT INTO t1 VALUES(1, 't1.1');
840 INSERT INTO t2 VALUES(1, 't2.1');
842 UPDATE t1 SET b = 't1.2';
843 UPDATE t2 SET b = 't2.2';
850 faultsim_restore_and_reopen
851 do_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1}
852 do_test pager1.4.6.3 { file exists $::mj_filename } {1}
853 do_execsql_test pager1.4.6.4 {
854 ATTACH 'test.db2' AS two;
857 do_test pager1.4.6.5 { file exists $::mj_filename } {0}
859 faultsim_restore_and_reopen
861 do_test pager1.4.6.8 {
862 set ::mj_filename1 $::mj_filename
866 PRAGMA journal_mode = DELETE;
867 ATTACH 'test.db3' AS three;
868 CREATE TABLE three.t3(a, b);
869 INSERT INTO t3 VALUES(1, 't3.1');
871 UPDATE t2 SET b = 't2.3';
872 UPDATE t3 SET b = 't3.3';
875 expr {$::mj_filename1 != $::mj_filename}
877 faultsim_restore_and_reopen
880 # The file-system now contains:
883 # * three hot-journal files
884 # * two master-journal files.
886 # The hot-journals associated with test.db2 and test.db3 point to
887 # master journal $::mj_filename. The hot-journal file associated with
888 # test.db points to master journal $::mj_filename1. So reading from
889 # test.db should delete $::mj_filename1.
891 do_test pager1.4.6.9 {
892 lsort [glob test.db*]
894 test.db test.db2 test.db3 \
895 test.db-journal test.db2-journal test.db3-journal \
896 [file tail $::mj_filename] [file tail $::mj_filename1]
899 # The master-journal $::mj_filename1 contains pointers to test.db and
900 # test.db2. However the hot-journal associated with test.db2 points to
901 # a different master-journal. Therefore, reading from test.db only should
902 # be enough to cause SQLite to delete $::mj_filename1.
904 do_test pager1.4.6.10 { file exists $::mj_filename } {1}
905 do_test pager1.4.6.11 { file exists $::mj_filename1 } {1}
906 do_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1}
907 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
908 do_test pager1.4.6.14 { file exists $::mj_filename1 } {0}
910 do_execsql_test pager1.4.6.12 {
911 ATTACH 'test.db2' AS two;
914 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
915 do_execsql_test pager1.4.6.14 {
916 ATTACH 'test.db3' AS three;
919 do_test pager1.4.6.15 { file exists $::mj_filename } {0}
924 testvfs tv -default 1
926 tv script copy_on_journal_delete
928 proc copy_on_journal_delete {method filename args} {
929 if {[string match *journal $filename]} faultsim_save
932 faultsim_delete_and_reopen
933 do_execsql_test pager1.4.7.1 {
934 PRAGMA journal_mode = DELETE;
935 CREATE TABLE t1(x PRIMARY KEY, y);
936 CREATE INDEX i1 ON t1(y);
937 INSERT INTO t1 VALUES('I', 'one');
938 INSERT INTO t1 VALUES('II', 'four');
939 INSERT INTO t1 VALUES('III', 'nine');
941 INSERT INTO t1 VALUES('IV', 'sixteen');
942 INSERT INTO t1 VALUES('V' , 'twentyfive');
949 test_syscall install fchmod
950 test_syscall fault 1 1
952 do_test pager1.4.7.2 {
953 faultsim_restore_and_reopen
954 catch {file attributes test.db-journal -permissions r--------}
955 catch {file attributes test.db-journal -readonly 1}
956 catchsql { SELECT * FROM t1 }
957 } {1 {unable to open database file}}
960 test_syscall fault 0 0
962 do_test pager1.4.7.3 {
964 catch {file attributes test.db-journal -permissions rw-rw-rw-}
965 catch {file attributes test.db-journal -readonly 0}
966 delete_file test.db-journal
967 file exists test.db-journal
969 do_test pager1.4.8.1 {
970 catch {file attributes test.db -permissions r--------}
971 catch {file attributes test.db -readonly 1}
973 db eval { SELECT * FROM t1 }
974 sqlite3_db_readonly db main
976 do_test pager1.4.8.2 {
977 sqlite3_db_readonly db xyz
979 do_test pager1.4.8.3 {
981 catch {file attributes test.db -readonly 0}
982 catch {file attributes test.db -permissions rw-rw-rw-} msg
984 db eval { SELECT * FROM t1 }
985 sqlite3_db_readonly db main
988 #-------------------------------------------------------------------------
989 # The following tests deal with multi-file commits.
991 # pager1-5.1.*: The case where a multi-file cannot be committed because
992 # another connection is holding a SHARED lock on one of the
993 # files. After the SHARED lock is removed, the COMMIT succeeds.
995 # pager1-5.2.*: Multi-file commits with journal_mode=memory.
997 # pager1-5.3.*: Multi-file commits with journal_mode=memory.
999 # pager1-5.4.*: Check that with synchronous=normal, the master-journal file
1000 # name is added to a journal file immediately after the last
1001 # journal record. But with synchronous=full, extra unused space
1002 # is allocated between the last journal record and the
1003 # master-journal file name so that the master-journal file
1004 # name does not lie on the same sector as the last journal file
1007 # pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is
1008 # truncated to zero bytes when a multi-file transaction is
1009 # committed (instead of the first couple of bytes being zeroed).
1012 do_test pager1-5.1.1 {
1013 faultsim_delete_and_reopen
1015 ATTACH 'test.db2' AS aux;
1016 CREATE TABLE t1(a, b);
1017 CREATE TABLE aux.t2(a, b);
1018 INSERT INTO t1 VALUES(17, 'Lenin');
1019 INSERT INTO t1 VALUES(22, 'Stalin');
1020 INSERT INTO t1 VALUES(53, 'Khrushchev');
1023 do_test pager1-5.1.2 {
1026 INSERT INTO t1 VALUES(64, 'Brezhnev');
1027 INSERT INTO t2 SELECT * FROM t1;
1029 sqlite3 db2 test.db2
1035 do_test pager1-5.1.3 {
1037 } {1 {database is locked}}
1038 do_test pager1-5.1.4 {
1041 execsql { SELECT * FROM t2 } db2
1042 } {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev}
1043 do_test pager1-5.1.5 {
1047 do_test pager1-5.2.1 {
1049 PRAGMA journal_mode = memory;
1051 INSERT INTO t1 VALUES(84, 'Andropov');
1052 INSERT INTO t2 VALUES(84, 'Andropov');
1056 do_test pager1-5.3.1 {
1058 PRAGMA journal_mode = off;
1060 INSERT INTO t1 VALUES(85, 'Gorbachev');
1061 INSERT INTO t2 VALUES(85, 'Gorbachev');
1066 do_test pager1-5.4.1 {
1069 sqlite3 db test.db -vfs tv
1070 execsql { ATTACH 'test.db2' AS aux }
1073 tv script max_journal_size
1076 proc max_journal_size {method args} {
1078 catch { set sz [file size test.db-journal] }
1079 if {$sz > $::max_journal} {
1080 set ::max_journal $sz
1085 PRAGMA journal_mode = DELETE;
1086 PRAGMA synchronous = NORMAL;
1088 INSERT INTO t1 VALUES(85, 'Gorbachev');
1089 INSERT INTO t2 VALUES(85, 'Gorbachev');
1093 # The size of the journal file is now:
1095 # 1) 512 byte header +
1096 # 2) 2 * (1024+8) byte records +
1097 # 3) 20+N bytes of master-journal pointer, where N is the size of
1098 # the master-journal name encoded as utf-8 with no nul term.
1100 set mj_pointer [expr {
1101 20 + [string length "test.db-mjXXXXXX9XX"]
1104 # NOTE: For item 3 above, if the current SQLite VFS lacks the concept of a
1105 # current directory, the length of the current directory name plus 1
1106 # character for the directory separator character are NOT counted as
1107 # part of the total size; otherwise, they are.
1110 set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
1112 expr {$::max_journal==(512+2*(1024+8)+$mj_pointer)}
1114 do_test pager1-5.4.2 {
1117 PRAGMA synchronous = full;
1119 DELETE FROM t1 WHERE b = 'Lenin';
1120 DELETE FROM t2 WHERE b = 'Lenin';
1124 # In synchronous=full mode, the master-journal pointer is not written
1125 # directly after the last record in the journal file. Instead, it is
1126 # written starting at the next (in this case 512 byte) sector boundary.
1128 set mj_pointer [expr {
1129 20 + [string length "test.db-mjXXXXXX9XX"]
1132 # NOTE: If the current SQLite VFS lacks the concept of a current directory,
1133 # the length of the current directory name plus 1 character for the
1134 # directory separator character are NOT counted as part of the total
1135 # size; otherwise, they are.
1138 set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
1140 expr {$::max_journal==(((512+2*(1024+8)+511)/512)*512 + $mj_pointer)}
1145 do_test pager1-5.5.1 {
1148 ATTACH 'test.db2' AS aux;
1149 PRAGMA journal_mode = PERSIST;
1150 CREATE TABLE t3(a, b);
1151 INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1;
1152 UPDATE t3 SET b = randomblob(1501);
1154 expr [file size test.db-journal] > 15000
1156 do_test pager1-5.5.2 {
1158 PRAGMA synchronous = full;
1160 DELETE FROM t1 WHERE b = 'Stalin';
1161 DELETE FROM t2 WHERE b = 'Stalin';
1164 file size test.db-journal
1168 #-------------------------------------------------------------------------
1169 # The following tests work with "PRAGMA max_page_count"
1171 do_test pager1-6.1 {
1172 faultsim_delete_and_reopen
1174 PRAGMA auto_vacuum = none;
1175 PRAGMA max_page_count = 10;
1176 CREATE TABLE t2(a, b);
1177 CREATE TABLE t3(a, b);
1178 CREATE TABLE t4(a, b);
1179 CREATE TABLE t5(a, b);
1180 CREATE TABLE t6(a, b);
1181 CREATE TABLE t7(a, b);
1182 CREATE TABLE t8(a, b);
1183 CREATE TABLE t9(a, b);
1184 CREATE TABLE t10(a, b);
1187 do_catchsql_test pager1-6.2 {
1188 CREATE TABLE t11(a, b)
1189 } {1 {database or disk is full}}
1190 do_execsql_test pager1-6.4 { PRAGMA max_page_count } {10}
1191 do_execsql_test pager1-6.5 { PRAGMA max_page_count = 15 } {15}
1192 do_execsql_test pager1-6.6 { CREATE TABLE t11(a, b) } {}
1193 do_execsql_test pager1-6.7 {
1195 INSERT INTO t11 VALUES(1, 2);
1196 PRAGMA max_page_count = 13;
1198 do_execsql_test pager1-6.8 {
1199 INSERT INTO t11 VALUES(3, 4);
1200 PRAGMA max_page_count = 10;
1202 do_execsql_test pager1-6.9 { COMMIT } {}
1204 do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {11}
1205 do_execsql_test pager1-6.11 { SELECT * FROM t11 } {1 2 3 4}
1206 do_execsql_test pager1-6.12 { PRAGMA max_page_count } {11}
1209 #-------------------------------------------------------------------------
1210 # The following tests work with "PRAGMA journal_mode=TRUNCATE" and
1211 # "PRAGMA locking_mode=EXCLUSIVE".
1213 # Each test is specified with 5 variables. As follows:
1215 # $tn: Test Number. Used as part of the [do_test] test names.
1216 # $sql: SQL to execute.
1217 # $res: Expected result of executing $sql.
1218 # $js: The expected size of the journal file, in bytes, after executing
1219 # the SQL script. Or -1 if the journal is not expected to exist.
1220 # $ws: The expected size of the WAL file, in bytes, after executing
1221 # the SQL script. Or -1 if the WAL is not expected to exist.
1224 faultsim_delete_and_reopen
1225 foreach {tn sql res js ws} [subst {
1228 CREATE TABLE t1(a, b);
1229 PRAGMA auto_vacuum=OFF;
1230 PRAGMA synchronous=NORMAL;
1231 PRAGMA page_size=1024;
1232 PRAGMA locking_mode=EXCLUSIVE;
1233 PRAGMA journal_mode=TRUNCATE;
1234 INSERT INTO t1 VALUES(1, 2);
1235 } {exclusive truncate} 0 -1
1249 4 { PRAGMA journal_mode = WAL } wal -1 -1
1250 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024]
1251 6 { PRAGMA locking_mode = NORMAL } exclusive -1 [wal_file_size 1 1024]
1252 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024]
1254 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1
1255 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1
1256 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1
1259 do_execsql_test pager1-7.1.$tn.1 $sql $res
1260 catch { set J -1 ; set J [file size test.db-journal] }
1261 catch { set W -1 ; set W [file size test.db-wal] }
1262 do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
1266 do_test pager1-7.2.1 {
1267 faultsim_delete_and_reopen
1269 PRAGMA locking_mode = EXCLUSIVE;
1270 CREATE TABLE t1(a, b);
1272 PRAGMA journal_mode = delete;
1273 PRAGMA journal_mode = truncate;
1275 } {exclusive delete truncate}
1276 do_test pager1-7.2.2 {
1277 execsql { INSERT INTO t1 VALUES(1, 2) }
1278 execsql { PRAGMA journal_mode = persist }
1280 do_test pager1-7.2.3 {
1283 PRAGMA journal_mode = persist;
1284 PRAGMA journal_size_limit;
1288 #-------------------------------------------------------------------------
1289 # The following tests, pager1-8.*, test that the special filenames
1290 # ":memory:" and "" open temporary databases.
1292 foreach {tn filename} {
1296 do_test pager1-8.$tn.1 {
1297 faultsim_delete_and_reopen
1299 sqlite3 db $filename
1301 PRAGMA auto_vacuum = 1;
1303 INSERT INTO x1 VALUES('Charles');
1304 INSERT INTO x1 VALUES('James');
1305 INSERT INTO x1 VALUES('Mary');
1308 } {Charles James Mary}
1310 do_test pager1-8.$tn.2 {
1311 sqlite3 db2 $filename
1312 catchsql { SELECT * FROM x1 } db2
1313 } {1 {no such table: x1}}
1315 do_execsql_test pager1-8.$tn.3 {
1317 INSERT INTO x1 VALUES('William');
1318 INSERT INTO x1 VALUES('Anne');
1323 #-------------------------------------------------------------------------
1324 # The next block of tests - pager1-9.* - deal with interactions between
1325 # the pager and the backup API. Test cases:
1327 # pager1-9.1.*: Test that a backup completes successfully even if the
1328 # source db is written to during the backup op.
1330 # pager1-9.2.*: Test that a backup completes successfully even if the
1331 # source db is written to and then rolled back during a
1334 do_test pager1-9.0.1 {
1335 faultsim_delete_and_reopen
1336 db func a_string a_string
1338 PRAGMA cache_size = 10;
1340 CREATE TABLE ab(a, b, UNIQUE(a, b));
1341 INSERT INTO ab VALUES( a_string(200), a_string(300) );
1342 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1343 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1344 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1345 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1346 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1347 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1348 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1352 do_test pager1-9.0.2 {
1353 sqlite3 db2 test.db2
1354 db2 eval { PRAGMA cache_size = 10 }
1355 sqlite3_backup B db2 main db main
1356 list [B step 10000] [B finish]
1357 } {SQLITE_DONE SQLITE_OK}
1358 do_test pager1-9.0.3 {
1359 db one {SELECT md5sum(a, b) FROM ab}
1360 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1362 do_test pager1-9.1.1 {
1363 execsql { UPDATE ab SET a = a_string(201) }
1364 sqlite3_backup B db2 main db main
1367 do_test pager1-9.1.2 {
1368 execsql { UPDATE ab SET b = a_string(301) }
1369 list [B step 10000] [B finish]
1370 } {SQLITE_DONE SQLITE_OK}
1371 do_test pager1-9.1.3 {
1372 db one {SELECT md5sum(a, b) FROM ab}
1373 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1374 do_test pager1-9.1.4 { execsql { SELECT count(*) FROM ab } } {128}
1376 do_test pager1-9.2.1 {
1377 execsql { UPDATE ab SET a = a_string(202) }
1378 sqlite3_backup B db2 main db main
1381 do_test pager1-9.2.2 {
1384 UPDATE ab SET b = a_string(301);
1387 list [B step 10000] [B finish]
1388 } {SQLITE_DONE SQLITE_OK}
1389 do_test pager1-9.2.3 {
1390 db one {SELECT md5sum(a, b) FROM ab}
1391 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1392 do_test pager1-9.2.4 { execsql { SELECT count(*) FROM ab } } {128}
1396 do_test pager1-9.3.1 {
1397 testvfs tv -default 1
1399 faultsim_delete_and_reopen
1401 execsql { PRAGMA page_size = 1024 }
1402 for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" }
1404 if {[nonzero_reserved_bytes]} {
1405 # backup with a page size changes is not possible with the codec
1407 do_test pager1-9.3.2codec {
1408 sqlite3 db2 test.db2
1410 PRAGMA page_size = 4096;
1411 PRAGMA synchronous = OFF;
1412 CREATE TABLE t1(a, b);
1413 CREATE TABLE t2(a, b);
1415 sqlite3_backup B db2 main db main
1417 list [B step 10000] [B finish]
1418 } {SQLITE_READONLY SQLITE_READONLY}
1419 do_test pager1-9.3.3codec {
1424 } [file size test.db2]
1426 do_test pager1-9.3.2 {
1427 sqlite3 db2 test.db2
1429 PRAGMA page_size = 4096;
1430 PRAGMA synchronous = OFF;
1431 CREATE TABLE t1(a, b);
1432 CREATE TABLE t2(a, b);
1434 sqlite3_backup B db2 main db main
1436 list [B step 10000] [B finish]
1437 } {SQLITE_DONE SQLITE_OK}
1438 do_test pager1-9.3.3 {
1443 } [file size test.db]
1446 do_test pager1-9.4.1 {
1447 faultsim_delete_and_reopen
1448 sqlite3 db2 test.db2
1450 PRAGMA page_size = 4096;
1451 CREATE TABLE t1(a, b);
1452 CREATE TABLE t2(a, b);
1454 sqlite3_backup B db2 main db main
1455 list [B step 10000] [B finish]
1456 } {SQLITE_DONE SQLITE_OK}
1457 do_test pager1-9.4.2 {
1458 list [file size test.db2] [file size test.db]
1462 #-------------------------------------------------------------------------
1463 # Test that regardless of the value returned by xSectorSize(), the
1464 # minimum effective sector-size is 512 and the maximum 65536 bytes.
1466 testvfs tv -default 1
1467 foreach sectorsize {
1469 32 64 128 256 512 1024 2048
1470 4096 8192 16384 32768 65536 131072 262144
1472 tv sectorsize $sectorsize
1475 if {$sectorsize < 512} { set eff 512 }
1476 if {$sectorsize > 65536} { set eff 65536 }
1478 do_test pager1-10.$sectorsize.1 {
1479 faultsim_delete_and_reopen
1480 db func a_string a_string
1482 PRAGMA journal_mode = PERSIST;
1483 PRAGMA page_size = 1024;
1485 CREATE TABLE t1(a, b);
1486 CREATE TABLE t2(a, b);
1487 CREATE TABLE t3(a, b);
1490 file size test.db-journal
1491 } [expr $sectorsize > 65536 ? 65536 : ($sectorsize<32 ? 512 : $sectorsize)]
1493 do_test pager1-10.$sectorsize.2 {
1495 INSERT INTO t3 VALUES(a_string(300), a_string(300));
1496 INSERT INTO t3 SELECT * FROM t3; /* 2 */
1497 INSERT INTO t3 SELECT * FROM t3; /* 4 */
1498 INSERT INTO t3 SELECT * FROM t3; /* 8 */
1499 INSERT INTO t3 SELECT * FROM t3; /* 16 */
1500 INSERT INTO t3 SELECT * FROM t3; /* 32 */
1504 do_test pager1-10.$sectorsize.3 {
1508 PRAGMA cache_size = 10;
1511 recursive_select 32 t3 {db eval "INSERT INTO t2 VALUES(1, 2)"}
1518 do_test pager1-10.$sectorsize.4 {
1520 CREATE TABLE t6(a, b);
1521 CREATE TABLE t7(a, b);
1522 CREATE TABLE t5(a, b);
1528 CREATE TABLE t6(a, b);
1530 recursive_select 32 t3 {db eval "INSERT INTO t5 VALUES(1, 2)"}
1541 do_test pager1.10.x.1 {
1542 faultsim_delete_and_reopen
1544 PRAGMA auto_vacuum = none;
1545 PRAGMA page_size = 1024;
1548 for {set i 0} {$i<30} {incr i} {
1549 execsql { INSERT INTO t1 VALUES(zeroblob(900)) }
1553 do_test pager1.10.x.2 {
1560 do_test pager1.10.x.3 {
1565 recursive_select 30 t1
1575 testvfs tv -default 1
1576 faultsim_delete_and_reopen
1577 db func a_string a_string
1578 do_execsql_test pager1-11.1 {
1579 PRAGMA journal_mode = DELETE;
1580 PRAGMA cache_size = 10;
1582 CREATE TABLE zz(top PRIMARY KEY);
1583 INSERT INTO zz VALUES(a_string(222));
1584 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1585 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1586 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1587 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1588 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1591 UPDATE zz SET top = a_string(345);
1594 proc lockout {method args} { return SQLITE_IOERR }
1596 tv filter {xWrite xTruncate xSync}
1597 do_catchsql_test pager1-11.2 { COMMIT } {1 {disk I/O error}}
1600 do_test pager1-11.3 {
1603 PRAGMA journal_mode = TRUNCATE;
1604 PRAGMA integrity_check;
1607 do_test pager1-11.4 {
1609 file exists test.db-journal
1611 do_execsql_test pager1-11.5 { SELECT count(*) FROM zz } {32}
1615 #-------------------------------------------------------------------------
1616 # Test "PRAGMA page_size"
1618 testvfs tv -default 1
1621 512 1024 2048 4096 8192 16384 32768
1623 faultsim_delete_and_reopen
1625 # The sector-size (according to the VFS) is 1024 bytes. So if the
1626 # page-size requested using "PRAGMA page_size" is greater than the
1627 # compile time value of SQLITE_MAX_PAGE_SIZE, then the effective
1628 # page-size remains 1024 bytes.
1631 if {$eff > $::SQLITE_MAX_PAGE_SIZE} { set eff 1024 }
1633 do_test pager1-12.$pagesize.1 {
1636 PRAGMA page_size = $pagesize;
1637 CREATE VIEW v AS SELECT * FROM sqlite_master;
1641 do_test pager1-12.$pagesize.2 {
1644 SELECT count(*) FROM v;
1645 PRAGMA main.page_size;
1648 do_test pager1-12.$pagesize.3 {
1650 SELECT count(*) FROM v;
1651 PRAGMA main.page_size;
1659 #-------------------------------------------------------------------------
1660 # Test specal "PRAGMA journal_mode=PERSIST" test cases.
1662 # pager1-13.1.*: This tests a special case encountered in persistent
1663 # journal mode: If the journal associated with a transaction
1664 # is smaller than the journal file (because a previous
1665 # transaction left a very large non-hot journal file in the
1666 # file-system), then SQLite has to be careful that there is
1667 # not a journal-header left over from a previous transaction
1668 # immediately following the journal content just written.
1669 # If there is, and the process crashes so that the journal
1670 # becomes a hot-journal and must be rolled back by another
1671 # process, there is a danger that the other process may roll
1672 # back the aborted transaction, then continue copying data
1673 # from an older transaction from the remainder of the journal.
1674 # See the syncJournal() function for details.
1676 # pager1-13.2.*: Same test as the previous. This time, throw an index into
1677 # the mix to make the integrity-check more likely to catch
1680 testvfs tv -default 1
1683 proc xSyncCb {method filename args} {
1684 set t [file tail $filename]
1685 if {$t == "test.db"} faultsim_save
1688 faultsim_delete_and_reopen
1689 db func a_string a_string
1691 # The UPDATE statement at the end of this test case creates a really big
1692 # journal. Since the cache-size is only 10 pages, the journal contains
1693 # frequent journal headers.
1695 do_execsql_test pager1-13.1.1 {
1696 PRAGMA page_size = 1024;
1697 PRAGMA journal_mode = PERSIST;
1698 PRAGMA cache_size = 10;
1700 CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
1701 INSERT INTO t1 VALUES(NULL, a_string(400));
1702 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 2 */
1703 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 4 */
1704 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 8 */
1705 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 16 */
1706 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 32 */
1707 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 64 */
1708 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 128 */
1710 UPDATE t1 SET b = a_string(400);
1713 if {$::tcl_platform(platform)!="windows"} {
1714 # Run transactions of increasing sizes. Eventually, one (or more than one)
1715 # of these will write just enough content that one of the old headers created
1716 # by the transaction in the block above lies immediately after the content
1717 # journalled by the current transaction.
1719 for {set nUp 1} {$nUp<64} {incr nUp} {
1720 do_execsql_test pager1-13.1.2.$nUp.1 {
1721 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1723 do_execsql_test pager1-13.1.2.$nUp.2 { PRAGMA integrity_check } {ok}
1725 # Try to access the snapshot of the file-system.
1727 sqlite3 db2 sv_test.db
1728 do_test pager1-13.1.2.$nUp.3 {
1729 execsql { SELECT sum(length(b)) FROM t1 } db2
1730 } [expr {128*400 - ($nUp-1)}]
1731 do_test pager1-13.1.2.$nUp.4 {
1732 execsql { PRAGMA integrity_check } db2
1738 if {$::tcl_platform(platform)!="windows"} {
1739 # Same test as above. But this time with an index on the table.
1741 do_execsql_test pager1-13.2.1 {
1742 CREATE INDEX i1 ON t1(b);
1743 UPDATE t1 SET b = a_string(400);
1745 for {set nUp 1} {$nUp<64} {incr nUp} {
1746 do_execsql_test pager1-13.2.2.$nUp.1 {
1747 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1749 do_execsql_test pager1-13.2.2.$nUp.2 { PRAGMA integrity_check } {ok}
1750 sqlite3 db2 sv_test.db
1751 do_test pager1-13.2.2.$nUp.3 {
1752 execsql { SELECT sum(length(b)) FROM t1 } db2
1753 } [expr {128*400 - ($nUp-1)}]
1754 do_test pager1-13.2.2.$nUp.4 {
1755 execsql { PRAGMA integrity_check } db2
1764 #-------------------------------------------------------------------------
1765 # Test specal "PRAGMA journal_mode=OFF" test cases.
1767 faultsim_delete_and_reopen
1768 do_execsql_test pager1-14.1.1 {
1769 PRAGMA journal_mode = OFF;
1770 CREATE TABLE t1(a, b);
1772 INSERT INTO t1 VALUES(1, 2);
1776 do_catchsql_test pager1-14.1.2 {
1778 INSERT INTO t1 VALUES(3, 4);
1781 do_execsql_test pager1-14.1.3 {
1784 do_catchsql_test pager1-14.1.4 {
1786 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1787 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1788 } {1 {UNIQUE constraint failed: t1.rowid}}
1789 do_execsql_test pager1-14.1.5 {
1794 #-------------------------------------------------------------------------
1795 # Test opening and closing the pager sub-system with different values
1796 # for the sqlite3_vfs.szOsFile variable.
1798 faultsim_delete_and_reopen
1799 do_execsql_test pager1-15.0 {
1800 CREATE TABLE tx(y, z);
1801 INSERT INTO tx VALUES('Ayutthaya', 'Beijing');
1802 INSERT INTO tx VALUES('London', 'Tokyo');
1805 for {set i 0} {$i<513} {incr i 3} {
1806 testvfs tv -default 1 -szosfile $i
1808 do_execsql_test pager1-15.$i.1 {
1810 } {Ayutthaya Beijing London Tokyo}
1815 #-------------------------------------------------------------------------
1816 # Check that it is not possible to open a database file if the full path
1817 # to the associated journal file will be longer than sqlite3_vfs.mxPathname.
1819 testvfs tv -default 1
1822 proc xOpenCb {method filename args} {
1823 set ::file_len [string length $filename]
1829 for {set ii [expr $::file_len-5]} {$ii < [expr $::file_len+20]} {incr ii} {
1830 testvfs tv -default 1 -mxpathname $ii
1832 # The length of the full path to file "test.db-journal" is ($::file_len+8).
1833 # If the configured sqlite3_vfs.mxPathname value greater than or equal to
1834 # this, then the file can be opened. Otherwise, it cannot.
1836 if {$ii >= [expr $::file_len+8]} {
1839 set res {1 {unable to open database file}}
1842 do_test pager1-16.1.$ii {
1843 list [catch { sqlite3 db test.db } msg] $msg
1851 #-------------------------------------------------------------------------
1852 # Test the pagers response to the b-tree layer requesting illegal page
1855 # + The locking page,
1857 # + A page with a page number greater than (2^31-1).
1859 # These tests will not work if SQLITE_DIRECT_OVERFLOW_READ is defined. In
1860 # that case IO errors are sometimes reported instead of SQLITE_CORRUPT.
1862 ifcapable !direct_read {
1863 do_test pager1-18.1 {
1864 faultsim_delete_and_reopen
1865 db func a_string a_string
1867 PRAGMA page_size = 1024;
1868 CREATE TABLE t1(a, b);
1869 INSERT INTO t1 VALUES(a_string(500), a_string(200));
1870 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1871 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1872 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1873 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1874 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1875 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1876 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1879 do_test pager1-18.2 {
1880 set root [db one "SELECT rootpage FROM sqlite_master"]
1881 set lockingpage [expr (0x10000/1024) + 1]
1883 PRAGMA writable_schema = 1;
1884 UPDATE sqlite_master SET rootpage = $lockingpage;
1887 catchsql { SELECT count(*) FROM t1 } db2
1888 } {1 {database disk image is malformed}}
1890 do_test pager1-18.3.1 {
1893 INSERT INTO t2 VALUES(a_string(5000));
1895 set pgno [expr ([file size test.db] / 1024)-2]
1896 hexio_write test.db [expr ($pgno-1)*1024] 00000000
1898 # even though x is malformed, because typeof() does
1899 # not load the content of x, the error is not noticed.
1900 catchsql { SELECT typeof(x) FROM t2 } db2
1902 do_test pager1-18.3.2 {
1903 # in this case, the value of x is loaded and so the error is
1905 catchsql { SELECT length(x||'') FROM t2 } db2
1906 } {1 {database disk image is malformed}}
1908 do_test pager1-18.3.3 {
1911 INSERT INTO t2 VALUES(randomblob(5000));
1913 set pgno [expr ([file size test.db] / 1024)-2]
1914 hexio_write test.db [expr ($pgno-1)*1024] 00000000
1916 # even though x is malformed, because length() and typeof() do
1917 # not load the content of x, the error is not noticed.
1918 catchsql { SELECT length(x), typeof(x) FROM t2 } db2
1920 do_test pager1-18.3.4 {
1921 # in this case, the value of x is loaded and so the error is
1923 catchsql { SELECT length(x||'') FROM t2 } db2
1924 } {1 {database disk image is malformed}}
1926 do_test pager1-18.4 {
1927 hexio_write test.db [expr ($pgno-1)*1024] 90000000
1929 catchsql { SELECT length(x||'') FROM t2 } db2
1930 } {1 {database disk image is malformed}}
1932 do_test pager1-18.5 {
1935 CREATE TABLE t1(a, b);
1936 CREATE TABLE t2(a, b);
1937 PRAGMA writable_schema = 1;
1938 UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1';
1939 PRAGMA writable_schema = 0;
1940 ALTER TABLE t1 RENAME TO x1;
1942 catchsql { SELECT * FROM x1 }
1943 } {1 {database disk image is malformed}}
1946 do_test pager1-18.6 {
1947 faultsim_delete_and_reopen
1948 db func a_string a_string
1950 PRAGMA page_size = 1024;
1952 INSERT INTO t1 VALUES(a_string(800));
1953 INSERT INTO t1 VALUES(a_string(800));
1956 set root [db one "SELECT rootpage FROM sqlite_master"]
1959 hexio_write test.db [expr ($root-1)*1024 + 8] 00000000
1961 catchsql { SELECT length(x) FROM t1 }
1962 } {1 {database disk image is malformed}}
1965 do_test pager1-19.1 {
1967 db func a_string a_string
1969 PRAGMA page_size = 512;
1970 PRAGMA auto_vacuum = 1;
1971 CREATE TABLE t1(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1972 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1973 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1974 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1975 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1976 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1977 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1978 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1979 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1980 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1981 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1982 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1983 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1985 CREATE TABLE t2(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1986 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1987 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1988 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1989 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1990 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1991 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1992 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1993 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1994 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1995 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1996 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1997 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1999 INSERT INTO t1(aa) VALUES( a_string(100000) );
2000 INSERT INTO t2(aa) VALUES( a_string(100000) );
2005 #-------------------------------------------------------------------------
2006 # Test a couple of special cases that come up while committing
2009 # pager1-20.1.*: Committing an in-memory database transaction when the
2010 # database has not been modified at all.
2012 # pager1-20.2.*: As above, but with a normal db in exclusive-locking mode.
2014 # pager1-20.3.*: Committing a transaction in WAL mode where the database has
2015 # been modified, but all dirty pages have been flushed to
2016 # disk before the commit.
2018 do_test pager1-20.1.1 {
2022 CREATE TABLE one(two, three);
2023 INSERT INTO one VALUES('a', 'b');
2026 do_test pager1-20.1.2 {
2033 do_test pager1-20.2.1 {
2034 faultsim_delete_and_reopen
2036 PRAGMA locking_mode = exclusive;
2037 PRAGMA journal_mode = persist;
2038 CREATE TABLE one(two, three);
2039 INSERT INTO one VALUES('a', 'b');
2041 } {exclusive persist}
2042 do_test pager1-20.2.2 {
2050 do_test pager1-20.3.1 {
2051 faultsim_delete_and_reopen
2052 db func a_string a_string
2054 PRAGMA cache_size = 10;
2055 PRAGMA journal_mode = wal;
2059 INSERT INTO t1 VALUES(a_string(800));
2060 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */
2061 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */
2062 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */
2063 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */
2064 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */
2068 do_test pager1-20.3.2 {
2071 INSERT INTO t2 VALUES('xxxx');
2073 recursive_select 32 t1
2078 #-------------------------------------------------------------------------
2079 # Test that a WAL database may not be opened if:
2081 # pager1-21.1.*: The VFS has an iVersion less than 2, or
2082 # pager1-21.2.*: The VFS does not provide xShmXXX() methods.
2085 do_test pager1-21.0 {
2086 faultsim_delete_and_reopen
2088 PRAGMA journal_mode = WAL;
2089 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
2090 INSERT INTO ko DEFAULT VALUES;
2093 do_test pager1-21.1 {
2095 sqlite3 db2 test.db -vfs tv
2096 catchsql { SELECT * FROM ko } db2
2097 } {1 {unable to open database file}}
2100 do_test pager1-21.2 {
2101 testvfs tv -iversion 1
2102 sqlite3 db2 test.db -vfs tv
2103 catchsql { SELECT * FROM ko } db2
2104 } {1 {unable to open database file}}
2109 #-------------------------------------------------------------------------
2110 # Test that a "PRAGMA wal_checkpoint":
2112 # pager1-22.1.*: is a no-op on a non-WAL db, and
2113 # pager1-22.2.*: does not cause xSync calls with a synchronous=off db.
2116 do_test pager1-22.1.1 {
2117 faultsim_delete_and_reopen
2119 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
2120 INSERT INTO ko DEFAULT VALUES;
2122 execsql { PRAGMA wal_checkpoint }
2124 do_test pager1-22.2.1 {
2125 testvfs tv -default 1
2128 proc xSyncCb {args} {incr ::synccount}
2132 PRAGMA synchronous = off;
2133 PRAGMA journal_mode = WAL;
2134 INSERT INTO ko DEFAULT VALUES;
2136 execsql { PRAGMA wal_checkpoint }
2143 #-------------------------------------------------------------------------
2144 # Tests for changing journal mode.
2146 # pager1-23.1.*: Test that when changing from PERSIST to DELETE mode,
2147 # the journal file is deleted.
2149 # pager1-23.2.*: Same test as above, but while a shared lock is held
2150 # on the database file.
2152 # pager1-23.3.*: Same test as above, but while a reserved lock is held
2153 # on the database file.
2155 # pager1-23.4.*: And, for fun, while holding an exclusive lock.
2157 # pager1-23.5.*: Try to set various different journal modes with an
2158 # in-memory database (only MEMORY and OFF should work).
2160 # pager1-23.6.*: Try to set locking_mode=normal on an in-memory database
2161 # (doesn't work - in-memory databases always use
2162 # locking_mode=exclusive).
2164 do_test pager1-23.1.1 {
2165 faultsim_delete_and_reopen
2167 PRAGMA journal_mode = PERSIST;
2168 CREATE TABLE t1(a, b);
2170 file exists test.db-journal
2172 do_test pager1-23.1.2 {
2173 execsql { PRAGMA journal_mode = DELETE }
2174 file exists test.db-journal
2177 do_test pager1-23.2.1 {
2179 PRAGMA journal_mode = PERSIST;
2180 INSERT INTO t1 VALUES('Canberra', 'ACT');
2182 db eval { SELECT * FROM t1 } {
2183 db eval { PRAGMA journal_mode = DELETE }
2185 execsql { PRAGMA journal_mode }
2187 do_test pager1-23.2.2 {
2188 file exists test.db-journal
2191 do_test pager1-23.3.1 {
2193 PRAGMA journal_mode = PERSIST;
2194 INSERT INTO t1 VALUES('Darwin', 'NT');
2197 db eval { PRAGMA journal_mode = DELETE }
2198 execsql { PRAGMA journal_mode }
2200 do_test pager1-23.3.2 {
2201 file exists test.db-journal
2203 do_test pager1-23.3.3 {
2207 do_test pager1-23.4.1 {
2209 PRAGMA journal_mode = PERSIST;
2210 INSERT INTO t1 VALUES('Adelaide', 'SA');
2213 db eval { PRAGMA journal_mode = DELETE }
2214 execsql { PRAGMA journal_mode }
2216 do_test pager1-23.4.2 {
2217 file exists test.db-journal
2219 do_test pager1-23.4.3 {
2223 do_test pager1-23.5.1 {
2224 faultsim_delete_and_reopen
2227 foreach {tn mode possible} {
2235 do_test pager1-23.5.$tn.1 {
2236 execsql "PRAGMA journal_mode = off"
2237 execsql "PRAGMA journal_mode = $mode"
2238 } [if $possible {list $mode} {list off}]
2239 do_test pager1-23.5.$tn.2 {
2240 execsql "PRAGMA journal_mode = memory"
2241 execsql "PRAGMA journal_mode = $mode"
2242 } [if $possible {list $mode} {list memory}]
2244 do_test pager1-23.6.1 {
2245 execsql {PRAGMA locking_mode = normal}
2247 do_test pager1-23.6.2 {
2248 execsql {PRAGMA locking_mode = exclusive}
2250 do_test pager1-23.6.3 {
2251 execsql {PRAGMA locking_mode}
2253 do_test pager1-23.6.4 {
2254 execsql {PRAGMA main.locking_mode}
2257 #-------------------------------------------------------------------------
2259 do_test pager1-24.1.1 {
2260 faultsim_delete_and_reopen
2261 db func a_string a_string
2263 PRAGMA cache_size = 10;
2264 PRAGMA auto_vacuum = FULL;
2265 CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z));
2266 CREATE TABLE x2(x, y, z, PRIMARY KEY(y, z));
2267 INSERT INTO x2 VALUES(a_string(400), a_string(500), a_string(600));
2268 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2269 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2270 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2271 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2272 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2273 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2274 INSERT INTO x1 SELECT * FROM x2;
2277 do_test pager1-24.1.2 {
2280 DELETE FROM x1 WHERE rowid<32;
2282 recursive_select 64 x2
2284 do_test pager1-24.1.3 {
2286 UPDATE x1 SET z = a_string(300) WHERE rowid>40;
2288 PRAGMA integrity_check;
2289 SELECT count(*) FROM x1;
2293 do_test pager1-24.1.4 {
2296 INSERT INTO x1 SELECT * FROM x2;
2298 DELETE FROM x1 WHERE rowid<32;
2299 UPDATE x1 SET z = a_string(299) WHERE rowid>40;
2301 recursive_select 64 x2 {db eval COMMIT}
2303 PRAGMA integrity_check;
2304 SELECT count(*) FROM x1;
2308 do_test pager1-24.1.5 {
2311 INSERT INTO x1 SELECT * FROM x2;
2313 recursive_select 64 x2 { db eval {CREATE TABLE x3(x, y, z)} }
2314 execsql { SELECT * FROM x3 }
2317 #-------------------------------------------------------------------------
2319 do_test pager1-25-1 {
2320 faultsim_delete_and_reopen
2324 CREATE TABLE t1(a, b);
2330 do_test pager1-25-2 {
2331 faultsim_delete_and_reopen
2334 CREATE TABLE t1(a, b);
2341 #-------------------------------------------------------------------------
2342 # Sector-size tests.
2344 do_test pager1-26.1 {
2345 testvfs tv -default 1
2347 faultsim_delete_and_reopen
2348 db func a_string a_string
2350 PRAGMA page_size = 512;
2351 CREATE TABLE tbl(a PRIMARY KEY, b UNIQUE);
2353 INSERT INTO tbl VALUES(a_string(25), a_string(600));
2354 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2355 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2356 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2357 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2358 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2359 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2360 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2364 do_execsql_test pager1-26.1 {
2365 UPDATE tbl SET b = a_string(550);
2370 #-------------------------------------------------------------------------
2372 do_test pager1.27.1 {
2373 faultsim_delete_and_reopen
2374 sqlite3_pager_refcounts db
2377 CREATE TABLE t1(a, b);
2379 sqlite3_pager_refcounts db
2383 #-------------------------------------------------------------------------
2384 # Test that attempting to open a write-transaction with
2385 # locking_mode=exclusive in WAL mode fails if there are other clients on
2386 # the same database.
2390 do_multiclient_test tn {
2391 do_test pager1-28.$tn.1 {
2393 PRAGMA journal_mode = WAL;
2394 CREATE TABLE t1(a, b);
2395 INSERT INTO t1 VALUES('a', 'b');
2398 do_test pager1-28.$tn.2 { sql2 { SELECT * FROM t1 } } {a b}
2400 do_test pager1-28.$tn.3 { sql1 { PRAGMA locking_mode=exclusive } } {exclusive}
2401 do_test pager1-28.$tn.4 {
2402 csql1 { BEGIN; INSERT INTO t1 VALUES('c', 'd'); }
2403 } {1 {database is locked}}
2404 code2 { db2 close ; sqlite3 db2 test.db }
2405 do_test pager1-28.$tn.4 {
2406 sql1 { INSERT INTO t1 VALUES('c', 'd'); COMMIT }
2411 #-------------------------------------------------------------------------
2412 # Normally, when changing from journal_mode=PERSIST to DELETE the pager
2413 # attempts to delete the journal file. However, if it cannot obtain a
2414 # RESERVED lock on the database file, this step is skipped.
2416 do_multiclient_test tn {
2417 do_test pager1-28.$tn.1 {
2419 PRAGMA journal_mode = PERSIST;
2420 CREATE TABLE t1(a, b);
2421 INSERT INTO t1 VALUES('a', 'b');
2424 do_test pager1-28.$tn.2 { file exists test.db-journal } 1
2425 do_test pager1-28.$tn.3 { sql1 { PRAGMA journal_mode = DELETE } } delete
2426 do_test pager1-28.$tn.4 { file exists test.db-journal } 0
2428 do_test pager1-28.$tn.5 {
2430 PRAGMA journal_mode = PERSIST;
2431 INSERT INTO t1 VALUES('c', 'd');
2434 do_test pager1-28.$tn.6 { file exists test.db-journal } 1
2435 do_test pager1-28.$tn.7 {
2436 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2438 do_test pager1-28.$tn.8 { file exists test.db-journal } 1
2439 do_test pager1-28.$tn.9 { sql1 { PRAGMA journal_mode = DELETE } } delete
2440 do_test pager1-28.$tn.10 { file exists test.db-journal } 1
2442 do_test pager1-28.$tn.11 { sql2 COMMIT } {}
2443 do_test pager1-28.$tn.12 { file exists test.db-journal } 0
2445 do_test pager1-28-$tn.13 {
2446 code1 { set channel [db incrblob -readonly t1 a 2] }
2448 PRAGMA journal_mode = PERSIST;
2449 INSERT INTO t1 VALUES('g', 'h');
2452 do_test pager1-28.$tn.14 { file exists test.db-journal } 1
2453 do_test pager1-28.$tn.15 {
2454 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2456 do_test pager1-28.$tn.16 { sql1 { PRAGMA journal_mode = DELETE } } delete
2457 do_test pager1-28.$tn.17 { file exists test.db-journal } 1
2459 do_test pager1-28.$tn.17 { csql2 { COMMIT } } {1 {database is locked}}
2460 do_test pager1-28-$tn.18 { code1 { read $channel } } c
2461 do_test pager1-28-$tn.19 { code1 { close $channel } } {}
2462 do_test pager1-28.$tn.20 { sql2 { COMMIT } } {}
2465 do_test pager1-29.1 {
2466 faultsim_delete_and_reopen
2468 PRAGMA page_size = 1024;
2469 PRAGMA auto_vacuum = full;
2470 PRAGMA locking_mode=exclusive;
2471 CREATE TABLE t1(a, b);
2472 INSERT INTO t1 VALUES(1, 2);
2476 if {[nonzero_reserved_bytes]} {
2477 # VACUUM with size changes is not possible with the codec.
2478 do_test pager1-29.2 {
2480 PRAGMA page_size = 4096;
2483 } {1 {attempt to write a readonly database}}
2485 do_test pager1-29.2 {
2487 PRAGMA page_size = 4096;
2494 #-------------------------------------------------------------------------
2495 # Test that if an empty database file (size 0 bytes) is opened in
2496 # exclusive-locking mode, any journal file is deleted from the file-system
2497 # without being rolled back. And that the RESERVED lock obtained while
2498 # doing this is not released.
2500 do_test pager1-30.1 {
2503 delete_file test.db-journal
2504 set fd [open test.db-journal w]
2505 seek $fd [expr 512+1032*2]
2506 puts -nonewline $fd x
2511 PRAGMA locking_mode=EXCLUSIVE;
2512 SELECT count(*) FROM sqlite_master;
2515 } {exclusive 0 main reserved temp closed}
2517 #-------------------------------------------------------------------------
2518 # Test that if the "page-size" field in a journal-header is 0, the journal
2519 # file can still be rolled back. This is required for backward compatibility -
2520 # versions of SQLite prior to 3.5.8 always set this field to zero.
2522 if {$tcl_platform(platform)=="unix"} {
2523 do_test pager1-31.1 {
2524 faultsim_delete_and_reopen
2526 PRAGMA cache_size = 10;
2527 PRAGMA page_size = 1024;
2528 CREATE TABLE t1(x, y, UNIQUE(x, y));
2529 INSERT INTO t1 VALUES(randomblob(1500), randomblob(1500));
2530 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2531 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2532 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2533 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2534 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2535 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2536 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2537 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2538 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2539 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2541 UPDATE t1 SET y = randomblob(1499);
2543 copy_file test.db test.db2
2544 copy_file test.db-journal test.db2-journal
2546 hexio_write test.db2-journal 24 00000000
2547 sqlite3 db2 test.db2
2548 execsql { PRAGMA integrity_check } db2
2552 #-------------------------------------------------------------------------
2553 # Test that a database file can be "pre-hinted" to a certain size and that
2554 # subsequent spilling of the pager cache does not result in the database
2555 # file being shrunk.
2560 do_test pager1-32.1 {
2563 CREATE TABLE t1(x, y);
2569 INSERT INTO t1 VALUES(1, randomblob(10000));
2571 file_control_chunksize_test db main 1024
2572 file_control_sizehint_test db main 20971520; # 20MB
2574 PRAGMA cache_size = 10;
2575 INSERT INTO t1 VALUES(1, randomblob(10000));
2576 INSERT INTO t1 VALUES(2, randomblob(10000));
2577 INSERT INTO t1 SELECT x+2, randomblob(10000) from t1;
2578 INSERT INTO t1 SELECT x+4, randomblob(10000) from t1;
2579 INSERT INTO t1 SELECT x+8, randomblob(10000) from t1;
2580 INSERT INTO t1 SELECT x+16, randomblob(10000) from t1;
2581 SELECT count(*) FROM t1;
2588 # Cleanup 20MB file left by the previous test.
2591 #-------------------------------------------------------------------------
2592 # Test that if a transaction is committed in journal_mode=DELETE mode,
2593 # and the call to unlink() returns an ENOENT error, the COMMIT does not
2596 if {$::tcl_platform(platform)=="unix"} {
2597 do_test pager1-33.1 {
2601 INSERT INTO t1 VALUES('one');
2602 INSERT INTO t1 VALUES('two');
2604 INSERT INTO t1 VALUES('three');
2605 INSERT INTO t1 VALUES('four');
2607 forcedelete bak-journal
2608 file rename test.db-journal bak-journal
2611 } {1 {disk I/O error}}
2613 do_test pager1-33.2 {
2614 file rename bak-journal test.db-journal
2615 execsql { SELECT * FROM t1 }
2619 #-------------------------------------------------------------------------
2620 # Test that appending pages to the database file then moving those pages
2621 # to the free-list before the transaction is committed does not cause
2624 foreach {tn pragma strsize} {
2625 1 { PRAGMA mmap_size = 0 } 2400
2627 3 { PRAGMA mmap_size = 0 } 4400
2631 db func a_string a_string
2633 do_execsql_test 34.$tn.1 {
2634 CREATE TABLE t1(a, b);
2635 INSERT INTO t1 VALUES(1, 2);
2637 do_execsql_test 34.$tn.2 {
2639 INSERT INTO t1 VALUES(2, a_string($strsize));
2640 DELETE FROM t1 WHERE oid=2;
2642 PRAGMA integrity_check;
2646 #-------------------------------------------------------------------------
2653 CREATE TABLE t1(x, y);
2654 PRAGMA journal_mode = WAL;
2655 INSERT INTO t1 VALUES(1, 2);
2660 CREATE TABLE t2(a, b);
2663 hexio_write test.db-shm [expr 16*1024] [string repeat 0055 8192]
2667 do_multiclient_test tn {
2669 PRAGMA auto_vacuum = 0;
2670 CREATE TABLE t1(x, y);
2671 INSERT INTO t1 VALUES(1, 2);
2675 sql2 { PRAGMA max_page_count = 2 }
2676 list [catch { sql2 { CREATE TABLE t2(x) } } msg] $msg
2677 } {1 {database or disk is full}}
2679 sql1 { PRAGMA checkpoint_fullfsync = 1 }
2680 sql1 { CREATE TABLE t2(x) }
2683 sql2 { INSERT INTO t2 VALUES('xyz') }
2684 list [catch { sql2 { CREATE TABLE t3(x) } } msg] $msg
2685 } {1 {database or disk is full}}
2688 forcedelete test1 test2
2690 1 {file:?mode=memory&cache=shared}
2691 2 {file:one?mode=memory&cache=shared}
2692 3 {file:test1?cache=shared}
2693 4 {file:test2?another=parameter&yet=anotherone}
2698 sqlite3_config_uri 1
2703 INSERT INTO t1 VALUES(1);
2708 do_execsql_test 37.$tn.2 {
2715 sqlite3_config_uri 0
2721 set fd [open test.db w]
2722 puts $fd "hello world"
2725 catchsql { CREATE TABLE t1(x) }
2726 } {1 {file is not a database}}
2735 PRAGMA auto_vacuum = 1;
2737 INSERT INTO t1 VALUES('xxx');
2738 INSERT INTO t1 VALUES('two');
2739 INSERT INTO t1 VALUES(randomblob(400));
2740 INSERT INTO t1 VALUES(randomblob(400));
2741 INSERT INTO t1 VALUES(randomblob(400));
2742 INSERT INTO t1 VALUES(randomblob(400));
2744 UPDATE t1 SET x = 'one' WHERE rowid=1;
2746 set ::stmt [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
2747 sqlite3_step $::stmt
2748 sqlite3_column_text $::stmt 0
2751 execsql { CREATE TABLE t2(x) }
2752 sqlite3_step $::stmt
2753 sqlite3_column_text $::stmt 0
2756 sqlite3_finalize $::stmt
2760 do_execsql_test 39.4 {
2761 PRAGMA auto_vacuum = 2;
2773 PRAGMA cache_size = 1;
2774 PRAGMA incremental_vacuum;
2775 PRAGMA integrity_check;
2782 PRAGMA auto_vacuum = 1;
2783 CREATE TABLE t1(x PRIMARY KEY);
2784 INSERT INTO t1 VALUES(randomblob(1200));
2790 INSERT INTO t1 VALUES(randomblob(1200));
2791 INSERT INTO t1 VALUES(randomblob(1200));
2792 INSERT INTO t1 VALUES(randomblob(1200));
2799 PRAGMA cache_size = 1;
2801 PRAGMA integrity_check;
2808 CREATE TABLE t1(x PRIMARY KEY);
2809 INSERT INTO t1 VALUES(randomblob(200));
2810 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2811 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2812 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2813 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2814 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2815 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2819 testvfs tv -default 1
2820 tv sectorsize 16384;
2825 PRAGMA cache_size = 1;
2826 DELETE FROM t1 WHERE rowid%4;
2827 PRAGMA integrity_check;
2833 set pending_prev [sqlite3_test_control_pending_byte 0x1000000]
2837 CREATE TABLE t1(x, y);
2838 INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
2839 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2840 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2841 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2842 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2843 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2844 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2845 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2846 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2847 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2850 sqlite3_test_control_pending_byte 0x0010000
2852 db eval { PRAGMA mmap_size = 0 }
2853 catchsql { SELECT sum(length(y)) FROM t1 }
2854 } {1 {database disk image is malformed}}
2858 CREATE TABLE t1(x, y);
2859 INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
2860 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2861 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2862 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2863 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2864 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2865 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2866 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2867 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2871 testvfs tv -default 1
2872 tv sectorsize 16384;
2874 sqlite3 db test.db -vfs tv
2875 execsql { UPDATE t1 SET x = randomblob(200) }
2879 sqlite3_test_control_pending_byte $pending_prev
2884 CREATE TABLE t1(x, y);
2885 INSERT INTO t1 VALUES(1, 2);
2886 CREATE TABLE t2(x, y);
2887 INSERT INTO t2 VALUES(1, 2);
2888 CREATE TABLE t3(x, y);
2889 INSERT INTO t3 VALUES(1, 2);
2894 db eval { PRAGMA mmap_size = 0 }
2895 db eval { SELECT * FROM t1 }
2896 sqlite3_db_status db CACHE_MISS 0
2900 db eval { SELECT * FROM t2 }
2901 sqlite3_db_status db CACHE_MISS 1
2905 db eval { SELECT * FROM t3 }
2906 sqlite3_db_status db CACHE_MISS 0