3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the incremental vacuum feature.
14 # Note: There are also some tests for incremental vacuum and IO
15 # errors in incrvacuum_ioerr.test.
17 # $Id: incrvacuum.test,v 1.23 2009/02/18 20:31:18 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # If this build of the library does not support auto-vacuum, omit this
24 ifcapable {!autovacuum || !pragma} {
29 #---------------------------------------------------------------------
30 # Test the pragma on an empty database.
32 do_test incrvacuum-1.1 {
36 } $sqlite_options(default_autovacuum)
37 do_test incrvacuum-1.2.0 {
38 # File size is sometimes 1 instead of 0 due to the hack we put in
39 # to work around ticket #3260. Search for comments on #3260 in
41 expr {[file size test.db] > 1}
43 do_test incrvacuum-1.2 {
44 # This command will create the database.
46 pragma auto_vacuum = 'full';
50 do_test incrvacuum-1.2.1 {
51 expr {[file size test.db] > 0}
53 do_test incrvacuum-1.3 {
55 pragma auto_vacuum = 'incremental';
59 do_test incrvacuum-1.4 {
60 # In this case the invalid value is ignored and the auto_vacuum
61 # setting remains unchanged.
63 pragma auto_vacuum = 'invalid';
67 do_test incrvacuum-1.5 {
69 pragma auto_vacuum = 1;
73 do_test incrvacuum-1.6 {
75 pragma auto_vacuum = '2';
79 do_test incrvacuum-1.7 {
80 # Invalid value. auto_vacuum setting remains unchanged.
82 pragma auto_vacuum = 5;
87 #---------------------------------------------------------------------
88 # Test the pragma on a non-empty database. It is possible to toggle
89 # the connection between "full" and "incremental" mode, but not to
90 # change from either of these to "none", or from "none" to "full" or
93 do_test incrvacuum-2.1 {
95 pragma auto_vacuum = 1;
96 CREATE TABLE abc(a, b, c);
99 do_test incrvacuum-2.2 {
101 pragma auto_vacuum = 'none';
105 do_test incrvacuum-2.2.1 {
112 do_test incrvacuum-2.3 {
114 pragma auto_vacuum = 'incremental';
118 do_test incrvacuum-2.4 {
120 pragma auto_vacuum = 'full';
125 #---------------------------------------------------------------------
126 # Test that when the auto_vacuum mode is "incremental", the database
127 # does not shrink when pages are removed from it. But it does if
128 # the mode is set to "full".
130 do_test incrvacuum-3.1 {
135 do_test incrvacuum-3.2 {
136 set ::str [string repeat 1234567890 110]
138 PRAGMA auto_vacuum = 2;
140 CREATE TABLE tbl2(str);
141 INSERT INTO tbl2 VALUES($::str);
146 # 1 -> database header
147 # 2 -> first back-pointer page
150 # 5 -> table tbl2 overflow page.
152 expr {[file size test.db] / 1024}
154 do_test incrvacuum-3.3 {
159 expr {[file size test.db] / 1024}
161 do_test incrvacuum-3.4 {
163 PRAGMA auto_vacuum = 1;
164 INSERT INTO tbl2 VALUES('hello world');
166 expr {[file size test.db] / 1024}
169 #---------------------------------------------------------------------
170 # Try to run a very simple incremental vacuum. Also verify that
171 # PRAGMA incremental_vacuum is a harmless no-op against a database that
172 # does not support auto-vacuum.
174 do_test incrvacuum-4.1 {
175 set ::str [string repeat 1234567890 110]
177 PRAGMA auto_vacuum = 2;
178 INSERT INTO tbl2 VALUES($::str);
179 CREATE TABLE tbl1(a, b, c);
181 expr {[file size test.db] / 1024}
183 do_test incrvacuum-4.2 {
188 expr {[file size test.db] / 1024}
190 do_test incrvacuum-4.3 {
192 db eval {pragma incremental_vacuum(10)} {
195 list [expr {[file size test.db] / 1024}] $::nStep
198 #---------------------------------------------------------------------
199 # The following tests - incrvacuum-5.* - test incremental vacuum
200 # from within a transaction.
202 do_test incrvacuum-5.1.1 {
203 expr {[file size test.db] / 1024}
205 do_test incrvacuum-5.1.2 {
209 PRAGMA incremental_vacuum;
212 expr {[file size test.db] / 1024}
215 do_test incrvacuum-5.2.1 {
216 set ::str [string repeat abcdefghij 110]
219 CREATE TABLE tbl1(a);
220 INSERT INTO tbl1 VALUES($::str);
221 PRAGMA incremental_vacuum; -- this is a no-op.
224 expr {[file size test.db] / 1024}
226 do_test incrvacuum-5.2.2 {
227 set ::str [string repeat abcdefghij 110]
230 INSERT INTO tbl1 VALUES($::str);
231 INSERT INTO tbl1 SELECT * FROM tbl1;
232 DELETE FROM tbl1 WHERE oid%2; -- Put 2 overflow pages on free-list.
235 expr {[file size test.db] / 1024}
237 do_test incrvacuum-5.2.3 {
240 PRAGMA incremental_vacuum; -- Vacuum up the two pages.
241 CREATE TABLE tbl2(b); -- Use one free page as a table root.
242 INSERT INTO tbl2 VALUES('a nice string');
245 expr {[file size test.db] / 1024}
247 do_test incrvacuum-5.2.4 {
252 do_test incrvacuum-5.2.5 {
256 PRAGMA incremental_vacuum;
258 expr {[file size test.db] / 1024}
262 # Test cases incrvacuum-5.3.* use the following list as input data.
263 # Two new databases are opened, one with incremental vacuum enabled,
264 # the other with no auto-vacuum completely disabled. After executing
265 # each element of the following list on both databases, test that
266 # the integrity-check passes and the contents of each are identical.
268 set TestScriptList [list {
270 CREATE TABLE t1(a, b);
271 CREATE TABLE t2(a, b);
272 CREATE INDEX t1_i ON t1(a);
273 CREATE INDEX t2_i ON t2(a);
275 INSERT INTO t1 VALUES($::str1, $::str2);
276 INSERT INTO t1 VALUES($::str1||$::str2, $::str2||$::str1);
277 INSERT INTO t2 SELECT b, a FROM t1;
278 INSERT INTO t2 SELECT a, b FROM t1;
279 INSERT INTO t1 SELECT b, a FROM t2;
280 UPDATE t2 SET b = '';
281 PRAGMA incremental_vacuum;
283 UPDATE t2 SET b = (SELECT b FROM t1 WHERE t1.oid = t2.oid);
284 PRAGMA incremental_vacuum;
286 CREATE TABLE t3(a, b);
287 INSERT INTO t3 SELECT * FROM t2;
289 PRAGMA incremental_vacuum;
291 CREATE INDEX t3_i ON t3(a);
296 PRAGMA incremental_vacuum;
297 INSERT INTO t3 VALUES('hello', 'world');
300 INSERT INTO t3 VALUES('hello', 'world');
304 # If this build omits subqueries, step 2 in the above list will not
305 # work. Replace it with "" in this case.
307 ifcapable !subquery { lset TestScriptList 2 "" }
309 # Compare the contents of databases $A and $B.
311 proc compare_dbs {A B tname} {
312 set tbl_list [execsql {
313 SELECT tbl_name FROM sqlite_master WHERE type = 'table'
316 do_test ${tname}.1 [subst {
318 SELECT tbl_name FROM sqlite_master WHERE type = 'table'
323 foreach tbl $tbl_list {
324 set control [execsql "SELECT * FROM $tbl" $A]
325 do_test ${tname}.[incr tn] [subst {
326 execsql "SELECT * FROM $tbl" $B
331 set ::str1 [string repeat abcdefghij 130]
332 set ::str2 [string repeat 1234567890 105]
334 forcedelete test1.db test1.db-journal test2.db test2.db-journal
337 execsql { PRAGMA auto_vacuum = 'none' } db1
338 execsql { PRAGMA auto_vacuum = 'incremental' } db2
341 foreach sql $::TestScriptList {
345 compare_dbs db1 db2 incrvacuum-5.3.${tn}
346 do_test incrvacuum-5.3.${tn}.integrity1 {
347 execsql { PRAGMA integrity_check; } db1
349 do_test incrvacuum-5.3.${tn}.integrity2 {
350 execsql { PRAGMA integrity_check; } db2
357 # End of test cases 5.3.*
359 #---------------------------------------------------------------------
360 # The following tests - incrvacuum-6.* - test running incremental
361 # vacuum while another statement (a read) is being executed.
363 for {set jj 0} {$jj < 10} {incr jj} {
364 # Build some test data. Two tables are created in an empty
365 # database. tbl1 data is a contiguous block starting at page 5 (pages
366 # 3 and 4 are the table roots). tbl2 is a contiguous block starting
369 # Then drop tbl1 so that when an incr vacuum is run the pages
370 # of tbl2 have to be moved to fill the gap.
372 do_test incrvacuum-6.${jj}.1 {
374 DROP TABLE IF EXISTS tbl1;
375 DROP TABLE IF EXISTS tbl2;
376 PRAGMA incremental_vacuum;
377 CREATE TABLE tbl1(a, b);
378 CREATE TABLE tbl2(a, b);
381 for {set ii 0} {$ii < 1000} {incr ii} {
382 db eval {INSERT INTO tbl1 VALUES($ii, $ii || $ii)}
385 INSERT INTO tbl2 SELECT * FROM tbl1;
389 expr {[file size test.db] / 1024}
392 # Run a linear scan query on tbl2. After reading ($jj*100) rows,
393 # run the incremental vacuum to shrink the database.
395 do_test incrvacuum-6.${jj}.2 {
397 db eval {SELECT a FROM tbl2} {} {
398 if {$a == [expr $jj*100]} {
399 db eval {PRAGMA incremental_vacuum}
403 list [expr {[file size test.db] / 1024}] $nRow
407 #---------------------------------------------------------------------
408 # This test - incrvacuum-7.* - is to check that the database can be
409 # written in the middle of an incremental vacuum.
413 do_test incrvacuum-7.${::iWrite}.1 {
415 DROP TABLE IF EXISTS tbl1;
416 DROP TABLE IF EXISTS tbl2;
417 PRAGMA incremental_vacuum;
418 CREATE TABLE tbl1(a, b);
419 CREATE TABLE tbl2(a, b);
422 for {set ii 0} {$ii < 1000} {incr ii} {
423 db eval {INSERT INTO tbl1 VALUES($ii, $ii || $ii)}
426 INSERT INTO tbl2 SELECT * FROM tbl1;
430 expr {[file size test.db] / 1024}
433 do_test incrvacuum-7.${::iWrite}.2 {
435 db eval {PRAGMA incremental_vacuum} {
437 if {$::nRow == $::iWrite} {
439 CREATE TABLE tbl1(a, b);
440 INSERT INTO tbl1 VALUES('hello', 'world');
444 list [expr {[file size test.db] / 1024}]
447 do_test incrvacuum-7.${::iWrite}.3 {
453 if {$::nRow == $::iWrite} break
457 #---------------------------------------------------------------------
458 # This test - incrvacuum-8.* - is to check that nothing goes wrong
459 # with an incremental-vacuum if it is the first statement executed
460 # after an existing database is opened.
462 # At one point, this would always return SQLITE_SCHEMA (which
463 # causes an infinite loop in tclsqlite.c if using the Tcl interface).
465 do_test incrvacuum-8.1 {
469 PRAGMA incremental_vacuum(50);
473 #---------------------------------------------------------------------
474 # At one point this test case was causing an assert() to fail.
476 do_test incrvacuum-9.1 {
478 forcedelete test.db test.db-journal
482 PRAGMA auto_vacuum = 'incremental';
483 CREATE TABLE t1(a, b, c);
484 CREATE TABLE t2(a, b, c);
485 INSERT INTO t2 VALUES(randstr(500,500),randstr(500,500),randstr(500,500));
486 INSERT INTO t1 VALUES(1, 2, 3);
487 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
488 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
489 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
490 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
491 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
492 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
493 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
494 INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
498 do_test incrvacuum-9.2 {
500 PRAGMA synchronous = 'OFF';
502 UPDATE t1 SET a = a, b = b, c = c;
504 PRAGMA incremental_vacuum(10);
509 do_test incrvacuum-9.3 {
511 PRAGMA cache_size = 10;
513 UPDATE t1 SET a = a, b = b, c = c;
515 PRAGMA incremental_vacuum(10);
520 #---------------------------------------------------------------------
521 # Test that the parameter to the incremental_vacuum pragma works. That
522 # is, if the user executes "PRAGMA incremental_vacuum(N)", at most
523 # N pages are vacuumed.
525 do_test incrvacuum-10.1 {
530 expr [file size test.db] / 1024
533 do_test incrvacuum-10.2 {
535 PRAGMA incremental_vacuum(1);
537 expr [file size test.db] / 1024
540 do_test incrvacuum-10.3 {
542 PRAGMA incremental_vacuum(5);
544 expr [file size test.db] / 1024
547 do_test incrvacuum-10.4 {
549 PRAGMA incremental_vacuum('1');
551 expr [file size test.db] / 1024
554 do_test incrvacuum-10.5 {
556 PRAGMA incremental_vacuum("+3");
558 expr [file size test.db] / 1024
561 do_test incrvacuum-10.6 {
563 PRAGMA incremental_vacuum = 1;
565 expr [file size test.db] / 1024
568 do_test incrvacuum-10.7 {
569 # Use a really big number as an argument to incremetal_vacuum. Should
570 # be interpreted as "free all possible space".
572 PRAGMA incremental_vacuum(2147483649);
574 expr [file size test.db] / 1024
577 do_test incrvacuum-10.8 {
580 INSERT INTO t1 VALUES(hex(randomblob(1000)));
583 # A negative number means free all possible space.
585 PRAGMA incremental_vacuum=-1;
587 expr [file size test.db] / 1024
590 #----------------------------------------------------------------
591 # Test that if we set the auto_vacuum mode to 'incremental', then
592 # create a database, thereafter that database defaults to incremental
596 forcedelete test.db test.db-journal
599 ifcapable default_autovacuum {
600 do_test incrvacuum-11.1-av-dflt-on {
606 do_test incrvacuum-11.1-av-dflt-off {
612 do_test incrvacuum-11.2 {
614 PRAGMA auto_vacuum = incremental;
617 do_test incrvacuum-11.3 {
622 do_test incrvacuum-11.4 {
623 # The database has now been created.
624 expr {[file size test.db]>0}
626 do_test incrvacuum-11.5 {
627 # Close and reopen the connection.
631 # Test we are still in incremental vacuum mode.
632 execsql { PRAGMA auto_vacuum; }
634 do_test incrvacuum-11.6 {
636 PRAGMA auto_vacuum = 'full';
640 do_test incrvacuum-11.7 {
641 # Close and reopen the connection.
645 # Test we are still in "full" auto-vacuum mode.
646 execsql { PRAGMA auto_vacuum; }
649 #----------------------------------------------------------------------
650 # Special case: What happens if the database is locked when a "PRAGMA
651 # auto_vacuum = XXX" statement is executed.
654 forcedelete test.db test.db-journal
657 do_test incrvacuum-12.1 {
659 PRAGMA auto_vacuum = 1;
661 expr {[file size test.db]>0}
664 # Try to change the auto-vacuum from "full" to "incremental" while the
665 # database is locked. Nothing should change.
667 do_test incrvacuum-12.2 {
669 execsql { BEGIN EXCLUSIVE; } db2
670 catchsql { PRAGMA auto_vacuum = 2; }
671 } {1 {database is locked}}
673 do_test incrvacuum-12.3 {
674 execsql { ROLLBACK; } db2
675 execsql { PRAGMA auto_vacuum }
676 } {2} ;# Still 2 because PRAGMA auto_vacuum setting held in case of vacuum
677 do_test incrvacuum-12.4 {
680 execsql { PRAGMA auto_vacuum }
681 } {1} ;# Revert to 1 because the database file did not change
683 do_test incrvacuum-12.5 {
684 execsql { SELECT * FROM sqlite_master }
685 execsql { PRAGMA auto_vacuum }
688 #----------------------------------------------------------------------
689 # Special case #2: What if one process prepares a "PRAGMA auto_vacuum = XXX"
690 # statement when the database is empty, but doesn't execute it until
691 # after some other process has created the database.
695 forcedelete test.db test.db-journal
696 sqlite3 db test.db ; set ::DB [sqlite3_connection_pointer db]
699 do_test incrvacuum-13.1 {
700 # File size is sometimes 1 instead of 0 due to the hack we put in
701 # to work around ticket #3260. Search for comments on #3260 in
703 expr {[file size test.db]>1}
705 do_test incrvacuum-13.2 {
706 set ::STMT [sqlite3_prepare $::DB {PRAGMA auto_vacuum = 2} -1 DUMMY]
708 PRAGMA auto_vacuum = none;
709 PRAGMA default_cache_size = 1024;
713 do_test incrvacuum-13.3 {
714 expr {[file size test.db]>0}
716 do_test incrvacuum-13.4 {
717 set rc [sqlite3_step $::STMT]
718 list $rc [sqlite3_finalize $::STMT]
719 } {SQLITE_DONE SQLITE_OK}
720 do_test incrvacuum-13.5 {
727 # Verify that the incremental_vacuum pragma fails gracefully if it
728 # is used against an invalid database file.
730 if {[permutation] == ""} {
731 do_test incrvacuum-14.1 {
732 set out [open invalid.db w]
733 puts $out "This is not an SQLite database file"
735 sqlite3 db3 invalid.db
737 PRAGMA incremental_vacuum(10);
739 } {1 {file is not a database}}
743 do_test incrvacuum-15.1 {
749 set str [string repeat "abcdefghij" 500]
752 PRAGMA cache_size = 10;
753 PRAGMA auto_vacuum = incremental;
754 CREATE TABLE t1(x, y);
755 INSERT INTO t1 VALUES('a', $str);
756 INSERT INTO t1 VALUES('b', $str);
757 INSERT INTO t1 VALUES('c', $str);
758 INSERT INTO t1 VALUES('d', $str);
759 INSERT INTO t1 VALUES('e', $str);
760 INSERT INTO t1 VALUES('f', $str);
761 INSERT INTO t1 VALUES('g', $str);
762 INSERT INTO t1 VALUES('h', $str);
763 INSERT INTO t1 VALUES('i', $str);
764 INSERT INTO t1 VALUES('j', $str);
765 INSERT INTO t1 VALUES('j', $str);
767 CREATE TABLE t2(x PRIMARY KEY, y);
768 INSERT INTO t2 VALUES('a', $str);
769 INSERT INTO t2 VALUES('b', $str);
770 INSERT INTO t2 VALUES('c', $str);
771 INSERT INTO t2 VALUES('d', $str);
775 PRAGMA incremental_vacuum;
778 catchsql {INSERT INTO t2 SELECT * FROM t1}
782 PRAGMA integrity_check;