Index: src/malloc.c ================================================================== --- src/malloc.c +++ src/malloc.c @@ -774,12 +774,17 @@ if( db->nVdbeExec>0 ){ AtomicStore(&db->u1.isInterrupted, 1); } DisableLookaside; if( db->pParse ){ + Parse *pParse; sqlite3ErrorMsg(db->pParse, "out of memory"); db->pParse->rc = SQLITE_NOMEM_BKPT; + for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ + pParse->nErr++; + pParse->rc = SQLITE_NOMEM; + } } } return 0; } Index: src/pcache.c ================================================================== --- src/pcache.c +++ src/pcache.c @@ -620,15 +620,24 @@ /* ** Change the page number of page p to newPgno. */ void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ PCache *pCache = p->pCache; + sqlite3_pcache_page *pOther; assert( p->nRef>0 ); assert( newPgno>0 ); assert( sqlite3PcachePageSanity(p) ); pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); + pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0); sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); + if( pOther ){ + PgHdr *pPg = (PgHdr*)pOther->pExtra; + pPg->pgno = p->pgno; + if( pPg->pPage==0 ){ + sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pOther, 0); + } + } p->pgno = newPgno; if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); } } Index: src/pcache1.c ================================================================== --- src/pcache1.c +++ src/pcache1.c @@ -1121,27 +1121,43 @@ unsigned int iNew ){ PCache1 *pCache = (PCache1 *)p; PgHdr1 *pPage = (PgHdr1 *)pPg; PgHdr1 **pp; - unsigned int h; + unsigned int hOld, hNew; assert( pPage->iKey==iOld ); assert( pPage->pCache==pCache ); pcache1EnterMutex(pCache->pGroup); - h = iOld%pCache->nHash; - pp = &pCache->apHash[h]; + hOld = iOld%pCache->nHash; + pp = &pCache->apHash[hOld]; while( (*pp)!=pPage ){ pp = &(*pp)->pNext; } *pp = pPage->pNext; - h = iNew%pCache->nHash; + hNew = iNew%pCache->nHash; + pp = &pCache->apHash[hNew]; + while( *pp ){ + if( (*pp)->iKey==iNew ){ + /* If there is already another pcache entry at iNew, change it to iOld, + ** thus swapping the positions of iNew and iOld */ + PgHdr1 *pOld = *pp; + *pp = pOld->pNext; + pOld->pNext = pCache->apHash[hOld]; + pCache->apHash[hOld] = pOld; + pOld->iKey = iOld; + break; + }else{ + pp = &(*pp)->pNext; + } + } + pPage->iKey = iNew; - pPage->pNext = pCache->apHash[h]; - pCache->apHash[h] = pPage; + pPage->pNext = pCache->apHash[hNew]; + pCache->apHash[hNew] = pPage; if( iNew>pCache->iMaxKey ){ pCache->iMaxKey = iNew; } pcache1LeaveMutex(pCache->pGroup);