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