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 SELECT statement.
14 # $Id: autovacuum.test,v 1.17 2005/03/09 13:09:45 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} {
26 # Return a string $len characters long. The returned string is $char repeated
27 # over and over. For example, [make_str abc 8] returns "abcabcab".
28 proc make_str {char len} {
29 set str [string repeat $char. $len]
30 return [string range $str 0 [expr $len-1]]
33 # Return the number of pages in the file test.db by looking at the file system.
35 return [expr [file size test.db] / 1024]
38 #-------------------------------------------------------------------------
39 # Test cases autovacuum-1.* work as follows:
41 # 1. A table with a single indexed field is created.
42 # 2. Approximately 20 rows are inserted into the table. Each row is long
43 # enough such that it uses at least 2 overflow pages for both the table
45 # 3. The rows are deleted in a psuedo-random order. Sometimes only one row
46 # is deleted per transaction, sometimes more than one.
47 # 4. After each transaction the table data is checked to ensure it is correct
48 # and a "PRAGMA integrity_check" is executed.
49 # 5. Once all the rows are deleted the file is checked to make sure it
50 # consists of exactly 4 pages.
52 # Steps 2-5 are repeated for a few different psuedo-random delete patterns
53 # (defined by the $delete_orders list).
54 set delete_orders [list]
55 lappend delete_orders {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
56 lappend delete_orders {20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1}
57 lappend delete_orders {8 18 2 4 14 11 13 3 10 7 9 5 12 17 19 15 20 6 16 1}
58 lappend delete_orders {10 3 11 17 19 20 7 4 13 6 1 14 16 12 9 18 8 15 5 2}
59 lappend delete_orders {{1 2 3 4 5 6 7 8 9 10} {11 12 13 14 15 16 17 18 19 20}}
60 lappend delete_orders {{19 8 17 15} {16 11 9 14} {18 5 3 1} {13 20 7 2} {6 12}}
62 # The length of each table entry.
66 do_test autovacuum-1.1 {
68 PRAGMA auto_vacuum = 1;
70 CREATE INDEX av1_idx ON av1(a);
75 foreach delete_order $delete_orders {
80 foreach i [lsort -integer [eval concat $delete_order]] {
81 execsql "INSERT INTO av1 (oid, a) VALUES($i, '[make_str $i $ENTRY_LEN]')"
82 lappend ::tbl_data [make_str $i $ENTRY_LEN]
85 # Make sure the integrity check passes with the initial data.
86 ifcapable {integrityck} {
87 do_test autovacuum-1.$tn.1 {
89 pragma integrity_check
94 foreach delete $delete_order {
95 # Delete one set of rows from the table.
96 do_test autovacuum-1.$tn.($delete).1 {
98 DELETE FROM av1 WHERE oid = [join $delete "OR oid = "]
102 # Do the integrity check.
103 ifcapable {integrityck} {
104 do_test autovacuum-1.$tn.($delete).2 {
106 pragma integrity_check
110 # Ensure the data remaining in the table is what was expected.
112 set idx [lsearch $::tbl_data [make_str $d $ENTRY_LEN]]
113 set ::tbl_data [lreplace $::tbl_data $idx $idx]
115 do_test autovacuum-1.$tn.($delete).3 {
122 # All rows have been deleted. Ensure the file has shrunk to 4 pages.
123 do_test autovacuum-1.$tn.3 {
128 #---------------------------------------------------------------------------
129 # Tests cases autovacuum-2.* test that root pages are allocated
130 # and deallocated correctly at the start of the file. Operation is roughly as
133 # autovacuum-2.1.*: Drop the tables that currently exist in the database.
134 # autovacuum-2.2.*: Create some tables. Ensure that data pages can be
135 # moved correctly to make space for new root-pages.
136 # autovacuum-2.3.*: Drop one of the tables just created (not the last one),
137 # and check that one of the other tables is moved to
138 # the free root-page location.
139 # autovacuum-2.4.*: Check that a table can be created correctly when the
140 # root-page it requires is on the free-list.
141 # autovacuum-2.5.*: Check that a table with indices can be dropped. This
142 # is slightly tricky because dropping one of the
143 # indices/table btrees could move the root-page of another.
144 # The code-generation layer of SQLite overcomes this problem
145 # by dropping the btrees in descending order of root-pages.
146 # This test ensures that this actually happens.
148 do_test autovacuum-2.1.1 {
153 do_test autovacuum-2.1.2 {
157 # Create a table and put some data in it.
158 do_test autovacuum-2.2.1 {
161 SELECT rootpage FROM sqlite_master ORDER BY rootpage;
164 do_test autovacuum-2.2.2 {
166 INSERT INTO av1 VALUES('[make_str abc 3000]');
167 INSERT INTO av1 VALUES('[make_str def 3000]');
168 INSERT INTO av1 VALUES('[make_str ghi 3000]');
169 INSERT INTO av1 VALUES('[make_str jkl 3000]');
171 set ::av1_data [db eval {select * from av1}]
175 # Create another table. Check it is located immediately after the first.
176 # This test case moves the second page in an over-flow chain.
177 do_test autovacuum-2.2.3 {
180 SELECT rootpage FROM sqlite_master ORDER BY rootpage;
183 do_test autovacuum-2.2.4 {
187 # Create another table. Check it is located immediately after the second.
188 # This test case moves the first page in an over-flow chain.
189 do_test autovacuum-2.2.5 {
192 SELECT rootpage FROM sqlite_master ORDER BY rootpage;
195 do_test autovacuum-2.2.6 {
199 # Create another table. Check it is located immediately after the second.
200 # This test case moves a btree leaf page.
201 do_test autovacuum-2.2.7 {
204 SELECT rootpage FROM sqlite_master ORDER BY rootpage;
207 do_test autovacuum-2.2.8 {
210 do_test autovacuum-2.2.9 {
216 do_test autovacuum-2.3.1 {
218 INSERT INTO av2 SELECT 'av1' || x FROM av1;
219 INSERT INTO av3 SELECT 'av2' || x FROM av1;
220 INSERT INTO av4 SELECT 'av3' || x FROM av1;
222 set ::av2_data [execsql {select x from av2}]
223 set ::av3_data [execsql {select x from av3}]
224 set ::av4_data [execsql {select x from av4}]
227 do_test autovacuum-2.3.2 {
230 SELECT rootpage FROM sqlite_master ORDER BY rootpage;
233 do_test autovacuum-2.3.3 {
236 do_test autovacuum-2.3.4 {
241 do_test autovacuum-2.3.5 {
247 # Drop all the tables in the file. This puts all pages except the first 2
248 # (the sqlite_master root-page and the first pointer map page) on the
250 do_test autovacuum-2.4.1 {
259 do_test autovacuum-2.4.2 {
260 for {set i 3} {$i<=10} {incr i} {
261 execsql "CREATE TABLE av$i (x)"
265 do_test autovacuum-2.4.3 {
267 SELECT rootpage FROM sqlite_master ORDER by rootpage
271 # Right now there are 5 free pages in the database. Consume and then free
272 # a 520 pages. Then create 520 tables. This ensures that at least some of the
273 # desired root-pages reside on the second free-list trunk page, and that the
274 # trunk itself is required at some point.
275 do_test autovacuum-2.4.4 {
277 INSERT INTO av3 VALUES ('[make_str abcde [expr 1020*520 + 500]]');
281 set root_page_list [list]
282 for {set i 3} {$i<=532} {incr i} {
283 # 207 and 412 are pointer-map pages.
284 if { $i!=207 && $i!=412 } {
285 lappend root_page_list $i
288 do_test autovacuum-2.4.5 {
289 for {set i 11} {$i<=530} {incr i} {
290 execsql "CREATE TABLE av$i (x)"
293 SELECT rootpage FROM sqlite_master ORDER by rootpage
297 # Just for fun, delete all those tables and see if the database is 1 page.
298 do_test autovacuum-2.4.6 {
302 integrity_check autovacuum-2.4.6
303 do_test autovacuum-2.4.7 {
305 for {set i 3} {$i<=530} {incr i} {
306 execsql "DROP TABLE av$i"
312 # Create some tables with indices to drop.
313 do_test autovacuum-2.5.1 {
315 CREATE TABLE av1(a PRIMARY KEY, b, c);
316 INSERT INTO av1 VALUES('av1 a', 'av1 b', 'av1 c');
318 CREATE TABLE av2(a PRIMARY KEY, b, c);
319 CREATE INDEX av2_i1 ON av2(b);
320 CREATE INDEX av2_i2 ON av2(c);
321 INSERT INTO av2 VALUES('av2 a', 'av2 b', 'av2 c');
323 CREATE TABLE av3(a PRIMARY KEY, b, c);
324 CREATE INDEX av3_i1 ON av3(b);
325 INSERT INTO av3 VALUES('av3 a', 'av3 b', 'av3 c');
327 CREATE TABLE av4(a, b, c);
328 CREATE INDEX av4_i1 ON av4(a);
329 CREATE INDEX av4_i2 ON av4(b);
330 CREATE INDEX av4_i3 ON av4(c);
331 CREATE INDEX av4_i4 ON av4(a, b, c);
332 INSERT INTO av4 VALUES('av4 a', 'av4 b', 'av4 c');
336 do_test autovacuum-2.5.2 {
338 SELECT name, rootpage FROM sqlite_master;
340 } [list av1 3 sqlite_autoindex_av1_1 4 \
341 av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
342 av3 9 sqlite_autoindex_av3_1 10 av3_i1 11 \
343 av4 12 av4_i1 13 av4_i2 14 av4_i3 15 av4_i4 16 \
346 # The following 4 tests are SELECT queries that use the indices created.
347 # If the root-pages in the internal schema are not updated correctly when
348 # a table or indice is moved, these queries will fail. They are repeated
349 # after each table is dropped (i.e. as test cases 2.5.*.[1..4]).
350 do_test autovacuum-2.5.2.1 {
352 SELECT * FROM av1 WHERE a = 'av1 a';
354 } {{av1 a} {av1 b} {av1 c}}
355 do_test autovacuum-2.5.2.2 {
357 SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
359 } {{av2 a} {av2 b} {av2 c}}
360 do_test autovacuum-2.5.2.3 {
362 SELECT * FROM av3 WHERE a = 'av3 a' AND b = 'av3 b';
364 } {{av3 a} {av3 b} {av3 c}}
365 do_test autovacuum-2.5.2.4 {
367 SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
369 } {{av4 a} {av4 b} {av4 c}}
371 # Drop table av3. Indices av4_i2, av4_i3 and av4_i4 are moved to fill the two
372 # root pages vacated. The operation proceeds as:
373 # Step 1: Delete av3_i1 (root-page 11). Move root-page of av4_i4 to page 11.
374 # Step 2: Delete av3 (root-page 10). Move root-page of av4_i3 to page 10.
375 # Step 3: Delete sqlite_autoindex_av1_3 (root-page 9). Move av4_i2 to page 9.
376 do_test autovacuum-2.5.3 {
379 SELECT name, rootpage FROM sqlite_master;
381 } [list av1 3 sqlite_autoindex_av1_1 4 \
382 av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
383 av4 12 av4_i1 13 av4_i2 9 av4_i3 10 av4_i4 11 \
385 do_test autovacuum-2.5.3.1 {
387 SELECT * FROM av1 WHERE a = 'av1 a';
389 } {{av1 a} {av1 b} {av1 c}}
390 do_test autovacuum-2.5.3.2 {
392 SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
394 } {{av2 a} {av2 b} {av2 c}}
395 do_test autovacuum-2.5.3.3 {
397 SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
399 } {{av4 a} {av4 b} {av4 c}}
402 # Step 1: Delete av1 (root page 4). Root-page of av4_i1 fills the gap.
403 # Step 2: Delete sqlite_autoindex_av1_1 (root page 3). Move av4 to the gap.
404 do_test autovacuum-2.5.4 {
407 SELECT name, rootpage FROM sqlite_master;
409 } [list av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
410 av4 3 av4_i1 4 av4_i2 9 av4_i3 10 av4_i4 11 \
412 do_test autovacuum-2.5.4.2 {
414 SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
416 } {{av2 a} {av2 b} {av2 c}}
417 do_test autovacuum-2.5.4.4 {
419 SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
421 } {{av4 a} {av4 b} {av4 c}}
424 # Step 1: Delete av4_i4.
425 # Step 2: Delete av4_i3.
426 # Step 3: Delete av4_i2.
427 # Step 4: Delete av4_i1. av2_i2 replaces it.
428 # Step 5: Delete av4. av2_i1 replaces it.
429 do_test autovacuum-2.5.5 {
432 SELECT name, rootpage FROM sqlite_master;
434 } [list av2 5 sqlite_autoindex_av2_1 6 av2_i1 3 av2_i2 4]
435 do_test autovacuum-2.5.5.2 {
437 SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
439 } {{av2 a} {av2 b} {av2 c}}
441 #--------------------------------------------------------------------------
442 # Test cases autovacuum-3.* test the operation of the "PRAGMA auto_vacuum"
445 do_test autovacuum-3.1 {
450 do_test autovacuum-3.2 {
457 do_test autovacuum-3.3 {
459 PRAGMA auto_vacuum = 0;
464 do_test autovacuum-3.4 {
466 file delete -force test.db
472 do_test autovacuum-3.5 {
478 do_test autovacuum-3.6 {
480 PRAGMA auto_vacuum = 1;
484 do_test autovacuum-3.7 {
489 } [expr $AUTOVACUUM?1:2]
491 #-----------------------------------------------------------------------
492 # Test that if a statement transaction around a CREATE INDEX statement is
493 # rolled back no corruption occurs.
495 do_test autovacuum-4.1 {
497 CREATE TABLE av1(a, b);
500 for {set i 0} {$i<100} {incr i} {
501 execsql "INSERT INTO av1 VALUES($i, '[string repeat X 200]');"
503 execsql "INSERT INTO av1 VALUES(99, '[string repeat X 200]');"
505 SELECT sum(a) FROM av1;
508 do_test autovacuum-4.2 {
510 CREATE UNIQUE INDEX av1_i ON av1(a);
512 } {1 {indexed columns are not unique}}
513 do_test autovacuum-4.3 {
515 SELECT sum(a) FROM av1;
518 do_test autovacuum-4.4 {