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 # $Id: incrvacuum2.test,v 1.6 2009/07/25 13:42:50 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # If this build of the library does not support auto-vacuum, omit this
21 ifcapable {!autovacuum || !pragma} {
26 set testprefix incrvacuum2
28 # Create a database in incremental vacuum mode that has many
29 # pages on the freelist.
31 do_test incrvacuum2-1.1 {
33 PRAGMA page_size=1024;
34 PRAGMA auto_vacuum=incremental;
36 INSERT INTO t1 VALUES(zeroblob(30000));
42 # Vacuum off a single page.
44 do_test incrvacuum2-1.2 {
46 PRAGMA incremental_vacuum(1);
51 # Vacuum off five pages
53 do_test incrvacuum2-1.3 {
55 PRAGMA incremental_vacuum(5);
60 # Vacuum off all the rest
62 do_test incrvacuum2-1.4 {
64 PRAGMA incremental_vacuum(1000);
69 # Make sure incremental vacuum works on attached databases.
72 do_test incrvacuum2-2.1 {
73 forcedelete test2.db test2.db-journal
75 ATTACH DATABASE 'test2.db' AS aux;
76 PRAGMA aux.auto_vacuum=incremental;
77 CREATE TABLE aux.t2(x);
78 INSERT INTO t2 VALUES(zeroblob(30000));
79 INSERT INTO t1 SELECT * FROM t2;
83 list [file size test.db] [file size test2.db]
85 do_test incrvacuum2-2.2 {
87 PRAGMA aux.incremental_vacuum(1)
89 list [file size test.db] [file size test2.db]
91 do_test incrvacuum2-2.3 {
93 PRAGMA aux.incremental_vacuum(5)
95 list [file size test.db] [file size test2.db]
97 do_test incrvacuum2-2.4 {
99 PRAGMA main.incremental_vacuum(5)
101 list [file size test.db] [file size test2.db]
103 do_test incrvacuum2-2.5 {
105 PRAGMA aux.incremental_vacuum
107 list [file size test.db] [file size test2.db]
109 do_test incrvacuum2-2.6 {
111 PRAGMA incremental_vacuum(1)
113 list [file size test.db] [file size test2.db]
117 do_test incrvacuum2-3.1 {
119 PRAGMA auto_vacuum = 'full';
122 INSERT INTO abc VALUES(randstr(1500,1500));
126 do_test incrvacuum2-3.2 {
130 PRAGMA incremental_vacuum;
135 integrity_check incrvacuum2-3.3
137 if {[wal_is_capable]} {
138 # At one point, when a specific page was being extracted from the b-tree
139 # free-list (e.g. during an incremental-vacuum), all trunk pages that
140 # occurred before the specific page in the free-list trunk were being
141 # written to the journal or wal file. This is not necessary. Only the
142 # extracted page and the page that contains the pointer to it need to
145 # This problem was fixed by [d03d63d77e] (just before 3.7.6 release).
147 # This test case builds a database containing many free pages. Then runs
148 # "PRAGMA incremental_vacuum(1)" until the db contains zero free pages.
149 # Each "PRAGMA incremental_vacuum(1)" should modify at most 4 pages. The
150 # worst case is when a trunk page is removed from the end of the db file.
151 # In this case pages written are:
153 # 1. The previous trunk page (that contains a pointer to the recycled
155 # 2. The leaf page transformed into a trunk page to replace the recycled
157 # 3. The trunk page that contained a pointer to the leaf page used
159 # 4. Page 1. Page 1 is always updated, even in WAL mode, since it contains
160 # the "number of free-list pages" field.
166 do_execsql_test 4.1 {
167 PRAGMA page_size = 512;
168 PRAGMA auto_vacuum = 2;
170 INSERT INTO t1 VALUES(randomblob(400));
171 INSERT INTO t1 SELECT * FROM t1; -- 2
172 INSERT INTO t1 SELECT * FROM t1; -- 4
173 INSERT INTO t1 SELECT * FROM t1; -- 8
174 INSERT INTO t1 SELECT * FROM t1; -- 16
175 INSERT INTO t1 SELECT * FROM t1; -- 32
176 INSERT INTO t1 SELECT * FROM t1; -- 128
177 INSERT INTO t1 SELECT * FROM t1; -- 256
178 INSERT INTO t1 SELECT * FROM t1; -- 512
179 INSERT INTO t1 SELECT * FROM t1; -- 1024
180 INSERT INTO t1 SELECT * FROM t1; -- 2048
181 INSERT INTO t1 SELECT * FROM t1; -- 4096
182 INSERT INTO t1 SELECT * FROM t1; -- 8192
183 DELETE FROM t1 WHERE oid>512;
189 PRAGMA journal_mode = WAL;
190 PRAGMA incremental_vacuum(1);
194 execsql { PRAGMA wal_checkpoint }
195 file size test.db-wal
196 } [expr {32+2*(512+24)}]
202 while {[file size test.db] > [expr 512*3]} {
203 execsql { PRAGMA journal_mode = WAL }
204 execsql { PRAGMA wal_checkpoint }
205 execsql { PRAGMA incremental_vacuum(1) }
206 set newsz [file size test.db-wal]
207 if {$newsz>$maxsz} {set maxsz $newsz}
210 } [expr {32+3*(512+24)}]