Fossil SCM

Update to the latest stable check-in of SQLite, as a beta-test for SQLite.

drh 2010-04-15 23:53 trunk
Commit 7c37b46b12331fda2b89b3d1ba11047174ccad06
2 files changed +301 -84 +1 -3
+301 -84
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -628,11 +628,11 @@
628628
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
629629
** [sqlite_version()] and [sqlite_source_id()].
630630
*/
631631
#define SQLITE_VERSION "3.6.23"
632632
#define SQLITE_VERSION_NUMBER 3006023
633
-#define SQLITE_SOURCE_ID "2010-04-07 20:32:19 e388fe8be878c80ef0bfd1699a7268cdb22cb3c6"
633
+#define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
634634
635635
/*
636636
** CAPI3REF: Run-Time Library Version Numbers
637637
** KEYWORDS: sqlite3_version, sqlite3_sourceid
638638
**
@@ -4404,12 +4404,10 @@
44044404
** ^For the purposes of this API, a transaction is said to have been
44054405
** rolled back if an explicit "ROLLBACK" statement is executed, or
44064406
** an error or constraint causes an implicit rollback to occur.
44074407
** ^The rollback callback is not invoked if a transaction is
44084408
** automatically rolled back because the database connection is closed.
4409
-** ^The rollback callback is not invoked if a transaction is
4410
-** rolled back because a commit callback returned non-zero.
44114409
**
44124410
** See also the [sqlite3_update_hook()] interface.
44134411
*/
44144412
SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
44154413
SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
@@ -32356,10 +32354,91 @@
3235632354
** file simultaneously, or one process from reading the database while
3235732355
** another is writing.
3235832356
*/
3235932357
#ifndef SQLITE_OMIT_DISKIO
3236032358
32359
+/*
32360
+******************** NOTES ON THE DESIGN OF THE PAGER ************************
32361
+**
32362
+** Within this comment block, a page is deemed to have been synced
32363
+** automatically as soon as it is written when PRAGMA synchronous=OFF.
32364
+** Otherwise, the page is not synced until the xSync method of the VFS
32365
+** is called successfully on the file containing the page.
32366
+**
32367
+** Definition: A page of the database file is said to be "overwriteable" if
32368
+** one or more of the following are true about the page:
32369
+**
32370
+** (a) The original content of the page as it was at the beginning of
32371
+** the transaction has been written into the rollback journal and
32372
+** synced.
32373
+**
32374
+** (b) The page was a freelist leaf page at the start of the transaction.
32375
+**
32376
+** (c) The page number is greater than the largest page that existed in
32377
+** the database file at the start of the transaction.
32378
+**
32379
+** (1) A page of the database file is never overwritten unless one of the
32380
+** following are true:
32381
+**
32382
+** (a) The page and all other pages on the same sector are overwriteable.
32383
+**
32384
+** (b) The atomic page write optimization is enabled, and the entire
32385
+** transaction other than the update of the transaction sequence
32386
+** number consists of a single page change.
32387
+**
32388
+** (2) The content of a page written into the rollback journal exactly matches
32389
+** both the content in the database when the rollback journal was written
32390
+** and the content in the database at the beginning of the current
32391
+** transaction.
32392
+**
32393
+** (3) Writes to the database file are an integer multiple of the page size
32394
+** in length and are aligned to a page boundary.
32395
+**
32396
+** (4) Reads from the database file are either aligned on a page boundary and
32397
+** an integer multiple of the page size in length or are taken from the
32398
+** first 100 bytes of the database file.
32399
+**
32400
+** (5) All writes to the database file are synced prior to the rollback journal
32401
+** being deleted, truncated, or zeroed.
32402
+**
32403
+** (6) If a master journal file is used, then all writes to the database file
32404
+** are synced prior to the master journal being deleted.
32405
+**
32406
+** Definition: Two databases (or the same database at two points it time)
32407
+** are said to be "logically equivalent" if they give the same answer to
32408
+** all queries. Note in particular the the content of freelist leaf
32409
+** pages can be changed arbitarily without effecting the logical equivalence
32410
+** of the database.
32411
+**
32412
+** (7) At any time, if any subset, including the empty set and the total set,
32413
+** of the unsynced changes to a rollback journal are removed and the
32414
+** journal is rolled back, the resulting database file will be logical
32415
+** equivalent to the database file at the beginning of the transaction.
32416
+**
32417
+** (8) When a transaction is rolled back, the xTruncate method of the VFS
32418
+** is called to restore the database file to the same size it was at
32419
+** the beginning of the transaction. (In some VFSes, the xTruncate
32420
+** method is a no-op, but that does not change the fact the SQLite will
32421
+** invoke it.)
32422
+**
32423
+** (9) Whenever the database file is modified, at least one bit in the range
32424
+** of bytes from 24 through 39 inclusive will be changed prior to releasing
32425
+** the EXCLUSIVE lock.
32426
+**
32427
+** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
32428
+** than one billion transactions.
32429
+**
32430
+** (11) A database file is well-formed at the beginning and at the conclusion
32431
+** of every transaction.
32432
+**
32433
+** (12) An EXCLUSIVE lock is held on the database file when writing to
32434
+** the database file.
32435
+**
32436
+** (13) A SHARED lock is held on the database file while reading any
32437
+** content out of the database file.
32438
+*/
32439
+
3236132440
/*
3236232441
** Macros for troubleshooting. Normally turned off
3236332442
*/
3236432443
#if 0
3236532444
int sqlite3PagerTrace=1; /* True to enable tracing */
@@ -32532,11 +32611,12 @@
3253232611
** It is used when committing or otherwise ending a transaction. If
3253332612
** the dbModified flag is clear then less work has to be done.
3253432613
**
3253532614
** journalStarted
3253632615
**
32537
-** This flag is set whenever the the main journal is synced.
32616
+** This flag is set whenever the the main journal is opened and
32617
+** initialized
3253832618
**
3253932619
** The point of this flag is that it must be set after the
3254032620
** first journal header in a journal file has been synced to disk.
3254132621
** After this has happened, new pages appended to the database
3254232622
** do not need the PGHDR_NEED_SYNC flag set, as they do not need
@@ -32558,11 +32638,14 @@
3255832638
** master journal name is only written to the journal file the first
3255932639
** time CommitPhaseOne() is called.
3256032640
**
3256132641
** doNotSync
3256232642
**
32563
-** This variable is set and cleared by sqlite3PagerWrite().
32643
+** When enabled, cache spills are prohibited and the journal file cannot
32644
+** be synced. This variable is set and cleared by sqlite3PagerWrite()
32645
+** in order to prevent a journal sync from happening in between the
32646
+** journalling of two pages on the same sector.
3256432647
**
3256532648
** needSync
3256632649
**
3256732650
** TODO: It might be easier to set this variable in writeJournalHdr()
3256832651
** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -32618,10 +32701,11 @@
3261832701
sqlite3_file *fd; /* File descriptor for database */
3261932702
sqlite3_file *jfd; /* File descriptor for main journal */
3262032703
sqlite3_file *sjfd; /* File descriptor for sub-journal */
3262132704
i64 journalOff; /* Current write offset in the journal file */
3262232705
i64 journalHdr; /* Byte offset to previous journal header */
32706
+ i64 journalSizeLimit; /* Size limit for persistent journal files */
3262332707
PagerSavepoint *aSavepoint; /* Array of active savepoints */
3262432708
int nSavepoint; /* Number of elements in aSavepoint[] */
3262532709
char dbFileVers[16]; /* Changes whenever database file changes */
3262632710
u32 sectorSize; /* Assumed sector size during rollback */
3262732711
@@ -32644,11 +32728,10 @@
3264432728
void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
3264532729
void (*xCodecFree)(void*); /* Destructor for the codec */
3264632730
void *pCodec; /* First argument to xCodec... methods */
3264732731
#endif
3264832732
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
32649
- i64 journalSizeLimit; /* Size limit for persistent journal files */
3265032733
PCache *pPCache; /* Pointer to page cache object */
3265132734
sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
3265232735
};
3265332736
3265432737
/*
@@ -33160,10 +33243,11 @@
3316033243
** to populate the entire journal header sector.
3316133244
*/
3316233245
for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
3316333246
IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
3316433247
rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
33248
+ assert( pPager->journalHdr <= pPager->journalOff );
3316533249
pPager->journalOff += nHeader;
3316633250
}
3316733251
3316833252
return rc;
3316933253
}
@@ -33318,10 +33402,11 @@
3331833402
){
3331933403
return SQLITE_OK;
3332033404
}
3332133405
pPager->setMaster = 1;
3332233406
assert( isOpen(pPager->jfd) );
33407
+ assert( pPager->journalHdr <= pPager->journalOff );
3332333408
3332433409
/* Calculate the length in bytes and the checksum of zMaster */
3332533410
for(nMaster=0; zMaster[nMaster]; nMaster++){
3332633411
cksum += zMaster[nMaster];
3332733412
}
@@ -33747,22 +33832,22 @@
3374733832
** allocated by this function. If this is the case and an allocation fails,
3374833833
** SQLITE_NOMEM is returned.
3374933834
*/
3375033835
static int pager_playback_one_page(
3375133836
Pager *pPager, /* The pager being played back */
33752
- int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
33753
- int isUnsync, /* True if reading from unsynced main journal */
3375433837
i64 *pOffset, /* Offset of record to playback */
33755
- int isSavepnt, /* True for a savepoint rollback */
33756
- Bitvec *pDone /* Bitvec of pages already played back */
33838
+ Bitvec *pDone, /* Bitvec of pages already played back */
33839
+ int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
33840
+ int isSavepnt /* True for a savepoint rollback */
3375733841
){
3375833842
int rc;
3375933843
PgHdr *pPg; /* An existing page in the cache */
3376033844
Pgno pgno; /* The page number of a page in journal */
3376133845
u32 cksum; /* Checksum used for sanity checking */
3376233846
char *aData; /* Temporary storage for the page */
3376333847
sqlite3_file *jfd; /* The file descriptor for the journal file */
33848
+ int isSynced; /* True if journal page is synced */
3376433849
3376533850
assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
3376633851
assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
3376733852
assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
3376833853
assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
@@ -33842,16 +33927,21 @@
3384233927
assert( pPg || !MEMDB );
3384333928
PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
3384433929
PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
3384533930
(isMainJrnl?"main-journal":"sub-journal")
3384633931
));
33932
+ if( isMainJrnl ){
33933
+ isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
33934
+ }else{
33935
+ isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
33936
+ }
3384733937
if( (pPager->state>=PAGER_EXCLUSIVE)
33848
- && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
3384933938
&& isOpen(pPager->fd)
33850
- && !isUnsync
33939
+ && isSynced
3385133940
){
3385233941
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
33942
+ testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
3385333943
rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
3385433944
if( pgno>pPager->dbFileSize ){
3385533945
pPager->dbFileSize = pgno;
3385633946
}
3385733947
if( pPager->pBackup ){
@@ -33896,11 +33986,12 @@
3389633986
pPager->xReiniter(pPg);
3389733987
if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
3389833988
/* If the contents of this page were just restored from the main
3389933989
** journal file, then its content must be as they were when the
3390033990
** transaction was first opened. In this case we can mark the page
33901
- ** as clean, since there will be no need to write it out to the.
33991
+ ** as clean, since there will be no need to write it out to the
33992
+ ** database.
3390233993
**
3390333994
** There is one exception to this rule. If the page is being rolled
3390433995
** back as part of a savepoint (or statement) rollback from an
3390533996
** unsynced portion of the main journal file, then it is not safe
3390633997
** to mark the page as clean. This is because marking the page as
@@ -34243,12 +34334,10 @@
3424334334
/* This loop terminates either when a readJournalHdr() or
3424434335
** pager_playback_one_page() call returns SQLITE_DONE or an IO error
3424534336
** occurs.
3424634337
*/
3424734338
while( 1 ){
34248
- int isUnsync = 0;
34249
-
3425034339
/* Read the next journal header from the journal file. If there are
3425134340
** not enough bytes left in the journal file for a complete header, or
3425234341
** it is corrupted, then a process must of failed while writing it.
3425334342
** This indicates nothing more needs to be rolled back.
3425434343
*/
@@ -34285,11 +34374,10 @@
3428534374
** should be computed based on the journal file size.
3428634375
*/
3428734376
if( nRec==0 && !isHot &&
3428834377
pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
3428934378
nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
34290
- isUnsync = 1;
3429134379
}
3429234380
3429334381
/* If this is the first header read from the journal, truncate the
3429434382
** database file back to its original size.
3429534383
*/
@@ -34307,11 +34395,11 @@
3430734395
for(u=0; u<nRec; u++){
3430834396
if( needPagerReset ){
3430934397
pager_reset(pPager);
3431034398
needPagerReset = 0;
3431134399
}
34312
- rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0);
34400
+ rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
3431334401
if( rc!=SQLITE_OK ){
3431434402
if( rc==SQLITE_DONE ){
3431534403
rc = SQLITE_OK;
3431634404
pPager->journalOff = szJ;
3431734405
break;
@@ -34460,11 +34548,11 @@
3446034548
*/
3446134549
if( pSavepoint ){
3446234550
iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
3446334551
pPager->journalOff = pSavepoint->iOffset;
3446434552
while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
34465
- rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
34553
+ rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
3446634554
}
3446734555
assert( rc!=SQLITE_DONE );
3446834556
}else{
3446934557
pPager->journalOff = 0;
3447034558
}
@@ -34490,11 +34578,11 @@
3449034578
&& pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
3449134579
){
3449234580
nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
3449334581
}
3449434582
for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
34495
- rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
34583
+ rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
3449634584
}
3449734585
assert( rc!=SQLITE_DONE );
3449834586
}
3449934587
assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
3450034588
@@ -34505,11 +34593,11 @@
3450534593
if( pSavepoint ){
3450634594
u32 ii; /* Loop counter */
3450734595
i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
3450834596
for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
3450934597
assert( offset==ii*(4+pPager->pageSize) );
34510
- rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone);
34598
+ rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
3451134599
}
3451234600
assert( rc!=SQLITE_DONE );
3451334601
}
3451434602
3451534603
sqlite3BitvecDestroy(pDone);
@@ -34942,10 +35030,35 @@
3494235030
assert( pPager->dbSize>=nPage );
3494335031
assert( pPager->state>=PAGER_RESERVED );
3494435032
pPager->dbSize = nPage;
3494535033
assertTruncateConstraint(pPager);
3494635034
}
35035
+
35036
+/*
35037
+** This function is called before attempting a hot-journal rollback. It
35038
+** syncs the journal file to disk, then sets pPager->journalHdr to the
35039
+** size of the journal file so that the pager_playback() routine knows
35040
+** that the entire journal file has been synced.
35041
+**
35042
+** Syncing a hot-journal to disk before attempting to roll it back ensures
35043
+** that if a power-failure occurs during the rollback, the process that
35044
+** attempts rollback following system recovery sees the same journal
35045
+** content as this process.
35046
+**
35047
+** If everything goes as planned, SQLITE_OK is returned. Otherwise,
35048
+** an SQLite error code.
35049
+*/
35050
+static int pagerSyncHotJournal(Pager *pPager){
35051
+ int rc = SQLITE_OK;
35052
+ if( !pPager->noSync ){
35053
+ rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
35054
+ }
35055
+ if( rc==SQLITE_OK ){
35056
+ rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
35057
+ }
35058
+ return rc;
35059
+}
3494735060
3494835061
/*
3494935062
** Shutdown the page cache. Free all memory and close all files.
3495035063
**
3495135064
** If a transaction was in progress when this routine is called, that
@@ -34972,11 +35085,13 @@
3497235085
** call which may be made from within pagerUnlockAndRollback(). If it
3497335086
** is not -1, then the unsynced portion of an open journal file may
3497435087
** be played back into the database. If a power failure occurs while
3497535088
** this is happening, the database may become corrupt.
3497635089
*/
34977
- pPager->journalHdr = -1;
35090
+ if( isOpen(pPager->jfd) ){
35091
+ pPager->errCode = pagerSyncHotJournal(pPager);
35092
+ }
3497835093
pagerUnlockAndRollback(pPager);
3497935094
}
3498035095
sqlite3EndBenignMalloc();
3498135096
enable_simulated_io_errors();
3498235097
PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
@@ -35062,11 +35177,11 @@
3506235177
/* This block deals with an obscure problem. If the last connection
3506335178
** that wrote to this database was operating in persistent-journal
3506435179
** mode, then the journal file may at this point actually be larger
3506535180
** than Pager.journalOff bytes. If the next thing in the journal
3506635181
** file happens to be a journal-header (written as part of the
35067
- ** previous connections transaction), and a crash or power-failure
35182
+ ** previous connection's transaction), and a crash or power-failure
3506835183
** occurs after nRec is updated but before this connection writes
3506935184
** anything else to the journal file (or commits/rolls back its
3507035185
** transaction), then SQLite may become confused when doing the
3507135186
** hot-journal rollback following recovery. It may roll back all
3507235187
** of this connections data, then proceed to rolling back the old,
@@ -35134,10 +35249,11 @@
3513435249
/* The journal file was just successfully synced. Set Pager.needSync
3513535250
** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
3513635251
*/
3513735252
pPager->needSync = 0;
3513835253
pPager->journalStarted = 1;
35254
+ pPager->journalHdr = pPager->journalOff;
3513935255
sqlite3PcacheClearSyncFlags(pPager->pPCache);
3514035256
}
3514135257
3514235258
return SQLITE_OK;
3514335259
}
@@ -35996,23 +36112,32 @@
3599636112
}
3599736113
if( rc!=SQLITE_OK ){
3599836114
goto failed;
3599936115
}
3600036116
36001
- /* TODO: Why are these cleared here? Is it necessary? */
36117
+ /* Reset the journal status fields to indicates that we have no
36118
+ ** rollback journal at this time. */
3600236119
pPager->journalStarted = 0;
3600336120
pPager->journalOff = 0;
3600436121
pPager->setMaster = 0;
3600536122
pPager->journalHdr = 0;
36123
+
36124
+ /* Make sure the journal file has been synced to disk. */
3600636125
3600736126
/* Playback and delete the journal. Drop the database write
3600836127
** lock and reacquire the read lock. Purge the cache before
3600936128
** playing back the hot-journal so that we don't end up with
36010
- ** an inconsistent cache.
36129
+ ** an inconsistent cache. Sync the hot journal before playing
36130
+ ** it back since the process that crashed and left the hot journal
36131
+ ** probably did not sync it and we are required to always sync
36132
+ ** the journal before playing it back.
3601136133
*/
3601236134
if( isOpen(pPager->jfd) ){
36013
- rc = pager_playback(pPager, 1);
36135
+ rc = pagerSyncHotJournal(pPager);
36136
+ if( rc==SQLITE_OK ){
36137
+ rc = pager_playback(pPager, 1);
36138
+ }
3601436139
if( rc!=SQLITE_OK ){
3601536140
rc = pager_error(pPager, rc);
3601636141
goto failed;
3601736142
}
3601836143
}
@@ -36114,11 +36239,11 @@
3611436239
** of the page. This occurs in two seperate scenarios:
3611536240
**
3611636241
** a) When reading a free-list leaf page from the database, and
3611736242
**
3611836243
** b) When a savepoint is being rolled back and we need to load
36119
-** a new page into the cache to populate with the data read
36244
+** a new page into the cache to be filled with the data read
3612036245
** from the savepoint journal.
3612136246
**
3612236247
** If noContent is true, then the data returned is zeroed instead of
3612336248
** being read from the database. Additionally, the bits corresponding
3612436249
** to pgno in Pager.pInJournal (bitvec of pages already written to the
@@ -36546,10 +36671,12 @@
3654636671
3654736672
/* We should never write to the journal file the page that
3654836673
** contains the database locks. The following assert verifies
3654936674
** that we do not. */
3655036675
assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
36676
+
36677
+ assert( pPager->journalHdr <= pPager->journalOff );
3655136678
CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
3655236679
cksum = pager_cksum(pPager, (u8*)pData2);
3655336680
rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
3655436681
if( rc==SQLITE_OK ){
3655536682
rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
@@ -60151,15 +60278,14 @@
6015160278
** Syncing an in-memory journal is a no-op. And, in fact, this routine
6015260279
** is never called in a working implementation. This implementation
6015360280
** exists purely as a contingency, in case some malfunction in some other
6015460281
** part of SQLite causes Sync to be called by mistake.
6015560282
*/
60156
-static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ /*NO_TEST*/
60157
- UNUSED_PARAMETER2(NotUsed, NotUsed2); /*NO_TEST*/
60158
- assert( 0 ); /*NO_TEST*/
60159
- return SQLITE_OK; /*NO_TEST*/
60160
-} /*NO_TEST*/
60283
+static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
60284
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
60285
+ return SQLITE_OK;
60286
+}
6016160287
6016260288
/*
6016360289
** Query the size of the file in bytes.
6016460290
*/
6016560291
static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
@@ -60751,11 +60877,11 @@
6075160877
}
6075260878
}
6075360879
6075460880
/*
6075560881
** Allocate and return a pointer to an expression to load the column iCol
60756
-** from datasource iSrc datasource in SrcList pSrc.
60882
+** from datasource iSrc in SrcList pSrc.
6075760883
*/
6075860884
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
6075960885
Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
6076060886
if( p ){
6076160887
struct SrcList_item *pItem = &pSrc->a[iSrc];
@@ -60763,10 +60889,12 @@
6076360889
p->iTable = pItem->iCursor;
6076460890
if( p->pTab->iPKey==iCol ){
6076560891
p->iColumn = -1;
6076660892
}else{
6076760893
p->iColumn = (ynVar)iCol;
60894
+ testcase( iCol==BMS );
60895
+ testcase( iCol==BMS-1 );
6076860896
pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
6076960897
}
6077060898
ExprSetProperty(p, EP_Resolved);
6077160899
}
6077260900
return p;
@@ -79646,32 +79774,32 @@
7964679774
/* Call the parser to process a CREATE TABLE, INDEX or VIEW.
7964779775
** But because db->init.busy is set to 1, no VDBE code is generated
7964879776
** or executed. All the parser does is build the internal data
7964979777
** structures that describe the table, index, or view.
7965079778
*/
79651
- char *zErr;
7965279779
int rc;
79780
+ sqlite3_stmt *pStmt;
79781
+
7965379782
assert( db->init.busy );
7965479783
db->init.iDb = iDb;
7965579784
db->init.newTnum = atoi(argv[1]);
7965679785
db->init.orphanTrigger = 0;
79657
- rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
79786
+ rc = sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
7965879787
db->init.iDb = 0;
79659
- assert( rc!=SQLITE_OK || zErr==0 );
7966079788
if( SQLITE_OK!=rc ){
7966179789
if( db->init.orphanTrigger ){
7966279790
assert( iDb==1 );
7966379791
}else{
7966479792
pData->rc = rc;
7966579793
if( rc==SQLITE_NOMEM ){
7966679794
db->mallocFailed = 1;
7966779795
}else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
79668
- corruptSchema(pData, argv[0], zErr);
79796
+ corruptSchema(pData, argv[0], sqlite3_errmsg(db));
7966979797
}
7967079798
}
79671
- sqlite3DbFree(db, zErr);
7967279799
}
79800
+ sqlite3_finalize(pStmt);
7967379801
}else if( argv[0]==0 ){
7967479802
corruptSchema(pData, 0, 0);
7967579803
}else{
7967679804
/* If the SQL column is blank it means this is an index that
7967779805
** was created to be the PRIMARY KEY or to fulfill a UNIQUE
@@ -82959,12 +83087,12 @@
8295983087
** (13) The subquery and outer query do not both use LIMIT
8296083088
**
8296183089
** (14) The subquery does not use OFFSET
8296283090
**
8296383091
** (15) The outer query is not part of a compound select or the
82964
-** subquery does not have both an ORDER BY and a LIMIT clause.
82965
-** (See ticket #2339)
83092
+** subquery does not have a LIMIT clause.
83093
+** (See ticket #2339 and ticket [02a8e81d44]).
8296683094
**
8296783095
** (16) The outer query is not an aggregate or the subquery does
8296883096
** not contain ORDER BY. (Ticket #2942) This used to not matter
8296983097
** until we introduced the group_concat() function.
8297083098
**
@@ -83043,11 +83171,11 @@
8304383171
** because they could be computed at compile-time. But when LIMIT and OFFSET
8304483172
** became arbitrary expressions, we were forced to add restrictions (13)
8304583173
** and (14). */
8304683174
if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
8304783175
if( pSub->pOffset ) return 0; /* Restriction (14) */
83048
- if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
83176
+ if( p->pRightmost && pSub->pLimit ){
8304983177
return 0; /* Restriction (15) */
8305083178
}
8305183179
if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
8305283180
if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit)
8305383181
&& (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
@@ -89537,10 +89665,15 @@
8953789665
#ifndef SQLITE_OMIT_OR_OPTIMIZATION
8953889666
const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
8953989667
const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
8954089668
WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
8954189669
WhereTerm *pTerm; /* A single term of the WHERE clause */
89670
+
89671
+ /* No OR-clause optimization allowed if the NOT INDEXED clause is used */
89672
+ if( pSrc->notIndexed ){
89673
+ return;
89674
+ }
8954289675
8954389676
/* Search the WHERE clause terms for a usable WO_OR term. */
8954489677
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
8954589678
if( pTerm->eOperator==WO_OR
8954689679
&& ((pTerm->prereqAll & ~maskSrc) & notReady)==0
@@ -89580,12 +89713,13 @@
8958089713
}
8958189714
8958289715
/* If there is an ORDER BY clause, increase the scan cost to account
8958389716
** for the cost of the sort. */
8958489717
if( pOrderBy!=0 ){
89718
+ WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
89719
+ rTotal, rTotal+nRow*estLog(nRow)));
8958589720
rTotal += nRow*estLog(nRow);
89586
- WHERETRACE(("... sorting increases OR cost to %.9g\n", rTotal));
8958789721
}
8958889722
8958989723
/* If the cost of scanning using this OR term for optimization is
8959089724
** less than the current cost stored in pCost, replace the contents
8959189725
** of pCost. */
@@ -89658,21 +89792,21 @@
8965889792
/* The NOT INDEXED clause appears in the SQL. */
8965989793
return;
8966089794
}
8966189795
8966289796
assert( pParse->nQueryLoop >= (double)1 );
89663
- nTableRow = pSrc->pIndex ? pSrc->pIndex->aiRowEst[0] : 1000000;
89797
+ pTable = pSrc->pTab;
89798
+ nTableRow = pTable->pIndex ? pTable->pIndex->aiRowEst[0] : 1000000;
8966489799
logN = estLog(nTableRow);
8966589800
costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
8966689801
if( costTempIdx>=pCost->rCost ){
8966789802
/* The cost of creating the transient table would be greater than
8966889803
** doing the full table scan */
8966989804
return;
8967089805
}
8967189806
8967289807
/* Search for any equality comparison term */
89673
- pTable = pSrc->pTab;
8967489808
pWCEnd = &pWC->a[pWC->nTerm];
8967589809
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
8967689810
if( termCanDriveIndex(pTerm, pSrc, notReady) ){
8967789811
WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
8967889812
pCost->rCost, costTempIdx));
@@ -89736,12 +89870,17 @@
8973689870
pWCEnd = &pWC->a[pWC->nTerm];
8973789871
idxCols = 0;
8973889872
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
8973989873
if( termCanDriveIndex(pTerm, pSrc, notReady) ){
8974089874
int iCol = pTerm->u.leftColumn;
89741
- if( iCol<BMS && iCol>=0 ) idxCols |= 1<<iCol;
89742
- nColumn++;
89875
+ Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
89876
+ testcase( iCol==BMS );
89877
+ testcase( iCol==BMS-1 );
89878
+ if( (idxCols & cMask)==0 ){
89879
+ nColumn++;
89880
+ idxCols |= cMask;
89881
+ }
8974389882
}
8974489883
}
8974589884
assert( nColumn>0 );
8974689885
pLevel->plan.nEq = nColumn;
8974789886
@@ -89751,14 +89890,16 @@
8975189890
** original table never needs to be accessed. Automatic indices must
8975289891
** be a covering index because the index will not be updated if the
8975389892
** original table changes and the index and table cannot both be used
8975489893
** if they go out of sync.
8975589894
*/
89756
- extraCols = pSrc->colUsed & ~idxCols;
89895
+ extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
8975789896
mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
89897
+ testcase( pTable->nCol==BMS-1 );
89898
+ testcase( pTable->nCol==BMS-2 );
8975889899
for(i=0; i<mxBitCol; i++){
89759
- if( extraCols & (1<<i) ) nColumn++;
89900
+ if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
8976089901
}
8976189902
if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
8976289903
nColumn += pTable->nCol - BMS + 1;
8976389904
}
8976489905
pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
@@ -89776,25 +89917,31 @@
8977689917
pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
8977789918
pIdx->zName = "auto-index";
8977889919
pIdx->nColumn = nColumn;
8977989920
pIdx->pTable = pTable;
8978089921
n = 0;
89922
+ idxCols = 0;
8978189923
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
8978289924
if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89783
- Expr *pX = pTerm->pExpr;
89784
- pIdx->aiColumn[n] = pTerm->u.leftColumn;
89785
- pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
89786
- pIdx->azColl[n] = pColl->zName;
89787
- n++;
89925
+ int iCol = pTerm->u.leftColumn;
89926
+ Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
89927
+ if( (idxCols & cMask)==0 ){
89928
+ Expr *pX = pTerm->pExpr;
89929
+ idxCols |= cMask;
89930
+ pIdx->aiColumn[n] = pTerm->u.leftColumn;
89931
+ pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
89932
+ pIdx->azColl[n] = pColl->zName;
89933
+ n++;
89934
+ }
8978889935
}
8978989936
}
8979089937
assert( n==pLevel->plan.nEq );
8979189938
8979289939
/* Add additional columns needed to make the automatic index into
8979389940
** a covering index */
8979489941
for(i=0; i<mxBitCol; i++){
89795
- if( extraCols & (1<<i) ){
89942
+ if( extraCols & (((Bitmask)1)<<i) ){
8979689943
pIdx->aiColumn[n] = i;
8979789944
pIdx->azColl[n] = "BINARY";
8979889945
n++;
8979989946
}
8980089947
}
@@ -90519,18 +90666,18 @@
9051990666
**
9052090667
** bInEst:
9052190668
** Set to true if there was at least one "x IN (SELECT ...)" term used
9052290669
** in determining the value of nInMul.
9052390670
**
90524
- ** nBound:
90671
+ ** estBound:
9052590672
** An estimate on the amount of the table that must be searched. A
9052690673
** value of 100 means the entire table is searched. Range constraints
9052790674
** might reduce this to a value less than 100 to indicate that only
9052890675
** a fraction of the table needs searching. In the absence of
9052990676
** sqlite_stat2 ANALYZE data, a single inequality reduces the search
9053090677
** space to 1/3rd its original size. So an x>? constraint reduces
90531
- ** nBound to 33. Two constraints (x>? AND x<?) reduce nBound to 11.
90678
+ ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11.
9053290679
**
9053390680
** bSort:
9053490681
** Boolean. True if there is an ORDER BY clause that will require an
9053590682
** external sort (i.e. scanning the index being evaluated will not
9053690683
** correctly order records).
@@ -90548,17 +90695,18 @@
9054890695
** SELECT a, b, c FROM tbl WHERE a = 1;
9054990696
*/
9055090697
int nEq;
9055190698
int bInEst = 0;
9055290699
int nInMul = 1;
90553
- int nBound = 100;
90700
+ int estBound = 100;
90701
+ int nBound = 0; /* Number of range constraints seen */
9055490702
int bSort = 0;
9055590703
int bLookup = 0;
90704
+ WhereTerm *pTerm; /* A single term of the WHERE clause */
9055690705
9055790706
/* Determine the values of nEq and nInMul */
9055890707
for(nEq=0; nEq<pProbe->nColumn; nEq++){
90559
- WhereTerm *pTerm; /* A single term of the WHERE clause */
9056090708
int j = pProbe->aiColumn[nEq];
9056190709
pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
9056290710
if( pTerm==0 ) break;
9056390711
wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
9056490712
if( pTerm->eOperator & WO_IN ){
@@ -90574,22 +90722,24 @@
9057490722
wsFlags |= WHERE_COLUMN_NULL;
9057590723
}
9057690724
used |= pTerm->prereqRight;
9057790725
}
9057890726
90579
- /* Determine the value of nBound. */
90727
+ /* Determine the value of estBound. */
9058090728
if( nEq<pProbe->nColumn ){
9058190729
int j = pProbe->aiColumn[nEq];
9058290730
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
9058390731
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
9058490732
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
90585
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &nBound);
90733
+ whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
9058690734
if( pTop ){
90735
+ nBound = 1;
9058790736
wsFlags |= WHERE_TOP_LIMIT;
9058890737
used |= pTop->prereqRight;
9058990738
}
9059090739
if( pBtm ){
90740
+ nBound++;
9059190741
wsFlags |= WHERE_BTM_LIMIT;
9059290742
used |= pBtm->prereqRight;
9059390743
}
9059490744
wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
9059590745
}
@@ -90635,12 +90785,11 @@
9063590785
}else{
9063690786
bLookup = 1;
9063790787
}
9063890788
}
9063990789
90640
- /**** Begin adding up the cost of using this index (Needs improvements)
90641
- **
90790
+ /*
9064290791
** Estimate the number of rows of output. For an IN operator,
9064390792
** do not let the estimate exceed half the rows in the table.
9064490793
*/
9064590794
nRow = (double)(aiRowEst[nEq] * nInMul);
9064690795
if( bInEst && nRow*2>aiRowEst[0] ){
@@ -90655,12 +90804,12 @@
9065590804
cost = nRow + nInMul*estLog(aiRowEst[0]);
9065690805
9065790806
/* Adjust the number of rows and the cost downward to reflect rows
9065890807
** that are excluded by range constraints.
9065990808
*/
90660
- nRow = (nRow * (double)nBound) / (double)100;
90661
- cost = (cost * (double)nBound) / (double)100;
90809
+ nRow = (nRow * (double)estBound) / (double)100;
90810
+ cost = (cost * (double)estBound) / (double)100;
9066290811
9066390812
/* Add in the estimated cost of sorting the result
9066490813
*/
9066590814
if( bSort ){
9066690815
cost += cost*estLog(cost);
@@ -90672,22 +90821,80 @@
9067290821
*/
9067390822
if( pIdx && bLookup==0 ){
9067490823
cost /= (double)2;
9067590824
}
9067690825
/**** Cost of using this index has now been computed ****/
90826
+
90827
+ /* If there are additional constraints on this table that cannot
90828
+ ** be used with the current index, but which might lower the number
90829
+ ** of output rows, adjust the nRow value accordingly. This only
90830
+ ** matters if the current index is the least costly, so do not bother
90831
+ ** with this step if we already know this index will not be chosen.
90832
+ ** Also, never reduce the output row count below 2 using this step.
90833
+ **
90834
+ ** Do not reduce the output row count if pSrc is the only table that
90835
+ ** is notReady; if notReady is a power of two. This will be the case
90836
+ ** when the main sqlite3WhereBegin() loop is scanning for a table with
90837
+ ** and "optimal" index, and on such a scan the output row count
90838
+ ** reduction is not valid because it does not update the "pCost->used"
90839
+ ** bitmap. The notReady bitmap will also be a power of two when we
90840
+ ** are scanning for the last table in a 64-way join. We are willing
90841
+ ** to bypass this optimization in that corner case.
90842
+ */
90843
+ if( nRow>2 && cost<=pCost->rCost && (notReady & (notReady-1))!=0 ){
90844
+ int k; /* Loop counter */
90845
+ int nSkipEq = nEq; /* Number of == constraints to skip */
90846
+ int nSkipRange = nBound; /* Number of < constraints to skip */
90847
+ Bitmask thisTab; /* Bitmap for pSrc */
90848
+
90849
+ thisTab = getMask(pWC->pMaskSet, iCur);
90850
+ for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
90851
+ if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
90852
+ if( (pTerm->prereqAll & notReady)!=thisTab ) continue;
90853
+ if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
90854
+ if( nSkipEq ){
90855
+ /* Ignore the first nEq equality matches since the index
90856
+ ** has already accounted for these */
90857
+ nSkipEq--;
90858
+ }else{
90859
+ /* Assume each additional equality match reduces the result
90860
+ ** set size by a factor of 10 */
90861
+ nRow /= 10;
90862
+ }
90863
+ }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
90864
+ if( nSkipRange ){
90865
+ /* Ignore the first nBound range constraints since the index
90866
+ ** has already accounted for these */
90867
+ nSkipRange--;
90868
+ }else{
90869
+ /* Assume each additional range constraint reduces the result
90870
+ ** set size by a factor of 3 */
90871
+ nRow /= 3;
90872
+ }
90873
+ }else{
90874
+ /* Any other expression lowers the output row count by half */
90875
+ nRow /= 2;
90876
+ }
90877
+ }
90878
+ if( nRow<2 ) nRow = 2;
90879
+ }
90880
+
9067790881
9067890882
WHERETRACE((
90679
- "%s(%s): nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
90883
+ "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
9068090884
" notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
9068190885
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
90682
- nEq, nInMul, nBound, bSort, bLookup, wsFlags, notReady, nRow, cost, used
90886
+ nEq, nInMul, estBound, bSort, bLookup, wsFlags,
90887
+ notReady, nRow, cost, used
9068390888
));
9068490889
9068590890
/* If this index is the best we have seen so far, then record this
9068690891
** index and its cost in the pCost structure.
9068790892
*/
90688
- if( (!pIdx || wsFlags) && cost<pCost->rCost ){
90893
+ if( (!pIdx || wsFlags)
90894
+ && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->nRow))
90895
+ ){
9068990896
pCost->rCost = cost;
9069090897
pCost->nRow = nRow;
9069190898
pCost->used = used;
9069290899
pCost->plan.wsFlags = (wsFlags&wsFlagMask);
9069390900
pCost->plan.nEq = nEq;
@@ -90718,11 +90925,12 @@
9071890925
|| pCost->plan.u.pIdx==0
9071990926
|| pCost->plan.u.pIdx==pSrc->pIndex
9072090927
);
9072190928
9072290929
WHERETRACE(("best index is: %s\n",
90723
- (pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
90930
+ ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
90931
+ pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
9072490932
));
9072590933
9072690934
bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
9072790935
bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
9072890936
pCost->plan.wsFlags |= eqTermMask;
@@ -91654,11 +91862,11 @@
9165491862
9165591863
/*
9165691864
** Free a WhereInfo structure
9165791865
*/
9165891866
static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
91659
- if( pWInfo ){
91867
+ if( ALWAYS(pWInfo) ){
9166091868
int i;
9166191869
for(i=0; i<pWInfo->nLevel; i++){
9166291870
sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
9166391871
if( pInfo ){
9166491872
/* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
@@ -91791,10 +91999,11 @@
9179191999
sqlite3 *db; /* Database connection */
9179292000
9179392001
/* The number of tables in the FROM clause is limited by the number of
9179492002
** bits in a Bitmask
9179592003
*/
92004
+ testcase( pTabList->nSrc==BMS );
9179692005
if( pTabList->nSrc>BMS ){
9179792006
sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
9179892007
return 0;
9179992008
}
9180092009
@@ -91930,24 +92139,29 @@
9193092139
9193192140
memset(&bestPlan, 0, sizeof(bestPlan));
9193292141
bestPlan.rCost = SQLITE_BIG_DBL;
9193392142
9193492143
/* Loop through the remaining entries in the FROM clause to find the
91935
- ** next nested loop. The FROM clause entries may be iterated through
92144
+ ** next nested loop. The loop tests all FROM clause entries
9193692145
** either once or twice.
9193792146
**
91938
- ** The first iteration, which is always performed, searches for the
91939
- ** FROM clause entry that permits the lowest-cost, "optimal" scan. In
92147
+ ** The first test is always performed if there are two or more entries
92148
+ ** remaining and never performed if there is only one FROM clause entry
92149
+ ** to choose from. The first test looks for an "optimal" scan. In
9194092150
** this context an optimal scan is one that uses the same strategy
9194192151
** for the given FROM clause entry as would be selected if the entry
9194292152
** were used as the innermost nested loop. In other words, a table
9194392153
** is chosen such that the cost of running that table cannot be reduced
91944
- ** by waiting for other tables to run first.
92154
+ ** by waiting for other tables to run first. This "optimal" test works
92155
+ ** by first assuming that the FROM clause is on the inner loop and finding
92156
+ ** its query plan, then checking to see if that query plan uses any
92157
+ ** other FROM clause terms that are notReady. If no notReady terms are
92158
+ ** used then the "optimal" query plan works.
9194592159
**
91946
- ** The second iteration is only performed if no optimal scan strategies
91947
- ** were found by the first. This iteration is used to search for the
91948
- ** lowest cost scan overall.
92160
+ ** The second loop iteration is only performed if no optimal scan
92161
+ ** strategies were found by the first loop. This 2nd iteration is used to
92162
+ ** search for the lowest cost scan overall.
9194992163
**
9195092164
** Previous versions of SQLite performed only the second iteration -
9195192165
** the next outermost loop was always that with the lowest overall
9195292166
** cost. However, this meant that SQLite could select the wrong plan
9195392167
** for scripts such as the following:
@@ -91961,13 +92175,12 @@
9196192175
** However, since the cost of a linear scan through table t2 is the same
9196292176
** as the cost of a linear scan through table t1, a simple greedy
9196392177
** algorithm may choose to use t2 for the outer loop, which is a much
9196492178
** costlier approach.
9196592179
*/
91966
- for(isOptimal=1; isOptimal>=0 && bestJ<0; isOptimal--){
91967
- Bitmask mask = (isOptimal ? 0 : notReady);
91968
- assert( (nTabList-iFrom)>1 || isOptimal );
92180
+ for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){
92181
+ Bitmask mask; /* Mask of tables not yet ready */
9196992182
for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
9197092183
int doNotReorder; /* True if this table should not be reordered */
9197192184
WhereCost sCost; /* Cost information from best[Virtual]Index() */
9197292185
ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
9197392186
@@ -91976,10 +92189,11 @@
9197692189
m = getMask(pMaskSet, pTabItem->iCursor);
9197792190
if( (m & notReady)==0 ){
9197892191
if( j==iFrom ) iFrom++;
9197992192
continue;
9198092193
}
92194
+ mask = (isOptimal ? m : notReady);
9198192195
pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
9198292196
9198392197
assert( pTabItem->pTab );
9198492198
#ifndef SQLITE_OMIT_VIRTUALTABLE
9198592199
if( IsVirtual(pTabItem->pTab) ){
@@ -91991,12 +92205,15 @@
9199192205
bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost);
9199292206
}
9199392207
assert( isOptimal || (sCost.used&notReady)==0 );
9199492208
9199592209
if( (sCost.used&notReady)==0
91996
- && (j==iFrom || sCost.rCost<bestPlan.rCost)
92210
+ && (bestJ<0 || sCost.rCost<bestPlan.rCost
92211
+ || (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow))
9199792212
){
92213
+ WHERETRACE(("... best so far with cost=%g and nRow=%g\n",
92214
+ sCost.rCost, sCost.nRow));
9199892215
bestPlan = sCost;
9199992216
bestJ = j;
9200092217
}
9200192218
if( doNotReorder ) break;
9200292219
}
@@ -92118,10 +92335,12 @@
9211892335
#endif
9211992336
if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
9212092337
&& (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
9212192338
int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
9212292339
sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
92340
+ testcase( pTab->nCol==BMS-1 );
92341
+ testcase( pTab->nCol==BMS );
9212392342
if( !pWInfo->okOnePass && pTab->nCol<BMS ){
9212492343
Bitmask b = pTabItem->colUsed;
9212592344
int n = 0;
9212692345
for(; b; b=b>>1, n++){}
9212792346
sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
@@ -92297,11 +92516,11 @@
9229792516
){
9229892517
int ws = pLevel->plan.wsFlags;
9229992518
if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
9230092519
sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
9230192520
}
92302
- if( (ws & (WHERE_INDEXED|WHERE_TEMP_INDEX)) == WHERE_INDEXED ){
92521
+ if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
9230392522
sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
9230492523
}
9230592524
}
9230692525
9230792526
/* If this scan uses an index, make code substitutions to read data
@@ -92345,14 +92564,12 @@
9234592564
}
9234692565
}
9234792566
9234892567
/* Final cleanup
9234992568
*/
92350
- if( pWInfo ){
92351
- pParse->nQueryLoop = pWInfo->savedNQueryLoop;
92352
- whereInfoFree(db, pWInfo);
92353
- }
92569
+ pParse->nQueryLoop = pWInfo->savedNQueryLoop;
92570
+ whereInfoFree(db, pWInfo);
9235492571
return;
9235592572
}
9235692573
9235792574
/************** End of where.c ***********************************************/
9235892575
/************** Begin file parse.c *******************************************/
9235992576
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -628,11 +628,11 @@
628 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
629 ** [sqlite_version()] and [sqlite_source_id()].
630 */
631 #define SQLITE_VERSION "3.6.23"
632 #define SQLITE_VERSION_NUMBER 3006023
633 #define SQLITE_SOURCE_ID "2010-04-07 20:32:19 e388fe8be878c80ef0bfd1699a7268cdb22cb3c6"
634
635 /*
636 ** CAPI3REF: Run-Time Library Version Numbers
637 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
638 **
@@ -4404,12 +4404,10 @@
4404 ** ^For the purposes of this API, a transaction is said to have been
4405 ** rolled back if an explicit "ROLLBACK" statement is executed, or
4406 ** an error or constraint causes an implicit rollback to occur.
4407 ** ^The rollback callback is not invoked if a transaction is
4408 ** automatically rolled back because the database connection is closed.
4409 ** ^The rollback callback is not invoked if a transaction is
4410 ** rolled back because a commit callback returned non-zero.
4411 **
4412 ** See also the [sqlite3_update_hook()] interface.
4413 */
4414 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4415 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
@@ -32356,10 +32354,91 @@
32356 ** file simultaneously, or one process from reading the database while
32357 ** another is writing.
32358 */
32359 #ifndef SQLITE_OMIT_DISKIO
32360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32361 /*
32362 ** Macros for troubleshooting. Normally turned off
32363 */
32364 #if 0
32365 int sqlite3PagerTrace=1; /* True to enable tracing */
@@ -32532,11 +32611,12 @@
32532 ** It is used when committing or otherwise ending a transaction. If
32533 ** the dbModified flag is clear then less work has to be done.
32534 **
32535 ** journalStarted
32536 **
32537 ** This flag is set whenever the the main journal is synced.
 
32538 **
32539 ** The point of this flag is that it must be set after the
32540 ** first journal header in a journal file has been synced to disk.
32541 ** After this has happened, new pages appended to the database
32542 ** do not need the PGHDR_NEED_SYNC flag set, as they do not need
@@ -32558,11 +32638,14 @@
32558 ** master journal name is only written to the journal file the first
32559 ** time CommitPhaseOne() is called.
32560 **
32561 ** doNotSync
32562 **
32563 ** This variable is set and cleared by sqlite3PagerWrite().
 
 
 
32564 **
32565 ** needSync
32566 **
32567 ** TODO: It might be easier to set this variable in writeJournalHdr()
32568 ** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -32618,10 +32701,11 @@
32618 sqlite3_file *fd; /* File descriptor for database */
32619 sqlite3_file *jfd; /* File descriptor for main journal */
32620 sqlite3_file *sjfd; /* File descriptor for sub-journal */
32621 i64 journalOff; /* Current write offset in the journal file */
32622 i64 journalHdr; /* Byte offset to previous journal header */
 
32623 PagerSavepoint *aSavepoint; /* Array of active savepoints */
32624 int nSavepoint; /* Number of elements in aSavepoint[] */
32625 char dbFileVers[16]; /* Changes whenever database file changes */
32626 u32 sectorSize; /* Assumed sector size during rollback */
32627
@@ -32644,11 +32728,10 @@
32644 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
32645 void (*xCodecFree)(void*); /* Destructor for the codec */
32646 void *pCodec; /* First argument to xCodec... methods */
32647 #endif
32648 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
32649 i64 journalSizeLimit; /* Size limit for persistent journal files */
32650 PCache *pPCache; /* Pointer to page cache object */
32651 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
32652 };
32653
32654 /*
@@ -33160,10 +33243,11 @@
33160 ** to populate the entire journal header sector.
33161 */
33162 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
33163 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
33164 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
 
33165 pPager->journalOff += nHeader;
33166 }
33167
33168 return rc;
33169 }
@@ -33318,10 +33402,11 @@
33318 ){
33319 return SQLITE_OK;
33320 }
33321 pPager->setMaster = 1;
33322 assert( isOpen(pPager->jfd) );
 
33323
33324 /* Calculate the length in bytes and the checksum of zMaster */
33325 for(nMaster=0; zMaster[nMaster]; nMaster++){
33326 cksum += zMaster[nMaster];
33327 }
@@ -33747,22 +33832,22 @@
33747 ** allocated by this function. If this is the case and an allocation fails,
33748 ** SQLITE_NOMEM is returned.
33749 */
33750 static int pager_playback_one_page(
33751 Pager *pPager, /* The pager being played back */
33752 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
33753 int isUnsync, /* True if reading from unsynced main journal */
33754 i64 *pOffset, /* Offset of record to playback */
33755 int isSavepnt, /* True for a savepoint rollback */
33756 Bitvec *pDone /* Bitvec of pages already played back */
 
33757 ){
33758 int rc;
33759 PgHdr *pPg; /* An existing page in the cache */
33760 Pgno pgno; /* The page number of a page in journal */
33761 u32 cksum; /* Checksum used for sanity checking */
33762 char *aData; /* Temporary storage for the page */
33763 sqlite3_file *jfd; /* The file descriptor for the journal file */
 
33764
33765 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
33766 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
33767 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
33768 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
@@ -33842,16 +33927,21 @@
33842 assert( pPg || !MEMDB );
33843 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
33844 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
33845 (isMainJrnl?"main-journal":"sub-journal")
33846 ));
 
 
 
 
 
33847 if( (pPager->state>=PAGER_EXCLUSIVE)
33848 && (pPg==0 || 0==(pPg->flags&PGHDR_NEED_SYNC))
33849 && isOpen(pPager->fd)
33850 && !isUnsync
33851 ){
33852 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
 
33853 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
33854 if( pgno>pPager->dbFileSize ){
33855 pPager->dbFileSize = pgno;
33856 }
33857 if( pPager->pBackup ){
@@ -33896,11 +33986,12 @@
33896 pPager->xReiniter(pPg);
33897 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
33898 /* If the contents of this page were just restored from the main
33899 ** journal file, then its content must be as they were when the
33900 ** transaction was first opened. In this case we can mark the page
33901 ** as clean, since there will be no need to write it out to the.
 
33902 **
33903 ** There is one exception to this rule. If the page is being rolled
33904 ** back as part of a savepoint (or statement) rollback from an
33905 ** unsynced portion of the main journal file, then it is not safe
33906 ** to mark the page as clean. This is because marking the page as
@@ -34243,12 +34334,10 @@
34243 /* This loop terminates either when a readJournalHdr() or
34244 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
34245 ** occurs.
34246 */
34247 while( 1 ){
34248 int isUnsync = 0;
34249
34250 /* Read the next journal header from the journal file. If there are
34251 ** not enough bytes left in the journal file for a complete header, or
34252 ** it is corrupted, then a process must of failed while writing it.
34253 ** This indicates nothing more needs to be rolled back.
34254 */
@@ -34285,11 +34374,10 @@
34285 ** should be computed based on the journal file size.
34286 */
34287 if( nRec==0 && !isHot &&
34288 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
34289 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
34290 isUnsync = 1;
34291 }
34292
34293 /* If this is the first header read from the journal, truncate the
34294 ** database file back to its original size.
34295 */
@@ -34307,11 +34395,11 @@
34307 for(u=0; u<nRec; u++){
34308 if( needPagerReset ){
34309 pager_reset(pPager);
34310 needPagerReset = 0;
34311 }
34312 rc = pager_playback_one_page(pPager,1,isUnsync,&pPager->journalOff,0,0);
34313 if( rc!=SQLITE_OK ){
34314 if( rc==SQLITE_DONE ){
34315 rc = SQLITE_OK;
34316 pPager->journalOff = szJ;
34317 break;
@@ -34460,11 +34548,11 @@
34460 */
34461 if( pSavepoint ){
34462 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
34463 pPager->journalOff = pSavepoint->iOffset;
34464 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
34465 rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
34466 }
34467 assert( rc!=SQLITE_DONE );
34468 }else{
34469 pPager->journalOff = 0;
34470 }
@@ -34490,11 +34578,11 @@
34490 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
34491 ){
34492 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
34493 }
34494 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
34495 rc = pager_playback_one_page(pPager, 1, 0, &pPager->journalOff, 1, pDone);
34496 }
34497 assert( rc!=SQLITE_DONE );
34498 }
34499 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
34500
@@ -34505,11 +34593,11 @@
34505 if( pSavepoint ){
34506 u32 ii; /* Loop counter */
34507 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
34508 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
34509 assert( offset==ii*(4+pPager->pageSize) );
34510 rc = pager_playback_one_page(pPager, 0, 0, &offset, 1, pDone);
34511 }
34512 assert( rc!=SQLITE_DONE );
34513 }
34514
34515 sqlite3BitvecDestroy(pDone);
@@ -34942,10 +35030,35 @@
34942 assert( pPager->dbSize>=nPage );
34943 assert( pPager->state>=PAGER_RESERVED );
34944 pPager->dbSize = nPage;
34945 assertTruncateConstraint(pPager);
34946 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34947
34948 /*
34949 ** Shutdown the page cache. Free all memory and close all files.
34950 **
34951 ** If a transaction was in progress when this routine is called, that
@@ -34972,11 +35085,13 @@
34972 ** call which may be made from within pagerUnlockAndRollback(). If it
34973 ** is not -1, then the unsynced portion of an open journal file may
34974 ** be played back into the database. If a power failure occurs while
34975 ** this is happening, the database may become corrupt.
34976 */
34977 pPager->journalHdr = -1;
 
 
34978 pagerUnlockAndRollback(pPager);
34979 }
34980 sqlite3EndBenignMalloc();
34981 enable_simulated_io_errors();
34982 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
@@ -35062,11 +35177,11 @@
35062 /* This block deals with an obscure problem. If the last connection
35063 ** that wrote to this database was operating in persistent-journal
35064 ** mode, then the journal file may at this point actually be larger
35065 ** than Pager.journalOff bytes. If the next thing in the journal
35066 ** file happens to be a journal-header (written as part of the
35067 ** previous connections transaction), and a crash or power-failure
35068 ** occurs after nRec is updated but before this connection writes
35069 ** anything else to the journal file (or commits/rolls back its
35070 ** transaction), then SQLite may become confused when doing the
35071 ** hot-journal rollback following recovery. It may roll back all
35072 ** of this connections data, then proceed to rolling back the old,
@@ -35134,10 +35249,11 @@
35134 /* The journal file was just successfully synced. Set Pager.needSync
35135 ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
35136 */
35137 pPager->needSync = 0;
35138 pPager->journalStarted = 1;
 
35139 sqlite3PcacheClearSyncFlags(pPager->pPCache);
35140 }
35141
35142 return SQLITE_OK;
35143 }
@@ -35996,23 +36112,32 @@
35996 }
35997 if( rc!=SQLITE_OK ){
35998 goto failed;
35999 }
36000
36001 /* TODO: Why are these cleared here? Is it necessary? */
 
36002 pPager->journalStarted = 0;
36003 pPager->journalOff = 0;
36004 pPager->setMaster = 0;
36005 pPager->journalHdr = 0;
 
 
36006
36007 /* Playback and delete the journal. Drop the database write
36008 ** lock and reacquire the read lock. Purge the cache before
36009 ** playing back the hot-journal so that we don't end up with
36010 ** an inconsistent cache.
 
 
 
36011 */
36012 if( isOpen(pPager->jfd) ){
36013 rc = pager_playback(pPager, 1);
 
 
 
36014 if( rc!=SQLITE_OK ){
36015 rc = pager_error(pPager, rc);
36016 goto failed;
36017 }
36018 }
@@ -36114,11 +36239,11 @@
36114 ** of the page. This occurs in two seperate scenarios:
36115 **
36116 ** a) When reading a free-list leaf page from the database, and
36117 **
36118 ** b) When a savepoint is being rolled back and we need to load
36119 ** a new page into the cache to populate with the data read
36120 ** from the savepoint journal.
36121 **
36122 ** If noContent is true, then the data returned is zeroed instead of
36123 ** being read from the database. Additionally, the bits corresponding
36124 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
@@ -36546,10 +36671,12 @@
36546
36547 /* We should never write to the journal file the page that
36548 ** contains the database locks. The following assert verifies
36549 ** that we do not. */
36550 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
 
 
36551 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
36552 cksum = pager_cksum(pPager, (u8*)pData2);
36553 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
36554 if( rc==SQLITE_OK ){
36555 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
@@ -60151,15 +60278,14 @@
60151 ** Syncing an in-memory journal is a no-op. And, in fact, this routine
60152 ** is never called in a working implementation. This implementation
60153 ** exists purely as a contingency, in case some malfunction in some other
60154 ** part of SQLite causes Sync to be called by mistake.
60155 */
60156 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ /*NO_TEST*/
60157 UNUSED_PARAMETER2(NotUsed, NotUsed2); /*NO_TEST*/
60158 assert( 0 ); /*NO_TEST*/
60159 return SQLITE_OK; /*NO_TEST*/
60160 } /*NO_TEST*/
60161
60162 /*
60163 ** Query the size of the file in bytes.
60164 */
60165 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
@@ -60751,11 +60877,11 @@
60751 }
60752 }
60753
60754 /*
60755 ** Allocate and return a pointer to an expression to load the column iCol
60756 ** from datasource iSrc datasource in SrcList pSrc.
60757 */
60758 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
60759 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
60760 if( p ){
60761 struct SrcList_item *pItem = &pSrc->a[iSrc];
@@ -60763,10 +60889,12 @@
60763 p->iTable = pItem->iCursor;
60764 if( p->pTab->iPKey==iCol ){
60765 p->iColumn = -1;
60766 }else{
60767 p->iColumn = (ynVar)iCol;
 
 
60768 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
60769 }
60770 ExprSetProperty(p, EP_Resolved);
60771 }
60772 return p;
@@ -79646,32 +79774,32 @@
79646 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
79647 ** But because db->init.busy is set to 1, no VDBE code is generated
79648 ** or executed. All the parser does is build the internal data
79649 ** structures that describe the table, index, or view.
79650 */
79651 char *zErr;
79652 int rc;
 
 
79653 assert( db->init.busy );
79654 db->init.iDb = iDb;
79655 db->init.newTnum = atoi(argv[1]);
79656 db->init.orphanTrigger = 0;
79657 rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
79658 db->init.iDb = 0;
79659 assert( rc!=SQLITE_OK || zErr==0 );
79660 if( SQLITE_OK!=rc ){
79661 if( db->init.orphanTrigger ){
79662 assert( iDb==1 );
79663 }else{
79664 pData->rc = rc;
79665 if( rc==SQLITE_NOMEM ){
79666 db->mallocFailed = 1;
79667 }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
79668 corruptSchema(pData, argv[0], zErr);
79669 }
79670 }
79671 sqlite3DbFree(db, zErr);
79672 }
 
79673 }else if( argv[0]==0 ){
79674 corruptSchema(pData, 0, 0);
79675 }else{
79676 /* If the SQL column is blank it means this is an index that
79677 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
@@ -82959,12 +83087,12 @@
82959 ** (13) The subquery and outer query do not both use LIMIT
82960 **
82961 ** (14) The subquery does not use OFFSET
82962 **
82963 ** (15) The outer query is not part of a compound select or the
82964 ** subquery does not have both an ORDER BY and a LIMIT clause.
82965 ** (See ticket #2339)
82966 **
82967 ** (16) The outer query is not an aggregate or the subquery does
82968 ** not contain ORDER BY. (Ticket #2942) This used to not matter
82969 ** until we introduced the group_concat() function.
82970 **
@@ -83043,11 +83171,11 @@
83043 ** because they could be computed at compile-time. But when LIMIT and OFFSET
83044 ** became arbitrary expressions, we were forced to add restrictions (13)
83045 ** and (14). */
83046 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
83047 if( pSub->pOffset ) return 0; /* Restriction (14) */
83048 if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){
83049 return 0; /* Restriction (15) */
83050 }
83051 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
83052 if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit)
83053 && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
@@ -89537,10 +89665,15 @@
89537 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
89538 const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
89539 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
89540 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
89541 WhereTerm *pTerm; /* A single term of the WHERE clause */
 
 
 
 
 
89542
89543 /* Search the WHERE clause terms for a usable WO_OR term. */
89544 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89545 if( pTerm->eOperator==WO_OR
89546 && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
@@ -89580,12 +89713,13 @@
89580 }
89581
89582 /* If there is an ORDER BY clause, increase the scan cost to account
89583 ** for the cost of the sort. */
89584 if( pOrderBy!=0 ){
 
 
89585 rTotal += nRow*estLog(nRow);
89586 WHERETRACE(("... sorting increases OR cost to %.9g\n", rTotal));
89587 }
89588
89589 /* If the cost of scanning using this OR term for optimization is
89590 ** less than the current cost stored in pCost, replace the contents
89591 ** of pCost. */
@@ -89658,21 +89792,21 @@
89658 /* The NOT INDEXED clause appears in the SQL. */
89659 return;
89660 }
89661
89662 assert( pParse->nQueryLoop >= (double)1 );
89663 nTableRow = pSrc->pIndex ? pSrc->pIndex->aiRowEst[0] : 1000000;
 
89664 logN = estLog(nTableRow);
89665 costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
89666 if( costTempIdx>=pCost->rCost ){
89667 /* The cost of creating the transient table would be greater than
89668 ** doing the full table scan */
89669 return;
89670 }
89671
89672 /* Search for any equality comparison term */
89673 pTable = pSrc->pTab;
89674 pWCEnd = &pWC->a[pWC->nTerm];
89675 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89676 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89677 WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
89678 pCost->rCost, costTempIdx));
@@ -89736,12 +89870,17 @@
89736 pWCEnd = &pWC->a[pWC->nTerm];
89737 idxCols = 0;
89738 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89739 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89740 int iCol = pTerm->u.leftColumn;
89741 if( iCol<BMS && iCol>=0 ) idxCols |= 1<<iCol;
89742 nColumn++;
 
 
 
 
 
89743 }
89744 }
89745 assert( nColumn>0 );
89746 pLevel->plan.nEq = nColumn;
89747
@@ -89751,14 +89890,16 @@
89751 ** original table never needs to be accessed. Automatic indices must
89752 ** be a covering index because the index will not be updated if the
89753 ** original table changes and the index and table cannot both be used
89754 ** if they go out of sync.
89755 */
89756 extraCols = pSrc->colUsed & ~idxCols;
89757 mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
 
 
89758 for(i=0; i<mxBitCol; i++){
89759 if( extraCols & (1<<i) ) nColumn++;
89760 }
89761 if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
89762 nColumn += pTable->nCol - BMS + 1;
89763 }
89764 pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
@@ -89776,25 +89917,31 @@
89776 pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
89777 pIdx->zName = "auto-index";
89778 pIdx->nColumn = nColumn;
89779 pIdx->pTable = pTable;
89780 n = 0;
 
89781 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89782 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89783 Expr *pX = pTerm->pExpr;
89784 pIdx->aiColumn[n] = pTerm->u.leftColumn;
89785 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
89786 pIdx->azColl[n] = pColl->zName;
89787 n++;
 
 
 
 
 
89788 }
89789 }
89790 assert( n==pLevel->plan.nEq );
89791
89792 /* Add additional columns needed to make the automatic index into
89793 ** a covering index */
89794 for(i=0; i<mxBitCol; i++){
89795 if( extraCols & (1<<i) ){
89796 pIdx->aiColumn[n] = i;
89797 pIdx->azColl[n] = "BINARY";
89798 n++;
89799 }
89800 }
@@ -90519,18 +90666,18 @@
90519 **
90520 ** bInEst:
90521 ** Set to true if there was at least one "x IN (SELECT ...)" term used
90522 ** in determining the value of nInMul.
90523 **
90524 ** nBound:
90525 ** An estimate on the amount of the table that must be searched. A
90526 ** value of 100 means the entire table is searched. Range constraints
90527 ** might reduce this to a value less than 100 to indicate that only
90528 ** a fraction of the table needs searching. In the absence of
90529 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
90530 ** space to 1/3rd its original size. So an x>? constraint reduces
90531 ** nBound to 33. Two constraints (x>? AND x<?) reduce nBound to 11.
90532 **
90533 ** bSort:
90534 ** Boolean. True if there is an ORDER BY clause that will require an
90535 ** external sort (i.e. scanning the index being evaluated will not
90536 ** correctly order records).
@@ -90548,17 +90695,18 @@
90548 ** SELECT a, b, c FROM tbl WHERE a = 1;
90549 */
90550 int nEq;
90551 int bInEst = 0;
90552 int nInMul = 1;
90553 int nBound = 100;
 
90554 int bSort = 0;
90555 int bLookup = 0;
 
90556
90557 /* Determine the values of nEq and nInMul */
90558 for(nEq=0; nEq<pProbe->nColumn; nEq++){
90559 WhereTerm *pTerm; /* A single term of the WHERE clause */
90560 int j = pProbe->aiColumn[nEq];
90561 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
90562 if( pTerm==0 ) break;
90563 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
90564 if( pTerm->eOperator & WO_IN ){
@@ -90574,22 +90722,24 @@
90574 wsFlags |= WHERE_COLUMN_NULL;
90575 }
90576 used |= pTerm->prereqRight;
90577 }
90578
90579 /* Determine the value of nBound. */
90580 if( nEq<pProbe->nColumn ){
90581 int j = pProbe->aiColumn[nEq];
90582 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
90583 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
90584 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
90585 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &nBound);
90586 if( pTop ){
 
90587 wsFlags |= WHERE_TOP_LIMIT;
90588 used |= pTop->prereqRight;
90589 }
90590 if( pBtm ){
 
90591 wsFlags |= WHERE_BTM_LIMIT;
90592 used |= pBtm->prereqRight;
90593 }
90594 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
90595 }
@@ -90635,12 +90785,11 @@
90635 }else{
90636 bLookup = 1;
90637 }
90638 }
90639
90640 /**** Begin adding up the cost of using this index (Needs improvements)
90641 **
90642 ** Estimate the number of rows of output. For an IN operator,
90643 ** do not let the estimate exceed half the rows in the table.
90644 */
90645 nRow = (double)(aiRowEst[nEq] * nInMul);
90646 if( bInEst && nRow*2>aiRowEst[0] ){
@@ -90655,12 +90804,12 @@
90655 cost = nRow + nInMul*estLog(aiRowEst[0]);
90656
90657 /* Adjust the number of rows and the cost downward to reflect rows
90658 ** that are excluded by range constraints.
90659 */
90660 nRow = (nRow * (double)nBound) / (double)100;
90661 cost = (cost * (double)nBound) / (double)100;
90662
90663 /* Add in the estimated cost of sorting the result
90664 */
90665 if( bSort ){
90666 cost += cost*estLog(cost);
@@ -90672,22 +90821,80 @@
90672 */
90673 if( pIdx && bLookup==0 ){
90674 cost /= (double)2;
90675 }
90676 /**** Cost of using this index has now been computed ****/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90677
90678 WHERETRACE((
90679 "%s(%s): nEq=%d nInMul=%d nBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
90680 " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
90681 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
90682 nEq, nInMul, nBound, bSort, bLookup, wsFlags, notReady, nRow, cost, used
 
90683 ));
90684
90685 /* If this index is the best we have seen so far, then record this
90686 ** index and its cost in the pCost structure.
90687 */
90688 if( (!pIdx || wsFlags) && cost<pCost->rCost ){
 
 
90689 pCost->rCost = cost;
90690 pCost->nRow = nRow;
90691 pCost->used = used;
90692 pCost->plan.wsFlags = (wsFlags&wsFlagMask);
90693 pCost->plan.nEq = nEq;
@@ -90718,11 +90925,12 @@
90718 || pCost->plan.u.pIdx==0
90719 || pCost->plan.u.pIdx==pSrc->pIndex
90720 );
90721
90722 WHERETRACE(("best index is: %s\n",
90723 (pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
 
90724 ));
90725
90726 bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
90727 bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
90728 pCost->plan.wsFlags |= eqTermMask;
@@ -91654,11 +91862,11 @@
91654
91655 /*
91656 ** Free a WhereInfo structure
91657 */
91658 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
91659 if( pWInfo ){
91660 int i;
91661 for(i=0; i<pWInfo->nLevel; i++){
91662 sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
91663 if( pInfo ){
91664 /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
@@ -91791,10 +91999,11 @@
91791 sqlite3 *db; /* Database connection */
91792
91793 /* The number of tables in the FROM clause is limited by the number of
91794 ** bits in a Bitmask
91795 */
 
91796 if( pTabList->nSrc>BMS ){
91797 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
91798 return 0;
91799 }
91800
@@ -91930,24 +92139,29 @@
91930
91931 memset(&bestPlan, 0, sizeof(bestPlan));
91932 bestPlan.rCost = SQLITE_BIG_DBL;
91933
91934 /* Loop through the remaining entries in the FROM clause to find the
91935 ** next nested loop. The FROM clause entries may be iterated through
91936 ** either once or twice.
91937 **
91938 ** The first iteration, which is always performed, searches for the
91939 ** FROM clause entry that permits the lowest-cost, "optimal" scan. In
 
91940 ** this context an optimal scan is one that uses the same strategy
91941 ** for the given FROM clause entry as would be selected if the entry
91942 ** were used as the innermost nested loop. In other words, a table
91943 ** is chosen such that the cost of running that table cannot be reduced
91944 ** by waiting for other tables to run first.
 
 
 
 
91945 **
91946 ** The second iteration is only performed if no optimal scan strategies
91947 ** were found by the first. This iteration is used to search for the
91948 ** lowest cost scan overall.
91949 **
91950 ** Previous versions of SQLite performed only the second iteration -
91951 ** the next outermost loop was always that with the lowest overall
91952 ** cost. However, this meant that SQLite could select the wrong plan
91953 ** for scripts such as the following:
@@ -91961,13 +92175,12 @@
91961 ** However, since the cost of a linear scan through table t2 is the same
91962 ** as the cost of a linear scan through table t1, a simple greedy
91963 ** algorithm may choose to use t2 for the outer loop, which is a much
91964 ** costlier approach.
91965 */
91966 for(isOptimal=1; isOptimal>=0 && bestJ<0; isOptimal--){
91967 Bitmask mask = (isOptimal ? 0 : notReady);
91968 assert( (nTabList-iFrom)>1 || isOptimal );
91969 for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
91970 int doNotReorder; /* True if this table should not be reordered */
91971 WhereCost sCost; /* Cost information from best[Virtual]Index() */
91972 ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
91973
@@ -91976,10 +92189,11 @@
91976 m = getMask(pMaskSet, pTabItem->iCursor);
91977 if( (m & notReady)==0 ){
91978 if( j==iFrom ) iFrom++;
91979 continue;
91980 }
 
91981 pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
91982
91983 assert( pTabItem->pTab );
91984 #ifndef SQLITE_OMIT_VIRTUALTABLE
91985 if( IsVirtual(pTabItem->pTab) ){
@@ -91991,12 +92205,15 @@
91991 bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost);
91992 }
91993 assert( isOptimal || (sCost.used&notReady)==0 );
91994
91995 if( (sCost.used&notReady)==0
91996 && (j==iFrom || sCost.rCost<bestPlan.rCost)
 
91997 ){
 
 
91998 bestPlan = sCost;
91999 bestJ = j;
92000 }
92001 if( doNotReorder ) break;
92002 }
@@ -92118,10 +92335,12 @@
92118 #endif
92119 if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
92120 && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
92121 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
92122 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
 
 
92123 if( !pWInfo->okOnePass && pTab->nCol<BMS ){
92124 Bitmask b = pTabItem->colUsed;
92125 int n = 0;
92126 for(; b; b=b>>1, n++){}
92127 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
@@ -92297,11 +92516,11 @@
92297 ){
92298 int ws = pLevel->plan.wsFlags;
92299 if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
92300 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
92301 }
92302 if( (ws & (WHERE_INDEXED|WHERE_TEMP_INDEX)) == WHERE_INDEXED ){
92303 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
92304 }
92305 }
92306
92307 /* If this scan uses an index, make code substitutions to read data
@@ -92345,14 +92564,12 @@
92345 }
92346 }
92347
92348 /* Final cleanup
92349 */
92350 if( pWInfo ){
92351 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
92352 whereInfoFree(db, pWInfo);
92353 }
92354 return;
92355 }
92356
92357 /************** End of where.c ***********************************************/
92358 /************** Begin file parse.c *******************************************/
92359
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -628,11 +628,11 @@
628 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
629 ** [sqlite_version()] and [sqlite_source_id()].
630 */
631 #define SQLITE_VERSION "3.6.23"
632 #define SQLITE_VERSION_NUMBER 3006023
633 #define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
634
635 /*
636 ** CAPI3REF: Run-Time Library Version Numbers
637 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
638 **
@@ -4404,12 +4404,10 @@
4404 ** ^For the purposes of this API, a transaction is said to have been
4405 ** rolled back if an explicit "ROLLBACK" statement is executed, or
4406 ** an error or constraint causes an implicit rollback to occur.
4407 ** ^The rollback callback is not invoked if a transaction is
4408 ** automatically rolled back because the database connection is closed.
 
 
4409 **
4410 ** See also the [sqlite3_update_hook()] interface.
4411 */
4412 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4413 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
@@ -32356,10 +32354,91 @@
32354 ** file simultaneously, or one process from reading the database while
32355 ** another is writing.
32356 */
32357 #ifndef SQLITE_OMIT_DISKIO
32358
32359 /*
32360 ******************** NOTES ON THE DESIGN OF THE PAGER ************************
32361 **
32362 ** Within this comment block, a page is deemed to have been synced
32363 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
32364 ** Otherwise, the page is not synced until the xSync method of the VFS
32365 ** is called successfully on the file containing the page.
32366 **
32367 ** Definition: A page of the database file is said to be "overwriteable" if
32368 ** one or more of the following are true about the page:
32369 **
32370 ** (a) The original content of the page as it was at the beginning of
32371 ** the transaction has been written into the rollback journal and
32372 ** synced.
32373 **
32374 ** (b) The page was a freelist leaf page at the start of the transaction.
32375 **
32376 ** (c) The page number is greater than the largest page that existed in
32377 ** the database file at the start of the transaction.
32378 **
32379 ** (1) A page of the database file is never overwritten unless one of the
32380 ** following are true:
32381 **
32382 ** (a) The page and all other pages on the same sector are overwriteable.
32383 **
32384 ** (b) The atomic page write optimization is enabled, and the entire
32385 ** transaction other than the update of the transaction sequence
32386 ** number consists of a single page change.
32387 **
32388 ** (2) The content of a page written into the rollback journal exactly matches
32389 ** both the content in the database when the rollback journal was written
32390 ** and the content in the database at the beginning of the current
32391 ** transaction.
32392 **
32393 ** (3) Writes to the database file are an integer multiple of the page size
32394 ** in length and are aligned to a page boundary.
32395 **
32396 ** (4) Reads from the database file are either aligned on a page boundary and
32397 ** an integer multiple of the page size in length or are taken from the
32398 ** first 100 bytes of the database file.
32399 **
32400 ** (5) All writes to the database file are synced prior to the rollback journal
32401 ** being deleted, truncated, or zeroed.
32402 **
32403 ** (6) If a master journal file is used, then all writes to the database file
32404 ** are synced prior to the master journal being deleted.
32405 **
32406 ** Definition: Two databases (or the same database at two points it time)
32407 ** are said to be "logically equivalent" if they give the same answer to
32408 ** all queries. Note in particular the the content of freelist leaf
32409 ** pages can be changed arbitarily without effecting the logical equivalence
32410 ** of the database.
32411 **
32412 ** (7) At any time, if any subset, including the empty set and the total set,
32413 ** of the unsynced changes to a rollback journal are removed and the
32414 ** journal is rolled back, the resulting database file will be logical
32415 ** equivalent to the database file at the beginning of the transaction.
32416 **
32417 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
32418 ** is called to restore the database file to the same size it was at
32419 ** the beginning of the transaction. (In some VFSes, the xTruncate
32420 ** method is a no-op, but that does not change the fact the SQLite will
32421 ** invoke it.)
32422 **
32423 ** (9) Whenever the database file is modified, at least one bit in the range
32424 ** of bytes from 24 through 39 inclusive will be changed prior to releasing
32425 ** the EXCLUSIVE lock.
32426 **
32427 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
32428 ** than one billion transactions.
32429 **
32430 ** (11) A database file is well-formed at the beginning and at the conclusion
32431 ** of every transaction.
32432 **
32433 ** (12) An EXCLUSIVE lock is held on the database file when writing to
32434 ** the database file.
32435 **
32436 ** (13) A SHARED lock is held on the database file while reading any
32437 ** content out of the database file.
32438 */
32439
32440 /*
32441 ** Macros for troubleshooting. Normally turned off
32442 */
32443 #if 0
32444 int sqlite3PagerTrace=1; /* True to enable tracing */
@@ -32532,11 +32611,12 @@
32611 ** It is used when committing or otherwise ending a transaction. If
32612 ** the dbModified flag is clear then less work has to be done.
32613 **
32614 ** journalStarted
32615 **
32616 ** This flag is set whenever the the main journal is opened and
32617 ** initialized
32618 **
32619 ** The point of this flag is that it must be set after the
32620 ** first journal header in a journal file has been synced to disk.
32621 ** After this has happened, new pages appended to the database
32622 ** do not need the PGHDR_NEED_SYNC flag set, as they do not need
@@ -32558,11 +32638,14 @@
32638 ** master journal name is only written to the journal file the first
32639 ** time CommitPhaseOne() is called.
32640 **
32641 ** doNotSync
32642 **
32643 ** When enabled, cache spills are prohibited and the journal file cannot
32644 ** be synced. This variable is set and cleared by sqlite3PagerWrite()
32645 ** in order to prevent a journal sync from happening in between the
32646 ** journalling of two pages on the same sector.
32647 **
32648 ** needSync
32649 **
32650 ** TODO: It might be easier to set this variable in writeJournalHdr()
32651 ** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -32618,10 +32701,11 @@
32701 sqlite3_file *fd; /* File descriptor for database */
32702 sqlite3_file *jfd; /* File descriptor for main journal */
32703 sqlite3_file *sjfd; /* File descriptor for sub-journal */
32704 i64 journalOff; /* Current write offset in the journal file */
32705 i64 journalHdr; /* Byte offset to previous journal header */
32706 i64 journalSizeLimit; /* Size limit for persistent journal files */
32707 PagerSavepoint *aSavepoint; /* Array of active savepoints */
32708 int nSavepoint; /* Number of elements in aSavepoint[] */
32709 char dbFileVers[16]; /* Changes whenever database file changes */
32710 u32 sectorSize; /* Assumed sector size during rollback */
32711
@@ -32644,11 +32728,10 @@
32728 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
32729 void (*xCodecFree)(void*); /* Destructor for the codec */
32730 void *pCodec; /* First argument to xCodec... methods */
32731 #endif
32732 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
 
32733 PCache *pPCache; /* Pointer to page cache object */
32734 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
32735 };
32736
32737 /*
@@ -33160,10 +33243,11 @@
33243 ** to populate the entire journal header sector.
33244 */
33245 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
33246 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
33247 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
33248 assert( pPager->journalHdr <= pPager->journalOff );
33249 pPager->journalOff += nHeader;
33250 }
33251
33252 return rc;
33253 }
@@ -33318,10 +33402,11 @@
33402 ){
33403 return SQLITE_OK;
33404 }
33405 pPager->setMaster = 1;
33406 assert( isOpen(pPager->jfd) );
33407 assert( pPager->journalHdr <= pPager->journalOff );
33408
33409 /* Calculate the length in bytes and the checksum of zMaster */
33410 for(nMaster=0; zMaster[nMaster]; nMaster++){
33411 cksum += zMaster[nMaster];
33412 }
@@ -33747,22 +33832,22 @@
33832 ** allocated by this function. If this is the case and an allocation fails,
33833 ** SQLITE_NOMEM is returned.
33834 */
33835 static int pager_playback_one_page(
33836 Pager *pPager, /* The pager being played back */
 
 
33837 i64 *pOffset, /* Offset of record to playback */
33838 Bitvec *pDone, /* Bitvec of pages already played back */
33839 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
33840 int isSavepnt /* True for a savepoint rollback */
33841 ){
33842 int rc;
33843 PgHdr *pPg; /* An existing page in the cache */
33844 Pgno pgno; /* The page number of a page in journal */
33845 u32 cksum; /* Checksum used for sanity checking */
33846 char *aData; /* Temporary storage for the page */
33847 sqlite3_file *jfd; /* The file descriptor for the journal file */
33848 int isSynced; /* True if journal page is synced */
33849
33850 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
33851 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
33852 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
33853 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
@@ -33842,16 +33927,21 @@
33927 assert( pPg || !MEMDB );
33928 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
33929 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
33930 (isMainJrnl?"main-journal":"sub-journal")
33931 ));
33932 if( isMainJrnl ){
33933 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
33934 }else{
33935 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
33936 }
33937 if( (pPager->state>=PAGER_EXCLUSIVE)
 
33938 && isOpen(pPager->fd)
33939 && isSynced
33940 ){
33941 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
33942 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
33943 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
33944 if( pgno>pPager->dbFileSize ){
33945 pPager->dbFileSize = pgno;
33946 }
33947 if( pPager->pBackup ){
@@ -33896,11 +33986,12 @@
33986 pPager->xReiniter(pPg);
33987 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
33988 /* If the contents of this page were just restored from the main
33989 ** journal file, then its content must be as they were when the
33990 ** transaction was first opened. In this case we can mark the page
33991 ** as clean, since there will be no need to write it out to the
33992 ** database.
33993 **
33994 ** There is one exception to this rule. If the page is being rolled
33995 ** back as part of a savepoint (or statement) rollback from an
33996 ** unsynced portion of the main journal file, then it is not safe
33997 ** to mark the page as clean. This is because marking the page as
@@ -34243,12 +34334,10 @@
34334 /* This loop terminates either when a readJournalHdr() or
34335 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
34336 ** occurs.
34337 */
34338 while( 1 ){
 
 
34339 /* Read the next journal header from the journal file. If there are
34340 ** not enough bytes left in the journal file for a complete header, or
34341 ** it is corrupted, then a process must of failed while writing it.
34342 ** This indicates nothing more needs to be rolled back.
34343 */
@@ -34285,11 +34374,10 @@
34374 ** should be computed based on the journal file size.
34375 */
34376 if( nRec==0 && !isHot &&
34377 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
34378 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
 
34379 }
34380
34381 /* If this is the first header read from the journal, truncate the
34382 ** database file back to its original size.
34383 */
@@ -34307,11 +34395,11 @@
34395 for(u=0; u<nRec; u++){
34396 if( needPagerReset ){
34397 pager_reset(pPager);
34398 needPagerReset = 0;
34399 }
34400 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
34401 if( rc!=SQLITE_OK ){
34402 if( rc==SQLITE_DONE ){
34403 rc = SQLITE_OK;
34404 pPager->journalOff = szJ;
34405 break;
@@ -34460,11 +34548,11 @@
34548 */
34549 if( pSavepoint ){
34550 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
34551 pPager->journalOff = pSavepoint->iOffset;
34552 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
34553 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
34554 }
34555 assert( rc!=SQLITE_DONE );
34556 }else{
34557 pPager->journalOff = 0;
34558 }
@@ -34490,11 +34578,11 @@
34578 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
34579 ){
34580 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
34581 }
34582 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
34583 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
34584 }
34585 assert( rc!=SQLITE_DONE );
34586 }
34587 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
34588
@@ -34505,11 +34593,11 @@
34593 if( pSavepoint ){
34594 u32 ii; /* Loop counter */
34595 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
34596 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
34597 assert( offset==ii*(4+pPager->pageSize) );
34598 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
34599 }
34600 assert( rc!=SQLITE_DONE );
34601 }
34602
34603 sqlite3BitvecDestroy(pDone);
@@ -34942,10 +35030,35 @@
35030 assert( pPager->dbSize>=nPage );
35031 assert( pPager->state>=PAGER_RESERVED );
35032 pPager->dbSize = nPage;
35033 assertTruncateConstraint(pPager);
35034 }
35035
35036 /*
35037 ** This function is called before attempting a hot-journal rollback. It
35038 ** syncs the journal file to disk, then sets pPager->journalHdr to the
35039 ** size of the journal file so that the pager_playback() routine knows
35040 ** that the entire journal file has been synced.
35041 **
35042 ** Syncing a hot-journal to disk before attempting to roll it back ensures
35043 ** that if a power-failure occurs during the rollback, the process that
35044 ** attempts rollback following system recovery sees the same journal
35045 ** content as this process.
35046 **
35047 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
35048 ** an SQLite error code.
35049 */
35050 static int pagerSyncHotJournal(Pager *pPager){
35051 int rc = SQLITE_OK;
35052 if( !pPager->noSync ){
35053 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
35054 }
35055 if( rc==SQLITE_OK ){
35056 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
35057 }
35058 return rc;
35059 }
35060
35061 /*
35062 ** Shutdown the page cache. Free all memory and close all files.
35063 **
35064 ** If a transaction was in progress when this routine is called, that
@@ -34972,11 +35085,13 @@
35085 ** call which may be made from within pagerUnlockAndRollback(). If it
35086 ** is not -1, then the unsynced portion of an open journal file may
35087 ** be played back into the database. If a power failure occurs while
35088 ** this is happening, the database may become corrupt.
35089 */
35090 if( isOpen(pPager->jfd) ){
35091 pPager->errCode = pagerSyncHotJournal(pPager);
35092 }
35093 pagerUnlockAndRollback(pPager);
35094 }
35095 sqlite3EndBenignMalloc();
35096 enable_simulated_io_errors();
35097 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
@@ -35062,11 +35177,11 @@
35177 /* This block deals with an obscure problem. If the last connection
35178 ** that wrote to this database was operating in persistent-journal
35179 ** mode, then the journal file may at this point actually be larger
35180 ** than Pager.journalOff bytes. If the next thing in the journal
35181 ** file happens to be a journal-header (written as part of the
35182 ** previous connection's transaction), and a crash or power-failure
35183 ** occurs after nRec is updated but before this connection writes
35184 ** anything else to the journal file (or commits/rolls back its
35185 ** transaction), then SQLite may become confused when doing the
35186 ** hot-journal rollback following recovery. It may roll back all
35187 ** of this connections data, then proceed to rolling back the old,
@@ -35134,10 +35249,11 @@
35249 /* The journal file was just successfully synced. Set Pager.needSync
35250 ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
35251 */
35252 pPager->needSync = 0;
35253 pPager->journalStarted = 1;
35254 pPager->journalHdr = pPager->journalOff;
35255 sqlite3PcacheClearSyncFlags(pPager->pPCache);
35256 }
35257
35258 return SQLITE_OK;
35259 }
@@ -35996,23 +36112,32 @@
36112 }
36113 if( rc!=SQLITE_OK ){
36114 goto failed;
36115 }
36116
36117 /* Reset the journal status fields to indicates that we have no
36118 ** rollback journal at this time. */
36119 pPager->journalStarted = 0;
36120 pPager->journalOff = 0;
36121 pPager->setMaster = 0;
36122 pPager->journalHdr = 0;
36123
36124 /* Make sure the journal file has been synced to disk. */
36125
36126 /* Playback and delete the journal. Drop the database write
36127 ** lock and reacquire the read lock. Purge the cache before
36128 ** playing back the hot-journal so that we don't end up with
36129 ** an inconsistent cache. Sync the hot journal before playing
36130 ** it back since the process that crashed and left the hot journal
36131 ** probably did not sync it and we are required to always sync
36132 ** the journal before playing it back.
36133 */
36134 if( isOpen(pPager->jfd) ){
36135 rc = pagerSyncHotJournal(pPager);
36136 if( rc==SQLITE_OK ){
36137 rc = pager_playback(pPager, 1);
36138 }
36139 if( rc!=SQLITE_OK ){
36140 rc = pager_error(pPager, rc);
36141 goto failed;
36142 }
36143 }
@@ -36114,11 +36239,11 @@
36239 ** of the page. This occurs in two seperate scenarios:
36240 **
36241 ** a) When reading a free-list leaf page from the database, and
36242 **
36243 ** b) When a savepoint is being rolled back and we need to load
36244 ** a new page into the cache to be filled with the data read
36245 ** from the savepoint journal.
36246 **
36247 ** If noContent is true, then the data returned is zeroed instead of
36248 ** being read from the database. Additionally, the bits corresponding
36249 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
@@ -36546,10 +36671,12 @@
36671
36672 /* We should never write to the journal file the page that
36673 ** contains the database locks. The following assert verifies
36674 ** that we do not. */
36675 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
36676
36677 assert( pPager->journalHdr <= pPager->journalOff );
36678 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
36679 cksum = pager_cksum(pPager, (u8*)pData2);
36680 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
36681 if( rc==SQLITE_OK ){
36682 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
@@ -60151,15 +60278,14 @@
60278 ** Syncing an in-memory journal is a no-op. And, in fact, this routine
60279 ** is never called in a working implementation. This implementation
60280 ** exists purely as a contingency, in case some malfunction in some other
60281 ** part of SQLite causes Sync to be called by mistake.
60282 */
60283 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
60284 UNUSED_PARAMETER2(NotUsed, NotUsed2);
60285 return SQLITE_OK;
60286 }
 
