wal.test 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. # 2010 April 13
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the operation of the library in
  13. # "PRAGMA journal_mode=WAL" mode.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/lock_common.tcl
  18. source $testdir/malloc_common.tcl
  19. source $testdir/wal_common.tcl
  20. set testprefix wal
  21. ifcapable !wal {finish_test ; return }
  22. proc reopen_db {} {
  23. catch { db close }
  24. forcedelete test.db test.db-wal test.db-wal-summary
  25. sqlite3_wal db test.db
  26. }
  27. set ::blobcnt 0
  28. proc blob {nByte} {
  29. incr ::blobcnt
  30. return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte]
  31. }
  32. proc sqlite3_wal {args} {
  33. eval sqlite3 $args
  34. [lindex $args 0] eval { PRAGMA auto_vacuum = 0 }
  35. [lindex $args 0] eval { PRAGMA page_size = 1024 }
  36. [lindex $args 0] eval { PRAGMA journal_mode = wal }
  37. [lindex $args 0] eval { PRAGMA synchronous = normal }
  38. [lindex $args 0] function blob blob
  39. }
  40. proc log_deleted {logfile} {
  41. return [expr [file exists $logfile]==0]
  42. }
  43. #
  44. # These are 'warm-body' tests used while developing the WAL code. They
  45. # serve to prove that a few really simple cases work:
  46. #
  47. # wal-1.*: Read and write the database.
  48. # wal-2.*: Test MVCC with one reader, one writer.
  49. # wal-3.*: Test transaction rollback.
  50. # wal-4.*: Test savepoint/statement rollback.
  51. # wal-5.*: Test the temp database.
  52. # wal-6.*: Test creating databases with different page sizes.
  53. #
  54. #
  55. #
  56. do_test wal-0.1 {
  57. execsql { PRAGMA auto_vacuum = 0 }
  58. execsql { PRAGMA synchronous = normal }
  59. execsql { PRAGMA journal_mode = wal }
  60. } {wal}
  61. do_test wal-0.2 {
  62. file size test.db
  63. } {1024}
  64. do_test wal-1.0 {
  65. execsql {
  66. BEGIN;
  67. CREATE TABLE t1(a, b);
  68. }
  69. list [file exists test.db-journal] \
  70. [file exists test.db-wal] \
  71. [file size test.db]
  72. } {0 1 1024}
  73. do_test wal-1.1 {
  74. execsql COMMIT
  75. list [file exists test.db-journal] [file exists test.db-wal]
  76. } {0 1}
  77. do_test wal-1.2 {
  78. # There are now two pages in the log.
  79. file size test.db-wal
  80. } [wal_file_size 2 1024]
  81. do_test wal-1.3 {
  82. execsql { SELECT * FROM sqlite_master }
  83. } {table t1 t1 2 {CREATE TABLE t1(a, b)}}
  84. do_test wal-1.4 {
  85. execsql { INSERT INTO t1 VALUES(1, 2) }
  86. execsql { INSERT INTO t1 VALUES(3, 4) }
  87. execsql { INSERT INTO t1 VALUES(5, 6) }
  88. execsql { INSERT INTO t1 VALUES(7, 8) }
  89. execsql { INSERT INTO t1 VALUES(9, 10) }
  90. } {}
  91. do_test wal-1.5 {
  92. execsql { SELECT * FROM t1 }
  93. } {1 2 3 4 5 6 7 8 9 10}
  94. do_test wal-2.1 {
  95. sqlite3_wal db2 ./test.db
  96. execsql { BEGIN; SELECT * FROM t1 } db2
  97. } {1 2 3 4 5 6 7 8 9 10}
  98. do_test wal-2.2 {
  99. execsql { INSERT INTO t1 VALUES(11, 12) }
  100. execsql { SELECT * FROM t1 }
  101. } {1 2 3 4 5 6 7 8 9 10 11 12}
  102. do_test wal-2.3 {
  103. execsql { SELECT * FROM t1 } db2
  104. } {1 2 3 4 5 6 7 8 9 10}
  105. do_test wal-2.4 {
  106. execsql { INSERT INTO t1 VALUES(13, 14) }
  107. execsql { SELECT * FROM t1 }
  108. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  109. do_test wal-2.5 {
  110. execsql { SELECT * FROM t1 } db2
  111. } {1 2 3 4 5 6 7 8 9 10}
  112. do_test wal-2.6 {
  113. execsql { COMMIT; SELECT * FROM t1 } db2
  114. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  115. do_test wal-3.1 {
  116. execsql { BEGIN; DELETE FROM t1 }
  117. execsql { SELECT * FROM t1 }
  118. } {}
  119. do_test wal-3.2 {
  120. execsql { SELECT * FROM t1 } db2
  121. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  122. do_test wal-3.3 {
  123. execsql { ROLLBACK }
  124. execsql { SELECT * FROM t1 }
  125. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  126. db2 close
  127. #-------------------------------------------------------------------------
  128. # The following tests, wal-4.*, test that savepoints work with WAL
  129. # databases.
  130. #
  131. do_test wal-4.1 {
  132. execsql {
  133. DELETE FROM t1;
  134. BEGIN;
  135. INSERT INTO t1 VALUES('a', 'b');
  136. SAVEPOINT sp;
  137. INSERT INTO t1 VALUES('c', 'd');
  138. SELECT * FROM t1;
  139. }
  140. } {a b c d}
  141. do_test wal-4.2 {
  142. execsql {
  143. ROLLBACK TO sp;
  144. SELECT * FROM t1;
  145. }
  146. } {a b}
  147. do_test wal-4.3 {
  148. execsql {
  149. COMMIT;
  150. SELECT * FROM t1;
  151. }
  152. } {a b}
  153. do_test wal-4.4.1 {
  154. db close
  155. sqlite3 db test.db
  156. db func blob blob
  157. list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
  158. } {{a b} 0}
  159. do_test wal-4.4.2 {
  160. execsql { PRAGMA cache_size = 10 }
  161. execsql {
  162. CREATE TABLE t2(a, b);
  163. INSERT INTO t2 VALUES(blob(400), blob(400));
  164. SAVEPOINT tr;
  165. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
  166. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
  167. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
  168. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
  169. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
  170. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
  171. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
  172. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
  173. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
  174. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
  175. SELECT count(*) FROM t2;
  176. }
  177. } {32}
  178. do_test wal-4.4.3 {
  179. execsql { ROLLBACK TO tr }
  180. } {}
  181. do_test wal-4.4.4 {
  182. set logsize [file size test.db-wal]
  183. execsql {
  184. INSERT INTO t1 VALUES('x', 'y');
  185. RELEASE tr;
  186. }
  187. expr { $logsize == [file size test.db-wal] }
  188. } {1}
  189. do_test wal-4.4.5 {
  190. execsql { SELECT count(*) FROM t2 }
  191. } {1}
  192. do_test wal-4.4.6 {
  193. forcecopy test.db test2.db
  194. forcecopy test.db-wal test2.db-wal
  195. sqlite3 db2 test2.db
  196. execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
  197. } {1 2}
  198. do_test wal-4.4.7 {
  199. execsql { PRAGMA integrity_check } db2
  200. } {ok}
  201. db2 close
  202. do_test wal-4.5.1 {
  203. reopen_db
  204. db func blob blob
  205. execsql {
  206. PRAGMA journal_mode = WAL;
  207. CREATE TABLE t1(a, b);
  208. INSERT INTO t1 VALUES('a', 'b');
  209. }
  210. sqlite3 db test.db
  211. db func blob blob
  212. list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
  213. } {{a b} 0}
  214. do_test wal-4.5.2 {
  215. execsql { PRAGMA cache_size = 10 }
  216. execsql {
  217. CREATE TABLE t2(a, b);
  218. BEGIN;
  219. INSERT INTO t2 VALUES(blob(400), blob(400));
  220. SAVEPOINT tr;
  221. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
  222. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
  223. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
  224. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
  225. INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
  226. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
  227. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
  228. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
  229. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
  230. INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
  231. SELECT count(*) FROM t2;
  232. }
  233. } {32}
  234. do_test wal-4.5.3 {
  235. execsql { ROLLBACK TO tr }
  236. } {}
  237. do_test wal-4.5.4 {
  238. set logsize [file size test.db-wal]
  239. execsql {
  240. INSERT INTO t1 VALUES('x', 'y');
  241. RELEASE tr;
  242. COMMIT;
  243. }
  244. expr { $logsize == [file size test.db-wal] }
  245. } {1}
  246. do_test wal-4.5.5 {
  247. execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 }
  248. } {1 2}
  249. do_test wal-4.5.6 {
  250. forcecopy test.db test2.db
  251. forcecopy test.db-wal test2.db-wal
  252. sqlite3 db2 test2.db
  253. execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
  254. } {1 2}
  255. do_test wal-4.5.7 {
  256. execsql { PRAGMA integrity_check } db2
  257. } {ok}
  258. db2 close
  259. do_test wal-4.6.1 {
  260. execsql {
  261. DELETE FROM t2;
  262. PRAGMA wal_checkpoint;
  263. BEGIN;
  264. INSERT INTO t2 VALUES('w', 'x');
  265. SAVEPOINT save;
  266. INSERT INTO t2 VALUES('y', 'z');
  267. ROLLBACK TO save;
  268. COMMIT;
  269. }
  270. execsql { SELECT * FROM t2 }
  271. } {w x}
  272. reopen_db
  273. do_test wal-5.1 {
  274. execsql {
  275. CREATE TEMP TABLE t2(a, b);
  276. INSERT INTO t2 VALUES(1, 2);
  277. }
  278. } {}
  279. do_test wal-5.2 {
  280. execsql {
  281. BEGIN;
  282. INSERT INTO t2 VALUES(3, 4);
  283. SELECT * FROM t2;
  284. }
  285. } {1 2 3 4}
  286. do_test wal-5.3 {
  287. execsql {
  288. ROLLBACK;
  289. SELECT * FROM t2;
  290. }
  291. } {1 2}
  292. do_test wal-5.4 {
  293. execsql {
  294. CREATE TEMP TABLE t3(x UNIQUE);
  295. BEGIN;
  296. INSERT INTO t2 VALUES(3, 4);
  297. INSERT INTO t3 VALUES('abc');
  298. }
  299. catchsql { INSERT INTO t3 VALUES('abc') }
  300. } {1 {column x is not unique}}
  301. do_test wal-5.5 {
  302. execsql {
  303. COMMIT;
  304. SELECT * FROM t2;
  305. }
  306. } {1 2 3 4}
  307. db close
  308. foreach sector {512 4096} {
  309. sqlite3_simulate_device -sectorsize $sector
  310. foreach pgsz {512 1024 2048 4096} {
  311. forcedelete test.db test.db-wal
  312. do_test wal-6.$sector.$pgsz.1 {
  313. sqlite3 db test.db -vfs devsym
  314. execsql "
  315. PRAGMA page_size = $pgsz;
  316. PRAGMA auto_vacuum = 0;
  317. PRAGMA journal_mode = wal;
  318. "
  319. execsql "
  320. CREATE TABLE t1(a, b);
  321. INSERT INTO t1 VALUES(1, 2);
  322. "
  323. db close
  324. file size test.db
  325. } [expr $pgsz*2]
  326. do_test wal-6.$sector.$pgsz.2 {
  327. log_deleted test.db-wal
  328. } {1}
  329. }
  330. }
  331. do_test wal-7.1 {
  332. forcedelete test.db test.db-wal
  333. sqlite3_wal db test.db
  334. execsql {
  335. PRAGMA page_size = 1024;
  336. CREATE TABLE t1(a, b);
  337. INSERT INTO t1 VALUES(1, 2);
  338. }
  339. list [file size test.db] [file size test.db-wal]
  340. } [list 1024 [wal_file_size 3 1024]]
  341. do_test wal-7.2 {
  342. execsql { PRAGMA wal_checkpoint }
  343. list [file size test.db] [file size test.db-wal]
  344. } [list 2048 [wal_file_size 3 1024]]
  345. # Execute some transactions in auto-vacuum mode to test database file
  346. # truncation.
  347. #
  348. do_test wal-8.1 {
  349. reopen_db
  350. catch { db close }
  351. forcedelete test.db test.db-wal
  352. sqlite3 db test.db
  353. db function blob blob
  354. execsql {
  355. PRAGMA auto_vacuum = 1;
  356. PRAGMA journal_mode = wal;
  357. PRAGMA auto_vacuum;
  358. }
  359. } {wal 1}
  360. do_test wal-8.2 {
  361. execsql {
  362. PRAGMA page_size = 1024;
  363. CREATE TABLE t1(x);
  364. INSERT INTO t1 VALUES(blob(900));
  365. INSERT INTO t1 VALUES(blob(900));
  366. INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
  367. INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
  368. INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
  369. INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
  370. INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
  371. PRAGMA wal_checkpoint;
  372. }
  373. file size test.db
  374. } [expr 68*1024]
  375. do_test wal-8.3 {
  376. execsql {
  377. DELETE FROM t1 WHERE rowid<54;
  378. PRAGMA wal_checkpoint;
  379. }
  380. file size test.db
  381. } [expr 14*1024]
  382. # Run some "warm-body" tests to ensure that log-summary files with more
  383. # than 256 entries (log summaries that contain index blocks) work Ok.
  384. #
  385. do_test wal-9.1 {
  386. reopen_db
  387. execsql {
  388. PRAGMA cache_size=2000;
  389. CREATE TABLE t1(x PRIMARY KEY);
  390. INSERT INTO t1 VALUES(blob(900));
  391. INSERT INTO t1 VALUES(blob(900));
  392. INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
  393. INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
  394. INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
  395. INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
  396. INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
  397. INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */
  398. INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */
  399. }
  400. file size test.db
  401. } 1024
  402. do_test wal-9.2 {
  403. sqlite3_wal db2 test.db
  404. execsql {PRAGMA integrity_check } db2
  405. } {ok}
  406. do_test wal-9.3 {
  407. forcedelete test2.db test2.db-wal
  408. copy_file test.db test2.db
  409. copy_file test.db-wal test2.db-wal
  410. sqlite3_wal db3 test2.db
  411. execsql {PRAGMA integrity_check } db3
  412. } {ok}
  413. db3 close
  414. do_test wal-9.4 {
  415. execsql { PRAGMA wal_checkpoint }
  416. db2 close
  417. sqlite3_wal db2 test.db
  418. execsql {PRAGMA integrity_check } db2
  419. } {ok}
  420. foreach handle {db db2 db3} { catch { $handle close } }
  421. unset handle
  422. #-------------------------------------------------------------------------
  423. # The following block of tests - wal-10.* - test that the WAL locking
  424. # scheme works in simple cases. This block of tests is run twice. Once
  425. # using multiple connections in the address space of the current process,
  426. # and once with all connections except one running in external processes.
  427. #
  428. do_multiclient_test tn {
  429. # Initialize the database schema and contents.
  430. #
  431. do_test wal-10.$tn.1 {
  432. execsql {
  433. PRAGMA auto_vacuum = 0;
  434. PRAGMA journal_mode = wal;
  435. CREATE TABLE t1(a, b);
  436. INSERT INTO t1 VALUES(1, 2);
  437. SELECT * FROM t1;
  438. }
  439. } {wal 1 2}
  440. # Open a transaction and write to the database using [db]. Check that [db2]
  441. # is still able to read the snapshot before the transaction was opened.
  442. #
  443. do_test wal-10.$tn.2 {
  444. execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); }
  445. sql2 {SELECT * FROM t1}
  446. } {1 2}
  447. # Have [db] commit the transaction. Check that [db2] is now seeing the
  448. # new, updated snapshot.
  449. #
  450. do_test wal-10.$tn.3 {
  451. execsql { COMMIT }
  452. sql2 {SELECT * FROM t1}
  453. } {1 2 3 4}
  454. # Have [db2] open a read transaction. Then write to the db via [db]. Check
  455. # that [db2] is still seeing the original snapshot. Then read with [db3].
  456. # [db3] should see the newly committed data.
  457. #
  458. do_test wal-10.$tn.4 {
  459. sql2 { BEGIN ; SELECT * FROM t1}
  460. } {1 2 3 4}
  461. do_test wal-10.$tn.5 {
  462. execsql { INSERT INTO t1 VALUES(5, 6); }
  463. sql2 {SELECT * FROM t1}
  464. } {1 2 3 4}
  465. do_test wal-10.$tn.6 {
  466. sql3 {SELECT * FROM t1}
  467. } {1 2 3 4 5 6}
  468. do_test wal-10.$tn.7 {
  469. sql2 COMMIT
  470. } {}
  471. # Have [db2] open a write transaction. Then attempt to write to the
  472. # database via [db]. This should fail (writer lock cannot be obtained).
  473. #
  474. # Then open a read-transaction with [db]. Commit the [db2] transaction
  475. # to disk. Verify that [db] still cannot write to the database (because
  476. # it is reading an old snapshot).
  477. #
  478. # Close the current [db] transaction. Open a new one. [db] can now write
  479. # to the database (as it is not locked and [db] is reading the latest
  480. # snapshot).
  481. #
  482. do_test wal-10.$tn.7 {
  483. sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; }
  484. catchsql { INSERT INTO t1 VALUES(9, 10) }
  485. } {1 {database is locked}}
  486. do_test wal-10.$tn.8 {
  487. execsql { BEGIN ; SELECT * FROM t1 }
  488. } {1 2 3 4 5 6}
  489. do_test wal-10.$tn.9 {
  490. sql2 COMMIT
  491. catchsql { INSERT INTO t1 VALUES(9, 10) }
  492. } {1 {database is locked}}
  493. do_test wal-10.$tn.10 {
  494. execsql { COMMIT }
  495. execsql { BEGIN }
  496. execsql { INSERT INTO t1 VALUES(9, 10) }
  497. execsql { COMMIT }
  498. execsql { SELECT * FROM t1 }
  499. } {1 2 3 4 5 6 7 8 9 10}
  500. # Open a read transaction with [db2]. Check that this prevents [db] from
  501. # checkpointing the database. But not from writing to it.
  502. #
  503. do_test wal-10.$tn.11 {
  504. sql2 { BEGIN; SELECT * FROM t1 }
  505. } {1 2 3 4 5 6 7 8 9 10}
  506. do_test wal-10.$tn.12 {
  507. catchsql { PRAGMA wal_checkpoint }
  508. } {0 {0 7 7}} ;# Reader no longer block checkpoints
  509. do_test wal-10.$tn.13 {
  510. execsql { INSERT INTO t1 VALUES(11, 12) }
  511. sql2 {SELECT * FROM t1}
  512. } {1 2 3 4 5 6 7 8 9 10}
  513. # Writers do not block checkpoints any more either.
  514. #
  515. do_test wal-10.$tn.14 {
  516. catchsql { PRAGMA wal_checkpoint }
  517. } {0 {0 8 7}}
  518. # The following series of test cases used to verify another blocking
  519. # case in WAL - a case which no longer blocks.
  520. #
  521. do_test wal-10.$tn.15 {
  522. sql2 { COMMIT; BEGIN; SELECT * FROM t1; }
  523. } {1 2 3 4 5 6 7 8 9 10 11 12}
  524. do_test wal-10.$tn.16 {
  525. catchsql { PRAGMA wal_checkpoint }
  526. } {0 {0 8 8}}
  527. do_test wal-10.$tn.17 {
  528. execsql { PRAGMA wal_checkpoint }
  529. } {0 8 8}
  530. do_test wal-10.$tn.18 {
  531. sql3 { BEGIN; SELECT * FROM t1 }
  532. } {1 2 3 4 5 6 7 8 9 10 11 12}
  533. do_test wal-10.$tn.19 {
  534. catchsql { INSERT INTO t1 VALUES(13, 14) }
  535. } {0 {}}
  536. do_test wal-10.$tn.20 {
  537. execsql { SELECT * FROM t1 }
  538. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  539. do_test wal-10.$tn.21 {
  540. sql3 COMMIT
  541. sql2 COMMIT
  542. } {}
  543. do_test wal-10.$tn.22 {
  544. execsql { SELECT * FROM t1 }
  545. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  546. # Another series of tests that used to demonstrate blocking behavior
  547. # but which now work.
  548. #
  549. do_test wal-10.$tn.23 {
  550. execsql { PRAGMA wal_checkpoint }
  551. } {0 9 9}
  552. do_test wal-10.$tn.24 {
  553. sql2 { BEGIN; SELECT * FROM t1; }
  554. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
  555. do_test wal-10.$tn.25 {
  556. execsql { PRAGMA wal_checkpoint }
  557. } {0 9 9}
  558. do_test wal-10.$tn.26 {
  559. catchsql { INSERT INTO t1 VALUES(15, 16) }
  560. } {0 {}}
  561. do_test wal-10.$tn.27 {
  562. sql3 { INSERT INTO t1 VALUES(17, 18) }
  563. } {}
  564. do_test wal-10.$tn.28 {
  565. code3 {
  566. set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL]
  567. sqlite3_step $::STMT
  568. }
  569. execsql { SELECT * FROM t1 }
  570. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}
  571. do_test wal-10.$tn.29 {
  572. execsql { INSERT INTO t1 VALUES(19, 20) }
  573. catchsql { PRAGMA wal_checkpoint }
  574. } {0 {0 3 0}}
  575. do_test wal-10.$tn.30 {
  576. code3 { sqlite3_finalize $::STMT }
  577. execsql { PRAGMA wal_checkpoint }
  578. } {0 3 0}
  579. # At one point, if a reader failed to upgrade to a writer because it
  580. # was reading an old snapshot, the write-locks were not being released.
  581. # Test that this bug has been fixed.
  582. #
  583. do_test wal-10.$tn.31 {
  584. sql2 COMMIT
  585. execsql { BEGIN ; SELECT * FROM t1 }
  586. sql2 { INSERT INTO t1 VALUES(21, 22) }
  587. catchsql { INSERT INTO t1 VALUES(23, 24) }
  588. } {1 {database is locked}}
  589. do_test wal-10.$tn.32 {
  590. # This statement would fail when the bug was present.
  591. sql2 { INSERT INTO t1 VALUES(23, 24) }
  592. } {}
  593. do_test wal-10.$tn.33 {
  594. execsql { SELECT * FROM t1 ; COMMIT }
  595. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  596. do_test wal-10.$tn.34 {
  597. execsql { SELECT * FROM t1 }
  598. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
  599. # Test that if a checkpointer cannot obtain the required locks, it
  600. # releases all locks before returning a busy error.
  601. #
  602. do_test wal-10.$tn.35 {
  603. execsql {
  604. DELETE FROM t1;
  605. INSERT INTO t1 VALUES('a', 'b');
  606. INSERT INTO t1 VALUES('c', 'd');
  607. }
  608. sql2 {
  609. BEGIN;
  610. SELECT * FROM t1;
  611. }
  612. } {a b c d}
  613. do_test wal-10.$tn.36 {
  614. catchsql { PRAGMA wal_checkpoint }
  615. } {0 {0 8 8}}
  616. do_test wal-10.$tn.36 {
  617. sql3 { INSERT INTO t1 VALUES('e', 'f') }
  618. sql2 { SELECT * FROM t1 }
  619. } {a b c d}
  620. do_test wal-10.$tn.37 {
  621. sql2 COMMIT
  622. execsql { PRAGMA wal_checkpoint }
  623. } {0 9 9}
  624. }
  625. #-------------------------------------------------------------------------
  626. # This block of tests, wal-11.*, test that nothing goes terribly wrong
  627. # if frames must be written to the log file before a transaction is
  628. # committed (in order to free up memory).
  629. #
  630. do_test wal-11.1 {
  631. reopen_db
  632. execsql {
  633. PRAGMA cache_size = 10;
  634. PRAGMA page_size = 1024;
  635. CREATE TABLE t1(x PRIMARY KEY);
  636. }
  637. list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
  638. } {1 3}
  639. do_test wal-11.2 {
  640. execsql { PRAGMA wal_checkpoint }
  641. list [expr [file size test.db]/1024] [file size test.db-wal]
  642. } [list 3 [wal_file_size 3 1024]]
  643. do_test wal-11.3 {
  644. execsql { INSERT INTO t1 VALUES( blob(900) ) }
  645. list [expr [file size test.db]/1024] [file size test.db-wal]
  646. } [list 3 [wal_file_size 4 1024]]
  647. do_test wal-11.4 {
  648. execsql {
  649. BEGIN;
  650. INSERT INTO t1 SELECT blob(900) FROM t1; -- 2
  651. INSERT INTO t1 SELECT blob(900) FROM t1; -- 4
  652. INSERT INTO t1 SELECT blob(900) FROM t1; -- 8
  653. INSERT INTO t1 SELECT blob(900) FROM t1; -- 16
  654. }
  655. list [expr [file size test.db]/1024] [file size test.db-wal]
  656. } [list 3 [wal_file_size 32 1024]]
  657. do_test wal-11.5 {
  658. execsql {
  659. SELECT count(*) FROM t1;
  660. PRAGMA integrity_check;
  661. }
  662. } {16 ok}
  663. do_test wal-11.6 {
  664. execsql COMMIT
  665. list [expr [file size test.db]/1024] [file size test.db-wal]
  666. } [list 3 [wal_file_size 41 1024]]
  667. do_test wal-11.7 {
  668. execsql {
  669. SELECT count(*) FROM t1;
  670. PRAGMA integrity_check;
  671. }
  672. } {16 ok}
  673. do_test wal-11.8 {
  674. execsql { PRAGMA wal_checkpoint }
  675. list [expr [file size test.db]/1024] [file size test.db-wal]
  676. } [list 37 [wal_file_size 41 1024]]
  677. do_test wal-11.9 {
  678. db close
  679. list [expr [file size test.db]/1024] [log_deleted test.db-wal]
  680. } {37 1}
  681. sqlite3_wal db test.db
  682. set nWal 39
  683. if {[permutation]!="mmap"} {set nWal 37}
  684. ifcapable !mmap {set nWal 37}
  685. do_test wal-11.10 {
  686. execsql {
  687. PRAGMA cache_size = 10;
  688. BEGIN;
  689. INSERT INTO t1 SELECT blob(900) FROM t1; -- 32
  690. SELECT count(*) FROM t1;
  691. }
  692. list [expr [file size test.db]/1024] [file size test.db-wal]
  693. } [list 37 [wal_file_size $nWal 1024]]
  694. do_test wal-11.11 {
  695. execsql {
  696. SELECT count(*) FROM t1;
  697. ROLLBACK;
  698. SELECT count(*) FROM t1;
  699. }
  700. } {32 16}
  701. do_test wal-11.12 {
  702. list [expr [file size test.db]/1024] [file size test.db-wal]
  703. } [list 37 [wal_file_size $nWal 1024]]
  704. do_test wal-11.13 {
  705. execsql {
  706. INSERT INTO t1 VALUES( blob(900) );
  707. SELECT count(*) FROM t1;
  708. PRAGMA integrity_check;
  709. }
  710. } {17 ok}
  711. do_test wal-11.14 {
  712. list [expr [file size test.db]/1024] [file size test.db-wal]
  713. } [list 37 [wal_file_size $nWal 1024]]
  714. #-------------------------------------------------------------------------
  715. # This block of tests, wal-12.*, tests the fix for a problem that
  716. # could occur if a log that is a prefix of an older log is written
  717. # into a reused log file.
  718. #
  719. reopen_db
  720. do_test wal-12.1 {
  721. execsql {
  722. PRAGMA page_size = 1024;
  723. CREATE TABLE t1(x, y);
  724. CREATE TABLE t2(x, y);
  725. INSERT INTO t1 VALUES('A', 1);
  726. }
  727. list [expr [file size test.db]/1024] [file size test.db-wal]
  728. } [list 1 [wal_file_size 5 1024]]
  729. do_test wal-12.2 {
  730. db close
  731. sqlite3 db test.db
  732. execsql {
  733. PRAGMA synchronous = normal;
  734. UPDATE t1 SET y = 0 WHERE x = 'A';
  735. }
  736. list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
  737. } {3 1}
  738. do_test wal-12.3 {
  739. execsql { INSERT INTO t2 VALUES('B', 1) }
  740. list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
  741. } {3 2}
  742. do_test wal-12.4 {
  743. forcecopy test.db test2.db
  744. forcecopy test.db-wal test2.db-wal
  745. sqlite3_wal db2 test2.db
  746. execsql { SELECT * FROM t2 } db2
  747. } {B 1}
  748. db2 close
  749. do_test wal-12.5 {
  750. execsql {
  751. PRAGMA wal_checkpoint;
  752. UPDATE t2 SET y = 2 WHERE x = 'B';
  753. PRAGMA wal_checkpoint;
  754. UPDATE t1 SET y = 1 WHERE x = 'A';
  755. PRAGMA wal_checkpoint;
  756. UPDATE t1 SET y = 0 WHERE x = 'A';
  757. }
  758. execsql { SELECT * FROM t2 }
  759. } {B 2}
  760. do_test wal-12.6 {
  761. forcecopy test.db test2.db
  762. forcecopy test.db-wal test2.db-wal
  763. sqlite3_wal db2 test2.db
  764. execsql { SELECT * FROM t2 } db2
  765. } {B 2}
  766. db2 close
  767. db close
  768. #-------------------------------------------------------------------------
  769. # Test large log summaries.
  770. #
  771. # In this case "large" usually means a log file that requires a wal-index
  772. # mapping larger than 64KB (the default initial allocation). A 64KB wal-index
  773. # is large enough for a log file that contains approximately 13100 frames.
  774. # So the following tests create logs containing at least this many frames.
  775. #
  776. # wal-13.1.*: This test case creates a very large log file within the
  777. # file-system (around 200MB). The log file does not contain
  778. # any valid frames. Test that the database file can still be
  779. # opened and queried, and that the invalid log file causes no
  780. # problems.
  781. #
  782. # wal-13.2.*: Test that a process may create a large log file and query
  783. # the database (including the log file that it itself created).
  784. #
  785. # wal-13.3.*: Test that if a very large log file is created, and then a
  786. # second connection is opened on the database file, it is possible
  787. # to query the database (and the very large log) using the
  788. # second connection.
  789. #
  790. # wal-13.4.*: Same test as wal-13.3.*. Except in this case the second
  791. # connection is opened by an external process.
  792. #
  793. do_test wal-13.1.1 {
  794. list [file exists test.db] [file exists test.db-wal]
  795. } {1 0}
  796. do_test wal-13.1.2 {
  797. set fd [open test.db-wal w]
  798. seek $fd [expr 200*1024*1024]
  799. puts $fd ""
  800. close $fd
  801. sqlite3 db test.db
  802. execsql { SELECT * FROM t2 }
  803. } {B 2}
  804. breakpoint
  805. do_test wal-13.1.3 {
  806. db close
  807. file exists test.db-wal
  808. } {0}
  809. do_test wal-13.2.1 {
  810. sqlite3 db test.db
  811. execsql { SELECT count(*) FROM t2 }
  812. } {1}
  813. do_test wal-13.2.2 {
  814. db function blob blob
  815. for {set i 0} {$i < 16} {incr i} {
  816. execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 }
  817. }
  818. execsql { SELECT count(*) FROM t2 }
  819. } [expr int(pow(2, 16))]
  820. do_test wal-13.2.3 {
  821. expr [file size test.db-wal] > [wal_file_size 33000 1024]
  822. } 1
  823. do_multiclient_test tn {
  824. incr tn 2
  825. do_test wal-13.$tn.0 {
  826. sql1 {
  827. PRAGMA journal_mode = WAL;
  828. CREATE TABLE t1(x);
  829. INSERT INTO t1 SELECT randomblob(800);
  830. }
  831. sql1 { SELECT count(*) FROM t1 }
  832. } {1}
  833. for {set ii 1} {$ii<16} {incr ii} {
  834. do_test wal-13.$tn.$ii.a {
  835. sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 }
  836. sql2 { SELECT count(*) FROM t1 }
  837. } [expr (1<<$ii)]
  838. do_test wal-13.$tn.$ii.b {
  839. sql1 { SELECT count(*) FROM t1 }
  840. } [expr (1<<$ii)]
  841. do_test wal-13.$tn.$ii.c {
  842. sql1 { SELECT count(*) FROM t1 }
  843. } [expr (1<<$ii)]
  844. do_test wal-13.$tn.$ii.d {
  845. sql1 { PRAGMA integrity_check }
  846. } {ok}
  847. }
  848. }
  849. #-------------------------------------------------------------------------
  850. # Check a fun corruption case has been fixed.
  851. #
  852. # The problem was that after performing a checkpoint using a connection
  853. # that had an out-of-date pager-cache, the next time the connection was
  854. # used it did not realize the cache was out-of-date and proceeded to
  855. # operate with an inconsistent cache. Leading to corruption.
  856. #
  857. catch { db close }
  858. catch { db2 close }
  859. catch { db3 close }
  860. forcedelete test.db test.db-wal
  861. sqlite3 db test.db
  862. sqlite3 db2 test.db
  863. do_test wal-14 {
  864. execsql {
  865. PRAGMA journal_mode = WAL;
  866. CREATE TABLE t1(a PRIMARY KEY, b);
  867. INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
  868. INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
  869. INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
  870. INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
  871. }
  872. db2 eval {
  873. INSERT INTO t1 SELECT randomblob(10), randomblob(100);
  874. INSERT INTO t1 SELECT randomblob(10), randomblob(100);
  875. INSERT INTO t1 SELECT randomblob(10), randomblob(100);
  876. INSERT INTO t1 SELECT randomblob(10), randomblob(100);
  877. }
  878. # After executing the "PRAGMA wal_checkpoint", connection [db] was being
  879. # left with an inconsistent cache. Running the CREATE INDEX statement
  880. # in this state led to database corruption.
  881. catchsql {
  882. PRAGMA wal_checkpoint;
  883. CREATE INDEX i1 on t1(b);
  884. }
  885. db2 eval { PRAGMA integrity_check }
  886. } {ok}
  887. catch { db close }
  888. catch { db2 close }
  889. #-------------------------------------------------------------------------
  890. # The following block of tests - wal-15.* - focus on testing the
  891. # implementation of the sqlite3_wal_checkpoint() interface.
  892. #
  893. forcedelete test.db test.db-wal
  894. sqlite3 db test.db
  895. do_test wal-15.1 {
  896. execsql {
  897. PRAGMA auto_vacuum = 0;
  898. PRAGMA page_size = 1024;
  899. PRAGMA journal_mode = WAL;
  900. }
  901. execsql {
  902. CREATE TABLE t1(a, b);
  903. INSERT INTO t1 VALUES(1, 2);
  904. }
  905. } {}
  906. # Test that an error is returned if the database name is not recognized
  907. #
  908. do_test wal-15.2.1 {
  909. sqlite3_wal_checkpoint db aux
  910. } {SQLITE_ERROR}
  911. do_test wal-15.2.2 {
  912. sqlite3_errcode db
  913. } {SQLITE_ERROR}
  914. do_test wal-15.2.3 {
  915. sqlite3_errmsg db
  916. } {unknown database: aux}
  917. # Test that an error is returned if an attempt is made to checkpoint
  918. # if a transaction is open on the database.
  919. #
  920. do_test wal-15.3.1 {
  921. execsql {
  922. BEGIN;
  923. INSERT INTO t1 VALUES(3, 4);
  924. }
  925. sqlite3_wal_checkpoint db main
  926. } {SQLITE_LOCKED}
  927. do_test wal-15.3.2 {
  928. sqlite3_errcode db
  929. } {SQLITE_LOCKED}
  930. do_test wal-15.3.3 {
  931. sqlite3_errmsg db
  932. } {database table is locked}
  933. # Earlier versions returned an error is returned if the db cannot be
  934. # checkpointed because of locks held by another connection. Check that
  935. # this is no longer the case.
  936. #
  937. sqlite3 db2 test.db
  938. do_test wal-15.4.1 {
  939. execsql {
  940. BEGIN;
  941. SELECT * FROM t1;
  942. } db2
  943. } {1 2}
  944. do_test wal-15.4.2 {
  945. execsql { COMMIT }
  946. sqlite3_wal_checkpoint db
  947. } {SQLITE_OK}
  948. do_test wal-15.4.3 {
  949. sqlite3_errmsg db
  950. } {not an error}
  951. # After [db2] drops its lock, [db] may checkpoint the db.
  952. #
  953. do_test wal-15.4.4 {
  954. execsql { COMMIT } db2
  955. sqlite3_wal_checkpoint db
  956. } {SQLITE_OK}
  957. do_test wal-15.4.5 {
  958. sqlite3_errmsg db
  959. } {not an error}
  960. do_test wal-15.4.6 {
  961. file size test.db
  962. } [expr 1024*2]
  963. catch { db2 close }
  964. catch { db close }
  965. #-------------------------------------------------------------------------
  966. # The following block of tests - wal-16.* - test that if a NULL pointer or
  967. # an empty string is passed as the second argument of the wal_checkpoint()
  968. # API, an attempt is made to checkpoint all attached databases.
  969. #
  970. foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} {
  971. 1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1
  972. 2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1
  973. 3 {db eval "PRAGMA wal_checkpoint"} {0 10 10} 1 1
  974. 4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0
  975. 5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1
  976. 6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0
  977. 7 {db eval "PRAGMA main.wal_checkpoint"} {0 10 10} 1 0
  978. 8 {db eval "PRAGMA aux.wal_checkpoint"} {0 13 13} 0 1
  979. 9 {db eval "PRAGMA temp.wal_checkpoint"} {0 -1 -1} 0 0
  980. } {
  981. do_test wal-16.$tn.1 {
  982. forcedelete test2.db test2.db-wal test2.db-journal
  983. forcedelete test.db test.db-wal test.db-journal
  984. sqlite3 db test.db
  985. execsql {
  986. ATTACH 'test2.db' AS aux;
  987. PRAGMA main.auto_vacuum = 0;
  988. PRAGMA aux.auto_vacuum = 0;
  989. PRAGMA main.journal_mode = WAL;
  990. PRAGMA aux.journal_mode = WAL;
  991. PRAGMA main.synchronous = NORMAL;
  992. PRAGMA aux.synchronous = NORMAL;
  993. }
  994. } {wal wal}
  995. do_test wal-16.$tn.2 {
  996. execsql {
  997. CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b));
  998. CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b));
  999. INSERT INTO t2 VALUES(1, randomblob(1000));
  1000. INSERT INTO t2 VALUES(2, randomblob(1000));
  1001. INSERT INTO t1 SELECT * FROM t2;
  1002. }
  1003. list [file size test.db] [file size test.db-wal]
  1004. } [list [expr 1*1024] [wal_file_size 10 1024]]
  1005. do_test wal-16.$tn.3 {
  1006. list [file size test2.db] [file size test2.db-wal]
  1007. } [list [expr 1*1024] [wal_file_size 13 1024]]
  1008. do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res
  1009. do_test wal-16.$tn.5 {
  1010. list [file size test.db] [file size test.db-wal]
  1011. } [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]]
  1012. do_test wal-16.$tn.6 {
  1013. list [file size test2.db] [file size test2.db-wal]
  1014. } [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 13 1024]]
  1015. catch { db close }
  1016. }
  1017. #-------------------------------------------------------------------------
  1018. # The following tests - wal-17.* - attempt to verify that the correct
  1019. # number of "padding" frames are appended to the log file when a transaction
  1020. # is committed in synchronous=FULL mode.
  1021. #
  1022. # Do this by creating a database that uses 512 byte pages. Then writing
  1023. # a transaction that modifies 171 pages. In synchronous=NORMAL mode, this
  1024. # produces a log file of:
  1025. #
  1026. # 32 + (24+512)*171 = 90312 bytes.
  1027. #
  1028. # Slightly larger than 11*8192 = 90112 bytes.
  1029. #
  1030. # Run the test using various different sector-sizes. In each case, the
  1031. # WAL code should write the 90300 bytes of log file containing the
  1032. # transaction, then append as may frames as are required to extend the
  1033. # log file so that no part of the next transaction will be written into
  1034. # a disk-sector used by transaction just committed.
  1035. #
  1036. set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000]
  1037. catch { db close }
  1038. foreach {tn sectorsize logsize} "
  1039. 1 128 [wal_file_size 172 512]
  1040. 2 256 [wal_file_size 172 512]
  1041. 3 512 [wal_file_size 172 512]
  1042. 4 1024 [wal_file_size 172 512]
  1043. 5 2048 [wal_file_size 172 512]
  1044. 6 4096 [wal_file_size 176 512]
  1045. 7 8192 [wal_file_size 184 512]
  1046. " {
  1047. forcedelete test.db test.db-wal test.db-journal
  1048. sqlite3_simulate_device -sectorsize $sectorsize
  1049. sqlite3 db test.db -vfs devsym
  1050. do_test wal-17.$tn.1 {
  1051. execsql {
  1052. PRAGMA auto_vacuum = 0;
  1053. PRAGMA page_size = 512;
  1054. PRAGMA cache_size = -2000;
  1055. PRAGMA journal_mode = WAL;
  1056. PRAGMA synchronous = FULL;
  1057. }
  1058. execsql {
  1059. BEGIN;
  1060. CREATE TABLE t(x);
  1061. }
  1062. for {set i 0} {$i<166} {incr i} {
  1063. execsql { INSERT INTO t VALUES(randomblob(400)) }
  1064. }
  1065. execsql COMMIT
  1066. file size test.db-wal
  1067. } $logsize
  1068. do_test wal-17.$tn.2 {
  1069. file size test.db
  1070. } 512
  1071. do_test wal-17.$tn.3 {
  1072. db close
  1073. file size test.db
  1074. } [expr 512*171]
  1075. }
  1076. sqlite3_test_control_pending_byte $old_pending_byte
  1077. #-------------------------------------------------------------------------
  1078. # This test - wal-18.* - verifies a couple of specific conditions that
  1079. # may be encountered while recovering a log file are handled correctly:
  1080. #
  1081. # wal-18.1.* When the first 32-bits of a frame checksum is correct but
  1082. # the second 32-bits are false, and
  1083. #
  1084. # wal-18.2.* When the page-size field that occurs at the start of a log
  1085. # file is a power of 2 greater than 16384 or smaller than 512.
  1086. #
  1087. forcedelete test.db test.db-wal test.db-journal
  1088. do_test wal-18.0 {
  1089. sqlite3 db test.db
  1090. execsql {
  1091. PRAGMA page_size = 1024;
  1092. PRAGMA auto_vacuum = 0;
  1093. PRAGMA journal_mode = WAL;
  1094. PRAGMA synchronous = OFF;
  1095. CREATE TABLE t1(a, b, UNIQUE(a, b));
  1096. INSERT INTO t1 VALUES(0, 0);
  1097. PRAGMA wal_checkpoint;
  1098. INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2
  1099. INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4
  1100. INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6
  1101. }
  1102. forcecopy test.db testX.db
  1103. forcecopy test.db-wal testX.db-wal
  1104. db close
  1105. list [file size testX.db] [file size testX.db-wal]
  1106. } [list [expr 3*1024] [wal_file_size 6 1024]]
  1107. unset -nocomplain nFrame result
  1108. foreach {nFrame result} {
  1109. 0 {0 0}
  1110. 1 {0 0}
  1111. 2 {0 0 1 2}
  1112. 3 {0 0 1 2}
  1113. 4 {0 0 1 2 3 4}
  1114. 5 {0 0 1 2 3 4}
  1115. 6 {0 0 1 2 3 4 5 6}
  1116. } {
  1117. do_test wal-18.1.$nFrame {
  1118. forcecopy testX.db test.db
  1119. forcecopy testX.db-wal test.db-wal
  1120. hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000
  1121. sqlite3 db test.db
  1122. execsql {
  1123. SELECT * FROM t1;
  1124. PRAGMA integrity_check;
  1125. }
  1126. } [concat $result ok]
  1127. db close
  1128. }
  1129. proc randomblob {pgsz} {
  1130. sqlite3 rbdb :memory:
  1131. set blob [rbdb one {SELECT randomblob($pgsz)}]
  1132. rbdb close
  1133. set blob
  1134. }
  1135. proc logcksum {ckv1 ckv2 blob} {
  1136. upvar $ckv1 c1
  1137. upvar $ckv2 c2
  1138. # Since the magic number at the start of the -wal file header is
  1139. # 931071618 that indicates that the content should always be read as
  1140. # little-endian.
  1141. #
  1142. set scanpattern i*
  1143. binary scan $blob $scanpattern values
  1144. foreach {v1 v2} $values {
  1145. set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
  1146. set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
  1147. }
  1148. }
  1149. forcecopy test.db testX.db
  1150. foreach {tn pgsz works} {
  1151. 1 128 0
  1152. 2 256 0
  1153. 3 512 1
  1154. 4 1024 1
  1155. 5 2048 1
  1156. 6 4096 1
  1157. 7 8192 1
  1158. 8 16384 1
  1159. 9 32768 1
  1160. 10 65536 1
  1161. 11 131072 0
  1162. 11 1016 0
  1163. } {
  1164. if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} {
  1165. set works 0
  1166. }
  1167. for {set pg 1} {$pg <= 3} {incr pg} {
  1168. forcecopy testX.db test.db
  1169. forcedelete test.db-wal
  1170. # Check that the database now exists and consists of three pages. And
  1171. # that there is no associated wal file.
  1172. #
  1173. do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0
  1174. do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1
  1175. do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3]
  1176. do_test wal-18.2.$tn.$pg.4 {
  1177. # Create a wal file that contains a single frame (database page
  1178. # number $pg) with the commit flag set. The frame checksum is
  1179. # correct, but the contents of the database page are corrupt.
  1180. #
  1181. # The page-size in the log file header is set to $pgsz. If the
  1182. # WAL code considers $pgsz to be a valid SQLite database file page-size,
  1183. # the database will be corrupt (because the garbage frame contents
  1184. # will be treated as valid content). If $pgsz is invalid (too small
  1185. # or too large), the db will not be corrupt as the log file will
  1186. # be ignored.
  1187. #
  1188. set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23]
  1189. set framebody [randomblob $pgsz]
  1190. set framehdr [binary format IIII $pg 5 22 23]
  1191. set c1 0
  1192. set c2 0
  1193. logcksum c1 c2 $walhdr
  1194. append walhdr [binary format II $c1 $c2]
  1195. logcksum c1 c2 [string range $framehdr 0 7]
  1196. logcksum c1 c2 $framebody
  1197. set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2]
  1198. set fd [open test.db-wal w]
  1199. fconfigure $fd -encoding binary -translation binary
  1200. puts -nonewline $fd $walhdr
  1201. puts -nonewline $fd $framehdr
  1202. puts -nonewline $fd $framebody
  1203. close $fd
  1204. file size test.db-wal
  1205. } [wal_file_size 1 $pgsz]
  1206. do_test wal-18.2.$tn.$pg.5 {
  1207. sqlite3 db test.db
  1208. set rc [catch { db one {PRAGMA integrity_check} } msg]
  1209. expr { $rc!=0 || $msg!="ok" }
  1210. } $works
  1211. db close
  1212. }
  1213. }
  1214. #-------------------------------------------------------------------------
  1215. # The following test - wal-19.* - fixes a bug that was present during
  1216. # development.
  1217. #
  1218. # When a database connection in WAL mode is closed, it attempts an
  1219. # EXCLUSIVE lock on the database file. If the lock is obtained, the
  1220. # connection knows that it is the last connection to disconnect from
  1221. # the database, so it runs a checkpoint operation. The bug was that
  1222. # the connection was not updating its private copy of the wal-index
  1223. # header before doing so, meaning that it could checkpoint an old
  1224. # snapshot.
  1225. #
  1226. do_test wal-19.1 {
  1227. forcedelete test.db test.db-wal test.db-journal
  1228. sqlite3 db test.db
  1229. sqlite3 db2 test.db
  1230. execsql {
  1231. PRAGMA journal_mode = WAL;
  1232. CREATE TABLE t1(a, b);
  1233. INSERT INTO t1 VALUES(1, 2);
  1234. INSERT INTO t1 VALUES(3, 4);
  1235. }
  1236. execsql { SELECT * FROM t1 } db2
  1237. } {1 2 3 4}
  1238. do_test wal-19.2 {
  1239. execsql {
  1240. INSERT INTO t1 VALUES(5, 6);
  1241. SELECT * FROM t1;
  1242. }
  1243. } {1 2 3 4 5 6}
  1244. do_test wal-19.3 {
  1245. db close
  1246. db2 close
  1247. file exists test.db-wal
  1248. } {0}
  1249. do_test wal-19.4 {
  1250. # When the bug was present, the following was returning {1 2 3 4} only,
  1251. # as [db2] had an out-of-date copy of the wal-index header when it was
  1252. # closed.
  1253. #
  1254. sqlite3 db test.db
  1255. execsql { SELECT * FROM t1 }
  1256. } {1 2 3 4 5 6}
  1257. #-------------------------------------------------------------------------
  1258. # This test - wal-20.* - uses two connections. One in this process and
  1259. # the other in an external process. The procedure is:
  1260. #
  1261. # 1. Using connection 1, create the database schema.
  1262. #
  1263. # 2. Using connection 2 (in an external process), add so much
  1264. # data to the database without checkpointing that a wal-index
  1265. # larger than 64KB is required.
  1266. #
  1267. # 3. Using connection 1, checkpoint the database. Make sure all
  1268. # the data is present and the database is not corrupt.
  1269. #
  1270. # At one point, SQLite was failing to grow the mapping of the wal-index
  1271. # file in step 3 and the checkpoint was corrupting the database file.
  1272. #
  1273. do_test wal-20.1 {
  1274. catch {db close}
  1275. forcedelete test.db test.db-wal test.db-journal
  1276. sqlite3 db test.db
  1277. execsql {
  1278. PRAGMA journal_mode = WAL;
  1279. CREATE TABLE t1(x);
  1280. INSERT INTO t1 VALUES(randomblob(900));
  1281. SELECT count(*) FROM t1;
  1282. }
  1283. } {wal 1}
  1284. do_test wal-20.2 {
  1285. set ::buddy [launch_testfixture]
  1286. testfixture $::buddy {
  1287. sqlite3 db test.db
  1288. db transaction { db eval {
  1289. PRAGMA wal_autocheckpoint = 0;
  1290. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
  1291. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
  1292. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
  1293. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
  1294. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
  1295. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
  1296. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
  1297. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
  1298. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
  1299. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
  1300. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
  1301. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
  1302. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
  1303. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
  1304. } }
  1305. }
  1306. } {0}
  1307. do_test wal-20.3 {
  1308. close $::buddy
  1309. execsql { PRAGMA wal_checkpoint }
  1310. execsql { SELECT count(*) FROM t1 }
  1311. } {16384}
  1312. do_test wal-20.4 {
  1313. db close
  1314. sqlite3 db test.db
  1315. execsql { SELECT count(*) FROM t1 }
  1316. } {16384}
  1317. integrity_check wal-20.5
  1318. catch { db2 close }
  1319. catch { db close }
  1320. do_test wal-21.1 {
  1321. faultsim_delete_and_reopen
  1322. execsql {
  1323. PRAGMA journal_mode = WAL;
  1324. CREATE TABLE t1(a, b);
  1325. INSERT INTO t1 VALUES(1, 2);
  1326. INSERT INTO t1 VALUES(3, 4);
  1327. INSERT INTO t1 VALUES(5, 6);
  1328. INSERT INTO t1 VALUES(7, 8);
  1329. INSERT INTO t1 VALUES(9, 10);
  1330. INSERT INTO t1 VALUES(11, 12);
  1331. }
  1332. } {wal}
  1333. do_test wal-21.2 {
  1334. execsql {
  1335. PRAGMA cache_size = 10;
  1336. PRAGMA wal_checkpoint;
  1337. BEGIN;
  1338. SAVEPOINT s;
  1339. INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1;
  1340. ROLLBACK TO s;
  1341. COMMIT;
  1342. }
  1343. execsql { SELECT * FROM t1 }
  1344. } {1 2 3 4 5 6 7 8 9 10 11 12}
  1345. do_test wal-21.3 {
  1346. execsql { PRAGMA integrity_check }
  1347. } {ok}
  1348. #-------------------------------------------------------------------------
  1349. # Test reading and writing of databases with different page-sizes.
  1350. #
  1351. foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} {
  1352. do_multiclient_test tn [string map [list %PGSZ% $pgsz] {
  1353. do_test wal-22.%PGSZ%.$tn.1 {
  1354. sql1 {
  1355. PRAGMA main.page_size = %PGSZ%;
  1356. PRAGMA auto_vacuum = 0;
  1357. PRAGMA journal_mode = WAL;
  1358. CREATE TABLE t1(x UNIQUE);
  1359. INSERT INTO t1 SELECT randomblob(800);
  1360. INSERT INTO t1 SELECT randomblob(800);
  1361. INSERT INTO t1 SELECT randomblob(800);
  1362. }
  1363. } {wal}
  1364. do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok}
  1365. do_test wal-22.%PGSZ%.$tn.3 {
  1366. sql1 {PRAGMA wal_checkpoint}
  1367. expr {[file size test.db] % %PGSZ%}
  1368. } {0}
  1369. }]
  1370. }
  1371. #-------------------------------------------------------------------------
  1372. # Test that when 1 or more pages are recovered from a WAL file,
  1373. # sqlite3_log() is invoked to report this to the user.
  1374. #
  1375. ifcapable curdir {
  1376. set walfile [file nativename [file join [get_pwd] test.db-wal]]
  1377. } else {
  1378. set walfile test.db-wal
  1379. }
  1380. catch {db close}
  1381. forcedelete test.db
  1382. do_test wal-23.1 {
  1383. faultsim_delete_and_reopen
  1384. execsql {
  1385. CREATE TABLE t1(a, b);
  1386. PRAGMA journal_mode = WAL;
  1387. INSERT INTO t1 VALUES(1, 2);
  1388. INSERT INTO t1 VALUES(3, 4);
  1389. }
  1390. faultsim_save_and_close
  1391. sqlite3_shutdown
  1392. test_sqlite3_log [list lappend ::log]
  1393. set ::log [list]
  1394. sqlite3 db test.db
  1395. execsql { SELECT * FROM t1 }
  1396. } {1 2 3 4}
  1397. do_test wal-23.2 { set ::log } {}
  1398. do_test wal-23.3 {
  1399. db close
  1400. set ::log [list]
  1401. faultsim_restore_and_reopen
  1402. execsql { SELECT * FROM t1 }
  1403. } {1 2 3 4}
  1404. do_test wal-23.4 {
  1405. set ::log
  1406. } [list SQLITE_NOTICE_RECOVER_WAL \
  1407. "recovered 2 frames from WAL file $walfile"]
  1408. ifcapable autovacuum {
  1409. # This block tests that if the size of a database is reduced by a
  1410. # transaction (because of an incremental or auto-vacuum), that no
  1411. # data is written to the WAL file for the truncated pages as part
  1412. # of the commit. e.g. if a transaction reduces the size of a database
  1413. # to N pages, data for page N+1 should not be written to the WAL file
  1414. # when committing the transaction. At one point such data was being
  1415. # written.
  1416. #
  1417. catch {db close}
  1418. forcedelete test.db
  1419. sqlite3 db test.db
  1420. do_execsql_test 24.1 {
  1421. PRAGMA auto_vacuum = 2;
  1422. PRAGMA journal_mode = WAL;
  1423. PRAGMA page_size = 1024;
  1424. CREATE TABLE t1(x);
  1425. INSERT INTO t1 VALUES(randomblob(5000));
  1426. INSERT INTO t1 SELECT * FROM t1;
  1427. INSERT INTO t1 SELECT * FROM t1;
  1428. INSERT INTO t1 SELECT * FROM t1;
  1429. INSERT INTO t1 SELECT * FROM t1;
  1430. } {wal}
  1431. do_test 24.2 {
  1432. execsql {
  1433. DELETE FROM t1;
  1434. PRAGMA wal_checkpoint;
  1435. }
  1436. db close
  1437. sqlite3 db test.db
  1438. file exists test.db-wal
  1439. } 0
  1440. do_test 24.3 {
  1441. file size test.db
  1442. } [expr 84 * 1024]
  1443. do_test 24.4 {
  1444. execsql {
  1445. PRAGMA cache_size = 200;
  1446. PRAGMA incremental_vacuum;
  1447. PRAGMA wal_checkpoint;
  1448. }
  1449. file size test.db
  1450. } [expr 3 * 1024]
  1451. # WAL file now contains a single frame - the new root page for table t1.
  1452. # It would be two frames (the new root page and a padding frame) if the
  1453. # ZERO_DAMAGE flag were not set.
  1454. do_test 24.5 {
  1455. file size test.db-wal
  1456. } [wal_file_size 1 1024]
  1457. }
  1458. db close
  1459. sqlite3_shutdown
  1460. test_sqlite3_log
  1461. sqlite3_initialize
  1462. finish_test