/ Check-in [982b753d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update this branch with the latest changes from the trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tempfiles-lazy-open
Files: files | file ages | folders
SHA1: 982b753d0b6a3ed9fba33ed41523b2cd42280276
User & Date: dan 2016-04-11 09:39:25
Context
2016-04-11
18:07
Fixes for OOM and IO error handling with temp file databases. check-in: 4eb06e84 user: dan tags: tempfiles-lazy-open
09:39
Update this branch with the latest changes from the trunk. check-in: 982b753d user: dan tags: tempfiles-lazy-open
01:43
Back off of the parser optimization in the previous check-in, slightly, to preserve some backwards compatibility regarding some undocumented behavior in the '#AAA' style query parameter. check-in: ef1966c2 user: drh tags: trunk
2016-04-06
18:20
For a pager backed by a temp file, store the main journal in memory until it is at least sqlite3_config.nStmtSpill bytes in size. Prevent the backup API from being used to change the page-size of a temp file. check-in: 84c55701 user: dan tags: tempfiles-lazy-open
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  8135   8135     assert( pCur->eState==CURSOR_VALID );
  8136   8136     assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
  8137   8137   
  8138   8138     iCellDepth = pCur->iPage;
  8139   8139     iCellIdx = pCur->aiIdx[iCellDepth];
  8140   8140     pPage = pCur->apPage[iCellDepth];
  8141   8141     pCell = findCell(pPage, iCellIdx);
         8142  +
         8143  +  /* If the bPreserve flag is set to true, then the cursor position must
         8144  +  ** be preserved following this delete operation. If the current delete
         8145  +  ** will cause a b-tree rebalance, then this is done by saving the cursor
         8146  +  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
         8147  +  ** returning. 
         8148  +  **
         8149  +  ** Or, if the current delete will not cause a rebalance, then the cursor
         8150  +  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
         8151  +  ** before or after the deleted entry. In this case set bSkipnext to true.  */
         8152  +  if( bPreserve ){
         8153  +    if( !pPage->leaf 
         8154  +     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
         8155  +    ){
         8156  +      /* A b-tree rebalance will be required after deleting this entry.
         8157  +      ** Save the cursor key.  */
         8158  +      rc = saveCursorKey(pCur);
         8159  +      if( rc ) return rc;
         8160  +    }else{
         8161  +      bSkipnext = 1;
         8162  +    }
         8163  +  }
  8142   8164   
  8143   8165     /* If the page containing the entry to delete is not a leaf page, move
  8144   8166     ** the cursor to the largest entry in the tree that is smaller than
  8145   8167     ** the entry being deleted. This cell will replace the cell being deleted
  8146   8168     ** from the internal node. The 'previous' entry is used for this instead
  8147   8169     ** of the 'next' entry, as the previous entry is always a part of the
  8148   8170     ** sub-tree headed by the child page of the cell being deleted. This makes
................................................................................
  8162   8184   
  8163   8185     /* If this is a delete operation to remove a row from a table b-tree,
  8164   8186     ** invalidate any incrblob cursors open on the row being deleted.  */
  8165   8187     if( pCur->pKeyInfo==0 ){
  8166   8188       invalidateIncrblobCursors(p, pCur->info.nKey, 0);
  8167   8189     }
  8168   8190   
  8169         -  /* If the bPreserve flag is set to true, then the cursor position must
  8170         -  ** be preserved following this delete operation. If the current delete
  8171         -  ** will cause a b-tree rebalance, then this is done by saving the cursor
  8172         -  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
  8173         -  ** returning. 
  8174         -  **
  8175         -  ** Or, if the current delete will not cause a rebalance, then the cursor
  8176         -  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
  8177         -  ** before or after the deleted entry. In this case set bSkipnext to true.  */
  8178         -  if( bPreserve ){
  8179         -    if( !pPage->leaf 
  8180         -     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
  8181         -    ){
  8182         -      /* A b-tree rebalance will be required after deleting this entry.
  8183         -      ** Save the cursor key.  */
  8184         -      rc = saveCursorKey(pCur);
  8185         -      if( rc ) return rc;
  8186         -    }else{
  8187         -      bSkipnext = 1;
  8188         -    }
  8189         -  }
  8190         -
  8191   8191     /* Make the page containing the entry to be deleted writable. Then free any
  8192   8192     ** overflow pages associated with the entry and finally remove the cell
  8193   8193     ** itself from within the page.  */
  8194   8194     rc = sqlite3PagerWrite(pPage->pDbPage);
  8195   8195     if( rc ) return rc;
  8196   8196     rc = clearCell(pPage, pCell, &szCell);
  8197   8197     dropCell(pPage, iCellIdx, szCell, &rc);

Changes to src/delete.c.

   139    139     SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
   140    140     Select *pSelect = NULL;      /* Complete SELECT tree */
   141    141   
   142    142     /* Check that there isn't an ORDER BY without a LIMIT clause.
   143    143     */
   144    144     if( pOrderBy && (pLimit == 0) ) {
   145    145       sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
   146         -    goto limit_where_cleanup_2;
          146  +    goto limit_where_cleanup;
   147    147     }
   148    148   
   149    149     /* We only need to generate a select expression if there
   150    150     ** is a limit/offset term to enforce.
   151    151     */
   152    152     if( pLimit == 0 ) {
   153    153       /* if pLimit is null, pOffset will always be null as well. */
................................................................................
   161    161     ** becomes:
   162    162     **   DELETE FROM table_a WHERE rowid IN ( 
   163    163     **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   164    164     **   );
   165    165     */
   166    166   
   167    167     pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   168         -  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
          168  +  if( pSelectRowid == 0 ) goto limit_where_cleanup;
   169    169     pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
   170         -  if( pEList == 0 ) goto limit_where_cleanup_2;
          170  +  if( pEList == 0 ) goto limit_where_cleanup;
   171    171   
   172    172     /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
   173    173     ** and the SELECT subtree. */
   174    174     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
   175    175     if( pSelectSrc == 0 ) {
   176    176       sqlite3ExprListDelete(pParse->db, pEList);
   177         -    goto limit_where_cleanup_2;
          177  +    goto limit_where_cleanup;
   178    178     }
   179    179   
   180    180     /* generate the SELECT expression tree. */
   181    181     pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
   182    182                                pOrderBy,0,pLimit,pOffset);
   183    183     if( pSelect == 0 ) return 0;
   184    184   
   185    185     /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
   186    186     pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   187         -  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
   188         -  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
   189         -  if( pInClause == 0 ) goto limit_where_cleanup_1;
   190         -
   191         -  pInClause->x.pSelect = pSelect;
   192         -  pInClause->flags |= EP_xIsSelect;
   193         -  sqlite3ExprSetHeightAndFlags(pParse, pInClause);
          187  +  pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0) : 0;
          188  +  sqlite3PExprAddSelect(pParse, pInClause, pSelect);
   194    189     return pInClause;
   195    190   
   196         -  /* something went wrong. clean up anything allocated. */
   197         -limit_where_cleanup_1:
   198         -  sqlite3SelectDelete(pParse->db, pSelect);
   199         -  return 0;
   200         -
   201         -limit_where_cleanup_2:
          191  +limit_where_cleanup:
   202    192     sqlite3ExprDelete(pParse->db, pWhere);
   203    193     sqlite3ExprListDelete(pParse->db, pOrderBy);
   204    194     sqlite3ExprDelete(pParse->db, pLimit);
   205    195     sqlite3ExprDelete(pParse->db, pOffset);
   206    196     return 0;
   207    197   }
   208    198   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */

Changes to src/expr.c.

   557    557       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   558    558     }
   559    559     if( p ) {
   560    560       sqlite3ExprCheckHeight(pParse, p->nHeight);
   561    561     }
   562    562     return p;
   563    563   }
          564  +
          565  +/*
          566  +** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
          567  +** do a memory allocation failure) then delete the pSelect object.
          568  +*/
          569  +void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
          570  +  if( pExpr ){
          571  +    pExpr->x.pSelect = pSelect;
          572  +    ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
          573  +    sqlite3ExprSetHeightAndFlags(pParse, pExpr);
          574  +  }else{
          575  +    assert( pParse->db->mallocFailed );
          576  +    sqlite3SelectDelete(pParse->db, pSelect);
          577  +  }
          578  +}
          579  +
   564    580   
   565    581   /*
   566    582   ** If the expression is always either TRUE or FALSE (respectively),
   567    583   ** then return 1.  If one cannot determine the truth value of the
   568    584   ** expression at compile-time return 0.
   569    585   **
   570    586   ** This is an optimization.  If is OK to return 0 here even if

Changes to src/memjournal.c.

    90     90   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
    91     91     if( (iAmt+iOfst)>p->endpoint.iOffset ){
    92     92       return SQLITE_IOERR_SHORT_READ;
    93     93     }
    94     94   #endif
    95     95   
    96     96     assert( (iAmt+iOfst)<=p->endpoint.iOffset );
           97  +  assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
    97     98     if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
    98     99       sqlite3_int64 iOff = 0;
    99    100       for(pChunk=p->pFirst; 
   100    101           ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
   101    102           pChunk=pChunk->pNext
   102    103       ){
   103    104         iOff += p->nChunkSize;
   104    105       }
   105    106     }else{
   106    107       pChunk = p->readpoint.pChunk;
          108  +    assert( pChunk!=0 );
   107    109     }
   108    110   
   109    111     iChunkOffset = (int)(iOfst%p->nChunkSize);
   110    112     do {
   111    113       int iSpace = p->nChunkSize - iChunkOffset;
   112    114       int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
   113    115       memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
   114    116       zOut += nCopy;
   115    117       nRead -= iSpace;
   116    118       iChunkOffset = 0;
   117    119     } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
   118         -  p->readpoint.iOffset = iOfst+iAmt;
          120  +  p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
   119    121     p->readpoint.pChunk = pChunk;
   120    122   
   121    123     return SQLITE_OK;
   122    124   }
   123    125   
   124    126   /*
   125    127   ** Free the list of FileChunk structures headed at MemJournal.pFirst.

Changes to src/os_unix.c.

  1498   1498   ** routine to lower a locking level.
  1499   1499   */
  1500   1500   static int unixLock(sqlite3_file *id, int eFileLock){
  1501   1501     /* The following describes the implementation of the various locks and
  1502   1502     ** lock transitions in terms of the POSIX advisory shared and exclusive
  1503   1503     ** lock primitives (called read-locks and write-locks below, to avoid
  1504   1504     ** confusion with SQLite lock names). The algorithms are complicated
  1505         -  ** slightly in order to be compatible with windows systems simultaneously
         1505  +  ** slightly in order to be compatible with Windows95 systems simultaneously
  1506   1506     ** accessing the same database file, in case that is ever required.
  1507   1507     **
  1508   1508     ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
  1509   1509     ** byte', each single bytes at well known offsets, and the 'shared byte
  1510   1510     ** range', a range of 510 bytes at a well known offset.
  1511   1511     **
  1512   1512     ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
  1513         -  ** byte'.  If this is successful, a random byte from the 'shared byte
  1514         -  ** range' is read-locked and the lock on the 'pending byte' released.
         1513  +  ** byte'.  If this is successful, 'shared byte range' is read-locked
         1514  +  ** and the lock on the 'pending byte' released.  (Legacy note:  When
         1515  +  ** SQLite was first developed, Windows95 systems were still very common,
         1516  +  ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
         1517  +  ** single randomly selected by from the 'shared byte range' is locked.
         1518  +  ** Windows95 is now pretty much extinct, but this work-around for the
         1519  +  ** lack of shared-locks on Windows95 lives on, for backwards
         1520  +  ** compatibility.)
  1515   1521     **
  1516   1522     ** A process may only obtain a RESERVED lock after it has a SHARED lock.
  1517   1523     ** A RESERVED lock is implemented by grabbing a write-lock on the
  1518   1524     ** 'reserved byte'. 
  1519   1525     **
  1520   1526     ** A process may only obtain a PENDING lock after it has obtained a
  1521   1527     ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
................................................................................
  1526   1532     ** after a crash.
  1527   1533     **
  1528   1534     ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
  1529   1535     ** implemented by obtaining a write-lock on the entire 'shared byte
  1530   1536     ** range'. Since all other locks require a read-lock on one of the bytes
  1531   1537     ** within this range, this ensures that no other locks are held on the
  1532   1538     ** database. 
  1533         -  **
  1534         -  ** The reason a single byte cannot be used instead of the 'shared byte
  1535         -  ** range' is that some versions of windows do not support read-locks. By
  1536         -  ** locking a random byte from a range, concurrent SHARED locks may exist
  1537         -  ** even if the locking primitive used is always a write-lock.
  1538   1539     */
  1539   1540     int rc = SQLITE_OK;
  1540   1541     unixFile *pFile = (unixFile*)id;
  1541   1542     unixInodeInfo *pInode;
  1542   1543     struct flock lock;
  1543   1544     int tErrno = 0;
  1544   1545   

Changes to src/parse.y.

   867    867     Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
   868    868     spanSet(&A,&X,&Z); /*A-overwrites-X*/
   869    869     A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
   870    870   }
   871    871   term(A) ::= INTEGER|FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   872    872   term(A) ::= STRING(X).             {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   873    873   expr(A) ::= VARIABLE(X).     {
   874         -  Token t = X; /*A-overwrites-X*/
   875         -  if( t.n>=2 && t.z[0]=='#' && sqlite3Isdigit(t.z[1]) ){
          874  +  if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){
          875  +    spanExpr(&A, pParse, TK_VARIABLE, X);
          876  +    sqlite3ExprAssignVarNumber(pParse, A.pExpr);
          877  +  }else{
   876    878       /* When doing a nested parse, one can include terms in an expression
   877    879       ** that look like this:   #1 #2 ...  These terms refer to registers
   878    880       ** in the virtual machine.  #N is the N-th register. */
          881  +    Token t = X; /*A-overwrites-X*/
          882  +    assert( t.n>=2 );
   879    883       spanSet(&A, &t, &t);
   880    884       if( pParse->nested==0 ){
   881    885         sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
   882    886         A.pExpr = 0;
   883    887       }else{
   884    888         A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &t);
   885    889         if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable);
   886    890       }
   887         -  }else{
   888         -    spanExpr(&A, pParse, TK_VARIABLE, t);
   889         -    sqlite3ExprAssignVarNumber(pParse, A.pExpr);
   890    891     }
   891    892   }
   892    893   expr(A) ::= expr(A) COLLATE ids(C). {
   893    894     A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1);
   894    895     A.zEnd = &C.z[C.n];
   895    896   }
   896    897   %ifndef SQLITE_OMIT_CAST
................................................................................
  1118   1119         exprNot(pParse, N, &A);
  1119   1120       }
  1120   1121       A.zEnd = &E.z[E.n];
  1121   1122     }
  1122   1123     expr(A) ::= LP(B) select(X) RP(E). {
  1123   1124       spanSet(&A,&B,&E); /*A-overwrites-B*/
  1124   1125       A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
  1125         -    if( A.pExpr ){
  1126         -      A.pExpr->x.pSelect = X;
  1127         -      ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
  1128         -      sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
  1129         -    }else{
  1130         -      sqlite3SelectDelete(pParse->db, X);
  1131         -    }
         1126  +    sqlite3PExprAddSelect(pParse, A.pExpr, X);
  1132   1127     }
  1133   1128     expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
  1134   1129       A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
  1135         -    if( A.pExpr ){
  1136         -      A.pExpr->x.pSelect = Y;
  1137         -      ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
  1138         -      sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
  1139         -    }else{
  1140         -      sqlite3SelectDelete(pParse->db, Y);
  1141         -    }
         1130  +    sqlite3PExprAddSelect(pParse, A.pExpr, Y);
  1142   1131       exprNot(pParse, N, &A);
  1143   1132       A.zEnd = &E.z[E.n];
  1144   1133     }
  1145   1134     expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z). [IN] {
  1146   1135       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
         1136  +    Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
  1147   1137       A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
  1148         -    if( A.pExpr ){
  1149         -      A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
  1150         -      ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery);
  1151         -      sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
  1152         -    }else{
  1153         -      sqlite3SrcListDelete(pParse->db, pSrc);
  1154         -    }
         1138  +    sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
  1155   1139       exprNot(pParse, N, &A);
  1156   1140       A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
  1157   1141     }
  1158   1142     expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
  1159   1143       Expr *p;
  1160   1144       spanSet(&A,&B,&E); /*A-overwrites-B*/
  1161   1145       p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
  1162         -    if( p ){
  1163         -      p->x.pSelect = Y;
  1164         -      ExprSetProperty(p, EP_xIsSelect|EP_Subquery);
  1165         -      sqlite3ExprSetHeightAndFlags(pParse, p);
  1166         -    }else{
  1167         -      sqlite3SelectDelete(pParse->db, Y);
  1168         -    }
         1146  +    sqlite3PExprAddSelect(pParse, p, Y);
  1169   1147     }
  1170   1148   %endif SQLITE_OMIT_SUBQUERY
  1171   1149   
  1172   1150   /* CASE expressions */
  1173   1151   expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
  1174   1152     spanSet(&A,&C,&E);  /*A-overwrites-C*/
  1175   1153     A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0);