60287
60288 /*
60289 ** Query the size of the file in bytes.
60290 */
60291 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
@@ -60751,11 +60877,11 @@
60877 }
60878 }
60879
60880 /*
60881 ** Allocate and return a pointer to an expression to load the column iCol
60882 ** from datasource iSrc in SrcList pSrc.
60883 */
60884 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
60885 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
60886 if( p ){
60887 struct SrcList_item *pItem = &pSrc->a[iSrc];
@@ -60763,10 +60889,12 @@
60889 p->iTable = pItem->iCursor;
60890 if( p->pTab->iPKey==iCol ){
60891 p->iColumn = -1;
60892 }else{
60893 p->iColumn = (ynVar)iCol;
60894 testcase( iCol==BMS );
60895 testcase( iCol==BMS-1 );
60896 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
60897 }
60898 ExprSetProperty(p, EP_Resolved);
60899 }
60900 return p;
@@ -79646,32 +79774,32 @@
79774 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
79775 ** But because db->init.busy is set to 1, no VDBE code is generated
79776 ** or executed. All the parser does is build the internal data
79777 ** structures that describe the table, index, or view.
79778 */
 
79779 int rc;
79780 sqlite3_stmt *pStmt;
79781
79782 assert( db->init.busy );
79783 db->init.iDb = iDb;
79784 db->init.newTnum = atoi(argv[1]);
79785 db->init.orphanTrigger = 0;
79786 rc = sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
79787 db->init.iDb = 0;
 
79788 if( SQLITE_OK!=rc ){
79789 if( db->init.orphanTrigger ){
79790 assert( iDb==1 );
79791 }else{
79792 pData->rc = rc;
79793 if( rc==SQLITE_NOMEM ){
79794 db->mallocFailed = 1;
79795 }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){
79796 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
79797 }
79798 }
 
79799 }
79800 sqlite3_finalize(pStmt);
79801 }else if( argv[0]==0 ){
79802 corruptSchema(pData, 0, 0);
79803 }else{
79804 /* If the SQL column is blank it means this is an index that
79805 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
@@ -82959,12 +83087,12 @@
83087 ** (13) The subquery and outer query do not both use LIMIT
83088 **
83089 ** (14) The subquery does not use OFFSET
83090 **
83091 ** (15) The outer query is not part of a compound select or the
83092 ** subquery does not have a LIMIT clause.
83093 ** (See ticket #2339 and ticket [02a8e81d44]).
83094 **
83095 ** (16) The outer query is not an aggregate or the subquery does
83096 ** not contain ORDER BY. (Ticket #2942) This used to not matter
83097 ** until we introduced the group_concat() function.
83098 **
@@ -83043,11 +83171,11 @@
83171 ** because they could be computed at compile-time. But when LIMIT and OFFSET
83172 ** became arbitrary expressions, we were forced to add restrictions (13)
83173 ** and (14). */
83174 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
83175 if( pSub->pOffset ) return 0; /* Restriction (14) */
83176 if( p->pRightmost && pSub->pLimit ){
83177 return 0; /* Restriction (15) */
83178 }
83179 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
83180 if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit)
83181 && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
@@ -89537,10 +89665,15 @@
89665 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
89666 const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
89667 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
89668 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
89669 WhereTerm *pTerm; /* A single term of the WHERE clause */
89670
89671 /* No OR-clause optimization allowed if the NOT INDEXED clause is used */
89672 if( pSrc->notIndexed ){
89673 return;
89674 }
89675
89676 /* Search the WHERE clause terms for a usable WO_OR term. */
89677 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89678 if( pTerm->eOperator==WO_OR
89679 && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
@@ -89580,12 +89713,13 @@
89713 }
89714
89715 /* If there is an ORDER BY clause, increase the scan cost to account
89716 ** for the cost of the sort. */
89717 if( pOrderBy!=0 ){
89718 WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
89719 rTotal, rTotal+nRow*estLog(nRow)));
89720 rTotal += nRow*estLog(nRow);
 
89721 }
89722
89723 /* If the cost of scanning using this OR term for optimization is
89724 ** less than the current cost stored in pCost, replace the contents
89725 ** of pCost. */
@@ -89658,21 +89792,21 @@
89792 /* The NOT INDEXED clause appears in the SQL. */
89793 return;
89794 }
89795
89796 assert( pParse->nQueryLoop >= (double)1 );
89797 pTable = pSrc->pTab;
89798 nTableRow = pTable->pIndex ? pTable->pIndex->aiRowEst[0] : 1000000;
89799 logN = estLog(nTableRow);
89800 costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
89801 if( costTempIdx>=pCost->rCost ){
89802 /* The cost of creating the transient table would be greater than
89803 ** doing the full table scan */
89804 return;
89805 }
89806
89807 /* Search for any equality comparison term */
 
