3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 set testdir [file dirname $argv0]
13 source $testdir/tester.tcl
14 set testprefix temptable2
17 CREATE TEMP TABLE t1(a, b);
18 CREATE INDEX i1 ON t1(a, b);
22 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100000 )
23 INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM X;
27 PRAGMA temp.integrity_check;
30 #-------------------------------------------------------------------------
34 CREATE TEMP TABLE t2(a, b);
35 INSERT INTO t2 VALUES(1, 2);
40 INSERT INTO t2 VALUES(3, 4);
49 #-------------------------------------------------------------------------
52 do_execsql_test 3.1.1 {
53 PRAGMA main.cache_size = 10;
54 PRAGMA temp.cache_size = 10;
56 CREATE TEMP TABLE t1(a, b);
57 CREATE INDEX i1 ON t1(a, b);
59 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000 )
60 INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
62 SELECT count(*) FROM t1;
64 do_execsql_test 3.1.2 {
66 UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==0;
69 do_execsql_test 3.1.3 {
70 SELECT count(*) FROM t1;
72 do_execsql_test 3.1.4 { PRAGMA temp.integrity_check } {ok}
74 do_execsql_test 3.2.1 {
76 UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==0;
78 UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==1;
80 UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==2;
83 do_execsql_test 3.2.2 { PRAGMA temp.integrity_check } {ok}
85 #-------------------------------------------------------------------------
88 do_execsql_test 4.1.1 {
89 PRAGMA main.cache_size = 10;
90 PRAGMA temp.cache_size = 10;
92 CREATE TEMP TABLE t1(a, b);
93 CREATE INDEX i1 ON t1(a, b);
95 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<10 )
96 INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
98 SELECT count(*) FROM t1;
99 PRAGMA temp.page_count;
102 do_execsql_test 4.1.2 {
104 UPDATE t1 SET b=randomblob(100);
108 do_execsql_test 4.1.3 {
109 CREATE TEMP TABLE t2(a, b);
110 CREATE INDEX i2 ON t2(a, b);
111 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 )
112 INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x;
114 SELECT count(*) FROM t2;
115 SELECT count(*) FROM t1;
119 set n [db one { PRAGMA temp.page_count }]
120 expr ($n >280 && $n < 300)
123 do_execsql_test 4.1.4 { PRAGMA temp.integrity_check } {ok}
125 #-------------------------------------------------------------------------
128 do_execsql_test 5.1.1 {
129 PRAGMA main.cache_size = 10;
130 PRAGMA temp.cache_size = 10;
132 CREATE TEMP TABLE t2(a, b);
133 CREATE INDEX i2 ON t2(a, b);
134 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 )
135 INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x;
137 CREATE TEMP TABLE t1(a, b);
138 CREATE INDEX i1 ON t1(a, b);
139 INSERT INTO t1 VALUES(1, 2);
142 # Test that the temp database is now much bigger than the configured
143 # cache size (10 pages).
145 set n [db one { PRAGMA temp.page_count }]
146 expr ($n > 270 && $n < 290)
149 do_execsql_test 5.1.3 {
152 UPDATE t2 SET a=randomblob(100);
153 SELECT count(*) FROM t1;
157 do_execsql_test 5.1.4 {
158 UPDATE t2 SET a=randomblob(100);
163 do_execsql_test 5.1.5 { PRAGMA temp.integrity_check } {ok}
165 #-------------------------------------------------------------------------
168 # 1. Page is DIRTY at the start of a transaction.
169 # 2. Page is written out as part of the transaction.
170 # 3. Page is then read back in.
171 # 4. Transaction is rolled back. Is the page now clean or dirty?
173 # This actually does work. Step 4 marks the page as clean. But it also
174 # writes to the database file itself. So marking it clean is correct -
175 # the page does match the contents of the db file.
179 do_execsql_test 6.1 {
180 PRAGMA main.cache_size = 10;
181 PRAGMA temp.cache_size = 10;
183 CREATE TEMP TABLE t1(x);
184 INSERT INTO t1 VALUES('one');
186 CREATE TEMP TABLE t2(a, b);
187 CREATE INDEX i2 ON t2(a, b);
188 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 )
189 INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x;
192 do_execsql_test 6.2 {
193 UPDATE t1 SET x='two'; -- step 1
195 UPDATE t2 SET a=randomblob(100); -- step 2
196 SELECT * FROM t1; -- step 3
199 SELECT count(*) FROM t2;
203 #-------------------------------------------------------------------------
207 do_execsql_test 7.1 {
208 PRAGMA auto_vacuum=INCREMENTAL;
210 INSERT INTO t1 VALUES(zeroblob(900));
211 INSERT INTO t1 VALUES(zeroblob(900));
212 INSERT INTO t1 SELECT x FROM t1;
213 INSERT INTO t1 SELECT x FROM t1;
214 INSERT INTO t1 SELECT x FROM t1;
215 INSERT INTO t1 SELECT x FROM t1;
217 DELETE FROM t1 WHERE rowid%2;
218 PRAGMA incremental_vacuum(4);
220 PRAGMA integrity_check;
223 #-------------------------------------------------------------------------
224 # Try changing the page size using a backup operation when pages are
225 # stored in main-memory only.
228 do_execsql_test 8.1 {
229 PRAGMA auto_vacuum = OFF;
230 CREATE TABLE t2(a, b);
231 CREATE INDEX i2 ON t2(a, b);
232 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<20 )
233 INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x ORDER BY 1, 2;
240 PRAGMA auto_vacuum = OFF;
241 PRAGMA page_size = 8192;
242 CREATE TABLE t1(a, b);
243 CREATE INDEX i1 ON t1(a, b);
244 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100 )
245 INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x ORDER BY 1, 2;
251 sqlite3_backup B tmp main db main
258 SELECT count(*) FROM t1;
259 PRAGMA integrity_check;
265 tmp eval { UPDATE t1 SET a=randomblob(100) }
269 sqlite3_backup B tmp main db main
276 #-------------------------------------------------------------------------
277 # Try inserts and deletes with a large db in auto-vacuum mode. Check
285 do_execsql_test 9.$tn.1.1 {
286 PRAGMA cache_size = 15;
287 PRAGMA auto_vacuum = 1;
289 execsql "PRAGMA journal_mode = $mode"
291 do_execsql_test 9.$tn.1.2 {
292 CREATE TABLE tx(a, b);
293 CREATE INDEX i1 ON tx(a);
294 CREATE INDEX i2 ON tx(b);
295 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000 )
296 INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x;
299 for {set i 2} {$i<20} {incr i} {
300 do_execsql_test 9.$tn.$i.1 { DELETE FROM tx WHERE (random()%3)==0 }
302 do_execsql_test 9.$tn.$i.2 { PRAGMA integrity_check } ok
304 do_execsql_test 9.$tn.$i.3 {
305 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<400 )
306 INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x;
309 do_execsql_test 9.$tn.$i.4 { PRAGMA integrity_check } ok
311 do_execsql_test 9.$tn.$i.5 {
313 DELETE FROM tx WHERE (random()%3)==0;
314 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 )
315 INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x;
319 do_execsql_test 9.$tn.$i.6 { PRAGMA integrity_check } ok
323 #-------------------------------------------------------------------------
324 # When using mmap mode with a temp file, SQLite must search the cache
325 # before using a mapped page even when there is no write transaction
326 # open. For a temp file, the on-disk version may not be up to date.
329 do_execsql_test 10.0 {
330 PRAGMA cache_size = 50;
331 PRAGMA page_size = 1024;
332 CREATE TABLE t1(a, b, PRIMARY KEY(a)) WITHOUT ROWID;
333 CREATE INDEX i1 ON t1(a);
334 CREATE TABLE t2(x, y);
335 INSERT INTO t2 VALUES(1, 2);
338 do_execsql_test 10.1 {
340 WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 )
341 INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x;
343 INSERT INTO t2 VALUES(3, 4);
347 if {[permutation]!="journaltest" && $::TEMP_STORE<2} {
348 # The journaltest permutation does not support mmap, so this part of
349 # the test is omitted.
350 do_execsql_test 10.2 { PRAGMA mmap_size = 512000 } 512000
354 do_execsql_test 10.3 { SELECT * FROM t2 } {1 2 3 4}
355 do_execsql_test 10.4 { PRAGMA integrity_check } ok