Changes to src/sqlite.h.in.

  8077   8077     sqlite3_snapshot **ppSnapshot
  8078   8078   );
  8079   8079   
  8080   8080   /*
  8081   8081   ** CAPI3REF: Start a read transaction on an historical snapshot
  8082   8082   ** EXPERIMENTAL
  8083   8083   **
  8084         -** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the
  8085         -** read transaction that is currently open on schema S of
  8086         -** [database connection] D so that it refers to historical [snapshot] P.
         8084  +** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a
         8085  +** read transaction for schema S of
         8086  +** [database connection] D such that the read transaction
         8087  +** refers to historical [snapshot] P, rather than the most
         8088  +** recent change to the database.
  8087   8089   ** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success
  8088   8090   ** or an appropriate [error code] if it fails.
  8089   8091   **
  8090   8092   ** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be
  8091         -** the first operation, apart from other sqlite3_snapshot_open() calls,
  8092         -** following the [BEGIN] that starts a new read transaction.
  8093         -** ^A [snapshot] will fail to open if it has been overwritten by a 
         8093  +** the first operation following the [BEGIN] that takes the schema S
         8094  +** out of [autocommit mode].
         8095  +** ^In other words, schema S must not currently be in
         8096  +** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the
         8097  +** database connection D must be out of [autocommit mode].
         8098  +** ^A [snapshot] will fail to open if it has been overwritten by a
  8094   8099   ** [checkpoint].
  8095         -** ^A [snapshot] will fail to open if the database connection D has not
  8096         -** previously completed at least one read operation against the database 
  8097         -** file.  (Hint: Run "[PRAGMA application_id]" against a newly opened
         8100  +** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
         8101  +** database connection D does not know that the database file for
         8102  +** schema S is in [WAL mode].  A database connection might not know
         8103  +** that the database file is in [WAL mode] if there has been no prior
         8104  +** I/O on that database connection, or if the database entered [WAL mode] 
         8105  +** after the most recent I/O on the database connection.)^
         8106  +** (Hint: Run "[PRAGMA application_id]" against a newly opened
  8098   8107   ** database connection in order to make it ready to use snapshots.)
  8099   8108   **
  8100   8109   ** The [sqlite3_snapshot_open()] interface is only available when the
  8101   8110   ** SQLITE_ENABLE_SNAPSHOT compile-time option is used.
  8102   8111   */
  8103   8112   SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
  8104   8113     sqlite3 *db,

