blob: 841aa52009f11942b233df69657dee3307b950ed [file] [log] [blame]
drhc11d4f92003-04-06 21:08:24 +00001/*
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.
drhc11d4f92003-04-06 21:08:24 +000016*/
17#include "sqliteInt.h"
danielk1977f4208042005-12-06 17:48:31 +000018#include "vdbeInt.h"
drhc11d4f92003-04-06 21:08:24 +000019
drhfdbcdee2007-03-27 14:44:50 +000020#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drhcda455b2010-02-24 19:23:56 +000021
22/*
drh9ef5e772016-08-19 14:20:56 +000023** Execute zSql on database db.
24**
drh6a754dc2016-08-19 15:12:38 +000025** If zSql returns rows, then each row will have exactly one
26** column. (This will only happen if zSql begins with "SELECT".)
27** Take each row of result and call execSql() again recursively.
drh9ef5e772016-08-19 14:20:56 +000028**
29** The execSqlF() routine does the same thing, except it accepts
30** a format string as its third argument
danielk19773df6b252004-05-29 10:23:19 +000031*/
drhcda455b2010-02-24 19:23:56 +000032static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
danielk19773df6b252004-05-29 10:23:19 +000033 sqlite3_stmt *pStmt;
danielk19773df6b252004-05-29 10:23:19 +000034 int rc;
35
drh9ef5e772016-08-19 14:20:56 +000036 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
37 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
danielk19773df6b252004-05-29 10:23:19 +000038 if( rc!=SQLITE_OK ) return rc;
drh6a754dc2016-08-19 15:12:38 +000039 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
drh9ef5e772016-08-19 14:20:56 +000040 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
drh6a754dc2016-08-19 15:12:38 +000041 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
drh9ef5e772016-08-19 14:20:56 +000042 if( zSubSql ){
43 assert( zSubSql[0]!='S' );
44 rc = execSql(db, pzErrMsg, zSubSql);
45 if( rc!=SQLITE_OK ) break;
danielk19773df6b252004-05-29 10:23:19 +000046 }
47 }
drh6a754dc2016-08-19 15:12:38 +000048 assert( rc!=SQLITE_ROW );
49 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drh9ef5e772016-08-19 14:20:56 +000050 if( rc ){
51 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
52 }
53 (void)sqlite3_finalize(pStmt);
54 return rc;
55}
56static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
57 char *z;
58 va_list ap;
59 int rc;
60 va_start(ap, zSql);
61 z = sqlite3VMPrintf(db, zSql, ap);
62 va_end(ap);
63 if( z==0 ) return SQLITE_NOMEM;
64 rc = execSql(db, pzErrMsg, z);
65 sqlite3DbFree(db, z);
66 return rc;
danielk19773df6b252004-05-29 10:23:19 +000067}
68
drhc11d4f92003-04-06 21:08:24 +000069/*
drh8b8d28dd2013-09-30 11:01:28 +000070** The VACUUM command is used to clean up the database,
drhc11d4f92003-04-06 21:08:24 +000071** collapse free space, etc. It is modelled after the VACUUM command
drh8b8d28dd2013-09-30 11:01:28 +000072** in PostgreSQL. The VACUUM command works as follows:
drhc11d4f92003-04-06 21:08:24 +000073**
drh8b8d28dd2013-09-30 11:01:28 +000074** (1) Create a new transient database file
75** (2) Copy all content from the database being vacuumed into
76** the new transient database file
77** (3) Copy content from the transient database back into the
78** original database.
79**
80** The transient database requires temporary disk space approximately
81** equal to the size of the original database. The copy operation of
82** step (3) requires additional temporary disk space approximately equal
83** to the size of the original database for the rollback journal.
84** Hence, temporary disk space that is approximately 2x the size of the
peter.d.reid60ec9142014-09-06 16:39:46 +000085** original database is required. Every page of the database is written
drh8b8d28dd2013-09-30 11:01:28 +000086** approximately 3 times: Once for step (2) and twice for step (3).
87** Two writes per page are required in step (3) because the original
88** database content must be written into the rollback journal prior to
89** overwriting the database with the vacuumed content.
90**
91** Only 1x temporary space and only 1x writes would be required if
drh86a11b82014-11-07 13:24:29 +000092** the copy of step (3) were replaced by deleting the original database
drh8b8d28dd2013-09-30 11:01:28 +000093** and renaming the transient database as the original. But that will
94** not work if other processes are attached to the original database.
95** And a power loss in between deleting the original and renaming the
96** transient would cause the database file to appear to be deleted
97** following reboot.
drhc11d4f92003-04-06 21:08:24 +000098*/
drh9ef5e772016-08-19 14:20:56 +000099void sqlite3Vacuum(Parse *pParse, Token *pNm){
danielk19774adee202004-05-08 08:23:19 +0000100 Vdbe *v = sqlite3GetVdbe(pParse);
drheeea4122017-02-18 13:47:11 +0000101 int iDb = 0;
102 if( v==0 ) return;
103 if( pNm ){
104#ifndef SQLITE_BUG_COMPATIBLE_20160819
105 /* Default behavior: Report an error if the argument to VACUUM is
106 ** not recognized */
107 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
108 if( iDb<0 ) return;
109#else
110 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
111 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
112 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
113 ** The buggy behavior is required for binary compatibility with some
114 ** legacy applications. */
115 iDb = sqlite3FindDb(pParse->db, pNm);
116 if( iDb<0 ) iDb = 0;
117#endif
118 }
119 if( iDb!=1 ){
drh9ef5e772016-08-19 14:20:56 +0000120 sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
121 sqlite3VdbeUsesBtree(v, iDb);
danielk197796fb0dd2004-06-30 09:49:22 +0000122 }
drh6f8c91c2003-12-07 00:24:35 +0000123 return;
124}
125
126/*
127** This routine implements the OP_Vacuum opcode of the VDBE.
128*/
drh9ef5e772016-08-19 14:20:56 +0000129int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
danielk19773df6b252004-05-29 10:23:19 +0000130 int rc = SQLITE_OK; /* Return code from service routines */
drhf2a611c2004-09-05 00:33:43 +0000131 Btree *pMain; /* The database being vacuumed */
drh0058d842006-10-10 13:07:36 +0000132 Btree *pTemp; /* The temporary database we vacuum into */
drh0058d842006-10-10 13:07:36 +0000133 int saved_flags; /* Saved value of the db->flags */
drhe63b2c22008-05-21 13:44:13 +0000134 int saved_nChange; /* Saved value of db->nChange */
135 int saved_nTotalChange; /* Saved value of db->nTotalChange */
drh3d2a5292016-07-13 22:55:01 +0000136 u8 saved_mTrace; /* Saved trace settings */
drh0058d842006-10-10 13:07:36 +0000137 Db *pDb = 0; /* Database to detach at end of vacuum */
danielk197743996e82009-05-30 10:46:10 +0000138 int isMemDb; /* True if vacuuming a :memory: database */
drhacd07812010-05-10 14:10:57 +0000139 int nRes; /* Bytes of reserved space at the end of each page */
140 int nDb; /* Number of attached databases */
drh9ef5e772016-08-19 14:20:56 +0000141 const char *zDbMain; /* Schema name of database to vacuum */
danielk19773a3f38e2005-05-22 06:49:56 +0000142
drh663d56d2009-01-22 23:04:45 +0000143 if( !db->autoCommit ){
144 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
145 return SQLITE_ERROR;
146 }
drh4f7d3a52013-06-27 23:54:02 +0000147 if( db->nVdbeActive>1 ){
dan099d1472010-09-24 09:32:45 +0000148 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
149 return SQLITE_ERROR;
150 }
drh663d56d2009-01-22 23:04:45 +0000151
dan75cbd982009-09-21 16:06:03 +0000152 /* Save the current value of the database flags so that it can be
153 ** restored before returning. Then set the writable-schema flag, and
154 ** disable CHECK and foreign key constraints. */
drh0cd2d4c2005-11-03 02:15:02 +0000155 saved_flags = db->flags;
drhe63b2c22008-05-21 13:44:13 +0000156 saved_nChange = db->nChange;
157 saved_nTotalChange = db->nTotalChange;
drh3d2a5292016-07-13 22:55:01 +0000158 saved_mTrace = db->mTrace;
drh9ef5e772016-08-19 14:20:56 +0000159 db->flags |= (SQLITE_WriteSchema | SQLITE_IgnoreChecks
160 | SQLITE_PreferBuiltin | SQLITE_Vacuum);
drh6a754dc2016-08-19 15:12:38 +0000161 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
drh1637a512016-07-13 23:18:27 +0000162 db->mTrace = 0;
drh45a304e2003-04-25 02:43:08 +0000163
drh9ef5e772016-08-19 14:20:56 +0000164 zDbMain = db->aDb[iDb].zDbSName;
165 pMain = db->aDb[iDb].pBt;
danielk197743996e82009-05-30 10:46:10 +0000166 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
drh13bff812003-04-15 01:19:47 +0000167
danielk197746c43ed2004-06-30 06:30:25 +0000168 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
169 ** can be set to 'off' for this file, as it is not recovered if a crash
170 ** occurs anyway. The integrity of the database is maintained by a
171 ** (possibly synchronous) transaction opened on the main database before
172 ** sqlite3BtreeCopyFile() is called.
173 **
174 ** An optimisation would be to use a non-journaled pager.
drh46979882008-04-30 16:38:23 +0000175 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
176 ** that actually made the VACUUM run slower. Very little journalling
177 ** actually occurs when doing a vacuum since the vacuum_db is initially
178 ** empty. Only the journal header is written. Apparently it takes more
179 ** time to parse and run the PRAGMA to turn journalling off than it does
180 ** to write the journal header file.
danielk197746c43ed2004-06-30 06:30:25 +0000181 */
drhacd07812010-05-10 14:10:57 +0000182 nDb = db->nDb;
drh9ef5e772016-08-19 14:20:56 +0000183 rc = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
danielk19773df6b252004-05-29 10:23:19 +0000184 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000185 assert( (db->nDb-1)==nDb );
186 pDb = &db->aDb[nDb];
187 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
188 pTemp = pDb->pBt;
danielk1977f653d782008-03-20 11:04:21 +0000189
danfa401de2009-10-16 14:55:03 +0000190 /* The call to execSql() to attach the temp database has left the file
191 ** locked (as there was more than one active statement when the transaction
192 ** to read the schema was concluded. Unlock it here so that this doesn't
193 ** cause problems for the call to BtreeSetPageSize() below. */
194 sqlite3BtreeCommit(pTemp);
195
drhad0961b2015-02-21 00:19:25 +0000196 nRes = sqlite3BtreeGetOptimalReserve(pMain);
drhcdb7a0f2008-06-17 01:03:25 +0000197
198 /* A VACUUM cannot change the pagesize of an encrypted database. */
199#ifdef SQLITE_HAS_CODEC
200 if( db->nextPagesize ){
201 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
202 int nKey;
203 char *zKey;
drh76cf8582017-06-16 13:43:37 +0000204 sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
drhcdb7a0f2008-06-17 01:03:25 +0000205 if( nKey ) db->nextPagesize = 0;
206 }
207#endif
208
drh9ef5e772016-08-19 14:20:56 +0000209 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
drh9d608752016-07-26 04:49:43 +0000210 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
dan75ba6762016-11-02 14:50:19 +0000211 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
danf6029632012-02-28 17:57:34 +0000212
213 /* Begin a transaction and take an exclusive lock on the main database
214 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
215 ** to ensure that we do not try to change the page-size on a WAL database.
216 */
drh9ef5e772016-08-19 14:20:56 +0000217 rc = execSql(db, pzErrMsg, "BEGIN");
danf6029632012-02-28 17:57:34 +0000218 if( rc!=SQLITE_OK ) goto end_of_vacuum;
219 rc = sqlite3BtreeBeginTrans(pMain, 2);
220 if( rc!=SQLITE_OK ) goto end_of_vacuum;
221
drh811bdbd2010-05-05 03:39:53 +0000222 /* Do not attempt to change the page size for a WAL database */
drh0b9b4302010-06-11 17:01:24 +0000223 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
224 ==PAGER_JOURNALMODE_WAL ){
drh811bdbd2010-05-05 03:39:53 +0000225 db->nextPagesize = 0;
226 }
227
drhce4869f2009-04-02 20:16:58 +0000228 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
229 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
drh5dc348a2009-05-05 17:37:22 +0000230 || NEVER(db->mallocFailed)
danielk1977f653d782008-03-20 11:04:21 +0000231 ){
mistachkinfad30392016-02-13 23:43:46 +0000232 rc = SQLITE_NOMEM_BKPT;
danielk1977884c5b32007-03-06 16:03:55 +0000233 goto end_of_vacuum;
234 }
danielk19773df6b252004-05-29 10:23:19 +0000235
danielk197742741be2005-01-08 12:42:39 +0000236#ifndef SQLITE_OMIT_AUTOVACUUM
drhddac25c2007-12-05 01:38:23 +0000237 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
238 sqlite3BtreeGetAutoVacuum(pMain));
danielk197742741be2005-01-08 12:42:39 +0000239#endif
240
danielk19773df6b252004-05-29 10:23:19 +0000241 /* Query the schema of the main database. Create a mirror schema
242 ** in the temporary database.
243 */
drh9ef5e772016-08-19 14:20:56 +0000244 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
245 rc = execSqlF(db, pzErrMsg,
246 "SELECT sql FROM \"%w\".sqlite_master"
247 " WHERE type='table'AND name<>'sqlite_sequence'"
248 " AND coalesce(rootpage,1)>0",
249 zDbMain
drh66b224c2006-09-11 11:13:26 +0000250 );
danielk197727c77432004-11-22 13:35:41 +0000251 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000252 rc = execSqlF(db, pzErrMsg,
253 "SELECT sql FROM \"%w\".sqlite_master"
254 " WHERE type='index' AND length(sql)>10",
255 zDbMain
256 );
danielk197727c77432004-11-22 13:35:41 +0000257 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000258 db->init.iDb = 0;
danielk19773df6b252004-05-29 10:23:19 +0000259
260 /* Loop through the tables in the main database. For each, do
drh397308d2009-10-20 15:01:58 +0000261 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
danielk19773df6b252004-05-29 10:23:19 +0000262 ** the contents to the temporary database.
263 */
drh9ef5e772016-08-19 14:20:56 +0000264 rc = execSqlF(db, pzErrMsg,
265 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
266 "||' SELECT*FROM\"%w\".'||quote(name)"
267 "FROM vacuum_db.sqlite_master "
268 "WHERE type='table'AND coalesce(rootpage,1)>0",
269 zDbMain
danielk19773df6b252004-05-29 10:23:19 +0000270 );
dane34162b2015-04-01 18:20:25 +0000271 assert( (db->flags & SQLITE_Vacuum)!=0 );
272 db->flags &= ~SQLITE_Vacuum;
danielk19773df6b252004-05-29 10:23:19 +0000273 if( rc!=SQLITE_OK ) goto end_of_vacuum;
274
drhfdd48a72006-09-11 23:45:48 +0000275 /* Copy the triggers, views, and virtual tables from the main database
276 ** over to the temporary database. None of these objects has any
277 ** associated storage, so all we have to do is copy their entries
278 ** from the SQLITE_MASTER table.
danielk19773df6b252004-05-29 10:23:19 +0000279 */
drh9ef5e772016-08-19 14:20:56 +0000280 rc = execSqlF(db, pzErrMsg,
281 "INSERT INTO vacuum_db.sqlite_master"
282 " SELECT*FROM \"%w\".sqlite_master"
283 " WHERE type IN('view','trigger')"
284 " OR(type='table'AND rootpage=0)",
285 zDbMain
danielk19773df6b252004-05-29 10:23:19 +0000286 );
drhfdd48a72006-09-11 23:45:48 +0000287 if( rc ) goto end_of_vacuum;
danielk19773df6b252004-05-29 10:23:19 +0000288
danc5f20a02011-10-07 16:57:59 +0000289 /* At this point, there is a write transaction open on both the
290 ** vacuum database and the main database. Assuming no error occurs,
291 ** both transactions are closed by this block - the main database
292 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
293 ** call to sqlite3BtreeCommit().
danielk19773df6b252004-05-29 10:23:19 +0000294 */
drh5dc348a2009-05-05 17:37:22 +0000295 {
danielk19773df6b252004-05-29 10:23:19 +0000296 u32 meta;
drh8cbd3732005-02-12 00:19:30 +0000297 int i;
298
299 /* This array determines which meta meta values are preserved in the
300 ** vacuum. Even entries are the meta value number and odd entries
301 ** are an increment to apply to the meta value after the vacuum.
302 ** The increment is used to increase the schema cookie so that other
303 ** connections to the same database will know to reread the schema.
304 */
305 static const unsigned char aCopy[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000306 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
307 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
308 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
309 BTREE_USER_VERSION, 0, /* Preserve the user version */
drhb8a67ec2013-05-01 20:36:23 +0000310 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
drh8cbd3732005-02-12 00:19:30 +0000311 };
danielk19773df6b252004-05-29 10:23:19 +0000312
drhc9ac5ca2005-11-04 22:03:30 +0000313 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
314 assert( 1==sqlite3BtreeIsInTrans(pMain) );
danielk19771d850a72004-05-31 08:26:49 +0000315
drh8cbd3732005-02-12 00:19:30 +0000316 /* Copy Btree meta values */
danielk197700e13612008-11-17 19:18:54 +0000317 for(i=0; i<ArraySize(aCopy); i+=2){
drh5dc348a2009-05-05 17:37:22 +0000318 /* GetMeta() and UpdateMeta() cannot fail in this context because
319 ** we already have page 1 loaded into cache and marked dirty. */
danielk1977602b4662009-07-02 07:47:33 +0000320 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
drh8cbd3732005-02-12 00:19:30 +0000321 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
drh5dc348a2009-05-05 17:37:22 +0000322 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
drh8cbd3732005-02-12 00:19:30 +0000323 }
danielk19773df6b252004-05-29 10:23:19 +0000324
325 rc = sqlite3BtreeCopyFile(pMain, pTemp);
danielk1977369f27e2004-06-15 11:40:04 +0000326 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drhc9ac5ca2005-11-04 22:03:30 +0000327 rc = sqlite3BtreeCommit(pTemp);
328 if( rc!=SQLITE_OK ) goto end_of_vacuum;
danielk197706249db2008-08-23 16:17:55 +0000329#ifndef SQLITE_OMIT_AUTOVACUUM
330 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
331#endif
drha5f66832003-04-18 02:31:04 +0000332 }
drh13bff812003-04-15 01:19:47 +0000333
drh5dc348a2009-05-05 17:37:22 +0000334 assert( rc==SQLITE_OK );
335 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
danielk1977f653d782008-03-20 11:04:21 +0000336
drh13bff812003-04-15 01:19:47 +0000337end_of_vacuum:
drh0cd2d4c2005-11-03 02:15:02 +0000338 /* Restore the original value of db->flags */
drh9ef5e772016-08-19 14:20:56 +0000339 db->init.iDb = 0;
drh0cd2d4c2005-11-03 02:15:02 +0000340 db->flags = saved_flags;
drhe63b2c22008-05-21 13:44:13 +0000341 db->nChange = saved_nChange;
342 db->nTotalChange = saved_nTotalChange;
drh3d2a5292016-07-13 22:55:01 +0000343 db->mTrace = saved_mTrace;
drh16adb772010-05-05 04:11:44 +0000344 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
danielk19773a3f38e2005-05-22 06:49:56 +0000345
danielk197746c43ed2004-06-30 06:30:25 +0000346 /* Currently there is an SQL level transaction open on the vacuum
347 ** database. No locks are held on any other files (since the main file
348 ** was committed at the btree level). So it safe to end the transaction
349 ** by manually setting the autoCommit flag to true and detaching the
350 ** vacuum database. The vacuum_db journal file is deleted when the pager
351 ** is closed by the DETACH.
352 */
353 db->autoCommit = 1;
danielk1977261919c2005-12-06 12:52:59 +0000354
danielk19770203bde2006-01-11 16:10:20 +0000355 if( pDb ){
danielk19770203bde2006-01-11 16:10:20 +0000356 sqlite3BtreeClose(pDb->pBt);
danielk19770203bde2006-01-11 16:10:20 +0000357 pDb->pBt = 0;
358 pDb->pSchema = 0;
danielk197796fb0dd2004-06-30 09:49:22 +0000359 }
danielk1977f4208042005-12-06 17:48:31 +0000360
drh03faf632011-04-04 23:08:14 +0000361 /* This both clears the schemas and reduces the size of the db->aDb[]
362 ** array. */
drh81028a42012-05-15 18:28:27 +0000363 sqlite3ResetAllSchemasOfConnection(db);
drh89dec812005-04-28 19:03:37 +0000364
danielk19773df6b252004-05-29 10:23:19 +0000365 return rc;
drh3f7d4e42004-07-24 14:35:58 +0000366}
drhc7792fa2011-04-02 16:28:52 +0000367
drhfdbcdee2007-03-27 14:44:50 +0000368#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */