drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 April 6 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used to implement the VACUUM command. |
| 13 | ** |
| 14 | ** Most of the code in this file may be omitted by defining the |
| 15 | ** SQLITE_OMIT_VACUUM macro. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
danielk1977 | f420804 | 2005-12-06 17:48:31 +0000 | [diff] [blame] | 18 | #include "vdbeInt.h" |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 19 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 20 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 21 | /* |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 22 | ** Finalize a prepared statement. If there was an error, store the |
| 23 | ** text of the error message in *pzErrMsg. Return the result code. |
| 24 | */ |
| 25 | static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ |
| 26 | int rc; |
| 27 | rc = sqlite3VdbeFinalize((Vdbe*)pStmt); |
| 28 | if( rc ){ |
| 29 | sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); |
| 30 | } |
| 31 | return rc; |
| 32 | } |
| 33 | |
| 34 | /* |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 35 | ** Execute zSql on database db. Return an error code. |
| 36 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 37 | static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 38 | sqlite3_stmt *pStmt; |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 39 | VVA_ONLY( int rc; ) |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 40 | if( !zSql ){ |
| 41 | return SQLITE_NOMEM; |
| 42 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 43 | if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 44 | sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 45 | return sqlite3_errcode(db); |
| 46 | } |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 47 | VVA_ONLY( rc = ) sqlite3_step(pStmt); |
dan | 1696124 | 2011-09-30 12:01:01 +0000 | [diff] [blame] | 48 | assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) ); |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 49 | return vacuumFinalize(db, pStmt, pzErrMsg); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | ** Execute zSql on database db. The statement returns exactly |
| 54 | ** one column. Execute this as SQL on the same database. |
| 55 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 56 | static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 57 | sqlite3_stmt *pStmt; |
| 58 | int rc; |
| 59 | |
| 60 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 61 | if( rc!=SQLITE_OK ) return rc; |
| 62 | |
| 63 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 64 | rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 65 | if( rc!=SQLITE_OK ){ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 66 | vacuumFinalize(db, pStmt, pzErrMsg); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 67 | return rc; |
| 68 | } |
| 69 | } |
| 70 | |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 71 | return vacuumFinalize(db, pStmt, pzErrMsg); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 74 | /* |
drh | 8b8d28dd | 2013-09-30 11:01:28 +0000 | [diff] [blame] | 75 | ** The VACUUM command is used to clean up the database, |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 76 | ** collapse free space, etc. It is modelled after the VACUUM command |
drh | 8b8d28dd | 2013-09-30 11:01:28 +0000 | [diff] [blame] | 77 | ** in PostgreSQL. The VACUUM command works as follows: |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 78 | ** |
drh | 8b8d28dd | 2013-09-30 11:01:28 +0000 | [diff] [blame] | 79 | ** (1) Create a new transient database file |
| 80 | ** (2) Copy all content from the database being vacuumed into |
| 81 | ** the new transient database file |
| 82 | ** (3) Copy content from the transient database back into the |
| 83 | ** original database. |
| 84 | ** |
| 85 | ** The transient database requires temporary disk space approximately |
| 86 | ** equal to the size of the original database. The copy operation of |
| 87 | ** step (3) requires additional temporary disk space approximately equal |
| 88 | ** to the size of the original database for the rollback journal. |
| 89 | ** Hence, temporary disk space that is approximately 2x the size of the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 90 | ** original database is required. Every page of the database is written |
drh | 8b8d28dd | 2013-09-30 11:01:28 +0000 | [diff] [blame] | 91 | ** approximately 3 times: Once for step (2) and twice for step (3). |
| 92 | ** Two writes per page are required in step (3) because the original |
| 93 | ** database content must be written into the rollback journal prior to |
| 94 | ** overwriting the database with the vacuumed content. |
| 95 | ** |
| 96 | ** Only 1x temporary space and only 1x writes would be required if |
| 97 | ** the copy of step (3) were replace by deleting the original database |
| 98 | ** and renaming the transient database as the original. But that will |
| 99 | ** not work if other processes are attached to the original database. |
| 100 | ** And a power loss in between deleting the original and renaming the |
| 101 | ** transient would cause the database file to appear to be deleted |
| 102 | ** following reboot. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 103 | */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 104 | void sqlite3Vacuum(Parse *pParse){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 105 | Vdbe *v = sqlite3GetVdbe(pParse); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 106 | if( v ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 107 | sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); |
dan | 07973a6 | 2012-10-15 18:02:57 +0000 | [diff] [blame] | 108 | sqlite3VdbeUsesBtree(v, 0); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 109 | } |
drh | 6f8c91c | 2003-12-07 00:24:35 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | ** This routine implements the OP_Vacuum opcode of the VDBE. |
| 115 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 116 | int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 117 | int rc = SQLITE_OK; /* Return code from service routines */ |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 118 | Btree *pMain; /* The database being vacuumed */ |
drh | 0058d84 | 2006-10-10 13:07:36 +0000 | [diff] [blame] | 119 | Btree *pTemp; /* The temporary database we vacuum into */ |
| 120 | char *zSql = 0; /* SQL statements */ |
| 121 | int saved_flags; /* Saved value of the db->flags */ |
drh | e63b2c2 | 2008-05-21 13:44:13 +0000 | [diff] [blame] | 122 | int saved_nChange; /* Saved value of db->nChange */ |
| 123 | int saved_nTotalChange; /* Saved value of db->nTotalChange */ |
drh | 702b919 | 2009-12-17 03:49:56 +0000 | [diff] [blame] | 124 | void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ |
drh | 0058d84 | 2006-10-10 13:07:36 +0000 | [diff] [blame] | 125 | Db *pDb = 0; /* Database to detach at end of vacuum */ |
danielk1977 | 43996e8 | 2009-05-30 10:46:10 +0000 | [diff] [blame] | 126 | int isMemDb; /* True if vacuuming a :memory: database */ |
drh | acd0781 | 2010-05-10 14:10:57 +0000 | [diff] [blame] | 127 | int nRes; /* Bytes of reserved space at the end of each page */ |
| 128 | int nDb; /* Number of attached databases */ |
danielk1977 | 3a3f38e | 2005-05-22 06:49:56 +0000 | [diff] [blame] | 129 | |
drh | 663d56d | 2009-01-22 23:04:45 +0000 | [diff] [blame] | 130 | if( !db->autoCommit ){ |
| 131 | sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); |
| 132 | return SQLITE_ERROR; |
| 133 | } |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 134 | if( db->nVdbeActive>1 ){ |
dan | 099d147 | 2010-09-24 09:32:45 +0000 | [diff] [blame] | 135 | sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); |
| 136 | return SQLITE_ERROR; |
| 137 | } |
drh | 663d56d | 2009-01-22 23:04:45 +0000 | [diff] [blame] | 138 | |
dan | 75cbd98 | 2009-09-21 16:06:03 +0000 | [diff] [blame] | 139 | /* Save the current value of the database flags so that it can be |
| 140 | ** restored before returning. Then set the writable-schema flag, and |
| 141 | ** disable CHECK and foreign key constraints. */ |
drh | 0cd2d4c | 2005-11-03 02:15:02 +0000 | [diff] [blame] | 142 | saved_flags = db->flags; |
drh | e63b2c2 | 2008-05-21 13:44:13 +0000 | [diff] [blame] | 143 | saved_nChange = db->nChange; |
| 144 | saved_nTotalChange = db->nTotalChange; |
drh | 702b919 | 2009-12-17 03:49:56 +0000 | [diff] [blame] | 145 | saved_xTrace = db->xTrace; |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 146 | db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; |
drh | 6a9c64b | 2010-01-12 23:54:14 +0000 | [diff] [blame] | 147 | db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); |
drh | 702b919 | 2009-12-17 03:49:56 +0000 | [diff] [blame] | 148 | db->xTrace = 0; |
drh | 45a304e | 2003-04-25 02:43:08 +0000 | [diff] [blame] | 149 | |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 150 | pMain = db->aDb[0].pBt; |
danielk1977 | 43996e8 | 2009-05-30 10:46:10 +0000 | [diff] [blame] | 151 | isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain)); |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 152 | |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 153 | /* Attach the temporary database as 'vacuum_db'. The synchronous pragma |
| 154 | ** can be set to 'off' for this file, as it is not recovered if a crash |
| 155 | ** occurs anyway. The integrity of the database is maintained by a |
| 156 | ** (possibly synchronous) transaction opened on the main database before |
| 157 | ** sqlite3BtreeCopyFile() is called. |
| 158 | ** |
| 159 | ** An optimisation would be to use a non-journaled pager. |
drh | 4697988 | 2008-04-30 16:38:23 +0000 | [diff] [blame] | 160 | ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but |
| 161 | ** that actually made the VACUUM run slower. Very little journalling |
| 162 | ** actually occurs when doing a vacuum since the vacuum_db is initially |
| 163 | ** empty. Only the journal header is written. Apparently it takes more |
| 164 | ** time to parse and run the PRAGMA to turn journalling off than it does |
| 165 | ** to write the journal header file. |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 166 | */ |
drh | acd0781 | 2010-05-10 14:10:57 +0000 | [diff] [blame] | 167 | nDb = db->nDb; |
drh | 2ec050c | 2010-03-02 23:34:54 +0000 | [diff] [blame] | 168 | if( sqlite3TempInMemory(db) ){ |
| 169 | zSql = "ATTACH ':memory:' AS vacuum_db;"; |
| 170 | }else{ |
| 171 | zSql = "ATTACH '' AS vacuum_db;"; |
| 172 | } |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 173 | rc = execSql(db, pzErrMsg, zSql); |
drh | acd0781 | 2010-05-10 14:10:57 +0000 | [diff] [blame] | 174 | if( db->nDb>nDb ){ |
| 175 | pDb = &db->aDb[db->nDb-1]; |
| 176 | assert( strcmp(pDb->zName,"vacuum_db")==0 ); |
| 177 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 178 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 179 | pTemp = db->aDb[db->nDb-1].pBt; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 180 | |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 181 | /* The call to execSql() to attach the temp database has left the file |
| 182 | ** locked (as there was more than one active statement when the transaction |
| 183 | ** to read the schema was concluded. Unlock it here so that this doesn't |
| 184 | ** cause problems for the call to BtreeSetPageSize() below. */ |
| 185 | sqlite3BtreeCommit(pTemp); |
| 186 | |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 187 | nRes = sqlite3BtreeGetReserve(pMain); |
drh | cdb7a0f | 2008-06-17 01:03:25 +0000 | [diff] [blame] | 188 | |
| 189 | /* A VACUUM cannot change the pagesize of an encrypted database. */ |
| 190 | #ifdef SQLITE_HAS_CODEC |
| 191 | if( db->nextPagesize ){ |
| 192 | extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); |
| 193 | int nKey; |
| 194 | char *zKey; |
| 195 | sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); |
| 196 | if( nKey ) db->nextPagesize = 0; |
| 197 | } |
| 198 | #endif |
| 199 | |
dan | f602963 | 2012-02-28 17:57:34 +0000 | [diff] [blame] | 200 | rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF"); |
| 201 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 202 | |
| 203 | /* Begin a transaction and take an exclusive lock on the main database |
| 204 | ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below, |
| 205 | ** to ensure that we do not try to change the page-size on a WAL database. |
| 206 | */ |
| 207 | rc = execSql(db, pzErrMsg, "BEGIN;"); |
| 208 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 209 | rc = sqlite3BtreeBeginTrans(pMain, 2); |
| 210 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 211 | |
drh | 811bdbd | 2010-05-05 03:39:53 +0000 | [diff] [blame] | 212 | /* Do not attempt to change the page size for a WAL database */ |
drh | 0b9b430 | 2010-06-11 17:01:24 +0000 | [diff] [blame] | 213 | if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) |
| 214 | ==PAGER_JOURNALMODE_WAL ){ |
drh | 811bdbd | 2010-05-05 03:39:53 +0000 | [diff] [blame] | 215 | db->nextPagesize = 0; |
| 216 | } |
| 217 | |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 218 | if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) |
| 219 | || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 220 | || NEVER(db->mallocFailed) |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 221 | ){ |
danielk1977 | 884c5b3 | 2007-03-06 16:03:55 +0000 | [diff] [blame] | 222 | rc = SQLITE_NOMEM; |
| 223 | goto end_of_vacuum; |
| 224 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 225 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 226 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | ddac25c | 2007-12-05 01:38:23 +0000 | [diff] [blame] | 227 | sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac : |
| 228 | sqlite3BtreeGetAutoVacuum(pMain)); |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 229 | #endif |
| 230 | |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 231 | /* Query the schema of the main database. Create a mirror schema |
| 232 | ** in the temporary database. |
| 233 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 234 | rc = execExecSql(db, pzErrMsg, |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 235 | "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " |
drh | 66b224c | 2006-09-11 11:13:26 +0000 | [diff] [blame] | 236 | " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 237 | " AND coalesce(rootpage,1)>0" |
drh | 66b224c | 2006-09-11 11:13:26 +0000 | [diff] [blame] | 238 | ); |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 239 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 240 | rc = execExecSql(db, pzErrMsg, |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 241 | "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 242 | " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' "); |
| 243 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 244 | rc = execExecSql(db, pzErrMsg, |
drh | a21a929 | 2007-10-20 20:58:57 +0000 | [diff] [blame] | 245 | "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 246 | " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'"); |
| 247 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 248 | |
| 249 | /* Loop through the tables in the main database. For each, do |
drh | 397308d | 2009-10-20 15:01:58 +0000 | [diff] [blame] | 250 | ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 251 | ** the contents to the temporary database. |
| 252 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 253 | rc = execExecSql(db, pzErrMsg, |
danielk1977 | bd26f92 | 2004-05-29 10:43:06 +0000 | [diff] [blame] | 254 | "SELECT 'INSERT INTO vacuum_db.' || quote(name) " |
drh | 397308d | 2009-10-20 15:01:58 +0000 | [diff] [blame] | 255 | "|| ' SELECT * FROM main.' || quote(name) || ';'" |
| 256 | "FROM main.sqlite_master " |
drh | 66b224c | 2006-09-11 11:13:26 +0000 | [diff] [blame] | 257 | "WHERE type = 'table' AND name!='sqlite_sequence' " |
drh | 5554827 | 2013-10-23 16:03:07 +0000 | [diff] [blame] | 258 | " AND coalesce(rootpage,1)>0" |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 259 | ); |
| 260 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 261 | |
drh | 62d5491 | 2005-02-03 01:08:20 +0000 | [diff] [blame] | 262 | /* Copy over the sequence table |
| 263 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 264 | rc = execExecSql(db, pzErrMsg, |
drh | 62d5491 | 2005-02-03 01:08:20 +0000 | [diff] [blame] | 265 | "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' " |
drh | 0f68fd1 | 2005-02-16 03:27:04 +0000 | [diff] [blame] | 266 | "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' " |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 267 | ); |
| 268 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 269 | rc = execExecSql(db, pzErrMsg, |
drh | 62d5491 | 2005-02-03 01:08:20 +0000 | [diff] [blame] | 270 | "SELECT 'INSERT INTO vacuum_db.' || quote(name) " |
drh | 397308d | 2009-10-20 15:01:58 +0000 | [diff] [blame] | 271 | "|| ' SELECT * FROM main.' || quote(name) || ';' " |
drh | 0f68fd1 | 2005-02-16 03:27:04 +0000 | [diff] [blame] | 272 | "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';" |
drh | 62d5491 | 2005-02-03 01:08:20 +0000 | [diff] [blame] | 273 | ); |
| 274 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 275 | |
| 276 | |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 277 | /* Copy the triggers, views, and virtual tables from the main database |
| 278 | ** over to the temporary database. None of these objects has any |
| 279 | ** associated storage, so all we have to do is copy their entries |
| 280 | ** from the SQLITE_MASTER table. |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 281 | */ |
drh | cda455b | 2010-02-24 19:23:56 +0000 | [diff] [blame] | 282 | rc = execSql(db, pzErrMsg, |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 283 | "INSERT INTO vacuum_db.sqlite_master " |
| 284 | " SELECT type, name, tbl_name, rootpage, sql" |
drh | 397308d | 2009-10-20 15:01:58 +0000 | [diff] [blame] | 285 | " FROM main.sqlite_master" |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 286 | " WHERE type='view' OR type='trigger'" |
| 287 | " OR (type='table' AND rootpage=0)" |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 288 | ); |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 289 | if( rc ) goto end_of_vacuum; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 290 | |
dan | c5f20a0 | 2011-10-07 16:57:59 +0000 | [diff] [blame] | 291 | /* At this point, there is a write transaction open on both the |
| 292 | ** vacuum database and the main database. Assuming no error occurs, |
| 293 | ** both transactions are closed by this block - the main database |
| 294 | ** transaction by sqlite3BtreeCopyFile() and the other by an explicit |
| 295 | ** call to sqlite3BtreeCommit(). |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 296 | */ |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 297 | { |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 298 | u32 meta; |
drh | 8cbd373 | 2005-02-12 00:19:30 +0000 | [diff] [blame] | 299 | int i; |
| 300 | |
| 301 | /* This array determines which meta meta values are preserved in the |
| 302 | ** vacuum. Even entries are the meta value number and odd entries |
| 303 | ** are an increment to apply to the meta value after the vacuum. |
| 304 | ** The increment is used to increase the schema cookie so that other |
| 305 | ** connections to the same database will know to reread the schema. |
| 306 | */ |
| 307 | static const unsigned char aCopy[] = { |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 308 | BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */ |
| 309 | BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */ |
| 310 | BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */ |
| 311 | BTREE_USER_VERSION, 0, /* Preserve the user version */ |
drh | b8a67ec | 2013-05-01 20:36:23 +0000 | [diff] [blame] | 312 | BTREE_APPLICATION_ID, 0, /* Preserve the application id */ |
drh | 8cbd373 | 2005-02-12 00:19:30 +0000 | [diff] [blame] | 313 | }; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 314 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 315 | assert( 1==sqlite3BtreeIsInTrans(pTemp) ); |
| 316 | assert( 1==sqlite3BtreeIsInTrans(pMain) ); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 317 | |
drh | 8cbd373 | 2005-02-12 00:19:30 +0000 | [diff] [blame] | 318 | /* Copy Btree meta values */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 319 | for(i=0; i<ArraySize(aCopy); i+=2){ |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 320 | /* GetMeta() and UpdateMeta() cannot fail in this context because |
| 321 | ** we already have page 1 loaded into cache and marked dirty. */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 322 | sqlite3BtreeGetMeta(pMain, aCopy[i], &meta); |
drh | 8cbd373 | 2005-02-12 00:19:30 +0000 | [diff] [blame] | 323 | rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]); |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 324 | if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum; |
drh | 8cbd373 | 2005-02-12 00:19:30 +0000 | [diff] [blame] | 325 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 326 | |
| 327 | rc = sqlite3BtreeCopyFile(pMain, pTemp); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 328 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 329 | rc = sqlite3BtreeCommit(pTemp); |
| 330 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
danielk1977 | 06249db | 2008-08-23 16:17:55 +0000 | [diff] [blame] | 331 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 332 | sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp)); |
| 333 | #endif |
drh | a5f6683 | 2003-04-18 02:31:04 +0000 | [diff] [blame] | 334 | } |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 335 | |
drh | 5dc348a | 2009-05-05 17:37:22 +0000 | [diff] [blame] | 336 | assert( rc==SQLITE_OK ); |
| 337 | rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 338 | |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 339 | end_of_vacuum: |
drh | 0cd2d4c | 2005-11-03 02:15:02 +0000 | [diff] [blame] | 340 | /* Restore the original value of db->flags */ |
| 341 | db->flags = saved_flags; |
drh | e63b2c2 | 2008-05-21 13:44:13 +0000 | [diff] [blame] | 342 | db->nChange = saved_nChange; |
| 343 | db->nTotalChange = saved_nTotalChange; |
drh | 702b919 | 2009-12-17 03:49:56 +0000 | [diff] [blame] | 344 | db->xTrace = saved_xTrace; |
drh | 16adb77 | 2010-05-05 04:11:44 +0000 | [diff] [blame] | 345 | sqlite3BtreeSetPageSize(pMain, -1, -1, 1); |
danielk1977 | 3a3f38e | 2005-05-22 06:49:56 +0000 | [diff] [blame] | 346 | |
danielk1977 | 46c43ed | 2004-06-30 06:30:25 +0000 | [diff] [blame] | 347 | /* Currently there is an SQL level transaction open on the vacuum |
| 348 | ** database. No locks are held on any other files (since the main file |
| 349 | ** was committed at the btree level). So it safe to end the transaction |
| 350 | ** by manually setting the autoCommit flag to true and detaching the |
| 351 | ** vacuum database. The vacuum_db journal file is deleted when the pager |
| 352 | ** is closed by the DETACH. |
| 353 | */ |
| 354 | db->autoCommit = 1; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 355 | |
danielk1977 | 0203bde | 2006-01-11 16:10:20 +0000 | [diff] [blame] | 356 | if( pDb ){ |
danielk1977 | 0203bde | 2006-01-11 16:10:20 +0000 | [diff] [blame] | 357 | sqlite3BtreeClose(pDb->pBt); |
danielk1977 | 0203bde | 2006-01-11 16:10:20 +0000 | [diff] [blame] | 358 | pDb->pBt = 0; |
| 359 | pDb->pSchema = 0; |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 360 | } |
danielk1977 | f420804 | 2005-12-06 17:48:31 +0000 | [diff] [blame] | 361 | |
drh | 03faf63 | 2011-04-04 23:08:14 +0000 | [diff] [blame] | 362 | /* This both clears the schemas and reduces the size of the db->aDb[] |
| 363 | ** array. */ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 364 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 365 | |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 366 | return rc; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 367 | } |
drh | c7792fa | 2011-04-02 16:28:52 +0000 | [diff] [blame] | 368 | |
drh | fdbcdee | 2007-03-27 14:44:50 +0000 | [diff] [blame] | 369 | #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */ |