4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code used to implement the VACUUM command.
14 ** Most of the code in this file may be omitted by defining the
15 ** SQLITE_OMIT_VACUUM macro.
19 #include "sqliteInt.h"
22 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
24 ** Execute zSql on database db. Return an error code.
26 static int execSql(sqlite3 *db, const char *zSql){
31 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
32 return sqlite3_errcode(db);
34 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
35 return sqlite3_finalize(pStmt);
39 ** Execute zSql on database db. The statement returns exactly
40 ** one column. Execute this as SQL on the same database.
42 static int execExecSql(sqlite3 *db, const char *zSql){
46 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
47 if( rc!=SQLITE_OK ) return rc;
49 while( SQLITE_ROW==sqlite3_step(pStmt) ){
50 rc = execSql(db, (char*)sqlite3_column_text(pStmt, 0));
52 sqlite3_finalize(pStmt);
57 return sqlite3_finalize(pStmt);
61 ** The non-standard VACUUM command is used to clean up the database,
62 ** collapse free space, etc. It is modelled after the VACUUM command
65 ** In version 1.0.x of SQLite, the VACUUM command would call
66 ** gdbm_reorganize() on all the database tables. But beginning
67 ** with 2.0.0, SQLite no longer uses GDBM so this command has
70 void sqlite3Vacuum(Parse *pParse){
71 Vdbe *v = sqlite3GetVdbe(pParse);
73 sqlite3VdbeAddOp(v, OP_Vacuum, 0, 0);
79 ** This routine implements the OP_Vacuum opcode of the VDBE.
81 int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
82 int rc = SQLITE_OK; /* Return code from service routines */
83 Btree *pMain; /* The database being vacuumed */
84 Btree *pTemp; /* The temporary database we vacuum into */
85 char *zSql = 0; /* SQL statements */
86 int saved_flags; /* Saved value of the db->flags */
87 Db *pDb = 0; /* Database to detach at end of vacuum */
89 /* Save the current value of the write-schema flag before setting it. */
90 saved_flags = db->flags;
91 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
93 if( !db->autoCommit ){
94 sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction",
99 pMain = db->aDb[0].pBt;
101 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
102 ** can be set to 'off' for this file, as it is not recovered if a crash
103 ** occurs anyway. The integrity of the database is maintained by a
104 ** (possibly synchronous) transaction opened on the main database before
105 ** sqlite3BtreeCopyFile() is called.
107 ** An optimisation would be to use a non-journaled pager.
109 zSql = "ATTACH '' AS vacuum_db;";
110 rc = execSql(db, zSql);
111 if( rc!=SQLITE_OK ) goto end_of_vacuum;
112 pDb = &db->aDb[db->nDb-1];
113 assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
114 pTemp = db->aDb[db->nDb-1].pBt;
115 sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain),
116 sqlite3BtreeGetReserve(pMain));
117 if( db->mallocFailed ){
121 assert( sqlite3BtreeGetPageSize(pTemp)==sqlite3BtreeGetPageSize(pMain) );
122 rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
127 #ifndef SQLITE_OMIT_AUTOVACUUM
128 sqlite3BtreeSetAutoVacuum(pTemp, sqlite3BtreeGetAutoVacuum(pMain));
131 /* Begin a transaction */
132 rc = execSql(db, "BEGIN EXCLUSIVE;");
133 if( rc!=SQLITE_OK ) goto end_of_vacuum;
135 /* Query the schema of the main database. Create a mirror schema
136 ** in the temporary database.
139 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14,100000000) "
140 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
143 if( rc!=SQLITE_OK ) goto end_of_vacuum;
145 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14,100000000)"
146 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
147 if( rc!=SQLITE_OK ) goto end_of_vacuum;
149 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21,100000000) "
150 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
151 if( rc!=SQLITE_OK ) goto end_of_vacuum;
153 /* Loop through the tables in the main database. For each, do
154 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM xxx;" to copy
155 ** the contents to the temporary database.
158 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
159 "|| ' SELECT * FROM ' || quote(name) || ';'"
160 "FROM sqlite_master "
161 "WHERE type = 'table' AND name!='sqlite_sequence' "
165 if( rc!=SQLITE_OK ) goto end_of_vacuum;
167 /* Copy over the sequence table
170 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
171 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
173 if( rc!=SQLITE_OK ) goto end_of_vacuum;
175 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
176 "|| ' SELECT * FROM ' || quote(name) || ';' "
177 "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
179 if( rc!=SQLITE_OK ) goto end_of_vacuum;
182 /* Copy the triggers, views, and virtual tables from the main database
183 ** over to the temporary database. None of these objects has any
184 ** associated storage, so all we have to do is copy their entries
185 ** from the SQLITE_MASTER table.
188 "INSERT INTO vacuum_db.sqlite_master "
189 " SELECT type, name, tbl_name, rootpage, sql"
190 " FROM sqlite_master"
191 " WHERE type='view' OR type='trigger'"
192 " OR (type='table' AND rootpage=0)"
194 if( rc ) goto end_of_vacuum;
196 /* At this point, unless the main db was completely empty, there is now a
197 ** transaction open on the vacuum database, but not on the main database.
198 ** Open a btree level transaction on the main database. This allows a
199 ** call to sqlite3BtreeCopyFile(). The main database btree level
200 ** transaction is then committed, so the SQL level never knows it was
201 ** opened for writing. This way, the SQL transaction used to create the
202 ** temporary database never needs to be committed.
208 /* This array determines which meta meta values are preserved in the
209 ** vacuum. Even entries are the meta value number and odd entries
210 ** are an increment to apply to the meta value after the vacuum.
211 ** The increment is used to increase the schema cookie so that other
212 ** connections to the same database will know to reread the schema.
214 static const unsigned char aCopy[] = {
215 1, 1, /* Add one to the old schema cookie */
216 3, 0, /* Preserve the default page cache size */
217 5, 0, /* Preserve the default text encoding */
218 6, 0, /* Preserve the user version */
221 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
222 assert( 1==sqlite3BtreeIsInTrans(pMain) );
224 /* Copy Btree meta values */
225 for(i=0; i<sizeof(aCopy)/sizeof(aCopy[0]); i+=2){
226 rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
227 if( rc!=SQLITE_OK ) goto end_of_vacuum;
228 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
229 if( rc!=SQLITE_OK ) goto end_of_vacuum;
232 rc = sqlite3BtreeCopyFile(pMain, pTemp);
233 if( rc!=SQLITE_OK ) goto end_of_vacuum;
234 rc = sqlite3BtreeCommit(pTemp);
235 if( rc!=SQLITE_OK ) goto end_of_vacuum;
236 rc = sqlite3BtreeCommit(pMain);
240 /* Restore the original value of db->flags */
241 db->flags = saved_flags;
243 /* Currently there is an SQL level transaction open on the vacuum
244 ** database. No locks are held on any other files (since the main file
245 ** was committed at the btree level). So it safe to end the transaction
246 ** by manually setting the autoCommit flag to true and detaching the
247 ** vacuum database. The vacuum_db journal file is deleted when the pager
248 ** is closed by the DETACH.
253 sqlite3BtreeClose(pDb->pBt);
258 sqlite3ResetInternalSchema(db, 0);
262 #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */