danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 1 | # 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 | # |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 15 | # $Id: pragma2.test,v 1.4 2007/10/09 08:29:33 danielk1977 Exp $ |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $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. |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 25 | # pragma2-4.*: Tests for PRAGMA cache_spill |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 26 | # |
| 27 | |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 28 | ifcapable !pragma||!schema_pragmas { |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 29 | finish_test |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | # Delete the preexisting database to avoid the special setup |
| 34 | # that the "all.test" script does. |
| 35 | # |
| 36 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 37 | delete_file test.db test.db-journal |
| 38 | delete_file test3.db test3.db-journal |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 39 | sqlite3 db test.db; set DB [sqlite3_connection_pointer db] |
drh | 5db8281 | 2007-06-27 10:20:00 +0000 | [diff] [blame] | 40 | db eval {PRAGMA auto_vacuum=0} |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 41 | |
drh | 9d356fb | 2015-02-27 20:28:08 +0000 | [diff] [blame] | 42 | |
| 43 | # EVIDENCE-OF: R-17887-14874 PRAGMA database.freelist_count; Return the |
| 44 | # number of unused pages in the database file. |
| 45 | # |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 46 | do_test pragma2-1.1 { |
| 47 | execsql { |
| 48 | PRAGMA freelist_count; |
| 49 | } |
| 50 | } {0} |
| 51 | do_test pragma2-1.2 { |
| 52 | execsql { |
| 53 | CREATE TABLE abc(a, b, c); |
| 54 | PRAGMA freelist_count; |
| 55 | } |
| 56 | } {0} |
| 57 | do_test pragma2-1.3 { |
| 58 | execsql { |
| 59 | DROP TABLE abc; |
| 60 | PRAGMA freelist_count; |
| 61 | } |
| 62 | } {1} |
| 63 | do_test pragma2-1.4 { |
| 64 | execsql { |
| 65 | PRAGMA main.freelist_count; |
| 66 | } |
| 67 | } {1} |
| 68 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 69 | forcedelete test2.db |
| 70 | forcedelete test2.db-journal |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 71 | |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 72 | ifcapable 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 | } |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 123 | |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 124 | # Default setting of PRAGMA cache_spill is always ON |
| 125 | # |
drh | e4bf4f0 | 2013-10-11 20:14:37 +0000 | [diff] [blame] | 126 | # 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 | # |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 131 | db close |
| 132 | delete_file test.db test.db-journal |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 133 | delete_file test2.db test2.db-journal |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 134 | sqlite3 db test.db |
| 135 | do_execsql_test pragma2-4.1 { |
| 136 | PRAGMA cache_spill; |
| 137 | PRAGMA main.cache_spill; |
| 138 | PRAGMA temp.cache_spill; |
| 139 | } {1 1 1} |
| 140 | do_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} |
| 146 | do_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; |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 160 | 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; |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 164 | PRAGMA cache_spill=ON; |
| 165 | } {} |
dan | e704713 | 2013-08-19 18:37:18 +0000 | [diff] [blame] | 166 | sqlite3_release_memory |
drh | e4bf4f0 | 2013-10-11 20:14:37 +0000 | [diff] [blame] | 167 | # |
| 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 | # |
drh | d4b5c60 | 2013-08-17 00:25:07 +0000 | [diff] [blame] | 172 | do_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 |
| 179 | do_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 | |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 189 | # Verify that newly attached databases inherit the cache_spill=OFF |
| 190 | # setting. |
| 191 | # |
| 192 | do_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} |
| 200 | do_execsql_test pragma2-4.7 { |
| 201 | COMMIT; |
dan | e704713 | 2013-08-19 18:37:18 +0000 | [diff] [blame] | 202 | } |
| 203 | sqlite3_release_memory |
| 204 | do_execsql_test pragma2-4.8 { |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 205 | 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 | |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 212 | finish_test |