From 918232aadb0d46093d97fdf6063243756741e712 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 18 Mar 2021 13:42:53 +0000 Subject: [PATCH 1/4] Allow instruction OP_OpenDup to duplicate cursors created by OP_OpenDup, as well as by OP_OpenEphemeral. FossilOrigin-Name: b8de980b2fb78088ef74e053cb987bb84319d13dc96ce1e89baaaa3fe8cf1efc --- manifest | 23 ++++++++++++----------- manifest.uuid | 2 +- src/btree.c | 8 ++++++++ src/btree.h | 1 + src/vdbe.c | 7 ++++--- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index 2aa2055a9b..0f0b50aee4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Version\s3.35.2 -D 2021-03-17T19:07:21.223 +C Allow\sinstruction\sOP_OpenDup\sto\sduplicate\scursors\screated\sby\sOP_OpenDup,\sas\swell\sas\sby\sOP_OpenEphemeral. +D 2021-03-18T13:42:53.871 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -483,8 +483,8 @@ F src/auth.c 08954fdc4cc2da5264ba5b75cfd90b67a6fc7d1710a02ccf917c38eadec77853 F src/backup.c 3014889fa06e20e6adfa0d07b60097eec1f6e5b06671625f476a714d2356513d F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c bafa3a2e8b6622a3aa8791f90c8ecc70e8ae551ba9023f865213890f5b8a8994 -F src/btree.h 096cc53baa58be22b02c896d1cf933c38cfc6d65f9253c1367ece8cc88a24de5 +F src/btree.c 83cb92554f14752d1446ecebf9d2b38292d36af23d1b422862653192b742e1b7 +F src/btree.h a25ab8dfe82764e0449e12091f4dbedcf2995a70db6a27e224d1f3772a0d640c F src/btreeInt.h 7614cae30f95b6aed0c7cac7718276a55cfe2c77058cbfd8bef5b75329757331 F src/build.c fec73c39d756f31d35ccbaa80fe1e040a8d675a318d4d30f41c444167bf3b860 F src/callback.c d0b853dd413255d2e337b34545e54d888ea02f20da5ad0e63585b389624c4a6c @@ -614,7 +614,7 @@ F src/upsert.c df8f1727d62b5987c4fd302cd4d7c0c84ae57cd65683c5a34a740dfe24039235 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0 F src/util.c 41c7a72da1df47864faa378a1c720b38adb288c6838cb6be5594511b6287a048 F src/vacuum.c 492422c1463c076473bae1858799c7a0a5fe87a133d1223239447c422cd26286 -F src/vdbe.c b79b222f07690874d436eef4ad5e76845b2796e7094b1d0545cc94333f0d8e50 +F src/vdbe.c ae4ebb8762fc1b30f11a0498eba74c193f052c6724f73ffb63548875200813c6 F src/vdbe.h 25dabb25c7e157b84e59260cfb5b466c3ac103ede9f36f4db371332c47601abe F src/vdbeInt.h 3df118924e1711f1bbc8e30c46260d0ab6c3b029b32dd411f789111f76434f3c F src/vdbeapi.c 4a43e303ec3354c785f453e881521969378e85628278ab74ba4a9df790c0d93b @@ -1910,9 +1910,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 0ed7eb9115510511f13dca715bb2b178580648517d8d30436a992d70a2e85ac1 -R 6b9cbc5338ab96ec85af6f269ca8689c -T +sym-release * -T +sym-version-3.35.2 * -U drh -Z df8a52cb5f4a5da779c9bf4ff3cf1b0f +P ea80f3002f4120f5dcee76e8779dfdc88e1e096c5cdd06904c20fd26d50c3827 +R 5690d05e7243d41ceb9332de8bdd3707 +T *branch * opendup-fix +T *sym-opendup-fix * +T -sym-trunk * +U dan +Z fd7ffe22c065e32af24a29a1b9c91b9f diff --git a/manifest.uuid b/manifest.uuid index a2044fac2f..92f59550cc 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ea80f3002f4120f5dcee76e8779dfdc88e1e096c5cdd06904c20fd26d50c3827 \ No newline at end of file +b8de980b2fb78088ef74e053cb987bb84319d13dc96ce1e89baaaa3fe8cf1efc \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 709445b16c..79814279e3 100644 --- a/src/btree.c +++ b/src/btree.c @@ -10705,6 +10705,14 @@ int sqlite3BtreeIsReadonly(Btree *p){ */ int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } +/* +** Return the Btree object used to open the cursor provided as an +** argument. +*/ +Btree *sqlite3BtreeGetBtree(BtCursor *pCsr){ + return pCsr->pBtree; +} + #if !defined(SQLITE_OMIT_SHARED_CACHE) /* ** Return true if the Btree passed as the only argument is sharable. diff --git a/src/btree.h b/src/btree.h index b7afecc426..b8b2f3e4be 100644 --- a/src/btree.h +++ b/src/btree.h @@ -363,6 +363,7 @@ void sqlite3BtreeCursorList(Btree*); #endif int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64); +Btree *sqlite3BtreeGetBtree(BtCursor*); /* ** If we are not using shared cache, then there is no need to diff --git a/src/vdbe.c b/src/vdbe.c index 71f30b7540..1f178f2b24 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -3873,7 +3873,7 @@ case OP_OpenDup: { pOrig = p->apCsr[pOp->p2]; assert( pOrig ); - assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */ + assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); if( pCx==0 ) goto no_mem; @@ -3883,8 +3883,9 @@ case OP_OpenDup: { pCx->isTable = pOrig->isTable; pCx->pgnoRoot = pOrig->pgnoRoot; pCx->isOrdered = pOrig->isOrdered; - rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR, - pCx->pKeyInfo, pCx->uc.pCursor); + rc = sqlite3BtreeCursor(sqlite3BtreeGetBtree(pOrig->uc.pCursor), + pCx->pgnoRoot, BTREE_WRCSR, pCx->pKeyInfo, pCx->uc.pCursor + ); /* The sqlite3BtreeCursor() routine can only fail for the first cursor ** opened for a database. Since there is already an open cursor when this ** opcode is run, the sqlite3BtreeCursor() cannot fail */ From eeee8a5f01f0b11497646a9a3e5784c3b274ca7c Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 18 Mar 2021 14:31:37 +0000 Subject: [PATCH 2/4] Automatically close ephemeral b-trees when their last cursor is closed. FossilOrigin-Name: 39b5af18c0580c8e92516d410f8c465bfec31b2d0be9df1cfd6a1d1a19b4fc14 --- manifest | 21 ++++++++---------- manifest.uuid | 2 +- src/btree.c | 18 ++++++++-------- src/btree.h | 1 - src/vdbe.c | 59 +++++++++++++++++++++++++++------------------------ src/vdbeaux.c | 11 +++------- 6 files changed, 53 insertions(+), 59 deletions(-) diff --git a/manifest b/manifest index 0f0b50aee4..e3cc0013f4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Allow\sinstruction\sOP_OpenDup\sto\sduplicate\scursors\screated\sby\sOP_OpenDup,\sas\swell\sas\sby\sOP_OpenEphemeral. -D 2021-03-18T13:42:53.871 +C Automatically\sclose\sephemeral\sb-trees\swhen\stheir\slast\scursor\sis\sclosed. +D 2021-03-18T14:31:37.348 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -483,8 +483,8 @@ F src/auth.c 08954fdc4cc2da5264ba5b75cfd90b67a6fc7d1710a02ccf917c38eadec77853 F src/backup.c 3014889fa06e20e6adfa0d07b60097eec1f6e5b06671625f476a714d2356513d F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c 83cb92554f14752d1446ecebf9d2b38292d36af23d1b422862653192b742e1b7 -F src/btree.h a25ab8dfe82764e0449e12091f4dbedcf2995a70db6a27e224d1f3772a0d640c +F src/btree.c b048dcb502fd8abd5b161189150607d8ca9477b52fc5cba907246f795eb1fe36 +F src/btree.h 096cc53baa58be22b02c896d1cf933c38cfc6d65f9253c1367ece8cc88a24de5 F src/btreeInt.h 7614cae30f95b6aed0c7cac7718276a55cfe2c77058cbfd8bef5b75329757331 F src/build.c fec73c39d756f31d35ccbaa80fe1e040a8d675a318d4d30f41c444167bf3b860 F src/callback.c d0b853dd413255d2e337b34545e54d888ea02f20da5ad0e63585b389624c4a6c @@ -614,11 +614,11 @@ F src/upsert.c df8f1727d62b5987c4fd302cd4d7c0c84ae57cd65683c5a34a740dfe24039235 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0 F src/util.c 41c7a72da1df47864faa378a1c720b38adb288c6838cb6be5594511b6287a048 F src/vacuum.c 492422c1463c076473bae1858799c7a0a5fe87a133d1223239447c422cd26286 -F src/vdbe.c ae4ebb8762fc1b30f11a0498eba74c193f052c6724f73ffb63548875200813c6 +F src/vdbe.c 819860625f65ebc1ad029fbef07b62c56904e057a6d76f0d5486e2c41d0fccd4 F src/vdbe.h 25dabb25c7e157b84e59260cfb5b466c3ac103ede9f36f4db371332c47601abe F src/vdbeInt.h 3df118924e1711f1bbc8e30c46260d0ab6c3b029b32dd411f789111f76434f3c F src/vdbeapi.c 4a43e303ec3354c785f453e881521969378e85628278ab74ba4a9df790c0d93b -F src/vdbeaux.c fb51483c2bcaf45c5de63c26cce8649ef37f1332e8e035867033d21ef5e7fc2c +F src/vdbeaux.c 6df834c4ff0c86a62b94fe0762d0abb7dbda54a862f0fe82e35d22fc4490414d F src/vdbeblob.c 253ed82894924c362a7fa3079551d3554cd1cdace39aa833da77d3bc67e7c1b1 F src/vdbemem.c 947f2a65910edb4014dc981d33e414a68c51f169f9df8c4c493a0ba840b6eb1f F src/vdbesort.c f5b5e473a7cee44e47a94817b042fd7172cf3aa2c0a7928a8339d612bcfdec5a @@ -1910,10 +1910,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P ea80f3002f4120f5dcee76e8779dfdc88e1e096c5cdd06904c20fd26d50c3827 -R 5690d05e7243d41ceb9332de8bdd3707 -T *branch * opendup-fix -T *sym-opendup-fix * -T -sym-trunk * +P b8de980b2fb78088ef74e053cb987bb84319d13dc96ce1e89baaaa3fe8cf1efc +R d059e62ed2003127849996336a75bcfd U dan -Z fd7ffe22c065e32af24a29a1b9c91b9f +Z 5c6f726925b0e9cfdd8d4a838991a1a4 diff --git a/manifest.uuid b/manifest.uuid index 92f59550cc..556c036b9c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b8de980b2fb78088ef74e053cb987bb84319d13dc96ce1e89baaaa3fe8cf1efc \ No newline at end of file +39b5af18c0580c8e92516d410f8c465bfec31b2d0be9df1cfd6a1d1a19b4fc14 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 79814279e3..830e3555a9 100644 --- a/src/btree.c +++ b/src/btree.c @@ -2742,6 +2742,7 @@ int sqlite3BtreeClose(Btree *p){ /* Close all cursors opened via this handle. */ assert( sqlite3_mutex_held(p->db->mutex) ); sqlite3BtreeEnter(p); + pBt->openFlags &= ~BTREE_SINGLE; pCur = pBt->pCursor; while( pCur ){ BtCursor *pTmp = pCur; @@ -4541,7 +4542,14 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ unlockBtreeIfUnused(pBt); sqlite3_free(pCur->aOverflow); sqlite3_free(pCur->pKey); - sqlite3BtreeLeave(pBtree); + if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){ + /* Since the BtShared is not sharable, there is no need to + ** worry about the missing sqlite3BtreeLeave() call here. */ + assert( pBtree->sharable==0 ); + sqlite3BtreeClose(pBtree); + }else{ + sqlite3BtreeLeave(pBtree); + } pCur->pBtree = 0; } return SQLITE_OK; @@ -10705,14 +10713,6 @@ int sqlite3BtreeIsReadonly(Btree *p){ */ int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } -/* -** Return the Btree object used to open the cursor provided as an -** argument. -*/ -Btree *sqlite3BtreeGetBtree(BtCursor *pCsr){ - return pCsr->pBtree; -} - #if !defined(SQLITE_OMIT_SHARED_CACHE) /* ** Return true if the Btree passed as the only argument is sharable. diff --git a/src/btree.h b/src/btree.h index b8b2f3e4be..b7afecc426 100644 --- a/src/btree.h +++ b/src/btree.h @@ -363,7 +363,6 @@ void sqlite3BtreeCursorList(Btree*); #endif int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64); -Btree *sqlite3BtreeGetBtree(BtCursor*); /* ** If we are not using shared cache, then there is no need to diff --git a/src/vdbe.c b/src/vdbe.c index 1f178f2b24..a6bfd236cd 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -3873,7 +3873,7 @@ case OP_OpenDup: { pOrig = p->apCsr[pOp->p2]; assert( pOrig ); - assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ + assert( pOrig->pBtx ); /* Only ephemeral cursors can be duplicated */ pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); if( pCx==0 ) goto no_mem; @@ -3883,9 +3883,9 @@ case OP_OpenDup: { pCx->isTable = pOrig->isTable; pCx->pgnoRoot = pOrig->pgnoRoot; pCx->isOrdered = pOrig->isOrdered; - rc = sqlite3BtreeCursor(sqlite3BtreeGetBtree(pOrig->uc.pCursor), - pCx->pgnoRoot, BTREE_WRCSR, pCx->pKeyInfo, pCx->uc.pCursor - ); + pCx->pBtx = pOrig->pBtx; + rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR, + pCx->pKeyInfo, pCx->uc.pCursor); /* The sqlite3BtreeCursor() routine can only fail for the first cursor ** opened for a database. Since there is already an open cursor when this ** opcode is run, the sqlite3BtreeCursor() cannot fail */ @@ -3966,33 +3966,36 @@ case OP_OpenEphemeral: { vfsFlags); if( rc==SQLITE_OK ){ rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0); - } - if( rc==SQLITE_OK ){ - /* If a transient index is required, create it by calling - ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before - ** opening it. If a transient table is required, just use the - ** automatically created table with root-page 1 (an BLOB_INTKEY table). - */ - if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ - assert( pOp->p4type==P4_KEYINFO ); - rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot, - BTREE_BLOBKEY | pOp->p5); - if( rc==SQLITE_OK ){ - assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); - assert( pKeyInfo->db==db ); - assert( pKeyInfo->enc==ENC(db) ); - rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR, - pKeyInfo, pCx->uc.pCursor); + if( rc==SQLITE_OK ){ + /* If a transient index is required, create it by calling + ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before + ** opening it. If a transient table is required, just use the + ** automatically created table with root-page 1 (an BLOB_INTKEY table). + */ + if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ + assert( pOp->p4type==P4_KEYINFO ); + rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot, + BTREE_BLOBKEY | pOp->p5); + if( rc==SQLITE_OK ){ + assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); + assert( pKeyInfo->db==db ); + assert( pKeyInfo->enc==ENC(db) ); + rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR, + pKeyInfo, pCx->uc.pCursor); + } + pCx->isTable = 0; + }else{ + pCx->pgnoRoot = SCHEMA_ROOT; + rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR, + 0, pCx->uc.pCursor); + pCx->isTable = 1; } - pCx->isTable = 0; - }else{ - pCx->pgnoRoot = SCHEMA_ROOT; - rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR, - 0, pCx->uc.pCursor); - pCx->isTable = 1; + } + pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); + if( rc ){ + sqlite3BtreeClose(pCx->pBtx); } } - pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); } if( rc ) goto abort_due_to_error; pCx->nullRow = 1; diff --git a/src/vdbeaux.c b/src/vdbeaux.c index ede652dfac..1cb5a919a1 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -2472,20 +2472,15 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ return; } assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE ); + assert( pCx->pBtx==0 || pCx->isEphemeral ); switch( pCx->eCurType ){ case CURTYPE_SORTER: { sqlite3VdbeSorterClose(p->db, pCx); break; } case CURTYPE_BTREE: { - if( pCx->isEphemeral ){ - if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx); - /* The pCx->pCursor will be close automatically, if it exists, by - ** the call above. */ - }else{ - assert( pCx->uc.pCursor!=0 ); - sqlite3BtreeCloseCursor(pCx->uc.pCursor); - } + assert( pCx->uc.pCursor!=0 ); + sqlite3BtreeCloseCursor(pCx->uc.pCursor); break; } #ifndef SQLITE_OMIT_VIRTUALTABLE From 5a4a15fd2e9706444fc107cff36eba1bc4b6f1aa Mon Sep 17 00:00:00 2001 From: drh <> Date: Thu, 18 Mar 2021 15:42:59 +0000 Subject: [PATCH 3/4] Further sanity checking of the OpenDup cursors. FossilOrigin-Name: 0ec71cf16983697f68ab8d921d80a5f39611e4cb5e6e19cbc4a9ad6d49bfc835 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/btree.c | 20 ++++++++++++-------- src/vdbe.c | 16 +++++++--------- src/vdbeInt.h | 1 + 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/manifest b/manifest index e3cc0013f4..feaba5c150 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Automatically\sclose\sephemeral\sb-trees\swhen\stheir\slast\scursor\sis\sclosed. -D 2021-03-18T14:31:37.348 +C Further\ssanity\schecking\sof\sthe\sOpenDup\scursors. +D 2021-03-18T15:42:59.646 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -483,7 +483,7 @@ F src/auth.c 08954fdc4cc2da5264ba5b75cfd90b67a6fc7d1710a02ccf917c38eadec77853 F src/backup.c 3014889fa06e20e6adfa0d07b60097eec1f6e5b06671625f476a714d2356513d F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c b048dcb502fd8abd5b161189150607d8ca9477b52fc5cba907246f795eb1fe36 +F src/btree.c 25cdff8bd4b3c9bb009bfe585a7edde48fdb9aa1579173922038aac6bb47c0c6 F src/btree.h 096cc53baa58be22b02c896d1cf933c38cfc6d65f9253c1367ece8cc88a24de5 F src/btreeInt.h 7614cae30f95b6aed0c7cac7718276a55cfe2c77058cbfd8bef5b75329757331 F src/build.c fec73c39d756f31d35ccbaa80fe1e040a8d675a318d4d30f41c444167bf3b860 @@ -614,9 +614,9 @@ F src/upsert.c df8f1727d62b5987c4fd302cd4d7c0c84ae57cd65683c5a34a740dfe24039235 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0 F src/util.c 41c7a72da1df47864faa378a1c720b38adb288c6838cb6be5594511b6287a048 F src/vacuum.c 492422c1463c076473bae1858799c7a0a5fe87a133d1223239447c422cd26286 -F src/vdbe.c 819860625f65ebc1ad029fbef07b62c56904e057a6d76f0d5486e2c41d0fccd4 +F src/vdbe.c cba3e495c8ab25af825202dfa944e6f91d5c5cf86d415af07cac3834989d64a6 F src/vdbe.h 25dabb25c7e157b84e59260cfb5b466c3ac103ede9f36f4db371332c47601abe -F src/vdbeInt.h 3df118924e1711f1bbc8e30c46260d0ab6c3b029b32dd411f789111f76434f3c +F src/vdbeInt.h 000d9ab1ea4cb55a80de15e28f3f595645b4fddef34bca4347fb3db8031d9041 F src/vdbeapi.c 4a43e303ec3354c785f453e881521969378e85628278ab74ba4a9df790c0d93b F src/vdbeaux.c 6df834c4ff0c86a62b94fe0762d0abb7dbda54a862f0fe82e35d22fc4490414d F src/vdbeblob.c 253ed82894924c362a7fa3079551d3554cd1cdace39aa833da77d3bc67e7c1b1 @@ -1910,7 +1910,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P b8de980b2fb78088ef74e053cb987bb84319d13dc96ce1e89baaaa3fe8cf1efc -R d059e62ed2003127849996336a75bcfd -U dan -Z 5c6f726925b0e9cfdd8d4a838991a1a4 +P 39b5af18c0580c8e92516d410f8c465bfec31b2d0be9df1cfd6a1d1a19b4fc14 +R a76f75bf1ec726e3bc7a2a3639bd46d6 +U drh +Z 8ca7de05c1a42b71a557e12a74405656 diff --git a/manifest.uuid b/manifest.uuid index 556c036b9c..aa2b3946c6 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -39b5af18c0580c8e92516d410f8c465bfec31b2d0be9df1cfd6a1d1a19b4fc14 \ No newline at end of file +0ec71cf16983697f68ab8d921d80a5f39611e4cb5e6e19cbc4a9ad6d49bfc835 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 830e3555a9..d7dddde1a5 100644 --- a/src/btree.c +++ b/src/btree.c @@ -2737,20 +2737,24 @@ static void freeTempSpace(BtShared *pBt){ */ int sqlite3BtreeClose(Btree *p){ BtShared *pBt = p->pBt; - BtCursor *pCur; /* Close all cursors opened via this handle. */ assert( sqlite3_mutex_held(p->db->mutex) ); sqlite3BtreeEnter(p); - pBt->openFlags &= ~BTREE_SINGLE; - pCur = pBt->pCursor; - while( pCur ){ - BtCursor *pTmp = pCur; - pCur = pCur->pNext; - if( pTmp->pBtree==p ){ - sqlite3BtreeCloseCursor(pTmp); + + /* Verify that no other cursors have this Btree open */ +#ifdef SQLITE_DEBUG + { + BtCursor *pCur = pBt->pCursor; + while( pCur ){ + BtCursor *pTmp = pCur; + pCur = pCur->pNext; + assert( pTmp->pBtree!=p ); + } } +#endif + pBt->openFlags &= ~BTREE_SINGLE; /* Rollback any active transaction and free the handle structure. ** The call to sqlite3BtreeRollback() drops any table-locks held by diff --git a/src/vdbe.c b/src/vdbe.c index a6bfd236cd..73ff2d6270 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -272,11 +272,6 @@ static VdbeCursor *allocateCursor( assert( iCur>=0 && iCurnCursor ); if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ - /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag - ** is clear. Otherwise, if this is an ephemeral cursor created by - ** OP_OpenDup, the cursor will not be closed and will still be part - ** of a BtShared.pCursor list. */ - if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0; sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); p->apCsr[iCur] = 0; } @@ -3873,7 +3868,7 @@ case OP_OpenDup: { pOrig = p->apCsr[pOp->p2]; assert( pOrig ); - assert( pOrig->pBtx ); /* Only ephemeral cursors can be duplicated */ + assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); if( pCx==0 ) goto no_mem; @@ -3884,6 +3879,8 @@ case OP_OpenDup: { pCx->pgnoRoot = pOrig->pgnoRoot; pCx->isOrdered = pOrig->isOrdered; pCx->pBtx = pOrig->pBtx; + pCx->hasBeenDuped = 1; + pOrig->hasBeenDuped = 1; rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR, pCx->pKeyInfo, pCx->uc.pCursor); /* The sqlite3BtreeCursor() routine can only fail for the first cursor @@ -3950,9 +3947,10 @@ case OP_OpenEphemeral: { aMem[pOp->p3].z = ""; } pCx = p->apCsr[pOp->p1]; - if( pCx && ALWAYS(pCx->pBtx) ){ - /* If the ephermeral table is already open, erase all existing content - ** so that the table is empty again, rather than creating a new table. */ + if( pCx && !pCx->hasBeenDuped ){ + /* If the ephermeral table is already open and has no duplicates from + ** OP_OpenDup, then erase all existing content so that the table is + ** empty again, rather than creating a new table. */ assert( pCx->isEphemeral ); pCx->seqCount = 0; pCx->cacheStatus = CACHE_STALE; diff --git a/src/vdbeInt.h b/src/vdbeInt.h index 4f8a2edf37..cb423f20a1 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -86,6 +86,7 @@ struct VdbeCursor { Bool isEphemeral:1; /* True for an ephemeral table */ Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */ Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */ + Bool hasBeenDuped:1; /* This cursor was source or target of OP_OpenDup */ u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */ Btree *pBtx; /* Separate file holding temporary table */ i64 seqCount; /* Sequence counter */ From 76f7b16f1ee50a7677989ad93e1652bd31c131b7 Mon Sep 17 00:00:00 2001 From: drh <> Date: Thu, 18 Mar 2021 16:36:31 +0000 Subject: [PATCH 4/4] Remove unnecessary code from sqlite3BtreeClose() and add test cases. FossilOrigin-Name: edbfdcaefae1e9a42c046c058341bf5b0f533c0b0cbd39543fcaa2a83d67561d --- manifest | 14 +++--- manifest.uuid | 2 +- src/btree.c | 1 - test/with2.test | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index feaba5c150..16608f226a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Further\ssanity\schecking\sof\sthe\sOpenDup\scursors. -D 2021-03-18T15:42:59.646 +C Remove\sunnecessary\scode\sfrom\ssqlite3BtreeClose()\sand\sadd\stest\scases. +D 2021-03-18T16:36:31.997 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -483,7 +483,7 @@ F src/auth.c 08954fdc4cc2da5264ba5b75cfd90b67a6fc7d1710a02ccf917c38eadec77853 F src/backup.c 3014889fa06e20e6adfa0d07b60097eec1f6e5b06671625f476a714d2356513d F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 8acc2f464ee76324bf13310df5692a262b801808984c1b79defb2503bbafadb6 -F src/btree.c 25cdff8bd4b3c9bb009bfe585a7edde48fdb9aa1579173922038aac6bb47c0c6 +F src/btree.c 227adc33e70b293955c8f36bc0264cd9e8398aadb0d2a3cd3a9c90cbe88b71fc F src/btree.h 096cc53baa58be22b02c896d1cf933c38cfc6d65f9253c1367ece8cc88a24de5 F src/btreeInt.h 7614cae30f95b6aed0c7cac7718276a55cfe2c77058cbfd8bef5b75329757331 F src/build.c fec73c39d756f31d35ccbaa80fe1e040a8d675a318d4d30f41c444167bf3b860 @@ -1788,7 +1788,7 @@ F test/windowerr.test a8b752402109c15aa1c5efe1b93ccb0ce1ef84fa964ae1cd6684dd0b3c F test/windowfault.test d543d46571b32d19f198cb04b6505747fabf3cc369970daae47074ee793612be F test/windowpushd.test 5b9c114e8173c3addacf58a0fcd941437b14649f2033700184479a13f188ad00 F test/with1.test 780be387f01e290e768bdfd1827280f9e37ba37223eb4736aba386864fac5a94 -F test/with2.test e0030e2f0267a910d6c0e4f46f2dfe941c1cc0d4f659ba69b3597728e7e8f1ab +F test/with2.test 7f890304fad815e49123d658012f4e463c7a2e9682044187ce21495e34a08ab9 F test/with3.test 85e059bf4c2ef5626411ee59f399b4bb4b4a0f009bcb7db86f254e570ed11831 F test/with4.test 257be66c0c67fee1defbbac0f685c3465e2cad037f21ce65f23f86084f198205 F test/with5.test 6248213c41fab36290b5b73aa3f937309dfba337004d9d8434c3fabc8c7d4be8 @@ -1910,7 +1910,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 39b5af18c0580c8e92516d410f8c465bfec31b2d0be9df1cfd6a1d1a19b4fc14 -R a76f75bf1ec726e3bc7a2a3639bd46d6 +P 0ec71cf16983697f68ab8d921d80a5f39611e4cb5e6e19cbc4a9ad6d49bfc835 +R d6bb4de826da78f3ee27cd7f97de25cd U drh -Z 8ca7de05c1a42b71a557e12a74405656 +Z 33a255e216d282f4bd457b0447324171 diff --git a/manifest.uuid b/manifest.uuid index aa2b3946c6..882cd91e81 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0ec71cf16983697f68ab8d921d80a5f39611e4cb5e6e19cbc4a9ad6d49bfc835 \ No newline at end of file +edbfdcaefae1e9a42c046c058341bf5b0f533c0b0cbd39543fcaa2a83d67561d \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index d7dddde1a5..2c05a07410 100644 --- a/src/btree.c +++ b/src/btree.c @@ -2754,7 +2754,6 @@ int sqlite3BtreeClose(Btree *p){ } } #endif - pBt->openFlags &= ~BTREE_SINGLE; /* Rollback any active transaction and free the handle structure. ** The call to sqlite3BtreeRollback() drops any table-locks held by diff --git a/test/with2.test b/test/with2.test index 004ec94b97..5676f9601f 100644 --- a/test/with2.test +++ b/test/with2.test @@ -414,5 +414,121 @@ do_execsql_test 8.3 { SELECT * FROM q; } {1 2 3 4 5} +# 2021-03-18 +# Ticket bb8a9fd4a9b7fce5 +reset_db +do_execsql_test 9.1 { + WITH xyz(a) AS ( + WITH abc AS ( SELECT 1234 ) SELECT * FROM abc + ) + SELECT * FROM xyz AS one, xyz AS two, ( + SELECT * FROM xyz UNION ALL SELECT * FROM xyz + ); +} {1234 1234 1234 1234 1234 1234} +load_static_extension db series +do_execsql_test 9.2 { + WITH + cst(rsx, rsy) AS ( + SELECT 100, 100 + ), + cst2(minx, maxx, stepx, miny, maxy, stepy, qualitativex, qualitativey) AS ( + SELECT NULL, NULL, NULL, NULL, NULL, NULL, 0, 0 + ), + ds0(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS ( + SELECT 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11 + ), + ds(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS ( + SELECT m, n, x, + y, x2, + y2, + title, size, mark, label, markmode + FROM ds0 + WINDOW w AS (PARTITION BY m, x ORDER BY n) + ), + d(m, n, x, y, x2, y2, labelx,labely,title,size,mark,label,markmode) AS ( + SELECT m, n, x, y, x2, y2, x, y, title, size, mark, label, markmode + FROM ds, cst2 + ), + ylabels(y, label) AS ( + SELECT y, MIN(labely) FROM d GROUP BY y + ), + yaxis(maxy, miny, stepy , minstepy) AS ( + WITH + xt0(minx, maxx) AS ( + SELECT coalesce(miny, min(min(y2), + min(y))), coalesce(maxy, max(max(y2), + max(y))) + qualitativey + FROM d, cst2 + ), + xt1(mx, mn) AS (SELECT maxx, minx FROM xt0), + xt2(mx, mn, step) AS (SELECT mx, mn, (mx-mn) FROM xt1), + + xt3(mx, mn, ms) AS ( + SELECT mx, mn, first_value(rs) OVER (order by x desc) AS ms + FROM (SELECT mx, mn, step, f,(mx-mn) as rng, + 1.0*step/f as rs, 1.0*(mx-mn)/(step/f) AS x + FROM xt2, (SELECT 1 AS f UNION ALL SELECT 2 + UNION ALL SELECT 4 + UNION ALL SELECT 5)) AS src + WHERE x < 10 limit 1), + xt4(minstepy) AS ( + SELECT MIN(abs(y2-y)) FROM d WHERE y2 != y + ) + SELECT (mx/ms)*ms, (mn/ms)*ms, coalesce(stepy, ms), + coalesce(minstepy, ms, stepy) FROM xt3, cst2,xt4 + ), + distinct_mark_n_m(mark, ze, zem, title) AS ( + SELECT DISTINCT mark, n AS ze, m AS zem, title FROM ds0 + ), + facet0(m, mi, title, radial) AS ( + SELECT md, row_number() OVER () - 1, title, 'radial' + IN (SELECT mark FROM distinct_mark_n_m WHERE zem = md) + FROM (SELECT DISTINCT zem AS md, title AS title + FROM distinct_mark_n_m ORDER BY 2, 1) + ), + facet(m, mi, xorigin, yorigin, title, radial) AS ( + SELECT m, mi, + rsx * 1.2 * IFNULL(CASE WHEN ( + 0 + ) > 0 THEN mi / ( + 0 + ) ELSE mi % ( + 2 + ) END, mi), + rsy * 1.2 * IFNULL(CASE WHEN ( + 2 + ) > 0 THEN mi / ( + 2 + ) ELSE mi / ( + 0 + ) END, 0), + title, radial FROM facet0, cst + ), + radygrid(m, mi, tty, wty, ttx, ttx2, xorigin, yorigin) AS ( + SELECT m, mi, rsy / 2 / ((maxy-miny)/stepy) * (value-1) AS tty, + coalesce(NULL, miny + stepy * (value-1)) AS wty, + xorigin, xorigin+rsx, xorigin + rsx / 2, + yorigin + rsy / 2 + FROM generate_series(1), yaxis, cst, + facet LEFT JOIN ylabels ON ylabels.y = (miny + (value-1) * stepy) + WHERE radial AND stop = 1+1.0*(maxy-miny)/stepy + ), + ypos(m, mi, pcx, pcy, radial) AS ( + SELECT m, mi, xorigin, yorigin + CASE + WHEN 0 BETWEEN miny AND maxy THEN + rsy - (0 - miny) * rsy / (maxy-miny) + WHEN 0 >= maxy THEN 0 + ELSE rsy + END, radial FROM yaxis, cst, facet WHERE NOT radial + UNION ALL + SELECT m, mi, xorigin + rsx / 2, yorigin + (CASE + WHEN 0 BETWEEN miny AND maxy THEN + rsy - (0 - miny) * rsy / 2 / (maxy-miny) + WHEN 0 >= maxy THEN 0 + ELSE rsy + END ) / 2, radial FROM yaxis, cst, facet WHERE radial + ) + SELECT * FROM radygrid , ypos; +} {} finish_test