Fossil SCM

Update the built-in SQLite to the latest 3.18.0 prerelease version that supports PRAGMA optimize.

drh 2017-03-06 19:53 optimize-pragma
Commit f32c36e58aaf53f809e4ee7d99d0f0288ce4ed83
2 files changed +239 -75 +34 -14
+239 -75
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398398
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399399
** [sqlite_version()] and [sqlite_source_id()].
400400
*/
401401
#define SQLITE_VERSION "3.18.0"
402402
#define SQLITE_VERSION_NUMBER 3018000
403
-#define SQLITE_SOURCE_ID "2017-02-23 02:15:33 7a959f6d1ea038988cdb4c02d6f37abaec2580a0"
403
+#define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
404404
405405
/*
406406
** CAPI3REF: Run-Time Library Version Numbers
407407
** KEYWORDS: sqlite3_version sqlite3_sourceid
408408
**
@@ -2315,24 +2315,34 @@
23152315
** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
23162316
** names are not also used by explicitly declared columns. ^If
23172317
** the table has a column of type [INTEGER PRIMARY KEY] then that column
23182318
** is another alias for the rowid.
23192319
**
2320
-** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
2321
-** most recent successful [INSERT] into a rowid table or [virtual table]
2322
-** on database connection D.
2323
-** ^Inserts into [WITHOUT ROWID] tables are not recorded.
2324
-** ^If no successful [INSERT]s into rowid tables
2325
-** have ever occurred on the database connection D,
2326
-** then sqlite3_last_insert_rowid(D) returns zero.
2320
+** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2321
+** the most recent successful [INSERT] into a rowid table or [virtual table]
2322
+** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2323
+** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2324
+** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2325
+** zero.
23272326
**
2328
-** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2329
-** method, then this routine will return the [rowid] of the inserted
2330
-** row as long as the trigger or virtual table method is running.
2331
-** But once the trigger or virtual table method ends, the value returned
2332
-** by this routine reverts to what it was before the trigger or virtual
2333
-** table method began.)^
2327
+** As well as being set automatically as rows are inserted into database
2328
+** tables, the value returned by this function may be set explicitly by
2329
+** [sqlite3_set_last_insert_rowid()]
2330
+**
2331
+** Some virtual table implementations may INSERT rows into rowid tables as
2332
+** part of committing a transaction (e.g. to flush data accumulated in memory
2333
+** to disk). In this case subsequent calls to this function return the rowid
2334
+** associated with these internal INSERT operations, which leads to
2335
+** unintuitive results. Virtual table implementations that do write to rowid
2336
+** tables in this way can avoid this problem by restoring the original
2337
+** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2338
+** control to the user.
2339
+**
2340
+** ^(If an [INSERT] occurs within a trigger then this routine will
2341
+** return the [rowid] of the inserted row as long as the trigger is
2342
+** running. Once the trigger program ends, the value returned
2343
+** by this routine reverts to what it was before the trigger was fired.)^
23342344
**
23352345
** ^An [INSERT] that fails due to a constraint violation is not a
23362346
** successful [INSERT] and does not change the value returned by this
23372347
** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
23382348
** and INSERT OR ABORT make no changes to the return value of this
@@ -2355,10 +2365,20 @@
23552365
** unpredictable and might not equal either the old or the new
23562366
** last insert [rowid].
23572367
*/
23582368
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
23592369
2370
+/*
2371
+** CAPI3REF: Set the Last Insert Rowid value.
2372
+** METHOD: sqlite3
2373
+**
2374
+** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2375
+** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2376
+** without inserting a row into the database.
2377
+*/
2378
+SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2379
+
23602380
/*
23612381
** CAPI3REF: Count The Number Of Rows Modified
23622382
** METHOD: sqlite3
23632383
**
23642384
** ^This function returns the number of rows modified, inserted or
@@ -19513,22 +19533,23 @@
1951319533
**
1951419534
** Move the date backwards to the beginning of the current day,
1951519535
** or month or year.
1951619536
*/
1951719537
if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
19538
+ if( !p->validJD && !p->validYMD && !p->validHMS ) break;
1951819539
z += 9;
1951919540
computeYMD(p);
1952019541
p->validHMS = 1;
1952119542
p->h = p->m = 0;
1952219543
p->s = 0.0;
19544
+ p->rawS = 0;
1952319545
p->validTZ = 0;
1952419546
p->validJD = 0;
1952519547
if( sqlite3_stricmp(z,"month")==0 ){
1952619548
p->D = 1;
1952719549
rc = 0;
1952819550
}else if( sqlite3_stricmp(z,"year")==0 ){
19529
- computeYMD(p);
1953019551
p->M = 1;
1953119552
p->D = 1;
1953219553
rc = 0;
1953319554
}else if( sqlite3_stricmp(z,"day")==0 ){
1953419555
rc = 0;
@@ -60238,21 +60259,22 @@
6023860259
}
6023960260
#endif
6024060261
6024160262
6024260263
/*
60243
-** Defragment the page given. All Cells are moved to the
60244
-** end of the page and all free space is collected into one
60245
-** big FreeBlk that occurs in between the header and cell
60246
-** pointer array and the cell content area.
60264
+** Defragment the page given. This routine reorganizes cells within the
60265
+** page so that there are no free-blocks on the free-block list.
60266
+**
60267
+** Parameter nMaxFrag is the maximum amount of fragmented space that may be
60268
+** present in the page after this routine returns.
6024760269
**
6024860270
** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
6024960271
** b-tree page so that there are no freeblocks or fragment bytes, all
6025060272
** unused bytes are contained in the unallocated space region, and all
6025160273
** cells are packed tightly at the end of the page.
6025260274
*/
60253
-static int defragmentPage(MemPage *pPage){
60275
+static int defragmentPage(MemPage *pPage, int nMaxFrag){
6025460276
int i; /* Loop counter */
6025560277
int pc; /* Address of the i-th cell */
6025660278
int hdr; /* Offset to the page header */
6025760279
int size; /* Size of a cell */
6025860280
int usableSize; /* Number of usable bytes on a page */
@@ -60263,11 +60285,10 @@
6026360285
unsigned char *temp; /* Temp area for cell content */
6026460286
unsigned char *src; /* Source of content */
6026560287
int iCellFirst; /* First allowable cell index */
6026660288
int iCellLast; /* Last possible cell index */
6026760289
60268
-
6026960290
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6027060291
assert( pPage->pBt!=0 );
6027160292
assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
6027260293
assert( pPage->nOverflow==0 );
6027360294
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
@@ -60275,13 +60296,60 @@
6027560296
src = data = pPage->aData;
6027660297
hdr = pPage->hdrOffset;
6027760298
cellOffset = pPage->cellOffset;
6027860299
nCell = pPage->nCell;
6027960300
assert( nCell==get2byte(&data[hdr+3]) );
60301
+ iCellFirst = cellOffset + 2*nCell;
6028060302
usableSize = pPage->pBt->usableSize;
60303
+
60304
+ /* This block handles pages with two or fewer free blocks and nMaxFrag
60305
+ ** or fewer fragmented bytes. In this case it is faster to move the
60306
+ ** two (or one) blocks of cells using memmove() and add the required
60307
+ ** offsets to each pointer in the cell-pointer array than it is to
60308
+ ** reconstruct the entire page. */
60309
+ if( (int)data[hdr+7]<=nMaxFrag ){
60310
+ int iFree = get2byte(&data[hdr+1]);
60311
+ if( iFree ){
60312
+ int iFree2 = get2byte(&data[iFree]);
60313
+
60314
+ /* pageFindSlot() has already verified that free blocks are sorted
60315
+ ** in order of offset within the page, and that no block extends
60316
+ ** past the end of the page. Provided the two free slots do not
60317
+ ** overlap, this guarantees that the memmove() calls below will not
60318
+ ** overwrite the usableSize byte buffer, even if the database page
60319
+ ** is corrupt. */
60320
+ assert( iFree2==0 || iFree2>iFree );
60321
+ assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
60322
+ assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
60323
+
60324
+ if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
60325
+ u8 *pEnd = &data[cellOffset + nCell*2];
60326
+ u8 *pAddr;
60327
+ int sz2 = 0;
60328
+ int sz = get2byte(&data[iFree+2]);
60329
+ int top = get2byte(&data[hdr+5]);
60330
+ if( iFree2 ){
60331
+ if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_BKPT;
60332
+ sz2 = get2byte(&data[iFree2+2]);
60333
+ assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
60334
+ memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
60335
+ sz += sz2;
60336
+ }
60337
+ cbrk = top+sz;
60338
+ assert( cbrk+(iFree-top) <= usableSize );
60339
+ memmove(&data[cbrk], &data[top], iFree-top);
60340
+ for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
60341
+ pc = get2byte(pAddr);
60342
+ if( pc<iFree ){ put2byte(pAddr, pc+sz); }
60343
+ else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
60344
+ }
60345
+ goto defragment_out;
60346
+ }
60347
+ }
60348
+ }
60349
+
6028160350
cbrk = usableSize;
60282
- iCellFirst = cellOffset + 2*nCell;
6028360351
iCellLast = usableSize - 4;
6028460352
for(i=0; i<nCell; i++){
6028560353
u8 *pAddr; /* The i-th cell pointer */
6028660354
pAddr = &data[cellOffset + i*2];
6028760355
pc = get2byte(pAddr);
@@ -60311,20 +60379,22 @@
6031160379
memcpy(&temp[x], &data[x], (cbrk+size) - x);
6031260380
src = temp;
6031360381
}
6031460382
memcpy(&data[cbrk], &src[pc], size);
6031560383
}
60384
+ data[hdr+7] = 0;
60385
+
60386
+ defragment_out:
60387
+ if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
60388
+ return SQLITE_CORRUPT_BKPT;
60389
+ }
6031660390
assert( cbrk>=iCellFirst );
6031760391
put2byte(&data[hdr+5], cbrk);
6031860392
data[hdr+1] = 0;
6031960393
data[hdr+2] = 0;
60320
- data[hdr+7] = 0;
6032160394
memset(&data[iCellFirst], 0, cbrk-iCellFirst);
6032260395
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60323
- if( cbrk-iCellFirst!=pPage->nFree ){
60324
- return SQLITE_CORRUPT_BKPT;
60325
- }
6032660396
return SQLITE_OK;
6032760397
}
6032860398
6032960399
/*
6033060400
** Search the free-list on page pPg for space to store a cell nByte bytes in
@@ -60458,14 +60528,14 @@
6045860528
** to see if defragmentation is necessary.
6045960529
*/
6046060530
testcase( gap+2+nByte==top );
6046160531
if( gap+2+nByte>top ){
6046260532
assert( pPage->nCell>0 || CORRUPT_DB );
60463
- rc = defragmentPage(pPage);
60533
+ rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
6046460534
if( rc ) return rc;
6046560535
top = get2byteNotZero(&data[hdr+5]);
60466
- assert( gap+nByte<=top );
60536
+ assert( gap+2+nByte<=top );
6046760537
}
6046860538
6046960539
6047060540
/* Allocate memory from the gap in between the cell pointer array
6047160541
** and the cell content area. The btreeInitPage() call has already
@@ -66634,11 +66704,11 @@
6663466704
** copied into the parent, because if the parent is page 1 then it will
6663566705
** by smaller than the child due to the database header, and so all the
6663666706
** free space needs to be up front.
6663766707
*/
6663866708
assert( nNew==1 || CORRUPT_DB );
66639
- rc = defragmentPage(apNew[0]);
66709
+ rc = defragmentPage(apNew[0], -1);
6664066710
testcase( rc!=SQLITE_OK );
6664166711
assert( apNew[0]->nFree ==
6664266712
(get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
6664366713
|| rc!=SQLITE_OK
6664466714
);
@@ -71286,10 +71356,11 @@
7128671356
** Remember the SQL string for a prepared statement.
7128771357
*/
7128871358
SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
7128971359
assert( isPrepareV2==1 || isPrepareV2==0 );
7129071360
if( p==0 ) return;
71361
+ if( !isPrepareV2 ) p->expmask = 0;
7129171362
#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
7129271363
if( !isPrepareV2 ) return;
7129371364
#endif
7129471365
assert( p->zSql==0 );
7129571366
p->zSql = sqlite3DbStrNDup(p->db, z, n);
@@ -71314,10 +71385,11 @@
7131471385
pB->pPrev = pTmp;
7131571386
zTmp = pA->zSql;
7131671387
pA->zSql = pB->zSql;
7131771388
pB->zSql = zTmp;
7131871389
pB->isPrepareV2 = pA->isPrepareV2;
71390
+ pB->expmask = pA->expmask;
7131971391
}
7132071392
7132171393
/*
7132271394
** Resize the Vdbe.aOp array so that it is at least nOp elements larger
7132371395
** than its current size. nOp is guaranteed to be less than or equal
@@ -75779,12 +75851,12 @@
7577975851
** to sqlite3_reoptimize() that re-preparing the statement may result
7578075852
** in a better query plan.
7578175853
*/
7578275854
SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
7578375855
assert( iVar>0 );
75784
- if( iVar>32 ){
75785
- v->expmask = 0xffffffff;
75856
+ if( iVar>=32 ){
75857
+ v->expmask |= 0x80000000;
7578675858
}else{
7578775859
v->expmask |= ((u32)1 << (iVar-1));
7578875860
}
7578975861
}
7579075862
@@ -76050,11 +76122,12 @@
7605076122
sqlite3_mutex_enter(mutex);
7605176123
for(i=0; i<p->nVar; i++){
7605276124
sqlite3VdbeMemRelease(&p->aVar[i]);
7605376125
p->aVar[i].flags = MEM_Null;
7605476126
}
76055
- if( p->isPrepareV2 && p->expmask ){
76127
+ assert( p->isPrepareV2 || p->expmask==0 );
76128
+ if( p->expmask ){
7605676129
p->expired = 1;
7605776130
}
7605876131
sqlite3_mutex_leave(mutex);
7605976132
return rc;
7606076133
}
@@ -77154,13 +77227,12 @@
7715477227
** parameter in the WHERE clause might influence the choice of query plan
7715577228
** for a statement, then the statement will be automatically recompiled,
7715677229
** as if there had been a schema change, on the first sqlite3_step() call
7715777230
** following any change to the bindings of that parameter.
7715877231
*/
77159
- if( p->isPrepareV2 &&
77160
- ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
77161
- ){
77232
+ assert( p->isPrepareV2 || p->expmask==0 );
77233
+ if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
7716277234
p->expired = 1;
7716377235
}
7716477236
return SQLITE_OK;
7716577237
}
7716677238
@@ -77419,14 +77491,16 @@
7741977491
Vdbe *pFrom = (Vdbe*)pFromStmt;
7742077492
Vdbe *pTo = (Vdbe*)pToStmt;
7742177493
if( pFrom->nVar!=pTo->nVar ){
7742277494
return SQLITE_ERROR;
7742377495
}
77424
- if( pTo->isPrepareV2 && pTo->expmask ){
77496
+ assert( pTo->isPrepareV2 || pTo->expmask==0 );
77497
+ if( pTo->expmask ){
7742577498
pTo->expired = 1;
7742677499
}
77427
- if( pFrom->isPrepareV2 && pFrom->expmask ){
77500
+ assert( pFrom->isPrepareV2 || pFrom->expmask==0 );
77501
+ if( pFrom->expmask ){
7742877502
pFrom->expired = 1;
7742977503
}
7743077504
return sqlite3TransferBindings(pFromStmt, pToStmt);
7743177505
}
7743277506
#endif
@@ -112643,11 +112717,11 @@
112643112717
/* ColNames: */ 0, 0,
112644112718
/* iArg: */ 0 },
112645112719
#endif
112646112720
{/* zName: */ "optimize",
112647112721
/* ePragTyp: */ PragTyp_OPTIMIZE,
112648
- /* ePragFlg: */ PragFlg_NoColumns,
112722
+ /* ePragFlg: */ PragFlg_Result1,
112649112723
/* ColNames: */ 0, 0,
112650112724
/* iArg: */ 0 },
112651112725
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112652112726
{/* zName: */ "page_count",
112653112727
/* ePragTyp: */ PragTyp_PAGE_COUNT,
@@ -114678,38 +114752,55 @@
114678114752
break;
114679114753
}
114680114754
114681114755
/*
114682114756
** PRAGMA optimize
114757
+ ** PRAGMA optimize(MASK)
114683114758
** PRAGMA schema.optimize
114759
+ ** PRAGMA schema.optimize(MASK)
114684114760
**
114685114761
** Attempt to optimize the database. All schemas are optimized in the first
114686
- ** form, and only the specified schema is optimized in the second form.
114762
+ ** two forms, and only the specified schema is optimized in the latter two.
114687114763
**
114688114764
** The details of optimizations performed by this pragma does are expected
114689114765
** to change and improve over time. Applications should anticipate that
114690114766
** this pragma will perform new optimizations in future releases.
114691114767
**
114692
- ** Argments to this pragma are currently ignored, but future enhancements
114693
- ** might make use of arguments to control which optimizations are allowed
114694
- ** or to suggest limits on how much CPU time and I/O should be expended
114695
- ** in the optimization effort.
114696
- **
114697
- ** The current implementation runs ANALYZE on any tables which might have
114698
- ** benefitted from having recent statistics at some point since the start
114699
- ** of the current connection. Only tables in "schema" are analyzed in the
114700
- ** second form. In the first form, all tables except TEMP tables are
114701
- ** checked.
114702
- **
114703
- ** In the current implementation, a table is analyzed only if both of
114768
+ ** The optional argument is a bitmask of optimizations to perform:
114769
+ **
114770
+ ** 0x0001 Debugging mode. Do not actually perform any optimizations
114771
+ ** but instead return one line of text for each optimization
114772
+ ** that would have been done. Off by default.
114773
+ **
114774
+ ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
114775
+ ** See below for additional information.
114776
+ **
114777
+ ** 0x0004 (Not yet implemented) Record usage and performance
114778
+ ** information from the current session in the
114779
+ ** database file so that it will be available to "optimize"
114780
+ ** pragmas run by future database connections.
114781
+ **
114782
+ ** 0x0008 (Not yet implemented) Create indexes that might have
114783
+ ** been helpful to recent queries
114784
+ **
114785
+ ** The default MASK is 0x000e, which means perform all of the optimizations
114786
+ ** listed above except do not set Debug Mode. New optimizations may be
114787
+ ** added in future releases but they will be turned off by default. The
114788
+ ** default MASK will always be 0x0e.
114789
+ **
114790
+ ** DETERMINATION OF WHEN TO RUN ANALYZE
114791
+ **
114792
+ ** In the current implementation, a table is analyzed if only if all of
114704114793
** the following are true:
114705114794
**
114706
- ** (1) The query planner used sqlite_stat1-style statistics for one or
114795
+ ** (1) MASK bit 0x02 is set.
114796
+ **
114797
+ ** (2) The query planner used sqlite_stat1-style statistics for one or
114707114798
** more indexes of the table at some point during the lifetime of
114708114799
** the current connection.
114709114800
**
114710
- ** (2) One or more indexes of the table are currently unanalyzed OR
114801
+ ** (3) One or more indexes of the table are currently unanalyzed OR
114711114802
** the number of rows in the table has increased by 25 times or more
114712114803
** since the last time ANALYZE was run.
114713114804
**
114714114805
** The rules for when tables are analyzed are likely to change in
114715114806
** future releases.
@@ -114721,11 +114812,18 @@
114721114812
Schema *pSchema; /* The current schema */
114722114813
Table *pTab; /* A table in the schema */
114723114814
Index *pIdx; /* An index of the table */
114724114815
LogEst szThreshold; /* Size threshold above which reanalysis is needd */
114725114816
char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
114817
+ u32 opMask; /* Mask of operations to perform */
114726114818
114819
+ if( zRight ){
114820
+ opMask = (u32)sqlite3Atoi(zRight);
114821
+ if( (opMask & 0x02)==0 ) break;
114822
+ }else{
114823
+ opMask = 0xe;
114824
+ }
114727114825
iTabCur = pParse->nTab++;
114728114826
for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114729114827
if( iDb==1 ) continue;
114730114828
sqlite3CodeVerifySchema(pParse, iDb);
114731114829
pSchema = db->aDb[iDb].pSchema;
@@ -114746,16 +114844,22 @@
114746114844
}
114747114845
}
114748114846
if( szThreshold ){
114749114847
sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
114750114848
sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
114751
- sqlite3VdbeCurrentAddr(v)+2, szThreshold);
114849
+ sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
114752114850
VdbeCoverage(v);
114753114851
}
114754114852
zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
114755114853
db->aDb[iDb].zDbSName, pTab->zName);
114756
- sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
114854
+ if( opMask & 0x01 ){
114855
+ int r1 = sqlite3GetTempReg(pParse);
114856
+ sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
114857
+ sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
114858
+ }else{
114859
+ sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
114860
+ }
114757114861
}
114758114862
}
114759114863
sqlite3VdbeAddOp0(v, OP_Expire);
114760114864
break;
114761114865
}
@@ -140638,10 +140742,25 @@
140638140742
return 0;
140639140743
}
140640140744
#endif
140641140745
return db->lastRowid;
140642140746
}
140747
+
140748
+/*
140749
+** Set the value returned by the sqlite3_last_insert_rowid() API function.
140750
+*/
140751
+SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
140752
+#ifdef SQLITE_ENABLE_API_ARMOR
140753
+ if( !sqlite3SafetyCheckOk(db) ){
140754
+ (void)SQLITE_MISUSE_BKPT;
140755
+ return;
140756
+ }
140757
+#endif
140758
+ sqlite3_mutex_enter(db->mutex);
140759
+ db->lastRowid = iRowid;
140760
+ sqlite3_mutex_leave(db->mutex);
140761
+}
140643140762
140644140763
/*
140645140764
** Return the number of changes in the most recent call to sqlite3_exec().
140646140765
*/
140647140766
SQLITE_API int sqlite3_changes(sqlite3 *db){
@@ -148448,12 +148567,14 @@
148448148567
** segments.
148449148568
*/
148450148569
const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
148451148570
148452148571
Fts3Table *p = (Fts3Table*)pVtab;
148453
- int rc = sqlite3Fts3PendingTermsFlush(p);
148572
+ int rc;
148573
+ i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
148454148574
148575
+ rc = sqlite3Fts3PendingTermsFlush(p);
148455148576
if( rc==SQLITE_OK
148456148577
&& p->nLeafAdd>(nMinMerge/16)
148457148578
&& p->nAutoincrmerge && p->nAutoincrmerge!=0xff
148458148579
){
148459148580
int mxLevel = 0; /* Maximum relative level value in db */
@@ -148464,10 +148585,11 @@
148464148585
A = p->nLeafAdd * mxLevel;
148465148586
A += (A/2);
148466148587
if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
148467148588
}
148468148589
sqlite3Fts3SegmentsClose(p);
148590
+ sqlite3_set_last_insert_rowid(p->db, iLastRowid);
148469148591
return rc;
148470148592
}
148471148593
148472148594
/*
148473148595
** If it is currently unknown whether or not the FTS table has an %_stat
@@ -168799,10 +168921,11 @@
168799168921
int nStep; /* Rows processed for current object */
168800168922
int nProgress; /* Rows processed for all objects */
168801168923
RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
168802168924
const char *zVfsName; /* Name of automatically created rbu vfs */
168803168925
rbu_file *pTargetFd; /* File handle open on target db */
168926
+ int nPagePerSector; /* Pages per sector for pTargetFd */
168804168927
i64 iOalSz;
168805168928
i64 nPhaseOneStep;
168806168929
168807168930
/* The following state variables are used as part of the incremental
168808168931
** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
@@ -171063,10 +171186,27 @@
171063171186
171064171187
if( p->rc==SQLITE_OK ){
171065171188
if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
171066171189
p->rc = SQLITE_DONE;
171067171190
p->eStage = RBU_STAGE_DONE;
171191
+ }else{
171192
+ int nSectorSize;
171193
+ sqlite3_file *pDb = p->pTargetFd->pReal;
171194
+ sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
171195
+ assert( p->nPagePerSector==0 );
171196
+ nSectorSize = pDb->pMethods->xSectorSize(pDb);
171197
+ if( nSectorSize>p->pgsz ){
171198
+ p->nPagePerSector = nSectorSize / p->pgsz;
171199
+ }else{
171200
+ p->nPagePerSector = 1;
171201
+ }
171202
+
171203
+ /* Call xSync() on the wal file. This causes SQLite to sync the
171204
+ ** directory in which the target database and the wal file reside, in
171205
+ ** case it has not been synced since the rename() call in
171206
+ ** rbuMoveOalFile(). */
171207
+ p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
171068171208
}
171069171209
}
171070171210
}
171071171211
171072171212
/*
@@ -171718,13 +171858,30 @@
171718171858
if( p->rc==SQLITE_OK ){
171719171859
p->eStage = RBU_STAGE_DONE;
171720171860
p->rc = SQLITE_DONE;
171721171861
}
171722171862
}else{
171723
- RbuFrame *pFrame = &p->aFrame[p->nStep];
171724
- rbuCheckpointFrame(p, pFrame);
171725
- p->nStep++;
171863
+ /* At one point the following block copied a single frame from the
171864
+ ** wal file to the database file. So that one call to sqlite3rbu_step()
171865
+ ** checkpointed a single frame.
171866
+ **
171867
+ ** However, if the sector-size is larger than the page-size, and the
171868
+ ** application calls sqlite3rbu_savestate() or close() immediately
171869
+ ** after this step, then rbu_step() again, then a power failure occurs,
171870
+ ** then the database page written here may be damaged. Work around
171871
+ ** this by checkpointing frames until the next page in the aFrame[]
171872
+ ** lies on a different disk sector to the current one. */
171873
+ u32 iSector;
171874
+ do{
171875
+ RbuFrame *pFrame = &p->aFrame[p->nStep];
171876
+ iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
171877
+ rbuCheckpointFrame(p, pFrame);
171878
+ p->nStep++;
171879
+ }while( p->nStep<p->nFrame
171880
+ && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
171881
+ && p->rc==SQLITE_OK
171882
+ );
171726171883
}
171727171884
p->nProgress++;
171728171885
}
171729171886
break;
171730171887
}
@@ -172160,10 +172317,16 @@
172160172317
172161172318
/* Commit the transaction to the *-oal file. */
172162172319
if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172163172320
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
172164172321
}
172322
+
172323
+ /* Sync the db file if currently doing an incremental checkpoint */
172324
+ if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
172325
+ sqlite3_file *pDb = p->pTargetFd->pReal;
172326
+ p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
172327
+ }
172165172328
172166172329
rbuSaveState(p, p->eStage);
172167172330
172168172331
if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172169172332
p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
@@ -172284,10 +172447,16 @@
172284172447
assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
172285172448
if( p->eStage==RBU_STAGE_OAL ){
172286172449
assert( rc!=SQLITE_DONE );
172287172450
if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
172288172451
}
172452
+
172453
+ /* Sync the db file */
172454
+ if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
172455
+ sqlite3_file *pDb = p->pTargetFd->pReal;
172456
+ rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
172457
+ }
172289172458
172290172459
p->rc = rc;
172291172460
rbuSaveState(p, p->eStage);
172292172461
rc = p->rc;
172293172462
@@ -197884,11 +198053,11 @@
197884198053
int nArg, /* Number of args */
197885198054
sqlite3_value **apUnused /* Function arguments */
197886198055
){
197887198056
assert( nArg==0 );
197888198057
UNUSED_PARAM2(nArg, apUnused);
197889
- sqlite3_result_text(pCtx, "fts5: 2017-02-20 13:11:07 ff213f2ef5bf96754a2264685d25546d8b5ccf0a", -1, SQLITE_TRANSIENT);
198058
+ sqlite3_result_text(pCtx, "fts5: 2017-03-03 16:51:46 915a9a28783fbb2f4c0794eb4264ce8c0b9d42f7", -1, SQLITE_TRANSIENT);
197890198059
}
197891198060
197892198061
static int fts5Init(sqlite3 *db){
197893198062
static const sqlite3_module fts5Mod = {
197894198063
/* iVersion */ 2,
@@ -198547,15 +198716,10 @@
198547198716
sqlite3_step(pDel);
198548198717
rc = sqlite3_reset(pDel);
198549198718
}
198550198719
}
198551198720
198552
- /* Write the averages record */
198553
- if( rc==SQLITE_OK ){
198554
- rc = fts5StorageSaveTotals(p);
198555
- }
198556
-
198557198721
return rc;
198558198722
}
198559198723
198560198724
/*
198561198725
** Delete all entries in the FTS5 index.
@@ -198755,15 +198919,10 @@
198755198919
if( rc==SQLITE_OK ){
198756198920
rc = fts5StorageInsertDocsize(p, iRowid, &buf);
198757198921
}
198758198922
sqlite3_free(buf.p);
198759198923
198760
- /* Write the averages record */
198761
- if( rc==SQLITE_OK ){
198762
- rc = fts5StorageSaveTotals(p);
198763
- }
198764
-
198765198924
return rc;
198766198925
}
198767198926
198768198927
static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
198769198928
Fts5Config *pConfig = p->pConfig;
@@ -199094,16 +199253,21 @@
199094199253
199095199254
/*
199096199255
** Flush any data currently held in-memory to disk.
199097199256
*/
199098199257
static int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
199099
- if( bCommit && p->bTotalsValid ){
199100
- int rc = fts5StorageSaveTotals(p);
199101
- p->bTotalsValid = 0;
199102
- if( rc!=SQLITE_OK ) return rc;
199258
+ int rc = SQLITE_OK;
199259
+ i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
199260
+ if( p->bTotalsValid ){
199261
+ rc = fts5StorageSaveTotals(p);
199262
+ if( bCommit ) p->bTotalsValid = 0;
199103199263
}
199104
- return sqlite3Fts5IndexSync(p->pIndex, bCommit);
199264
+ if( rc==SQLITE_OK ){
199265
+ rc = sqlite3Fts5IndexSync(p->pIndex, bCommit);
199266
+ }
199267
+ sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
199268
+ return rc;
199105199269
}
199106199270
199107199271
static int sqlite3Fts5StorageRollback(Fts5Storage *p){
199108199272
p->bTotalsValid = 0;
199109199273
return sqlite3Fts5IndexRollback(p->pIndex);
199110199274
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399 ** [sqlite_version()] and [sqlite_source_id()].
400 */
401 #define SQLITE_VERSION "3.18.0"
402 #define SQLITE_VERSION_NUMBER 3018000
403 #define SQLITE_SOURCE_ID "2017-02-23 02:15:33 7a959f6d1ea038988cdb4c02d6f37abaec2580a0"
404
405 /*
406 ** CAPI3REF: Run-Time Library Version Numbers
407 ** KEYWORDS: sqlite3_version sqlite3_sourceid
408 **
@@ -2315,24 +2315,34 @@
2315 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2316 ** names are not also used by explicitly declared columns. ^If
2317 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
2318 ** is another alias for the rowid.
2319 **
2320 ** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
2321 ** most recent successful [INSERT] into a rowid table or [virtual table]
2322 ** on database connection D.
2323 ** ^Inserts into [WITHOUT ROWID] tables are not recorded.
2324 ** ^If no successful [INSERT]s into rowid tables
2325 ** have ever occurred on the database connection D,
2326 ** then sqlite3_last_insert_rowid(D) returns zero.
2327 **
2328 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2329 ** method, then this routine will return the [rowid] of the inserted
2330 ** row as long as the trigger or virtual table method is running.
2331 ** But once the trigger or virtual table method ends, the value returned
2332 ** by this routine reverts to what it was before the trigger or virtual
2333 ** table method began.)^
 
 
 
 
 
 
 
 
 
 
 
2334 **
2335 ** ^An [INSERT] that fails due to a constraint violation is not a
2336 ** successful [INSERT] and does not change the value returned by this
2337 ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2338 ** and INSERT OR ABORT make no changes to the return value of this
@@ -2355,10 +2365,20 @@
2355 ** unpredictable and might not equal either the old or the new
2356 ** last insert [rowid].
2357 */
2358 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2359
 
 
 
 
 
 
 
 
 
 
2360 /*
2361 ** CAPI3REF: Count The Number Of Rows Modified
2362 ** METHOD: sqlite3
2363 **
2364 ** ^This function returns the number of rows modified, inserted or
@@ -19513,22 +19533,23 @@
19513 **
19514 ** Move the date backwards to the beginning of the current day,
19515 ** or month or year.
19516 */
19517 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
 
19518 z += 9;
19519 computeYMD(p);
19520 p->validHMS = 1;
19521 p->h = p->m = 0;
19522 p->s = 0.0;
 
19523 p->validTZ = 0;
19524 p->validJD = 0;
19525 if( sqlite3_stricmp(z,"month")==0 ){
19526 p->D = 1;
19527 rc = 0;
19528 }else if( sqlite3_stricmp(z,"year")==0 ){
19529 computeYMD(p);
19530 p->M = 1;
19531 p->D = 1;
19532 rc = 0;
19533 }else if( sqlite3_stricmp(z,"day")==0 ){
19534 rc = 0;
@@ -60238,21 +60259,22 @@
60238 }
60239 #endif
60240
60241
60242 /*
60243 ** Defragment the page given. All Cells are moved to the
60244 ** end of the page and all free space is collected into one
60245 ** big FreeBlk that occurs in between the header and cell
60246 ** pointer array and the cell content area.
 
60247 **
60248 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
60249 ** b-tree page so that there are no freeblocks or fragment bytes, all
60250 ** unused bytes are contained in the unallocated space region, and all
60251 ** cells are packed tightly at the end of the page.
60252 */
60253 static int defragmentPage(MemPage *pPage){
60254 int i; /* Loop counter */
60255 int pc; /* Address of the i-th cell */
60256 int hdr; /* Offset to the page header */
60257 int size; /* Size of a cell */
60258 int usableSize; /* Number of usable bytes on a page */
@@ -60263,11 +60285,10 @@
60263 unsigned char *temp; /* Temp area for cell content */
60264 unsigned char *src; /* Source of content */
60265 int iCellFirst; /* First allowable cell index */
60266 int iCellLast; /* Last possible cell index */
60267
60268
60269 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60270 assert( pPage->pBt!=0 );
60271 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
60272 assert( pPage->nOverflow==0 );
60273 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
@@ -60275,13 +60296,60 @@
60275 src = data = pPage->aData;
60276 hdr = pPage->hdrOffset;
60277 cellOffset = pPage->cellOffset;
60278 nCell = pPage->nCell;
60279 assert( nCell==get2byte(&data[hdr+3]) );
 
60280 usableSize = pPage->pBt->usableSize;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60281 cbrk = usableSize;
60282 iCellFirst = cellOffset + 2*nCell;
60283 iCellLast = usableSize - 4;
60284 for(i=0; i<nCell; i++){
60285 u8 *pAddr; /* The i-th cell pointer */
60286 pAddr = &data[cellOffset + i*2];
60287 pc = get2byte(pAddr);
@@ -60311,20 +60379,22 @@
60311 memcpy(&temp[x], &data[x], (cbrk+size) - x);
60312 src = temp;
60313 }
60314 memcpy(&data[cbrk], &src[pc], size);
60315 }
 
 
 
 
 
 
60316 assert( cbrk>=iCellFirst );
60317 put2byte(&data[hdr+5], cbrk);
60318 data[hdr+1] = 0;
60319 data[hdr+2] = 0;
60320 data[hdr+7] = 0;
60321 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
60322 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60323 if( cbrk-iCellFirst!=pPage->nFree ){
60324 return SQLITE_CORRUPT_BKPT;
60325 }
60326 return SQLITE_OK;
60327 }
60328
60329 /*
60330 ** Search the free-list on page pPg for space to store a cell nByte bytes in
@@ -60458,14 +60528,14 @@
60458 ** to see if defragmentation is necessary.
60459 */
60460 testcase( gap+2+nByte==top );
60461 if( gap+2+nByte>top ){
60462 assert( pPage->nCell>0 || CORRUPT_DB );
60463 rc = defragmentPage(pPage);
60464 if( rc ) return rc;
60465 top = get2byteNotZero(&data[hdr+5]);
60466 assert( gap+nByte<=top );
60467 }
60468
60469
60470 /* Allocate memory from the gap in between the cell pointer array
60471 ** and the cell content area. The btreeInitPage() call has already
@@ -66634,11 +66704,11 @@
66634 ** copied into the parent, because if the parent is page 1 then it will
66635 ** by smaller than the child due to the database header, and so all the
66636 ** free space needs to be up front.
66637 */
66638 assert( nNew==1 || CORRUPT_DB );
66639 rc = defragmentPage(apNew[0]);
66640 testcase( rc!=SQLITE_OK );
66641 assert( apNew[0]->nFree ==
66642 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
66643 || rc!=SQLITE_OK
66644 );
@@ -71286,10 +71356,11 @@
71286 ** Remember the SQL string for a prepared statement.
71287 */
71288 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
71289 assert( isPrepareV2==1 || isPrepareV2==0 );
71290 if( p==0 ) return;
 
71291 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
71292 if( !isPrepareV2 ) return;
71293 #endif
71294 assert( p->zSql==0 );
71295 p->zSql = sqlite3DbStrNDup(p->db, z, n);
@@ -71314,10 +71385,11 @@
71314 pB->pPrev = pTmp;
71315 zTmp = pA->zSql;
71316 pA->zSql = pB->zSql;
71317 pB->zSql = zTmp;
71318 pB->isPrepareV2 = pA->isPrepareV2;
 
71319 }
71320
71321 /*
71322 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
71323 ** than its current size. nOp is guaranteed to be less than or equal
@@ -75779,12 +75851,12 @@
75779 ** to sqlite3_reoptimize() that re-preparing the statement may result
75780 ** in a better query plan.
75781 */
75782 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
75783 assert( iVar>0 );
75784 if( iVar>32 ){
75785 v->expmask = 0xffffffff;
75786 }else{
75787 v->expmask |= ((u32)1 << (iVar-1));
75788 }
75789 }
75790
@@ -76050,11 +76122,12 @@
76050 sqlite3_mutex_enter(mutex);
76051 for(i=0; i<p->nVar; i++){
76052 sqlite3VdbeMemRelease(&p->aVar[i]);
76053 p->aVar[i].flags = MEM_Null;
76054 }
76055 if( p->isPrepareV2 && p->expmask ){
 
76056 p->expired = 1;
76057 }
76058 sqlite3_mutex_leave(mutex);
76059 return rc;
76060 }
@@ -77154,13 +77227,12 @@
77154 ** parameter in the WHERE clause might influence the choice of query plan
77155 ** for a statement, then the statement will be automatically recompiled,
77156 ** as if there had been a schema change, on the first sqlite3_step() call
77157 ** following any change to the bindings of that parameter.
77158 */
77159 if( p->isPrepareV2 &&
77160 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
77161 ){
77162 p->expired = 1;
77163 }
77164 return SQLITE_OK;
77165 }
77166
@@ -77419,14 +77491,16 @@
77419 Vdbe *pFrom = (Vdbe*)pFromStmt;
77420 Vdbe *pTo = (Vdbe*)pToStmt;
77421 if( pFrom->nVar!=pTo->nVar ){
77422 return SQLITE_ERROR;
77423 }
77424 if( pTo->isPrepareV2 && pTo->expmask ){
 
77425 pTo->expired = 1;
77426 }
77427 if( pFrom->isPrepareV2 && pFrom->expmask ){
 
77428 pFrom->expired = 1;
77429 }
77430 return sqlite3TransferBindings(pFromStmt, pToStmt);
77431 }
77432 #endif
@@ -112643,11 +112717,11 @@
112643 /* ColNames: */ 0, 0,
112644 /* iArg: */ 0 },
112645 #endif
112646 {/* zName: */ "optimize",
112647 /* ePragTyp: */ PragTyp_OPTIMIZE,
112648 /* ePragFlg: */ PragFlg_NoColumns,
112649 /* ColNames: */ 0, 0,
112650 /* iArg: */ 0 },
112651 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112652 {/* zName: */ "page_count",
112653 /* ePragTyp: */ PragTyp_PAGE_COUNT,
@@ -114678,38 +114752,55 @@
114678 break;
114679 }
114680
114681 /*
114682 ** PRAGMA optimize
 
114683 ** PRAGMA schema.optimize
 
114684 **
114685 ** Attempt to optimize the database. All schemas are optimized in the first
114686 ** form, and only the specified schema is optimized in the second form.
114687 **
114688 ** The details of optimizations performed by this pragma does are expected
114689 ** to change and improve over time. Applications should anticipate that
114690 ** this pragma will perform new optimizations in future releases.
114691 **
114692 ** Argments to this pragma are currently ignored, but future enhancements
114693 ** might make use of arguments to control which optimizations are allowed
114694 ** or to suggest limits on how much CPU time and I/O should be expended
114695 ** in the optimization effort.
114696 **
114697 ** The current implementation runs ANALYZE on any tables which might have
114698 ** benefitted from having recent statistics at some point since the start
114699 ** of the current connection. Only tables in "schema" are analyzed in the
114700 ** second form. In the first form, all tables except TEMP tables are
114701 ** checked.
114702 **
114703 ** In the current implementation, a table is analyzed only if both of
 
 
 
 
 
 
 
 
 
 
 
 
 
114704 ** the following are true:
114705 **
114706 ** (1) The query planner used sqlite_stat1-style statistics for one or
 
 
114707 ** more indexes of the table at some point during the lifetime of
114708 ** the current connection.
114709 **
114710 ** (2) One or more indexes of the table are currently unanalyzed OR
114711 ** the number of rows in the table has increased by 25 times or more
114712 ** since the last time ANALYZE was run.
114713 **
114714 ** The rules for when tables are analyzed are likely to change in
114715 ** future releases.
@@ -114721,11 +114812,18 @@
114721 Schema *pSchema; /* The current schema */
114722 Table *pTab; /* A table in the schema */
114723 Index *pIdx; /* An index of the table */
114724 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
114725 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
 
114726
 
 
 
 
 
 
114727 iTabCur = pParse->nTab++;
114728 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114729 if( iDb==1 ) continue;
114730 sqlite3CodeVerifySchema(pParse, iDb);
114731 pSchema = db->aDb[iDb].pSchema;
@@ -114746,16 +114844,22 @@
114746 }
114747 }
114748 if( szThreshold ){
114749 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
114750 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
114751 sqlite3VdbeCurrentAddr(v)+2, szThreshold);
114752 VdbeCoverage(v);
114753 }
114754 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
114755 db->aDb[iDb].zDbSName, pTab->zName);
114756 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
 
 
 
 
 
 
114757 }
114758 }
114759 sqlite3VdbeAddOp0(v, OP_Expire);
114760 break;
114761 }
@@ -140638,10 +140742,25 @@
140638 return 0;
140639 }
140640 #endif
140641 return db->lastRowid;
140642 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140643
140644 /*
140645 ** Return the number of changes in the most recent call to sqlite3_exec().
140646 */
140647 SQLITE_API int sqlite3_changes(sqlite3 *db){
@@ -148448,12 +148567,14 @@
148448 ** segments.
148449 */
148450 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
148451
148452 Fts3Table *p = (Fts3Table*)pVtab;
148453 int rc = sqlite3Fts3PendingTermsFlush(p);
 
148454
 
148455 if( rc==SQLITE_OK
148456 && p->nLeafAdd>(nMinMerge/16)
148457 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
148458 ){
148459 int mxLevel = 0; /* Maximum relative level value in db */
@@ -148464,10 +148585,11 @@
148464 A = p->nLeafAdd * mxLevel;
148465 A += (A/2);
148466 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
148467 }
148468 sqlite3Fts3SegmentsClose(p);
 
148469 return rc;
148470 }
148471
148472 /*
148473 ** If it is currently unknown whether or not the FTS table has an %_stat
@@ -168799,10 +168921,11 @@
168799 int nStep; /* Rows processed for current object */
168800 int nProgress; /* Rows processed for all objects */
168801 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
168802 const char *zVfsName; /* Name of automatically created rbu vfs */
168803 rbu_file *pTargetFd; /* File handle open on target db */
 
168804 i64 iOalSz;
168805 i64 nPhaseOneStep;
168806
168807 /* The following state variables are used as part of the incremental
168808 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
@@ -171063,10 +171186,27 @@
171063
171064 if( p->rc==SQLITE_OK ){
171065 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
171066 p->rc = SQLITE_DONE;
171067 p->eStage = RBU_STAGE_DONE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171068 }
171069 }
171070 }
171071
171072 /*
@@ -171718,13 +171858,30 @@
171718 if( p->rc==SQLITE_OK ){
171719 p->eStage = RBU_STAGE_DONE;
171720 p->rc = SQLITE_DONE;
171721 }
171722 }else{
171723 RbuFrame *pFrame = &p->aFrame[p->nStep];
171724 rbuCheckpointFrame(p, pFrame);
171725 p->nStep++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171726 }
171727 p->nProgress++;
171728 }
171729 break;
171730 }
@@ -172160,10 +172317,16 @@
172160
172161 /* Commit the transaction to the *-oal file. */
172162 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172163 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
172164 }
 
 
 
 
 
 
172165
172166 rbuSaveState(p, p->eStage);
172167
172168 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172169 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
@@ -172284,10 +172447,16 @@
172284 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
172285 if( p->eStage==RBU_STAGE_OAL ){
172286 assert( rc!=SQLITE_DONE );
172287 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
172288 }
 
 
 
 
 
 
172289
172290 p->rc = rc;
172291 rbuSaveState(p, p->eStage);
172292 rc = p->rc;
172293
@@ -197884,11 +198053,11 @@
197884 int nArg, /* Number of args */
197885 sqlite3_value **apUnused /* Function arguments */
197886 ){
197887 assert( nArg==0 );
197888 UNUSED_PARAM2(nArg, apUnused);
197889 sqlite3_result_text(pCtx, "fts5: 2017-02-20 13:11:07 ff213f2ef5bf96754a2264685d25546d8b5ccf0a", -1, SQLITE_TRANSIENT);
197890 }
197891
197892 static int fts5Init(sqlite3 *db){
197893 static const sqlite3_module fts5Mod = {
197894 /* iVersion */ 2,
@@ -198547,15 +198716,10 @@
198547 sqlite3_step(pDel);
198548 rc = sqlite3_reset(pDel);
198549 }
198550 }
198551
198552 /* Write the averages record */
198553 if( rc==SQLITE_OK ){
198554 rc = fts5StorageSaveTotals(p);
198555 }
198556
198557 return rc;
198558 }
198559
198560 /*
198561 ** Delete all entries in the FTS5 index.
@@ -198755,15 +198919,10 @@
198755 if( rc==SQLITE_OK ){
198756 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
198757 }
198758 sqlite3_free(buf.p);
198759
198760 /* Write the averages record */
198761 if( rc==SQLITE_OK ){
198762 rc = fts5StorageSaveTotals(p);
198763 }
198764
198765 return rc;
198766 }
198767
198768 static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
198769 Fts5Config *pConfig = p->pConfig;
@@ -199094,16 +199253,21 @@
199094
199095 /*
199096 ** Flush any data currently held in-memory to disk.
199097 */
199098 static int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
199099 if( bCommit && p->bTotalsValid ){
199100 int rc = fts5StorageSaveTotals(p);
199101 p->bTotalsValid = 0;
199102 if( rc!=SQLITE_OK ) return rc;
 
199103 }
199104 return sqlite3Fts5IndexSync(p->pIndex, bCommit);
 
 
 
 
199105 }
199106
199107 static int sqlite3Fts5StorageRollback(Fts5Storage *p){
199108 p->bTotalsValid = 0;
199109 return sqlite3Fts5IndexRollback(p->pIndex);
199110
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399 ** [sqlite_version()] and [sqlite_source_id()].
400 */
401 #define SQLITE_VERSION "3.18.0"
402 #define SQLITE_VERSION_NUMBER 3018000
403 #define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
404
405 /*
406 ** CAPI3REF: Run-Time Library Version Numbers
407 ** KEYWORDS: sqlite3_version sqlite3_sourceid
408 **
@@ -2315,24 +2315,34 @@
2315 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2316 ** names are not also used by explicitly declared columns. ^If
2317 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
2318 ** is another alias for the rowid.
2319 **
2320 ** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2321 ** the most recent successful [INSERT] into a rowid table or [virtual table]
2322 ** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2323 ** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2324 ** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2325 ** zero.
 
2326 **
2327 ** As well as being set automatically as rows are inserted into database
2328 ** tables, the value returned by this function may be set explicitly by
2329 ** [sqlite3_set_last_insert_rowid()]
2330 **
2331 ** Some virtual table implementations may INSERT rows into rowid tables as
2332 ** part of committing a transaction (e.g. to flush data accumulated in memory
2333 ** to disk). In this case subsequent calls to this function return the rowid
2334 ** associated with these internal INSERT operations, which leads to
2335 ** unintuitive results. Virtual table implementations that do write to rowid
2336 ** tables in this way can avoid this problem by restoring the original
2337 ** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2338 ** control to the user.
2339 **
2340 ** ^(If an [INSERT] occurs within a trigger then this routine will
2341 ** return the [rowid] of the inserted row as long as the trigger is
2342 ** running. Once the trigger program ends, the value returned
2343 ** by this routine reverts to what it was before the trigger was fired.)^
2344 **
2345 ** ^An [INSERT] that fails due to a constraint violation is not a
2346 ** successful [INSERT] and does not change the value returned by this
2347 ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2348 ** and INSERT OR ABORT make no changes to the return value of this
@@ -2355,10 +2365,20 @@
2365 ** unpredictable and might not equal either the old or the new
2366 ** last insert [rowid].
2367 */
2368 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2369
2370 /*
2371 ** CAPI3REF: Set the Last Insert Rowid value.
2372 ** METHOD: sqlite3
2373 **
2374 ** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2375 ** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2376 ** without inserting a row into the database.
2377 */
2378 SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2379
2380 /*
2381 ** CAPI3REF: Count The Number Of Rows Modified
2382 ** METHOD: sqlite3
2383 **
2384 ** ^This function returns the number of rows modified, inserted or
@@ -19513,22 +19533,23 @@
19533 **
19534 ** Move the date backwards to the beginning of the current day,
19535 ** or month or year.
19536 */
19537 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
19538 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
19539 z += 9;
19540 computeYMD(p);
19541 p->validHMS = 1;
19542 p->h = p->m = 0;
19543 p->s = 0.0;
19544 p->rawS = 0;
19545 p->validTZ = 0;
19546 p->validJD = 0;
19547 if( sqlite3_stricmp(z,"month")==0 ){
19548 p->D = 1;
19549 rc = 0;
19550 }else if( sqlite3_stricmp(z,"year")==0 ){
 
19551 p->M = 1;
19552 p->D = 1;
19553 rc = 0;
19554 }else if( sqlite3_stricmp(z,"day")==0 ){
19555 rc = 0;
@@ -60238,21 +60259,22 @@
60259 }
60260 #endif
60261
60262
60263 /*
60264 ** Defragment the page given. This routine reorganizes cells within the
60265 ** page so that there are no free-blocks on the free-block list.
60266 **
60267 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be
60268 ** present in the page after this routine returns.
60269 **
60270 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
60271 ** b-tree page so that there are no freeblocks or fragment bytes, all
60272 ** unused bytes are contained in the unallocated space region, and all
60273 ** cells are packed tightly at the end of the page.
60274 */
60275 static int defragmentPage(MemPage *pPage, int nMaxFrag){
60276 int i; /* Loop counter */
60277 int pc; /* Address of the i-th cell */
60278 int hdr; /* Offset to the page header */
60279 int size; /* Size of a cell */
60280 int usableSize; /* Number of usable bytes on a page */
@@ -60263,11 +60285,10 @@
60285 unsigned char *temp; /* Temp area for cell content */
60286 unsigned char *src; /* Source of content */
60287 int iCellFirst; /* First allowable cell index */
60288 int iCellLast; /* Last possible cell index */
60289
 
60290 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
60291 assert( pPage->pBt!=0 );
60292 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
60293 assert( pPage->nOverflow==0 );
60294 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
@@ -60275,13 +60296,60 @@
60296 src = data = pPage->aData;
60297 hdr = pPage->hdrOffset;
60298 cellOffset = pPage->cellOffset;
60299 nCell = pPage->nCell;
60300 assert( nCell==get2byte(&data[hdr+3]) );
60301 iCellFirst = cellOffset + 2*nCell;
60302 usableSize = pPage->pBt->usableSize;
60303
60304 /* This block handles pages with two or fewer free blocks and nMaxFrag
60305 ** or fewer fragmented bytes. In this case it is faster to move the
60306 ** two (or one) blocks of cells using memmove() and add the required
60307 ** offsets to each pointer in the cell-pointer array than it is to
60308 ** reconstruct the entire page. */
60309 if( (int)data[hdr+7]<=nMaxFrag ){
60310 int iFree = get2byte(&data[hdr+1]);
60311 if( iFree ){
60312 int iFree2 = get2byte(&data[iFree]);
60313
60314 /* pageFindSlot() has already verified that free blocks are sorted
60315 ** in order of offset within the page, and that no block extends
60316 ** past the end of the page. Provided the two free slots do not
60317 ** overlap, this guarantees that the memmove() calls below will not
60318 ** overwrite the usableSize byte buffer, even if the database page
60319 ** is corrupt. */
60320 assert( iFree2==0 || iFree2>iFree );
60321 assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
60322 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
60323
60324 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
60325 u8 *pEnd = &data[cellOffset + nCell*2];
60326 u8 *pAddr;
60327 int sz2 = 0;
60328 int sz = get2byte(&data[iFree+2]);
60329 int top = get2byte(&data[hdr+5]);
60330 if( iFree2 ){
60331 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_BKPT;
60332 sz2 = get2byte(&data[iFree2+2]);
60333 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
60334 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
60335 sz += sz2;
60336 }
60337 cbrk = top+sz;
60338 assert( cbrk+(iFree-top) <= usableSize );
60339 memmove(&data[cbrk], &data[top], iFree-top);
60340 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
60341 pc = get2byte(pAddr);
60342 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
60343 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
60344 }
60345 goto defragment_out;
60346 }
60347 }
60348 }
60349
60350 cbrk = usableSize;
 
60351 iCellLast = usableSize - 4;
60352 for(i=0; i<nCell; i++){
60353 u8 *pAddr; /* The i-th cell pointer */
60354 pAddr = &data[cellOffset + i*2];
60355 pc = get2byte(pAddr);
@@ -60311,20 +60379,22 @@
60379 memcpy(&temp[x], &data[x], (cbrk+size) - x);
60380 src = temp;
60381 }
60382 memcpy(&data[cbrk], &src[pc], size);
60383 }
60384 data[hdr+7] = 0;
60385
60386 defragment_out:
60387 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
60388 return SQLITE_CORRUPT_BKPT;
60389 }
60390 assert( cbrk>=iCellFirst );
60391 put2byte(&data[hdr+5], cbrk);
60392 data[hdr+1] = 0;
60393 data[hdr+2] = 0;
 
60394 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
60395 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 
 
 
60396 return SQLITE_OK;
60397 }
60398
60399 /*
60400 ** Search the free-list on page pPg for space to store a cell nByte bytes in
@@ -60458,14 +60528,14 @@
60528 ** to see if defragmentation is necessary.
60529 */
60530 testcase( gap+2+nByte==top );
60531 if( gap+2+nByte>top ){
60532 assert( pPage->nCell>0 || CORRUPT_DB );
60533 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
60534 if( rc ) return rc;
60535 top = get2byteNotZero(&data[hdr+5]);
60536 assert( gap+2+nByte<=top );
60537 }
60538
60539
60540 /* Allocate memory from the gap in between the cell pointer array
60541 ** and the cell content area. The btreeInitPage() call has already
@@ -66634,11 +66704,11 @@
66704 ** copied into the parent, because if the parent is page 1 then it will
66705 ** by smaller than the child due to the database header, and so all the
66706 ** free space needs to be up front.
66707 */
66708 assert( nNew==1 || CORRUPT_DB );
66709 rc = defragmentPage(apNew[0], -1);
66710 testcase( rc!=SQLITE_OK );
66711 assert( apNew[0]->nFree ==
66712 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
66713 || rc!=SQLITE_OK
66714 );
@@ -71286,10 +71356,11 @@
71356 ** Remember the SQL string for a prepared statement.
71357 */
71358 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
71359 assert( isPrepareV2==1 || isPrepareV2==0 );
71360 if( p==0 ) return;
71361 if( !isPrepareV2 ) p->expmask = 0;
71362 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
71363 if( !isPrepareV2 ) return;
71364 #endif
71365 assert( p->zSql==0 );
71366 p->zSql = sqlite3DbStrNDup(p->db, z, n);
@@ -71314,10 +71385,11 @@
71385 pB->pPrev = pTmp;
71386 zTmp = pA->zSql;
71387 pA->zSql = pB->zSql;
71388 pB->zSql = zTmp;
71389 pB->isPrepareV2 = pA->isPrepareV2;
71390 pB->expmask = pA->expmask;
71391 }
71392
71393 /*
71394 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
71395 ** than its current size. nOp is guaranteed to be less than or equal
@@ -75779,12 +75851,12 @@
75851 ** to sqlite3_reoptimize() that re-preparing the statement may result
75852 ** in a better query plan.
75853 */
75854 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
75855 assert( iVar>0 );
75856 if( iVar>=32 ){
75857 v->expmask |= 0x80000000;
75858 }else{
75859 v->expmask |= ((u32)1 << (iVar-1));
75860 }
75861 }
75862
@@ -76050,11 +76122,12 @@
76122 sqlite3_mutex_enter(mutex);
76123 for(i=0; i<p->nVar; i++){
76124 sqlite3VdbeMemRelease(&p->aVar[i]);
76125 p->aVar[i].flags = MEM_Null;
76126 }
76127 assert( p->isPrepareV2 || p->expmask==0 );
76128 if( p->expmask ){
76129 p->expired = 1;
76130 }
76131 sqlite3_mutex_leave(mutex);
76132 return rc;
76133 }
@@ -77154,13 +77227,12 @@
77227 ** parameter in the WHERE clause might influence the choice of query plan
77228 ** for a statement, then the statement will be automatically recompiled,
77229 ** as if there had been a schema change, on the first sqlite3_step() call
77230 ** following any change to the bindings of that parameter.
77231 */
77232 assert( p->isPrepareV2 || p->expmask==0 );
77233 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
 
77234 p->expired = 1;
77235 }
77236 return SQLITE_OK;
77237 }
77238
@@ -77419,14 +77491,16 @@
77491 Vdbe *pFrom = (Vdbe*)pFromStmt;
77492 Vdbe *pTo = (Vdbe*)pToStmt;
77493 if( pFrom->nVar!=pTo->nVar ){
77494 return SQLITE_ERROR;
77495 }
77496 assert( pTo->isPrepareV2 || pTo->expmask==0 );
77497 if( pTo->expmask ){
77498 pTo->expired = 1;
77499 }
77500 assert( pFrom->isPrepareV2 || pFrom->expmask==0 );
77501 if( pFrom->expmask ){
77502 pFrom->expired = 1;
77503 }
77504 return sqlite3TransferBindings(pFromStmt, pToStmt);
77505 }
77506 #endif
@@ -112643,11 +112717,11 @@
112717 /* ColNames: */ 0, 0,
112718 /* iArg: */ 0 },
112719 #endif
112720 {/* zName: */ "optimize",
112721 /* ePragTyp: */ PragTyp_OPTIMIZE,
112722 /* ePragFlg: */ PragFlg_Result1,
112723 /* ColNames: */ 0, 0,
112724 /* iArg: */ 0 },
112725 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112726 {/* zName: */ "page_count",
112727 /* ePragTyp: */ PragTyp_PAGE_COUNT,
@@ -114678,38 +114752,55 @@
114752 break;
114753 }
114754
114755 /*
114756 ** PRAGMA optimize
114757 ** PRAGMA optimize(MASK)
114758 ** PRAGMA schema.optimize
114759 ** PRAGMA schema.optimize(MASK)
114760 **
114761 ** Attempt to optimize the database. All schemas are optimized in the first
114762 ** two forms, and only the specified schema is optimized in the latter two.
114763 **
114764 ** The details of optimizations performed by this pragma does are expected
114765 ** to change and improve over time. Applications should anticipate that
114766 ** this pragma will perform new optimizations in future releases.
114767 **
114768 ** The optional argument is a bitmask of optimizations to perform:
114769 **
114770 ** 0x0001 Debugging mode. Do not actually perform any optimizations
114771 ** but instead return one line of text for each optimization
114772 ** that would have been done. Off by default.
114773 **
114774 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
114775 ** See below for additional information.
114776 **
114777 ** 0x0004 (Not yet implemented) Record usage and performance
114778 ** information from the current session in the
114779 ** database file so that it will be available to "optimize"
114780 ** pragmas run by future database connections.
114781 **
114782 ** 0x0008 (Not yet implemented) Create indexes that might have
114783 ** been helpful to recent queries
114784 **
114785 ** The default MASK is 0x000e, which means perform all of the optimizations
114786 ** listed above except do not set Debug Mode. New optimizations may be
114787 ** added in future releases but they will be turned off by default. The
114788 ** default MASK will always be 0x0e.
114789 **
114790 ** DETERMINATION OF WHEN TO RUN ANALYZE
114791 **
114792 ** In the current implementation, a table is analyzed if only if all of
114793 ** the following are true:
114794 **
114795 ** (1) MASK bit 0x02 is set.
114796 **
114797 ** (2) The query planner used sqlite_stat1-style statistics for one or
114798 ** more indexes of the table at some point during the lifetime of
114799 ** the current connection.
114800 **
114801 ** (3) One or more indexes of the table are currently unanalyzed OR
114802 ** the number of rows in the table has increased by 25 times or more
114803 ** since the last time ANALYZE was run.
114804 **
114805 ** The rules for when tables are analyzed are likely to change in
114806 ** future releases.
@@ -114721,11 +114812,18 @@
114812 Schema *pSchema; /* The current schema */
114813 Table *pTab; /* A table in the schema */
114814 Index *pIdx; /* An index of the table */
114815 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
114816 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
114817 u32 opMask; /* Mask of operations to perform */
114818
114819 if( zRight ){
114820 opMask = (u32)sqlite3Atoi(zRight);
114821 if( (opMask & 0x02)==0 ) break;
114822 }else{
114823 opMask = 0xe;
114824 }
114825 iTabCur = pParse->nTab++;
114826 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114827 if( iDb==1 ) continue;
114828 sqlite3CodeVerifySchema(pParse, iDb);
114829 pSchema = db->aDb[iDb].pSchema;
@@ -114746,16 +114844,22 @@
114844 }
114845 }
114846 if( szThreshold ){
114847 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
114848 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
114849 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
114850 VdbeCoverage(v);
114851 }
114852 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
114853 db->aDb[iDb].zDbSName, pTab->zName);
114854 if( opMask & 0x01 ){
114855 int r1 = sqlite3GetTempReg(pParse);
114856 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
114857 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
114858 }else{
114859 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
114860 }
114861 }
114862 }
114863 sqlite3VdbeAddOp0(v, OP_Expire);
114864 break;
114865 }
@@ -140638,10 +140742,25 @@
140742 return 0;
140743 }
140744 #endif
140745 return db->lastRowid;
140746 }
140747
140748 /*
140749 ** Set the value returned by the sqlite3_last_insert_rowid() API function.
140750 */
140751 SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
140752 #ifdef SQLITE_ENABLE_API_ARMOR
140753 if( !sqlite3SafetyCheckOk(db) ){
140754 (void)SQLITE_MISUSE_BKPT;
140755 return;
140756 }
140757 #endif
140758 sqlite3_mutex_enter(db->mutex);
140759 db->lastRowid = iRowid;
140760 sqlite3_mutex_leave(db->mutex);
140761 }
140762
140763 /*
140764 ** Return the number of changes in the most recent call to sqlite3_exec().
140765 */
140766 SQLITE_API int sqlite3_changes(sqlite3 *db){
@@ -148448,12 +148567,14 @@
148567 ** segments.
148568 */
148569 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
148570
148571 Fts3Table *p = (Fts3Table*)pVtab;
148572 int rc;
148573 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
148574
148575 rc = sqlite3Fts3PendingTermsFlush(p);
148576 if( rc==SQLITE_OK
148577 && p->nLeafAdd>(nMinMerge/16)
148578 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
148579 ){
148580 int mxLevel = 0; /* Maximum relative level value in db */
@@ -148464,10 +148585,11 @@
148585 A = p->nLeafAdd * mxLevel;
148586 A += (A/2);
148587 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
148588 }
148589 sqlite3Fts3SegmentsClose(p);
148590 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
148591 return rc;
148592 }
148593
148594 /*
148595 ** If it is currently unknown whether or not the FTS table has an %_stat
@@ -168799,10 +168921,11 @@
168921 int nStep; /* Rows processed for current object */
168922 int nProgress; /* Rows processed for all objects */
168923 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
168924 const char *zVfsName; /* Name of automatically created rbu vfs */
168925 rbu_file *pTargetFd; /* File handle open on target db */
168926 int nPagePerSector; /* Pages per sector for pTargetFd */
168927 i64 iOalSz;
168928 i64 nPhaseOneStep;
168929
168930 /* The following state variables are used as part of the incremental
168931 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
@@ -171063,10 +171186,27 @@
171186
171187 if( p->rc==SQLITE_OK ){
171188 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
171189 p->rc = SQLITE_DONE;
171190 p->eStage = RBU_STAGE_DONE;
171191 }else{
171192 int nSectorSize;
171193 sqlite3_file *pDb = p->pTargetFd->pReal;
171194 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
171195 assert( p->nPagePerSector==0 );
171196 nSectorSize = pDb->pMethods->xSectorSize(pDb);
171197 if( nSectorSize>p->pgsz ){
171198 p->nPagePerSector = nSectorSize / p->pgsz;
171199 }else{
171200 p->nPagePerSector = 1;
171201 }
171202
171203 /* Call xSync() on the wal file. This causes SQLite to sync the
171204 ** directory in which the target database and the wal file reside, in
171205 ** case it has not been synced since the rename() call in
171206 ** rbuMoveOalFile(). */
171207 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
171208 }
171209 }
171210 }
171211
171212 /*
@@ -171718,13 +171858,30 @@
171858 if( p->rc==SQLITE_OK ){
171859 p->eStage = RBU_STAGE_DONE;
171860 p->rc = SQLITE_DONE;
171861 }
171862 }else{
171863 /* At one point the following block copied a single frame from the
171864 ** wal file to the database file. So that one call to sqlite3rbu_step()
171865 ** checkpointed a single frame.
171866 **
171867 ** However, if the sector-size is larger than the page-size, and the
171868 ** application calls sqlite3rbu_savestate() or close() immediately
171869 ** after this step, then rbu_step() again, then a power failure occurs,
171870 ** then the database page written here may be damaged. Work around
171871 ** this by checkpointing frames until the next page in the aFrame[]
171872 ** lies on a different disk sector to the current one. */
171873 u32 iSector;
171874 do{
171875 RbuFrame *pFrame = &p->aFrame[p->nStep];
171876 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
171877 rbuCheckpointFrame(p, pFrame);
171878 p->nStep++;
171879 }while( p->nStep<p->nFrame
171880 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
171881 && p->rc==SQLITE_OK
171882 );
171883 }
171884 p->nProgress++;
171885 }
171886 break;
171887 }
@@ -172160,10 +172317,16 @@
172317
172318 /* Commit the transaction to the *-oal file. */
172319 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172320 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
172321 }
172322
172323 /* Sync the db file if currently doing an incremental checkpoint */
172324 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
172325 sqlite3_file *pDb = p->pTargetFd->pReal;
172326 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
172327 }
172328
172329 rbuSaveState(p, p->eStage);
172330
172331 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
172332 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
@@ -172284,10 +172447,16 @@
172447 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
172448 if( p->eStage==RBU_STAGE_OAL ){
172449 assert( rc!=SQLITE_DONE );
172450 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
172451 }
172452
172453 /* Sync the db file */
172454 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
172455 sqlite3_file *pDb = p->pTargetFd->pReal;
172456 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
172457 }
172458
172459 p->rc = rc;
172460 rbuSaveState(p, p->eStage);
172461 rc = p->rc;
172462
@@ -197884,11 +198053,11 @@
198053 int nArg, /* Number of args */
198054 sqlite3_value **apUnused /* Function arguments */
198055 ){
198056 assert( nArg==0 );
198057 UNUSED_PARAM2(nArg, apUnused);
198058 sqlite3_result_text(pCtx, "fts5: 2017-03-03 16:51:46 915a9a28783fbb2f4c0794eb4264ce8c0b9d42f7", -1, SQLITE_TRANSIENT);
198059 }
198060
198061 static int fts5Init(sqlite3 *db){
198062 static const sqlite3_module fts5Mod = {
198063 /* iVersion */ 2,
@@ -198547,15 +198716,10 @@
198716 sqlite3_step(pDel);
198717 rc = sqlite3_reset(pDel);
198718 }
198719 }
198720
 
 
 
 
 
198721 return rc;
198722 }
198723
198724 /*
198725 ** Delete all entries in the FTS5 index.
@@ -198755,15 +198919,10 @@
198919 if( rc==SQLITE_OK ){
198920 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
198921 }
198922 sqlite3_free(buf.p);
198923
 
 
 
 
 
198924 return rc;
198925 }
198926
198927 static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
198928 Fts5Config *pConfig = p->pConfig;
@@ -199094,16 +199253,21 @@
199253
199254 /*
199255 ** Flush any data currently held in-memory to disk.
199256 */
199257 static int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
199258 int rc = SQLITE_OK;
199259 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
199260 if( p->bTotalsValid ){
199261 rc = fts5StorageSaveTotals(p);
199262 if( bCommit ) p->bTotalsValid = 0;
199263 }
199264 if( rc==SQLITE_OK ){
199265 rc = sqlite3Fts5IndexSync(p->pIndex, bCommit);
199266 }
199267 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
199268 return rc;
199269 }
199270
199271 static int sqlite3Fts5StorageRollback(Fts5Storage *p){
199272 p->bTotalsValid = 0;
199273 return sqlite3Fts5IndexRollback(p->pIndex);
199274
+34 -14
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121121
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122122
** [sqlite_version()] and [sqlite_source_id()].
123123
*/
124124
#define SQLITE_VERSION "3.18.0"
125125
#define SQLITE_VERSION_NUMBER 3018000
126
-#define SQLITE_SOURCE_ID "2017-02-23 02:15:33 7a959f6d1ea038988cdb4c02d6f37abaec2580a0"
126
+#define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
127127
128128
/*
129129
** CAPI3REF: Run-Time Library Version Numbers
130130
** KEYWORDS: sqlite3_version sqlite3_sourceid
131131
**
@@ -2038,24 +2038,34 @@
20382038
** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
20392039
** names are not also used by explicitly declared columns. ^If
20402040
** the table has a column of type [INTEGER PRIMARY KEY] then that column
20412041
** is another alias for the rowid.
20422042
**
2043
-** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
2044
-** most recent successful [INSERT] into a rowid table or [virtual table]
2045
-** on database connection D.
2046
-** ^Inserts into [WITHOUT ROWID] tables are not recorded.
2047
-** ^If no successful [INSERT]s into rowid tables
2048
-** have ever occurred on the database connection D,
2049
-** then sqlite3_last_insert_rowid(D) returns zero.
2043
+** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2044
+** the most recent successful [INSERT] into a rowid table or [virtual table]
2045
+** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2046
+** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2047
+** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2048
+** zero.
20502049
**
2051
-** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2052
-** method, then this routine will return the [rowid] of the inserted
2053
-** row as long as the trigger or virtual table method is running.
2054
-** But once the trigger or virtual table method ends, the value returned
2055
-** by this routine reverts to what it was before the trigger or virtual
2056
-** table method began.)^
2050
+** As well as being set automatically as rows are inserted into database
2051
+** tables, the value returned by this function may be set explicitly by
2052
+** [sqlite3_set_last_insert_rowid()]
2053
+**
2054
+** Some virtual table implementations may INSERT rows into rowid tables as
2055
+** part of committing a transaction (e.g. to flush data accumulated in memory
2056
+** to disk). In this case subsequent calls to this function return the rowid
2057
+** associated with these internal INSERT operations, which leads to
2058
+** unintuitive results. Virtual table implementations that do write to rowid
2059
+** tables in this way can avoid this problem by restoring the original
2060
+** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2061
+** control to the user.
2062
+**
2063
+** ^(If an [INSERT] occurs within a trigger then this routine will
2064
+** return the [rowid] of the inserted row as long as the trigger is
2065
+** running. Once the trigger program ends, the value returned
2066
+** by this routine reverts to what it was before the trigger was fired.)^
20572067
**
20582068
** ^An [INSERT] that fails due to a constraint violation is not a
20592069
** successful [INSERT] and does not change the value returned by this
20602070
** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
20612071
** and INSERT OR ABORT make no changes to the return value of this
@@ -2078,10 +2088,20 @@
20782088
** unpredictable and might not equal either the old or the new
20792089
** last insert [rowid].
20802090
*/
20812091
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
20822092
2093
+/*
2094
+** CAPI3REF: Set the Last Insert Rowid value.
2095
+** METHOD: sqlite3
2096
+**
2097
+** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2098
+** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2099
+** without inserting a row into the database.
2100
+*/
2101
+SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2102
+
20832103
/*
20842104
** CAPI3REF: Count The Number Of Rows Modified
20852105
** METHOD: sqlite3
20862106
**
20872107
** ^This function returns the number of rows modified, inserted or
20882108
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.18.0"
125 #define SQLITE_VERSION_NUMBER 3018000
126 #define SQLITE_SOURCE_ID "2017-02-23 02:15:33 7a959f6d1ea038988cdb4c02d6f37abaec2580a0"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
@@ -2038,24 +2038,34 @@
2038 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2039 ** names are not also used by explicitly declared columns. ^If
2040 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
2041 ** is another alias for the rowid.
2042 **
2043 ** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
2044 ** most recent successful [INSERT] into a rowid table or [virtual table]
2045 ** on database connection D.
2046 ** ^Inserts into [WITHOUT ROWID] tables are not recorded.
2047 ** ^If no successful [INSERT]s into rowid tables
2048 ** have ever occurred on the database connection D,
2049 ** then sqlite3_last_insert_rowid(D) returns zero.
2050 **
2051 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
2052 ** method, then this routine will return the [rowid] of the inserted
2053 ** row as long as the trigger or virtual table method is running.
2054 ** But once the trigger or virtual table method ends, the value returned
2055 ** by this routine reverts to what it was before the trigger or virtual
2056 ** table method began.)^
 
 
 
 
 
 
 
 
 
 
 
2057 **
2058 ** ^An [INSERT] that fails due to a constraint violation is not a
2059 ** successful [INSERT] and does not change the value returned by this
2060 ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2061 ** and INSERT OR ABORT make no changes to the return value of this
@@ -2078,10 +2088,20 @@
2078 ** unpredictable and might not equal either the old or the new
2079 ** last insert [rowid].
2080 */
2081 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2082
 
 
 
 
 
 
 
 
 
 
2083 /*
2084 ** CAPI3REF: Count The Number Of Rows Modified
2085 ** METHOD: sqlite3
2086 **
2087 ** ^This function returns the number of rows modified, inserted or
2088
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.18.0"
125 #define SQLITE_VERSION_NUMBER 3018000
126 #define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
@@ -2038,24 +2038,34 @@
2038 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2039 ** names are not also used by explicitly declared columns. ^If
2040 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
2041 ** is another alias for the rowid.
2042 **
2043 ** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2044 ** the most recent successful [INSERT] into a rowid table or [virtual table]
2045 ** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2046 ** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2047 ** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2048 ** zero.
 
2049 **
2050 ** As well as being set automatically as rows are inserted into database
2051 ** tables, the value returned by this function may be set explicitly by
2052 ** [sqlite3_set_last_insert_rowid()]
2053 **
2054 ** Some virtual table implementations may INSERT rows into rowid tables as
2055 ** part of committing a transaction (e.g. to flush data accumulated in memory
2056 ** to disk). In this case subsequent calls to this function return the rowid
2057 ** associated with these internal INSERT operations, which leads to
2058 ** unintuitive results. Virtual table implementations that do write to rowid
2059 ** tables in this way can avoid this problem by restoring the original
2060 ** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2061 ** control to the user.
2062 **
2063 ** ^(If an [INSERT] occurs within a trigger then this routine will
2064 ** return the [rowid] of the inserted row as long as the trigger is
2065 ** running. Once the trigger program ends, the value returned
2066 ** by this routine reverts to what it was before the trigger was fired.)^
2067 **
2068 ** ^An [INSERT] that fails due to a constraint violation is not a
2069 ** successful [INSERT] and does not change the value returned by this
2070 ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2071 ** and INSERT OR ABORT make no changes to the return value of this
@@ -2078,10 +2088,20 @@
2088 ** unpredictable and might not equal either the old or the new
2089 ** last insert [rowid].
2090 */
2091 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2092
2093 /*
2094 ** CAPI3REF: Set the Last Insert Rowid value.
2095 ** METHOD: sqlite3
2096 **
2097 ** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2098 ** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2099 ** without inserting a row into the database.
2100 */
2101 SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2102
2103 /*
2104 ** CAPI3REF: Count The Number Of Rows Modified
2105 ** METHOD: sqlite3
2106 **
2107 ** ^This function returns the number of rows modified, inserted or
2108

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button