1
0

e_vacuum.test 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # 2010 September 24
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. #
  12. # This file implements tests to verify that the "testable statements" in
  13. # the lang_vacuum.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. sqlite3_test_control_pending_byte 0x1000000
  18. proc create_db {{sql ""}} {
  19. catch { db close }
  20. forcedelete test.db
  21. sqlite3 db test.db
  22. db transaction {
  23. execsql { PRAGMA page_size = 1024; }
  24. execsql $sql
  25. execsql {
  26. CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
  27. INSERT INTO t1 VALUES(1, randomblob(400));
  28. INSERT INTO t1 SELECT a+1, randomblob(400) FROM t1;
  29. INSERT INTO t1 SELECT a+2, randomblob(400) FROM t1;
  30. INSERT INTO t1 SELECT a+4, randomblob(400) FROM t1;
  31. INSERT INTO t1 SELECT a+8, randomblob(400) FROM t1;
  32. INSERT INTO t1 SELECT a+16, randomblob(400) FROM t1;
  33. INSERT INTO t1 SELECT a+32, randomblob(400) FROM t1;
  34. INSERT INTO t1 SELECT a+64, randomblob(400) FROM t1;
  35. CREATE TABLE t2(a PRIMARY KEY, b UNIQUE);
  36. INSERT INTO t2 SELECT * FROM t1;
  37. }
  38. }
  39. return [expr {[file size test.db] / 1024}]
  40. }
  41. # This proc returns the number of contiguous blocks of pages that make up
  42. # the table or index named by the only argument. For example, if the table
  43. # occupies database pages 3, 4, 8 and 9, then this command returns 2 (there
  44. # are 2 fragments - one consisting of pages 3 and 4, the other of fragments
  45. # 8 and 9).
  46. #
  47. proc fragment_count {name} {
  48. execsql { CREATE VIRTUAL TABLE temp.stat USING dbstat }
  49. set nFrag 1
  50. db eval {SELECT pageno FROM stat WHERE name = 't1' ORDER BY pageno} {
  51. if {[info exists prevpageno] && $prevpageno != $pageno-1} {
  52. incr nFrag
  53. }
  54. set prevpageno $pageno
  55. }
  56. execsql { DROP TABLE temp.stat }
  57. set nFrag
  58. }
  59. # -- syntax diagram vacuum-stmt
  60. #
  61. do_execsql_test e_vacuum-0.1 { VACUUM } {}
  62. # EVIDENCE-OF: R-51469-36013 Unless SQLite is running in
  63. # "auto_vacuum=FULL" mode, when a large amount of data is deleted from
  64. # the database file it leaves behind empty space, or "free" database
  65. # pages.
  66. #
  67. # EVIDENCE-OF: R-60541-63059 Running VACUUM to rebuild the database
  68. # reclaims this space and reduces the size of the database file.
  69. #
  70. foreach {tn avmode sz} {
  71. 1 none 7
  72. 2 full 8
  73. 3 incremental 8
  74. } {
  75. set nPage [create_db "PRAGMA auto_vacuum = $avmode"]
  76. do_execsql_test e_vacuum-1.1.$tn.1 {
  77. DELETE FROM t1;
  78. DELETE FROM t2;
  79. } {}
  80. if {$avmode == "full"} {
  81. # This branch tests the "unless ... auto_vacuum=FULL" in the requirement
  82. # above. If auto_vacuum is set to FULL, then no empty space is left in
  83. # the database file.
  84. do_execsql_test e_vacuum-1.1.$tn.2 {PRAGMA freelist_count} 0
  85. } else {
  86. set freelist [expr {$nPage - $sz}]
  87. if {$avmode == "incremental"} {
  88. # The page size is 1024 bytes. Therefore, assuming the database contains
  89. # somewhere between 207 and 411 pages (it does), there are 2 pointer-map
  90. # pages.
  91. incr freelist -2
  92. }
  93. do_execsql_test e_vacuum-1.1.$tn.3 {PRAGMA freelist_count} $freelist
  94. do_execsql_test e_vacuum-1.1.$tn.4 {VACUUM} {}
  95. }
  96. do_test e_vacuum-1.1.$tn.5 { expr {[file size test.db] / 1024} } $sz
  97. }
  98. # EVIDENCE-OF: R-50943-18433 Frequent inserts, updates, and deletes can
  99. # cause the database file to become fragmented - where data for a single
  100. # table or index is scattered around the database file.
  101. #
  102. # EVIDENCE-OF: R-05791-54928 Running VACUUM ensures that each table and
  103. # index is largely stored contiguously within the database file.
  104. #
  105. # e_vacuum-1.2.1 - Perform many INSERT, UPDATE and DELETE ops on table t1.
  106. # e_vacuum-1.2.2 - Verify that t1 and its indexes are now quite fragmented.
  107. # e_vacuum-1.2.3 - Run VACUUM.
  108. # e_vacuum-1.2.4 - Verify that t1 and its indexes are now much
  109. # less fragmented.
  110. #
  111. ifcapable vtab&&compound {
  112. create_db
  113. register_dbstat_vtab db
  114. do_execsql_test e_vacuum-1.2.1 {
  115. DELETE FROM t1 WHERE a%2;
  116. INSERT INTO t1 SELECT b, a FROM t2 WHERE a%2;
  117. UPDATE t1 SET b=randomblob(600) WHERE (a%2)==0;
  118. } {}
  119. do_test e_vacuum-1.2.2.1 { expr [fragment_count t1]>100 } 1
  120. do_test e_vacuum-1.2.2.2 { expr [fragment_count sqlite_autoindex_t1_1]>100 } 1
  121. do_test e_vacuum-1.2.2.3 { expr [fragment_count sqlite_autoindex_t1_2]>100 } 1
  122. do_execsql_test e_vacuum-1.2.3 { VACUUM } {}
  123. # In practice, the tables and indexes each end up stored as two fragments -
  124. # one containing the root page and another containing all other pages.
  125. #
  126. do_test e_vacuum-1.2.4.1 { fragment_count t1 } 2
  127. do_test e_vacuum-1.2.4.2 { fragment_count sqlite_autoindex_t1_1 } 2
  128. do_test e_vacuum-1.2.4.3 { fragment_count sqlite_autoindex_t1_2 } 2
  129. }
  130. # EVIDENCE-OF: R-20474-44465 Normally, the database page_size and
  131. # whether or not the database supports auto_vacuum must be configured
  132. # before the database file is actually created.
  133. #
  134. do_test e_vacuum-1.3.1.1 {
  135. create_db "PRAGMA page_size = 1024 ; PRAGMA auto_vacuum = FULL"
  136. execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
  137. } {1024 1}
  138. do_test e_vacuum-1.3.1.2 {
  139. execsql { PRAGMA page_size = 2048 }
  140. execsql { PRAGMA auto_vacuum = NONE }
  141. execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
  142. } {1024 1}
  143. # EVIDENCE-OF: R-08570-19916 However, when not in write-ahead log mode,
  144. # the page_size and/or auto_vacuum properties of an existing database
  145. # may be changed by using the page_size and/or pragma auto_vacuum
  146. # pragmas and then immediately VACUUMing the database.
  147. #
  148. do_test e_vacuum-1.3.2.1 {
  149. execsql { PRAGMA journal_mode = delete }
  150. execsql { PRAGMA page_size = 2048 }
  151. execsql { PRAGMA auto_vacuum = NONE }
  152. execsql VACUUM
  153. execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
  154. } {2048 0}
  155. # EVIDENCE-OF: R-48521-51450 When in write-ahead log mode, only the
  156. # auto_vacuum support property can be changed using VACUUM.
  157. #
  158. ifcapable wal {
  159. do_test e_vacuum-1.3.3.1 {
  160. execsql { PRAGMA journal_mode = wal }
  161. execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
  162. } {2048 0}
  163. do_test e_vacuum-1.3.3.2 {
  164. execsql { PRAGMA page_size = 1024 }
  165. execsql { PRAGMA auto_vacuum = FULL }
  166. execsql VACUUM
  167. execsql { PRAGMA page_size ; PRAGMA auto_vacuum }
  168. } {2048 1}
  169. }
  170. # EVIDENCE-OF: R-38001-03952 VACUUM only works on the main database. It
  171. # is not possible to VACUUM an attached database file.
  172. forcedelete test.db2
  173. create_db { PRAGMA auto_vacuum = NONE }
  174. do_execsql_test e_vacuum-2.1.1 {
  175. ATTACH 'test.db2' AS aux;
  176. PRAGMA aux.page_size = 1024;
  177. CREATE TABLE aux.t3 AS SELECT * FROM t1;
  178. DELETE FROM t3;
  179. } {}
  180. set original_size [file size test.db2]
  181. # Try everything we can think of to get the aux database vacuumed:
  182. do_execsql_test e_vacuum-2.1.3 { VACUUM } {}
  183. do_execsql_test e_vacuum-2.1.4 { VACUUM aux } {}
  184. do_execsql_test e_vacuum-2.1.5 { VACUUM 'test.db2' } {}
  185. # Despite our efforts, space in the aux database has not been reclaimed:
  186. do_test e_vacuum-2.1.6 { expr {[file size test.db2]==$::original_size} } 1
  187. # EVIDENCE-OF: R-17495-17419 The VACUUM command may change the ROWIDs of
  188. # entries in any tables that do not have an explicit INTEGER PRIMARY
  189. # KEY.
  190. #
  191. # Tests e_vacuum-3.1.1 - 3.1.2 demonstrate that rowids can change when
  192. # a database is VACUUMed. Tests e_vacuum-3.1.3 - 3.1.4 show that adding
  193. # an INTEGER PRIMARY KEY column to a table stops this from happening.
  194. #
  195. do_execsql_test e_vacuum-3.1.1 {
  196. CREATE TABLE t4(x);
  197. INSERT INTO t4(x) VALUES('x');
  198. INSERT INTO t4(x) VALUES('y');
  199. INSERT INTO t4(x) VALUES('z');
  200. DELETE FROM t4 WHERE x = 'y';
  201. SELECT rowid, x FROM t4;
  202. } {1 x 3 z}
  203. do_execsql_test e_vacuum-3.1.2 {
  204. VACUUM;
  205. SELECT rowid, x FROM t4;
  206. } {1 x 2 z}
  207. do_execsql_test e_vacuum-3.1.3 {
  208. CREATE TABLE t5(x, y INTEGER PRIMARY KEY);
  209. INSERT INTO t5(x) VALUES('x');
  210. INSERT INTO t5(x) VALUES('y');
  211. INSERT INTO t5(x) VALUES('z');
  212. DELETE FROM t5 WHERE x = 'y';
  213. SELECT rowid, x FROM t5;
  214. } {1 x 3 z}
  215. do_execsql_test e_vacuum-3.1.4 {
  216. VACUUM;
  217. SELECT rowid, x FROM t5;
  218. } {1 x 3 z}
  219. # EVIDENCE-OF: R-49563-33883 A VACUUM will fail if there is an open
  220. # transaction, or if there are one or more active SQL statements when it
  221. # is run.
  222. #
  223. do_execsql_test e_vacuum-3.2.1.1 { BEGIN } {}
  224. do_catchsql_test e_vacuum-3.2.1.2 {
  225. VACUUM
  226. } {1 {cannot VACUUM from within a transaction}}
  227. do_execsql_test e_vacuum-3.2.1.3 { COMMIT } {}
  228. do_execsql_test e_vacuum-3.2.1.4 { VACUUM } {}
  229. do_execsql_test e_vacuum-3.2.1.5 { SAVEPOINT x } {}
  230. do_catchsql_test e_vacuum-3.2.1.6 {
  231. VACUUM
  232. } {1 {cannot VACUUM from within a transaction}}
  233. do_execsql_test e_vacuum-3.2.1.7 { COMMIT } {}
  234. do_execsql_test e_vacuum-3.2.1.8 { VACUUM } {}
  235. create_db
  236. do_test e_vacuum-3.2.2.1 {
  237. set res ""
  238. db eval { SELECT a FROM t1 } {
  239. if {$a == 10} { set res [catchsql VACUUM] }
  240. }
  241. set res
  242. } {1 {cannot VACUUM - SQL statements in progress}}
  243. # EVIDENCE-OF: R-38735-12540 As of SQLite version 3.1, an alternative to
  244. # using the VACUUM command to reclaim space after data has been deleted
  245. # is auto-vacuum mode, enabled using the auto_vacuum pragma.
  246. #
  247. do_test e_vacuum-3.3.1 {
  248. create_db { PRAGMA auto_vacuum = FULL }
  249. execsql { PRAGMA auto_vacuum }
  250. } {1}
  251. # EVIDENCE-OF: R-64844-34873 When auto_vacuum is enabled for a database
  252. # free pages may be reclaimed after deleting data, causing the file to
  253. # shrink, without rebuilding the entire database using VACUUM.
  254. #
  255. do_test e_vacuum-3.3.2.1 {
  256. create_db { PRAGMA auto_vacuum = FULL }
  257. execsql {
  258. DELETE FROM t1;
  259. DELETE FROM t2;
  260. }
  261. expr {[file size test.db] / 1024}
  262. } {8}
  263. do_test e_vacuum-3.3.2.2 {
  264. create_db { PRAGMA auto_vacuum = INCREMENTAL }
  265. execsql {
  266. DELETE FROM t1;
  267. DELETE FROM t2;
  268. PRAGMA incremental_vacuum;
  269. }
  270. expr {[file size test.db] / 1024}
  271. } {8}
  272. finish_test