From: drh Date: Wed, 13 Sep 2017 18:38:31 +0000 (+0000) Subject: Experimental sqlite3_stmt_retryable() interface. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7e3d4884d39ea8b2faa2b426b321e1ed8b3cd77c;p=thirdparty%2Fsqlite.git Experimental sqlite3_stmt_retryable() interface. FossilOrigin-Name: ebada0723ac91f288fe89baf4a7eb6383f71d6588afaf17467f45a56677de22d --- diff --git a/manifest b/manifest index 42cba1232c..c2f0637f6a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Disable\sFTS3\smatchinfo()\stests\sthat\sassume\sa\slittleEndian\splatform\swhen\nrunning\son\sbigEndian. -D 2017-09-13T12:55:59.555 +C Experimental\ssqlite3_stmt_retryable()\sinterface. +D 2017-09-13T18:38:31.171 F Makefile.in c644bbe8ebe4aae82ad6783eae6b6beea4c727b99ff97568b847ced5e2ac7afb F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6a7a74bf60ad395098c0bd175ab054cd65ef85d7f034198d52bcc4d9e5fb4c6b @@ -422,7 +422,7 @@ F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c d2d1bf12d2b5382450620d7cede84c7ffe57e6a89fa9a908f1aba68df2731cd9 F src/legacy.c 134ab3e3fae00a0f67a5187981d6935b24b337bcf0f4b3e5c9fa5763da95bf4e F src/loadext.c 20865b183bb8a3723d59cf1efffc3c50217eb452c1021d077b908c94da26b0b2 -F src/main.c 34a58563358fe40979186124d1a3614b9a00c833124d7ebfa8e5d604ed1d2521 +F src/main.c 39f1375c02ea2d201bd09efcc09dcbb7ebce241280fa17bb7aa4ef7cc718061c F src/malloc.c a02c9e69bc76bee0f639416b947a946412890b606301454727feadcb313536d6 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 F src/mem1.c c12a42539b1ba105e3707d0e628ad70e611040d8f5e38cf942cee30c867083de @@ -458,12 +458,12 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 4324a94573b1e29286f8121e4881db59eaedc014afeb274c8d3e07ed282e0e20 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac F src/select.c c9b3d8444bbf6f167d84f41ca6f3672e2521cb163a8c706b19058dc82fffe9b8 -F src/shell.c c1206a23d9239f8f51751d3be9b8c3b02fa4103546bea1add7f864d84a8276ab -F src/shell.c.in bb9720a8c5c98d3984b16ab7540e7142bcae959666ecf248bfc523a1d44220ee -F src/sqlite.h.in ab4f8a29d1580dfaeb6891fa1b83cff8229ba0daa56994707ceaca71495d9ab7 +F src/shell.c dc398976107b68dadccce23d27d0fd16f8409b1d782612969866aab015799582 +F src/shell.c.in 30d6799f86445a8b989bed45c7d5f33c8192d838605e0abe99e0398892779974 +F src/sqlite.h.in eea8fde8bf4f3ab8b82ab3dce54f86b113f75cf48fd43ce92efd2f0cd7cc6e61 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h a1fd3aa82f967da436164e0728a7d6841651fd0c6e27b9044e0eb9f6c8462e47 -F src/sqliteInt.h 2272cd09b7f137bb2423af589d636b5b741037db9b329d359c1ef6556fac5a4c +F src/sqliteInt.h 0568e9b1aed70ea7db3630820e9e35ed6a87ea6ef3ae20693467d8eb255639e3 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b F src/status.c 9737ed017279a9e0c5da748701c3c7bf1e8ae0dae459aad20dd64fcff97a7e35 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34 @@ -527,9 +527,9 @@ F src/util.c ece10cb34b4a62cbb3aab8d711b67e93a2765e0b81d0f72458cb801a3ac60703 F src/vacuum.c 90839322fd5f00df9617eb21b68beda9b6e2a2937576b0d65985e4aeb1c53739 F src/vdbe.c 0a0ef7f0759ee4c3ed5ff0c6e9641216d4b31ebb0fea9a7b5a9c4a86ce875ff3 F src/vdbe.h d50cadf12bcf9fb99117ef392ce1ea283aa429270481426b6e8b0280c101fd97 -F src/vdbeInt.h 1fe00770144c12c4913128f35262d11527ef3284561baaab59b947a41c08d0d9 -F src/vdbeapi.c 9c670ca0dcc1cd86373aa353b747b26fe531ca5cd4331690c611d1f03842e2a1 -F src/vdbeaux.c 41d90002d774e234f95d1298fc70d25e3420e8d9ebd65aa270a8b771c5525790 +F src/vdbeInt.h 207b6ac7842768601bd5563a0c5dfad9b7dc10be5577d71d8fb53258bfd371f2 +F src/vdbeapi.c e706b81a65f879e7b9640248a386ef5f90e29d61169680e64803d2eafe407763 +F src/vdbeaux.c 0b2b1942102b33060bd960cc5c1d4997394ffa7f7189148dab4af5187fa09191 F src/vdbeblob.c db3cf91060f6f4b2f1358a4200e844697990752177784c7c95da00b7ac9f1c7b F src/vdbemem.c 2f9d672af5260f0145787e1dc2c6985414987cc0dc575133a0dc17dda767d868 F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2f @@ -1653,7 +1653,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 8525c30c1d6676e51b9d9c642450aae3722c129edafdbc39b8435fcfe09b1817 -R 6ec072e8cc5f89299e9fb58896934d7c +P 87ccdf9cbb9284553330683d4971be4f523ce922089aee6dffccfc18b3004263 +R e43ecae42b6f451608f028a9fca6964c +T *branch * sqlite3_stmt_retryable +T *sym-sqlite3_stmt_retryable * +T -sym-trunk * U drh -Z 5fb925822a75ef5ce57cd890a3d631bf +Z 5c6219255517ab512c38a3a9f7c2d38b diff --git a/manifest.uuid b/manifest.uuid index 5e4e028228..e21dc80141 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -87ccdf9cbb9284553330683d4971be4f523ce922089aee6dffccfc18b3004263 \ No newline at end of file +ebada0723ac91f288fe89baf4a7eb6383f71d6588afaf17467f45a56677de22d \ No newline at end of file diff --git a/src/main.c b/src/main.c index 3942ed9465..aaf49acdd1 100644 --- a/src/main.c +++ b/src/main.c @@ -1506,9 +1506,13 @@ static int sqliteDefaultBusyCallback( */ int sqlite3InvokeBusyHandler(BusyHandler *p){ int rc; - if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; + if( p->xFunc==0 || p->nBusy<0 ){ + p->nTimeout++; + return 0; + } rc = p->xFunc(p->pArg, p->nBusy); if( rc==0 ){ + p->nTimeout++; p->nBusy = -1; }else{ p->nBusy++; diff --git a/src/shell.c b/src/shell.c index 6a19e3610a..1825eec872 100644 --- a/src/shell.c +++ b/src/shell.c @@ -3617,7 +3617,7 @@ static int shell_exec( ){ sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ int rc = SQLITE_OK; /* Return Code */ - int rc2; + int rc2, rc3; const char *zLeftover; /* Tail of unprocessed SQL */ if( pzErrMsg ){ @@ -3718,13 +3718,23 @@ static int shell_exec( /* Finalize the statement just executed. If this fails, save a ** copy of the error message. Otherwise, set zSql to point to the ** next statement to execute. */ + rc3 = sqlite3_stmt_retryable(pStmt); rc2 = sqlite3_finalize(pStmt); if( rc!=SQLITE_NOMEM ) rc = rc2; if( rc==SQLITE_OK ){ zSql = zLeftover; while( IsSpace(zSql[0]) ) zSql++; - }else if( pzErrMsg ){ - *pzErrMsg = save_err_msg(db); + }else{ + if( rc==SQLITE_BUSY ){ + if( rc3 ){ + raw_printf(pArg->out, "Busy: might succeed on retry\n"); + }else{ + raw_printf(pArg->out, "Busy: cannot succeed - rollback required\n"); + } + } + if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } } /* clear saved stmt handle */ diff --git a/src/shell.c.in b/src/shell.c.in index 82680d484c..62c9a75297 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -2257,7 +2257,7 @@ static int shell_exec( ){ sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ int rc = SQLITE_OK; /* Return Code */ - int rc2; + int rc2, rc3; const char *zLeftover; /* Tail of unprocessed SQL */ if( pzErrMsg ){ @@ -2358,13 +2358,23 @@ static int shell_exec( /* Finalize the statement just executed. If this fails, save a ** copy of the error message. Otherwise, set zSql to point to the ** next statement to execute. */ + rc3 = sqlite3_stmt_retryable(pStmt); rc2 = sqlite3_finalize(pStmt); if( rc!=SQLITE_NOMEM ) rc = rc2; if( rc==SQLITE_OK ){ zSql = zLeftover; while( IsSpace(zSql[0]) ) zSql++; - }else if( pzErrMsg ){ - *pzErrMsg = save_err_msg(db); + }else{ + if( rc==SQLITE_BUSY ){ + if( rc3 ){ + raw_printf(pArg->out, "Busy: might succeed on retry\n"); + }else{ + raw_printf(pArg->out, "Busy: cannot succeed - rollback required\n"); + } + } + if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } } /* clear saved stmt handle */ diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 9ff366304f..99f49d9e3a 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -3764,6 +3764,18 @@ char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); */ int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); +/* +** CAPI3REF: Determine If An SQL Statement Can Be Retried After SQLITE_BUSY +** METHOD: sqlite3_stmt +** +** ^The sqlite3_stmt_retryable(X) interface returns true (non-zero) if +** the previous run of [prepared statement] X failed with an +** [SQLITE_BUSY] error but might succeed if run again. If X has not been +** previously run or if the previous run of X did not return SQLITE_BUSY, +** then the result of this function is a meaningless boolean. +*/ +int sqlite3_stmt_retryable(sqlite3_stmt *pStmt); + /* ** CAPI3REF: Determine If A Prepared Statement Has Been Reset ** METHOD: sqlite3_stmt diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 58d64ba2f2..d80334f26e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -949,6 +949,7 @@ struct BusyHandler { int (*xFunc)(void *,int); /* The busy callback */ void *pArg; /* First arg to busy callback */ int nBusy; /* Incremented with each busy call */ + u32 nTimeout; /* Number of timeouts */ }; /* diff --git a/src/vdbeInt.h b/src/vdbeInt.h index cb783653c0..4000ecc0e7 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -393,6 +393,7 @@ struct Vdbe { bft usesStmtJournal:1; /* True if uses a statement journal */ bft readOnly:1; /* True for statements that do not write */ bft bIsReader:1; /* True for statements that read */ + bft retryable:1; /* True if the statement might succeed next time */ yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */ yDbMask lockMask; /* Subset of btreeMask that requires a lock */ u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */ diff --git a/src/vdbeapi.c b/src/vdbeapi.c index b9df40b8fd..96f1ef175b 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -1613,6 +1613,14 @@ int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; } +/* +** Return true if the prepared statement failed with SQLITE_BUSY on its +** previous run but might succeed if tried again. +*/ +int sqlite3_stmt_retryable(sqlite3_stmt *pStmt){ + return pStmt ? ((Vdbe*)pStmt)->retryable : 0; +} + /* ** Return true if the prepared statement is in need of being reset. */ diff --git a/src/vdbeaux.c b/src/vdbeaux.c index e5465d16e7..34c78e35c7 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -2702,7 +2702,9 @@ int sqlite3VdbeHalt(Vdbe *p){ ** or hit an 'OR FAIL' constraint and there are no deferred foreign ** key constraints to hold up the transaction. This means a commit ** is required. */ + db->busyHandler.nTimeout = 0; rc = vdbeCommit(db, p); + p->retryable = db->busyHandler.nTimeout>0; } if( rc==SQLITE_BUSY && p->readOnly ){ sqlite3VdbeLeave(p);