blob: 9908cf14295f630913c73942341c83d4c9d7060e [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 );
drh34b27ed2018-05-03 21:51:30 +000042 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
43 ** or INSERT. Historically there have been attacks that first
drh1e32bed2020-06-19 13:33:53 +000044 ** corrupt the sqlite_schema.sql field with other kinds of statements
drh34b27ed2018-05-03 21:51:30 +000045 ** then run VACUUM to get those statements to execute at inappropriate
46 ** times. */
47 if( zSubSql
48 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
49 ){
drh9ef5e772016-08-19 14:20:56 +000050 rc = execSql(db, pzErrMsg, zSubSql);
51 if( rc!=SQLITE_OK ) break;
danielk19773df6b252004-05-29 10:23:19 +000052 }
53 }
drh6a754dc2016-08-19 15:12:38 +000054 assert( rc!=SQLITE_ROW );
55 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drh9ef5e772016-08-19 14:20:56 +000056 if( rc ){
57 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
58 }
59 (void)sqlite3_finalize(pStmt);
60 return rc;
61}
62static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
63 char *z;
64 va_list ap;
65 int rc;
66 va_start(ap, zSql);
67 z = sqlite3VMPrintf(db, zSql, ap);
68 va_end(ap);
69 if( z==0 ) return SQLITE_NOMEM;
70 rc = execSql(db, pzErrMsg, z);
71 sqlite3DbFree(db, z);
72 return rc;
danielk19773df6b252004-05-29 10:23:19 +000073}
74
drhc11d4f92003-04-06 21:08:24 +000075/*
drh8b8d28dd2013-09-30 11:01:28 +000076** The VACUUM command is used to clean up the database,
drhc11d4f92003-04-06 21:08:24 +000077** collapse free space, etc. It is modelled after the VACUUM command
drh8b8d28dd2013-09-30 11:01:28 +000078** in PostgreSQL. The VACUUM command works as follows:
drhc11d4f92003-04-06 21:08:24 +000079**
drh8b8d28dd2013-09-30 11:01:28 +000080** (1) Create a new transient database file
81** (2) Copy all content from the database being vacuumed into
82** the new transient database file
83** (3) Copy content from the transient database back into the
84** original database.
85**
86** The transient database requires temporary disk space approximately
87** equal to the size of the original database. The copy operation of
88** step (3) requires additional temporary disk space approximately equal
89** to the size of the original database for the rollback journal.
90** Hence, temporary disk space that is approximately 2x the size of the
peter.d.reid60ec9142014-09-06 16:39:46 +000091** original database is required. Every page of the database is written
drh8b8d28dd2013-09-30 11:01:28 +000092** approximately 3 times: Once for step (2) and twice for step (3).
93** Two writes per page are required in step (3) because the original
94** database content must be written into the rollback journal prior to
95** overwriting the database with the vacuumed content.
96**
97** Only 1x temporary space and only 1x writes would be required if
drh86a11b82014-11-07 13:24:29 +000098** the copy of step (3) were replaced by deleting the original database
drh8b8d28dd2013-09-30 11:01:28 +000099** and renaming the transient database as the original. But that will
100** not work if other processes are attached to the original database.
101** And a power loss in between deleting the original and renaming the
102** transient would cause the database file to appear to be deleted
103** following reboot.
drhc11d4f92003-04-06 21:08:24 +0000104*/
drh2f6239e2018-12-08 00:43:08 +0000105void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
danielk19774adee202004-05-08 08:23:19 +0000106 Vdbe *v = sqlite3GetVdbe(pParse);
drheeea4122017-02-18 13:47:11 +0000107 int iDb = 0;
drh2f6239e2018-12-08 00:43:08 +0000108 if( v==0 ) goto build_vacuum_end;
drh29e78002019-04-23 22:00:39 +0000109 if( pParse->nErr ) goto build_vacuum_end;
drheeea4122017-02-18 13:47:11 +0000110 if( pNm ){
111#ifndef SQLITE_BUG_COMPATIBLE_20160819
112 /* Default behavior: Report an error if the argument to VACUUM is
113 ** not recognized */
114 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
drh2f6239e2018-12-08 00:43:08 +0000115 if( iDb<0 ) goto build_vacuum_end;
drheeea4122017-02-18 13:47:11 +0000116#else
117 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
118 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
119 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
120 ** The buggy behavior is required for binary compatibility with some
121 ** legacy applications. */
122 iDb = sqlite3FindDb(pParse->db, pNm);
123 if( iDb<0 ) iDb = 0;
124#endif
125 }
126 if( iDb!=1 ){
drh2f6239e2018-12-08 00:43:08 +0000127 int iIntoReg = 0;
drhee751fa2019-01-02 14:34:46 +0000128 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
drh2f6239e2018-12-08 00:43:08 +0000129 iIntoReg = ++pParse->nMem;
130 sqlite3ExprCode(pParse, pInto, iIntoReg);
drhb0b7db92018-12-07 17:28:28 +0000131 }
drh2f6239e2018-12-08 00:43:08 +0000132 sqlite3VdbeAddOp2(v, OP_Vacuum, iDb, iIntoReg);
133 sqlite3VdbeUsesBtree(v, iDb);
danielk197796fb0dd2004-06-30 09:49:22 +0000134 }
drh2f6239e2018-12-08 00:43:08 +0000135build_vacuum_end:
136 sqlite3ExprDelete(pParse->db, pInto);
drh6f8c91c2003-12-07 00:24:35 +0000137 return;
138}
139
140/*
141** This routine implements the OP_Vacuum opcode of the VDBE.
142*/
drh67752572019-04-04 15:25:52 +0000143SQLITE_NOINLINE int sqlite3RunVacuum(
drh2f6239e2018-12-08 00:43:08 +0000144 char **pzErrMsg, /* Write error message here */
145 sqlite3 *db, /* Database connection */
146 int iDb, /* Which attached DB to vacuum */
drhd0f820a2019-03-19 20:42:42 +0000147 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
drh2f6239e2018-12-08 00:43:08 +0000148){
danielk19773df6b252004-05-29 10:23:19 +0000149 int rc = SQLITE_OK; /* Return code from service routines */
drhf2a611c2004-09-05 00:33:43 +0000150 Btree *pMain; /* The database being vacuumed */
drh0058d842006-10-10 13:07:36 +0000151 Btree *pTemp; /* The temporary database we vacuum into */
drh70d5dfb2018-12-06 16:50:55 +0000152 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
153 u64 saved_flags; /* Saved value of db->flags */
dan2c718872021-06-22 18:32:05 +0000154 i64 saved_nChange; /* Saved value of db->nChange */
155 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
drhd0f820a2019-03-19 20:42:42 +0000156 u32 saved_openFlags; /* Saved value of db->openFlags */
drh3d2a5292016-07-13 22:55:01 +0000157 u8 saved_mTrace; /* Saved trace settings */
drh0058d842006-10-10 13:07:36 +0000158 Db *pDb = 0; /* Database to detach at end of vacuum */
danielk197743996e82009-05-30 10:46:10 +0000159 int isMemDb; /* True if vacuuming a :memory: database */
drhacd07812010-05-10 14:10:57 +0000160 int nRes; /* Bytes of reserved space at the end of each page */
161 int nDb; /* Number of attached databases */
drh9ef5e772016-08-19 14:20:56 +0000162 const char *zDbMain; /* Schema name of database to vacuum */
drh2f6239e2018-12-08 00:43:08 +0000163 const char *zOut; /* Name of output file */
dan80b30f92022-10-24 15:51:24 +0000164 u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */
danielk19773a3f38e2005-05-22 06:49:56 +0000165
drh663d56d2009-01-22 23:04:45 +0000166 if( !db->autoCommit ){
167 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
drheacc8812019-04-04 18:20:25 +0000168 return SQLITE_ERROR; /* IMP: R-12218-18073 */
drh663d56d2009-01-22 23:04:45 +0000169 }
drh4f7d3a52013-06-27 23:54:02 +0000170 if( db->nVdbeActive>1 ){
dan099d1472010-09-24 09:32:45 +0000171 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
drheacc8812019-04-04 18:20:25 +0000172 return SQLITE_ERROR; /* IMP: R-15610-35227 */
dan099d1472010-09-24 09:32:45 +0000173 }
drhd0f820a2019-03-19 20:42:42 +0000174 saved_openFlags = db->openFlags;
drh2f6239e2018-12-08 00:43:08 +0000175 if( pOut ){
176 if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
177 sqlite3SetString(pzErrMsg, db, "non-text filename");
178 return SQLITE_ERROR;
179 }
180 zOut = (const char*)sqlite3_value_text(pOut);
drhd0f820a2019-03-19 20:42:42 +0000181 db->openFlags &= ~SQLITE_OPEN_READONLY;
182 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
drh2f6239e2018-12-08 00:43:08 +0000183 }else{
184 zOut = "";
185 }
drh663d56d2009-01-22 23:04:45 +0000186
dan75cbd982009-09-21 16:06:03 +0000187 /* Save the current value of the database flags so that it can be
188 ** restored before returning. Then set the writable-schema flag, and
189 ** disable CHECK and foreign key constraints. */
drh0cd2d4c2005-11-03 02:15:02 +0000190 saved_flags = db->flags;
drh67752572019-04-04 15:25:52 +0000191 saved_mDbFlags = db->mDbFlags;
drhe63b2c22008-05-21 13:44:13 +0000192 saved_nChange = db->nChange;
193 saved_nTotalChange = db->nTotalChange;
drh3d2a5292016-07-13 22:55:01 +0000194 saved_mTrace = db->mTrace;
drh8257aa82017-07-26 19:59:13 +0000195 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
drh67752572019-04-04 15:25:52 +0000196 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
drhd5b44d62018-12-06 17:06:02 +0000197 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
drh0f0d3dd2018-11-06 19:26:04 +0000198 | SQLITE_Defensive | SQLITE_CountRows);
drh1637a512016-07-13 23:18:27 +0000199 db->mTrace = 0;
drh45a304e2003-04-25 02:43:08 +0000200
drh9ef5e772016-08-19 14:20:56 +0000201 zDbMain = db->aDb[iDb].zDbSName;
202 pMain = db->aDb[iDb].pBt;
danielk197743996e82009-05-30 10:46:10 +0000203 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
drh13bff812003-04-15 01:19:47 +0000204
danielk197746c43ed2004-06-30 06:30:25 +0000205 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
206 ** can be set to 'off' for this file, as it is not recovered if a crash
207 ** occurs anyway. The integrity of the database is maintained by a
208 ** (possibly synchronous) transaction opened on the main database before
209 ** sqlite3BtreeCopyFile() is called.
210 **
211 ** An optimisation would be to use a non-journaled pager.
drh46979882008-04-30 16:38:23 +0000212 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
213 ** that actually made the VACUUM run slower. Very little journalling
214 ** actually occurs when doing a vacuum since the vacuum_db is initially
215 ** empty. Only the journal header is written. Apparently it takes more
216 ** time to parse and run the PRAGMA to turn journalling off than it does
217 ** to write the journal header file.
danielk197746c43ed2004-06-30 06:30:25 +0000218 */
drhacd07812010-05-10 14:10:57 +0000219 nDb = db->nDb;
drh2f6239e2018-12-08 00:43:08 +0000220 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
drhd0f820a2019-03-19 20:42:42 +0000221 db->openFlags = saved_openFlags;
danielk19773df6b252004-05-29 10:23:19 +0000222 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000223 assert( (db->nDb-1)==nDb );
224 pDb = &db->aDb[nDb];
225 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
226 pTemp = pDb->pBt;
drh2f6239e2018-12-08 00:43:08 +0000227 if( pOut ){
drh7464f572018-12-07 23:48:41 +0000228 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
229 i64 sz = 0;
230 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
231 rc = SQLITE_ERROR;
232 sqlite3SetString(pzErrMsg, db, "output file already exists");
233 goto end_of_vacuum;
234 }
drh67752572019-04-04 15:25:52 +0000235 db->mDbFlags |= DBFLAG_VacuumInto;
dan80b30f92022-10-24 15:51:24 +0000236
237 /* For a VACUUM INTO, the pager-flags are set to the same values as
238 ** they are for the database being vacuumed, except that PAGER_CACHESPILL
239 ** is always set. */
240 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
drh7464f572018-12-07 23:48:41 +0000241 }
drh45248de2020-04-20 15:18:43 +0000242 nRes = sqlite3BtreeGetRequestedReserve(pMain);
drhcdb7a0f2008-06-17 01:03:25 +0000243
drh9ef5e772016-08-19 14:20:56 +0000244 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
drh9d608752016-07-26 04:49:43 +0000245 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
dan80b30f92022-10-24 15:51:24 +0000246 sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL);
danf6029632012-02-28 17:57:34 +0000247
248 /* Begin a transaction and take an exclusive lock on the main database
249 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
250 ** to ensure that we do not try to change the page-size on a WAL database.
251 */
drh9ef5e772016-08-19 14:20:56 +0000252 rc = execSql(db, pzErrMsg, "BEGIN");
danf6029632012-02-28 17:57:34 +0000253 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh2f6239e2018-12-08 00:43:08 +0000254 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
danf6029632012-02-28 17:57:34 +0000255 if( rc!=SQLITE_OK ) goto end_of_vacuum;
256
drh811bdbd2010-05-05 03:39:53 +0000257 /* Do not attempt to change the page size for a WAL database */
drh0b9b4302010-06-11 17:01:24 +0000258 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
drhcfb52492021-10-29 13:10:02 +0000259 ==PAGER_JOURNALMODE_WAL
260 && pOut==0
261 ){
drh811bdbd2010-05-05 03:39:53 +0000262 db->nextPagesize = 0;
263 }
264
drhce4869f2009-04-02 20:16:58 +0000265 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
266 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
drh5dc348a2009-05-05 17:37:22 +0000267 || NEVER(db->mallocFailed)
danielk1977f653d782008-03-20 11:04:21 +0000268 ){
mistachkinfad30392016-02-13 23:43:46 +0000269 rc = SQLITE_NOMEM_BKPT;
danielk1977884c5b32007-03-06 16:03:55 +0000270 goto end_of_vacuum;
271 }
danielk19773df6b252004-05-29 10:23:19 +0000272
danielk197742741be2005-01-08 12:42:39 +0000273#ifndef SQLITE_OMIT_AUTOVACUUM
drhddac25c2007-12-05 01:38:23 +0000274 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
275 sqlite3BtreeGetAutoVacuum(pMain));
danielk197742741be2005-01-08 12:42:39 +0000276#endif
277
danielk19773df6b252004-05-29 10:23:19 +0000278 /* Query the schema of the main database. Create a mirror schema
279 ** in the temporary database.
280 */
drh9ef5e772016-08-19 14:20:56 +0000281 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
282 rc = execSqlF(db, pzErrMsg,
drh1e32bed2020-06-19 13:33:53 +0000283 "SELECT sql FROM \"%w\".sqlite_schema"
drh9ef5e772016-08-19 14:20:56 +0000284 " WHERE type='table'AND name<>'sqlite_sequence'"
drh34b27ed2018-05-03 21:51:30 +0000285 " AND coalesce(rootpage,1)>0",
drh9ef5e772016-08-19 14:20:56 +0000286 zDbMain
drh66b224c2006-09-11 11:13:26 +0000287 );
danielk197727c77432004-11-22 13:35:41 +0000288 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000289 rc = execSqlF(db, pzErrMsg,
drh1e32bed2020-06-19 13:33:53 +0000290 "SELECT sql FROM \"%w\".sqlite_schema"
drh34b27ed2018-05-03 21:51:30 +0000291 " WHERE type='index'",
drh9ef5e772016-08-19 14:20:56 +0000292 zDbMain
293 );
danielk197727c77432004-11-22 13:35:41 +0000294 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drh9ef5e772016-08-19 14:20:56 +0000295 db->init.iDb = 0;
danielk19773df6b252004-05-29 10:23:19 +0000296
297 /* Loop through the tables in the main database. For each, do
drh397308d2009-10-20 15:01:58 +0000298 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
danielk19773df6b252004-05-29 10:23:19 +0000299 ** the contents to the temporary database.
300 */
drh9ef5e772016-08-19 14:20:56 +0000301 rc = execSqlF(db, pzErrMsg,
302 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
303 "||' SELECT*FROM\"%w\".'||quote(name)"
drh1e32bed2020-06-19 13:33:53 +0000304 "FROM vacuum_db.sqlite_schema "
drh9ef5e772016-08-19 14:20:56 +0000305 "WHERE type='table'AND coalesce(rootpage,1)>0",
306 zDbMain
danielk19773df6b252004-05-29 10:23:19 +0000307 );
drh8257aa82017-07-26 19:59:13 +0000308 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
drh67752572019-04-04 15:25:52 +0000309 db->mDbFlags &= ~DBFLAG_Vacuum;
danielk19773df6b252004-05-29 10:23:19 +0000310 if( rc!=SQLITE_OK ) goto end_of_vacuum;
311
drhfdd48a72006-09-11 23:45:48 +0000312 /* Copy the triggers, views, and virtual tables from the main database
313 ** over to the temporary database. None of these objects has any
314 ** associated storage, so all we have to do is copy their entries
drh346a70c2020-06-15 20:27:35 +0000315 ** from the schema table.
danielk19773df6b252004-05-29 10:23:19 +0000316 */
drh9ef5e772016-08-19 14:20:56 +0000317 rc = execSqlF(db, pzErrMsg,
drh1e32bed2020-06-19 13:33:53 +0000318 "INSERT INTO vacuum_db.sqlite_schema"
319 " SELECT*FROM \"%w\".sqlite_schema"
drh9ef5e772016-08-19 14:20:56 +0000320 " WHERE type IN('view','trigger')"
321 " OR(type='table'AND rootpage=0)",
322 zDbMain
danielk19773df6b252004-05-29 10:23:19 +0000323 );
drhfdd48a72006-09-11 23:45:48 +0000324 if( rc ) goto end_of_vacuum;
danielk19773df6b252004-05-29 10:23:19 +0000325
danc5f20a02011-10-07 16:57:59 +0000326 /* At this point, there is a write transaction open on both the
327 ** vacuum database and the main database. Assuming no error occurs,
328 ** both transactions are closed by this block - the main database
329 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
330 ** call to sqlite3BtreeCommit().
danielk19773df6b252004-05-29 10:23:19 +0000331 */
drh5dc348a2009-05-05 17:37:22 +0000332 {
danielk19773df6b252004-05-29 10:23:19 +0000333 u32 meta;
drh8cbd3732005-02-12 00:19:30 +0000334 int i;
335
336 /* This array determines which meta meta values are preserved in the
337 ** vacuum. Even entries are the meta value number and odd entries
338 ** are an increment to apply to the meta value after the vacuum.
339 ** The increment is used to increase the schema cookie so that other
340 ** connections to the same database will know to reread the schema.
341 */
342 static const unsigned char aCopy[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000343 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
344 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
345 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
346 BTREE_USER_VERSION, 0, /* Preserve the user version */
drhb8a67ec2013-05-01 20:36:23 +0000347 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
drh8cbd3732005-02-12 00:19:30 +0000348 };
danielk19773df6b252004-05-29 10:23:19 +0000349
drh99744fa2020-08-25 19:09:07 +0000350 assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) );
351 assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) );
danielk19771d850a72004-05-31 08:26:49 +0000352
drh8cbd3732005-02-12 00:19:30 +0000353 /* Copy Btree meta values */
danielk197700e13612008-11-17 19:18:54 +0000354 for(i=0; i<ArraySize(aCopy); i+=2){
drh5dc348a2009-05-05 17:37:22 +0000355 /* GetMeta() and UpdateMeta() cannot fail in this context because
356 ** we already have page 1 loaded into cache and marked dirty. */
danielk1977602b4662009-07-02 07:47:33 +0000357 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
drh8cbd3732005-02-12 00:19:30 +0000358 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
drh5dc348a2009-05-05 17:37:22 +0000359 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
drh8cbd3732005-02-12 00:19:30 +0000360 }
danielk19773df6b252004-05-29 10:23:19 +0000361
drh2f6239e2018-12-08 00:43:08 +0000362 if( pOut==0 ){
drhb0b7db92018-12-07 17:28:28 +0000363 rc = sqlite3BtreeCopyFile(pMain, pTemp);
drhb0b7db92018-12-07 17:28:28 +0000364 }
danielk1977369f27e2004-06-15 11:40:04 +0000365 if( rc!=SQLITE_OK ) goto end_of_vacuum;
drhc9ac5ca2005-11-04 22:03:30 +0000366 rc = sqlite3BtreeCommit(pTemp);
367 if( rc!=SQLITE_OK ) goto end_of_vacuum;
danielk197706249db2008-08-23 16:17:55 +0000368#ifndef SQLITE_OMIT_AUTOVACUUM
drh2f6239e2018-12-08 00:43:08 +0000369 if( pOut==0 ){
drhb0b7db92018-12-07 17:28:28 +0000370 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
371 }
danielk197706249db2008-08-23 16:17:55 +0000372#endif
drha5f66832003-04-18 02:31:04 +0000373 }
drh13bff812003-04-15 01:19:47 +0000374
drh5dc348a2009-05-05 17:37:22 +0000375 assert( rc==SQLITE_OK );
drh2f6239e2018-12-08 00:43:08 +0000376 if( pOut==0 ){
drh41724eb2022-05-06 22:29:45 +0000377 nRes = sqlite3BtreeGetRequestedReserve(pTemp);
drhb0b7db92018-12-07 17:28:28 +0000378 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
379 }
danielk1977f653d782008-03-20 11:04:21 +0000380
drh13bff812003-04-15 01:19:47 +0000381end_of_vacuum:
drh0cd2d4c2005-11-03 02:15:02 +0000382 /* Restore the original value of db->flags */
drh9ef5e772016-08-19 14:20:56 +0000383 db->init.iDb = 0;
drh8257aa82017-07-26 19:59:13 +0000384 db->mDbFlags = saved_mDbFlags;
drh0cd2d4c2005-11-03 02:15:02 +0000385 db->flags = saved_flags;
drhe63b2c22008-05-21 13:44:13 +0000386 db->nChange = saved_nChange;
387 db->nTotalChange = saved_nTotalChange;
drh3d2a5292016-07-13 22:55:01 +0000388 db->mTrace = saved_mTrace;
drhe937df82020-05-07 01:56:57 +0000389 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
danielk19773a3f38e2005-05-22 06:49:56 +0000390
danielk197746c43ed2004-06-30 06:30:25 +0000391 /* Currently there is an SQL level transaction open on the vacuum
392 ** database. No locks are held on any other files (since the main file
393 ** was committed at the btree level). So it safe to end the transaction
394 ** by manually setting the autoCommit flag to true and detaching the
395 ** vacuum database. The vacuum_db journal file is deleted when the pager
396 ** is closed by the DETACH.
397 */
398 db->autoCommit = 1;
danielk1977261919c2005-12-06 12:52:59 +0000399
danielk19770203bde2006-01-11 16:10:20 +0000400 if( pDb ){
danielk19770203bde2006-01-11 16:10:20 +0000401 sqlite3BtreeClose(pDb->pBt);
danielk19770203bde2006-01-11 16:10:20 +0000402 pDb->pBt = 0;
403 pDb->pSchema = 0;
danielk197796fb0dd2004-06-30 09:49:22 +0000404 }
danielk1977f4208042005-12-06 17:48:31 +0000405
drh7464f572018-12-07 23:48:41 +0000406 /* This both clears the schemas and reduces the size of the db->aDb[]
407 ** array. */
408 sqlite3ResetAllSchemasOfConnection(db);
drh89dec812005-04-28 19:03:37 +0000409
danielk19773df6b252004-05-29 10:23:19 +0000410 return rc;
drh3f7d4e42004-07-24 14:35:58 +0000411}
drhc7792fa2011-04-02 16:28:52 +0000412
drhfdbcdee2007-03-27 14:44:50 +0000413#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */