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