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