89808 pWCEnd = &pWC->a[pWC->nTerm];
89809 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89810 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89811 WHERETRACE(("auto-index reduces cost from %.2f to %.2f\n",
89812 pCost->rCost, costTempIdx));
@@ -89736,12 +89870,17 @@
89870 pWCEnd = &pWC->a[pWC->nTerm];
89871 idxCols = 0;
89872 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89873 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89874 int iCol = pTerm->u.leftColumn;
89875 Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
89876 testcase( iCol==BMS );
89877 testcase( iCol==BMS-1 );
89878 if( (idxCols & cMask)==0 ){
89879 nColumn++;
89880 idxCols |= cMask;
89881 }
89882 }
89883 }
89884 assert( nColumn>0 );
89885 pLevel->plan.nEq = nColumn;
89886
@@ -89751,14 +89890,16 @@
89890 ** original table never needs to be accessed. Automatic indices must
89891 ** be a covering index because the index will not be updated if the
89892 ** original table changes and the index and table cannot both be used
89893 ** if they go out of sync.
89894 */
89895 extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
89896 mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
89897 testcase( pTable->nCol==BMS-1 );
89898 testcase( pTable->nCol==BMS-2 );
89899 for(i=0; i<mxBitCol; i++){
89900 if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
89901 }
89902 if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
89903 nColumn += pTable->nCol - BMS + 1;
89904 }
89905 pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
@@ -89776,25 +89917,31 @@
89917 pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
89918 pIdx->zName = "auto-index";
89919 pIdx->nColumn = nColumn;
89920 pIdx->pTable = pTable;
89921 n = 0;
89922 idxCols = 0;
89923 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
89924 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
89925 int iCol = pTerm->u.leftColumn;
89926 Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
89927 if( (idxCols & cMask)==0 ){
89928 Expr *pX = pTerm->pExpr;
89929 idxCols |= cMask;
89930 pIdx->aiColumn[n] = pTerm->u.leftColumn;
89931 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
89932 pIdx->azColl[n] = pColl->zName;
89933 n++;
89934 }
89935 }
89936 }
89937 assert( n==pLevel->plan.nEq );
89938
89939 /* Add additional columns needed to make the automatic index into
89940 ** a covering index */
89941 for(i=0; i<mxBitCol; i++){
89942 if( extraCols & (((Bitmask)1)<<i) ){
89943 pIdx->aiColumn[n] = i;
89944 pIdx->azColl[n] = "BINARY";
89945 n++;
89946 }
89947 }
@@ -90519,18 +90666,18 @@
90666 **
90667 ** bInEst:
90668 ** Set to true if there was at least one "x IN (SELECT ...)" term used
90669 ** in determining the value of nInMul.
90670 **
90671 ** estBound:
90672 ** An estimate on the amount of the table that must be searched. A
90673 ** value of 100 means the entire table is searched. Range constraints
90674 ** might reduce this to a value less than 100 to indicate that only
90675 ** a fraction of the table needs searching. In the absence of
90676 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
90677 ** space to 1/3rd its original size. So an x>? constraint reduces
90678 ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11.
90679 **
90680 ** bSort:
90681 ** Boolean. True if there is an ORDER BY clause that will require an
90682 ** external sort (i.e. scanning the index being evaluated will not
90683 ** correctly order records).
@@ -90548,17 +90695,18 @@
90695 ** SELECT a, b, c FROM tbl WHERE a = 1;
90696 */
90697 int nEq;
90698 int bInEst = 0;
90699 int nInMul = 1;
90700 int estBound = 100;
90701 int nBound = 0; /* Number of range constraints seen */
90702 int bSort = 0;
90703 int bLookup = 0;
90704 WhereTerm *pTerm; /* A single term of the WHERE clause */
90705
90706 /* Determine the values of nEq and nInMul */
90707 for(nEq=0; nEq<pProbe->nColumn; nEq++){
 
90708 int j = pProbe->aiColumn[nEq];
90709 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
90710 if( pTerm==0 ) break;
90711 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
90712 if( pTerm->eOperator & WO_IN ){
@@ -90574,22 +90722,24 @@
90722 wsFlags |= WHERE_COLUMN_NULL;
90723 }
90724 used |= pTerm->prereqRight;
90725 }
90726
90727 /* Determine the value of estBound. */
90728 if( nEq<pProbe->nColumn ){
90729 int j = pProbe->aiColumn[nEq];
90730 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
90731 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
90732 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
90733 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
90734 if( pTop ){
90735 nBound = 1;
90736 wsFlags |= WHERE_TOP_LIMIT;
90737 used |= pTop->prereqRight;
90738 }
90739 if( pBtm ){
90740 nBound++;
90741 wsFlags |= WHERE_BTM_LIMIT;
90742 used |= pBtm->prereqRight;
90743 }
90744 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
90745 }
@@ -90635,12 +90785,11 @@
90785 }else{
90786 bLookup = 1;
90787 }
90788 }
90789
90790 /*
 
90791 ** Estimate the number of rows of output. For an IN operator,
90792 ** do not let the estimate exceed half the rows in the table.
90793 */
90794 nRow = (double)(aiRowEst[nEq] * nInMul);
90795 if( bInEst && nRow*2>aiRowEst[0] ){
@@ -90655,12 +90804,12 @@
90804 cost = nRow + nInMul*estLog(aiRowEst[0]);
90805
90806 /* Adjust the number of rows and the cost downward to reflect rows
90807 ** that are excluded by range constraints.
90808 */
90809 nRow = (nRow * (double)estBound) / (double)100;
90810 cost = (cost * (double)estBound) / (double)100;
90811
90812 /* Add in the estimated cost of sorting the result
90813 */
90814 if( bSort ){
90815 cost += cost*estLog(cost);
@@ -90672,22 +90821,80 @@
90821 */
90822 if( pIdx && bLookup==0 ){
90823 cost /= (double)2;
90824 }
90825 /**** Cost of using this index has now been computed ****/
90826
90827 /* If there are additional constraints on this table that cannot
90828 ** be used with the current index, but which might lower the number
90829 ** of output rows, adjust the nRow value accordingly. This only
90830 ** matters if the current index is the least costly, so do not bother
90831 ** with this step if we already know this index will not be chosen.
90832 ** Also, never reduce the output row count below 2 using this step.
90833 **
90834 ** Do not reduce the output row count if pSrc is the only table that
90835 ** is notReady; if notReady is a power of two. This will be the case
90836 ** when the main sqlite3WhereBegin() loop is scanning for a table with
90837 ** and "optimal" index, and on such a scan the output row count
90838 ** reduction is not valid because it does not update the "pCost->used"
90839 ** bitmap. The notReady bitmap will also be a power of two when we
90840 ** are scanning for the last table in a 64-way join. We are willing
90841 ** to bypass this optimization in that corner case.
90842 */
90843 if( nRow>2 && cost<=pCost->rCost && (notReady & (notReady-1))!=0 ){
90844 int k; /* Loop counter */
90845 int nSkipEq = nEq; /* Number of == constraints to skip */
90846 int nSkipRange = nBound; /* Number of < constraints to skip */
90847 Bitmask thisTab; /* Bitmap for pSrc */
90848
90849 thisTab = getMask(pWC->pMaskSet, iCur);
90850 for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
90851 if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
90852 if( (pTerm->prereqAll & notReady)!=thisTab ) continue;
90853 if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
90854 if( nSkipEq ){
90855 /* Ignore the first nEq equality matches since the index
90856 ** has already accounted for these */
90857 nSkipEq--;
90858 }else{
90859 /* Assume each additional equality match reduces the result
90860 ** set size by a factor of 10 */
90861 nRow /= 10;
90862 }
90863 }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
90864 if( nSkipRange ){
90865 /* Ignore the first nBound range constraints since the index
90866 ** has already accounted for these */
90867 nSkipRange--;
90868 }else{
90869 /* Assume each additional range constraint reduces the result
90870 ** set size by a factor of 3 */
90871 nRow /= 3;
90872 }
90873 }else{
90874 /* Any other expression lowers the output row count by half */
90875 nRow /= 2;
90876 }
90877 }
90878 if( nRow<2 ) nRow = 2;
90879 }
90880
90881
90882 WHERETRACE((
90883 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
90884 " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
90885 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
90886 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
90887 notReady, nRow, cost, used
90888 ));
90889
90890 /* If this index is the best we have seen so far, then record this
90891 ** index and its cost in the pCost structure.
90892 */
90893 if( (!pIdx || wsFlags)
90894 && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->nRow))
90895 ){
90896 pCost->rCost = cost;
90897 pCost->nRow = nRow;
90898 pCost->used = used;
90899 pCost->plan.wsFlags = (wsFlags&wsFlagMask);
90900 pCost->plan.nEq = nEq;
@@ -90718,11 +90925,12 @@
90925 || pCost->plan.u.pIdx==0
90926 || pCost->plan.u.pIdx==pSrc->pIndex
90927 );
90928
90929 WHERETRACE(("best index is: %s\n",
90930 ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" :
90931 pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
90932 ));
90933
90934 bestOrClauseIndex(pParse, pWC, pSrc, notReady, pOrderBy, pCost);
90935 bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
90936 pCost->plan.wsFlags |= eqTermMask;
@@ -91654,11 +91862,11 @@
91862
91863 /*
91864 ** Free a WhereInfo structure
91865 */
91866 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
91867 if( ALWAYS(pWInfo) ){
91868 int i;
91869 for(i=0; i<pWInfo->nLevel; i++){
91870 sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
91871 if( pInfo ){
91872 /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
@@ -91791,10 +91999,11 @@
91999 sqlite3 *db; /* Database connection */
92000
92001 /* The number of tables in the FROM clause is limited by the number of
92002 ** bits in a Bitmask
92003 */
92004 testcase( pTabList->nSrc==BMS );
92005 if( pTabList->nSrc>BMS ){
92006 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
92007 return 0;
92008 }
92009
@@ -91930,24 +92139,29 @@
92139
92140 memset(&bestPlan, 0, sizeof(bestPlan));
92141 bestPlan.rCost = SQLITE_BIG_DBL;
92142
92143 /* Loop through the remaining entries in the FROM clause to find the
92144 ** next nested loop. The loop tests all FROM clause entries
92145 ** either once or twice.
92146 **
92147 ** The first test is always performed if there are two or more entries
92148 ** remaining and never performed if there is only one FROM clause entry
92149 ** to choose from. The first test looks for an "optimal" scan. In
92150 ** this context an optimal scan is one that uses the same strategy
92151 ** for the given FROM clause entry as would be selected if the entry
92152 ** were used as the innermost nested loop. In other words, a table
92153 ** is chosen such that the cost of running that table cannot be reduced
92154 ** by waiting for other tables to run first. This "optimal" test works
92155 ** by first assuming that the FROM clause is on the inner loop and finding
92156 ** its query plan, then checking to see if that query plan uses any
92157 ** other FROM clause terms that are notReady. If no notReady terms are
92158 ** used then the "optimal" query plan works.
92159 **
92160 ** The second loop iteration is only performed if no optimal scan
92161 ** strategies were found by the first loop. This 2nd iteration is used to
92162 ** search for the lowest cost scan overall.
92163 **
92164 ** Previous versions of SQLite performed only the second iteration -
92165 ** the next outermost loop was always that with the lowest overall
92166 ** cost. However, this meant that SQLite could select the wrong plan
92167 ** for scripts such as the following:
@@ -91961,13 +92175,12 @@
92175 ** However, since the cost of a linear scan through table t2 is the same
92176 ** as the cost of a linear scan through table t1, a simple greedy
92177 ** algorithm may choose to use t2 for the outer loop, which is a much
92178 ** costlier approach.
92179 */
92180 for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){
92181 Bitmask mask; /* Mask of tables not yet ready */
 
92182 for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
92183 int doNotReorder; /* True if this table should not be reordered */
92184 WhereCost sCost; /* Cost information from best[Virtual]Index() */
92185 ExprList *pOrderBy; /* ORDER BY clause for index to optimize */
92186
@@ -91976,10 +92189,11 @@
92189 m = getMask(pMaskSet, pTabItem->iCursor);
92190 if( (m & notReady)==0 ){
92191 if( j==iFrom ) iFrom++;
92192 continue;
92193 }
92194 mask = (isOptimal ? m : notReady);
92195 pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
92196
92197 assert( pTabItem->pTab );
92198 #ifndef SQLITE_OMIT_VIRTUALTABLE
92199 if( IsVirtual(pTabItem->pTab) ){
@@ -91991,12 +92205,15 @@
92205 bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost);
92206 }
92207 assert( isOptimal || (sCost.used&notReady)==0 );
92208
92209 if( (sCost.used&notReady)==0
92210 && (bestJ<0 || sCost.rCost<bestPlan.rCost
92211 || (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow))
92212 ){
92213 WHERETRACE(("... best so far with cost=%g and nRow=%g\n",
92214 sCost.rCost, sCost.nRow));
92215 bestPlan = sCost;
92216 bestJ = j;
92217 }
92218 if( doNotReorder ) break;
92219 }
@@ -92118,10 +92335,12 @@
92335 #endif
92336 if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
92337 && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
92338 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
92339 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
92340 testcase( pTab->nCol==BMS-1 );
92341 testcase( pTab->nCol==BMS );
92342 if( !pWInfo->okOnePass && pTab->nCol<BMS ){
92343 Bitmask b = pTabItem->colUsed;
92344 int n = 0;
92345 for(; b; b=b>>1, n++){}
92346 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
@@ -92297,11 +92516,11 @@
92516 ){
92517 int ws = pLevel->plan.wsFlags;
92518 if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
92519 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
92520 }
92521 if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
92522 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
92523 }
92524 }
92525
92526 /* If this scan uses an index, make code substitutions to read data
@@ -92345,14 +92564,12 @@
92564 }
92565 }
92566
92567 /* Final cleanup
92568 */
92569 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
92570 whereInfoFree(db, pWInfo);
 
 
92571 return;
92572 }
92573
92574 /************** End of where.c ***********************************************/
92575 /************** Begin file parse.c *******************************************/
92576
+1 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.6.23"
111111
#define SQLITE_VERSION_NUMBER 3006023
112
-#define SQLITE_SOURCE_ID "2010-04-07 20:32:19 e388fe8be878c80ef0bfd1699a7268cdb22cb3c6"
112
+#define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -3883,12 +3883,10 @@
38833883
** ^For the purposes of this API, a transaction is said to have been
38843884
** rolled back if an explicit "ROLLBACK" statement is executed, or
38853885
** an error or constraint causes an implicit rollback to occur.
38863886
** ^The rollback callback is not invoked if a transaction is
38873887
** automatically rolled back because the database connection is closed.
3888
-** ^The rollback callback is not invoked if a transaction is
3889
-** rolled back because a commit callback returned non-zero.
38903888
**
38913889
** See also the [sqlite3_update_hook()] interface.
38923890
*/
38933891
SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
38943892
SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
38953893
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.6.23"
111 #define SQLITE_VERSION_NUMBER 3006023
112 #define SQLITE_SOURCE_ID "2010-04-07 20:32:19 e388fe8be878c80ef0bfd1699a7268cdb22cb3c6"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -3883,12 +3883,10 @@
3883 ** ^For the purposes of this API, a transaction is said to have been
3884 ** rolled back if an explicit "ROLLBACK" statement is executed, or
3885 ** an error or constraint causes an implicit rollback to occur.
3886 ** ^The rollback callback is not invoked if a transaction is
3887 ** automatically rolled back because the database connection is closed.
3888 ** ^The rollback callback is not invoked if a transaction is
3889 ** rolled back because a commit callback returned non-zero.
3890 **
3891 ** See also the [sqlite3_update_hook()] interface.
3892 */
3893 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
3894 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
3895
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.6.23"
111 #define SQLITE_VERSION_NUMBER 3006023
112 #define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -3883,12 +3883,10 @@
3883 ** ^For the purposes of this API, a transaction is said to have been
3884 ** rolled back if an explicit "ROLLBACK" statement is executed, or
3885 ** an error or constraint causes an implicit rollback to occur.
3886 ** ^The rollback callback is not invoked if a transaction is
3887 ** automatically rolled back because the database connection is closed.
 
 
3888 **
3889 ** See also the [sqlite3_update_hook()] interface.
3890 */
3891 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
3892 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
3893

Keyboard Shortcuts

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