blob: 3ade8c1a07bd550f32a140a1c22473f91395f150 [file] [log] [blame]
danielk1977180b56a2007-06-24 08:00:42 +00001# 2002 March 6
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.
12#
13# This file implements tests for the PRAGMA command.
14#
danielk19775a8f9372007-10-09 08:29:32 +000015# $Id: pragma2.test,v 1.4 2007/10/09 08:29:33 danielk1977 Exp $
danielk1977180b56a2007-06-24 08:00:42 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Test organization:
21#
22# pragma2-1.*: Test freelist_count pragma on the main database.
23# pragma2-2.*: Test freelist_count pragma on an attached database.
24# pragma2-3.*: Test trying to write to the freelist_count is a no-op.
drhd4b5c602013-08-17 00:25:07 +000025# pragma2-4.*: Tests for PRAGMA cache_spill
danielk1977180b56a2007-06-24 08:00:42 +000026#
27
danielk19774152e672007-09-12 17:01:45 +000028ifcapable !pragma||!schema_pragmas {
danielk1977180b56a2007-06-24 08:00:42 +000029 finish_test
30 return
31}
32
33# Delete the preexisting database to avoid the special setup
34# that the "all.test" script does.
35#
36db close
mistachkinfda06be2011-08-02 00:57:34 +000037delete_file test.db test.db-journal
38delete_file test3.db test3.db-journal
danielk1977180b56a2007-06-24 08:00:42 +000039sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
drh5db82812007-06-27 10:20:00 +000040db eval {PRAGMA auto_vacuum=0}
danielk1977180b56a2007-06-24 08:00:42 +000041
drh9d356fb2015-02-27 20:28:08 +000042
43# EVIDENCE-OF: R-17887-14874 PRAGMA database.freelist_count; Return the
44# number of unused pages in the database file.
45#
danielk1977180b56a2007-06-24 08:00:42 +000046do_test pragma2-1.1 {
47 execsql {
48 PRAGMA freelist_count;
49 }
50} {0}
51do_test pragma2-1.2 {
52 execsql {
53 CREATE TABLE abc(a, b, c);
54 PRAGMA freelist_count;
55 }
56} {0}
57do_test pragma2-1.3 {
58 execsql {
59 DROP TABLE abc;
60 PRAGMA freelist_count;
61 }
62} {1}
63do_test pragma2-1.4 {
64 execsql {
65 PRAGMA main.freelist_count;
66 }
67} {1}
68
mistachkinfda06be2011-08-02 00:57:34 +000069forcedelete test2.db
70forcedelete test2.db-journal
danielk1977180b56a2007-06-24 08:00:42 +000071
danielk19775a8f9372007-10-09 08:29:32 +000072ifcapable attach {
73 do_test pragma2-2.1 {
74 execsql {
75 ATTACH 'test2.db' AS aux;
76 PRAGMA aux.auto_vacuum=OFF;
77 PRAGMA aux.freelist_count;
78 }
79 } {0}
80 do_test pragma2-2.2 {
81 execsql {
82 CREATE TABLE aux.abc(a, b, c);
83 PRAGMA aux.freelist_count;
84 }
85 } {0}
86 do_test pragma2-2.3 {
87 set ::val [string repeat 0123456789 1000]
88 execsql {
89 INSERT INTO aux.abc VALUES(1, 2, $::val);
90 PRAGMA aux.freelist_count;
91 }
92 } {0}
93 do_test pragma2-2.4 {
94 expr {[file size test2.db] / 1024}
95 } {11}
96 do_test pragma2-2.5 {
97 execsql {
98 DELETE FROM aux.abc;
99 PRAGMA aux.freelist_count;
100 }
101 } {9}
102
103 do_test pragma2-3.1 {
104 execsql {
105 PRAGMA aux.freelist_count;
106 PRAGMA main.freelist_count;
107 PRAGMA freelist_count;
108 }
109 } {9 1 1}
110 do_test pragma2-3.2 {
111 execsql {
112 PRAGMA freelist_count = 500;
113 PRAGMA freelist_count;
114 }
115 } {1 1}
116 do_test pragma2-3.3 {
117 execsql {
118 PRAGMA aux.freelist_count = 500;
119 PRAGMA aux.freelist_count;
120 }
121 } {9 9}
122}
danielk1977180b56a2007-06-24 08:00:42 +0000123
drhd4b5c602013-08-17 00:25:07 +0000124# Default setting of PRAGMA cache_spill is always ON
125#
drhe4bf4f02013-10-11 20:14:37 +0000126# EVIDENCE-OF: R-51036-62828 PRAGMA cache_spill; PRAGMA
127# cache_spill=boolean;
128#
129# EVIDENCE-OF: R-23955-02765 Cache_spill is enabled by default
130#
drhd4b5c602013-08-17 00:25:07 +0000131db close
132delete_file test.db test.db-journal
drhd3605a42013-08-17 15:42:29 +0000133delete_file test2.db test2.db-journal
drhd4b5c602013-08-17 00:25:07 +0000134sqlite3 db test.db
135do_execsql_test pragma2-4.1 {
136 PRAGMA cache_spill;
137 PRAGMA main.cache_spill;
138 PRAGMA temp.cache_spill;
139} {1 1 1}
140do_execsql_test pragma2-4.2 {
141 PRAGMA cache_spill=OFF;
142 PRAGMA cache_spill;
143 PRAGMA main.cache_spill;
144 PRAGMA temp.cache_spill;
145} {0 0 0}
146do_execsql_test pragma2-4.3 {
147 PRAGMA page_size=1024;
148 PRAGMA cache_size=50;
149 BEGIN;
150 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
151 INSERT INTO t1 VALUES(1, randomblob(400), 1, randomblob(400));
152 INSERT INTO t1 SELECT a+1, randomblob(400), a+1, randomblob(400) FROM t1;
153 INSERT INTO t1 SELECT a+2, randomblob(400), a+2, randomblob(400) FROM t1;
154 INSERT INTO t1 SELECT a+4, randomblob(400), a+4, randomblob(400) FROM t1;
155 INSERT INTO t1 SELECT a+8, randomblob(400), a+8, randomblob(400) FROM t1;
156 INSERT INTO t1 SELECT a+16, randomblob(400), a+16, randomblob(400) FROM t1;
157 INSERT INTO t1 SELECT a+32, randomblob(400), a+32, randomblob(400) FROM t1;
158 INSERT INTO t1 SELECT a+64, randomblob(400), a+64, randomblob(400) FROM t1;
159 COMMIT;
drhd3605a42013-08-17 15:42:29 +0000160 ATTACH 'test2.db' AS aux1;
161 CREATE TABLE aux1.t2(a INTEGER PRIMARY KEY, b, c, d);
162 INSERT INTO t2 SELECT * FROM t1;
163 DETACH aux1;
drhd4b5c602013-08-17 00:25:07 +0000164 PRAGMA cache_spill=ON;
165} {}
dane7047132013-08-19 18:37:18 +0000166sqlite3_release_memory
drhe4bf4f02013-10-11 20:14:37 +0000167#
168# EVIDENCE-OF: R-07634-40532 The cache_spill pragma enables or disables
169# the ability of the pager to spill dirty cache pages to the database
170# file in the middle of a transaction.
171#
drhd4b5c602013-08-17 00:25:07 +0000172do_test pragma2-4.4 {
173 db eval {
174 BEGIN;
175 UPDATE t1 SET c=c+1;
176 PRAGMA lock_status;
177 }
178} {main exclusive temp unknown} ;# EXCLUSIVE lock due to cache spill
179do_test pragma2-4.5 {
180 db eval {
181 COMMIT;
182 PRAGMA cache_spill=OFF;
183 BEGIN;
184 UPDATE t1 SET c=c-1;
185 PRAGMA lock_status;
186 }
187} {main reserved temp unknown} ;# No cache spill, so no exclusive lock
188
drhd3605a42013-08-17 15:42:29 +0000189# Verify that newly attached databases inherit the cache_spill=OFF
190# setting.
191#
192do_execsql_test pragma2-4.6 {
193 COMMIT;
194 ATTACH 'test2.db' AS aux1;
195 PRAGMA aux1.cache_size=50;
196 BEGIN;
197 UPDATE t2 SET c=c+1;
198 PRAGMA lock_status;
199} {main unlocked temp unknown aux1 reserved}
200do_execsql_test pragma2-4.7 {
201 COMMIT;
dane7047132013-08-19 18:37:18 +0000202}
203sqlite3_release_memory
204do_execsql_test pragma2-4.8 {
drhd3605a42013-08-17 15:42:29 +0000205 PRAGMA cache_spill=ON; -- Applies to all databases
206 BEGIN;
207 UPDATE t2 SET c=c-1;
208 PRAGMA lock_status;
209} {main unlocked temp unknown aux1 exclusive}
210
211
danielk1977180b56a2007-06-24 08:00:42 +0000212finish_test