Debian Codegraph @

www/rbu.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>The RBU Extension</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 The RBU Extension
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="#the_rbu_extension">1. The RBU Extension</a></div>
129 <div class="fancy-toc1"><a href="#rbu_updates">2. RBU Updates</a></div>
130 <div class="fancy-toc2"><a href="#rbu_update_limitations">2.1. RBU Update Limitations</a></div>
131 <div class="fancy-toc2"><a href="#preparing_an_rbu_update_file">2.2. Preparing an RBU Update File</a></div>
132 <div class="fancy-toc3"><a href="#the_rbu_database_schema">2.2.1. The RBU Database Schema</a></div>
133 <div class="fancy-toc3"><a href="#rbu_database_contents">2.2.2. RBU Database Contents</a></div>
134 <div class="fancy-toc3"><a href="#using_rbu_with_fts3_4_tables">2.2.3. Using RBU with FTS3/4 Tables</a></div>
135 <div class="fancy-toc3"><a href="#automatically_generating_rbu_updates_with_sqldiff">2.2.4. Automatically Generating RBU Updates with sqldiff</a></div>
136 <div class="fancy-toc2"><a href="#rbu_update_c_c_programming">2.3. RBU Update C/C++ Programming</a></div>
137 <div class="fancy-toc1"><a href="#rbu_vacuum">3. RBU Vacuum</a></div>
138 <div class="fancy-toc2"><a href="#rbu_vacuum_limitations">3.1. RBU Vacuum Limitations</a></div>
139 <div class="fancy-toc2"><a href="#rbu_vacuum_c_c_programming">3.2. RBU Vacuum C/C++ Programming</a></div>
140 </div>
141 </div>
142 <script>
143 function toggle_toc(){
144 var sub = document.getElementById("toc_sub")
145 var mk = document.getElementById("toc_mk")
146 if( sub.style.display!="block" ){
147 sub.style.display = "block";
148 mk.innerHTML = "&#x25bc;";
149 } else {
150 sub.style.display = "none";
151 mk.innerHTML = "&#x25ba;";
152 }
153 }
154 </script>
155 </div>
156
157
158
159 <h1 align="center" id="the_rbu_extension"><span>1. </span>The RBU Extension</h1>
160
161 <p>The RBU extension is an add-on for SQLite designed for use with large
162 SQLite database files on low-power devices at the edge of a network. RBU
163 may be used for two separate tasks:
164
165 </p><ul>
166 <li> <b>RBU Update operations</b>. An <a href="rbu.html#rbu_updates">RBU Update</a> is a bulk update of a
167 database file that may include many insert, update and delete
168 operations on one or more tables.
169 </li><li> <b>RBU Vacuum operations</b>. An <a href="rbu.html#rbu_vacuum">RBU Vacuum</a> optimizes and rebuilds an
170 entire database file, with results similar to SQLite's native VACUUM
171 command.
172 </li></ul>
173
174 <p>The acronym RBU stands for "Resumable Bulk Update".
175
176 </p><p>Both of the RBU functions may be accomplished using SQLite's built-in
177 SQL commands - RBU update via a series of <a href="lang_insert.html">INSERT</a>, <a href="lang_delete.html">DELETE</a> and
178 <a href="lang_update.html">UPDATE</a> commands within a single transaction, and RBU vacuum by a single
179 <a href="lang_vacuum.html">VACUUM</a> command. The RBU module provides the following advantages over
180 these simpler approaches:
181
182
183 </p><ol>
184 <li><b>RBU may be more efficient</b>
185
186 <p>The most efficient way to apply changes to a B-Tree (the data structure
187 that SQLite uses to store each table and index on disk) is to make the
188 changes in key order. But if an SQL table has one or more indexes, the key
189 order for each index may be different from the main table and the other
190 auxiliary indexes. As a result, when executing a series of <a href="lang_insert.html">INSERT</a>,
191 <a href="lang_update.html">UPDATE</a> and <a href="lang_delete.html">DELETE</a> statements it is not generally possible to order the
192 operations so that all b-trees are updated in key order. The RBU update
193 process works around this by applying all changes to the main table in one
194 pass, then applying changes to each index in separate passes, ensuring each
195 B-Tree is updated optimally. For a large database file (one that does not
196 fit in the OS disk cache) this procedure can result in two orders of
197 magnitude faster updates.
198
199 </p><p>An RBU Vacuum operation requires less temporary disk space and writes
200 less data to disk than an SQLite VACUUM. An SQLite VACUUM requires roughly
201 twice the size of the final database file in temporary disk space to run.
202 The total amount of data written is around three times the size of the
203 final database file. By contrast, an RBU Vacuum requires roughly the size
204 of the final database file in temporary disk space and writes a total of
205 twice that to disk.
206
207 </p><p>On the other hand, an RBU Vacuum uses more CPU than a regular SQLite
208 VACUUM - in one test as much as five times as much. For this reason, an RBU
209 Vacuum is often significantly slower than an SQLite VACUUM under the same
210 conditions.
211
212 </p></li><li><b>RBU runs in the background</b>
213
214 <p>An ongoing RBU operation (either an update or a vacuum) does not
215 interfere with read access to the database file.
216
217 </p></li><li><b>RBU runs incrementally</b>
218
219 <p>RBU operations may be suspended and then later resumed, perhaps with
220 intervening power outages and/or system resets. For an RBU update, the
221 original database content remains visible to all database readers until
222 the entire update has been applied - even if the update is suspended and
223 then later resumed.
224
225 </p></li></ol>
226
227 <p>The RBU extension is not enabled by default. To enable it, compile the
228 <a href="amalgamation.html">amalgamation</a> with the <a href="compile.html#enable_rbu">SQLITE_ENABLE_RBU</a> compile-time option.
229
230 <a name="rbu_updates"></a>
231
232 </p><h1 id="rbu_updates"><span>2. </span>RBU Updates</h1>
233 <h2 id="rbu_update_limitations"><span>2.1. </span>RBU Update Limitations</h2>
234
235 <p>The following limitations apply to RBU updates:
236
237 </p><ul>
238 <li><p>The changes must consist of <a href="lang_insert.html">INSERT</a>, <a href="lang_update.html">UPDATE</a>, and <a href="lang_delete.html">DELETE</a>
239 operations only. CREATE and DROP operations are not
240 supported.</p></li>
241 <li><p><a href="lang_insert.html">INSERT</a> statements may not use default values.</p></li>
242 <li><p><a href="lang_update.html">UPDATE</a> and <a href="lang_delete.html">DELETE</a> statements must identify the target rows
243 by rowid or by non-NULL PRIMARY KEY values.</p></li>
244 <li><p><a href="lang_update.html">UPDATE</a> statements may not modify PRIMARY KEY or rowid values.
245 </p></li>
246 <li><p>RBU updates cannot be applied to any tables that contain a column
247 named "rbu_control".</p></li>
248 <li><p>The RBU update will not fire any triggers.</p></li>
249 <li><p>The RBU update will not detect or prevent foreign key or
250 CHECK constraint violations.</p></li>
251 <li><p>All RBU updates use the "OR ROLLBACK" constraint handling mechanism.
252 </p></li>
253 <li><p>The target database may not be in <a href="wal.html">WAL mode</a>.</p></li>
254 <li><p></p><s>The target database may not contain <a href="expridx.html">indexes on expressions</a>.</s>
255 Indexes on expressions are supported beginning with SQLite 3.30.0
256 (2019-10-04).
257 </li><li><p>No other writes may occur on the target database while the
258 RBU update is being applied. A read-lock is held on the target
259 database to prevent this.</p></li>
260 </ul>
261
262
263 <h2 id="preparing_an_rbu_update_file"><span>2.2. </span>Preparing an RBU Update File</h2>
264
265 <p>All changes to be applied by RBU are stored in a separate SQLite database
266 called the "RBU database". The database that is to be modified is called
267 the "target database".
268
269 </p><p>For each table in the target database that will be modified by the update,
270 a corresponding table is created within the RBU database. The RBU database
271 table schema is not the same as that of the target database, but is derived
272 from it as <a href="rbu.html#database_tables">described below</a>.
273
274 </p><p>The RBU database table contains a single row for each target database
275 row inserted, updated or deleted by the update. Populating the RBU database
276 tables is described in <a href="rbu.html#database_contents">the following section</a>.
277 </p>
278
279 <a name="database_tables"></a>
280
281 <h3 id="the_rbu_database_schema"><span>2.2.1. </span>The RBU Database Schema</h3>
282
283 <p>
284 For each table in the target database, the RBU database should contain a table
285 named "data&lt;<i>integer</i>&gt;_&lt;<i>target-table-name</i>&gt;" where
286 &lt;<i>target-table-name</i>&gt; is the name of the table in the target
287 database and &lt;<i>integer</i>&gt; is any sequence of zero or more numeric
288 characters (0-9). Tables within the RBU database are processed in order by
289 name (from smallest to largest according to the BINARY collation sequence),
290 so the order in which target tables are updated is influenced by the selection
291 of the &lt;<i>integer</i>&gt; portion of the data_% table name. While this can
292 be useful when using RBU to update
293 <a href="rbu.html#fts4_tables">certain types of virtual tables</a>, there is normally no
294 reason to use anything other than an empty string in place of
295 &lt;<i>integer</i>&gt;.
296
297 </p><p>The data_% table must have all the same columns as the target table, plus
298 one additional column named "rbu_control". The data_% table should have no
299 PRIMARY KEY or UNIQUE constraints, but each column should have the same type as
300 the corresponding column in the target database. The rbu_control column should
301 have no type at all. For example, if the target database contains:
302
303 </p><div class="codeblock"><pre>CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
304 </pre></div>
305
306 <p>Then the RBU database should contain:
307
308 </p><div class="codeblock"><pre>CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
309 </pre></div>
310
311 <p>The order of the columns in the data_% table does not matter.
312
313 </p><p>If the target database table is a virtual table or a table that has no
314 PRIMARY KEY declaration, the data_% table must also contain a column
315 named "rbu_rowid". The rbu_rowid column is mapped to the tables <a href="lang_createtable.html#rowid">ROWID</a>.
316 For example, if the target database contains either of the following:
317
318 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE x1 USING fts3(a, b);
319 CREATE TABLE x1(a, b);
320 </pre></div>
321
322 <p>then the RBU database should contain:
323
324 </p><div class="codeblock"><pre>CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
325 </pre></div>
326
327 <p>Virtual tables for which the "rowid" column does
328 not function like a primary key value cannot be updated using RBU.
329
330 </p><p>
331 All non-hidden columns (i.e. all columns matched by "SELECT *") of the
332 target table must be present in the input table. For virtual tables,
333 hidden columns are optional - they are updated by RBU if present in
334 the input table, or not otherwise. For example, to write to an fts4
335 table with a hidden languageid column such as:
336
337 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
338 </pre></div>
339
340 <p>Either of the following input table schemas may be used:
341
342 </p><div class="codeblock"><pre>CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
343 CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
344 </pre></div>
345
346 <a name="database_contents"></a>
347
348 <h3 id="rbu_database_contents"><span>2.2.2. </span>RBU Database Contents</h3>
349
350 <p>For each row to INSERT into the target database as part of the RBU
351 update, the corresponding data_% table should contain a single record
352 with the "rbu_control" column set to contain integer value 0. The
353 other columns should be set to the values that make up the new record
354 to insert.
355
356 </p><p>The "rbu_control" column may also be set to integer value 2 for
357 an INSERT. In this case, the new row silently replaces any existing row that
358 has the same primary key values. This is equivalent to a DELETE followed by an
359 INSERT with the same primary key values. It is not the same as an SQL REPLACE
360 command, as in that case the new row may replace any conflicting rows (i.e.
361 those that conflict due to UNIQUE constraints or indexes), not just those with
362 conflicting primary keys.
363
364 </p><p>If the target database table has an INTEGER PRIMARY KEY, it is not
365 possible to insert a NULL value into the IPK column. Attempting to
366 do so results in an SQLITE_MISMATCH error.
367
368 </p><p>For each row to DELETE from the target database as part of the RBU
369 update, the corresponding data_% table should contain a single record
370 with the "rbu_control" column set to contain integer value 1. The
371 real primary key values of the row to delete should be stored in the
372 corresponding columns of the data_% table. The values stored in the
373 other columns are not used.
374
375 </p><p>For each row to UPDATE from the target database as part of the RBU
376 update, the corresponding data_% table should contain a single record
377 with the "rbu_control" column set to contain a value of type text.
378 The real primary key values identifying the row to update should be
379 stored in the corresponding columns of the data_% table row, as should
380 the new values of all columns being update. The text value in the
381 "rbu_control" column must contain the same number of characters as
382 there are columns in the target database table, and must consist entirely
383 of 'x' and '.' characters (or in some special cases 'd' - see below). For
384 each column that is being updated, the corresponding character is set to
385 'x'. For those that remain as they are, the corresponding character of the
386 rbu_control value should be set to '.'. For example, given the tables
387 above, the update statement:
388
389 </p><div class="codeblock"><pre>UPDATE t1 SET c = 'usa' WHERE a = 4;
390 </pre></div>
391
392 <p>is represented by the data_t1 row created by:
393
394 </p><div class="codeblock"><pre>INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
395 </pre></div>
396
397 <p>If RBU is used to update a large BLOB value within a target database, it
398 may be more efficient to store a patch or delta that can be used to modify
399 the existing BLOB instead of an entirely new value within the RBU database.
400 RBU allows deltas to be specified in two ways:
401
402 </p><ul>
403 <li> In the "fossil delta" format - the format used for blob deltas by the
404 <a href="http://fossil-scm.org">Fossil source-code management system</a>, or
405
406 </li><li> In a custom format defined by the RBU application.
407 </li></ul>
408
409 <p> The fossil delta format may only be used to update BLOB values. Instead
410 of storing the new BLOB within the data_% table, the fossil delta is stored
411 instead. And instead of specifying an 'x' as part of the rbu_control string
412 for the column to be updated, an 'f' character is stored. When processing
413 an 'f' update, RBU loads the original BLOB data from disk, applies the fossil
414 delta to it and stores the results back into the database file. The RBU
415 databases generated by <a href="rbu.html#sqldiff">sqldiff --rbu</a> make use of fossil deltas wherever
416 doing so would save space in the RBU database.
417
418 </p><p> To use a custom delta format, the RBU application must register a
419 user-defined SQL function named "rbu_delta" before beginning to process the
420 update. rbu_delta() will be invoked with two arguments - the original value
421 stored in the target table column and the delta value provided as part of
422 the RBU update. It should return the result of applying the delta to the
423 original value. To use the custom delta function, the character of the
424 rbu_control value corresponding to the target column to update must be
425 set to 'd' instead of 'x'. Then, instead of updating the target table with the
426 value stored in the corresponding data_% column, RBU invokes the user-defined
427 SQL function "rbu_delta()" and the store in the target table column.
428
429 </p><p>For example, this row:
430
431 </p><div class="codeblock"><pre>INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
432 </pre></div>
433
434 <p>causes RBU to update the target database table in a way similar to:
435
436 </p><div class="codeblock"><pre>UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
437 </pre></div>
438
439 <p>If the target database table is a virtual table or a table with no PRIMARY
440 KEY, the rbu_control value should not include a character corresponding
441 to the rbu_rowid value. For example, this:
442
443 </p><div class="codeblock"><pre>INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
444 VALUES(NULL, 'usa', 12, '.x');
445 </pre></div>
446
447
448 <p>causes a result similar to:
449
450 </p><div class="codeblock"><pre>UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
451 </pre></div>
452
453 <p>The data_% tables themselves should have no PRIMARY KEY declarations.
454 However, RBU is more efficient if reading the rows in from each data_%
455 table in "rowid" order is roughly the same as reading them sorted by
456 the PRIMARY KEY of the corresponding target database table. In other
457 words, rows should be sorted using the destination table PRIMARY KEY
458 fields before they are inserted into the data_% tables.
459
460 <a name="fts4_tables"></a>
461
462 </p><h3 id="using_rbu_with_fts3_4_tables"><span>2.2.3. </span>Using RBU with FTS3/4 Tables</h3>
463
464 <p>Usually, an <a href="fts3.html">FTS3 or FTS4</a> table is an example of a virtual table
465 with a rowid that works like a PRIMARY KEY. So, for the following FTS4 tables:
466
467 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE ft1 USING fts4(addr, text);
468 CREATE VIRTUAL TABLE ft2 USING fts4; -- implicit "content" column
469 </pre></div>
470
471 <p>The data_% tables may be created as follows:
472
473 </p><div class="codeblock"><pre>CREATE TABLE data_ft1 USING fts4(addr, text, rbu_rowid, rbu_control);
474 CREATE TABLE data_ft2 USING fts4(content, rbu_rowid, rbu_control);
475 </pre></div>
476
477 <p>And populated as if the target table were an ordinary SQLite table with no
478 explicit PRIMARY KEY columns.
479
480 </p><p><a href="fts3.html#_contentless_fts4_tables_">Contentless FTS4 tables</a> are handled similarly,
481 except that any attempt to update or delete rows will cause an error when
482 applying the update.
483
484 </p><p><a href="fts3.html#_external_content_fts4_tables_">External content FTS4 tables</a> may also be
485 updated using RBU. In this case the user is required to configure the RBU
486 database so that the same set of UPDATE, DELETE and INSERT operations are
487 applied to the FTS4 index as to the underlying content table. As for all
488 updates of external content FTS4 tables, the user is also required to ensure
489 that any UPDATE or DELETE operations are applied to the FTS4 index before
490 they are applied to the underlying content table (refer to FTS4 documentation
491 for a detailed explanation). In RBU, this is done by ensuring that the name
492 of the data_% table used to write to the FTS4 table sorts before the name
493 of the data_% table used to update the underlying content table using the
494 <a href="datatype3.html#collation">BINARY</a> collation sequence. In order to avoid duplicating data within the
495 RBU database, an SQL view may be used in place of one of the data_% tables.
496 For example, for the target database schema:
497
498 </p><div class="codeblock"><pre>CREATE TABLE ccc(addr, text);
499 CREATE VIRTUAL TABLE ccc_fts USING fts4(addr, text, content=ccc);
500 </pre></div>
501
502 <p>
503 The following RBU database schema may be used:
504
505 </p><div class="codeblock"><pre>CREATE TABLE data_ccc(addr, text, rbu_rowid, rbu_control);
506 CREATE VIEW data0_ccc_fts AS SELECT * FROM data_ccc;
507 </pre></div>
508
509 <p>
510 The data_ccc table may then be populated as normal with the updates intended
511 for target database table ccc. The same updates will be read by RBU from
512 the data0_ccc_fts view and applied to FTS table ccc_fts. Because
513 "data0_ccc_fts" is smaller than "data_ccc", the FTS table will be updated
514 first, as required.
515
516 </p><p>
517 Cases in which the underlying content table has an explicit INTEGER PRIMARY
518 KEY column are slightly more difficult, as the text values stored in the
519 rbu_control column are slightly different for the FTS index and its
520 underlying content table. For the underlying content table, a character
521 must be included in any rbu_control text values for the explicit IPK, but
522 for the FTS table itself, which has an implicit rowid, it should not. This
523 is inconvenient, but can be solved using a more complicated view, as follows:
524
525 </p><div class="codeblock"><pre>-- Target database schema
526 CREATE TABLE ddd(i INTEGER PRIMARY KEY, k TEXT);
527 CREATE VIRTUAL TABLE ddd_fts USING fts4(k, content=ddd);
528
529 -- RBU database schema
530 CREATE TABLE data_ccc(i, k, rbu_control);
531 CREATE VIEW data0_ccc_fts AS SELECT i AS rbu_rowid, k, CASE
532 WHEN rbu_control IN (0,1) THEN rbu_control ELSE substr(rbu_control, 2) END
533 FROM data_ccc;
534 </pre></div>
535
536 <p>
537 The substr() function in the SQL view above returns the text of the
538 rbu_control argument with the first character (the one corresponding to
539 column "i", which is not required by the FTS table) removed.
540
541 <a name="sqldiff"></a>
542
543 </p><h3 id="automatically_generating_rbu_updates_with_sqldiff"><span>2.2.4. </span>Automatically Generating RBU Updates with sqldiff</h3>
544
545 <p>
546 As of SQLite <a href="releaselog/3_9_0.html">version 3.9.0</a> (2015-10-14),
547 the <a href="sqldiff.html">sqldiff</a> utility is able to generate
548 RBU databases representing the difference between two databases with
549 identical schemas. For example, the following command:
550
551 </p><div class="codeblock"><pre>sqldiff --rbu t1.db t2.db
552 </pre></div>
553
554 <p>
555 Outputs an SQL script to create an RBU database which, if used to update
556 database t1.db, patches it so that its contents are identical to that of
557 database t2.db.
558
559 </p><p>
560 By default, sqldiff attempts to process all non-virtual tables within
561 the two databases provided to it. If any table appears in one database
562 but not the other, or if any table has a slightly different schema in
563 one database it is an error. The "--table" option may be useful if this
564 causes a problem
565
566 </p><p>
567 Virtual tables are ignored by default by sqldiff. However, it is possible
568 to explicitly create an RBU data_% table for a virtual table that features
569 a rowid that functions like a primary key using a command such as:
570
571 </p><div class="codeblock"><pre>sqldiff --rbu --table &lt;<i>virtual-table-name</i>&gt; t1.db t2.db
572 </pre></div>
573
574 <p>
575 Unfortunately, even though virtual tables are ignored by default, any
576 <a href="fts3.html#*shadowtab">underlying database tables</a> that they create in order to
577 store data within the database are not, and <a href="sqldiff.html">sqldiff</a> will include add these
578 to any RBU database. For this reason, users attempting to use sqldiff to
579 create RBU updates to apply to target databases with one or more virtual
580 tables will likely have to run sqldiff using the --table option separately
581 for each table to update in the target database.
582
583 </p><h2 id="rbu_update_c_c_programming"><span>2.3. </span>RBU Update C/C++ Programming</h2>
584
585 <p>The RBU extension interface allows an application to apply an RBU update
586 stored in an RBU database to an existing target database.
587 The procedure is as follows:
588
589 </p><ol>
590 <li><p>
591 Open an RBU handle using the sqlite3rbu_open(T,A,S) function.
592
593 </p><p>The T argument is the name of the target database file.
594 The A argument is the name of the RBU database file.
595 The S argument is the name of a "state database" used to store
596 state information needed to resume the update after an interruption.
597 The S argument can be NULL in which case the state information
598 is stored in the RBU database in various tables whose names all
599 begin with "rbu_".
600
601 </p><p>The sqlite3rbu_open(T,A,S) function returns a pointer to
602 an "sqlite3rbu" object, which is then passed into the subsequent
603 interfaces.
604
605
606 </p></li><li><p>
607 Register any required virtual table modules with the database
608 handle returned by sqlite3rbu_db(X) (where argument X is the sqlite3rbu
609 pointer returned from sqlite3rbu_open()). Also, if required, register
610 the rbu_delta() SQL function using
611 <a href="c3ref/create_function.html">sqlite3_create_function_v2()</a>.
612
613 </p></li><li><p>
614 Invoke the sqlite3rbu_step(X) function one or more times on
615 the sqlite3rbu object pointer X. Each call to sqlite3rbu_step()
616 performs a single b-tree operation, so thousands of calls may be
617 required to apply a complete update. The sqlite3rbu_step()
618 interface will return SQLITE_DONE when the update has been
619 completely applied.
620
621 </p></li><li><p>
622 Call sqlite3rbu_close(X) to destroy the sqlite3rbu object pointer.
623 If sqlite3rbu_step(X) has been called enough times to completely
624 apply the update to the target database, then the RBU database
625 is marked as fully applied. Otherwise, the state of the RBU
626 update application is saved in the state database (or in the RBU
627 database if the name of the state database file in sqlite3rbu_open()
628 is NULL) for later resumption of the update.
629 </p></li></ol>
630
631 <p>If an update is only partially applied to the target database by the
632 time sqlite3rbu_close() is called, state information is saved
633 within the state database if it exists, or otherwise in the RBU database.
634 This allows subsequent processes to automatically
635 resume the RBU update from where it left off.
636 If state information is stored in the RBU database, it can be removed
637 by dropping all tables whose names begin with "rbu_".
638
639 </p><p>For more details, refer to the comments in
640 <a href="http://sqlite.org/src/doc/trunk/ext/rbu/sqlite3rbu.h">header file
641 sqlite3rbu.h</a>.
642
643 <a name="rbu_vacuum"></a>
644
645 </p><h1 id="rbu_vacuum"><span>3. </span>RBU Vacuum</h1>
646
647 <h2 id="rbu_vacuum_limitations"><span>3.1. </span>RBU Vacuum Limitations</h2>
648
649 <p>When compared with SQLite's built-in VACUUM command, RBU Vacuum has the
650 following limitations:
651
652 </p><ul>
653 <li><p>It may not be used on a database that contains <a href="expridx.html">indexes on expressions</a>.
654 </p></li><li><p>The database being vacuumed may not be in <a href="wal.html">WAL mode</a>.
655 </p></li></ul>
656
657 <h2 id="rbu_vacuum_c_c_programming"><span>3.2. </span>RBU Vacuum C/C++ Programming</h2>
658
659 <p> This section provides an overview of and example code demonstrating the
660 integration of RBU Vacuum into an application program. For full details,
661 refer to the comments in
662 <a href="http://sqlite.org/src/doc/trunk/ext/rbu/sqlite3rbu.h">header file
663 sqlite3rbu.h</a>.
664
665 </p><p> RBU Vacuum applications all implement some variation of the following
666 procedure:
667
668 </p><ol>
669 <li><p> An RBU handle is created by calling sqlite3rbu_vacuum(T, S).
670
671 </p><p> Argument T is the name of the database file to vacuum. Argument S is
672 the name of a database in which the RBU module will save its state if the
673 vacuum operation is suspended.
674
675 </p><p> If state database S does not exist when sqlite3rbu_vacuum() is
676 invoked, it is automatically created and populated with the single table
677 used to store the state of an RBU vacuum - "rbu_state". If an ongoing RBU
678 vacuum is suspended, this table is populated with state data. The next
679 time sqlite3rbu_vacuum() is called with the same S parameter, it detects
680 this data and attempts to resume the suspended vacuum operation. When
681 an RBU vacuum operation is completed or encounters an error, RBU
682 automatically deletes the contents of the rbu_state table. In this case,
683 the next call to sqlite3rbu_vacuum() starts an entirely new vacuum
684 operation from scratch.
685
686 </p><p> It is a good idea to establish a convention for determining the RBU
687 vacuum state database name based on the target database name. The
688 example code below uses "&lt;target&gt;-vacuum", where &lt;target&gt; is
689 the name of the database being vacuumed.
690
691 </p></li><li><p> Any custom collation sequences used by indexes within the database
692 being vacuumed are registered with both of the database handles returned
693 by the sqlite3rbu_db() function.
694
695 </p></li><li><p> Function sqlite3rbu_step() is called on the RBU handle until either
696 the RBU vacuum is finished, an error occurs or the application wishes to
697 suspend the RBU vacuum.
698
699 </p><p> Each call to sqlite3rbu_step() does a small amount of work towards
700 completing the vacuum operation. Depending on the size of the database, a
701 single vacuum may require thousands of calls to sqlite3rbu_step().
702 sqlite3rbu_step() returns SQLITE_DONE if the vacuum operation has
703 finished, SQLITE_OK if the vacuum operation has not finished but no error
704 has occurred, and an SQLite error code if an error is encountered. If
705 an error does occur, all subsequent calls to sqlite3rbu_step() immediately
706 return the same error code.
707
708 </p></li><li><p> Finally, sqlite3rbu_close() is called to close the RBU handle. If the
709 application stopped calling sqlite3rbu_step() before either the vacuum
710 finished or an error occurred, the state of the vacuum is saved in the
711 state database so that it may be resumed later on.
712
713 </p><p> Like sqlite3rbu_step(), if the vacuum operation has finished,
714 sqlite3rbu_close() returns SQLITE_DONE. If the vacuum has not finished
715 but no error has occurred, SQLITE_OK is returned. Or, if an error has
716 occurred, an SQLite error code is returned. If an error occurred as part
717 of a prior call to sqlite3rbu_step(), sqlite3rbu_close() returns the
718 same error code.
719 </p></li></ol>
720
721 <p>The following example code illustrates the techniques described above.
722
723 </p><div class="codeblock"><pre><i>/*</i>
724 <i>** Either start a new RBU vacuum or resume a suspended RBU vacuum on </i>
725 <i>** database zTarget. Return when either an error occurs, the RBU </i>
726 <i>** vacuum is finished or when the application signals an interrupt</i>
727 <i>** (code not shown).</i>
728 <i>**</i>
729 <i>** If the RBU vacuum is completed successfully, return SQLITE_DONE.</i>
730 <i>** If an error occurs, return SQLite error code. Or, if the application</i>
731 <i>** signals an interrupt, suspend the RBU vacuum operation so that it</i>
732 <i>** may be resumed by a subsequent call to this function and return</i>
733 <i>** SQLITE_OK.</i>
734 <i>**</i>
735 <i>** This function uses the database named "&lt;zTarget&gt;-vacuum" for</i>
736 <i>** the state database, where &lt;zTarget&gt; is the name of the database </i>
737 <i>** being vacuumed.</i>
738 <i>*/</i>
739 int do_rbu_vacuum(const char *zTarget){
740 int rc;
741 char *zState; <i>/* Name of state database */</i>
742 sqlite3rbu *pRbu; <i>/* RBU vacuum handle */</i>
743
744 zState = sqlite3_mprintf("%s-vacuum", zTarget);
745 if( zState==0 ) return SQLITE_NOMEM;
746 pRbu = sqlite3rbu_vacuum(zTarget, zState);
747 sqlite3_free(zState);
748
749 if( pRbu ){
750 sqlite3 *dbTarget = sqlite3rbu_db(pRbu, 0);
751 sqlite3 *dbState = sqlite3rbu_db(pRbu, 1);
752
753 <i>/* Any custom collation sequences used by the target database must</i>
754 <i>** be registered with both database handles here. */</i>
755
756 while( sqlite3rbu_step(pRbu)==SQLITE_OK ){
757 if( <i>&lt;application has signaled interrupt&gt;</i> ) break;
758 }
759 }
760 rc = sqlite3rbu_close(pRbu);
761 return rc;
762 }
763 </pre></div>
764 <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/rbu.in?m=0f520f8581">2022-01-08 05:02:57</a> UTC </small></i></p>
765