Changes to src/sqliteInt.h.

  3445   3445   #ifdef SQLITE_DEBUG
  3446   3446   int sqlite3NoTempsInRange(Parse*,int,int);
  3447   3447   #endif
  3448   3448   Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  3449   3449   Expr *sqlite3Expr(sqlite3*,int,const char*);
  3450   3450   void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  3451   3451   Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
         3452  +void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  3452   3453   Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  3453   3454   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  3454   3455   void sqlite3ExprAssignVarNumber(Parse*, Expr*);
  3455   3456   void sqlite3ExprDelete(sqlite3*, Expr*);
  3456   3457   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3457   3458   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3458   3459   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);

Changes to src/util.c.

  1444   1444   ** Convert a LogEst into an integer.
  1445   1445   **
  1446   1446   ** Note that this routine is only used when one or more of various
  1447   1447   ** non-standard compile-time options is enabled.
  1448   1448   */
  1449   1449   u64 sqlite3LogEstToInt(LogEst x){
  1450   1450     u64 n;
  1451         -  if( x<10 ) return 1;
  1452   1451     n = x%10;
  1453   1452     x /= 10;
  1454   1453     if( n>=5 ) n -= 2;
  1455   1454     else if( n>=1 ) n -= 1;
  1456   1455   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1457   1456       defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1458   1457     if( x>60 ) return (u64)LARGEST_INT64;

Changes to src/vdbe.c.

  2751   2751           len -= pRec->u.nZero;
  2752   2752         }
  2753   2753       }
  2754   2754       nData += len;
  2755   2755       testcase( serial_type==127 );
  2756   2756       testcase( serial_type==128 );
  2757   2757       nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
  2758         -  }while( (--pRec)>=pData0 );
         2758  +    if( pRec==pData0 ) break;
         2759  +    pRec--;
         2760  +  }while(1);
  2759   2761   
  2760   2762     /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
  2761   2763     ** which determines the total number of bytes in the header. The varint
  2762   2764     ** value is the size of the header in bytes including the size varint
  2763   2765     ** itself. */
  2764   2766     testcase( nHdr==126 );
  2765   2767     testcase( nHdr==127 );

