| | @@ -452,11 +452,11 @@ |
| 452 | 452 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 453 | 453 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 454 | 454 | */ |
| 455 | 455 | #define SQLITE_VERSION "3.37.0" |
| 456 | 456 | #define SQLITE_VERSION_NUMBER 3037000 |
| 457 | | -#define SQLITE_SOURCE_ID "2021-10-26 09:53:51 4b41535b096dec4b15a85e657102a72d4288728da6103f3fdcbe0e6f244c673a" |
| 457 | +#define SQLITE_SOURCE_ID "2021-11-03 16:35:23 3206edff947b9edb485466f05b2baadf725d798229630c7e83e88c0b9ae278ca" |
| 458 | 458 | |
| 459 | 459 | /* |
| 460 | 460 | ** CAPI3REF: Run-Time Library Version Numbers |
| 461 | 461 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 462 | 462 | ** |
| | @@ -6709,10 +6709,76 @@ |
| 6709 | 6709 | ** |
| 6710 | 6710 | ** See also the [sqlite3_update_hook()] interface. |
| 6711 | 6711 | */ |
| 6712 | 6712 | SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); |
| 6713 | 6713 | SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); |
| 6714 | + |
| 6715 | +/* |
| 6716 | +** CAPI3REF: Autovacuum Compaction Amount Callback |
| 6717 | +** METHOD: sqlite3 |
| 6718 | +** |
| 6719 | +** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback |
| 6720 | +** function C that is invoked prior to each autovacuum of the database |
| 6721 | +** file. ^The callback is passed a copy of the generic data pointer (P), |
| 6722 | +** the schema-name of the attached database that is being autovacuumed, |
| 6723 | +** the the size of the database file in pages, the number of free pages, |
| 6724 | +** and the number of bytes per page, respectively. The callback should |
| 6725 | +** return the number of free pages that should be removed by the |
| 6726 | +** autovacuum. ^If the callback returns zero, then no autovacuum happens. |
| 6727 | +** ^If the value returned is greater than or equal to the number of |
| 6728 | +** free pages, then a complete autovacuum happens. |
| 6729 | +** |
| 6730 | +** <p>^If there are multiple ATTACH-ed database files that are being |
| 6731 | +** modified as part of a transaction commit, then the autovacuum pages |
| 6732 | +** callback is invoked separately for each file. |
| 6733 | +** |
| 6734 | +** <p><b>The callback is not reentrant.</b> The callback function should |
| 6735 | +** not attempt to invoke any other SQLite interface. If it does, bad |
| 6736 | +** things may happen, including segmentation faults and corrupt database |
| 6737 | +** files. The callback function should be a simple function that |
| 6738 | +** does some arithmetic on its input parameters and returns a result. |
| 6739 | +** |
| 6740 | +** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional |
| 6741 | +** destructor for the P parameter. ^If X is not NULL, then X(P) is |
| 6742 | +** invoked whenever the database connection closes or when the callback |
| 6743 | +** is overwritten by another invocation of sqlite3_autovacuum_pages(). |
| 6744 | +** |
| 6745 | +** <p>^There is only one autovacuum pages callback per database connection. |
| 6746 | +** ^Each call to the sqlite3_autovacuum_pages() interface overrides all |
| 6747 | +** previous invocations for that database connection. ^If the callback |
| 6748 | +** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer, |
| 6749 | +** then the autovacuum steps callback is cancelled. The return value |
| 6750 | +** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might |
| 6751 | +** be some other error code if something goes wrong. The current |
| 6752 | +** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other |
| 6753 | +** return codes might be added in future releases. |
| 6754 | +** |
| 6755 | +** <p>If no autovacuum pages callback is specified (the usual case) or |
| 6756 | +** a NULL pointer is provided for the callback, |
| 6757 | +** then the default behavior is to vacuum all free pages. So, in other |
| 6758 | +** words, the default behavior is the same as if the callback function |
| 6759 | +** were something like this: |
| 6760 | +** |
| 6761 | +** <blockquote><pre> |
| 6762 | +** unsigned int demonstration_autovac_pages_callback( |
| 6763 | +** void *pClientData, |
| 6764 | +** const char *zSchema, |
| 6765 | +** unsigned int nDbPage, |
| 6766 | +** unsigned int nFreePage, |
| 6767 | +** unsigned int nBytePerPage |
| 6768 | +** ){ |
| 6769 | +** return nFreePage; |
| 6770 | +** } |
| 6771 | +** </pre></blockquote> |
| 6772 | +*/ |
| 6773 | +SQLITE_API int sqlite3_autovacuum_pages( |
| 6774 | + sqlite3 *db, |
| 6775 | + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 6776 | + void*, |
| 6777 | + void(*)(void*) |
| 6778 | +); |
| 6779 | + |
| 6714 | 6780 | |
| 6715 | 6781 | /* |
| 6716 | 6782 | ** CAPI3REF: Data Change Notification Callbacks |
| 6717 | 6783 | ** METHOD: sqlite3 |
| 6718 | 6784 | ** |
| | @@ -16437,10 +16503,13 @@ |
| 16437 | 16503 | int (*xCommitCallback)(void*); /* Invoked at every commit. */ |
| 16438 | 16504 | void *pRollbackArg; /* Argument to xRollbackCallback() */ |
| 16439 | 16505 | void (*xRollbackCallback)(void*); /* Invoked at every commit. */ |
| 16440 | 16506 | void *pUpdateArg; |
| 16441 | 16507 | void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); |
| 16508 | + void *pAutovacPagesArg; /* Client argument to autovac_pages */ |
| 16509 | + void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */ |
| 16510 | + unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32); |
| 16442 | 16511 | Parse *pParse; /* Current parse */ |
| 16443 | 16512 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 16444 | 16513 | void *pPreUpdateArg; /* First argument to xPreUpdateCallback */ |
| 16445 | 16514 | void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */ |
| 16446 | 16515 | void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64 |
| | @@ -18694,12 +18763,14 @@ |
| 18694 | 18763 | } InitData; |
| 18695 | 18764 | |
| 18696 | 18765 | /* |
| 18697 | 18766 | ** Allowed values for mInitFlags |
| 18698 | 18767 | */ |
| 18768 | +#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */ |
| 18699 | 18769 | #define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */ |
| 18700 | 18770 | #define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */ |
| 18771 | +#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */ |
| 18701 | 18772 | |
| 18702 | 18773 | /* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled |
| 18703 | 18774 | ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning |
| 18704 | 18775 | ** parameters are for temporary use during development, to help find |
| 18705 | 18776 | ** optimial values for parameters in the query planner. The should not |
| | @@ -24126,16 +24197,19 @@ |
| 24126 | 24197 | pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); |
| 24127 | 24198 | if( pFile ){ |
| 24128 | 24199 | rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); |
| 24129 | 24200 | if( rc!=SQLITE_OK ){ |
| 24130 | 24201 | sqlite3_free(pFile); |
| 24202 | + *ppFile = 0; |
| 24131 | 24203 | }else{ |
| 24132 | 24204 | *ppFile = pFile; |
| 24133 | 24205 | } |
| 24134 | 24206 | }else{ |
| 24207 | + *ppFile = 0; |
| 24135 | 24208 | rc = SQLITE_NOMEM_BKPT; |
| 24136 | 24209 | } |
| 24210 | + assert( *ppFile!=0 || rc!=SQLITE_OK ); |
| 24137 | 24211 | return rc; |
| 24138 | 24212 | } |
| 24139 | 24213 | SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){ |
| 24140 | 24214 | assert( pFile ); |
| 24141 | 24215 | sqlite3OsClose(pFile); |
| | @@ -38079,11 +38153,13 @@ |
| 38079 | 38153 | } |
| 38080 | 38154 | } |
| 38081 | 38155 | |
| 38082 | 38156 | /* Forward declaration */ |
| 38083 | 38157 | static int unixGetTempname(int nBuf, char *zBuf); |
| 38084 | | -static int unixFcntlExternalReader(unixFile*, int*); |
| 38158 | +#ifndef SQLITE_OMIT_WAL |
| 38159 | + static int unixFcntlExternalReader(unixFile*, int*); |
| 38160 | +#endif |
| 38085 | 38161 | |
| 38086 | 38162 | /* |
| 38087 | 38163 | ** Information and control of an open file handle. |
| 38088 | 38164 | */ |
| 38089 | 38165 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| | @@ -38198,11 +38274,16 @@ |
| 38198 | 38274 | return proxyFileControl(id,op,pArg); |
| 38199 | 38275 | } |
| 38200 | 38276 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
| 38201 | 38277 | |
| 38202 | 38278 | case SQLITE_FCNTL_EXTERNAL_READER: { |
| 38279 | +#ifndef SQLITE_OMIT_WAL |
| 38203 | 38280 | return unixFcntlExternalReader((unixFile*)id, (int*)pArg); |
| 38281 | +#else |
| 38282 | + *(int*)pArg = 0; |
| 38283 | + return SQLITE_OK; |
| 38284 | +#endif |
| 38204 | 38285 | } |
| 38205 | 38286 | } |
| 38206 | 38287 | return SQLITE_NOTFOUND; |
| 38207 | 38288 | } |
| 38208 | 38289 | |
| | @@ -48902,10 +48983,11 @@ |
| 48902 | 48983 | int *pOutFlags |
| 48903 | 48984 | ){ |
| 48904 | 48985 | MemFile *pFile = (MemFile*)pFd; |
| 48905 | 48986 | MemStore *p = 0; |
| 48906 | 48987 | int szName; |
| 48988 | + UNUSED_PARAMETER(pVfs); |
| 48907 | 48989 | |
| 48908 | 48990 | memset(pFile, 0, sizeof(*pFile)); |
| 48909 | 48991 | szName = sqlite3Strlen30(zName); |
| 48910 | 48992 | if( szName>1 && zName[0]=='/' ){ |
| 48911 | 48993 | int i; |
| | @@ -60864,13 +60946,17 @@ |
| 60864 | 60946 | ** If the wal-index is currently smaller the iPage pages then the size |
| 60865 | 60947 | ** of the wal-index might be increased, but only if it is safe to do |
| 60866 | 60948 | ** so. It is safe to enlarge the wal-index if pWal->writeLock is true |
| 60867 | 60949 | ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE. |
| 60868 | 60950 | ** |
| 60869 | | -** If this call is successful, *ppPage is set to point to the wal-index |
| 60870 | | -** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, |
| 60871 | | -** then an SQLite error code is returned and *ppPage is set to 0. |
| 60951 | +** Three possible result scenarios: |
| 60952 | +** |
| 60953 | +** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page |
| 60954 | +** (2) rc>=SQLITE_ERROR and *ppPage==NULL |
| 60955 | +** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0 |
| 60956 | +** |
| 60957 | +** Scenario (3) can only occur when pWal->writeLock is false and iPage==0 |
| 60872 | 60958 | */ |
| 60873 | 60959 | static SQLITE_NOINLINE int walIndexPageRealloc( |
| 60874 | 60960 | Wal *pWal, /* The WAL context */ |
| 60875 | 60961 | int iPage, /* The page we seek */ |
| 60876 | 60962 | volatile u32 **ppPage /* Write the page pointer here */ |
| | @@ -60899,11 +60985,13 @@ |
| 60899 | 60985 | if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT; |
| 60900 | 60986 | }else{ |
| 60901 | 60987 | rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, |
| 60902 | 60988 | pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] |
| 60903 | 60989 | ); |
| 60904 | | - assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 ); |
| 60990 | + assert( pWal->apWiData[iPage]!=0 |
| 60991 | + || rc!=SQLITE_OK |
| 60992 | + || (pWal->writeLock==0 && iPage==0) ); |
| 60905 | 60993 | testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK ); |
| 60906 | 60994 | if( rc==SQLITE_OK ){ |
| 60907 | 60995 | if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM; |
| 60908 | 60996 | }else if( (rc&0xff)==SQLITE_READONLY ){ |
| 60909 | 60997 | pWal->readOnly |= WAL_SHM_RDONLY; |
| | @@ -61238,12 +61326,12 @@ |
| 61238 | 61326 | ** in the wal-index file. Set pLoc->iZero to one less than the frame |
| 61239 | 61327 | ** number of the first frame indexed by this hash table. If a |
| 61240 | 61328 | ** slot in the hash table is set to N, it refers to frame number |
| 61241 | 61329 | ** (pLoc->iZero+N) in the log. |
| 61242 | 61330 | ** |
| 61243 | | -** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the |
| 61244 | | -** first frame indexed by the hash table, frame (pLoc->iZero+1). |
| 61331 | +** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the |
| 61332 | +** first frame indexed by the hash table, frame (pLoc->iZero). |
| 61245 | 61333 | */ |
| 61246 | 61334 | static int walHashGet( |
| 61247 | 61335 | Wal *pWal, /* WAL handle */ |
| 61248 | 61336 | int iHash, /* Find the iHash'th table */ |
| 61249 | 61337 | WalHashLoc *pLoc /* OUT: Hash table location */ |
| | @@ -61251,19 +61339,20 @@ |
| 61251 | 61339 | int rc; /* Return code */ |
| 61252 | 61340 | |
| 61253 | 61341 | rc = walIndexPage(pWal, iHash, &pLoc->aPgno); |
| 61254 | 61342 | assert( rc==SQLITE_OK || iHash>0 ); |
| 61255 | 61343 | |
| 61256 | | - if( rc==SQLITE_OK ){ |
| 61344 | + if( pLoc->aPgno ){ |
| 61257 | 61345 | pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE]; |
| 61258 | 61346 | if( iHash==0 ){ |
| 61259 | 61347 | pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; |
| 61260 | 61348 | pLoc->iZero = 0; |
| 61261 | 61349 | }else{ |
| 61262 | 61350 | pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; |
| 61263 | 61351 | } |
| 61264 | | - pLoc->aPgno = &pLoc->aPgno[-1]; |
| 61352 | + }else if( NEVER(rc==SQLITE_OK) ){ |
| 61353 | + rc = SQLITE_ERROR; |
| 61265 | 61354 | } |
| 61266 | 61355 | return rc; |
| 61267 | 61356 | } |
| 61268 | 61357 | |
| 61269 | 61358 | /* |
| | @@ -61341,25 +61430,26 @@ |
| 61341 | 61430 | } |
| 61342 | 61431 | |
| 61343 | 61432 | /* Zero the entries in the aPgno array that correspond to frames with |
| 61344 | 61433 | ** frame numbers greater than pWal->hdr.mxFrame. |
| 61345 | 61434 | */ |
| 61346 | | - nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]); |
| 61347 | | - memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte); |
| 61435 | + nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]); |
| 61436 | + assert( nByte>=0 ); |
| 61437 | + memset((void *)&sLoc.aPgno[iLimit], 0, nByte); |
| 61348 | 61438 | |
| 61349 | 61439 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 61350 | 61440 | /* Verify that the every entry in the mapping region is still reachable |
| 61351 | 61441 | ** via the hash table even after the cleanup. |
| 61352 | 61442 | */ |
| 61353 | 61443 | if( iLimit ){ |
| 61354 | 61444 | int j; /* Loop counter */ |
| 61355 | 61445 | int iKey; /* Hash key */ |
| 61356 | | - for(j=1; j<=iLimit; j++){ |
| 61446 | + for(j=0; j<iLimit; j++){ |
| 61357 | 61447 | for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){ |
| 61358 | | - if( sLoc.aHash[iKey]==j ) break; |
| 61448 | + if( sLoc.aHash[iKey]==j+1 ) break; |
| 61359 | 61449 | } |
| 61360 | | - assert( sLoc.aHash[iKey]==j ); |
| 61450 | + assert( sLoc.aHash[iKey]==j+1 ); |
| 61361 | 61451 | } |
| 61362 | 61452 | } |
| 61363 | 61453 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 61364 | 61454 | } |
| 61365 | 61455 | |
| | @@ -61387,32 +61477,32 @@ |
| 61387 | 61477 | |
| 61388 | 61478 | /* If this is the first entry to be added to this hash-table, zero the |
| 61389 | 61479 | ** entire hash table and aPgno[] array before proceeding. |
| 61390 | 61480 | */ |
| 61391 | 61481 | if( idx==1 ){ |
| 61392 | | - int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT] |
| 61393 | | - - (u8 *)&sLoc.aPgno[1]); |
| 61394 | | - memset((void*)&sLoc.aPgno[1], 0, nByte); |
| 61482 | + int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno); |
| 61483 | + assert( nByte>=0 ); |
| 61484 | + memset((void*)sLoc.aPgno, 0, nByte); |
| 61395 | 61485 | } |
| 61396 | 61486 | |
| 61397 | 61487 | /* If the entry in aPgno[] is already set, then the previous writer |
| 61398 | 61488 | ** must have exited unexpectedly in the middle of a transaction (after |
| 61399 | 61489 | ** writing one or more dirty pages to the WAL to free up memory). |
| 61400 | 61490 | ** Remove the remnants of that writers uncommitted transaction from |
| 61401 | 61491 | ** the hash-table before writing any new entries. |
| 61402 | 61492 | */ |
| 61403 | | - if( sLoc.aPgno[idx] ){ |
| 61493 | + if( sLoc.aPgno[idx-1] ){ |
| 61404 | 61494 | walCleanupHash(pWal); |
| 61405 | | - assert( !sLoc.aPgno[idx] ); |
| 61495 | + assert( !sLoc.aPgno[idx-1] ); |
| 61406 | 61496 | } |
| 61407 | 61497 | |
| 61408 | 61498 | /* Write the aPgno[] array entry and the hash-table slot. */ |
| 61409 | 61499 | nCollide = idx; |
| 61410 | 61500 | for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 61411 | 61501 | if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT; |
| 61412 | 61502 | } |
| 61413 | | - sLoc.aPgno[idx] = iPage; |
| 61503 | + sLoc.aPgno[idx-1] = iPage; |
| 61414 | 61504 | AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx); |
| 61415 | 61505 | |
| 61416 | 61506 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 61417 | 61507 | /* Verify that the number of entries in the hash table exactly equals |
| 61418 | 61508 | ** the number of entries in the mapping region. |
| | @@ -61429,22 +61519,21 @@ |
| 61429 | 61519 | ** thing to check, so only do this occasionally - not on every |
| 61430 | 61520 | ** iteration. |
| 61431 | 61521 | */ |
| 61432 | 61522 | if( (idx&0x3ff)==0 ){ |
| 61433 | 61523 | int i; /* Loop counter */ |
| 61434 | | - for(i=1; i<=idx; i++){ |
| 61524 | + for(i=0; i<idx; i++){ |
| 61435 | 61525 | for(iKey=walHash(sLoc.aPgno[i]); |
| 61436 | 61526 | sLoc.aHash[iKey]; |
| 61437 | 61527 | iKey=walNextHash(iKey)){ |
| 61438 | | - if( sLoc.aHash[iKey]==i ) break; |
| 61528 | + if( sLoc.aHash[iKey]==i+1 ) break; |
| 61439 | 61529 | } |
| 61440 | | - assert( sLoc.aHash[iKey]==i ); |
| 61530 | + assert( sLoc.aHash[iKey]==i+1 ); |
| 61441 | 61531 | } |
| 61442 | 61532 | } |
| 61443 | 61533 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 61444 | 61534 | } |
| 61445 | | - |
| 61446 | 61535 | |
| 61447 | 61536 | return rc; |
| 61448 | 61537 | } |
| 61449 | 61538 | |
| 61450 | 61539 | |
| | @@ -61562,11 +61651,12 @@ |
| 61562 | 61651 | u32 iFrame; /* Index of last frame read */ |
| 61563 | 61652 | u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE); |
| 61564 | 61653 | u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE); |
| 61565 | 61654 | u32 nHdr, nHdr32; |
| 61566 | 61655 | rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare); |
| 61567 | | - if( rc ) break; |
| 61656 | + assert( aShare!=0 || rc!=SQLITE_OK ); |
| 61657 | + if( aShare==0 ) break; |
| 61568 | 61658 | pWal->apWiData[iPg] = aPrivate; |
| 61569 | 61659 | |
| 61570 | 61660 | for(iFrame=iFirst; iFrame<=iLast; iFrame++){ |
| 61571 | 61661 | i64 iOffset = walFrameOffset(iFrame, szPage); |
| 61572 | 61662 | u32 pgno; /* Database page number for frame */ |
| | @@ -62059,11 +62149,10 @@ |
| 62059 | 62149 | if( rc==SQLITE_OK ){ |
| 62060 | 62150 | int j; /* Counter variable */ |
| 62061 | 62151 | int nEntry; /* Number of entries in this segment */ |
| 62062 | 62152 | ht_slot *aIndex; /* Sorted index for this segment */ |
| 62063 | 62153 | |
| 62064 | | - sLoc.aPgno++; |
| 62065 | 62154 | if( (i+1)==nSegment ){ |
| 62066 | 62155 | nEntry = (int)(iLast - sLoc.iZero); |
| 62067 | 62156 | }else{ |
| 62068 | 62157 | nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno); |
| 62069 | 62158 | } |
| | @@ -63198,11 +63287,12 @@ |
| 63198 | 63287 | i64 iDbOff; /* Offset of db file entry */ |
| 63199 | 63288 | i64 iWalOff; /* Offset of wal file entry */ |
| 63200 | 63289 | |
| 63201 | 63290 | rc = walHashGet(pWal, walFramePage(i), &sLoc); |
| 63202 | 63291 | if( rc!=SQLITE_OK ) break; |
| 63203 | | - pgno = sLoc.aPgno[i-sLoc.iZero]; |
| 63292 | + assert( i - sLoc.iZero - 1 >=0 ); |
| 63293 | + pgno = sLoc.aPgno[i-sLoc.iZero-1]; |
| 63204 | 63294 | iDbOff = (i64)(pgno-1) * szPage; |
| 63205 | 63295 | |
| 63206 | 63296 | if( iDbOff+szPage<=szDb ){ |
| 63207 | 63297 | iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE; |
| 63208 | 63298 | rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff); |
| | @@ -63431,11 +63521,11 @@ |
| 63431 | 63521 | } |
| 63432 | 63522 | nCollide = HASHTABLE_NSLOT; |
| 63433 | 63523 | iKey = walHash(pgno); |
| 63434 | 63524 | while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ |
| 63435 | 63525 | u32 iFrame = iH + sLoc.iZero; |
| 63436 | | - if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 63526 | + if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){ |
| 63437 | 63527 | assert( iFrame>iRead || CORRUPT_DB ); |
| 63438 | 63528 | iRead = iFrame; |
| 63439 | 63529 | } |
| 63440 | 63530 | if( (nCollide--)==0 ){ |
| 63441 | 63531 | return SQLITE_CORRUPT_BKPT; |
| | @@ -69375,27 +69465,30 @@ |
| 69375 | 69465 | } |
| 69376 | 69466 | |
| 69377 | 69467 | /* |
| 69378 | 69468 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
| 69379 | 69469 | ** is committed for an auto-vacuum database. |
| 69380 | | -** |
| 69381 | | -** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 69382 | | -** the database file should be truncated to during the commit process. |
| 69383 | | -** i.e. the database has been reorganized so that only the first *pnTrunc |
| 69384 | | -** pages are in use. |
| 69385 | 69470 | */ |
| 69386 | | -static int autoVacuumCommit(BtShared *pBt){ |
| 69471 | +static int autoVacuumCommit(Btree *p){ |
| 69387 | 69472 | int rc = SQLITE_OK; |
| 69388 | | - Pager *pPager = pBt->pPager; |
| 69389 | | - VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) |
| 69473 | + Pager *pPager; |
| 69474 | + BtShared *pBt; |
| 69475 | + sqlite3 *db; |
| 69476 | + VVA_ONLY( int nRef ); |
| 69477 | + |
| 69478 | + assert( p!=0 ); |
| 69479 | + pBt = p->pBt; |
| 69480 | + pPager = pBt->pPager; |
| 69481 | + VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); ) |
| 69390 | 69482 | |
| 69391 | 69483 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 69392 | 69484 | invalidateAllOverflowCache(pBt); |
| 69393 | 69485 | assert(pBt->autoVacuum); |
| 69394 | 69486 | if( !pBt->incrVacuum ){ |
| 69395 | 69487 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 69396 | 69488 | Pgno nFree; /* Number of pages on the freelist initially */ |
| 69489 | + Pgno nVac; /* Number of pages to vacuum */ |
| 69397 | 69490 | Pgno iFree; /* The next page to be freed */ |
| 69398 | 69491 | Pgno nOrig; /* Database size before freeing */ |
| 69399 | 69492 | |
| 69400 | 69493 | nOrig = btreePagecount(pBt); |
| 69401 | 69494 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| | @@ -69405,22 +69498,46 @@ |
| 69405 | 69498 | */ |
| 69406 | 69499 | return SQLITE_CORRUPT_BKPT; |
| 69407 | 69500 | } |
| 69408 | 69501 | |
| 69409 | 69502 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 69410 | | - nFin = finalDbSize(pBt, nOrig, nFree); |
| 69503 | + db = p->db; |
| 69504 | + if( db->xAutovacPages ){ |
| 69505 | + int iDb; |
| 69506 | + for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){ |
| 69507 | + if( db->aDb[iDb].pBt==p ) break; |
| 69508 | + } |
| 69509 | + nVac = db->xAutovacPages( |
| 69510 | + db->pAutovacPagesArg, |
| 69511 | + db->aDb[iDb].zDbSName, |
| 69512 | + nOrig, |
| 69513 | + nFree, |
| 69514 | + pBt->pageSize |
| 69515 | + ); |
| 69516 | + if( nVac>nFree ){ |
| 69517 | + nVac = nFree; |
| 69518 | + } |
| 69519 | + if( nVac==0 ){ |
| 69520 | + return SQLITE_OK; |
| 69521 | + } |
| 69522 | + }else{ |
| 69523 | + nVac = nFree; |
| 69524 | + } |
| 69525 | + nFin = finalDbSize(pBt, nOrig, nVac); |
| 69411 | 69526 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
| 69412 | 69527 | if( nFin<nOrig ){ |
| 69413 | 69528 | rc = saveAllCursors(pBt, 0, 0); |
| 69414 | 69529 | } |
| 69415 | 69530 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 69416 | | - rc = incrVacuumStep(pBt, nFin, iFree, 1); |
| 69531 | + rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree); |
| 69417 | 69532 | } |
| 69418 | 69533 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
| 69419 | 69534 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 69420 | | - put4byte(&pBt->pPage1->aData[32], 0); |
| 69421 | | - put4byte(&pBt->pPage1->aData[36], 0); |
| 69535 | + if( nVac==nFree ){ |
| 69536 | + put4byte(&pBt->pPage1->aData[32], 0); |
| 69537 | + put4byte(&pBt->pPage1->aData[36], 0); |
| 69538 | + } |
| 69422 | 69539 | put4byte(&pBt->pPage1->aData[28], nFin); |
| 69423 | 69540 | pBt->bDoTruncate = 1; |
| 69424 | 69541 | pBt->nPage = nFin; |
| 69425 | 69542 | } |
| 69426 | 69543 | if( rc!=SQLITE_OK ){ |
| | @@ -69467,11 +69584,11 @@ |
| 69467 | 69584 | if( p->inTrans==TRANS_WRITE ){ |
| 69468 | 69585 | BtShared *pBt = p->pBt; |
| 69469 | 69586 | sqlite3BtreeEnter(p); |
| 69470 | 69587 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 69471 | 69588 | if( pBt->autoVacuum ){ |
| 69472 | | - rc = autoVacuumCommit(pBt); |
| 69589 | + rc = autoVacuumCommit(p); |
| 69473 | 69590 | if( rc!=SQLITE_OK ){ |
| 69474 | 69591 | sqlite3BtreeLeave(p); |
| 69475 | 69592 | return rc; |
| 69476 | 69593 | } |
| 69477 | 69594 | } |
| | @@ -77429,10 +77546,12 @@ |
| 77429 | 77546 | nByte = 1; |
| 77430 | 77547 | } |
| 77431 | 77548 | if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ |
| 77432 | 77549 | return SQLITE_NOMEM_BKPT; |
| 77433 | 77550 | } |
| 77551 | + assert( pMem->z!=0 ); |
| 77552 | + assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte ); |
| 77434 | 77553 | |
| 77435 | 77554 | memset(&pMem->z[pMem->n], 0, pMem->u.nZero); |
| 77436 | 77555 | pMem->n += pMem->u.nZero; |
| 77437 | 77556 | pMem->flags &= ~(MEM_Zero|MEM_Term); |
| 77438 | 77557 | return SQLITE_OK; |
| | @@ -89396,20 +89515,25 @@ |
| 89396 | 89515 | rc = SQLITE_CORRUPT_BKPT; |
| 89397 | 89516 | goto abort_due_to_error; |
| 89398 | 89517 | } |
| 89399 | 89518 | } |
| 89400 | 89519 | |
| 89401 | | -/* Opcode: TypeCheck P1 P2 * P4 * |
| 89520 | +/* Opcode: TypeCheck P1 P2 P3 P4 * |
| 89402 | 89521 | ** Synopsis: typecheck(r[P1@P2]) |
| 89403 | 89522 | ** |
| 89404 | 89523 | ** Apply affinities to the range of P2 registers beginning with P1. |
| 89405 | 89524 | ** Take the affinities from the Table object in P4. If any value |
| 89406 | 89525 | ** cannot be coerced into the correct type, then raise an error. |
| 89407 | 89526 | ** |
| 89408 | 89527 | ** This opcode is similar to OP_Affinity except that this opcode |
| 89409 | 89528 | ** forces the register type to the Table column type. This is used |
| 89410 | 89529 | ** to implement "strict affinity". |
| 89530 | +** |
| 89531 | +** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 |
| 89532 | +** is zero. When P3 is non-zero, no type checking occurs for |
| 89533 | +** static generated columns. Virtual columns are computed at query time |
| 89534 | +** and so they are never checked. |
| 89411 | 89535 | ** |
| 89412 | 89536 | ** Preconditions: |
| 89413 | 89537 | ** |
| 89414 | 89538 | ** <ul> |
| 89415 | 89539 | ** <li> P2 should be the number of non-virtual columns in the |
| | @@ -89429,11 +89553,14 @@ |
| 89429 | 89553 | assert( pTab->tabFlags & TF_Strict ); |
| 89430 | 89554 | assert( pTab->nNVCol==pOp->p2 ); |
| 89431 | 89555 | aCol = pTab->aCol; |
| 89432 | 89556 | pIn1 = &aMem[pOp->p1]; |
| 89433 | 89557 | for(i=0; i<pTab->nCol; i++){ |
| 89434 | | - if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 89558 | + if( aCol[i].colFlags & COLFLAG_GENERATED ){ |
| 89559 | + if( aCol[i].colFlags & COLFLAG_VIRTUAL ) continue; |
| 89560 | + if( pOp->p3 ){ pIn1++; continue; } |
| 89561 | + } |
| 89435 | 89562 | assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); |
| 89436 | 89563 | applyAffinity(pIn1, aCol[i].affinity, encoding); |
| 89437 | 89564 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 89438 | 89565 | switch( aCol[i].eCType ){ |
| 89439 | 89566 | case COLTYPE_BLOB: { |
| | @@ -90153,10 +90280,11 @@ |
| 90153 | 90280 | assert( p->bIsReader ); |
| 90154 | 90281 | assert( p->readOnly==0 || pOp->p2==0 ); |
| 90155 | 90282 | assert( pOp->p2>=0 && pOp->p2<=2 ); |
| 90156 | 90283 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 90157 | 90284 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
| 90285 | + assert( rc==SQLITE_OK ); |
| 90158 | 90286 | if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ |
| 90159 | 90287 | rc = SQLITE_READONLY; |
| 90160 | 90288 | goto abort_due_to_error; |
| 90161 | 90289 | } |
| 90162 | 90290 | pBt = db->aDb[pOp->p1].pBt; |
| | @@ -90196,11 +90324,12 @@ |
| 90196 | 90324 | p->nStmtDefCons = db->nDeferredCons; |
| 90197 | 90325 | p->nStmtDefImmCons = db->nDeferredImmCons; |
| 90198 | 90326 | } |
| 90199 | 90327 | } |
| 90200 | 90328 | assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); |
| 90201 | | - if( pOp->p5 |
| 90329 | + if( rc==SQLITE_OK |
| 90330 | + && pOp->p5 |
| 90202 | 90331 | && (iMeta!=pOp->p3 |
| 90203 | 90332 | || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i) |
| 90204 | 90333 | ){ |
| 90205 | 90334 | /* |
| 90206 | 90335 | ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema |
| | @@ -97454,10 +97583,11 @@ |
| 97454 | 97583 | pTask->file2.iEof += pIncr->mxSz; |
| 97455 | 97584 | }else{ |
| 97456 | 97585 | vdbeMergeEngineFree(pMerger); |
| 97457 | 97586 | rc = SQLITE_NOMEM_BKPT; |
| 97458 | 97587 | } |
| 97588 | + assert( *ppOut!=0 || rc!=SQLITE_OK ); |
| 97459 | 97589 | return rc; |
| 97460 | 97590 | } |
| 97461 | 97591 | |
| 97462 | 97592 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 97463 | 97593 | /* |
| | @@ -103136,11 +103266,11 @@ |
| 103136 | 103266 | } |
| 103137 | 103267 | |
| 103138 | 103268 | return pRet; |
| 103139 | 103269 | } |
| 103140 | 103270 | #else |
| 103141 | | -SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
| 103271 | +SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){ |
| 103142 | 103272 | assert( p==0 ); |
| 103143 | 103273 | return 0; |
| 103144 | 103274 | } |
| 103145 | 103275 | #endif |
| 103146 | 103276 | |
| | @@ -108142,11 +108272,11 @@ |
| 108142 | 108272 | VdbeCoverage(v); |
| 108143 | 108273 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); |
| 108144 | 108274 | sqlite3ReleaseTempReg(pParse, r1); |
| 108145 | 108275 | |
| 108146 | 108276 | /* Reload the table definition */ |
| 108147 | | - renameReloadSchema(pParse, iDb, INITFLAG_AlterRename); |
| 108277 | + renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd); |
| 108148 | 108278 | |
| 108149 | 108279 | /* Verify that constraints are still satisfied */ |
| 108150 | 108280 | if( pNew->pCheck!=0 |
| 108151 | 108281 | || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0) |
| 108152 | 108282 | ){ |
| | @@ -109035,10 +109165,13 @@ |
| 109035 | 109165 | }else{ |
| 109036 | 109166 | p->pTab->nTabRef++; |
| 109037 | 109167 | rc = sqlite3ViewGetColumnNames(pParse, p->pTab); |
| 109038 | 109168 | } |
| 109039 | 109169 | } |
| 109170 | + } |
| 109171 | + if( rc==SQLITE_OK && db->mallocFailed ){ |
| 109172 | + rc = SQLITE_NOMEM; |
| 109040 | 109173 | } |
| 109041 | 109174 | sNC.pSrcList = pSrc; |
| 109042 | 109175 | if( rc==SQLITE_OK && pStep->pWhere ){ |
| 109043 | 109176 | rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); |
| 109044 | 109177 | } |
| | @@ -123938,28 +124071,34 @@ |
| 123938 | 124071 | |
| 123939 | 124072 | /* Before computing generated columns, first go through and make sure |
| 123940 | 124073 | ** that appropriate affinity has been applied to the regular columns |
| 123941 | 124074 | */ |
| 123942 | 124075 | sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); |
| 123943 | | - if( (pTab->tabFlags & TF_HasStored)!=0 |
| 123944 | | - && (pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1))->opcode==OP_Affinity |
| 123945 | | - ){ |
| 123946 | | - /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 123947 | | - ** columns. '@' is the no-op affinity and those columns have not |
| 123948 | | - ** yet been computed. */ |
| 123949 | | - int ii, jj; |
| 123950 | | - char *zP4 = pOp->p4.z; |
| 123951 | | - assert( zP4!=0 ); |
| 123952 | | - assert( pOp->p4type==P4_DYNAMIC ); |
| 123953 | | - for(ii=jj=0; zP4[jj]; ii++){ |
| 123954 | | - if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 123955 | | - continue; |
| 123956 | | - } |
| 123957 | | - if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 123958 | | - zP4[jj] = SQLITE_AFF_NONE; |
| 123959 | | - } |
| 123960 | | - jj++; |
| 124076 | + if( (pTab->tabFlags & TF_HasStored)!=0 ){ |
| 124077 | + pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1); |
| 124078 | + if( pOp->opcode==OP_Affinity ){ |
| 124079 | + /* Change the OP_Affinity argument to '@' (NONE) for all stored |
| 124080 | + ** columns. '@' is the no-op affinity and those columns have not |
| 124081 | + ** yet been computed. */ |
| 124082 | + int ii, jj; |
| 124083 | + char *zP4 = pOp->p4.z; |
| 124084 | + assert( zP4!=0 ); |
| 124085 | + assert( pOp->p4type==P4_DYNAMIC ); |
| 124086 | + for(ii=jj=0; zP4[jj]; ii++){ |
| 124087 | + if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ |
| 124088 | + continue; |
| 124089 | + } |
| 124090 | + if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ |
| 124091 | + zP4[jj] = SQLITE_AFF_NONE; |
| 124092 | + } |
| 124093 | + jj++; |
| 124094 | + } |
| 124095 | + }else if( pOp->opcode==OP_TypeCheck ){ |
| 124096 | + /* If an OP_TypeCheck was generated because the table is STRICT, |
| 124097 | + ** then set the P3 operand to indicate that generated columns should |
| 124098 | + ** not be checked */ |
| 124099 | + pOp->p3 = 1; |
| 123961 | 124100 | } |
| 123962 | 124101 | } |
| 123963 | 124102 | |
| 123964 | 124103 | /* Because there can be multiple generated columns that refer to one another, |
| 123965 | 124104 | ** this is a two-pass algorithm. On the first pass, mark all generated |
| | @@ -125983,11 +126122,12 @@ |
| 125983 | 126122 | default: { |
| 125984 | 126123 | int nConflictCk; /* Number of opcodes in conflict check logic */ |
| 125985 | 126124 | |
| 125986 | 126125 | assert( onError==OE_Replace ); |
| 125987 | 126126 | nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; |
| 125988 | | - assert( nConflictCk>0 ); |
| 126127 | + assert( nConflictCk>0 || db->mallocFailed ); |
| 126128 | + testcase( nConflictCk<=0 ); |
| 125989 | 126129 | testcase( nConflictCk>1 ); |
| 125990 | 126130 | if( regTrigCnt ){ |
| 125991 | 126131 | sqlite3MultiWrite(pParse); |
| 125992 | 126132 | nReplaceTrig++; |
| 125993 | 126133 | } |
| | @@ -126269,12 +126409,13 @@ |
| 126269 | 126409 | |
| 126270 | 126410 | assert( op==OP_OpenRead || op==OP_OpenWrite ); |
| 126271 | 126411 | assert( op==OP_OpenWrite || p5==0 ); |
| 126272 | 126412 | if( IsVirtual(pTab) ){ |
| 126273 | 126413 | /* This routine is a no-op for virtual tables. Leave the output |
| 126274 | | - ** variables *piDataCur and *piIdxCur uninitialized so that valgrind |
| 126275 | | - ** can detect if they are used by mistake in the caller. */ |
| 126414 | + ** variables *piDataCur and *piIdxCur set to illegal cursor numbers |
| 126415 | + ** for improved error detection. */ |
| 126416 | + *piDataCur = *piIdxCur = -999; |
| 126276 | 126417 | return 0; |
| 126277 | 126418 | } |
| 126278 | 126419 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 126279 | 126420 | v = pParse->pVdbe; |
| 126280 | 126421 | assert( v!=0 ); |
| | @@ -127281,10 +127422,14 @@ |
| 127281 | 127422 | /* Version 3.34.0 and later */ |
| 127282 | 127423 | int (*txn_state)(sqlite3*,const char*); |
| 127283 | 127424 | /* Version 3.36.1 and later */ |
| 127284 | 127425 | sqlite3_int64 (*changes64)(sqlite3*); |
| 127285 | 127426 | sqlite3_int64 (*total_changes64)(sqlite3*); |
| 127427 | + /* Version 3.37.0 and later */ |
| 127428 | + int (*autovacuum_pages)(sqlite3*, |
| 127429 | + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), |
| 127430 | + void*, void(*)(void*)); |
| 127286 | 127431 | }; |
| 127287 | 127432 | |
| 127288 | 127433 | /* |
| 127289 | 127434 | ** This is the function signature used for all extension entry points. It |
| 127290 | 127435 | ** is also defined in the file "loadext.c". |
| | @@ -127587,10 +127732,15 @@ |
| 127587 | 127732 | #define sqlite3_create_filename sqlite3_api->create_filename |
| 127588 | 127733 | #define sqlite3_free_filename sqlite3_api->free_filename |
| 127589 | 127734 | #define sqlite3_database_file_object sqlite3_api->database_file_object |
| 127590 | 127735 | /* Version 3.34.0 and later */ |
| 127591 | 127736 | #define sqlite3_txn_state sqlite3_api->txn_state |
| 127737 | +/* Version 3.36.1 and later */ |
| 127738 | +#define sqlite3_changes64 sqlite3_api->changes64 |
| 127739 | +#define sqlite3_total_changes64 sqlite3_api->total_changes64 |
| 127740 | +* Version 3.37.0 and later */ |
| 127741 | +#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages |
| 127592 | 127742 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 127593 | 127743 | |
| 127594 | 127744 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 127595 | 127745 | /* This case when the file really is being compiled as a loadable |
| 127596 | 127746 | ** extension */ |
| | @@ -128074,10 +128224,12 @@ |
| 128074 | 128224 | /* Version 3.34.0 and later */ |
| 128075 | 128225 | sqlite3_txn_state, |
| 128076 | 128226 | /* Version 3.36.1 and later */ |
| 128077 | 128227 | sqlite3_changes64, |
| 128078 | 128228 | sqlite3_total_changes64, |
| 128229 | + /* Version 3.37.0 and later */ |
| 128230 | + sqlite3_autovacuum_pages, |
| 128079 | 128231 | }; |
| 128080 | 128232 | |
| 128081 | 128233 | /* True if x is the directory separator character |
| 128082 | 128234 | */ |
| 128083 | 128235 | #if SQLITE_OS_WIN |
| | @@ -130911,11 +131063,11 @@ |
| 130911 | 131063 | if( pCol->notNull ){ |
| 130912 | 131064 | jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
| 130913 | 131065 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 130914 | 131066 | pCol->zCnName); |
| 130915 | 131067 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 130916 | | - if( bStrict ){ |
| 131068 | + if( bStrict && pCol->eCType!=COLTYPE_ANY ){ |
| 130917 | 131069 | sqlite3VdbeGoto(v, doError); |
| 130918 | 131070 | }else{ |
| 130919 | 131071 | integrityCheckResultRow(v); |
| 130920 | 131072 | } |
| 130921 | 131073 | sqlite3VdbeJumpHere(v, jmp2); |
| | @@ -131872,14 +132024,19 @@ |
| 131872 | 132024 | sqlite3 *db = pData->db; |
| 131873 | 132025 | if( db->mallocFailed ){ |
| 131874 | 132026 | pData->rc = SQLITE_NOMEM_BKPT; |
| 131875 | 132027 | }else if( pData->pzErrMsg[0]!=0 ){ |
| 131876 | 132028 | /* A error message has already been generated. Do not overwrite it */ |
| 131877 | | - }else if( pData->mInitFlags & (INITFLAG_AlterRename|INITFLAG_AlterDrop) ){ |
| 132029 | + }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){ |
| 132030 | + static const char *azAlterType[] = { |
| 132031 | + "rename", |
| 132032 | + "drop column", |
| 132033 | + "add column" |
| 132034 | + }; |
| 131878 | 132035 | *pData->pzErrMsg = sqlite3MPrintf(db, |
| 131879 | 132036 | "error in %s %s after %s: %s", azObj[0], azObj[1], |
| 131880 | | - (pData->mInitFlags & INITFLAG_AlterRename) ? "rename" : "drop column", |
| 132037 | + azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], |
| 131881 | 132038 | zExtra |
| 131882 | 132039 | ); |
| 131883 | 132040 | pData->rc = SQLITE_ERROR; |
| 131884 | 132041 | }else if( db->flags & SQLITE_WriteSchema ){ |
| 131885 | 132042 | pData->rc = SQLITE_CORRUPT_BKPT; |
| | @@ -143906,11 +144063,13 @@ |
| 143906 | 144063 | rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0); |
| 143907 | 144064 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 143908 | 144065 | |
| 143909 | 144066 | /* Do not attempt to change the page size for a WAL database */ |
| 143910 | 144067 | if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) |
| 143911 | | - ==PAGER_JOURNALMODE_WAL ){ |
| 144068 | + ==PAGER_JOURNALMODE_WAL |
| 144069 | + && pOut==0 |
| 144070 | + ){ |
| 143912 | 144071 | db->nextPagesize = 0; |
| 143913 | 144072 | } |
| 143914 | 144073 | |
| 143915 | 144074 | if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) |
| 143916 | 144075 | || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) |
| | @@ -147905,12 +148064,23 @@ |
| 147905 | 148064 | |
| 147906 | 148065 | /* Load the value for the inequality constraint at the end of the |
| 147907 | 148066 | ** range (if any). |
| 147908 | 148067 | */ |
| 147909 | 148068 | nConstraint = nEq; |
| 148069 | + assert( pLevel->p2==0 ); |
| 147910 | 148070 | if( pRangeEnd ){ |
| 147911 | 148071 | Expr *pRight = pRangeEnd->pExpr->pRight; |
| 148072 | + if( addrSeekScan ){ |
| 148073 | + /* For a seek-scan that has a range on the lowest term of the index, |
| 148074 | + ** we have to make the top of the loop be code that sets the end |
| 148075 | + ** condition of the range. Otherwise, the OP_SeekScan might jump |
| 148076 | + ** over that initialization, leaving the range-end value set to the |
| 148077 | + ** range-start value, resulting in a wrong answer. |
| 148078 | + ** See ticket 5981a8c041a3c2f3 (2021-11-02). |
| 148079 | + */ |
| 148080 | + pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 148081 | + } |
| 147912 | 148082 | codeExprOrVector(pParse, pRight, regBase+nEq, nTop); |
| 147913 | 148083 | whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 147914 | 148084 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 147915 | 148085 | && sqlite3ExprCanBeNull(pRight) |
| 147916 | 148086 | ){ |
| | @@ -147940,11 +148110,11 @@ |
| 147940 | 148110 | } |
| 147941 | 148111 | sqlite3DbFree(db, zStartAff); |
| 147942 | 148112 | sqlite3DbFree(db, zEndAff); |
| 147943 | 148113 | |
| 147944 | 148114 | /* Top of the loop body */ |
| 147945 | | - pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 148115 | + if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 147946 | 148116 | |
| 147947 | 148117 | /* Check if the index cursor is past the end of the range. */ |
| 147948 | 148118 | if( nConstraint ){ |
| 147949 | 148119 | if( regBignull ){ |
| 147950 | 148120 | /* Except, skip the end-of-range check while doing the NULL-scan */ |
| | @@ -167564,10 +167734,13 @@ |
| 167564 | 167734 | ** So it needs to be freed here. Todo: Why not roll the temp schema into |
| 167565 | 167735 | ** the same sqliteMalloc() as the one that allocates the database |
| 167566 | 167736 | ** structure? |
| 167567 | 167737 | */ |
| 167568 | 167738 | sqlite3DbFree(db, db->aDb[1].pSchema); |
| 167739 | + if( db->xAutovacDestr ){ |
| 167740 | + db->xAutovacDestr(db->pAutovacPagesArg); |
| 167741 | + } |
| 167569 | 167742 | sqlite3_mutex_leave(db->mutex); |
| 167570 | 167743 | db->eOpenState = SQLITE_STATE_CLOSED; |
| 167571 | 167744 | sqlite3_mutex_free(db->mutex); |
| 167572 | 167745 | assert( sqlite3LookasideUsed(db,0)==0 ); |
| 167573 | 167746 | if( db->lookaside.bMalloced ){ |
| | @@ -168464,10 +168637,38 @@ |
| 168464 | 168637 | db->pPreUpdateArg = pArg; |
| 168465 | 168638 | sqlite3_mutex_leave(db->mutex); |
| 168466 | 168639 | return pRet; |
| 168467 | 168640 | } |
| 168468 | 168641 | #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 168642 | + |
| 168643 | +/* |
| 168644 | +** Register a function to be invoked prior to each autovacuum that |
| 168645 | +** determines the number of pages to vacuum. |
| 168646 | +*/ |
| 168647 | +SQLITE_API int sqlite3_autovacuum_pages( |
| 168648 | + sqlite3 *db, /* Attach the hook to this database */ |
| 168649 | + unsigned int (*xCallback)(void*,const char*,u32,u32,u32), |
| 168650 | + void *pArg, /* Argument to the function */ |
| 168651 | + void (*xDestructor)(void*) /* Destructor for pArg */ |
| 168652 | +){ |
| 168653 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 168654 | + if( !sqlite3SafetyCheckOk(db) ){ |
| 168655 | + if( xDestructor ) xDestructor(pArg); |
| 168656 | + return SQLITE_MISUSE_BKPT; |
| 168657 | + } |
| 168658 | +#endif |
| 168659 | + sqlite3_mutex_enter(db->mutex); |
| 168660 | + if( db->xAutovacDestr ){ |
| 168661 | + db->xAutovacDestr(db->pAutovacPagesArg); |
| 168662 | + } |
| 168663 | + db->xAutovacPages = xCallback; |
| 168664 | + db->pAutovacPagesArg = pArg; |
| 168665 | + db->xAutovacDestr = xDestructor; |
| 168666 | + sqlite3_mutex_leave(db->mutex); |
| 168667 | + return SQLITE_OK; |
| 168668 | +} |
| 168669 | + |
| 168469 | 168670 | |
| 168470 | 168671 | #ifndef SQLITE_OMIT_WAL |
| 168471 | 168672 | /* |
| 168472 | 168673 | ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). |
| 168473 | 168674 | ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file |
| | @@ -190861,11 +191062,11 @@ |
| 190861 | 191062 | # define ALWAYS(X) (X) |
| 190862 | 191063 | # define NEVER(X) (X) |
| 190863 | 191064 | # endif |
| 190864 | 191065 | # define testcase(X) |
| 190865 | 191066 | #endif |
| 190866 | | -#if defined(NDEBUG) |
| 191067 | +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST) |
| 190867 | 191068 | # define VVA(X) |
| 190868 | 191069 | #else |
| 190869 | 191070 | # define VVA(X) X |
| 190870 | 191071 | #endif |
| 190871 | 191072 | |
| | @@ -221613,20 +221814,29 @@ |
| 221613 | 221814 | Fts5PoslistWriter writer; |
| 221614 | 221815 | int bOk; /* True if ok to populate */ |
| 221615 | 221816 | int bMiss; |
| 221616 | 221817 | }; |
| 221617 | 221818 | |
| 221819 | +/* |
| 221820 | +** Clear the position lists associated with all phrases in the expression |
| 221821 | +** passed as the first argument. Argument bLive is true if the expression |
| 221822 | +** might be pointing to a real entry, otherwise it has just been reset. |
| 221823 | +** |
| 221824 | +** At present this function is only used for detail=col and detail=none |
| 221825 | +** fts5 tables. This implies that all phrases must be at most 1 token |
| 221826 | +** in size, as phrase matches are not supported without detail=full. |
| 221827 | +*/ |
| 221618 | 221828 | static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){ |
| 221619 | 221829 | Fts5PoslistPopulator *pRet; |
| 221620 | 221830 | pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 221621 | 221831 | if( pRet ){ |
| 221622 | 221832 | int i; |
| 221623 | 221833 | memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 221624 | 221834 | for(i=0; i<pExpr->nPhrase; i++){ |
| 221625 | 221835 | Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist; |
| 221626 | 221836 | Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
| 221627 | | - assert( pExpr->apExprPhrase[i]->nTerm==1 ); |
| 221837 | + assert( pExpr->apExprPhrase[i]->nTerm<=1 ); |
| 221628 | 221838 | if( bLive && |
| 221629 | 221839 | (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof) |
| 221630 | 221840 | ){ |
| 221631 | 221841 | pRet[i].bMiss = 1; |
| 221632 | 221842 | }else{ |
| | @@ -231987,11 +232197,11 @@ |
| 231987 | 232197 | int nArg, /* Number of args */ |
| 231988 | 232198 | sqlite3_value **apUnused /* Function arguments */ |
| 231989 | 232199 | ){ |
| 231990 | 232200 | assert( nArg==0 ); |
| 231991 | 232201 | UNUSED_PARAM2(nArg, apUnused); |
| 231992 | | - sqlite3_result_text(pCtx, "fts5: 2021-10-26 09:53:51 4b41535b096dec4b15a85e657102a72d4288728da6103f3fdcbe0e6f244c673a", -1, SQLITE_TRANSIENT); |
| 232202 | + sqlite3_result_text(pCtx, "fts5: 2021-11-02 17:55:01 1d9004cd015073853ce0ca811a68ea5411733eedee993b97a38a42ba139d7590", -1, SQLITE_TRANSIENT); |
| 231993 | 232203 | } |
| 231994 | 232204 | |
| 231995 | 232205 | /* |
| 231996 | 232206 | ** Return true if zName is the extension on one of the shadow tables used |
| 231997 | 232207 | ** by this module. |
| 231998 | 232208 | |