1180b56a1Sdanielk1977# 2002 March 6 2180b56a1Sdanielk1977# 3180b56a1Sdanielk1977# The author disclaims copyright to this source code. In place of 4180b56a1Sdanielk1977# a legal notice, here is a blessing: 5180b56a1Sdanielk1977# 6180b56a1Sdanielk1977# May you do good and not evil. 7180b56a1Sdanielk1977# May you find forgiveness for yourself and forgive others. 8180b56a1Sdanielk1977# May you share freely, never taking more than you give. 9180b56a1Sdanielk1977# 10180b56a1Sdanielk1977#*********************************************************************** 11180b56a1Sdanielk1977# This file implements regression tests for SQLite library. 12180b56a1Sdanielk1977# 13180b56a1Sdanielk1977# This file implements tests for the PRAGMA command. 14180b56a1Sdanielk1977# 155a8f9374Sdanielk1977# $Id: pragma2.test,v 1.4 2007/10/09 08:29:33 danielk1977 Exp $ 16180b56a1Sdanielk1977 17180b56a1Sdanielk1977set testdir [file dirname $argv0] 18180b56a1Sdanielk1977source $testdir/tester.tcl 19180b56a1Sdanielk1977 20180b56a1Sdanielk1977# Test organization: 21180b56a1Sdanielk1977# 22180b56a1Sdanielk1977# pragma2-1.*: Test freelist_count pragma on the main database. 23180b56a1Sdanielk1977# pragma2-2.*: Test freelist_count pragma on an attached database. 24180b56a1Sdanielk1977# pragma2-3.*: Test trying to write to the freelist_count is a no-op. 25d4b5c60eSdrh# pragma2-4.*: Tests for PRAGMA cache_spill 26180b56a1Sdanielk1977# 27180b56a1Sdanielk1977 284152e677Sdanielk1977ifcapable !pragma||!schema_pragmas { 29180b56a1Sdanielk1977 finish_test 30180b56a1Sdanielk1977 return 31180b56a1Sdanielk1977} 32180b56a1Sdanielk1977 33e045d483Sdrhtest_set_config_pagecache 0 0 34e045d483Sdrh 35180b56a1Sdanielk1977# Delete the preexisting database to avoid the special setup 36180b56a1Sdanielk1977# that the "all.test" script does. 37180b56a1Sdanielk1977# 38180b56a1Sdanielk1977db close 39fda06befSmistachkindelete_file test.db test.db-journal 40fda06befSmistachkindelete_file test3.db test3.db-journal 41180b56a1Sdanielk1977sqlite3 db test.db; set DB [sqlite3_connection_pointer db] 425db82818Sdrhdb eval {PRAGMA auto_vacuum=0} 43180b56a1Sdanielk1977 449d356fbeSdrh 45b3366b99Sdrh# EVIDENCE-OF: R-11211-21323 PRAGMA schema.freelist_count; Return the 469d356fbeSdrh# number of unused pages in the database file. 479d356fbeSdrh# 48180b56a1Sdanielk1977do_test pragma2-1.1 { 49180b56a1Sdanielk1977 execsql { 50180b56a1Sdanielk1977 PRAGMA freelist_count; 51180b56a1Sdanielk1977 } 52180b56a1Sdanielk1977} {0} 53180b56a1Sdanielk1977do_test pragma2-1.2 { 54180b56a1Sdanielk1977 execsql { 55180b56a1Sdanielk1977 CREATE TABLE abc(a, b, c); 56180b56a1Sdanielk1977 PRAGMA freelist_count; 57180b56a1Sdanielk1977 } 58180b56a1Sdanielk1977} {0} 59180b56a1Sdanielk1977do_test pragma2-1.3 { 60180b56a1Sdanielk1977 execsql { 61180b56a1Sdanielk1977 DROP TABLE abc; 62180b56a1Sdanielk1977 PRAGMA freelist_count; 63180b56a1Sdanielk1977 } 64180b56a1Sdanielk1977} {1} 65180b56a1Sdanielk1977do_test pragma2-1.4 { 66180b56a1Sdanielk1977 execsql { 67180b56a1Sdanielk1977 PRAGMA main.freelist_count; 68180b56a1Sdanielk1977 } 69180b56a1Sdanielk1977} {1} 70180b56a1Sdanielk1977 71fda06befSmistachkinforcedelete test2.db 72fda06befSmistachkinforcedelete test2.db-journal 73180b56a1Sdanielk1977 745a8f9374Sdanielk1977ifcapable attach { 75180b56a1Sdanielk1977 do_test pragma2-2.1 { 76180b56a1Sdanielk1977 execsql { 77180b56a1Sdanielk1977 ATTACH 'test2.db' AS aux; 785db82818Sdrh PRAGMA aux.auto_vacuum=OFF; 79180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 80180b56a1Sdanielk1977 } 81180b56a1Sdanielk1977 } {0} 82180b56a1Sdanielk1977 do_test pragma2-2.2 { 83180b56a1Sdanielk1977 execsql { 84180b56a1Sdanielk1977 CREATE TABLE aux.abc(a, b, c); 85180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 86180b56a1Sdanielk1977 } 87180b56a1Sdanielk1977 } {0} 88180b56a1Sdanielk1977 do_test pragma2-2.3 { 89180b56a1Sdanielk1977 set ::val [string repeat 0123456789 1000] 90180b56a1Sdanielk1977 execsql { 91180b56a1Sdanielk1977 INSERT INTO aux.abc VALUES(1, 2, $::val); 92180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 93180b56a1Sdanielk1977 } 94180b56a1Sdanielk1977 } {0} 95180b56a1Sdanielk1977 do_test pragma2-2.4 { 96180b56a1Sdanielk1977 expr {[file size test2.db] / 1024} 97180b56a1Sdanielk1977 } {11} 98180b56a1Sdanielk1977 do_test pragma2-2.5 { 99180b56a1Sdanielk1977 execsql { 100180b56a1Sdanielk1977 DELETE FROM aux.abc; 101180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 102180b56a1Sdanielk1977 } 103180b56a1Sdanielk1977 } {9} 104180b56a1Sdanielk1977 105180b56a1Sdanielk1977 do_test pragma2-3.1 { 106180b56a1Sdanielk1977 execsql { 107180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 108180b56a1Sdanielk1977 PRAGMA main.freelist_count; 109180b56a1Sdanielk1977 PRAGMA freelist_count; 110180b56a1Sdanielk1977 } 111180b56a1Sdanielk1977 } {9 1 1} 112180b56a1Sdanielk1977 do_test pragma2-3.2 { 113180b56a1Sdanielk1977 execsql { 114180b56a1Sdanielk1977 PRAGMA freelist_count = 500; 115180b56a1Sdanielk1977 PRAGMA freelist_count; 116180b56a1Sdanielk1977 } 117180b56a1Sdanielk1977 } {1 1} 118180b56a1Sdanielk1977 do_test pragma2-3.3 { 119180b56a1Sdanielk1977 execsql { 120180b56a1Sdanielk1977 PRAGMA aux.freelist_count = 500; 121180b56a1Sdanielk1977 PRAGMA aux.freelist_count; 122180b56a1Sdanielk1977 } 123180b56a1Sdanielk1977 } {9 9} 1245a8f9374Sdanielk1977} 125180b56a1Sdanielk1977 126d4b5c60eSdrh# Default setting of PRAGMA cache_spill is always ON 127d4b5c60eSdrh# 128*15427279Sdrh# EVIDENCE-OF: R-63549-59887 PRAGMA cache_spill; PRAGMA 129*15427279Sdrh# cache_spill=boolean; PRAGMA schema.cache_spill=N; 130e4bf4f08Sdrh# 131e4bf4f08Sdrh# EVIDENCE-OF: R-23955-02765 Cache_spill is enabled by default 132e4bf4f08Sdrh# 133d4b5c60eSdrhdb close 134d4b5c60eSdrhdelete_file test.db test.db-journal 135d3605a4fSdrhdelete_file test2.db test2.db-journal 136d4b5c60eSdrhsqlite3 db test.db 137d4b5c60eSdrhdo_execsql_test pragma2-4.1 { 138d66b2e02Sdrh PRAGMA main.cache_size=2000; 139d66b2e02Sdrh PRAGMA temp.cache_size=2000; 140d4b5c60eSdrh PRAGMA cache_spill; 141d4b5c60eSdrh PRAGMA main.cache_spill; 142d4b5c60eSdrh PRAGMA temp.cache_spill; 1439b0cf34fSdrh} {2000 2000 2000} 144d4b5c60eSdrhdo_execsql_test pragma2-4.2 { 145d4b5c60eSdrh PRAGMA cache_spill=OFF; 146d4b5c60eSdrh PRAGMA cache_spill; 147d4b5c60eSdrh PRAGMA main.cache_spill; 148d4b5c60eSdrh PRAGMA temp.cache_spill; 149d4b5c60eSdrh} {0 0 0} 150d4b5c60eSdrhdo_execsql_test pragma2-4.3 { 151d4b5c60eSdrh PRAGMA page_size=1024; 152d4b5c60eSdrh PRAGMA cache_size=50; 153d4b5c60eSdrh BEGIN; 154d4b5c60eSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d); 155d4b5c60eSdrh INSERT INTO t1 VALUES(1, randomblob(400), 1, randomblob(400)); 156d4b5c60eSdrh INSERT INTO t1 SELECT a+1, randomblob(400), a+1, randomblob(400) FROM t1; 157d4b5c60eSdrh INSERT INTO t1 SELECT a+2, randomblob(400), a+2, randomblob(400) FROM t1; 158d4b5c60eSdrh INSERT INTO t1 SELECT a+4, randomblob(400), a+4, randomblob(400) FROM t1; 159d4b5c60eSdrh INSERT INTO t1 SELECT a+8, randomblob(400), a+8, randomblob(400) FROM t1; 160d4b5c60eSdrh INSERT INTO t1 SELECT a+16, randomblob(400), a+16, randomblob(400) FROM t1; 161d4b5c60eSdrh INSERT INTO t1 SELECT a+32, randomblob(400), a+32, randomblob(400) FROM t1; 162d4b5c60eSdrh INSERT INTO t1 SELECT a+64, randomblob(400), a+64, randomblob(400) FROM t1; 163d4b5c60eSdrh COMMIT; 164d3605a4fSdrh ATTACH 'test2.db' AS aux1; 165d3605a4fSdrh CREATE TABLE aux1.t2(a INTEGER PRIMARY KEY, b, c, d); 166d3605a4fSdrh INSERT INTO t2 SELECT * FROM t1; 167d3605a4fSdrh DETACH aux1; 168d4b5c60eSdrh PRAGMA cache_spill=ON; 169d4b5c60eSdrh} {} 170e704713cSdansqlite3_release_memory 171e4bf4f08Sdrh# 172e4bf4f08Sdrh# EVIDENCE-OF: R-07634-40532 The cache_spill pragma enables or disables 173e4bf4f08Sdrh# the ability of the pager to spill dirty cache pages to the database 174e4bf4f08Sdrh# file in the middle of a transaction. 175e4bf4f08Sdrh# 176d4b5c60eSdrhdo_test pragma2-4.4 { 177d4b5c60eSdrh db eval { 178d4b5c60eSdrh BEGIN; 179d4b5c60eSdrh UPDATE t1 SET c=c+1; 180d4b5c60eSdrh PRAGMA lock_status; 181d4b5c60eSdrh } 182d4b5c60eSdrh} {main exclusive temp unknown} ;# EXCLUSIVE lock due to cache spill 1839b0cf34fSdrhdo_test pragma2-4.5.1 { 184d4b5c60eSdrh db eval { 1859b0cf34fSdrh ROLLBACK; 186d4b5c60eSdrh PRAGMA cache_spill=OFF; 1879b0cf34fSdrh PRAGMA Cache_Spill; 188d4b5c60eSdrh BEGIN; 1899b0cf34fSdrh UPDATE t1 SET c=c+1; 190d4b5c60eSdrh PRAGMA lock_status; 191d4b5c60eSdrh } 1929b0cf34fSdrh} {0 main reserved temp unknown} ;# No cache spill, so no exclusive lock 193*15427279Sdrh 194*15427279Sdrh 195*15427279Sdrh# EVIDENCE-OF: R-34657-61226 The "PRAGMA cache_spill=N" form of this 196*15427279Sdrh# pragma sets a minimum cache size threshold required for spilling to 197*15427279Sdrh# occur. 1989b0cf34fSdrhdo_test pragma2-4.5.2 { 1999b0cf34fSdrh db eval { 2009b0cf34fSdrh ROLLBACK; 2019b0cf34fSdrh PRAGMA cache_spill=100000; 2029b0cf34fSdrh PRAGMA cache_spill; 2039b0cf34fSdrh BEGIN; 2049b0cf34fSdrh UPDATE t1 SET c=c+1; 2059b0cf34fSdrh PRAGMA lock_status; 2069b0cf34fSdrh } 2074f9c8ec6Sdrh} {100000 main reserved temp unknown} ;# Big spill threshold -> no excl lock 20829fbdb73Sdrhifcapable !memorymanage { 2099b0cf34fSdrh do_test pragma2-4.5.3 { 2109b0cf34fSdrh db eval { 2119b0cf34fSdrh ROLLBACK; 2129b0cf34fSdrh PRAGMA cache_spill=25; 2134f9c8ec6Sdrh PRAGMA main.cache_spill; 2149b0cf34fSdrh BEGIN; 2159b0cf34fSdrh UPDATE t1 SET c=c+1; 2169b0cf34fSdrh PRAGMA lock_status; 2179b0cf34fSdrh } 2184f9c8ec6Sdrh } {50 main exclusive temp unknown} ;# Small cache spill -> exclusive lock 2194f9c8ec6Sdrh do_test pragma2-4.5.4 { 2204f9c8ec6Sdrh db eval { 2214f9c8ec6Sdrh ROLLBACK; 2224f9c8ec6Sdrh PRAGMA cache_spill(-25); 2234f9c8ec6Sdrh PRAGMA main.cache_spill; 2244f9c8ec6Sdrh BEGIN; 2254f9c8ec6Sdrh UPDATE t1 SET c=c+1; 2264f9c8ec6Sdrh PRAGMA lock_status; 2274f9c8ec6Sdrh } 2284f9c8ec6Sdrh } {50 main exclusive temp unknown} ;# Small cache spill -> exclusive lock 22929fbdb73Sdrh} 2309b0cf34fSdrh 231d4b5c60eSdrh 232d3605a4fSdrh# Verify that newly attached databases inherit the cache_spill=OFF 233d3605a4fSdrh# setting. 234d3605a4fSdrh# 235d3605a4fSdrhdo_execsql_test pragma2-4.6 { 2369b0cf34fSdrh ROLLBACK; 2379b0cf34fSdrh PRAGMA cache_spill=OFF; 238d3605a4fSdrh ATTACH 'test2.db' AS aux1; 239d3605a4fSdrh PRAGMA aux1.cache_size=50; 240d3605a4fSdrh BEGIN; 241d3605a4fSdrh UPDATE t2 SET c=c+1; 242d3605a4fSdrh PRAGMA lock_status; 243d3605a4fSdrh} {main unlocked temp unknown aux1 reserved} 244d3605a4fSdrhdo_execsql_test pragma2-4.7 { 245d3605a4fSdrh COMMIT; 246e704713cSdan} 247e704713cSdansqlite3_release_memory 248e704713cSdando_execsql_test pragma2-4.8 { 249d3605a4fSdrh PRAGMA cache_spill=ON; -- Applies to all databases 250d3605a4fSdrh BEGIN; 251d3605a4fSdrh UPDATE t2 SET c=c-1; 252d3605a4fSdrh PRAGMA lock_status; 253d3605a4fSdrh} {main unlocked temp unknown aux1 exclusive} 2544f9c8ec6Sdrhdb close 2554f9c8ec6Sdrhforcedelete test.db 2564f9c8ec6Sdrhsqlite3 db test.db 2574f9c8ec6Sdrh 2584f9c8ec6Sdrhdo_execsql_test pragma2-5.1 { 2594f9c8ec6Sdrh PRAGMA page_size=16384; 2604f9c8ec6Sdrh CREATE TABLE t1(x); 2614f9c8ec6Sdrh PRAGMA cache_size=2; 2624f9c8ec6Sdrh PRAGMA cache_spill=YES; 2634f9c8ec6Sdrh PRAGMA cache_spill; 2644f9c8ec6Sdrh} {2} 2654f9c8ec6Sdrhdo_execsql_test pragma2-5.2 { 2664f9c8ec6Sdrh PRAGMA cache_spill=NO; 2674f9c8ec6Sdrh PRAGMA cache_spill; 2684f9c8ec6Sdrh} {0} 2694f9c8ec6Sdrhdo_execsql_test pragma2-5.3 { 2704f9c8ec6Sdrh PRAGMA cache_spill(-51); 2714f9c8ec6Sdrh PRAGMA cache_spill; 2724f9c8ec6Sdrh} {3} 273d3605a4fSdrh 274e045d483Sdrhtest_restore_config_pagecache 275180b56a1Sdanielk1977finish_test 276