Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: a931b0d12713b6e2c94b0d6d49e18e4f1edda800 [file] [log] [blame]
drha059ad02001-04-17 20:09:111/*
drh9e572e62004-04-23 23:43:102** 2004 April 6
drha059ad02001-04-17 20:09:113**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drha059ad02001-04-17 20:09:116**
drhb19a2bc2001-09-16 00:13:267** 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.
drha059ad02001-04-17 20:09:1110**
11*************************************************************************
peter.d.reid60ec9142014-09-06 16:39:4612** This file implements an external (disk-based) database using BTrees.
drha3152892007-05-05 11:48:5213** See the header comment on "btreeInt.h" for additional information.
14** Including a description of file format and an overview of operation.
drha059ad02001-04-17 20:09:1115*/
drha3152892007-05-05 11:48:5216#include "btreeInt.h"
paulb95a8862003-04-01 21:16:4117
drh8c42ca92001-06-22 19:15:0018/*
drha3152892007-05-05 11:48:5219** The header string that appears at the beginning of every
20** SQLite database.
drh556b2a22005-06-14 16:04:0521*/
drh556b2a22005-06-14 16:04:0522static const char zMagicHeader[] = SQLITE_FILE_HEADER;
drh08ed44e2001-04-29 23:32:5523
drh8c42ca92001-06-22 19:15:0024/*
drha3152892007-05-05 11:48:5225** Set this global variable to 1 to enable tracing using the TRACE
26** macro.
drh615ae552005-01-16 23:21:0027*/
drhe8f52c52008-07-12 14:52:2028#if 0
danielk1977a50d9aa2009-06-08 14:49:4529int sqlite3BtreeTrace=1; /* True to enable tracing */
drhe8f52c52008-07-12 14:52:2030# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
31#else
32# define TRACE(X)
drh615ae552005-01-16 23:21:0033#endif
drh615ae552005-01-16 23:21:0034
drh5d433ce2010-08-14 16:02:5235/*
36** Extract a 2-byte big-endian integer from an array of unsigned bytes.
37** But if the value is zero, make it 65536.
38**
39** This routine is used to extract the "offset to cell content area" value
40** from the header of a btree page. If the page size is 65536 and the page
41** is empty, the offset should be 65536, but the 2-byte value stores zero.
42** This routine makes the necessary adjustment to 65536.
43*/
44#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
drh86f8c192007-08-22 00:39:1945
dan09ff9e12013-03-11 11:49:0346/*
47** Values passed as the 5th argument to allocateBtreePage()
48*/
49#define BTALLOC_ANY 0 /* Allocate any page */
50#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
51#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
52
53/*
larrybrbc917382023-06-07 08:40:3154** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
dan09ff9e12013-03-11 11:49:0355** defined, or 0 if it is. For example:
56**
57** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
58*/
59#ifndef SQLITE_OMIT_AUTOVACUUM
60#define IfNotOmitAV(expr) (expr)
61#else
62#define IfNotOmitAV(expr) 0
63#endif
64
drhe53831d2007-08-17 01:14:3865#ifndef SQLITE_OMIT_SHARED_CACHE
66/*
danielk1977502b4e02008-09-02 14:07:2467** A list of BtShared objects that are eligible for participation
68** in shared cache. This variable has file scope during normal builds,
larrybrbc917382023-06-07 08:40:3169** but the test harness needs to access it so we make it global for
danielk1977502b4e02008-09-02 14:07:2470** test builds.
drh7555d8e2009-03-20 13:15:3071**
drhccb21132020-06-19 11:34:5772** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
drhe53831d2007-08-17 01:14:3873*/
74#ifdef SQLITE_TEST
drh78f82d12008-09-02 00:52:5275BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:3876#else
drh78f82d12008-09-02 00:52:5277static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:3878#endif
drhe53831d2007-08-17 01:14:3879#endif /* SQLITE_OMIT_SHARED_CACHE */
80
81#ifndef SQLITE_OMIT_SHARED_CACHE
82/*
83** Enable or disable the shared pager and schema features.
84**
85** This routine has no effect on existing database connections.
86** The shared cache setting effects only future calls to
87** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
88*/
89int sqlite3_enable_shared_cache(int enable){
danielk1977502b4e02008-09-02 14:07:2490 sqlite3GlobalConfig.sharedCacheEnabled = enable;
drhe53831d2007-08-17 01:14:3891 return SQLITE_OK;
92}
93#endif
94
drhd677b3d2007-08-20 22:48:4195
danielk1977aef0bf62005-12-30 16:28:0196
97#ifdef SQLITE_OMIT_SHARED_CACHE
98 /*
drhc25eabe2009-02-24 18:57:3199 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
100 ** and clearAllSharedCacheTableLocks()
danielk1977aef0bf62005-12-30 16:28:01101 ** manipulate entries in the BtShared.pLock linked list used to store
102 ** shared-cache table level locks. If the library is compiled with the
103 ** shared-cache feature disabled, then there is only ever one user
larrybrbc917382023-06-07 08:40:31104 ** of each BtShared structure and so this locking is not necessary.
danielk1977da184232006-01-05 11:34:32105 ** So define the lock related functions as no-ops.
danielk1977aef0bf62005-12-30 16:28:01106 */
drhc25eabe2009-02-24 18:57:31107 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
108 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
109 #define clearAllSharedCacheTableLocks(a)
danielk197794b30732009-07-02 17:21:57110 #define downgradeAllSharedCacheTableLocks(a)
danielk197796d48e92009-06-29 06:00:37111 #define hasSharedCacheTableLock(a,b,c,d) 1
112 #define hasReadConflicts(a, b) 0
drhe53831d2007-08-17 01:14:38113#endif
danielk1977aef0bf62005-12-30 16:28:01114
drh37ccfcf2020-08-31 18:49:04115#ifdef SQLITE_DEBUG
116/*
drha7fc1682020-11-24 19:55:49117** Return and reset the seek counter for a Btree object.
drh37ccfcf2020-08-31 18:49:04118*/
119sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){
120 u64 n = pBt->nSeek;
121 pBt->nSeek = 0;
122 return n;
123}
124#endif
125
daneebf2f52017-11-18 17:30:08126/*
127** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
128** (MemPage*) as an argument. The (MemPage*) must not be NULL.
129**
130** If SQLITE_DEBUG is not defined, then this macro is equivalent to
131** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
132** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
133** with the page number and filename associated with the (MemPage*).
134*/
135#ifdef SQLITE_DEBUG
136int corruptPageError(int lineno, MemPage *p){
drh8bfe66a2018-01-22 15:45:12137 char *zMsg;
138 sqlite3BeginBenignMalloc();
drh2e89f1c2023-04-01 12:22:57139 zMsg = sqlite3_mprintf("database corruption page %u of %s",
140 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
daneebf2f52017-11-18 17:30:08141 );
drh8bfe66a2018-01-22 15:45:12142 sqlite3EndBenignMalloc();
daneebf2f52017-11-18 17:30:08143 if( zMsg ){
144 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
145 }
146 sqlite3_free(zMsg);
147 return SQLITE_CORRUPT_BKPT;
148}
149# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
150#else
151# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
152#endif
153
drh1d09f4d2024-02-01 14:17:01154/* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
155** or if the lock tracking is disabled. This is always the value for
156** release builds.
157*/
158#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
159
drhe53831d2007-08-17 01:14:38160#ifndef SQLITE_OMIT_SHARED_CACHE
danielk197796d48e92009-06-29 06:00:37161
drh1d09f4d2024-02-01 14:17:01162#if 0
163/* ^---- Change to 1 and recompile to enable shared-lock tracing
164** for debugging purposes.
165**
166** Print all shared-cache locks on a BtShared. Debugging use only.
167*/
168static void sharedLockTrace(
169 BtShared *pBt,
170 const char *zMsg,
171 int iRoot,
172 int eLockType
173){
174 BtLock *pLock;
175 if( iRoot>0 ){
176 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W");
177 }else{
178 printf("%s-%p:", zMsg, pBt);
179 }
180 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
181 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
182 pLock->eLock==READ_LOCK ? "R" : "W");
183 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
184 pLock = pLock->pNext;
185 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W");
186 }
187 }
188 printf("\n");
189 fflush(stdout);
190}
191#undef SHARED_LOCK_TRACE
192#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) sharedLockTrace(X,MSG,TAB,TYPE)
193#endif /* Shared-lock tracing */
194
danielk197796d48e92009-06-29 06:00:37195#ifdef SQLITE_DEBUG
196/*
drh0ee3dbe2009-10-16 15:05:18197**** This function is only used as part of an assert() statement. ***
198**
larrybrbc917382023-06-07 08:40:31199** Check to see if pBtree holds the required locks to read or write to the
drh0ee3dbe2009-10-16 15:05:18200** table with root page iRoot. Return 1 if it does and 0 if not.
201**
larrybrbc917382023-06-07 08:40:31202** For example, when writing to a table with root-page iRoot via
danielk197796d48e92009-06-29 06:00:37203** Btree connection pBtree:
204**
205** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
206**
larrybrbc917382023-06-07 08:40:31207** When writing to an index that resides in a sharable database, the
danielk197796d48e92009-06-29 06:00:37208** caller should have first obtained a lock specifying the root page of
drh0ee3dbe2009-10-16 15:05:18209** the corresponding table. This makes things a bit more complicated,
210** as this module treats each table as a separate structure. To determine
211** the table corresponding to the index being written, this
danielk197796d48e92009-06-29 06:00:37212** function has to search through the database schema.
213**
drh0ee3dbe2009-10-16 15:05:18214** Instead of a lock on the table/index rooted at page iRoot, the caller may
danielk197796d48e92009-06-29 06:00:37215** hold a write-lock on the schema table (root page 1). This is also
216** acceptable.
217*/
218static int hasSharedCacheTableLock(
219 Btree *pBtree, /* Handle that must hold lock */
220 Pgno iRoot, /* Root page of b-tree */
221 int isIndex, /* True if iRoot is the root of an index b-tree */
222 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
223){
224 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
225 Pgno iTab = 0;
226 BtLock *pLock;
227
drh0ee3dbe2009-10-16 15:05:18228 /* If this database is not shareable, or if the client is reading
larrybrbc917382023-06-07 08:40:31229 ** and has the read-uncommitted flag set, then no lock is required.
drh0ee3dbe2009-10-16 15:05:18230 ** Return true immediately.
231 */
danielk197796d48e92009-06-29 06:00:37232 if( (pBtree->sharable==0)
drh169dd922017-06-26 13:57:49233 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
danielk197796d48e92009-06-29 06:00:37234 ){
235 return 1;
236 }
237
drh0ee3dbe2009-10-16 15:05:18238 /* If the client is reading or writing an index and the schema is
239 ** not loaded, then it is too difficult to actually check to see if
240 ** the correct locks are held. So do not bother - just return true.
241 ** This case does not come up very often anyhow.
242 */
drh2c5e35f2014-08-05 11:04:21243 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
drh0ee3dbe2009-10-16 15:05:18244 return 1;
245 }
246
danielk197796d48e92009-06-29 06:00:37247 /* Figure out the root-page that the lock should be held on. For table
248 ** b-trees, this is just the root page of the b-tree being read or
249 ** written. For index b-trees, it is the root page of the associated
250 ** table. */
251 if( isIndex ){
252 HashElem *p;
dan877859f2020-06-17 20:29:56253 int bSeen = 0;
danielk197796d48e92009-06-29 06:00:37254 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
255 Index *pIdx = (Index *)sqliteHashData(p);
drhe684ac62022-03-08 13:59:46256 if( pIdx->tnum==iRoot ){
dan877859f2020-06-17 20:29:56257 if( bSeen ){
drh1ffede82015-01-30 20:59:27258 /* Two or more indexes share the same root page. There must
259 ** be imposter tables. So just return true. The assert is not
260 ** useful in that case. */
261 return 1;
262 }
shane5eff7cf2009-08-10 03:57:58263 iTab = pIdx->pTable->tnum;
dan877859f2020-06-17 20:29:56264 bSeen = 1;
danielk197796d48e92009-06-29 06:00:37265 }
266 }
267 }else{
268 iTab = iRoot;
269 }
270
drh1d09f4d2024-02-01 14:17:01271 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
272
larrybrbc917382023-06-07 08:40:31273 /* Search for the required lock. Either a write-lock on root-page iTab, a
danielk197796d48e92009-06-29 06:00:37274 ** write-lock on the schema table, or (if the client is reading) a
275 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
276 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
larrybrbc917382023-06-07 08:40:31277 if( pLock->pBtree==pBtree
danielk197796d48e92009-06-29 06:00:37278 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
larrybrbc917382023-06-07 08:40:31279 && pLock->eLock>=eLockType
danielk197796d48e92009-06-29 06:00:37280 ){
281 return 1;
282 }
283 }
284
285 /* Failed to find the required lock. */
286 return 0;
287}
drh0ee3dbe2009-10-16 15:05:18288#endif /* SQLITE_DEBUG */
danielk197796d48e92009-06-29 06:00:37289
drh0ee3dbe2009-10-16 15:05:18290#ifdef SQLITE_DEBUG
danielk197796d48e92009-06-29 06:00:37291/*
drh0ee3dbe2009-10-16 15:05:18292**** This function may be used as part of assert() statements only. ****
danielk197796d48e92009-06-29 06:00:37293**
drh0ee3dbe2009-10-16 15:05:18294** Return true if it would be illegal for pBtree to write into the
295** table or index rooted at iRoot because other shared connections are
296** simultaneously reading that same table or index.
297**
298** It is illegal for pBtree to write if some other Btree object that
299** shares the same BtShared object is currently reading or writing
300** the iRoot table. Except, if the other Btree object has the
301** read-uncommitted flag set, then it is OK for the other object to
302** have a read cursor.
303**
304** For example, before writing to any part of the table or index
305** rooted at page iRoot, one should call:
danielk197796d48e92009-06-29 06:00:37306**
307** assert( !hasReadConflicts(pBtree, iRoot) );
308*/
309static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
310 BtCursor *p;
311 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
larrybrbc917382023-06-07 08:40:31312 if( p->pgnoRoot==iRoot
danielk197796d48e92009-06-29 06:00:37313 && p->pBtree!=pBtree
drh169dd922017-06-26 13:57:49314 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
danielk197796d48e92009-06-29 06:00:37315 ){
316 return 1;
317 }
318 }
319 return 0;
320}
321#endif /* #ifdef SQLITE_DEBUG */
322
danielk1977da184232006-01-05 11:34:32323/*
larrybrbc917382023-06-07 08:40:31324** Query to see if Btree handle p may obtain a lock of type eLock
danielk1977aef0bf62005-12-30 16:28:01325** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
drhc25eabe2009-02-24 18:57:31326** SQLITE_OK if the lock may be obtained (by calling
327** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
danielk1977aef0bf62005-12-30 16:28:01328*/
drhc25eabe2009-02-24 18:57:31329static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
danielk1977aef0bf62005-12-30 16:28:01330 BtShared *pBt = p->pBt;
331 BtLock *pIter;
332
drh1fee73e2007-08-29 04:00:57333 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40334 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
335 assert( p->db!=0 );
drh169dd922017-06-26 13:57:49336 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
larrybrbc917382023-06-07 08:40:31337
danielk19775b413d72009-04-01 09:41:54338 /* If requesting a write-lock, then the Btree must have an open write
larrybrbc917382023-06-07 08:40:31339 ** transaction on this file. And, obviously, for this to be so there
danielk19775b413d72009-04-01 09:41:54340 ** must be an open write transaction on the file itself.
341 */
342 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
343 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
larrybrbc917382023-06-07 08:40:31344
drh0ee3dbe2009-10-16 15:05:18345 /* This routine is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38346 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32347 return SQLITE_OK;
348 }
349
danielk1977641b0f42007-12-21 04:47:25350 /* If some other connection is holding an exclusive lock, the
351 ** requested lock may not be obtained.
352 */
drhc9166342012-01-05 23:32:06353 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
danielk1977404ca072009-03-16 13:19:36354 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
355 return SQLITE_LOCKED_SHAREDCACHE;
danielk1977641b0f42007-12-21 04:47:25356 }
357
danielk1977e0d9e6f2009-07-03 16:25:06358 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
larrybrbc917382023-06-07 08:40:31359 /* The condition (pIter->eLock!=eLock) in the following if(...)
danielk1977e0d9e6f2009-07-03 16:25:06360 ** statement is a simplification of:
361 **
362 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
363 **
364 ** since we know that if eLock==WRITE_LOCK, then no other connection
365 ** may hold a WRITE_LOCK on any table in this file (since there can
366 ** only be a single writer).
367 */
368 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
369 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
370 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
371 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
372 if( eLock==WRITE_LOCK ){
373 assert( p==pBt->pWriter );
drhc9166342012-01-05 23:32:06374 pBt->btsFlags |= BTS_PENDING;
danielk1977da184232006-01-05 11:34:32375 }
danielk1977e0d9e6f2009-07-03 16:25:06376 return SQLITE_LOCKED_SHAREDCACHE;
danielk1977aef0bf62005-12-30 16:28:01377 }
378 }
379 return SQLITE_OK;
380}
drhe53831d2007-08-17 01:14:38381#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01382
drhe53831d2007-08-17 01:14:38383#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01384/*
385** Add a lock on the table with root-page iTable to the shared-btree used
larrybrbc917382023-06-07 08:40:31386** by Btree handle p. Parameter eLock must be either READ_LOCK or
danielk1977aef0bf62005-12-30 16:28:01387** WRITE_LOCK.
388**
danielk19779d104862009-07-09 08:27:14389** This function assumes the following:
390**
drh0ee3dbe2009-10-16 15:05:18391** (a) The specified Btree object p is connected to a sharable
392** database (one with the BtShared.sharable flag set), and
danielk19779d104862009-07-09 08:27:14393**
drh0ee3dbe2009-10-16 15:05:18394** (b) No other Btree objects hold a lock that conflicts
danielk19779d104862009-07-09 08:27:14395** with the requested lock (i.e. querySharedCacheTableLock() has
396** already been called and returned SQLITE_OK).
397**
larrybrbc917382023-06-07 08:40:31398** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
danielk19779d104862009-07-09 08:27:14399** is returned if a malloc attempt fails.
danielk1977aef0bf62005-12-30 16:28:01400*/
drhc25eabe2009-02-24 18:57:31401static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
danielk1977aef0bf62005-12-30 16:28:01402 BtShared *pBt = p->pBt;
403 BtLock *pLock = 0;
404 BtLock *pIter;
405
drh1d09f4d2024-02-01 14:17:01406 SHARED_LOCK_TRACE(pBt,"setLock", iTable, eLock);
407
drh1fee73e2007-08-29 04:00:57408 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40409 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
410 assert( p->db!=0 );
drhd677b3d2007-08-20 22:48:41411
danielk1977e0d9e6f2009-07-03 16:25:06412 /* A connection with the read-uncommitted flag set will never try to
413 ** obtain a read-lock using this function. The only read-lock obtained
larrybrbc917382023-06-07 08:40:31414 ** by a connection in read-uncommitted mode is on the sqlite_schema
danielk1977e0d9e6f2009-07-03 16:25:06415 ** table, and that lock is obtained in BtreeBeginTrans(). */
drh169dd922017-06-26 13:57:49416 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
danielk1977e0d9e6f2009-07-03 16:25:06417
larrybrbc917382023-06-07 08:40:31418 /* This function should only be called on a sharable b-tree after it
danielk19779d104862009-07-09 08:27:14419 ** has been determined that no other b-tree holds a conflicting lock. */
420 assert( p->sharable );
drhc25eabe2009-02-24 18:57:31421 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
danielk1977aef0bf62005-12-30 16:28:01422
423 /* First search the list for an existing lock on this table. */
424 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
425 if( pIter->iTable==iTable && pIter->pBtree==p ){
426 pLock = pIter;
427 break;
428 }
429 }
430
431 /* If the above search did not find a BtLock struct associating Btree p
432 ** with table iTable, allocate one and link it into the list.
433 */
434 if( !pLock ){
drh17435752007-08-16 04:30:38435 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
danielk1977aef0bf62005-12-30 16:28:01436 if( !pLock ){
mistachkinfad30392016-02-13 23:43:46437 return SQLITE_NOMEM_BKPT;
danielk1977aef0bf62005-12-30 16:28:01438 }
439 pLock->iTable = iTable;
440 pLock->pBtree = p;
441 pLock->pNext = pBt->pLock;
442 pBt->pLock = pLock;
443 }
444
445 /* Set the BtLock.eLock variable to the maximum of the current lock
446 ** and the requested lock. This means if a write-lock was already held
447 ** and a read-lock requested, we don't incorrectly downgrade the lock.
448 */
449 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53450 if( eLock>pLock->eLock ){
451 pLock->eLock = eLock;
452 }
danielk1977aef0bf62005-12-30 16:28:01453
454 return SQLITE_OK;
455}
drhe53831d2007-08-17 01:14:38456#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01457
drhe53831d2007-08-17 01:14:38458#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01459/*
drhc25eabe2009-02-24 18:57:31460** Release all the table locks (locks obtained via calls to
drh0ee3dbe2009-10-16 15:05:18461** the setSharedCacheTableLock() procedure) held by Btree object p.
danielk1977fa542f12009-04-02 18:28:08462**
larrybrbc917382023-06-07 08:40:31463** This function assumes that Btree p has an open read or write
drhc9166342012-01-05 23:32:06464** transaction. If it does not, then the BTS_PENDING flag
danielk1977fa542f12009-04-02 18:28:08465** may be incorrectly cleared.
danielk1977aef0bf62005-12-30 16:28:01466*/
drhc25eabe2009-02-24 18:57:31467static void clearAllSharedCacheTableLocks(Btree *p){
danielk1977641b0f42007-12-21 04:47:25468 BtShared *pBt = p->pBt;
469 BtLock **ppIter = &pBt->pLock;
danielk1977da184232006-01-05 11:34:32470
drh1fee73e2007-08-29 04:00:57471 assert( sqlite3BtreeHoldsMutex(p) );
drhe53831d2007-08-17 01:14:38472 assert( p->sharable || 0==*ppIter );
danielk1977fa542f12009-04-02 18:28:08473 assert( p->inTrans>0 );
danielk1977da184232006-01-05 11:34:32474
drh1d09f4d2024-02-01 14:17:01475 SHARED_LOCK_TRACE(pBt, "clearAllLocks", 0, 0);
476
danielk1977aef0bf62005-12-30 16:28:01477 while( *ppIter ){
478 BtLock *pLock = *ppIter;
drhc9166342012-01-05 23:32:06479 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
danielk1977fa542f12009-04-02 18:28:08480 assert( pLock->pBtree->inTrans>=pLock->eLock );
danielk1977aef0bf62005-12-30 16:28:01481 if( pLock->pBtree==p ){
482 *ppIter = pLock->pNext;
danielk1977602b4662009-07-02 07:47:33483 assert( pLock->iTable!=1 || pLock==&p->lock );
484 if( pLock->iTable!=1 ){
485 sqlite3_free(pLock);
486 }
danielk1977aef0bf62005-12-30 16:28:01487 }else{
488 ppIter = &pLock->pNext;
489 }
490 }
danielk1977641b0f42007-12-21 04:47:25491
drhc9166342012-01-05 23:32:06492 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
danielk1977404ca072009-03-16 13:19:36493 if( pBt->pWriter==p ){
494 pBt->pWriter = 0;
drhc9166342012-01-05 23:32:06495 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
danielk1977404ca072009-03-16 13:19:36496 }else if( pBt->nTransaction==2 ){
larrybrbc917382023-06-07 08:40:31497 /* This function is called when Btree p is concluding its
danielk1977404ca072009-03-16 13:19:36498 ** transaction. If there currently exists a writer, and p is not
499 ** that writer, then the number of locks held by connections other
500 ** than the writer must be about to drop to zero. In this case
drhc9166342012-01-05 23:32:06501 ** set the BTS_PENDING flag to 0.
danielk1977404ca072009-03-16 13:19:36502 **
drhc9166342012-01-05 23:32:06503 ** If there is not currently a writer, then BTS_PENDING must
danielk1977404ca072009-03-16 13:19:36504 ** be zero already. So this next line is harmless in that case.
505 */
drhc9166342012-01-05 23:32:06506 pBt->btsFlags &= ~BTS_PENDING;
danielk1977641b0f42007-12-21 04:47:25507 }
danielk1977aef0bf62005-12-30 16:28:01508}
danielk197794b30732009-07-02 17:21:57509
danielk1977e0d9e6f2009-07-03 16:25:06510/*
drh0ee3dbe2009-10-16 15:05:18511** This function changes all write-locks held by Btree p into read-locks.
danielk1977e0d9e6f2009-07-03 16:25:06512*/
danielk197794b30732009-07-02 17:21:57513static void downgradeAllSharedCacheTableLocks(Btree *p){
514 BtShared *pBt = p->pBt;
drh1d09f4d2024-02-01 14:17:01515
516 SHARED_LOCK_TRACE(pBt, "downgradeLocks", 0, 0);
517
danielk197794b30732009-07-02 17:21:57518 if( pBt->pWriter==p ){
519 BtLock *pLock;
520 pBt->pWriter = 0;
drhc9166342012-01-05 23:32:06521 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
danielk197794b30732009-07-02 17:21:57522 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
523 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
524 pLock->eLock = READ_LOCK;
525 }
526 }
527}
528
danielk1977aef0bf62005-12-30 16:28:01529#endif /* SQLITE_OMIT_SHARED_CACHE */
530
drh3908fe92017-09-01 14:50:19531static void releasePage(MemPage *pPage); /* Forward reference */
532static void releasePageOne(MemPage *pPage); /* Forward reference */
drh352a35a2017-08-15 03:46:47533static void releasePageNotNull(MemPage *pPage); /* Forward reference */
drh980b1a72006-08-16 16:42:48534
drh1fee73e2007-08-29 04:00:57535/*
drh0ee3dbe2009-10-16 15:05:18536***** This routine is used inside of assert() only ****
537**
538** Verify that the cursor holds the mutex on its BtShared
drh1fee73e2007-08-29 04:00:57539*/
drh0ee3dbe2009-10-16 15:05:18540#ifdef SQLITE_DEBUG
drh1fee73e2007-08-29 04:00:57541static int cursorHoldsMutex(BtCursor *p){
drhff0587c2007-08-29 17:43:19542 return sqlite3_mutex_held(p->pBt->mutex);
drh1fee73e2007-08-29 04:00:57543}
drh5e08d0f2016-06-04 21:05:54544
545/* Verify that the cursor and the BtShared agree about what is the current
larrybrbc917382023-06-07 08:40:31546** database connetion. This is important in shared-cache mode. If the database
drh5e08d0f2016-06-04 21:05:54547** connection pointers get out-of-sync, it is possible for routines like
548** btreeInitPage() to reference an stale connection pointer that references a
549** a connection that has already closed. This routine is used inside assert()
550** statements only and for the purpose of double-checking that the btree code
551** does keep the database connection pointers up-to-date.
552*/
dan7a2347e2016-01-07 16:43:54553static int cursorOwnsBtShared(BtCursor *p){
554 assert( cursorHoldsMutex(p) );
555 return (p->pBtree->db==p->pBt->db);
556}
drh1fee73e2007-08-29 04:00:57557#endif
558
danielk197792d4d7a2007-05-04 12:05:56559/*
dan5a500af2014-03-11 20:33:04560** Invalidate the overflow cache of the cursor passed as the first argument.
561** on the shared btree structure pBt.
danielk197792d4d7a2007-05-04 12:05:56562*/
drh036dbec2014-03-11 23:40:44563#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
danielk197792d4d7a2007-05-04 12:05:56564
565/*
566** Invalidate the overflow page-list cache for all cursors opened
567** on the shared btree structure pBt.
568*/
569static void invalidateAllOverflowCache(BtShared *pBt){
570 BtCursor *p;
drh1fee73e2007-08-29 04:00:57571 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56572 for(p=pBt->pCursor; p; p=p->pNext){
573 invalidateOverflowCache(p);
574 }
575}
danielk197796d48e92009-06-29 06:00:37576
dan5a500af2014-03-11 20:33:04577#ifndef SQLITE_OMIT_INCRBLOB
danielk197796d48e92009-06-29 06:00:37578/*
579** This function is called before modifying the contents of a table
drh0ee3dbe2009-10-16 15:05:18580** to invalidate any incrblob cursors that are open on the
drheeb844a2009-08-08 18:01:07581** row or one of the rows being modified.
danielk197796d48e92009-06-29 06:00:37582**
583** If argument isClearTable is true, then the entire contents of the
584** table is about to be deleted. In this case invalidate all incrblob
585** cursors open on any row within the table with root-page pgnoRoot.
586**
587** Otherwise, if argument isClearTable is false, then the row with
588** rowid iRow is being replaced or deleted. In this case invalidate
drh0ee3dbe2009-10-16 15:05:18589** only those incrblob cursors open on that specific row.
danielk197796d48e92009-06-29 06:00:37590*/
591static void invalidateIncrblobCursors(
592 Btree *pBtree, /* The database file to check */
drh9ca431a2017-03-29 18:03:50593 Pgno pgnoRoot, /* The table that might be changing */
danielk197796d48e92009-06-29 06:00:37594 i64 iRow, /* The rowid that might be changing */
595 int isClearTable /* True if all rows are being deleted */
596){
597 BtCursor *p;
drh49bb56e2021-05-14 20:01:36598 assert( pBtree->hasIncrblobCur );
danielk197796d48e92009-06-29 06:00:37599 assert( sqlite3BtreeHoldsMutex(pBtree) );
drh69180952015-06-25 13:03:10600 pBtree->hasIncrblobCur = 0;
601 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
602 if( (p->curFlags & BTCF_Incrblob)!=0 ){
603 pBtree->hasIncrblobCur = 1;
drh9ca431a2017-03-29 18:03:50604 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
drh69180952015-06-25 13:03:10605 p->eState = CURSOR_INVALID;
606 }
danielk197796d48e92009-06-29 06:00:37607 }
608 }
609}
610
danielk197792d4d7a2007-05-04 12:05:56611#else
dan5a500af2014-03-11 20:33:04612 /* Stub function when INCRBLOB is omitted */
drh9ca431a2017-03-29 18:03:50613 #define invalidateIncrblobCursors(w,x,y,z)
drh0ee3dbe2009-10-16 15:05:18614#endif /* SQLITE_OMIT_INCRBLOB */
danielk197792d4d7a2007-05-04 12:05:56615
drh980b1a72006-08-16 16:42:48616/*
larrybrbc917382023-06-07 08:40:31617** Set bit pgno of the BtShared.pHasContent bitvec. This is called
618** when a page that previously contained data becomes a free-list leaf
danielk1977bea2a942009-01-20 17:06:27619** page.
620**
621** The BtShared.pHasContent bitvec exists to work around an obscure
622** bug caused by the interaction of two useful IO optimizations surrounding
623** free-list leaf pages:
624**
625** 1) When all data is deleted from a page and the page becomes
626** a free-list leaf page, the page is not written to the database
627** (as free-list leaf pages contain no meaningful data). Sometimes
628** such a page is not even journalled (as it will not be modified,
629** why bother journalling it?).
630**
631** 2) When a free-list leaf page is reused, its content is not read
632** from the database or written to the journal file (why should it
633** be, if it is not at all meaningful?).
634**
635** By themselves, these optimizations work fine and provide a handy
636** performance boost to bulk delete or insert operations. However, if
637** a page is moved to the free-list and then reused within the same
638** transaction, a problem comes up. If the page is not journalled when
639** it is moved to the free-list and it is also not journalled when it
640** is extracted from the free-list and reused, then the original data
641** may be lost. In the event of a rollback, it may not be possible
642** to restore the database to its original configuration.
643**
larrybrbc917382023-06-07 08:40:31644** The solution is the BtShared.pHasContent bitvec. Whenever a page is
danielk1977bea2a942009-01-20 17:06:27645** moved to become a free-list leaf page, the corresponding bit is
646** set in the bitvec. Whenever a leaf page is extracted from the free-list,
drh0ee3dbe2009-10-16 15:05:18647** optimization 2 above is omitted if the corresponding bit is already
danielk1977bea2a942009-01-20 17:06:27648** set in BtShared.pHasContent. The contents of the bitvec are cleared
649** at the end of every transaction.
650*/
651static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
652 int rc = SQLITE_OK;
653 if( !pBt->pHasContent ){
drhdd3cd972010-03-27 17:12:36654 assert( pgno<=pBt->nPage );
655 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
drh4c301aa2009-07-15 17:25:45656 if( !pBt->pHasContent ){
mistachkinfad30392016-02-13 23:43:46657 rc = SQLITE_NOMEM_BKPT;
danielk1977bea2a942009-01-20 17:06:27658 }
659 }
660 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
661 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
662 }
663 return rc;
664}
665
666/*
667** Query the BtShared.pHasContent vector.
668**
669** This function is called when a free-list leaf page is removed from the
670** free-list for reuse. It returns false if it is safe to retrieve the
671** page from the pager layer with the 'no-content' flag set. True otherwise.
672*/
673static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
674 Bitvec *p = pBt->pHasContent;
pdrdb9cb172020-03-08 13:33:58675 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
danielk1977bea2a942009-01-20 17:06:27676}
677
678/*
679** Clear (destroy) the BtShared.pHasContent bitvec. This should be
680** invoked at the conclusion of each write-transaction.
681*/
682static void btreeClearHasContent(BtShared *pBt){
683 sqlite3BitvecDestroy(pBt->pHasContent);
684 pBt->pHasContent = 0;
685}
686
687/*
drh138eeeb2013-03-27 03:15:23688** Release all of the apPage[] pages for a cursor.
689*/
690static void btreeReleaseAllCursorPages(BtCursor *pCur){
691 int i;
drh352a35a2017-08-15 03:46:47692 if( pCur->iPage>=0 ){
693 for(i=0; i<pCur->iPage; i++){
694 releasePageNotNull(pCur->apPage[i]);
695 }
696 releasePageNotNull(pCur->pPage);
697 pCur->iPage = -1;
drh138eeeb2013-03-27 03:15:23698 }
drh138eeeb2013-03-27 03:15:23699}
700
danf0ee1d32015-09-12 19:26:11701/*
702** The cursor passed as the only argument must point to a valid entry
703** when this function is called (i.e. have eState==CURSOR_VALID). This
704** function saves the current cursor key in variables pCur->nKey and
larrybrbc917382023-06-07 08:40:31705** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
danf0ee1d32015-09-12 19:26:11706** code otherwise.
707**
708** If the cursor is open on an intkey table, then the integer key
709** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
larrybrbc917382023-06-07 08:40:31710** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
711** set to point to a malloced buffer pCur->nKey bytes in size containing
danf0ee1d32015-09-12 19:26:11712** the key.
713*/
714static int saveCursorKey(BtCursor *pCur){
drha7c90c42016-06-04 20:37:10715 int rc = SQLITE_OK;
danf0ee1d32015-09-12 19:26:11716 assert( CURSOR_VALID==pCur->eState );
717 assert( 0==pCur->pKey );
718 assert( cursorHoldsMutex(pCur) );
719
drha7c90c42016-06-04 20:37:10720 if( pCur->curIntKey ){
721 /* Only the rowid is required for a table btree */
722 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
723 }else{
danfffaf232018-12-14 13:18:35724 /* For an index btree, save the complete key content. It is possible
725 ** that the current key is corrupt. In that case, it is possible that
726 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
larrybrbc917382023-06-07 08:40:31727 ** up to the size of 1 varint plus 1 8-byte value when the cursor
728 ** position is restored. Hence the 17 bytes of padding allocated
danfffaf232018-12-14 13:18:35729 ** below. */
drhd66c4f82016-06-04 20:58:35730 void *pKey;
drha7c90c42016-06-04 20:37:10731 pCur->nKey = sqlite3BtreePayloadSize(pCur);
drhef86b942025-02-17 17:33:14732 pKey = sqlite3Malloc( ((i64)pCur->nKey) + 9 + 8 );
danf0ee1d32015-09-12 19:26:11733 if( pKey ){
drhcb3cabd2016-11-25 19:18:28734 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
danf0ee1d32015-09-12 19:26:11735 if( rc==SQLITE_OK ){
drhe6c628e2019-01-21 16:01:17736 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
danf0ee1d32015-09-12 19:26:11737 pCur->pKey = pKey;
738 }else{
739 sqlite3_free(pKey);
740 }
741 }else{
mistachkinfad30392016-02-13 23:43:46742 rc = SQLITE_NOMEM_BKPT;
danf0ee1d32015-09-12 19:26:11743 }
744 }
745 assert( !pCur->curIntKey || !pCur->pKey );
746 return rc;
747}
drh138eeeb2013-03-27 03:15:23748
749/*
larrybrbc917382023-06-07 08:40:31750** Save the current cursor position in the variables BtCursor.nKey
drh980b1a72006-08-16 16:42:48751** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
drhea8ffdf2009-07-22 00:35:23752**
753** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
larrybrbc917382023-06-07 08:40:31754** prior to calling this routine.
drh980b1a72006-08-16 16:42:48755*/
756static int saveCursorPosition(BtCursor *pCur){
757 int rc;
758
drhd2f83132015-03-25 17:35:01759 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
drh980b1a72006-08-16 16:42:48760 assert( 0==pCur->pKey );
drh1fee73e2007-08-29 04:00:57761 assert( cursorHoldsMutex(pCur) );
drh980b1a72006-08-16 16:42:48762
drh7b14b652019-12-29 22:08:20763 if( pCur->curFlags & BTCF_Pinned ){
764 return SQLITE_CONSTRAINT_PINNED;
765 }
drhd2f83132015-03-25 17:35:01766 if( pCur->eState==CURSOR_SKIPNEXT ){
767 pCur->eState = CURSOR_VALID;
768 }else{
769 pCur->skipNext = 0;
770 }
drh980b1a72006-08-16 16:42:48771
danf0ee1d32015-09-12 19:26:11772 rc = saveCursorKey(pCur);
drh980b1a72006-08-16 16:42:48773 if( rc==SQLITE_OK ){
drh138eeeb2013-03-27 03:15:23774 btreeReleaseAllCursorPages(pCur);
drh980b1a72006-08-16 16:42:48775 pCur->eState = CURSOR_REQUIRESEEK;
776 }
777
dane755e102015-09-30 12:59:12778 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
drh980b1a72006-08-16 16:42:48779 return rc;
780}
781
drh637f3d82014-08-22 22:26:07782/* Forward reference */
783static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
784
drh980b1a72006-08-16 16:42:48785/*
drh0ee3dbe2009-10-16 15:05:18786** Save the positions of all cursors (except pExcept) that are open on
drh637f3d82014-08-22 22:26:07787** the table with root-page iRoot. "Saving the cursor position" means that
788** the location in the btree is remembered in such a way that it can be
789** moved back to the same spot after the btree has been modified. This
790** routine is called just before cursor pExcept is used to modify the
791** table, for example in BtreeDelete() or BtreeInsert().
792**
larrybrbc917382023-06-07 08:40:31793** If there are two or more cursors on the same btree, then all such
drh27fb7462015-06-30 02:47:36794** cursors should have their BTCF_Multiple flag set. The btreeCursor()
795** routine enforces that rule. This routine only needs to be called in
796** the uncommon case when pExpect has the BTCF_Multiple flag set.
797**
798** If pExpect!=NULL and if no other cursors are found on the same root-page,
799** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
800** pointless call to this routine.
801**
drh637f3d82014-08-22 22:26:07802** Implementation note: This routine merely checks to see if any cursors
803** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
804** event that cursors are in need to being saved.
drh980b1a72006-08-16 16:42:48805*/
806static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
807 BtCursor *p;
drh1fee73e2007-08-29 04:00:57808 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34809 assert( pExcept==0 || pExcept->pBt==pBt );
drh980b1a72006-08-16 16:42:48810 for(p=pBt->pCursor; p; p=p->pNext){
drh637f3d82014-08-22 22:26:07811 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
812 }
drh27fb7462015-06-30 02:47:36813 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
814 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
815 return SQLITE_OK;
drh637f3d82014-08-22 22:26:07816}
817
818/* This helper routine to saveAllCursors does the actual work of saving
819** the cursors if and when a cursor is found that actually requires saving.
820** The common case is that no cursors need to be saved, so this routine is
821** broken out from its caller to avoid unnecessary stack pointer movement.
822*/
823static int SQLITE_NOINLINE saveCursorsOnList(
drh3f387402014-09-24 01:23:00824 BtCursor *p, /* The first cursor that needs saving */
825 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
826 BtCursor *pExcept /* Do not save this cursor */
drh637f3d82014-08-22 22:26:07827){
828 do{
drh138eeeb2013-03-27 03:15:23829 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
drhd2f83132015-03-25 17:35:01830 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
drh138eeeb2013-03-27 03:15:23831 int rc = saveCursorPosition(p);
832 if( SQLITE_OK!=rc ){
833 return rc;
834 }
835 }else{
drh85ef6302017-08-02 15:50:09836 testcase( p->iPage>=0 );
drh138eeeb2013-03-27 03:15:23837 btreeReleaseAllCursorPages(p);
drh980b1a72006-08-16 16:42:48838 }
839 }
drh637f3d82014-08-22 22:26:07840 p = p->pNext;
841 }while( p );
drh980b1a72006-08-16 16:42:48842 return SQLITE_OK;
843}
844
845/*
drhbf700f32007-03-31 02:36:44846** Clear the current cursor position.
847*/
danielk1977be51a652008-10-08 17:58:48848void sqlite3BtreeClearCursor(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57849 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38850 sqlite3_free(pCur->pKey);
drhbf700f32007-03-31 02:36:44851 pCur->pKey = 0;
852 pCur->eState = CURSOR_INVALID;
853}
854
855/*
danielk19773509a652009-07-06 18:56:13856** In this version of BtreeMoveto, pKey is a packed index record
857** such as is generated by the OP_MakeRecord opcode. Unpack the
drheab10642022-03-06 20:22:24858** record and then call sqlite3BtreeIndexMoveto() to do the work.
danielk19773509a652009-07-06 18:56:13859*/
860static int btreeMoveto(
861 BtCursor *pCur, /* Cursor open on the btree to be searched */
862 const void *pKey, /* Packed key if the btree is an index */
863 i64 nKey, /* Integer key for tables. Size of pKey for indices */
864 int bias, /* Bias search to the high end */
865 int *pRes /* Write search results here */
866){
867 int rc; /* Status code */
868 UnpackedRecord *pIdxKey; /* Unpacked index key */
danielk19773509a652009-07-06 18:56:13869
870 if( pKey ){
danb0c4c942019-01-24 15:16:17871 KeyInfo *pKeyInfo = pCur->pKeyInfo;
danielk19773509a652009-07-06 18:56:13872 assert( nKey==(i64)(int)nKey );
danb0c4c942019-01-24 15:16:17873 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
mistachkinfad30392016-02-13 23:43:46874 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
drh8658a8d2025-06-02 13:54:33875 sqlite3VdbeRecordUnpack((int)nKey, pKey, pIdxKey);
danb0c4c942019-01-24 15:16:17876 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
mistachkin88a79732017-09-04 19:31:54877 rc = SQLITE_CORRUPT_BKPT;
drh42a410d2021-06-19 18:32:20878 }else{
879 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes);
drh094b7582013-11-30 12:49:28880 }
drh42a410d2021-06-19 18:32:20881 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
danielk19773509a652009-07-06 18:56:13882 }else{
883 pIdxKey = 0;
drh42a410d2021-06-19 18:32:20884 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes);
danielk19773509a652009-07-06 18:56:13885 }
886 return rc;
887}
888
889/*
drh980b1a72006-08-16 16:42:48890** Restore the cursor to the position it was in (or as close to as possible)
larrybrbc917382023-06-07 08:40:31891** when saveCursorPosition() was called. Note that this call deletes the
drh980b1a72006-08-16 16:42:48892** saved position info stored by saveCursorPosition(), so there can be
larrybrbc917382023-06-07 08:40:31893** at most one effective restoreCursorPosition() call after each
drh980b1a72006-08-16 16:42:48894** saveCursorPosition().
drh980b1a72006-08-16 16:42:48895*/
danielk197730548662009-07-09 05:07:37896static int btreeRestoreCursorPosition(BtCursor *pCur){
drhbf700f32007-03-31 02:36:44897 int rc;
mistachkin4e2d3d42019-04-01 03:07:21898 int skipNext = 0;
dan7a2347e2016-01-07 16:43:54899 assert( cursorOwnsBtShared(pCur) );
drhfb982642007-08-30 01:19:59900 assert( pCur->eState>=CURSOR_REQUIRESEEK );
901 if( pCur->eState==CURSOR_FAULT ){
drh4c301aa2009-07-15 17:25:45902 return pCur->skipNext;
drhfb982642007-08-30 01:19:59903 }
drh980b1a72006-08-16 16:42:48904 pCur->eState = CURSOR_INVALID;
drhb336d1a2019-03-30 19:17:35905 if( sqlite3FaultSim(410) ){
906 rc = SQLITE_IOERR;
907 }else{
908 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
909 }
drh980b1a72006-08-16 16:42:48910 if( rc==SQLITE_OK ){
drh17435752007-08-16 04:30:38911 sqlite3_free(pCur->pKey);
drh980b1a72006-08-16 16:42:48912 pCur->pKey = 0;
drhbf700f32007-03-31 02:36:44913 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
drh0c873bf2019-01-28 00:42:06914 if( skipNext ) pCur->skipNext = skipNext;
drh9b47ee32013-08-20 03:13:51915 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
916 pCur->eState = CURSOR_SKIPNEXT;
917 }
drh980b1a72006-08-16 16:42:48918 }
919 return rc;
920}
921
drha3460582008-07-11 21:02:53922#define restoreCursorPosition(p) \
drhfb982642007-08-30 01:19:59923 (p->eState>=CURSOR_REQUIRESEEK ? \
danielk197730548662009-07-09 05:07:37924 btreeRestoreCursorPosition(p) : \
drh16a9b832007-05-05 18:39:25925 SQLITE_OK)
drh980b1a72006-08-16 16:42:48926
drha3460582008-07-11 21:02:53927/*
drh6848dad2014-08-22 23:33:03928** Determine whether or not a cursor has moved from the position where
929** it was last placed, or has been invalidated for any other reason.
930** Cursors can move when the row they are pointing at is deleted out
931** from under them, for example. Cursor might also move if a btree
932** is rebalanced.
drha3460582008-07-11 21:02:53933**
drh6848dad2014-08-22 23:33:03934** Calling this routine with a NULL cursor pointer returns false.
drh86dd3712014-03-25 11:00:21935**
drh6848dad2014-08-22 23:33:03936** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
937** back to where it ought to be if this routine returns true.
drha3460582008-07-11 21:02:53938*/
drh6848dad2014-08-22 23:33:03939int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
drh5ba5f5b2018-06-02 16:32:04940 assert( EIGHT_BYTE_ALIGNMENT(pCur)
941 || pCur==sqlite3BtreeFakeValidCursor() );
942 assert( offsetof(BtCursor, eState)==0 );
943 assert( sizeof(pCur->eState)==1 );
944 return CURSOR_VALID != *(u8*)pCur;
drh6848dad2014-08-22 23:33:03945}
946
947/*
drhfe0cf7a2017-08-16 19:20:20948** Return a pointer to a fake BtCursor object that will always answer
949** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
950** cursor returned must not be used with any other Btree interface.
951*/
952BtCursor *sqlite3BtreeFakeValidCursor(void){
953 static u8 fakeCursor = CURSOR_VALID;
954 assert( offsetof(BtCursor, eState)==0 );
955 return (BtCursor*)&fakeCursor;
956}
957
958/*
drh6848dad2014-08-22 23:33:03959** This routine restores a cursor back to its original position after it
960** has been moved by some outside activity (such as a btree rebalance or
larrybrbc917382023-06-07 08:40:31961** a row having been deleted out from under the cursor).
drh6848dad2014-08-22 23:33:03962**
963** On success, the *pDifferentRow parameter is false if the cursor is left
964** pointing at exactly the same row. *pDifferntRow is the row the cursor
965** was pointing to has been deleted, forcing the cursor to point to some
966** nearby row.
967**
968** This routine should only be called for a cursor that just returned
969** TRUE from sqlite3BtreeCursorHasMoved().
970*/
971int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
drha3460582008-07-11 21:02:53972 int rc;
973
drh6848dad2014-08-22 23:33:03974 assert( pCur!=0 );
975 assert( pCur->eState!=CURSOR_VALID );
drha3460582008-07-11 21:02:53976 rc = restoreCursorPosition(pCur);
977 if( rc ){
drh6848dad2014-08-22 23:33:03978 *pDifferentRow = 1;
drha3460582008-07-11 21:02:53979 return rc;
980 }
drh606a3572015-03-25 18:29:10981 if( pCur->eState!=CURSOR_VALID ){
drh6848dad2014-08-22 23:33:03982 *pDifferentRow = 1;
drha3460582008-07-11 21:02:53983 }else{
drh6848dad2014-08-22 23:33:03984 *pDifferentRow = 0;
drha3460582008-07-11 21:02:53985 }
986 return SQLITE_OK;
987}
988
drhf7854c72015-10-27 13:24:37989#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh28935362013-12-07 20:39:19990/*
drh0df57012015-08-14 15:05:55991** Provide hints to the cursor. The particular hint given (and the type
992** and number of the varargs parameters) is determined by the eHintType
993** parameter. See the definitions of the BTREE_HINT_* macros for details.
drh28935362013-12-07 20:39:19994*/
drh0df57012015-08-14 15:05:55995void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
drhf7854c72015-10-27 13:24:37996 /* Used only by system that substitute their own storage engine */
drhed369172023-04-10 18:44:00997#ifdef SQLITE_DEBUG
998 if( ALWAYS(eHintType==BTREE_HINT_RANGE) ){
999 va_list ap;
1000 Expr *pExpr;
1001 Walker w;
1002 memset(&w, 0, sizeof(w));
1003 w.xExprCallback = sqlite3CursorRangeHintExprCheck;
1004 va_start(ap, eHintType);
1005 pExpr = va_arg(ap, Expr*);
1006 w.u.aMem = va_arg(ap, Mem*);
1007 va_end(ap);
1008 assert( pExpr!=0 );
1009 assert( w.u.aMem!=0 );
1010 sqlite3WalkExpr(&w, pExpr);
1011 }
1012#endif /* SQLITE_DEBUG */
drh28935362013-12-07 20:39:191013}
drhed369172023-04-10 18:44:001014#endif /* SQLITE_ENABLE_CURSOR_HINTS */
1015
drhf7854c72015-10-27 13:24:371016
1017/*
1018** Provide flag hints to the cursor.
1019*/
1020void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
1021 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
drhc071c472025-02-22 16:44:141022 pCur->hints = (u8)x;
drhf7854c72015-10-27 13:24:371023}
1024
drh28935362013-12-07 20:39:191025
danielk1977599fcba2004-11-08 07:13:131026#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:421027/*
drha3152892007-05-05 11:48:521028** Given a page number of a regular database page, return the page
1029** number for the pointer-map page that contains the entry for the
1030** input page number.
drh5f77b2e2010-08-21 15:09:371031**
1032** Return 0 (not a valid page) for pgno==1 since there is
1033** no pointer map associated with page 1. The integrity_check logic
1034** requires that ptrmapPageno(*,1)!=1.
danielk1977afcdd022004-10-31 16:25:421035*/
danielk1977266664d2006-02-10 08:24:211036static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
danielk197789d40042008-11-17 14:20:561037 int nPagesPerMapPage;
1038 Pgno iPtrMap, ret;
drh1fee73e2007-08-29 04:00:571039 assert( sqlite3_mutex_held(pBt->mutex) );
drh5f77b2e2010-08-21 15:09:371040 if( pgno<2 ) return 0;
drhd677b3d2007-08-20 22:48:411041 nPagesPerMapPage = (pBt->usableSize/5)+1;
1042 iPtrMap = (pgno-2)/nPagesPerMapPage;
larrybrbc917382023-06-07 08:40:311043 ret = (iPtrMap*nPagesPerMapPage) + 2;
danielk1977266664d2006-02-10 08:24:211044 if( ret==PENDING_BYTE_PAGE(pBt) ){
1045 ret++;
1046 }
1047 return ret;
1048}
danielk1977a19df672004-11-03 11:37:071049
danielk1977afcdd022004-10-31 16:25:421050/*
danielk1977afcdd022004-10-31 16:25:421051** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:411052**
1053** This routine updates the pointer map entry for page number 'key'
1054** so that it maps to type 'eType' and parent page number 'pgno'.
drh98add2e2009-07-20 17:11:491055**
1056** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
1057** a no-op. If an error occurs, the appropriate error code is written
1058** into *pRC.
danielk1977afcdd022004-10-31 16:25:421059*/
drh98add2e2009-07-20 17:11:491060static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
danielk19773b8a05f2007-03-19 17:44:261061 DbPage *pDbPage; /* The pointer map page */
1062 u8 *pPtrmap; /* The pointer map data */
1063 Pgno iPtrmap; /* The pointer map page number */
1064 int offset; /* Offset in pointer map page */
drh98add2e2009-07-20 17:11:491065 int rc; /* Return code from subfunctions */
1066
1067 if( *pRC ) return;
danielk1977afcdd022004-10-31 16:25:421068
drh1fee73e2007-08-29 04:00:571069 assert( sqlite3_mutex_held(pBt->mutex) );
drh067b92b2020-06-19 15:24:121070 /* The super-journal page number must never be used as a pointer map page */
danielk1977266664d2006-02-10 08:24:211071 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
1072
danielk1977ac11ee62005-01-15 12:45:511073 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:181074 if( key==0 ){
drh98add2e2009-07-20 17:11:491075 *pRC = SQLITE_CORRUPT_BKPT;
1076 return;
danielk1977fdb7cdb2005-01-17 02:12:181077 }
danielk1977266664d2006-02-10 08:24:211078 iPtrmap = PTRMAP_PAGENO(pBt, key);
drh9584f582015-11-04 20:22:371079 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
danielk1977687566d2004-11-02 12:56:411080 if( rc!=SQLITE_OK ){
drh98add2e2009-07-20 17:11:491081 *pRC = rc;
1082 return;
danielk1977afcdd022004-10-31 16:25:421083 }
drh203b1ea2018-12-14 03:14:181084 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
1085 /* The first byte of the extra data is the MemPage.isInit byte.
1086 ** If that byte is set, it means this page is also being used
1087 ** as a btree page. */
1088 *pRC = SQLITE_CORRUPT_BKPT;
1089 goto ptrmap_exit;
1090 }
danielk19778c666b12008-07-18 09:34:571091 offset = PTRMAP_PTROFFSET(iPtrmap, key);
drhacfc72b2009-06-05 18:44:151092 if( offset<0 ){
drh98add2e2009-07-20 17:11:491093 *pRC = SQLITE_CORRUPT_BKPT;
drh4925a552009-07-07 11:39:581094 goto ptrmap_exit;
drhacfc72b2009-06-05 18:44:151095 }
drhfc243732011-05-17 15:21:561096 assert( offset <= (int)pBt->usableSize-5 );
danielk19773b8a05f2007-03-19 17:44:261097 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:421098
drh615ae552005-01-16 23:21:001099 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
drh2e89f1c2023-04-01 12:22:571100 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
drh98add2e2009-07-20 17:11:491101 *pRC= rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:441102 if( rc==SQLITE_OK ){
1103 pPtrmap[offset] = eType;
1104 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:421105 }
danielk1977afcdd022004-10-31 16:25:421106 }
1107
drh4925a552009-07-07 11:39:581108ptrmap_exit:
danielk19773b8a05f2007-03-19 17:44:261109 sqlite3PagerUnref(pDbPage);
danielk1977afcdd022004-10-31 16:25:421110}
1111
1112/*
1113** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:411114**
1115** This routine retrieves the pointer map entry for page 'key', writing
1116** the type and parent page number to *pEType and *pPgno respectively.
1117** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:421118*/
danielk1977aef0bf62005-12-30 16:28:011119static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:261120 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:421121 int iPtrmap; /* Pointer map page index */
1122 u8 *pPtrmap; /* Pointer map page data */
1123 int offset; /* Offset of entry in pointer map */
1124 int rc;
1125
drh1fee73e2007-08-29 04:00:571126 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:411127
danielk1977266664d2006-02-10 08:24:211128 iPtrmap = PTRMAP_PAGENO(pBt, key);
drh9584f582015-11-04 20:22:371129 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
danielk1977afcdd022004-10-31 16:25:421130 if( rc!=0 ){
1131 return rc;
1132 }
danielk19773b8a05f2007-03-19 17:44:261133 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:421134
danielk19778c666b12008-07-18 09:34:571135 offset = PTRMAP_PTROFFSET(iPtrmap, key);
drhfc243732011-05-17 15:21:561136 if( offset<0 ){
1137 sqlite3PagerUnref(pDbPage);
1138 return SQLITE_CORRUPT_BKPT;
1139 }
1140 assert( offset <= (int)pBt->usableSize-5 );
drh43617e92006-03-06 20:55:461141 assert( pEType!=0 );
1142 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:411143 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:421144
danielk19773b8a05f2007-03-19 17:44:261145 sqlite3PagerUnref(pDbPage);
drhcc97ca42017-06-07 22:32:591146 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
danielk1977afcdd022004-10-31 16:25:421147 return SQLITE_OK;
1148}
1149
danielk197785d90ca2008-07-19 14:25:151150#else /* if defined SQLITE_OMIT_AUTOVACUUM */
drh98add2e2009-07-20 17:11:491151 #define ptrmapPut(w,x,y,z,rc)
danielk197785d90ca2008-07-19 14:25:151152 #define ptrmapGet(w,x,y,z) SQLITE_OK
drh0f1bf4c2019-01-13 20:17:211153 #define ptrmapPutOvflPtr(x, y, z, rc)
danielk197785d90ca2008-07-19 14:25:151154#endif
danielk1977afcdd022004-10-31 16:25:421155
drh0d316a42002-08-11 20:10:471156/*
drh271efa52004-05-30 19:19:051157** Given a btree page and a cell index (0 means the first cell on
1158** the page, 1 means the second cell, and so forth) return a pointer
1159** to the cell content.
1160**
drhf44890a2015-06-27 03:58:151161** findCellPastPtr() does the same except it skips past the initial
1162** 4-byte child pointer found on interior pages, if there is one.
1163**
drh271efa52004-05-30 19:19:051164** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:201165*/
drh1688c862008-07-18 02:44:171166#define findCell(P,I) \
drh329428e2015-06-30 13:28:181167 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
drhf44890a2015-06-27 03:58:151168#define findCellPastPtr(P,I) \
drh329428e2015-06-30 13:28:181169 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
drh68f2a572011-06-03 17:50:491170
drh43605152004-05-29 21:46:491171
1172/*
drh5fa60512015-06-19 17:19:341173** This is common tail processing for btreeParseCellPtr() and
1174** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
1175** on a single B-tree page. Make necessary adjustments to the CellInfo
1176** structure.
drh43605152004-05-29 21:46:491177*/
drh5fa60512015-06-19 17:19:341178static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
1179 MemPage *pPage, /* Page containing the cell */
1180 u8 *pCell, /* Pointer to the cell text. */
1181 CellInfo *pInfo /* Fill in this structure */
1182){
1183 /* If the payload will not fit completely on the local page, we have
1184 ** to decide how much to store locally and how much to spill onto
1185 ** overflow pages. The strategy is to minimize the amount of unused
1186 ** space on overflow pages while keeping the amount of local storage
1187 ** in between minLocal and maxLocal.
1188 **
1189 ** Warning: changing the way overflow payload is distributed in any
1190 ** way will result in an incompatible file format.
1191 */
1192 int minLocal; /* Minimum amount of payload held locally */
1193 int maxLocal; /* Maximum amount of payload held locally */
1194 int surplus; /* Overflow payload available for local storage */
1195
1196 minLocal = pPage->minLocal;
1197 maxLocal = pPage->maxLocal;
1198 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
1199 testcase( surplus==maxLocal );
1200 testcase( surplus==maxLocal+1 );
1201 if( surplus <= maxLocal ){
1202 pInfo->nLocal = (u16)surplus;
1203 }else{
1204 pInfo->nLocal = (u16)minLocal;
drh43605152004-05-29 21:46:491205 }
drh45ac1c72015-12-18 03:59:161206 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
drh43605152004-05-29 21:46:491207}
1208
1209/*
danebbf3682020-12-09 16:32:111210** Given a record with nPayload bytes of payload stored within btree
1211** page pPage, return the number of bytes of payload stored locally.
1212*/
dan59964b42020-12-14 15:25:141213static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
danebbf3682020-12-09 16:32:111214 int maxLocal; /* Maximum amount of payload held locally */
1215 maxLocal = pPage->maxLocal;
drhc071c472025-02-22 16:44:141216 assert( nPayload>=0 );
danebbf3682020-12-09 16:32:111217 if( nPayload<=maxLocal ){
drhc071c472025-02-22 16:44:141218 return (int)nPayload;
danebbf3682020-12-09 16:32:111219 }else{
1220 int minLocal; /* Minimum amount of payload held locally */
1221 int surplus; /* Overflow payload available for local storage */
1222 minLocal = pPage->minLocal;
drhc071c472025-02-22 16:44:141223 surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
1224 return (surplus <= maxLocal) ? surplus : minLocal;
danebbf3682020-12-09 16:32:111225 }
1226}
1227
1228/*
drh5fa60512015-06-19 17:19:341229** The following routines are implementations of the MemPage.xParseCell()
1230** method.
danielk19771cc5ed82007-05-16 17:28:431231**
drh5fa60512015-06-19 17:19:341232** Parse a cell content block and fill in the CellInfo structure.
1233**
1234** btreeParseCellPtr() => table btree leaf nodes
1235** btreeParseCellNoPayload() => table btree internal nodes
1236** btreeParseCellPtrIndex() => index btree nodes
1237**
1238** There is also a wrapper function btreeParseCell() that works for
1239** all MemPage types and that references the cell by index rather than
1240** by pointer.
drh43605152004-05-29 21:46:491241*/
drh5fa60512015-06-19 17:19:341242static void btreeParseCellPtrNoPayload(
1243 MemPage *pPage, /* Page containing the cell */
1244 u8 *pCell, /* Pointer to the cell text. */
1245 CellInfo *pInfo /* Fill in this structure */
1246){
1247 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1248 assert( pPage->leaf==0 );
drh5fa60512015-06-19 17:19:341249 assert( pPage->childPtrSize==4 );
drh94a31152015-07-01 04:08:401250#ifndef SQLITE_DEBUG
1251 UNUSED_PARAMETER(pPage);
1252#endif
drh5fa60512015-06-19 17:19:341253 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
1254 pInfo->nPayload = 0;
1255 pInfo->nLocal = 0;
drh5fa60512015-06-19 17:19:341256 pInfo->pPayload = 0;
1257 return;
1258}
danielk197730548662009-07-09 05:07:371259static void btreeParseCellPtr(
drh3aac2dd2004-04-26 14:10:201260 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:491261 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:561262 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:201263){
drh3e28ff52014-09-24 00:59:081264 u8 *pIter; /* For scanning through pCell */
drh271efa52004-05-30 19:19:051265 u32 nPayload; /* Number of bytes of cell payload */
drh56cb04e2015-06-19 18:24:371266 u64 iKey; /* Extracted Key value */
drh43605152004-05-29 21:46:491267
drh1fee73e2007-08-29 04:00:571268 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhab01f612004-05-22 02:55:231269 assert( pPage->leaf==0 || pPage->leaf==1 );
drh5fa60512015-06-19 17:19:341270 assert( pPage->intKeyLeaf );
1271 assert( pPage->childPtrSize==0 );
drh56cb04e2015-06-19 18:24:371272 pIter = pCell;
1273
1274 /* The next block of code is equivalent to:
1275 **
1276 ** pIter += getVarint32(pIter, nPayload);
1277 **
1278 ** The code is inlined to avoid a function call.
1279 */
1280 nPayload = *pIter;
1281 if( nPayload>=0x80 ){
drheeab2c62015-06-19 20:08:391282 u8 *pEnd = &pIter[8];
drh56cb04e2015-06-19 18:24:371283 nPayload &= 0x7f;
1284 do{
1285 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1286 }while( (*pIter)>=0x80 && pIter<pEnd );
drh6f11bef2004-05-13 01:12:561287 }
drh56cb04e2015-06-19 18:24:371288 pIter++;
1289
1290 /* The next block of code is equivalent to:
1291 **
1292 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
1293 **
drh29bbc2b2022-01-02 16:48:001294 ** The code is inlined and the loop is unrolled for performance.
1295 ** This routine is a high-runner.
drh56cb04e2015-06-19 18:24:371296 */
1297 iKey = *pIter;
1298 if( iKey>=0x80 ){
drh29bbc2b2022-01-02 16:48:001299 u8 x;
drh485a92c2023-02-28 12:31:401300 iKey = (iKey<<7) ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001301 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401302 iKey = (iKey<<7) ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001303 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401304 iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001305 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401306 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001307 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401308 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001309 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401310 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001311 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401312 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
drh29bbc2b2022-01-02 16:48:001313 if( x>=0x80 ){
drh485a92c2023-02-28 12:31:401314 iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter);
drh29bbc2b2022-01-02 16:48:001315 }
1316 }
1317 }
1318 }
1319 }
drh485a92c2023-02-28 12:31:401320 }else{
1321 iKey ^= 0x204000;
drh56cb04e2015-06-19 18:24:371322 }
drh485a92c2023-02-28 12:31:401323 }else{
1324 iKey ^= 0x4000;
drh56cb04e2015-06-19 18:24:371325 }
1326 }
1327 pIter++;
1328
1329 pInfo->nKey = *(i64*)&iKey;
drh72365832007-03-06 15:53:441330 pInfo->nPayload = nPayload;
drhab1cc582014-09-23 21:25:191331 pInfo->pPayload = pIter;
drh0a45c272009-07-08 01:49:111332 testcase( nPayload==pPage->maxLocal );
mistachkin2b5fbb22021-12-31 18:26:501333 testcase( nPayload==(u32)pPage->maxLocal+1 );
drhc071c472025-02-22 16:44:141334 assert( nPayload>=0 );
1335 assert( pPage->maxLocal <= BT_MAX_LOCAL );
drhab1cc582014-09-23 21:25:191336 if( nPayload<=pPage->maxLocal ){
drh271efa52004-05-30 19:19:051337 /* This is the (easy) common case where the entire payload fits
1338 ** on the local page. No overflow is required.
1339 */
drhc071c472025-02-22 16:44:141340 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
drhab1cc582014-09-23 21:25:191341 if( pInfo->nSize<4 ) pInfo->nSize = 4;
drhf49661a2008-12-10 16:45:501342 pInfo->nLocal = (u16)nPayload;
drh6f11bef2004-05-13 01:12:561343 }else{
drh5fa60512015-06-19 17:19:341344 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
drh6f11bef2004-05-13 01:12:561345 }
drh3aac2dd2004-04-26 14:10:201346}
drh5fa60512015-06-19 17:19:341347static void btreeParseCellPtrIndex(
1348 MemPage *pPage, /* Page containing the cell */
1349 u8 *pCell, /* Pointer to the cell text. */
1350 CellInfo *pInfo /* Fill in this structure */
1351){
1352 u8 *pIter; /* For scanning through pCell */
1353 u32 nPayload; /* Number of bytes of cell payload */
drh3aac2dd2004-04-26 14:10:201354
drh5fa60512015-06-19 17:19:341355 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1356 assert( pPage->leaf==0 || pPage->leaf==1 );
1357 assert( pPage->intKeyLeaf==0 );
drh5fa60512015-06-19 17:19:341358 pIter = pCell + pPage->childPtrSize;
1359 nPayload = *pIter;
1360 if( nPayload>=0x80 ){
drheeab2c62015-06-19 20:08:391361 u8 *pEnd = &pIter[8];
drh5fa60512015-06-19 17:19:341362 nPayload &= 0x7f;
1363 do{
1364 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1365 }while( *(pIter)>=0x80 && pIter<pEnd );
1366 }
1367 pIter++;
1368 pInfo->nKey = nPayload;
1369 pInfo->nPayload = nPayload;
1370 pInfo->pPayload = pIter;
1371 testcase( nPayload==pPage->maxLocal );
mistachkin2b5fbb22021-12-31 18:26:501372 testcase( nPayload==(u32)pPage->maxLocal+1 );
drhc071c472025-02-22 16:44:141373 assert( nPayload>=0 );
1374 assert( pPage->maxLocal <= BT_MAX_LOCAL );
drh5fa60512015-06-19 17:19:341375 if( nPayload<=pPage->maxLocal ){
1376 /* This is the (easy) common case where the entire payload fits
1377 ** on the local page. No overflow is required.
1378 */
drhc071c472025-02-22 16:44:141379 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
drh5fa60512015-06-19 17:19:341380 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1381 pInfo->nLocal = (u16)nPayload;
drh5fa60512015-06-19 17:19:341382 }else{
1383 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
drh3aac2dd2004-04-26 14:10:201384 }
1385}
danielk197730548662009-07-09 05:07:371386static void btreeParseCell(
drh43605152004-05-29 21:46:491387 MemPage *pPage, /* Page containing the cell */
1388 int iCell, /* The cell index. First cell is 0 */
1389 CellInfo *pInfo /* Fill in this structure */
1390){
drh5fa60512015-06-19 17:19:341391 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
drh43605152004-05-29 21:46:491392}
drh3aac2dd2004-04-26 14:10:201393
1394/*
drh5fa60512015-06-19 17:19:341395** The following routines are implementations of the MemPage.xCellSize
1396** method.
1397**
drh43605152004-05-29 21:46:491398** Compute the total number of bytes that a Cell needs in the cell
1399** data area of the btree-page. The return number includes the cell
1400** data header and the local payload, but not any overflow page or
1401** the space used by the cell pointer.
drh25ada072015-06-19 15:07:141402**
drh5fa60512015-06-19 17:19:341403** cellSizePtrNoPayload() => table internal nodes
drh19ae01b2022-02-23 22:56:101404** cellSizePtrTableLeaf() => table leaf nodes
drh2d782902023-02-25 16:03:281405** cellSizePtr() => index internal nodes
1406** cellSizeIdxLeaf() => index leaf nodes
drh3b7511c2001-05-26 13:15:441407*/
danielk1977ae5558b2009-04-29 11:31:471408static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
drh2d782902023-02-25 16:03:281409 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
drh3f387402014-09-24 01:23:001410 u8 *pEnd; /* End mark for a varint */
1411 u32 nSize; /* Size value to return */
danielk1977ae5558b2009-04-29 11:31:471412
1413#ifdef SQLITE_DEBUG
1414 /* The value returned by this function should always be the same as
1415 ** the (CellInfo.nSize) value found by doing a full parse of the
1416 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1417 ** this function verifies that this invariant is not violated. */
1418 CellInfo debuginfo;
drh5fa60512015-06-19 17:19:341419 pPage->xParseCell(pPage, pCell, &debuginfo);
danielk1977ae5558b2009-04-29 11:31:471420#endif
1421
drh2d782902023-02-25 16:03:281422 assert( pPage->childPtrSize==4 );
1423 nSize = *pIter;
1424 if( nSize>=0x80 ){
1425 pEnd = &pIter[8];
1426 nSize &= 0x7f;
1427 do{
1428 nSize = (nSize<<7) | (*++pIter & 0x7f);
1429 }while( *(pIter)>=0x80 && pIter<pEnd );
1430 }
1431 pIter++;
1432 testcase( nSize==pPage->maxLocal );
1433 testcase( nSize==(u32)pPage->maxLocal+1 );
1434 if( nSize<=pPage->maxLocal ){
1435 nSize += (u32)(pIter - pCell);
1436 assert( nSize>4 );
1437 }else{
1438 int minLocal = pPage->minLocal;
1439 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1440 testcase( nSize==pPage->maxLocal );
1441 testcase( nSize==(u32)pPage->maxLocal+1 );
1442 if( nSize>pPage->maxLocal ){
1443 nSize = minLocal;
1444 }
1445 nSize += 4 + (u16)(pIter - pCell);
1446 }
1447 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1448 return (u16)nSize;
1449}
1450static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
1451 u8 *pIter = pCell; /* For looping over bytes of pCell */
1452 u8 *pEnd; /* End mark for a varint */
1453 u32 nSize; /* Size value to return */
1454
1455#ifdef SQLITE_DEBUG
1456 /* The value returned by this function should always be the same as
1457 ** the (CellInfo.nSize) value found by doing a full parse of the
1458 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1459 ** this function verifies that this invariant is not violated. */
1460 CellInfo debuginfo;
1461 pPage->xParseCell(pPage, pCell, &debuginfo);
1462#endif
1463
1464 assert( pPage->childPtrSize==0 );
drh3e28ff52014-09-24 00:59:081465 nSize = *pIter;
1466 if( nSize>=0x80 ){
drheeab2c62015-06-19 20:08:391467 pEnd = &pIter[8];
drh3e28ff52014-09-24 00:59:081468 nSize &= 0x7f;
1469 do{
1470 nSize = (nSize<<7) | (*++pIter & 0x7f);
1471 }while( *(pIter)>=0x80 && pIter<pEnd );
1472 }
1473 pIter++;
drh0a45c272009-07-08 01:49:111474 testcase( nSize==pPage->maxLocal );
mistachkin2b5fbb22021-12-31 18:26:501475 testcase( nSize==(u32)pPage->maxLocal+1 );
drh3e28ff52014-09-24 00:59:081476 if( nSize<=pPage->maxLocal ){
1477 nSize += (u32)(pIter - pCell);
1478 if( nSize<4 ) nSize = 4;
1479 }else{
danielk1977ae5558b2009-04-29 11:31:471480 int minLocal = pPage->minLocal;
1481 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
drh0a45c272009-07-08 01:49:111482 testcase( nSize==pPage->maxLocal );
mistachkin2b5fbb22021-12-31 18:26:501483 testcase( nSize==(u32)pPage->maxLocal+1 );
danielk1977ae5558b2009-04-29 11:31:471484 if( nSize>pPage->maxLocal ){
1485 nSize = minLocal;
1486 }
drh3e28ff52014-09-24 00:59:081487 nSize += 4 + (u16)(pIter - pCell);
danielk1977ae5558b2009-04-29 11:31:471488 }
drhdc41d602014-09-22 19:51:351489 assert( nSize==debuginfo.nSize || CORRUPT_DB );
shane60a4b532009-05-06 18:57:091490 return (u16)nSize;
danielk1977ae5558b2009-04-29 11:31:471491}
drh25ada072015-06-19 15:07:141492static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
1493 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1494 u8 *pEnd; /* End mark for a varint */
1495
1496#ifdef SQLITE_DEBUG
1497 /* The value returned by this function should always be the same as
1498 ** the (CellInfo.nSize) value found by doing a full parse of the
1499 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1500 ** this function verifies that this invariant is not violated. */
1501 CellInfo debuginfo;
drh5fa60512015-06-19 17:19:341502 pPage->xParseCell(pPage, pCell, &debuginfo);
drh94a31152015-07-01 04:08:401503#else
1504 UNUSED_PARAMETER(pPage);
drh25ada072015-06-19 15:07:141505#endif
1506
1507 assert( pPage->childPtrSize==4 );
1508 pEnd = pIter + 9;
1509 while( (*pIter++)&0x80 && pIter<pEnd );
1510 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
1511 return (u16)(pIter - pCell);
1512}
drh19ae01b2022-02-23 22:56:101513static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
1514 u8 *pIter = pCell; /* For looping over bytes of pCell */
1515 u8 *pEnd; /* End mark for a varint */
1516 u32 nSize; /* Size value to return */
1517
1518#ifdef SQLITE_DEBUG
1519 /* The value returned by this function should always be the same as
1520 ** the (CellInfo.nSize) value found by doing a full parse of the
1521 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1522 ** this function verifies that this invariant is not violated. */
1523 CellInfo debuginfo;
1524 pPage->xParseCell(pPage, pCell, &debuginfo);
1525#endif
1526
1527 nSize = *pIter;
1528 if( nSize>=0x80 ){
1529 pEnd = &pIter[8];
1530 nSize &= 0x7f;
1531 do{
1532 nSize = (nSize<<7) | (*++pIter & 0x7f);
1533 }while( *(pIter)>=0x80 && pIter<pEnd );
1534 }
1535 pIter++;
larrybrbc917382023-06-07 08:40:311536 /* pIter now points at the 64-bit integer key value, a variable length
drh19ae01b2022-02-23 22:56:101537 ** integer. The following block moves pIter to point at the first byte
1538 ** past the end of the key value. */
1539 if( (*pIter++)&0x80
1540 && (*pIter++)&0x80
1541 && (*pIter++)&0x80
1542 && (*pIter++)&0x80
1543 && (*pIter++)&0x80
1544 && (*pIter++)&0x80
1545 && (*pIter++)&0x80
1546 && (*pIter++)&0x80 ){ pIter++; }
1547 testcase( nSize==pPage->maxLocal );
1548 testcase( nSize==(u32)pPage->maxLocal+1 );
1549 if( nSize<=pPage->maxLocal ){
1550 nSize += (u32)(pIter - pCell);
1551 if( nSize<4 ) nSize = 4;
1552 }else{
1553 int minLocal = pPage->minLocal;
1554 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1555 testcase( nSize==pPage->maxLocal );
1556 testcase( nSize==(u32)pPage->maxLocal+1 );
1557 if( nSize>pPage->maxLocal ){
1558 nSize = minLocal;
1559 }
1560 nSize += 4 + (u16)(pIter - pCell);
1561 }
1562 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1563 return (u16)nSize;
1564}
drh25ada072015-06-19 15:07:141565
drh0ee3dbe2009-10-16 15:05:181566
1567#ifdef SQLITE_DEBUG
1568/* This variation on cellSizePtr() is used inside of assert() statements
1569** only. */
drha9121e42008-02-19 14:59:351570static u16 cellSize(MemPage *pPage, int iCell){
drh25ada072015-06-19 15:07:141571 return pPage->xCellSize(pPage, findCell(pPage, iCell));
drh43605152004-05-29 21:46:491572}
danielk1977bc6ada42004-06-30 08:20:161573#endif
drh3b7511c2001-05-26 13:15:441574
danielk197779a40da2005-01-16 08:00:011575#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:441576/*
drh0f1bf4c2019-01-13 20:17:211577** The cell pCell is currently part of page pSrc but will ultimately be part
drh3b4cb712022-03-01 19:19:201578** of pPage. (pSrc and pPage are often the same.) If pCell contains a
drh0f1bf4c2019-01-13 20:17:211579** pointer to an overflow page, insert an entry into the pointer-map for
1580** the overflow page that will be valid after pCell has been moved to pPage.
danielk1977ac11ee62005-01-15 12:45:511581*/
drh0f1bf4c2019-01-13 20:17:211582static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
drhfa67c3c2008-07-11 02:21:401583 CellInfo info;
drh98add2e2009-07-20 17:11:491584 if( *pRC ) return;
drhfa67c3c2008-07-11 02:21:401585 assert( pCell!=0 );
drh5fa60512015-06-19 17:19:341586 pPage->xParseCell(pPage, pCell, &info);
drh45ac1c72015-12-18 03:59:161587 if( info.nLocal<info.nPayload ){
drhe7acce62018-12-14 16:00:381588 Pgno ovfl;
drhbc6d9492023-07-13 14:49:391589 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
drh0f1bf4c2019-01-13 20:17:211590 testcase( pSrc!=pPage );
drhe7acce62018-12-14 16:00:381591 *pRC = SQLITE_CORRUPT_BKPT;
1592 return;
1593 }
1594 ovfl = get4byte(&pCell[info.nSize-4]);
drh98add2e2009-07-20 17:11:491595 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
danielk1977ac11ee62005-01-15 12:45:511596 }
danielk1977ac11ee62005-01-15 12:45:511597}
danielk197779a40da2005-01-16 08:00:011598#endif
1599
danielk1977ac11ee62005-01-15 12:45:511600
drhda200cc2004-05-09 11:51:381601/*
dane6d065a2017-02-24 19:58:221602** Defragment the page given. This routine reorganizes cells within the
1603** page so that there are no free-blocks on the free-block list.
1604**
1605** Parameter nMaxFrag is the maximum amount of fragmented space that may be
1606** present in the page after this routine returns.
drhfdab0262014-11-20 15:30:501607**
1608** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
1609** b-tree page so that there are no freeblocks or fragment bytes, all
1610** unused bytes are contained in the unallocated space region, and all
1611** cells are packed tightly at the end of the page.
drh365d68f2001-05-11 11:02:461612*/
dane6d065a2017-02-24 19:58:221613static int defragmentPage(MemPage *pPage, int nMaxFrag){
drh43605152004-05-29 21:46:491614 int i; /* Loop counter */
peter.d.reid60ec9142014-09-06 16:39:461615 int pc; /* Address of the i-th cell */
drh43605152004-05-29 21:46:491616 int hdr; /* Offset to the page header */
1617 int size; /* Size of a cell */
1618 int usableSize; /* Number of usable bytes on a page */
1619 int cellOffset; /* Offset to the cell pointer array */
drh281b21d2008-08-22 12:57:081620 int cbrk; /* Offset to the cell content area */
drh43605152004-05-29 21:46:491621 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:441622 unsigned char *data; /* The page data */
1623 unsigned char *temp; /* Temp area for cell content */
drh588400b2014-09-27 05:00:251624 unsigned char *src; /* Source of content */
drh17146622009-07-07 17:38:381625 int iCellFirst; /* First allowable cell index */
1626 int iCellLast; /* Last possible cell index */
dan7f65b7a2021-04-10 20:27:061627 int iCellStart; /* First cell offset in input */
drh17146622009-07-07 17:38:381628
danielk19773b8a05f2007-03-19 17:44:261629 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:101630 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:351631 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:491632 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:571633 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh1dd13032022-07-26 15:41:341634 data = pPage->aData;
drh9e572e62004-04-23 23:43:101635 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:491636 cellOffset = pPage->cellOffset;
1637 nCell = pPage->nCell;
drh45616c72019-02-28 13:21:361638 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
dane6d065a2017-02-24 19:58:221639 iCellFirst = cellOffset + 2*nCell;
dan30741eb2017-03-03 20:02:531640 usableSize = pPage->pBt->usableSize;
dane6d065a2017-02-24 19:58:221641
1642 /* This block handles pages with two or fewer free blocks and nMaxFrag
1643 ** or fewer fragmented bytes. In this case it is faster to move the
1644 ** two (or one) blocks of cells using memmove() and add the required
larrybrbc917382023-06-07 08:40:311645 ** offsets to each pointer in the cell-pointer array than it is to
dane6d065a2017-02-24 19:58:221646 ** reconstruct the entire page. */
1647 if( (int)data[hdr+7]<=nMaxFrag ){
1648 int iFree = get2byte(&data[hdr+1]);
drh119e1ff2019-03-30 18:39:131649 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
dane6d065a2017-02-24 19:58:221650 if( iFree ){
1651 int iFree2 = get2byte(&data[iFree]);
drh5881dfe2018-12-13 03:36:131652 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
dane6d065a2017-02-24 19:58:221653 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
1654 u8 *pEnd = &data[cellOffset + nCell*2];
1655 u8 *pAddr;
1656 int sz2 = 0;
1657 int sz = get2byte(&data[iFree+2]);
1658 int top = get2byte(&data[hdr+5]);
drh4b9e7362020-02-18 23:58:581659 if( top>=iFree ){
daneebf2f52017-11-18 17:30:081660 return SQLITE_CORRUPT_PAGE(pPage);
drh4e6cec12017-09-28 13:47:351661 }
dane6d065a2017-02-24 19:58:221662 if( iFree2 ){
drh5881dfe2018-12-13 03:36:131663 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
dane6d065a2017-02-24 19:58:221664 sz2 = get2byte(&data[iFree2+2]);
drh5881dfe2018-12-13 03:36:131665 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
dane6d065a2017-02-24 19:58:221666 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
1667 sz += sz2;
drhbcdb4cc2022-09-18 17:59:281668 }else if( iFree+sz>usableSize ){
dandcc427c2019-03-21 21:18:361669 return SQLITE_CORRUPT_PAGE(pPage);
dane6d065a2017-02-24 19:58:221670 }
dandcc427c2019-03-21 21:18:361671
dane6d065a2017-02-24 19:58:221672 cbrk = top+sz;
dan30741eb2017-03-03 20:02:531673 assert( cbrk+(iFree-top) <= usableSize );
dane6d065a2017-02-24 19:58:221674 memmove(&data[cbrk], &data[top], iFree-top);
1675 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
1676 pc = get2byte(pAddr);
1677 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
1678 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
1679 }
1680 goto defragment_out;
1681 }
1682 }
1683 }
1684
drh281b21d2008-08-22 12:57:081685 cbrk = usableSize;
drh17146622009-07-07 17:38:381686 iCellLast = usableSize - 4;
dan7f65b7a2021-04-10 20:27:061687 iCellStart = get2byte(&data[hdr+5]);
drhf15b77b2022-07-07 21:04:031688 if( nCell>0 ){
1689 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
drh75201162023-06-09 15:54:181690 memcpy(temp, data, usableSize);
drhf15b77b2022-07-07 21:04:031691 src = temp;
1692 for(i=0; i<nCell; i++){
1693 u8 *pAddr; /* The i-th cell pointer */
1694 pAddr = &data[cellOffset + i*2];
1695 pc = get2byte(pAddr);
1696 testcase( pc==iCellFirst );
1697 testcase( pc==iCellLast );
1698 /* These conditions have already been verified in btreeInitPage()
1699 ** if PRAGMA cell_size_check=ON.
1700 */
drh20e09ba2023-02-25 15:34:091701 if( pc>iCellLast ){
drhf15b77b2022-07-07 21:04:031702 return SQLITE_CORRUPT_PAGE(pPage);
1703 }
drh20e09ba2023-02-25 15:34:091704 assert( pc>=0 && pc<=iCellLast );
drhf15b77b2022-07-07 21:04:031705 size = pPage->xCellSize(pPage, &src[pc]);
1706 cbrk -= size;
1707 if( cbrk<iCellStart || pc+size>usableSize ){
1708 return SQLITE_CORRUPT_PAGE(pPage);
1709 }
1710 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
1711 testcase( cbrk+size==usableSize );
1712 testcase( pc+size==usableSize );
1713 put2byte(pAddr, cbrk);
1714 memcpy(&data[cbrk], &src[pc], size);
shane0af3f892008-11-12 04:55:341715 }
drh2af926b2001-05-15 00:39:251716 }
dane6d065a2017-02-24 19:58:221717 data[hdr+7] = 0;
dane6d065a2017-02-24 19:58:221718
drhf15b77b2022-07-07 21:04:031719defragment_out:
drhb0ea9432019-02-09 21:06:401720 assert( pPage->nFree>=0 );
dan3b2ede12017-02-25 16:24:021721 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
daneebf2f52017-11-18 17:30:081722 return SQLITE_CORRUPT_PAGE(pPage);
dan3b2ede12017-02-25 16:24:021723 }
drh17146622009-07-07 17:38:381724 assert( cbrk>=iCellFirst );
drh281b21d2008-08-22 12:57:081725 put2byte(&data[hdr+5], cbrk);
drh43605152004-05-29 21:46:491726 data[hdr+1] = 0;
1727 data[hdr+2] = 0;
drh17146622009-07-07 17:38:381728 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
drhc5053fb2008-11-27 02:22:101729 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
shane0af3f892008-11-12 04:55:341730 return SQLITE_OK;
drh365d68f2001-05-11 11:02:461731}
1732
drha059ad02001-04-17 20:09:111733/*
dan8e9ba0c2014-10-14 17:27:041734** Search the free-list on page pPg for space to store a cell nByte bytes in
1735** size. If one can be found, return a pointer to the space and remove it
1736** from the free-list.
1737**
1738** If no suitable space can be found on the free-list, return NULL.
1739**
drhba0f9992014-10-30 20:48:441740** This function may detect corruption within pPg. If corruption is
1741** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
dan61e94c92014-10-27 08:02:161742**
drhb7580e82015-06-25 18:36:131743** Slots on the free list that are between 1 and 3 bytes larger than nByte
1744** will be ignored if adding the extra space to the fragmentation count
1745** causes the fragmentation count to exceed 60.
dan8e9ba0c2014-10-14 17:27:041746*/
drhb7580e82015-06-25 18:36:131747static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
drh298f45c2019-02-08 22:34:591748 const int hdr = pPg->hdrOffset; /* Offset to page header */
1749 u8 * const aData = pPg->aData; /* Page data */
1750 int iAddr = hdr + 1; /* Address of ptr to pc */
drh009a48e2022-02-23 18:23:151751 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
1752 int pc = get2byte(pTmp); /* Address of a free slot */
drh298f45c2019-02-08 22:34:591753 int x; /* Excess size of the slot */
1754 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
1755 int size; /* Size of the free slot */
dan8e9ba0c2014-10-14 17:27:041756
drhb7580e82015-06-25 18:36:131757 assert( pc>0 );
drh298f45c2019-02-08 22:34:591758 while( pc<=maxPC ){
drh113762a2014-11-19 16:36:251759 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
1760 ** freeblock form a big-endian integer which is the size of the freeblock
1761 ** in bytes, including the 4-byte header. */
drh009a48e2022-02-23 18:23:151762 pTmp = &aData[pc+2];
1763 size = get2byte(pTmp);
drhb7580e82015-06-25 18:36:131764 if( (x = size - nByte)>=0 ){
dan8e9ba0c2014-10-14 17:27:041765 testcase( x==4 );
1766 testcase( x==3 );
drh298f45c2019-02-08 22:34:591767 if( x<4 ){
drhfdab0262014-11-20 15:30:501768 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
1769 ** number of bytes in fragments may not exceed 60. */
drhb7580e82015-06-25 18:36:131770 if( aData[hdr+7]>57 ) return 0;
1771
dan8e9ba0c2014-10-14 17:27:041772 /* Remove the slot from the free-list. Update the number of
1773 ** fragmented bytes within the page. */
1774 memcpy(&aData[iAddr], &aData[pc], 2);
1775 aData[hdr+7] += (u8)x;
dan1942d1f2022-04-18 15:56:581776 return &aData[pc];
drh298f45c2019-02-08 22:34:591777 }else if( x+pc > maxPC ){
1778 /* This slot extends off the end of the usable part of the page */
1779 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1780 return 0;
dan8e9ba0c2014-10-14 17:27:041781 }else{
1782 /* The slot remains on the free-list. Reduce its size to account
drh298f45c2019-02-08 22:34:591783 ** for the portion used by the new allocation. */
dan8e9ba0c2014-10-14 17:27:041784 put2byte(&aData[pc+2], x);
1785 }
1786 return &aData[pc + x];
1787 }
drhb7580e82015-06-25 18:36:131788 iAddr = pc;
drh009a48e2022-02-23 18:23:151789 pTmp = &aData[pc];
1790 pc = get2byte(pTmp);
drhebaa9472022-07-07 20:29:491791 if( pc<=iAddr ){
drh298f45c2019-02-08 22:34:591792 if( pc ){
drhebaa9472022-07-07 20:29:491793 /* The next slot in the chain comes before the current slot */
drh298f45c2019-02-08 22:34:591794 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1795 }
1796 return 0;
1797 }
drh87d63c92017-08-23 23:09:031798 }
drh298f45c2019-02-08 22:34:591799 if( pc>maxPC+nByte-4 ){
1800 /* The free slot chain extends off the end of the page */
daneebf2f52017-11-18 17:30:081801 *pRc = SQLITE_CORRUPT_PAGE(pPg);
drh87d63c92017-08-23 23:09:031802 }
dan8e9ba0c2014-10-14 17:27:041803 return 0;
1804}
1805
1806/*
danielk19776011a752009-04-01 16:25:321807** Allocate nByte bytes of space from within the B-Tree page passed
drh0a45c272009-07-08 01:49:111808** as the first argument. Write into *pIdx the index into pPage->aData[]
1809** of the first byte of allocated space. Return either SQLITE_OK or
1810** an error code (usually SQLITE_CORRUPT).
drhbd03cae2001-06-02 02:40:571811**
drh0a45c272009-07-08 01:49:111812** The caller guarantees that there is sufficient space to make the
1813** allocation. This routine might need to defragment in order to bring
1814** all the space together, however. This routine will avoid using
1815** the first two bytes past the cell pointer area since presumably this
1816** allocation is being made in order to insert a new cell, so we will
1817** also end up needing a new cell pointer.
drh7e3b0a02001-04-28 16:52:401818*/
drh34ceb7e2023-04-07 14:33:331819static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
danielk19776011a752009-04-01 16:25:321820 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1821 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
drh0a45c272009-07-08 01:49:111822 int top; /* First byte of cell content area */
drhfefa0942014-11-05 21:21:081823 int rc = SQLITE_OK; /* Integer return code */
drh009a48e2022-02-23 18:23:151824 u8 *pTmp; /* Temp ptr into data[] */
drh0a45c272009-07-08 01:49:111825 int gap; /* First byte of gap between cell pointers and cell content */
larrybrbc917382023-06-07 08:40:311826
danielk19773b8a05f2007-03-19 17:44:261827 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:101828 assert( pPage->pBt );
drh1fee73e2007-08-29 04:00:571829 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhfa67c3c2008-07-11 02:21:401830 assert( nByte>=0 ); /* Minimum cell size is 4 */
1831 assert( pPage->nFree>=nByte );
1832 assert( pPage->nOverflow==0 );
mistachkina95d8ca2014-10-27 19:42:021833 assert( nByte < (int)(pPage->pBt->usableSize-8) );
drh43605152004-05-29 21:46:491834
drh0a45c272009-07-08 01:49:111835 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1836 gap = pPage->cellOffset + 2*pPage->nCell;
drh75b31dc2014-08-20 00:54:461837 assert( gap<=65536 );
drhfdab0262014-11-20 15:30:501838 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
1839 ** and the reserved space is zero (the usual value for reserved space)
1840 ** then the cell content offset of an empty page wants to be 65536.
1841 ** However, that integer is too large to be stored in a 2-byte unsigned
1842 ** integer, so a value of 0 is used in its place. */
drh009a48e2022-02-23 18:23:151843 pTmp = &data[hdr+5];
1844 top = get2byte(pTmp);
drhded340e2015-06-25 15:04:561845 if( gap>top ){
drh291508f2019-05-08 04:33:171846 if( top==0 && pPage->pBt->usableSize==65536 ){
drhded340e2015-06-25 15:04:561847 top = 65536;
1848 }else{
daneebf2f52017-11-18 17:30:081849 return SQLITE_CORRUPT_PAGE(pPage);
drh9e572e62004-04-23 23:43:101850 }
drhd8c34e32023-04-03 12:33:121851 }else if( top>(int)pPage->pBt->usableSize ){
1852 return SQLITE_CORRUPT_PAGE(pPage);
drh9e572e62004-04-23 23:43:101853 }
drh43605152004-05-29 21:46:491854
drhd4a67442019-02-11 19:27:361855 /* If there is enough space between gap and top for one more cell pointer,
1856 ** and if the freelist is not empty, then search the
1857 ** freelist looking for a slot big enough to satisfy the request.
drh4c04f3c2014-08-20 11:56:141858 */
drh5e2f8b92001-05-28 00:41:151859 testcase( gap+2==top );
drh7aa128d2002-06-21 13:09:161860 testcase( gap+1==top );
drh14acc042001-06-10 19:56:581861 testcase( gap==top );
drhe674bf12015-06-25 16:01:441862 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
drhb7580e82015-06-25 18:36:131863 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
dan8e9ba0c2014-10-14 17:27:041864 if( pSpace ){
drh3b76c452020-01-03 17:40:301865 int g2;
drh2b96b692019-08-05 16:22:201866 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
drh3b76c452020-01-03 17:40:301867 *pIdx = g2 = (int)(pSpace-data);
drhb9154182021-06-20 22:49:261868 if( g2<=gap ){
drh2b96b692019-08-05 16:22:201869 return SQLITE_CORRUPT_PAGE(pPage);
1870 }else{
1871 return SQLITE_OK;
1872 }
drhb7580e82015-06-25 18:36:131873 }else if( rc ){
1874 return rc;
drh9e572e62004-04-23 23:43:101875 }
1876 }
drh43605152004-05-29 21:46:491877
drh4c04f3c2014-08-20 11:56:141878 /* The request could not be fulfilled using a freelist slot. Check
1879 ** to see if defragmentation is necessary.
drh0a45c272009-07-08 01:49:111880 */
1881 testcase( gap+2+nByte==top );
1882 if( gap+2+nByte>top ){
drh1fd2d7d2014-12-02 16:16:471883 assert( pPage->nCell>0 || CORRUPT_DB );
drhb0ea9432019-02-09 21:06:401884 assert( pPage->nFree>=0 );
dane6d065a2017-02-24 19:58:221885 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
drh0a45c272009-07-08 01:49:111886 if( rc ) return rc;
drh5d433ce2010-08-14 16:02:521887 top = get2byteNotZero(&data[hdr+5]);
dan3b2ede12017-02-25 16:24:021888 assert( gap+2+nByte<=top );
drh0a45c272009-07-08 01:49:111889 }
1890
1891
drh43605152004-05-29 21:46:491892 /* Allocate memory from the gap in between the cell pointer array
drh5860a612019-02-12 16:58:261893 ** and the cell content area. The btreeComputeFreeSpace() call has already
drhc314dc72009-07-21 11:52:341894 ** validated the freelist. Given that the freelist is valid, there
1895 ** is no way that the allocation can extend off the end of the page.
1896 ** The assert() below verifies the previous sentence.
drh43605152004-05-29 21:46:491897 */
drh0a45c272009-07-08 01:49:111898 top -= nByte;
drh43605152004-05-29 21:46:491899 put2byte(&data[hdr+5], top);
drhfcd71b62011-04-05 22:08:241900 assert( top+nByte <= (int)pPage->pBt->usableSize );
drh0a45c272009-07-08 01:49:111901 *pIdx = top;
1902 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:401903}
1904
1905/*
drh9e572e62004-04-23 23:43:101906** Return a section of the pPage->aData to the freelist.
drh7fb91642014-08-20 14:37:091907** The first byte of the new free block is pPage->aData[iStart]
1908** and the size of the block is iSize bytes.
drh306dc212001-05-21 13:45:101909**
drh5f5c7532014-08-20 17:56:271910** Adjacent freeblocks are coalesced.
1911**
drh5860a612019-02-12 16:58:261912** Even though the freeblock list was checked by btreeComputeFreeSpace(),
drh5f5c7532014-08-20 17:56:271913** that routine will not detect overlap between cells or freeblocks. Nor
larrybrbc917382023-06-07 08:40:311914** does it detect cells or freeblocks that encroach into the reserved bytes
drh5f5c7532014-08-20 17:56:271915** at the end of the page. So do additional corruption checks inside this
1916** routine and return SQLITE_CORRUPT if any problems are found.
drh7e3b0a02001-04-28 16:52:401917*/
drhc071c472025-02-22 16:44:141918static int freeSpace(MemPage *pPage, int iStart, int iSize){
1919 int iPtr; /* Address of ptr to next freeblock */
1920 int iFreeBlk; /* Address of the next freeblock */
drh5f5c7532014-08-20 17:56:271921 u8 hdr; /* Page header size. 0 or 100 */
drhc071c472025-02-22 16:44:141922 int nFrag = 0; /* Reduction in fragmentation */
1923 int iOrigSize = iSize; /* Original value of iSize */
1924 int x; /* Offset to cell content area */
1925 int iEnd = iStart + iSize; /* First byte past the iStart buffer */
drh7fb91642014-08-20 14:37:091926 unsigned char *data = pPage->aData; /* Page content */
drh009a48e2022-02-23 18:23:151927 u8 *pTmp; /* Temporary ptr into data[] */
drh2af926b2001-05-15 00:39:251928
drh9e572e62004-04-23 23:43:101929 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:261930 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
dancf3d17c2015-05-25 15:03:491931 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
drhe33ea172025-07-04 12:25:241932 assert( CORRUPT_DB || iEnd <= (int)pPage->pBt->usableSize );
drh1fee73e2007-08-29 04:00:571933 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh7fb91642014-08-20 14:37:091934 assert( iSize>=4 ); /* Minimum cell size is 4 */
drhe33ea172025-07-04 12:25:241935 assert( CORRUPT_DB || iStart<=(int)pPage->pBt->usableSize-4 );
drhfcce93f2006-02-22 03:08:321936
larrybrbc917382023-06-07 08:40:311937 /* The list of freeblocks must be in ascending order. Find the
drh5f5c7532014-08-20 17:56:271938 ** spot on the list where iStart should be inserted.
drh0a45c272009-07-08 01:49:111939 */
drh43605152004-05-29 21:46:491940 hdr = pPage->hdrOffset;
drh7fb91642014-08-20 14:37:091941 iPtr = hdr + 1;
drh7bc4c452014-08-20 18:43:441942 if( data[iPtr+1]==0 && data[iPtr]==0 ){
1943 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
1944 }else{
drh85f071b2016-09-17 19:34:321945 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
drhdce232a2022-07-07 20:11:351946 if( iFreeBlk<=iPtr ){
drh05e8c542020-01-14 16:39:541947 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
daneebf2f52017-11-18 17:30:081948 return SQLITE_CORRUPT_PAGE(pPage);
drh85f071b2016-09-17 19:34:321949 }
drh7bc4c452014-08-20 18:43:441950 iPtr = iFreeBlk;
shanedcc50b72008-11-13 18:29:501951 }
drhc071c472025-02-22 16:44:141952 if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
daneebf2f52017-11-18 17:30:081953 return SQLITE_CORRUPT_PAGE(pPage);
drh5e398e42017-08-23 20:36:061954 }
drh0aa09452022-02-14 13:53:491955 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
larrybrbc917382023-06-07 08:40:311956
drh7bc4c452014-08-20 18:43:441957 /* At this point:
1958 ** iFreeBlk: First freeblock after iStart, or zero if none
drh3e24a342015-06-15 16:09:351959 ** iPtr: The address of a pointer to iFreeBlk
drh7bc4c452014-08-20 18:43:441960 **
1961 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
1962 */
1963 if( iFreeBlk && iEnd+3>=iFreeBlk ){
1964 nFrag = iFreeBlk - iEnd;
daneebf2f52017-11-18 17:30:081965 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
drh7bc4c452014-08-20 18:43:441966 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
drhc071c472025-02-22 16:44:141967 if( iEnd > (int)pPage->pBt->usableSize ){
daneebf2f52017-11-18 17:30:081968 return SQLITE_CORRUPT_PAGE(pPage);
drhcc97ca42017-06-07 22:32:591969 }
drh7bc4c452014-08-20 18:43:441970 iSize = iEnd - iStart;
1971 iFreeBlk = get2byte(&data[iFreeBlk]);
1972 }
larrybrbc917382023-06-07 08:40:311973
drh3f387402014-09-24 01:23:001974 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
1975 ** pointer in the page header) then check to see if iStart should be
1976 ** coalesced onto the end of iPtr.
drh7bc4c452014-08-20 18:43:441977 */
1978 if( iPtr>hdr+1 ){
1979 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
1980 if( iPtrEnd+3>=iStart ){
daneebf2f52017-11-18 17:30:081981 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
drh7bc4c452014-08-20 18:43:441982 nFrag += iStart - iPtrEnd;
1983 iSize = iEnd - iPtr;
1984 iStart = iPtr;
shanedcc50b72008-11-13 18:29:501985 }
drh9e572e62004-04-23 23:43:101986 }
daneebf2f52017-11-18 17:30:081987 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
drhc071c472025-02-22 16:44:141988 data[hdr+7] -= (u8)nFrag;
drh9e572e62004-04-23 23:43:101989 }
drh009a48e2022-02-23 18:23:151990 pTmp = &data[hdr+5];
1991 x = get2byte(pTmp);
drhb39f0372023-04-07 13:21:201992 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
1993 /* Overwrite deleted information with zeros when the secure_delete
1994 ** option is enabled */
1995 memset(&data[iStart], 0, iSize);
1996 }
drh5e398e42017-08-23 20:36:061997 if( iStart<=x ){
drh5f5c7532014-08-20 17:56:271998 /* The new freeblock is at the beginning of the cell content area,
1999 ** so just extend the cell content area rather than create another
2000 ** freelist entry */
drh3b76c452020-01-03 17:40:302001 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
drh48118e42020-01-29 13:50:112002 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
drh5f5c7532014-08-20 17:56:272003 put2byte(&data[hdr+1], iFreeBlk);
2004 put2byte(&data[hdr+5], iEnd);
2005 }else{
2006 /* Insert the new freeblock into the freelist */
2007 put2byte(&data[iPtr], iStart);
drhb39f0372023-04-07 13:21:202008 put2byte(&data[iStart], iFreeBlk);
drhc071c472025-02-22 16:44:142009 assert( iSize>=0 && iSize<=0xffff );
2010 put2byte(&data[iStart+2], (u16)iSize);
drh4b70f112004-05-02 21:12:192011 }
drh5f5c7532014-08-20 17:56:272012 pPage->nFree += iOrigSize;
shanedcc50b72008-11-13 18:29:502013 return SQLITE_OK;
drh4b70f112004-05-02 21:12:192014}
2015
2016/*
drh271efa52004-05-30 19:19:052017** Decode the flags byte (the first byte of the header) for a page
2018** and initialize fields of the MemPage structure accordingly.
drh44845222008-07-17 18:39:572019**
2020** Only the following combinations are supported. Anything different
2021** indicates a corrupt database files:
2022**
drhbf9b9942022-11-19 13:09:032023** PTF_ZERODATA (0x02, 2)
2024** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
2025** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
2026** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
drh271efa52004-05-30 19:19:052027*/
drh44845222008-07-17 18:39:572028static int decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:012029 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:052030
2031 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
drh1fee73e2007-08-29 04:00:572032 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh271efa52004-05-30 19:19:052033 pBt = pPage->pBt;
drhbf9b9942022-11-19 13:09:032034 pPage->max1bytePayload = pBt->max1bytePayload;
2035 if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){
2036 pPage->childPtrSize = 0;
2037 pPage->leaf = 1;
2038 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){
drh25ada072015-06-19 15:07:142039 pPage->intKeyLeaf = 1;
drh19ae01b2022-02-23 22:56:102040 pPage->xCellSize = cellSizePtrTableLeaf;
drh5fa60512015-06-19 17:19:342041 pPage->xParseCell = btreeParseCellPtr;
drhbf9b9942022-11-19 13:09:032042 pPage->intKey = 1;
2043 pPage->maxLocal = pBt->maxLeaf;
2044 pPage->minLocal = pBt->minLeaf;
2045 }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){
2046 pPage->intKey = 0;
2047 pPage->intKeyLeaf = 0;
drh2d782902023-02-25 16:03:282048 pPage->xCellSize = cellSizePtrIdxLeaf;
drhbf9b9942022-11-19 13:09:032049 pPage->xParseCell = btreeParseCellPtrIndex;
2050 pPage->maxLocal = pBt->maxLocal;
2051 pPage->minLocal = pBt->minLocal;
drh25ada072015-06-19 15:07:142052 }else{
drhbf9b9942022-11-19 13:09:032053 pPage->intKey = 0;
2054 pPage->intKeyLeaf = 0;
drh2d782902023-02-25 16:03:282055 pPage->xCellSize = cellSizePtrIdxLeaf;
drhbf9b9942022-11-19 13:09:032056 pPage->xParseCell = btreeParseCellPtrIndex;
2057 return SQLITE_CORRUPT_PAGE(pPage);
2058 }
2059 }else{
2060 pPage->childPtrSize = 4;
2061 pPage->leaf = 0;
2062 if( flagByte==(PTF_ZERODATA) ){
2063 pPage->intKey = 0;
2064 pPage->intKeyLeaf = 0;
2065 pPage->xCellSize = cellSizePtr;
2066 pPage->xParseCell = btreeParseCellPtrIndex;
2067 pPage->maxLocal = pBt->maxLocal;
2068 pPage->minLocal = pBt->minLocal;
2069 }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
drh25ada072015-06-19 15:07:142070 pPage->intKeyLeaf = 0;
drh25ada072015-06-19 15:07:142071 pPage->xCellSize = cellSizePtrNoPayload;
drh5fa60512015-06-19 17:19:342072 pPage->xParseCell = btreeParseCellPtrNoPayload;
drhbf9b9942022-11-19 13:09:032073 pPage->intKey = 1;
2074 pPage->maxLocal = pBt->maxLeaf;
2075 pPage->minLocal = pBt->minLeaf;
2076 }else{
2077 pPage->intKey = 0;
2078 pPage->intKeyLeaf = 0;
2079 pPage->xCellSize = cellSizePtr;
2080 pPage->xParseCell = btreeParseCellPtrIndex;
2081 return SQLITE_CORRUPT_PAGE(pPage);
drh25ada072015-06-19 15:07:142082 }
drh271efa52004-05-30 19:19:052083 }
drh44845222008-07-17 18:39:572084 return SQLITE_OK;
drh271efa52004-05-30 19:19:052085}
2086
2087/*
drhb0ea9432019-02-09 21:06:402088** Compute the amount of freespace on the page. In other words, fill
2089** in the pPage->nFree field.
drh7e3b0a02001-04-28 16:52:402090*/
drhb0ea9432019-02-09 21:06:402091static int btreeComputeFreeSpace(MemPage *pPage){
drh14e845a2017-05-25 21:35:562092 int pc; /* Address of a freeblock within pPage->aData[] */
2093 u8 hdr; /* Offset to beginning of page header */
2094 u8 *data; /* Equal to pPage->aData */
drh14e845a2017-05-25 21:35:562095 int usableSize; /* Amount of usable space on each page */
drh14e845a2017-05-25 21:35:562096 int nFree; /* Number of unused bytes on the page */
2097 int top; /* First byte of the cell content area */
2098 int iCellFirst; /* First allowable cell or freeblock offset */
2099 int iCellLast; /* Last possible cell or freeblock offset */
drh2af926b2001-05-15 00:39:252100
danielk197771d5d2c2008-09-29 11:49:472101 assert( pPage->pBt!=0 );
drh1421d982015-05-27 03:46:182102 assert( pPage->pBt->db!=0 );
danielk197771d5d2c2008-09-29 11:49:472103 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:262104 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbf4bca52007-09-06 22:19:142105 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
2106 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
drhb0ea9432019-02-09 21:06:402107 assert( pPage->isInit==1 );
2108 assert( pPage->nFree<0 );
danielk197771d5d2c2008-09-29 11:49:472109
drhb0ea9432019-02-09 21:06:402110 usableSize = pPage->pBt->usableSize;
drh14e845a2017-05-25 21:35:562111 hdr = pPage->hdrOffset;
2112 data = pPage->aData;
drh14e845a2017-05-25 21:35:562113 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
2114 ** the start of the cell content area. A zero value for this integer is
2115 ** interpreted as 65536. */
2116 top = get2byteNotZero(&data[hdr+5]);
drhb0ea9432019-02-09 21:06:402117 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
drh14e845a2017-05-25 21:35:562118 iCellLast = usableSize - 4;
danielk197793c829c2009-06-03 17:26:172119
drh14e845a2017-05-25 21:35:562120 /* Compute the total free space on the page
2121 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
2122 ** start of the first freeblock on the page, or is zero if there are no
2123 ** freeblocks. */
2124 pc = get2byte(&data[hdr+1]);
2125 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
2126 if( pc>0 ){
2127 u32 next, size;
dan9a20ea92020-01-03 15:51:232128 if( pc<top ){
drh14e845a2017-05-25 21:35:562129 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
2130 ** always be at least one cell before the first freeblock.
2131 */
larrybrbc917382023-06-07 08:40:312132 return SQLITE_CORRUPT_PAGE(pPage);
drhee696e22004-08-30 16:52:172133 }
drh14e845a2017-05-25 21:35:562134 while( 1 ){
2135 if( pc>iCellLast ){
drhcc97ca42017-06-07 22:32:592136 /* Freeblock off the end of the page */
daneebf2f52017-11-18 17:30:082137 return SQLITE_CORRUPT_PAGE(pPage);
drh14e845a2017-05-25 21:35:562138 }
2139 next = get2byte(&data[pc]);
2140 size = get2byte(&data[pc+2]);
2141 nFree = nFree + size;
2142 if( next<=pc+size+3 ) break;
2143 pc = next;
2144 }
2145 if( next>0 ){
drhcc97ca42017-06-07 22:32:592146 /* Freeblock not in ascending order */
daneebf2f52017-11-18 17:30:082147 return SQLITE_CORRUPT_PAGE(pPage);
drh14e845a2017-05-25 21:35:562148 }
2149 if( pc+size>(unsigned int)usableSize ){
drhcc97ca42017-06-07 22:32:592150 /* Last freeblock extends past page end */
daneebf2f52017-11-18 17:30:082151 return SQLITE_CORRUPT_PAGE(pPage);
drh14e845a2017-05-25 21:35:562152 }
danielk197771d5d2c2008-09-29 11:49:472153 }
drh14e845a2017-05-25 21:35:562154
2155 /* At this point, nFree contains the sum of the offset to the start
2156 ** of the cell-content area plus the number of free bytes within
2157 ** the cell-content area. If this is greater than the usable-size
2158 ** of the page, then the page must be corrupted. This check also
2159 ** serves to verify that the offset to the start of the cell-content
2160 ** area, according to the page header, lies within the page.
2161 */
drhdfcecdf2019-05-08 00:17:452162 if( nFree>usableSize || nFree<iCellFirst ){
daneebf2f52017-11-18 17:30:082163 return SQLITE_CORRUPT_PAGE(pPage);
drh14e845a2017-05-25 21:35:562164 }
2165 pPage->nFree = (u16)(nFree - iCellFirst);
drhb0ea9432019-02-09 21:06:402166 return SQLITE_OK;
2167}
2168
2169/*
drh5860a612019-02-12 16:58:262170** Do additional sanity check after btreeInitPage() if
larrybrbc917382023-06-07 08:40:312171** PRAGMA cell_size_check=ON
drh5860a612019-02-12 16:58:262172*/
2173static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
2174 int iCellFirst; /* First allowable cell or freeblock offset */
2175 int iCellLast; /* Last possible cell or freeblock offset */
2176 int i; /* Index into the cell pointer array */
2177 int sz; /* Size of a cell */
2178 int pc; /* Address of a freeblock within pPage->aData[] */
2179 u8 *data; /* Equal to pPage->aData */
2180 int usableSize; /* Maximum usable space on the page */
2181 int cellOffset; /* Start of cell content area */
2182
2183 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
2184 usableSize = pPage->pBt->usableSize;
2185 iCellLast = usableSize - 4;
2186 data = pPage->aData;
2187 cellOffset = pPage->cellOffset;
2188 if( !pPage->leaf ) iCellLast--;
2189 for(i=0; i<pPage->nCell; i++){
2190 pc = get2byteAligned(&data[cellOffset+i*2]);
2191 testcase( pc==iCellFirst );
2192 testcase( pc==iCellLast );
2193 if( pc<iCellFirst || pc>iCellLast ){
2194 return SQLITE_CORRUPT_PAGE(pPage);
2195 }
2196 sz = pPage->xCellSize(pPage, &data[pc]);
2197 testcase( pc+sz==usableSize );
2198 if( pc+sz>usableSize ){
2199 return SQLITE_CORRUPT_PAGE(pPage);
2200 }
2201 }
2202 return SQLITE_OK;
2203}
2204
2205/*
drhb0ea9432019-02-09 21:06:402206** Initialize the auxiliary information for a disk block.
2207**
2208** Return SQLITE_OK on success. If we see that the page does
larrybrbc917382023-06-07 08:40:312209** not contain a well-formed database page, then return
drhb0ea9432019-02-09 21:06:402210** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
2211** guarantee that the page is well-formed. It only shows that
2212** we failed to detect any corruption.
2213*/
2214static int btreeInitPage(MemPage *pPage){
drhb0ea9432019-02-09 21:06:402215 u8 *data; /* Equal to pPage->aData */
2216 BtShared *pBt; /* The main btree structure */
drhb0ea9432019-02-09 21:06:402217
2218 assert( pPage->pBt!=0 );
2219 assert( pPage->pBt->db!=0 );
2220 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2221 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
2222 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
2223 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
2224 assert( pPage->isInit==0 );
2225
2226 pBt = pPage->pBt;
drh5860a612019-02-12 16:58:262227 data = pPage->aData + pPage->hdrOffset;
drhb0ea9432019-02-09 21:06:402228 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
2229 ** the b-tree page type. */
drh5860a612019-02-12 16:58:262230 if( decodeFlags(pPage, data[0]) ){
drhb0ea9432019-02-09 21:06:402231 return SQLITE_CORRUPT_PAGE(pPage);
2232 }
2233 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
2234 pPage->maskPage = (u16)(pBt->pageSize - 1);
2235 pPage->nOverflow = 0;
drhc071c472025-02-22 16:44:142236 pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
drh5860a612019-02-12 16:58:262237 pPage->aCellIdx = data + pPage->childPtrSize + 8;
drha055abb2022-03-01 20:15:042238 pPage->aDataEnd = pPage->aData + pBt->pageSize;
drh5860a612019-02-12 16:58:262239 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
drhb0ea9432019-02-09 21:06:402240 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
2241 ** number of cells on the page. */
drh5860a612019-02-12 16:58:262242 pPage->nCell = get2byte(&data[3]);
drhb0ea9432019-02-09 21:06:402243 if( pPage->nCell>MX_CELL(pBt) ){
2244 /* To many cells for a single page. The page must be corrupt */
2245 return SQLITE_CORRUPT_PAGE(pPage);
2246 }
2247 testcase( pPage->nCell==MX_CELL(pBt) );
2248 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
2249 ** possible for a root page of a table that contains no rows) then the
2250 ** offset to the cell content area will equal the page size minus the
2251 ** bytes of reserved space. */
2252 assert( pPage->nCell>0
mistachkin065f3bf2019-03-20 05:45:032253 || get2byteNotZero(&data[5])==(int)pBt->usableSize
drhb0ea9432019-02-09 21:06:402254 || CORRUPT_DB );
drhb0ea9432019-02-09 21:06:402255 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
drh14e845a2017-05-25 21:35:562256 pPage->isInit = 1;
drh5860a612019-02-12 16:58:262257 if( pBt->db->flags & SQLITE_CellSizeCk ){
2258 return btreeCellSizeCheck(pPage);
2259 }
drh9e572e62004-04-23 23:43:102260 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:402261}
2262
2263/*
drh8b2f49b2001-06-08 00:21:522264** Set up a raw page so that it looks like a database page holding
2265** no entries.
drhbd03cae2001-06-02 02:40:572266*/
drh9e572e62004-04-23 23:43:102267static void zeroPage(MemPage *pPage, int flags){
2268 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:012269 BtShared *pBt = pPage->pBt;
drhc071c472025-02-22 16:44:142270 int hdr = pPage->hdrOffset;
2271 int first;
drh9e572e62004-04-23 23:43:102272
drh37034292022-03-01 16:22:542273 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
drhbf4bca52007-09-06 22:19:142274 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2275 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
danielk19773b8a05f2007-03-19 17:44:262276 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:572277 assert( sqlite3_mutex_held(pBt->mutex) );
drha5907a82017-06-19 11:44:222278 if( pBt->btsFlags & BTS_FAST_SECURE ){
drh5b47efa2010-02-12 18:18:392279 memset(&data[hdr], 0, pBt->usableSize - hdr);
2280 }
drh1bd10f82008-12-10 21:19:562281 data[hdr] = (char)flags;
drhfe485992014-02-12 23:52:162282 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
drh43605152004-05-29 21:46:492283 memset(&data[hdr+1], 0, 4);
2284 data[hdr+7] = 0;
2285 put2byte(&data[hdr+5], pBt->usableSize);
shaneh1df2db72010-08-18 02:28:482286 pPage->nFree = (u16)(pBt->usableSize - first);
drh271efa52004-05-30 19:19:052287 decodeFlags(pPage, flags);
drhc071c472025-02-22 16:44:142288 pPage->cellOffset = (u16)first;
drha055abb2022-03-01 20:15:042289 pPage->aDataEnd = &data[pBt->pageSize];
drh3def2352011-11-11 00:27:152290 pPage->aCellIdx = &data[first];
drhf44890a2015-06-27 03:58:152291 pPage->aDataOfst = &data[pPage->childPtrSize];
drh43605152004-05-29 21:46:492292 pPage->nOverflow = 0;
drhb2eced52010-08-12 02:41:122293 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
2294 pPage->maskPage = (u16)(pBt->pageSize - 1);
drh43605152004-05-29 21:46:492295 pPage->nCell = 0;
danielk197771d5d2c2008-09-29 11:49:472296 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:572297}
2298
drh897a8202008-09-18 01:08:152299
2300/*
2301** Convert a DbPage obtained from the pager into a MemPage used by
2302** the btree layer.
2303*/
2304static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
2305 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
drh8dd1c252015-11-04 22:31:022306 if( pgno!=pPage->pgno ){
2307 pPage->aData = sqlite3PagerGetData(pDbPage);
2308 pPage->pDbPage = pDbPage;
2309 pPage->pBt = pBt;
2310 pPage->pgno = pgno;
2311 pPage->hdrOffset = pgno==1 ? 100 : 0;
2312 }
2313 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
larrybrbc917382023-06-07 08:40:312314 return pPage;
drh897a8202008-09-18 01:08:152315}
2316
drhbd03cae2001-06-02 02:40:572317/*
drh3aac2dd2004-04-26 14:10:202318** Get a page from the pager. Initialize the MemPage.pBt and
drh7e8c6f12015-05-28 03:28:272319** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
drh538f5702007-04-13 02:14:302320**
drh7e8c6f12015-05-28 03:28:272321** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
2322** about the content of the page at this time. So do not go to the disk
drh538f5702007-04-13 02:14:302323** to fetch the content. Just fill in the content with zeros for now.
2324** If in the future we call sqlite3PagerWrite() on this page, that
2325** means we have started to be concerned about content and the disk
2326** read should occur at that point.
drh3aac2dd2004-04-26 14:10:202327*/
danielk197730548662009-07-09 05:07:372328static int btreeGetPage(
drh16a9b832007-05-05 18:39:252329 BtShared *pBt, /* The btree */
2330 Pgno pgno, /* Number of the page to fetch */
2331 MemPage **ppPage, /* Return the page in this parameter */
drhb00fc3b2013-08-21 23:42:322332 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
drh16a9b832007-05-05 18:39:252333){
drh3aac2dd2004-04-26 14:10:202334 int rc;
danielk19773b8a05f2007-03-19 17:44:262335 DbPage *pDbPage;
2336
drhb00fc3b2013-08-21 23:42:322337 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
drh1fee73e2007-08-29 04:00:572338 assert( sqlite3_mutex_held(pBt->mutex) );
drh9584f582015-11-04 20:22:372339 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
drh3aac2dd2004-04-26 14:10:202340 if( rc ) return rc;
drh897a8202008-09-18 01:08:152341 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
drh3aac2dd2004-04-26 14:10:202342 return SQLITE_OK;
2343}
2344
2345/*
danielk1977bea2a942009-01-20 17:06:272346** Retrieve a page from the pager cache. If the requested page is not
2347** already in the pager cache return NULL. Initialize the MemPage.pBt and
2348** MemPage.aData elements if needed.
2349*/
2350static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
2351 DbPage *pDbPage;
2352 assert( sqlite3_mutex_held(pBt->mutex) );
2353 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
2354 if( pDbPage ){
2355 return btreePageFromDbPage(pDbPage, pgno, pBt);
2356 }
2357 return 0;
2358}
2359
2360/*
danielk197789d40042008-11-17 14:20:562361** Return the size of the database file in pages. If there is any kind of
2362** error, return ((unsigned int)-1).
danielk197767fd7a92008-09-10 17:53:352363*/
drhb1299152010-03-30 22:58:332364static Pgno btreePagecount(BtShared *pBt){
drh406dfcb2020-01-07 18:10:012365 return pBt->nPage;
drhb1299152010-03-30 22:58:332366}
drh584e8b72020-07-22 17:12:592367Pgno sqlite3BtreeLastPage(Btree *p){
drhb1299152010-03-30 22:58:332368 assert( sqlite3BtreeHoldsMutex(p) );
drh584e8b72020-07-22 17:12:592369 return btreePagecount(p->pBt);
danielk197767fd7a92008-09-10 17:53:352370}
2371
2372/*
drh28f58dd2015-06-27 19:45:032373** Get a page from the pager and initialize it.
drhde647132004-05-07 17:57:492374*/
2375static int getAndInitPage(
dan11dcd112013-03-15 18:29:182376 BtShared *pBt, /* The database file */
2377 Pgno pgno, /* Number of the page to get */
2378 MemPage **ppPage, /* Write the page pointer here */
drh28f58dd2015-06-27 19:45:032379 int bReadOnly /* True for a read-only page */
drhde647132004-05-07 17:57:492380){
2381 int rc;
drh28f58dd2015-06-27 19:45:032382 DbPage *pDbPage;
drhd4170ac2023-06-22 01:03:392383 MemPage *pPage;
drh1fee73e2007-08-29 04:00:572384 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197789bc4bc2009-07-21 19:25:242385
danba3cbf32010-06-30 04:29:032386 if( pgno>btreePagecount(pBt) ){
drhd4170ac2023-06-22 01:03:392387 *ppPage = 0;
2388 return SQLITE_CORRUPT_BKPT;
drh28f58dd2015-06-27 19:45:032389 }
drh9584f582015-11-04 20:22:372390 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
drh28f58dd2015-06-27 19:45:032391 if( rc ){
drhd4170ac2023-06-22 01:03:392392 *ppPage = 0;
2393 return rc;
drh28f58dd2015-06-27 19:45:032394 }
drhd4170ac2023-06-22 01:03:392395 pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2396 if( pPage->isInit==0 ){
drh8dd1c252015-11-04 22:31:022397 btreePageFromDbPage(pDbPage, pgno, pBt);
drhd4170ac2023-06-22 01:03:392398 rc = btreeInitPage(pPage);
drh28f58dd2015-06-27 19:45:032399 if( rc!=SQLITE_OK ){
drhd4170ac2023-06-22 01:03:392400 releasePage(pPage);
2401 *ppPage = 0;
2402 return rc;
danielk197789bc4bc2009-07-21 19:25:242403 }
drhee696e22004-08-30 16:52:172404 }
drhd4170ac2023-06-22 01:03:392405 assert( pPage->pgno==pgno || CORRUPT_DB );
2406 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
2407 *ppPage = pPage;
drh28f58dd2015-06-27 19:45:032408 return SQLITE_OK;
drhde647132004-05-07 17:57:492409}
2410
2411/*
drh3aac2dd2004-04-26 14:10:202412** Release a MemPage. This should be called once for each prior
danielk197730548662009-07-09 05:07:372413** call to btreeGetPage.
drh3908fe92017-09-01 14:50:192414**
2415** Page1 is a special case and must be released using releasePageOne().
drh3aac2dd2004-04-26 14:10:202416*/
drhbbf0f862015-06-27 14:59:262417static void releasePageNotNull(MemPage *pPage){
2418 assert( pPage->aData );
2419 assert( pPage->pBt );
2420 assert( pPage->pDbPage!=0 );
2421 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2422 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2423 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2424 sqlite3PagerUnrefNotNull(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:202425}
drh3aac2dd2004-04-26 14:10:202426static void releasePage(MemPage *pPage){
drhbbf0f862015-06-27 14:59:262427 if( pPage ) releasePageNotNull(pPage);
drh3aac2dd2004-04-26 14:10:202428}
drh3908fe92017-09-01 14:50:192429static void releasePageOne(MemPage *pPage){
2430 assert( pPage!=0 );
2431 assert( pPage->aData );
2432 assert( pPage->pBt );
2433 assert( pPage->pDbPage!=0 );
2434 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2435 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2436 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2437 sqlite3PagerUnrefPageOne(pPage->pDbPage);
2438}
drh3aac2dd2004-04-26 14:10:202439
2440/*
drh7e8c6f12015-05-28 03:28:272441** Get an unused page.
2442**
2443** This works just like btreeGetPage() with the addition:
2444**
2445** * If the page is already in use for some other purpose, immediately
2446** release it and return an SQLITE_CURRUPT error.
2447** * Make sure the isInit flag is clear
2448*/
2449static int btreeGetUnusedPage(
2450 BtShared *pBt, /* The btree */
2451 Pgno pgno, /* Number of the page to fetch */
2452 MemPage **ppPage, /* Return the page in this parameter */
2453 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2454){
2455 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
2456 if( rc==SQLITE_OK ){
2457 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
2458 releasePage(*ppPage);
2459 *ppPage = 0;
2460 return SQLITE_CORRUPT_BKPT;
2461 }
2462 (*ppPage)->isInit = 0;
2463 }else{
2464 *ppPage = 0;
2465 }
2466 return rc;
2467}
2468
drha059ad02001-04-17 20:09:112469
2470/*
drha6abd042004-06-09 17:37:222471** During a rollback, when the pager reloads information into the cache
2472** so that the cache is restored to its original state at the start of
2473** the transaction, for each page restored this routine is called.
2474**
2475** This routine needs to reset the extra data section at the end of the
2476** page to agree with the restored data.
2477*/
danielk1977eaa06f62008-09-18 17:34:442478static void pageReinit(DbPage *pData){
drh07d183d2005-05-01 22:52:422479 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:262480 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
danielk1977d217e6f2009-04-01 17:13:512481 assert( sqlite3PagerPageRefcount(pData)>0 );
danielk197771d5d2c2008-09-29 11:49:472482 if( pPage->isInit ){
drh1fee73e2007-08-29 04:00:572483 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drha6abd042004-06-09 17:37:222484 pPage->isInit = 0;
danielk1977d217e6f2009-04-01 17:13:512485 if( sqlite3PagerPageRefcount(pData)>1 ){
drh5e8d8872009-03-30 17:19:482486 /* pPage might not be a btree page; it might be an overflow page
2487 ** or ptrmap page or a free page. In those cases, the following
danielk197730548662009-07-09 05:07:372488 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
drh5e8d8872009-03-30 17:19:482489 ** But no harm is done by this. And it is very important that
danielk197730548662009-07-09 05:07:372490 ** btreeInitPage() be called on every btree page so we make
larrybrbc917382023-06-07 08:40:312491 ** the call for every page that comes in for re-initializing. */
danielk197730548662009-07-09 05:07:372492 btreeInitPage(pPage);
danielk197771d5d2c2008-09-29 11:49:472493 }
drha6abd042004-06-09 17:37:222494 }
2495}
2496
2497/*
drhe5fe6902007-12-07 18:55:282498** Invoke the busy handler for a btree.
2499*/
danielk19771ceedd32008-11-19 10:22:332500static int btreeInvokeBusyHandler(void *pArg){
drhe5fe6902007-12-07 18:55:282501 BtShared *pBt = (BtShared*)pArg;
2502 assert( pBt->db );
2503 assert( sqlite3_mutex_held(pBt->db->mutex) );
drh783e1592020-05-06 20:55:382504 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
drhe5fe6902007-12-07 18:55:282505}
2506
2507/*
drhad3e0102004-09-03 23:32:182508** Open a database file.
larrybrbc917382023-06-07 08:40:312509**
drh382c0242001-10-06 16:33:022510** zFilename is the name of the database file. If zFilename is NULL
drh75c014c2010-08-30 15:02:282511** then an ephemeral database is created. The ephemeral database might
2512** be exclusively in memory, or it might use a disk-based memory cache.
larrybrbc917382023-06-07 08:40:312513** Either way, the ephemeral database will be automatically deleted
drh75c014c2010-08-30 15:02:282514** when sqlite3BtreeClose() is called.
2515**
drhe53831d2007-08-17 01:14:382516** If zFilename is ":memory:" then an in-memory database is created
2517** that is automatically destroyed when it is closed.
drhc47fd8e2009-04-30 13:30:322518**
drh33f111d2012-01-17 15:29:142519** The "flags" parameter is a bitmask that might contain bits like
2520** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
drh75c014c2010-08-30 15:02:282521**
drhc47fd8e2009-04-30 13:30:322522** If the database is already opened in the same database connection
2523** and we are in shared cache mode, then the open will fail with an
2524** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
2525** objects in the same database connection since doing so will lead
2526** to problems with locking.
drha059ad02001-04-17 20:09:112527*/
drh23e11ca2004-05-04 17:27:282528int sqlite3BtreeOpen(
dan3a6d8ae2011-04-23 15:54:542529 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
drh3aac2dd2004-04-26 14:10:202530 const char *zFilename, /* Name of the file containing the BTree database */
drhe5fe6902007-12-07 18:55:282531 sqlite3 *db, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:202532 Btree **ppBtree, /* Pointer to new Btree object written here */
drh33f4e022007-09-03 15:19:342533 int flags, /* Options */
2534 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
drh6019e162001-07-02 17:51:452535){
drh7555d8e2009-03-20 13:15:302536 BtShared *pBt = 0; /* Shared part of btree structure */
2537 Btree *p; /* Handle to return */
2538 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
2539 int rc = SQLITE_OK; /* Result code from this function */
2540 u8 nReserve; /* Byte of unused space on each page */
2541 unsigned char zDbHeader[100]; /* Database header content */
danielk1977aef0bf62005-12-30 16:28:012542
drh75c014c2010-08-30 15:02:282543 /* True if opening an ephemeral, temporary database */
2544 const int isTempDb = zFilename==0 || zFilename[0]==0;
2545
larrybrbc917382023-06-07 08:40:312546 /* Set the variable isMemdb to true for an in-memory database, or
drhb0a7c9c2010-12-06 21:09:592547 ** false for a file-based database.
danielk1977aef0bf62005-12-30 16:28:012548 */
drhb0a7c9c2010-12-06 21:09:592549#ifdef SQLITE_OMIT_MEMORYDB
2550 const int isMemdb = 0;
2551#else
2552 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
drh9c67b2a2012-05-28 13:58:002553 || (isTempDb && sqlite3TempInMemory(db))
2554 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
danielk1977aef0bf62005-12-30 16:28:012555#endif
2556
drhe5fe6902007-12-07 18:55:282557 assert( db!=0 );
dan3a6d8ae2011-04-23 15:54:542558 assert( pVfs!=0 );
drhe5fe6902007-12-07 18:55:282559 assert( sqlite3_mutex_held(db->mutex) );
drhd4187c72010-08-30 22:15:452560 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
2561
2562 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
2563 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
2564
2565 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
2566 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
drh153c62c2007-08-24 03:51:332567
drh75c014c2010-08-30 15:02:282568 if( isMemdb ){
2569 flags |= BTREE_MEMORY;
2570 }
2571 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
2572 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
2573 }
drh17435752007-08-16 04:30:382574 p = sqlite3MallocZero(sizeof(Btree));
danielk1977aef0bf62005-12-30 16:28:012575 if( !p ){
mistachkinfad30392016-02-13 23:43:462576 return SQLITE_NOMEM_BKPT;
danielk1977aef0bf62005-12-30 16:28:012577 }
2578 p->inTrans = TRANS_NONE;
drhe5fe6902007-12-07 18:55:282579 p->db = db;
danielk1977602b4662009-07-02 07:47:332580#ifndef SQLITE_OMIT_SHARED_CACHE
2581 p->lock.pBtree = p;
2582 p->lock.iTable = 1;
2583#endif
danielk1977aef0bf62005-12-30 16:28:012584
drh198bf392006-01-06 21:52:492585#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:382586 /*
2587 ** If this Btree is a candidate for shared cache, try to find an
2588 ** existing BtShared object that we can share with
2589 */
drh4ab9d252012-05-26 20:08:492590 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
drhf1f12682009-09-09 14:17:522591 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
drh6b5f0eb2015-03-31 16:33:082592 int nFilename = sqlite3Strlen30(zFilename)+1;
danielk1977adfb9b02007-09-17 07:02:562593 int nFullPathname = pVfs->mxPathname+1;
drh6b5f0eb2015-03-31 16:33:082594 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
drh30ddce62011-10-15 00:16:302595 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
drh6b5f0eb2015-03-31 16:33:082596
drhff0587c2007-08-29 17:43:192597 p->sharable = 1;
drhff0587c2007-08-29 17:43:192598 if( !zFullPathname ){
2599 sqlite3_free(p);
mistachkinfad30392016-02-13 23:43:462600 return SQLITE_NOMEM_BKPT;
drhff0587c2007-08-29 17:43:192601 }
drhafc8b7f2012-05-26 18:06:382602 if( isMemdb ){
drh6b5f0eb2015-03-31 16:33:082603 memcpy(zFullPathname, zFilename, nFilename);
drhafc8b7f2012-05-26 18:06:382604 }else{
2605 rc = sqlite3OsFullPathname(pVfs, zFilename,
2606 nFullPathname, zFullPathname);
2607 if( rc ){
drhc398c652019-11-22 00:42:012608 if( rc==SQLITE_OK_SYMLINK ){
2609 rc = SQLITE_OK;
2610 }else{
2611 sqlite3_free(zFullPathname);
2612 sqlite3_free(p);
2613 return rc;
2614 }
drhafc8b7f2012-05-26 18:06:382615 }
drh070ad6b2011-11-17 11:43:192616 }
drh30ddce62011-10-15 00:16:302617#if SQLITE_THREADSAFE
drh7555d8e2009-03-20 13:15:302618 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
2619 sqlite3_mutex_enter(mutexOpen);
drhccb21132020-06-19 11:34:572620 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
drhff0587c2007-08-29 17:43:192621 sqlite3_mutex_enter(mutexShared);
drh30ddce62011-10-15 00:16:302622#endif
drh78f82d12008-09-02 00:52:522623 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
drhff0587c2007-08-29 17:43:192624 assert( pBt->nRef>0 );
drhd4e0bb02012-05-27 01:19:042625 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
drhff0587c2007-08-29 17:43:192626 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
drhc47fd8e2009-04-30 13:30:322627 int iDb;
2628 for(iDb=db->nDb-1; iDb>=0; iDb--){
2629 Btree *pExisting = db->aDb[iDb].pBt;
2630 if( pExisting && pExisting->pBt==pBt ){
2631 sqlite3_mutex_leave(mutexShared);
2632 sqlite3_mutex_leave(mutexOpen);
2633 sqlite3_free(zFullPathname);
2634 sqlite3_free(p);
2635 return SQLITE_CONSTRAINT;
2636 }
2637 }
drhff0587c2007-08-29 17:43:192638 p->pBt = pBt;
2639 pBt->nRef++;
2640 break;
2641 }
2642 }
2643 sqlite3_mutex_leave(mutexShared);
2644 sqlite3_free(zFullPathname);
danielk1977aef0bf62005-12-30 16:28:012645 }
drhff0587c2007-08-29 17:43:192646#ifdef SQLITE_DEBUG
2647 else{
2648 /* In debug mode, we mark all persistent databases as sharable
2649 ** even when they are not. This exercises the locking code and
2650 ** gives more opportunity for asserts(sqlite3_mutex_held())
2651 ** statements to find locking problems.
2652 */
2653 p->sharable = 1;
2654 }
2655#endif
danielk1977aef0bf62005-12-30 16:28:012656 }
2657#endif
drha059ad02001-04-17 20:09:112658 if( pBt==0 ){
drhe53831d2007-08-17 01:14:382659 /*
2660 ** The following asserts make sure that structures used by the btree are
2661 ** the right size. This is to guard against size changes that result
2662 ** when compiling on a different architecture.
danielk197703aded42004-11-22 05:26:272663 */
drh062cf272015-03-23 19:03:512664 assert( sizeof(i64)==8 );
2665 assert( sizeof(u64)==8 );
drhe53831d2007-08-17 01:14:382666 assert( sizeof(u32)==4 );
2667 assert( sizeof(u16)==2 );
2668 assert( sizeof(Pgno)==4 );
drh56a41072023-06-16 14:39:212669
2670 /* Suppress false-positive compiler warning from PVS-Studio */
2671 memset(&zDbHeader[16], 0, 8);
larrybrbc917382023-06-07 08:40:312672
drhe53831d2007-08-17 01:14:382673 pBt = sqlite3MallocZero( sizeof(*pBt) );
2674 if( pBt==0 ){
mistachkinfad30392016-02-13 23:43:462675 rc = SQLITE_NOMEM_BKPT;
drhe53831d2007-08-17 01:14:382676 goto btree_open_out;
2677 }
danielk197771d5d2c2008-09-29 11:49:472678 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
drha2ee5892016-12-09 16:02:002679 sizeof(MemPage), flags, vfsFlags, pageReinit);
drhe53831d2007-08-17 01:14:382680 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:422681 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
drhe53831d2007-08-17 01:14:382682 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
2683 }
2684 if( rc!=SQLITE_OK ){
2685 goto btree_open_out;
2686 }
shanehbd2aaf92010-09-01 02:38:212687 pBt->openFlags = (u8)flags;
danielk19772a50ff02009-04-10 09:47:062688 pBt->db = db;
drh80262892018-03-26 16:37:532689 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
drhe53831d2007-08-17 01:14:382690 p->pBt = pBt;
larrybrbc917382023-06-07 08:40:312691
drhe53831d2007-08-17 01:14:382692 pBt->pCursor = 0;
2693 pBt->pPage1 = 0;
drhc9166342012-01-05 23:32:062694 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
drha5907a82017-06-19 11:44:222695#if defined(SQLITE_SECURE_DELETE)
drhc9166342012-01-05 23:32:062696 pBt->btsFlags |= BTS_SECURE_DELETE;
drha5907a82017-06-19 11:44:222697#elif defined(SQLITE_FAST_SECURE_DELETE)
2698 pBt->btsFlags |= BTS_OVERWRITE;
drh5b47efa2010-02-12 18:18:392699#endif
drh113762a2014-11-19 16:36:252700 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2701 ** determined by the 2-byte integer located at an offset of 16 bytes from
2702 ** the beginning of the database file. */
drhb2eced52010-08-12 02:41:122703 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
drhe53831d2007-08-17 01:14:382704 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
2705 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
danielk1977a1644fd2007-08-29 12:31:252706 pBt->pageSize = 0;
drhe53831d2007-08-17 01:14:382707#ifndef SQLITE_OMIT_AUTOVACUUM
2708 /* If the magic name ":memory:" will create an in-memory database, then
2709 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
2710 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
2711 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
2712 ** regular file-name. In this case the auto-vacuum applies as per normal.
2713 */
2714 if( zFilename && !isMemdb ){
2715 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
2716 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
2717 }
2718#endif
2719 nReserve = 0;
2720 }else{
drh113762a2014-11-19 16:36:252721 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2722 ** determined by the one-byte unsigned integer found at an offset of 20
2723 ** into the database file header. */
drhe53831d2007-08-17 01:14:382724 nReserve = zDbHeader[20];
drhc9166342012-01-05 23:32:062725 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
drhe53831d2007-08-17 01:14:382726#ifndef SQLITE_OMIT_AUTOVACUUM
2727 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
2728 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
2729#endif
2730 }
drhfa9601a2009-06-18 17:22:392731 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
drhc0b61812009-04-30 01:22:412732 if( rc ) goto btree_open_out;
drhe53831d2007-08-17 01:14:382733 pBt->usableSize = pBt->pageSize - nReserve;
2734 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
larrybrbc917382023-06-07 08:40:312735
drhe53831d2007-08-17 01:14:382736#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2737 /* Add the new BtShared object to the linked list sharable BtShareds.
2738 */
dan272989b2016-07-06 10:12:022739 pBt->nRef = 1;
drhe53831d2007-08-17 01:14:382740 if( p->sharable ){
drh30ddce62011-10-15 00:16:302741 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
drhccb21132020-06-19 11:34:572742 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)
danielk1977075c23a2008-09-01 18:34:202743 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:102744 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
drh3285db22007-09-03 22:00:392745 if( pBt->mutex==0 ){
mistachkinfad30392016-02-13 23:43:462746 rc = SQLITE_NOMEM_BKPT;
drh3285db22007-09-03 22:00:392747 goto btree_open_out;
2748 }
drhff0587c2007-08-29 17:43:192749 }
drhe53831d2007-08-17 01:14:382750 sqlite3_mutex_enter(mutexShared);
drh78f82d12008-09-02 00:52:522751 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
2752 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
drhe53831d2007-08-17 01:14:382753 sqlite3_mutex_leave(mutexShared);
danielk1977951af802004-11-05 15:45:092754 }
drheee46cf2004-11-06 00:02:482755#endif
drh90f5ecb2004-07-22 01:19:352756 }
danielk1977aef0bf62005-12-30 16:28:012757
drhcfed7bc2006-03-13 14:28:052758#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:382759 /* If the new Btree uses a sharable pBtShared, then link the new
2760 ** Btree into the list of all sharable Btrees for the same connection.
drhabddb0c2007-08-20 13:14:282761 ** The list is kept in ascending order by pBt address.
danielk197754f01982006-01-18 15:25:172762 */
drhe53831d2007-08-17 01:14:382763 if( p->sharable ){
2764 int i;
2765 Btree *pSib;
drhe5fe6902007-12-07 18:55:282766 for(i=0; i<db->nDb; i++){
2767 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
drhe53831d2007-08-17 01:14:382768 while( pSib->pPrev ){ pSib = pSib->pPrev; }
drh3bfa7e82016-03-22 14:37:592769 if( (uptr)p->pBt<(uptr)pSib->pBt ){
drhe53831d2007-08-17 01:14:382770 p->pNext = pSib;
2771 p->pPrev = 0;
2772 pSib->pPrev = p;
2773 }else{
drh3bfa7e82016-03-22 14:37:592774 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
drhe53831d2007-08-17 01:14:382775 pSib = pSib->pNext;
2776 }
2777 p->pNext = pSib->pNext;
2778 p->pPrev = pSib;
2779 if( p->pNext ){
2780 p->pNext->pPrev = p;
2781 }
2782 pSib->pNext = p;
2783 }
2784 break;
2785 }
2786 }
danielk1977aef0bf62005-12-30 16:28:012787 }
danielk1977aef0bf62005-12-30 16:28:012788#endif
2789 *ppBtree = p;
danielk1977dddbcdc2007-04-26 14:42:342790
2791btree_open_out:
2792 if( rc!=SQLITE_OK ){
2793 if( pBt && pBt->pPager ){
dan7fb89902016-08-12 16:21:152794 sqlite3PagerClose(pBt->pPager, 0);
danielk1977dddbcdc2007-04-26 14:42:342795 }
drh17435752007-08-16 04:30:382796 sqlite3_free(pBt);
2797 sqlite3_free(p);
danielk1977dddbcdc2007-04-26 14:42:342798 *ppBtree = 0;
drh75c014c2010-08-30 15:02:282799 }else{
dan0f5a1862016-08-13 14:30:232800 sqlite3_file *pFile;
2801
drh75c014c2010-08-30 15:02:282802 /* If the B-Tree was successfully opened, set the pager-cache size to the
2803 ** default value. Except, when opening on an existing shared pager-cache,
2804 ** do not change the pager-cache size.
2805 */
2806 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
dan78f04752020-09-04 19:10:432807 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE);
drh75c014c2010-08-30 15:02:282808 }
dan0f5a1862016-08-13 14:30:232809
2810 pFile = sqlite3PagerFile(pBt->pPager);
2811 if( pFile->pMethods ){
2812 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
2813 }
danielk1977dddbcdc2007-04-26 14:42:342814 }
drh7555d8e2009-03-20 13:15:302815 if( mutexOpen ){
2816 assert( sqlite3_mutex_held(mutexOpen) );
2817 sqlite3_mutex_leave(mutexOpen);
2818 }
dan272989b2016-07-06 10:12:022819 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
danielk1977dddbcdc2007-04-26 14:42:342820 return rc;
drha059ad02001-04-17 20:09:112821}
2822
2823/*
drhe53831d2007-08-17 01:14:382824** Decrement the BtShared.nRef counter. When it reaches zero,
2825** remove the BtShared structure from the sharing list. Return
2826** true if the BtShared.nRef counter reaches zero and return
2827** false if it is still positive.
2828*/
2829static int removeFromSharingList(BtShared *pBt){
2830#ifndef SQLITE_OMIT_SHARED_CACHE
drh067b92b2020-06-19 15:24:122831 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )
drhe53831d2007-08-17 01:14:382832 BtShared *pList;
2833 int removed = 0;
2834
drhd677b3d2007-08-20 22:48:412835 assert( sqlite3_mutex_notheld(pBt->mutex) );
drh067b92b2020-06-19 15:24:122836 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
2837 sqlite3_mutex_enter(pMainMtx);
drhe53831d2007-08-17 01:14:382838 pBt->nRef--;
2839 if( pBt->nRef<=0 ){
drh78f82d12008-09-02 00:52:522840 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
2841 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
drhe53831d2007-08-17 01:14:382842 }else{
drh78f82d12008-09-02 00:52:522843 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
drh34004ce2008-07-11 16:15:172844 while( ALWAYS(pList) && pList->pNext!=pBt ){
drhe53831d2007-08-17 01:14:382845 pList=pList->pNext;
2846 }
drh34004ce2008-07-11 16:15:172847 if( ALWAYS(pList) ){
drhe53831d2007-08-17 01:14:382848 pList->pNext = pBt->pNext;
2849 }
2850 }
drh3285db22007-09-03 22:00:392851 if( SQLITE_THREADSAFE ){
2852 sqlite3_mutex_free(pBt->mutex);
2853 }
drhe53831d2007-08-17 01:14:382854 removed = 1;
2855 }
drh067b92b2020-06-19 15:24:122856 sqlite3_mutex_leave(pMainMtx);
drhe53831d2007-08-17 01:14:382857 return removed;
2858#else
stephanb504aab2025-05-31 09:44:002859 UNUSED_PARAMETER( pBt );
drhe53831d2007-08-17 01:14:382860 return 1;
2861#endif
2862}
2863
2864/*
larrybrbc917382023-06-07 08:40:312865** Make sure pBt->pTmpSpace points to an allocation of
drh92787cf2014-10-15 11:55:512866** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
2867** pointer.
drhf7141992008-06-19 00:16:082868*/
drh2f0bc1d2021-12-03 13:42:412869static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){
2870 assert( pBt!=0 );
2871 assert( pBt->pTmpSpace==0 );
2872 /* This routine is called only by btreeCursor() when allocating the
2873 ** first write cursor for the BtShared object */
2874 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
2875 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
2876 if( pBt->pTmpSpace==0 ){
2877 BtCursor *pCur = pBt->pCursor;
2878 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
2879 memset(pCur, 0, sizeof(*pCur));
2880 return SQLITE_NOMEM_BKPT;
drhf7141992008-06-19 00:16:082881 }
drh2f0bc1d2021-12-03 13:42:412882
2883 /* One of the uses of pBt->pTmpSpace is to format cells before
2884 ** inserting them into a leaf page (function fillInCell()). If
2885 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
2886 ** by the various routines that manipulate binary cells. Which
2887 ** can mean that fillInCell() only initializes the first 2 or 3
2888 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
2889 ** it into a database page. This is not actually a problem, but it
larrybrbc917382023-06-07 08:40:312890 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
drh2f0bc1d2021-12-03 13:42:412891 ** data is passed to system call write(). So to avoid this error,
2892 ** zero the first 4 bytes of temp space here.
2893 **
2894 ** Also: Provide four bytes of initialized space before the
2895 ** beginning of pTmpSpace as an area available to prepend the
2896 ** left-child pointer to the beginning of a cell.
2897 */
drh11e4fdb2021-12-03 14:57:052898 memset(pBt->pTmpSpace, 0, 8);
2899 pBt->pTmpSpace += 4;
drh2f0bc1d2021-12-03 13:42:412900 return SQLITE_OK;
drhf7141992008-06-19 00:16:082901}
2902
2903/*
2904** Free the pBt->pTmpSpace allocation
2905*/
2906static void freeTempSpace(BtShared *pBt){
drh92787cf2014-10-15 11:55:512907 if( pBt->pTmpSpace ){
2908 pBt->pTmpSpace -= 4;
2909 sqlite3PageFree(pBt->pTmpSpace);
2910 pBt->pTmpSpace = 0;
2911 }
drhf7141992008-06-19 00:16:082912}
2913
2914/*
drha059ad02001-04-17 20:09:112915** Close an open database and invalidate all cursors.
2916*/
danielk1977aef0bf62005-12-30 16:28:012917int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:012918 BtShared *pBt = p->pBt;
danielk1977aef0bf62005-12-30 16:28:012919
danielk1977aef0bf62005-12-30 16:28:012920 /* Close all cursors opened via this handle. */
drhe5fe6902007-12-07 18:55:282921 assert( sqlite3_mutex_held(p->db->mutex) );
drhe53831d2007-08-17 01:14:382922 sqlite3BtreeEnter(p);
drh5a4a15f2021-03-18 15:42:592923
2924 /* Verify that no other cursors have this Btree open */
2925#ifdef SQLITE_DEBUG
2926 {
2927 BtCursor *pCur = pBt->pCursor;
2928 while( pCur ){
2929 BtCursor *pTmp = pCur;
2930 pCur = pCur->pNext;
2931 assert( pTmp->pBtree!=p );
2932
danielk1977aef0bf62005-12-30 16:28:012933 }
drha059ad02001-04-17 20:09:112934 }
drh5a4a15f2021-03-18 15:42:592935#endif
danielk1977aef0bf62005-12-30 16:28:012936
danielk19778d34dfd2006-01-24 16:37:572937 /* Rollback any active transaction and free the handle structure.
2938 ** The call to sqlite3BtreeRollback() drops any table-locks held by
2939 ** this handle.
2940 */
drh47b7fc72014-11-11 01:33:572941 sqlite3BtreeRollback(p, SQLITE_OK, 0);
drhe53831d2007-08-17 01:14:382942 sqlite3BtreeLeave(p);
danielk1977aef0bf62005-12-30 16:28:012943
danielk1977aef0bf62005-12-30 16:28:012944 /* If there are still other outstanding references to the shared-btree
larrybrbc917382023-06-07 08:40:312945 ** structure, return now. The remainder of this procedure cleans
danielk1977aef0bf62005-12-30 16:28:012946 ** up the shared-btree.
2947 */
drhe53831d2007-08-17 01:14:382948 assert( p->wantToLock==0 && p->locked==0 );
2949 if( !p->sharable || removeFromSharingList(pBt) ){
2950 /* The pBt is no longer on the sharing list, so we can access
2951 ** it without having to hold the mutex.
2952 **
2953 ** Clean out and delete the BtShared object.
2954 */
2955 assert( !pBt->pCursor );
dan7fb89902016-08-12 16:21:152956 sqlite3PagerClose(pBt->pPager, p->db);
drhe53831d2007-08-17 01:14:382957 if( pBt->xFreeSchema && pBt->pSchema ){
2958 pBt->xFreeSchema(pBt->pSchema);
2959 }
drhb9755982010-07-24 16:34:372960 sqlite3DbFree(0, pBt->pSchema);
drhf7141992008-06-19 00:16:082961 freeTempSpace(pBt);
drh65bbf292008-06-19 01:03:172962 sqlite3_free(pBt);
danielk1977aef0bf62005-12-30 16:28:012963 }
2964
drhe53831d2007-08-17 01:14:382965#ifndef SQLITE_OMIT_SHARED_CACHE
drhcab5ed72007-08-22 11:41:182966 assert( p->wantToLock==0 );
2967 assert( p->locked==0 );
2968 if( p->pPrev ) p->pPrev->pNext = p->pNext;
2969 if( p->pNext ) p->pNext->pPrev = p->pPrev;
danielk1977aef0bf62005-12-30 16:28:012970#endif
2971
drhe53831d2007-08-17 01:14:382972 sqlite3_free(p);
drha059ad02001-04-17 20:09:112973 return SQLITE_OK;
2974}
2975
2976/*
drh9b0cf342015-11-12 14:57:192977** Change the "soft" limit on the number of pages in the cache.
2978** Unused and unmodified pages will be recycled when the number of
2979** pages in the cache exceeds this soft limit. But the size of the
2980** cache is allowed to grow larger than this limit if it contains
2981** dirty pages or pages still in active use.
drhf57b14a2001-09-14 18:54:082982*/
danielk1977aef0bf62005-12-30 16:28:012983int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
2984 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:282985 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:412986 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:262987 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhd677b3d2007-08-20 22:48:412988 sqlite3BtreeLeave(p);
drhf57b14a2001-09-14 18:54:082989 return SQLITE_OK;
2990}
2991
drh9b0cf342015-11-12 14:57:192992/*
2993** Change the "spill" limit on the number of pages in the cache.
2994** If the number of pages exceeds this limit during a write transaction,
2995** the pager might attempt to "spill" pages to the journal early in
2996** order to free up memory.
2997**
2998** The value returned is the current spill size. If zero is passed
2999** as an argument, no changes are made to the spill size setting, so
3000** using mxPage of 0 is a way to query the current spill size.
3001*/
3002int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
3003 BtShared *pBt = p->pBt;
3004 int res;
3005 assert( sqlite3_mutex_held(p->db->mutex) );
3006 sqlite3BtreeEnter(p);
3007 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
3008 sqlite3BtreeLeave(p);
3009 return res;
3010}
3011
drh18c7e402014-03-14 11:46:103012#if SQLITE_MAX_MMAP_SIZE>0
drhf57b14a2001-09-14 18:54:083013/*
dan5d8a1372013-03-19 19:28:063014** Change the limit on the amount of the database file that may be
3015** memory mapped.
3016*/
drh9b4c59f2013-04-15 17:03:423017int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
dan5d8a1372013-03-19 19:28:063018 BtShared *pBt = p->pBt;
3019 assert( sqlite3_mutex_held(p->db->mutex) );
3020 sqlite3BtreeEnter(p);
drh9b4c59f2013-04-15 17:03:423021 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
dan5d8a1372013-03-19 19:28:063022 sqlite3BtreeLeave(p);
3023 return SQLITE_OK;
3024}
drh18c7e402014-03-14 11:46:103025#endif /* SQLITE_MAX_MMAP_SIZE>0 */
dan5d8a1372013-03-19 19:28:063026
3027/*
drh973b6e32003-02-12 14:09:423028** Change the way data is synced to disk in order to increase or decrease
3029** how well the database resists damage due to OS crashes and power
3030** failures. Level 1 is the same as asynchronous (no syncs() occur and
3031** there is a high probability of damage) Level 2 is the default. There
3032** is a very low but non-zero probability of damage. Level 3 reduces the
3033** probability of damage to near zero but with a write performance reduction.
3034*/
danielk197793758c82005-01-21 08:13:143035#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh40c39412013-08-16 20:42:203036int sqlite3BtreeSetPagerFlags(
drhc97d8462010-11-19 18:23:353037 Btree *p, /* The btree to set the safety level on */
drh40c39412013-08-16 20:42:203038 unsigned pgFlags /* Various PAGER_* flags */
drhc97d8462010-11-19 18:23:353039){
danielk1977aef0bf62005-12-30 16:28:013040 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:283041 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:413042 sqlite3BtreeEnter(p);
drh40c39412013-08-16 20:42:203043 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
drhd677b3d2007-08-20 22:48:413044 sqlite3BtreeLeave(p);
drh973b6e32003-02-12 14:09:423045 return SQLITE_OK;
3046}
danielk197793758c82005-01-21 08:13:143047#endif
drh973b6e32003-02-12 14:09:423048
drh2c8997b2005-08-27 16:36:483049/*
drh90f5ecb2004-07-22 01:19:353050** Change the default pages size and the number of reserved bytes per page.
larrybrbc917382023-06-07 08:40:313051** Or, if the page size has already been fixed, return SQLITE_READONLY
drhce4869f2009-04-02 20:16:583052** without changing anything.
drh06f50212004-11-02 14:24:333053**
3054** The page size must be a power of 2 between 512 and 65536. If the page
3055** size supplied does not meet this constraint then the page size is not
3056** changed.
3057**
3058** Page sizes are constrained to be a power of two so that the region
3059** of the database file used for locking (beginning at PENDING_BYTE,
3060** the first byte past the 1GB boundary, 0x40000000) needs to occur
3061** at the beginning of a page.
danielk197728129562005-01-11 10:25:063062**
3063** If parameter nReserve is less than zero, then the number of reserved
3064** bytes per page is left unchanged.
drhce4869f2009-04-02 20:16:583065**
drhc9166342012-01-05 23:32:063066** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
drhce4869f2009-04-02 20:16:583067** and autovacuum mode can no longer be changed.
drh90f5ecb2004-07-22 01:19:353068*/
drhce4869f2009-04-02 20:16:583069int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
danielk1977a1644fd2007-08-29 12:31:253070 int rc = SQLITE_OK;
drhe937df82020-05-07 01:56:573071 int x;
danielk1977aef0bf62005-12-30 16:28:013072 BtShared *pBt = p->pBt;
drhe937df82020-05-07 01:56:573073 assert( nReserve>=0 && nReserve<=255 );
drhd677b3d2007-08-20 22:48:413074 sqlite3BtreeEnter(p);
drhc071c472025-02-22 16:44:143075 pBt->nReserveWanted = (u8)nReserve;
drhe937df82020-05-07 01:56:573076 x = pBt->pageSize - pBt->usableSize;
drhf453e8d2025-06-23 13:42:593077 if( x==nReserve && (pageSize==0 || (u32)pageSize==pBt->pageSize) ){
drhb68d6312025-06-20 13:12:553078 sqlite3BtreeLeave(p);
3079 return SQLITE_OK;
3080 }
drhe937df82020-05-07 01:56:573081 if( nReserve<x ) nReserve = x;
drhc9166342012-01-05 23:32:063082 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
drhd677b3d2007-08-20 22:48:413083 sqlite3BtreeLeave(p);
drh90f5ecb2004-07-22 01:19:353084 return SQLITE_READONLY;
3085 }
drhf49661a2008-12-10 16:45:503086 assert( nReserve>=0 && nReserve<=255 );
drh06f50212004-11-02 14:24:333087 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
3088 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:423089 assert( (pageSize & 7)==0 );
dandd14ecb2015-05-05 10:03:083090 assert( !pBt->pCursor );
drh906602a2021-01-21 21:36:253091 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
drhb2eced52010-08-12 02:41:123092 pBt->pageSize = (u32)pageSize;
drhf7141992008-06-19 00:16:083093 freeTempSpace(pBt);
drh90f5ecb2004-07-22 01:19:353094 }
drhfa9601a2009-06-18 17:22:393095 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
drhf49661a2008-12-10 16:45:503096 pBt->usableSize = pBt->pageSize - (u16)nReserve;
drhc9166342012-01-05 23:32:063097 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
drhd677b3d2007-08-20 22:48:413098 sqlite3BtreeLeave(p);
danielk1977a1644fd2007-08-29 12:31:253099 return rc;
drh90f5ecb2004-07-22 01:19:353100}
3101
3102/*
3103** Return the currently defined page size
3104*/
danielk1977aef0bf62005-12-30 16:28:013105int sqlite3BtreeGetPageSize(Btree *p){
3106 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:353107}
drh7f751222009-03-17 22:33:003108
dan0094f372012-09-28 20:23:423109/*
3110** This function is similar to sqlite3BtreeGetReserve(), except that it
3111** may only be called if it is guaranteed that the b-tree mutex is already
3112** held.
3113**
3114** This is useful in one special case in the backup API code where it is
larrybrbc917382023-06-07 08:40:313115** known that the shared b-tree mutex is held, but the mutex on the
dan0094f372012-09-28 20:23:423116** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
3117** were to be called, it might collide with some other operation on the
mistachkin48864df2013-03-21 21:20:323118** database handle that owns *p, causing undefined behavior.
dan0094f372012-09-28 20:23:423119*/
3120int sqlite3BtreeGetReserveNoMutex(Btree *p){
drhad0961b2015-02-21 00:19:253121 int n;
dan0094f372012-09-28 20:23:423122 assert( sqlite3_mutex_held(p->pBt->mutex) );
drhad0961b2015-02-21 00:19:253123 n = p->pBt->pageSize - p->pBt->usableSize;
3124 return n;
dan0094f372012-09-28 20:23:423125}
3126
drh7f751222009-03-17 22:33:003127/*
3128** Return the number of bytes of space at the end of every page that
larrybrbc917382023-06-07 08:40:313129** are intentionally left unused. This is the "reserved" space that is
drh7f751222009-03-17 22:33:003130** sometimes used by extensions.
drhad0961b2015-02-21 00:19:253131**
drh4d347662020-04-22 00:50:213132** The value returned is the larger of the current reserve size and
3133** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
3134** The amount of reserve can only grow - never shrink.
drh7f751222009-03-17 22:33:003135*/
drh45248de2020-04-20 15:18:433136int sqlite3BtreeGetRequestedReserve(Btree *p){
drhe937df82020-05-07 01:56:573137 int n1, n2;
drhd677b3d2007-08-20 22:48:413138 sqlite3BtreeEnter(p);
drhe937df82020-05-07 01:56:573139 n1 = (int)p->pBt->nReserveWanted;
3140 n2 = sqlite3BtreeGetReserveNoMutex(p);
drhd677b3d2007-08-20 22:48:413141 sqlite3BtreeLeave(p);
drhe937df82020-05-07 01:56:573142 return n1>n2 ? n1 : n2;
drh2011d5f2004-07-22 02:40:373143}
drhf8e632b2007-05-08 14:51:363144
drhad0961b2015-02-21 00:19:253145
drhf8e632b2007-05-08 14:51:363146/*
3147** Set the maximum page count for a database if mxPage is positive.
3148** No changes are made if mxPage is 0 or negative.
3149** Regardless of the value of mxPage, return the maximum page count.
3150*/
drhe9261db2020-07-20 12:47:323151Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
3152 Pgno n;
drhd677b3d2007-08-20 22:48:413153 sqlite3BtreeEnter(p);
3154 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
3155 sqlite3BtreeLeave(p);
3156 return n;
drhf8e632b2007-05-08 14:51:363157}
drh5b47efa2010-02-12 18:18:393158
3159/*
drha5907a82017-06-19 11:44:223160** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
3161**
3162** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
3163** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
3164** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
3165** newFlag==(-1) No changes
3166**
3167** This routine acts as a query if newFlag is less than zero
3168**
3169** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
3170** freelist leaf pages are not written back to the database. Thus in-page
3171** deleted content is cleared, but freelist deleted content is not.
3172**
3173** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
3174** that freelist leaf pages are written back into the database, increasing
3175** the amount of disk I/O.
drh5b47efa2010-02-12 18:18:393176*/
3177int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
3178 int b;
drhaf034ed2010-02-12 19:46:263179 if( p==0 ) return 0;
drh5b47efa2010-02-12 18:18:393180 sqlite3BtreeEnter(p);
drha5907a82017-06-19 11:44:223181 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
3182 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
drh5b47efa2010-02-12 18:18:393183 if( newFlag>=0 ){
drha5907a82017-06-19 11:44:223184 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
drhc071c472025-02-22 16:44:143185 p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag);
drha5907a82017-06-19 11:44:223186 }
3187 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
drh5b47efa2010-02-12 18:18:393188 sqlite3BtreeLeave(p);
3189 return b;
3190}
drh90f5ecb2004-07-22 01:19:353191
3192/*
danielk1977951af802004-11-05 15:45:093193** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
3194** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
larrybrbc917382023-06-07 08:40:313195** is disabled. The default value for the auto-vacuum property is
danielk1977951af802004-11-05 15:45:093196** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
3197*/
danielk1977aef0bf62005-12-30 16:28:013198int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
danielk1977951af802004-11-05 15:45:093199#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:483200 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:093201#else
danielk1977dddbcdc2007-04-26 14:42:343202 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:413203 int rc = SQLITE_OK;
drh076d4662009-02-18 20:31:183204 u8 av = (u8)autoVacuum;
drhd677b3d2007-08-20 22:48:413205
3206 sqlite3BtreeEnter(p);
drhc9166342012-01-05 23:32:063207 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:413208 rc = SQLITE_READONLY;
3209 }else{
drh076d4662009-02-18 20:31:183210 pBt->autoVacuum = av ?1:0;
3211 pBt->incrVacuum = av==2 ?1:0;
danielk1977951af802004-11-05 15:45:093212 }
drhd677b3d2007-08-20 22:48:413213 sqlite3BtreeLeave(p);
3214 return rc;
danielk1977951af802004-11-05 15:45:093215#endif
3216}
3217
3218/*
larrybrbc917382023-06-07 08:40:313219** Return the value of the 'auto-vacuum' property. If auto-vacuum is
danielk1977951af802004-11-05 15:45:093220** enabled 1 is returned. Otherwise 0.
3221*/
danielk1977aef0bf62005-12-30 16:28:013222int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:093223#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:343224 return BTREE_AUTOVACUUM_NONE;
danielk1977951af802004-11-05 15:45:093225#else
drhd677b3d2007-08-20 22:48:413226 int rc;
3227 sqlite3BtreeEnter(p);
3228 rc = (
danielk1977dddbcdc2007-04-26 14:42:343229 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
3230 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
3231 BTREE_AUTOVACUUM_INCR
3232 );
drhd677b3d2007-08-20 22:48:413233 sqlite3BtreeLeave(p);
3234 return rc;
danielk1977951af802004-11-05 15:45:093235#endif
3236}
3237
danf5da7db2017-03-16 18:14:393238/*
3239** If the user has not set the safety-level for this database connection
3240** using "PRAGMA synchronous", and if the safety-level is not already
3241** set to the value passed to this function as the second parameter,
3242** set it so.
3243*/
drh2ed57372017-10-05 20:57:383244#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
3245 && !defined(SQLITE_OMIT_WAL)
danf5da7db2017-03-16 18:14:393246static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
3247 sqlite3 *db;
3248 Db *pDb;
3249 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
3250 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
larrybrbc917382023-06-07 08:40:313251 if( pDb->bSyncSet==0
3252 && pDb->safety_level!=safety_level
3253 && pDb!=&db->aDb[1]
danf5da7db2017-03-16 18:14:393254 ){
3255 pDb->safety_level = safety_level;
3256 sqlite3PagerSetFlags(pBt->pPager,
3257 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
3258 }
3259 }
3260}
3261#else
danfc8f4b62017-03-16 18:54:423262# define setDefaultSyncFlag(pBt,safety_level)
danf5da7db2017-03-16 18:14:393263#endif
danielk1977951af802004-11-05 15:45:093264
drh0314cf32018-04-28 01:27:093265/* Forward declaration */
3266static int newDatabase(BtShared*);
3267
3268
danielk1977951af802004-11-05 15:45:093269/*
drha34b6762004-05-07 13:30:423270** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:103271** also acquire a readlock on that file.
3272**
3273** SQLITE_OK is returned on success. If the file is not a
3274** well-formed database file, then SQLITE_CORRUPT is returned.
3275** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
larrybrbc917382023-06-07 08:40:313276** is returned if we run out of memory.
drh306dc212001-05-21 13:45:103277*/
danielk1977aef0bf62005-12-30 16:28:013278static int lockBtree(BtShared *pBt){
drhc2a4bab2010-04-02 12:46:453279 int rc; /* Result code from subfunctions */
3280 MemPage *pPage1; /* Page 1 of the database file */
dane6370e92019-01-11 17:41:233281 u32 nPage; /* Number of pages in the database */
3282 u32 nPageFile = 0; /* Number of pages in the database file */
drhd677b3d2007-08-20 22:48:413283
drh1fee73e2007-08-29 04:00:573284 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977295dc102009-04-01 19:07:033285 assert( pBt->pPage1==0 );
danielk197789bc4bc2009-07-21 19:25:243286 rc = sqlite3PagerSharedLock(pBt->pPager);
3287 if( rc!=SQLITE_OK ) return rc;
drhb00fc3b2013-08-21 23:42:323288 rc = btreeGetPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:103289 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:103290
3291 /* Do some checking to help insure the file we opened really is
larrybrbc917382023-06-07 08:40:313292 ** a valid database file.
drh306dc212001-05-21 13:45:103293 */
drh7d4c94b2021-10-04 22:34:383294 nPage = get4byte(28+(u8*)pPage1->aData);
dane6370e92019-01-11 17:41:233295 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
drhb28e59b2010-06-17 02:13:393296 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
drhc2a4bab2010-04-02 12:46:453297 nPage = nPageFile;
drh97b59a52010-03-31 02:31:333298 }
drh0314cf32018-04-28 01:27:093299 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
3300 nPage = 0;
3301 }
drh97b59a52010-03-31 02:31:333302 if( nPage>0 ){
drh43b18e12010-08-17 19:40:083303 u32 pageSize;
3304 u32 usableSize;
drhb6f41482004-05-14 01:58:113305 u8 *page1 = pPage1->aData;
danielk1977ad0132d2008-06-07 08:58:223306 rc = SQLITE_NOTADB;
drh113762a2014-11-19 16:36:253307 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
3308 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
3309 ** 61 74 20 33 00. */
drhb6f41482004-05-14 01:58:113310 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:343311 goto page1_init_failed;
drh306dc212001-05-21 13:45:103312 }
dan5cf53532010-05-01 16:40:203313
3314#ifdef SQLITE_OMIT_WAL
3315 if( page1[18]>1 ){
drhc9166342012-01-05 23:32:063316 pBt->btsFlags |= BTS_READ_ONLY;
dan5cf53532010-05-01 16:40:203317 }
3318 if( page1[19]>1 ){
3319 goto page1_init_failed;
3320 }
3321#else
dane04dc882010-04-20 18:53:153322 if( page1[18]>2 ){
drhc9166342012-01-05 23:32:063323 pBt->btsFlags |= BTS_READ_ONLY;
drh309169a2007-04-24 17:27:513324 }
dane04dc882010-04-20 18:53:153325 if( page1[19]>2 ){
drhb6f41482004-05-14 01:58:113326 goto page1_init_failed;
3327 }
drhe5ae5732008-06-15 02:51:473328
drh0ccda522021-08-23 15:56:013329 /* If the read version is set to 2, this database should be accessed
larrybrbc917382023-06-07 08:40:313330 ** in WAL mode. If the log is not already open, open it now. Then
dana470aeb2010-04-21 11:43:383331 ** return SQLITE_OK and return without populating BtShared.pPage1.
3332 ** The caller detects this and calls this function again. This is
3333 ** required as the version of page 1 currently in the page1 buffer
3334 ** may not be the latest version - there may be a newer one in the log
3335 ** file.
3336 */
drhc9166342012-01-05 23:32:063337 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
dane04dc882010-04-20 18:53:153338 int isOpen = 0;
drh7ed91f22010-04-29 22:34:073339 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
dane04dc882010-04-20 18:53:153340 if( rc!=SQLITE_OK ){
3341 goto page1_init_failed;
drhe243de52016-03-08 15:14:263342 }else{
danf5da7db2017-03-16 18:14:393343 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
drhe243de52016-03-08 15:14:263344 if( isOpen==0 ){
drh3908fe92017-09-01 14:50:193345 releasePageOne(pPage1);
drhe243de52016-03-08 15:14:263346 return SQLITE_OK;
3347 }
dane04dc882010-04-20 18:53:153348 }
dan8b5444b2010-04-27 14:37:473349 rc = SQLITE_NOTADB;
danf5da7db2017-03-16 18:14:393350 }else{
3351 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
dane04dc882010-04-20 18:53:153352 }
dan5cf53532010-05-01 16:40:203353#endif
dane04dc882010-04-20 18:53:153354
drh113762a2014-11-19 16:36:253355 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
3356 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
3357 **
drhe5ae5732008-06-15 02:51:473358 ** The original design allowed these amounts to vary, but as of
3359 ** version 3.6.0, we require them to be fixed.
3360 */
3361 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
3362 goto page1_init_failed;
3363 }
drh113762a2014-11-19 16:36:253364 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
3365 ** determined by the 2-byte integer located at an offset of 16 bytes from
3366 ** the beginning of the database file. */
drhb2eced52010-08-12 02:41:123367 pageSize = (page1[16]<<8) | (page1[17]<<16);
drh113762a2014-11-19 16:36:253368 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
3369 ** between 512 and 65536 inclusive. */
drhb2eced52010-08-12 02:41:123370 if( ((pageSize-1)&pageSize)!=0
larrybrbc917382023-06-07 08:40:313371 || pageSize>SQLITE_MAX_PAGE_SIZE
3372 || pageSize<=256
drh7dc385e2007-09-06 23:39:363373 ){
drh07d183d2005-05-01 22:52:423374 goto page1_init_failed;
3375 }
3376 assert( (pageSize & 7)==0 );
drh113762a2014-11-19 16:36:253377 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
3378 ** integer at offset 20 is the number of bytes of space at the end of
larrybrbc917382023-06-07 08:40:313379 ** each page to reserve for extensions.
drh113762a2014-11-19 16:36:253380 **
3381 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
3382 ** determined by the one-byte unsigned integer found at an offset of 20
3383 ** into the database file header. */
danielk1977f653d782008-03-20 11:04:213384 usableSize = pageSize - page1[20];
shaneh1df2db72010-08-18 02:28:483385 if( (u32)pageSize!=pBt->pageSize ){
danielk1977f653d782008-03-20 11:04:213386 /* After reading the first page of the database assuming a page size
3387 ** of BtShared.pageSize, we have discovered that the page-size is
3388 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
3389 ** zero and return SQLITE_OK. The caller will call this function
3390 ** again with the correct page-size.
3391 */
drh3908fe92017-09-01 14:50:193392 releasePageOne(pPage1);
drh43b18e12010-08-17 19:40:083393 pBt->usableSize = usableSize;
3394 pBt->pageSize = pageSize;
drh997670f2023-06-12 13:31:203395 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
drhf7141992008-06-19 00:16:083396 freeTempSpace(pBt);
drhfa9601a2009-06-18 17:22:393397 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
3398 pageSize-usableSize);
drh5e483932009-07-10 16:51:303399 return rc;
danielk1977f653d782008-03-20 11:04:213400 }
drh5a6f8182022-01-17 14:42:383401 if( nPage>nPageFile ){
3402 if( sqlite3WritableSchema(pBt->db)==0 ){
3403 rc = SQLITE_CORRUPT_BKPT;
3404 goto page1_init_failed;
3405 }else{
3406 nPage = nPageFile;
3407 }
drhc2a4bab2010-04-02 12:46:453408 }
drh113762a2014-11-19 16:36:253409 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
3410 ** be less than 480. In other words, if the page size is 512, then the
3411 ** reserved space size cannot exceed 32. */
drhb33e1b92009-06-18 11:29:203412 if( usableSize<480 ){
drhb6f41482004-05-14 01:58:113413 goto page1_init_failed;
3414 }
drhb102d622023-06-10 19:33:023415 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
drh43b18e12010-08-17 19:40:083416 pBt->pageSize = pageSize;
3417 pBt->usableSize = usableSize;
drh057cd3a2005-02-15 16:23:023418#ifndef SQLITE_OMIT_AUTOVACUUM
3419 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
danielk197727b1f952007-06-25 08:16:583420 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
drh057cd3a2005-02-15 16:23:023421#endif
drh306dc212001-05-21 13:45:103422 }
drhb6f41482004-05-14 01:58:113423
3424 /* maxLocal is the maximum amount of payload to store locally for
3425 ** a cell. Make sure it is small enough so that at least minFanout
3426 ** cells can will fit on one page. We assume a 10-byte page header.
3427 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:493428 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:113429 ** 4-byte child pointer
3430 ** 9-byte nKey value
3431 ** 4-byte nData value
3432 ** 4-byte overflow page pointer
drhe22e03e2010-08-18 21:19:033433 ** So a cell consists of a 2-byte pointer, a header which is as much as
drh43605152004-05-29 21:46:493434 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
3435 ** page pointer.
drhb6f41482004-05-14 01:58:113436 */
shaneh1df2db72010-08-18 02:28:483437 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
3438 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
3439 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
3440 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
drhc9166342012-01-05 23:32:063441 if( pBt->maxLocal>127 ){
3442 pBt->max1bytePayload = 127;
3443 }else{
mistachkin0547e2f2012-01-08 00:54:023444 pBt->max1bytePayload = (u8)pBt->maxLocal;
drhc9166342012-01-05 23:32:063445 }
drh2e38c322004-09-03 18:38:443446 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:203447 pBt->pPage1 = pPage1;
drhdd3cd972010-03-27 17:12:363448 pBt->nPage = nPage;
drhb6f41482004-05-14 01:58:113449 return SQLITE_OK;
drh306dc212001-05-21 13:45:103450
drh72f82862001-05-24 21:06:343451page1_init_failed:
drh3908fe92017-09-01 14:50:193452 releasePageOne(pPage1);
drh3aac2dd2004-04-26 14:10:203453 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:343454 return rc;
drh306dc212001-05-21 13:45:103455}
3456
drh85ec3b62013-05-14 23:12:063457#ifndef NDEBUG
3458/*
3459** Return the number of cursors open on pBt. This is for use
3460** in assert() expressions, so it is only compiled if NDEBUG is not
3461** defined.
3462**
3463** Only write cursors are counted if wrOnly is true. If wrOnly is
3464** false then all cursors are counted.
3465**
3466** For the purposes of this routine, a cursor is any cursor that
peter.d.reid60ec9142014-09-06 16:39:463467** is capable of reading or writing to the database. Cursors that
drh85ec3b62013-05-14 23:12:063468** have been tripped into the CURSOR_FAULT state are not counted.
3469*/
3470static int countValidCursors(BtShared *pBt, int wrOnly){
3471 BtCursor *pCur;
3472 int r = 0;
3473 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
drh036dbec2014-03-11 23:40:443474 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
larrybrbc917382023-06-07 08:40:313475 && pCur->eState!=CURSOR_FAULT ) r++;
drh85ec3b62013-05-14 23:12:063476 }
3477 return r;
3478}
3479#endif
3480
drh306dc212001-05-21 13:45:103481/*
drhb8ca3072001-12-05 00:21:203482** If there are no outstanding cursors and we are not in the middle
3483** of a transaction but there is a read lock on the database, then
larrybrbc917382023-06-07 08:40:313484** this routine unrefs the first page of the database file which
drhb8ca3072001-12-05 00:21:203485** has the effect of releasing the read lock.
3486**
drhb8ca3072001-12-05 00:21:203487** If there is a transaction in progress, this routine is a no-op.
3488*/
danielk1977aef0bf62005-12-30 16:28:013489static void unlockBtreeIfUnused(BtShared *pBt){
drh1fee73e2007-08-29 04:00:573490 assert( sqlite3_mutex_held(pBt->mutex) );
drh85ec3b62013-05-14 23:12:063491 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
danielk19771bc9ee92009-07-04 15:41:023492 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
drhb2325b72014-09-24 18:31:073493 MemPage *pPage1 = pBt->pPage1;
3494 assert( pPage1->aData );
danielk1977c1761e82009-06-25 09:40:033495 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
drh3aac2dd2004-04-26 14:10:203496 pBt->pPage1 = 0;
drh3908fe92017-09-01 14:50:193497 releasePageOne(pPage1);
drhb8ca3072001-12-05 00:21:203498 }
3499}
3500
3501/*
drhe39f2f92009-07-23 01:43:593502** If pBt points to an empty file then convert that empty file
3503** into a new empty database by initializing the first page of
3504** the database.
drh8b2f49b2001-06-08 00:21:523505*/
danielk1977aef0bf62005-12-30 16:28:013506static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:103507 MemPage *pP1;
3508 unsigned char *data;
drh8c42ca92001-06-22 19:15:003509 int rc;
drhd677b3d2007-08-20 22:48:413510
drh1fee73e2007-08-29 04:00:573511 assert( sqlite3_mutex_held(pBt->mutex) );
drhdd3cd972010-03-27 17:12:363512 if( pBt->nPage>0 ){
3513 return SQLITE_OK;
danielk1977ad0132d2008-06-07 08:58:223514 }
drh3aac2dd2004-04-26 14:10:203515 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:103516 assert( pP1!=0 );
3517 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:263518 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:523519 if( rc ) return rc;
drh9e572e62004-04-23 23:43:103520 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
3521 assert( sizeof(zMagicHeader)==16 );
shaneh1df2db72010-08-18 02:28:483522 data[16] = (u8)((pBt->pageSize>>8)&0xff);
3523 data[17] = (u8)((pBt->pageSize>>16)&0xff);
drh9e572e62004-04-23 23:43:103524 data[18] = 1;
3525 data[19] = 1;
drhf49661a2008-12-10 16:45:503526 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
3527 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
drhe5ae5732008-06-15 02:51:473528 data[21] = 64;
3529 data[22] = 32;
3530 data[23] = 32;
drhb6f41482004-05-14 01:58:113531 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:463532 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhc9166342012-01-05 23:32:063533 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
danielk1977003ba062004-11-04 02:57:333534#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:343535 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
danielk1977418899a2007-06-24 10:14:003536 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
danielk1977dddbcdc2007-04-26 14:42:343537 put4byte(&data[36 + 4*4], pBt->autoVacuum);
danielk1977418899a2007-06-24 10:14:003538 put4byte(&data[36 + 7*4], pBt->incrVacuum);
danielk1977003ba062004-11-04 02:57:333539#endif
drhdd3cd972010-03-27 17:12:363540 pBt->nPage = 1;
3541 data[31] = 1;
drh8b2f49b2001-06-08 00:21:523542 return SQLITE_OK;
3543}
3544
3545/*
danb483eba2012-10-13 19:58:113546** Initialize the first page of the database file (creating a database
3547** consisting of a single page and no schema objects). Return SQLITE_OK
3548** if successful, or an SQLite error code otherwise.
3549*/
3550int sqlite3BtreeNewDb(Btree *p){
3551 int rc;
3552 sqlite3BtreeEnter(p);
3553 p->pBt->nPage = 0;
3554 rc = newDatabase(p->pBt);
3555 sqlite3BtreeLeave(p);
3556 return rc;
3557}
3558
3559/*
danielk1977ee5741e2004-05-31 10:01:343560** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:423561** is started if the second argument is nonzero, otherwise a read-
3562** transaction. If the second argument is 2 or more and exclusive
3563** transaction is started, meaning that no other process is allowed
3564** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:493565** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:423566** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:523567**
larrybrbc917382023-06-07 08:40:313568** A write-transaction must be started before attempting any
3569** changes to the database. None of the following routines
danielk1977ee5741e2004-05-31 10:01:343570** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:523571**
drh23e11ca2004-05-04 17:27:283572** sqlite3BtreeCreateTable()
3573** sqlite3BtreeCreateIndex()
3574** sqlite3BtreeClearTable()
3575** sqlite3BtreeDropTable()
3576** sqlite3BtreeInsert()
3577** sqlite3BtreeDelete()
3578** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:413579**
drhb8ef32c2005-03-14 02:01:493580** If an initial attempt to acquire the lock fails because of lock contention
3581** and the database was previously unlocked, then invoke the busy handler
3582** if there is one. But if there was previously a read-lock, do not
larrybrbc917382023-06-07 08:40:313583** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
drhb8ef32c2005-03-14 02:01:493584** returned when there is already a read-lock in order to avoid a deadlock.
3585**
3586** Suppose there are two processes A and B. A has a read lock and B has
3587** a reserved lock. B tries to promote to exclusive but is blocked because
3588** of A's read lock. A tries to promote to reserved but is blocked by B.
3589** One or the other of the two processes must give way or there can be
3590** no progress. By returning SQLITE_BUSY and not invoking the busy callback
3591** when A already has a read lock, we encourage A to give up and let B
3592** proceed.
drha059ad02001-04-17 20:09:113593*/
drha03be792023-05-17 19:23:243594static SQLITE_NOINLINE int btreeBeginTrans(
larrybrbc917382023-06-07 08:40:313595 Btree *p, /* The btree in which to start the transaction */
drha03be792023-05-17 19:23:243596 int wrflag, /* True to start a write transaction */
3597 int *pSchemaVersion /* Put schema version number here, if not NULL */
3598){
danielk1977aef0bf62005-12-30 16:28:013599 BtShared *pBt = p->pBt;
dan7bb8b8a2020-05-06 20:27:183600 Pager *pPager = pBt->pPager;
danielk1977ee5741e2004-05-31 10:01:343601 int rc = SQLITE_OK;
3602
drhd677b3d2007-08-20 22:48:413603 sqlite3BtreeEnter(p);
danielk1977aef0bf62005-12-30 16:28:013604 btreeIntegrity(p);
3605
danielk1977ee5741e2004-05-31 10:01:343606 /* If the btree is already in a write-transaction, or it
3607 ** is already in a read-transaction and a read-transaction
3608 ** is requested, this is a no-op.
3609 */
danielk1977aef0bf62005-12-30 16:28:013610 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
drhd677b3d2007-08-20 22:48:413611 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:343612 }
dan56c517a2013-09-26 11:04:333613 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
drhb8ef32c2005-03-14 02:01:493614
larrybrbc917382023-06-07 08:40:313615 if( (p->db->flags & SQLITE_ResetDatabase)
3616 && sqlite3PagerIsreadonly(pPager)==0
danea933f02018-07-19 11:44:023617 ){
3618 pBt->btsFlags &= ~BTS_READ_ONLY;
3619 }
3620
drhb8ef32c2005-03-14 02:01:493621 /* Write transactions are not possible on a read-only database */
drhc9166342012-01-05 23:32:063622 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
drhd677b3d2007-08-20 22:48:413623 rc = SQLITE_READONLY;
3624 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:343625 }
3626
danielk1977404ca072009-03-16 13:19:363627#ifndef SQLITE_OMIT_SHARED_CACHE
drh5a1fb182016-01-08 19:34:393628 {
3629 sqlite3 *pBlock = 0;
larrybrbc917382023-06-07 08:40:313630 /* If another database handle has already opened a write transaction
drh5a1fb182016-01-08 19:34:393631 ** on this shared-btree structure and a second write transaction is
3632 ** requested, return SQLITE_LOCKED.
3633 */
3634 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
3635 || (pBt->btsFlags & BTS_PENDING)!=0
3636 ){
3637 pBlock = pBt->pWriter->db;
3638 }else if( wrflag>1 ){
3639 BtLock *pIter;
3640 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3641 if( pIter->pBtree!=p ){
3642 pBlock = pIter->pBtree->db;
3643 break;
3644 }
danielk1977641b0f42007-12-21 04:47:253645 }
3646 }
drh5a1fb182016-01-08 19:34:393647 if( pBlock ){
3648 sqlite3ConnectionBlocked(p->db, pBlock);
3649 rc = SQLITE_LOCKED_SHAREDCACHE;
3650 goto trans_begun;
3651 }
danielk1977404ca072009-03-16 13:19:363652 }
danielk1977641b0f42007-12-21 04:47:253653#endif
3654
larrybrbc917382023-06-07 08:40:313655 /* Any read-only or read-write transaction implies a read-lock on
3656 ** page 1. So if some other shared-cache client already has a write-lock
danielk1977602b4662009-07-02 07:47:333657 ** on page 1, the transaction cannot be opened. */
drh346a70c2020-06-15 20:27:353658 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
drh4c301aa2009-07-15 17:25:453659 if( SQLITE_OK!=rc ) goto trans_begun;
danielk1977602b4662009-07-02 07:47:333660
drhc9166342012-01-05 23:32:063661 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
3662 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
drhb8ef32c2005-03-14 02:01:493663 do {
dan11a81822020-05-07 14:26:403664 sqlite3PagerWalDb(pPager, p->db);
dan58021b22020-05-05 20:30:073665
3666#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
3667 /* If transitioning from no transaction directly to a write transaction,
3668 ** block for the WRITER lock first if possible. */
3669 if( pBt->pPage1==0 && wrflag ){
3670 assert( pBt->inTransaction==TRANS_NONE );
dan861fb1e2020-05-06 19:14:413671 rc = sqlite3PagerWalWriteLock(pPager, 1);
dan7bb8b8a2020-05-06 20:27:183672 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
dan58021b22020-05-05 20:30:073673 }
3674#endif
3675
danielk1977295dc102009-04-01 19:07:033676 /* Call lockBtree() until either pBt->pPage1 is populated or
3677 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
3678 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
larrybrbc917382023-06-07 08:40:313679 ** reading page 1 it discovers that the page-size of the database
danielk1977295dc102009-04-01 19:07:033680 ** file is not pBt->pageSize. In this case lockBtree() will update
3681 ** pBt->pageSize to the page-size of the file on disk.
3682 */
3683 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
drh309169a2007-04-24 17:27:513684
drhb8ef32c2005-03-14 02:01:493685 if( rc==SQLITE_OK && wrflag ){
drhc9166342012-01-05 23:32:063686 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
drh309169a2007-04-24 17:27:513687 rc = SQLITE_READONLY;
3688 }else{
dan58021b22020-05-05 20:30:073689 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
drh309169a2007-04-24 17:27:513690 if( rc==SQLITE_OK ){
3691 rc = newDatabase(pBt);
dan8bf6d702018-07-05 17:16:553692 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
3693 /* if there was no transaction opened when this function was
3694 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
3695 ** code to SQLITE_BUSY. */
3696 rc = SQLITE_BUSY;
drh309169a2007-04-24 17:27:513697 }
drhb8ef32c2005-03-14 02:01:493698 }
3699 }
larrybrbc917382023-06-07 08:40:313700
danielk1977bd434552009-03-18 10:33:003701 if( rc!=SQLITE_OK ){
danfc87ab82020-05-06 19:22:593702 (void)sqlite3PagerWalWriteLock(pPager, 0);
drhb8ef32c2005-03-14 02:01:493703 unlockBtreeIfUnused(pBt);
3704 }
danded19592025-05-30 20:52:183705#if defined(SQLITE_ENABLE_SETLK_TIMEOUT)
dan88ed38c2025-05-30 18:23:033706 if( rc==SQLITE_BUSY_TIMEOUT ){
3707 /* If a blocking lock timed out, break out of the loop here so that
danded19592025-05-30 20:52:183708 ** the busy-handler is not invoked. */
dan88ed38c2025-05-30 18:23:033709 break;
3710 }
3711#endif
danf9b76712010-06-01 14:12:453712 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
danielk19771ceedd32008-11-19 10:22:333713 btreeInvokeBusyHandler(pBt) );
dan7bb8b8a2020-05-06 20:27:183714 sqlite3PagerWalDb(pPager, 0);
3715#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
3716 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
3717#endif
danielk1977aef0bf62005-12-30 16:28:013718
3719 if( rc==SQLITE_OK ){
3720 if( p->inTrans==TRANS_NONE ){
3721 pBt->nTransaction++;
danielk1977602b4662009-07-02 07:47:333722#ifndef SQLITE_OMIT_SHARED_CACHE
3723 if( p->sharable ){
drhf2f105d2012-08-20 15:53:543724 assert( p->lock.pBtree==p && p->lock.iTable==1 );
danielk1977602b4662009-07-02 07:47:333725 p->lock.eLock = READ_LOCK;
3726 p->lock.pNext = pBt->pLock;
3727 pBt->pLock = &p->lock;
3728 }
3729#endif
danielk1977aef0bf62005-12-30 16:28:013730 }
3731 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
3732 if( p->inTrans>pBt->inTransaction ){
3733 pBt->inTransaction = p->inTrans;
3734 }
danielk1977404ca072009-03-16 13:19:363735 if( wrflag ){
dan59257dc2010-08-04 11:34:313736 MemPage *pPage1 = pBt->pPage1;
3737#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977404ca072009-03-16 13:19:363738 assert( !pBt->pWriter );
3739 pBt->pWriter = p;
drhc9166342012-01-05 23:32:063740 pBt->btsFlags &= ~BTS_EXCLUSIVE;
3741 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
danielk1977641b0f42007-12-21 04:47:253742#endif
dan59257dc2010-08-04 11:34:313743
3744 /* If the db-size header field is incorrect (as it may be if an old
3745 ** client has been writing the database file), update it now. Doing
larrybrbc917382023-06-07 08:40:313746 ** this sooner rather than later means the database size can safely
dan59257dc2010-08-04 11:34:313747 ** re-read the database size from page 1 if a savepoint or transaction
3748 ** rollback occurs within the transaction.
3749 */
3750 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
3751 rc = sqlite3PagerWrite(pPage1->pDbPage);
3752 if( rc==SQLITE_OK ){
3753 put4byte(&pPage1->aData[28], pBt->nPage);
3754 }
3755 }
3756 }
danielk1977aef0bf62005-12-30 16:28:013757 }
3758
drhd677b3d2007-08-20 22:48:413759trans_begun:
drhbb2d9b12018-06-06 16:28:403760 if( rc==SQLITE_OK ){
3761 if( pSchemaVersion ){
3762 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
3763 }
3764 if( wrflag ){
3765 /* This call makes sure that the pager has the correct number of
3766 ** open savepoints. If the second parameter is greater than 0 and
3767 ** the sub-journal is not already open, then it will be opened here.
3768 */
dan7bb8b8a2020-05-06 20:27:183769 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
drhbb2d9b12018-06-06 16:28:403770 }
danielk1977fd7f0452008-12-17 17:30:263771 }
danielk197712dd5492008-12-18 15:45:073772
danielk1977aef0bf62005-12-30 16:28:013773 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:413774 sqlite3BtreeLeave(p);
drhb8ca3072001-12-05 00:21:203775 return rc;
drha059ad02001-04-17 20:09:113776}
drha03be792023-05-17 19:23:243777int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
3778 BtShared *pBt;
3779 if( p->sharable
3780 || p->inTrans==TRANS_NONE
3781 || (p->inTrans==TRANS_READ && wrflag!=0)
3782 ){
3783 return btreeBeginTrans(p,wrflag,pSchemaVersion);
3784 }
3785 pBt = p->pBt;
3786 if( pSchemaVersion ){
3787 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
3788 }
3789 if( wrflag ){
3790 /* This call makes sure that the pager has the correct number of
3791 ** open savepoints. If the second parameter is greater than 0 and
3792 ** the sub-journal is not already open, then it will be opened here.
3793 */
3794 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
3795 }else{
3796 return SQLITE_OK;
3797 }
3798}
drha059ad02001-04-17 20:09:113799
danielk1977687566d2004-11-02 12:56:413800#ifndef SQLITE_OMIT_AUTOVACUUM
3801
3802/*
3803** Set the pointer-map entries for all children of page pPage. Also, if
3804** pPage contains cells that point to overflow pages, set the pointer
3805** map entries for the overflow pages as well.
3806*/
3807static int setChildPtrmaps(MemPage *pPage){
3808 int i; /* Counter variable */
3809 int nCell; /* Number of cells in page pPage */
danielk19772df71c72007-05-24 07:22:423810 int rc; /* Return code */
danielk1977aef0bf62005-12-30 16:28:013811 BtShared *pBt = pPage->pBt;
danielk1977687566d2004-11-02 12:56:413812 Pgno pgno = pPage->pgno;
3813
drh1fee73e2007-08-29 04:00:573814 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh14e845a2017-05-25 21:35:563815 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
drh2a702542016-12-12 18:12:033816 if( rc!=SQLITE_OK ) return rc;
danielk1977687566d2004-11-02 12:56:413817 nCell = pPage->nCell;
3818
3819 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:433820 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:413821
drh0f1bf4c2019-01-13 20:17:213822 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
danielk197726836652005-01-17 01:33:133823
danielk1977687566d2004-11-02 12:56:413824 if( !pPage->leaf ){
3825 Pgno childPgno = get4byte(pCell);
drh98add2e2009-07-20 17:11:493826 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
danielk1977687566d2004-11-02 12:56:413827 }
3828 }
3829
3830 if( !pPage->leaf ){
3831 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh98add2e2009-07-20 17:11:493832 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
danielk1977687566d2004-11-02 12:56:413833 }
3834
danielk1977687566d2004-11-02 12:56:413835 return rc;
3836}
3837
3838/*
drhf3aed592009-07-08 18:12:493839** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
3840** that it points to iTo. Parameter eType describes the type of pointer to
3841** be modified, as follows:
danielk1977687566d2004-11-02 12:56:413842**
larrybrbc917382023-06-07 08:40:313843** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
danielk1977687566d2004-11-02 12:56:413844** page of pPage.
3845**
3846** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
3847** page pointed to by one of the cells on pPage.
3848**
3849** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
3850** overflow page in the list.
3851*/
danielk1977fdb7cdb2005-01-17 02:12:183852static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
drh1fee73e2007-08-29 04:00:573853 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhc5053fb2008-11-27 02:22:103854 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977687566d2004-11-02 12:56:413855 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:323856 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:183857 if( get4byte(pPage->aData)!=iFrom ){
daneebf2f52017-11-18 17:30:083858 return SQLITE_CORRUPT_PAGE(pPage);
danielk1977fdb7cdb2005-01-17 02:12:183859 }
danielk1977f78fc082004-11-02 14:40:323860 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:413861 }else{
danielk1977687566d2004-11-02 12:56:413862 int i;
3863 int nCell;
drha1f75d92015-05-24 10:18:123864 int rc;
danielk1977687566d2004-11-02 12:56:413865
drh14e845a2017-05-25 21:35:563866 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
drha1f75d92015-05-24 10:18:123867 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:413868 nCell = pPage->nCell;
3869
danielk1977687566d2004-11-02 12:56:413870 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:433871 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:413872 if( eType==PTRMAP_OVERFLOW1 ){
3873 CellInfo info;
drh5fa60512015-06-19 17:19:343874 pPage->xParseCell(pPage, pCell, &info);
drhb701c9a2017-01-12 15:11:033875 if( info.nLocal<info.nPayload ){
3876 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
daneebf2f52017-11-18 17:30:083877 return SQLITE_CORRUPT_PAGE(pPage);
drhb701c9a2017-01-12 15:11:033878 }
3879 if( iFrom==get4byte(pCell+info.nSize-4) ){
3880 put4byte(pCell+info.nSize-4, iTo);
3881 break;
3882 }
danielk1977687566d2004-11-02 12:56:413883 }
3884 }else{
drh005c9d82022-10-10 12:02:533885 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
3886 return SQLITE_CORRUPT_PAGE(pPage);
3887 }
danielk1977687566d2004-11-02 12:56:413888 if( get4byte(pCell)==iFrom ){
3889 put4byte(pCell, iTo);
3890 break;
3891 }
3892 }
3893 }
larrybrbc917382023-06-07 08:40:313894
danielk1977687566d2004-11-02 12:56:413895 if( i==nCell ){
larrybrbc917382023-06-07 08:40:313896 if( eType!=PTRMAP_BTREE ||
danielk1977fdb7cdb2005-01-17 02:12:183897 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
daneebf2f52017-11-18 17:30:083898 return SQLITE_CORRUPT_PAGE(pPage);
danielk1977fdb7cdb2005-01-17 02:12:183899 }
danielk1977687566d2004-11-02 12:56:413900 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
3901 }
danielk1977687566d2004-11-02 12:56:413902 }
danielk1977fdb7cdb2005-01-17 02:12:183903 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:413904}
3905
danielk1977003ba062004-11-04 02:57:333906
danielk19777701e812005-01-10 12:59:513907/*
larrybrbc917382023-06-07 08:40:313908** Move the open database page pDbPage to location iFreePage in the
danielk19777701e812005-01-10 12:59:513909** database. The pDbPage reference remains valid.
drhe64ca7b2009-07-16 18:21:173910**
3911** The isCommit flag indicates that there is no need to remember that
larrybrbc917382023-06-07 08:40:313912** the journal needs to be sync()ed before database page pDbPage->pgno
drhe64ca7b2009-07-16 18:21:173913** can be written to. The caller has already promised not to write to that
3914** page.
danielk19777701e812005-01-10 12:59:513915*/
danielk1977003ba062004-11-04 02:57:333916static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:013917 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:513918 MemPage *pDbPage, /* Open page to move */
3919 u8 eType, /* Pointer map 'type' entry for pDbPage */
3920 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
danielk19774c999992008-07-16 18:17:553921 Pgno iFreePage, /* The location to move pDbPage to */
drhe64ca7b2009-07-16 18:21:173922 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
danielk1977003ba062004-11-04 02:57:333923){
3924 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
3925 Pgno iDbPage = pDbPage->pgno;
3926 Pager *pPager = pBt->pPager;
3927 int rc;
3928
larrybrbc917382023-06-07 08:40:313929 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
danielk1977a0bf2652004-11-04 14:30:043930 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
drh1fee73e2007-08-29 04:00:573931 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:343932 assert( pDbPage->pBt==pBt );
drh49272bc2018-10-31 01:04:183933 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
danielk1977003ba062004-11-04 02:57:333934
drh85b623f2007-12-13 21:54:093935 /* Move page iDbPage from its current location to page number iFreePage */
larrybrbc917382023-06-07 08:40:313936 TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n",
danielk1977003ba062004-11-04 02:57:333937 iDbPage, iFreePage, iPtrPage, eType));
danielk19774c999992008-07-16 18:17:553938 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
danielk1977003ba062004-11-04 02:57:333939 if( rc!=SQLITE_OK ){
3940 return rc;
3941 }
3942 pDbPage->pgno = iFreePage;
3943
3944 /* If pDbPage was a btree-page, then it may have child pages and/or cells
3945 ** that point to overflow pages. The pointer map entries for all these
3946 ** pages need to be changed.
3947 **
3948 ** If pDbPage is an overflow page, then the first 4 bytes may store a
3949 ** pointer to a subsequent overflow page. If this is the case, then
3950 ** the pointer map needs to be updated for the subsequent overflow page.
3951 */
danielk1977a0bf2652004-11-04 14:30:043952 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:333953 rc = setChildPtrmaps(pDbPage);
3954 if( rc!=SQLITE_OK ){
3955 return rc;
3956 }
3957 }else{
3958 Pgno nextOvfl = get4byte(pDbPage->aData);
3959 if( nextOvfl!=0 ){
drh98add2e2009-07-20 17:11:493960 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
danielk1977003ba062004-11-04 02:57:333961 if( rc!=SQLITE_OK ){
3962 return rc;
3963 }
3964 }
3965 }
3966
3967 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
3968 ** that it points at iFreePage. Also fix the pointer map entry for
3969 ** iPtrPage.
3970 */
danielk1977a0bf2652004-11-04 14:30:043971 if( eType!=PTRMAP_ROOTPAGE ){
drhb00fc3b2013-08-21 23:42:323972 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:043973 if( rc!=SQLITE_OK ){
3974 return rc;
3975 }
danielk19773b8a05f2007-03-19 17:44:263976 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:043977 if( rc!=SQLITE_OK ){
3978 releasePage(pPtrPage);
3979 return rc;
3980 }
danielk1977fdb7cdb2005-01-17 02:12:183981 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:333982 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:183983 if( rc==SQLITE_OK ){
drh98add2e2009-07-20 17:11:493984 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
danielk1977fdb7cdb2005-01-17 02:12:183985 }
danielk1977003ba062004-11-04 02:57:333986 }
danielk1977003ba062004-11-04 02:57:333987 return rc;
3988}
3989
danielk1977dddbcdc2007-04-26 14:42:343990/* Forward declaration required by incrVacuumStep(). */
drh4f0c5872007-03-26 22:05:013991static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:413992
3993/*
dan51f0b6d2013-02-22 20:16:343994** Perform a single step of an incremental-vacuum. If successful, return
larrybrbc917382023-06-07 08:40:313995** SQLITE_OK. If there is no work to do (and therefore no point in
3996** calling this function again), return SQLITE_DONE. Or, if an error
dan51f0b6d2013-02-22 20:16:343997** occurs, return some other error code.
danielk1977dddbcdc2007-04-26 14:42:343998**
larrybrbc917382023-06-07 08:40:313999** More specifically, this function attempts to re-organize the database so
dan51f0b6d2013-02-22 20:16:344000** that the last page of the file currently in use is no longer in use.
danielk1977dddbcdc2007-04-26 14:42:344001**
dan51f0b6d2013-02-22 20:16:344002** Parameter nFin is the number of pages that this database would contain
4003** were this function called until it returns SQLITE_DONE.
4004**
larrybrbc917382023-06-07 08:40:314005** If the bCommit parameter is non-zero, this function assumes that the
4006** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
4007** or an error. bCommit is passed true for an auto-vacuum-on-commit
dan51f0b6d2013-02-22 20:16:344008** operation, or false for an incremental vacuum.
danielk1977dddbcdc2007-04-26 14:42:344009*/
dan51f0b6d2013-02-22 20:16:344010static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
danielk1977dddbcdc2007-04-26 14:42:344011 Pgno nFreeList; /* Number of pages still on the free-list */
drhdd3cd972010-03-27 17:12:364012 int rc;
danielk1977dddbcdc2007-04-26 14:42:344013
drh1fee73e2007-08-29 04:00:574014 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977fa542f12009-04-02 18:28:084015 assert( iLastPg>nFin );
danielk1977dddbcdc2007-04-26 14:42:344016
4017 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
danielk1977dddbcdc2007-04-26 14:42:344018 u8 eType;
4019 Pgno iPtrPage;
4020
4021 nFreeList = get4byte(&pBt->pPage1->aData[36]);
danielk1977fa542f12009-04-02 18:28:084022 if( nFreeList==0 ){
danielk1977dddbcdc2007-04-26 14:42:344023 return SQLITE_DONE;
4024 }
4025
4026 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
4027 if( rc!=SQLITE_OK ){
4028 return rc;
4029 }
4030 if( eType==PTRMAP_ROOTPAGE ){
4031 return SQLITE_CORRUPT_BKPT;
4032 }
4033
4034 if( eType==PTRMAP_FREEPAGE ){
dan51f0b6d2013-02-22 20:16:344035 if( bCommit==0 ){
danielk1977dddbcdc2007-04-26 14:42:344036 /* Remove the page from the files free-list. This is not required
dan51f0b6d2013-02-22 20:16:344037 ** if bCommit is non-zero. In that case, the free-list will be
larrybrbc917382023-06-07 08:40:314038 ** truncated to zero after this function returns, so it doesn't
danielk1977dddbcdc2007-04-26 14:42:344039 ** matter if it still contains some garbage entries.
4040 */
4041 Pgno iFreePg;
4042 MemPage *pFreePg;
dan51f0b6d2013-02-22 20:16:344043 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
danielk1977dddbcdc2007-04-26 14:42:344044 if( rc!=SQLITE_OK ){
4045 return rc;
4046 }
4047 assert( iFreePg==iLastPg );
4048 releasePage(pFreePg);
4049 }
4050 } else {
4051 Pgno iFreePg; /* Index of free page to move pLastPg to */
4052 MemPage *pLastPg;
dan51f0b6d2013-02-22 20:16:344053 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
4054 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
danielk1977dddbcdc2007-04-26 14:42:344055
drhb00fc3b2013-08-21 23:42:324056 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
danielk1977dddbcdc2007-04-26 14:42:344057 if( rc!=SQLITE_OK ){
4058 return rc;
4059 }
4060
dan51f0b6d2013-02-22 20:16:344061 /* If bCommit is zero, this loop runs exactly once and page pLastPg
danielk1977b4626a32007-04-28 15:47:434062 ** is swapped with the first free page pulled off the free list.
4063 **
dan51f0b6d2013-02-22 20:16:344064 ** On the other hand, if bCommit is greater than zero, then keep
danielk1977b4626a32007-04-28 15:47:434065 ** looping until a free-page located within the first nFin pages
4066 ** of the file is found.
4067 */
dan51f0b6d2013-02-22 20:16:344068 if( bCommit==0 ){
4069 eMode = BTALLOC_LE;
4070 iNear = nFin;
4071 }
danielk1977dddbcdc2007-04-26 14:42:344072 do {
4073 MemPage *pFreePg;
drhdba3a5a2022-06-15 14:57:044074 Pgno dbSize = btreePagecount(pBt);
dan51f0b6d2013-02-22 20:16:344075 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
danielk1977dddbcdc2007-04-26 14:42:344076 if( rc!=SQLITE_OK ){
4077 releasePage(pLastPg);
4078 return rc;
4079 }
4080 releasePage(pFreePg);
drhdba3a5a2022-06-15 14:57:044081 if( iFreePg>dbSize ){
4082 releasePage(pLastPg);
4083 return SQLITE_CORRUPT_BKPT;
4084 }
dan51f0b6d2013-02-22 20:16:344085 }while( bCommit && iFreePg>nFin );
danielk1977dddbcdc2007-04-26 14:42:344086 assert( iFreePg<iLastPg );
larrybrbc917382023-06-07 08:40:314087
dane1df4e32013-03-05 11:27:044088 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
danielk1977dddbcdc2007-04-26 14:42:344089 releasePage(pLastPg);
4090 if( rc!=SQLITE_OK ){
4091 return rc;
danielk1977662278e2007-11-05 15:30:124092 }
danielk1977dddbcdc2007-04-26 14:42:344093 }
4094 }
4095
dan51f0b6d2013-02-22 20:16:344096 if( bCommit==0 ){
danbc1a3c62013-02-23 16:40:464097 do {
danielk19773460d192008-12-27 15:23:134098 iLastPg--;
danbc1a3c62013-02-23 16:40:464099 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
4100 pBt->bDoTruncate = 1;
drhdd3cd972010-03-27 17:12:364101 pBt->nPage = iLastPg;
danielk1977dddbcdc2007-04-26 14:42:344102 }
4103 return SQLITE_OK;
4104}
4105
4106/*
dan51f0b6d2013-02-22 20:16:344107** The database opened by the first argument is an auto-vacuum database
larrybrbc917382023-06-07 08:40:314108** nOrig pages in size containing nFree free pages. Return the expected
dan51f0b6d2013-02-22 20:16:344109** size of the database in pages following an auto-vacuum operation.
4110*/
4111static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
4112 int nEntry; /* Number of entries on one ptrmap page */
4113 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
4114 Pgno nFin; /* Return value */
4115
4116 nEntry = pBt->usableSize/5;
4117 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
4118 nFin = nOrig - nFree - nPtrmap;
4119 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
4120 nFin--;
4121 }
4122 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
4123 nFin--;
4124 }
dan51f0b6d2013-02-22 20:16:344125
4126 return nFin;
4127}
4128
4129/*
danielk1977dddbcdc2007-04-26 14:42:344130** A write-transaction must be opened before calling this function.
4131** It performs a single unit of work towards an incremental vacuum.
4132**
4133** If the incremental vacuum is finished after this function has run,
shanebe217792009-03-05 04:20:314134** SQLITE_DONE is returned. If it is not finished, but no error occurred,
larrybrbc917382023-06-07 08:40:314135** SQLITE_OK is returned. Otherwise an SQLite error code.
danielk1977dddbcdc2007-04-26 14:42:344136*/
4137int sqlite3BtreeIncrVacuum(Btree *p){
drhd677b3d2007-08-20 22:48:414138 int rc;
danielk1977dddbcdc2007-04-26 14:42:344139 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:414140
4141 sqlite3BtreeEnter(p);
danielk1977dddbcdc2007-04-26 14:42:344142 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
4143 if( !pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:414144 rc = SQLITE_DONE;
4145 }else{
dan51f0b6d2013-02-22 20:16:344146 Pgno nOrig = btreePagecount(pBt);
4147 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
4148 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
4149
drhbc2cf3b2020-07-14 12:40:534150 if( nOrig<nFin || nFree>=nOrig ){
dan91384712013-02-24 11:50:434151 rc = SQLITE_CORRUPT_BKPT;
4152 }else if( nFree>0 ){
dan11dcd112013-03-15 18:29:184153 rc = saveAllCursors(pBt, 0, 0);
4154 if( rc==SQLITE_OK ){
4155 invalidateAllOverflowCache(pBt);
4156 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
4157 }
dan51f0b6d2013-02-22 20:16:344158 if( rc==SQLITE_OK ){
4159 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
4160 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
4161 }
4162 }else{
4163 rc = SQLITE_DONE;
drhdd3cd972010-03-27 17:12:364164 }
danielk1977dddbcdc2007-04-26 14:42:344165 }
drhd677b3d2007-08-20 22:48:414166 sqlite3BtreeLeave(p);
4167 return rc;
danielk1977dddbcdc2007-04-26 14:42:344168}
4169
4170/*
danielk19773b8a05f2007-03-19 17:44:264171** This routine is called prior to sqlite3PagerCommit when a transaction
drhf7b54962013-05-28 12:11:544172** is committed for an auto-vacuum database.
danielk1977687566d2004-11-02 12:56:414173*/
drh1bbfc672021-10-15 23:02:274174static int autoVacuumCommit(Btree *p){
danielk1977dddbcdc2007-04-26 14:42:344175 int rc = SQLITE_OK;
drh1bbfc672021-10-15 23:02:274176 Pager *pPager;
4177 BtShared *pBt;
4178 sqlite3 *db;
4179 VVA_ONLY( int nRef );
4180
4181 assert( p!=0 );
larrybrbc917382023-06-07 08:40:314182 pBt = p->pBt;
drh1bbfc672021-10-15 23:02:274183 pPager = pBt->pPager;
4184 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
danielk1977687566d2004-11-02 12:56:414185
drh1fee73e2007-08-29 04:00:574186 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:564187 invalidateAllOverflowCache(pBt);
danielk1977dddbcdc2007-04-26 14:42:344188 assert(pBt->autoVacuum);
4189 if( !pBt->incrVacuum ){
drhea8ffdf2009-07-22 00:35:234190 Pgno nFin; /* Number of pages in database after autovacuuming */
4191 Pgno nFree; /* Number of pages on the freelist initially */
drh1bbfc672021-10-15 23:02:274192 Pgno nVac; /* Number of pages to vacuum */
drh41d628c2009-07-11 17:04:084193 Pgno iFree; /* The next page to be freed */
drh41d628c2009-07-11 17:04:084194 Pgno nOrig; /* Database size before freeing */
danielk1977687566d2004-11-02 12:56:414195
drhb1299152010-03-30 22:58:334196 nOrig = btreePagecount(pBt);
danielk1977ef165ce2009-04-06 17:50:034197 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
4198 /* It is not possible to create a database for which the final page
4199 ** is either a pointer-map page or the pending-byte page. If one
4200 ** is encountered, this indicates corruption.
4201 */
danielk19773460d192008-12-27 15:23:134202 return SQLITE_CORRUPT_BKPT;
4203 }
danielk1977ef165ce2009-04-06 17:50:034204
danielk19773460d192008-12-27 15:23:134205 nFree = get4byte(&pBt->pPage1->aData[36]);
drh1bbfc672021-10-15 23:02:274206 db = p->db;
4207 if( db->xAutovacPages ){
4208 int iDb;
4209 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
4210 if( db->aDb[iDb].pBt==p ) break;
4211 }
4212 nVac = db->xAutovacPages(
4213 db->pAutovacPagesArg,
4214 db->aDb[iDb].zDbSName,
4215 nOrig,
4216 nFree,
4217 pBt->pageSize
4218 );
4219 if( nVac>nFree ){
4220 nVac = nFree;
4221 }
4222 if( nVac==0 ){
4223 return SQLITE_OK;
4224 }
4225 }else{
4226 nVac = nFree;
4227 }
4228 nFin = finalDbSize(pBt, nOrig, nVac);
drhc5e47ac2009-06-04 00:11:564229 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
dan0aed84d2013-03-26 14:16:204230 if( nFin<nOrig ){
4231 rc = saveAllCursors(pBt, 0, 0);
4232 }
danielk19773460d192008-12-27 15:23:134233 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
drh1bbfc672021-10-15 23:02:274234 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
danielk1977dddbcdc2007-04-26 14:42:344235 }
danielk19773460d192008-12-27 15:23:134236 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
danielk19773460d192008-12-27 15:23:134237 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
drh1bbfc672021-10-15 23:02:274238 if( nVac==nFree ){
4239 put4byte(&pBt->pPage1->aData[32], 0);
4240 put4byte(&pBt->pPage1->aData[36], 0);
4241 }
drhdd3cd972010-03-27 17:12:364242 put4byte(&pBt->pPage1->aData[28], nFin);
danbc1a3c62013-02-23 16:40:464243 pBt->bDoTruncate = 1;
drhdd3cd972010-03-27 17:12:364244 pBt->nPage = nFin;
danielk1977dddbcdc2007-04-26 14:42:344245 }
4246 if( rc!=SQLITE_OK ){
4247 sqlite3PagerRollback(pPager);
4248 }
danielk1977687566d2004-11-02 12:56:414249 }
4250
dan0aed84d2013-03-26 14:16:204251 assert( nRef>=sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:414252 return rc;
4253}
danielk1977dddbcdc2007-04-26 14:42:344254
danielk1977a50d9aa2009-06-08 14:49:454255#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
4256# define setChildPtrmaps(x) SQLITE_OK
4257#endif
danielk1977687566d2004-11-02 12:56:414258
4259/*
drh80e35f42007-03-30 14:06:344260** This routine does the first phase of a two-phase commit. This routine
4261** causes a rollback journal to be created (if it does not already exist)
4262** and populated with enough information so that if a power loss occurs
4263** the database can be restored to its original state by playing back
4264** the journal. Then the contents of the journal are flushed out to
4265** the disk. After the journal is safely on oxide, the changes to the
4266** database are written into the database file and flushed to oxide.
4267** At the end of this call, the rollback journal still exists on the
4268** disk and we are still holding all locks, so the transaction has not
drh51898cf2009-04-19 20:51:064269** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
drh80e35f42007-03-30 14:06:344270** commit process.
4271**
4272** This call is a no-op if no write-transaction is currently active on pBt.
4273**
drh067b92b2020-06-19 15:24:124274** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
4275** the name of a super-journal file that should be written into the
larrybrbc917382023-06-07 08:40:314276** individual journal file, or is NULL, indicating no super-journal file
drh80e35f42007-03-30 14:06:344277** (single database transaction).
4278**
drh067b92b2020-06-19 15:24:124279** When this is called, the super-journal should already have been
drh80e35f42007-03-30 14:06:344280** created, populated with this journal pointer and synced to disk.
4281**
4282** Once this is routine has returned, the only thing required to commit
4283** the write-transaction for this database file is to delete the journal.
4284*/
drh067b92b2020-06-19 15:24:124285int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
drh80e35f42007-03-30 14:06:344286 int rc = SQLITE_OK;
4287 if( p->inTrans==TRANS_WRITE ){
4288 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:414289 sqlite3BtreeEnter(p);
drh80e35f42007-03-30 14:06:344290#ifndef SQLITE_OMIT_AUTOVACUUM
4291 if( pBt->autoVacuum ){
drh1bbfc672021-10-15 23:02:274292 rc = autoVacuumCommit(p);
drh80e35f42007-03-30 14:06:344293 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:414294 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:344295 return rc;
4296 }
4297 }
danbc1a3c62013-02-23 16:40:464298 if( pBt->bDoTruncate ){
4299 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
4300 }
drh80e35f42007-03-30 14:06:344301#endif
drh067b92b2020-06-19 15:24:124302 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
drhd677b3d2007-08-20 22:48:414303 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:344304 }
4305 return rc;
4306}
4307
4308/*
danielk197794b30732009-07-02 17:21:574309** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
4310** at the conclusion of a transaction.
4311*/
4312static void btreeEndTransaction(Btree *p){
4313 BtShared *pBt = p->pBt;
drh1713afb2013-06-28 01:24:574314 sqlite3 *db = p->db;
danielk197794b30732009-07-02 17:21:574315 assert( sqlite3BtreeHoldsMutex(p) );
4316
danbc1a3c62013-02-23 16:40:464317#ifndef SQLITE_OMIT_AUTOVACUUM
4318 pBt->bDoTruncate = 0;
4319#endif
danc0537fe2013-06-28 19:41:434320 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
danfa401de2009-10-16 14:55:034321 /* If there are other active statements that belong to this database
4322 ** handle, downgrade to a read-only transaction. The other statements
4323 ** may still be reading from the database. */
danielk197794b30732009-07-02 17:21:574324 downgradeAllSharedCacheTableLocks(p);
4325 p->inTrans = TRANS_READ;
4326 }else{
larrybrbc917382023-06-07 08:40:314327 /* If the handle had any kind of transaction open, decrement the
4328 ** transaction count of the shared btree. If the transaction count
danielk197794b30732009-07-02 17:21:574329 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
4330 ** call below will unlock the pager. */
4331 if( p->inTrans!=TRANS_NONE ){
4332 clearAllSharedCacheTableLocks(p);
4333 pBt->nTransaction--;
4334 if( 0==pBt->nTransaction ){
4335 pBt->inTransaction = TRANS_NONE;
4336 }
4337 }
4338
larrybrbc917382023-06-07 08:40:314339 /* Set the current transaction state to TRANS_NONE and unlock the
danielk197794b30732009-07-02 17:21:574340 ** pager if this call closed the only read or write transaction. */
4341 p->inTrans = TRANS_NONE;
4342 unlockBtreeIfUnused(pBt);
4343 }
4344
4345 btreeIntegrity(p);
4346}
4347
4348/*
drh2aa679f2001-06-25 02:11:074349** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:564350**
drh6e345992007-03-30 11:12:084351** This routine implements the second phase of a 2-phase commit. The
drh51898cf2009-04-19 20:51:064352** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
4353** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
4354** routine did all the work of writing information out to disk and flushing the
drh6e345992007-03-30 11:12:084355** contents so that they are written onto the disk platter. All this
drh51898cf2009-04-19 20:51:064356** routine has to do is delete or truncate or zero the header in the
4357** the rollback journal (which causes the transaction to commit) and
4358** drop locks.
drh6e345992007-03-30 11:12:084359**
larrybrbc917382023-06-07 08:40:314360** Normally, if an error occurs while the pager layer is attempting to
dan60939d02011-03-29 15:40:554361** finalize the underlying journal file, this function returns an error and
4362** the upper layer will attempt a rollback. However, if the second argument
larrybrbc917382023-06-07 08:40:314363** is non-zero then this b-tree transaction is part of a multi-file
4364** transaction. In this case, the transaction has already been committed
4365** (by deleting a super-journal file) and the caller will ignore this
dan60939d02011-03-29 15:40:554366** functions return code. So, even if an error occurs in the pager layer,
4367** reset the b-tree objects internal state to indicate that the write
4368** transaction has been closed. This is quite safe, as the pager will have
4369** transitioned to the error state.
4370**
drh5e00f6c2001-09-13 13:46:564371** This will release the write lock on the database file. If there
4372** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:114373*/
dan60939d02011-03-29 15:40:554374int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
danielk1977aef0bf62005-12-30 16:28:014375
drh075ed302010-10-14 01:17:304376 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
drhd677b3d2007-08-20 22:48:414377 sqlite3BtreeEnter(p);
danielk1977aef0bf62005-12-30 16:28:014378 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:014379
larrybrbc917382023-06-07 08:40:314380 /* If the handle has a write-transaction open, commit the shared-btrees
danielk1977aef0bf62005-12-30 16:28:014381 ** transaction and set the shared state to TRANS_READ.
4382 */
4383 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:474384 int rc;
drh075ed302010-10-14 01:17:304385 BtShared *pBt = p->pBt;
danielk1977aef0bf62005-12-30 16:28:014386 assert( pBt->inTransaction==TRANS_WRITE );
4387 assert( pBt->nTransaction>0 );
drh80e35f42007-03-30 14:06:344388 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
dan60939d02011-03-29 15:40:554389 if( rc!=SQLITE_OK && bCleanup==0 ){
drhd677b3d2007-08-20 22:48:414390 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:474391 return rc;
4392 }
drh2b994ce2021-03-18 12:36:094393 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
danielk1977aef0bf62005-12-30 16:28:014394 pBt->inTransaction = TRANS_READ;
danbf0e57a2013-05-14 20:36:314395 btreeClearHasContent(pBt);
danielk1977ee5741e2004-05-31 10:01:344396 }
danielk1977aef0bf62005-12-30 16:28:014397
danielk197794b30732009-07-02 17:21:574398 btreeEndTransaction(p);
drhd677b3d2007-08-20 22:48:414399 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:474400 return SQLITE_OK;
drha059ad02001-04-17 20:09:114401}
4402
drh80e35f42007-03-30 14:06:344403/*
4404** Do both phases of a commit.
4405*/
4406int sqlite3BtreeCommit(Btree *p){
4407 int rc;
drhd677b3d2007-08-20 22:48:414408 sqlite3BtreeEnter(p);
drh80e35f42007-03-30 14:06:344409 rc = sqlite3BtreeCommitPhaseOne(p, 0);
4410 if( rc==SQLITE_OK ){
dan60939d02011-03-29 15:40:554411 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
drh80e35f42007-03-30 14:06:344412 }
drhd677b3d2007-08-20 22:48:414413 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:344414 return rc;
4415}
4416
drhc39e0002004-05-07 23:50:574417/*
drhfb982642007-08-30 01:19:594418** This routine sets the state to CURSOR_FAULT and the error
drh47b7fc72014-11-11 01:33:574419** code to errCode for every cursor on any BtShared that pBtree
4420** references. Or if the writeOnly flag is set to 1, then only
4421** trip write cursors and leave read cursors unchanged.
drhfb982642007-08-30 01:19:594422**
drh47b7fc72014-11-11 01:33:574423** Every cursor is a candidate to be tripped, including cursors
4424** that belong to other database connections that happen to be
4425** sharing the cache with pBtree.
drhfb982642007-08-30 01:19:594426**
dan80231042014-11-12 14:56:024427** This routine gets called when a rollback occurs. If the writeOnly
4428** flag is true, then only write-cursors need be tripped - read-only
larrybrbc917382023-06-07 08:40:314429** cursors save their current positions so that they may continue
4430** following the rollback. Or, if writeOnly is false, all cursors are
dan80231042014-11-12 14:56:024431** tripped. In general, writeOnly is false if the transaction being
4432** rolled back modified the database schema. In this case b-tree root
4433** pages may be moved or deleted from the database altogether, making
4434** it unsafe for read cursors to continue.
4435**
larrybrbc917382023-06-07 08:40:314436** If the writeOnly flag is true and an error is encountered while
4437** saving the current position of a read-only cursor, all cursors,
dan80231042014-11-12 14:56:024438** including all read-cursors are tripped.
4439**
4440** SQLITE_OK is returned if successful, or if an error occurs while
4441** saving a cursor position, an SQLite error code.
drhfb982642007-08-30 01:19:594442*/
dan80231042014-11-12 14:56:024443int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
drhfb982642007-08-30 01:19:594444 BtCursor *p;
dan80231042014-11-12 14:56:024445 int rc = SQLITE_OK;
4446
drh47b7fc72014-11-11 01:33:574447 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
dan80231042014-11-12 14:56:024448 if( pBtree ){
4449 sqlite3BtreeEnter(pBtree);
4450 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
dan80231042014-11-12 14:56:024451 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
drhd2f83132015-03-25 17:35:014452 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
drhbea3b972014-11-18 20:22:054453 rc = saveCursorPosition(p);
dan80231042014-11-12 14:56:024454 if( rc!=SQLITE_OK ){
4455 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
4456 break;
4457 }
4458 }
4459 }else{
4460 sqlite3BtreeClearCursor(p);
4461 p->eState = CURSOR_FAULT;
4462 p->skipNext = errCode;
4463 }
drh85ef6302017-08-02 15:50:094464 btreeReleaseAllCursorPages(p);
danielk1977bc2ca9e2008-11-13 14:28:284465 }
dan80231042014-11-12 14:56:024466 sqlite3BtreeLeave(pBtree);
drhfb982642007-08-30 01:19:594467 }
dan80231042014-11-12 14:56:024468 return rc;
drhfb982642007-08-30 01:19:594469}
4470
4471/*
drh41422652019-05-10 14:34:184472** Set the pBt->nPage field correctly, according to the current
4473** state of the database. Assume pBt->pPage1 is valid.
4474*/
4475static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
4476 int nPage = get4byte(&pPage1->aData[28]);
4477 testcase( nPage==0 );
4478 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
mistachkin2b5fbb22021-12-31 18:26:504479 testcase( pBt->nPage!=(u32)nPage );
drh41422652019-05-10 14:34:184480 pBt->nPage = nPage;
4481}
4482
4483/*
drh47b7fc72014-11-11 01:33:574484** Rollback the transaction in progress.
4485**
4486** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
4487** Only write cursors are tripped if writeOnly is true but all cursors are
4488** tripped if writeOnly is false. Any attempt to use
4489** a tripped cursor will result in an error.
drh5e00f6c2001-09-13 13:46:564490**
4491** This will release the write lock on the database file. If there
4492** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:114493*/
drh47b7fc72014-11-11 01:33:574494int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
danielk19778d34dfd2006-01-24 16:37:574495 int rc;
danielk1977aef0bf62005-12-30 16:28:014496 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:474497 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:014498
drh47b7fc72014-11-11 01:33:574499 assert( writeOnly==1 || writeOnly==0 );
4500 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
drhd677b3d2007-08-20 22:48:414501 sqlite3BtreeEnter(p);
drh0f198a72012-02-13 16:43:164502 if( tripCode==SQLITE_OK ){
4503 rc = tripCode = saveAllCursors(pBt, 0, 0);
drh47b7fc72014-11-11 01:33:574504 if( rc ) writeOnly = 0;
drh0f198a72012-02-13 16:43:164505 }else{
4506 rc = SQLITE_OK;
danielk19772b8c13e2006-01-24 14:21:244507 }
drh0f198a72012-02-13 16:43:164508 if( tripCode ){
dan80231042014-11-12 14:56:024509 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
4510 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
4511 if( rc2!=SQLITE_OK ) rc = rc2;
drh0f198a72012-02-13 16:43:164512 }
danielk1977aef0bf62005-12-30 16:28:014513 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:014514
4515 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:574516 int rc2;
danielk1977aef0bf62005-12-30 16:28:014517
danielk19778d34dfd2006-01-24 16:37:574518 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:264519 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:574520 if( rc2!=SQLITE_OK ){
4521 rc = rc2;
4522 }
4523
drh24cd67e2004-05-10 16:18:474524 /* The rollback may have destroyed the pPage1->aData value. So
danielk197730548662009-07-09 05:07:374525 ** call btreeGetPage() on page 1 again to make
drh16a9b832007-05-05 18:39:254526 ** sure pPage1->aData is set correctly. */
drhb00fc3b2013-08-21 23:42:324527 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh41422652019-05-10 14:34:184528 btreeSetNPage(pBt, pPage1);
drh3908fe92017-09-01 14:50:194529 releasePageOne(pPage1);
drh24cd67e2004-05-10 16:18:474530 }
drh85ec3b62013-05-14 23:12:064531 assert( countValidCursors(pBt, 1)==0 );
danielk1977aef0bf62005-12-30 16:28:014532 pBt->inTransaction = TRANS_READ;
danbf0e57a2013-05-14 20:36:314533 btreeClearHasContent(pBt);
drh24cd67e2004-05-10 16:18:474534 }
danielk1977aef0bf62005-12-30 16:28:014535
danielk197794b30732009-07-02 17:21:574536 btreeEndTransaction(p);
drhd677b3d2007-08-20 22:48:414537 sqlite3BtreeLeave(p);
drha059ad02001-04-17 20:09:114538 return rc;
4539}
4540
4541/*
peter.d.reid60ec9142014-09-06 16:39:464542** Start a statement subtransaction. The subtransaction can be rolled
larrybrbc917382023-06-07 08:40:314543** back independently of the main transaction. You must start a transaction
4544** before starting a subtransaction. The subtransaction is ended automatically
danielk1977bd434552009-03-18 10:33:004545** if the main transaction commits or rolls back.
drhab01f612004-05-22 02:55:234546**
4547** Statement subtransactions are used around individual SQL statements
4548** that are contained within a BEGIN...COMMIT block. If a constraint
4549** error occurs within the statement, the effect of that one statement
4550** can be rolled back without having to rollback the entire transaction.
danielk1977bd434552009-03-18 10:33:004551**
4552** A statement sub-transaction is implemented as an anonymous savepoint. The
4553** value passed as the second parameter is the total number of savepoints,
4554** including the new anonymous savepoint, open on the B-Tree. i.e. if there
4555** are no active savepoints and no other statement-transactions open,
4556** iStatement is 1. This anonymous savepoint can be released or rolled back
4557** using the sqlite3BtreeSavepoint() function.
drh663fc632002-02-02 18:49:194558*/
danielk1977bd434552009-03-18 10:33:004559int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
drh663fc632002-02-02 18:49:194560 int rc;
danielk1977aef0bf62005-12-30 16:28:014561 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:414562 sqlite3BtreeEnter(p);
drh64022502009-01-09 14:11:044563 assert( p->inTrans==TRANS_WRITE );
drhc9166342012-01-05 23:32:064564 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
danielk1977bd434552009-03-18 10:33:004565 assert( iStatement>0 );
4566 assert( iStatement>p->db->nSavepoint );
drh5e0ccc22010-03-29 19:36:524567 assert( pBt->inTransaction==TRANS_WRITE );
4568 /* At the pager level, a statement transaction is a savepoint with
4569 ** an index greater than all savepoints created explicitly using
4570 ** SQL statements. It is illegal to open, release or rollback any
4571 ** such savepoints while the statement transaction savepoint is active.
4572 */
4573 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
drhd677b3d2007-08-20 22:48:414574 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:194575 return rc;
4576}
4577
4578/*
danielk1977fd7f0452008-12-17 17:30:264579** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
4580** or SAVEPOINT_RELEASE. This function either releases or rolls back the
larrybrbc917382023-06-07 08:40:314581** savepoint identified by parameter iSavepoint, depending on the value
danielk197712dd5492008-12-18 15:45:074582** of op.
4583**
4584** Normally, iSavepoint is greater than or equal to zero. However, if op is
larrybrbc917382023-06-07 08:40:314585** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
danielk197712dd5492008-12-18 15:45:074586** contents of the entire transaction are rolled back. This is different
4587** from a normal transaction rollback, as no locks are released and the
4588** transaction remains open.
danielk1977fd7f0452008-12-17 17:30:264589*/
4590int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
4591 int rc = SQLITE_OK;
4592 if( p && p->inTrans==TRANS_WRITE ){
4593 BtShared *pBt = p->pBt;
danielk1977fd7f0452008-12-17 17:30:264594 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4595 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
4596 sqlite3BtreeEnter(p);
drh2343c7e2017-02-02 00:46:554597 if( op==SAVEPOINT_ROLLBACK ){
4598 rc = saveAllCursors(pBt, 0, 0);
4599 }
4600 if( rc==SQLITE_OK ){
4601 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
4602 }
drh9f0bbf92009-01-02 21:08:094603 if( rc==SQLITE_OK ){
drhc9166342012-01-05 23:32:064604 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
4605 pBt->nPage = 0;
4606 }
drh9f0bbf92009-01-02 21:08:094607 rc = newDatabase(pBt);
drh41422652019-05-10 14:34:184608 btreeSetNPage(pBt, pBt->pPage1);
drhb9b49bf2010-08-05 03:21:394609
larrybrbc917382023-06-07 08:40:314610 /* pBt->nPage might be zero if the database was corrupt when
dana9a54652019-04-22 11:47:404611 ** the transaction was started. Otherwise, it must be at least 1. */
4612 assert( CORRUPT_DB || pBt->nPage>0 );
drh9f0bbf92009-01-02 21:08:094613 }
danielk1977fd7f0452008-12-17 17:30:264614 sqlite3BtreeLeave(p);
4615 }
4616 return rc;
4617}
4618
4619/*
drh8b2f49b2001-06-08 00:21:524620** Create a new cursor for the BTree whose root is on the page
danielk19773e8add92009-07-04 17:16:004621** iTable. If a read-only cursor is requested, it is assumed that
4622** the caller already has at least a read-only transaction open
4623** on the database already. If a write-cursor is requested, then
4624** the caller is assumed to have an open write transaction.
drh1bee3d72001-10-15 00:44:354625**
drhe807bdb2016-01-21 17:06:334626** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
4627** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
4628** can be used for reading or for writing if other conditions for writing
4629** are also met. These are the conditions that must be met in order
4630** for writing to be allowed:
drh6446c4d2001-12-15 14:22:184631**
drhe807bdb2016-01-21 17:06:334632** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
drhf74b8d92002-09-01 23:20:454633**
drhfe5d71d2007-03-19 11:54:104634** 2: Other database connections that share the same pager cache
4635** but which are not in the READ_UNCOMMITTED state may not have
4636** cursors open with wrFlag==0 on the same table. Otherwise
4637** the changes made by this write cursor would be visible to
4638** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:454639**
4640** 3: The database must be writable (not on read-only media)
4641**
4642** 4: There must be an active transaction.
4643**
drhe807bdb2016-01-21 17:06:334644** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
4645** is set. If FORDELETE is set, that is a hint to the implementation that
4646** this cursor will only be used to seek to and delete entries of an index
4647** as part of a larger DELETE statement. The FORDELETE hint is not used by
larrybrbc917382023-06-07 08:40:314648** this implementation. But in a hypothetical alternative storage engine
drhe807bdb2016-01-21 17:06:334649** in which index entries are automatically deleted when corresponding table
4650** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
larrybrbc917382023-06-07 08:40:314651** operations on this cursor can be no-ops and all READ operations can
drhe807bdb2016-01-21 17:06:334652** return a null row (2-bytes: 0x01 0x00).
4653**
drh6446c4d2001-12-15 14:22:184654** No checking is done to make sure that page iTable really is the
4655** root page of a b-tree. If it is not, then the cursor acquired
4656** will not work correctly.
danielk197771d5d2c2008-09-29 11:49:474657**
drhf25a5072009-11-18 23:01:254658** It is assumed that the sqlite3BtreeCursorZero() has been called
4659** on pCur to initialize the memory space prior to invoking this routine.
drha059ad02001-04-17 20:09:114660*/
drhd677b3d2007-08-20 22:48:414661static int btreeCursor(
danielk1977cd3e8f72008-03-25 09:47:354662 Btree *p, /* The btree */
drhabc38152020-07-22 13:38:044663 Pgno iTable, /* Root page of table to open */
danielk1977cd3e8f72008-03-25 09:47:354664 int wrFlag, /* 1 to write. 0 read-only */
4665 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4666 BtCursor *pCur /* Space for new cursor */
drh3aac2dd2004-04-26 14:10:204667){
danielk19773e8add92009-07-04 17:16:004668 BtShared *pBt = p->pBt; /* Shared b-tree handle */
drh27fb7462015-06-30 02:47:364669 BtCursor *pX; /* Looping over other all cursors */
drhecdc7532001-09-23 02:35:534670
drh1fee73e2007-08-29 04:00:574671 assert( sqlite3BtreeHoldsMutex(p) );
larrybrbc917382023-06-07 08:40:314672 assert( wrFlag==0
4673 || wrFlag==BTREE_WRCSR
4674 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
danfd261ec2015-10-22 20:54:334675 );
danielk197796d48e92009-06-29 06:00:374676
larrybrbc917382023-06-07 08:40:314677 /* The following assert statements verify that if this is a sharable
4678 ** b-tree database, the connection is holding the required table locks,
4679 ** and that no other connection has any open cursor that conflicts with
drhac801802019-11-17 11:47:504680 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
4681 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
4682 || iTable<1 );
danielk197796d48e92009-06-29 06:00:374683 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
4684
danielk19773e8add92009-07-04 17:16:004685 /* Assert that the caller has opened the required transaction. */
4686 assert( p->inTrans>TRANS_NONE );
4687 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
4688 assert( pBt->pPage1 && pBt->pPage1->aData );
drh98ef0f62015-06-30 01:25:524689 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
danielk19773e8add92009-07-04 17:16:004690
drhdb561bc2019-10-25 14:46:054691 if( iTable<=1 ){
4692 if( iTable<1 ){
4693 return SQLITE_CORRUPT_BKPT;
4694 }else if( btreePagecount(pBt)==0 ){
4695 assert( wrFlag==0 );
4696 iTable = 0;
4697 }
danielk19773e8add92009-07-04 17:16:004698 }
danielk1977aef0bf62005-12-30 16:28:014699
danielk1977aef0bf62005-12-30 16:28:014700 /* Now that no other errors can occur, finish filling in the BtCursor
danielk19773e8add92009-07-04 17:16:004701 ** variables and link the cursor into the BtShared list. */
drhabc38152020-07-22 13:38:044702 pCur->pgnoRoot = iTable;
danielk1977172114a2009-07-07 15:47:124703 pCur->iPage = -1;
drh1e968a02008-03-25 00:22:214704 pCur->pKeyInfo = pKeyInfo;
danielk1977aef0bf62005-12-30 16:28:014705 pCur->pBtree = p;
drhd0679ed2007-08-28 22:24:344706 pCur->pBt = pBt;
drh2f0bc1d2021-12-03 13:42:414707 pCur->curFlags = 0;
drh27fb7462015-06-30 02:47:364708 /* If there are two or more cursors on the same btree, then all such
4709 ** cursors *must* have the BTCF_Multiple flag set. */
4710 for(pX=pBt->pCursor; pX; pX=pX->pNext){
drhabc38152020-07-22 13:38:044711 if( pX->pgnoRoot==iTable ){
drh27fb7462015-06-30 02:47:364712 pX->curFlags |= BTCF_Multiple;
drh2f0bc1d2021-12-03 13:42:414713 pCur->curFlags = BTCF_Multiple;
drh27fb7462015-06-30 02:47:364714 }
drha059ad02001-04-17 20:09:114715 }
drh2f0bc1d2021-12-03 13:42:414716 pCur->eState = CURSOR_INVALID;
drh27fb7462015-06-30 02:47:364717 pCur->pNext = pBt->pCursor;
drha059ad02001-04-17 20:09:114718 pBt->pCursor = pCur;
drh2f0bc1d2021-12-03 13:42:414719 if( wrFlag ){
4720 pCur->curFlags |= BTCF_WriteFlag;
4721 pCur->curPagerFlags = 0;
4722 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
4723 }else{
4724 pCur->curPagerFlags = PAGER_GET_READONLY;
4725 }
danielk1977aef0bf62005-12-30 16:28:014726 return SQLITE_OK;
drha059ad02001-04-17 20:09:114727}
drhdb561bc2019-10-25 14:46:054728static int btreeCursorWithLock(
4729 Btree *p, /* The btree */
drhabc38152020-07-22 13:38:044730 Pgno iTable, /* Root page of table to open */
drhdb561bc2019-10-25 14:46:054731 int wrFlag, /* 1 to write. 0 read-only */
4732 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4733 BtCursor *pCur /* Space for new cursor */
4734){
4735 int rc;
4736 sqlite3BtreeEnter(p);
4737 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4738 sqlite3BtreeLeave(p);
4739 return rc;
4740}
drhd677b3d2007-08-20 22:48:414741int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:354742 Btree *p, /* The btree */
drhabc38152020-07-22 13:38:044743 Pgno iTable, /* Root page of table to open */
danielk1977cd3e8f72008-03-25 09:47:354744 int wrFlag, /* 1 to write. 0 read-only */
4745 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
4746 BtCursor *pCur /* Write new cursor here */
drhd677b3d2007-08-20 22:48:414747){
drhdb561bc2019-10-25 14:46:054748 if( p->sharable ){
4749 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
dan08f901b2015-05-25 19:24:364750 }else{
drhdb561bc2019-10-25 14:46:054751 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
dan08f901b2015-05-25 19:24:364752 }
drhd677b3d2007-08-20 22:48:414753}
drh7f751222009-03-17 22:33:004754
4755/*
4756** Return the size of a BtCursor object in bytes.
4757**
4758** This interfaces is needed so that users of cursors can preallocate
4759** sufficient storage to hold a cursor. The BtCursor object is opaque
4760** to users so they cannot do the sizeof() themselves - they must call
4761** this routine.
4762*/
4763int sqlite3BtreeCursorSize(void){
drhc54055b2009-11-13 17:05:534764 return ROUND8(sizeof(BtCursor));
danielk1977cd3e8f72008-03-25 09:47:354765}
4766
drhc2e400a2024-10-18 01:14:144767#ifdef SQLITE_DEBUG
4768/*
4769** Return true if and only if the Btree object will be automatically
4770** closed with the BtCursor closes. This is used within assert() statements
4771** only.
4772*/
4773int sqlite3BtreeClosesWithCursor(
4774 Btree *pBtree, /* the btree object */
4775 BtCursor *pCur /* Corresponding cursor */
4776){
4777 BtShared *pBt = pBtree->pBt;
4778 if( (pBt->openFlags & BTREE_SINGLE)==0 ) return 0;
4779 if( pBt->pCursor!=pCur ) return 0;
4780 if( pCur->pNext!=0 ) return 0;
4781 if( pCur->pBtree!=pBtree ) return 0;
4782 return 1;
4783}
4784#endif
4785
drh7f751222009-03-17 22:33:004786/*
drhf25a5072009-11-18 23:01:254787** Initialize memory that will be converted into a BtCursor object.
4788**
4789** The simple approach here would be to memset() the entire object
4790** to zero. But it turns out that the apPage[] and aiIdx[] arrays
4791** do not need to be zeroed and they are large, so we can save a lot
4792** of run-time by skipping the initialization of those elements.
4793*/
4794void sqlite3BtreeCursorZero(BtCursor *p){
drhda6bc672018-01-24 16:04:214795 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
drhf25a5072009-11-18 23:01:254796}
4797
4798/*
drh5e00f6c2001-09-13 13:46:564799** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:574800** when the last cursor is closed.
drha059ad02001-04-17 20:09:114801*/
drh3aac2dd2004-04-26 14:10:204802int sqlite3BtreeCloseCursor(BtCursor *pCur){
drhff0587c2007-08-29 17:43:194803 Btree *pBtree = pCur->pBtree;
danielk1977cd3e8f72008-03-25 09:47:354804 if( pBtree ){
4805 BtShared *pBt = pCur->pBt;
4806 sqlite3BtreeEnter(pBtree);
drh27fb7462015-06-30 02:47:364807 assert( pBt->pCursor!=0 );
4808 if( pBt->pCursor==pCur ){
danielk1977cd3e8f72008-03-25 09:47:354809 pBt->pCursor = pCur->pNext;
drh27fb7462015-06-30 02:47:364810 }else{
4811 BtCursor *pPrev = pBt->pCursor;
4812 do{
4813 if( pPrev->pNext==pCur ){
4814 pPrev->pNext = pCur->pNext;
4815 break;
4816 }
4817 pPrev = pPrev->pNext;
4818 }while( ALWAYS(pPrev) );
danielk1977cd3e8f72008-03-25 09:47:354819 }
drh352a35a2017-08-15 03:46:474820 btreeReleaseAllCursorPages(pCur);
danielk1977cd3e8f72008-03-25 09:47:354821 unlockBtreeIfUnused(pBt);
dan85753662014-12-11 16:38:184822 sqlite3_free(pCur->aOverflow);
drhf38dd3b2017-08-14 23:53:024823 sqlite3_free(pCur->pKey);
daneeee8a52021-03-18 14:31:374824 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
4825 /* Since the BtShared is not sharable, there is no need to
4826 ** worry about the missing sqlite3BtreeLeave() call here. */
4827 assert( pBtree->sharable==0 );
4828 sqlite3BtreeClose(pBtree);
4829 }else{
4830 sqlite3BtreeLeave(pBtree);
4831 }
dan97c8cb32019-01-01 18:00:174832 pCur->pBtree = 0;
drha059ad02001-04-17 20:09:114833 }
drh8c42ca92001-06-22 19:15:004834 return SQLITE_OK;
drha059ad02001-04-17 20:09:114835}
4836
drh5e2f8b92001-05-28 00:41:154837/*
drh86057612007-06-26 01:04:484838** Make sure the BtCursor* given in the argument has a valid
4839** BtCursor.info structure. If it is not already valid, call
danielk197730548662009-07-09 05:07:374840** btreeParseCell() to fill it in.
drhab01f612004-05-22 02:55:234841**
4842** BtCursor.info is a cache of the information in the current cell.
danielk197730548662009-07-09 05:07:374843** Using this cache reduces the number of calls to btreeParseCell().
drh9188b382004-05-14 21:12:224844*/
drh9188b382004-05-14 21:12:224845#ifndef NDEBUG
drha224ee22018-02-19 13:53:564846 static int cellInfoEqual(CellInfo *a, CellInfo *b){
4847 if( a->nKey!=b->nKey ) return 0;
4848 if( a->pPayload!=b->pPayload ) return 0;
4849 if( a->nPayload!=b->nPayload ) return 0;
4850 if( a->nLocal!=b->nLocal ) return 0;
4851 if( a->nSize!=b->nSize ) return 0;
4852 return 1;
4853 }
danielk19771cc5ed82007-05-16 17:28:434854 static void assertCellInfo(BtCursor *pCur){
drh9188b382004-05-14 21:12:224855 CellInfo info;
drh51c6d962004-06-06 00:42:254856 memset(&info, 0, sizeof(info));
drh352a35a2017-08-15 03:46:474857 btreeParseCell(pCur->pPage, pCur->ix, &info);
drha224ee22018-02-19 13:53:564858 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
drh9188b382004-05-14 21:12:224859 }
danielk19771cc5ed82007-05-16 17:28:434860#else
4861 #define assertCellInfo(x)
4862#endif
drhc5b41ac2015-06-17 02:11:464863static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
4864 if( pCur->info.nSize==0 ){
drhc5b41ac2015-06-17 02:11:464865 pCur->curFlags |= BTCF_ValidNKey;
drh352a35a2017-08-15 03:46:474866 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
drhc5b41ac2015-06-17 02:11:464867 }else{
4868 assertCellInfo(pCur);
drh86057612007-06-26 01:04:484869 }
drhc5b41ac2015-06-17 02:11:464870}
drh9188b382004-05-14 21:12:224871
drhea8ffdf2009-07-22 00:35:234872#ifndef NDEBUG /* The next routine used only within assert() statements */
4873/*
4874** Return true if the given BtCursor is valid. A valid cursor is one
4875** that is currently pointing to a row in a (non-empty) table.
4876** This is a verification routine is used only within assert() statements.
4877*/
4878int sqlite3BtreeCursorIsValid(BtCursor *pCur){
4879 return pCur && pCur->eState==CURSOR_VALID;
4880}
4881#endif /* NDEBUG */
drhd6ef5af2016-11-15 04:00:244882int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
4883 assert( pCur!=0 );
4884 return pCur->eState==CURSOR_VALID;
4885}
drhea8ffdf2009-07-22 00:35:234886
drh9188b382004-05-14 21:12:224887/*
drha7c90c42016-06-04 20:37:104888** Return the value of the integer key or "rowid" for a table btree.
4889** This routine is only valid for a cursor that is pointing into a
4890** ordinary table btree. If the cursor points to an index btree or
4891** is invalid, the result of this routine is undefined.
drh7e3b0a02001-04-28 16:52:404892*/
drha7c90c42016-06-04 20:37:104893i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:574894 assert( cursorHoldsMutex(pCur) );
drhc5352b92014-11-17 20:33:074895 assert( pCur->eState==CURSOR_VALID );
drha7c90c42016-06-04 20:37:104896 assert( pCur->curIntKey );
drhc5352b92014-11-17 20:33:074897 getCellInfo(pCur);
drha7c90c42016-06-04 20:37:104898 return pCur->info.nKey;
drha059ad02001-04-17 20:09:114899}
drh2af926b2001-05-15 00:39:254900
drh7b14b652019-12-29 22:08:204901/*
4902** Pin or unpin a cursor.
4903*/
4904void sqlite3BtreeCursorPin(BtCursor *pCur){
4905 assert( (pCur->curFlags & BTCF_Pinned)==0 );
4906 pCur->curFlags |= BTCF_Pinned;
4907}
4908void sqlite3BtreeCursorUnpin(BtCursor *pCur){
4909 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
4910 pCur->curFlags &= ~BTCF_Pinned;
4911}
4912
drh72f82862001-05-24 21:06:344913/*
drh2fc865c2017-12-16 20:20:374914** Return the offset into the database file for the start of the
4915** payload to which the cursor is pointing.
4916*/
drh092457b2017-12-29 15:04:494917i64 sqlite3BtreeOffset(BtCursor *pCur){
drh2fc865c2017-12-16 20:20:374918 assert( cursorHoldsMutex(pCur) );
4919 assert( pCur->eState==CURSOR_VALID );
drh2fc865c2017-12-16 20:20:374920 getCellInfo(pCur);
drhfe6d20e2017-12-29 14:33:544921 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
drh2fc865c2017-12-16 20:20:374922 (i64)(pCur->info.pPayload - pCur->pPage->aData);
4923}
4924
4925/*
drha7c90c42016-06-04 20:37:104926** Return the number of bytes of payload for the entry that pCur is
4927** currently pointing to. For table btrees, this will be the amount
4928** of data. For index btrees, this will be the size of the key.
drhea8ffdf2009-07-22 00:35:234929**
4930** The caller must guarantee that the cursor is pointing to a non-NULL
4931** valid entry. In other words, the calling procedure must guarantee
4932** that the cursor has Cursor.eState==CURSOR_VALID.
drh0e1c19e2004-05-11 00:58:564933*/
drha7c90c42016-06-04 20:37:104934u32 sqlite3BtreePayloadSize(BtCursor *pCur){
4935 assert( cursorHoldsMutex(pCur) );
drhea8ffdf2009-07-22 00:35:234936 assert( pCur->eState==CURSOR_VALID );
4937 getCellInfo(pCur);
drha7c90c42016-06-04 20:37:104938 return pCur->info.nPayload;
drh0e1c19e2004-05-11 00:58:564939}
4940
4941/*
drh53d30dd2019-02-04 21:10:244942** Return an upper bound on the size of any record for the table
4943** that the cursor is pointing into.
4944**
4945** This is an optimization. Everything will still work if this
4946** routine always returns 2147483647 (which is the largest record
4947** that SQLite can handle) or more. But returning a smaller value might
4948** prevent large memory allocations when trying to interpret a
larrybrbc917382023-06-07 08:40:314949** corrupt database.
drh53d30dd2019-02-04 21:10:244950**
4951** The current implementation merely returns the size of the underlying
4952** database file.
4953*/
4954sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
4955 assert( cursorHoldsMutex(pCur) );
4956 assert( pCur->eState==CURSOR_VALID );
4957 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
4958}
4959
4960/*
danielk1977d04417962007-05-02 13:16:304961** Given the page number of an overflow page in the database (parameter
larrybrbc917382023-06-07 08:40:314962** ovfl), this function finds the page number of the next page in the
danielk1977d04417962007-05-02 13:16:304963** linked list of overflow pages. If possible, it uses the auto-vacuum
larrybrbc917382023-06-07 08:40:314964** pointer-map data instead of reading the content of page ovfl to do so.
danielk1977d04417962007-05-02 13:16:304965**
4966** If an error occurs an SQLite error code is returned. Otherwise:
4967**
larrybrbc917382023-06-07 08:40:314968** The page number of the next overflow page in the linked list is
4969** written to *pPgnoNext. If page ovfl is the last page in its linked
4970** list, *pPgnoNext is set to zero.
danielk1977d04417962007-05-02 13:16:304971**
danielk1977bea2a942009-01-20 17:06:274972** If ppPage is not NULL, and a reference to the MemPage object corresponding
4973** to page number pOvfl was obtained, then *ppPage is set to point to that
4974** reference. It is the responsibility of the caller to call releasePage()
4975** on *ppPage to free the reference. In no reference was obtained (because
4976** the pointer-map was used to obtain the value for *pPgnoNext), then
4977** *ppPage is set to zero.
danielk1977d04417962007-05-02 13:16:304978*/
4979static int getOverflowPage(
drhfa3be902009-07-07 02:44:074980 BtShared *pBt, /* The database file */
4981 Pgno ovfl, /* Current overflow page number */
danielk1977bea2a942009-01-20 17:06:274982 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
danielk1977d04417962007-05-02 13:16:304983 Pgno *pPgnoNext /* OUT: Next overflow page number */
4984){
4985 Pgno next = 0;
danielk1977bea2a942009-01-20 17:06:274986 MemPage *pPage = 0;
drh1bd10f82008-12-10 21:19:564987 int rc = SQLITE_OK;
danielk1977d04417962007-05-02 13:16:304988
drh1fee73e2007-08-29 04:00:574989 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977bea2a942009-01-20 17:06:274990 assert(pPgnoNext);
danielk1977d04417962007-05-02 13:16:304991
4992#ifndef SQLITE_OMIT_AUTOVACUUM
4993 /* Try to find the next page in the overflow list using the
larrybrbc917382023-06-07 08:40:314994 ** autovacuum pointer-map pages. Guess that the next page in
4995 ** the overflow list is page number (ovfl+1). If that guess turns
4996 ** out to be wrong, fall back to loading the data of page
danielk1977d04417962007-05-02 13:16:304997 ** number ovfl to determine the next page number.
4998 */
4999 if( pBt->autoVacuum ){
5000 Pgno pgno;
5001 Pgno iGuess = ovfl+1;
5002 u8 eType;
5003
5004 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
5005 iGuess++;
5006 }
5007
drhb1299152010-03-30 22:58:335008 if( iGuess<=btreePagecount(pBt) ){
danielk1977d04417962007-05-02 13:16:305009 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
danielk1977bea2a942009-01-20 17:06:275010 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
danielk1977d04417962007-05-02 13:16:305011 next = iGuess;
danielk1977bea2a942009-01-20 17:06:275012 rc = SQLITE_DONE;
danielk1977d04417962007-05-02 13:16:305013 }
5014 }
5015 }
5016#endif
5017
danielk1977d8a3f3d2009-07-11 11:45:235018 assert( next==0 || rc==SQLITE_DONE );
danielk1977bea2a942009-01-20 17:06:275019 if( rc==SQLITE_OK ){
drhb00fc3b2013-08-21 23:42:325020 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
danielk1977d8a3f3d2009-07-11 11:45:235021 assert( rc==SQLITE_OK || pPage==0 );
5022 if( rc==SQLITE_OK ){
danielk1977d04417962007-05-02 13:16:305023 next = get4byte(pPage->aData);
5024 }
danielk1977443c0592009-01-16 15:21:055025 }
danielk197745d68822009-01-16 16:23:385026
danielk1977bea2a942009-01-20 17:06:275027 *pPgnoNext = next;
5028 if( ppPage ){
5029 *ppPage = pPage;
5030 }else{
5031 releasePage(pPage);
5032 }
5033 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
danielk1977d04417962007-05-02 13:16:305034}
5035
danielk1977da107192007-05-04 08:32:135036/*
5037** Copy data from a buffer to a page, or from a page to a buffer.
5038**
5039** pPayload is a pointer to data stored on database page pDbPage.
5040** If argument eOp is false, then nByte bytes of data are copied
5041** from pPayload to the buffer pointed at by pBuf. If eOp is true,
5042** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
5043** of data are copied from the buffer pBuf to pPayload.
5044**
5045** SQLITE_OK is returned on success, otherwise an error code.
5046*/
5047static int copyPayload(
5048 void *pPayload, /* Pointer to page data */
5049 void *pBuf, /* Pointer to buffer */
5050 int nByte, /* Number of bytes to copy */
5051 int eOp, /* 0 -> copy from page, 1 -> copy to page */
5052 DbPage *pDbPage /* Page containing pPayload */
5053){
5054 if( eOp ){
5055 /* Copy data from buffer to page (a write operation) */
5056 int rc = sqlite3PagerWrite(pDbPage);
5057 if( rc!=SQLITE_OK ){
5058 return rc;
5059 }
5060 memcpy(pPayload, pBuf, nByte);
5061 }else{
5062 /* Copy data from page to buffer (a read operation) */
5063 memcpy(pBuf, pPayload, nByte);
5064 }
5065 return SQLITE_OK;
5066}
danielk1977d04417962007-05-02 13:16:305067
5068/*
danielk19779f8d6402007-05-02 17:48:455069** This function is used to read or overwrite payload information
dan5a500af2014-03-11 20:33:045070** for the entry that the pCur cursor is pointing to. The eOp
5071** argument is interpreted as follows:
5072**
5073** 0: The operation is a read. Populate the overflow cache.
5074** 1: The operation is a write. Populate the overflow cache.
danielk19779f8d6402007-05-02 17:48:455075**
5076** A total of "amt" bytes are read or written beginning at "offset".
5077** Data is read to or from the buffer pBuf.
drh72f82862001-05-24 21:06:345078**
drh3bcdfd22009-07-12 02:32:215079** The content being read or written might appear on the main page
5080** or be scattered out on multiple overflow pages.
danielk1977da107192007-05-04 08:32:135081**
drh42e28f12017-01-27 00:31:595082** If the current cursor entry uses one or more overflow pages
5083** this function may allocate space for and lazily populate
larrybrbc917382023-06-07 08:40:315084** the overflow page-list cache array (BtCursor.aOverflow).
5085** Subsequent calls use this cache to make seeking to the supplied offset
dan5a500af2014-03-11 20:33:045086** more efficient.
danielk1977da107192007-05-04 08:32:135087**
drh42e28f12017-01-27 00:31:595088** Once an overflow page-list cache has been allocated, it must be
danielk1977da107192007-05-04 08:32:135089** invalidated if some other cursor writes to the same table, or if
5090** the cursor is moved to a different row. Additionally, in auto-vacuum
5091** mode, the following events may invalidate an overflow page-list cache.
5092**
5093** * An incremental vacuum,
5094** * A commit in auto_vacuum="full" mode,
5095** * Creating a table (may require moving an overflow page).
drh72f82862001-05-24 21:06:345096*/
danielk19779f8d6402007-05-02 17:48:455097static int accessPayload(
drh3aac2dd2004-04-26 14:10:205098 BtCursor *pCur, /* Cursor pointing to entry to read from */
danielk197789d40042008-11-17 14:20:565099 u32 offset, /* Begin reading this far into payload */
5100 u32 amt, /* Read this many bytes */
larrybrbc917382023-06-07 08:40:315101 unsigned char *pBuf, /* Write the bytes into this buffer */
danielk19779f8d6402007-05-02 17:48:455102 int eOp /* zero to read. non-zero to write. */
drh3aac2dd2004-04-26 14:10:205103){
5104 unsigned char *aPayload;
danielk1977da107192007-05-04 08:32:135105 int rc = SQLITE_OK;
danielk19772dec9702007-05-02 16:48:375106 int iIdx = 0;
drh352a35a2017-08-15 03:46:475107 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
danielk19770d065412008-11-12 18:21:365108 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
drh4c417182014-03-31 23:57:415109#ifdef SQLITE_DIRECT_OVERFLOW_READ
drh8bb9fd32017-01-26 16:27:325110 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
drh4c417182014-03-31 23:57:415111#endif
drh3aac2dd2004-04-26 14:10:205112
danielk1977da107192007-05-04 08:32:135113 assert( pPage );
drh42e28f12017-01-27 00:31:595114 assert( eOp==0 || eOp==1 );
danielk1977da184232006-01-05 11:34:325115 assert( pCur->eState==CURSOR_VALID );
drh7bc6a812022-09-30 22:40:575116 if( pCur->ix>=pPage->nCell ){
drha7149082021-10-13 20:11:305117 return SQLITE_CORRUPT_PAGE(pPage);
5118 }
drh1fee73e2007-08-29 04:00:575119 assert( cursorHoldsMutex(pCur) );
danielk1977da107192007-05-04 08:32:135120
drh86057612007-06-26 01:04:485121 getCellInfo(pCur);
drhab1cc582014-09-23 21:25:195122 aPayload = pCur->info.pPayload;
drhab1cc582014-09-23 21:25:195123 assert( offset+amt <= pCur->info.nPayload );
danielk1977da107192007-05-04 08:32:135124
drh0b982072016-03-22 14:10:455125 assert( aPayload > pPage->aData );
drhc5e7f942016-03-22 15:25:165126 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
drh0b982072016-03-22 14:10:455127 /* Trying to read or write past the end of the data is an error. The
5128 ** conditional above is really:
5129 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
5130 ** but is recast into its current form to avoid integer overflow problems
5131 */
daneebf2f52017-11-18 17:30:085132 return SQLITE_CORRUPT_PAGE(pPage);
drh3aac2dd2004-04-26 14:10:205133 }
danielk1977da107192007-05-04 08:32:135134
5135 /* Check if data must be read/written to/from the btree page itself. */
drhfa1a98a2004-05-14 19:08:175136 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:255137 int a = amt;
drhfa1a98a2004-05-14 19:08:175138 if( a+offset>pCur->info.nLocal ){
5139 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:255140 }
drh42e28f12017-01-27 00:31:595141 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
drh2aa679f2001-06-25 02:11:075142 offset = 0;
drha34b6762004-05-07 13:30:425143 pBuf += a;
drh2af926b2001-05-15 00:39:255144 amt -= a;
drhdd793422001-06-28 01:54:485145 }else{
drhfa1a98a2004-05-14 19:08:175146 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:575147 }
danielk1977da107192007-05-04 08:32:135148
dan85753662014-12-11 16:38:185149
danielk1977da107192007-05-04 08:32:135150 if( rc==SQLITE_OK && amt>0 ){
danielk197789d40042008-11-17 14:20:565151 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
danielk1977da107192007-05-04 08:32:135152 Pgno nextPage;
5153
drhfa1a98a2004-05-14 19:08:175154 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
larrybrbc917382023-06-07 08:40:315155
drha38c9512014-04-01 01:24:345156 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
drha38c9512014-04-01 01:24:345157 **
5158 ** The aOverflow[] array is sized at one entry for each overflow page
5159 ** in the overflow chain. The page number of the first overflow page is
5160 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
5161 ** means "not yet known" (the cache is lazily populated).
danielk1977da107192007-05-04 08:32:135162 */
drh42e28f12017-01-27 00:31:595163 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
danielk19772dec9702007-05-02 16:48:375164 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
drhda6bc672018-01-24 16:04:215165 if( pCur->aOverflow==0
mistachkin97f90592018-02-04 01:30:545166 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
drhda6bc672018-01-24 16:04:215167 ){
drh84dea822024-02-12 13:51:085168 Pgno *aNew;
5169 if( sqlite3FaultSim(413) ){
5170 aNew = 0;
5171 }else{
5172 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
5173 }
dan5a500af2014-03-11 20:33:045174 if( aNew==0 ){
drhcd645532017-01-20 20:43:145175 return SQLITE_NOMEM_BKPT;
dan5a500af2014-03-11 20:33:045176 }else{
dan5a500af2014-03-11 20:33:045177 pCur->aOverflow = aNew;
5178 }
5179 }
drhcd645532017-01-20 20:43:145180 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
5181 pCur->curFlags |= BTCF_ValidOvfl;
drhcdf360a2017-01-27 01:13:495182 }else{
drh1d402372024-04-12 13:05:365183 /* Sanity check the validity of the overflow page cache */
drh06915d02024-04-15 20:43:215184 assert( pCur->aOverflow[0]==nextPage
5185 || pCur->aOverflow[0]==0
5186 || CORRUPT_DB );
drh1d402372024-04-12 13:05:365187 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 );
5188
drhcdf360a2017-01-27 01:13:495189 /* If the overflow page-list cache has been allocated and the
5190 ** entry for the first required overflow page is valid, skip
5191 ** directly to it.
5192 */
5193 if( pCur->aOverflow[offset/ovflSize] ){
5194 iIdx = (offset/ovflSize);
5195 nextPage = pCur->aOverflow[iIdx];
5196 offset = (offset%ovflSize);
5197 }
danielk19772dec9702007-05-02 16:48:375198 }
danielk1977da107192007-05-04 08:32:135199
drhcd645532017-01-20 20:43:145200 assert( rc==SQLITE_OK && amt>0 );
5201 while( nextPage ){
danielk1977da107192007-05-04 08:32:135202 /* If required, populate the overflow page-list cache. */
drh584e8b72020-07-22 17:12:595203 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
drh42e28f12017-01-27 00:31:595204 assert( pCur->aOverflow[iIdx]==0
5205 || pCur->aOverflow[iIdx]==nextPage
5206 || CORRUPT_DB );
5207 pCur->aOverflow[iIdx] = nextPage;
danielk1977da107192007-05-04 08:32:135208
danielk1977d04417962007-05-02 13:16:305209 if( offset>=ovflSize ){
5210 /* The only reason to read this page is to obtain the page
danielk1977da107192007-05-04 08:32:135211 ** number for the next page in the overflow chain. The page
drhfd131da2007-08-07 17:13:035212 ** data is not required. So first try to lookup the overflow
5213 ** page-list cache, if any, then fall back to the getOverflowPage()
danielk1977da107192007-05-04 08:32:135214 ** function.
danielk1977d04417962007-05-02 13:16:305215 */
drha38c9512014-04-01 01:24:345216 assert( pCur->curFlags & BTCF_ValidOvfl );
dan85753662014-12-11 16:38:185217 assert( pCur->pBtree->db==pBt->db );
drha38c9512014-04-01 01:24:345218 if( pCur->aOverflow[iIdx+1] ){
danielk1977da107192007-05-04 08:32:135219 nextPage = pCur->aOverflow[iIdx+1];
drha38c9512014-04-01 01:24:345220 }else{
danielk1977da107192007-05-04 08:32:135221 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
drha38c9512014-04-01 01:24:345222 }
danielk1977da107192007-05-04 08:32:135223 offset -= ovflSize;
danielk1977d04417962007-05-02 13:16:305224 }else{
danielk19779f8d6402007-05-02 17:48:455225 /* Need to read this page properly. It contains some of the
5226 ** range of data that is being read (eOp==0) or written (eOp!=0).
danielk1977d04417962007-05-02 13:16:305227 */
danielk1977cfe9a692004-06-16 12:00:295228 int a = amt;
danf4ba1092011-10-08 14:57:075229 if( a + offset > ovflSize ){
5230 a = ovflSize - offset;
danielk19779f8d6402007-05-02 17:48:455231 }
danf4ba1092011-10-08 14:57:075232
5233#ifdef SQLITE_DIRECT_OVERFLOW_READ
5234 /* If all the following are true:
5235 **
larrybrbc917382023-06-07 08:40:315236 ** 1) this is a read operation, and
danf4ba1092011-10-08 14:57:075237 ** 2) data is required from the start of this overflow page, and
dan09236752018-11-22 19:10:145238 ** 3) there are no dirty pages in the page-cache
drh8bb9fd32017-01-26 16:27:325239 ** 4) the database is file-backed, and
drhd930b5c2017-01-26 02:26:025240 ** 5) the page is not in the WAL file
larrybrbc917382023-06-07 08:40:315241 ** 6) at least 4 bytes have already been read into the output buffer
danf4ba1092011-10-08 14:57:075242 **
5243 ** then data can be read directly from the database file into the
5244 ** output buffer, bypassing the page-cache altogether. This speeds
5245 ** up loading large records that span many overflow pages.
5246 */
drh42e28f12017-01-27 00:31:595247 if( eOp==0 /* (1) */
danf4ba1092011-10-08 14:57:075248 && offset==0 /* (2) */
dan09236752018-11-22 19:10:145249 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
drh8bb9fd32017-01-26 16:27:325250 && &pBuf[-4]>=pBufStart /* (6) */
danf4ba1092011-10-08 14:57:075251 ){
dan09236752018-11-22 19:10:145252 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
danf4ba1092011-10-08 14:57:075253 u8 aSave[4];
5254 u8 *aWrite = &pBuf[-4];
drh8bb9fd32017-01-26 16:27:325255 assert( aWrite>=pBufStart ); /* due to (6) */
danf4ba1092011-10-08 14:57:075256 memcpy(aSave, aWrite, 4);
dan27d47fb2011-12-21 17:00:165257 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
danf4ba1092011-10-08 14:57:075258 nextPage = get4byte(aWrite);
5259 memcpy(aWrite, aSave, 4);
5260 }else
5261#endif
5262
5263 {
5264 DbPage *pDbPage;
drh9584f582015-11-04 20:22:375265 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
drh42e28f12017-01-27 00:31:595266 (eOp==0 ? PAGER_GET_READONLY : 0)
dan11dcd112013-03-15 18:29:185267 );
danf4ba1092011-10-08 14:57:075268 if( rc==SQLITE_OK ){
5269 aPayload = sqlite3PagerGetData(pDbPage);
5270 nextPage = get4byte(aPayload);
drh42e28f12017-01-27 00:31:595271 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
danf4ba1092011-10-08 14:57:075272 sqlite3PagerUnref(pDbPage);
5273 offset = 0;
5274 }
5275 }
5276 amt -= a;
drh6ee610b2017-01-27 01:25:005277 if( amt==0 ) return rc;
danf4ba1092011-10-08 14:57:075278 pBuf += a;
danielk1977cfe9a692004-06-16 12:00:295279 }
drhcd645532017-01-20 20:43:145280 if( rc ) break;
5281 iIdx++;
drh2af926b2001-05-15 00:39:255282 }
drh2af926b2001-05-15 00:39:255283 }
danielk1977cfe9a692004-06-16 12:00:295284
danielk1977da107192007-05-04 08:32:135285 if( rc==SQLITE_OK && amt>0 ){
drhcc97ca42017-06-07 22:32:595286 /* Overflow chain ends prematurely */
daneebf2f52017-11-18 17:30:085287 return SQLITE_CORRUPT_PAGE(pPage);
drha7fcb052001-12-14 15:09:555288 }
danielk1977da107192007-05-04 08:32:135289 return rc;
drh2af926b2001-05-15 00:39:255290}
5291
drh72f82862001-05-24 21:06:345292/*
drhcb3cabd2016-11-25 19:18:285293** Read part of the payload for the row at which that cursor pCur is currently
5294** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:205295** begins at "offset".
drh8c1238a2003-01-02 14:43:555296**
drhcb3cabd2016-11-25 19:18:285297** pCur can be pointing to either a table or an index b-tree.
5298** If pointing to a table btree, then the content section is read. If
5299** pCur is pointing to an index b-tree then the key section is read.
5300**
5301** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
5302** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
5303** cursor might be invalid or might need to be restored before being read.
drh5d1a8722009-07-22 18:07:405304**
drh3aac2dd2004-04-26 14:10:205305** Return SQLITE_OK on success or an error code if anything goes
5306** wrong. An error is returned if "offset+amt" is larger than
5307** the available payload.
drh72f82862001-05-24 21:06:345308*/
drhcb3cabd2016-11-25 19:18:285309int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drh1fee73e2007-08-29 04:00:575310 assert( cursorHoldsMutex(pCur) );
drh5d1a8722009-07-22 18:07:405311 assert( pCur->eState==CURSOR_VALID );
drh352a35a2017-08-15 03:46:475312 assert( pCur->iPage>=0 && pCur->pPage );
drh5d1a8722009-07-22 18:07:405313 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
drh3aac2dd2004-04-26 14:10:205314}
drh83ec2762017-01-26 16:54:475315
5316/*
5317** This variant of sqlite3BtreePayload() works even if the cursor has not
5318** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
5319** interface.
5320*/
danielk19773588ceb2008-06-10 17:30:265321#ifndef SQLITE_OMIT_INCRBLOB
drh83ec2762017-01-26 16:54:475322static SQLITE_NOINLINE int accessPayloadChecked(
5323 BtCursor *pCur,
5324 u32 offset,
5325 u32 amt,
5326 void *pBuf
5327){
drhcb3cabd2016-11-25 19:18:285328 int rc;
danielk19773588ceb2008-06-10 17:30:265329 if ( pCur->eState==CURSOR_INVALID ){
5330 return SQLITE_ABORT;
5331 }
dan7a2347e2016-01-07 16:43:545332 assert( cursorOwnsBtShared(pCur) );
drh945b0942017-01-26 21:30:005333 rc = btreeRestoreCursorPosition(pCur);
drh83ec2762017-01-26 16:54:475334 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
5335}
5336int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
5337 if( pCur->eState==CURSOR_VALID ){
5338 assert( cursorOwnsBtShared(pCur) );
5339 return accessPayload(pCur, offset, amt, pBuf, 0);
5340 }else{
5341 return accessPayloadChecked(pCur, offset, amt, pBuf);
danielk1977da184232006-01-05 11:34:325342 }
drh2af926b2001-05-15 00:39:255343}
drhcb3cabd2016-11-25 19:18:285344#endif /* SQLITE_OMIT_INCRBLOB */
drh2af926b2001-05-15 00:39:255345
drh72f82862001-05-24 21:06:345346/*
larrybrbc917382023-06-07 08:40:315347** Return a pointer to payload information from the entry that the
drh0e1c19e2004-05-11 00:58:565348** pCur cursor is pointing to. The pointer is to the beginning of
drh2a8d2262013-12-09 20:43:225349** the key if index btrees (pPage->intKey==0) and is the data for
5350** table btrees (pPage->intKey==1). The number of bytes of available
5351** key/data is written into *pAmt. If *pAmt==0, then the value
5352** returned will not be a valid pointer.
drh0e1c19e2004-05-11 00:58:565353**
5354** This routine is an optimization. It is common for the entire key
5355** and data to fit on the local page and for there to be no overflow
5356** pages. When that is so, this routine can be used to access the
5357** key and data without making a copy. If the key and/or data spills
drh7f751222009-03-17 22:33:005358** onto overflow pages, then accessPayload() must be used to reassemble
drh0e1c19e2004-05-11 00:58:565359** the key/data and copy it into a preallocated buffer.
5360**
5361** The pointer returned by this routine looks directly into the cached
5362** page of the database. The data might change or move the next time
5363** any btree routine is called.
5364*/
drh2a8d2262013-12-09 20:43:225365static const void *fetchPayload(
drh0e1c19e2004-05-11 00:58:565366 BtCursor *pCur, /* Cursor pointing to entry to read from */
drh2a8d2262013-12-09 20:43:225367 u32 *pAmt /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:565368){
danf2f72a02017-10-19 15:17:385369 int amt;
drh352a35a2017-08-15 03:46:475370 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
danielk1977da184232006-01-05 11:34:325371 assert( pCur->eState==CURSOR_VALID );
drh2a8d2262013-12-09 20:43:225372 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
dan7a2347e2016-01-07 16:43:545373 assert( cursorOwnsBtShared(pCur) );
drhcd789f92021-10-11 09:39:425374 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
drh86dd3712014-03-25 11:00:215375 assert( pCur->info.nSize>0 );
drh352a35a2017-08-15 03:46:475376 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
5377 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
danf2f72a02017-10-19 15:17:385378 amt = pCur->info.nLocal;
5379 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
5380 /* There is too little space on the page for the expected amount
5381 ** of local content. Database must be corrupt. */
5382 assert( CORRUPT_DB );
5383 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
5384 }
5385 *pAmt = (u32)amt;
drhab1cc582014-09-23 21:25:195386 return (void*)pCur->info.pPayload;
drh0e1c19e2004-05-11 00:58:565387}
5388
5389
5390/*
drhe51c44f2004-05-30 20:46:095391** For the entry that cursor pCur is point to, return as
5392** many bytes of the key or data as are available on the local
5393** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:565394**
5395** The pointer returned is ephemeral. The key/data may move
drhd677b3d2007-08-20 22:48:415396** or be destroyed on the next call to any Btree routine,
5397** including calls from other threads against the same cache.
5398** Hence, a mutex on the BtShared should be held prior to calling
5399** this routine.
drh0e1c19e2004-05-11 00:58:565400**
5401** These routines is used to get quick access to key and data
5402** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:565403*/
drha7c90c42016-06-04 20:37:105404const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
drh2a8d2262013-12-09 20:43:225405 return fetchPayload(pCur, pAmt);
drh0e1c19e2004-05-11 00:58:565406}
5407
5408
5409/*
drh8178a752003-01-05 21:41:405410** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:235411** page number of the child page to move to.
danielk1977a299d612009-07-13 11:22:105412**
5413** This function returns SQLITE_CORRUPT if the page-header flags field of
5414** the new child page does not match the flags field of the parent (i.e.
5415** if an intkey page appears to be the parent of a non-intkey page, or
5416** vice-versa).
drh72f82862001-05-24 21:06:345417*/
drh3aac2dd2004-04-26 14:10:205418static int moveToChild(BtCursor *pCur, u32 newPgno){
drhd4170ac2023-06-22 01:03:395419 int rc;
dan7a2347e2016-01-07 16:43:545420 assert( cursorOwnsBtShared(pCur) );
danielk1977da184232006-01-05 11:34:325421 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:475422 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
dan11dcd112013-03-15 18:29:185423 assert( pCur->iPage>=0 );
danielk197771d5d2c2008-09-29 11:49:475424 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
5425 return SQLITE_CORRUPT_BKPT;
5426 }
drh271efa52004-05-30 19:19:055427 pCur->info.nSize = 0;
drh036dbec2014-03-11 23:40:445428 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
drh352a35a2017-08-15 03:46:475429 pCur->aiIdx[pCur->iPage] = pCur->ix;
5430 pCur->apPage[pCur->iPage] = pCur->pPage;
drh75e96b32017-04-01 00:20:065431 pCur->ix = 0;
drh352a35a2017-08-15 03:46:475432 pCur->iPage++;
drhd4170ac2023-06-22 01:03:395433 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
drhbc532ae2023-07-08 14:27:555434 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
drhd4170ac2023-06-22 01:03:395435 if( rc==SQLITE_OK
5436 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
5437 ){
5438 releasePage(pCur->pPage);
5439 rc = SQLITE_CORRUPT_PGNO(newPgno);
5440 }
5441 if( rc ){
5442 pCur->pPage = pCur->apPage[--pCur->iPage];
5443 }
5444 return rc;
drh72f82862001-05-24 21:06:345445}
5446
drhd879e3e2017-02-13 13:35:555447#ifdef SQLITE_DEBUG
danielk1977bf93c562008-09-29 15:53:255448/*
larrybrbc917382023-06-07 08:40:315449** Page pParent is an internal (non-leaf) tree page. This function
danielk1977bf93c562008-09-29 15:53:255450** asserts that page number iChild is the left-child if the iIdx'th
5451** cell in page pParent. Or, if iIdx is equal to the total number of
5452** cells in pParent, that page number iChild is the right-child of
5453** the page.
5454*/
5455static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
drhcbd33492015-03-25 13:06:545456 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
5457 ** in a corrupt database */
danielk1977bf93c562008-09-29 15:53:255458 assert( iIdx<=pParent->nCell );
5459 if( iIdx==pParent->nCell ){
5460 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
5461 }else{
5462 assert( get4byte(findCell(pParent, iIdx))==iChild );
5463 }
5464}
5465#else
larrybrbc917382023-06-07 08:40:315466# define assertParentIndex(x,y,z)
danielk1977bf93c562008-09-29 15:53:255467#endif
5468
drh72f82862001-05-24 21:06:345469/*
drh5e2f8b92001-05-28 00:41:155470** Move the cursor up to the parent page.
5471**
5472** pCur->idx is set to the cell index that contains the pointer
5473** to the page we are coming from. If we are coming from the
5474** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:575475** the largest cell index.
drh72f82862001-05-24 21:06:345476*/
danielk197730548662009-07-09 05:07:375477static void moveToParent(BtCursor *pCur){
drh352a35a2017-08-15 03:46:475478 MemPage *pLeaf;
dan7a2347e2016-01-07 16:43:545479 assert( cursorOwnsBtShared(pCur) );
danielk1977da184232006-01-05 11:34:325480 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:475481 assert( pCur->iPage>0 );
drh352a35a2017-08-15 03:46:475482 assert( pCur->pPage );
danielk1977bf93c562008-09-29 15:53:255483 assertParentIndex(
larrybrbc917382023-06-07 08:40:315484 pCur->apPage[pCur->iPage-1],
5485 pCur->aiIdx[pCur->iPage-1],
drh352a35a2017-08-15 03:46:475486 pCur->pPage->pgno
danielk1977bf93c562008-09-29 15:53:255487 );
dan6c2688c2012-01-12 15:05:035488 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
drh271efa52004-05-30 19:19:055489 pCur->info.nSize = 0;
drh036dbec2014-03-11 23:40:445490 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
drh75e96b32017-04-01 00:20:065491 pCur->ix = pCur->aiIdx[pCur->iPage-1];
drh352a35a2017-08-15 03:46:475492 pLeaf = pCur->pPage;
5493 pCur->pPage = pCur->apPage[--pCur->iPage];
5494 releasePageNotNull(pLeaf);
drh72f82862001-05-24 21:06:345495}
5496
5497/*
danielk19778f880a82009-07-13 09:41:455498** Move the cursor to point to the root page of its b-tree structure.
5499**
5500** If the table has a virtual root page, then the cursor is moved to point
5501** to the virtual root page instead of the actual root page. A table has a
larrybrbc917382023-06-07 08:40:315502** virtual root page when the actual root page contains no cells and a
danielk19778f880a82009-07-13 09:41:455503** single child page. This can only happen with the table rooted at page 1.
5504**
larrybrbc917382023-06-07 08:40:315505** If the b-tree structure is empty, the cursor state is set to
drh44548e72017-08-14 18:13:525506** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
5507** the cursor is set to point to the first cell located on the root
5508** (or virtual root) page and the cursor state is set to CURSOR_VALID.
danielk19778f880a82009-07-13 09:41:455509**
5510** If this function returns successfully, it may be assumed that the
larrybrbc917382023-06-07 08:40:315511** page-header flags indicate that the [virtual] root-page is the expected
danielk19778f880a82009-07-13 09:41:455512** kind of b-tree page (i.e. if when opening the cursor the caller did not
5513** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
larrybrbc917382023-06-07 08:40:315514** indicating a table b-tree, or if the caller did specify a KeyInfo
danielk19778f880a82009-07-13 09:41:455515** structure the flags byte is set to 0x02 or 0x0A, indicating an index
5516** b-tree).
drh72f82862001-05-24 21:06:345517*/
drh5e2f8b92001-05-28 00:41:155518static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:205519 MemPage *pRoot;
drh777e4c42006-01-13 04:31:585520 int rc = SQLITE_OK;
drhbd03cae2001-06-02 02:40:575521
dan7a2347e2016-01-07 16:43:545522 assert( cursorOwnsBtShared(pCur) );
drhfb982642007-08-30 01:19:595523 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
5524 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
5525 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
drh85ef6302017-08-02 15:50:095526 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
drh44548e72017-08-14 18:13:525527 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
danielk197771d5d2c2008-09-29 11:49:475528
5529 if( pCur->iPage>=0 ){
drh7ad3eb62016-10-24 01:01:095530 if( pCur->iPage ){
drh352a35a2017-08-15 03:46:475531 releasePageNotNull(pCur->pPage);
5532 while( --pCur->iPage ){
5533 releasePageNotNull(pCur->apPage[pCur->iPage]);
5534 }
drh7f8f6592021-12-13 19:59:555535 pRoot = pCur->pPage = pCur->apPage[0];
drh7ad3eb62016-10-24 01:01:095536 goto skip_init;
drhbbf0f862015-06-27 14:59:265537 }
dana205a482011-08-27 18:48:575538 }else if( pCur->pgnoRoot==0 ){
5539 pCur->eState = CURSOR_INVALID;
drh44548e72017-08-14 18:13:525540 return SQLITE_EMPTY;
drh777e4c42006-01-13 04:31:585541 }else{
drh28f58dd2015-06-27 19:45:035542 assert( pCur->iPage==(-1) );
drh85ef6302017-08-02 15:50:095543 if( pCur->eState>=CURSOR_REQUIRESEEK ){
5544 if( pCur->eState==CURSOR_FAULT ){
5545 assert( pCur->skipNext!=SQLITE_OK );
5546 return pCur->skipNext;
5547 }
5548 sqlite3BtreeClearCursor(pCur);
5549 }
drhe6aec722022-07-07 22:59:355550 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
drhd4170ac2023-06-22 01:03:395551 pCur->curPagerFlags);
drh4c301aa2009-07-15 17:25:455552 if( rc!=SQLITE_OK ){
drh777e4c42006-01-13 04:31:585553 pCur->eState = CURSOR_INVALID;
drhf0357d82017-08-14 17:03:585554 return rc;
drh777e4c42006-01-13 04:31:585555 }
danielk1977172114a2009-07-07 15:47:125556 pCur->iPage = 0;
drh352a35a2017-08-15 03:46:475557 pCur->curIntKey = pCur->pPage->intKey;
drhc39e0002004-05-07 23:50:575558 }
drh352a35a2017-08-15 03:46:475559 pRoot = pCur->pPage;
drhec9b6222022-03-07 18:32:085560 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
dan7df42ab2014-01-20 18:25:445561
5562 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
5563 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
5564 ** NULL, the caller expects a table b-tree. If this is not the case,
larrybrbc917382023-06-07 08:40:315565 ** return an SQLITE_CORRUPT error.
dan7df42ab2014-01-20 18:25:445566 **
5567 ** Earlier versions of SQLite assumed that this test could not fail
5568 ** if the root page was already loaded when this function was called (i.e.
larrybrbc917382023-06-07 08:40:315569 ** if pCur->iPage>=0). But this is not so if the database is corrupted
5570 ** in such a way that page pRoot is linked into a second b-tree table
dan7df42ab2014-01-20 18:25:445571 ** (or the freelist). */
5572 assert( pRoot->intKey==1 || pRoot->intKey==0 );
5573 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
daneebf2f52017-11-18 17:30:085574 return SQLITE_CORRUPT_PAGE(pCur->pPage);
dan7df42ab2014-01-20 18:25:445575 }
danielk19778f880a82009-07-13 09:41:455576
larrybrbc917382023-06-07 08:40:315577skip_init:
drh75e96b32017-04-01 00:20:065578 pCur->ix = 0;
drh271efa52004-05-30 19:19:055579 pCur->info.nSize = 0;
drh036dbec2014-03-11 23:40:445580 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
danielk197771d5d2c2008-09-29 11:49:475581
drh4e8fe3f2013-12-06 23:25:275582 if( pRoot->nCell>0 ){
5583 pCur->eState = CURSOR_VALID;
5584 }else if( !pRoot->leaf ){
drh8856d6a2004-04-29 14:42:465585 Pgno subpage;
drhc85240d2009-06-04 16:14:335586 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
drh43605152004-05-29 21:46:495587 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
danielk1977da184232006-01-05 11:34:325588 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:195589 rc = moveToChild(pCur, subpage);
danielk197771d5d2c2008-09-29 11:49:475590 }else{
drh4e8fe3f2013-12-06 23:25:275591 pCur->eState = CURSOR_INVALID;
drh44548e72017-08-14 18:13:525592 rc = SQLITE_EMPTY;
drh8856d6a2004-04-29 14:42:465593 }
5594 return rc;
drh72f82862001-05-24 21:06:345595}
drh2af926b2001-05-15 00:39:255596
drh5e2f8b92001-05-28 00:41:155597/*
5598** Move the cursor down to the left-most leaf entry beneath the
5599** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:585600**
5601** The left-most leaf is the one with the smallest key - the first
5602** in ascending order.
drh5e2f8b92001-05-28 00:41:155603*/
5604static int moveToLeftmost(BtCursor *pCur){
5605 Pgno pgno;
drhd677b3d2007-08-20 22:48:415606 int rc = SQLITE_OK;
drh3aac2dd2004-04-26 14:10:205607 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:155608
dan7a2347e2016-01-07 16:43:545609 assert( cursorOwnsBtShared(pCur) );
danielk1977da184232006-01-05 11:34:325610 assert( pCur->eState==CURSOR_VALID );
drh352a35a2017-08-15 03:46:475611 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
drh75e96b32017-04-01 00:20:065612 assert( pCur->ix<pPage->nCell );
5613 pgno = get4byte(findCell(pPage, pCur->ix));
drh8178a752003-01-05 21:41:405614 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:155615 }
drhd677b3d2007-08-20 22:48:415616 return rc;
drh5e2f8b92001-05-28 00:41:155617}
5618
drh2dcc9aa2002-12-04 13:40:255619/*
5620** Move the cursor down to the right-most leaf entry beneath the
5621** page to which it is currently pointing. Notice the difference
5622** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
5623** finds the left-most entry beneath the *entry* whereas moveToRightmost()
5624** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:585625**
5626** The right-most entry is the one with the largest key - the last
5627** key in ascending order.
drh2dcc9aa2002-12-04 13:40:255628*/
5629static int moveToRightmost(BtCursor *pCur){
5630 Pgno pgno;
drhd677b3d2007-08-20 22:48:415631 int rc = SQLITE_OK;
drh1bd10f82008-12-10 21:19:565632 MemPage *pPage = 0;
drh2dcc9aa2002-12-04 13:40:255633
dan7a2347e2016-01-07 16:43:545634 assert( cursorOwnsBtShared(pCur) );
danielk1977da184232006-01-05 11:34:325635 assert( pCur->eState==CURSOR_VALID );
drh352a35a2017-08-15 03:46:475636 while( !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:495637 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh75e96b32017-04-01 00:20:065638 pCur->ix = pPage->nCell;
drh8178a752003-01-05 21:41:405639 rc = moveToChild(pCur, pgno);
drhee6438d2014-09-01 13:29:325640 if( rc ) return rc;
drh2dcc9aa2002-12-04 13:40:255641 }
drh75e96b32017-04-01 00:20:065642 pCur->ix = pPage->nCell-1;
drhee6438d2014-09-01 13:29:325643 assert( pCur->info.nSize==0 );
5644 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
5645 return SQLITE_OK;
drh2dcc9aa2002-12-04 13:40:255646}
5647
drh5e00f6c2001-09-13 13:46:565648/* Move the cursor to the first entry in the table. Return SQLITE_OK
5649** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:585650** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:565651*/
drh3aac2dd2004-04-26 14:10:205652int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:565653 int rc;
drhd677b3d2007-08-20 22:48:415654
dan7a2347e2016-01-07 16:43:545655 assert( cursorOwnsBtShared(pCur) );
drhe5fe6902007-12-07 18:55:285656 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh5e00f6c2001-09-13 13:46:565657 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:415658 if( rc==SQLITE_OK ){
drh352a35a2017-08-15 03:46:475659 assert( pCur->pPage->nCell>0 );
drh44548e72017-08-14 18:13:525660 *pRes = 0;
5661 rc = moveToLeftmost(pCur);
5662 }else if( rc==SQLITE_EMPTY ){
drhbc532ae2023-07-08 14:27:555663 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
drh44548e72017-08-14 18:13:525664 *pRes = 1;
5665 rc = SQLITE_OK;
drh5e00f6c2001-09-13 13:46:565666 }
drh5e00f6c2001-09-13 13:46:565667 return rc;
5668}
drh5e2f8b92001-05-28 00:41:155669
drh33f32732025-07-02 14:53:485670/* Set *pRes to 1 (true) if the BTree pointed to by cursor pCur contains zero
5671** rows of content. Set *pRes to 0 (false) if the table contains content.
5672** Return SQLITE_OK on success or some error code (ex: SQLITE_NOMEM) if
5673** something goes wrong.
drh21667662025-07-02 13:19:245674*/
drh33f32732025-07-02 14:53:485675int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){
drh21667662025-07-02 13:19:245676 int rc;
5677
5678 assert( cursorOwnsBtShared(pCur) );
5679 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh33f32732025-07-02 14:53:485680 if( pCur->eState==CURSOR_VALID ){
5681 *pRes = 0;
5682 return SQLITE_OK;
5683 }
drh21667662025-07-02 13:19:245684 rc = moveToRoot(pCur);
drh33f32732025-07-02 14:53:485685 if( rc==SQLITE_EMPTY ){
5686 *pRes = 1;
5687 rc = SQLITE_OK;
5688 }else{
5689 *pRes = 0;
5690 }
5691 return rc;
drh21667662025-07-02 13:19:245692}
5693
drhae7ede42024-04-12 12:32:095694#ifdef SQLITE_DEBUG
5695/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that
5696** this flags are true for a consistent database.
5697**
5698** This routine is is called from within assert() statements only.
5699** It is an internal verification routine and does not appear in production
5700** builds.
5701*/
5702static int cursorIsAtLastEntry(BtCursor *pCur){
5703 int ii;
5704 for(ii=0; ii<pCur->iPage; ii++){
5705 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
5706 }
5707 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
5708}
5709#endif
5710
drh9562b552002-02-19 15:00:075711/* Move the cursor to the last entry in the table. Return SQLITE_OK
5712** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:585713** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:075714*/
drh85728a22022-11-19 00:22:125715static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
5716 int rc = moveToRoot(pCur);
5717 if( rc==SQLITE_OK ){
5718 assert( pCur->eState==CURSOR_VALID );
5719 *pRes = 0;
5720 rc = moveToRightmost(pCur);
5721 if( rc==SQLITE_OK ){
5722 pCur->curFlags |= BTCF_AtLast;
5723 }else{
5724 pCur->curFlags &= ~BTCF_AtLast;
5725 }
5726 }else if( rc==SQLITE_EMPTY ){
5727 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5728 *pRes = 1;
5729 rc = SQLITE_OK;
5730 }
5731 return rc;
5732}
drh3aac2dd2004-04-26 14:10:205733int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
dan7a2347e2016-01-07 16:43:545734 assert( cursorOwnsBtShared(pCur) );
drhe5fe6902007-12-07 18:55:285735 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
danielk19773f632d52009-05-02 10:03:095736
5737 /* If the cursor already points to the last entry, this is a no-op. */
drh036dbec2014-03-11 23:40:445738 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
drhae7ede42024-04-12 12:32:095739 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB );
drheb265342019-05-08 23:55:045740 *pRes = 0;
danielk19773f632d52009-05-02 10:03:095741 return SQLITE_OK;
5742 }
drh85728a22022-11-19 00:22:125743 return btreeLast(pCur, pRes);
drh9562b552002-02-19 15:00:075744}
5745
drh42a410d2021-06-19 18:32:205746/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
5747** table near the key intKey. Return a success code.
drh3aac2dd2004-04-26 14:10:205748**
drh5e2f8b92001-05-28 00:41:155749** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:575750** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:155751** were present. The cursor might point to an entry that comes
5752** before or after the key.
5753**
drh64022502009-01-09 14:11:045754** An integer is written into *pRes which is the result of
larrybrbc917382023-06-07 08:40:315755** comparing the key with the entry to which the cursor is
drh64022502009-01-09 14:11:045756** pointing. The meaning of the integer written into
5757** *pRes is as follows:
drhbd03cae2001-06-02 02:40:575758**
5759** *pRes<0 The cursor is left pointing at an entry that
drh42a410d2021-06-19 18:32:205760** is smaller than intKey or if the table is empty
drh1a844c32002-12-04 22:29:285761** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:575762**
5763** *pRes==0 The cursor is left pointing at an entry that
drh42a410d2021-06-19 18:32:205764** exactly matches intKey.
drhbd03cae2001-06-02 02:40:575765**
5766** *pRes>0 The cursor is left pointing at an entry that
drh42a410d2021-06-19 18:32:205767** is larger than intKey.
drha059ad02001-04-17 20:09:115768*/
drh42a410d2021-06-19 18:32:205769int sqlite3BtreeTableMoveto(
drhe63d9992008-08-13 19:11:485770 BtCursor *pCur, /* The cursor to be moved */
drhe63d9992008-08-13 19:11:485771 i64 intKey, /* The table key */
5772 int biasRight, /* If true, bias the search to the high end */
5773 int *pRes /* Write search results here */
drhe4d90812007-03-29 05:51:495774){
drh72f82862001-05-24 21:06:345775 int rc;
drhd677b3d2007-08-20 22:48:415776
dan7a2347e2016-01-07 16:43:545777 assert( cursorOwnsBtShared(pCur) );
drhe5fe6902007-12-07 18:55:285778 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
danielk19775cb09632009-07-09 11:36:015779 assert( pRes );
drh42a410d2021-06-19 18:32:205780 assert( pCur->pKeyInfo==0 );
5781 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
drha2c20e42008-03-29 16:01:045782
5783 /* If the cursor is already positioned at the point we are trying
5784 ** to move to, then just return without doing any work */
drh42a410d2021-06-19 18:32:205785 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
drhe63d9992008-08-13 19:11:485786 if( pCur->info.nKey==intKey ){
drha2c20e42008-03-29 16:01:045787 *pRes = 0;
5788 return SQLITE_OK;
5789 }
drh451e76d2017-01-21 16:54:195790 if( pCur->info.nKey<intKey ){
5791 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
drhae7ede42024-04-12 12:32:095792 assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB );
drh451e76d2017-01-21 16:54:195793 *pRes = -1;
5794 return SQLITE_OK;
5795 }
drh7f11afa2017-01-21 21:47:545796 /* If the requested key is one more than the previous key, then
5797 ** try to get there using sqlite3BtreeNext() rather than a full
5798 ** binary search. This is an optimization only. The correct answer
larrybrbc917382023-06-07 08:40:315799 ** is still obtained without this case, only a little more slowly. */
drh0c873bf2019-01-28 00:42:065800 if( pCur->info.nKey+1==intKey ){
drh7f11afa2017-01-21 21:47:545801 *pRes = 0;
drh2ab792e2017-05-30 18:34:075802 rc = sqlite3BtreeNext(pCur, 0);
5803 if( rc==SQLITE_OK ){
drh7f11afa2017-01-21 21:47:545804 getCellInfo(pCur);
5805 if( pCur->info.nKey==intKey ){
5806 return SQLITE_OK;
5807 }
drhe85e1da2021-10-01 21:01:075808 }else if( rc!=SQLITE_DONE ){
drh2ab792e2017-05-30 18:34:075809 return rc;
drh451e76d2017-01-21 16:54:195810 }
5811 }
drha2c20e42008-03-29 16:01:045812 }
5813 }
5814
drh37ccfcf2020-08-31 18:49:045815#ifdef SQLITE_DEBUG
5816 pCur->pBtree->nSeek++; /* Performance measurement during testing */
5817#endif
5818
drh42a410d2021-06-19 18:32:205819 rc = moveToRoot(pCur);
5820 if( rc ){
5821 if( rc==SQLITE_EMPTY ){
5822 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5823 *pRes = -1;
5824 return SQLITE_OK;
5825 }
5826 return rc;
dan1fed5da2014-02-25 21:01:255827 }
drh42a410d2021-06-19 18:32:205828 assert( pCur->pPage );
5829 assert( pCur->pPage->isInit );
5830 assert( pCur->eState==CURSOR_VALID );
5831 assert( pCur->pPage->nCell > 0 );
5832 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
5833 assert( pCur->curIntKey );
5834
5835 for(;;){
5836 int lwr, upr, idx, c;
5837 Pgno chldPg;
5838 MemPage *pPage = pCur->pPage;
5839 u8 *pCell; /* Pointer to current cell in pPage */
5840
5841 /* pPage->nCell must be greater than zero. If this is the root-page
5842 ** the cursor would have been INVALID above and this for(;;) loop
5843 ** not run. If this is not the root-page, then the moveToChild() routine
5844 ** would have already detected db corruption. Similarly, pPage must
5845 ** be the right kind (index or table) of b-tree page. Otherwise
5846 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5847 assert( pPage->nCell>0 );
5848 assert( pPage->intKey );
5849 lwr = 0;
5850 upr = pPage->nCell-1;
5851 assert( biasRight==0 || biasRight==1 );
5852 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
drh42a410d2021-06-19 18:32:205853 for(;;){
5854 i64 nCellKey;
5855 pCell = findCellPastPtr(pPage, idx);
5856 if( pPage->intKeyLeaf ){
5857 while( 0x80 <= *(pCell++) ){
5858 if( pCell>=pPage->aDataEnd ){
5859 return SQLITE_CORRUPT_PAGE(pPage);
5860 }
5861 }
5862 }
5863 getVarint(pCell, (u64*)&nCellKey);
5864 if( nCellKey<intKey ){
5865 lwr = idx+1;
5866 if( lwr>upr ){ c = -1; break; }
5867 }else if( nCellKey>intKey ){
5868 upr = idx-1;
5869 if( lwr>upr ){ c = +1; break; }
5870 }else{
5871 assert( nCellKey==intKey );
5872 pCur->ix = (u16)idx;
5873 if( !pPage->leaf ){
5874 lwr = idx;
5875 goto moveto_table_next_layer;
5876 }else{
5877 pCur->curFlags |= BTCF_ValidNKey;
5878 pCur->info.nKey = nCellKey;
5879 pCur->info.nSize = 0;
5880 *pRes = 0;
5881 return SQLITE_OK;
5882 }
5883 }
5884 assert( lwr+upr>=0 );
5885 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5886 }
5887 assert( lwr==upr+1 || !pPage->leaf );
5888 assert( pPage->isInit );
5889 if( pPage->leaf ){
5890 assert( pCur->ix<pCur->pPage->nCell );
5891 pCur->ix = (u16)idx;
5892 *pRes = c;
5893 rc = SQLITE_OK;
5894 goto moveto_table_finish;
5895 }
5896moveto_table_next_layer:
5897 if( lwr>=pPage->nCell ){
5898 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5899 }else{
5900 chldPg = get4byte(findCell(pPage, lwr));
5901 }
5902 pCur->ix = (u16)lwr;
5903 rc = moveToChild(pCur, chldPg);
5904 if( rc ) break;
5905 }
5906moveto_table_finish:
5907 pCur->info.nSize = 0;
5908 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5909 return rc;
5910}
5911
drhc5a55db2022-03-07 01:29:365912/*
drh397b82c2025-06-19 19:33:065913** Compare the "idx"-th cell on the page pPage against the key
5914** pointing to by pIdxKey using xRecordCompare. Return negative or
drhc5a55db2022-03-07 01:29:365915** zero if the cell is less than or equal pIdxKey. Return positive
5916** if unknown.
5917**
5918** Return value negative: Cell at pCur[idx] less than pIdxKey
5919**
5920** Return value is zero: Cell at pCur[idx] equals pIdxKey
5921**
5922** Return value positive: Nothing is known about the relationship
5923** of the cell at pCur[idx] and pIdxKey.
5924**
5925** This routine is part of an optimization. It is always safe to return
5926** a positive value as that will cause the optimization to be skipped.
5927*/
5928static int indexCellCompare(
drh397b82c2025-06-19 19:33:065929 MemPage *pPage,
drhc5a55db2022-03-07 01:29:365930 int idx,
5931 UnpackedRecord *pIdxKey,
5932 RecordCompare xRecordCompare
5933){
drhc5a55db2022-03-07 01:29:365934 int c;
5935 int nCell; /* Size of the pCell cell in bytes */
5936 u8 *pCell = findCellPastPtr(pPage, idx);
5937
5938 nCell = pCell[0];
5939 if( nCell<=pPage->max1bytePayload ){
5940 /* This branch runs if the record-size field of the cell is a
5941 ** single byte varint and the record fits entirely on the main
5942 ** b-tree page. */
5943 testcase( pCell+nCell+1==pPage->aDataEnd );
5944 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
larrybrbc917382023-06-07 08:40:315945 }else if( !(pCell[1] & 0x80)
drhc5a55db2022-03-07 01:29:365946 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5947 ){
larrybrbc917382023-06-07 08:40:315948 /* The record-size field is a 2 byte varint and the record
drhc5a55db2022-03-07 01:29:365949 ** fits entirely on the main b-tree page. */
5950 testcase( pCell+nCell+2==pPage->aDataEnd );
5951 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5952 }else{
5953 /* If the record extends into overflow pages, do not attempt
5954 ** the optimization. */
5955 c = 99;
5956 }
5957 return c;
5958}
5959
5960/*
5961** Return true (non-zero) if pCur is current pointing to the last
5962** page of a table.
5963*/
5964static int cursorOnLastPage(BtCursor *pCur){
5965 int i;
5966 assert( pCur->eState==CURSOR_VALID );
5967 for(i=0; i<pCur->iPage; i++){
5968 MemPage *pPage = pCur->apPage[i];
5969 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
5970 }
5971 return 1;
5972}
5973
drh42a410d2021-06-19 18:32:205974/* Move the cursor so that it points to an entry in an index table
5975** near the key pIdxKey. Return a success code.
5976**
5977** If an exact match is not found, then the cursor is always
5978** left pointing at a leaf page which would hold the entry if it
5979** were present. The cursor might point to an entry that comes
5980** before or after the key.
5981**
5982** An integer is written into *pRes which is the result of
larrybrbc917382023-06-07 08:40:315983** comparing the key with the entry to which the cursor is
drh42a410d2021-06-19 18:32:205984** pointing. The meaning of the integer written into
5985** *pRes is as follows:
5986**
5987** *pRes<0 The cursor is left pointing at an entry that
5988** is smaller than pIdxKey or if the table is empty
5989** and the cursor is therefore left point to nothing.
5990**
5991** *pRes==0 The cursor is left pointing at an entry that
5992** exactly matches pIdxKey.
5993**
5994** *pRes>0 The cursor is left pointing at an entry that
5995** is larger than pIdxKey.
5996**
5997** The pIdxKey->eqSeen field is set to 1 if there
larrybrbc917382023-06-07 08:40:315998** exists an entry in the table that exactly matches pIdxKey.
drh42a410d2021-06-19 18:32:205999*/
6000int sqlite3BtreeIndexMoveto(
6001 BtCursor *pCur, /* The cursor to be moved */
6002 UnpackedRecord *pIdxKey, /* Unpacked index key */
6003 int *pRes /* Write search results here */
6004){
6005 int rc;
6006 RecordCompare xRecordCompare;
6007
6008 assert( cursorOwnsBtShared(pCur) );
6009 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
6010 assert( pRes );
6011 assert( pCur->pKeyInfo!=0 );
6012
6013#ifdef SQLITE_DEBUG
6014 pCur->pBtree->nSeek++; /* Performance measurement during testing */
6015#endif
6016
6017 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
6018 pIdxKey->errCode = 0;
larrybrbc917382023-06-07 08:40:316019 assert( pIdxKey->default_rc==1
6020 || pIdxKey->default_rc==0
drh42a410d2021-06-19 18:32:206021 || pIdxKey->default_rc==-1
6022 );
dan1fed5da2014-02-25 21:01:256023
drhc5a55db2022-03-07 01:29:366024
6025 /* Check to see if we can skip a lot of work. Two cases:
6026 **
6027 ** (1) If the cursor is already pointing to the very last cell
6028 ** in the table and the pIdxKey search key is greater than or
6029 ** equal to that last cell, then no movement is required.
6030 **
6031 ** (2) If the cursor is on the last page of the table and the first
6032 ** cell on that last page is less than or equal to the pIdxKey
6033 ** search key, then we can start the search on the current page
6034 ** without needing to go back to root.
6035 */
6036 if( pCur->eState==CURSOR_VALID
6037 && pCur->pPage->leaf
6038 && cursorOnLastPage(pCur)
6039 ){
6040 int c;
6041 if( pCur->ix==pCur->pPage->nCell-1
drh397b82c2025-06-19 19:33:066042 && (c = indexCellCompare(pCur->pPage,pCur->ix,pIdxKey,xRecordCompare))<=0
drh605137a2022-03-11 14:20:066043 && pIdxKey->errCode==SQLITE_OK
drhc5a55db2022-03-07 01:29:366044 ){
6045 *pRes = c;
6046 return SQLITE_OK; /* Cursor already pointing at the correct spot */
6047 }
larrybrbc917382023-06-07 08:40:316048 if( pCur->iPage>0
drh397b82c2025-06-19 19:33:066049 && indexCellCompare(pCur->pPage, 0, pIdxKey, xRecordCompare)<=0
drh605137a2022-03-11 14:20:066050 && pIdxKey->errCode==SQLITE_OK
drhc5a55db2022-03-07 01:29:366051 ){
danccd34892024-08-29 16:27:576052 pCur->curFlags &= ~(BTCF_ValidOvfl|BTCF_AtLast);
drh1d497682022-06-19 16:55:076053 if( !pCur->pPage->isInit ){
6054 return SQLITE_CORRUPT_BKPT;
6055 }
drhc5a55db2022-03-07 01:29:366056 goto bypass_moveto_root; /* Start search on the current page */
6057 }
drh605137a2022-03-11 14:20:066058 pIdxKey->errCode = SQLITE_OK;
drhc5a55db2022-03-07 01:29:366059 }
6060
drh5e2f8b92001-05-28 00:41:156061 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:416062 if( rc ){
drh44548e72017-08-14 18:13:526063 if( rc==SQLITE_EMPTY ){
drh352a35a2017-08-15 03:46:476064 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
drh44548e72017-08-14 18:13:526065 *pRes = -1;
6066 return SQLITE_OK;
6067 }
drhd677b3d2007-08-20 22:48:416068 return rc;
6069 }
drhc5a55db2022-03-07 01:29:366070
6071bypass_moveto_root:
drh352a35a2017-08-15 03:46:476072 assert( pCur->pPage );
6073 assert( pCur->pPage->isInit );
drh44548e72017-08-14 18:13:526074 assert( pCur->eState==CURSOR_VALID );
drh352a35a2017-08-15 03:46:476075 assert( pCur->pPage->nCell > 0 );
drhc5a55db2022-03-07 01:29:366076 assert( pCur->curIntKey==0 );
6077 assert( pIdxKey!=0 );
drh14684382006-11-30 13:05:296078 for(;;){
drhec3e6b12013-11-25 02:38:556079 int lwr, upr, idx, c;
drh72f82862001-05-24 21:06:346080 Pgno chldPg;
drh352a35a2017-08-15 03:46:476081 MemPage *pPage = pCur->pPage;
drhec3e6b12013-11-25 02:38:556082 u8 *pCell; /* Pointer to current cell in pPage */
danielk1977171fff32009-07-11 05:06:516083
6084 /* pPage->nCell must be greater than zero. If this is the root-page
6085 ** the cursor would have been INVALID above and this for(;;) loop
6086 ** not run. If this is not the root-page, then the moveToChild() routine
danielk19773fd7cf52009-07-13 07:30:526087 ** would have already detected db corruption. Similarly, pPage must
6088 ** be the right kind (index or table) of b-tree page. Otherwise
6089 ** a moveToChild() or moveToRoot() call would have detected corruption. */
danielk1977171fff32009-07-11 05:06:516090 assert( pPage->nCell>0 );
drhc5a55db2022-03-07 01:29:366091 assert( pPage->intKey==0 );
drh72f82862001-05-24 21:06:346092 lwr = 0;
6093 upr = pPage->nCell-1;
drh42a410d2021-06-19 18:32:206094 idx = upr>>1; /* idx = (lwr+upr)/2; */
drh42a410d2021-06-19 18:32:206095 for(;;){
6096 int nCell; /* Size of the pCell cell in bytes */
6097 pCell = findCellPastPtr(pPage, idx);
drhec3e6b12013-11-25 02:38:556098
drh42a410d2021-06-19 18:32:206099 /* The maximum supported page-size is 65536 bytes. This means that
6100 ** the maximum number of record bytes stored on an index B-Tree
6101 ** page is less than 16384 bytes and may be stored as a 2-byte
larrybrbc917382023-06-07 08:40:316102 ** varint. This information is used to attempt to avoid parsing
6103 ** the entire cell by checking for the cases where the record is
6104 ** stored entirely within the b-tree page by inspecting the first
drh42a410d2021-06-19 18:32:206105 ** 2 bytes of the cell.
6106 */
6107 nCell = pCell[0];
6108 if( nCell<=pPage->max1bytePayload ){
6109 /* This branch runs if the record-size field of the cell is a
6110 ** single byte varint and the record fits entirely on the main
6111 ** b-tree page. */
6112 testcase( pCell+nCell+1==pPage->aDataEnd );
6113 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
larrybrbc917382023-06-07 08:40:316114 }else if( !(pCell[1] & 0x80)
drh42a410d2021-06-19 18:32:206115 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
6116 ){
larrybrbc917382023-06-07 08:40:316117 /* The record-size field is a 2 byte varint and the record
drh42a410d2021-06-19 18:32:206118 ** fits entirely on the main b-tree page. */
6119 testcase( pCell+nCell+2==pPage->aDataEnd );
6120 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
6121 }else{
6122 /* The record flows over onto one or more overflow pages. In
6123 ** this case the whole cell needs to be parsed, a buffer allocated
6124 ** and accessPayload() used to retrieve the record into the
larrybrbc917382023-06-07 08:40:316125 ** buffer before VdbeRecordCompare() can be called.
drh42a410d2021-06-19 18:32:206126 **
6127 ** If the record is corrupt, the xRecordCompare routine may read
larrybrbc917382023-06-07 08:40:316128 ** up to two varints past the end of the buffer. An extra 18
drh42a410d2021-06-19 18:32:206129 ** bytes of padding is allocated at the end of the buffer in
6130 ** case this happens. */
6131 void *pCellKey;
6132 u8 * const pCellBody = pCell - pPage->childPtrSize;
6133 const int nOverrun = 18; /* Size of the overrun padding */
6134 pPage->xParseCell(pPage, pCellBody, &pCur->info);
6135 nCell = (int)pCur->info.nKey;
6136 testcase( nCell<0 ); /* True if key size is 2^32 or more */
6137 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
6138 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
6139 testcase( nCell==2 ); /* Minimum legal index key size */
6140 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
6141 rc = SQLITE_CORRUPT_PAGE(pPage);
6142 goto moveto_index_finish;
6143 }
drhef86b942025-02-17 17:33:146144 pCellKey = sqlite3Malloc( (u64)nCell+(u64)nOverrun );
drh42a410d2021-06-19 18:32:206145 if( pCellKey==0 ){
6146 rc = SQLITE_NOMEM_BKPT;
6147 goto moveto_index_finish;
6148 }
6149 pCur->ix = (u16)idx;
6150 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
6151 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
6152 pCur->curFlags &= ~BTCF_ValidOvfl;
6153 if( rc ){
drhfacf0302008-06-17 15:12:006154 sqlite3_free(pCellKey);
drh42a410d2021-06-19 18:32:206155 goto moveto_index_finish;
drhe51c44f2004-05-30 20:46:096156 }
drh42a410d2021-06-19 18:32:206157 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
6158 sqlite3_free(pCellKey);
drh72f82862001-05-24 21:06:346159 }
larrybrbc917382023-06-07 08:40:316160 assert(
drh42a410d2021-06-19 18:32:206161 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
6162 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
6163 );
6164 if( c<0 ){
6165 lwr = idx+1;
6166 }else if( c>0 ){
6167 upr = idx-1;
6168 }else{
6169 assert( c==0 );
6170 *pRes = 0;
6171 rc = SQLITE_OK;
6172 pCur->ix = (u16)idx;
6173 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
6174 goto moveto_index_finish;
6175 }
6176 if( lwr>upr ) break;
6177 assert( lwr+upr>=0 );
6178 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
drh72f82862001-05-24 21:06:346179 }
drhb07028f2011-10-14 21:49:186180 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
danielk197771d5d2c2008-09-29 11:49:476181 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:206182 if( pPage->leaf ){
drh3b79f752022-04-13 10:49:506183 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
drh75e96b32017-04-01 00:20:066184 pCur->ix = (u16)idx;
drhec3e6b12013-11-25 02:38:556185 *pRes = c;
6186 rc = SQLITE_OK;
drh42a410d2021-06-19 18:32:206187 goto moveto_index_finish;
drhebf10b12013-11-25 17:38:266188 }
drhebf10b12013-11-25 17:38:266189 if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:496190 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:346191 }else{
danielk19771cc5ed82007-05-16 17:28:436192 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:346193 }
drhd4170ac2023-06-22 01:03:396194
6195 /* This block is similar to an in-lined version of:
6196 **
6197 ** pCur->ix = (u16)lwr;
6198 ** rc = moveToChild(pCur, chldPg);
6199 ** if( rc ) break;
6200 */
6201 pCur->info.nSize = 0;
6202 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
6203 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
6204 return SQLITE_CORRUPT_BKPT;
6205 }
6206 pCur->aiIdx[pCur->iPage] = (u16)lwr;
6207 pCur->apPage[pCur->iPage] = pCur->pPage;
6208 pCur->ix = 0;
6209 pCur->iPage++;
6210 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
6211 if( rc==SQLITE_OK
6212 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
6213 ){
6214 releasePage(pCur->pPage);
6215 rc = SQLITE_CORRUPT_PGNO(chldPg);
6216 }
6217 if( rc ){
6218 pCur->pPage = pCur->apPage[--pCur->iPage];
6219 break;
6220 }
6221 /*
6222 ***** End of in-lined moveToChild() call */
6223 }
drh42a410d2021-06-19 18:32:206224moveto_index_finish:
drhd2022b02013-11-25 16:23:526225 pCur->info.nSize = 0;
drhd95ef5c2016-11-11 18:19:056226 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
drhe63d9992008-08-13 19:11:486227 return rc;
6228}
6229
drhd677b3d2007-08-20 22:48:416230
drh72f82862001-05-24 21:06:346231/*
drhc39e0002004-05-07 23:50:576232** Return TRUE if the cursor is not pointing at an entry of the table.
6233**
6234** TRUE will be returned after a call to sqlite3BtreeNext() moves
6235** past the last entry in the table or sqlite3BtreePrev() moves past
6236** the first entry. TRUE is also returned if the table is empty.
6237*/
6238int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:326239 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
6240 ** have been deleted? This API will need to change to return an error code
6241 ** as well as the boolean result value.
6242 */
6243 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:576244}
6245
6246/*
drh5e98e832017-02-17 19:24:066247** Return an estimate for the number of rows in the table that pCur is
larrybrbc917382023-06-07 08:40:316248** pointing to. Return a negative number if no estimate is currently
drh5e98e832017-02-17 19:24:066249** available.
6250*/
6251i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
6252 i64 n;
6253 u8 i;
6254
6255 assert( cursorOwnsBtShared(pCur) );
6256 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh555227b2017-02-23 02:15:336257
drhe7bdb212024-02-19 16:22:586258 /* Currently this interface is only called by the OP_IfSizeBetween
6259 ** opcode and the OP_Count opcode with P3=1. In either case,
6260 ** the cursor will always be valid unless the btree is empty. */
6261 if( pCur->eState!=CURSOR_VALID ) return 0;
drh352a35a2017-08-15 03:46:476262 if( NEVER(pCur->pPage->leaf==0) ) return -1;
drh555227b2017-02-23 02:15:336263
drh352a35a2017-08-15 03:46:476264 n = pCur->pPage->nCell;
6265 for(i=0; i<pCur->iPage; i++){
drh37794b42025-06-19 20:00:376266 n *= pCur->apPage[i]->nCell+1;
drh5e98e832017-02-17 19:24:066267 }
6268 return n;
6269}
6270
6271/*
larrybrbc917382023-06-07 08:40:316272** Advance the cursor to the next entry in the database.
drh2ab792e2017-05-30 18:34:076273** Return value:
6274**
6275** SQLITE_OK success
6276** SQLITE_DONE cursor is already pointing at the last element
6277** otherwise some kind of error occurred
drhe39a7322014-02-03 14:04:116278**
drhee6438d2014-09-01 13:29:326279** The main entry point is sqlite3BtreeNext(). That routine is optimized
6280** for the common case of merely incrementing the cell counter BtCursor.aiIdx
6281** to the next cell on the current page. The (slower) btreeNext() helper
6282** routine is called when it is necessary to move to a different page or
6283** to restore the cursor.
6284**
drh89997982017-07-11 18:11:336285** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
6286** cursor corresponds to an SQL index and this routine could have been
6287** skipped if the SQL index had been a unique index. The F argument
6288** is a hint to the implement. SQLite btree implementation does not use
6289** this hint, but COMDB2 does.
drh72f82862001-05-24 21:06:346290*/
drh89997982017-07-11 18:11:336291static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
drh72f82862001-05-24 21:06:346292 int rc;
danielk197771d5d2c2008-09-29 11:49:476293 int idx;
danielk197797a227c2006-01-20 16:32:046294 MemPage *pPage;
drh8b18dd42004-05-12 19:18:156295
dan7a2347e2016-01-07 16:43:546296 assert( cursorOwnsBtShared(pCur) );
drhf66f26a2013-08-19 20:04:106297 if( pCur->eState!=CURSOR_VALID ){
drhee6438d2014-09-01 13:29:326298 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
drhf66f26a2013-08-19 20:04:106299 rc = restoreCursorPosition(pCur);
6300 if( rc!=SQLITE_OK ){
6301 return rc;
6302 }
6303 if( CURSOR_INVALID==pCur->eState ){
drh2ab792e2017-05-30 18:34:076304 return SQLITE_DONE;
drhf66f26a2013-08-19 20:04:106305 }
drh0c873bf2019-01-28 00:42:066306 if( pCur->eState==CURSOR_SKIPNEXT ){
drh9b47ee32013-08-20 03:13:516307 pCur->eState = CURSOR_VALID;
drh0c873bf2019-01-28 00:42:066308 if( pCur->skipNext>0 ) return SQLITE_OK;
drhf66f26a2013-08-19 20:04:106309 }
danielk1977da184232006-01-05 11:34:326310 }
danielk1977da184232006-01-05 11:34:326311
drh352a35a2017-08-15 03:46:476312 pPage = pCur->pPage;
drh75e96b32017-04-01 00:20:066313 idx = ++pCur->ix;
drh1eca5b52023-03-20 18:35:486314 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
6315 if( !pPage->isInit ){
drhf3cd0c82018-06-08 19:13:576316 return SQLITE_CORRUPT_BKPT;
6317 }
danbb246c42012-01-12 14:25:556318
danielk197771d5d2c2008-09-29 11:49:476319 if( idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:426320 if( !pPage->leaf ){
drh43605152004-05-29 21:46:496321 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drhee6438d2014-09-01 13:29:326322 if( rc ) return rc;
6323 return moveToLeftmost(pCur);
drh72f82862001-05-24 21:06:346324 }
drh5e2f8b92001-05-28 00:41:156325 do{
danielk197771d5d2c2008-09-29 11:49:476326 if( pCur->iPage==0 ){
danielk1977da184232006-01-05 11:34:326327 pCur->eState = CURSOR_INVALID;
drh2ab792e2017-05-30 18:34:076328 return SQLITE_DONE;
drh5e2f8b92001-05-28 00:41:156329 }
danielk197730548662009-07-09 05:07:376330 moveToParent(pCur);
drh352a35a2017-08-15 03:46:476331 pPage = pCur->pPage;
drh75e96b32017-04-01 00:20:066332 }while( pCur->ix>=pPage->nCell );
drh44845222008-07-17 18:39:576333 if( pPage->intKey ){
drh89997982017-07-11 18:11:336334 return sqlite3BtreeNext(pCur, 0);
drh8b18dd42004-05-12 19:18:156335 }else{
drhee6438d2014-09-01 13:29:326336 return SQLITE_OK;
drh8b18dd42004-05-12 19:18:156337 }
drh8178a752003-01-05 21:41:406338 }
drh3aac2dd2004-04-26 14:10:206339 if( pPage->leaf ){
drh8178a752003-01-05 21:41:406340 return SQLITE_OK;
drhee6438d2014-09-01 13:29:326341 }else{
6342 return moveToLeftmost(pCur);
drh72f82862001-05-24 21:06:346343 }
drh72f82862001-05-24 21:06:346344}
drh2ab792e2017-05-30 18:34:076345int sqlite3BtreeNext(BtCursor *pCur, int flags){
drhee6438d2014-09-01 13:29:326346 MemPage *pPage;
drh89997982017-07-11 18:11:336347 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
dan7a2347e2016-01-07 16:43:546348 assert( cursorOwnsBtShared(pCur) );
drh2ab792e2017-05-30 18:34:076349 assert( flags==0 || flags==1 );
drhee6438d2014-09-01 13:29:326350 pCur->info.nSize = 0;
6351 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
drh89997982017-07-11 18:11:336352 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
drh352a35a2017-08-15 03:46:476353 pPage = pCur->pPage;
drh75e96b32017-04-01 00:20:066354 if( (++pCur->ix)>=pPage->nCell ){
6355 pCur->ix--;
drh89997982017-07-11 18:11:336356 return btreeNext(pCur);
drhee6438d2014-09-01 13:29:326357 }
6358 if( pPage->leaf ){
6359 return SQLITE_OK;
6360 }else{
6361 return moveToLeftmost(pCur);
6362 }
6363}
drh72f82862001-05-24 21:06:346364
drh3b7511c2001-05-26 13:15:446365/*
drh2ab792e2017-05-30 18:34:076366** Step the cursor to the back to the previous entry in the database.
6367** Return values:
6368**
6369** SQLITE_OK success
6370** SQLITE_DONE the cursor is already on the first element of the table
6371** otherwise some kind of error occurred
drhe39a7322014-02-03 14:04:116372**
drhee6438d2014-09-01 13:29:326373** The main entry point is sqlite3BtreePrevious(). That routine is optimized
6374** for the common case of merely decrementing the cell counter BtCursor.aiIdx
drh3f387402014-09-24 01:23:006375** to the previous cell on the current page. The (slower) btreePrevious()
6376** helper routine is called when it is necessary to move to a different page
6377** or to restore the cursor.
drhee6438d2014-09-01 13:29:326378**
drh89997982017-07-11 18:11:336379** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
6380** the cursor corresponds to an SQL index and this routine could have been
6381** skipped if the SQL index had been a unique index. The F argument is a
6382** hint to the implement. The native SQLite btree implementation does not
6383** use this hint, but COMDB2 does.
drh2dcc9aa2002-12-04 13:40:256384*/
drh89997982017-07-11 18:11:336385static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
drh2dcc9aa2002-12-04 13:40:256386 int rc;
drh8178a752003-01-05 21:41:406387 MemPage *pPage;
danielk1977da184232006-01-05 11:34:326388
dan7a2347e2016-01-07 16:43:546389 assert( cursorOwnsBtShared(pCur) );
drhee6438d2014-09-01 13:29:326390 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
6391 assert( pCur->info.nSize==0 );
drhf66f26a2013-08-19 20:04:106392 if( pCur->eState!=CURSOR_VALID ){
drh7682a472014-09-29 15:00:286393 rc = restoreCursorPosition(pCur);
drhee6438d2014-09-01 13:29:326394 if( rc!=SQLITE_OK ){
6395 return rc;
drhf66f26a2013-08-19 20:04:106396 }
6397 if( CURSOR_INVALID==pCur->eState ){
drh2ab792e2017-05-30 18:34:076398 return SQLITE_DONE;
drhf66f26a2013-08-19 20:04:106399 }
drh0c873bf2019-01-28 00:42:066400 if( CURSOR_SKIPNEXT==pCur->eState ){
drh9b47ee32013-08-20 03:13:516401 pCur->eState = CURSOR_VALID;
drh0c873bf2019-01-28 00:42:066402 if( pCur->skipNext<0 ) return SQLITE_OK;
drhf66f26a2013-08-19 20:04:106403 }
danielk1977da184232006-01-05 11:34:326404 }
danielk1977da184232006-01-05 11:34:326405
drh352a35a2017-08-15 03:46:476406 pPage = pCur->pPage;
drhd9215332024-01-28 00:31:476407 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
6408 if( !pPage->isInit ){
6409 return SQLITE_CORRUPT_BKPT;
6410 }
drha34b6762004-05-07 13:30:426411 if( !pPage->leaf ){
drh75e96b32017-04-01 00:20:066412 int idx = pCur->ix;
danielk197771d5d2c2008-09-29 11:49:476413 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
drhee6438d2014-09-01 13:29:326414 if( rc ) return rc;
drh2dcc9aa2002-12-04 13:40:256415 rc = moveToRightmost(pCur);
6416 }else{
drh75e96b32017-04-01 00:20:066417 while( pCur->ix==0 ){
danielk197771d5d2c2008-09-29 11:49:476418 if( pCur->iPage==0 ){
danielk1977da184232006-01-05 11:34:326419 pCur->eState = CURSOR_INVALID;
drh2ab792e2017-05-30 18:34:076420 return SQLITE_DONE;
drh2dcc9aa2002-12-04 13:40:256421 }
danielk197730548662009-07-09 05:07:376422 moveToParent(pCur);
drh2dcc9aa2002-12-04 13:40:256423 }
drhee6438d2014-09-01 13:29:326424 assert( pCur->info.nSize==0 );
drhd95ef5c2016-11-11 18:19:056425 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
danielk197771d5d2c2008-09-29 11:49:476426
drh75e96b32017-04-01 00:20:066427 pCur->ix--;
drh352a35a2017-08-15 03:46:476428 pPage = pCur->pPage;
drh44845222008-07-17 18:39:576429 if( pPage->intKey && !pPage->leaf ){
drh89997982017-07-11 18:11:336430 rc = sqlite3BtreePrevious(pCur, 0);
drh8b18dd42004-05-12 19:18:156431 }else{
6432 rc = SQLITE_OK;
6433 }
drh2dcc9aa2002-12-04 13:40:256434 }
drh2dcc9aa2002-12-04 13:40:256435 return rc;
6436}
drh2ab792e2017-05-30 18:34:076437int sqlite3BtreePrevious(BtCursor *pCur, int flags){
dan7a2347e2016-01-07 16:43:546438 assert( cursorOwnsBtShared(pCur) );
drh2ab792e2017-05-30 18:34:076439 assert( flags==0 || flags==1 );
drh89997982017-07-11 18:11:336440 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
drhee6438d2014-09-01 13:29:326441 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
6442 pCur->info.nSize = 0;
6443 if( pCur->eState!=CURSOR_VALID
drh75e96b32017-04-01 00:20:066444 || pCur->ix==0
drh352a35a2017-08-15 03:46:476445 || pCur->pPage->leaf==0
drhee6438d2014-09-01 13:29:326446 ){
drh89997982017-07-11 18:11:336447 return btreePrevious(pCur);
drhee6438d2014-09-01 13:29:326448 }
drh75e96b32017-04-01 00:20:066449 pCur->ix--;
drhee6438d2014-09-01 13:29:326450 return SQLITE_OK;
6451}
drh2dcc9aa2002-12-04 13:40:256452
6453/*
drh3b7511c2001-05-26 13:15:446454** Allocate a new page from the database file.
6455**
danielk19773b8a05f2007-03-19 17:44:266456** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:446457** has already been called on the new page.) The new page has also
6458** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:266459** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:446460**
6461** SQLITE_OK is returned on success. Any other return value indicates
drh1c8bade2015-05-29 18:42:116462** an error. *ppPage is set to NULL in the event of an error.
drhbea00b92002-07-08 10:59:506463**
larrybrbc917382023-06-07 08:40:316464** If the "nearby" parameter is not 0, then an effort is made to
drh199e3cf2002-07-18 11:01:476465** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:506466** attempt to keep related pages close to each other in the database file,
6467** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:026468**
drh82e647d2013-03-02 03:25:556469** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
6470** anywhere on the free-list, then it is guaranteed to be returned. If
6471** eMode is BTALLOC_LT then the page returned will be less than or equal
6472** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
6473** are no restrictions on which page is returned.
drh3b7511c2001-05-26 13:15:446474*/
drh4f0c5872007-03-26 22:05:016475static int allocateBtreePage(
drh82e647d2013-03-02 03:25:556476 BtShared *pBt, /* The btree */
6477 MemPage **ppPage, /* Store pointer to the allocated page here */
6478 Pgno *pPgno, /* Store the page number here */
6479 Pgno nearby, /* Search for a page near this one */
6480 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
danielk1977cb1a7eb2004-11-05 12:27:026481){
drh3aac2dd2004-04-26 14:10:206482 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:006483 int rc;
drh35cd6432009-06-05 14:17:216484 u32 n; /* Number of pages on the freelist */
drh042d6a12009-06-17 13:57:166485 u32 k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:516486 MemPage *pTrunk = 0;
6487 MemPage *pPrevTrunk = 0;
drh1662b5a2009-06-04 19:06:096488 Pgno mxPage; /* Total size of the database file */
drh30e58752002-03-02 20:41:576489
drh1fee73e2007-08-29 04:00:576490 assert( sqlite3_mutex_held(pBt->mutex) );
dan09ff9e12013-03-11 11:49:036491 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
drh3aac2dd2004-04-26 14:10:206492 pPage1 = pBt->pPage1;
drhb1299152010-03-30 22:58:336493 mxPage = btreePagecount(pBt);
drhda017572022-11-07 12:21:066494 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
6495 ** stores the total number of pages on the freelist. */
drh3aac2dd2004-04-26 14:10:206496 n = get4byte(&pPage1->aData[36]);
drhdf35a082009-07-09 02:24:356497 testcase( n==mxPage-1 );
6498 if( n>=mxPage ){
drh1662b5a2009-06-04 19:06:096499 return SQLITE_CORRUPT_BKPT;
6500 }
drh3aac2dd2004-04-26 14:10:206501 if( n>0 ){
drh91025292004-05-03 19:49:326502 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:026503 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:026504 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
drhc6e956f2015-06-24 13:32:106505 u32 nSearch = 0; /* Count of the number of search attempts */
larrybrbc917382023-06-07 08:40:316506
drh82e647d2013-03-02 03:25:556507 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:026508 ** shows that the page 'nearby' is somewhere on the free-list, then
6509 ** the entire-list will be searched for that page.
6510 */
6511#ifndef SQLITE_OMIT_AUTOVACUUM
dan51f0b6d2013-02-22 20:16:346512 if( eMode==BTALLOC_EXACT ){
drh41af5b32020-07-31 02:07:166513 if( nearby<=mxPage ){
dan51f0b6d2013-02-22 20:16:346514 u8 eType;
6515 assert( nearby>0 );
6516 assert( pBt->autoVacuum );
6517 rc = ptrmapGet(pBt, nearby, &eType, 0);
6518 if( rc ) return rc;
6519 if( eType==PTRMAP_FREEPAGE ){
6520 searchList = 1;
6521 }
danielk1977cb1a7eb2004-11-05 12:27:026522 }
dan51f0b6d2013-02-22 20:16:346523 }else if( eMode==BTALLOC_LE ){
6524 searchList = 1;
danielk1977cb1a7eb2004-11-05 12:27:026525 }
6526#endif
6527
6528 /* Decrement the free-list count by 1. Set iTrunk to the index of the
6529 ** first free-list trunk page. iPrevTrunk is initially 1.
6530 */
danielk19773b8a05f2007-03-19 17:44:266531 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:446532 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:206533 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:026534
6535 /* The code within this loop is run only once if the 'searchList' variable
6536 ** is not true. Otherwise, it runs once for each trunk-page on the
drh82e647d2013-03-02 03:25:556537 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
6538 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
danielk1977cb1a7eb2004-11-05 12:27:026539 */
6540 do {
6541 pPrevTrunk = pTrunk;
6542 if( pPrevTrunk ){
drh113762a2014-11-19 16:36:256543 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
6544 ** is the page number of the next freelist trunk page in the list or
6545 ** zero if this is the last freelist trunk page. */
danielk1977cb1a7eb2004-11-05 12:27:026546 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:506547 }else{
drh113762a2014-11-19 16:36:256548 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
6549 ** stores the page number of the first page of the freelist, or zero if
6550 ** the freelist is empty. */
danielk1977cb1a7eb2004-11-05 12:27:026551 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:506552 }
drhdf35a082009-07-09 02:24:356553 testcase( iTrunk==mxPage );
drh9e7804d2015-06-24 12:24:036554 if( iTrunk>mxPage || nSearch++ > n ){
drhc62aab52017-06-11 18:26:156555 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
drh1662b5a2009-06-04 19:06:096556 }else{
drh7e8c6f12015-05-28 03:28:276557 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
drh1662b5a2009-06-04 19:06:096558 }
danielk1977cb1a7eb2004-11-05 12:27:026559 if( rc ){
drhd3627af2006-12-18 18:34:516560 pTrunk = 0;
6561 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:026562 }
drhb07028f2011-10-14 21:49:186563 assert( pTrunk!=0 );
6564 assert( pTrunk->aData!=0 );
drh113762a2014-11-19 16:36:256565 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
6566 ** is the number of leaf page pointers to follow. */
6567 k = get4byte(&pTrunk->aData[4]);
danielk1977cb1a7eb2004-11-05 12:27:026568 if( k==0 && !searchList ){
larrybrbc917382023-06-07 08:40:316569 /* The trunk has no leaves and the list is not being searched.
6570 ** So extract the trunk page itself and use it as the newly
danielk1977cb1a7eb2004-11-05 12:27:026571 ** allocated page */
6572 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:266573 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:516574 if( rc ){
6575 goto end_allocate_page;
6576 }
danielk1977cb1a7eb2004-11-05 12:27:026577 *pPgno = iTrunk;
6578 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
6579 *ppPage = pTrunk;
6580 pTrunk = 0;
drh2e89f1c2023-04-01 12:22:576581 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
drh042d6a12009-06-17 13:57:166582 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
danielk1977cb1a7eb2004-11-05 12:27:026583 /* Value of k is out of range. Database corruption */
drhcc97ca42017-06-07 22:32:596584 rc = SQLITE_CORRUPT_PGNO(iTrunk);
drhd3627af2006-12-18 18:34:516585 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:026586#ifndef SQLITE_OMIT_AUTOVACUUM
larrybrbc917382023-06-07 08:40:316587 }else if( searchList
6588 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
dan51f0b6d2013-02-22 20:16:346589 ){
danielk1977cb1a7eb2004-11-05 12:27:026590 /* The list is being searched and this trunk page is the page
6591 ** to allocate, regardless of whether it has leaves.
6592 */
dan51f0b6d2013-02-22 20:16:346593 *pPgno = iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:026594 *ppPage = pTrunk;
6595 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:266596 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:516597 if( rc ){
6598 goto end_allocate_page;
6599 }
danielk1977cb1a7eb2004-11-05 12:27:026600 if( k==0 ){
6601 if( !pPrevTrunk ){
6602 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
6603 }else{
danf48c3552010-08-23 15:41:246604 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
6605 if( rc!=SQLITE_OK ){
6606 goto end_allocate_page;
6607 }
danielk1977cb1a7eb2004-11-05 12:27:026608 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
6609 }
6610 }else{
larrybrbc917382023-06-07 08:40:316611 /* The trunk page is required by the caller but it contains
danielk1977cb1a7eb2004-11-05 12:27:026612 ** pointers to free-list leaves. The first leaf becomes a trunk
6613 ** page in this case.
6614 */
6615 MemPage *pNewTrunk;
6616 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
larrybrbc917382023-06-07 08:40:316617 if( iNewTrunk>mxPage ){
drhcc97ca42017-06-07 22:32:596618 rc = SQLITE_CORRUPT_PGNO(iTrunk);
drh1662b5a2009-06-04 19:06:096619 goto end_allocate_page;
6620 }
drhdf35a082009-07-09 02:24:356621 testcase( iNewTrunk==mxPage );
drh7e8c6f12015-05-28 03:28:276622 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:026623 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:516624 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:026625 }
danielk19773b8a05f2007-03-19 17:44:266626 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:026627 if( rc!=SQLITE_OK ){
6628 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:516629 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:026630 }
6631 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
6632 put4byte(&pNewTrunk->aData[4], k-1);
6633 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:516634 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:026635 if( !pPrevTrunk ){
drhc5053fb2008-11-27 02:22:106636 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
danielk1977cb1a7eb2004-11-05 12:27:026637 put4byte(&pPage1->aData[32], iNewTrunk);
6638 }else{
danielk19773b8a05f2007-03-19 17:44:266639 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:516640 if( rc ){
6641 goto end_allocate_page;
6642 }
danielk1977cb1a7eb2004-11-05 12:27:026643 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
6644 }
danielk1977cb1a7eb2004-11-05 12:27:026645 }
6646 pTrunk = 0;
drh2e89f1c2023-04-01 12:22:576647 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
danielk1977cb1a7eb2004-11-05 12:27:026648#endif
danielk1977e5765212009-06-17 11:13:286649 }else if( k>0 ){
danielk1977cb1a7eb2004-11-05 12:27:026650 /* Extract a leaf from the trunk */
drh042d6a12009-06-17 13:57:166651 u32 closest;
danielk1977cb1a7eb2004-11-05 12:27:026652 Pgno iPage;
6653 unsigned char *aData = pTrunk->aData;
6654 if( nearby>0 ){
drh042d6a12009-06-17 13:57:166655 u32 i;
danielk1977cb1a7eb2004-11-05 12:27:026656 closest = 0;
danf38b65a2013-02-22 20:57:476657 if( eMode==BTALLOC_LE ){
6658 for(i=0; i<k; i++){
6659 iPage = get4byte(&aData[8+i*4]);
dan87ade192013-02-23 17:49:166660 if( iPage<=nearby ){
danf38b65a2013-02-22 20:57:476661 closest = i;
6662 break;
6663 }
6664 }
6665 }else{
6666 int dist;
6667 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
6668 for(i=1; i<k; i++){
6669 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
6670 if( d2<dist ){
6671 closest = i;
6672 dist = d2;
6673 }
danielk1977cb1a7eb2004-11-05 12:27:026674 }
6675 }
6676 }else{
6677 closest = 0;
6678 }
6679
6680 iPage = get4byte(&aData[8+closest*4]);
drhdf35a082009-07-09 02:24:356681 testcase( iPage==mxPage );
drh07812192021-04-07 12:21:356682 if( iPage>mxPage || iPage<2 ){
drhcc97ca42017-06-07 22:32:596683 rc = SQLITE_CORRUPT_PGNO(iTrunk);
drh1662b5a2009-06-04 19:06:096684 goto end_allocate_page;
6685 }
drhdf35a082009-07-09 02:24:356686 testcase( iPage==mxPage );
larrybrbc917382023-06-07 08:40:316687 if( !searchList
6688 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
dan51f0b6d2013-02-22 20:16:346689 ){
danielk1977bea2a942009-01-20 17:06:276690 int noContent;
shane1f9e6aa2008-06-09 19:27:116691 *pPgno = iPage;
drh2e89f1c2023-04-01 12:22:576692 TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u"
6693 ": %u more free pages\n",
danielk1977cb1a7eb2004-11-05 12:27:026694 *pPgno, closest+1, k, pTrunk->pgno, n-1));
drh93b4fc72011-04-07 14:47:016695 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6696 if( rc ) goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:026697 if( closest<k-1 ){
6698 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
6699 }
6700 put4byte(&aData[4], k-1);
drh3f387402014-09-24 01:23:006701 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
drh7e8c6f12015-05-28 03:28:276702 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
danielk1977cb1a7eb2004-11-05 12:27:026703 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:266704 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:066705 if( rc!=SQLITE_OK ){
6706 releasePage(*ppPage);
drh1c8bade2015-05-29 18:42:116707 *ppPage = 0;
danielk1977aac0a382005-01-16 11:07:066708 }
danielk1977cb1a7eb2004-11-05 12:27:026709 }
6710 searchList = 0;
6711 }
drhee696e22004-08-30 16:52:176712 }
danielk1977cb1a7eb2004-11-05 12:27:026713 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:516714 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:026715 }while( searchList );
drh3b7511c2001-05-26 13:15:446716 }else{
danbc1a3c62013-02-23 16:40:466717 /* There are no pages on the freelist, so append a new page to the
6718 ** database image.
6719 **
6720 ** Normally, new pages allocated by this block can be requested from the
6721 ** pager layer with the 'no-content' flag set. This prevents the pager
6722 ** from trying to read the pages content from disk. However, if the
6723 ** current transaction has already run one or more incremental-vacuum
6724 ** steps, then the page we are about to allocate may contain content
6725 ** that is required in the event of a rollback. In this case, do
6726 ** not set the no-content flag. This causes the pager to load and journal
6727 ** the current page content before overwriting it.
6728 **
larrybrbc917382023-06-07 08:40:316729 ** Note that the pager will not actually attempt to load or journal
danbc1a3c62013-02-23 16:40:466730 ** content for any page that really does lie past the end of the database
6731 ** file on disk. So the effects of disabling the no-content optimization
6732 ** here are confined to those pages that lie between the end of the
6733 ** database image and the end of the database file.
6734 */
drh3f387402014-09-24 01:23:006735 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
danbc1a3c62013-02-23 16:40:466736
drhdd3cd972010-03-27 17:12:366737 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6738 if( rc ) return rc;
6739 pBt->nPage++;
6740 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
danielk1977bea2a942009-01-20 17:06:276741
danielk1977afcdd022004-10-31 16:25:426742#ifndef SQLITE_OMIT_AUTOVACUUM
drhdd3cd972010-03-27 17:12:366743 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
danielk1977afcdd022004-10-31 16:25:426744 /* If *pPgno refers to a pointer-map page, allocate two new pages
6745 ** at the end of the file instead of one. The first allocated page
6746 ** becomes a new pointer-map page, the second is used by the caller.
6747 */
danielk1977ac861692009-03-28 10:54:226748 MemPage *pPg = 0;
drh2e89f1c2023-04-01 12:22:576749 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
drhdd3cd972010-03-27 17:12:366750 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
drh7e8c6f12015-05-28 03:28:276751 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
danielk1977ac861692009-03-28 10:54:226752 if( rc==SQLITE_OK ){
6753 rc = sqlite3PagerWrite(pPg->pDbPage);
6754 releasePage(pPg);
6755 }
6756 if( rc ) return rc;
drhdd3cd972010-03-27 17:12:366757 pBt->nPage++;
6758 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
danielk1977afcdd022004-10-31 16:25:426759 }
6760#endif
drhdd3cd972010-03-27 17:12:366761 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
6762 *pPgno = pBt->nPage;
danielk1977afcdd022004-10-31 16:25:426763
danielk1977599fcba2004-11-08 07:13:136764 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh7e8c6f12015-05-28 03:28:276765 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
drh3b7511c2001-05-26 13:15:446766 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:266767 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:066768 if( rc!=SQLITE_OK ){
6769 releasePage(*ppPage);
drh7e8c6f12015-05-28 03:28:276770 *ppPage = 0;
danielk1977aac0a382005-01-16 11:07:066771 }
drh2e89f1c2023-04-01 12:22:576772 TRACE(("ALLOCATE: %u from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:446773 }
danielk1977599fcba2004-11-08 07:13:136774
danba14c692019-01-25 13:42:126775 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:516776
6777end_allocate_page:
6778 releasePage(pTrunk);
6779 releasePage(pPrevTrunk);
drh7e8c6f12015-05-28 03:28:276780 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
6781 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
drh3b7511c2001-05-26 13:15:446782 return rc;
6783}
6784
6785/*
larrybrbc917382023-06-07 08:40:316786** This function is used to add page iPage to the database file free-list.
danielk1977bea2a942009-01-20 17:06:276787** It is assumed that the page is not already a part of the free-list.
drh5e2f8b92001-05-28 00:41:156788**
danielk1977bea2a942009-01-20 17:06:276789** The value passed as the second argument to this function is optional.
larrybrbc917382023-06-07 08:40:316790** If the caller happens to have a pointer to the MemPage object
6791** corresponding to page iPage handy, it may pass it as the second value.
danielk1977bea2a942009-01-20 17:06:276792** Otherwise, it may pass NULL.
6793**
6794** If a pointer to a MemPage object is passed as the second argument,
6795** its reference count is not altered by this function.
drh3b7511c2001-05-26 13:15:446796*/
danielk1977bea2a942009-01-20 17:06:276797static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
6798 MemPage *pTrunk = 0; /* Free-list trunk page */
larrybrbc917382023-06-07 08:40:316799 Pgno iTrunk = 0; /* Page number of free-list trunk page */
danielk1977bea2a942009-01-20 17:06:276800 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
6801 MemPage *pPage; /* Page being freed. May be NULL. */
6802 int rc; /* Return Code */
drh25050f22019-04-09 01:26:316803 u32 nFree; /* Initial number of pages on free-list */
drh8b2f49b2001-06-08 00:21:526804
danielk1977bea2a942009-01-20 17:06:276805 assert( sqlite3_mutex_held(pBt->mutex) );
danfb0246b2015-05-26 12:18:176806 assert( CORRUPT_DB || iPage>1 );
danielk1977bea2a942009-01-20 17:06:276807 assert( !pMemPage || pMemPage->pgno==iPage );
6808
drh9a4e8862022-02-14 18:18:566809 if( iPage<2 || iPage>pBt->nPage ){
drh58b42ad2019-03-25 19:50:196810 return SQLITE_CORRUPT_BKPT;
6811 }
danielk1977bea2a942009-01-20 17:06:276812 if( pMemPage ){
6813 pPage = pMemPage;
6814 sqlite3PagerRef(pPage->pDbPage);
6815 }else{
6816 pPage = btreePageLookup(pBt, iPage);
6817 }
drh3aac2dd2004-04-26 14:10:206818
drha34b6762004-05-07 13:30:426819 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:266820 rc = sqlite3PagerWrite(pPage1->pDbPage);
danielk1977bea2a942009-01-20 17:06:276821 if( rc ) goto freepage_out;
6822 nFree = get4byte(&pPage1->aData[36]);
6823 put4byte(&pPage1->aData[36], nFree+1);
drh3aac2dd2004-04-26 14:10:206824
drhc9166342012-01-05 23:32:066825 if( pBt->btsFlags & BTS_SECURE_DELETE ){
drh5b47efa2010-02-12 18:18:396826 /* If the secure_delete option is enabled, then
6827 ** always fully overwrite deleted information with zeros.
6828 */
drhb00fc3b2013-08-21 23:42:326829 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
shaneh84f4b2f2010-02-26 01:46:546830 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
drh5b47efa2010-02-12 18:18:396831 ){
6832 goto freepage_out;
6833 }
6834 memset(pPage->aData, 0, pPage->pBt->pageSize);
danielk1977bea2a942009-01-20 17:06:276835 }
drhfcce93f2006-02-22 03:08:326836
danielk1977687566d2004-11-02 12:56:416837 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:026838 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:416839 */
drhe7d53842022-11-21 14:13:106840 if( ISAUTOVACUUM(pBt) ){
drh98add2e2009-07-20 17:11:496841 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
danielk1977bea2a942009-01-20 17:06:276842 if( rc ) goto freepage_out;
danielk1977687566d2004-11-02 12:56:416843 }
danielk1977687566d2004-11-02 12:56:416844
danielk1977bea2a942009-01-20 17:06:276845 /* Now manipulate the actual database free-list structure. There are two
6846 ** possibilities. If the free-list is currently empty, or if the first
6847 ** trunk page in the free-list is full, then this page will become a
6848 ** new free-list trunk page. Otherwise, it will become a leaf of the
6849 ** first trunk page in the current free-list. This block tests if it
6850 ** is possible to add the page as a new free-list leaf.
6851 */
6852 if( nFree!=0 ){
drhc046e3e2009-07-15 11:26:446853 u32 nLeaf; /* Initial number of leaf cells on trunk page */
danielk1977bea2a942009-01-20 17:06:276854
6855 iTrunk = get4byte(&pPage1->aData[32]);
drh10248222020-07-28 20:32:126856 if( iTrunk>btreePagecount(pBt) ){
6857 rc = SQLITE_CORRUPT_BKPT;
6858 goto freepage_out;
6859 }
drhb00fc3b2013-08-21 23:42:326860 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
danielk1977bea2a942009-01-20 17:06:276861 if( rc!=SQLITE_OK ){
6862 goto freepage_out;
6863 }
6864
6865 nLeaf = get4byte(&pTrunk->aData[4]);
drheeb844a2009-08-08 18:01:076866 assert( pBt->usableSize>32 );
6867 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
danielk1977bea2a942009-01-20 17:06:276868 rc = SQLITE_CORRUPT_BKPT;
6869 goto freepage_out;
6870 }
drheeb844a2009-08-08 18:01:076871 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
danielk1977bea2a942009-01-20 17:06:276872 /* In this case there is room on the trunk page to insert the page
6873 ** being freed as a new leaf.
drh45b1fac2008-07-04 17:52:426874 **
6875 ** Note that the trunk page is not really full until it contains
6876 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
6877 ** coded. But due to a coding error in versions of SQLite prior to
6878 ** 3.6.0, databases with freelist trunk pages holding more than
6879 ** usableSize/4 - 8 entries will be reported as corrupt. In order
6880 ** to maintain backwards compatibility with older versions of SQLite,
drhc046e3e2009-07-15 11:26:446881 ** we will continue to restrict the number of entries to usableSize/4 - 8
drh45b1fac2008-07-04 17:52:426882 ** for now. At some point in the future (once everyone has upgraded
6883 ** to 3.6.0 or later) we should consider fixing the conditional above
6884 ** to read "usableSize/4-2" instead of "usableSize/4-8".
drh113762a2014-11-19 16:36:256885 **
6886 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
6887 ** avoid using the last six entries in the freelist trunk page array in
6888 ** order that database files created by newer versions of SQLite can be
6889 ** read by older versions of SQLite.
drh45b1fac2008-07-04 17:52:426890 */
danielk19773b8a05f2007-03-19 17:44:266891 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhf5345442007-04-09 12:45:026892 if( rc==SQLITE_OK ){
danielk1977bea2a942009-01-20 17:06:276893 put4byte(&pTrunk->aData[4], nLeaf+1);
6894 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
drhc9166342012-01-05 23:32:066895 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
danielk1977bea2a942009-01-20 17:06:276896 sqlite3PagerDontWrite(pPage->pDbPage);
6897 }
danielk1977bea2a942009-01-20 17:06:276898 rc = btreeSetHasContent(pBt, iPage);
drhf5345442007-04-09 12:45:026899 }
drh2e89f1c2023-04-01 12:22:576900 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
danielk1977bea2a942009-01-20 17:06:276901 goto freepage_out;
drh3aac2dd2004-04-26 14:10:206902 }
drh3b7511c2001-05-26 13:15:446903 }
danielk1977bea2a942009-01-20 17:06:276904
6905 /* If control flows to this point, then it was not possible to add the
6906 ** the page being freed as a leaf page of the first trunk in the free-list.
larrybrbc917382023-06-07 08:40:316907 ** Possibly because the free-list is empty, or possibly because the
danielk1977bea2a942009-01-20 17:06:276908 ** first trunk in the free-list is full. Either way, the page being freed
6909 ** will become the new first trunk page in the free-list.
6910 */
drhb00fc3b2013-08-21 23:42:326911 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
drhc046e3e2009-07-15 11:26:446912 goto freepage_out;
6913 }
6914 rc = sqlite3PagerWrite(pPage->pDbPage);
6915 if( rc!=SQLITE_OK ){
danielk1977bea2a942009-01-20 17:06:276916 goto freepage_out;
6917 }
6918 put4byte(pPage->aData, iTrunk);
6919 put4byte(&pPage->aData[4], 0);
6920 put4byte(&pPage1->aData[32], iPage);
drh2e89f1c2023-04-01 12:22:576921 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
danielk1977bea2a942009-01-20 17:06:276922
6923freepage_out:
6924 if( pPage ){
6925 pPage->isInit = 0;
6926 }
6927 releasePage(pPage);
6928 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:446929 return rc;
6930}
drhc314dc72009-07-21 11:52:346931static void freePage(MemPage *pPage, int *pRC){
6932 if( (*pRC)==SQLITE_OK ){
6933 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6934 }
danielk1977bea2a942009-01-20 17:06:276935}
drh3b7511c2001-05-26 13:15:446936
6937/*
drh86c779f2021-05-15 13:08:446938** Free the overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:446939*/
drh86c779f2021-05-15 13:08:446940static SQLITE_NOINLINE int clearCellOverflow(
drh9bfdc252014-09-24 02:05:416941 MemPage *pPage, /* The page that contains the Cell */
6942 unsigned char *pCell, /* First byte of the Cell */
drh80159da2016-12-09 17:32:516943 CellInfo *pInfo /* Size information about the cell */
drh9bfdc252014-09-24 02:05:416944){
drh60172a52017-08-02 18:27:506945 BtShared *pBt;
drh3aac2dd2004-04-26 14:10:206946 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:566947 int rc;
drh94440812007-03-06 11:42:196948 int nOvfl;
shaneh1df2db72010-08-18 02:28:486949 u32 ovflPageSize;
drh3b7511c2001-05-26 13:15:446950
drh1fee73e2007-08-29 04:00:576951 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh86c779f2021-05-15 13:08:446952 assert( pInfo->nLocal!=pInfo->nPayload );
drh6fcf83a2018-05-05 01:23:286953 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
6954 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
6955 if( pCell + pInfo->nSize > pPage->aDataEnd ){
drhcc97ca42017-06-07 22:32:596956 /* Cell extends past end of page */
daneebf2f52017-11-18 17:30:086957 return SQLITE_CORRUPT_PAGE(pPage);
drhe42a9b42011-08-31 13:27:196958 }
drh80159da2016-12-09 17:32:516959 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
drh60172a52017-08-02 18:27:506960 pBt = pPage->pBt;
shane63207ab2009-02-04 01:49:306961 assert( pBt->usableSize > 4 );
drh94440812007-03-06 11:42:196962 ovflPageSize = pBt->usableSize - 4;
drh80159da2016-12-09 17:32:516963 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
larrybrbc917382023-06-07 08:40:316964 assert( nOvfl>0 ||
drh80159da2016-12-09 17:32:516965 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
dan0f8076d2015-05-25 18:47:266966 );
drh72365832007-03-06 15:53:446967 while( nOvfl-- ){
shane63207ab2009-02-04 01:49:306968 Pgno iNext = 0;
danielk1977bea2a942009-01-20 17:06:276969 MemPage *pOvfl = 0;
drhb1299152010-03-30 22:58:336970 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
larrybrbc917382023-06-07 08:40:316971 /* 0 is not a legal page number and page 1 cannot be an
6972 ** overflow page. Therefore if ovflPgno<2 or past the end of the
danielk1977e589a672009-04-11 16:06:156973 ** file the database must be corrupt. */
drh49285702005-09-17 15:20:266974 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:556975 }
danielk1977bea2a942009-01-20 17:06:276976 if( nOvfl ){
6977 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
6978 if( rc ) return rc;
6979 }
dan887d4b22010-02-25 12:09:166980
shaneh1da207e2010-03-09 14:41:126981 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
dan887d4b22010-02-25 12:09:166982 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
6983 ){
larrybrbc917382023-06-07 08:40:316984 /* There is no reason any cursor should have an outstanding reference
dan887d4b22010-02-25 12:09:166985 ** to an overflow page belonging to a cell that is being deleted/updated.
larrybrbc917382023-06-07 08:40:316986 ** So if there exists more than one reference to this page, then it
6987 ** must not really be an overflow page and the database must be corrupt.
6988 ** It is helpful to detect this before calling freePage2(), as
dan887d4b22010-02-25 12:09:166989 ** freePage2() may zero the page contents if secure-delete mode is
6990 ** enabled. If this 'overflow' page happens to be a page that the
6991 ** caller is iterating through or using in some other way, this
6992 ** can be problematic.
6993 */
6994 rc = SQLITE_CORRUPT_BKPT;
6995 }else{
6996 rc = freePage2(pBt, pOvfl, ovflPgno);
6997 }
6998
danielk1977bea2a942009-01-20 17:06:276999 if( pOvfl ){
7000 sqlite3PagerUnref(pOvfl->pDbPage);
7001 }
drh3b7511c2001-05-26 13:15:447002 if( rc ) return rc;
danielk1977bea2a942009-01-20 17:06:277003 ovflPgno = iNext;
drh3b7511c2001-05-26 13:15:447004 }
drh5e2f8b92001-05-28 00:41:157005 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:447006}
7007
drh86c779f2021-05-15 13:08:447008/* Call xParseCell to compute the size of a cell. If the cell contains
7009** overflow, then invoke cellClearOverflow to clear out that overflow.
larrybrbc917382023-06-07 08:40:317010** Store the result code (SQLITE_OK or some error code) in rc.
drh86c779f2021-05-15 13:08:447011**
7012** Implemented as macro to force inlining for performance.
7013*/
7014#define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \
7015 pPage->xParseCell(pPage, pCell, &sInfo); \
7016 if( sInfo.nLocal!=sInfo.nPayload ){ \
7017 rc = clearCellOverflow(pPage, pCell, &sInfo); \
7018 }else{ \
7019 rc = SQLITE_OK; \
7020 }
7021
7022
drh3b7511c2001-05-26 13:15:447023/*
drh91025292004-05-03 19:49:327024** Create the byte sequence used to represent a cell on page pPage
7025** and write that byte sequence into pCell[]. Overflow pages are
7026** allocated and filled in as necessary. The calling procedure
7027** is responsible for making sure sufficient space has been allocated
7028** for pCell[].
7029**
7030** Note that pCell does not necessary need to point to the pPage->aData
7031** area. pCell might point to some temporary storage. The cell will
7032** be constructed in this temporary area then copied into pPage->aData
7033** later.
drh3b7511c2001-05-26 13:15:447034*/
7035static int fillInCell(
drh3aac2dd2004-04-26 14:10:207036 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:197037 unsigned char *pCell, /* Complete text of the cell */
drh8eeb4462016-05-21 20:03:427038 const BtreePayload *pX, /* Payload with which to construct the cell */
drh4b70f112004-05-02 21:12:197039 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:447040){
drh3b7511c2001-05-26 13:15:447041 int nPayload;
drh8c6fa9b2004-05-26 00:01:537042 const u8 *pSrc;
drh5e27e1d2017-08-23 14:45:597043 int nSrc, n, rc, mn;
drh3aac2dd2004-04-26 14:10:207044 int spaceLeft;
drh5e27e1d2017-08-23 14:45:597045 MemPage *pToRelease;
drh3aac2dd2004-04-26 14:10:207046 unsigned char *pPrior;
7047 unsigned char *pPayload;
drh5e27e1d2017-08-23 14:45:597048 BtShared *pBt;
7049 Pgno pgnoOvfl;
drh4b70f112004-05-02 21:12:197050 int nHeader;
drh3b7511c2001-05-26 13:15:447051
drh1fee73e2007-08-29 04:00:577052 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:417053
drhc5053fb2008-11-27 02:22:107054 /* pPage is not necessarily writeable since pCell might be auxiliary
7055 ** buffer space that is separate from the pPage buffer area */
drh5e27e1d2017-08-23 14:45:597056 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
drhc5053fb2008-11-27 02:22:107057 || sqlite3PagerIswriteable(pPage->pDbPage) );
7058
drh91025292004-05-03 19:49:327059 /* Fill in the header. */
drh6200c882014-09-23 22:36:257060 nHeader = pPage->childPtrSize;
drhdfc2daa2016-05-21 23:25:297061 if( pPage->intKey ){
7062 nPayload = pX->nData + pX->nZero;
7063 pSrc = pX->pData;
7064 nSrc = pX->nData;
7065 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
drh6200c882014-09-23 22:36:257066 nHeader += putVarint32(&pCell[nHeader], nPayload);
drhdfc2daa2016-05-21 23:25:297067 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
drh6f11bef2004-05-13 01:12:567068 }else{
drh8eeb4462016-05-21 20:03:427069 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
7070 nSrc = nPayload = (int)pX->nKey;
7071 pSrc = pX->pKey;
drhdfc2daa2016-05-21 23:25:297072 nHeader += putVarint32(&pCell[nHeader], nPayload);
drh3aac2dd2004-04-26 14:10:207073 }
larrybrbc917382023-06-07 08:40:317074
drhdfc2daa2016-05-21 23:25:297075 /* Fill in the payload */
drh5e27e1d2017-08-23 14:45:597076 pPayload = &pCell[nHeader];
drh6200c882014-09-23 22:36:257077 if( nPayload<=pPage->maxLocal ){
drh5e27e1d2017-08-23 14:45:597078 /* This is the common case where everything fits on the btree page
7079 ** and no overflow pages are required. */
drh6200c882014-09-23 22:36:257080 n = nHeader + nPayload;
7081 testcase( n==3 );
7082 testcase( n==4 );
drh7c6433c2024-02-26 15:27:337083 if( n<4 ){
7084 n = 4;
7085 pPayload[nPayload] = 0;
7086 }
drh6200c882014-09-23 22:36:257087 *pnSize = n;
drh5e27e1d2017-08-23 14:45:597088 assert( nSrc<=nPayload );
7089 testcase( nSrc<nPayload );
7090 memcpy(pPayload, pSrc, nSrc);
7091 memset(pPayload+nSrc, 0, nPayload-nSrc);
7092 return SQLITE_OK;
drh6200c882014-09-23 22:36:257093 }
drh5e27e1d2017-08-23 14:45:597094
7095 /* If we reach this point, it means that some of the content will need
7096 ** to spill onto overflow pages.
7097 */
7098 mn = pPage->minLocal;
7099 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
7100 testcase( n==pPage->maxLocal );
7101 testcase( n==pPage->maxLocal+1 );
7102 if( n > pPage->maxLocal ) n = mn;
7103 spaceLeft = n;
7104 *pnSize = n + nHeader + 4;
7105 pPrior = &pCell[nHeader+n];
7106 pToRelease = 0;
7107 pgnoOvfl = 0;
7108 pBt = pPage->pBt;
drh3b7511c2001-05-26 13:15:447109
drh6200c882014-09-23 22:36:257110 /* At this point variables should be set as follows:
7111 **
7112 ** nPayload Total payload size in bytes
7113 ** pPayload Begin writing payload here
7114 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
7115 ** that means content must spill into overflow pages.
7116 ** *pnSize Size of the local cell (not counting overflow pages)
7117 ** pPrior Where to write the pgno of the first overflow page
7118 **
7119 ** Use a call to btreeParseCellPtr() to verify that the values above
7120 ** were computed correctly.
7121 */
drhd879e3e2017-02-13 13:35:557122#ifdef SQLITE_DEBUG
drh6200c882014-09-23 22:36:257123 {
7124 CellInfo info;
drh5fa60512015-06-19 17:19:347125 pPage->xParseCell(pPage, pCell, &info);
drhcc5f8a42016-02-06 22:32:067126 assert( nHeader==(int)(info.pPayload - pCell) );
drh8eeb4462016-05-21 20:03:427127 assert( info.nKey==pX->nKey );
drh6200c882014-09-23 22:36:257128 assert( *pnSize == info.nSize );
7129 assert( spaceLeft == info.nLocal );
drh6200c882014-09-23 22:36:257130 }
7131#endif
7132
7133 /* Write the payload into the local Cell and any extra into overflow pages */
drh5e27e1d2017-08-23 14:45:597134 while( 1 ){
7135 n = nPayload;
7136 if( n>spaceLeft ) n = spaceLeft;
7137
7138 /* If pToRelease is not zero than pPayload points into the data area
7139 ** of pToRelease. Make sure pToRelease is still writeable. */
7140 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
7141
7142 /* If pPayload is part of the data area of pPage, then make sure pPage
7143 ** is still writeable */
7144 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
7145 || sqlite3PagerIswriteable(pPage->pDbPage) );
7146
7147 if( nSrc>=n ){
7148 memcpy(pPayload, pSrc, n);
7149 }else if( nSrc>0 ){
7150 n = nSrc;
7151 memcpy(pPayload, pSrc, n);
7152 }else{
7153 memset(pPayload, 0, n);
7154 }
7155 nPayload -= n;
7156 if( nPayload<=0 ) break;
7157 pPayload += n;
7158 pSrc += n;
7159 nSrc -= n;
7160 spaceLeft -= n;
drh3b7511c2001-05-26 13:15:447161 if( spaceLeft==0 ){
drh5e27e1d2017-08-23 14:45:597162 MemPage *pOvfl = 0;
danielk1977afcdd022004-10-31 16:25:427163#ifndef SQLITE_OMIT_AUTOVACUUM
7164 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
danielk1977b39f70b2007-05-17 18:28:117165 if( pBt->autoVacuum ){
7166 do{
7167 pgnoOvfl++;
larrybrbc917382023-06-07 08:40:317168 } while(
7169 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
danielk1977b39f70b2007-05-17 18:28:117170 );
danielk1977b39f70b2007-05-17 18:28:117171 }
danielk1977afcdd022004-10-31 16:25:427172#endif
drhf49661a2008-12-10 16:45:507173 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:427174#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:077175 /* If the database supports auto-vacuum, and the second or subsequent
7176 ** overflow page is being allocated, add an entry to the pointer-map
larrybrbc917382023-06-07 08:40:317177 ** for that page now.
danielk19774ef24492007-05-23 09:52:417178 **
larrybrbc917382023-06-07 08:40:317179 ** If this is the first overflow page, then write a partial entry
danielk19774ef24492007-05-23 09:52:417180 ** to the pointer-map. If we write nothing to this pointer-map slot,
7181 ** then the optimistic overflow chain processing in clearCell()
mistachkin48864df2013-03-21 21:20:327182 ** may misinterpret the uninitialized values and delete the
danielk19774ef24492007-05-23 09:52:417183 ** wrong pages from the database.
danielk1977afcdd022004-10-31 16:25:427184 */
danielk19774ef24492007-05-23 09:52:417185 if( pBt->autoVacuum && rc==SQLITE_OK ){
7186 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
drh98add2e2009-07-20 17:11:497187 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
danielk197789a4be82007-05-23 13:34:327188 if( rc ){
7189 releasePage(pOvfl);
7190 }
danielk1977afcdd022004-10-31 16:25:427191 }
7192#endif
drh3b7511c2001-05-26 13:15:447193 if( rc ){
drh9b171272004-05-08 02:03:227194 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:447195 return rc;
7196 }
drhc5053fb2008-11-27 02:22:107197
7198 /* If pToRelease is not zero than pPrior points into the data area
7199 ** of pToRelease. Make sure pToRelease is still writeable. */
7200 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
7201
7202 /* If pPrior is part of the data area of pPage, then make sure pPage
7203 ** is still writeable */
7204 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
7205 || sqlite3PagerIswriteable(pPage->pDbPage) );
7206
drh3aac2dd2004-04-26 14:10:207207 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:227208 releasePage(pToRelease);
7209 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:207210 pPrior = pOvfl->aData;
7211 put4byte(pPrior, 0);
7212 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:117213 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:447214 }
drhdd793422001-06-28 01:54:487215 }
drh9b171272004-05-08 02:03:227216 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:447217 return SQLITE_OK;
7218}
7219
drh14acc042001-06-10 19:56:587220/*
7221** Remove the i-th cell from pPage. This routine effects pPage only.
7222** The cell content is not freed or deallocated. It is assumed that
7223** the cell content has been copied someplace else. This routine just
7224** removes the reference to the cell from pPage.
7225**
7226** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:587227*/
drh98add2e2009-07-20 17:11:497228static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
drh43b18e12010-08-17 19:40:087229 u32 pc; /* Offset to cell content of cell being deleted */
drh43605152004-05-29 21:46:497230 u8 *data; /* pPage->aData */
7231 u8 *ptr; /* Used to move bytes around within data[] */
shanedcc50b72008-11-13 18:29:507232 int rc; /* The return code */
drhc314dc72009-07-21 11:52:347233 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
drh43605152004-05-29 21:46:497234
drh98add2e2009-07-20 17:11:497235 if( *pRC ) return;
drh2dfe9662022-01-02 11:25:517236 assert( idx>=0 );
7237 assert( idx<pPage->nCell );
dan0f8076d2015-05-25 18:47:267238 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:267239 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:577240 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhb0ea9432019-02-09 21:06:407241 assert( pPage->nFree>=0 );
drhda200cc2004-05-09 11:51:387242 data = pPage->aData;
drh3def2352011-11-11 00:27:157243 ptr = &pPage->aCellIdx[2*idx];
mistachkinbeacaac2022-01-12 00:28:127244 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
shane0af3f892008-11-12 04:55:347245 pc = get2byte(ptr);
drhc314dc72009-07-21 11:52:347246 hdr = pPage->hdrOffset;
mistachkin2b5fbb22021-12-31 18:26:507247 testcase( pc==(u32)get2byte(&data[hdr+5]) );
drhc314dc72009-07-21 11:52:347248 testcase( pc+sz==pPage->pBt->usableSize );
drh5e398e42017-08-23 20:36:067249 if( pc+sz > pPage->pBt->usableSize ){
drh98add2e2009-07-20 17:11:497250 *pRC = SQLITE_CORRUPT_BKPT;
7251 return;
shane0af3f892008-11-12 04:55:347252 }
shanedcc50b72008-11-13 18:29:507253 rc = freeSpace(pPage, pc, sz);
drh98add2e2009-07-20 17:11:497254 if( rc ){
7255 *pRC = rc;
7256 return;
shanedcc50b72008-11-13 18:29:507257 }
drh14acc042001-06-10 19:56:587258 pPage->nCell--;
drhfdab0262014-11-20 15:30:507259 if( pPage->nCell==0 ){
7260 memset(&data[hdr+1], 0, 4);
7261 data[hdr+7] = 0;
7262 put2byte(&data[hdr+5], pPage->pBt->usableSize);
7263 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
7264 - pPage->childPtrSize - 8;
7265 }else{
7266 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
7267 put2byte(&data[hdr+3], pPage->nCell);
7268 pPage->nFree += 2;
7269 }
drh14acc042001-06-10 19:56:587270}
7271
7272/*
7273** Insert a new cell on pPage at cell index "i". pCell points to the
7274** content of the cell.
7275**
7276** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:497277** will not fit, then make a copy of the cell content into pTemp if
7278** pTemp is not null. Regardless of pTemp, allocate a new entry
drh2cbd78b2012-02-02 19:37:187279** in pPage->apOvfl[] and make it point to the cell content (either
larrybrbc917382023-06-07 08:40:317280** in pTemp or the original pCell) and also record its index.
7281** Allocating a new entry in pPage->aCell[] implies that
drh43605152004-05-29 21:46:497282** pPage->nOverflow is incremented.
drh34ceb7e2023-04-07 14:33:337283**
7284** The insertCellFast() routine below works exactly the same as
7285** insertCell() except that it lacks the pTemp and iChild parameters
7286** which are assumed zero. Other than that, the two routines are the
7287** same.
7288**
7289** Fixes or enhancements to this routine should be reflected in
7290** insertCellFast()!
drh14acc042001-06-10 19:56:587291*/
drhb53d8fa2022-11-21 15:55:577292static int insertCell(
drh24cd67e2004-05-10 16:18:477293 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:497294 int i, /* New cell becomes the i-th cell of the page */
7295 u8 *pCell, /* Content of the new cell */
7296 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:447297 u8 *pTemp, /* Temp storage space for pCell, if needed */
drhb53d8fa2022-11-21 15:55:577298 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
drh24cd67e2004-05-10 16:18:477299){
drh383d30f2010-02-26 13:07:377300 int idx = 0; /* Where to write new cell content in data[] */
drh43605152004-05-29 21:46:497301 int j; /* Loop counter */
drh43605152004-05-29 21:46:497302 u8 *data; /* The content of the whole page */
drh2c8fb922015-06-25 19:53:487303 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
danielk19774dbaa892009-06-16 16:50:227304
drh43605152004-05-29 21:46:497305 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
danf216e322014-08-14 19:53:377306 assert( MX_CELL(pPage->pBt)<=10921 );
7307 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
drh2cbd78b2012-02-02 19:37:187308 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
7309 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
drh1fee73e2007-08-29 04:00:577310 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh996f5cc2019-07-17 16:18:017311 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
drhb0ea9432019-02-09 21:06:407312 assert( pPage->nFree>=0 );
drh706c33d2023-04-07 15:07:587313 assert( iChild>0 );
drh43605152004-05-29 21:46:497314 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:477315 if( pTemp ){
drhd6176c42014-10-11 17:22:557316 memcpy(pTemp, pCell, sz);
drh43605152004-05-29 21:46:497317 pCell = pTemp;
drh24cd67e2004-05-10 16:18:477318 }
drh706c33d2023-04-07 15:07:587319 put4byte(pCell, iChild);
drh43605152004-05-29 21:46:497320 j = pPage->nOverflow++;
drha2ee5892016-12-09 16:02:007321 /* Comparison against ArraySize-1 since we hold back one extra slot
7322 ** as a contingency. In other words, never need more than 3 overflow
7323 ** slots but 4 are allocated, just to be safe. */
7324 assert( j < ArraySize(pPage->apOvfl)-1 );
drh2cbd78b2012-02-02 19:37:187325 pPage->apOvfl[j] = pCell;
7326 pPage->aiOvfl[j] = (u16)i;
drhfe647dc2015-06-23 18:24:257327
7328 /* When multiple overflows occur, they are always sequential and in
7329 ** sorted order. This invariants arise because multiple overflows can
7330 ** only occur when inserting divider cells into the parent page during
7331 ** balancing, and the dividers are adjacent and sorted.
7332 */
7333 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
7334 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
drh14acc042001-06-10 19:56:587335 }else{
danielk19776e465eb2007-08-21 13:11:007336 int rc = sqlite3PagerWrite(pPage->pDbPage);
drh3da5e2a2023-04-07 16:30:337337 if( NEVER(rc!=SQLITE_OK) ){
drhb53d8fa2022-11-21 15:55:577338 return rc;
danielk19776e465eb2007-08-21 13:11:007339 }
7340 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:497341 data = pPage->aData;
drh2c8fb922015-06-25 19:53:487342 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
drh0a45c272009-07-08 01:49:117343 rc = allocateSpace(pPage, sz, &idx);
drhb53d8fa2022-11-21 15:55:577344 if( rc ){ return rc; }
drhcd8fb7c2015-06-02 14:02:187345 /* The allocateSpace() routine guarantees the following properties
7346 ** if it returns successfully */
drh2c8fb922015-06-25 19:53:487347 assert( idx >= 0 );
7348 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
drhfcd71b62011-04-05 22:08:247349 assert( idx+sz <= (int)pPage->pBt->usableSize );
drh0a45c272009-07-08 01:49:117350 pPage->nFree -= (u16)(2 + sz);
drh706c33d2023-04-07 15:07:587351 /* In a corrupt database where an entry in the cell index section of
7352 ** a btree page has a value of 3 or less, the pCell value might point
7353 ** as many as 4 bytes in front of the start of the aData buffer for
7354 ** the source page. Make sure this does not cause problems by not
7355 ** reading the first 4 bytes */
7356 memcpy(&data[idx+4], pCell+4, sz-4);
7357 put4byte(&data[idx], iChild);
drh2c8fb922015-06-25 19:53:487358 pIns = pPage->aCellIdx + i*2;
7359 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
7360 put2byte(pIns, idx);
7361 pPage->nCell++;
7362 /* increment the cell count */
7363 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
drh56785a02019-02-16 22:45:557364 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
danielk1977a19df672004-11-03 11:37:077365#ifndef SQLITE_OMIT_AUTOVACUUM
7366 if( pPage->pBt->autoVacuum ){
drh5b046da2022-11-28 20:08:157367 int rc2 = SQLITE_OK;
danielk1977a19df672004-11-03 11:37:077368 /* The cell may contain a pointer to an overflow page. If so, write
7369 ** the entry for the overflow page into the pointer map.
7370 */
drh5b046da2022-11-28 20:08:157371 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
drhd3fc2e62022-11-28 21:17:307372 if( rc2 ) return rc2;
danielk1977a19df672004-11-03 11:37:077373 }
7374#endif
drh14acc042001-06-10 19:56:587375 }
drhb53d8fa2022-11-21 15:55:577376 return SQLITE_OK;
drh14acc042001-06-10 19:56:587377}
7378
7379/*
drh34ceb7e2023-04-07 14:33:337380** This variant of insertCell() assumes that the pTemp and iChild
7381** parameters are both zero. Use this variant in sqlite3BtreeInsert()
7382** for performance improvement, and also so that this variant is only
7383** called from that one place, and is thus inlined, and thus runs must
7384** faster.
7385**
7386** Fixes or enhancements to this routine should be reflected into
7387** the insertCell() routine.
7388*/
7389static int insertCellFast(
7390 MemPage *pPage, /* Page into which we are copying */
7391 int i, /* New cell becomes the i-th cell of the page */
7392 u8 *pCell, /* Content of the new cell */
7393 int sz /* Bytes of content in pCell */
7394){
7395 int idx = 0; /* Where to write new cell content in data[] */
7396 int j; /* Loop counter */
7397 u8 *data; /* The content of the whole page */
7398 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
7399
7400 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
7401 assert( MX_CELL(pPage->pBt)<=10921 );
7402 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
7403 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
7404 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
7405 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7406 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
7407 assert( pPage->nFree>=0 );
drh706c33d2023-04-07 15:07:587408 assert( pPage->nOverflow==0 );
7409 if( sz+2>pPage->nFree ){
drh34ceb7e2023-04-07 14:33:337410 j = pPage->nOverflow++;
7411 /* Comparison against ArraySize-1 since we hold back one extra slot
7412 ** as a contingency. In other words, never need more than 3 overflow
7413 ** slots but 4 are allocated, just to be safe. */
7414 assert( j < ArraySize(pPage->apOvfl)-1 );
7415 pPage->apOvfl[j] = pCell;
7416 pPage->aiOvfl[j] = (u16)i;
7417
7418 /* When multiple overflows occur, they are always sequential and in
7419 ** sorted order. This invariants arise because multiple overflows can
7420 ** only occur when inserting divider cells into the parent page during
7421 ** balancing, and the dividers are adjacent and sorted.
7422 */
7423 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
7424 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
7425 }else{
7426 int rc = sqlite3PagerWrite(pPage->pDbPage);
7427 if( rc!=SQLITE_OK ){
7428 return rc;
7429 }
7430 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
7431 data = pPage->aData;
7432 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
7433 rc = allocateSpace(pPage, sz, &idx);
7434 if( rc ){ return rc; }
7435 /* The allocateSpace() routine guarantees the following properties
7436 ** if it returns successfully */
7437 assert( idx >= 0 );
7438 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
7439 assert( idx+sz <= (int)pPage->pBt->usableSize );
7440 pPage->nFree -= (u16)(2 + sz);
7441 memcpy(&data[idx], pCell, sz);
7442 pIns = pPage->aCellIdx + i*2;
7443 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
7444 put2byte(pIns, idx);
7445 pPage->nCell++;
7446 /* increment the cell count */
7447 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
7448 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
7449#ifndef SQLITE_OMIT_AUTOVACUUM
7450 if( pPage->pBt->autoVacuum ){
7451 int rc2 = SQLITE_OK;
7452 /* The cell may contain a pointer to an overflow page. If so, write
7453 ** the entry for the overflow page into the pointer map.
7454 */
7455 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
7456 if( rc2 ) return rc2;
7457 }
7458#endif
7459 }
7460 return SQLITE_OK;
7461}
7462
7463/*
drhe3dadac2019-01-23 19:25:597464** The following parameters determine how many adjacent pages get involved
7465** in a balancing operation. NN is the number of neighbors on either side
7466** of the page that participate in the balancing operation. NB is the
7467** total number of pages that participate, including the target page and
7468** NN neighbors on either side.
7469**
7470** The minimum value of NN is 1 (of course). Increasing NN above 1
7471** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
7472** in exchange for a larger degradation in INSERT and UPDATE performance.
7473** The value of NN appears to give the best results overall.
7474**
7475** (Later:) The description above makes it seem as if these values are
7476** tunable - as if you could change them and recompile and it would all work.
7477** But that is unlikely. NB has been 3 since the inception of SQLite and
7478** we have never tested any other value.
7479*/
7480#define NN 1 /* Number of neighbors on either side of pPage */
7481#define NB 3 /* (NN*2+1): Total pages involved in the balance */
7482
7483/*
drh1ffd2472015-06-23 02:37:307484** A CellArray object contains a cache of pointers and sizes for a
drhc0d269e2016-08-03 14:51:167485** consecutive sequence of cells that might be held on multiple pages.
drhe3dadac2019-01-23 19:25:597486**
7487** The cells in this array are the divider cell or cells from the pParent
7488** page plus up to three child pages. There are a total of nCell cells.
7489**
7490** pRef is a pointer to one of the pages that contributes cells. This is
7491** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
7492** which should be common to all pages that contribute cells to this array.
7493**
7494** apCell[] and szCell[] hold, respectively, pointers to the start of each
7495** cell and the size of each cell. Some of the apCell[] pointers might refer
7496** to overflow cells. In other words, some apCel[] pointers might not point
7497** to content area of the pages.
7498**
7499** A szCell[] of zero means the size of that cell has not yet been computed.
7500**
7501** The cells come from as many as four different pages:
7502**
7503** -----------
7504** | Parent |
7505** -----------
7506** / | \
7507** / | \
7508** --------- --------- ---------
7509** |Child-1| |Child-2| |Child-3|
7510** --------- --------- ---------
7511**
drh26b7ec82019-02-01 14:50:437512** The order of cells is in the array is for an index btree is:
drhe3dadac2019-01-23 19:25:597513**
7514** 1. All cells from Child-1 in order
7515** 2. The first divider cell from Parent
7516** 3. All cells from Child-2 in order
7517** 4. The second divider cell from Parent
7518** 5. All cells from Child-3 in order
7519**
drh26b7ec82019-02-01 14:50:437520** For a table-btree (with rowids) the items 2 and 4 are empty because
7521** content exists only in leaves and there are no divider cells.
7522**
7523** For an index btree, the apEnd[] array holds pointer to the end of page
7524** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
7525** respectively. The ixNx[] array holds the number of cells contained in
7526** each of these 5 stages, and all stages to the left. Hence:
7527**
drhe3dadac2019-01-23 19:25:597528** ixNx[0] = Number of cells in Child-1.
7529** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
7530** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
7531** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
7532** ixNx[4] = Total number of cells.
drh26b7ec82019-02-01 14:50:437533**
7534** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
7535** are used and they point to the leaf pages only, and the ixNx value are:
7536**
7537** ixNx[0] = Number of cells in Child-1.
drh9c7e44c2019-02-14 15:27:127538** ixNx[1] = Number of cells in Child-1 and Child-2.
7539** ixNx[2] = Total number of cells.
7540**
7541** Sometimes when deleting, a child page can have zero cells. In those
7542** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
7543** entries, shift down. The end result is that each ixNx[] entry should
7544** be larger than the previous
drhfa1a98a2004-05-14 19:08:177545*/
drh1ffd2472015-06-23 02:37:307546typedef struct CellArray CellArray;
7547struct CellArray {
7548 int nCell; /* Number of cells in apCell[] */
7549 MemPage *pRef; /* Reference page */
7550 u8 **apCell; /* All cells begin balanced */
7551 u16 *szCell; /* Local size of all cells in apCell[] */
drhe3dadac2019-01-23 19:25:597552 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */
7553 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
drh1ffd2472015-06-23 02:37:307554};
drhfa1a98a2004-05-14 19:08:177555
drh1ffd2472015-06-23 02:37:307556/*
7557** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
7558** computed.
7559*/
7560static void populateCellCache(CellArray *p, int idx, int N){
drh47de1f92022-11-19 18:17:407561 MemPage *pRef = p->pRef;
7562 u16 *szCell = p->szCell;
drh1ffd2472015-06-23 02:37:307563 assert( idx>=0 && idx+N<=p->nCell );
7564 while( N>0 ){
7565 assert( p->apCell[idx]!=0 );
drh47de1f92022-11-19 18:17:407566 if( szCell[idx]==0 ){
7567 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
drh1ffd2472015-06-23 02:37:307568 }else{
7569 assert( CORRUPT_DB ||
drh47de1f92022-11-19 18:17:407570 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
drh1ffd2472015-06-23 02:37:307571 }
7572 idx++;
7573 N--;
drhfa1a98a2004-05-14 19:08:177574 }
drh1ffd2472015-06-23 02:37:307575}
7576
7577/*
7578** Return the size of the Nth element of the cell array
7579*/
7580static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
7581 assert( N>=0 && N<p->nCell );
7582 assert( p->szCell[N]==0 );
7583 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
7584 return p->szCell[N];
7585}
7586static u16 cachedCellSize(CellArray *p, int N){
7587 assert( N>=0 && N<p->nCell );
7588 if( p->szCell[N] ) return p->szCell[N];
7589 return computeCellSize(p, N);
7590}
7591
7592/*
larrybrbc917382023-06-07 08:40:317593** Array apCell[] contains pointers to nCell b-tree page cells. The
dan8e9ba0c2014-10-14 17:27:047594** szCell[] array contains the size in bytes of each cell. This function
7595** replaces the current contents of page pPg with the contents of the cell
7596** array.
7597**
7598** Some of the cells in apCell[] may currently be stored in pPg. This
larrybrbc917382023-06-07 08:40:317599** function works around problems caused by this by making a copy of any
dan8e9ba0c2014-10-14 17:27:047600** such cells before overwriting the page data.
7601**
larrybrbc917382023-06-07 08:40:317602** The MemPage.nFree field is invalidated by this function. It is the
dan8e9ba0c2014-10-14 17:27:047603** responsibility of the caller to set it correctly.
drhfa1a98a2004-05-14 19:08:177604*/
drh658873b2015-06-22 20:02:047605static int rebuildPage(
drhe3dadac2019-01-23 19:25:597606 CellArray *pCArray, /* Content to be added to page pPg */
7607 int iFirst, /* First cell in pCArray to use */
dan33ea4862014-10-09 19:35:377608 int nCell, /* Final number of cells on page */
drhe3dadac2019-01-23 19:25:597609 MemPage *pPg /* The page to be reconstructed */
dan33ea4862014-10-09 19:35:377610){
7611 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
7612 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
7613 const int usableSize = pPg->pBt->usableSize;
7614 u8 * const pEnd = &aData[usableSize];
drhe3dadac2019-01-23 19:25:597615 int i = iFirst; /* Which cell to copy from pCArray*/
drha0466432019-01-29 16:41:137616 u32 j; /* Start of cell content area */
drhe3dadac2019-01-23 19:25:597617 int iEnd = i+nCell; /* Loop terminator */
dan33ea4862014-10-09 19:35:377618 u8 *pCellptr = pPg->aCellIdx;
7619 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
7620 u8 *pData;
drhe3dadac2019-01-23 19:25:597621 int k; /* Current slot in pCArray->apEnd[] */
7622 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
dan33ea4862014-10-09 19:35:377623
drh5d9446d2023-10-05 11:06:107624 assert( nCell>0 );
drhe3dadac2019-01-23 19:25:597625 assert( i<iEnd );
7626 j = get2byte(&aData[hdr+5]);
drh38119052023-09-17 16:36:227627 if( j>(u32)usableSize ){ j = 0; }
drhe3dadac2019-01-23 19:25:597628 memcpy(&pTmp[j], &aData[j], usableSize - j);
7629
drh09d8e692024-05-27 15:13:497630 assert( pCArray->ixNx[NB*2-1]>i );
7631 for(k=0; pCArray->ixNx[k]<=i; k++){}
drhe3dadac2019-01-23 19:25:597632 pSrcEnd = pCArray->apEnd[k];
dan33ea4862014-10-09 19:35:377633
dan8e9ba0c2014-10-14 17:27:047634 pData = pEnd;
drhe3dadac2019-01-23 19:25:597635 while( 1/*exit by break*/ ){
7636 u8 *pCell = pCArray->apCell[i];
7637 u16 sz = pCArray->szCell[i];
7638 assert( sz>0 );
drh8cae5a42021-04-20 20:48:157639 if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){
drhb2b61bb2020-01-04 14:50:067640 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
dan33ea4862014-10-09 19:35:377641 pCell = &pTmp[pCell - aData];
drhe3dadac2019-01-23 19:25:597642 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
7643 && (uptr)(pCell)<(uptr)pSrcEnd
7644 ){
7645 return SQLITE_CORRUPT_BKPT;
dan33ea4862014-10-09 19:35:377646 }
drhe3dadac2019-01-23 19:25:597647
7648 pData -= sz;
dan33ea4862014-10-09 19:35:377649 put2byte(pCellptr, (pData - aData));
7650 pCellptr += 2;
drhe5cf3e92020-01-04 12:34:447651 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
drheca3c672021-04-22 20:01:027652 memmove(pData, pCell, sz);
drhe5cf3e92020-01-04 12:34:447653 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
drhe3dadac2019-01-23 19:25:597654 i++;
7655 if( i>=iEnd ) break;
7656 if( pCArray->ixNx[k]<=i ){
7657 k++;
7658 pSrcEnd = pCArray->apEnd[k];
7659 }
dan33ea4862014-10-09 19:35:377660 }
7661
dand7b545b2014-10-13 18:03:277662 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
drhc071c472025-02-22 16:44:147663 assert( nCell < 10922 );
7664 pPg->nCell = (u16)nCell;
dan33ea4862014-10-09 19:35:377665 pPg->nOverflow = 0;
7666
7667 put2byte(&aData[hdr+1], 0);
7668 put2byte(&aData[hdr+3], pPg->nCell);
7669 put2byte(&aData[hdr+5], pData - aData);
7670 aData[hdr+7] = 0x00;
drh658873b2015-06-22 20:02:047671 return SQLITE_OK;
dan33ea4862014-10-09 19:35:377672}
7673
dan8e9ba0c2014-10-14 17:27:047674/*
drhe3dadac2019-01-23 19:25:597675** The pCArray objects contains pointers to b-tree cells and the cell sizes.
7676** This function attempts to add the cells stored in the array to page pPg.
7677** If it cannot (because the page needs to be defragmented before the cells
7678** will fit), non-zero is returned. Otherwise, if the cells are added
7679** successfully, zero is returned.
dan8e9ba0c2014-10-14 17:27:047680**
7681** Argument pCellptr points to the first entry in the cell-pointer array
7682** (part of page pPg) to populate. After cell apCell[0] is written to the
7683** page body, a 16-bit offset is written to pCellptr. And so on, for each
7684** cell in the array. It is the responsibility of the caller to ensure
7685** that it is safe to overwrite this part of the cell-pointer array.
7686**
larrybrbc917382023-06-07 08:40:317687** When this function is called, *ppData points to the start of the
dan8e9ba0c2014-10-14 17:27:047688** content area on page pPg. If the size of the content area is extended,
7689** *ppData is updated to point to the new start of the content area
7690** before returning.
7691**
7692** Finally, argument pBegin points to the byte immediately following the
7693** end of the space required by this page for the cell-pointer area (for
7694** all cells - not just those inserted by the current call). If the content
larrybrbc917382023-06-07 08:40:317695** area must be extended to before this point in order to accommodate all
dan8e9ba0c2014-10-14 17:27:047696** cells in apCell[], then the cells do not fit and non-zero is returned.
7697*/
dand7b545b2014-10-13 18:03:277698static int pageInsertArray(
dan8e9ba0c2014-10-14 17:27:047699 MemPage *pPg, /* Page to add cells to */
7700 u8 *pBegin, /* End of cell-pointer array */
drhe3dadac2019-01-23 19:25:597701 u8 **ppData, /* IN/OUT: Page content-area pointer */
dan8e9ba0c2014-10-14 17:27:047702 u8 *pCellptr, /* Pointer to cell-pointer area */
drhf7838932015-06-23 15:36:347703 int iFirst, /* Index of first cell to add */
dan8e9ba0c2014-10-14 17:27:047704 int nCell, /* Number of cells to add to pPg */
drhf7838932015-06-23 15:36:347705 CellArray *pCArray /* Array of cells */
dand7b545b2014-10-13 18:03:277706){
drhe3dadac2019-01-23 19:25:597707 int i = iFirst; /* Loop counter - cell index to insert */
7708 u8 *aData = pPg->aData; /* Complete page */
7709 u8 *pData = *ppData; /* Content area. A subset of aData[] */
7710 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
7711 int k; /* Current slot in pCArray->apEnd[] */
7712 u8 *pEnd; /* Maximum extent of cell data */
dan23eba452014-10-24 18:43:577713 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
drhe3dadac2019-01-23 19:25:597714 if( iEnd<=iFirst ) return 0;
drh09d8e692024-05-27 15:13:497715 assert( pCArray->ixNx[NB*2-1]>i );
7716 for(k=0; pCArray->ixNx[k]<=i ; k++){}
drhe3dadac2019-01-23 19:25:597717 pEnd = pCArray->apEnd[k];
7718 while( 1 /*Exit by break*/ ){
drhf7838932015-06-23 15:36:347719 int sz, rc;
dand7b545b2014-10-13 18:03:277720 u8 *pSlot;
dan666a42f2019-08-24 21:02:477721 assert( pCArray->szCell[i]!=0 );
7722 sz = pCArray->szCell[i];
drhb7580e82015-06-25 18:36:137723 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
drhcca66982016-04-05 13:19:197724 if( (pData - pBegin)<sz ) return 1;
dand7b545b2014-10-13 18:03:277725 pData -= sz;
dand7b545b2014-10-13 18:03:277726 pSlot = pData;
7727 }
drh48310f82015-10-10 16:41:287728 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
7729 ** database. But they might for a corrupt database. Hence use memmove()
7730 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
7731 assert( (pSlot+sz)<=pCArray->apCell[i]
7732 || pSlot>=(pCArray->apCell[i]+sz)
7733 || CORRUPT_DB );
drhe3dadac2019-01-23 19:25:597734 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
7735 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
7736 ){
7737 assert( CORRUPT_DB );
7738 (void)SQLITE_CORRUPT_BKPT;
7739 return 1;
7740 }
drh48310f82015-10-10 16:41:287741 memmove(pSlot, pCArray->apCell[i], sz);
dand7b545b2014-10-13 18:03:277742 put2byte(pCellptr, (pSlot - aData));
7743 pCellptr += 2;
drhe3dadac2019-01-23 19:25:597744 i++;
7745 if( i>=iEnd ) break;
7746 if( pCArray->ixNx[k]<=i ){
7747 k++;
7748 pEnd = pCArray->apEnd[k];
7749 }
dand7b545b2014-10-13 18:03:277750 }
7751 *ppData = pData;
7752 return 0;
7753}
7754
dan8e9ba0c2014-10-14 17:27:047755/*
drhe3dadac2019-01-23 19:25:597756** The pCArray object contains pointers to b-tree cells and their sizes.
7757**
7758** This function adds the space associated with each cell in the array
7759** that is currently stored within the body of pPg to the pPg free-list.
7760** The cell-pointers and other fields of the page are not updated.
dan8e9ba0c2014-10-14 17:27:047761**
7762** This function returns the total number of cells added to the free-list.
7763*/
dand7b545b2014-10-13 18:03:277764static int pageFreeArray(
7765 MemPage *pPg, /* Page to edit */
drhf7838932015-06-23 15:36:347766 int iFirst, /* First cell to delete */
dand7b545b2014-10-13 18:03:277767 int nCell, /* Cells to delete */
drhf7838932015-06-23 15:36:347768 CellArray *pCArray /* Array of cells */
dand7b545b2014-10-13 18:03:277769){
7770 u8 * const aData = pPg->aData;
7771 u8 * const pEnd = &aData[pPg->pBt->usableSize];
dan89ca0b32014-10-25 20:36:287772 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
dand7b545b2014-10-13 18:03:277773 int nRet = 0;
drh50dc8d92023-04-06 20:14:107774 int i, j;
drhf7838932015-06-23 15:36:347775 int iEnd = iFirst + nCell;
drh50dc8d92023-04-06 20:14:107776 int nFree = 0;
7777 int aOfst[10];
7778 int aAfter[10];
dand7b545b2014-10-13 18:03:277779
drhf7838932015-06-23 15:36:347780 for(i=iFirst; i<iEnd; i++){
7781 u8 *pCell = pCArray->apCell[i];
drh8b0ba7b2015-12-16 13:07:357782 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
drhf7838932015-06-23 15:36:347783 int sz;
drh50dc8d92023-04-06 20:14:107784 int iAfter;
7785 int iOfst;
drhf7838932015-06-23 15:36:347786 /* No need to use cachedCellSize() here. The sizes of all cells that
7787 ** are to be freed have already been computing while deciding which
7788 ** cells need freeing */
7789 sz = pCArray->szCell[i]; assert( sz>0 );
drh50dc8d92023-04-06 20:14:107790 iOfst = (u16)(pCell - aData);
7791 iAfter = iOfst+sz;
7792 for(j=0; j<nFree; j++){
7793 if( aOfst[j]==iAfter ){
7794 aOfst[j] = iOfst;
7795 break;
7796 }else if( aAfter[j]==iOfst ){
7797 aAfter[j] = iAfter;
7798 break;
drhfefa0942014-11-05 21:21:087799 }
drh50dc8d92023-04-06 20:14:107800 }
7801 if( j>=nFree ){
drhf9b58eb2023-05-01 20:42:157802 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
drh50dc8d92023-04-06 20:14:107803 for(j=0; j<nFree; j++){
7804 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
7805 }
7806 nFree = 0;
drhc3c23f32021-05-06 11:02:557807 }
drh50dc8d92023-04-06 20:14:107808 aOfst[nFree] = iOfst;
7809 aAfter[nFree] = iAfter;
drh65aae442023-04-07 18:27:327810 if( &aData[iAfter]>pEnd ) return 0;
drh50dc8d92023-04-06 20:14:107811 nFree++;
dand7b545b2014-10-13 18:03:277812 }
7813 nRet++;
7814 }
7815 }
drh50dc8d92023-04-06 20:14:107816 for(j=0; j<nFree; j++){
7817 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
drhfefa0942014-11-05 21:21:087818 }
dand7b545b2014-10-13 18:03:277819 return nRet;
7820}
7821
dand7b545b2014-10-13 18:03:277822/*
drha0466432019-01-29 16:41:137823** pCArray contains pointers to and sizes of all cells in the page being
drhe3dadac2019-01-23 19:25:597824** balanced. The current page, pPg, has pPg->nCell cells starting with
7825** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
drh5ab63772014-11-27 03:46:047826** starting at apCell[iNew].
7827**
7828** This routine makes the necessary adjustments to pPg so that it contains
7829** the correct cells after being balanced.
7830**
dand7b545b2014-10-13 18:03:277831** The pPg->nFree field is invalid when this function returns. It is the
7832** responsibility of the caller to set it correctly.
7833*/
drh658873b2015-06-22 20:02:047834static int editPage(
dan09c68402014-10-11 20:00:247835 MemPage *pPg, /* Edit this page */
7836 int iOld, /* Index of first cell currently on page */
7837 int iNew, /* Index of new first cell on page */
7838 int nNew, /* Final number of cells on page */
drh1ffd2472015-06-23 02:37:307839 CellArray *pCArray /* Array of cells and sizes */
dan09c68402014-10-11 20:00:247840){
dand7b545b2014-10-13 18:03:277841 u8 * const aData = pPg->aData;
7842 const int hdr = pPg->hdrOffset;
7843 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
7844 int nCell = pPg->nCell; /* Cells stored on pPg */
7845 u8 *pData;
7846 u8 *pCellptr;
7847 int i;
7848 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
7849 int iNewEnd = iNew + nNew;
dan09c68402014-10-11 20:00:247850
7851#ifdef SQLITE_DEBUG
dand7b545b2014-10-13 18:03:277852 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
7853 memcpy(pTmp, aData, pPg->pBt->usableSize);
dan09c68402014-10-11 20:00:247854#endif
7855
dand7b545b2014-10-13 18:03:277856 /* Remove cells from the start and end of the page */
drha0466432019-01-29 16:41:137857 assert( nCell>=0 );
dand7b545b2014-10-13 18:03:277858 if( iOld<iNew ){
drhf7838932015-06-23 15:36:347859 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
drhfde25922020-05-05 19:54:027860 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
dand7b545b2014-10-13 18:03:277861 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
7862 nCell -= nShift;
7863 }
7864 if( iNewEnd < iOldEnd ){
drha0466432019-01-29 16:41:137865 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
7866 assert( nCell>=nTail );
7867 nCell -= nTail;
dand7b545b2014-10-13 18:03:277868 }
dan09c68402014-10-11 20:00:247869
drhaddc7e02023-05-08 13:38:147870 pData = &aData[get2byte(&aData[hdr+5])];
dand7b545b2014-10-13 18:03:277871 if( pData<pBegin ) goto editpage_fail;
drhd8c34e32023-04-03 12:33:127872 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
dand7b545b2014-10-13 18:03:277873
7874 /* Add cells to the start of the page */
7875 if( iNew<iOld ){
drh5ab63772014-11-27 03:46:047876 int nAdd = MIN(nNew,iOld-iNew);
7877 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
drha0466432019-01-29 16:41:137878 assert( nAdd>=0 );
dand7b545b2014-10-13 18:03:277879 pCellptr = pPg->aCellIdx;
7880 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
7881 if( pageInsertArray(
7882 pPg, pBegin, &pData, pCellptr,
drhf7838932015-06-23 15:36:347883 iNew, nAdd, pCArray
dand7b545b2014-10-13 18:03:277884 ) ) goto editpage_fail;
7885 nCell += nAdd;
7886 }
7887
7888 /* Add any overflow cells */
7889 for(i=0; i<pPg->nOverflow; i++){
7890 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
7891 if( iCell>=0 && iCell<nNew ){
drhfefa0942014-11-05 21:21:087892 pCellptr = &pPg->aCellIdx[iCell * 2];
drh4b986b22019-03-08 14:02:117893 if( nCell>iCell ){
7894 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
7895 }
dand7b545b2014-10-13 18:03:277896 nCell++;
dan666a42f2019-08-24 21:02:477897 cachedCellSize(pCArray, iCell+iNew);
dand7b545b2014-10-13 18:03:277898 if( pageInsertArray(
7899 pPg, pBegin, &pData, pCellptr,
drhf7838932015-06-23 15:36:347900 iCell+iNew, 1, pCArray
dand7b545b2014-10-13 18:03:277901 ) ) goto editpage_fail;
dan09c68402014-10-11 20:00:247902 }
dand7b545b2014-10-13 18:03:277903 }
dan09c68402014-10-11 20:00:247904
dand7b545b2014-10-13 18:03:277905 /* Append cells to the end of the page */
drha0466432019-01-29 16:41:137906 assert( nCell>=0 );
dand7b545b2014-10-13 18:03:277907 pCellptr = &pPg->aCellIdx[nCell*2];
7908 if( pageInsertArray(
7909 pPg, pBegin, &pData, pCellptr,
drhf7838932015-06-23 15:36:347910 iNew+nCell, nNew-nCell, pCArray
drhc071c472025-02-22 16:44:147911 )
7912 ){
7913 goto editpage_fail;
7914 }
dan09c68402014-10-11 20:00:247915
drhc071c472025-02-22 16:44:147916 assert( nNew < 10922 );
7917 pPg->nCell = (u16)nNew;
dand7b545b2014-10-13 18:03:277918 pPg->nOverflow = 0;
dan09c68402014-10-11 20:00:247919
dand7b545b2014-10-13 18:03:277920 put2byte(&aData[hdr+3], pPg->nCell);
7921 put2byte(&aData[hdr+5], pData - aData);
dan09c68402014-10-11 20:00:247922
7923#ifdef SQLITE_DEBUG
dan23eba452014-10-24 18:43:577924 for(i=0; i<nNew && !CORRUPT_DB; i++){
drh1ffd2472015-06-23 02:37:307925 u8 *pCell = pCArray->apCell[i+iNew];
drh329428e2015-06-30 13:28:187926 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
drh1c715f62016-04-05 13:35:437927 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
dand7b545b2014-10-13 18:03:277928 pCell = &pTmp[pCell - aData];
dan09c68402014-10-11 20:00:247929 }
drh1ffd2472015-06-23 02:37:307930 assert( 0==memcmp(pCell, &aData[iOff],
7931 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
dand7b545b2014-10-13 18:03:277932 }
dan09c68402014-10-11 20:00:247933#endif
7934
drh658873b2015-06-22 20:02:047935 return SQLITE_OK;
dan09c68402014-10-11 20:00:247936 editpage_fail:
dan09c68402014-10-11 20:00:247937 /* Unable to edit this page. Rebuild it from scratch instead. */
drh5d9446d2023-10-05 11:06:107938 if( nNew<1 ) return SQLITE_CORRUPT_BKPT;
drh1ffd2472015-06-23 02:37:307939 populateCellCache(pCArray, iNew, nNew);
drhe3dadac2019-01-23 19:25:597940 return rebuildPage(pCArray, iNew, nNew, pPg);
drhfa1a98a2004-05-14 19:08:177941}
7942
danielk1977ac245ec2005-01-14 13:50:117943
drh615ae552005-01-16 23:21:007944#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:497945/*
7946** This version of balance() handles the common special case where
7947** a new entry is being inserted on the extreme right-end of the
7948** tree, in other words, when the new entry will become the largest
7949** entry in the tree.
7950**
drhc314dc72009-07-21 11:52:347951** Instead of trying to balance the 3 right-most leaf pages, just add
drhf222e712005-01-14 22:55:497952** a new page to the right-hand side and put the one new entry in
7953** that page. This leaves the right side of the tree somewhat
7954** unbalanced. But odds are that we will be inserting new entries
7955** at the end soon afterwards so the nearly empty page will quickly
7956** fill up. On average.
7957**
7958** pPage is the leaf page which is the right-most page in the tree.
7959** pParent is its parent. pPage must have a single overflow entry
7960** which is also the right-most entry on the page.
danielk1977a50d9aa2009-06-08 14:49:457961**
7962** The pSpace buffer is used to store a temporary copy of the divider
7963** cell that will be inserted into pParent. Such a cell consists of a 4
7964** byte page number followed by a variable length integer. In other
7965** words, at most 13 bytes. Hence the pSpace buffer must be at
7966** least 13 bytes in size.
drhf222e712005-01-14 22:55:497967*/
danielk1977a50d9aa2009-06-08 14:49:457968static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
7969 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
danielk19774dbaa892009-06-16 16:50:227970 MemPage *pNew; /* Newly allocated page */
danielk19776f235cc2009-06-04 14:46:087971 int rc; /* Return Code */
7972 Pgno pgnoNew; /* Page number of pNew */
danielk1977ac245ec2005-01-14 13:50:117973
drh1fee73e2007-08-29 04:00:577974 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk1977a50d9aa2009-06-08 14:49:457975 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
danielk1977e56b60e2009-06-10 09:11:067976 assert( pPage->nOverflow==1 );
larrybrbc917382023-06-07 08:40:317977
drh6301c432018-12-13 21:52:187978 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
drh68133502019-02-11 17:22:307979 assert( pPage->nFree>=0 );
7980 assert( pParent->nFree>=0 );
drhd677b3d2007-08-20 22:48:417981
larrybrbc917382023-06-07 08:40:317982 /* Allocate a new page. This page will become the right-sibling of
danielk1977a50d9aa2009-06-08 14:49:457983 ** pPage. Make the parent page writable, so that the new divider cell
7984 ** may be inserted. If both these operations are successful, proceed.
7985 */
drh4f0c5872007-03-26 22:05:017986 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk19774dbaa892009-06-16 16:50:227987
danielk1977eaa06f62008-09-18 17:34:447988 if( rc==SQLITE_OK ){
danielk1977a50d9aa2009-06-08 14:49:457989
7990 u8 *pOut = &pSpace[4];
drh2cbd78b2012-02-02 19:37:187991 u8 *pCell = pPage->apOvfl[0];
drh25ada072015-06-19 15:07:147992 u16 szCell = pPage->xCellSize(pPage, pCell);
danielk19776f235cc2009-06-04 14:46:087993 u8 *pStop;
drhe3dadac2019-01-23 19:25:597994 CellArray b;
danielk19776f235cc2009-06-04 14:46:087995
drhc5053fb2008-11-27 02:22:107996 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
danba14c692019-01-25 13:42:127997 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
danielk1977e56b60e2009-06-10 09:11:067998 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
drhe3dadac2019-01-23 19:25:597999 b.nCell = 1;
8000 b.pRef = pPage;
8001 b.apCell = &pCell;
8002 b.szCell = &szCell;
8003 b.apEnd[0] = pPage->aDataEnd;
8004 b.ixNx[0] = 2;
drheeb85062024-05-27 19:52:248005 b.ixNx[NB*2-1] = 0x7fffffff;
drhe3dadac2019-01-23 19:25:598006 rc = rebuildPage(&b, 0, 1, pNew);
8007 if( NEVER(rc) ){
8008 releasePage(pNew);
8009 return rc;
8010 }
dan8e9ba0c2014-10-14 17:27:048011 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
danielk19774dbaa892009-06-16 16:50:228012
8013 /* If this is an auto-vacuum database, update the pointer map
larrybrbc917382023-06-07 08:40:318014 ** with entries for the new page, and any pointer from the
danielk19774dbaa892009-06-16 16:50:228015 ** cell on the page to an overflow page. If either of these
8016 ** operations fails, the return code is set, but the contents
larrybrbc917382023-06-07 08:40:318017 ** of the parent page are still manipulated by the code below.
danielk19774dbaa892009-06-16 16:50:228018 ** That is Ok, at this point the parent page is guaranteed to
8019 ** be marked as dirty. Returning an error code will cause a
8020 ** rollback, undoing any changes made to the parent page.
8021 */
drhe7d53842022-11-21 14:13:108022 if( ISAUTOVACUUM(pBt) ){
drh98add2e2009-07-20 17:11:498023 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
8024 if( szCell>pNew->minLocal ){
drh0f1bf4c2019-01-13 20:17:218025 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
danielk19774dbaa892009-06-16 16:50:228026 }
8027 }
larrybrbc917382023-06-07 08:40:318028
danielk19776f235cc2009-06-04 14:46:088029 /* Create a divider cell to insert into pParent. The divider cell
8030 ** consists of a 4-byte page number (the page number of pPage) and
8031 ** a variable length key value (which must be the same value as the
8032 ** largest key on pPage).
danielk1977eaa06f62008-09-18 17:34:448033 **
larrybrbc917382023-06-07 08:40:318034 ** To find the largest key value on pPage, first find the right-most
8035 ** cell on pPage. The first two fields of this cell are the
danielk19776f235cc2009-06-04 14:46:088036 ** record-length (a variable length integer at most 32-bits in size)
8037 ** and the key value (a variable length integer, may have any value).
8038 ** The first of the while(...) loops below skips over the record-length
8039 ** field. The second while(...) loop copies the key value from the
danielk1977a50d9aa2009-06-08 14:49:458040 ** cell on pPage into the pSpace buffer.
danielk1977eaa06f62008-09-18 17:34:448041 */
danielk1977eaa06f62008-09-18 17:34:448042 pCell = findCell(pPage, pPage->nCell-1);
danielk19776f235cc2009-06-04 14:46:088043 pStop = &pCell[9];
8044 while( (*(pCell++)&0x80) && pCell<pStop );
8045 pStop = &pCell[9];
8046 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
8047
danielk19774dbaa892009-06-16 16:50:228048 /* Insert the new divider cell into pParent. */
drhcb89f4a2016-05-21 11:23:268049 if( rc==SQLITE_OK ){
drhb53d8fa2022-11-21 15:55:578050 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
8051 0, pPage->pgno);
drhcb89f4a2016-05-21 11:23:268052 }
danielk19776f235cc2009-06-04 14:46:088053
8054 /* Set the right-child pointer of pParent to point to the new page. */
danielk1977eaa06f62008-09-18 17:34:448055 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
larrybrbc917382023-06-07 08:40:318056
danielk1977e08a3c42008-09-18 18:17:038057 /* Release the reference to the new page. */
8058 releasePage(pNew);
danielk1977ac11ee62005-01-15 12:45:518059 }
8060
danielk1977eaa06f62008-09-18 17:34:448061 return rc;
danielk1977ac245ec2005-01-14 13:50:118062}
drh615ae552005-01-16 23:21:008063#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:498064
danielk19774dbaa892009-06-16 16:50:228065#if 0
drhc3b70572003-01-04 19:44:078066/*
danielk19774dbaa892009-06-16 16:50:228067** This function does not contribute anything to the operation of SQLite.
larrybrbc917382023-06-07 08:40:318068** it is sometimes activated temporarily while debugging code responsible
danielk19774dbaa892009-06-16 16:50:228069** for setting pointer-map entries.
8070*/
8071static int ptrmapCheckPages(MemPage **apPage, int nPage){
8072 int i, j;
8073 for(i=0; i<nPage; i++){
8074 Pgno n;
8075 u8 e;
8076 MemPage *pPage = apPage[i];
8077 BtShared *pBt = pPage->pBt;
8078 assert( pPage->isInit );
8079
8080 for(j=0; j<pPage->nCell; j++){
8081 CellInfo info;
8082 u8 *z;
larrybrbc917382023-06-07 08:40:318083
danielk19774dbaa892009-06-16 16:50:228084 z = findCell(pPage, j);
drh5fa60512015-06-19 17:19:348085 pPage->xParseCell(pPage, z, &info);
drh45ac1c72015-12-18 03:59:168086 if( info.nLocal<info.nPayload ){
8087 Pgno ovfl = get4byte(&z[info.nSize-4]);
danielk19774dbaa892009-06-16 16:50:228088 ptrmapGet(pBt, ovfl, &e, &n);
8089 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
8090 }
8091 if( !pPage->leaf ){
8092 Pgno child = get4byte(z);
8093 ptrmapGet(pBt, child, &e, &n);
8094 assert( n==pPage->pgno && e==PTRMAP_BTREE );
8095 }
8096 }
8097 if( !pPage->leaf ){
8098 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
8099 ptrmapGet(pBt, child, &e, &n);
8100 assert( n==pPage->pgno && e==PTRMAP_BTREE );
8101 }
8102 }
8103 return 1;
8104}
8105#endif
8106
danielk1977cd581a72009-06-23 15:43:398107/*
larrybrbc917382023-06-07 08:40:318108** This function is used to copy the contents of the b-tree node stored
danielk1977cd581a72009-06-23 15:43:398109** on page pFrom to page pTo. If page pFrom was not a leaf page, then
8110** the pointer-map entries for each child page are updated so that the
8111** parent page stored in the pointer map is page pTo. If pFrom contained
8112** any cells with overflow page pointers, then the corresponding pointer
8113** map entries are also updated so that the parent page is page pTo.
8114**
8115** If pFrom is currently carrying any overflow cells (entries in the
larrybrbc917382023-06-07 08:40:318116** MemPage.apOvfl[] array), they are not copied to pTo.
danielk1977cd581a72009-06-23 15:43:398117**
danielk197730548662009-07-09 05:07:378118** Before returning, page pTo is reinitialized using btreeInitPage().
danielk1977cd581a72009-06-23 15:43:398119**
larrybrbc917382023-06-07 08:40:318120** The performance of this function is not critical. It is only used by
danielk1977cd581a72009-06-23 15:43:398121** the balance_shallower() and balance_deeper() procedures, neither of
8122** which are called often under normal circumstances.
8123*/
drhc314dc72009-07-21 11:52:348124static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
8125 if( (*pRC)==SQLITE_OK ){
8126 BtShared * const pBt = pFrom->pBt;
8127 u8 * const aFrom = pFrom->aData;
8128 u8 * const aTo = pTo->aData;
8129 int const iFromHdr = pFrom->hdrOffset;
8130 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
drhdc9b5f82009-12-05 18:34:088131 int rc;
drhc314dc72009-07-21 11:52:348132 int iData;
larrybrbc917382023-06-07 08:40:318133
8134
drhc314dc72009-07-21 11:52:348135 assert( pFrom->isInit );
8136 assert( pFrom->nFree>=iToHdr );
drhfcd71b62011-04-05 22:08:248137 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
larrybrbc917382023-06-07 08:40:318138
drhc314dc72009-07-21 11:52:348139 /* Copy the b-tree node content from page pFrom to page pTo. */
8140 iData = get2byte(&aFrom[iFromHdr+5]);
8141 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
8142 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
larrybrbc917382023-06-07 08:40:318143
drhc314dc72009-07-21 11:52:348144 /* Reinitialize page pTo so that the contents of the MemPage structure
dan89e060e2009-12-05 18:03:508145 ** match the new data. The initialization of pTo can actually fail under
larrybrbc917382023-06-07 08:40:318146 ** fairly obscure circumstances, even though it is a copy of initialized
dan89e060e2009-12-05 18:03:508147 ** page pFrom.
8148 */
drhc314dc72009-07-21 11:52:348149 pTo->isInit = 0;
dan89e060e2009-12-05 18:03:508150 rc = btreeInitPage(pTo);
drh8357c662019-02-11 22:50:018151 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
dan89e060e2009-12-05 18:03:508152 if( rc!=SQLITE_OK ){
8153 *pRC = rc;
8154 return;
8155 }
larrybrbc917382023-06-07 08:40:318156
drhc314dc72009-07-21 11:52:348157 /* If this is an auto-vacuum database, update the pointer-map entries
8158 ** for any b-tree or overflow pages that pTo now contains the pointers to.
8159 */
drhe7d53842022-11-21 14:13:108160 if( ISAUTOVACUUM(pBt) ){
drhc314dc72009-07-21 11:52:348161 *pRC = setChildPtrmaps(pTo);
8162 }
danielk1977cd581a72009-06-23 15:43:398163 }
danielk1977cd581a72009-06-23 15:43:398164}
8165
8166/*
danielk19774dbaa892009-06-16 16:50:228167** This routine redistributes cells on the iParentIdx'th child of pParent
8168** (hereafter "the page") and up to 2 siblings so that all pages have about the
8169** same amount of free space. Usually a single sibling on either side of the
8170** page are used in the balancing, though both siblings might come from one
larrybrbc917382023-06-07 08:40:318171** side if the page is the first or last child of its parent. If the page
danielk19774dbaa892009-06-16 16:50:228172** has fewer than 2 siblings (something which can only happen if the page
8173** is a root page or a child of a root page) then all available siblings
8174** participate in the balancing.
drh8b2f49b2001-06-08 00:21:528175**
larrybrbc917382023-06-07 08:40:318176** The number of siblings of the page might be increased or decreased by
8177** one or two in an effort to keep pages nearly full but not over full.
drh14acc042001-06-10 19:56:588178**
danielk19774dbaa892009-06-16 16:50:228179** Note that when this routine is called, some of the cells on the page
8180** might not actually be stored in MemPage.aData[]. This can happen
8181** if the page is overfull. This routine ensures that all cells allocated
8182** to the page and its siblings fit into MemPage.aData[] before returning.
drh14acc042001-06-10 19:56:588183**
danielk19774dbaa892009-06-16 16:50:228184** In the course of balancing the page and its siblings, cells may be
8185** inserted into or removed from the parent page (pParent). Doing so
8186** may cause the parent page to become overfull or underfull. If this
8187** happens, it is the responsibility of the caller to invoke the correct
larrybrbc917382023-06-07 08:40:318188** balancing routine to fix this problem (see the balance() routine).
drh8c42ca92001-06-22 19:15:008189**
drh5e00f6c2001-09-13 13:46:568190** If this routine fails for any reason, it might leave the database
danielk19776067a9b2009-06-09 09:41:008191** in a corrupted state. So if this routine fails, the database should
drh5e00f6c2001-09-13 13:46:568192** be rolled back.
danielk19774dbaa892009-06-16 16:50:228193**
8194** The third argument to this function, aOvflSpace, is a pointer to a
drhcd09c532009-07-20 19:30:008195** buffer big enough to hold one page. If while inserting cells into the parent
8196** page (pParent) the parent page becomes overfull, this buffer is
8197** used to store the parent's overflow cells. Because this function inserts
danielk19774dbaa892009-06-16 16:50:228198** a maximum of four divider cells into the parent page, and the maximum
8199** size of a cell stored within an internal node is always less than 1/4
8200** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
8201** enough for all overflow cells.
8202**
larrybrbc917382023-06-07 08:40:318203** If aOvflSpace is set to a null pointer, this function returns
danielk19774dbaa892009-06-16 16:50:228204** SQLITE_NOMEM.
drh8b2f49b2001-06-08 00:21:528205*/
danielk19774dbaa892009-06-16 16:50:228206static int balance_nonroot(
8207 MemPage *pParent, /* Parent page of siblings being balanced */
8208 int iParentIdx, /* Index of "the page" in pParent */
danielk1977cd581a72009-06-23 15:43:398209 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
dan428c2182012-08-06 18:50:118210 int isRoot, /* True if pParent is a root-page */
8211 int bBulk /* True if this call is part of a bulk load */
danielk19774dbaa892009-06-16 16:50:228212){
drh16a9b832007-05-05 18:39:258213 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:078214 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
danielk1977a4124bd2008-12-23 10:37:478215 int nNew = 0; /* Number of pages in apNew[] */
danielk19774dbaa892009-06-16 16:50:228216 int nOld; /* Number of pages in apOld[] */
drh14acc042001-06-10 19:56:588217 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:428218 int nxDiv; /* Next divider slot in pParent->aCell[] */
shane85095702009-06-15 16:27:088219 int rc = SQLITE_OK; /* The return code */
shane36840fd2009-06-26 16:32:138220 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:158221 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:328222 int usableSpace; /* Bytes in pPage beyond the header */
8223 int pageFlags; /* Value of pPage->aData[0] */
drhe5ae5732008-06-15 02:51:478224 int iSpace1 = 0; /* First unused byte of aSpace1[] */
danielk19776067a9b2009-06-09 09:41:008225 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
drhc071c472025-02-22 16:44:148226 u64 szScratch; /* Size of scratch memory requested */
drhc3b70572003-01-04 19:44:078227 MemPage *apOld[NB]; /* pPage and up to two siblings */
drha2fce642004-06-05 00:01:448228 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
danielk19774dbaa892009-06-16 16:50:228229 u8 *pRight; /* Location in parent of right-sibling pointer */
8230 u8 *apDiv[NB-1]; /* Divider cells in pParent */
drh1ffd2472015-06-23 02:37:308231 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
8232 int cntOld[NB+2]; /* Old index in b.apCell[] */
drh2a0df922014-10-30 23:14:568233 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
danielk19774dbaa892009-06-16 16:50:228234 u8 *aSpace1; /* Space for copies of dividers cells */
8235 Pgno pgno; /* Temp var to store a page number in */
dane6593d82014-10-24 16:40:498236 u8 abDone[NB+2]; /* True after i'th new page is populated */
8237 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
drh7d4c94b2021-10-04 22:34:388238 CellArray b; /* Parsed information on cells being balanced */
drh8b2f49b2001-06-08 00:21:528239
dan33ea4862014-10-09 19:35:378240 memset(abDone, 0, sizeof(abDone));
drh09d8e692024-05-27 15:13:498241 assert( sizeof(b) - sizeof(b.ixNx) == offsetof(CellArray,ixNx) );
drheeb85062024-05-27 19:52:248242 memset(&b, 0, sizeof(b)-sizeof(b.ixNx[0]));
drh09d8e692024-05-27 15:13:498243 b.ixNx[NB*2-1] = 0x7fffffff;
danielk1977a50d9aa2009-06-08 14:49:458244 pBt = pParent->pBt;
8245 assert( sqlite3_mutex_held(pBt->mutex) );
8246 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
danielk1977474b7cc2008-07-09 11:49:468247
danielk19774dbaa892009-06-16 16:50:228248 /* At this point pParent may have at most one overflow cell. And if
larrybrbc917382023-06-07 08:40:318249 ** this overflow cell is present, it must be the cell with
danielk19774dbaa892009-06-16 16:50:228250 ** index iParentIdx. This scenario comes about when this function
drhcd09c532009-07-20 19:30:008251 ** is called (indirectly) from sqlite3BtreeDelete().
8252 */
danielk19774dbaa892009-06-16 16:50:228253 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
drh2cbd78b2012-02-02 19:37:188254 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
danielk19774dbaa892009-06-16 16:50:228255
danielk197711a8a862009-06-17 11:49:528256 if( !aOvflSpace ){
mistachkinfad30392016-02-13 23:43:468257 return SQLITE_NOMEM_BKPT;
danielk197711a8a862009-06-17 11:49:528258 }
drh68133502019-02-11 17:22:308259 assert( pParent->nFree>=0 );
danielk197711a8a862009-06-17 11:49:528260
larrybrbc917382023-06-07 08:40:318261 /* Find the sibling pages to balance. Also locate the cells in pParent
8262 ** that divide the siblings. An attempt is made to find NN siblings on
8263 ** either side of pPage. More siblings are taken from one side, however,
danielk1977a50d9aa2009-06-08 14:49:458264 ** if there are fewer than NN siblings on the other side. If pParent
larrybrbc917382023-06-07 08:40:318265 ** has NB or fewer children then all children of pParent are taken.
danielk19774dbaa892009-06-16 16:50:228266 **
8267 ** This loop also drops the divider cells from the parent page. This
8268 ** way, the remainder of the function does not have to deal with any
drhcd09c532009-07-20 19:30:008269 ** overflow cells in the parent page, since if any existed they will
8270 ** have already been removed.
8271 */
danielk19774dbaa892009-06-16 16:50:228272 i = pParent->nOverflow + pParent->nCell;
8273 if( i<2 ){
drhc3b70572003-01-04 19:44:078274 nxDiv = 0;
danielk19774dbaa892009-06-16 16:50:228275 }else{
dan7d6885a2012-08-08 14:04:568276 assert( bBulk==0 || bBulk==1 );
larrybrbc917382023-06-07 08:40:318277 if( iParentIdx==0 ){
danielk19774dbaa892009-06-16 16:50:228278 nxDiv = 0;
8279 }else if( iParentIdx==i ){
dan7d6885a2012-08-08 14:04:568280 nxDiv = i-2+bBulk;
drh14acc042001-06-10 19:56:588281 }else{
danielk19774dbaa892009-06-16 16:50:228282 nxDiv = iParentIdx-1;
drh8b2f49b2001-06-08 00:21:528283 }
dan7d6885a2012-08-08 14:04:568284 i = 2-bBulk;
danielk19774dbaa892009-06-16 16:50:228285 }
dan7d6885a2012-08-08 14:04:568286 nOld = i+1;
danielk19774dbaa892009-06-16 16:50:228287 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
8288 pRight = &pParent->aData[pParent->hdrOffset+8];
8289 }else{
8290 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
8291 }
8292 pgno = get4byte(pRight);
8293 while( 1 ){
dan1f9f5762021-03-01 16:15:418294 if( rc==SQLITE_OK ){
drhd4170ac2023-06-22 01:03:398295 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
dan1f9f5762021-03-01 16:15:418296 }
danielk19774dbaa892009-06-16 16:50:228297 if( rc ){
danielk197789bc4bc2009-07-21 19:25:248298 memset(apOld, 0, (i+1)*sizeof(MemPage*));
danielk19774dbaa892009-06-16 16:50:228299 goto balance_cleanup;
8300 }
drh85a379b2019-02-09 22:33:448301 if( apOld[i]->nFree<0 ){
8302 rc = btreeComputeFreeSpace(apOld[i]);
8303 if( rc ){
8304 memset(apOld, 0, (i)*sizeof(MemPage*));
8305 goto balance_cleanup;
8306 }
8307 }
danb9f8a182021-06-22 14:59:348308 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
danielk19774dbaa892009-06-16 16:50:228309 if( (i--)==0 ) break;
8310
drh9cc5b4e2016-12-26 01:41:338311 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
drh2cbd78b2012-02-02 19:37:188312 apDiv[i] = pParent->apOvfl[0];
danielk19774dbaa892009-06-16 16:50:228313 pgno = get4byte(apDiv[i]);
drh25ada072015-06-19 15:07:148314 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
danielk19774dbaa892009-06-16 16:50:228315 pParent->nOverflow = 0;
8316 }else{
8317 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
8318 pgno = get4byte(apDiv[i]);
drh25ada072015-06-19 15:07:148319 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
danielk19774dbaa892009-06-16 16:50:228320
8321 /* Drop the cell from the parent page. apDiv[i] still points to
8322 ** the cell within the parent, even though it has been dropped.
8323 ** This is safe because dropping a cell only overwrites the first
8324 ** four bytes of it, and this function does not need the first
8325 ** four bytes of the divider cell. So the pointer is safe to use
larrybrbc917382023-06-07 08:40:318326 ** later on.
danielk197711a8a862009-06-17 11:49:528327 **
drh8a575d92011-10-12 17:00:288328 ** But not if we are in secure-delete mode. In secure-delete mode,
danielk197711a8a862009-06-17 11:49:528329 ** the dropCell() routine will overwrite the entire cell with zeroes.
8330 ** In this case, temporarily copy the cell into the aOvflSpace[]
8331 ** buffer. It will be copied out again as soon as the aSpace[] buffer
8332 ** is allocated. */
drha5907a82017-06-19 11:44:228333 if( pBt->btsFlags & BTS_FAST_SECURE ){
drh8a575d92011-10-12 17:00:288334 int iOff;
8335
dan1f9f5762021-03-01 16:15:418336 /* If the following if() condition is not true, the db is corrupted.
8337 ** The call to dropCell() below will detect this. */
drh8a575d92011-10-12 17:00:288338 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
dan1f9f5762021-03-01 16:15:418339 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
dan2ed11e72010-02-26 15:09:198340 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
8341 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
8342 }
drh5b47efa2010-02-12 18:18:398343 }
drh98add2e2009-07-20 17:11:498344 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
danielk19774dbaa892009-06-16 16:50:228345 }
drh8b2f49b2001-06-08 00:21:528346 }
8347
drha9121e42008-02-19 14:59:358348 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
drh8d97f1f2005-05-05 18:14:138349 ** alignment */
drha9121e42008-02-19 14:59:358350 nMaxCells = (nMaxCells + 3)&~3;
drh8d97f1f2005-05-05 18:14:138351
drh8b2f49b2001-06-08 00:21:528352 /*
danielk1977634f2982005-03-28 08:44:078353 ** Allocate space for memory structures
8354 */
drhfacf0302008-06-17 15:12:008355 szScratch =
drh1ffd2472015-06-23 02:37:308356 nMaxCells*sizeof(u8*) /* b.apCell */
8357 + nMaxCells*sizeof(u16) /* b.szCell */
dan33ea4862014-10-09 19:35:378358 + pBt->pageSize; /* aSpace1 */
drh5279d342014-11-04 13:41:328359
drhf012dc42019-03-19 15:36:468360 assert( szScratch<=7*(int)pBt->pageSize );
drhb2a0f752017-08-28 15:51:358361 b.apCell = sqlite3StackAllocRaw(0, szScratch );
drh1ffd2472015-06-23 02:37:308362 if( b.apCell==0 ){
mistachkinfad30392016-02-13 23:43:468363 rc = SQLITE_NOMEM_BKPT;
danielk1977634f2982005-03-28 08:44:078364 goto balance_cleanup;
8365 }
drh1ffd2472015-06-23 02:37:308366 b.szCell = (u16*)&b.apCell[nMaxCells];
8367 aSpace1 = (u8*)&b.szCell[nMaxCells];
drhea598cb2009-04-05 12:22:088368 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
drh14acc042001-06-10 19:56:588369
8370 /*
8371 ** Load pointers to all cells on sibling pages and the divider cells
drh1ffd2472015-06-23 02:37:308372 ** into the local b.apCell[] array. Make copies of the divider cells
dan33ea4862014-10-09 19:35:378373 ** into space obtained from aSpace1[]. The divider cells have already
8374 ** been removed from pParent.
drh4b70f112004-05-02 21:12:198375 **
8376 ** If the siblings are on leaf pages, then the child pointers of the
8377 ** divider cells are stripped from the cells before they are copied
drh1ffd2472015-06-23 02:37:308378 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
drh4b70f112004-05-02 21:12:198379 ** child pointers. If siblings are not leaves, then all cell in
drh1ffd2472015-06-23 02:37:308380 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
drh4b70f112004-05-02 21:12:198381 ** are alike.
drh96f5b762004-05-16 16:24:368382 **
8383 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
8384 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:528385 */
drh1ffd2472015-06-23 02:37:308386 b.pRef = apOld[0];
8387 leafCorrection = b.pRef->leaf*4;
8388 leafData = b.pRef->intKeyLeaf;
drh8b2f49b2001-06-08 00:21:528389 for(i=0; i<nOld; i++){
dan33ea4862014-10-09 19:35:378390 MemPage *pOld = apOld[i];
drh4edfdd32015-06-23 14:49:428391 int limit = pOld->nCell;
8392 u8 *aData = pOld->aData;
8393 u16 maskPage = pOld->maskPage;
drh4f4bf772015-06-23 17:09:538394 u8 *piCell = aData + pOld->cellOffset;
drhfe647dc2015-06-23 18:24:258395 u8 *piEnd;
drhe12ca5a2019-05-02 15:56:398396 VVA_ONLY( int nCellAtStart = b.nCell; )
danielk19774dbaa892009-06-16 16:50:228397
drh73d340a2015-05-28 11:23:118398 /* Verify that all sibling pages are of the same "type" (table-leaf,
8399 ** table-interior, index-leaf, or index-interior).
8400 */
8401 if( pOld->aData[0]!=apOld[0]->aData[0] ){
drh50f9caf2024-02-02 18:42:098402 rc = SQLITE_CORRUPT_PAGE(pOld);
drh73d340a2015-05-28 11:23:118403 goto balance_cleanup;
8404 }
8405
drhfe647dc2015-06-23 18:24:258406 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
drh8d7f1632018-01-23 13:30:388407 ** contains overflow cells, include them in the b.apCell[] array
drhfe647dc2015-06-23 18:24:258408 ** in the correct spot.
8409 **
8410 ** Note that when there are multiple overflow cells, it is always the
8411 ** case that they are sequential and adjacent. This invariant arises
8412 ** because multiple overflows can only occurs when inserting divider
8413 ** cells into a parent on a prior balance, and divider cells are always
8414 ** adjacent and are inserted in order. There is an assert() tagged
8415 ** with "NOTE 1" in the overflow cell insertion loop to prove this
8416 ** invariant.
drh4edfdd32015-06-23 14:49:428417 **
8418 ** This must be done in advance. Once the balance starts, the cell
8419 ** offset section of the btree page will be overwritten and we will no
8420 ** long be able to find the cells if a pointer to each cell is not saved
8421 ** first.
8422 */
drh36b78ee2016-01-20 01:32:008423 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
drh68f2a572011-06-03 17:50:498424 if( pOld->nOverflow>0 ){
drh27e80a32019-08-15 13:17:498425 if( NEVER(limit<pOld->aiOvfl[0]) ){
drh50f9caf2024-02-02 18:42:098426 rc = SQLITE_CORRUPT_PAGE(pOld);
drhe12ca5a2019-05-02 15:56:398427 goto balance_cleanup;
8428 }
drhfe647dc2015-06-23 18:24:258429 limit = pOld->aiOvfl[0];
drh68f2a572011-06-03 17:50:498430 for(j=0; j<limit; j++){
drh329428e2015-06-30 13:28:188431 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
drhfe647dc2015-06-23 18:24:258432 piCell += 2;
8433 b.nCell++;
drh68f2a572011-06-03 17:50:498434 }
drhfe647dc2015-06-23 18:24:258435 for(k=0; k<pOld->nOverflow; k++){
8436 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
drh4edfdd32015-06-23 14:49:428437 b.apCell[b.nCell] = pOld->apOvfl[k];
drh1ffd2472015-06-23 02:37:308438 b.nCell++;
drh68f2a572011-06-03 17:50:498439 }
drh1ffd2472015-06-23 02:37:308440 }
drhfe647dc2015-06-23 18:24:258441 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
8442 while( piCell<piEnd ){
drh4edfdd32015-06-23 14:49:428443 assert( b.nCell<nMaxCells );
drh329428e2015-06-30 13:28:188444 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
drh4f4bf772015-06-23 17:09:538445 piCell += 2;
drh4edfdd32015-06-23 14:49:428446 b.nCell++;
drh4edfdd32015-06-23 14:49:428447 }
drhe12ca5a2019-05-02 15:56:398448 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
drh4edfdd32015-06-23 14:49:428449
drh1ffd2472015-06-23 02:37:308450 cntOld[i] = b.nCell;
danielk19774dbaa892009-06-16 16:50:228451 if( i<nOld-1 && !leafData){
shane36840fd2009-06-26 16:32:138452 u16 sz = (u16)szNew[i];
danielk19774dbaa892009-06-16 16:50:228453 u8 *pTemp;
drh1ffd2472015-06-23 02:37:308454 assert( b.nCell<nMaxCells );
8455 b.szCell[b.nCell] = sz;
danielk19774dbaa892009-06-16 16:50:228456 pTemp = &aSpace1[iSpace1];
8457 iSpace1 += sz;
drhe22e03e2010-08-18 21:19:038458 assert( sz<=pBt->maxLocal+23 );
drhfcd71b62011-04-05 22:08:248459 assert( iSpace1 <= (int)pBt->pageSize );
danielk19774dbaa892009-06-16 16:50:228460 memcpy(pTemp, apDiv[i], sz);
drh1ffd2472015-06-23 02:37:308461 b.apCell[b.nCell] = pTemp+leafCorrection;
danielk19774dbaa892009-06-16 16:50:228462 assert( leafCorrection==0 || leafCorrection==4 );
drh1ffd2472015-06-23 02:37:308463 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
danielk19774dbaa892009-06-16 16:50:228464 if( !pOld->leaf ){
8465 assert( leafCorrection==0 );
dan5b482a92021-04-20 13:31:518466 assert( pOld->hdrOffset==0 || CORRUPT_DB );
danielk19774dbaa892009-06-16 16:50:228467 /* The right pointer of the child page pOld becomes the left
8468 ** pointer of the divider cell */
drh1ffd2472015-06-23 02:37:308469 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
danielk19774dbaa892009-06-16 16:50:228470 }else{
8471 assert( leafCorrection==4 );
drh1ffd2472015-06-23 02:37:308472 while( b.szCell[b.nCell]<4 ){
dan8f1eb8a2014-12-06 14:56:498473 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
8474 ** does exist, pad it with 0x00 bytes. */
drh1ffd2472015-06-23 02:37:308475 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
8476 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
danee7172f2014-12-24 18:11:508477 aSpace1[iSpace1++] = 0x00;
drh1ffd2472015-06-23 02:37:308478 b.szCell[b.nCell]++;
danielk1977ac11ee62005-01-15 12:45:518479 }
8480 }
drh1ffd2472015-06-23 02:37:308481 b.nCell++;
drh8b2f49b2001-06-08 00:21:528482 }
drh8b2f49b2001-06-08 00:21:528483 }
8484
8485 /*
drh1ffd2472015-06-23 02:37:308486 ** Figure out the number of pages needed to hold all b.nCell cells.
drh6019e162001-07-02 17:51:458487 ** Store this number in "k". Also compute szNew[] which is the total
8488 ** size of all cells on the i-th page and cntNew[] which is the index
larrybrbc917382023-06-07 08:40:318489 ** in b.apCell[] of the cell that divides page i from page i+1.
drh1ffd2472015-06-23 02:37:308490 ** cntNew[k] should equal b.nCell.
drh6019e162001-07-02 17:51:458491 **
drh96f5b762004-05-16 16:24:368492 ** Values computed by this block:
8493 **
8494 ** k: The total number of sibling pages
8495 ** szNew[i]: Spaced used on the i-th sibling page.
drh1ffd2472015-06-23 02:37:308496 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
drh96f5b762004-05-16 16:24:368497 ** the right of the i-th sibling page.
8498 ** usableSpace: Number of bytes of space available on each sibling.
larrybrbc917382023-06-07 08:40:318499 **
drh8b2f49b2001-06-08 00:21:528500 */
drh43605152004-05-29 21:46:498501 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh26b7ec82019-02-01 14:50:438502 for(i=k=0; i<nOld; i++, k++){
drh658873b2015-06-22 20:02:048503 MemPage *p = apOld[i];
drh26b7ec82019-02-01 14:50:438504 b.apEnd[k] = p->aDataEnd;
8505 b.ixNx[k] = cntOld[i];
drh9c7e44c2019-02-14 15:27:128506 if( k && b.ixNx[k]==b.ixNx[k-1] ){
8507 k--; /* Omit b.ixNx[] entry for child pages with no cells */
8508 }
drh26b7ec82019-02-01 14:50:438509 if( !leafData ){
8510 k++;
8511 b.apEnd[k] = pParent->aDataEnd;
8512 b.ixNx[k] = cntOld[i]+1;
8513 }
drhb0ea9432019-02-09 21:06:408514 assert( p->nFree>=0 );
drh658873b2015-06-22 20:02:048515 szNew[i] = usableSpace - p->nFree;
drh658873b2015-06-22 20:02:048516 for(j=0; j<p->nOverflow; j++){
8517 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
8518 }
8519 cntNew[i] = cntOld[i];
8520 }
8521 k = nOld;
8522 for(i=0; i<k; i++){
8523 int sz;
8524 while( szNew[i]>usableSpace ){
8525 if( i+1>=k ){
8526 k = i+2;
8527 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
8528 szNew[k-1] = 0;
drh1ffd2472015-06-23 02:37:308529 cntNew[k-1] = b.nCell;
drh658873b2015-06-22 20:02:048530 }
drh1ffd2472015-06-23 02:37:308531 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
drh658873b2015-06-22 20:02:048532 szNew[i] -= sz;
8533 if( !leafData ){
drh1ffd2472015-06-23 02:37:308534 if( cntNew[i]<b.nCell ){
8535 sz = 2 + cachedCellSize(&b, cntNew[i]);
8536 }else{
8537 sz = 0;
8538 }
drh658873b2015-06-22 20:02:048539 }
8540 szNew[i+1] += sz;
8541 cntNew[i]--;
8542 }
drh1ffd2472015-06-23 02:37:308543 while( cntNew[i]<b.nCell ){
8544 sz = 2 + cachedCellSize(&b, cntNew[i]);
drh658873b2015-06-22 20:02:048545 if( szNew[i]+sz>usableSpace ) break;
8546 szNew[i] += sz;
8547 cntNew[i]++;
8548 if( !leafData ){
drh1ffd2472015-06-23 02:37:308549 if( cntNew[i]<b.nCell ){
8550 sz = 2 + cachedCellSize(&b, cntNew[i]);
8551 }else{
8552 sz = 0;
8553 }
drh658873b2015-06-22 20:02:048554 }
8555 szNew[i+1] -= sz;
8556 }
drh1ffd2472015-06-23 02:37:308557 if( cntNew[i]>=b.nCell ){
drh658873b2015-06-22 20:02:048558 k = i+1;
drh672073a2015-06-24 12:07:408559 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
drh658873b2015-06-22 20:02:048560 rc = SQLITE_CORRUPT_BKPT;
8561 goto balance_cleanup;
drh6019e162001-07-02 17:51:458562 }
8563 }
drh96f5b762004-05-16 16:24:368564
8565 /*
8566 ** The packing computed by the previous block is biased toward the siblings
drh2a0df922014-10-30 23:14:568567 ** on the left side (siblings with smaller keys). The left siblings are
8568 ** always nearly full, while the right-most sibling might be nearly empty.
8569 ** The next block of code attempts to adjust the packing of siblings to
8570 ** get a better balance.
drh96f5b762004-05-16 16:24:368571 **
8572 ** This adjustment is more than an optimization. The packing above might
8573 ** be so out of balance as to be illegal. For example, the right-most
8574 ** sibling might be completely empty. This adjustment is not optional.
8575 */
drh6019e162001-07-02 17:51:458576 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:368577 int szRight = szNew[i]; /* Size of sibling on the right */
8578 int szLeft = szNew[i-1]; /* Size of sibling on the left */
8579 int r; /* Index of right-most cell in left sibling */
8580 int d; /* Index of first cell to the left of right sibling */
8581
8582 r = cntNew[i-1] - 1;
8583 d = r + 1 - leafData;
drh008d64c2015-06-23 16:00:248584 (void)cachedCellSize(&b, d);
drh672073a2015-06-24 12:07:408585 do{
drh16635fa2022-11-19 18:43:328586 int szR, szD;
drh1ffd2472015-06-23 02:37:308587 assert( d<nMaxCells );
8588 assert( r<nMaxCells );
drh16635fa2022-11-19 18:43:328589 szR = cachedCellSize(&b, r);
8590 szD = b.szCell[d];
drh1ffd2472015-06-23 02:37:308591 if( szRight!=0
drh16635fa2022-11-19 18:43:328592 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
drh1ffd2472015-06-23 02:37:308593 break;
8594 }
drh16635fa2022-11-19 18:43:328595 szRight += szD + 2;
8596 szLeft -= szR + 2;
drh008d64c2015-06-23 16:00:248597 cntNew[i-1] = r;
drh008d64c2015-06-23 16:00:248598 r--;
8599 d--;
drh672073a2015-06-24 12:07:408600 }while( r>=0 );
drh96f5b762004-05-16 16:24:368601 szNew[i] = szRight;
8602 szNew[i-1] = szLeft;
drh672073a2015-06-24 12:07:408603 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
8604 rc = SQLITE_CORRUPT_BKPT;
8605 goto balance_cleanup;
8606 }
drh6019e162001-07-02 17:51:458607 }
drh09d0deb2005-08-02 17:13:098608
larrybrbc917382023-06-07 08:40:318609 /* Sanity check: For a non-corrupt database file one of the following
drh2a0df922014-10-30 23:14:568610 ** must be true:
8611 ** (1) We found one or more cells (cntNew[0])>0), or
8612 ** (2) pPage is a virtual root page. A virtual root page is when
8613 ** the real root page is page 1 and we are the only child of
8614 ** that page.
drh09d0deb2005-08-02 17:13:098615 */
drh2a0df922014-10-30 23:14:568616 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
drh2e89f1c2023-04-01 12:22:578617 TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n",
dan33ea4862014-10-09 19:35:378618 apOld[0]->pgno, apOld[0]->nCell,
8619 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
8620 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
danielk1977e5765212009-06-17 11:13:288621 ));
8622
drh8b2f49b2001-06-08 00:21:528623 /*
drh6b308672002-07-08 02:16:378624 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:528625 */
danielk1977a50d9aa2009-06-08 14:49:458626 pageFlags = apOld[0]->aData[0];
drh14acc042001-06-10 19:56:588627 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:388628 MemPage *pNew;
drh6b308672002-07-08 02:16:378629 if( i<nOld ){
drhda200cc2004-05-09 11:51:388630 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:378631 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:268632 rc = sqlite3PagerWrite(pNew->pDbPage);
drhf5345442007-04-09 12:45:028633 nNew++;
drh41d26392021-06-20 22:17:498634 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
8635 && rc==SQLITE_OK
8636 ){
drh9e673ac2021-02-01 12:39:508637 rc = SQLITE_CORRUPT_BKPT;
8638 }
danielk197728129562005-01-11 10:25:068639 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:378640 }else{
drh7aa8f852006-03-28 00:24:448641 assert( i>0 );
dan428c2182012-08-06 18:50:118642 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
drh6b308672002-07-08 02:16:378643 if( rc ) goto balance_cleanup;
dan33ea4862014-10-09 19:35:378644 zeroPage(pNew, pageFlags);
drhda200cc2004-05-09 11:51:388645 apNew[i] = pNew;
drhf5345442007-04-09 12:45:028646 nNew++;
drh1ffd2472015-06-23 02:37:308647 cntOld[i] = b.nCell;
danielk19774dbaa892009-06-16 16:50:228648
8649 /* Set the pointer-map entry for the new sibling page. */
drhe7d53842022-11-21 14:13:108650 if( ISAUTOVACUUM(pBt) ){
drh98add2e2009-07-20 17:11:498651 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
danielk19774dbaa892009-06-16 16:50:228652 if( rc!=SQLITE_OK ){
8653 goto balance_cleanup;
8654 }
8655 }
drh6b308672002-07-08 02:16:378656 }
drh8b2f49b2001-06-08 00:21:528657 }
8658
8659 /*
larrybrbc917382023-06-07 08:40:318660 ** Reassign page numbers so that the new pages are in ascending order.
dan33ea4862014-10-09 19:35:378661 ** This helps to keep entries in the disk file in order so that a scan
larrybrbc917382023-06-07 08:40:318662 ** of the table is closer to a linear scan through the file. That in turn
dan33ea4862014-10-09 19:35:378663 ** helps the operating system to deliver pages from the disk more rapidly.
drhf9ffac92002-03-02 19:00:318664 **
drh9c3a1142022-08-31 15:04:428665 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
8666 ** (5), that is not a performance concern.
drhf9ffac92002-03-02 19:00:318667 **
larrybrbc917382023-06-07 08:40:318668 ** When NB==3, this one optimization makes the database about 25% faster
dan33ea4862014-10-09 19:35:378669 ** for large insertions and deletions.
drhf9ffac92002-03-02 19:00:318670 */
dan33ea4862014-10-09 19:35:378671 for(i=0; i<nNew; i++){
drh9c3a1142022-08-31 15:04:428672 aPgno[i] = apNew[i]->pgno;
8673 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
8674 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
dan33ea4862014-10-09 19:35:378675 }
drh9c3a1142022-08-31 15:04:428676 for(i=0; i<nNew-1; i++){
8677 int iB = i;
8678 for(j=i+1; j<nNew; j++){
8679 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
drhf9ffac92002-03-02 19:00:318680 }
drh9c3a1142022-08-31 15:04:428681
8682 /* If apNew[i] has a page number that is bigger than any of the
8683 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
8684 ** entry that has the smallest page number (which we know to be
8685 ** entry apNew[iB]).
8686 */
8687 if( iB!=i ){
8688 Pgno pgnoA = apNew[i]->pgno;
8689 Pgno pgnoB = apNew[iB]->pgno;
8690 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
8691 u16 fgA = apNew[i]->pDbPage->flags;
8692 u16 fgB = apNew[iB]->pDbPage->flags;
8693 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
8694 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
8695 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
8696 apNew[i]->pgno = pgnoB;
8697 apNew[iB]->pgno = pgnoA;
drhf9ffac92002-03-02 19:00:318698 }
8699 }
dan33ea4862014-10-09 19:35:378700
drh2e89f1c2023-04-01 12:22:578701 TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) "
8702 "%u(%u nc=%u) %u(%u nc=%u)\n",
dan33ea4862014-10-09 19:35:378703 apNew[0]->pgno, szNew[0], cntNew[0],
danielk19774dbaa892009-06-16 16:50:228704 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
dan33ea4862014-10-09 19:35:378705 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
danielk19774dbaa892009-06-16 16:50:228706 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
dan33ea4862014-10-09 19:35:378707 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
danielk19774dbaa892009-06-16 16:50:228708 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
dan33ea4862014-10-09 19:35:378709 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
8710 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
8711 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
8712 ));
danielk19774dbaa892009-06-16 16:50:228713
8714 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
drh55f66b32019-07-16 19:44:328715 assert( nNew>=1 && nNew<=ArraySize(apNew) );
8716 assert( apNew[nNew-1]!=0 );
danielk19774dbaa892009-06-16 16:50:228717 put4byte(pRight, apNew[nNew-1]->pgno);
drh24cd67e2004-05-10 16:18:478718
dan33ea4862014-10-09 19:35:378719 /* If the sibling pages are not leaves, ensure that the right-child pointer
larrybrbc917382023-06-07 08:40:318720 ** of the right-most new sibling page is set to the value that was
dan33ea4862014-10-09 19:35:378721 ** originally in the same field of the right-most old sibling page. */
8722 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
drhe77e5892025-06-26 18:57:208723 MemPage *pOld;
8724 if( nNew>nOld ){
8725 pOld = apNew[nOld-1];
8726 }else{
8727 pOld = apOld[nOld-1];
8728 }
dan33ea4862014-10-09 19:35:378729 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
8730 }
danielk1977ac11ee62005-01-15 12:45:518731
larrybrbc917382023-06-07 08:40:318732 /* Make any required updates to pointer map entries associated with
dan33ea4862014-10-09 19:35:378733 ** cells stored on sibling pages following the balance operation. Pointer
8734 ** map entries associated with divider cells are set by the insertCell()
8735 ** routine. The associated pointer map entries are:
8736 **
8737 ** a) if the cell contains a reference to an overflow chain, the
8738 ** entry associated with the first page in the overflow chain, and
8739 **
8740 ** b) if the sibling pages are not leaves, the child page associated
8741 ** with the cell.
8742 **
larrybrbc917382023-06-07 08:40:318743 ** If the sibling pages are not leaves, then the pointer map entry
8744 ** associated with the right-child of each sibling may also need to be
8745 ** updated. This happens below, after the sibling pages have been
dan33ea4862014-10-09 19:35:378746 ** populated, not here.
danielk1977ac11ee62005-01-15 12:45:518747 */
drhe7d53842022-11-21 14:13:108748 if( ISAUTOVACUUM(pBt) ){
drh0f1bf4c2019-01-13 20:17:218749 MemPage *pOld;
8750 MemPage *pNew = pOld = apNew[0];
dan33ea4862014-10-09 19:35:378751 int cntOldNext = pNew->nCell + pNew->nOverflow;
dan33ea4862014-10-09 19:35:378752 int iNew = 0;
8753 int iOld = 0;
danielk1977ac11ee62005-01-15 12:45:518754
drh1ffd2472015-06-23 02:37:308755 for(i=0; i<b.nCell; i++){
8756 u8 *pCell = b.apCell[i];
drh9c7e44c2019-02-14 15:27:128757 while( i==cntOldNext ){
8758 iOld++;
8759 assert( iOld<nNew || iOld<nOld );
drhdd2d9a32019-05-07 17:47:438760 assert( iOld>=0 && iOld<NB );
drh9c7e44c2019-02-14 15:27:128761 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
dan33ea4862014-10-09 19:35:378762 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
drh4b70f112004-05-02 21:12:198763 }
dan33ea4862014-10-09 19:35:378764 if( i==cntNew[iNew] ){
8765 pNew = apNew[++iNew];
8766 if( !leafData ) continue;
8767 }
danielk197785d90ca2008-07-19 14:25:158768
dan33ea4862014-10-09 19:35:378769 /* Cell pCell is destined for new sibling page pNew. Originally, it
larrybrbc917382023-06-07 08:40:318770 ** was either part of sibling page iOld (possibly an overflow cell),
dan33ea4862014-10-09 19:35:378771 ** or else the divider cell to the left of sibling page iOld. So,
8772 ** if sibling page iOld had the same page number as pNew, and if
8773 ** pCell really was a part of sibling page iOld (not a divider or
8774 ** overflow cell), we can skip updating the pointer map entries. */
drhd52d52b2014-12-06 02:05:448775 if( iOld>=nNew
8776 || pNew->pgno!=aPgno[iOld]
drh9c7e44c2019-02-14 15:27:128777 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
drhd52d52b2014-12-06 02:05:448778 ){
dan33ea4862014-10-09 19:35:378779 if( !leafCorrection ){
8780 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
8781 }
drh1ffd2472015-06-23 02:37:308782 if( cachedCellSize(&b,i)>pNew->minLocal ){
drh0f1bf4c2019-01-13 20:17:218783 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
danielk1977ac11ee62005-01-15 12:45:518784 }
drhea82b372015-06-23 21:35:288785 if( rc ) goto balance_cleanup;
drh43605152004-05-29 21:46:498786 }
drh14acc042001-06-10 19:56:588787 }
8788 }
dan33ea4862014-10-09 19:35:378789
8790 /* Insert new divider cells into pParent. */
8791 for(i=0; i<nNew-1; i++){
8792 u8 *pCell;
8793 u8 *pTemp;
8794 int sz;
drhc3c23f32021-05-06 11:02:558795 u8 *pSrcEnd;
dan33ea4862014-10-09 19:35:378796 MemPage *pNew = apNew[i];
8797 j = cntNew[i];
8798
8799 assert( j<nMaxCells );
drh1ffd2472015-06-23 02:37:308800 assert( b.apCell[j]!=0 );
8801 pCell = b.apCell[j];
8802 sz = b.szCell[j] + leafCorrection;
dan33ea4862014-10-09 19:35:378803 pTemp = &aOvflSpace[iOvflSpace];
8804 if( !pNew->leaf ){
8805 memcpy(&pNew->aData[8], pCell, 4);
8806 }else if( leafData ){
larrybrbc917382023-06-07 08:40:318807 /* If the tree is a leaf-data tree, and the siblings are leaves,
8808 ** then there is no divider cell in b.apCell[]. Instead, the divider
8809 ** cell consists of the integer key for the right-most cell of
dan33ea4862014-10-09 19:35:378810 ** the sibling-page assembled above only.
8811 */
8812 CellInfo info;
8813 j--;
drh1ffd2472015-06-23 02:37:308814 pNew->xParseCell(pNew, b.apCell[j], &info);
dan33ea4862014-10-09 19:35:378815 pCell = pTemp;
8816 sz = 4 + putVarint(&pCell[4], info.nKey);
8817 pTemp = 0;
8818 }else{
8819 pCell -= 4;
8820 /* Obscure case for non-leaf-data trees: If the cell at pCell was
8821 ** previously stored on a leaf node, and its reported size was 4
larrybrbc917382023-06-07 08:40:318822 ** bytes, then it may actually be smaller than this
dan33ea4862014-10-09 19:35:378823 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
larrybrbc917382023-06-07 08:40:318824 ** any cell). But it is important to pass the correct size to
dan33ea4862014-10-09 19:35:378825 ** insertCell(), so reparse the cell now.
8826 **
drhc1fb2b82016-03-09 03:29:278827 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
8828 ** and WITHOUT ROWID tables with exactly one column which is the
8829 ** primary key.
dan33ea4862014-10-09 19:35:378830 */
drh1ffd2472015-06-23 02:37:308831 if( b.szCell[j]==4 ){
dan33ea4862014-10-09 19:35:378832 assert(leafCorrection==4);
drh25ada072015-06-19 15:07:148833 sz = pParent->xCellSize(pParent, pCell);
dan33ea4862014-10-09 19:35:378834 }
8835 }
8836 iOvflSpace += sz;
8837 assert( sz<=pBt->maxLocal+23 );
8838 assert( iOvflSpace <= (int)pBt->pageSize );
drh09d8e692024-05-27 15:13:498839 assert( b.ixNx[NB*2-1]>j );
8840 for(k=0; b.ixNx[k]<=j; k++){}
drhc3c23f32021-05-06 11:02:558841 pSrcEnd = b.apEnd[k];
drhbc6d9492023-07-13 14:49:398842 if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz) ){
drhc3c23f32021-05-06 11:02:558843 rc = SQLITE_CORRUPT_BKPT;
8844 goto balance_cleanup;
8845 }
drhb53d8fa2022-11-21 15:55:578846 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
drhd2cfbea2019-05-08 03:34:538847 if( rc!=SQLITE_OK ) goto balance_cleanup;
dan33ea4862014-10-09 19:35:378848 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
8849 }
8850
8851 /* Now update the actual sibling pages. The order in which they are updated
8852 ** is important, as this code needs to avoid disrupting any page from which
8853 ** cells may still to be read. In practice, this means:
8854 **
drhd836d422014-10-31 14:26:368855 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
8856 ** then it is not safe to update page apNew[iPg] until after
8857 ** the left-hand sibling apNew[iPg-1] has been updated.
dan33ea4862014-10-09 19:35:378858 **
drhd836d422014-10-31 14:26:368859 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
8860 ** then it is not safe to update page apNew[iPg] until after
8861 ** the right-hand sibling apNew[iPg+1] has been updated.
dan33ea4862014-10-09 19:35:378862 **
8863 ** If neither of the above apply, the page is safe to update.
drhd836d422014-10-31 14:26:368864 **
8865 ** The iPg value in the following loop starts at nNew-1 goes down
8866 ** to 0, then back up to nNew-1 again, thus making two passes over
8867 ** the pages. On the initial downward pass, only condition (1) above
8868 ** needs to be tested because (2) will always be true from the previous
8869 ** step. On the upward pass, both conditions are always true, so the
8870 ** upwards pass simply processes pages that were missed on the downward
8871 ** pass.
dan33ea4862014-10-09 19:35:378872 */
drhbec021b2014-10-31 12:22:008873 for(i=1-nNew; i<nNew; i++){
8874 int iPg = i<0 ? -i : i;
drhbec021b2014-10-31 12:22:008875 assert( iPg>=0 && iPg<nNew );
drh56a41072023-06-16 14:39:218876 assert( iPg>=1 || i>=0 );
8877 assert( iPg<ArraySize(cntOld) );
drhd836d422014-10-31 14:26:368878 if( abDone[iPg] ) continue; /* Skip pages already processed */
8879 if( i>=0 /* On the upwards pass, or... */
8880 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
dan33ea4862014-10-09 19:35:378881 ){
dan09c68402014-10-11 20:00:248882 int iNew;
8883 int iOld;
8884 int nNewCell;
8885
drhd836d422014-10-31 14:26:368886 /* Verify condition (1): If cells are moving left, update iPg
8887 ** only after iPg-1 has already been updated. */
8888 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
8889
8890 /* Verify condition (2): If cells are moving right, update iPg
8891 ** only after iPg+1 has already been updated. */
8892 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
8893
dan09c68402014-10-11 20:00:248894 if( iPg==0 ){
8895 iNew = iOld = 0;
8896 nNewCell = cntNew[0];
8897 }else{
drh1ffd2472015-06-23 02:37:308898 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
dan09c68402014-10-11 20:00:248899 iNew = cntNew[iPg-1] + !leafData;
8900 nNewCell = cntNew[iPg] - iNew;
8901 }
8902
drh1ffd2472015-06-23 02:37:308903 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
drh658873b2015-06-22 20:02:048904 if( rc ) goto balance_cleanup;
drhd836d422014-10-31 14:26:368905 abDone[iPg]++;
dand7b545b2014-10-13 18:03:278906 apNew[iPg]->nFree = usableSpace-szNew[iPg];
dan09c68402014-10-11 20:00:248907 assert( apNew[iPg]->nOverflow==0 );
8908 assert( apNew[iPg]->nCell==nNewCell );
dan33ea4862014-10-09 19:35:378909 }
8910 }
drhd836d422014-10-31 14:26:368911
8912 /* All pages have been processed exactly once */
dan33ea4862014-10-09 19:35:378913 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
8914
drh7aa8f852006-03-28 00:24:448915 assert( nOld>0 );
8916 assert( nNew>0 );
drh14acc042001-06-10 19:56:588917
danielk197713bd99f2009-06-24 05:40:348918 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
8919 /* The root page of the b-tree now contains no cells. The only sibling
8920 ** page is the right-child of the parent. Copy the contents of the
8921 ** child page into the parent, decreasing the overall height of the
8922 ** b-tree structure by one. This is described as the "balance-shallower"
8923 ** sub-algorithm in some documentation.
8924 **
larrybrbc917382023-06-07 08:40:318925 ** If this is an auto-vacuum database, the call to copyNodeContent()
8926 ** sets all pointer-map entries corresponding to database image pages
danielk197713bd99f2009-06-24 05:40:348927 ** for which the pointer is stored within the content being copied.
8928 **
drh768f2902014-10-31 02:51:418929 ** It is critical that the child page be defragmented before being
8930 ** copied into the parent, because if the parent is page 1 then it will
8931 ** by smaller than the child due to the database header, and so all the
8932 ** free space needs to be up front.
8933 */
drh9b5351d2015-09-30 14:19:088934 assert( nNew==1 || CORRUPT_DB );
dan3b2ede12017-02-25 16:24:028935 rc = defragmentPage(apNew[0], -1);
drh768f2902014-10-31 02:51:418936 testcase( rc!=SQLITE_OK );
larrybrbc917382023-06-07 08:40:318937 assert( apNew[0]->nFree ==
drh1c960262019-03-25 18:44:088938 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
8939 - apNew[0]->nCell*2)
drh768f2902014-10-31 02:51:418940 || rc!=SQLITE_OK
danielk197713bd99f2009-06-24 05:40:348941 );
drhc314dc72009-07-21 11:52:348942 copyNodeContent(apNew[0], pParent, &rc);
8943 freePage(apNew[0], &rc);
drhe7d53842022-11-21 14:13:108944 }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){
dan33ea4862014-10-09 19:35:378945 /* Fix the pointer map entries associated with the right-child of each
8946 ** sibling page. All other pointer map entries have already been taken
8947 ** care of. */
8948 for(i=0; i<nNew; i++){
8949 u32 key = get4byte(&apNew[i]->aData[8]);
8950 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
danielk19774dbaa892009-06-16 16:50:228951 }
dan33ea4862014-10-09 19:35:378952 }
danielk19774dbaa892009-06-16 16:50:228953
dan33ea4862014-10-09 19:35:378954 assert( pParent->isInit );
drh2e89f1c2023-04-01 12:22:578955 TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n",
drh1ffd2472015-06-23 02:37:308956 nOld, nNew, b.nCell));
danielk19774dbaa892009-06-16 16:50:228957
dan33ea4862014-10-09 19:35:378958 /* Free any old pages that were not reused as new pages.
8959 */
8960 for(i=nNew; i<nOld; i++){
8961 freePage(apOld[i], &rc);
8962 }
danielk19774dbaa892009-06-16 16:50:228963
8964#if 0
drhe7d53842022-11-21 14:13:108965 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
danielk19774dbaa892009-06-16 16:50:228966 /* The ptrmapCheckPages() contains assert() statements that verify that
larrybrbc917382023-06-07 08:40:318967 ** all pointer map pages are set correctly. This is helpful while
danielk19774dbaa892009-06-16 16:50:228968 ** debugging. This is usually disabled because a corrupt database may
8969 ** cause an assert() statement to fail. */
8970 ptrmapCheckPages(apNew, nNew);
8971 ptrmapCheckPages(&pParent, 1);
danielk19774dbaa892009-06-16 16:50:228972 }
dan33ea4862014-10-09 19:35:378973#endif
danielk1977cd581a72009-06-23 15:43:398974
drh8b2f49b2001-06-08 00:21:528975 /*
drh14acc042001-06-10 19:56:588976 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:528977 */
drh14acc042001-06-10 19:56:588978balance_cleanup:
drhb2a0f752017-08-28 15:51:358979 sqlite3StackFree(0, b.apCell);
drh8b2f49b2001-06-08 00:21:528980 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:328981 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:528982 }
drh14acc042001-06-10 19:56:588983 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:328984 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:528985 }
danielk1977eaa06f62008-09-18 17:34:448986
drh8b2f49b2001-06-08 00:21:528987 return rc;
8988}
8989
drh43605152004-05-29 21:46:498990
8991/*
danielk1977a50d9aa2009-06-08 14:49:458992** This function is called when the root page of a b-tree structure is
8993** overfull (has one or more overflow pages).
drh43605152004-05-29 21:46:498994**
danielk1977a50d9aa2009-06-08 14:49:458995** A new child page is allocated and the contents of the current root
8996** page, including overflow cells, are copied into the child. The root
larrybrbc917382023-06-07 08:40:318997** page is then overwritten to make it an empty page with the right-child
danielk1977a50d9aa2009-06-08 14:49:458998** pointer pointing to the new page.
8999**
larrybrbc917382023-06-07 08:40:319000** Before returning, all pointer-map entries corresponding to pages
danielk1977a50d9aa2009-06-08 14:49:459001** that the new child-page now contains pointers to are updated. The
9002** entry corresponding to the new right-child pointer of the root
9003** page is also updated.
9004**
larrybrbc917382023-06-07 08:40:319005** If successful, *ppChild is set to contain a reference to the child
danielk1977a50d9aa2009-06-08 14:49:459006** page and SQLITE_OK is returned. In this case the caller is required
9007** to call releasePage() on *ppChild exactly once. If an error occurs,
9008** an error code is returned and *ppChild is set to 0.
drh43605152004-05-29 21:46:499009*/
danielk1977a50d9aa2009-06-08 14:49:459010static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
9011 int rc; /* Return value from subprocedures */
9012 MemPage *pChild = 0; /* Pointer to a new child page */
shane5eff7cf2009-08-10 03:57:589013 Pgno pgnoChild = 0; /* Page number of the new child page */
danielk1977a50d9aa2009-06-08 14:49:459014 BtShared *pBt = pRoot->pBt; /* The BTree */
drh43605152004-05-29 21:46:499015
danielk1977a50d9aa2009-06-08 14:49:459016 assert( pRoot->nOverflow>0 );
drh1fee73e2007-08-29 04:00:579017 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977bc2ca9e2008-11-13 14:28:289018
larrybrbc917382023-06-07 08:40:319019 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
danielk1977a50d9aa2009-06-08 14:49:459020 ** page that will become the new right-child of pPage. Copy the contents
9021 ** of the node stored on pRoot into the new child page.
9022 */
drh98add2e2009-07-20 17:11:499023 rc = sqlite3PagerWrite(pRoot->pDbPage);
9024 if( rc==SQLITE_OK ){
9025 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
drhc314dc72009-07-21 11:52:349026 copyNodeContent(pRoot, pChild, &rc);
drhe7d53842022-11-21 14:13:109027 if( ISAUTOVACUUM(pBt) ){
drhc314dc72009-07-21 11:52:349028 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
drh98add2e2009-07-20 17:11:499029 }
9030 }
9031 if( rc ){
danielk1977a50d9aa2009-06-08 14:49:459032 *ppChild = 0;
danielk197771d5d2c2008-09-29 11:49:479033 releasePage(pChild);
danielk1977a50d9aa2009-06-08 14:49:459034 return rc;
danielk197771d5d2c2008-09-29 11:49:479035 }
danielk1977a50d9aa2009-06-08 14:49:459036 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
9037 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drh12fe9a02019-02-19 16:42:549038 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
danielk197771d5d2c2008-09-29 11:49:479039
drh2e89f1c2023-04-01 12:22:579040 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
danielk1977a50d9aa2009-06-08 14:49:459041
9042 /* Copy the overflow cells from pRoot to pChild */
drh2cbd78b2012-02-02 19:37:189043 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
9044 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
9045 memcpy(pChild->apOvfl, pRoot->apOvfl,
9046 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
danielk1977a50d9aa2009-06-08 14:49:459047 pChild->nOverflow = pRoot->nOverflow;
danielk1977a50d9aa2009-06-08 14:49:459048
9049 /* Zero the contents of pRoot. Then install pChild as the right-child. */
9050 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
9051 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
9052
9053 *ppChild = pChild;
9054 return SQLITE_OK;
drh43605152004-05-29 21:46:499055}
9056
9057/*
drha2d50282019-12-23 18:02:159058** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
9059** on the same B-tree as pCur.
9060**
drh87463962021-10-05 22:51:269061** This can occur if a database is corrupt with two or more SQL tables
drha2d50282019-12-23 18:02:159062** pointing to the same b-tree. If an insert occurs on one SQL table
9063** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
9064** table linked to the same b-tree. If the secondary insert causes a
9065** rebalance, that can change content out from under the cursor on the
9066** first SQL table, violating invariants on the first insert.
9067*/
9068static int anotherValidCursor(BtCursor *pCur){
9069 BtCursor *pOther;
9070 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
9071 if( pOther!=pCur
9072 && pOther->eState==CURSOR_VALID
9073 && pOther->pPage==pCur->pPage
9074 ){
drh50f9caf2024-02-02 18:42:099075 return SQLITE_CORRUPT_PAGE(pCur->pPage);
drha2d50282019-12-23 18:02:159076 }
9077 }
9078 return SQLITE_OK;
9079}
9080
9081/*
danielk197771d5d2c2008-09-29 11:49:479082** The page that pCur currently points to has just been modified in
9083** some way. This function figures out if this modification means the
larrybrbc917382023-06-07 08:40:319084** tree needs to be balanced, and if so calls the appropriate balancing
danielk1977a50d9aa2009-06-08 14:49:459085** routine. Balancing routines are:
9086**
9087** balance_quick()
danielk1977a50d9aa2009-06-08 14:49:459088** balance_deeper()
9089** balance_nonroot()
drh43605152004-05-29 21:46:499090*/
danielk1977a50d9aa2009-06-08 14:49:459091static int balance(BtCursor *pCur){
drh43605152004-05-29 21:46:499092 int rc = SQLITE_OK;
danielk1977a50d9aa2009-06-08 14:49:459093 u8 aBalanceQuickSpace[13];
9094 u8 *pFree = 0;
danielk197771d5d2c2008-09-29 11:49:479095
drhcc5f8a42016-02-06 22:32:069096 VVA_ONLY( int balance_quick_called = 0 );
9097 VVA_ONLY( int balance_deeper_called = 0 );
danielk1977a50d9aa2009-06-08 14:49:459098
9099 do {
dan01fd42b2019-07-13 09:55:339100 int iPage;
drh352a35a2017-08-15 03:46:479101 MemPage *pPage = pCur->pPage;
danielk1977a50d9aa2009-06-08 14:49:459102
drha941ff72019-02-12 00:58:109103 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
drhc4c0ff82022-03-31 16:09:139104 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
drhde948482022-03-29 13:16:329105 /* No rebalance required as long as:
9106 ** (1) There are no overflow cells
9107 ** (2) The amount of free space on the page is less than 2/3rds of
9108 ** the total usable space on the page. */
dan01fd42b2019-07-13 09:55:339109 break;
9110 }else if( (iPage = pCur->iPage)==0 ){
drha2d50282019-12-23 18:02:159111 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
danielk1977a50d9aa2009-06-08 14:49:459112 /* The root page of the b-tree is overfull. In this case call the
9113 ** balance_deeper() function to create a new child for the root-page
9114 ** and copy the current contents of the root-page to it. The
9115 ** next iteration of the do-loop will balance the child page.
larrybrbc917382023-06-07 08:40:319116 */
drhcc5f8a42016-02-06 22:32:069117 assert( balance_deeper_called==0 );
9118 VVA_ONLY( balance_deeper_called++ );
danielk1977a50d9aa2009-06-08 14:49:459119 rc = balance_deeper(pPage, &pCur->apPage[1]);
9120 if( rc==SQLITE_OK ){
9121 pCur->iPage = 1;
drh75e96b32017-04-01 00:20:069122 pCur->ix = 0;
danielk1977a50d9aa2009-06-08 14:49:459123 pCur->aiIdx[0] = 0;
drh352a35a2017-08-15 03:46:479124 pCur->apPage[0] = pPage;
9125 pCur->pPage = pCur->apPage[1];
9126 assert( pCur->pPage->nOverflow );
danielk1977a50d9aa2009-06-08 14:49:459127 }
danielk1977a50d9aa2009-06-08 14:49:459128 }else{
danielk1977a50d9aa2009-06-08 14:49:459129 break;
9130 }
danad658b22022-09-30 20:15:219131 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
9132 /* The page being written is not a root page, and there is currently
larrybrbc917382023-06-07 08:40:319133 ** more than one reference to it. This only happens if the page is one
danad658b22022-09-30 20:15:219134 ** of its own ancestor pages. Corruption. */
drh50f9caf2024-02-02 18:42:099135 rc = SQLITE_CORRUPT_PAGE(pPage);
danielk1977a50d9aa2009-06-08 14:49:459136 }else{
9137 MemPage * const pParent = pCur->apPage[iPage-1];
9138 int const iIdx = pCur->aiIdx[iPage-1];
9139
9140 rc = sqlite3PagerWrite(pParent->pDbPage);
drh68133502019-02-11 17:22:309141 if( rc==SQLITE_OK && pParent->nFree<0 ){
9142 rc = btreeComputeFreeSpace(pParent);
9143 }
danielk1977a50d9aa2009-06-08 14:49:459144 if( rc==SQLITE_OK ){
9145#ifndef SQLITE_OMIT_QUICKBALANCE
drh3e28ff52014-09-24 00:59:089146 if( pPage->intKeyLeaf
danielk1977a50d9aa2009-06-08 14:49:459147 && pPage->nOverflow==1
drh2cbd78b2012-02-02 19:37:189148 && pPage->aiOvfl[0]==pPage->nCell
danielk1977a50d9aa2009-06-08 14:49:459149 && pParent->pgno!=1
9150 && pParent->nCell==iIdx
9151 ){
9152 /* Call balance_quick() to create a new sibling of pPage on which
9153 ** to store the overflow cell. balance_quick() inserts a new cell
9154 ** into pParent, which may cause pParent overflow. If this
larrybrbc917382023-06-07 08:40:319155 ** happens, the next iteration of the do-loop will balance pParent
danielk1977a50d9aa2009-06-08 14:49:459156 ** use either balance_nonroot() or balance_deeper(). Until this
9157 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
larrybrbc917382023-06-07 08:40:319158 ** buffer.
danielk1977a50d9aa2009-06-08 14:49:459159 **
9160 ** The purpose of the following assert() is to check that only a
9161 ** single call to balance_quick() is made for each call to this
9162 ** function. If this were not verified, a subtle bug involving reuse
9163 ** of the aBalanceQuickSpace[] might sneak in.
9164 */
larrybrbc917382023-06-07 08:40:319165 assert( balance_quick_called==0 );
drhcc5f8a42016-02-06 22:32:069166 VVA_ONLY( balance_quick_called++ );
danielk1977a50d9aa2009-06-08 14:49:459167 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
9168 }else
9169#endif
9170 {
9171 /* In this case, call balance_nonroot() to redistribute cells
9172 ** between pPage and up to 2 of its sibling pages. This involves
9173 ** modifying the contents of pParent, which may cause pParent to
9174 ** become overfull or underfull. The next iteration of the do-loop
9175 ** will balance the parent page to correct this.
larrybrbc917382023-06-07 08:40:319176 **
danielk1977a50d9aa2009-06-08 14:49:459177 ** If the parent page becomes overfull, the overflow cell or cells
larrybrbc917382023-06-07 08:40:319178 ** are stored in the pSpace buffer allocated immediately below.
danielk1977a50d9aa2009-06-08 14:49:459179 ** A subsequent iteration of the do-loop will deal with this by
9180 ** calling balance_nonroot() (balance_deeper() may be called first,
9181 ** but it doesn't deal with overflow cells - just moves them to a
larrybrbc917382023-06-07 08:40:319182 ** different page). Once this subsequent call to balance_nonroot()
danielk1977a50d9aa2009-06-08 14:49:459183 ** has completed, it is safe to release the pSpace buffer used by
larrybrbc917382023-06-07 08:40:319184 ** the previous call, as the overflow cell data will have been
danielk1977a50d9aa2009-06-08 14:49:459185 ** copied either into the body of a database page or into the new
9186 ** pSpace buffer passed to the latter call to balance_nonroot().
9187 */
9188 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
drhe0997b32015-03-20 14:57:509189 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
9190 pCur->hints&BTREE_BULKLOAD);
danielk1977a50d9aa2009-06-08 14:49:459191 if( pFree ){
larrybrbc917382023-06-07 08:40:319192 /* If pFree is not NULL, it points to the pSpace buffer used
danielk1977a50d9aa2009-06-08 14:49:459193 ** by a previous call to balance_nonroot(). Its contents are
larrybrbc917382023-06-07 08:40:319194 ** now stored either on real database pages or within the
danielk1977a50d9aa2009-06-08 14:49:459195 ** new pSpace buffer, so it may be safely freed here. */
9196 sqlite3PageFree(pFree);
9197 }
9198
danielk19774dbaa892009-06-16 16:50:229199 /* The pSpace buffer will be freed after the next call to
9200 ** balance_nonroot(), or just before this function returns, whichever
9201 ** comes first. */
danielk1977a50d9aa2009-06-08 14:49:459202 pFree = pSpace;
danielk1977a50d9aa2009-06-08 14:49:459203 }
9204 }
9205
9206 pPage->nOverflow = 0;
9207
9208 /* The next iteration of the do-loop balances the parent page. */
9209 releasePage(pPage);
9210 pCur->iPage--;
drhcbd33492015-03-25 13:06:549211 assert( pCur->iPage>=0 );
drh352a35a2017-08-15 03:46:479212 pCur->pPage = pCur->apPage[pCur->iPage];
drh43605152004-05-29 21:46:499213 }
danielk1977a50d9aa2009-06-08 14:49:459214 }while( rc==SQLITE_OK );
9215
9216 if( pFree ){
9217 sqlite3PageFree(pFree);
drh43605152004-05-29 21:46:499218 }
9219 return rc;
9220}
9221
drh3de5d162018-05-03 03:59:029222/* Overwrite content from pX into pDest. Only do the write if the
9223** content is different from what is already there.
9224*/
9225static int btreeOverwriteContent(
9226 MemPage *pPage, /* MemPage on which writing will occur */
9227 u8 *pDest, /* Pointer to the place to start writing */
9228 const BtreePayload *pX, /* Source of data to write */
9229 int iOffset, /* Offset of first byte to write */
9230 int iAmt /* Number of bytes to be written */
9231){
9232 int nData = pX->nData - iOffset;
9233 if( nData<=0 ){
larrybr55be2162023-06-07 17:03:229234 /* Overwriting with zeros */
drh3de5d162018-05-03 03:59:029235 int i;
9236 for(i=0; i<iAmt && pDest[i]==0; i++){}
9237 if( i<iAmt ){
9238 int rc = sqlite3PagerWrite(pPage->pDbPage);
9239 if( rc ) return rc;
9240 memset(pDest + i, 0, iAmt - i);
9241 }
9242 }else{
9243 if( nData<iAmt ){
9244 /* Mixed read data and zeros at the end. Make a recursive call
9245 ** to write the zeros then fall through to write the real data */
drhd5aa9262018-05-03 16:56:069246 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
9247 iAmt-nData);
9248 if( rc ) return rc;
drh3de5d162018-05-03 03:59:029249 iAmt = nData;
9250 }
9251 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
9252 int rc = sqlite3PagerWrite(pPage->pDbPage);
9253 if( rc ) return rc;
drh55469bb2019-01-24 13:36:479254 /* In a corrupt database, it is possible for the source and destination
9255 ** buffers to overlap. This is harmless since the database is already
9256 ** corrupt but it does cause valgrind and ASAN warnings. So use
9257 ** memmove(). */
9258 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
drh3de5d162018-05-03 03:59:029259 }
9260 }
9261 return SQLITE_OK;
9262}
9263
9264/*
9265** Overwrite the cell that cursor pCur is pointing to with fresh content
drh1eb88d62023-01-03 15:11:019266** contained in pX. In this variant, pCur is pointing to an overflow
9267** cell.
drh3de5d162018-05-03 03:59:029268*/
drh1eb88d62023-01-03 15:11:019269static SQLITE_NOINLINE int btreeOverwriteOverflowCell(
larrybr55be2162023-06-07 17:03:229270 BtCursor *pCur, /* Cursor pointing to cell to overwrite */
drh1eb88d62023-01-03 15:11:019271 const BtreePayload *pX /* Content to write into the cell */
9272){
drh3de5d162018-05-03 03:59:029273 int iOffset; /* Next byte of pX->pData to write */
9274 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
9275 int rc; /* Return code */
9276 MemPage *pPage = pCur->pPage; /* Page being written */
9277 BtShared *pBt; /* Btree */
9278 Pgno ovflPgno; /* Next overflow page to write */
9279 u32 ovflPageSize; /* Size to write on overflow page */
9280
drh1eb88d62023-01-03 15:11:019281 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
9282
drh3de5d162018-05-03 03:59:029283 /* Overwrite the local portion first */
9284 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
9285 0, pCur->info.nLocal);
9286 if( rc ) return rc;
drh3de5d162018-05-03 03:59:029287
9288 /* Now overwrite the overflow pages */
9289 iOffset = pCur->info.nLocal;
drh30f7a252018-05-07 11:29:599290 assert( nTotal>=0 );
9291 assert( iOffset>=0 );
drh3de5d162018-05-03 03:59:029292 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
9293 pBt = pPage->pBt;
9294 ovflPageSize = pBt->usableSize - 4;
9295 do{
9296 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
9297 if( rc ) return rc;
drhf9241a52021-11-11 16:26:469298 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
drh50f9caf2024-02-02 18:42:099299 rc = SQLITE_CORRUPT_PAGE(pPage);
drh3de5d162018-05-03 03:59:029300 }else{
drh30f7a252018-05-07 11:29:599301 if( iOffset+ovflPageSize<(u32)nTotal ){
drhd5aa9262018-05-03 16:56:069302 ovflPgno = get4byte(pPage->aData);
9303 }else{
9304 ovflPageSize = nTotal - iOffset;
9305 }
9306 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
9307 iOffset, ovflPageSize);
drh3de5d162018-05-03 03:59:029308 }
drhd5aa9262018-05-03 16:56:069309 sqlite3PagerUnref(pPage->pDbPage);
drh3de5d162018-05-03 03:59:029310 if( rc ) return rc;
9311 iOffset += ovflPageSize;
drh3de5d162018-05-03 03:59:029312 }while( iOffset<nTotal );
larrybrbc917382023-06-07 08:40:319313 return SQLITE_OK;
drh3de5d162018-05-03 03:59:029314}
9315
drh1eb88d62023-01-03 15:11:019316/*
9317** Overwrite the cell that cursor pCur is pointing to with fresh content
9318** contained in pX.
9319*/
9320static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
9321 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
9322 MemPage *pPage = pCur->pPage; /* Page being written */
9323
9324 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
9325 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
9326 ){
drh50f9caf2024-02-02 18:42:099327 return SQLITE_CORRUPT_PAGE(pPage);
drh1eb88d62023-01-03 15:11:019328 }
9329 if( pCur->info.nLocal==nTotal ){
9330 /* The entire cell is local */
9331 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
9332 0, pCur->info.nLocal);
9333 }else{
9334 /* The cell contains overflow content */
9335 return btreeOverwriteOverflowCell(pCur, pX);
9336 }
9337}
9338
drhf74b8d92002-09-01 23:20:459339
9340/*
drh8eeb4462016-05-21 20:03:429341** Insert a new record into the BTree. The content of the new record
9342** is described by the pX object. The pCur cursor is used only to
9343** define what table the record should be inserted into, and is left
9344** pointing at a random location.
drh4b70f112004-05-02 21:12:199345**
drh8eeb4462016-05-21 20:03:429346** For a table btree (used for rowid tables), only the pX.nKey value of
9347** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
9348** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
9349** hold the content of the row.
9350**
9351** For an index btree (used for indexes and WITHOUT ROWID tables), the
larrybrbc917382023-06-07 08:40:319352** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
drh8eeb4462016-05-21 20:03:429353** pX.pData,nData,nZero fields must be zero.
danielk1977de630352009-05-04 11:42:299354**
9355** If the seekResult parameter is non-zero, then a successful call to
drheab10642022-03-06 20:22:249356** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
drheaf6ae22016-11-09 20:14:349357** been performed. In other words, if seekResult!=0 then the cursor
9358** is currently pointing to a cell that will be adjacent to the cell
9359** to be inserted. If seekResult<0 then pCur points to a cell that is
9360** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
9361** that is larger than (pKey,nKey).
danielk1977de630352009-05-04 11:42:299362**
drheaf6ae22016-11-09 20:14:349363** If seekResult==0, that means pCur is pointing at some unknown location.
9364** In that case, this routine must seek the cursor to the correct insertion
9365** point for (pKey,nKey) before doing the insertion. For index btrees,
9366** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
9367** key values and pX->aMem can be used instead of pX->pKey to avoid having
9368** to decode the key.
drh3b7511c2001-05-26 13:15:449369*/
drh3aac2dd2004-04-26 14:10:209370int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:589371 BtCursor *pCur, /* Insert data into the table of this cursor */
drh8eeb4462016-05-21 20:03:429372 const BtreePayload *pX, /* Content of the row to be inserted */
danf91c1312017-01-10 20:04:389373 int flags, /* True if this is likely an append */
drheab10642022-03-06 20:22:249374 int seekResult /* Result of prior IndexMoveto() call */
drh3b7511c2001-05-26 13:15:449375){
drh3b7511c2001-05-26 13:15:449376 int rc;
drh3e9ca092009-09-08 01:14:489377 int loc = seekResult; /* -1: before desired location +1: after */
drh1d452e12009-11-01 19:26:599378 int szNew = 0;
danielk197771d5d2c2008-09-29 11:49:479379 int idx;
drh3b7511c2001-05-26 13:15:449380 MemPage *pPage;
drhd677b3d2007-08-20 22:48:419381 Btree *p = pCur->pBtree;
drha34b6762004-05-07 13:30:429382 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:449383 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:449384
dancd1b2d02020-12-09 20:33:519385 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
dan7aae7352020-12-10 18:06:249386 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
danf91c1312017-01-10 20:04:389387
danielk19779c3acf32009-05-02 07:36:499388 /* Save the positions of any other cursors open on this table.
9389 **
danielk19773509a652009-07-06 18:56:139390 ** In some cases, the call to btreeMoveto() below is a no-op. For
danielk19779c3acf32009-05-02 07:36:499391 ** example, when inserting data into a table with auto-generated integer
larrybrbc917382023-06-07 08:40:319392 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
9393 ** integer key to use. It then calls this function to actually insert the
danielk19773509a652009-07-06 18:56:139394 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
danielk19779c3acf32009-05-02 07:36:499395 ** that the cursor is already where it needs to be and returns without
9396 ** doing any work. To avoid thwarting these optimizations, it is important
9397 ** not to clear the cursor here.
9398 */
drh27fb7462015-06-30 02:47:369399 if( pCur->curFlags & BTCF_Multiple ){
drhe7d53842022-11-21 14:13:109400 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
drh27fb7462015-06-30 02:47:369401 if( rc ) return rc;
danf5ea93b2021-04-08 19:39:009402 if( loc && pCur->iPage<0 ){
9403 /* This can only happen if the schema is corrupt such that there is more
9404 ** than one table or index with the same root page as used by the cursor.
9405 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
9406 ** the schema was loaded. This cannot be asserted though, as a user might
9407 ** set the flag, load the schema, and then unset the flag. */
drh50f9caf2024-02-02 18:42:099408 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
danf5ea93b2021-04-08 19:39:009409 }
drhd60f4f42012-03-23 14:23:529410 }
9411
drhc63e4092022-03-21 18:48:319412 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
9413 ** points to a valid cell.
9414 */
drhbd5fb3a2022-03-21 18:17:099415 if( pCur->eState>=CURSOR_REQUIRESEEK ){
drhc63e4092022-03-21 18:48:319416 testcase( pCur->eState==CURSOR_REQUIRESEEK );
9417 testcase( pCur->eState==CURSOR_FAULT );
drhbd5fb3a2022-03-21 18:17:099418 rc = moveToRoot(pCur);
9419 if( rc && rc!=SQLITE_EMPTY ) return rc;
9420 }
9421
9422 assert( cursorOwnsBtShared(pCur) );
9423 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
drhe7d53842022-11-21 14:13:109424 && p->pBt->inTransaction==TRANS_WRITE
9425 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
drhbd5fb3a2022-03-21 18:17:099426 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
9427
9428 /* Assert that the caller has been consistent. If this cursor was opened
9429 ** expecting an index b-tree, then the caller should be inserting blob
9430 ** keys with no associated data. If the cursor was opened expecting an
9431 ** intkey table, the caller should be inserting integer keys with a
9432 ** blob of associated data. */
9433 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
9434
danielk197771d5d2c2008-09-29 11:49:479435 if( pCur->pKeyInfo==0 ){
drh8eeb4462016-05-21 20:03:429436 assert( pX->pKey==0 );
larrybrbc917382023-06-07 08:40:319437 /* If this is an insert into a table b-tree, invalidate any incrblob
drhe0670b62014-02-12 21:31:129438 ** cursors open on the row being replaced */
drh49bb56e2021-05-14 20:01:369439 if( p->hasIncrblobCur ){
9440 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
9441 }
drhe0670b62014-02-12 21:31:129442
larrybrbc917382023-06-07 08:40:319443 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
drhd720d392018-05-07 17:27:049444 ** to a row with the same key as the new entry being inserted.
9445 */
9446#ifdef SQLITE_DEBUG
9447 if( flags & BTREE_SAVEPOSITION ){
9448 assert( pCur->curFlags & BTCF_ValidNKey );
9449 assert( pX->nKey==pCur->info.nKey );
drhd720d392018-05-07 17:27:049450 assert( loc==0 );
9451 }
9452#endif
danf91c1312017-01-10 20:04:389453
drhd720d392018-05-07 17:27:049454 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
9455 ** that the cursor is not pointing to a row to be overwritten.
9456 ** So do a complete check.
9457 */
drh7a1c28d2016-11-10 20:42:089458 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
drhd720d392018-05-07 17:27:049459 /* The cursor is pointing to the entry that is to be
drh3de5d162018-05-03 03:59:029460 ** overwritten */
drh30f7a252018-05-07 11:29:599461 assert( pX->nData>=0 && pX->nZero>=0 );
9462 if( pCur->info.nSize!=0
9463 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
9464 ){
drhd720d392018-05-07 17:27:049465 /* New entry is the same size as the old. Do an overwrite */
drh3de5d162018-05-03 03:59:029466 return btreeOverwriteCell(pCur, pX);
9467 }
drhd720d392018-05-07 17:27:049468 assert( loc==0 );
drh207c8172015-06-29 23:01:329469 }else if( loc==0 ){
drhd720d392018-05-07 17:27:049470 /* The cursor is *not* pointing to the cell to be overwritten, nor
9471 ** to an adjacent cell. Move the cursor so that it is pointing either
9472 ** to the cell to be overwritten or an adjacent cell.
9473 */
larrybrbc917382023-06-07 08:40:319474 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
drh42a410d2021-06-19 18:32:209475 (flags & BTREE_APPEND)!=0, &loc);
drh207c8172015-06-29 23:01:329476 if( rc ) return rc;
drhe0670b62014-02-12 21:31:129477 }
drhd720d392018-05-07 17:27:049478 }else{
9479 /* This is an index or a WITHOUT ROWID table */
9480
larrybrbc917382023-06-07 08:40:319481 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
drhd720d392018-05-07 17:27:049482 ** to a row with the same key as the new entry being inserted.
9483 */
9484 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
9485
9486 /* If the cursor is not already pointing either to the cell to be
9487 ** overwritten, or if a new cell is being inserted, if the cursor is
9488 ** not pointing to an immediately adjacent cell, then move the cursor
9489 ** so that it does.
9490 */
9491 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
9492 if( pX->nMem ){
9493 UnpackedRecord r;
9494 r.pKeyInfo = pCur->pKeyInfo;
9495 r.aMem = pX->aMem;
9496 r.nField = pX->nMem;
9497 r.default_rc = 0;
drhd720d392018-05-07 17:27:049498 r.eqSeen = 0;
drh42a410d2021-06-19 18:32:209499 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc);
drhd720d392018-05-07 17:27:049500 }else{
larrybrbc917382023-06-07 08:40:319501 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
drh42a410d2021-06-19 18:32:209502 (flags & BTREE_APPEND)!=0, &loc);
drhd720d392018-05-07 17:27:049503 }
9504 if( rc ) return rc;
drh9b4eaeb2016-11-09 00:10:339505 }
drh89ee2292018-05-07 18:41:199506
9507 /* If the cursor is currently pointing to an entry to be overwritten
9508 ** and the new content is the same as as the old, then use the
9509 ** overwrite optimization.
9510 */
9511 if( loc==0 ){
9512 getCellInfo(pCur);
9513 if( pCur->info.nKey==pX->nKey ){
9514 BtreePayload x2;
9515 x2.pData = pX->pKey;
drhc071c472025-02-22 16:44:149516 x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff );
drh89ee2292018-05-07 18:41:199517 x2.nZero = 0;
9518 return btreeOverwriteCell(pCur, &x2);
9519 }
9520 }
danielk1977da184232006-01-05 11:34:329521 }
larrybrbc917382023-06-07 08:40:319522 assert( pCur->eState==CURSOR_VALID
drha9c4c822023-04-22 12:47:169523 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
danielk1977da184232006-01-05 11:34:329524
drh352a35a2017-08-15 03:46:479525 pPage = pCur->pPage;
dancd1b2d02020-12-09 20:33:519526 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
drh44845222008-07-17 18:39:579527 assert( pPage->leaf || !pPage->intKey );
drhb0ea9432019-02-09 21:06:409528 if( pPage->nFree<0 ){
drhc63e4092022-03-21 18:48:319529 if( NEVER(pCur->eState>CURSOR_INVALID) ){
9530 /* ^^^^^--- due to the moveToRoot() call above */
drh50f9caf2024-02-02 18:42:099531 rc = SQLITE_CORRUPT_PAGE(pPage);
drha1085f02020-07-11 16:42:289532 }else{
9533 rc = btreeComputeFreeSpace(pPage);
9534 }
drhb0ea9432019-02-09 21:06:409535 if( rc ) return rc;
9536 }
danielk19778f880a82009-07-13 09:41:459537
drh2e89f1c2023-04-01 12:22:579538 TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n",
drh8eeb4462016-05-21 20:03:429539 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
drh3a4c1412004-05-09 20:40:119540 loc==0 ? "overwrite" : "new entry"));
drhf51672a2022-05-31 15:18:559541 assert( pPage->isInit || CORRUPT_DB );
drhe7d53842022-11-21 14:13:109542 newCell = p->pBt->pTmpSpace;
drh3fbb0222014-09-24 19:47:279543 assert( newCell!=0 );
drhecba1072022-11-19 20:10:559544 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
dancd1b2d02020-12-09 20:33:519545 if( flags & BTREE_PREFORMAT ){
dancd1b2d02020-12-09 20:33:519546 rc = SQLITE_OK;
drhe7d53842022-11-21 14:13:109547 szNew = p->pBt->nPreformatSize;
drh7c6433c2024-02-26 15:27:339548 if( szNew<4 ){
9549 szNew = 4;
9550 newCell[3] = 0;
9551 }
drhe7d53842022-11-21 14:13:109552 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
dan7aae7352020-12-10 18:06:249553 CellInfo info;
9554 pPage->xParseCell(pPage, newCell, &info);
dan9257ddb2020-12-10 19:54:139555 if( info.nPayload!=info.nLocal ){
dan7aae7352020-12-10 18:06:249556 Pgno ovfl = get4byte(&newCell[szNew-4]);
drhe7d53842022-11-21 14:13:109557 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
drh5a1d6592022-11-19 19:37:269558 if( NEVER(rc) ) goto end_insert;
dan7aae7352020-12-10 18:06:249559 }
9560 }
dancd1b2d02020-12-09 20:33:519561 }else{
9562 rc = fillInCell(pPage, newCell, pX, &szNew);
drh5a1d6592022-11-19 19:37:269563 if( rc ) goto end_insert;
dancd1b2d02020-12-09 20:33:519564 }
drh25ada072015-06-19 15:07:149565 assert( szNew==pPage->xCellSize(pPage, newCell) );
drhe7d53842022-11-21 14:13:109566 assert( szNew <= MX_CELL_SIZE(p->pBt) );
drh75e96b32017-04-01 00:20:069567 idx = pCur->ix;
danbb130c92023-03-20 14:59:199568 pCur->info.nSize = 0;
danielk1977b980d2212009-06-22 18:03:519569 if( loc==0 ){
drh80159da2016-12-09 17:32:519570 CellInfo info;
drh635480e2021-10-08 16:15:179571 assert( idx>=0 );
9572 if( idx>=pPage->nCell ){
drh50f9caf2024-02-02 18:42:099573 return SQLITE_CORRUPT_PAGE(pPage);
drh635480e2021-10-08 16:15:179574 }
danielk19776e465eb2007-08-21 13:11:009575 rc = sqlite3PagerWrite(pPage->pDbPage);
9576 if( rc ){
9577 goto end_insert;
9578 }
danielk197771d5d2c2008-09-29 11:49:479579 oldCell = findCell(pPage, idx);
drh4b70f112004-05-02 21:12:199580 if( !pPage->leaf ){
drh43605152004-05-29 21:46:499581 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:199582 }
drh86c779f2021-05-15 13:08:449583 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
drh554a19d2019-08-12 18:26:469584 testcase( pCur->curFlags & BTCF_ValidOvfl );
9585 invalidateOverflowCache(pCur);
larrybrbc917382023-06-07 08:40:319586 if( info.nSize==szNew && info.nLocal==info.nPayload
drhe7d53842022-11-21 14:13:109587 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
danca66f6c2017-06-08 11:14:089588 ){
drhf9238252016-12-09 18:09:429589 /* Overwrite the old cell with the new if they are the same size.
9590 ** We could also try to do this if the old cell is smaller, then add
9591 ** the leftover space to the free list. But experiments show that
9592 ** doing that is no faster then skipping this optimization and just
larrybrbc917382023-06-07 08:40:319593 ** calling dropCell() and insertCell().
danca66f6c2017-06-08 11:14:089594 **
9595 ** This optimization cannot be used on an autovacuum database if the
9596 ** new entry uses overflow pages, as the insertCell() call below is
9597 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
drhf9238252016-12-09 18:09:429598 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
drh93788182019-07-22 23:24:019599 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
drh50f9caf2024-02-02 18:42:099600 return SQLITE_CORRUPT_PAGE(pPage);
drh93788182019-07-22 23:24:019601 }
9602 if( oldCell+szNew > pPage->aDataEnd ){
drh50f9caf2024-02-02 18:42:099603 return SQLITE_CORRUPT_PAGE(pPage);
drh93788182019-07-22 23:24:019604 }
drh80159da2016-12-09 17:32:519605 memcpy(oldCell, newCell, szNew);
9606 return SQLITE_OK;
9607 }
9608 dropCell(pPage, idx, info.nSize, &rc);
drh2e38c322004-09-03 18:38:449609 if( rc ) goto end_insert;
drh7c717f72001-06-24 20:39:419610 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:199611 assert( pPage->leaf );
drh75e96b32017-04-01 00:20:069612 idx = ++pCur->ix;
drhc494fdb2024-04-12 02:57:529613 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
drh14acc042001-06-10 19:56:589614 }else{
drh4b70f112004-05-02 21:12:199615 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:449616 }
drh34ceb7e2023-04-07 14:33:339617 rc = insertCellFast(pPage, idx, newCell, szNew);
drh09a4e922016-05-21 12:29:049618 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
danielk19773f632d52009-05-02 10:03:099619 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
drh9bf9e9c2008-12-05 20:01:439620
larrybrbc917382023-06-07 08:40:319621 /* If no error has occurred and pPage has an overflow cell, call balance()
danielk1977a50d9aa2009-06-08 14:49:459622 ** to redistribute the cells within the tree. Since balance() may move
drh036dbec2014-03-11 23:40:449623 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
danielk1977a50d9aa2009-06-08 14:49:459624 ** variables.
danielk19773f632d52009-05-02 10:03:099625 **
danielk1977a50d9aa2009-06-08 14:49:459626 ** Previous versions of SQLite called moveToRoot() to move the cursor
9627 ** back to the root page as balance() used to invalidate the contents
danielk197754109bb2009-06-23 11:22:299628 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
9629 ** set the cursor state to "invalid". This makes common insert operations
9630 ** slightly faster.
danielk19773f632d52009-05-02 10:03:099631 **
danielk1977a50d9aa2009-06-08 14:49:459632 ** There is a subtle but important optimization here too. When inserting
9633 ** multiple records into an intkey b-tree using a single cursor (as can
9634 ** happen while processing an "INSERT INTO ... SELECT" statement), it
9635 ** is advantageous to leave the cursor pointing to the last entry in
9636 ** the b-tree if possible. If the cursor is left pointing to the last
9637 ** entry in the table, and the next row inserted has an integer key
9638 ** larger than the largest existing key, it is possible to insert the
9639 ** row without seeking the cursor. This can be a big performance boost.
danielk19773f632d52009-05-02 10:03:099640 */
drh09a4e922016-05-21 12:29:049641 if( pPage->nOverflow ){
9642 assert( rc==SQLITE_OK );
drhc494fdb2024-04-12 02:57:529643 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
danielk1977a50d9aa2009-06-08 14:49:459644 rc = balance(pCur);
9645
9646 /* Must make sure nOverflow is reset to zero even if the balance()
larrybrbc917382023-06-07 08:40:319647 ** fails. Internal data structure corruption will result otherwise.
danielk197754109bb2009-06-23 11:22:299648 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
9649 ** from trying to save the current position of the cursor. */
drh352a35a2017-08-15 03:46:479650 pCur->pPage->nOverflow = 0;
danielk197754109bb2009-06-23 11:22:299651 pCur->eState = CURSOR_INVALID;
danf91c1312017-01-10 20:04:389652 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
drh85ef6302017-08-02 15:50:099653 btreeReleaseAllCursorPages(pCur);
drh7b20a152017-01-12 19:10:559654 if( pCur->pKeyInfo ){
danf91c1312017-01-10 20:04:389655 assert( pCur->pKey==0 );
9656 pCur->pKey = sqlite3Malloc( pX->nKey );
9657 if( pCur->pKey==0 ){
9658 rc = SQLITE_NOMEM;
9659 }else{
9660 memcpy(pCur->pKey, pX->pKey, pX->nKey);
9661 }
9662 }
9663 pCur->eState = CURSOR_REQUIRESEEK;
9664 pCur->nKey = pX->nKey;
9665 }
danielk19773f632d52009-05-02 10:03:099666 }
drh352a35a2017-08-15 03:46:479667 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
drh9bf9e9c2008-12-05 20:01:439668
drh2e38c322004-09-03 18:38:449669end_insert:
drh5e2f8b92001-05-28 00:41:159670 return rc;
9671}
9672
dand2ffc972020-12-10 19:20:159673/*
9674** This function is used as part of copying the current row from cursor
9675** pSrc into cursor pDest. If the cursors are open on intkey tables, then
9676** parameter iKey is used as the rowid value when the record is copied
9677** into pDest. Otherwise, the record is copied verbatim.
9678**
9679** This function does not actually write the new value to cursor pDest.
9680** Instead, it creates and populates any required overflow pages and
9681** writes the data for the new cell into the BtShared.pTmpSpace buffer
9682** for the destination database. The size of the cell, in bytes, is left
9683** in BtShared.nPreformatSize. The caller completes the insertion by
9684** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
9685**
9686** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9687*/
dan7aae7352020-12-10 18:06:249688int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
dan7aae7352020-12-10 18:06:249689 BtShared *pBt = pDest->pBt;
9690 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
danebbf3682020-12-09 16:32:119691 const u8 *aIn; /* Pointer to next input buffer */
drhe5baf5c2020-12-16 14:20:459692 u32 nIn; /* Size of input buffer aIn[] */
dan7f607062020-12-15 19:27:209693 u32 nRem; /* Bytes of data still to copy */
dan036e0672020-12-08 20:19:079694
dan036e0672020-12-08 20:19:079695 getCellInfo(pSrc);
drhb47b1f62022-04-01 21:01:379696 if( pSrc->info.nPayload<0x80 ){
drhc071c472025-02-22 16:44:149697 *(aOut++) = (u8)pSrc->info.nPayload;
drhb47b1f62022-04-01 21:01:379698 }else{
9699 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
9700 }
dan7aae7352020-12-10 18:06:249701 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
danebbf3682020-12-09 16:32:119702 nIn = pSrc->info.nLocal;
9703 aIn = pSrc->info.pPayload;
drh0a8b6a92020-12-16 21:09:459704 if( aIn+nIn>pSrc->pPage->aDataEnd ){
drh50f9caf2024-02-02 18:42:099705 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
drh0a8b6a92020-12-16 21:09:459706 }
danebbf3682020-12-09 16:32:119707 nRem = pSrc->info.nPayload;
dan7aae7352020-12-10 18:06:249708 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
9709 memcpy(aOut, aIn, nIn);
drhc071c472025-02-22 16:44:149710 pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
drhd01dee52022-11-21 13:35:009711 return SQLITE_OK;
dan7aae7352020-12-10 18:06:249712 }else{
drhd01dee52022-11-21 13:35:009713 int rc = SQLITE_OK;
dan7aae7352020-12-10 18:06:249714 Pager *pSrcPager = pSrc->pBt->pPager;
9715 u8 *pPgnoOut = 0;
9716 Pgno ovflIn = 0;
9717 DbPage *pPageIn = 0;
9718 MemPage *pPageOut = 0;
drhe5baf5c2020-12-16 14:20:459719 u32 nOut; /* Size of output buffer aOut[] */
danebbf3682020-12-09 16:32:119720
dan7aae7352020-12-10 18:06:249721 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
drhc071c472025-02-22 16:44:149722 pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
dan7aae7352020-12-10 18:06:249723 if( nOut<pSrc->info.nPayload ){
9724 pPgnoOut = &aOut[nOut];
9725 pBt->nPreformatSize += 4;
9726 }
larrybrbc917382023-06-07 08:40:319727
dan7aae7352020-12-10 18:06:249728 if( nRem>nIn ){
drh0a8b6a92020-12-16 21:09:459729 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
drh50f9caf2024-02-02 18:42:099730 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
drh0a8b6a92020-12-16 21:09:459731 }
dan7aae7352020-12-10 18:06:249732 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
9733 }
larrybrbc917382023-06-07 08:40:319734
dan7aae7352020-12-10 18:06:249735 do {
9736 nRem -= nOut;
9737 do{
9738 assert( nOut>0 );
9739 if( nIn>0 ){
9740 int nCopy = MIN(nOut, nIn);
9741 memcpy(aOut, aIn, nCopy);
9742 nOut -= nCopy;
9743 nIn -= nCopy;
9744 aOut += nCopy;
9745 aIn += nCopy;
9746 }
9747 if( nOut>0 ){
9748 sqlite3PagerUnref(pPageIn);
9749 pPageIn = 0;
9750 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY);
9751 if( rc==SQLITE_OK ){
9752 aIn = (const u8*)sqlite3PagerGetData(pPageIn);
9753 ovflIn = get4byte(aIn);
9754 aIn += 4;
9755 nIn = pSrc->pBt->usableSize - 4;
9756 }
9757 }
9758 }while( rc==SQLITE_OK && nOut>0 );
larrybrbc917382023-06-07 08:40:319759
drhad1188b2021-10-02 18:22:249760 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
dan7aae7352020-12-10 18:06:249761 Pgno pgnoNew;
9762 MemPage *pNew = 0;
9763 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
9764 put4byte(pPgnoOut, pgnoNew);
drhe7d53842022-11-21 14:13:109765 if( ISAUTOVACUUM(pBt) && pPageOut ){
dan7aae7352020-12-10 18:06:249766 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
9767 }
9768 releasePage(pPageOut);
9769 pPageOut = pNew;
9770 if( pPageOut ){
9771 pPgnoOut = pPageOut->aData;
9772 put4byte(pPgnoOut, 0);
9773 aOut = &pPgnoOut[4];
9774 nOut = MIN(pBt->usableSize - 4, nRem);
danebbf3682020-12-09 16:32:119775 }
9776 }
dan7aae7352020-12-10 18:06:249777 }while( nRem>0 && rc==SQLITE_OK );
larrybrbc917382023-06-07 08:40:319778
dan7aae7352020-12-10 18:06:249779 releasePage(pPageOut);
9780 sqlite3PagerUnref(pPageIn);
drhd01dee52022-11-21 13:35:009781 return rc;
dan036e0672020-12-08 20:19:079782 }
dan036e0672020-12-08 20:19:079783}
9784
drh5e2f8b92001-05-28 00:41:159785/*
larrybrbc917382023-06-07 08:40:319786** Delete the entry that the cursor is pointing to.
danf0ee1d32015-09-12 19:26:119787**
drhe807bdb2016-01-21 17:06:339788** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
9789** the cursor is left pointing at an arbitrary location after the delete.
9790** But if that bit is set, then the cursor is left in a state such that
9791** the next call to BtreeNext() or BtreePrev() moves it to the same row
9792** as it would have been on if the call to BtreeDelete() had been omitted.
9793**
drhdef19e32016-01-27 16:26:259794** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
9795** associated with a single table entry and its indexes. Only one of those
9796** deletes is considered the "primary" delete. The primary delete occurs
9797** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
9798** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
9799** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
drhe807bdb2016-01-21 17:06:339800** but which might be used by alternative storage engines.
drh3b7511c2001-05-26 13:15:449801*/
drhe807bdb2016-01-21 17:06:339802int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
drhd677b3d2007-08-20 22:48:419803 Btree *p = pCur->pBtree;
larrybrbc917382023-06-07 08:40:319804 BtShared *pBt = p->pBt;
drh7e17a3a2022-01-02 14:55:439805 int rc; /* Return code */
9806 MemPage *pPage; /* Page to delete cell from */
9807 unsigned char *pCell; /* Pointer to cell to delete */
9808 int iCellIdx; /* Index of cell to delete */
larrybrbc917382023-06-07 08:40:319809 int iCellDepth; /* Depth of node containing pCell */
drh7e17a3a2022-01-02 14:55:439810 CellInfo info; /* Size of the cell being deleted */
9811 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
drh8b2f49b2001-06-08 00:21:529812
dan7a2347e2016-01-07 16:43:549813 assert( cursorOwnsBtShared(pCur) );
drh64022502009-01-09 14:11:049814 assert( pBt->inTransaction==TRANS_WRITE );
drhc9166342012-01-05 23:32:069815 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
drh036dbec2014-03-11 23:40:449816 assert( pCur->curFlags & BTCF_WriteFlag );
danielk197796d48e92009-06-29 06:00:379817 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
9818 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
drhdef19e32016-01-27 16:26:259819 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
drh500d7e52022-03-22 23:33:209820 if( pCur->eState!=CURSOR_VALID ){
9821 if( pCur->eState>=CURSOR_REQUIRESEEK ){
9822 rc = btreeRestoreCursorPosition(pCur);
9823 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
9824 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
9825 }else{
drh50f9caf2024-02-02 18:42:099826 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
drh500d7e52022-03-22 23:33:209827 }
danb560a712019-03-13 15:29:149828 }
drh500d7e52022-03-22 23:33:209829 assert( pCur->eState==CURSOR_VALID );
danielk1977da184232006-01-05 11:34:329830
danielk19774dbaa892009-06-16 16:50:229831 iCellDepth = pCur->iPage;
drh75e96b32017-04-01 00:20:069832 iCellIdx = pCur->ix;
drh352a35a2017-08-15 03:46:479833 pPage = pCur->pPage;
drh7e17a3a2022-01-02 14:55:439834 if( pPage->nCell<=iCellIdx ){
drh50f9caf2024-02-02 18:42:099835 return SQLITE_CORRUPT_PAGE(pPage);
drh7e17a3a2022-01-02 14:55:439836 }
danielk19774dbaa892009-06-16 16:50:229837 pCell = findCell(pPage, iCellIdx);
drh2dfe9662022-01-02 11:25:519838 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
drh50f9caf2024-02-02 18:42:099839 return SQLITE_CORRUPT_PAGE(pPage);
drh2dfe9662022-01-02 11:25:519840 }
drhaf336542023-04-02 16:43:209841 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
drh50f9caf2024-02-02 18:42:099842 return SQLITE_CORRUPT_PAGE(pPage);
drhaf336542023-04-02 16:43:209843 }
danielk19774dbaa892009-06-16 16:50:229844
drh7e17a3a2022-01-02 14:55:439845 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
drhbfc7a8b2016-04-09 17:04:059846 ** be preserved following this delete operation. If the current delete
9847 ** will cause a b-tree rebalance, then this is done by saving the cursor
larrybrbc917382023-06-07 08:40:319848 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
9849 ** returning.
drhbfc7a8b2016-04-09 17:04:059850 **
drh7e17a3a2022-01-02 14:55:439851 ** If the current delete will not cause a rebalance, then the cursor
drhbfc7a8b2016-04-09 17:04:059852 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
drh7e17a3a2022-01-02 14:55:439853 ** before or after the deleted entry.
9854 **
9855 ** The bPreserve value records which path is required:
9856 **
9857 ** bPreserve==0 Not necessary to save the cursor position
9858 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
9859 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
9860 */
9861 bPreserve = (flags & BTREE_SAVEPOSITION)!=0;
drhbfc7a8b2016-04-09 17:04:059862 if( bPreserve ){
larrybrbc917382023-06-07 08:40:319863 if( !pPage->leaf
drh500d7e52022-03-22 23:33:209864 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
9865 (int)(pBt->usableSize*2/3)
drh1641f112018-12-13 21:05:459866 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
drhbfc7a8b2016-04-09 17:04:059867 ){
9868 /* A b-tree rebalance will be required after deleting this entry.
9869 ** Save the cursor key. */
9870 rc = saveCursorKey(pCur);
9871 if( rc ) return rc;
9872 }else{
drh7e17a3a2022-01-02 14:55:439873 bPreserve = 2;
drhbfc7a8b2016-04-09 17:04:059874 }
9875 }
9876
danielk19774dbaa892009-06-16 16:50:229877 /* If the page containing the entry to delete is not a leaf page, move
9878 ** the cursor to the largest entry in the tree that is smaller than
9879 ** the entry being deleted. This cell will replace the cell being deleted
9880 ** from the internal node. The 'previous' entry is used for this instead
9881 ** of the 'next' entry, as the previous entry is always a part of the
9882 ** sub-tree headed by the child page of the cell being deleted. This makes
9883 ** balancing the tree following the delete operation easier. */
9884 if( !pPage->leaf ){
drh2ab792e2017-05-30 18:34:079885 rc = sqlite3BtreePrevious(pCur, 0);
9886 assert( rc!=SQLITE_DONE );
drh4c301aa2009-07-15 17:25:459887 if( rc ) return rc;
danielk19774dbaa892009-06-16 16:50:229888 }
9889
9890 /* Save the positions of any other cursors open on this table before
danf0ee1d32015-09-12 19:26:119891 ** making any modifications. */
drh27fb7462015-06-30 02:47:369892 if( pCur->curFlags & BTCF_Multiple ){
9893 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
9894 if( rc ) return rc;
9895 }
drhd60f4f42012-03-23 14:23:529896
9897 /* If this is a delete operation to remove a row from a table b-tree,
9898 ** invalidate any incrblob cursors open on the row being deleted. */
drh49bb56e2021-05-14 20:01:369899 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
drh9ca431a2017-03-29 18:03:509900 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
drhd60f4f42012-03-23 14:23:529901 }
9902
danf0ee1d32015-09-12 19:26:119903 /* Make the page containing the entry to be deleted writable. Then free any
9904 ** overflow pages associated with the entry and finally remove the cell
9905 ** itself from within the page. */
drha4ec1d42009-07-11 13:13:119906 rc = sqlite3PagerWrite(pPage->pDbPage);
9907 if( rc ) return rc;
drh86c779f2021-05-15 13:08:449908 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
drh80159da2016-12-09 17:32:519909 dropCell(pPage, iCellIdx, info.nSize, &rc);
drha4ec1d42009-07-11 13:13:119910 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:109911
danielk19774dbaa892009-06-16 16:50:229912 /* If the cell deleted was not located on a leaf page, then the cursor
9913 ** is currently pointing to the largest entry in the sub-tree headed
9914 ** by the child-page of the cell that was just deleted from an internal
9915 ** node. The cell from the leaf node needs to be moved to the internal
9916 ** node to replace the deleted cell. */
drh4b70f112004-05-02 21:12:199917 if( !pPage->leaf ){
drh352a35a2017-08-15 03:46:479918 MemPage *pLeaf = pCur->pPage;
danielk19774dbaa892009-06-16 16:50:229919 int nCell;
drh352a35a2017-08-15 03:46:479920 Pgno n;
danielk19774dbaa892009-06-16 16:50:229921 unsigned char *pTmp;
danielk1977e6efa742004-11-10 11:55:109922
drhb0ea9432019-02-09 21:06:409923 if( pLeaf->nFree<0 ){
9924 rc = btreeComputeFreeSpace(pLeaf);
9925 if( rc ) return rc;
9926 }
drh352a35a2017-08-15 03:46:479927 if( iCellDepth<pCur->iPage-1 ){
9928 n = pCur->apPage[iCellDepth+1]->pgno;
9929 }else{
9930 n = pCur->pPage->pgno;
9931 }
danielk19774dbaa892009-06-16 16:50:229932 pCell = findCell(pLeaf, pLeaf->nCell-1);
drh50f9caf2024-02-02 18:42:099933 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf);
drh25ada072015-06-19 15:07:149934 nCell = pLeaf->xCellSize(pLeaf, pCell);
drhfcd71b62011-04-05 22:08:249935 assert( MX_CELL_SIZE(pBt) >= nCell );
danielk19774dbaa892009-06-16 16:50:229936 pTmp = pBt->pTmpSpace;
drh3fbb0222014-09-24 19:47:279937 assert( pTmp!=0 );
drha4ec1d42009-07-11 13:13:119938 rc = sqlite3PagerWrite(pLeaf->pDbPage);
drhcb89f4a2016-05-21 11:23:269939 if( rc==SQLITE_OK ){
drhb53d8fa2022-11-21 15:55:579940 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
drhcb89f4a2016-05-21 11:23:269941 }
drh98add2e2009-07-20 17:11:499942 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
drha4ec1d42009-07-11 13:13:119943 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:159944 }
danielk19774dbaa892009-06-16 16:50:229945
9946 /* Balance the tree. If the entry deleted was located on a leaf page,
9947 ** then the cursor still points to that page. In this case the first
9948 ** call to balance() repairs the tree, and the if(...) condition is
9949 ** never true.
9950 **
9951 ** Otherwise, if the entry deleted was on an internal node page, then
9952 ** pCur is pointing to the leaf page from which a cell was removed to
9953 ** replace the cell deleted from the internal node. This is slightly
9954 ** tricky as the leaf node may be underfull, and the internal node may
9955 ** be either under or overfull. In this case run the balancing algorithm
9956 ** on the leaf node first. If the balance proceeds far enough up the
9957 ** tree that we can be sure that any problem in the internal node has
9958 ** been corrected, so be it. Otherwise, after balancing the leaf node,
larrybrbc917382023-06-07 08:40:319959 ** walk the cursor up the tree to the internal node and balance it as
danielk19774dbaa892009-06-16 16:50:229960 ** well. */
drhde948482022-03-29 13:16:329961 assert( pCur->pPage->nOverflow==0 );
9962 assert( pCur->pPage->nFree>=0 );
drhc4c0ff82022-03-31 16:09:139963 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
drhde948482022-03-29 13:16:329964 /* Optimization: If the free space is less than 2/3rds of the page,
9965 ** then balance() will always be a no-op. No need to invoke it. */
9966 rc = SQLITE_OK;
9967 }else{
9968 rc = balance(pCur);
9969 }
danielk19774dbaa892009-06-16 16:50:229970 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
drh352a35a2017-08-15 03:46:479971 releasePageNotNull(pCur->pPage);
9972 pCur->iPage--;
danielk19774dbaa892009-06-16 16:50:229973 while( pCur->iPage>iCellDepth ){
9974 releasePage(pCur->apPage[pCur->iPage--]);
9975 }
drh352a35a2017-08-15 03:46:479976 pCur->pPage = pCur->apPage[pCur->iPage];
danielk19774dbaa892009-06-16 16:50:229977 rc = balance(pCur);
9978 }
9979
danielk19776b456a22005-03-21 04:04:029980 if( rc==SQLITE_OK ){
drh7e17a3a2022-01-02 14:55:439981 if( bPreserve>1 ){
9982 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
drh352a35a2017-08-15 03:46:479983 assert( pPage==pCur->pPage || CORRUPT_DB );
drh78ac1092015-09-20 22:57:479984 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
danf0ee1d32015-09-12 19:26:119985 pCur->eState = CURSOR_SKIPNEXT;
9986 if( iCellIdx>=pPage->nCell ){
9987 pCur->skipNext = -1;
drh75e96b32017-04-01 00:20:069988 pCur->ix = pPage->nCell-1;
danf0ee1d32015-09-12 19:26:119989 }else{
9990 pCur->skipNext = 1;
9991 }
9992 }else{
9993 rc = moveToRoot(pCur);
9994 if( bPreserve ){
drh85ef6302017-08-02 15:50:099995 btreeReleaseAllCursorPages(pCur);
danf0ee1d32015-09-12 19:26:119996 pCur->eState = CURSOR_REQUIRESEEK;
9997 }
drh44548e72017-08-14 18:13:529998 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
danf0ee1d32015-09-12 19:26:119999 }
danielk19776b456a22005-03-21 04:04:0210000 }
drh5e2f8b92001-05-28 00:41:1510001 return rc;
drh3b7511c2001-05-26 13:15:4410002}
drh8b2f49b2001-06-08 00:21:5210003
10004/*
drhc6b52df2002-01-04 03:09:2910005** Create a new BTree table. Write into *piTable the page
10006** number for the root page of the new table.
10007**
drhab01f612004-05-22 02:55:2310008** The type of type is determined by the flags parameter. Only the
10009** following values of flags are currently in use. Other values for
10010** flags might not work:
10011**
10012** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
10013** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:5210014*/
drhabc38152020-07-22 13:38:0410015static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
danielk1977aef0bf62005-12-30 16:28:0110016 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:5210017 MemPage *pRoot;
10018 Pgno pgnoRoot;
10019 int rc;
larrybrbc917382023-06-07 08:40:3110020 int ptfFlags; /* Page-type flags for the root page of new table */
drhd677b3d2007-08-20 22:48:4110021
drh1fee73e2007-08-29 04:00:5710022 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:0410023 assert( pBt->inTransaction==TRANS_WRITE );
drhc9166342012-01-05 23:32:0610024 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
danielk1977e6efa742004-11-10 11:55:1010025
danielk1977003ba062004-11-04 02:57:3310026#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:0110027 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drhd677b3d2007-08-20 22:48:4110028 if( rc ){
10029 return rc;
10030 }
danielk1977003ba062004-11-04 02:57:3310031#else
danielk1977687566d2004-11-02 12:56:4110032 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:3310033 Pgno pgnoMove; /* Move a page here to make room for the root-page */
10034 MemPage *pPageMove; /* The page to move to. */
10035
danielk197720713f32007-05-03 11:43:3310036 /* Creating a new table may probably require moving an existing database
10037 ** to make room for the new tables root page. In case this page turns
10038 ** out to be an overflow page, delete all overflow page-map caches
10039 ** held by open cursors.
10040 */
danielk197792d4d7a2007-05-04 12:05:5610041 invalidateAllOverflowCache(pBt);
danielk197720713f32007-05-03 11:43:3310042
danielk1977003ba062004-11-04 02:57:3310043 /* Read the value of meta[3] from the database to determine where the
10044 ** root page of the new table should go. meta[3] is the largest root-page
10045 ** created so far, so the new root-page is (meta[3]+1).
10046 */
danielk1977602b4662009-07-02 07:47:3310047 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
drh10248222020-07-28 20:32:1210048 if( pgnoRoot>btreePagecount(pBt) ){
drh50f9caf2024-02-02 18:42:0910049 return SQLITE_CORRUPT_PGNO(pgnoRoot);
drh10248222020-07-28 20:32:1210050 }
danielk1977003ba062004-11-04 02:57:3310051 pgnoRoot++;
10052
danielk1977599fcba2004-11-08 07:13:1310053 /* The new root-page may not be allocated on a pointer-map page, or the
10054 ** PENDING_BYTE page.
10055 */
drh72190432008-01-31 14:54:4310056 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:1310057 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:3310058 pgnoRoot++;
10059 }
drh48bf2d72020-07-30 17:14:5510060 assert( pgnoRoot>=3 );
danielk1977003ba062004-11-04 02:57:3310061
10062 /* Allocate a page. The page that currently resides at pgnoRoot will
10063 ** be moved to the allocated page (unless the allocated page happens
10064 ** to reside at pgnoRoot).
10065 */
dan51f0b6d2013-02-22 20:16:3410066 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
danielk1977003ba062004-11-04 02:57:3310067 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:4110068 return rc;
10069 }
danielk1977003ba062004-11-04 02:57:3310070
10071 if( pgnoMove!=pgnoRoot ){
danielk1977f35843b2007-04-07 15:03:1710072 /* pgnoRoot is the page that will be used for the root-page of
10073 ** the new table (assuming an error did not occur). But we were
10074 ** allocated pgnoMove. If required (i.e. if it was not allocated
10075 ** by extending the file), the current page at position pgnoMove
10076 ** is already journaled.
10077 */
drheeb844a2009-08-08 18:01:0710078 u8 eType = 0;
10079 Pgno iPtrPage = 0;
danielk1977003ba062004-11-04 02:57:3310080
danf7679ad2013-04-03 11:38:3610081 /* Save the positions of any open cursors. This is required in
10082 ** case they are holding a reference to an xFetch reference
10083 ** corresponding to page pgnoRoot. */
10084 rc = saveAllCursors(pBt, 0, 0);
danielk1977003ba062004-11-04 02:57:3310085 releasePage(pPageMove);
danf7679ad2013-04-03 11:38:3610086 if( rc!=SQLITE_OK ){
10087 return rc;
10088 }
danielk1977f35843b2007-04-07 15:03:1710089
10090 /* Move the page currently at pgnoRoot to pgnoMove. */
drhb00fc3b2013-08-21 23:42:3210091 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:3310092 if( rc!=SQLITE_OK ){
10093 return rc;
10094 }
10095 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drh27731d72009-06-22 12:05:1010096 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
drh50f9caf2024-02-02 18:42:0910097 rc = SQLITE_CORRUPT_PGNO(pgnoRoot);
drh27731d72009-06-22 12:05:1010098 }
10099 if( rc!=SQLITE_OK ){
danielk1977003ba062004-11-04 02:57:3310100 releasePage(pRoot);
10101 return rc;
10102 }
drhccae6022005-02-26 17:31:2610103 assert( eType!=PTRMAP_ROOTPAGE );
10104 assert( eType!=PTRMAP_FREEPAGE );
danielk19774c999992008-07-16 18:17:5510105 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
danielk1977003ba062004-11-04 02:57:3310106 releasePage(pRoot);
danielk1977f35843b2007-04-07 15:03:1710107
10108 /* Obtain the page at pgnoRoot */
danielk1977003ba062004-11-04 02:57:3310109 if( rc!=SQLITE_OK ){
10110 return rc;
10111 }
drhb00fc3b2013-08-21 23:42:3210112 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:3310113 if( rc!=SQLITE_OK ){
10114 return rc;
10115 }
danielk19773b8a05f2007-03-19 17:44:2610116 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:3310117 if( rc!=SQLITE_OK ){
10118 releasePage(pRoot);
10119 return rc;
10120 }
10121 }else{
10122 pRoot = pPageMove;
larrybrbc917382023-06-07 08:40:3110123 }
danielk1977003ba062004-11-04 02:57:3310124
danielk197742741be2005-01-08 12:42:3910125 /* Update the pointer-map and meta-data with the new root-page number. */
drh98add2e2009-07-20 17:11:4910126 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
danielk1977003ba062004-11-04 02:57:3310127 if( rc ){
10128 releasePage(pRoot);
10129 return rc;
10130 }
drhbf592832010-03-30 15:51:1210131
10132 /* When the new root page was allocated, page 1 was made writable in
10133 ** order either to increase the database filesize, or to decrement the
10134 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
10135 */
10136 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
danielk1977aef0bf62005-12-30 16:28:0110137 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
drhbf592832010-03-30 15:51:1210138 if( NEVER(rc) ){
danielk1977003ba062004-11-04 02:57:3310139 releasePage(pRoot);
10140 return rc;
10141 }
danielk197742741be2005-01-08 12:42:3910142
danielk1977003ba062004-11-04 02:57:3310143 }else{
drh4f0c5872007-03-26 22:05:0110144 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:3310145 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:4110146 }
10147#endif
danielk19773b8a05f2007-03-19 17:44:2610148 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhd4187c72010-08-30 22:15:4510149 if( createTabFlags & BTREE_INTKEY ){
10150 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
10151 }else{
10152 ptfFlags = PTF_ZERODATA | PTF_LEAF;
10153 }
10154 zeroPage(pRoot, ptfFlags);
danielk19773b8a05f2007-03-19 17:44:2610155 sqlite3PagerUnref(pRoot->pDbPage);
drhd4187c72010-08-30 22:15:4510156 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
drhabc38152020-07-22 13:38:0410157 *piTable = pgnoRoot;
drh8b2f49b2001-06-08 00:21:5210158 return SQLITE_OK;
10159}
drhabc38152020-07-22 13:38:0410160int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
drhd677b3d2007-08-20 22:48:4110161 int rc;
10162 sqlite3BtreeEnter(p);
10163 rc = btreeCreateTable(p, piTable, flags);
10164 sqlite3BtreeLeave(p);
10165 return rc;
10166}
drh8b2f49b2001-06-08 00:21:5210167
10168/*
10169** Erase the given database page and all its children. Return
10170** the page to the freelist.
10171*/
drh4b70f112004-05-02 21:12:1910172static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:0110173 BtShared *pBt, /* The BTree that contains the table */
drh7ab641f2009-11-24 02:37:0210174 Pgno pgno, /* Page number to clear */
10175 int freePageFlag, /* Deallocate page if true */
dan2c718872021-06-22 18:32:0510176 i64 *pnChange /* Add number of Cells freed to this counter */
drh4b70f112004-05-02 21:12:1910177){
danielk1977146ba992009-07-22 14:08:1310178 MemPage *pPage;
drh8b2f49b2001-06-08 00:21:5210179 int rc;
drh4b70f112004-05-02 21:12:1910180 unsigned char *pCell;
10181 int i;
dan8ce71842014-01-14 20:14:0910182 int hdr;
drh80159da2016-12-09 17:32:5110183 CellInfo info;
drh8b2f49b2001-06-08 00:21:5210184
drh1fee73e2007-08-29 04:00:5710185 assert( sqlite3_mutex_held(pBt->mutex) );
drhb1299152010-03-30 22:58:3310186 if( pgno>btreePagecount(pBt) ){
drh50f9caf2024-02-02 18:42:0910187 return SQLITE_CORRUPT_PGNO(pgno);
danielk1977a1cb1832005-02-12 08:59:5510188 }
drhd4170ac2023-06-22 01:03:3910189 rc = getAndInitPage(pBt, pgno, &pPage, 0);
danielk1977146ba992009-07-22 14:08:1310190 if( rc ) return rc;
larrybrbc917382023-06-07 08:40:3110191 if( (pBt->openFlags & BTREE_SINGLE)==0
drh9a4e8862022-02-14 18:18:5610192 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
dan1273d692021-10-16 17:09:3610193 ){
drh50f9caf2024-02-02 18:42:0910194 rc = SQLITE_CORRUPT_PAGE(pPage);
drhccf46d02015-04-01 13:21:3310195 goto cleardatabasepage_out;
10196 }
dan8ce71842014-01-14 20:14:0910197 hdr = pPage->hdrOffset;
drh4b70f112004-05-02 21:12:1910198 for(i=0; i<pPage->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:4310199 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:1910200 if( !pPage->leaf ){
danielk197762c14b32008-11-19 09:05:2610201 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
danielk19776b456a22005-03-21 04:04:0210202 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:5210203 }
drh86c779f2021-05-15 13:08:4410204 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
danielk19776b456a22005-03-21 04:04:0210205 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:5210206 }
drha34b6762004-05-07 13:30:4210207 if( !pPage->leaf ){
dan8ce71842014-01-14 20:14:0910208 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
danielk19776b456a22005-03-21 04:04:0210209 if( rc ) goto cleardatabasepage_out;
dan020c4f32021-06-22 18:06:2310210 if( pPage->intKey ) pnChange = 0;
drha6df0e62021-06-03 18:51:5110211 }
10212 if( pnChange ){
drhafe028a2015-05-22 13:09:5010213 testcase( !pPage->intKey );
danielk1977c7af4842008-10-27 13:59:3310214 *pnChange += pPage->nCell;
drh2aa679f2001-06-25 02:11:0710215 }
10216 if( freePageFlag ){
drhc314dc72009-07-21 11:52:3410217 freePage(pPage, &rc);
danielk19773b8a05f2007-03-19 17:44:2610218 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
dan8ce71842014-01-14 20:14:0910219 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:0710220 }
danielk19776b456a22005-03-21 04:04:0210221
10222cleardatabasepage_out:
drh4b70f112004-05-02 21:12:1910223 releasePage(pPage);
drh2aa679f2001-06-25 02:11:0710224 return rc;
drh8b2f49b2001-06-08 00:21:5210225}
10226
10227/*
drhab01f612004-05-22 02:55:2310228** Delete all information from a single table in the database. iTable is
10229** the page number of the root of the table. After this routine returns,
10230** the root page is empty, but still exists.
10231**
10232** This routine will fail with SQLITE_LOCKED if there are any open
10233** read cursors on the table. Open write cursors are moved to the
10234** root of the table.
danielk1977c7af4842008-10-27 13:59:3310235**
drha6df0e62021-06-03 18:51:5110236** If pnChange is not NULL, then the integer value pointed to by pnChange
10237** is incremented by the number of entries in the table.
drh8b2f49b2001-06-08 00:21:5210238*/
dan2c718872021-06-22 18:32:0510239int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
drh8b2f49b2001-06-08 00:21:5210240 int rc;
danielk1977aef0bf62005-12-30 16:28:0110241 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:4110242 sqlite3BtreeEnter(p);
drh64022502009-01-09 14:11:0410243 assert( p->inTrans==TRANS_WRITE );
danielk197796d48e92009-06-29 06:00:3710244
drhc046e3e2009-07-15 11:26:4410245 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
drhd60f4f42012-03-23 14:23:5210246
drhc046e3e2009-07-15 11:26:4410247 if( SQLITE_OK==rc ){
drhd60f4f42012-03-23 14:23:5210248 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
10249 ** is the root of a table b-tree - if it is not, the following call is
10250 ** a no-op). */
drh49bb56e2021-05-14 20:01:3610251 if( p->hasIncrblobCur ){
10252 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
10253 }
danielk197762c14b32008-11-19 09:05:2610254 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
drh8b2f49b2001-06-08 00:21:5210255 }
drhd677b3d2007-08-20 22:48:4110256 sqlite3BtreeLeave(p);
10257 return rc;
drh8b2f49b2001-06-08 00:21:5210258}
10259
10260/*
drh079a3072014-03-19 14:10:5510261** Delete all information from the single table that pCur is open on.
10262**
10263** This routine only work for pCur on an ephemeral table.
10264*/
10265int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
10266 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
10267}
10268
10269/*
drh8b2f49b2001-06-08 00:21:5210270** Erase all information in a table and add the root of the table to
10271** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:2310272** page 1) is never added to the freelist.
10273**
10274** This routine will fail with SQLITE_LOCKED if there are any open
10275** cursors on the table.
drh205f48e2004-11-05 00:43:1110276**
10277** If AUTOVACUUM is enabled and the page at iTable is not the last
larrybrbc917382023-06-07 08:40:3110278** root page in the database file, then the last root page
drh205f48e2004-11-05 00:43:1110279** in the database file is moved into the slot formerly occupied by
10280** iTable and that last slot formerly occupied by the last root page
10281** is added to the freelist instead of iTable. In this say, all
10282** root pages are kept at the beginning of the database file, which
larrybrbc917382023-06-07 08:40:3110283** is necessary for AUTOVACUUM to work right. *piMoved is set to the
drh205f48e2004-11-05 00:43:1110284** page number that used to be the last root page in the file before
10285** the move. If no page gets moved, *piMoved is set to 0.
10286** The last root page is recorded in meta[3] and the value of
10287** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:5210288*/
danielk197789d40042008-11-17 14:20:5610289static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:5210290 int rc;
danielk1977a0bf2652004-11-04 14:30:0410291 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:0110292 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:0410293
drh1fee73e2007-08-29 04:00:5710294 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:0410295 assert( p->inTrans==TRANS_WRITE );
drh65f38d92016-11-22 01:26:4210296 assert( iTable>=2 );
drh9a518842019-03-08 01:52:3010297 if( iTable>btreePagecount(pBt) ){
drh50f9caf2024-02-02 18:42:0910298 return SQLITE_CORRUPT_PGNO(iTable);
drh9a518842019-03-08 01:52:3010299 }
drh055f2982016-01-15 15:06:4110300
danielk1977c7af4842008-10-27 13:59:3310301 rc = sqlite3BtreeClearTable(p, iTable, 0);
dan1273d692021-10-16 17:09:3610302 if( rc ) return rc;
10303 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
drhda125362021-10-16 18:53:3610304 if( NEVER(rc) ){
danielk19776b456a22005-03-21 04:04:0210305 releasePage(pPage);
10306 return rc;
10307 }
danielk1977a0bf2652004-11-04 14:30:0410308
drh205f48e2004-11-05 00:43:1110309 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:0410310
danielk1977a0bf2652004-11-04 14:30:0410311#ifdef SQLITE_OMIT_AUTOVACUUM
drh055f2982016-01-15 15:06:4110312 freePage(pPage, &rc);
10313 releasePage(pPage);
danielk1977a0bf2652004-11-04 14:30:0410314#else
drh055f2982016-01-15 15:06:4110315 if( pBt->autoVacuum ){
10316 Pgno maxRootPgno;
10317 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:0410318
drh055f2982016-01-15 15:06:4110319 if( iTable==maxRootPgno ){
10320 /* If the table being dropped is the table with the largest root-page
larrybrbc917382023-06-07 08:40:3110321 ** number in the database, put the root page on the free list.
danielk1977599fcba2004-11-08 07:13:1310322 */
drhc314dc72009-07-21 11:52:3410323 freePage(pPage, &rc);
danielk1977a0bf2652004-11-04 14:30:0410324 releasePage(pPage);
drh055f2982016-01-15 15:06:4110325 if( rc!=SQLITE_OK ){
10326 return rc;
10327 }
10328 }else{
10329 /* The table being dropped does not have the largest root-page
larrybrbc917382023-06-07 08:40:3110330 ** number in the database. So move the page that does into the
drh055f2982016-01-15 15:06:4110331 ** gap left by the deleted root-page.
10332 */
10333 MemPage *pMove;
10334 releasePage(pPage);
10335 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10336 if( rc!=SQLITE_OK ){
10337 return rc;
10338 }
10339 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
10340 releasePage(pMove);
10341 if( rc!=SQLITE_OK ){
10342 return rc;
10343 }
10344 pMove = 0;
10345 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10346 freePage(pMove, &rc);
10347 releasePage(pMove);
10348 if( rc!=SQLITE_OK ){
10349 return rc;
10350 }
10351 *piMoved = maxRootPgno;
danielk1977a0bf2652004-11-04 14:30:0410352 }
drh055f2982016-01-15 15:06:4110353
10354 /* Set the new 'max-root-page' value in the database header. This
10355 ** is the old value less one, less one more if that happens to
10356 ** be a root-page number, less one again if that is the
10357 ** PENDING_BYTE_PAGE.
drhc046e3e2009-07-15 11:26:4410358 */
drh055f2982016-01-15 15:06:4110359 maxRootPgno--;
10360 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
10361 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
10362 maxRootPgno--;
10363 }
10364 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
10365
10366 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
10367 }else{
10368 freePage(pPage, &rc);
danielk1977a0bf2652004-11-04 14:30:0410369 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:5210370 }
drh055f2982016-01-15 15:06:4110371#endif
larrybrbc917382023-06-07 08:40:3110372 return rc;
drh8b2f49b2001-06-08 00:21:5210373}
drhd677b3d2007-08-20 22:48:4110374int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
10375 int rc;
10376 sqlite3BtreeEnter(p);
dan7733a4d2011-09-02 18:03:1610377 rc = btreeDropTable(p, iTable, piMoved);
drhd677b3d2007-08-20 22:48:4110378 sqlite3BtreeLeave(p);
10379 return rc;
10380}
drh8b2f49b2001-06-08 00:21:5210381
drh001bbcb2003-03-19 03:14:0010382
drh8b2f49b2001-06-08 00:21:5210383/*
danielk1977602b4662009-07-02 07:47:3310384** This function may only be called if the b-tree connection already
10385** has a read or write transaction open on the database.
10386**
drh23e11ca2004-05-04 17:27:2810387** Read the meta-information out of a database file. Meta[0]
10388** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:3110389** through meta[15] are available for use by higher layers. Meta[0]
10390** is read-only, the others are read/write.
larrybrbc917382023-06-07 08:40:3110391**
drha3b321d2004-05-11 09:31:3110392** The schema layer numbers meta values differently. At the schema
10393** layer (and the SetCookie and ReadCookie opcodes) the number of
10394** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh91618562014-12-19 19:28:0210395**
10396** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
10397** of reading the value out of the header, it instead loads the "DataVersion"
10398** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
10399** database file. It is a number computed by the pager. But its access
10400** pattern is the same as header meta values, and so it is convenient to
10401** read it from this routine.
drh8b2f49b2001-06-08 00:21:5210402*/
danielk1977602b4662009-07-02 07:47:3310403void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
danielk1977aef0bf62005-12-30 16:28:0110404 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:5210405
drhd677b3d2007-08-20 22:48:4110406 sqlite3BtreeEnter(p);
danielk1977602b4662009-07-02 07:47:3310407 assert( p->inTrans>TRANS_NONE );
drh346a70c2020-06-15 20:27:3510408 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) );
danielk1977602b4662009-07-02 07:47:3310409 assert( pBt->pPage1 );
drh23e11ca2004-05-04 17:27:2810410 assert( idx>=0 && idx<=15 );
danielk1977ea897302008-09-19 15:10:5810411
drh91618562014-12-19 19:28:0210412 if( idx==BTREE_DATA_VERSION ){
drh2b994ce2021-03-18 12:36:0910413 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
drh91618562014-12-19 19:28:0210414 }else{
10415 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
10416 }
drhae157872004-08-14 19:20:0910417
danielk1977602b4662009-07-02 07:47:3310418 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
10419 ** database, mark the database as read-only. */
danielk1977003ba062004-11-04 02:57:3310420#ifdef SQLITE_OMIT_AUTOVACUUM
drhc9166342012-01-05 23:32:0610421 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
10422 pBt->btsFlags |= BTS_READ_ONLY;
10423 }
danielk1977003ba062004-11-04 02:57:3310424#endif
drhae157872004-08-14 19:20:0910425
drhd677b3d2007-08-20 22:48:4110426 sqlite3BtreeLeave(p);
drh8b2f49b2001-06-08 00:21:5210427}
10428
10429/*
drh23e11ca2004-05-04 17:27:2810430** Write meta-information back into the database. Meta[0] is
10431** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:5210432*/
danielk1977aef0bf62005-12-30 16:28:0110433int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
10434 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:1910435 unsigned char *pP1;
drha34b6762004-05-07 13:30:4210436 int rc;
drh23e11ca2004-05-04 17:27:2810437 assert( idx>=1 && idx<=15 );
drhd677b3d2007-08-20 22:48:4110438 sqlite3BtreeEnter(p);
drh64022502009-01-09 14:11:0410439 assert( p->inTrans==TRANS_WRITE );
10440 assert( pBt->pPage1!=0 );
10441 pP1 = pBt->pPage1->aData;
10442 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
10443 if( rc==SQLITE_OK ){
10444 put4byte(&pP1[36 + idx*4], iMeta);
danielk19774152e672007-09-12 17:01:4510445#ifndef SQLITE_OMIT_AUTOVACUUM
danielk19770d19f7a2009-06-03 11:25:0710446 if( idx==BTREE_INCR_VACUUM ){
drh64022502009-01-09 14:11:0410447 assert( pBt->autoVacuum || iMeta==0 );
10448 assert( iMeta==0 || iMeta==1 );
10449 pBt->incrVacuum = (u8)iMeta;
drhd677b3d2007-08-20 22:48:4110450 }
drh64022502009-01-09 14:11:0410451#endif
drh5df72a52002-06-06 23:16:0510452 }
drhd677b3d2007-08-20 22:48:4110453 sqlite3BtreeLeave(p);
10454 return rc;
drh8b2f49b2001-06-08 00:21:5210455}
drh8c42ca92001-06-22 19:15:0010456
danielk1977a5533162009-02-24 10:01:5110457/*
10458** The first argument, pCur, is a cursor opened on some b-tree. Count the
10459** number of entries in the b-tree and write the result to *pnEntry.
10460**
larrybrbc917382023-06-07 08:40:3110461** SQLITE_OK is returned if the operation is successfully executed.
danielk1977a5533162009-02-24 10:01:5110462** Otherwise, if an error is encountered (i.e. an IO error or database
10463** corruption) an SQLite error code is returned.
10464*/
drh21f6daa2019-10-11 14:21:4810465int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
danielk1977a5533162009-02-24 10:01:5110466 i64 nEntry = 0; /* Value to return in *pnEntry */
10467 int rc; /* Return code */
dana205a482011-08-27 18:48:5710468
drh44548e72017-08-14 18:13:5210469 rc = moveToRoot(pCur);
10470 if( rc==SQLITE_EMPTY ){
dana205a482011-08-27 18:48:5710471 *pnEntry = 0;
10472 return SQLITE_OK;
10473 }
danielk1977a5533162009-02-24 10:01:5110474
10475 /* Unless an error occurs, the following loop runs one iteration for each
larrybrbc917382023-06-07 08:40:3110476 ** page in the B-Tree structure (not including overflow pages).
danielk1977a5533162009-02-24 10:01:5110477 */
dan892edb62020-03-30 13:35:0510478 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
danielk1977a5533162009-02-24 10:01:5110479 int iIdx; /* Index of child node in parent */
10480 MemPage *pPage; /* Current page of the b-tree */
10481
larrybrbc917382023-06-07 08:40:3110482 /* If this is a leaf page or the tree is not an int-key tree, then
danielk1977a5533162009-02-24 10:01:5110483 ** this page contains countable entries. Increment the entry counter
10484 ** accordingly.
10485 */
drh352a35a2017-08-15 03:46:4710486 pPage = pCur->pPage;
danielk1977a5533162009-02-24 10:01:5110487 if( pPage->leaf || !pPage->intKey ){
10488 nEntry += pPage->nCell;
10489 }
10490
larrybrbc917382023-06-07 08:40:3110491 /* pPage is a leaf node. This loop navigates the cursor so that it
danielk1977a5533162009-02-24 10:01:5110492 ** points to the first interior cell that it points to the parent of
10493 ** the next page in the tree that has not yet been visited. The
10494 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
10495 ** of the page, or to the number of cells in the page if the next page
10496 ** to visit is the right-child of its parent.
10497 **
10498 ** If all pages in the tree have been visited, return SQLITE_OK to the
10499 ** caller.
10500 */
10501 if( pPage->leaf ){
10502 do {
10503 if( pCur->iPage==0 ){
10504 /* All pages of the b-tree have been visited. Return successfully. */
10505 *pnEntry = nEntry;
drh7efa4262014-12-16 00:08:3110506 return moveToRoot(pCur);
danielk1977a5533162009-02-24 10:01:5110507 }
danielk197730548662009-07-09 05:07:3710508 moveToParent(pCur);
drh352a35a2017-08-15 03:46:4710509 }while ( pCur->ix>=pCur->pPage->nCell );
danielk1977a5533162009-02-24 10:01:5110510
drh75e96b32017-04-01 00:20:0610511 pCur->ix++;
drh352a35a2017-08-15 03:46:4710512 pPage = pCur->pPage;
danielk1977a5533162009-02-24 10:01:5110513 }
10514
larrybrbc917382023-06-07 08:40:3110515 /* Descend to the child node of the cell that the cursor currently
danielk1977a5533162009-02-24 10:01:5110516 ** points at. This is the right-child if (iIdx==pPage->nCell).
10517 */
drh75e96b32017-04-01 00:20:0610518 iIdx = pCur->ix;
danielk1977a5533162009-02-24 10:01:5110519 if( iIdx==pPage->nCell ){
10520 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
10521 }else{
10522 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
10523 }
10524 }
10525
shanebe217792009-03-05 04:20:3110526 /* An error has occurred. Return an error code. */
danielk1977a5533162009-02-24 10:01:5110527 return rc;
10528}
drhdd793422001-06-28 01:54:4810529
drhdd793422001-06-28 01:54:4810530/*
drh5eddca62001-06-30 21:53:5310531** Return the pager associated with a BTree. This routine is used for
10532** testing and debugging only.
drhdd793422001-06-28 01:54:4810533*/
danielk1977aef0bf62005-12-30 16:28:0110534Pager *sqlite3BtreePager(Btree *p){
10535 return p->pBt->pPager;
drhdd793422001-06-28 01:54:4810536}
drh5eddca62001-06-30 21:53:5310537
drhb7f91642004-10-31 02:22:4710538#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:5310539/*
drh5dd74bf2023-01-11 16:17:3110540** Record an OOM error during integrity_check
10541*/
10542static void checkOom(IntegrityCk *pCheck){
10543 pCheck->rc = SQLITE_NOMEM;
10544 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
10545 if( pCheck->nErr==0 ) pCheck->nErr++;
10546}
10547
10548/*
10549** Invoke the progress handler, if appropriate. Also check for an
10550** interrupt.
10551*/
10552static void checkProgress(IntegrityCk *pCheck){
10553 sqlite3 *db = pCheck->db;
10554 if( AtomicLoad(&db->u1.isInterrupted) ){
10555 pCheck->rc = SQLITE_INTERRUPT;
10556 pCheck->nErr++;
10557 pCheck->mxErr = 0;
10558 }
10559#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10560 if( db->xProgress ){
10561 assert( db->nProgressOps>0 );
10562 pCheck->nStep++;
10563 if( (pCheck->nStep % db->nProgressOps)==0
10564 && db->xProgress(db->pProgressArg)
10565 ){
10566 pCheck->rc = SQLITE_INTERRUPT;
10567 pCheck->nErr++;
10568 pCheck->mxErr = 0;
10569 }
10570 }
10571#endif
10572}
10573
10574/*
drh5eddca62001-06-30 21:53:5310575** Append a message to the error message string.
10576*/
drh2e38c322004-09-03 18:38:4410577static void checkAppendMsg(
10578 IntegrityCk *pCheck,
drh2e38c322004-09-03 18:38:4410579 const char *zFormat,
10580 ...
10581){
10582 va_list ap;
drh5dd74bf2023-01-11 16:17:3110583 checkProgress(pCheck);
drh1dcdbc02007-01-27 02:24:5410584 if( !pCheck->mxErr ) return;
10585 pCheck->mxErr--;
10586 pCheck->nErr++;
drh2e38c322004-09-03 18:38:4410587 va_start(ap, zFormat);
drhf089aa42008-07-08 19:34:0610588 if( pCheck->errMsg.nChar ){
drh0cdbe1a2018-05-09 13:46:2610589 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
drh5eddca62001-06-30 21:53:5310590 }
drh867db832014-09-26 02:41:0510591 if( pCheck->zPfx ){
drhe5ea81a2023-04-01 13:14:5310592 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
10593 pCheck->v0, pCheck->v1, pCheck->v2);
drhf089aa42008-07-08 19:34:0610594 }
drh0cdbe1a2018-05-09 13:46:2610595 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
drhf089aa42008-07-08 19:34:0610596 va_end(ap);
drh0cdbe1a2018-05-09 13:46:2610597 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
drh5dd74bf2023-01-11 16:17:3110598 checkOom(pCheck);
drhc890fec2008-08-01 20:10:0810599 }
drh5eddca62001-06-30 21:53:5310600}
drhb7f91642004-10-31 02:22:4710601#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:5310602
drhb7f91642004-10-31 02:22:4710603#ifndef SQLITE_OMIT_INTEGRITY_CHECK
dan1235bb12012-04-03 17:43:2810604
10605/*
10606** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
10607** corresponds to page iPg is already set.
10608*/
10609static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
drhc0ba6a92023-10-24 11:06:4410610 assert( pCheck->aPgRef!=0 );
drh266e5d02023-10-21 11:06:0310611 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
dan1235bb12012-04-03 17:43:2810612 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
10613}
10614
10615/*
10616** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
10617*/
10618static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
drhc0ba6a92023-10-24 11:06:4410619 assert( pCheck->aPgRef!=0 );
drh266e5d02023-10-21 11:06:0310620 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
dan1235bb12012-04-03 17:43:2810621 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
10622}
10623
10624
drh5eddca62001-06-30 21:53:5310625/*
10626** Add 1 to the reference count for page iPage. If this is the second
10627** reference to the page, add an error message to pCheck->zErrMsg.
peter.d.reid60ec9142014-09-06 16:39:4610628** Return 1 if there are 2 or more references to the page and 0 if
drh5eddca62001-06-30 21:53:5310629** if this is the first reference to the page.
10630**
10631** Also check that the page number is in bounds.
10632*/
drh867db832014-09-26 02:41:0510633static int checkRef(IntegrityCk *pCheck, Pgno iPage){
drh266e5d02023-10-21 11:06:0310634 if( iPage>pCheck->nCkPage || iPage==0 ){
drh2e89f1c2023-04-01 12:22:5710635 checkAppendMsg(pCheck, "invalid page number %u", iPage);
drh5eddca62001-06-30 21:53:5310636 return 1;
10637 }
dan1235bb12012-04-03 17:43:2810638 if( getPageReferenced(pCheck, iPage) ){
drh2e89f1c2023-04-01 12:22:5710639 checkAppendMsg(pCheck, "2nd reference to page %u", iPage);
drh5eddca62001-06-30 21:53:5310640 return 1;
10641 }
dan1235bb12012-04-03 17:43:2810642 setPageReferenced(pCheck, iPage);
10643 return 0;
drh5eddca62001-06-30 21:53:5310644}
10645
danielk1977afcdd022004-10-31 16:25:4210646#ifndef SQLITE_OMIT_AUTOVACUUM
10647/*
larrybrbc917382023-06-07 08:40:3110648** Check that the entry in the pointer-map for page iChild maps to
danielk1977afcdd022004-10-31 16:25:4210649** page iParent, pointer type ptrType. If not, append an error message
10650** to pCheck.
10651*/
10652static void checkPtrmap(
10653 IntegrityCk *pCheck, /* Integrity check context */
10654 Pgno iChild, /* Child page number */
10655 u8 eType, /* Expected pointer map type */
drh867db832014-09-26 02:41:0510656 Pgno iParent /* Expected pointer map parent page number */
danielk1977afcdd022004-10-31 16:25:4210657){
10658 int rc;
10659 u8 ePtrmapType;
10660 Pgno iPtrmapParent;
10661
10662 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
10663 if( rc!=SQLITE_OK ){
drh5dd74bf2023-01-11 16:17:3110664 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) checkOom(pCheck);
drh2e89f1c2023-04-01 12:22:5710665 checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild);
danielk1977afcdd022004-10-31 16:25:4210666 return;
10667 }
10668
10669 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
drh867db832014-09-26 02:41:0510670 checkAppendMsg(pCheck,
larrybrbc917382023-06-07 08:40:3110671 "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)",
danielk1977afcdd022004-10-31 16:25:4210672 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
10673 }
10674}
10675#endif
10676
drh5eddca62001-06-30 21:53:5310677/*
10678** Check the integrity of the freelist or of an overflow page list.
10679** Verify that the number of pages on the list is N.
10680*/
drh30e58752002-03-02 20:41:5710681static void checkList(
10682 IntegrityCk *pCheck, /* Integrity checking context */
10683 int isFreeList, /* True for a freelist. False for overflow page list */
drhabc38152020-07-22 13:38:0410684 Pgno iPage, /* Page number for first page in the list */
drheaac9992019-02-26 16:17:0610685 u32 N /* Expected number of pages in the list */
drh30e58752002-03-02 20:41:5710686){
10687 int i;
drheaac9992019-02-26 16:17:0610688 u32 expected = N;
drh91d58662018-07-20 13:39:2810689 int nErrAtStart = pCheck->nErr;
10690 while( iPage!=0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:2610691 DbPage *pOvflPage;
10692 unsigned char *pOvflData;
drh867db832014-09-26 02:41:0510693 if( checkRef(pCheck, iPage) ) break;
drh91d58662018-07-20 13:39:2810694 N--;
drh9584f582015-11-04 20:22:3710695 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
drh2e89f1c2023-04-01 12:22:5710696 checkAppendMsg(pCheck, "failed to get page %u", iPage);
drh5eddca62001-06-30 21:53:5310697 break;
10698 }
danielk19773b8a05f2007-03-19 17:44:2610699 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:5710700 if( isFreeList ){
drhae104742018-12-14 17:57:0110701 u32 n = (u32)get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:4110702#ifndef SQLITE_OMIT_AUTOVACUUM
10703 if( pCheck->pBt->autoVacuum ){
drh867db832014-09-26 02:41:0510704 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
danielk1977687566d2004-11-02 12:56:4110705 }
10706#endif
drhae104742018-12-14 17:57:0110707 if( n>pCheck->pBt->usableSize/4-2 ){
drh867db832014-09-26 02:41:0510708 checkAppendMsg(pCheck,
drh2e89f1c2023-04-01 12:22:5710709 "freelist leaf count too big on page %u", iPage);
drhee696e22004-08-30 16:52:1710710 N--;
10711 }else{
drhae104742018-12-14 17:57:0110712 for(i=0; i<(int)n; i++){
danielk19773b8a05f2007-03-19 17:44:2610713 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:4110714#ifndef SQLITE_OMIT_AUTOVACUUM
10715 if( pCheck->pBt->autoVacuum ){
drh867db832014-09-26 02:41:0510716 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
danielk1977687566d2004-11-02 12:56:4110717 }
10718#endif
drh867db832014-09-26 02:41:0510719 checkRef(pCheck, iFreePage);
drhee696e22004-08-30 16:52:1710720 }
10721 N -= n;
drh30e58752002-03-02 20:41:5710722 }
drh30e58752002-03-02 20:41:5710723 }
danielk1977afcdd022004-10-31 16:25:4210724#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:4110725 else{
10726 /* If this database supports auto-vacuum and iPage is not the last
10727 ** page in this overflow list, check that the pointer-map entry for
10728 ** the following page matches iPage.
10729 */
10730 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:2610731 i = get4byte(pOvflData);
drh867db832014-09-26 02:41:0510732 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
danielk1977687566d2004-11-02 12:56:4110733 }
danielk1977afcdd022004-10-31 16:25:4210734 }
10735#endif
danielk19773b8a05f2007-03-19 17:44:2610736 iPage = get4byte(pOvflData);
10737 sqlite3PagerUnref(pOvflPage);
drh91d58662018-07-20 13:39:2810738 }
10739 if( N && nErrAtStart==pCheck->nErr ){
10740 checkAppendMsg(pCheck,
drh2e89f1c2023-04-01 12:22:5710741 "%s is %u but should be %u",
drh91d58662018-07-20 13:39:2810742 isFreeList ? "size" : "overflow list length",
10743 expected-N, expected);
drh5eddca62001-06-30 21:53:5310744 }
10745}
drhb7f91642004-10-31 02:22:4710746#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:5310747
drh67731a92015-04-16 11:56:0310748/*
10749** An implementation of a min-heap.
10750**
10751** aHeap[0] is the number of elements on the heap. aHeap[1] is the
drha33b6832015-04-16 21:57:3710752** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
drh67731a92015-04-16 11:56:0310753** and aHeap[N*2+1].
10754**
10755** The heap property is this: Every node is less than or equal to both
10756** of its daughter nodes. A consequence of the heap property is that the
drh42c0a2b2015-04-28 01:28:3610757** root node aHeap[1] is always the minimum value currently in the heap.
drh67731a92015-04-16 11:56:0310758**
10759** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
10760** the heap, preserving the heap property. The btreeHeapPull() routine
10761** removes the root element from the heap (the minimum value in the heap)
drh42c0a2b2015-04-28 01:28:3610762** and then moves other nodes around as necessary to preserve the heap
drh67731a92015-04-16 11:56:0310763** property.
10764**
10765** This heap is used for cell overlap and coverage testing. Each u32
larrybrbc917382023-06-07 08:40:3110766** entry represents the span of a cell or freeblock on a btree page.
drh67731a92015-04-16 11:56:0310767** The upper 16 bits are the index of the first byte of a range and the
10768** lower 16 bits are the index of the last byte of that range.
10769*/
10770static void btreeHeapInsert(u32 *aHeap, u32 x){
drhf92b0062023-01-27 20:15:4810771 u32 j, i;
10772 assert( aHeap!=0 );
10773 i = ++aHeap[0];
drh67731a92015-04-16 11:56:0310774 aHeap[i] = x;
drha33b6832015-04-16 21:57:3710775 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
drh67731a92015-04-16 11:56:0310776 x = aHeap[j];
10777 aHeap[j] = aHeap[i];
10778 aHeap[i] = x;
10779 i = j;
10780 }
10781}
10782static int btreeHeapPull(u32 *aHeap, u32 *pOut){
10783 u32 j, i, x;
10784 if( (x = aHeap[0])==0 ) return 0;
10785 *pOut = aHeap[1];
10786 aHeap[1] = aHeap[x];
10787 aHeap[x] = 0xffffffff;
10788 aHeap[0]--;
10789 i = 1;
10790 while( (j = i*2)<=aHeap[0] ){
10791 if( aHeap[j]>aHeap[j+1] ) j++;
10792 if( aHeap[i]<aHeap[j] ) break;
10793 x = aHeap[i];
10794 aHeap[i] = aHeap[j];
10795 aHeap[j] = x;
10796 i = j;
10797 }
larrybrbc917382023-06-07 08:40:3110798 return 1;
drh67731a92015-04-16 11:56:0310799}
10800
drhb7f91642004-10-31 02:22:4710801#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:5310802/*
10803** Do various sanity checks on a single page of a tree. Return
10804** the tree depth. Root pages return 0. Parents of root pages
10805** return 1, and so forth.
larrybrbc917382023-06-07 08:40:3110806**
drh5eddca62001-06-30 21:53:5310807** These checks are done:
10808**
10809** 1. Make sure that cells and freeblocks do not overlap
10810** but combine to completely cover the page.
drhe05b3f82015-07-01 17:53:4910811** 2. Make sure integer cell keys are in order.
10812** 3. Check the integrity of overflow pages.
10813** 4. Recursively call checkTreePage on all children.
10814** 5. Verify that the depth of all children is the same.
drh5eddca62001-06-30 21:53:5310815*/
10816static int checkTreePage(
drhaaab5722002-02-19 13:39:2110817 IntegrityCk *pCheck, /* Context for the sanity check */
drhabc38152020-07-22 13:38:0410818 Pgno iPage, /* Page number of the page to check */
drhcbc6b712015-07-02 16:17:3010819 i64 *piMinKey, /* Write minimum integer primary key here */
10820 i64 maxKey /* Error if integer primary key greater than this */
drh5eddca62001-06-30 21:53:5310821){
drhcbc6b712015-07-02 16:17:3010822 MemPage *pPage = 0; /* The page being analyzed */
10823 int i; /* Loop counter */
10824 int rc; /* Result code from subroutine call */
10825 int depth = -1, d2; /* Depth of a subtree */
10826 int pgno; /* Page number */
10827 int nFrag; /* Number of fragmented bytes on the page */
10828 int hdr; /* Offset to the page header */
10829 int cellStart; /* Offset to the start of the cell pointer array */
10830 int nCell; /* Number of cells */
10831 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
10832 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
10833 ** False if IPK must be strictly less than maxKey */
10834 u8 *data; /* Page content */
10835 u8 *pCell; /* Cell content */
10836 u8 *pCellIdx; /* Next element of the cell pointer array */
10837 BtShared *pBt; /* The BtShared object that owns pPage */
10838 u32 pc; /* Address of a cell */
10839 u32 usableSize; /* Usable size of the page */
10840 u32 contentOffset; /* Offset to the start of the cell content area */
10841 u32 *heap = 0; /* Min-heap used for checking cell coverage */
drhd2dc87f2015-07-02 19:47:0810842 u32 x, prev = 0; /* Next and previous entry on the min-heap */
drh867db832014-09-26 02:41:0510843 const char *saved_zPfx = pCheck->zPfx;
10844 int saved_v1 = pCheck->v1;
10845 int saved_v2 = pCheck->v2;
mistachkin532f1792015-07-14 17:18:0510846 u8 savedIsInit = 0;
danielk1977ef73ee92004-11-06 12:26:0710847
drh5eddca62001-06-30 21:53:5310848 /* Check that the page exists
10849 */
drh5dd74bf2023-01-11 16:17:3110850 checkProgress(pCheck);
10851 if( pCheck->mxErr==0 ) goto end_of_check;
drhd9cb6ac2005-10-20 07:28:1710852 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:1110853 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:5310854 if( iPage==0 ) return 0;
drh867db832014-09-26 02:41:0510855 if( checkRef(pCheck, iPage) ) return 0;
drhe5ea81a2023-04-01 13:14:5310856 pCheck->zPfx = "Tree %u page %u: ";
drh2c6c98d2023-05-18 23:25:4810857 pCheck->v1 = iPage;
drhabc38152020-07-22 13:38:0410858 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
drh867db832014-09-26 02:41:0510859 checkAppendMsg(pCheck,
drh2e38c322004-09-03 18:38:4410860 "unable to get the page. error code=%d", rc);
drh9132b882023-10-13 22:19:2310861 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
drh867db832014-09-26 02:41:0510862 goto end_of_check;
drh5eddca62001-06-30 21:53:5310863 }
danielk197793caf5a2009-07-11 06:55:3310864
10865 /* Clear MemPage.isInit to make sure the corruption detection code in
10866 ** btreeInitPage() is executed. */
drh72e191e2015-07-04 11:14:2010867 savedIsInit = pPage->isInit;
danielk197793caf5a2009-07-11 06:55:3310868 pPage->isInit = 0;
danielk197730548662009-07-09 05:07:3710869 if( (rc = btreeInitPage(pPage))!=0 ){
drh64022502009-01-09 14:11:0410870 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
drh867db832014-09-26 02:41:0510871 checkAppendMsg(pCheck,
danielk197730548662009-07-09 05:07:3710872 "btreeInitPage() returns error code %d", rc);
drh867db832014-09-26 02:41:0510873 goto end_of_check;
drh5eddca62001-06-30 21:53:5310874 }
drhb0ea9432019-02-09 21:06:4010875 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
10876 assert( rc==SQLITE_CORRUPT );
10877 checkAppendMsg(pCheck, "free space corruption", rc);
10878 goto end_of_check;
10879 }
drhcbc6b712015-07-02 16:17:3010880 data = pPage->aData;
10881 hdr = pPage->hdrOffset;
drh5eddca62001-06-30 21:53:5310882
drhcbc6b712015-07-02 16:17:3010883 /* Set up for cell analysis */
drhe5ea81a2023-04-01 13:14:5310884 pCheck->zPfx = "Tree %u page %u cell %u: ";
drhcbc6b712015-07-02 16:17:3010885 contentOffset = get2byteNotZero(&data[hdr+5]);
10886 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
10887
10888 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
10889 ** number of cells on the page. */
10890 nCell = get2byte(&data[hdr+3]);
10891 assert( pPage->nCell==nCell );
dand90ecb52024-02-02 16:51:2410892 if( pPage->leaf || pPage->intKey==0 ){
10893 pCheck->nRow += nCell;
10894 }
drhcbc6b712015-07-02 16:17:3010895
10896 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
10897 ** immediately follows the b-tree page header. */
10898 cellStart = hdr + 12 - 4*pPage->leaf;
10899 assert( pPage->aCellIdx==&data[cellStart] );
10900 pCellIdx = &data[cellStart + 2*(nCell-1)];
10901
10902 if( !pPage->leaf ){
10903 /* Analyze the right-child page of internal pages */
10904 pgno = get4byte(&data[hdr+8]);
10905#ifndef SQLITE_OMIT_AUTOVACUUM
10906 if( pBt->autoVacuum ){
drhe5ea81a2023-04-01 13:14:5310907 pCheck->zPfx = "Tree %u page %u right child: ";
drhcbc6b712015-07-02 16:17:3010908 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
10909 }
10910#endif
10911 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10912 keyCanBeEqual = 0;
10913 }else{
10914 /* For leaf pages, the coverage check will occur in the same loop
10915 ** as the other cell checks, so initialize the heap. */
10916 heap = pCheck->heap;
10917 heap[0] = 0;
drh5eddca62001-06-30 21:53:5310918 }
10919
drhcbc6b712015-07-02 16:17:3010920 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
10921 ** integer offsets to the cell contents. */
10922 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
drh6f11bef2004-05-13 01:12:5610923 CellInfo info;
drh5eddca62001-06-30 21:53:5310924
drhcbc6b712015-07-02 16:17:3010925 /* Check cell size */
drh867db832014-09-26 02:41:0510926 pCheck->v2 = i;
drhcbc6b712015-07-02 16:17:3010927 assert( pCellIdx==&data[cellStart + i*2] );
10928 pc = get2byteAligned(pCellIdx);
10929 pCellIdx -= 2;
10930 if( pc<contentOffset || pc>usableSize-4 ){
drh2e89f1c2023-04-01 12:22:5710931 checkAppendMsg(pCheck, "Offset %u out of range %u..%u",
drhcbc6b712015-07-02 16:17:3010932 pc, contentOffset, usableSize-4);
10933 doCoverageCheck = 0;
10934 continue;
shaneh195475d2010-02-19 04:28:0810935 }
drhcbc6b712015-07-02 16:17:3010936 pCell = &data[pc];
10937 pPage->xParseCell(pPage, pCell, &info);
10938 if( pc+info.nSize>usableSize ){
10939 checkAppendMsg(pCheck, "Extends off end of page");
10940 doCoverageCheck = 0;
10941 continue;
drh5eddca62001-06-30 21:53:5310942 }
10943
drhcbc6b712015-07-02 16:17:3010944 /* Check for integer primary key out of range */
10945 if( pPage->intKey ){
10946 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
10947 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
10948 }
10949 maxKey = info.nKey;
dan4b2667c2017-05-01 18:24:0110950 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
drhcbc6b712015-07-02 16:17:3010951 }
10952
10953 /* Check the content overflow list */
10954 if( info.nPayload>info.nLocal ){
drheaac9992019-02-26 16:17:0610955 u32 nPage; /* Number of pages on the overflow chain */
drhcbc6b712015-07-02 16:17:3010956 Pgno pgnoOvfl; /* First page of the overflow chain */
drh45ac1c72015-12-18 03:59:1610957 assert( pc + info.nSize - 4 <= usableSize );
drhcbc6b712015-07-02 16:17:3010958 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
drh45ac1c72015-12-18 03:59:1610959 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
drhda200cc2004-05-09 11:51:3810960#ifndef SQLITE_OMIT_AUTOVACUUM
10961 if( pBt->autoVacuum ){
drh867db832014-09-26 02:41:0510962 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
drhda200cc2004-05-09 11:51:3810963 }
10964#endif
drh867db832014-09-26 02:41:0510965 checkList(pCheck, 0, pgnoOvfl, nPage);
drh5eddca62001-06-30 21:53:5310966 }
10967
drh5eddca62001-06-30 21:53:5310968 if( !pPage->leaf ){
drhcbc6b712015-07-02 16:17:3010969 /* Check sanity of left child page for internal pages */
drh43605152004-05-29 21:46:4910970 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:4210971#ifndef SQLITE_OMIT_AUTOVACUUM
10972 if( pBt->autoVacuum ){
drh867db832014-09-26 02:41:0510973 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
danielk1977afcdd022004-10-31 16:25:4210974 }
10975#endif
drhcbc6b712015-07-02 16:17:3010976 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10977 keyCanBeEqual = 0;
10978 if( d2!=depth ){
drh867db832014-09-26 02:41:0510979 checkAppendMsg(pCheck, "Child page depth differs");
drhcbc6b712015-07-02 16:17:3010980 depth = d2;
drh5eddca62001-06-30 21:53:5310981 }
drhcbc6b712015-07-02 16:17:3010982 }else{
10983 /* Populate the coverage-checking heap for leaf pages */
10984 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
drh5eddca62001-06-30 21:53:5310985 }
10986 }
drhcbc6b712015-07-02 16:17:3010987 *piMinKey = maxKey;
shaneh195475d2010-02-19 04:28:0810988
drh5eddca62001-06-30 21:53:5310989 /* Check for complete coverage of the page
10990 */
drh867db832014-09-26 02:41:0510991 pCheck->zPfx = 0;
drhcbc6b712015-07-02 16:17:3010992 if( doCoverageCheck && pCheck->mxErr>0 ){
10993 /* For leaf pages, the min-heap has already been initialized and the
10994 ** cells have already been inserted. But for internal pages, that has
10995 ** not yet been done, so do it now */
10996 if( !pPage->leaf ){
10997 heap = pCheck->heap;
10998 heap[0] = 0;
drhcbc6b712015-07-02 16:17:3010999 for(i=nCell-1; i>=0; i--){
drh1910def2015-07-02 16:29:5611000 u32 size;
11001 pc = get2byteAligned(&data[cellStart+i*2]);
11002 size = pPage->xCellSize(pPage, &data[pc]);
drh67731a92015-04-16 11:56:0311003 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
danielk19777701e812005-01-10 12:59:5111004 }
drh2e38c322004-09-03 18:38:4411005 }
drh6324cdf2024-02-27 19:57:3111006 assert( heap!=0 );
drhcbc6b712015-07-02 16:17:3011007 /* Add the freeblocks to the min-heap
11008 **
11009 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
drhfdab0262014-11-20 15:30:5011010 ** is the offset of the first freeblock, or zero if there are no
larrybrbc917382023-06-07 08:40:3111011 ** freeblocks on the page.
drhcbc6b712015-07-02 16:17:3011012 */
drh8c2bbb62009-07-10 02:52:2011013 i = get2byte(&data[hdr+1]);
11014 while( i>0 ){
11015 int size, j;
drh5860a612019-02-12 16:58:2611016 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
drh8c2bbb62009-07-10 02:52:2011017 size = get2byte(&data[i+2]);
drh5860a612019-02-12 16:58:2611018 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
drhe56d4302015-07-08 01:22:5211019 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
drhfdab0262014-11-20 15:30:5011020 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
11021 ** big-endian integer which is the offset in the b-tree page of the next
11022 ** freeblock in the chain, or zero if the freeblock is the last on the
11023 ** chain. */
drh8c2bbb62009-07-10 02:52:2011024 j = get2byte(&data[i]);
drhfdab0262014-11-20 15:30:5011025 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
11026 ** increasing offset. */
drh5860a612019-02-12 16:58:2611027 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
11028 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
drh8c2bbb62009-07-10 02:52:2011029 i = j;
drh2e38c322004-09-03 18:38:4411030 }
larrybrbc917382023-06-07 08:40:3111031 /* Analyze the min-heap looking for overlap between cells and/or
drhcbc6b712015-07-02 16:17:3011032 ** freeblocks, and counting the number of untracked bytes in nFrag.
larrybrbc917382023-06-07 08:40:3111033 **
drhd2dc87f2015-07-02 19:47:0811034 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
11035 ** There is an implied first entry the covers the page header, the cell
11036 ** pointer index, and the gap between the cell pointer index and the start
larrybrbc917382023-06-07 08:40:3111037 ** of cell content.
drhd2dc87f2015-07-02 19:47:0811038 **
11039 ** The loop below pulls entries from the min-heap in order and compares
11040 ** the start_address against the previous end_address. If there is an
11041 ** overlap, that means bytes are used multiple times. If there is a gap,
11042 ** that gap is added to the fragmentation count.
drhcbc6b712015-07-02 16:17:3011043 */
11044 nFrag = 0;
drhd2dc87f2015-07-02 19:47:0811045 prev = contentOffset - 1; /* Implied first min-heap entry */
drh67731a92015-04-16 11:56:0311046 while( btreeHeapPull(heap,&x) ){
drhd2dc87f2015-07-02 19:47:0811047 if( (prev&0xffff)>=(x>>16) ){
drh867db832014-09-26 02:41:0511048 checkAppendMsg(pCheck,
drhabc38152020-07-22 13:38:0411049 "Multiple uses for byte %u of page %u", x>>16, iPage);
drh2e38c322004-09-03 18:38:4411050 break;
drh67731a92015-04-16 11:56:0311051 }else{
drhcbc6b712015-07-02 16:17:3011052 nFrag += (x>>16) - (prev&0xffff) - 1;
drh67731a92015-04-16 11:56:0311053 prev = x;
drh2e38c322004-09-03 18:38:4411054 }
11055 }
drhcbc6b712015-07-02 16:17:3011056 nFrag += usableSize - (prev&0xffff) - 1;
drhfdab0262014-11-20 15:30:5011057 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
11058 ** is stored in the fifth field of the b-tree page header.
11059 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
11060 ** number of fragmented free bytes within the cell content area.
11061 */
drhcbc6b712015-07-02 16:17:3011062 if( heap[0]==0 && nFrag!=data[hdr+7] ){
drh867db832014-09-26 02:41:0511063 checkAppendMsg(pCheck,
drh2e89f1c2023-04-01 12:22:5711064 "Fragmentation of %u bytes reported as %u on page %u",
drhcbc6b712015-07-02 16:17:3011065 nFrag, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:5311066 }
11067 }
drh867db832014-09-26 02:41:0511068
11069end_of_check:
drh72e191e2015-07-04 11:14:2011070 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
drh4b70f112004-05-02 21:12:1911071 releasePage(pPage);
drh867db832014-09-26 02:41:0511072 pCheck->zPfx = saved_zPfx;
11073 pCheck->v1 = saved_v1;
11074 pCheck->v2 = saved_v2;
drhda200cc2004-05-09 11:51:3811075 return depth+1;
drh5eddca62001-06-30 21:53:5311076}
drhb7f91642004-10-31 02:22:4711077#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:5311078
drhb7f91642004-10-31 02:22:4711079#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:5311080/*
11081** This routine does a complete check of the given BTree file. aRoot[] is
11082** an array of pages numbers were each page number is the root page of
11083** a table. nRoot is the number of entries in aRoot.
11084**
danielk19773509a652009-07-06 18:56:1311085** A read-only or read-write transaction must be opened before calling
11086** this function.
11087**
drhc890fec2008-08-01 20:10:0811088** Write the number of error seen in *pnErr. Except for some memory
drhe43ba702008-12-05 22:40:0811089** allocation errors, an error message held in memory obtained from
drhc890fec2008-08-01 20:10:0811090** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
drhe43ba702008-12-05 22:40:0811091** returned. If a memory allocation error occurs, NULL is returned.
drh17d2d592020-07-23 00:45:0611092**
11093** If the first entry in aRoot[] is 0, that indicates that the list of
11094** root pages is incomplete. This is a "partial integrity-check". This
11095** happens when performing an integrity check on a single table. The
11096** zero is skipped, of course. But in addition, the freelist checks
11097** and the checks to make sure every page is referenced are also skipped,
11098** since obviously it is not possible to know which pages are covered by
11099** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
11100** checks are still performed.
drh5eddca62001-06-30 21:53:5311101*/
drh5dd74bf2023-01-11 16:17:3111102int sqlite3BtreeIntegrityCheck(
drh21f6daa2019-10-11 14:21:4811103 sqlite3 *db, /* Database connection that is running the check */
drh1dcdbc02007-01-27 02:24:5411104 Btree *p, /* The btree to be checked */
drhabc38152020-07-22 13:38:0411105 Pgno *aRoot, /* An array of root pages numbers for individual trees */
dand90ecb52024-02-02 16:51:2411106 Mem *aCnt, /* Memory cells to write counts for each tree to */
drh1dcdbc02007-01-27 02:24:5411107 int nRoot, /* Number of entries in aRoot[] */
11108 int mxErr, /* Stop reporting errors after this many */
drh5dd74bf2023-01-11 16:17:3111109 int *pnErr, /* OUT: Write number of errors seen to this variable */
11110 char **pzOut /* OUT: Write the error message string here */
drh1dcdbc02007-01-27 02:24:5411111){
danielk197789d40042008-11-17 14:20:5611112 Pgno i;
drhaaab5722002-02-19 13:39:2111113 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:0111114 BtShared *pBt = p->pBt;
drhf10ce632019-01-11 14:46:4411115 u64 savedDbFlags = pBt->db->flags;
drhf089aa42008-07-08 19:34:0611116 char zErr[100];
drh17d2d592020-07-23 00:45:0611117 int bPartial = 0; /* True if not checking all btrees */
11118 int bCkFreelist = 1; /* True to scan the freelist */
drh8deae5a2020-07-29 12:23:2011119 VVA_ONLY( int nRef );
drh39670a52024-02-27 15:33:5411120
drh17d2d592020-07-23 00:45:0611121 assert( nRoot>0 );
drh39670a52024-02-27 15:33:5411122 assert( aCnt!=0 );
drh17d2d592020-07-23 00:45:0611123
11124 /* aRoot[0]==0 means this is a partial check */
11125 if( aRoot[0]==0 ){
11126 assert( nRoot>1 );
11127 bPartial = 1;
11128 if( aRoot[1]!=1 ) bCkFreelist = 0;
11129 }
drh5eddca62001-06-30 21:53:5311130
drhd677b3d2007-08-20 22:48:4111131 sqlite3BtreeEnter(p);
danielk19773509a652009-07-06 18:56:1311132 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
drhcc5f8a42016-02-06 22:32:0611133 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
11134 assert( nRef>=0 );
drh5dd74bf2023-01-11 16:17:3111135 memset(&sCheck, 0, sizeof(sCheck));
drh21f6daa2019-10-11 14:21:4811136 sCheck.db = db;
drh5eddca62001-06-30 21:53:5311137 sCheck.pBt = pBt;
11138 sCheck.pPager = pBt->pPager;
drh266e5d02023-10-21 11:06:0311139 sCheck.nCkPage = btreePagecount(sCheck.pBt);
drh1dcdbc02007-01-27 02:24:5411140 sCheck.mxErr = mxErr;
drhe05b3f82015-07-01 17:53:4911141 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
drh5f4a6862016-01-30 12:50:2511142 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
drh266e5d02023-10-21 11:06:0311143 if( sCheck.nCkPage==0 ){
drhe05b3f82015-07-01 17:53:4911144 goto integrity_ck_cleanup;
drh0de8c112002-07-06 16:32:1411145 }
dan1235bb12012-04-03 17:43:2811146
drhc42276f2023-10-27 10:49:1711147 sCheck.aPgRef = sqlite3MallocZero((sCheck.nCkPage / 8)+ 1);
11148 if( !sCheck.aPgRef ){
11149 checkOom(&sCheck);
11150 goto integrity_ck_cleanup;
danielk1977ac245ec2005-01-14 13:50:1111151 }
drhe05b3f82015-07-01 17:53:4911152 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
11153 if( sCheck.heap==0 ){
drh5dd74bf2023-01-11 16:17:3111154 checkOom(&sCheck);
drhe05b3f82015-07-01 17:53:4911155 goto integrity_ck_cleanup;
11156 }
11157
drh42cac6d2004-11-20 20:31:1111158 i = PENDING_BYTE_PAGE(pBt);
drh266e5d02023-10-21 11:06:0311159 if( i<=sCheck.nCkPage ) setPageReferenced(&sCheck, i);
drh5eddca62001-06-30 21:53:5311160
11161 /* Check the integrity of the freelist
11162 */
drh17d2d592020-07-23 00:45:0611163 if( bCkFreelist ){
drhe5ea81a2023-04-01 13:14:5311164 sCheck.zPfx = "Freelist: ";
drh17d2d592020-07-23 00:45:0611165 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
11166 get4byte(&pBt->pPage1->aData[36]));
11167 sCheck.zPfx = 0;
11168 }
drh5eddca62001-06-30 21:53:5311169
11170 /* Check all the tables.
11171 */
drh040d77a2018-07-20 15:44:0911172#ifndef SQLITE_OMIT_AUTOVACUUM
drh17d2d592020-07-23 00:45:0611173 if( !bPartial ){
11174 if( pBt->autoVacuum ){
drhed109c02020-07-23 09:14:2511175 Pgno mx = 0;
11176 Pgno mxInHdr;
drh17d2d592020-07-23 00:45:0611177 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
11178 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
11179 if( mx!=mxInHdr ){
11180 checkAppendMsg(&sCheck,
drh2e89f1c2023-04-01 12:22:5711181 "max rootpage (%u) disagrees with header (%u)",
drh17d2d592020-07-23 00:45:0611182 mx, mxInHdr
11183 );
11184 }
11185 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
drh040d77a2018-07-20 15:44:0911186 checkAppendMsg(&sCheck,
drh17d2d592020-07-23 00:45:0611187 "incremental_vacuum enabled with a max rootpage of zero"
drh040d77a2018-07-20 15:44:0911188 );
11189 }
drh040d77a2018-07-20 15:44:0911190 }
11191#endif
drhcbc6b712015-07-02 16:17:3011192 testcase( pBt->db->flags & SQLITE_CellSizeCk );
drhd5b44d62018-12-06 17:06:0211193 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
danielk197789d40042008-11-17 14:20:5611194 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
dand90ecb52024-02-02 16:51:2411195 sCheck.nRow = 0;
drh39670a52024-02-27 15:33:5411196 if( aRoot[i] ){
dand90ecb52024-02-02 16:51:2411197 i64 notUsed;
danielk1977687566d2004-11-02 12:56:4111198#ifndef SQLITE_OMIT_AUTOVACUUM
dand90ecb52024-02-02 16:51:2411199 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
11200 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
11201 }
danielk1977687566d2004-11-02 12:56:4111202#endif
dand90ecb52024-02-02 16:51:2411203 sCheck.v0 = aRoot[i];
11204 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
11205 }
drh39670a52024-02-27 15:33:5411206 sqlite3MemSetArrayInt64(aCnt, i, sCheck.nRow);
drh5eddca62001-06-30 21:53:5311207 }
drhcbc6b712015-07-02 16:17:3011208 pBt->db->flags = savedDbFlags;
drh5eddca62001-06-30 21:53:5311209
11210 /* Make sure every page in the file is referenced
11211 */
drh17d2d592020-07-23 00:45:0611212 if( !bPartial ){
drh266e5d02023-10-21 11:06:0311213 for(i=1; i<=sCheck.nCkPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:4211214#ifdef SQLITE_OMIT_AUTOVACUUM
drh17d2d592020-07-23 00:45:0611215 if( getPageReferenced(&sCheck, i)==0 ){
drhe5ea81a2023-04-01 13:14:5311216 checkAppendMsg(&sCheck, "Page %u: never used", i);
drh17d2d592020-07-23 00:45:0611217 }
danielk1977afcdd022004-10-31 16:25:4211218#else
drh17d2d592020-07-23 00:45:0611219 /* If the database supports auto-vacuum, make sure no tables contain
11220 ** references to pointer-map pages.
11221 */
larrybrbc917382023-06-07 08:40:3111222 if( getPageReferenced(&sCheck, i)==0 &&
drh17d2d592020-07-23 00:45:0611223 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
drhe5ea81a2023-04-01 13:14:5311224 checkAppendMsg(&sCheck, "Page %u: never used", i);
drh17d2d592020-07-23 00:45:0611225 }
larrybrbc917382023-06-07 08:40:3111226 if( getPageReferenced(&sCheck, i)!=0 &&
drh17d2d592020-07-23 00:45:0611227 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
drhe5ea81a2023-04-01 13:14:5311228 checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i);
drh17d2d592020-07-23 00:45:0611229 }
danielk1977afcdd022004-10-31 16:25:4211230#endif
drh47eb5612020-08-10 21:01:3211231 }
drh5eddca62001-06-30 21:53:5311232 }
11233
drh5eddca62001-06-30 21:53:5311234 /* Clean up and report errors.
11235 */
drhe05b3f82015-07-01 17:53:4911236integrity_ck_cleanup:
11237 sqlite3PageFree(sCheck.heap);
dan1235bb12012-04-03 17:43:2811238 sqlite3_free(sCheck.aPgRef);
drh1dcdbc02007-01-27 02:24:5411239 *pnErr = sCheck.nErr;
drh5dd74bf2023-01-11 16:17:3111240 if( sCheck.nErr==0 ){
11241 sqlite3_str_reset(&sCheck.errMsg);
11242 *pzOut = 0;
11243 }else{
11244 *pzOut = sqlite3StrAccumFinish(&sCheck.errMsg);
11245 }
drhe05b3f82015-07-01 17:53:4911246 /* Make sure this analysis did not leave any unref() pages. */
11247 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
11248 sqlite3BtreeLeave(p);
drh5dd74bf2023-01-11 16:17:3111249 return sCheck.rc;
drh5eddca62001-06-30 21:53:5311250}
drhb7f91642004-10-31 02:22:4711251#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:4111252
drh73509ee2003-04-06 20:44:4511253/*
drhd4e0bb02012-05-27 01:19:0411254** Return the full pathname of the underlying database file. Return
11255** an empty string if the database is in-memory or a TEMP database.
drhd0679ed2007-08-28 22:24:3411256**
11257** The pager filename is invariant as long as the pager is
11258** open so it is safe to access without the BtShared mutex.
drh73509ee2003-04-06 20:44:4511259*/
danielk1977aef0bf62005-12-30 16:28:0111260const char *sqlite3BtreeGetFilename(Btree *p){
11261 assert( p->pBt->pPager!=0 );
drhd4e0bb02012-05-27 01:19:0411262 return sqlite3PagerFilename(p->pBt->pPager, 1);
drh73509ee2003-04-06 20:44:4511263}
11264
11265/*
danielk19775865e3d2004-06-14 06:03:5711266** Return the pathname of the journal file for this database. The return
11267** value of this routine is the same regardless of whether the journal file
11268** has been created or not.
drhd0679ed2007-08-28 22:24:3411269**
11270** The pager journal filename is invariant as long as the pager is
11271** open so it is safe to access without the BtShared mutex.
danielk19775865e3d2004-06-14 06:03:5711272*/
danielk1977aef0bf62005-12-30 16:28:0111273const char *sqlite3BtreeGetJournalname(Btree *p){
11274 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:2611275 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:5711276}
11277
danielk19771d850a72004-05-31 08:26:4911278/*
drh99744fa2020-08-25 19:09:0711279** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
11280** to describe the current transaction state of Btree p.
danielk19771d850a72004-05-31 08:26:4911281*/
drh99744fa2020-08-25 19:09:0711282int sqlite3BtreeTxnState(Btree *p){
drhe5fe6902007-12-07 18:55:2811283 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
drh99744fa2020-08-25 19:09:0711284 return p ? p->inTrans : 0;
danielk19771d850a72004-05-31 08:26:4911285}
11286
dana550f2d2010-08-02 10:47:0511287#ifndef SQLITE_OMIT_WAL
11288/*
11289** Run a checkpoint on the Btree passed as the first argument.
11290**
larrybrbc917382023-06-07 08:40:3111291** Return SQLITE_LOCKED if this or any other connection has an open
dana550f2d2010-08-02 10:47:0511292** transaction on the shared-cache the argument Btree is connected to.
dana58f26f2010-11-16 18:56:5111293**
dancdc1f042010-11-18 12:11:0511294** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
dana550f2d2010-08-02 10:47:0511295*/
dancdc1f042010-11-18 12:11:0511296int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
dana550f2d2010-08-02 10:47:0511297 int rc = SQLITE_OK;
11298 if( p ){
11299 BtShared *pBt = p->pBt;
11300 sqlite3BtreeEnter(p);
11301 if( pBt->inTransaction!=TRANS_NONE ){
11302 rc = SQLITE_LOCKED;
11303 }else{
dan7fb89902016-08-12 16:21:1511304 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
dana550f2d2010-08-02 10:47:0511305 }
11306 sqlite3BtreeLeave(p);
11307 }
11308 return rc;
11309}
11310#endif
11311
danielk19771d850a72004-05-31 08:26:4911312/*
drh99744fa2020-08-25 19:09:0711313** Return true if there is currently a backup running on Btree p.
danielk19772372c2b2006-06-27 16:34:5611314*/
danielk197704103022009-02-03 16:51:2411315int sqlite3BtreeIsInBackup(Btree *p){
11316 assert( p );
11317 assert( sqlite3_mutex_held(p->db->mutex) );
11318 return p->nBackup!=0;
11319}
11320
danielk19772372c2b2006-06-27 16:34:5611321/*
danielk1977da184232006-01-05 11:34:3211322** This function returns a pointer to a blob of memory associated with
drh85b623f2007-12-13 21:54:0911323** a single shared-btree. The memory is used by client code for its own
larrybrbc917382023-06-07 08:40:3111324** purposes (for example, to store a high-level schema associated with
danielk1977da184232006-01-05 11:34:3211325** the shared-btree). The btree layer manages reference counting issues.
11326**
11327** The first time this is called on a shared-btree, nBytes bytes of memory
larrybrbc917382023-06-07 08:40:3111328** are allocated, zeroed, and returned to the caller. For each subsequent
danielk1977da184232006-01-05 11:34:3211329** call the nBytes parameter is ignored and a pointer to the same blob
larrybrbc917382023-06-07 08:40:3111330** of memory returned.
danielk1977da184232006-01-05 11:34:3211331**
danielk1977171bfed2008-06-23 09:50:5011332** If the nBytes parameter is 0 and the blob of memory has not yet been
11333** allocated, a null pointer is returned. If the blob has already been
11334** allocated, it is returned as normal.
11335**
larrybrbc917382023-06-07 08:40:3111336** Just before the shared-btree is closed, the function passed as the
11337** xFree argument when the memory allocation was made is invoked on the
drh4fa7d7c2011-04-03 02:41:0011338** blob of allocated memory. The xFree function should not call sqlite3_free()
danielk1977da184232006-01-05 11:34:3211339** on the memory, the btree layer does that.
11340*/
11341void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
11342 BtShared *pBt = p->pBt;
drhef86b942025-02-17 17:33:1411343 assert( nBytes==0 || nBytes==sizeof(Schema) );
drh27641702007-08-22 02:56:4211344 sqlite3BtreeEnter(p);
danielk1977171bfed2008-06-23 09:50:5011345 if( !pBt->pSchema && nBytes ){
drhb9755982010-07-24 16:34:3711346 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
danielk1977da184232006-01-05 11:34:3211347 pBt->xFreeSchema = xFree;
11348 }
drh27641702007-08-22 02:56:4211349 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:3211350 return pBt->pSchema;
11351}
11352
danielk1977c87d34d2006-01-06 13:00:2811353/*
larrybrbc917382023-06-07 08:40:3111354** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
11355** btree as the argument handle holds an exclusive lock on the
drh1e32bed2020-06-19 13:33:5311356** sqlite_schema table. Otherwise SQLITE_OK.
danielk1977c87d34d2006-01-06 13:00:2811357*/
11358int sqlite3BtreeSchemaLocked(Btree *p){
drh27641702007-08-22 02:56:4211359 int rc;
stephanb504aab2025-05-31 09:44:0011360 UNUSED_PARAMETER(p); /* only used in DEBUG builds */
drhe5fe6902007-12-07 18:55:2811361 assert( sqlite3_mutex_held(p->db->mutex) );
drh27641702007-08-22 02:56:4211362 sqlite3BtreeEnter(p);
drh346a70c2020-06-15 20:27:3511363 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
danielk1977404ca072009-03-16 13:19:3611364 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
drh27641702007-08-22 02:56:4211365 sqlite3BtreeLeave(p);
11366 return rc;
danielk1977c87d34d2006-01-06 13:00:2811367}
11368
drha154dcd2006-03-22 22:10:0711369
11370#ifndef SQLITE_OMIT_SHARED_CACHE
11371/*
11372** Obtain a lock on the table whose root page is iTab. The
11373** lock is a write lock if isWritelock is true or a read lock
11374** if it is false.
11375*/
danielk1977c00da102006-01-07 13:21:0411376int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:2711377 int rc = SQLITE_OK;
danielk1977602b4662009-07-02 07:47:3311378 assert( p->inTrans!=TRANS_NONE );
drh6a9ad3d2008-04-02 16:29:3011379 if( p->sharable ){
11380 u8 lockType = READ_LOCK + isWriteLock;
11381 assert( READ_LOCK+1==WRITE_LOCK );
11382 assert( isWriteLock==0 || isWriteLock==1 );
danielk1977602b4662009-07-02 07:47:3311383
drh6a9ad3d2008-04-02 16:29:3011384 sqlite3BtreeEnter(p);
drhc25eabe2009-02-24 18:57:3111385 rc = querySharedCacheTableLock(p, iTab, lockType);
drh6a9ad3d2008-04-02 16:29:3011386 if( rc==SQLITE_OK ){
drhc25eabe2009-02-24 18:57:3111387 rc = setSharedCacheTableLock(p, iTab, lockType);
drh6a9ad3d2008-04-02 16:29:3011388 }
11389 sqlite3BtreeLeave(p);
danielk1977c00da102006-01-07 13:21:0411390 }
11391 return rc;
11392}
drha154dcd2006-03-22 22:10:0711393#endif
danielk1977b82e7ed2006-01-11 14:09:3111394
danielk1977b4e9af92007-05-01 17:49:4911395#ifndef SQLITE_OMIT_INCRBLOB
11396/*
larrybrbc917382023-06-07 08:40:3111397** Argument pCsr must be a cursor opened for writing on an
11398** INTKEY table currently pointing at a valid table entry.
danielk1977b4e9af92007-05-01 17:49:4911399** This function modifies the data stored as part of that entry.
danielk1977ecaecf92009-07-08 08:05:3511400**
larrybrbc917382023-06-07 08:40:3111401** Only the data content may only be modified, it is not possible to
danielk1977ecaecf92009-07-08 08:05:3511402** change the length of the data stored. If this function is called with
11403** parameters that attempt to write past the end of the existing data,
11404** no modifications are made and SQLITE_CORRUPT is returned.
danielk1977b4e9af92007-05-01 17:49:4911405*/
danielk1977dcbb5d32007-05-04 18:36:4411406int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
danielk1977c9000e62009-07-08 13:55:2811407 int rc;
dan7a2347e2016-01-07 16:43:5411408 assert( cursorOwnsBtShared(pCsr) );
drhe5fe6902007-12-07 18:55:2811409 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
drh036dbec2014-03-11 23:40:4411410 assert( pCsr->curFlags & BTCF_Incrblob );
danielk19773588ceb2008-06-10 17:30:2611411
danielk1977c9000e62009-07-08 13:55:2811412 rc = restoreCursorPosition(pCsr);
11413 if( rc!=SQLITE_OK ){
11414 return rc;
11415 }
danielk19773588ceb2008-06-10 17:30:2611416 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
11417 if( pCsr->eState!=CURSOR_VALID ){
11418 return SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:4411419 }
11420
dan227a1c42013-04-03 11:17:3911421 /* Save the positions of all other cursors open on this table. This is
11422 ** required in case any of them are holding references to an xFetch
11423 ** version of the b-tree page modified by the accessPayload call below.
drh370c9f42013-04-03 20:04:0411424 **
drh3f387402014-09-24 01:23:0011425 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
drh370c9f42013-04-03 20:04:0411426 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
11427 ** saveAllCursors can only return SQLITE_OK.
dan227a1c42013-04-03 11:17:3911428 */
drh370c9f42013-04-03 20:04:0411429 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
11430 assert( rc==SQLITE_OK );
dan227a1c42013-04-03 11:17:3911431
larrybrbc917382023-06-07 08:40:3111432 /* Check some assumptions:
danielk1977dcbb5d32007-05-04 18:36:4411433 ** (a) the cursor is open for writing,
danielk1977c9000e62009-07-08 13:55:2811434 ** (b) there is a read/write transaction open,
11435 ** (c) the connection holds a write-lock on the table (if required),
11436 ** (d) there are no conflicting read-locks, and
11437 ** (e) the cursor points at a valid row of an intKey table.
danielk1977d04417962007-05-02 13:16:3011438 */
drh036dbec2014-03-11 23:40:4411439 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
danielk19774f029602009-07-08 18:45:3711440 return SQLITE_READONLY;
11441 }
drhc9166342012-01-05 23:32:0611442 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
11443 && pCsr->pBt->inTransaction==TRANS_WRITE );
danielk197796d48e92009-06-29 06:00:3711444 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
11445 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
drh352a35a2017-08-15 03:46:4711446 assert( pCsr->pPage->intKey );
danielk1977b4e9af92007-05-01 17:49:4911447
drhfb192682009-07-11 18:26:2811448 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
danielk1977b4e9af92007-05-01 17:49:4911449}
danielk19772dec9702007-05-02 16:48:3711450
larrybrbc917382023-06-07 08:40:3111451/*
dan5a500af2014-03-11 20:33:0411452** Mark this cursor as an incremental blob cursor.
danielk19772dec9702007-05-02 16:48:3711453*/
dan5a500af2014-03-11 20:33:0411454void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
drh036dbec2014-03-11 23:40:4411455 pCur->curFlags |= BTCF_Incrblob;
drh69180952015-06-25 13:03:1011456 pCur->pBtree->hasIncrblobCur = 1;
danielk19772dec9702007-05-02 16:48:3711457}
danielk1977b4e9af92007-05-01 17:49:4911458#endif
dane04dc882010-04-20 18:53:1511459
11460/*
larrybrbc917382023-06-07 08:40:3111461** Set both the "read version" (single byte at byte offset 18) and
dane04dc882010-04-20 18:53:1511462** "write version" (single byte at byte offset 19) fields in the database
11463** header to iVersion.
11464*/
11465int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
11466 BtShared *pBt = pBtree->pBt;
11467 int rc; /* Return code */
larrybrbc917382023-06-07 08:40:3111468
dane04dc882010-04-20 18:53:1511469 assert( iVersion==1 || iVersion==2 );
11470
danb9780022010-04-21 18:37:5711471 /* If setting the version fields to 1, do not automatically open the
11472 ** WAL connection, even if the version fields are currently set to 2.
11473 */
drhc9166342012-01-05 23:32:0611474 pBt->btsFlags &= ~BTS_NO_WAL;
11475 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
danb9780022010-04-21 18:37:5711476
drhbb2d9b12018-06-06 16:28:4011477 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
dane04dc882010-04-20 18:53:1511478 if( rc==SQLITE_OK ){
11479 u8 *aData = pBt->pPage1->aData;
danb9780022010-04-21 18:37:5711480 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
drhbb2d9b12018-06-06 16:28:4011481 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
danb9780022010-04-21 18:37:5711482 if( rc==SQLITE_OK ){
11483 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
11484 if( rc==SQLITE_OK ){
11485 aData[18] = (u8)iVersion;
11486 aData[19] = (u8)iVersion;
11487 }
11488 }
11489 }
dane04dc882010-04-20 18:53:1511490 }
11491
drhc9166342012-01-05 23:32:0611492 pBt->btsFlags &= ~BTS_NO_WAL;
dane04dc882010-04-20 18:53:1511493 return rc;
11494}
dan428c2182012-08-06 18:50:1111495
drhe0997b32015-03-20 14:57:5011496/*
11497** Return true if the cursor has a hint specified. This routine is
11498** only used from within assert() statements
11499*/
11500int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
11501 return (pCsr->hints & mask)!=0;
11502}
drhe0997b32015-03-20 14:57:5011503
drh781597f2014-05-21 08:21:0711504/*
11505** Return true if the given Btree is read-only.
11506*/
11507int sqlite3BtreeIsReadonly(Btree *p){
11508 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
11509}
drhdef68892014-11-04 12:11:2311510
11511/*
11512** Return the size of the header added to each page by this module.
11513*/
drh37c057b2014-12-30 00:57:2911514int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
dan20d876f2016-01-07 16:06:2211515
dan1b3d13e2022-11-28 18:41:4111516/*
11517** If no transaction is active and the database is not a temp-db, clear
11518** the in-memory pager cache.
11519*/
11520void sqlite3BtreeClearCache(Btree *p){
11521 BtShared *pBt = p->pBt;
11522 if( pBt->inTransaction==TRANS_NONE ){
11523 sqlite3PagerClearCache(pBt->pPager);
11524 }
11525}
11526
drh5a1fb182016-01-08 19:34:3911527#if !defined(SQLITE_OMIT_SHARED_CACHE)
dan20d876f2016-01-07 16:06:2211528/*
11529** Return true if the Btree passed as the only argument is sharable.
11530*/
11531int sqlite3BtreeSharable(Btree *p){
11532 return p->sharable;
11533}
dan272989b2016-07-06 10:12:0211534
11535/*
11536** Return the number of connections to the BtShared object accessed by
larrybrbc917382023-06-07 08:40:3111537** the Btree handle passed as the only argument. For private caches
dan272989b2016-07-06 10:12:0211538** this is always 1. For shared caches it may be 1 or greater.
11539*/
11540int sqlite3BtreeConnectionCount(Btree *p){
11541 testcase( p->sharable );
11542 return p->pBt->nRef;
11543}
drh5a1fb182016-01-08 19:34:3911544#endif