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