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 # Do not use a codec for tests in this file, as the database file is
21 # manipulated directly using tcl scripts (using the [hexio_write] command).
26 # pager1-1.*: Test inter-process locking (clients in multiple processes).
28 # pager1-2.*: Test intra-process locking (multiple clients in this process).
30 # pager1-3.*: Savepoint related tests.
32 # pager1-4.*: Hot-journal related tests.
34 # pager1-5.*: Cases related to multi-file commits.
36 # pager1-6.*: Cases related to "PRAGMA max_page_count"
38 # pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
40 # pager1-8.*: Cases using temporary and in-memory databases.
42 # pager1-9.*: Tests related to the backup API.
44 # pager1-10.*: Test that the assumed file-system sector-size is limited to
47 # pager1-12.*: Tests involving "PRAGMA page_size"
49 # pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST"
51 # pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF"
53 # pager1-15.*: Varying sqlite3_vfs.szOsFile
55 # pager1-16.*: Varying sqlite3_vfs.mxPathname
57 # pager1-17.*: Tests related to "PRAGMA omit_readlock"
58 # (The omit_readlock pragma has been removed and so have
61 # pager1-18.*: Test that the pager layer responds correctly if the b-tree
62 # requests an invalid page number (due to db corruption).
65 proc recursive_select {id table {script {}}} {
67 db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" {
68 recursive_select $rowid $table $script
71 if {$cnt==0} { eval $script }
74 set a_string_counter 1
76 global a_string_counter
78 string range [string repeat "${a_string_counter}." $n] 1 $n
80 db func a_string a_string
82 do_multiclient_test tn {
84 # Create and populate a database table using connection [db]. Check
85 # that connections [db2] and [db3] can see the schema and content.
87 do_test pager1-$tn.1 {
89 CREATE TABLE t1(a PRIMARY KEY, b);
90 CREATE INDEX i1 ON t1(b);
91 INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two');
94 do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
95 do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two}
97 # Open a transaction and add a row using [db]. This puts [db] in
98 # RESERVED state. Check that connections [db2] and [db3] can still
99 # read the database content as it was before the transaction was
100 # opened. [db] should see the inserted row.
102 do_test pager1-$tn.4 {
105 INSERT INTO t1 VALUES(3, 'three');
108 do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
109 do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
111 # [db] still has an open write transaction. Check that this prevents
112 # other connections (specifically [db2]) from writing to the database.
114 # Even if [db2] opens a transaction first, it may not write to the
115 # database. After the attempt to write the db within a transaction,
116 # [db2] is left with an open transaction, but not a read-lock on
117 # the main database. So it does not prevent [db] from committing.
119 do_test pager1-$tn.8 {
120 csql2 { UPDATE t1 SET a = a + 10 }
121 } {1 {database is locked}}
122 do_test pager1-$tn.9 {
125 UPDATE t1 SET a = a + 10;
127 } {1 {database is locked}}
129 # Have [db] commit its transactions. Check the other connections can
130 # now see the new database content.
132 do_test pager1-$tn.10 { sql1 { COMMIT } } {}
133 do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
134 do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
135 do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
137 # Check that, as noted above, [db2] really did keep an open transaction
138 # after the attempt to write the database failed.
140 do_test pager1-$tn.14 {
142 } {1 {cannot start a transaction within a transaction}}
143 do_test pager1-$tn.15 { sql2 { ROLLBACK } } {}
145 # Have [db2] open a transaction and take a read-lock on the database.
146 # Check that this prevents [db] from writing to the database (outside
147 # of any transaction). After this fails, check that [db3] can read
148 # the db (showing that [db] did not take a PENDING lock etc.)
150 do_test pager1-$tn.15 {
151 sql2 { BEGIN; SELECT * FROM t1; }
152 } {1 one 2 two 3 three}
153 do_test pager1-$tn.16 {
154 csql1 { UPDATE t1 SET a = a + 10 }
155 } {1 {database is locked}}
156 do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
158 # This time, have [db] open a transaction before writing the database.
159 # This works - [db] gets a RESERVED lock which does not conflict with
160 # the SHARED lock [db2] is holding.
162 do_test pager1-$tn.18 {
165 UPDATE t1 SET a = a + 10;
168 do_test pager1-$tn-19 {
169 sql1 { PRAGMA lock_status }
170 } {main reserved temp closed}
171 do_test pager1-$tn-20 {
172 sql2 { PRAGMA lock_status }
173 } {main shared temp closed}
175 # Check that all connections can still read the database. Only [db] sees
176 # the updated content (as the transaction has not been committed yet).
178 do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
179 do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
180 do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
182 # Because [db2] still has the SHARED lock, [db] is unable to commit the
183 # transaction. If it tries, an error is returned and the connection
184 # upgrades to a PENDING lock.
186 # Once this happens, [db] can read the database and see the new content,
187 # [db2] (still holding SHARED) can still read the old content, but [db3]
188 # (not holding any lock) is prevented by [db]'s PENDING from reading
191 do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}}
192 do_test pager1-$tn-25 {
193 sql1 { PRAGMA lock_status }
194 } {main pending temp closed}
195 do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
196 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
197 do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
199 # Have [db2] commit its read transaction, releasing the SHARED lock it
200 # is holding. Now, neither [db2] nor [db3] may read the database (as [db]
201 # is still holding a PENDING).
203 do_test pager1-$tn.29 { sql2 { COMMIT } } {}
204 do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
205 do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
207 # [db] is now able to commit the transaction. Once the transaction is
208 # committed, all three connections can read the new content.
210 do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {}
211 do_test pager1-$tn.26 { sql1 { COMMIT } } {}
212 do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three}
213 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three}
214 do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three}
216 # Install a busy-handler for connection [db].
221 if {$n>5} { sql2 COMMIT }
226 do_test pager1-$tn.29 {
227 sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') }
229 do_test pager1-$tn.30 {
230 sql2 { BEGIN ; SELECT * FROM t1 }
231 } {21 one 22 two 23 three}
232 do_test pager1-$tn.31 { sql1 COMMIT } {}
233 do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6}
236 #-------------------------------------------------------------------------
237 # Savepoint related test cases.
239 # pager1-3.1.2.*: Force a savepoint rollback to cause the database file
242 # pager1-3.1.3.*: Use a journal created in synchronous=off mode as part
243 # of a savepoint rollback.
245 do_test pager1-3.1.1 {
246 faultsim_delete_and_reopen
248 CREATE TABLE t1(a PRIMARY KEY, b);
249 CREATE TABLE counter(
253 INSERT INTO counter VALUES(0, 0);
254 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
255 UPDATE counter SET i = i+1;
257 CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN
258 UPDATE counter SET u = u+1;
261 execsql { SELECT * FROM counter }
264 do_execsql_test pager1-3.1.2 {
265 PRAGMA cache_size = 10;
267 INSERT INTO t1 VALUES(1, randomblob(1500));
268 INSERT INTO t1 VALUES(2, randomblob(1500));
269 INSERT INTO t1 VALUES(3, randomblob(1500));
270 SELECT * FROM counter;
272 do_catchsql_test pager1-3.1.3 {
273 INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1
274 } {1 {CHECK constraint failed: counter}}
275 do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
276 do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
277 do_execsql_test pager1-3.6 { COMMIT } {}
279 foreach {tn sql tcl} {
280 7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } {
281 testvfs tv -default 1
282 tv devchar safe_append
284 8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } {
285 testvfs tv -default 1
286 tv devchar sequential
288 9 { PRAGMA synchronous = FULL } { }
289 10 { PRAGMA synchronous = NORMAL } { }
290 11 { PRAGMA synchronous = OFF } { }
291 12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { }
292 13 { PRAGMA synchronous = FULL } {
293 testvfs tv -default 1
294 tv devchar sequential
296 14 { PRAGMA locking_mode = EXCLUSIVE } {
299 do_test pager1-3.$tn.1 {
301 faultsim_delete_and_reopen
302 db func a_string a_string
305 PRAGMA auto_vacuum = 2;
306 PRAGMA cache_size = 10;
307 CREATE TABLE z(x INTEGER PRIMARY KEY, y);
309 INSERT INTO z VALUES(NULL, a_string(800));
310 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2
311 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4
312 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8
313 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16
314 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32
315 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64
316 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128
317 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256
320 execsql { PRAGMA auto_vacuum }
322 do_execsql_test pager1-3.$tn.2 {
324 INSERT INTO z VALUES(NULL, a_string(800));
325 INSERT INTO z VALUES(NULL, a_string(800));
327 UPDATE z SET y = NULL WHERE x>256;
328 PRAGMA incremental_vacuum;
329 SELECT count(*) FROM z WHERE x < 100;
334 do_execsql_test pager1-3.$tn.3 {
337 UPDATE z SET y = y||x;
340 SELECT count(*) FROM z;
343 do_execsql_test pager1-3.$tn.4 {
345 UPDATE z SET y = y||x;
348 do_execsql_test pager1-3.$tn.5 {
349 SELECT count(*) FROM z;
351 PRAGMA integrity_check;
354 do_execsql_test pager1-3.$tn.6 {
363 #-------------------------------------------------------------------------
364 # Hot journal rollback related test cases.
366 # pager1.4.1.*: Test that the pager module deletes very small invalid
369 # pager1.4.2.*: Test that if the master journal pointer at the end of a
370 # hot-journal file appears to be corrupt (checksum does not
371 # compute) the associated journal is rolled back (and no
372 # xAccess() call to check for the presence of any master
373 # journal file is made).
375 # pager1.4.3.*: Test that the contents of a hot-journal are ignored if the
376 # page-size or sector-size in the journal header appear to
377 # be invalid (too large, too small or not a power of 2).
379 # pager1.4.4.*: Test hot-journal rollback of journal file with a master
380 # journal pointer generated in various "PRAGMA synchronous"
383 # pager1.4.5.*: Test that hot-journal rollback stops if it encounters a
384 # journal-record for which the checksum fails.
386 # pager1.4.6.*: Test that when rolling back a hot-journal that contains a
387 # master journal pointer, the master journal file is deleted
388 # after all the hot-journals that refer to it are deleted.
390 # pager1.4.7.*: Test that if a hot-journal file exists but a client can
391 # open it for reading only, the database cannot be accessed and
392 # SQLITE_CANTOPEN is returned.
394 do_test pager1.4.1.1 {
395 faultsim_delete_and_reopen
397 CREATE TABLE x(y, z);
398 INSERT INTO x VALUES(1, 2);
400 set fd [open test.db-journal w]
401 puts -nonewline $fd "helloworld"
403 file exists test.db-journal
405 do_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2}
406 do_test pager1.4.1.3 { file exists test.db-journal } {0}
408 # Set up a [testvfs] to snapshot the file-system just before SQLite
409 # deletes the master-journal to commit a multi-file transaction.
411 # In subsequent test cases, invoking [faultsim_restore_and_reopen] sets
412 # up the file system to contain two databases, two hot-journal files and
415 do_test pager1.4.2.1 {
416 testvfs tstvfs -default 1
417 tstvfs filter xDelete
418 tstvfs script xDeleteCallback
419 proc xDeleteCallback {method file args} {
420 set file [file tail $file]
421 if { [string match *mj* $file] } { faultsim_save }
423 faultsim_delete_and_reopen
424 db func a_string a_string
426 ATTACH 'test.db2' AS aux;
427 PRAGMA journal_mode = DELETE;
428 PRAGMA main.cache_size = 10;
429 PRAGMA aux.cache_size = 10;
430 CREATE TABLE t1(a UNIQUE, b UNIQUE);
431 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
432 INSERT INTO t1 VALUES(a_string(200), a_string(300));
433 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
434 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
435 INSERT INTO t2 SELECT * FROM t1;
437 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
438 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
439 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
440 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
441 REPLACE INTO t2 SELECT * FROM t1;
448 if {$::tcl_platform(platform)!="windows"} {
449 do_test pager1.4.2.2 {
450 faultsim_restore_and_reopen
452 SELECT count(*) FROM t1;
453 PRAGMA integrity_check;
456 do_test pager1.4.2.3 {
457 faultsim_restore_and_reopen
458 foreach f [glob test.db-mj*] { forcedelete $f }
460 SELECT count(*) FROM t1;
461 PRAGMA integrity_check;
464 do_test pager1.4.2.4 {
465 faultsim_restore_and_reopen
466 hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
468 SELECT count(*) FROM t1;
469 PRAGMA integrity_check;
472 do_test pager1.4.2.5 {
473 faultsim_restore_and_reopen
474 hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
475 foreach f [glob test.db-mj*] { forcedelete $f }
477 SELECT count(*) FROM t1;
478 PRAGMA integrity_check;
483 do_test pager1.4.3.1 {
484 testvfs tstvfs -default 1
486 tstvfs script xSyncCallback
487 proc xSyncCallback {method file args} {
488 set file [file tail $file]
489 if { 0==[string match *journal $file] } { faultsim_save }
491 faultsim_delete_and_reopen
493 PRAGMA journal_mode = DELETE;
494 CREATE TABLE t1(a, b);
495 INSERT INTO t1 VALUES(1, 2);
496 INSERT INTO t1 VALUES(3, 4);
502 foreach {tn ofst value result} {
507 6 20 131072 {1 2 3 4}
511 9 24 131072 {1 2 3 4}
515 do_test pager1.4.3.$tn {
516 faultsim_restore_and_reopen
517 hexio_write test.db-journal $ofst [format %.8x $value]
518 execsql { SELECT * FROM t1 }
523 # Set up a VFS that snapshots the file-system just before a master journal
524 # file is deleted to commit a multi-file transaction. Specifically, the
525 # file-system is saved just before the xDelete() call to remove the
526 # master journal file from the file-system.
529 testvfs tv -default 1
530 tv script copy_on_mj_delete
531 set ::mj_filename_length 0
532 proc copy_on_mj_delete {method filename args} {
533 if {[string match *mj* [file tail $filename]]} {
535 # NOTE: Is the file name relative? If so, add the length of the current
538 if {[is_relative_file $filename]} {
539 set ::mj_filename_length \
540 [expr {[string length $filename] + [string length $::pwd]}]
542 set ::mj_filename_length [string length $filename]
550 1 { set prefix "test.db" }
552 # This test depends on the underlying VFS being able to open paths
553 # 512 bytes in length. The idea is to create a hot-journal file that
554 # contains a master-journal pointer so large that it could contain
555 # a valid page record (if the file page-size is 512 bytes). So as to
556 # make sure SQLite doesn't get confused by this.
558 set nPadding [expr 511 - $::mj_filename_length]
559 if {$tcl_platform(platform)=="windows"} {
560 # TBD need to figure out how to do this correctly for Windows!!!
561 set nPadding [expr 255 - $::mj_filename_length]
564 # We cannot just create a really long database file name to open, as
565 # Linux limits a single component of a path to 255 bytes by default
566 # (and presumably other systems have limits too). So create a directory
567 # hierarchy to work in.
569 set dirname "d123456789012345678901234567890/"
570 set nDir [expr $nPadding / 32]
572 set p [string repeat $dirname $nDir]
577 set padding [string repeat x [expr $nPadding %32]]
578 set prefix "test.db${padding}"
584 PRAGMA main.synchronous=OFF;
585 PRAGMA aux.synchronous=OFF;
586 PRAGMA journal_mode = DELETE;
589 PRAGMA main.synchronous=OFF;
590 PRAGMA aux.synchronous=OFF;
591 PRAGMA main.page_size = 512;
592 PRAGMA aux.page_size = 512;
593 PRAGMA journal_mode = DELETE;
596 PRAGMA main.synchronous=NORMAL;
597 PRAGMA aux.synchronous=NORMAL;
598 PRAGMA journal_mode = DELETE;
601 PRAGMA main.synchronous=FULL;
602 PRAGMA aux.synchronous=FULL;
603 PRAGMA journal_mode = DELETE;
607 set tn "${tn1}.${tn2}"
609 # Set up a connection to have two databases, test.db (main) and
610 # test.db2 (aux). Then run a multi-file transaction on them. The
611 # VFS will snapshot the file-system just before the master-journal
612 # file is deleted to commit the transaction.
615 do_test pager1-4.4.$tn.1 {
616 faultsim_delete_and_reopen $prefix
618 ATTACH '${prefix}2' AS aux;
621 CREATE TABLE aux.b(x);
622 INSERT INTO a VALUES('double-you');
623 INSERT INTO a VALUES('why');
624 INSERT INTO a VALUES('zed');
625 INSERT INTO b VALUES('won');
626 INSERT INTO b VALUES('too');
627 INSERT INTO b VALUES('free');
631 INSERT INTO a SELECT * FROM b WHERE rowid<=3;
632 INSERT INTO b SELECT * FROM a WHERE rowid<=3;
638 # Check that the transaction was committed successfully.
640 do_execsql_test pager1-4.4.$tn.2 {
642 } {double-you why zed won too free}
643 do_execsql_test pager1-4.4.$tn.3 {
645 } {won too free double-you why zed}
647 # Restore the file-system and reopen the databases. Check that it now
648 # appears that the transaction was not committed (because the file-system
649 # was restored to the state where it had not been).
651 do_test pager1-4.4.$tn.4 {
652 faultsim_restore_and_reopen $prefix
653 execsql "ATTACH '${prefix}2' AS aux"
655 do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed}
656 do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free}
658 # Restore the file-system again. This time, before reopening the databases,
659 # delete the master-journal file from the file-system. It now appears that
660 # the transaction was committed (no master-journal file == no rollback).
662 do_test pager1-4.4.$tn.7 {
663 faultsim_restore_and_reopen $prefix
664 foreach f [glob ${prefix}-mj*] { forcedelete $f }
665 execsql "ATTACH '${prefix}2' AS aux"
667 do_execsql_test pager1-4.4.$tn.8 {
669 } {double-you why zed won too free}
670 do_execsql_test pager1-4.4.$tn.9 {
672 } {won too free double-you why zed}
682 # Set up a VFS to make a copy of the file-system just before deleting a
683 # journal file to commit a transaction. The transaction modifies exactly
684 # two database pages (and page 1 - the change counter).
686 testvfs tv -default 1
688 tv script copy_on_journal_delete
690 proc copy_on_journal_delete {method filename args} {
691 if {[string match *journal $filename]} faultsim_save
694 faultsim_delete_and_reopen
695 do_execsql_test pager1.4.5.1 {
696 PRAGMA journal_mode = DELETE;
697 PRAGMA page_size = 1024;
698 CREATE TABLE t1(a, b);
699 CREATE TABLE t2(a, b);
700 INSERT INTO t1 VALUES('I', 'II');
701 INSERT INTO t2 VALUES('III', 'IV');
703 INSERT INTO t1 VALUES(1, 2);
704 INSERT INTO t2 VALUES(3, 4);
709 # Check the transaction was committed:
711 do_execsql_test pager1.4.5.2 {
714 } {I II 1 2 III IV 3 4}
716 # Now try four tests:
718 # pager1-4.5.3: Restore the file-system. Check that the whole transaction
721 # pager1-4.5.4: Restore the file-system. Corrupt the first record in the
722 # journal. Check the transaction is not rolled back.
724 # pager1-4.5.5: Restore the file-system. Corrupt the second record in the
725 # journal. Check that the first record in the transaction is
726 # played back, but not the second.
728 # pager1-4.5.6: Restore the file-system. Try to open the database with a
729 # readonly connection. This should fail, as a read-only
730 # connection cannot roll back the database file.
732 faultsim_restore_and_reopen
733 do_execsql_test pager1.4.5.3 {
737 faultsim_restore_and_reopen
738 hexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF
739 do_execsql_test pager1.4.5.4 {
742 } {I II 1 2 III IV 3 4}
743 faultsim_restore_and_reopen
744 hexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF
745 do_execsql_test pager1.4.5.5 {
750 faultsim_restore_and_reopen
752 sqlite3 db test.db -readonly 1
753 do_catchsql_test pager1.4.5.6 {
756 } {1 {attempt to write a readonly database}}
759 # Snapshot the file-system just before multi-file commit. Save the name
760 # of the master journal file in $::mj_filename.
762 tv script copy_on_mj_delete
764 proc copy_on_mj_delete {method filename args} {
765 if {[string match *mj* [file tail $filename]]} {
766 set ::mj_filename $filename
771 do_test pager1.4.6.1 {
772 faultsim_delete_and_reopen
774 PRAGMA journal_mode = DELETE;
775 ATTACH 'test.db2' AS two;
776 CREATE TABLE t1(a, b);
777 CREATE TABLE two.t2(a, b);
778 INSERT INTO t1 VALUES(1, 't1.1');
779 INSERT INTO t2 VALUES(1, 't2.1');
781 UPDATE t1 SET b = 't1.2';
782 UPDATE t2 SET b = 't2.2';
789 faultsim_restore_and_reopen
790 do_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1}
791 do_test pager1.4.6.3 { file exists $::mj_filename } {1}
792 do_execsql_test pager1.4.6.4 {
793 ATTACH 'test.db2' AS two;
796 do_test pager1.4.6.5 { file exists $::mj_filename } {0}
798 faultsim_restore_and_reopen
800 do_test pager1.4.6.8 {
801 set ::mj_filename1 $::mj_filename
805 PRAGMA journal_mode = DELETE;
806 ATTACH 'test.db3' AS three;
807 CREATE TABLE three.t3(a, b);
808 INSERT INTO t3 VALUES(1, 't3.1');
810 UPDATE t2 SET b = 't2.3';
811 UPDATE t3 SET b = 't3.3';
814 expr {$::mj_filename1 != $::mj_filename}
816 faultsim_restore_and_reopen
819 # The file-system now contains:
822 # * three hot-journal files
823 # * two master-journal files.
825 # The hot-journals associated with test.db2 and test.db3 point to
826 # master journal $::mj_filename. The hot-journal file associated with
827 # test.db points to master journal $::mj_filename1. So reading from
828 # test.db should delete $::mj_filename1.
830 do_test pager1.4.6.9 {
831 lsort [glob test.db*]
833 test.db test.db2 test.db3 \
834 test.db-journal test.db2-journal test.db3-journal \
835 [file tail $::mj_filename] [file tail $::mj_filename1]
838 # The master-journal $::mj_filename1 contains pointers to test.db and
839 # test.db2. However the hot-journal associated with test.db2 points to
840 # a different master-journal. Therefore, reading from test.db only should
841 # be enough to cause SQLite to delete $::mj_filename1.
843 do_test pager1.4.6.10 { file exists $::mj_filename } {1}
844 do_test pager1.4.6.11 { file exists $::mj_filename1 } {1}
845 do_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1}
846 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
847 do_test pager1.4.6.14 { file exists $::mj_filename1 } {0}
849 do_execsql_test pager1.4.6.12 {
850 ATTACH 'test.db2' AS two;
853 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
854 do_execsql_test pager1.4.6.14 {
855 ATTACH 'test.db3' AS three;
858 do_test pager1.4.6.15 { file exists $::mj_filename } {0}
863 testvfs tv -default 1
865 tv script copy_on_journal_delete
867 proc copy_on_journal_delete {method filename args} {
868 if {[string match *journal $filename]} faultsim_save
871 faultsim_delete_and_reopen
872 do_execsql_test pager1.4.7.1 {
873 PRAGMA journal_mode = DELETE;
874 CREATE TABLE t1(x PRIMARY KEY, y);
875 CREATE INDEX i1 ON t1(y);
876 INSERT INTO t1 VALUES('I', 'one');
877 INSERT INTO t1 VALUES('II', 'four');
878 INSERT INTO t1 VALUES('III', 'nine');
880 INSERT INTO t1 VALUES('IV', 'sixteen');
881 INSERT INTO t1 VALUES('V' , 'twentyfive');
888 test_syscall install fchmod
889 test_syscall fault 1 1
891 do_test pager1.4.7.2 {
892 faultsim_restore_and_reopen
893 catch {file attributes test.db-journal -permissions r--------}
894 catch {file attributes test.db-journal -readonly 1}
895 catchsql { SELECT * FROM t1 }
896 } {1 {unable to open database file}}
899 test_syscall fault 0 0
901 do_test pager1.4.7.3 {
903 catch {file attributes test.db-journal -permissions rw-rw-rw-}
904 catch {file attributes test.db-journal -readonly 0}
905 delete_file test.db-journal
906 file exists test.db-journal
908 do_test pager1.4.8.1 {
909 catch {file attributes test.db -permissions r--------}
910 catch {file attributes test.db -readonly 1}
912 db eval { SELECT * FROM t1 }
913 sqlite3_db_readonly db main
915 do_test pager1.4.8.2 {
916 sqlite3_db_readonly db xyz
918 do_test pager1.4.8.3 {
920 catch {file attributes test.db -readonly 0}
921 catch {file attributes test.db -permissions rw-rw-rw-} msg
923 db eval { SELECT * FROM t1 }
924 sqlite3_db_readonly db main
927 #-------------------------------------------------------------------------
928 # The following tests deal with multi-file commits.
930 # pager1-5.1.*: The case where a multi-file cannot be committed because
931 # another connection is holding a SHARED lock on one of the
932 # files. After the SHARED lock is removed, the COMMIT succeeds.
934 # pager1-5.2.*: Multi-file commits with journal_mode=memory.
936 # pager1-5.3.*: Multi-file commits with journal_mode=memory.
938 # pager1-5.4.*: Check that with synchronous=normal, the master-journal file
939 # name is added to a journal file immediately after the last
940 # journal record. But with synchronous=full, extra unused space
941 # is allocated between the last journal record and the
942 # master-journal file name so that the master-journal file
943 # name does not lie on the same sector as the last journal file
946 # pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is
947 # truncated to zero bytes when a multi-file transaction is
948 # committed (instead of the first couple of bytes being zeroed).
951 do_test pager1-5.1.1 {
952 faultsim_delete_and_reopen
954 ATTACH 'test.db2' AS aux;
955 CREATE TABLE t1(a, b);
956 CREATE TABLE aux.t2(a, b);
957 INSERT INTO t1 VALUES(17, 'Lenin');
958 INSERT INTO t1 VALUES(22, 'Stalin');
959 INSERT INTO t1 VALUES(53, 'Khrushchev');
962 do_test pager1-5.1.2 {
965 INSERT INTO t1 VALUES(64, 'Brezhnev');
966 INSERT INTO t2 SELECT * FROM t1;
974 do_test pager1-5.1.3 {
976 } {1 {database is locked}}
977 do_test pager1-5.1.4 {
980 execsql { SELECT * FROM t2 } db2
981 } {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev}
982 do_test pager1-5.1.5 {
986 do_test pager1-5.2.1 {
988 PRAGMA journal_mode = memory;
990 INSERT INTO t1 VALUES(84, 'Andropov');
991 INSERT INTO t2 VALUES(84, 'Andropov');
995 do_test pager1-5.3.1 {
997 PRAGMA journal_mode = off;
999 INSERT INTO t1 VALUES(85, 'Gorbachev');
1000 INSERT INTO t2 VALUES(85, 'Gorbachev');
1005 do_test pager1-5.4.1 {
1008 sqlite3 db test.db -vfs tv
1009 execsql { ATTACH 'test.db2' AS aux }
1012 tv script max_journal_size
1015 proc max_journal_size {method args} {
1017 catch { set sz [file size test.db-journal] }
1018 if {$sz > $::max_journal} {
1019 set ::max_journal $sz
1024 PRAGMA journal_mode = DELETE;
1025 PRAGMA synchronous = NORMAL;
1027 INSERT INTO t1 VALUES(85, 'Gorbachev');
1028 INSERT INTO t2 VALUES(85, 'Gorbachev');
1032 # The size of the journal file is now:
1034 # 1) 512 byte header +
1035 # 2) 2 * (1024+8) byte records +
1036 # 3) 20+N bytes of master-journal pointer, where N is the size of
1037 # the master-journal name encoded as utf-8 with no nul term.
1039 set mj_pointer [expr {
1040 20 + [string length "test.db-mjXXXXXX9XX"]
1043 # NOTE: For item 3 above, if the current SQLite VFS lacks the concept of a
1044 # current directory, the length of the current directory name plus 1
1045 # character for the directory separator character are NOT counted as
1046 # part of the total size; otherwise, they are.
1049 set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
1051 expr {$::max_journal==(512+2*(1024+8)+$mj_pointer)}
1053 do_test pager1-5.4.2 {
1056 PRAGMA synchronous = full;
1058 DELETE FROM t1 WHERE b = 'Lenin';
1059 DELETE FROM t2 WHERE b = 'Lenin';
1063 # In synchronous=full mode, the master-journal pointer is not written
1064 # directly after the last record in the journal file. Instead, it is
1065 # written starting at the next (in this case 512 byte) sector boundary.
1067 set mj_pointer [expr {
1068 20 + [string length "test.db-mjXXXXXX9XX"]
1071 # NOTE: If the current SQLite VFS lacks the concept of a current directory,
1072 # the length of the current directory name plus 1 character for the
1073 # directory separator character are NOT counted as part of the total
1074 # size; otherwise, they are.
1077 set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
1079 expr {$::max_journal==(((512+2*(1024+8)+511)/512)*512 + $mj_pointer)}
1084 do_test pager1-5.5.1 {
1087 ATTACH 'test.db2' AS aux;
1088 PRAGMA journal_mode = PERSIST;
1089 CREATE TABLE t3(a, b);
1090 INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1;
1091 UPDATE t3 SET b = randomblob(1500);
1093 expr [file size test.db-journal] > 15000
1095 do_test pager1-5.5.2 {
1097 PRAGMA synchronous = full;
1099 DELETE FROM t1 WHERE b = 'Stalin';
1100 DELETE FROM t2 WHERE b = 'Stalin';
1103 file size test.db-journal
1107 #-------------------------------------------------------------------------
1108 # The following tests work with "PRAGMA max_page_count"
1110 do_test pager1-6.1 {
1111 faultsim_delete_and_reopen
1113 PRAGMA auto_vacuum = none;
1114 PRAGMA max_page_count = 10;
1115 CREATE TABLE t2(a, b);
1116 CREATE TABLE t3(a, b);
1117 CREATE TABLE t4(a, b);
1118 CREATE TABLE t5(a, b);
1119 CREATE TABLE t6(a, b);
1120 CREATE TABLE t7(a, b);
1121 CREATE TABLE t8(a, b);
1122 CREATE TABLE t9(a, b);
1123 CREATE TABLE t10(a, b);
1126 do_catchsql_test pager1-6.2 {
1127 CREATE TABLE t11(a, b)
1128 } {1 {database or disk is full}}
1129 do_execsql_test pager1-6.4 { PRAGMA max_page_count } {10}
1130 do_execsql_test pager1-6.5 { PRAGMA max_page_count = 15 } {15}
1131 do_execsql_test pager1-6.6 { CREATE TABLE t11(a, b) } {}
1132 do_execsql_test pager1-6.7 {
1134 INSERT INTO t11 VALUES(1, 2);
1135 PRAGMA max_page_count = 13;
1137 do_execsql_test pager1-6.8 {
1138 INSERT INTO t11 VALUES(3, 4);
1139 PRAGMA max_page_count = 10;
1141 do_execsql_test pager1-6.9 { COMMIT } {}
1143 do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {11}
1144 do_execsql_test pager1-6.11 { SELECT * FROM t11 } {1 2 3 4}
1145 do_execsql_test pager1-6.12 { PRAGMA max_page_count } {11}
1148 #-------------------------------------------------------------------------
1149 # The following tests work with "PRAGMA journal_mode=TRUNCATE" and
1150 # "PRAGMA locking_mode=EXCLUSIVE".
1152 # Each test is specified with 5 variables. As follows:
1154 # $tn: Test Number. Used as part of the [do_test] test names.
1155 # $sql: SQL to execute.
1156 # $res: Expected result of executing $sql.
1157 # $js: The expected size of the journal file, in bytes, after executing
1158 # the SQL script. Or -1 if the journal is not expected to exist.
1159 # $ws: The expected size of the WAL file, in bytes, after executing
1160 # the SQL script. Or -1 if the WAL is not expected to exist.
1163 faultsim_delete_and_reopen
1164 foreach {tn sql res js ws} [subst {
1167 CREATE TABLE t1(a, b);
1168 PRAGMA auto_vacuum=OFF;
1169 PRAGMA synchronous=NORMAL;
1170 PRAGMA page_size=1024;
1171 PRAGMA locking_mode=EXCLUSIVE;
1172 PRAGMA journal_mode=TRUNCATE;
1173 INSERT INTO t1 VALUES(1, 2);
1174 } {exclusive truncate} 0 -1
1188 4 { PRAGMA journal_mode = WAL } wal -1 -1
1189 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024]
1190 6 { PRAGMA locking_mode = NORMAL } exclusive -1 [wal_file_size 1 1024]
1191 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024]
1193 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1
1194 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1
1195 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1
1198 do_execsql_test pager1-7.1.$tn.1 $sql $res
1199 catch { set J -1 ; set J [file size test.db-journal] }
1200 catch { set W -1 ; set W [file size test.db-wal] }
1201 do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
1205 do_test pager1-7.2.1 {
1206 faultsim_delete_and_reopen
1208 PRAGMA locking_mode = EXCLUSIVE;
1209 CREATE TABLE t1(a, b);
1211 PRAGMA journal_mode = delete;
1212 PRAGMA journal_mode = truncate;
1214 } {exclusive delete truncate}
1215 do_test pager1-7.2.2 {
1216 execsql { INSERT INTO t1 VALUES(1, 2) }
1217 execsql { PRAGMA journal_mode = persist }
1219 do_test pager1-7.2.3 {
1222 PRAGMA journal_mode = persist;
1223 PRAGMA journal_size_limit;
1227 #-------------------------------------------------------------------------
1228 # The following tests, pager1-8.*, test that the special filenames
1229 # ":memory:" and "" open temporary databases.
1231 foreach {tn filename} {
1235 do_test pager1-8.$tn.1 {
1236 faultsim_delete_and_reopen
1238 sqlite3 db $filename
1240 PRAGMA auto_vacuum = 1;
1242 INSERT INTO x1 VALUES('Charles');
1243 INSERT INTO x1 VALUES('James');
1244 INSERT INTO x1 VALUES('Mary');
1247 } {Charles James Mary}
1249 do_test pager1-8.$tn.2 {
1250 sqlite3 db2 $filename
1251 catchsql { SELECT * FROM x1 } db2
1252 } {1 {no such table: x1}}
1254 do_execsql_test pager1-8.$tn.3 {
1256 INSERT INTO x1 VALUES('William');
1257 INSERT INTO x1 VALUES('Anne');
1262 #-------------------------------------------------------------------------
1263 # The next block of tests - pager1-9.* - deal with interactions between
1264 # the pager and the backup API. Test cases:
1266 # pager1-9.1.*: Test that a backup completes successfully even if the
1267 # source db is written to during the backup op.
1269 # pager1-9.2.*: Test that a backup completes successfully even if the
1270 # source db is written to and then rolled back during a
1273 do_test pager1-9.0.1 {
1274 faultsim_delete_and_reopen
1275 db func a_string a_string
1277 PRAGMA cache_size = 10;
1279 CREATE TABLE ab(a, b, UNIQUE(a, b));
1280 INSERT INTO ab VALUES( a_string(200), a_string(300) );
1281 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1282 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1283 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1284 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1285 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1286 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1287 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1291 do_test pager1-9.0.2 {
1292 sqlite3 db2 test.db2
1293 db2 eval { PRAGMA cache_size = 10 }
1294 sqlite3_backup B db2 main db main
1295 list [B step 10000] [B finish]
1296 } {SQLITE_DONE SQLITE_OK}
1297 do_test pager1-9.0.3 {
1298 db one {SELECT md5sum(a, b) FROM ab}
1299 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1301 do_test pager1-9.1.1 {
1302 execsql { UPDATE ab SET a = a_string(201) }
1303 sqlite3_backup B db2 main db main
1306 do_test pager1-9.1.2 {
1307 execsql { UPDATE ab SET b = a_string(301) }
1308 list [B step 10000] [B finish]
1309 } {SQLITE_DONE SQLITE_OK}
1310 do_test pager1-9.1.3 {
1311 db one {SELECT md5sum(a, b) FROM ab}
1312 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1313 do_test pager1-9.1.4 { execsql { SELECT count(*) FROM ab } } {128}
1315 do_test pager1-9.2.1 {
1316 execsql { UPDATE ab SET a = a_string(202) }
1317 sqlite3_backup B db2 main db main
1320 do_test pager1-9.2.2 {
1323 UPDATE ab SET b = a_string(301);
1326 list [B step 10000] [B finish]
1327 } {SQLITE_DONE SQLITE_OK}
1328 do_test pager1-9.2.3 {
1329 db one {SELECT md5sum(a, b) FROM ab}
1330 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1331 do_test pager1-9.2.4 { execsql { SELECT count(*) FROM ab } } {128}
1335 do_test pager1-9.3.1 {
1336 testvfs tv -default 1
1338 faultsim_delete_and_reopen
1340 execsql { PRAGMA page_size = 1024 }
1341 for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" }
1343 do_test pager1-9.3.2 {
1344 sqlite3 db2 test.db2
1347 PRAGMA page_size = 4096;
1348 PRAGMA synchronous = OFF;
1349 CREATE TABLE t1(a, b);
1350 CREATE TABLE t2(a, b);
1353 sqlite3_backup B db2 main db main
1355 list [B step 10000] [B finish]
1356 } {SQLITE_DONE SQLITE_OK}
1357 do_test pager1-9.3.3 {
1362 } [file size test.db]
1364 do_test pager1-9.4.1 {
1365 faultsim_delete_and_reopen
1366 sqlite3 db2 test.db2
1368 PRAGMA page_size = 4096;
1369 CREATE TABLE t1(a, b);
1370 CREATE TABLE t2(a, b);
1372 sqlite3_backup B db2 main db main
1373 list [B step 10000] [B finish]
1374 } {SQLITE_DONE SQLITE_OK}
1375 do_test pager1-9.4.2 {
1376 list [file size test.db2] [file size test.db]
1380 #-------------------------------------------------------------------------
1381 # Test that regardless of the value returned by xSectorSize(), the
1382 # minimum effective sector-size is 512 and the maximum 65536 bytes.
1384 testvfs tv -default 1
1385 foreach sectorsize {
1387 32 64 128 256 512 1024 2048
1388 4096 8192 16384 32768 65536 131072 262144
1390 tv sectorsize $sectorsize
1393 if {$sectorsize < 512} { set eff 512 }
1394 if {$sectorsize > 65536} { set eff 65536 }
1396 do_test pager1-10.$sectorsize.1 {
1397 faultsim_delete_and_reopen
1398 db func a_string a_string
1400 PRAGMA journal_mode = PERSIST;
1401 PRAGMA page_size = 1024;
1403 CREATE TABLE t1(a, b);
1404 CREATE TABLE t2(a, b);
1405 CREATE TABLE t3(a, b);
1408 file size test.db-journal
1409 } [expr $sectorsize > 65536 ? 65536 : ($sectorsize<32 ? 512 : $sectorsize)]
1411 do_test pager1-10.$sectorsize.2 {
1413 INSERT INTO t3 VALUES(a_string(300), a_string(300));
1414 INSERT INTO t3 SELECT * FROM t3; /* 2 */
1415 INSERT INTO t3 SELECT * FROM t3; /* 4 */
1416 INSERT INTO t3 SELECT * FROM t3; /* 8 */
1417 INSERT INTO t3 SELECT * FROM t3; /* 16 */
1418 INSERT INTO t3 SELECT * FROM t3; /* 32 */
1422 do_test pager1-10.$sectorsize.3 {
1426 PRAGMA cache_size = 10;
1429 recursive_select 32 t3 {db eval "INSERT INTO t2 VALUES(1, 2)"}
1436 do_test pager1-10.$sectorsize.4 {
1438 CREATE TABLE t6(a, b);
1439 CREATE TABLE t7(a, b);
1440 CREATE TABLE t5(a, b);
1446 CREATE TABLE t6(a, b);
1448 recursive_select 32 t3 {db eval "INSERT INTO t5 VALUES(1, 2)"}
1459 do_test pager1.10.x.1 {
1460 faultsim_delete_and_reopen
1462 PRAGMA auto_vacuum = none;
1463 PRAGMA page_size = 1024;
1466 for {set i 0} {$i<30} {incr i} {
1467 execsql { INSERT INTO t1 VALUES(zeroblob(900)) }
1471 do_test pager1.10.x.2 {
1478 do_test pager1.10.x.3 {
1483 recursive_select 30 t1
1493 testvfs tv -default 1
1494 faultsim_delete_and_reopen
1495 db func a_string a_string
1496 do_execsql_test pager1-11.1 {
1497 PRAGMA journal_mode = DELETE;
1498 PRAGMA cache_size = 10;
1500 CREATE TABLE zz(top PRIMARY KEY);
1501 INSERT INTO zz VALUES(a_string(222));
1502 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1503 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1504 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1505 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1506 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1509 UPDATE zz SET top = a_string(345);
1512 proc lockout {method args} { return SQLITE_IOERR }
1514 tv filter {xWrite xTruncate xSync}
1515 do_catchsql_test pager1-11.2 { COMMIT } {1 {disk I/O error}}
1518 do_test pager1-11.3 {
1521 PRAGMA journal_mode = TRUNCATE;
1522 PRAGMA integrity_check;
1525 do_test pager1-11.4 {
1527 file exists test.db-journal
1529 do_execsql_test pager1-11.5 { SELECT count(*) FROM zz } {32}
1533 #-------------------------------------------------------------------------
1534 # Test "PRAGMA page_size"
1536 testvfs tv -default 1
1539 512 1024 2048 4096 8192 16384 32768
1541 faultsim_delete_and_reopen
1543 # The sector-size (according to the VFS) is 1024 bytes. So if the
1544 # page-size requested using "PRAGMA page_size" is greater than the
1545 # compile time value of SQLITE_MAX_PAGE_SIZE, then the effective
1546 # page-size remains 1024 bytes.
1549 if {$eff > $::SQLITE_MAX_PAGE_SIZE} { set eff 1024 }
1551 do_test pager1-12.$pagesize.1 {
1554 PRAGMA page_size = $pagesize;
1555 CREATE VIEW v AS SELECT * FROM sqlite_master;
1559 do_test pager1-12.$pagesize.2 {
1562 SELECT count(*) FROM v;
1563 PRAGMA main.page_size;
1566 do_test pager1-12.$pagesize.3 {
1568 SELECT count(*) FROM v;
1569 PRAGMA main.page_size;
1577 #-------------------------------------------------------------------------
1578 # Test specal "PRAGMA journal_mode=PERSIST" test cases.
1580 # pager1-13.1.*: This tests a special case encountered in persistent
1581 # journal mode: If the journal associated with a transaction
1582 # is smaller than the journal file (because a previous
1583 # transaction left a very large non-hot journal file in the
1584 # file-system), then SQLite has to be careful that there is
1585 # not a journal-header left over from a previous transaction
1586 # immediately following the journal content just written.
1587 # If there is, and the process crashes so that the journal
1588 # becomes a hot-journal and must be rolled back by another
1589 # process, there is a danger that the other process may roll
1590 # back the aborted transaction, then continue copying data
1591 # from an older transaction from the remainder of the journal.
1592 # See the syncJournal() function for details.
1594 # pager1-13.2.*: Same test as the previous. This time, throw an index into
1595 # the mix to make the integrity-check more likely to catch
1598 testvfs tv -default 1
1601 proc xSyncCb {method filename args} {
1602 set t [file tail $filename]
1603 if {$t == "test.db"} faultsim_save
1606 faultsim_delete_and_reopen
1607 db func a_string a_string
1609 # The UPDATE statement at the end of this test case creates a really big
1610 # journal. Since the cache-size is only 10 pages, the journal contains
1611 # frequent journal headers.
1613 do_execsql_test pager1-13.1.1 {
1614 PRAGMA page_size = 1024;
1615 PRAGMA journal_mode = PERSIST;
1616 PRAGMA cache_size = 10;
1618 CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
1619 INSERT INTO t1 VALUES(NULL, a_string(400));
1620 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 2 */
1621 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 4 */
1622 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 8 */
1623 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 16 */
1624 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 32 */
1625 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 64 */
1626 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 128 */
1628 UPDATE t1 SET b = a_string(400);
1631 if {$::tcl_platform(platform)!="windows"} {
1632 # Run transactions of increasing sizes. Eventually, one (or more than one)
1633 # of these will write just enough content that one of the old headers created
1634 # by the transaction in the block above lies immediately after the content
1635 # journalled by the current transaction.
1637 for {set nUp 1} {$nUp<64} {incr nUp} {
1638 do_execsql_test pager1-13.1.2.$nUp.1 {
1639 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1641 do_execsql_test pager1-13.1.2.$nUp.2 { PRAGMA integrity_check } {ok}
1643 # Try to access the snapshot of the file-system.
1645 sqlite3 db2 sv_test.db
1646 do_test pager1-13.1.2.$nUp.3 {
1647 execsql { SELECT sum(length(b)) FROM t1 } db2
1648 } [expr {128*400 - ($nUp-1)}]
1649 do_test pager1-13.1.2.$nUp.4 {
1650 execsql { PRAGMA integrity_check } db2
1656 if {$::tcl_platform(platform)!="windows"} {
1657 # Same test as above. But this time with an index on the table.
1659 do_execsql_test pager1-13.2.1 {
1660 CREATE INDEX i1 ON t1(b);
1661 UPDATE t1 SET b = a_string(400);
1663 for {set nUp 1} {$nUp<64} {incr nUp} {
1664 do_execsql_test pager1-13.2.2.$nUp.1 {
1665 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1667 do_execsql_test pager1-13.2.2.$nUp.2 { PRAGMA integrity_check } {ok}
1668 sqlite3 db2 sv_test.db
1669 do_test pager1-13.2.2.$nUp.3 {
1670 execsql { SELECT sum(length(b)) FROM t1 } db2
1671 } [expr {128*400 - ($nUp-1)}]
1672 do_test pager1-13.2.2.$nUp.4 {
1673 execsql { PRAGMA integrity_check } db2
1682 #-------------------------------------------------------------------------
1683 # Test specal "PRAGMA journal_mode=OFF" test cases.
1685 faultsim_delete_and_reopen
1686 do_execsql_test pager1-14.1.1 {
1687 PRAGMA journal_mode = OFF;
1688 CREATE TABLE t1(a, b);
1690 INSERT INTO t1 VALUES(1, 2);
1694 do_catchsql_test pager1-14.1.2 {
1696 INSERT INTO t1 VALUES(3, 4);
1699 do_execsql_test pager1-14.1.3 {
1702 do_catchsql_test pager1-14.1.4 {
1704 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1705 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1706 } {1 {UNIQUE constraint failed: t1.rowid}}
1707 do_execsql_test pager1-14.1.5 {
1712 #-------------------------------------------------------------------------
1713 # Test opening and closing the pager sub-system with different values
1714 # for the sqlite3_vfs.szOsFile variable.
1716 faultsim_delete_and_reopen
1717 do_execsql_test pager1-15.0 {
1718 CREATE TABLE tx(y, z);
1719 INSERT INTO tx VALUES('Ayutthaya', 'Beijing');
1720 INSERT INTO tx VALUES('London', 'Tokyo');
1723 for {set i 0} {$i<513} {incr i 3} {
1724 testvfs tv -default 1 -szosfile $i
1726 do_execsql_test pager1-15.$i.1 {
1728 } {Ayutthaya Beijing London Tokyo}
1733 #-------------------------------------------------------------------------
1734 # Check that it is not possible to open a database file if the full path
1735 # to the associated journal file will be longer than sqlite3_vfs.mxPathname.
1737 testvfs tv -default 1
1740 proc xOpenCb {method filename args} {
1741 set ::file_len [string length $filename]
1747 for {set ii [expr $::file_len-5]} {$ii < [expr $::file_len+20]} {incr ii} {
1748 testvfs tv -default 1 -mxpathname $ii
1750 # The length of the full path to file "test.db-journal" is ($::file_len+8).
1751 # If the configured sqlite3_vfs.mxPathname value greater than or equal to
1752 # this, then the file can be opened. Otherwise, it cannot.
1754 if {$ii >= [expr $::file_len+8]} {
1757 set res {1 {unable to open database file}}
1760 do_test pager1-16.1.$ii {
1761 list [catch { sqlite3 db test.db } msg] $msg
1769 #-------------------------------------------------------------------------
1770 # Test the pagers response to the b-tree layer requesting illegal page
1773 # + The locking page,
1775 # + A page with a page number greater than (2^31-1).
1777 # These tests will not work if SQLITE_DIRECT_OVERFLOW_READ is defined. In
1778 # that case IO errors are sometimes reported instead of SQLITE_CORRUPT.
1780 ifcapable !direct_read {
1781 do_test pager1-18.1 {
1782 faultsim_delete_and_reopen
1783 db func a_string a_string
1785 PRAGMA page_size = 1024;
1786 CREATE TABLE t1(a, b);
1787 INSERT INTO t1 VALUES(a_string(500), a_string(200));
1788 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1789 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1790 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1791 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1792 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1793 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1794 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1797 do_test pager1-18.2 {
1798 set root [db one "SELECT rootpage FROM sqlite_master"]
1799 set lockingpage [expr (0x10000/1024) + 1]
1801 PRAGMA writable_schema = 1;
1802 UPDATE sqlite_master SET rootpage = $lockingpage;
1805 catchsql { SELECT count(*) FROM t1 } db2
1806 } {1 {database disk image is malformed}}
1808 do_test pager1-18.3.1 {
1811 INSERT INTO t2 VALUES(a_string(5000));
1813 set pgno [expr ([file size test.db] / 1024)-2]
1814 hexio_write test.db [expr ($pgno-1)*1024] 00000000
1816 # even though x is malformed, because typeof() does
1817 # not load the content of x, the error is not noticed.
1818 catchsql { SELECT typeof(x) FROM t2 } db2
1820 do_test pager1-18.3.2 {
1821 # in this case, the value of x is loaded and so the error is
1823 catchsql { SELECT length(x||'') FROM t2 } db2
1824 } {1 {database disk image is malformed}}
1826 do_test pager1-18.3.3 {
1829 INSERT INTO t2 VALUES(randomblob(5000));
1831 set pgno [expr ([file size test.db] / 1024)-2]
1832 hexio_write test.db [expr ($pgno-1)*1024] 00000000
1834 # even though x is malformed, because length() and typeof() do
1835 # not load the content of x, the error is not noticed.
1836 catchsql { SELECT length(x), typeof(x) FROM t2 } db2
1838 do_test pager1-18.3.4 {
1839 # in this case, the value of x is loaded and so the error is
1841 catchsql { SELECT length(x||'') FROM t2 } db2
1842 } {1 {database disk image is malformed}}
1844 do_test pager1-18.4 {
1845 hexio_write test.db [expr ($pgno-1)*1024] 90000000
1847 catchsql { SELECT length(x||'') FROM t2 } db2
1848 } {1 {database disk image is malformed}}
1850 do_test pager1-18.5 {
1853 CREATE TABLE t1(a, b);
1854 CREATE TABLE t2(a, b);
1855 PRAGMA writable_schema = 1;
1856 UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1';
1857 PRAGMA writable_schema = 0;
1858 ALTER TABLE t1 RENAME TO x1;
1860 catchsql { SELECT * FROM x1 }
1861 } {1 {database disk image is malformed}}
1864 do_test pager1-18.6 {
1865 faultsim_delete_and_reopen
1866 db func a_string a_string
1868 PRAGMA page_size = 1024;
1870 INSERT INTO t1 VALUES(a_string(800));
1871 INSERT INTO t1 VALUES(a_string(800));
1874 set root [db one "SELECT rootpage FROM sqlite_master"]
1877 hexio_write test.db [expr ($root-1)*1024 + 8] 00000000
1879 catchsql { SELECT length(x) FROM t1 }
1880 } {1 {database disk image is malformed}}
1883 do_test pager1-19.1 {
1885 db func a_string a_string
1887 PRAGMA page_size = 512;
1888 PRAGMA auto_vacuum = 1;
1889 CREATE TABLE t1(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1890 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1891 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1892 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1893 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1894 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1895 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1896 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1897 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1898 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1899 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1900 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1901 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1903 CREATE TABLE t2(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1904 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1905 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1906 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1907 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1908 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1909 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1910 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1911 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1912 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1913 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1914 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1915 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1917 INSERT INTO t1(aa) VALUES( a_string(100000) );
1918 INSERT INTO t2(aa) VALUES( a_string(100000) );
1923 #-------------------------------------------------------------------------
1924 # Test a couple of special cases that come up while committing
1927 # pager1-20.1.*: Committing an in-memory database transaction when the
1928 # database has not been modified at all.
1930 # pager1-20.2.*: As above, but with a normal db in exclusive-locking mode.
1932 # pager1-20.3.*: Committing a transaction in WAL mode where the database has
1933 # been modified, but all dirty pages have been flushed to
1934 # disk before the commit.
1936 do_test pager1-20.1.1 {
1940 CREATE TABLE one(two, three);
1941 INSERT INTO one VALUES('a', 'b');
1944 do_test pager1-20.1.2 {
1951 do_test pager1-20.2.1 {
1952 faultsim_delete_and_reopen
1954 PRAGMA locking_mode = exclusive;
1955 PRAGMA journal_mode = persist;
1956 CREATE TABLE one(two, three);
1957 INSERT INTO one VALUES('a', 'b');
1959 } {exclusive persist}
1960 do_test pager1-20.2.2 {
1968 do_test pager1-20.3.1 {
1969 faultsim_delete_and_reopen
1970 db func a_string a_string
1972 PRAGMA cache_size = 10;
1973 PRAGMA journal_mode = wal;
1977 INSERT INTO t1 VALUES(a_string(800));
1978 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */
1979 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */
1980 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */
1981 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */
1982 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */
1986 do_test pager1-20.3.2 {
1989 INSERT INTO t2 VALUES('xxxx');
1991 recursive_select 32 t1
1996 #-------------------------------------------------------------------------
1997 # Test that a WAL database may not be opened if:
1999 # pager1-21.1.*: The VFS has an iVersion less than 2, or
2000 # pager1-21.2.*: The VFS does not provide xShmXXX() methods.
2003 do_test pager1-21.0 {
2004 faultsim_delete_and_reopen
2006 PRAGMA journal_mode = WAL;
2007 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
2008 INSERT INTO ko DEFAULT VALUES;
2011 do_test pager1-21.1 {
2013 sqlite3 db2 test.db -vfs tv
2014 catchsql { SELECT * FROM ko } db2
2015 } {1 {unable to open database file}}
2018 do_test pager1-21.2 {
2019 testvfs tv -iversion 1
2020 sqlite3 db2 test.db -vfs tv
2021 catchsql { SELECT * FROM ko } db2
2022 } {1 {unable to open database file}}
2027 #-------------------------------------------------------------------------
2028 # Test that a "PRAGMA wal_checkpoint":
2030 # pager1-22.1.*: is a no-op on a non-WAL db, and
2031 # pager1-22.2.*: does not cause xSync calls with a synchronous=off db.
2034 do_test pager1-22.1.1 {
2035 faultsim_delete_and_reopen
2037 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
2038 INSERT INTO ko DEFAULT VALUES;
2040 execsql { PRAGMA wal_checkpoint }
2042 do_test pager1-22.2.1 {
2043 testvfs tv -default 1
2046 proc xSyncCb {args} {incr ::synccount}
2050 PRAGMA synchronous = off;
2051 PRAGMA journal_mode = WAL;
2052 INSERT INTO ko DEFAULT VALUES;
2054 execsql { PRAGMA wal_checkpoint }
2061 #-------------------------------------------------------------------------
2062 # Tests for changing journal mode.
2064 # pager1-23.1.*: Test that when changing from PERSIST to DELETE mode,
2065 # the journal file is deleted.
2067 # pager1-23.2.*: Same test as above, but while a shared lock is held
2068 # on the database file.
2070 # pager1-23.3.*: Same test as above, but while a reserved lock is held
2071 # on the database file.
2073 # pager1-23.4.*: And, for fun, while holding an exclusive lock.
2075 # pager1-23.5.*: Try to set various different journal modes with an
2076 # in-memory database (only MEMORY and OFF should work).
2078 # pager1-23.6.*: Try to set locking_mode=normal on an in-memory database
2079 # (doesn't work - in-memory databases always use
2080 # locking_mode=exclusive).
2082 do_test pager1-23.1.1 {
2083 faultsim_delete_and_reopen
2085 PRAGMA journal_mode = PERSIST;
2086 CREATE TABLE t1(a, b);
2088 file exists test.db-journal
2090 do_test pager1-23.1.2 {
2091 execsql { PRAGMA journal_mode = DELETE }
2092 file exists test.db-journal
2095 do_test pager1-23.2.1 {
2097 PRAGMA journal_mode = PERSIST;
2098 INSERT INTO t1 VALUES('Canberra', 'ACT');
2100 db eval { SELECT * FROM t1 } {
2101 db eval { PRAGMA journal_mode = DELETE }
2103 execsql { PRAGMA journal_mode }
2105 do_test pager1-23.2.2 {
2106 file exists test.db-journal
2109 do_test pager1-23.3.1 {
2111 PRAGMA journal_mode = PERSIST;
2112 INSERT INTO t1 VALUES('Darwin', 'NT');
2115 db eval { PRAGMA journal_mode = DELETE }
2116 execsql { PRAGMA journal_mode }
2118 do_test pager1-23.3.2 {
2119 file exists test.db-journal
2121 do_test pager1-23.3.3 {
2125 do_test pager1-23.4.1 {
2127 PRAGMA journal_mode = PERSIST;
2128 INSERT INTO t1 VALUES('Adelaide', 'SA');
2131 db eval { PRAGMA journal_mode = DELETE }
2132 execsql { PRAGMA journal_mode }
2134 do_test pager1-23.4.2 {
2135 file exists test.db-journal
2137 do_test pager1-23.4.3 {
2141 do_test pager1-23.5.1 {
2142 faultsim_delete_and_reopen
2145 foreach {tn mode possible} {
2153 do_test pager1-23.5.$tn.1 {
2154 execsql "PRAGMA journal_mode = off"
2155 execsql "PRAGMA journal_mode = $mode"
2156 } [if $possible {list $mode} {list off}]
2157 do_test pager1-23.5.$tn.2 {
2158 execsql "PRAGMA journal_mode = memory"
2159 execsql "PRAGMA journal_mode = $mode"
2160 } [if $possible {list $mode} {list memory}]
2162 do_test pager1-23.6.1 {
2163 execsql {PRAGMA locking_mode = normal}
2165 do_test pager1-23.6.2 {
2166 execsql {PRAGMA locking_mode = exclusive}
2168 do_test pager1-23.6.3 {
2169 execsql {PRAGMA locking_mode}
2171 do_test pager1-23.6.4 {
2172 execsql {PRAGMA main.locking_mode}
2175 #-------------------------------------------------------------------------
2177 do_test pager1-24.1.1 {
2178 faultsim_delete_and_reopen
2179 db func a_string a_string
2181 PRAGMA cache_size = 10;
2182 PRAGMA auto_vacuum = FULL;
2183 CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z));
2184 CREATE TABLE x2(x, y, z, PRIMARY KEY(y, z));
2185 INSERT INTO x2 VALUES(a_string(400), a_string(500), a_string(600));
2186 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2187 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2188 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2189 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2190 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2191 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2192 INSERT INTO x1 SELECT * FROM x2;
2195 do_test pager1-24.1.2 {
2198 DELETE FROM x1 WHERE rowid<32;
2200 recursive_select 64 x2
2202 do_test pager1-24.1.3 {
2204 UPDATE x1 SET z = a_string(300) WHERE rowid>40;
2206 PRAGMA integrity_check;
2207 SELECT count(*) FROM x1;
2211 do_test pager1-24.1.4 {
2214 INSERT INTO x1 SELECT * FROM x2;
2216 DELETE FROM x1 WHERE rowid<32;
2217 UPDATE x1 SET z = a_string(299) WHERE rowid>40;
2219 recursive_select 64 x2 {db eval COMMIT}
2221 PRAGMA integrity_check;
2222 SELECT count(*) FROM x1;
2226 do_test pager1-24.1.5 {
2229 INSERT INTO x1 SELECT * FROM x2;
2231 recursive_select 64 x2 { db eval {CREATE TABLE x3(x, y, z)} }
2232 execsql { SELECT * FROM x3 }
2235 #-------------------------------------------------------------------------
2237 do_test pager1-25-1 {
2238 faultsim_delete_and_reopen
2242 CREATE TABLE t1(a, b);
2248 do_test pager1-25-2 {
2249 faultsim_delete_and_reopen
2252 CREATE TABLE t1(a, b);
2259 #-------------------------------------------------------------------------
2260 # Sector-size tests.
2262 do_test pager1-26.1 {
2263 testvfs tv -default 1
2265 faultsim_delete_and_reopen
2266 db func a_string a_string
2268 PRAGMA page_size = 512;
2269 CREATE TABLE tbl(a PRIMARY KEY, b UNIQUE);
2271 INSERT INTO tbl VALUES(a_string(25), a_string(600));
2272 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2273 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2274 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2275 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2276 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2277 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2278 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2282 do_execsql_test pager1-26.1 {
2283 UPDATE tbl SET b = a_string(550);
2288 #-------------------------------------------------------------------------
2290 do_test pager1.27.1 {
2291 faultsim_delete_and_reopen
2292 sqlite3_pager_refcounts db
2295 CREATE TABLE t1(a, b);
2297 sqlite3_pager_refcounts db
2301 #-------------------------------------------------------------------------
2302 # Test that attempting to open a write-transaction with
2303 # locking_mode=exclusive in WAL mode fails if there are other clients on
2304 # the same database.
2308 do_multiclient_test tn {
2309 do_test pager1-28.$tn.1 {
2311 PRAGMA journal_mode = WAL;
2312 CREATE TABLE t1(a, b);
2313 INSERT INTO t1 VALUES('a', 'b');
2316 do_test pager1-28.$tn.2 { sql2 { SELECT * FROM t1 } } {a b}
2318 do_test pager1-28.$tn.3 { sql1 { PRAGMA locking_mode=exclusive } } {exclusive}
2319 do_test pager1-28.$tn.4 {
2320 csql1 { BEGIN; INSERT INTO t1 VALUES('c', 'd'); }
2321 } {1 {database is locked}}
2322 code2 { db2 close ; sqlite3 db2 test.db }
2323 do_test pager1-28.$tn.4 {
2324 sql1 { INSERT INTO t1 VALUES('c', 'd'); COMMIT }
2329 #-------------------------------------------------------------------------
2330 # Normally, when changing from journal_mode=PERSIST to DELETE the pager
2331 # attempts to delete the journal file. However, if it cannot obtain a
2332 # RESERVED lock on the database file, this step is skipped.
2334 do_multiclient_test tn {
2335 do_test pager1-28.$tn.1 {
2337 PRAGMA journal_mode = PERSIST;
2338 CREATE TABLE t1(a, b);
2339 INSERT INTO t1 VALUES('a', 'b');
2342 do_test pager1-28.$tn.2 { file exists test.db-journal } 1
2343 do_test pager1-28.$tn.3 { sql1 { PRAGMA journal_mode = DELETE } } delete
2344 do_test pager1-28.$tn.4 { file exists test.db-journal } 0
2346 do_test pager1-28.$tn.5 {
2348 PRAGMA journal_mode = PERSIST;
2349 INSERT INTO t1 VALUES('c', 'd');
2352 do_test pager1-28.$tn.6 { file exists test.db-journal } 1
2353 do_test pager1-28.$tn.7 {
2354 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2356 do_test pager1-28.$tn.8 { file exists test.db-journal } 1
2357 do_test pager1-28.$tn.9 { sql1 { PRAGMA journal_mode = DELETE } } delete
2358 do_test pager1-28.$tn.10 { file exists test.db-journal } 1
2360 do_test pager1-28.$tn.11 { sql2 COMMIT } {}
2361 do_test pager1-28.$tn.12 { file exists test.db-journal } 0
2363 do_test pager1-28-$tn.13 {
2364 code1 { set channel [db incrblob -readonly t1 a 2] }
2366 PRAGMA journal_mode = PERSIST;
2367 INSERT INTO t1 VALUES('g', 'h');
2370 do_test pager1-28.$tn.14 { file exists test.db-journal } 1
2371 do_test pager1-28.$tn.15 {
2372 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2374 do_test pager1-28.$tn.16 { sql1 { PRAGMA journal_mode = DELETE } } delete
2375 do_test pager1-28.$tn.17 { file exists test.db-journal } 1
2377 do_test pager1-28.$tn.17 { csql2 { COMMIT } } {1 {database is locked}}
2378 do_test pager1-28-$tn.18 { code1 { read $channel } } c
2379 do_test pager1-28-$tn.19 { code1 { close $channel } } {}
2380 do_test pager1-28.$tn.20 { sql2 { COMMIT } } {}
2383 do_test pager1-29.1 {
2384 faultsim_delete_and_reopen
2386 PRAGMA page_size = 1024;
2387 PRAGMA auto_vacuum = full;
2388 PRAGMA locking_mode=exclusive;
2389 CREATE TABLE t1(a, b);
2390 INSERT INTO t1 VALUES(1, 2);
2394 do_test pager1-29.2 {
2396 PRAGMA page_size = 4096;
2402 #-------------------------------------------------------------------------
2403 # Test that if an empty database file (size 0 bytes) is opened in
2404 # exclusive-locking mode, any journal file is deleted from the file-system
2405 # without being rolled back. And that the RESERVED lock obtained while
2406 # doing this is not released.
2408 do_test pager1-30.1 {
2411 delete_file test.db-journal
2412 set fd [open test.db-journal w]
2413 seek $fd [expr 512+1032*2]
2414 puts -nonewline $fd x
2419 PRAGMA locking_mode=EXCLUSIVE;
2420 SELECT count(*) FROM sqlite_master;
2423 } {exclusive 0 main reserved temp closed}
2425 #-------------------------------------------------------------------------
2426 # Test that if the "page-size" field in a journal-header is 0, the journal
2427 # file can still be rolled back. This is required for backward compatibility -
2428 # versions of SQLite prior to 3.5.8 always set this field to zero.
2430 if {$tcl_platform(platform)=="unix"} {
2431 do_test pager1-31.1 {
2432 faultsim_delete_and_reopen
2434 PRAGMA cache_size = 10;
2435 PRAGMA page_size = 1024;
2436 CREATE TABLE t1(x, y, UNIQUE(x, y));
2437 INSERT INTO t1 VALUES(randomblob(1500), randomblob(1500));
2438 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2439 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2440 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2441 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2442 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2443 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2444 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2445 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2446 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2447 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2449 UPDATE t1 SET y = randomblob(1499);
2451 copy_file test.db test.db2
2452 copy_file test.db-journal test.db2-journal
2454 hexio_write test.db2-journal 24 00000000
2455 sqlite3 db2 test.db2
2456 execsql { PRAGMA integrity_check } db2
2460 #-------------------------------------------------------------------------
2461 # Test that a database file can be "pre-hinted" to a certain size and that
2462 # subsequent spilling of the pager cache does not result in the database
2463 # file being shrunk.
2468 do_test pager1-32.1 {
2471 CREATE TABLE t1(x, y);
2477 INSERT INTO t1 VALUES(1, randomblob(10000));
2479 file_control_chunksize_test db main 1024
2480 file_control_sizehint_test db main 20971520; # 20MB
2482 PRAGMA cache_size = 10;
2483 INSERT INTO t1 VALUES(1, randomblob(10000));
2484 INSERT INTO t1 VALUES(2, randomblob(10000));
2485 INSERT INTO t1 SELECT x+2, randomblob(10000) from t1;
2486 INSERT INTO t1 SELECT x+4, randomblob(10000) from t1;
2487 INSERT INTO t1 SELECT x+8, randomblob(10000) from t1;
2488 INSERT INTO t1 SELECT x+16, randomblob(10000) from t1;
2489 SELECT count(*) FROM t1;
2496 # Cleanup 20MB file left by the previous test.
2499 #-------------------------------------------------------------------------
2500 # Test that if a transaction is committed in journal_mode=DELETE mode,
2501 # and the call to unlink() returns an ENOENT error, the COMMIT does not
2504 if {$::tcl_platform(platform)=="unix"} {
2505 do_test pager1-33.1 {
2509 INSERT INTO t1 VALUES('one');
2510 INSERT INTO t1 VALUES('two');
2512 INSERT INTO t1 VALUES('three');
2513 INSERT INTO t1 VALUES('four');
2515 forcedelete bak-journal
2516 file rename test.db-journal bak-journal
2519 } {1 {disk I/O error}}
2521 do_test pager1-33.2 {
2522 file rename bak-journal test.db-journal
2523 execsql { SELECT * FROM t1 }
2527 #-------------------------------------------------------------------------
2528 # Test that appending pages to the database file then moving those pages
2529 # to the free-list before the transaction is committed does not cause
2532 foreach {tn pragma strsize} {
2533 1 { PRAGMA mmap_size = 0 } 2400
2535 3 { PRAGMA mmap_size = 0 } 4400
2539 db func a_string a_string
2541 do_execsql_test 34.$tn.1 {
2542 CREATE TABLE t1(a, b);
2543 INSERT INTO t1 VALUES(1, 2);
2545 do_execsql_test 34.$tn.2 {
2547 INSERT INTO t1 VALUES(2, a_string($strsize));
2548 DELETE FROM t1 WHERE oid=2;
2550 PRAGMA integrity_check;
2554 #-------------------------------------------------------------------------
2561 CREATE TABLE t1(x, y);
2562 PRAGMA journal_mode = WAL;
2563 INSERT INTO t1 VALUES(1, 2);
2568 CREATE TABLE t2(a, b);
2571 hexio_write test.db-shm [expr 16*1024] [string repeat 0055 8192]
2575 do_multiclient_test tn {
2577 PRAGMA auto_vacuum = 0;
2578 CREATE TABLE t1(x, y);
2579 INSERT INTO t1 VALUES(1, 2);
2583 sql2 { PRAGMA max_page_count = 2 }
2584 list [catch { sql2 { CREATE TABLE t2(x) } } msg] $msg
2585 } {1 {database or disk is full}}
2587 sql1 { PRAGMA checkpoint_fullfsync = 1 }
2588 sql1 { CREATE TABLE t2(x) }
2591 sql2 { INSERT INTO t2 VALUES('xyz') }
2592 list [catch { sql2 { CREATE TABLE t3(x) } } msg] $msg
2593 } {1 {database or disk is full}}
2596 forcedelete test1 test2
2598 1 {file:?mode=memory&cache=shared}
2599 2 {file:one?mode=memory&cache=shared}
2600 3 {file:test1?cache=shared}
2601 4 {file:test2?another=parameter&yet=anotherone}
2606 sqlite3_config_uri 1
2611 INSERT INTO t1 VALUES(1);
2616 do_execsql_test 37.$tn.2 {
2623 sqlite3_config_uri 0
2629 set fd [open test.db w]
2630 puts $fd "hello world"
2633 catchsql { CREATE TABLE t1(x) }
2634 } {1 {file is encrypted or is not a database}}
2643 PRAGMA auto_vacuum = 1;
2645 INSERT INTO t1 VALUES('xxx');
2646 INSERT INTO t1 VALUES('two');
2647 INSERT INTO t1 VALUES(randomblob(400));
2648 INSERT INTO t1 VALUES(randomblob(400));
2649 INSERT INTO t1 VALUES(randomblob(400));
2650 INSERT INTO t1 VALUES(randomblob(400));
2652 UPDATE t1 SET x = 'one' WHERE rowid=1;
2654 set ::stmt [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
2655 sqlite3_step $::stmt
2656 sqlite3_column_text $::stmt 0
2659 execsql { CREATE TABLE t2(x) }
2660 sqlite3_step $::stmt
2661 sqlite3_column_text $::stmt 0
2664 sqlite3_finalize $::stmt
2668 do_execsql_test 39.4 {
2669 PRAGMA auto_vacuum = 2;
2681 PRAGMA cache_size = 1;
2682 PRAGMA incremental_vacuum;
2683 PRAGMA integrity_check;
2690 PRAGMA auto_vacuum = 1;
2691 CREATE TABLE t1(x PRIMARY KEY);
2692 INSERT INTO t1 VALUES(randomblob(1200));
2698 INSERT INTO t1 VALUES(randomblob(1200));
2699 INSERT INTO t1 VALUES(randomblob(1200));
2700 INSERT INTO t1 VALUES(randomblob(1200));
2707 PRAGMA cache_size = 1;
2709 PRAGMA integrity_check;
2716 CREATE TABLE t1(x PRIMARY KEY);
2717 INSERT INTO t1 VALUES(randomblob(200));
2718 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2719 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2720 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2721 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2722 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2723 INSERT INTO t1 SELECT randomblob(200) FROM t1;
2727 testvfs tv -default 1
2728 tv sectorsize 16384;
2733 PRAGMA cache_size = 1;
2734 DELETE FROM t1 WHERE rowid%4;
2735 PRAGMA integrity_check;
2741 set pending_prev [sqlite3_test_control_pending_byte 0x1000000]
2745 CREATE TABLE t1(x, y);
2746 INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
2747 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2748 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2749 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2750 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2751 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2752 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2753 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2754 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2755 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2758 sqlite3_test_control_pending_byte 0x0010000
2760 db eval { PRAGMA mmap_size = 0 }
2761 catchsql { SELECT sum(length(y)) FROM t1 }
2762 } {1 {database disk image is malformed}}
2766 CREATE TABLE t1(x, y);
2767 INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
2768 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2769 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2770 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2771 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2772 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2773 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2774 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2775 INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
2779 testvfs tv -default 1
2780 tv sectorsize 16384;
2782 sqlite3 db test.db -vfs tv
2783 execsql { UPDATE t1 SET x = randomblob(200) }
2787 sqlite3_test_control_pending_byte $pending_prev
2792 CREATE TABLE t1(x, y);
2793 INSERT INTO t1 VALUES(1, 2);
2794 CREATE TABLE t2(x, y);
2795 INSERT INTO t2 VALUES(1, 2);
2796 CREATE TABLE t3(x, y);
2797 INSERT INTO t3 VALUES(1, 2);
2802 db eval { PRAGMA mmap_size = 0 }
2803 db eval { SELECT * FROM t1 }
2804 sqlite3_db_status db CACHE_MISS 0
2808 db eval { SELECT * FROM t2 }
2809 sqlite3_db_status db CACHE_MISS 1
2813 db eval { SELECT * FROM t3 }
2814 sqlite3_db_status db CACHE_MISS 0