Changes to src/vdbeaux.c.

   787    787   
   788    788   static void vdbeFreeOpArray(sqlite3 *, Op *, int);
   789    789   
   790    790   /*
   791    791   ** Delete a P4 value if necessary.
   792    792   */
   793    793   static void freeP4(sqlite3 *db, int p4type, void *p4){
   794         -  if( p4 ){
   795         -    assert( db );
   796         -    switch( p4type ){
   797         -      case P4_FUNCCTX: {
   798         -        freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
   799         -        /* Fall through into the next case */
   800         -      }
   801         -      case P4_REAL:
   802         -      case P4_INT64:
   803         -      case P4_DYNAMIC:
   804         -      case P4_INTARRAY: {
   805         -        sqlite3DbFree(db, p4);
   806         -        break;
   807         -      }
   808         -      case P4_KEYINFO: {
   809         -        if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
   810         -        break;
   811         -      }
          794  +  assert( db );
          795  +  switch( p4type ){
          796  +    case P4_FUNCCTX: {
          797  +      freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc);
          798  +      /* Fall through into the next case */
          799  +    }
          800  +    case P4_REAL:
          801  +    case P4_INT64:
          802  +    case P4_DYNAMIC:
          803  +    case P4_INTARRAY: {
          804  +      sqlite3DbFree(db, p4);
          805  +      break;
          806  +    }
          807  +    case P4_KEYINFO: {
          808  +      if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
          809  +      break;
          810  +    }
   812    811   #ifdef SQLITE_ENABLE_CURSOR_HINTS
   813         -      case P4_EXPR: {
   814         -        sqlite3ExprDelete(db, (Expr*)p4);
   815         -        break;
   816         -      }
   817         -#endif
   818         -      case P4_MPRINTF: {
   819         -        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
   820         -        break;
   821         -      }
   822         -      case P4_FUNCDEF: {
   823         -        freeEphemeralFunction(db, (FuncDef*)p4);
   824         -        break;
   825         -      }
   826         -      case P4_MEM: {
   827         -        if( db->pnBytesFreed==0 ){
   828         -          sqlite3ValueFree((sqlite3_value*)p4);
   829         -        }else{
   830         -          Mem *p = (Mem*)p4;
   831         -          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
   832         -          sqlite3DbFree(db, p);
   833         -        }
   834         -        break;
   835         -      }
   836         -      case P4_VTAB : {
   837         -        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
   838         -        break;
   839         -      }
          812  +    case P4_EXPR: {
          813  +      sqlite3ExprDelete(db, (Expr*)p4);
          814  +      break;
          815  +    }
          816  +#endif
          817  +    case P4_MPRINTF: {
          818  +      if( db->pnBytesFreed==0 ) sqlite3_free(p4);
          819  +      break;
          820  +    }
          821  +    case P4_FUNCDEF: {
          822  +      freeEphemeralFunction(db, (FuncDef*)p4);
          823  +      break;
          824  +    }
          825  +    case P4_MEM: {
          826  +      if( db->pnBytesFreed==0 ){
          827  +        sqlite3ValueFree((sqlite3_value*)p4);
          828  +      }else{
          829  +        Mem *p = (Mem*)p4;
          830  +        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
          831  +        sqlite3DbFree(db, p);
          832  +      }
          833  +      break;
          834  +    }
          835  +    case P4_VTAB : {
          836  +      if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
          837  +      break;
   840    838       }
   841    839     }
   842    840   }
   843    841   
   844    842   /*
   845    843   ** Free the space allocated for aOp and any p4 values allocated for the
   846    844   ** opcodes contained within. If aOp is not NULL it is assumed to contain 

Changes to src/where.c.

  1647   1647   
  1648   1648   #ifdef WHERETRACE_ENABLED
  1649   1649   /*
  1650   1650   ** Print a WhereLoop object for debugging purposes
  1651   1651   */
  1652   1652   static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
  1653   1653     WhereInfo *pWInfo = pWC->pWInfo;
  1654         -  int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
         1654  +  int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
  1655   1655     struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
  1656   1656     Table *pTab = pItem->pTab;
         1657  +  Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
  1657   1658     sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
  1658         -                     p->iTab, nb, p->maskSelf, nb, p->prereq);
         1659  +                     p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
  1659   1660     sqlite3DebugPrintf(" %12s",
  1660   1661                        pItem->zAlias ? pItem->zAlias : pTab->zName);
  1661   1662     if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
  1662   1663       const char *zName;
  1663   1664       if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
  1664   1665         if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
  1665   1666           int i = sqlite3Strlen30(zName) - 1;

