xref: /sqlite-3.40.0/test/e_vacuum.test (revision f221e4b5)
15f90f52aSdan# 2010 September 24
25f90f52aSdan#
35f90f52aSdan# The author disclaims copyright to this source code.  In place of
45f90f52aSdan# a legal notice, here is a blessing:
55f90f52aSdan#
65f90f52aSdan#    May you do good and not evil.
75f90f52aSdan#    May you find forgiveness for yourself and forgive others.
85f90f52aSdan#    May you share freely, never taking more than you give.
95f90f52aSdan#
105f90f52aSdan#***********************************************************************
115f90f52aSdan#
125f90f52aSdan# This file implements tests to verify that the "testable statements" in
135f90f52aSdan# the lang_vacuum.html document are correct.
145f90f52aSdan#
155f90f52aSdan
165f90f52aSdanset testdir [file dirname $argv0]
175f90f52aSdansource $testdir/tester.tcl
185f90f52aSdan
195f90f52aSdansqlite3_test_control_pending_byte 0x1000000
205f90f52aSdan
215f90f52aSdanproc create_db {{sql ""}} {
225f90f52aSdan  catch { db close }
235f90f52aSdan  forcedelete test.db
245f90f52aSdan  sqlite3 db test.db
255f90f52aSdan
265f90f52aSdan  db transaction {
275f90f52aSdan    execsql { PRAGMA page_size = 1024; }
285f90f52aSdan    execsql $sql
295f90f52aSdan    execsql {
305f90f52aSdan      CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
315f90f52aSdan      INSERT INTO t1 VALUES(1, randomblob(400));
325f90f52aSdan      INSERT INTO t1 SELECT a+1,  randomblob(400) FROM t1;
335f90f52aSdan      INSERT INTO t1 SELECT a+2,  randomblob(400) FROM t1;
345f90f52aSdan      INSERT INTO t1 SELECT a+4,  randomblob(400) FROM t1;
355f90f52aSdan      INSERT INTO t1 SELECT a+8,  randomblob(400) FROM t1;
365f90f52aSdan      INSERT INTO t1 SELECT a+16, randomblob(400) FROM t1;
375f90f52aSdan      INSERT INTO t1 SELECT a+32, randomblob(400) FROM t1;
385f90f52aSdan      INSERT INTO t1 SELECT a+64, randomblob(400) FROM t1;
395f90f52aSdan
405f90f52aSdan      CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
415f90f52aSdan      INSERT INTO t2 SELECT * FROM t1;
425f90f52aSdan    }
435f90f52aSdan  }
445f90f52aSdan
455f90f52aSdan  return [expr {[file size test.db] / 1024}]
465f90f52aSdan}
475f90f52aSdan
485f90f52aSdan# This proc returns the number of contiguous blocks of pages that make up
495f90f52aSdan# the table or index named by the only argument. For example, if the table
505f90f52aSdan# occupies database pages 3, 4, 8 and 9, then this command returns 2 (there
515f90f52aSdan# are 2 fragments - one consisting of pages 3 and 4, the other of fragments
525f90f52aSdan# 8 and 9).
535f90f52aSdan#
545f90f52aSdanproc fragment_count {name} {
555f90f52aSdan  execsql { CREATE VIRTUAL TABLE temp.stat USING dbstat }
565f90f52aSdan  set nFrag 1
575f90f52aSdan  db eval {SELECT pageno FROM stat WHERE name = 't1' ORDER BY pageno} {
585f90f52aSdan    if {[info exists prevpageno] && $prevpageno != $pageno-1} {
595f90f52aSdan      incr nFrag
605f90f52aSdan    }
615f90f52aSdan    set prevpageno $pageno
625f90f52aSdan  }
635f90f52aSdan  execsql { DROP TABLE temp.stat }
645f90f52aSdan  set nFrag
655f90f52aSdan}
665f90f52aSdan
675f90f52aSdan
6839759747Sdrh# -- syntax diagram vacuum-stmt
695f90f52aSdan#
705f90f52aSdando_execsql_test e_vacuum-0.1 { VACUUM } {}
715f90f52aSdan
725f90f52aSdan# EVIDENCE-OF: R-51469-36013 Unless SQLite is running in
735f90f52aSdan# "auto_vacuum=FULL" mode, when a large amount of data is deleted from
745f90f52aSdan# the database file it leaves behind empty space, or "free" database
755f90f52aSdan# pages.
765f90f52aSdan#
775f90f52aSdan# EVIDENCE-OF: R-60541-63059 Running VACUUM to rebuild the database
785f90f52aSdan# reclaims this space and reduces the size of the database file.
795f90f52aSdan#
805f90f52aSdanforeach {tn avmode sz} {
815f90f52aSdan  1 none        7
825f90f52aSdan  2 full        8
835f90f52aSdan  3 incremental 8
845f90f52aSdan} {
855f90f52aSdan  set nPage [create_db "PRAGMA auto_vacuum = $avmode"]
865f90f52aSdan
875f90f52aSdan  do_execsql_test e_vacuum-1.1.$tn.1 {
885f90f52aSdan    DELETE FROM t1;
895f90f52aSdan    DELETE FROM t2;
905f90f52aSdan  } {}
915f90f52aSdan
925f90f52aSdan  if {$avmode == "full"} {
935f90f52aSdan    # This branch tests the "unless ... auto_vacuum=FULL" in the requirement
945f90f52aSdan    # above. If auto_vacuum is set to FULL, then no empty space is left in
955f90f52aSdan    # the database file.
965f90f52aSdan    do_execsql_test e_vacuum-1.1.$tn.2 {PRAGMA freelist_count} 0
975f90f52aSdan  } else {
985f90f52aSdan    set freelist [expr {$nPage - $sz}]
995f90f52aSdan    if {$avmode == "incremental"} {
1005f90f52aSdan      # The page size is 1024 bytes. Therefore, assuming the database contains
1015f90f52aSdan      # somewhere between 207 and 411 pages (it does), there are 2 pointer-map
1025f90f52aSdan      # pages.
1035f90f52aSdan      incr freelist -2
1045f90f52aSdan    }
1055f90f52aSdan    do_execsql_test e_vacuum-1.1.$tn.3 {PRAGMA freelist_count} $freelist
1065f90f52aSdan    do_execsql_test e_vacuum-1.1.$tn.4 {VACUUM} {}
1075f90f52aSdan  }
1085f90f52aSdan
1095f90f52aSdan  do_test e_vacuum-1.1.$tn.5 { expr {[file size test.db] / 1024} } $sz
1105f90f52aSdan}
1115f90f52aSdan
1125f90f52aSdan# EVIDENCE-OF: R-50943-18433 Frequent inserts, updates, and deletes can
1135f90f52aSdan# cause the database file to become fragmented - where data for a single
1145f90f52aSdan# table or index is scattered around the database file.
1155f90f52aSdan#
1165f90f52aSdan# EVIDENCE-OF: R-05791-54928 Running VACUUM ensures that each table and
1175f90f52aSdan# index is largely stored contiguously within the database file.
1185f90f52aSdan#
1195f90f52aSdan#   e_vacuum-1.2.1 - Perform many INSERT, UPDATE and DELETE ops on table t1.
1205f90f52aSdan#   e_vacuum-1.2.2 - Verify that t1 and its indexes are now quite fragmented.
1215f90f52aSdan#   e_vacuum-1.2.3 - Run VACUUM.
1225f90f52aSdan#   e_vacuum-1.2.4 - Verify that t1 and its indexes are now much
1235f90f52aSdan#                    less fragmented.
1245f90f52aSdan#
1252f56da3fSdanifcapable vtab&&compound {
1265f90f52aSdan  create_db
1275f90f52aSdan  register_dbstat_vtab db
1285f90f52aSdan  do_execsql_test e_vacuum-1.2.1 {
1295f90f52aSdan    DELETE FROM t1 WHERE a%2;
1305f90f52aSdan    INSERT INTO t1 SELECT b, a FROM t2 WHERE a%2;
1315f90f52aSdan    UPDATE t1 SET b=randomblob(600) WHERE (a%2)==0;
1325f90f52aSdan  } {}
1335f90f52aSdan
1345f90f52aSdan  do_test e_vacuum-1.2.2.1 { expr [fragment_count t1]>100 } 1
1355f90f52aSdan  do_test e_vacuum-1.2.2.2 { expr [fragment_count sqlite_autoindex_t1_1]>100 } 1
1365f90f52aSdan  do_test e_vacuum-1.2.2.3 { expr [fragment_count sqlite_autoindex_t1_2]>100 } 1
1375f90f52aSdan
1385f90f52aSdan  do_execsql_test e_vacuum-1.2.3 { VACUUM } {}
1395f90f52aSdan
1405f90f52aSdan  # In practice, the tables and indexes each end up stored as two fragments -
1415f90f52aSdan  # one containing the root page and another containing all other pages.
1425f90f52aSdan  #
1435f90f52aSdan  do_test e_vacuum-1.2.4.1 { fragment_count t1 }                    2
1445f90f52aSdan  do_test e_vacuum-1.2.4.2 { fragment_count sqlite_autoindex_t1_1 } 2
1455f90f52aSdan  do_test e_vacuum-1.2.4.3 { fragment_count sqlite_autoindex_t1_2 } 2
146e2248da1Sdan}
1475f90f52aSdan
1485f90f52aSdan# EVIDENCE-OF: R-20474-44465 Normally, the database page_size and
1495f90f52aSdan# whether or not the database supports auto_vacuum must be configured
1505f90f52aSdan# before the database file is actually created.
1515f90f52aSdan#
1525f90f52aSdando_test e_vacuum-1.3.1.1 {
1535f90f52aSdan  create_db "PRAGMA page_size = 1024 ; PRAGMA auto_vacuum = FULL"
1545f90f52aSdan  execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
1555f90f52aSdan} {1024 1}
1565f90f52aSdando_test e_vacuum-1.3.1.2 {
1575f90f52aSdan  execsql { PRAGMA page_size = 2048 }
1585f90f52aSdan  execsql { PRAGMA auto_vacuum = NONE }
1595f90f52aSdan  execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
1605f90f52aSdan} {1024 1}
1615f90f52aSdan
162af3906a7Sdrhif {![nonzero_reserved_bytes]} {
1635f90f52aSdan  # EVIDENCE-OF: R-08570-19916 However, when not in write-ahead log mode,
1645f90f52aSdan  # the page_size and/or auto_vacuum properties of an existing database
1655f90f52aSdan  # may be changed by using the page_size and/or pragma auto_vacuum
1665f90f52aSdan  # pragmas and then immediately VACUUMing the database.
1675f90f52aSdan  #
1685f90f52aSdan  do_test e_vacuum-1.3.2.1 {
1695f90f52aSdan    execsql { PRAGMA journal_mode = delete }
1705f90f52aSdan    execsql { PRAGMA page_size = 2048 }
1715f90f52aSdan    execsql { PRAGMA auto_vacuum = NONE }
1725f90f52aSdan    execsql VACUUM
1735f90f52aSdan    execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
1745f90f52aSdan  } {2048 0}
1755f90f52aSdan
1765f90f52aSdan  # EVIDENCE-OF: R-48521-51450 When in write-ahead log mode, only the
1775f90f52aSdan  # auto_vacuum support property can be changed using VACUUM.
1785f90f52aSdan  #
17905accd22Sdan  if {[wal_is_capable]} {
1805f90f52aSdan    do_test e_vacuum-1.3.3.1 {
1815f90f52aSdan      execsql { PRAGMA journal_mode = wal }
1825f90f52aSdan      execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
1835f90f52aSdan    } {2048 0}
1845f90f52aSdan    do_test e_vacuum-1.3.3.2 {
1855f90f52aSdan      execsql { PRAGMA page_size = 1024 }
1865f90f52aSdan      execsql { PRAGMA auto_vacuum = FULL }
1875f90f52aSdan      execsql VACUUM
1885f90f52aSdan      execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
1895f90f52aSdan    } {2048 1}
19014a7fa90Sshaneh  }
191af3906a7Sdrh}
1925f90f52aSdan
193*f221e4b5Sdrh# EVIDENCE-OF: R-40347-36128 By default, VACUUM operates on the main
194*f221e4b5Sdrh# database.
1955f90f52aSdanforcedelete test.db2
1960f0b13adSdrhcreate_db { PRAGMA auto_vacuum = NONE }
1975f90f52aSdando_execsql_test e_vacuum-2.1.1 {
1985f90f52aSdan  ATTACH 'test.db2' AS aux;
1995f90f52aSdan  PRAGMA aux.page_size = 1024;
2005f90f52aSdan  CREATE TABLE aux.t3 AS SELECT * FROM t1;
2015f90f52aSdan  DELETE FROM t3;
2025f90f52aSdan} {}
2030f0b13adSdrhset original_size [file size test.db2]
2045f90f52aSdan
2059ef5e770Sdrh# Vacuuming the main database does not affect aux
2065f90f52aSdando_execsql_test e_vacuum-2.1.3 { VACUUM } {}
2070f0b13adSdrhdo_test e_vacuum-2.1.6 { expr {[file size test.db2]==$::original_size} } 1
2085f90f52aSdan
209fb04a36cSdrh# EVIDENCE-OF: R-36598-60500 Attached databases can be vacuumed by
210fb04a36cSdrh# appending the appropriate schema-name to the VACUUM statement.
211fb04a36cSdrhdo_execsql_test e_vacuum-2.1.7 { VACUUM aux; } {}
212fb04a36cSdrhdo_test e_vacuum-2.1.8 { expr {[file size test.db2]<$::original_size} } 1
213fb04a36cSdrh
2145f90f52aSdan# EVIDENCE-OF: R-17495-17419 The VACUUM command may change the ROWIDs of
2155f90f52aSdan# entries in any tables that do not have an explicit INTEGER PRIMARY
2165f90f52aSdan# KEY.
2175f90f52aSdan#
2185f90f52aSdan#   Tests e_vacuum-3.1.1 - 3.1.2 demonstrate that rowids can change when
2195f90f52aSdan#   a database is VACUUMed. Tests e_vacuum-3.1.3 - 3.1.4 show that adding
2205f90f52aSdan#   an INTEGER PRIMARY KEY column to a table stops this from happening.
2215f90f52aSdan#
2221e30c7f3Sdrh#   Update 2019-01-07:  Rowids are now preserved by VACUUM.
2231e30c7f3Sdrh#
2245f90f52aSdando_execsql_test e_vacuum-3.1.1 {
2255f90f52aSdan  CREATE TABLE t4(x);
2265f90f52aSdan  INSERT INTO t4(x) VALUES('x');
2275f90f52aSdan  INSERT INTO t4(x) VALUES('y');
2285f90f52aSdan  INSERT INTO t4(x) VALUES('z');
2295f90f52aSdan  DELETE FROM t4 WHERE x = 'y';
2305f90f52aSdan  SELECT rowid, x FROM t4;
2315f90f52aSdan} {1 x 3 z}
2325f90f52aSdando_execsql_test e_vacuum-3.1.2 {
2335f90f52aSdan  VACUUM;
2345f90f52aSdan  SELECT rowid, x FROM t4;
2354e61e883Sdrh} {1 x 2 z}
2365f90f52aSdan
2374e61e883Sdrh# Rowids are preserved if an INTEGER PRIMARY KEY is used
2385f90f52aSdando_execsql_test e_vacuum-3.1.3 {
2395f90f52aSdan  CREATE TABLE t5(x, y INTEGER PRIMARY KEY);
2405f90f52aSdan  INSERT INTO t5(x) VALUES('x');
2415f90f52aSdan  INSERT INTO t5(x) VALUES('y');
2425f90f52aSdan  INSERT INTO t5(x) VALUES('z');
2435f90f52aSdan  DELETE FROM t5 WHERE x = 'y';
2445f90f52aSdan  SELECT rowid, x FROM t5;
2455f90f52aSdan} {1 x 3 z}
2465f90f52aSdando_execsql_test e_vacuum-3.1.4 {
2475f90f52aSdan  VACUUM;
2485f90f52aSdan  SELECT rowid, x FROM t5;
2495f90f52aSdan} {1 x 3 z}
2505f90f52aSdan
2514e61e883Sdrh# Rowid is preserved for VACUUM INTO
2524e61e883Sdrhdo_execsql_test e_vacuum-3.1.5 {
2534e61e883Sdrh  DROP TABLE t5;
2544e61e883Sdrh  CREATE TABLE t5(x);
2554e61e883Sdrh  INSERT INTO t5(x) VALUES('x');
2564e61e883Sdrh  INSERT INTO t5(x) VALUES('y');
2574e61e883Sdrh  INSERT INTO t5(x) VALUES('z');
2584e61e883Sdrh  DELETE FROM t5 WHERE x = 'y';
2594e61e883Sdrh  SELECT rowid, x FROM t5;
2604e61e883Sdrh} {1 x 3 z}
2614e61e883Sdrhforcedelete test2.db
2624e61e883Sdrhdo_execsql_test e_vacuum-3.1.6 {
2634e61e883Sdrh  VACUUM INTO 'test2.db';
2644e61e883Sdrh  ATTACH 'test2.db' AS aux1;
2654e61e883Sdrh  SELECT rowid, x FROM aux1.t5;
2664e61e883Sdrh  DETACH aux1;
2674e61e883Sdrh} {1 x 3 z}
2684e61e883Sdrh
269eb80863cSdrh# Rowids are not renumbered if the table being vacuumed
2704e61e883Sdrh# has indexes.
2714e61e883Sdrhdo_execsql_test e_vacuum-3.1.7 {
2724e61e883Sdrh  DROP TABLE t5;
2734e61e883Sdrh  CREATE TABLE t5(x,y,z);
2744e61e883Sdrh  INSERT INTO t5(x) VALUES('x');
2754e61e883Sdrh  INSERT INTO t5(x) VALUES('y');
2764e61e883Sdrh  INSERT INTO t5(x) VALUES('z');
2774e61e883Sdrh  UPDATE t5 SET y=x, z=random();
2784e61e883Sdrh  DELETE FROM t5 WHERE x = 'y';
2794e61e883Sdrh  CREATE INDEX t5x ON t5(x);
2804e61e883Sdrh  CREATE UNIQUE INDEX t5y ON t5(y);
2814e61e883Sdrh  CREATE INDEX t5zxy ON t5(z,x,y);
2824e61e883Sdrh  SELECT rowid, x FROM t5;
2834e61e883Sdrh} {1 x 3 z}
2844e61e883Sdrhdo_execsql_test e_vacuum-3.1.8 {
2854e61e883Sdrh  VACUUM;
2864e61e883Sdrh  SELECT rowid, x FROM t5;
2874e61e883Sdrh} {1 x 3 z}
2884e61e883Sdrh
289eacc8816Sdrh# EVIDENCE-OF: R-12218-18073 A VACUUM will fail if there is an open
290eacc8816Sdrh# transaction on the database connection that is attempting to run the
291eacc8816Sdrh# VACUUM.
2925f90f52aSdan#
2935f90f52aSdando_execsql_test  e_vacuum-3.2.1.1 { BEGIN } {}
2945f90f52aSdando_catchsql_test e_vacuum-3.2.1.2 {
2955f90f52aSdan  VACUUM
2965f90f52aSdan} {1 {cannot VACUUM from within a transaction}}
2975f90f52aSdando_execsql_test  e_vacuum-3.2.1.3 { COMMIT } {}
2985f90f52aSdando_execsql_test  e_vacuum-3.2.1.4 { VACUUM } {}
2995f90f52aSdando_execsql_test  e_vacuum-3.2.1.5 { SAVEPOINT x } {}
3005f90f52aSdando_catchsql_test e_vacuum-3.2.1.6 {
3015f90f52aSdan  VACUUM
3025f90f52aSdan} {1 {cannot VACUUM from within a transaction}}
3035f90f52aSdando_execsql_test  e_vacuum-3.2.1.7 { COMMIT } {}
3045f90f52aSdando_execsql_test  e_vacuum-3.2.1.8 { VACUUM } {}
3055f90f52aSdan
3065f90f52aSdancreate_db
3075f90f52aSdando_test e_vacuum-3.2.2.1 {
3085f90f52aSdan  set res ""
3095f90f52aSdan  db eval { SELECT a FROM t1 } {
3105f90f52aSdan    if {$a == 10} { set res [catchsql VACUUM] }
3115f90f52aSdan  }
3125f90f52aSdan  set res
3135f90f52aSdan} {1 {cannot VACUUM - SQL statements in progress}}
3145f90f52aSdan
3155f90f52aSdan
316fb04a36cSdrh# EVIDENCE-OF: R-55138-13241 An alternative to using the VACUUM command
317fb04a36cSdrh# to reclaim space after data has been deleted is auto-vacuum mode,
318fb04a36cSdrh# enabled using the auto_vacuum pragma.
3195f90f52aSdan#
3205f90f52aSdando_test e_vacuum-3.3.1 {
3215f90f52aSdan  create_db { PRAGMA auto_vacuum = FULL }
3225f90f52aSdan  execsql { PRAGMA auto_vacuum }
3235f90f52aSdan} {1}
3245f90f52aSdan
3255f90f52aSdan# EVIDENCE-OF: R-64844-34873 When auto_vacuum is enabled for a database
3265f90f52aSdan# free pages may be reclaimed after deleting data, causing the file to
3275f90f52aSdan# shrink, without rebuilding the entire database using VACUUM.
3285f90f52aSdan#
3295f90f52aSdando_test e_vacuum-3.3.2.1 {
3305f90f52aSdan  create_db { PRAGMA auto_vacuum = FULL }
3315f90f52aSdan  execsql {
3325f90f52aSdan    DELETE FROM t1;
3335f90f52aSdan    DELETE FROM t2;
3345f90f52aSdan  }
3355f90f52aSdan  expr {[file size test.db] / 1024}
3365f90f52aSdan} {8}
3375f90f52aSdando_test e_vacuum-3.3.2.2 {
3385f90f52aSdan  create_db { PRAGMA auto_vacuum = INCREMENTAL }
3395f90f52aSdan  execsql {
3405f90f52aSdan    DELETE FROM t1;
3415f90f52aSdan    DELETE FROM t2;
3425f90f52aSdan    PRAGMA incremental_vacuum;
3435f90f52aSdan  }
3445f90f52aSdan  expr {[file size test.db] / 1024}
3455f90f52aSdan} {8}
3465f90f52aSdan
3475f90f52aSdanfinish_test
348