Debian Codegraph @

www/sharedcache.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>SQLite Shared-Cache Mode</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 SQLite Shared-Cache Mode
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#sqlite_shared_cache_mode">1. SQLite Shared-Cache Mode</a></div>
129 <div class="fancy-toc2"><a href="#use_of_shared_cache_is_discouraged">1.1. Use of shared-cache is discouraged</a></div>
130 <div class="fancy-toc1"><a href="#shared_cache_locking_model">2. Shared-Cache Locking Model</a></div>
131 <div class="fancy-toc2"><a href="#transaction_level_locking">2.1. Transaction Level Locking</a></div>
132 <div class="fancy-toc2"><a href="#table_level_locking">2.2. Table Level Locking</a></div>
133 <div class="fancy-toc3"><a href="#read_uncommitted_isolation_mode">2.2.1. Read-Uncommitted Isolation Mode</a></div>
134 <div class="fancy-toc2"><a href="#schema_sqlite_schema_level_locking">2.3. Schema (sqlite_schema) Level Locking</a></div>
135 <div class="fancy-toc1"><a href="#thread_related_issues">3. Thread Related Issues</a></div>
136 <div class="fancy-toc1"><a href="#shared_cache_and_virtual_tables">4. Shared Cache And Virtual Tables</a></div>
137 <div class="fancy-toc1"><a href="#enabling_shared_cache_mode">5. Enabling Shared-Cache Mode</a></div>
138 <div class="fancy-toc1"><a href="#shared_cache_and_in_memory_databases">6. Shared Cache And In-Memory Databases</a></div>
139 </div>
140 </div>
141 <script>
142 function toggle_toc(){
143 var sub = document.getElementById("toc_sub")
144 var mk = document.getElementById("toc_mk")
145 if( sub.style.display!="block" ){
146 sub.style.display = "block";
147 mk.innerHTML = "&#x25bc;";
148 } else {
149 sub.style.display = "none";
150 mk.innerHTML = "&#x25ba;";
151 }
152 }
153 </script>
154 </div>
155
156
157
158
159
160 <a name="sqlite_shared_cache_mode"></a>
161
162 <h1 id="sqlite_shared_cache_mode"><span>1. </span>SQLite Shared-Cache Mode</h1>
163
164 <p>Starting with <a href="releaselog/3_3_0.html">version 3.3.0</a> (2006-01-11),
165 SQLite includes a special "shared-cache"
166 mode (disabled by default) intended for use in embedded servers. If
167 shared-cache mode is enabled and a thread establishes multiple connections
168 to the same database, the connections share a single data and schema cache.
169 This can significantly reduce the quantity of memory and IO required by
170 the system.</p>
171
172 <p>In <a href="releaselog/3_5_0.html">version 3.5.0</a> (2007-09-04),
173 shared-cache mode was modified so that the same
174 cache can be shared across an entire process rather than just within
175 a single thread. Prior to this change, there were restrictions on
176 passing database connections between threads. Those restrictions were
177 dropped in 3.5.0 update. This document describes shared-cache mode
178 as of version 3.5.0.</p>
179
180 <p>Shared-cache mode changes the semantics
181 of the locking model in some cases. The details are described by
182 this document. A basic understanding of the normal SQLite locking model (see
183 <a href="lockingv3.html">File Locking And Concurrency In SQLite Version 3</a>
184 for details) is assumed.</p>
185
186 <a name="dontuse"></a>
187
188 <h2 id="use_of_shared_cache_is_discouraged"><span>1.1. </span>Use of shared-cache is discouraged</h2>
189
190 <p>Shared-cache mode is an obsolete feature. The use of shared-cache mode
191 is discouraged. Most use cases for shared-cache are better served by
192 <a href="wal.html">WAL mode</a>.
193
194 </p><p>Shared-cache mode was invented in 2006 at the request of developers
195 of <a href="https://en.wikipedia.org/wiki/Symbian">Symbian</a>. Their problem was that
196 if the contacts database on the phone was being synced, that would lock the
197 database file. Then if a call came in, the database lock would prevent them
198 from querying the contacts database in order to find the appropriate
199 ring-tone for the incoming call, or a photo of the caller to show on screen,
200 and so forth.
201 <a href="wal.html">WAL mode</a> (circa 2010) is a better solution to this problem as it permits
202 simultaneous access without breaking transaction isolation.
203
204 </p><p>Applications that build their own copy of SQLite from source code
205 are encouraged to use the <a href="compile.html#omit_shared_cache">-DSQLITE_OMIT_SHARED_CACHE</a> compile-time option,
206 as the resulting binary will be both smaller and faster.
207
208 </p><p>The shared-cache interfaces described here will continue to be supported
209 in SQLite, to insure full backwards compatibility. However, the use of
210 shared-cache is discouraged.
211
212 </p><h1 id="shared_cache_locking_model"><span>2. </span>Shared-Cache Locking Model</h1>
213
214 <p>Externally, from the point of view of another process or thread, two
215 or more <a href="c3ref/sqlite3.html">database connections</a> using a shared-cache appear as a single
216 connection. The locking protocol used to arbitrate between multiple
217 shared-caches or regular database users is described elsewhere.
218 </p>
219
220 <table style="margin:auto">
221 <tr><td>
222 <img src="images/shared.gif">
223
224 </td></tr></table>
225 <p style="font-style:italic;text-align:center">Figure 1</p>
226
227 <p>Figure 1 depicts an example runtime configuration where three
228 database connections have been established. Connection 1 is a normal
229 SQLite database connection. Connections 2 and 3 share a cache
230 The normal locking
231 protocol is used to serialize database access between connection 1 and
232 the shared cache. The internal protocol used to serialize (or not, see
233 "Read-Uncommitted Isolation Mode" below) access to the shared-cache by
234 connections 2 and 3 is described in the remainder of this section.
235 </p>
236
237 <p>There are three levels to the shared-cache locking model,
238 transaction level locking, table level locking and schema level locking.
239 They are described in the following three sub-sections.</p>
240
241 <h2 id="transaction_level_locking"><span>2.1. </span>Transaction Level Locking</h2>
242
243 <p>SQLite connections can open two kinds of transactions, read and write
244 transactions. This is not done explicitly, a transaction is implicitly a
245 read-transaction until it first writes to a database table, at which point
246 it becomes a write-transaction.
247 </p>
248 <p>At most one connection to a single shared cache may open a
249 write transaction at any one time. This may co-exist with any number of read
250 transactions.
251 </p>
252
253 <h2 id="table_level_locking"><span>2.2. </span>Table Level Locking</h2>
254
255 <p>When two or more connections use a shared-cache, locks are used to
256 serialize concurrent access attempts on a per-table basis. Tables support
257 two types of locks, "read-locks" and "write-locks". Locks are granted to
258 connections - at any one time, each database connection has either a
259 read-lock, write-lock or no lock on each database table.
260 </p>
261
262 <p>At any one time, a single table may have any number of active read-locks
263 or a single active write lock. To read data from a table, a connection must
264 first obtain a read-lock. To write to a table, a connection must obtain a
265 write-lock on that table. If a required table lock cannot be obtained,
266 the query fails and SQLITE_LOCKED is returned to the caller.
267 </p>
268
269 <p>Once a connection obtains a table lock, it is not released until the
270 current transaction (read or write) is concluded.
271 </p>
272
273 <h3 id="read_uncommitted_isolation_mode"><span>2.2.1. </span>Read-Uncommitted Isolation Mode</h3>
274
275 <p>The behaviour described above may be modified slightly by using the
276 <a href="pragma.html#pragma_read_uncommitted">read_uncommitted</a> pragma to change the isolation level from serialized
277 (the default), to read-uncommitted.</p>
278
279 <p> A database connection in read-uncommitted mode does not attempt
280 to obtain read-locks before reading from database tables as described
281 above. This can lead to inconsistent query results if another database
282 connection modifies a table while it is being read, but it also means that
283 a read-transaction opened by a connection in read-uncommitted mode can
284 neither block nor be blocked by any other connection.</p>
285
286 <p>Read-uncommitted mode has no effect on the locks required to write to
287 database tables (i.e. read-uncommitted connections must still obtain
288 write-locks and hence database writes may still block or be blocked).
289 Also, read-uncommitted mode has no effect on the <a href="schematab.html">sqlite_schema</a>
290 locks required by the rules enumerated below (see section
291 "Schema (sqlite_schema) Level Locking").
292 </p>
293
294 <blockquote><pre>
295 /* Set the value of the read-uncommitted flag:
296 **
297 ** True -> Set the connection to read-uncommitted mode.
298 ** False -> Set the connection to serialized (the default) mode.
299 */
300 PRAGMA read_uncommitted = &lt;boolean&gt;;
301
302 /* Retrieve the current value of the read-uncommitted flag */
303 PRAGMA read_uncommitted;
304 </pre></blockquote>
305
306 <h2 id="schema_sqlite_schema_level_locking"><span>2.3. </span>Schema (sqlite_schema) Level Locking</h2>
307
308 <p>The <a href="schematab.html">sqlite_schema table</a> supports shared-cache read and write
309 locks in the same way as all other database tables (see description
310 above). The following special rules also apply:
311 </p>
312
313 <ul>
314 <li>A connection must obtain a read-lock on <i>sqlite_schema</i> before
315 accessing any database tables or obtaining any other read or write locks.</li>
316 <li>Before executing a statement that modifies the database schema (i.e.
317 a CREATE or DROP TABLE statement), a connection must obtain a write-lock on
318 <i>sqlite_schema</i>.
319 </li>
320 <li>A connection may not compile an SQL statement if any other connection
321 is holding a write-lock on the <i>sqlite_schema</i> table of any attached
322 database (including the default database, "main").
323 </li>
324 </ul>
325
326 <h1 id="thread_related_issues"><span>3. </span>Thread Related Issues</h1>
327
328 <p>In SQLite versions 3.3.0 through 3.4.2 when shared-cache mode is enabled,
329 a database connection may only be
330 used by the thread that called <a href="c3ref/open.html">sqlite3_open()</a> to create it.
331 And a connection could only share cache with another connection in the
332 same thread.
333 These restrictions were dropped beginning with SQLite
334 <a href="releaselog/3_5_0.html">version 3.5.0</a> (2007-09-04).
335 </p>
336
337 <h1 id="shared_cache_and_virtual_tables"><span>4. </span>Shared Cache And Virtual Tables</h1>
338
339 <p>
340 In older versions of SQLite,
341 shared cache mode could not be used together with virtual tables.
342 This restriction was removed in SQLite <a href="releaselog/3_6_17.html">version 3.6.17</a> (2009-08-10).
343
344 </p><h1 id="enabling_shared_cache_mode"><span>5. </span>Enabling Shared-Cache Mode</h1>
345
346 <p>Shared-cache mode is enabled on a per-process basis. Using the C
347 interface, the following API can be used to globally enable or disable
348 shared-cache mode:
349 </p>
350
351 <blockquote><pre>
352 int sqlite3_enable_shared_cache(int);
353 </pre></blockquote>
354
355 <p>Each call to <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a> affects subsequent database
356 connections created using <a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a>, or
357 <a href="c3ref/open.html">sqlite3_open_v2()</a>. Database connections that already exist are
358 unaffected. Each call to <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a> overrides
359 all previous calls within the same process.
360 </p>
361
362 <p>Individual database connections created using <a href="c3ref/open.html">sqlite3_open_v2()</a> can
363 choose to participate or not participate in shared cache mode by using
364 the <a href="c3ref/c_open_autoproxy.html">SQLITE_OPEN_SHAREDCACHE</a> or <a href="c3ref/c_open_autoproxy.html">SQLITE_OPEN_PRIVATECACHE</a> flags the
365 third parameter. The use of either of these flags overrides the
366 global shared cache mode setting established by <a href="c3ref/enable_shared_cache.html">sqlite3_enable_shared_cache()</a>.
367 No more than one of the flags should be used; if both SQLITE_OPEN_SHAREDCACHE
368 and SQLITE_OPEN_PRIVATECACHE flags are used in the third argument to
369 <a href="c3ref/open.html">sqlite3_open_v2()</a> then the behavior is undefined.</p>
370
371 <p>When <a href="uri.html">URI filenames</a> are used, the "cache" query parameter can be used
372 to specify whether or not the database will use shared cache. Use
373 "cache=shared" to enable shared cache and "cache=private" to disable
374 shared cache. The ability to use URI query parameters to specify the
375 cache sharing behavior of a database connection allows cache sharing to
376 be controlled in <a href="lang_attach.html">ATTACH</a> statements. For example:</p>
377
378 <blockquote><pre>
379 ATTACH 'file:aux.db?cache=shared' AS aux;
380 </pre></blockquote>
381
382 <a name="inmemsharedcache"></a>
383
384 <h1 id="shared_cache_and_in_memory_databases"><span>6. </span>Shared Cache And In-Memory Databases</h1>
385
386 <p>
387 Beginning with SQLite <a href="releaselog/3_7_13.html">version 3.7.13</a> (2012-06-11),
388 shared cache can be used on
389 <a href="inmemorydb.html">in-memory databases</a>, provided that the database is created using
390 a <a href="uri.html">URI filename</a>. For backwards compatibility, shared cache is always
391 disabled for in-memory
392 databases if the unadorned name ":memory:" is used to open the database.
393 Prior to version 3.7.13, shared cache was always
394 disabled for in-memory databases regardless of the database name used,
395 current system shared cache setting, or query parameters or flags.
396 </p>
397
398 <p>
399 Enabling shared-cache for an in-memory database allows two or more
400 database connections in the same process to have access to the same
401 in-memory database. An in-memory database in shared cache is automatically
402 deleted and memory is reclaimed when the last connection to that database
403 closes.
404 </p>
405 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/sharedcache.in?m=4a965f6f8f">2023-01-02 14:22:42</a> UTC </small></i></p>
406