Changes to src/wherecode.c.

   937    937               sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
   938    938             }
   939    939             pCompare->pLeft = 0;
   940    940             sqlite3ExprDelete(db, pCompare);
   941    941           }
   942    942         }
   943    943       }
   944         -    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
          944  +    /* These registers need to be preserved in case there is an IN operator
          945  +    ** loop.  So we could deallocate the registers here (and potentially
          946  +    ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0.  But it seems
          947  +    ** simpler and safer to simply not reuse the registers.
          948  +    **
          949  +    **    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
          950  +    */
   945    951       sqlite3ExprCachePop(pParse);
   946    952     }else
   947    953   #endif /* SQLITE_OMIT_VIRTUALTABLE */
   948    954   
   949    955     if( (pLoop->wsFlags & WHERE_IPK)!=0
   950    956      && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
   951    957     ){

Changes to test/bestindex1.test.

   160    160       0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   161    161     }
   162    162   
   163    163     do_eqp_test 2.2.$mode.6 { 
   164    164       SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid
   165    165     } $plan($mode)
   166    166   }
          167  +
          168  +# 2016-04-09.
          169  +# Demonstrate a register overwrite problem when using two virtual
          170  +# tables where the outer loop uses the IN operator.
          171  +#
          172  +set G(collist) [list PrimaryKey flagA columnA]
          173  +set G(cols) [join $G(collist) ,]
          174  +set G(nulls) "NULL"
          175  +
          176  +proc vtab_command {method args} {
          177  +  global G
          178  +
          179  +  switch -- $method {
          180  +    xConnect {
          181  +      return "CREATE TABLE t1($G(cols))"
          182  +    }
          183  +
          184  +    xBestIndex {
          185  +      set clist [lindex $args 0]
          186  +      #puts $clist
          187  +      set W [list]
          188  +      set U [list]
          189  +
          190  +      set i 0
          191  +      for {set idx 0} {$idx < [llength $clist]} {incr idx} {
          192  +        array set c [lindex $clist $idx]
          193  +        if {$c(op)=="eq" && $c(usable)} {
          194  +          lappend W "[lindex $G(collist) $c(column)] = %$i%"
          195  +          lappend U use $idx
          196  +          incr i
          197  +        }
          198  +      }
          199  +
          200  +      if {$W==""} {
          201  +        set sql "SELECT rowid, * FROM t1"
          202  +      } else {
          203  +        set sql "SELECT rowid, * FROM t1 WHERE [join $W { AND }]"
          204  +      }
          205  +
          206  +      return [concat [list idxstr $sql] $U]
          207  +    }
          208  +
          209  +    xFilter {
          210  +      foreach {idxnum idxstr vals} $args {}
          211  +
          212  +      set map [list]
          213  +      for {set i 0} {$i < [llength $vals]} {incr i} {
          214  +        lappend map "%$i%" 
          215  +        set v [lindex $vals $i]
          216  +        if {[string is integer $v]} { 
          217  +          lappend map $v 
          218  +        } else {
          219  +          lappend map "'$v'"
          220  +        }
          221  +      }
          222  +      set sql [string map $map $idxstr]
          223  +
          224  +      #puts "SQL: $sql"
          225  +      return [list sql $sql]
          226  +    }
          227  +  }
          228  +
          229  +  return {}
          230  +}
          231  +
          232  +db close
          233  +forcedelete test.db
          234  +sqlite3 db test.db
          235  +register_tcl_module db
          236  +
          237  +do_execsql_test 3.1 "
          238  +  CREATE TABLE t1($G(cols));
          239  +  INSERT INTO t1 VALUES(1, 0, 'ValueA');
          240  +  INSERT INTO t1 VALUES(2, 0, 'ValueA');
          241  +  INSERT INTO t1 VALUES(3, 0, 'ValueB');
          242  +  INSERT INTO t1 VALUES(4, 0, 'ValueB');
          243  +"
          244  +
          245  +do_execsql_test 3.2 {
          246  +  CREATE VIRTUAL TABLE VirtualTableA USING tcl(vtab_command);
          247  +  CREATE VIRTUAL TABLE VirtualTableB USING tcl(vtab_command);
          248  +}
          249  +
          250  +do_execsql_test 3.3 { SELECT primarykey FROM VirtualTableA } {1 2 3 4}
          251  +
          252  +do_execsql_test 3.4 {
          253  +  SELECT * FROM 
          254  +  VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey
          255  +  WHERE a.ColumnA IN ('ValueA', 'ValueB') AND a.FlagA=0
          256  +} {
          257  +  1 0 ValueA 1 0 ValueA
          258  +  2 0 ValueA 2 0 ValueA
          259  +  3 0 ValueB 3 0 ValueB
          260  +  4 0 ValueB 4 0 ValueB
          261  +}
          262  +
          263  +do_execsql_test 3.5 {
          264  +  SELECT * FROM 
          265  +  VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey
          266  +  WHERE a.FlagA=0 AND a.ColumnA IN ('ValueA', 'ValueB') 
          267  +} {
          268  +  1 0 ValueA 1 0 ValueA
          269  +  2 0 ValueA 2 0 ValueA
          270  +  3 0 ValueB 3 0 ValueB
          271  +  4 0 ValueB 4 0 ValueB
          272  +}
          273  +
   167    274   
   168    275   finish_test

