pager1.test 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. # 2010 June 15
  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. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. source $testdir/lock_common.tcl
  15. source $testdir/malloc_common.tcl
  16. source $testdir/wal_common.tcl
  17. set testprefix pager1
  18. # Do not use a codec for tests in this file, as the database file is
  19. # manipulated directly using tcl scripts (using the [hexio_write] command).
  20. #
  21. do_not_use_codec
  22. #
  23. # pager1-1.*: Test inter-process locking (clients in multiple processes).
  24. #
  25. # pager1-2.*: Test intra-process locking (multiple clients in this process).
  26. #
  27. # pager1-3.*: Savepoint related tests.
  28. #
  29. # pager1-4.*: Hot-journal related tests.
  30. #
  31. # pager1-5.*: Cases related to multi-file commits.
  32. #
  33. # pager1-6.*: Cases related to "PRAGMA max_page_count"
  34. #
  35. # pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
  36. #
  37. # pager1-8.*: Cases using temporary and in-memory databases.
  38. #
  39. # pager1-9.*: Tests related to the backup API.
  40. #
  41. # pager1-10.*: Test that the assumed file-system sector-size is limited to
  42. # 64KB.
  43. #
  44. # pager1-12.*: Tests involving "PRAGMA page_size"
  45. #
  46. # pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST"
  47. #
  48. # pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF"
  49. #
  50. # pager1-15.*: Varying sqlite3_vfs.szOsFile
  51. #
  52. # pager1-16.*: Varying sqlite3_vfs.mxPathname
  53. #
  54. # pager1-17.*: Tests related to "PRAGMA omit_readlock"
  55. # (The omit_readlock pragma has been removed and so have
  56. # these tests.)
  57. #
  58. # pager1-18.*: Test that the pager layer responds correctly if the b-tree
  59. # requests an invalid page number (due to db corruption).
  60. #
  61. proc recursive_select {id table {script {}}} {
  62. set cnt 0
  63. db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" {
  64. recursive_select $rowid $table $script
  65. incr cnt
  66. }
  67. if {$cnt==0} { eval $script }
  68. }
  69. set a_string_counter 1
  70. proc a_string {n} {
  71. global a_string_counter
  72. incr a_string_counter
  73. string range [string repeat "${a_string_counter}." $n] 1 $n
  74. }
  75. db func a_string a_string
  76. do_multiclient_test tn {
  77. # Create and populate a database table using connection [db]. Check
  78. # that connections [db2] and [db3] can see the schema and content.
  79. #
  80. do_test pager1-$tn.1 {
  81. sql1 {
  82. CREATE TABLE t1(a PRIMARY KEY, b);
  83. CREATE INDEX i1 ON t1(b);
  84. INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two');
  85. }
  86. } {}
  87. do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
  88. do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two}
  89. # Open a transaction and add a row using [db]. This puts [db] in
  90. # RESERVED state. Check that connections [db2] and [db3] can still
  91. # read the database content as it was before the transaction was
  92. # opened. [db] should see the inserted row.
  93. #
  94. do_test pager1-$tn.4 {
  95. sql1 {
  96. BEGIN;
  97. INSERT INTO t1 VALUES(3, 'three');
  98. }
  99. } {}
  100. do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
  101. do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  102. # [db] still has an open write transaction. Check that this prevents
  103. # other connections (specifically [db2]) from writing to the database.
  104. #
  105. # Even if [db2] opens a transaction first, it may not write to the
  106. # database. After the attempt to write the db within a transaction,
  107. # [db2] is left with an open transaction, but not a read-lock on
  108. # the main database. So it does not prevent [db] from committing.
  109. #
  110. do_test pager1-$tn.8 {
  111. csql2 { UPDATE t1 SET a = a + 10 }
  112. } {1 {database is locked}}
  113. do_test pager1-$tn.9 {
  114. csql2 {
  115. BEGIN;
  116. UPDATE t1 SET a = a + 10;
  117. }
  118. } {1 {database is locked}}
  119. # Have [db] commit its transactions. Check the other connections can
  120. # now see the new database content.
  121. #
  122. do_test pager1-$tn.10 { sql1 { COMMIT } } {}
  123. do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  124. do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  125. do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  126. # Check that, as noted above, [db2] really did keep an open transaction
  127. # after the attempt to write the database failed.
  128. #
  129. do_test pager1-$tn.14 {
  130. csql2 { BEGIN }
  131. } {1 {cannot start a transaction within a transaction}}
  132. do_test pager1-$tn.15 { sql2 { ROLLBACK } } {}
  133. # Have [db2] open a transaction and take a read-lock on the database.
  134. # Check that this prevents [db] from writing to the database (outside
  135. # of any transaction). After this fails, check that [db3] can read
  136. # the db (showing that [db] did not take a PENDING lock etc.)
  137. #
  138. do_test pager1-$tn.15 {
  139. sql2 { BEGIN; SELECT * FROM t1; }
  140. } {1 one 2 two 3 three}
  141. do_test pager1-$tn.16 {
  142. csql1 { UPDATE t1 SET a = a + 10 }
  143. } {1 {database is locked}}
  144. do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  145. # This time, have [db] open a transaction before writing the database.
  146. # This works - [db] gets a RESERVED lock which does not conflict with
  147. # the SHARED lock [db2] is holding.
  148. #
  149. do_test pager1-$tn.18 {
  150. sql1 {
  151. BEGIN;
  152. UPDATE t1 SET a = a + 10;
  153. }
  154. } {}
  155. do_test pager1-$tn-19 {
  156. sql1 { PRAGMA lock_status }
  157. } {main reserved temp closed}
  158. do_test pager1-$tn-20 {
  159. sql2 { PRAGMA lock_status }
  160. } {main shared temp closed}
  161. # Check that all connections can still read the database. Only [db] sees
  162. # the updated content (as the transaction has not been committed yet).
  163. #
  164. do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
  165. do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  166. do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  167. # Because [db2] still has the SHARED lock, [db] is unable to commit the
  168. # transaction. If it tries, an error is returned and the connection
  169. # upgrades to a PENDING lock.
  170. #
  171. # Once this happens, [db] can read the database and see the new content,
  172. # [db2] (still holding SHARED) can still read the old content, but [db3]
  173. # (not holding any lock) is prevented by [db]'s PENDING from reading
  174. # the database.
  175. #
  176. do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}}
  177. do_test pager1-$tn-25 {
  178. sql1 { PRAGMA lock_status }
  179. } {main pending temp closed}
  180. do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
  181. do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
  182. do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
  183. # Have [db2] commit its read transaction, releasing the SHARED lock it
  184. # is holding. Now, neither [db2] nor [db3] may read the database (as [db]
  185. # is still holding a PENDING).
  186. #
  187. do_test pager1-$tn.29 { sql2 { COMMIT } } {}
  188. do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
  189. do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
  190. # [db] is now able to commit the transaction. Once the transaction is
  191. # committed, all three connections can read the new content.
  192. #
  193. do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {}
  194. do_test pager1-$tn.26 { sql1 { COMMIT } } {}
  195. do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three}
  196. do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three}
  197. do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three}
  198. # Install a busy-handler for connection [db].
  199. #
  200. set ::nbusy [list]
  201. proc busy {n} {
  202. lappend ::nbusy $n
  203. if {$n>5} { sql2 COMMIT }
  204. return 0
  205. }
  206. db busy busy
  207. do_test pager1-$tn.29 {
  208. sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') }
  209. } {}
  210. do_test pager1-$tn.30 {
  211. sql2 { BEGIN ; SELECT * FROM t1 }
  212. } {21 one 22 two 23 three}
  213. do_test pager1-$tn.31 { sql1 COMMIT } {}
  214. do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6}
  215. }
  216. #-------------------------------------------------------------------------
  217. # Savepoint related test cases.
  218. #
  219. # pager1-3.1.2.*: Force a savepoint rollback to cause the database file
  220. # to grow.
  221. #
  222. # pager1-3.1.3.*: Use a journal created in synchronous=off mode as part
  223. # of a savepoint rollback.
  224. #
  225. do_test pager1-3.1.1 {
  226. faultsim_delete_and_reopen
  227. execsql {
  228. CREATE TABLE t1(a PRIMARY KEY, b);
  229. CREATE TABLE counter(
  230. i CHECK (i<5),
  231. u CHECK (u<10)
  232. );
  233. INSERT INTO counter VALUES(0, 0);
  234. CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
  235. UPDATE counter SET i = i+1;
  236. END;
  237. CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN
  238. UPDATE counter SET u = u+1;
  239. END;
  240. }
  241. execsql { SELECT * FROM counter }
  242. } {0 0}
  243. do_execsql_test pager1-3.1.2 {
  244. PRAGMA cache_size = 10;
  245. BEGIN;
  246. INSERT INTO t1 VALUES(1, randomblob(1500));
  247. INSERT INTO t1 VALUES(2, randomblob(1500));
  248. INSERT INTO t1 VALUES(3, randomblob(1500));
  249. SELECT * FROM counter;
  250. } {3 0}
  251. do_catchsql_test pager1-3.1.3 {
  252. INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1
  253. } {1 {constraint failed}}
  254. do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
  255. do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
  256. do_execsql_test pager1-3.6 { COMMIT } {}
  257. foreach {tn sql tcl} {
  258. 7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } {
  259. testvfs tv -default 1
  260. tv devchar safe_append
  261. }
  262. 8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } {
  263. testvfs tv -default 1
  264. tv devchar sequential
  265. }
  266. 9 { PRAGMA synchronous = FULL } { }
  267. 10 { PRAGMA synchronous = NORMAL } { }
  268. 11 { PRAGMA synchronous = OFF } { }
  269. 12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { }
  270. 13 { PRAGMA synchronous = FULL } {
  271. testvfs tv -default 1
  272. tv devchar sequential
  273. }
  274. 14 { PRAGMA locking_mode = EXCLUSIVE } {
  275. }
  276. } {
  277. do_test pager1-3.$tn.1 {
  278. eval $tcl
  279. faultsim_delete_and_reopen
  280. db func a_string a_string
  281. execsql $sql
  282. execsql {
  283. PRAGMA auto_vacuum = 2;
  284. PRAGMA cache_size = 10;
  285. CREATE TABLE z(x INTEGER PRIMARY KEY, y);
  286. BEGIN;
  287. INSERT INTO z VALUES(NULL, a_string(800));
  288. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2
  289. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4
  290. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8
  291. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16
  292. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32
  293. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64
  294. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128
  295. INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256
  296. COMMIT;
  297. }
  298. execsql { PRAGMA auto_vacuum }
  299. } {2}
  300. do_execsql_test pager1-3.$tn.2 {
  301. BEGIN;
  302. INSERT INTO z VALUES(NULL, a_string(800));
  303. INSERT INTO z VALUES(NULL, a_string(800));
  304. SAVEPOINT one;
  305. UPDATE z SET y = NULL WHERE x>256;
  306. PRAGMA incremental_vacuum;
  307. SELECT count(*) FROM z WHERE x < 100;
  308. ROLLBACK TO one;
  309. COMMIT;
  310. } {99}
  311. do_execsql_test pager1-3.$tn.3 {
  312. BEGIN;
  313. SAVEPOINT one;
  314. UPDATE z SET y = y||x;
  315. ROLLBACK TO one;
  316. COMMIT;
  317. SELECT count(*) FROM z;
  318. } {258}
  319. do_execsql_test pager1-3.$tn.4 {
  320. SAVEPOINT one;
  321. UPDATE z SET y = y||x;
  322. ROLLBACK TO one;
  323. } {}
  324. do_execsql_test pager1-3.$tn.5 {
  325. SELECT count(*) FROM z;
  326. RELEASE one;
  327. PRAGMA integrity_check;
  328. } {258 ok}
  329. do_execsql_test pager1-3.$tn.6 {
  330. SAVEPOINT one;
  331. RELEASE one;
  332. } {}
  333. db close
  334. catch { tv delete }
  335. }
  336. #-------------------------------------------------------------------------
  337. # Hot journal rollback related test cases.
  338. #
  339. # pager1.4.1.*: Test that the pager module deletes very small invalid
  340. # journal files.
  341. #
  342. # pager1.4.2.*: Test that if the master journal pointer at the end of a
  343. # hot-journal file appears to be corrupt (checksum does not
  344. # compute) the associated journal is rolled back (and no
  345. # xAccess() call to check for the presence of any master
  346. # journal file is made).
  347. #
  348. # pager1.4.3.*: Test that the contents of a hot-journal are ignored if the
  349. # page-size or sector-size in the journal header appear to
  350. # be invalid (too large, too small or not a power of 2).
  351. #
  352. # pager1.4.4.*: Test hot-journal rollback of journal file with a master
  353. # journal pointer generated in various "PRAGMA synchronous"
  354. # modes.
  355. #
  356. # pager1.4.5.*: Test that hot-journal rollback stops if it encounters a
  357. # journal-record for which the checksum fails.
  358. #
  359. # pager1.4.6.*: Test that when rolling back a hot-journal that contains a
  360. # master journal pointer, the master journal file is deleted
  361. # after all the hot-journals that refer to it are deleted.
  362. #
  363. # pager1.4.7.*: Test that if a hot-journal file exists but a client can
  364. # open it for reading only, the database cannot be accessed and
  365. # SQLITE_CANTOPEN is returned.
  366. #
  367. do_test pager1.4.1.1 {
  368. faultsim_delete_and_reopen
  369. execsql {
  370. CREATE TABLE x(y, z);
  371. INSERT INTO x VALUES(1, 2);
  372. }
  373. set fd [open test.db-journal w]
  374. puts -nonewline $fd "helloworld"
  375. close $fd
  376. file exists test.db-journal
  377. } {1}
  378. do_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2}
  379. do_test pager1.4.1.3 { file exists test.db-journal } {0}
  380. # Set up a [testvfs] to snapshot the file-system just before SQLite
  381. # deletes the master-journal to commit a multi-file transaction.
  382. #
  383. # In subsequent test cases, invoking [faultsim_restore_and_reopen] sets
  384. # up the file system to contain two databases, two hot-journal files and
  385. # a master-journal.
  386. #
  387. do_test pager1.4.2.1 {
  388. testvfs tstvfs -default 1
  389. tstvfs filter xDelete
  390. tstvfs script xDeleteCallback
  391. proc xDeleteCallback {method file args} {
  392. set file [file tail $file]
  393. if { [string match *mj* $file] } { faultsim_save }
  394. }
  395. faultsim_delete_and_reopen
  396. db func a_string a_string
  397. execsql {
  398. ATTACH 'test.db2' AS aux;
  399. PRAGMA journal_mode = DELETE;
  400. PRAGMA main.cache_size = 10;
  401. PRAGMA aux.cache_size = 10;
  402. CREATE TABLE t1(a UNIQUE, b UNIQUE);
  403. CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
  404. INSERT INTO t1 VALUES(a_string(200), a_string(300));
  405. INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
  406. INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
  407. INSERT INTO t2 SELECT * FROM t1;
  408. BEGIN;
  409. INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
  410. INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
  411. INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
  412. INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
  413. REPLACE INTO t2 SELECT * FROM t1;
  414. COMMIT;
  415. }
  416. db close
  417. tstvfs delete
  418. } {}
  419. if {$::tcl_platform(platform)!="windows"} {
  420. do_test pager1.4.2.2 {
  421. faultsim_restore_and_reopen
  422. execsql {
  423. SELECT count(*) FROM t1;
  424. PRAGMA integrity_check;
  425. }
  426. } {4 ok}
  427. do_test pager1.4.2.3 {
  428. faultsim_restore_and_reopen
  429. foreach f [glob test.db-mj*] { forcedelete $f }
  430. execsql {
  431. SELECT count(*) FROM t1;
  432. PRAGMA integrity_check;
  433. }
  434. } {64 ok}
  435. do_test pager1.4.2.4 {
  436. faultsim_restore_and_reopen
  437. hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
  438. execsql {
  439. SELECT count(*) FROM t1;
  440. PRAGMA integrity_check;
  441. }
  442. } {4 ok}
  443. do_test pager1.4.2.5 {
  444. faultsim_restore_and_reopen
  445. hexio_write test.db-journal [expr [file size test.db-journal]-30] 123456
  446. foreach f [glob test.db-mj*] { forcedelete $f }
  447. execsql {
  448. SELECT count(*) FROM t1;
  449. PRAGMA integrity_check;
  450. }
  451. } {4 ok}
  452. }
  453. do_test pager1.4.3.1 {
  454. testvfs tstvfs -default 1
  455. tstvfs filter xSync
  456. tstvfs script xSyncCallback
  457. proc xSyncCallback {method file args} {
  458. set file [file tail $file]
  459. if { 0==[string match *journal $file] } { faultsim_save }
  460. }
  461. faultsim_delete_and_reopen
  462. execsql {
  463. PRAGMA journal_mode = DELETE;
  464. CREATE TABLE t1(a, b);
  465. INSERT INTO t1 VALUES(1, 2);
  466. INSERT INTO t1 VALUES(3, 4);
  467. }
  468. db close
  469. tstvfs delete
  470. } {}
  471. foreach {tn ofst value result} {
  472. 2 20 31 {1 2 3 4}
  473. 3 20 32 {1 2 3 4}
  474. 4 20 33 {1 2 3 4}
  475. 5 20 65536 {1 2 3 4}
  476. 6 20 131072 {1 2 3 4}
  477. 7 24 511 {1 2 3 4}
  478. 8 24 513 {1 2 3 4}
  479. 9 24 131072 {1 2 3 4}
  480. 10 32 65536 {1 2}
  481. } {
  482. do_test pager1.4.3.$tn {
  483. faultsim_restore_and_reopen
  484. hexio_write test.db-journal $ofst [format %.8x $value]
  485. execsql { SELECT * FROM t1 }
  486. } $result
  487. }
  488. db close
  489. # Set up a VFS that snapshots the file-system just before a master journal
  490. # file is deleted to commit a multi-file transaction. Specifically, the
  491. # file-system is saved just before the xDelete() call to remove the
  492. # master journal file from the file-system.
  493. #
  494. set pwd [get_pwd]
  495. testvfs tv -default 1
  496. tv script copy_on_mj_delete
  497. set ::mj_filename_length 0
  498. proc copy_on_mj_delete {method filename args} {
  499. if {[string match *mj* [file tail $filename]]} {
  500. #
  501. # NOTE: Is the file name relative? If so, add the length of the current
  502. # directory.
  503. #
  504. if {[is_relative_file $filename]} {
  505. set ::mj_filename_length \
  506. [expr {[string length $filename] + [string length $::pwd]}]
  507. } else {
  508. set ::mj_filename_length [string length $filename]
  509. }
  510. faultsim_save
  511. }
  512. return SQLITE_OK
  513. }
  514. foreach {tn1 tcl} {
  515. 1 { set prefix "test.db" }
  516. 2 {
  517. # This test depends on the underlying VFS being able to open paths
  518. # 512 bytes in length. The idea is to create a hot-journal file that
  519. # contains a master-journal pointer so large that it could contain
  520. # a valid page record (if the file page-size is 512 bytes). So as to
  521. # make sure SQLite doesn't get confused by this.
  522. #
  523. set nPadding [expr 511 - $::mj_filename_length]
  524. if {$tcl_platform(platform)=="windows"} {
  525. # TBD need to figure out how to do this correctly for Windows!!!
  526. set nPadding [expr 255 - $::mj_filename_length]
  527. }
  528. # We cannot just create a really long database file name to open, as
  529. # Linux limits a single component of a path to 255 bytes by default
  530. # (and presumably other systems have limits too). So create a directory
  531. # hierarchy to work in.
  532. #
  533. set dirname "d123456789012345678901234567890/"
  534. set nDir [expr $nPadding / 32]
  535. if { $nDir } {
  536. set p [string repeat $dirname $nDir]
  537. file mkdir $p
  538. cd $p
  539. }
  540. set padding [string repeat x [expr $nPadding %32]]
  541. set prefix "test.db${padding}"
  542. }
  543. } {
  544. eval $tcl
  545. foreach {tn2 sql} {
  546. o {
  547. PRAGMA main.synchronous=OFF;
  548. PRAGMA aux.synchronous=OFF;
  549. PRAGMA journal_mode = DELETE;
  550. }
  551. o512 {
  552. PRAGMA main.synchronous=OFF;
  553. PRAGMA aux.synchronous=OFF;
  554. PRAGMA main.page_size = 512;
  555. PRAGMA aux.page_size = 512;
  556. PRAGMA journal_mode = DELETE;
  557. }
  558. n {
  559. PRAGMA main.synchronous=NORMAL;
  560. PRAGMA aux.synchronous=NORMAL;
  561. PRAGMA journal_mode = DELETE;
  562. }
  563. f {
  564. PRAGMA main.synchronous=FULL;
  565. PRAGMA aux.synchronous=FULL;
  566. PRAGMA journal_mode = DELETE;
  567. }
  568. } {
  569. set tn "${tn1}.${tn2}"
  570. # Set up a connection to have two databases, test.db (main) and
  571. # test.db2 (aux). Then run a multi-file transaction on them. The
  572. # VFS will snapshot the file-system just before the master-journal
  573. # file is deleted to commit the transaction.
  574. #
  575. tv filter xDelete
  576. do_test pager1-4.4.$tn.1 {
  577. faultsim_delete_and_reopen $prefix
  578. execsql "
  579. ATTACH '${prefix}2' AS aux;
  580. $sql
  581. CREATE TABLE a(x);
  582. CREATE TABLE aux.b(x);
  583. INSERT INTO a VALUES('double-you');
  584. INSERT INTO a VALUES('why');
  585. INSERT INTO a VALUES('zed');
  586. INSERT INTO b VALUES('won');
  587. INSERT INTO b VALUES('too');
  588. INSERT INTO b VALUES('free');
  589. "
  590. execsql {
  591. BEGIN;
  592. INSERT INTO a SELECT * FROM b WHERE rowid<=3;
  593. INSERT INTO b SELECT * FROM a WHERE rowid<=3;
  594. COMMIT;
  595. }
  596. } {}
  597. tv filter {}
  598. # Check that the transaction was committed successfully.
  599. #
  600. do_execsql_test pager1-4.4.$tn.2 {
  601. SELECT * FROM a
  602. } {double-you why zed won too free}
  603. do_execsql_test pager1-4.4.$tn.3 {
  604. SELECT * FROM b
  605. } {won too free double-you why zed}
  606. # Restore the file-system and reopen the databases. Check that it now
  607. # appears that the transaction was not committed (because the file-system
  608. # was restored to the state where it had not been).
  609. #
  610. do_test pager1-4.4.$tn.4 {
  611. faultsim_restore_and_reopen $prefix
  612. execsql "ATTACH '${prefix}2' AS aux"
  613. } {}
  614. do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed}
  615. do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free}
  616. # Restore the file-system again. This time, before reopening the databases,
  617. # delete the master-journal file from the file-system. It now appears that
  618. # the transaction was committed (no master-journal file == no rollback).
  619. #
  620. do_test pager1-4.4.$tn.7 {
  621. faultsim_restore_and_reopen $prefix
  622. foreach f [glob ${prefix}-mj*] { forcedelete $f }
  623. execsql "ATTACH '${prefix}2' AS aux"
  624. } {}
  625. do_execsql_test pager1-4.4.$tn.8 {
  626. SELECT * FROM a
  627. } {double-you why zed won too free}
  628. do_execsql_test pager1-4.4.$tn.9 {
  629. SELECT * FROM b
  630. } {won too free double-you why zed}
  631. }
  632. cd $pwd
  633. }
  634. db close
  635. tv delete
  636. forcedelete $dirname
  637. # Set up a VFS to make a copy of the file-system just before deleting a
  638. # journal file to commit a transaction. The transaction modifies exactly
  639. # two database pages (and page 1 - the change counter).
  640. #
  641. testvfs tv -default 1
  642. tv sectorsize 512
  643. tv script copy_on_journal_delete
  644. tv filter xDelete
  645. proc copy_on_journal_delete {method filename args} {
  646. if {[string match *journal $filename]} faultsim_save
  647. return SQLITE_OK
  648. }
  649. faultsim_delete_and_reopen
  650. do_execsql_test pager1.4.5.1 {
  651. PRAGMA journal_mode = DELETE;
  652. PRAGMA page_size = 1024;
  653. CREATE TABLE t1(a, b);
  654. CREATE TABLE t2(a, b);
  655. INSERT INTO t1 VALUES('I', 'II');
  656. INSERT INTO t2 VALUES('III', 'IV');
  657. BEGIN;
  658. INSERT INTO t1 VALUES(1, 2);
  659. INSERT INTO t2 VALUES(3, 4);
  660. COMMIT;
  661. } {delete}
  662. tv filter {}
  663. # Check the transaction was committed:
  664. #
  665. do_execsql_test pager1.4.5.2 {
  666. SELECT * FROM t1;
  667. SELECT * FROM t2;
  668. } {I II 1 2 III IV 3 4}
  669. # Now try four tests:
  670. #
  671. # pager1-4.5.3: Restore the file-system. Check that the whole transaction
  672. # is rolled back.
  673. #
  674. # pager1-4.5.4: Restore the file-system. Corrupt the first record in the
  675. # journal. Check the transaction is not rolled back.
  676. #
  677. # pager1-4.5.5: Restore the file-system. Corrupt the second record in the
  678. # journal. Check that the first record in the transaction is
  679. # played back, but not the second.
  680. #
  681. # pager1-4.5.6: Restore the file-system. Try to open the database with a
  682. # readonly connection. This should fail, as a read-only
  683. # connection cannot roll back the database file.
  684. #
  685. faultsim_restore_and_reopen
  686. do_execsql_test pager1.4.5.3 {
  687. SELECT * FROM t1;
  688. SELECT * FROM t2;
  689. } {I II III IV}
  690. faultsim_restore_and_reopen
  691. hexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF
  692. do_execsql_test pager1.4.5.4 {
  693. SELECT * FROM t1;
  694. SELECT * FROM t2;
  695. } {I II 1 2 III IV 3 4}
  696. faultsim_restore_and_reopen
  697. hexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF
  698. do_execsql_test pager1.4.5.5 {
  699. SELECT * FROM t1;
  700. SELECT * FROM t2;
  701. } {I II III IV 3 4}
  702. faultsim_restore_and_reopen
  703. db close
  704. sqlite3 db test.db -readonly 1
  705. do_catchsql_test pager1.4.5.6 {
  706. SELECT * FROM t1;
  707. SELECT * FROM t2;
  708. } {1 {attempt to write a readonly database}}
  709. db close
  710. # Snapshot the file-system just before multi-file commit. Save the name
  711. # of the master journal file in $::mj_filename.
  712. #
  713. tv script copy_on_mj_delete
  714. tv filter xDelete
  715. proc copy_on_mj_delete {method filename args} {
  716. if {[string match *mj* [file tail $filename]]} {
  717. set ::mj_filename $filename
  718. faultsim_save
  719. }
  720. return SQLITE_OK
  721. }
  722. do_test pager1.4.6.1 {
  723. faultsim_delete_and_reopen
  724. execsql {
  725. PRAGMA journal_mode = DELETE;
  726. ATTACH 'test.db2' AS two;
  727. CREATE TABLE t1(a, b);
  728. CREATE TABLE two.t2(a, b);
  729. INSERT INTO t1 VALUES(1, 't1.1');
  730. INSERT INTO t2 VALUES(1, 't2.1');
  731. BEGIN;
  732. UPDATE t1 SET b = 't1.2';
  733. UPDATE t2 SET b = 't2.2';
  734. COMMIT;
  735. }
  736. tv filter {}
  737. db close
  738. } {}
  739. faultsim_restore_and_reopen
  740. do_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1}
  741. do_test pager1.4.6.3 { file exists $::mj_filename } {1}
  742. do_execsql_test pager1.4.6.4 {
  743. ATTACH 'test.db2' AS two;
  744. SELECT * FROM t2;
  745. } {1 t2.1}
  746. do_test pager1.4.6.5 { file exists $::mj_filename } {0}
  747. faultsim_restore_and_reopen
  748. db close
  749. do_test pager1.4.6.8 {
  750. set ::mj_filename1 $::mj_filename
  751. tv filter xDelete
  752. sqlite3 db test.db2
  753. execsql {
  754. PRAGMA journal_mode = DELETE;
  755. ATTACH 'test.db3' AS three;
  756. CREATE TABLE three.t3(a, b);
  757. INSERT INTO t3 VALUES(1, 't3.1');
  758. BEGIN;
  759. UPDATE t2 SET b = 't2.3';
  760. UPDATE t3 SET b = 't3.3';
  761. COMMIT;
  762. }
  763. expr {$::mj_filename1 != $::mj_filename}
  764. } {1}
  765. faultsim_restore_and_reopen
  766. tv filter {}
  767. # The file-system now contains:
  768. #
  769. # * three databases
  770. # * three hot-journal files
  771. # * two master-journal files.
  772. #
  773. # The hot-journals associated with test.db2 and test.db3 point to
  774. # master journal $::mj_filename. The hot-journal file associated with
  775. # test.db points to master journal $::mj_filename1. So reading from
  776. # test.db should delete $::mj_filename1.
  777. #
  778. do_test pager1.4.6.9 {
  779. lsort [glob test.db*]
  780. } [lsort [list \
  781. test.db test.db2 test.db3 \
  782. test.db-journal test.db2-journal test.db3-journal \
  783. [file tail $::mj_filename] [file tail $::mj_filename1]
  784. ]]
  785. # The master-journal $::mj_filename1 contains pointers to test.db and
  786. # test.db2. However the hot-journal associated with test.db2 points to
  787. # a different master-journal. Therefore, reading from test.db only should
  788. # be enough to cause SQLite to delete $::mj_filename1.
  789. #
  790. do_test pager1.4.6.10 { file exists $::mj_filename } {1}
  791. do_test pager1.4.6.11 { file exists $::mj_filename1 } {1}
  792. do_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1}
  793. do_test pager1.4.6.13 { file exists $::mj_filename } {1}
  794. do_test pager1.4.6.14 { file exists $::mj_filename1 } {0}
  795. do_execsql_test pager1.4.6.12 {
  796. ATTACH 'test.db2' AS two;
  797. SELECT * FROM t2;
  798. } {1 t2.1}
  799. do_test pager1.4.6.13 { file exists $::mj_filename } {1}
  800. do_execsql_test pager1.4.6.14 {
  801. ATTACH 'test.db3' AS three;
  802. SELECT * FROM t3;
  803. } {1 t3.1}
  804. do_test pager1.4.6.15 { file exists $::mj_filename } {0}
  805. db close
  806. tv delete
  807. testvfs tv -default 1
  808. tv sectorsize 512
  809. tv script copy_on_journal_delete
  810. tv filter xDelete
  811. proc copy_on_journal_delete {method filename args} {
  812. if {[string match *journal $filename]} faultsim_save
  813. return SQLITE_OK
  814. }
  815. faultsim_delete_and_reopen
  816. do_execsql_test pager1.4.7.1 {
  817. PRAGMA journal_mode = DELETE;
  818. CREATE TABLE t1(x PRIMARY KEY, y);
  819. CREATE INDEX i1 ON t1(y);
  820. INSERT INTO t1 VALUES('I', 'one');
  821. INSERT INTO t1 VALUES('II', 'four');
  822. INSERT INTO t1 VALUES('III', 'nine');
  823. BEGIN;
  824. INSERT INTO t1 VALUES('IV', 'sixteen');
  825. INSERT INTO t1 VALUES('V' , 'twentyfive');
  826. COMMIT;
  827. } {delete}
  828. tv filter {}
  829. db close
  830. tv delete
  831. catch {
  832. test_syscall install fchmod
  833. test_syscall fault 1 1
  834. }
  835. do_test pager1.4.7.2 {
  836. faultsim_restore_and_reopen
  837. catch {file attributes test.db-journal -permissions r--------}
  838. catch {file attributes test.db-journal -readonly 1}
  839. catchsql { SELECT * FROM t1 }
  840. } {1 {unable to open database file}}
  841. catch {
  842. test_syscall reset
  843. test_syscall fault 0 0
  844. }
  845. do_test pager1.4.7.3 {
  846. db close
  847. catch {file attributes test.db-journal -permissions rw-rw-rw-}
  848. catch {file attributes test.db-journal -readonly 0}
  849. delete_file test.db-journal
  850. file exists test.db-journal
  851. } {0}
  852. do_test pager1.4.8.1 {
  853. catch {file attributes test.db -permissions r--------}
  854. catch {file attributes test.db -readonly 1}
  855. sqlite3 db test.db
  856. db eval { SELECT * FROM t1 }
  857. sqlite3_db_readonly db main
  858. } {1}
  859. do_test pager1.4.8.2 {
  860. sqlite3_db_readonly db xyz
  861. } {-1}
  862. do_test pager1.4.8.3 {
  863. db close
  864. catch {file attributes test.db -readonly 0}
  865. catch {file attributes test.db -permissions rw-rw-rw-} msg
  866. sqlite3 db test.db
  867. db eval { SELECT * FROM t1 }
  868. sqlite3_db_readonly db main
  869. } {0}
  870. #-------------------------------------------------------------------------
  871. # The following tests deal with multi-file commits.
  872. #
  873. # pager1-5.1.*: The case where a multi-file cannot be committed because
  874. # another connection is holding a SHARED lock on one of the
  875. # files. After the SHARED lock is removed, the COMMIT succeeds.
  876. #
  877. # pager1-5.2.*: Multi-file commits with journal_mode=memory.
  878. #
  879. # pager1-5.3.*: Multi-file commits with journal_mode=memory.
  880. #
  881. # pager1-5.4.*: Check that with synchronous=normal, the master-journal file
  882. # name is added to a journal file immediately after the last
  883. # journal record. But with synchronous=full, extra unused space
  884. # is allocated between the last journal record and the
  885. # master-journal file name so that the master-journal file
  886. # name does not lie on the same sector as the last journal file
  887. # record.
  888. #
  889. # pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is
  890. # truncated to zero bytes when a multi-file transaction is
  891. # committed (instead of the first couple of bytes being zeroed).
  892. #
  893. #
  894. do_test pager1-5.1.1 {
  895. faultsim_delete_and_reopen
  896. execsql {
  897. ATTACH 'test.db2' AS aux;
  898. CREATE TABLE t1(a, b);
  899. CREATE TABLE aux.t2(a, b);
  900. INSERT INTO t1 VALUES(17, 'Lenin');
  901. INSERT INTO t1 VALUES(22, 'Stalin');
  902. INSERT INTO t1 VALUES(53, 'Khrushchev');
  903. }
  904. } {}
  905. do_test pager1-5.1.2 {
  906. execsql {
  907. BEGIN;
  908. INSERT INTO t1 VALUES(64, 'Brezhnev');
  909. INSERT INTO t2 SELECT * FROM t1;
  910. }
  911. sqlite3 db2 test.db2
  912. execsql {
  913. BEGIN;
  914. SELECT * FROM t2;
  915. } db2
  916. } {}
  917. do_test pager1-5.1.3 {
  918. catchsql COMMIT
  919. } {1 {database is locked}}
  920. do_test pager1-5.1.4 {
  921. execsql COMMIT db2
  922. execsql COMMIT
  923. execsql { SELECT * FROM t2 } db2
  924. } {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev}
  925. do_test pager1-5.1.5 {
  926. db2 close
  927. } {}
  928. do_test pager1-5.2.1 {
  929. execsql {
  930. PRAGMA journal_mode = memory;
  931. BEGIN;
  932. INSERT INTO t1 VALUES(84, 'Andropov');
  933. INSERT INTO t2 VALUES(84, 'Andropov');
  934. COMMIT;
  935. }
  936. } {memory}
  937. do_test pager1-5.3.1 {
  938. execsql {
  939. PRAGMA journal_mode = off;
  940. BEGIN;
  941. INSERT INTO t1 VALUES(85, 'Gorbachev');
  942. INSERT INTO t2 VALUES(85, 'Gorbachev');
  943. COMMIT;
  944. }
  945. } {off}
  946. do_test pager1-5.4.1 {
  947. db close
  948. testvfs tv
  949. sqlite3 db test.db -vfs tv
  950. execsql { ATTACH 'test.db2' AS aux }
  951. tv filter xDelete
  952. tv script max_journal_size
  953. tv sectorsize 512
  954. set ::max_journal 0
  955. proc max_journal_size {method args} {
  956. set sz 0
  957. catch { set sz [file size test.db-journal] }
  958. if {$sz > $::max_journal} {
  959. set ::max_journal $sz
  960. }
  961. return SQLITE_OK
  962. }
  963. execsql {
  964. PRAGMA journal_mode = DELETE;
  965. PRAGMA synchronous = NORMAL;
  966. BEGIN;
  967. INSERT INTO t1 VALUES(85, 'Gorbachev');
  968. INSERT INTO t2 VALUES(85, 'Gorbachev');
  969. COMMIT;
  970. }
  971. # The size of the journal file is now:
  972. #
  973. # 1) 512 byte header +
  974. # 2) 2 * (1024+8) byte records +
  975. # 3) 20+N bytes of master-journal pointer, where N is the size of
  976. # the master-journal name encoded as utf-8 with no nul term.
  977. #
  978. set mj_pointer [expr {
  979. 20 + [string length "test.db-mjXXXXXX9XX"]
  980. }]
  981. #
  982. # NOTE: For item 3 above, if the current SQLite VFS lacks the concept of a
  983. # current directory, the length of the current directory name plus 1
  984. # character for the directory separator character are NOT counted as
  985. # part of the total size; otherwise, they are.
  986. #
  987. ifcapable curdir {
  988. set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
  989. }
  990. expr {$::max_journal==(512+2*(1024+8)+$mj_pointer)}
  991. } 1
  992. do_test pager1-5.4.2 {
  993. set ::max_journal 0
  994. execsql {
  995. PRAGMA synchronous = full;
  996. BEGIN;
  997. DELETE FROM t1 WHERE b = 'Lenin';
  998. DELETE FROM t2 WHERE b = 'Lenin';
  999. COMMIT;
  1000. }
  1001. # In synchronous=full mode, the master-journal pointer is not written
  1002. # directly after the last record in the journal file. Instead, it is
  1003. # written starting at the next (in this case 512 byte) sector boundary.
  1004. #
  1005. set mj_pointer [expr {
  1006. 20 + [string length "test.db-mjXXXXXX9XX"]
  1007. }]
  1008. #
  1009. # NOTE: If the current SQLite VFS lacks the concept of a current directory,
  1010. # the length of the current directory name plus 1 character for the
  1011. # directory separator character are NOT counted as part of the total
  1012. # size; otherwise, they are.
  1013. #
  1014. ifcapable curdir {
  1015. set mj_pointer [expr {$mj_pointer + [string length [get_pwd]] + 1}]
  1016. }
  1017. expr {$::max_journal==(((512+2*(1024+8)+511)/512)*512 + $mj_pointer)}
  1018. } 1
  1019. db close
  1020. tv delete
  1021. do_test pager1-5.5.1 {
  1022. sqlite3 db test.db
  1023. execsql {
  1024. ATTACH 'test.db2' AS aux;
  1025. PRAGMA journal_mode = PERSIST;
  1026. CREATE TABLE t3(a, b);
  1027. INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1;
  1028. UPDATE t3 SET b = randomblob(1500);
  1029. }
  1030. expr [file size test.db-journal] > 15000
  1031. } {1}
  1032. do_test pager1-5.5.2 {
  1033. execsql {
  1034. PRAGMA synchronous = full;
  1035. BEGIN;
  1036. DELETE FROM t1 WHERE b = 'Stalin';
  1037. DELETE FROM t2 WHERE b = 'Stalin';
  1038. COMMIT;
  1039. }
  1040. file size test.db-journal
  1041. } {0}
  1042. #-------------------------------------------------------------------------
  1043. # The following tests work with "PRAGMA max_page_count"
  1044. #
  1045. do_test pager1-6.1 {
  1046. faultsim_delete_and_reopen
  1047. execsql {
  1048. PRAGMA auto_vacuum = none;
  1049. PRAGMA max_page_count = 10;
  1050. CREATE TABLE t2(a, b);
  1051. CREATE TABLE t3(a, b);
  1052. CREATE TABLE t4(a, b);
  1053. CREATE TABLE t5(a, b);
  1054. CREATE TABLE t6(a, b);
  1055. CREATE TABLE t7(a, b);
  1056. CREATE TABLE t8(a, b);
  1057. CREATE TABLE t9(a, b);
  1058. CREATE TABLE t10(a, b);
  1059. }
  1060. } {10}
  1061. do_catchsql_test pager1-6.2 {
  1062. CREATE TABLE t11(a, b)
  1063. } {1 {database or disk is full}}
  1064. do_execsql_test pager1-6.4 { PRAGMA max_page_count } {10}
  1065. do_execsql_test pager1-6.5 { PRAGMA max_page_count = 15 } {15}
  1066. do_execsql_test pager1-6.6 { CREATE TABLE t11(a, b) } {}
  1067. do_execsql_test pager1-6.7 {
  1068. BEGIN;
  1069. INSERT INTO t11 VALUES(1, 2);
  1070. PRAGMA max_page_count = 13;
  1071. } {13}
  1072. do_execsql_test pager1-6.8 {
  1073. INSERT INTO t11 VALUES(3, 4);
  1074. PRAGMA max_page_count = 10;
  1075. } {11}
  1076. do_execsql_test pager1-6.9 { COMMIT } {}
  1077. do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {11}
  1078. do_execsql_test pager1-6.11 { SELECT * FROM t11 } {1 2 3 4}
  1079. do_execsql_test pager1-6.12 { PRAGMA max_page_count } {11}
  1080. #-------------------------------------------------------------------------
  1081. # The following tests work with "PRAGMA journal_mode=TRUNCATE" and
  1082. # "PRAGMA locking_mode=EXCLUSIVE".
  1083. #
  1084. # Each test is specified with 5 variables. As follows:
  1085. #
  1086. # $tn: Test Number. Used as part of the [do_test] test names.
  1087. # $sql: SQL to execute.
  1088. # $res: Expected result of executing $sql.
  1089. # $js: The expected size of the journal file, in bytes, after executing
  1090. # the SQL script. Or -1 if the journal is not expected to exist.
  1091. # $ws: The expected size of the WAL file, in bytes, after executing
  1092. # the SQL script. Or -1 if the WAL is not expected to exist.
  1093. #
  1094. ifcapable wal {
  1095. faultsim_delete_and_reopen
  1096. foreach {tn sql res js ws} [subst {
  1097. 1 {
  1098. CREATE TABLE t1(a, b);
  1099. PRAGMA auto_vacuum=OFF;
  1100. PRAGMA synchronous=NORMAL;
  1101. PRAGMA page_size=1024;
  1102. PRAGMA locking_mode=EXCLUSIVE;
  1103. PRAGMA journal_mode=TRUNCATE;
  1104. INSERT INTO t1 VALUES(1, 2);
  1105. } {exclusive truncate} 0 -1
  1106. 2 {
  1107. BEGIN IMMEDIATE;
  1108. SELECT * FROM t1;
  1109. COMMIT;
  1110. } {1 2} 0 -1
  1111. 3 {
  1112. BEGIN;
  1113. SELECT * FROM t1;
  1114. COMMIT;
  1115. } {1 2} 0 -1
  1116. 4 { PRAGMA journal_mode = WAL } wal -1 -1
  1117. 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024]
  1118. 6 { PRAGMA locking_mode = NORMAL } exclusive -1 [wal_file_size 1 1024]
  1119. 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024]
  1120. 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1
  1121. 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1
  1122. 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1
  1123. }] {
  1124. do_execsql_test pager1-7.1.$tn.1 $sql $res
  1125. catch { set J -1 ; set J [file size test.db-journal] }
  1126. catch { set W -1 ; set W [file size test.db-wal] }
  1127. do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
  1128. }
  1129. }
  1130. do_test pager1-7.2.1 {
  1131. faultsim_delete_and_reopen
  1132. execsql {
  1133. PRAGMA locking_mode = EXCLUSIVE;
  1134. CREATE TABLE t1(a, b);
  1135. BEGIN;
  1136. PRAGMA journal_mode = delete;
  1137. PRAGMA journal_mode = truncate;
  1138. }
  1139. } {exclusive delete truncate}
  1140. do_test pager1-7.2.2 {
  1141. execsql { INSERT INTO t1 VALUES(1, 2) }
  1142. execsql { PRAGMA journal_mode = persist }
  1143. } {truncate}
  1144. do_test pager1-7.2.3 {
  1145. execsql { COMMIT }
  1146. execsql {
  1147. PRAGMA journal_mode = persist;
  1148. PRAGMA journal_size_limit;
  1149. }
  1150. } {persist -1}
  1151. #-------------------------------------------------------------------------
  1152. # The following tests, pager1-8.*, test that the special filenames
  1153. # ":memory:" and "" open temporary databases.
  1154. #
  1155. foreach {tn filename} {
  1156. 1 :memory:
  1157. 2 ""
  1158. } {
  1159. do_test pager1-8.$tn.1 {
  1160. faultsim_delete_and_reopen
  1161. db close
  1162. sqlite3 db $filename
  1163. execsql {
  1164. PRAGMA auto_vacuum = 1;
  1165. CREATE TABLE x1(x);
  1166. INSERT INTO x1 VALUES('Charles');
  1167. INSERT INTO x1 VALUES('James');
  1168. INSERT INTO x1 VALUES('Mary');
  1169. SELECT * FROM x1;
  1170. }
  1171. } {Charles James Mary}
  1172. do_test pager1-8.$tn.2 {
  1173. sqlite3 db2 $filename
  1174. catchsql { SELECT * FROM x1 } db2
  1175. } {1 {no such table: x1}}
  1176. do_execsql_test pager1-8.$tn.3 {
  1177. BEGIN;
  1178. INSERT INTO x1 VALUES('William');
  1179. INSERT INTO x1 VALUES('Anne');
  1180. ROLLBACK;
  1181. } {}
  1182. }
  1183. #-------------------------------------------------------------------------
  1184. # The next block of tests - pager1-9.* - deal with interactions between
  1185. # the pager and the backup API. Test cases:
  1186. #
  1187. # pager1-9.1.*: Test that a backup completes successfully even if the
  1188. # source db is written to during the backup op.
  1189. #
  1190. # pager1-9.2.*: Test that a backup completes successfully even if the
  1191. # source db is written to and then rolled back during a
  1192. # backup operation.
  1193. #
  1194. do_test pager1-9.0.1 {
  1195. faultsim_delete_and_reopen
  1196. db func a_string a_string
  1197. execsql {
  1198. PRAGMA cache_size = 10;
  1199. BEGIN;
  1200. CREATE TABLE ab(a, b, UNIQUE(a, b));
  1201. INSERT INTO ab VALUES( a_string(200), a_string(300) );
  1202. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1203. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1204. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1205. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1206. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1207. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1208. INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
  1209. COMMIT;
  1210. }
  1211. } {}
  1212. do_test pager1-9.0.2 {
  1213. sqlite3 db2 test.db2
  1214. db2 eval { PRAGMA cache_size = 10 }
  1215. sqlite3_backup B db2 main db main
  1216. list [B step 10000] [B finish]
  1217. } {SQLITE_DONE SQLITE_OK}
  1218. do_test pager1-9.0.3 {
  1219. db one {SELECT md5sum(a, b) FROM ab}
  1220. } [db2 one {SELECT md5sum(a, b) FROM ab}]
  1221. do_test pager1-9.1.1 {
  1222. execsql { UPDATE ab SET a = a_string(201) }
  1223. sqlite3_backup B db2 main db main
  1224. B step 30
  1225. } {SQLITE_OK}
  1226. do_test pager1-9.1.2 {
  1227. execsql { UPDATE ab SET b = a_string(301) }
  1228. list [B step 10000] [B finish]
  1229. } {SQLITE_DONE SQLITE_OK}
  1230. do_test pager1-9.1.3 {
  1231. db one {SELECT md5sum(a, b) FROM ab}
  1232. } [db2 one {SELECT md5sum(a, b) FROM ab}]
  1233. do_test pager1-9.1.4 { execsql { SELECT count(*) FROM ab } } {128}
  1234. do_test pager1-9.2.1 {
  1235. execsql { UPDATE ab SET a = a_string(202) }
  1236. sqlite3_backup B db2 main db main
  1237. B step 30
  1238. } {SQLITE_OK}
  1239. do_test pager1-9.2.2 {
  1240. execsql {
  1241. BEGIN;
  1242. UPDATE ab SET b = a_string(301);
  1243. ROLLBACK;
  1244. }
  1245. list [B step 10000] [B finish]
  1246. } {SQLITE_DONE SQLITE_OK}
  1247. do_test pager1-9.2.3 {
  1248. db one {SELECT md5sum(a, b) FROM ab}
  1249. } [db2 one {SELECT md5sum(a, b) FROM ab}]
  1250. do_test pager1-9.2.4 { execsql { SELECT count(*) FROM ab } } {128}
  1251. db close
  1252. db2 close
  1253. do_test pager1-9.3.1 {
  1254. testvfs tv -default 1
  1255. tv sectorsize 4096
  1256. faultsim_delete_and_reopen
  1257. execsql { PRAGMA page_size = 1024 }
  1258. for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" }
  1259. } {}
  1260. do_test pager1-9.3.2 {
  1261. sqlite3 db2 test.db2
  1262. execsql {
  1263. PRAGMA page_size = 4096;
  1264. PRAGMA synchronous = OFF;
  1265. CREATE TABLE t1(a, b);
  1266. CREATE TABLE t2(a, b);
  1267. } db2
  1268. sqlite3_backup B db2 main db main
  1269. B step 30
  1270. list [B step 10000] [B finish]
  1271. } {SQLITE_DONE SQLITE_OK}
  1272. do_test pager1-9.3.3 {
  1273. db2 close
  1274. db close
  1275. tv delete
  1276. file size test.db2
  1277. } [file size test.db]
  1278. do_test pager1-9.4.1 {
  1279. faultsim_delete_and_reopen
  1280. sqlite3 db2 test.db2
  1281. execsql {
  1282. PRAGMA page_size = 4096;
  1283. CREATE TABLE t1(a, b);
  1284. CREATE TABLE t2(a, b);
  1285. } db2
  1286. sqlite3_backup B db2 main db main
  1287. list [B step 10000] [B finish]
  1288. } {SQLITE_DONE SQLITE_OK}
  1289. do_test pager1-9.4.2 {
  1290. list [file size test.db2] [file size test.db]
  1291. } {1024 0}
  1292. db2 close
  1293. #-------------------------------------------------------------------------
  1294. # Test that regardless of the value returned by xSectorSize(), the
  1295. # minimum effective sector-size is 512 and the maximum 65536 bytes.
  1296. #
  1297. testvfs tv -default 1
  1298. foreach sectorsize {
  1299. 16
  1300. 32 64 128 256 512 1024 2048
  1301. 4096 8192 16384 32768 65536 131072 262144
  1302. } {
  1303. tv sectorsize $sectorsize
  1304. tv devchar {}
  1305. set eff $sectorsize
  1306. if {$sectorsize < 512} { set eff 512 }
  1307. if {$sectorsize > 65536} { set eff 65536 }
  1308. do_test pager1-10.$sectorsize.1 {
  1309. faultsim_delete_and_reopen
  1310. db func a_string a_string
  1311. execsql {
  1312. PRAGMA journal_mode = PERSIST;
  1313. PRAGMA page_size = 1024;
  1314. BEGIN;
  1315. CREATE TABLE t1(a, b);
  1316. CREATE TABLE t2(a, b);
  1317. CREATE TABLE t3(a, b);
  1318. COMMIT;
  1319. }
  1320. file size test.db-journal
  1321. } [expr $sectorsize > 65536 ? 65536 : ($sectorsize<32 ? 512 : $sectorsize)]
  1322. do_test pager1-10.$sectorsize.2 {
  1323. execsql {
  1324. INSERT INTO t3 VALUES(a_string(300), a_string(300));
  1325. INSERT INTO t3 SELECT * FROM t3; /* 2 */
  1326. INSERT INTO t3 SELECT * FROM t3; /* 4 */
  1327. INSERT INTO t3 SELECT * FROM t3; /* 8 */
  1328. INSERT INTO t3 SELECT * FROM t3; /* 16 */
  1329. INSERT INTO t3 SELECT * FROM t3; /* 32 */
  1330. }
  1331. } {}
  1332. do_test pager1-10.$sectorsize.3 {
  1333. db close
  1334. sqlite3 db test.db
  1335. execsql {
  1336. PRAGMA cache_size = 10;
  1337. BEGIN;
  1338. }
  1339. recursive_select 32 t3 {db eval "INSERT INTO t2 VALUES(1, 2)"}
  1340. execsql {
  1341. COMMIT;
  1342. SELECT * FROM t2;
  1343. }
  1344. } {1 2}
  1345. do_test pager1-10.$sectorsize.4 {
  1346. execsql {
  1347. CREATE TABLE t6(a, b);
  1348. CREATE TABLE t7(a, b);
  1349. CREATE TABLE t5(a, b);
  1350. DROP TABLE t6;
  1351. DROP TABLE t7;
  1352. }
  1353. execsql {
  1354. BEGIN;
  1355. CREATE TABLE t6(a, b);
  1356. }
  1357. recursive_select 32 t3 {db eval "INSERT INTO t5 VALUES(1, 2)"}
  1358. execsql {
  1359. COMMIT;
  1360. SELECT * FROM t5;
  1361. }
  1362. } {1 2}
  1363. }
  1364. db close
  1365. tv sectorsize 4096
  1366. do_test pager1.10.x.1 {
  1367. faultsim_delete_and_reopen
  1368. execsql {
  1369. PRAGMA auto_vacuum = none;
  1370. PRAGMA page_size = 1024;
  1371. CREATE TABLE t1(x);
  1372. }
  1373. for {set i 0} {$i<30} {incr i} {
  1374. execsql { INSERT INTO t1 VALUES(zeroblob(900)) }
  1375. }
  1376. file size test.db
  1377. } {32768}
  1378. do_test pager1.10.x.2 {
  1379. execsql {
  1380. CREATE TABLE t2(x);
  1381. DROP TABLE t2;
  1382. }
  1383. file size test.db
  1384. } {33792}
  1385. do_test pager1.10.x.3 {
  1386. execsql {
  1387. BEGIN;
  1388. CREATE TABLE t2(x);
  1389. }
  1390. recursive_select 30 t1
  1391. execsql {
  1392. CREATE TABLE t3(x);
  1393. COMMIT;
  1394. }
  1395. } {}
  1396. db close
  1397. tv delete
  1398. testvfs tv -default 1
  1399. faultsim_delete_and_reopen
  1400. db func a_string a_string
  1401. do_execsql_test pager1-11.1 {
  1402. PRAGMA journal_mode = DELETE;
  1403. PRAGMA cache_size = 10;
  1404. BEGIN;
  1405. CREATE TABLE zz(top PRIMARY KEY);
  1406. INSERT INTO zz VALUES(a_string(222));
  1407. INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
  1408. INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
  1409. INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
  1410. INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
  1411. INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
  1412. COMMIT;
  1413. BEGIN;
  1414. UPDATE zz SET top = a_string(345);
  1415. } {delete}
  1416. proc lockout {method args} { return SQLITE_IOERR }
  1417. tv script lockout
  1418. tv filter {xWrite xTruncate xSync}
  1419. do_catchsql_test pager1-11.2 { COMMIT } {1 {disk I/O error}}
  1420. tv script {}
  1421. do_test pager1-11.3 {
  1422. sqlite3 db2 test.db
  1423. execsql {
  1424. PRAGMA journal_mode = TRUNCATE;
  1425. PRAGMA integrity_check;
  1426. } db2
  1427. } {truncate ok}
  1428. do_test pager1-11.4 {
  1429. db2 close
  1430. file exists test.db-journal
  1431. } {0}
  1432. do_execsql_test pager1-11.5 { SELECT count(*) FROM zz } {32}
  1433. db close
  1434. tv delete
  1435. #-------------------------------------------------------------------------
  1436. # Test "PRAGMA page_size"
  1437. #
  1438. testvfs tv -default 1
  1439. tv sectorsize 1024
  1440. foreach pagesize {
  1441. 512 1024 2048 4096 8192 16384 32768
  1442. } {
  1443. faultsim_delete_and_reopen
  1444. # The sector-size (according to the VFS) is 1024 bytes. So if the
  1445. # page-size requested using "PRAGMA page_size" is greater than the
  1446. # compile time value of SQLITE_MAX_PAGE_SIZE, then the effective
  1447. # page-size remains 1024 bytes.
  1448. #
  1449. set eff $pagesize
  1450. if {$eff > $::SQLITE_MAX_PAGE_SIZE} { set eff 1024 }
  1451. do_test pager1-12.$pagesize.1 {
  1452. sqlite3 db2 test.db
  1453. execsql "
  1454. PRAGMA page_size = $pagesize;
  1455. CREATE VIEW v AS SELECT * FROM sqlite_master;
  1456. " db2
  1457. file size test.db
  1458. } $eff
  1459. do_test pager1-12.$pagesize.2 {
  1460. sqlite3 db2 test.db
  1461. execsql {
  1462. SELECT count(*) FROM v;
  1463. PRAGMA main.page_size;
  1464. } db2
  1465. } [list 1 $eff]
  1466. do_test pager1-12.$pagesize.3 {
  1467. execsql {
  1468. SELECT count(*) FROM v;
  1469. PRAGMA main.page_size;
  1470. }
  1471. } [list 1 $eff]
  1472. db2 close
  1473. }
  1474. db close
  1475. tv delete
  1476. #-------------------------------------------------------------------------
  1477. # Test specal "PRAGMA journal_mode=PERSIST" test cases.
  1478. #
  1479. # pager1-13.1.*: This tests a special case encountered in persistent
  1480. # journal mode: If the journal associated with a transaction
  1481. # is smaller than the journal file (because a previous
  1482. # transaction left a very large non-hot journal file in the
  1483. # file-system), then SQLite has to be careful that there is
  1484. # not a journal-header left over from a previous transaction
  1485. # immediately following the journal content just written.
  1486. # If there is, and the process crashes so that the journal
  1487. # becomes a hot-journal and must be rolled back by another
  1488. # process, there is a danger that the other process may roll
  1489. # back the aborted transaction, then continue copying data
  1490. # from an older transaction from the remainder of the journal.
  1491. # See the syncJournal() function for details.
  1492. #
  1493. # pager1-13.2.*: Same test as the previous. This time, throw an index into
  1494. # the mix to make the integrity-check more likely to catch
  1495. # errors.
  1496. #
  1497. testvfs tv -default 1
  1498. tv script xSyncCb
  1499. tv filter xSync
  1500. proc xSyncCb {method filename args} {
  1501. set t [file tail $filename]
  1502. if {$t == "test.db"} faultsim_save
  1503. return SQLITE_OK
  1504. }
  1505. faultsim_delete_and_reopen
  1506. db func a_string a_string
  1507. # The UPDATE statement at the end of this test case creates a really big
  1508. # journal. Since the cache-size is only 10 pages, the journal contains
  1509. # frequent journal headers.
  1510. #
  1511. do_execsql_test pager1-13.1.1 {
  1512. PRAGMA page_size = 1024;
  1513. PRAGMA journal_mode = PERSIST;
  1514. PRAGMA cache_size = 10;
  1515. BEGIN;
  1516. CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
  1517. INSERT INTO t1 VALUES(NULL, a_string(400));
  1518. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 2 */
  1519. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 4 */
  1520. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 8 */
  1521. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 16 */
  1522. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 32 */
  1523. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 64 */
  1524. INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 128 */
  1525. COMMIT;
  1526. UPDATE t1 SET b = a_string(400);
  1527. } {persist}
  1528. if {$::tcl_platform(platform)!="windows"} {
  1529. # Run transactions of increasing sizes. Eventually, one (or more than one)
  1530. # of these will write just enough content that one of the old headers created
  1531. # by the transaction in the block above lies immediately after the content
  1532. # journalled by the current transaction.
  1533. #
  1534. for {set nUp 1} {$nUp<64} {incr nUp} {
  1535. do_execsql_test pager1-13.1.2.$nUp.1 {
  1536. UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
  1537. } {}
  1538. do_execsql_test pager1-13.1.2.$nUp.2 { PRAGMA integrity_check } {ok}
  1539. # Try to access the snapshot of the file-system.
  1540. #
  1541. sqlite3 db2 sv_test.db
  1542. do_test pager1-13.1.2.$nUp.3 {
  1543. execsql { SELECT sum(length(b)) FROM t1 } db2
  1544. } [expr {128*400 - ($nUp-1)}]
  1545. do_test pager1-13.1.2.$nUp.4 {
  1546. execsql { PRAGMA integrity_check } db2
  1547. } {ok}
  1548. db2 close
  1549. }
  1550. }
  1551. if {$::tcl_platform(platform)!="windows"} {
  1552. # Same test as above. But this time with an index on the table.
  1553. #
  1554. do_execsql_test pager1-13.2.1 {
  1555. CREATE INDEX i1 ON t1(b);
  1556. UPDATE t1 SET b = a_string(400);
  1557. } {}
  1558. for {set nUp 1} {$nUp<64} {incr nUp} {
  1559. do_execsql_test pager1-13.2.2.$nUp.1 {
  1560. UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
  1561. } {}
  1562. do_execsql_test pager1-13.2.2.$nUp.2 { PRAGMA integrity_check } {ok}
  1563. sqlite3 db2 sv_test.db
  1564. do_test pager1-13.2.2.$nUp.3 {
  1565. execsql { SELECT sum(length(b)) FROM t1 } db2
  1566. } [expr {128*400 - ($nUp-1)}]
  1567. do_test pager1-13.2.2.$nUp.4 {
  1568. execsql { PRAGMA integrity_check } db2
  1569. } {ok}
  1570. db2 close
  1571. }
  1572. }
  1573. db close
  1574. tv delete
  1575. #-------------------------------------------------------------------------
  1576. # Test specal "PRAGMA journal_mode=OFF" test cases.
  1577. #
  1578. faultsim_delete_and_reopen
  1579. do_execsql_test pager1-14.1.1 {
  1580. PRAGMA journal_mode = OFF;
  1581. CREATE TABLE t1(a, b);
  1582. BEGIN;
  1583. INSERT INTO t1 VALUES(1, 2);
  1584. COMMIT;
  1585. SELECT * FROM t1;
  1586. } {off 1 2}
  1587. do_catchsql_test pager1-14.1.2 {
  1588. BEGIN;
  1589. INSERT INTO t1 VALUES(3, 4);
  1590. ROLLBACK;
  1591. } {0 {}}
  1592. do_execsql_test pager1-14.1.3 {
  1593. SELECT * FROM t1;
  1594. } {1 2}
  1595. do_catchsql_test pager1-14.1.4 {
  1596. BEGIN;
  1597. INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
  1598. INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
  1599. } {1 {PRIMARY KEY must be unique}}
  1600. do_execsql_test pager1-14.1.5 {
  1601. COMMIT;
  1602. SELECT * FROM t1;
  1603. } {1 2 2 2}
  1604. #-------------------------------------------------------------------------
  1605. # Test opening and closing the pager sub-system with different values
  1606. # for the sqlite3_vfs.szOsFile variable.
  1607. #
  1608. faultsim_delete_and_reopen
  1609. do_execsql_test pager1-15.0 {
  1610. CREATE TABLE tx(y, z);
  1611. INSERT INTO tx VALUES('Ayutthaya', 'Beijing');
  1612. INSERT INTO tx VALUES('London', 'Tokyo');
  1613. } {}
  1614. db close
  1615. for {set i 0} {$i<513} {incr i 3} {
  1616. testvfs tv -default 1 -szosfile $i
  1617. sqlite3 db test.db
  1618. do_execsql_test pager1-15.$i.1 {
  1619. SELECT * FROM tx;
  1620. } {Ayutthaya Beijing London Tokyo}
  1621. db close
  1622. tv delete
  1623. }
  1624. #-------------------------------------------------------------------------
  1625. # Check that it is not possible to open a database file if the full path
  1626. # to the associated journal file will be longer than sqlite3_vfs.mxPathname.
  1627. #
  1628. testvfs tv -default 1
  1629. tv script xOpenCb
  1630. tv filter xOpen
  1631. proc xOpenCb {method filename args} {
  1632. set ::file_len [string length $filename]
  1633. }
  1634. sqlite3 db test.db
  1635. db close
  1636. tv delete
  1637. for {set ii [expr $::file_len-5]} {$ii < [expr $::file_len+20]} {incr ii} {
  1638. testvfs tv -default 1 -mxpathname $ii
  1639. # The length of the full path to file "test.db-journal" is ($::file_len+8).
  1640. # If the configured sqlite3_vfs.mxPathname value greater than or equal to
  1641. # this, then the file can be opened. Otherwise, it cannot.
  1642. #
  1643. if {$ii >= [expr $::file_len+8]} {
  1644. set res {0 {}}
  1645. } else {
  1646. set res {1 {unable to open database file}}
  1647. }
  1648. do_test pager1-16.1.$ii {
  1649. list [catch { sqlite3 db test.db } msg] $msg
  1650. } $res
  1651. catch {db close}
  1652. tv delete
  1653. }
  1654. #-------------------------------------------------------------------------
  1655. # Test the pagers response to the b-tree layer requesting illegal page
  1656. # numbers:
  1657. #
  1658. # + The locking page,
  1659. # + Page 0,
  1660. # + A page with a page number greater than (2^31-1).
  1661. #
  1662. # These tests will not work if SQLITE_DIRECT_OVERFLOW_READ is defined. In
  1663. # that case IO errors are sometimes reported instead of SQLITE_CORRUPT.
  1664. #
  1665. ifcapable !direct_read {
  1666. do_test pager1-18.1 {
  1667. faultsim_delete_and_reopen
  1668. db func a_string a_string
  1669. execsql {
  1670. PRAGMA page_size = 1024;
  1671. CREATE TABLE t1(a, b);
  1672. INSERT INTO t1 VALUES(a_string(500), a_string(200));
  1673. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1674. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1675. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1676. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1677. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1678. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1679. INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
  1680. }
  1681. } {}
  1682. do_test pager1-18.2 {
  1683. set root [db one "SELECT rootpage FROM sqlite_master"]
  1684. set lockingpage [expr (0x10000/1024) + 1]
  1685. execsql {
  1686. PRAGMA writable_schema = 1;
  1687. UPDATE sqlite_master SET rootpage = $lockingpage;
  1688. }
  1689. sqlite3 db2 test.db
  1690. catchsql { SELECT count(*) FROM t1 } db2
  1691. } {1 {database disk image is malformed}}
  1692. db2 close
  1693. do_test pager1-18.3.1 {
  1694. execsql {
  1695. CREATE TABLE t2(x);
  1696. INSERT INTO t2 VALUES(a_string(5000));
  1697. }
  1698. set pgno [expr ([file size test.db] / 1024)-2]
  1699. hexio_write test.db [expr ($pgno-1)*1024] 00000000
  1700. sqlite3 db2 test.db
  1701. # even though x is malformed, because typeof() does
  1702. # not load the content of x, the error is not noticed.
  1703. catchsql { SELECT typeof(x) FROM t2 } db2
  1704. } {0 text}
  1705. do_test pager1-18.3.2 {
  1706. # in this case, the value of x is loaded and so the error is
  1707. # detected
  1708. catchsql { SELECT length(x||'') FROM t2 } db2
  1709. } {1 {database disk image is malformed}}
  1710. db2 close
  1711. do_test pager1-18.3.3 {
  1712. execsql {
  1713. DELETE FROM t2;
  1714. INSERT INTO t2 VALUES(randomblob(5000));
  1715. }
  1716. set pgno [expr ([file size test.db] / 1024)-2]
  1717. hexio_write test.db [expr ($pgno-1)*1024] 00000000
  1718. sqlite3 db2 test.db
  1719. # even though x is malformed, because length() and typeof() do
  1720. # not load the content of x, the error is not noticed.
  1721. catchsql { SELECT length(x), typeof(x) FROM t2 } db2
  1722. } {0 {5000 blob}}
  1723. do_test pager1-18.3.4 {
  1724. # in this case, the value of x is loaded and so the error is
  1725. # detected
  1726. catchsql { SELECT length(x||'') FROM t2 } db2
  1727. } {1 {database disk image is malformed}}
  1728. db2 close
  1729. do_test pager1-18.4 {
  1730. hexio_write test.db [expr ($pgno-1)*1024] 90000000
  1731. sqlite3 db2 test.db
  1732. catchsql { SELECT length(x||'') FROM t2 } db2
  1733. } {1 {database disk image is malformed}}
  1734. db2 close
  1735. do_test pager1-18.5 {
  1736. sqlite3 db ""
  1737. execsql {
  1738. CREATE TABLE t1(a, b);
  1739. CREATE TABLE t2(a, b);
  1740. PRAGMA writable_schema = 1;
  1741. UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1';
  1742. PRAGMA writable_schema = 0;
  1743. ALTER TABLE t1 RENAME TO x1;
  1744. }
  1745. catchsql { SELECT * FROM x1 }
  1746. } {1 {database disk image is malformed}}
  1747. db close
  1748. do_test pager1-18.6 {
  1749. faultsim_delete_and_reopen
  1750. db func a_string a_string
  1751. execsql {
  1752. PRAGMA page_size = 1024;
  1753. CREATE TABLE t1(x);
  1754. INSERT INTO t1 VALUES(a_string(800));
  1755. INSERT INTO t1 VALUES(a_string(800));
  1756. }
  1757. set root [db one "SELECT rootpage FROM sqlite_master"]
  1758. db close
  1759. hexio_write test.db [expr ($root-1)*1024 + 8] 00000000
  1760. sqlite3 db test.db
  1761. catchsql { SELECT length(x) FROM t1 }
  1762. } {1 {database disk image is malformed}}
  1763. }
  1764. do_test pager1-19.1 {
  1765. sqlite3 db ""
  1766. db func a_string a_string
  1767. execsql {
  1768. PRAGMA page_size = 512;
  1769. PRAGMA auto_vacuum = 1;
  1770. CREATE TABLE t1(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
  1771. ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
  1772. ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
  1773. da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
  1774. ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
  1775. fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
  1776. ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
  1777. ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
  1778. ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
  1779. ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
  1780. ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
  1781. la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
  1782. ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
  1783. );
  1784. CREATE TABLE t2(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
  1785. ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
  1786. ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
  1787. da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
  1788. ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
  1789. fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
  1790. ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
  1791. ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
  1792. ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
  1793. ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
  1794. ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
  1795. la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
  1796. ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
  1797. );
  1798. INSERT INTO t1(aa) VALUES( a_string(100000) );
  1799. INSERT INTO t2(aa) VALUES( a_string(100000) );
  1800. VACUUM;
  1801. }
  1802. } {}
  1803. #-------------------------------------------------------------------------
  1804. # Test a couple of special cases that come up while committing
  1805. # transactions:
  1806. #
  1807. # pager1-20.1.*: Committing an in-memory database transaction when the
  1808. # database has not been modified at all.
  1809. #
  1810. # pager1-20.2.*: As above, but with a normal db in exclusive-locking mode.
  1811. #
  1812. # pager1-20.3.*: Committing a transaction in WAL mode where the database has
  1813. # been modified, but all dirty pages have been flushed to
  1814. # disk before the commit.
  1815. #
  1816. do_test pager1-20.1.1 {
  1817. catch {db close}
  1818. sqlite3 db :memory:
  1819. execsql {
  1820. CREATE TABLE one(two, three);
  1821. INSERT INTO one VALUES('a', 'b');
  1822. }
  1823. } {}
  1824. do_test pager1-20.1.2 {
  1825. execsql {
  1826. BEGIN EXCLUSIVE;
  1827. COMMIT;
  1828. }
  1829. } {}
  1830. do_test pager1-20.2.1 {
  1831. faultsim_delete_and_reopen
  1832. execsql {
  1833. PRAGMA locking_mode = exclusive;
  1834. PRAGMA journal_mode = persist;
  1835. CREATE TABLE one(two, three);
  1836. INSERT INTO one VALUES('a', 'b');
  1837. }
  1838. } {exclusive persist}
  1839. do_test pager1-20.2.2 {
  1840. execsql {
  1841. BEGIN EXCLUSIVE;
  1842. COMMIT;
  1843. }
  1844. } {}
  1845. ifcapable wal {
  1846. do_test pager1-20.3.1 {
  1847. faultsim_delete_and_reopen
  1848. db func a_string a_string
  1849. execsql {
  1850. PRAGMA cache_size = 10;
  1851. PRAGMA journal_mode = wal;
  1852. BEGIN;
  1853. CREATE TABLE t1(x);
  1854. CREATE TABLE t2(y);
  1855. INSERT INTO t1 VALUES(a_string(800));
  1856. INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */
  1857. INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */
  1858. INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */
  1859. INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */
  1860. INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */
  1861. COMMIT;
  1862. }
  1863. } {wal}
  1864. do_test pager1-20.3.2 {
  1865. execsql {
  1866. BEGIN;
  1867. INSERT INTO t2 VALUES('xxxx');
  1868. }
  1869. recursive_select 32 t1
  1870. execsql COMMIT
  1871. } {}
  1872. }
  1873. #-------------------------------------------------------------------------
  1874. # Test that a WAL database may not be opened if:
  1875. #
  1876. # pager1-21.1.*: The VFS has an iVersion less than 2, or
  1877. # pager1-21.2.*: The VFS does not provide xShmXXX() methods.
  1878. #
  1879. ifcapable wal {
  1880. do_test pager1-21.0 {
  1881. faultsim_delete_and_reopen
  1882. execsql {
  1883. PRAGMA journal_mode = WAL;
  1884. CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
  1885. INSERT INTO ko DEFAULT VALUES;
  1886. }
  1887. } {wal}
  1888. do_test pager1-21.1 {
  1889. testvfs tv -noshm 1
  1890. sqlite3 db2 test.db -vfs tv
  1891. catchsql { SELECT * FROM ko } db2
  1892. } {1 {unable to open database file}}
  1893. db2 close
  1894. tv delete
  1895. do_test pager1-21.2 {
  1896. testvfs tv -iversion 1
  1897. sqlite3 db2 test.db -vfs tv
  1898. catchsql { SELECT * FROM ko } db2
  1899. } {1 {unable to open database file}}
  1900. db2 close
  1901. tv delete
  1902. }
  1903. #-------------------------------------------------------------------------
  1904. # Test that a "PRAGMA wal_checkpoint":
  1905. #
  1906. # pager1-22.1.*: is a no-op on a non-WAL db, and
  1907. # pager1-22.2.*: does not cause xSync calls with a synchronous=off db.
  1908. #
  1909. ifcapable wal {
  1910. do_test pager1-22.1.1 {
  1911. faultsim_delete_and_reopen
  1912. execsql {
  1913. CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
  1914. INSERT INTO ko DEFAULT VALUES;
  1915. }
  1916. execsql { PRAGMA wal_checkpoint }
  1917. } {0 -1 -1}
  1918. do_test pager1-22.2.1 {
  1919. testvfs tv -default 1
  1920. tv filter xSync
  1921. tv script xSyncCb
  1922. proc xSyncCb {args} {incr ::synccount}
  1923. set ::synccount 0
  1924. sqlite3 db test.db
  1925. execsql {
  1926. PRAGMA synchronous = off;
  1927. PRAGMA journal_mode = WAL;
  1928. INSERT INTO ko DEFAULT VALUES;
  1929. }
  1930. execsql { PRAGMA wal_checkpoint }
  1931. set synccount
  1932. } {0}
  1933. db close
  1934. tv delete
  1935. }
  1936. #-------------------------------------------------------------------------
  1937. # Tests for changing journal mode.
  1938. #
  1939. # pager1-23.1.*: Test that when changing from PERSIST to DELETE mode,
  1940. # the journal file is deleted.
  1941. #
  1942. # pager1-23.2.*: Same test as above, but while a shared lock is held
  1943. # on the database file.
  1944. #
  1945. # pager1-23.3.*: Same test as above, but while a reserved lock is held
  1946. # on the database file.
  1947. #
  1948. # pager1-23.4.*: And, for fun, while holding an exclusive lock.
  1949. #
  1950. # pager1-23.5.*: Try to set various different journal modes with an
  1951. # in-memory database (only MEMORY and OFF should work).
  1952. #
  1953. # pager1-23.6.*: Try to set locking_mode=normal on an in-memory database
  1954. # (doesn't work - in-memory databases always use
  1955. # locking_mode=exclusive).
  1956. #
  1957. do_test pager1-23.1.1 {
  1958. faultsim_delete_and_reopen
  1959. execsql {
  1960. PRAGMA journal_mode = PERSIST;
  1961. CREATE TABLE t1(a, b);
  1962. }
  1963. file exists test.db-journal
  1964. } {1}
  1965. do_test pager1-23.1.2 {
  1966. execsql { PRAGMA journal_mode = DELETE }
  1967. file exists test.db-journal
  1968. } {0}
  1969. do_test pager1-23.2.1 {
  1970. execsql {
  1971. PRAGMA journal_mode = PERSIST;
  1972. INSERT INTO t1 VALUES('Canberra', 'ACT');
  1973. }
  1974. db eval { SELECT * FROM t1 } {
  1975. db eval { PRAGMA journal_mode = DELETE }
  1976. }
  1977. execsql { PRAGMA journal_mode }
  1978. } {delete}
  1979. do_test pager1-23.2.2 {
  1980. file exists test.db-journal
  1981. } {0}
  1982. do_test pager1-23.3.1 {
  1983. execsql {
  1984. PRAGMA journal_mode = PERSIST;
  1985. INSERT INTO t1 VALUES('Darwin', 'NT');
  1986. BEGIN IMMEDIATE;
  1987. }
  1988. db eval { PRAGMA journal_mode = DELETE }
  1989. execsql { PRAGMA journal_mode }
  1990. } {delete}
  1991. do_test pager1-23.3.2 {
  1992. file exists test.db-journal
  1993. } {0}
  1994. do_test pager1-23.3.3 {
  1995. execsql COMMIT
  1996. } {}
  1997. do_test pager1-23.4.1 {
  1998. execsql {
  1999. PRAGMA journal_mode = PERSIST;
  2000. INSERT INTO t1 VALUES('Adelaide', 'SA');
  2001. BEGIN EXCLUSIVE;
  2002. }
  2003. db eval { PRAGMA journal_mode = DELETE }
  2004. execsql { PRAGMA journal_mode }
  2005. } {delete}
  2006. do_test pager1-23.4.2 {
  2007. file exists test.db-journal
  2008. } {0}
  2009. do_test pager1-23.4.3 {
  2010. execsql COMMIT
  2011. } {}
  2012. do_test pager1-23.5.1 {
  2013. faultsim_delete_and_reopen
  2014. sqlite3 db :memory:
  2015. } {}
  2016. foreach {tn mode possible} {
  2017. 2 off 1
  2018. 3 memory 1
  2019. 4 persist 0
  2020. 5 delete 0
  2021. 6 wal 0
  2022. 7 truncate 0
  2023. } {
  2024. do_test pager1-23.5.$tn.1 {
  2025. execsql "PRAGMA journal_mode = off"
  2026. execsql "PRAGMA journal_mode = $mode"
  2027. } [if $possible {list $mode} {list off}]
  2028. do_test pager1-23.5.$tn.2 {
  2029. execsql "PRAGMA journal_mode = memory"
  2030. execsql "PRAGMA journal_mode = $mode"
  2031. } [if $possible {list $mode} {list memory}]
  2032. }
  2033. do_test pager1-23.6.1 {
  2034. execsql {PRAGMA locking_mode = normal}
  2035. } {exclusive}
  2036. do_test pager1-23.6.2 {
  2037. execsql {PRAGMA locking_mode = exclusive}
  2038. } {exclusive}
  2039. do_test pager1-23.6.3 {
  2040. execsql {PRAGMA locking_mode}
  2041. } {exclusive}
  2042. do_test pager1-23.6.4 {
  2043. execsql {PRAGMA main.locking_mode}
  2044. } {exclusive}
  2045. #-------------------------------------------------------------------------
  2046. #
  2047. do_test pager1-24.1.1 {
  2048. faultsim_delete_and_reopen
  2049. db func a_string a_string
  2050. execsql {
  2051. PRAGMA cache_size = 10;
  2052. PRAGMA auto_vacuum = FULL;
  2053. CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z));
  2054. CREATE TABLE x2(x, y, z, PRIMARY KEY(y, z));
  2055. INSERT INTO x2 VALUES(a_string(400), a_string(500), a_string(600));
  2056. INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
  2057. INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
  2058. INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
  2059. INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
  2060. INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
  2061. INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
  2062. INSERT INTO x1 SELECT * FROM x2;
  2063. }
  2064. } {}
  2065. do_test pager1-24.1.2 {
  2066. execsql {
  2067. BEGIN;
  2068. DELETE FROM x1 WHERE rowid<32;
  2069. }
  2070. recursive_select 64 x2
  2071. } {}
  2072. do_test pager1-24.1.3 {
  2073. execsql {
  2074. UPDATE x1 SET z = a_string(300) WHERE rowid>40;
  2075. COMMIT;
  2076. PRAGMA integrity_check;
  2077. SELECT count(*) FROM x1;
  2078. }
  2079. } {ok 33}
  2080. do_test pager1-24.1.4 {
  2081. execsql {
  2082. DELETE FROM x1;
  2083. INSERT INTO x1 SELECT * FROM x2;
  2084. BEGIN;
  2085. DELETE FROM x1 WHERE rowid<32;
  2086. UPDATE x1 SET z = a_string(299) WHERE rowid>40;
  2087. }
  2088. recursive_select 64 x2 {db eval COMMIT}
  2089. execsql {
  2090. PRAGMA integrity_check;
  2091. SELECT count(*) FROM x1;
  2092. }
  2093. } {ok 33}
  2094. do_test pager1-24.1.5 {
  2095. execsql {
  2096. DELETE FROM x1;
  2097. INSERT INTO x1 SELECT * FROM x2;
  2098. }
  2099. recursive_select 64 x2 { db eval {CREATE TABLE x3(x, y, z)} }
  2100. execsql { SELECT * FROM x3 }
  2101. } {}
  2102. #-------------------------------------------------------------------------
  2103. #
  2104. do_test pager1-25-1 {
  2105. faultsim_delete_and_reopen
  2106. execsql {
  2107. BEGIN;
  2108. SAVEPOINT abc;
  2109. CREATE TABLE t1(a, b);
  2110. ROLLBACK TO abc;
  2111. COMMIT;
  2112. }
  2113. db close
  2114. } {}
  2115. do_test pager1-25-2 {
  2116. faultsim_delete_and_reopen
  2117. execsql {
  2118. SAVEPOINT abc;
  2119. CREATE TABLE t1(a, b);
  2120. ROLLBACK TO abc;
  2121. COMMIT;
  2122. }
  2123. db close
  2124. } {}
  2125. #-------------------------------------------------------------------------
  2126. # Sector-size tests.
  2127. #
  2128. do_test pager1-26.1 {
  2129. testvfs tv -default 1
  2130. tv sectorsize 4096
  2131. faultsim_delete_and_reopen
  2132. db func a_string a_string
  2133. execsql {
  2134. PRAGMA page_size = 512;
  2135. CREATE TABLE tbl(a PRIMARY KEY, b UNIQUE);
  2136. BEGIN;
  2137. INSERT INTO tbl VALUES(a_string(25), a_string(600));
  2138. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2139. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2140. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2141. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2142. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2143. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2144. INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
  2145. COMMIT;
  2146. }
  2147. } {}
  2148. do_execsql_test pager1-26.1 {
  2149. UPDATE tbl SET b = a_string(550);
  2150. } {}
  2151. db close
  2152. tv delete
  2153. #-------------------------------------------------------------------------
  2154. #
  2155. do_test pager1.27.1 {
  2156. faultsim_delete_and_reopen
  2157. sqlite3_pager_refcounts db
  2158. execsql {
  2159. BEGIN;
  2160. CREATE TABLE t1(a, b);
  2161. }
  2162. sqlite3_pager_refcounts db
  2163. execsql COMMIT
  2164. } {}
  2165. #-------------------------------------------------------------------------
  2166. # Test that attempting to open a write-transaction with
  2167. # locking_mode=exclusive in WAL mode fails if there are other clients on
  2168. # the same database.
  2169. #
  2170. catch { db close }
  2171. ifcapable wal {
  2172. do_multiclient_test tn {
  2173. do_test pager1-28.$tn.1 {
  2174. sql1 {
  2175. PRAGMA journal_mode = WAL;
  2176. CREATE TABLE t1(a, b);
  2177. INSERT INTO t1 VALUES('a', 'b');
  2178. }
  2179. } {wal}
  2180. do_test pager1-28.$tn.2 { sql2 { SELECT * FROM t1 } } {a b}
  2181. do_test pager1-28.$tn.3 { sql1 { PRAGMA locking_mode=exclusive } } {exclusive}
  2182. do_test pager1-28.$tn.4 {
  2183. csql1 { BEGIN; INSERT INTO t1 VALUES('c', 'd'); }
  2184. } {1 {database is locked}}
  2185. code2 { db2 close ; sqlite3 db2 test.db }
  2186. do_test pager1-28.$tn.4 {
  2187. sql1 { INSERT INTO t1 VALUES('c', 'd'); COMMIT }
  2188. } {}
  2189. }
  2190. }
  2191. #-------------------------------------------------------------------------
  2192. # Normally, when changing from journal_mode=PERSIST to DELETE the pager
  2193. # attempts to delete the journal file. However, if it cannot obtain a
  2194. # RESERVED lock on the database file, this step is skipped.
  2195. #
  2196. do_multiclient_test tn {
  2197. do_test pager1-28.$tn.1 {
  2198. sql1 {
  2199. PRAGMA journal_mode = PERSIST;
  2200. CREATE TABLE t1(a, b);
  2201. INSERT INTO t1 VALUES('a', 'b');
  2202. }
  2203. } {persist}
  2204. do_test pager1-28.$tn.2 { file exists test.db-journal } 1
  2205. do_test pager1-28.$tn.3 { sql1 { PRAGMA journal_mode = DELETE } } delete
  2206. do_test pager1-28.$tn.4 { file exists test.db-journal } 0
  2207. do_test pager1-28.$tn.5 {
  2208. sql1 {
  2209. PRAGMA journal_mode = PERSIST;
  2210. INSERT INTO t1 VALUES('c', 'd');
  2211. }
  2212. } {persist}
  2213. do_test pager1-28.$tn.6 { file exists test.db-journal } 1
  2214. do_test pager1-28.$tn.7 {
  2215. sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
  2216. } {}
  2217. do_test pager1-28.$tn.8 { file exists test.db-journal } 1
  2218. do_test pager1-28.$tn.9 { sql1 { PRAGMA journal_mode = DELETE } } delete
  2219. do_test pager1-28.$tn.10 { file exists test.db-journal } 1
  2220. do_test pager1-28.$tn.11 { sql2 COMMIT } {}
  2221. do_test pager1-28.$tn.12 { file exists test.db-journal } 0
  2222. do_test pager1-28-$tn.13 {
  2223. code1 { set channel [db incrblob -readonly t1 a 2] }
  2224. sql1 {
  2225. PRAGMA journal_mode = PERSIST;
  2226. INSERT INTO t1 VALUES('g', 'h');
  2227. }
  2228. } {persist}
  2229. do_test pager1-28.$tn.14 { file exists test.db-journal } 1
  2230. do_test pager1-28.$tn.15 {
  2231. sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
  2232. } {}
  2233. do_test pager1-28.$tn.16 { sql1 { PRAGMA journal_mode = DELETE } } delete
  2234. do_test pager1-28.$tn.17 { file exists test.db-journal } 1
  2235. do_test pager1-28.$tn.17 { csql2 { COMMIT } } {1 {database is locked}}
  2236. do_test pager1-28-$tn.18 { code1 { read $channel } } c
  2237. do_test pager1-28-$tn.19 { code1 { close $channel } } {}
  2238. do_test pager1-28.$tn.20 { sql2 { COMMIT } } {}
  2239. }
  2240. do_test pager1-29.1 {
  2241. faultsim_delete_and_reopen
  2242. execsql {
  2243. PRAGMA page_size = 1024;
  2244. PRAGMA auto_vacuum = full;
  2245. PRAGMA locking_mode=exclusive;
  2246. CREATE TABLE t1(a, b);
  2247. INSERT INTO t1 VALUES(1, 2);
  2248. }
  2249. file size test.db
  2250. } [expr 1024*3]
  2251. do_test pager1-29.2 {
  2252. execsql {
  2253. PRAGMA page_size = 4096;
  2254. VACUUM;
  2255. }
  2256. file size test.db
  2257. } [expr 4096*3]
  2258. #-------------------------------------------------------------------------
  2259. # Test that if an empty database file (size 0 bytes) is opened in
  2260. # exclusive-locking mode, any journal file is deleted from the file-system
  2261. # without being rolled back. And that the RESERVED lock obtained while
  2262. # doing this is not released.
  2263. #
  2264. do_test pager1-30.1 {
  2265. db close
  2266. delete_file test.db
  2267. delete_file test.db-journal
  2268. set fd [open test.db-journal w]
  2269. seek $fd [expr 512+1032*2]
  2270. puts -nonewline $fd x
  2271. close $fd
  2272. sqlite3 db test.db
  2273. execsql {
  2274. PRAGMA locking_mode=EXCLUSIVE;
  2275. SELECT count(*) FROM sqlite_master;
  2276. PRAGMA lock_status;
  2277. }
  2278. } {exclusive 0 main reserved temp closed}
  2279. #-------------------------------------------------------------------------
  2280. # Test that if the "page-size" field in a journal-header is 0, the journal
  2281. # file can still be rolled back. This is required for backward compatibility -
  2282. # versions of SQLite prior to 3.5.8 always set this field to zero.
  2283. #
  2284. if {$tcl_platform(platform)=="unix"} {
  2285. do_test pager1-31.1 {
  2286. faultsim_delete_and_reopen
  2287. execsql {
  2288. PRAGMA cache_size = 10;
  2289. PRAGMA page_size = 1024;
  2290. CREATE TABLE t1(x, y, UNIQUE(x, y));
  2291. INSERT INTO t1 VALUES(randomblob(1500), randomblob(1500));
  2292. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2293. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2294. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2295. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2296. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2297. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2298. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2299. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2300. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2301. INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
  2302. BEGIN;
  2303. UPDATE t1 SET y = randomblob(1499);
  2304. }
  2305. copy_file test.db test.db2
  2306. copy_file test.db-journal test.db2-journal
  2307. hexio_write test.db2-journal 24 00000000
  2308. sqlite3 db2 test.db2
  2309. execsql { PRAGMA integrity_check } db2
  2310. } {ok}
  2311. }
  2312. #-------------------------------------------------------------------------
  2313. # Test that a database file can be "pre-hinted" to a certain size and that
  2314. # subsequent spilling of the pager cache does not result in the database
  2315. # file being shrunk.
  2316. #
  2317. catch {db close}
  2318. forcedelete test.db
  2319. do_test pager1-32.1 {
  2320. sqlite3 db test.db
  2321. execsql {
  2322. CREATE TABLE t1(x, y);
  2323. }
  2324. db close
  2325. sqlite3 db test.db
  2326. execsql {
  2327. BEGIN;
  2328. INSERT INTO t1 VALUES(1, randomblob(10000));
  2329. }
  2330. file_control_chunksize_test db main 1024
  2331. file_control_sizehint_test db main 20971520; # 20MB
  2332. execsql {
  2333. PRAGMA cache_size = 10;
  2334. INSERT INTO t1 VALUES(1, randomblob(10000));
  2335. INSERT INTO t1 VALUES(2, randomblob(10000));
  2336. INSERT INTO t1 SELECT x+2, randomblob(10000) from t1;
  2337. INSERT INTO t1 SELECT x+4, randomblob(10000) from t1;
  2338. INSERT INTO t1 SELECT x+8, randomblob(10000) from t1;
  2339. INSERT INTO t1 SELECT x+16, randomblob(10000) from t1;
  2340. SELECT count(*) FROM t1;
  2341. COMMIT;
  2342. }
  2343. db close
  2344. file size test.db
  2345. } {20971520}
  2346. # Cleanup 20MB file left by the previous test.
  2347. forcedelete test.db
  2348. #-------------------------------------------------------------------------
  2349. # Test that if a transaction is committed in journal_mode=DELETE mode,
  2350. # and the call to unlink() returns an ENOENT error, the COMMIT does not
  2351. # succeed.
  2352. #
  2353. if {$::tcl_platform(platform)=="unix"} {
  2354. do_test pager1-33.1 {
  2355. sqlite3 db test.db
  2356. execsql {
  2357. CREATE TABLE t1(x);
  2358. INSERT INTO t1 VALUES('one');
  2359. INSERT INTO t1 VALUES('two');
  2360. BEGIN;
  2361. INSERT INTO t1 VALUES('three');
  2362. INSERT INTO t1 VALUES('four');
  2363. }
  2364. forcedelete bak-journal
  2365. file rename test.db-journal bak-journal
  2366. catchsql COMMIT
  2367. } {1 {disk I/O error}}
  2368. do_test pager1-33.2 {
  2369. file rename bak-journal test.db-journal
  2370. execsql { SELECT * FROM t1 }
  2371. } {one two}
  2372. }
  2373. #-------------------------------------------------------------------------
  2374. # Test that appending pages to the database file then moving those pages
  2375. # to the free-list before the transaction is committed does not cause
  2376. # an error.
  2377. #
  2378. foreach {tn pragma strsize} {
  2379. 1 { PRAGMA mmap_size = 0 } 2400
  2380. 2 { } 2400
  2381. 3 { PRAGMA mmap_size = 0 } 4400
  2382. 4 { } 4400
  2383. } {
  2384. reset_db
  2385. db func a_string a_string
  2386. db eval $pragma
  2387. do_execsql_test 34.$tn.1 {
  2388. CREATE TABLE t1(a, b);
  2389. INSERT INTO t1 VALUES(1, 2);
  2390. }
  2391. do_execsql_test 34.$tn.2 {
  2392. BEGIN;
  2393. INSERT INTO t1 VALUES(2, a_string($strsize));
  2394. DELETE FROM t1 WHERE oid=2;
  2395. COMMIT;
  2396. PRAGMA integrity_check;
  2397. } {ok}
  2398. }
  2399. #-------------------------------------------------------------------------
  2400. #
  2401. reset_db
  2402. do_test 35 {
  2403. sqlite3 db test.db
  2404. execsql {
  2405. CREATE TABLE t1(x, y);
  2406. PRAGMA journal_mode = WAL;
  2407. INSERT INTO t1 VALUES(1, 2);
  2408. }
  2409. execsql {
  2410. BEGIN;
  2411. CREATE TABLE t2(a, b);
  2412. }
  2413. hexio_write test.db-shm [expr 16*1024] [string repeat 0055 8192]
  2414. catchsql ROLLBACK
  2415. } {0 {}}
  2416. do_multiclient_test tn {
  2417. sql1 {
  2418. PRAGMA auto_vacuum = 0;
  2419. CREATE TABLE t1(x, y);
  2420. INSERT INTO t1 VALUES(1, 2);
  2421. }
  2422. do_test 36.$tn.1 {
  2423. sql2 { PRAGMA max_page_count = 2 }
  2424. list [catch { sql2 { CREATE TABLE t2(x) } } msg] $msg
  2425. } {1 {database or disk is full}}
  2426. sql1 { PRAGMA checkpoint_fullfsync = 1 }
  2427. sql1 { CREATE TABLE t2(x) }
  2428. do_test 36.$tn.2 {
  2429. sql2 { INSERT INTO t2 VALUES('xyz') }
  2430. list [catch { sql2 { CREATE TABLE t3(x) } } msg] $msg
  2431. } {1 {database or disk is full}}
  2432. }
  2433. forcedelete test1 test2
  2434. foreach {tn uri} {
  2435. 1 {file:?mode=memory&cache=shared}
  2436. 2 {file:one?mode=memory&cache=shared}
  2437. 3 {file:test1?cache=shared}
  2438. 4 {file:test2?another=parameter&yet=anotherone}
  2439. } {
  2440. do_test 37.$tn {
  2441. catch { db close }
  2442. sqlite3_shutdown
  2443. sqlite3_config_uri 1
  2444. sqlite3 db $uri
  2445. db eval {
  2446. CREATE TABLE t1(x);
  2447. INSERT INTO t1 VALUES(1);
  2448. SELECT * FROM t1;
  2449. }
  2450. } {1}
  2451. do_execsql_test 37.$tn.2 {
  2452. VACUUM;
  2453. SELECT * FROM t1;
  2454. } {1}
  2455. db close
  2456. sqlite3_shutdown
  2457. sqlite3_config_uri 0
  2458. }
  2459. do_test 38.1 {
  2460. catch { db close }
  2461. forcedelete test.db
  2462. set fd [open test.db w]
  2463. puts $fd "hello world"
  2464. close $fd
  2465. sqlite3 db test.db
  2466. catchsql { CREATE TABLE t1(x) }
  2467. } {1 {file is encrypted or is not a database}}
  2468. do_test 38.2 {
  2469. catch { db close }
  2470. forcedelete test.db
  2471. } {}
  2472. do_test 39.1 {
  2473. sqlite3 db test.db
  2474. execsql {
  2475. PRAGMA auto_vacuum = 1;
  2476. CREATE TABLE t1(x);
  2477. INSERT INTO t1 VALUES('xxx');
  2478. INSERT INTO t1 VALUES('two');
  2479. INSERT INTO t1 VALUES(randomblob(400));
  2480. INSERT INTO t1 VALUES(randomblob(400));
  2481. INSERT INTO t1 VALUES(randomblob(400));
  2482. INSERT INTO t1 VALUES(randomblob(400));
  2483. BEGIN;
  2484. UPDATE t1 SET x = 'one' WHERE rowid=1;
  2485. }
  2486. set ::stmt [sqlite3_prepare db "SELECT * FROM t1 ORDER BY rowid" -1 dummy]
  2487. sqlite3_step $::stmt
  2488. sqlite3_column_text $::stmt 0
  2489. } {one}
  2490. do_test 39.2 {
  2491. execsql { CREATE TABLE t2(x) }
  2492. sqlite3_step $::stmt
  2493. sqlite3_column_text $::stmt 0
  2494. } {two}
  2495. do_test 39.3 {
  2496. sqlite3_finalize $::stmt
  2497. execsql COMMIT
  2498. } {}
  2499. do_execsql_test 39.4 {
  2500. PRAGMA auto_vacuum = 2;
  2501. CREATE TABLE t3(x);
  2502. CREATE TABLE t4(x);
  2503. DROP TABLE t2;
  2504. DROP TABLE t3;
  2505. DROP TABLE t4;
  2506. }
  2507. do_test 39.5 {
  2508. db close
  2509. sqlite3 db test.db
  2510. execsql {
  2511. PRAGMA cache_size = 1;
  2512. PRAGMA incremental_vacuum;
  2513. PRAGMA integrity_check;
  2514. }
  2515. } {ok}
  2516. do_test 40.1 {
  2517. reset_db
  2518. execsql {
  2519. PRAGMA auto_vacuum = 1;
  2520. CREATE TABLE t1(x PRIMARY KEY);
  2521. INSERT INTO t1 VALUES(randomblob(1200));
  2522. PRAGMA page_count;
  2523. }
  2524. } {6}
  2525. do_test 40.2 {
  2526. execsql {
  2527. INSERT INTO t1 VALUES(randomblob(1200));
  2528. INSERT INTO t1 VALUES(randomblob(1200));
  2529. INSERT INTO t1 VALUES(randomblob(1200));
  2530. }
  2531. } {}
  2532. do_test 40.3 {
  2533. db close
  2534. sqlite3 db test.db
  2535. execsql {
  2536. PRAGMA cache_size = 1;
  2537. CREATE TABLE t2(x);
  2538. PRAGMA integrity_check;
  2539. }
  2540. } {ok}
  2541. do_test 41.1 {
  2542. reset_db
  2543. execsql {
  2544. CREATE TABLE t1(x PRIMARY KEY);
  2545. INSERT INTO t1 VALUES(randomblob(200));
  2546. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2547. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2548. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2549. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2550. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2551. INSERT INTO t1 SELECT randomblob(200) FROM t1;
  2552. }
  2553. } {}
  2554. do_test 41.2 {
  2555. testvfs tv -default 1
  2556. tv sectorsize 16384;
  2557. tv devchar [list]
  2558. db close
  2559. sqlite3 db test.db
  2560. execsql {
  2561. PRAGMA cache_size = 1;
  2562. DELETE FROM t1 WHERE rowid%4;
  2563. PRAGMA integrity_check;
  2564. }
  2565. } {ok}
  2566. db close
  2567. tv delete
  2568. set pending_prev [sqlite3_test_control_pending_byte 0x1000000]
  2569. do_test 42.1 {
  2570. reset_db
  2571. execsql {
  2572. CREATE TABLE t1(x, y);
  2573. INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
  2574. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2575. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2576. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2577. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2578. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2579. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2580. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2581. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2582. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2583. }
  2584. db close
  2585. sqlite3_test_control_pending_byte 0x0010000
  2586. sqlite3 db test.db
  2587. db eval { PRAGMA mmap_size = 0 }
  2588. catchsql { SELECT sum(length(y)) FROM t1 }
  2589. } {1 {database disk image is malformed}}
  2590. do_test 42.2 {
  2591. reset_db
  2592. execsql {
  2593. CREATE TABLE t1(x, y);
  2594. INSERT INTO t1 VALUES(randomblob(200), randomblob(200));
  2595. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2596. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2597. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2598. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2599. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2600. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2601. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2602. INSERT INTO t1 SELECT randomblob(200), randomblob(200) FROM t1;
  2603. }
  2604. db close
  2605. testvfs tv -default 1
  2606. tv sectorsize 16384;
  2607. tv devchar [list]
  2608. sqlite3 db test.db -vfs tv
  2609. execsql { UPDATE t1 SET x = randomblob(200) }
  2610. } {}
  2611. db close
  2612. tv delete
  2613. sqlite3_test_control_pending_byte $pending_prev
  2614. do_test 43.1 {
  2615. reset_db
  2616. execsql {
  2617. CREATE TABLE t1(x, y);
  2618. INSERT INTO t1 VALUES(1, 2);
  2619. CREATE TABLE t2(x, y);
  2620. INSERT INTO t2 VALUES(1, 2);
  2621. CREATE TABLE t3(x, y);
  2622. INSERT INTO t3 VALUES(1, 2);
  2623. }
  2624. db close
  2625. sqlite3 db test.db
  2626. db eval { PRAGMA mmap_size = 0 }
  2627. db eval { SELECT * FROM t1 }
  2628. sqlite3_db_status db CACHE_MISS 0
  2629. } {0 2 0}
  2630. do_test 43.2 {
  2631. db eval { SELECT * FROM t2 }
  2632. sqlite3_db_status db CACHE_MISS 1
  2633. } {0 3 0}
  2634. do_test 43.3 {
  2635. db eval { SELECT * FROM t3 }
  2636. sqlite3_db_status db CACHE_MISS 0
  2637. } {0 1 0}
  2638. finish_test