Changes to test/delete4.test.

   135    135     INSERT INTO t4 VALUES(14, 'abcde','xyzzy');
   136    136     CREATE INDEX idx_t4_3 ON t4 (col0);
   137    137     CREATE INDEX idx_t4_0 ON t4 (col1, col0);
   138    138     DELETE FROM t4 WHERE col0=69 OR col0>7;
   139    139     PRAGMA integrity_check;
   140    140   } {ok}
   141    141   
   142         -
   143         -
          142  +# 2016-04-09
          143  +# Ticket https://sqlite.org/src/info/a306e56ff68b8fa5
          144  +# Failure to completely delete when reverse_unordered_selects is
          145  +# engaged.
          146  +#
          147  +db close
          148  +forcedelete test.db
          149  +sqlite3 db test.db
          150  +do_execsql_test 5.0 {
          151  +  PRAGMA page_size=1024;
          152  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
          153  +  CREATE INDEX x1 ON t1(b, c);
          154  +  INSERT INTO t1(a,b,c) VALUES(1, 1, zeroblob(80));
          155  +  INSERT INTO t1(a,b,c) SELECT a+1, 1, c FROM t1;
          156  +  INSERT INTO t1(a,b,c) SELECT a+2, 1, c FROM t1;
          157  +  INSERT INTO t1(a,b,c) SELECT a+10, 2, c FROM t1 WHERE b=1;
          158  +  INSERT INTO t1(a,b,c) SELECT a+20, 3, c FROM t1 WHERE b=1;
          159  +  PRAGMA reverse_unordered_selects = ON;
          160  +  DELETE FROM t1 WHERE b=2;
          161  +  SELECT a FROM t1 WHERE b=2;
          162  +} {}
   144    163   
   145    164   finish_test

Changes to test/savepoint7.test.

    90     90           INSERT INTO t2 VALUES($a,$b,$c);
    91     91           ROLLBACK TO x2;
    92     92         }
    93     93       }
    94     94     } msg]
    95     95     list $rc $msg [db eval {SELECT * FROM t2}]
    96     96   } {1 {abort due to ROLLBACK} {}}
           97  +
           98  +# Ticket: https://www.sqlite.org/src/tktview/7f7f8026eda387d544b
           99  +# Segfault in the in-memory journal logic triggered by a tricky
          100  +# combination of SAVEPOINT operations.
          101  +#
          102  +unset -nocomplain i
          103  +for {set i 248} {$i<=253} {incr i} {
          104  +  do_test savepoint7-3.$i {
          105  +    db close
          106  +    forcedelete test.db
          107  +    sqlite3 db test.db
          108  +    db eval {
          109  +      PRAGMA page_size=1024;
          110  +      PRAGMA temp_store=MEMORY;
          111  +      BEGIN;
          112  +      CREATE TABLE t1(x INTEGER PRIMARY KEY, y TEXT);
          113  +      WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<$::i)
          114  +      INSERT INTO t1(x,y) SELECT x*10, printf('%04d%.800c',x,'*') FROM c;
          115  +      SAVEPOINT one;
          116  +        SELECT count(*) FROM t1;
          117  +        WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<$::i)
          118  +        INSERT INTO t1(x,y) SELECT x*10+1, printf('%04d%.800c',x,'*') FROM c;
          119  +      ROLLBACK TO one;
          120  +        SELECT count(*) FROM t1;
          121  +        SAVEPOINT twoB;
          122  +          WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<10)
          123  +          INSERT INTO t1(x,y) SELECT x*10+2, printf('%04d%.800c',x,'*') FROM c;
          124  +        ROLLBACK TO twoB;
          125  +      RELEASE one;
          126  +      COMMIT;
          127  +    }
          128  +  } [list $i $i]
          129  +}
          130  +
    97    131   
    98    132   finish_test