1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Following a ROLLBACK that reverts changes to an RTREE, any pending queries

against that same RTREE abort with code SQLITE_ABORT_ROLLBACK.
dbsqlfuzz de7d17b72d0e842352c998dd86a47b7d0f707be9.

FossilOrigin-Name: b26e5a500ebc358047eff79db61b5edfec319b95fbf8685e6a798c5e6c1923fb
This commit is contained in:
drh
2024-03-20 15:26:30 +00:00
parent b2bc4c9338
commit a93344297f
4 changed files with 187 additions and 16 deletions

View File

@@ -170,6 +170,7 @@ struct Rtree {
u32 nBusy; /* Current number of users of this structure */
i64 nRowEst; /* Estimated number of rows in this table */
u32 nCursor; /* Number of open cursors */
u32 iGeneration; /* Cursors with smaller iGeneration are stale */
u32 nNodeRef; /* Number RtreeNodes with positive nRef */
char *zReadAuxSql; /* SQL for statement to read aux data */
@@ -286,6 +287,7 @@ struct RtreeCursor {
u8 atEOF; /* True if at end of search */
u8 bPoint; /* True if sPoint is valid */
u8 bAuxValid; /* True if pReadAux is valid */
u32 iGeneration; /* Stale if too small */
int iStrategy; /* Copy of idxNum search parameter */
int nConstraint; /* Number of entries in aConstraint */
RtreeConstraint *aConstraint; /* Search constraints. */
@@ -1087,6 +1089,7 @@ static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
if( pCsr ){
memset(pCsr, 0, sizeof(RtreeCursor));
pCsr->base.pVtab = pVTab;
pCsr->iGeneration = pRtree->iGeneration;
rc = SQLITE_OK;
pRtree->nCursor++;
}
@@ -1627,6 +1630,9 @@ static int rtreeStepToLeaf(RtreeCursor *pCur){
int eInt;
RtreeSearchPoint x;
if( pCur->iGeneration<pRtree->iGeneration ){
return SQLITE_ABORT_ROLLBACK;
}
eInt = pRtree->eCoordType==RTREE_COORD_INT32;
while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
u8 *pCellData;
@@ -1854,6 +1860,7 @@ static int rtreeFilter(
/* Reset the cursor to the same state as rtreeOpen() leaves it in. */
resetCursor(pCsr);
pCsr->iGeneration = pRtree->iGeneration;
pCsr->iStrategy = idxNum;
if( idxNum==1 ){
@@ -3233,6 +3240,16 @@ static int rtreeEndTransaction(sqlite3_vtab *pVtab){
nodeBlobReset(pRtree);
return SQLITE_OK;
}
static int rtreeRollback(sqlite3_vtab *pVtab){
Rtree *pRtree = (Rtree *)pVtab;
pRtree->iGeneration++;
return rtreeEndTransaction(pVtab);
}
static int rtreeRollbackTo(sqlite3_vtab *pVtab, int notUsed){
Rtree *pRtree = (Rtree *)pVtab;
pRtree->iGeneration++;
return SQLITE_OK;
}
/*
** The xRename method for rtree module virtual tables.
@@ -3351,12 +3368,12 @@ static sqlite3_module rtreeModule = {
rtreeBeginTransaction, /* xBegin - begin transaction */
rtreeEndTransaction, /* xSync - sync transaction */
rtreeEndTransaction, /* xCommit - commit transaction */
rtreeEndTransaction, /* xRollback - rollback transaction */
rtreeRollback, /* xRollback - rollback transaction */
0, /* xFindFunction - function overloading */
rtreeRename, /* xRename - rename the table */
rtreeSavepoint, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
rtreeRollbackTo, /* xRollbackTo */
rtreeShadowName, /* xShadowName */
rtreeIntegrity /* xIntegrity */
};

159
ext/rtree/rtreeJ.test Normal file
View File

@@ -0,0 +1,159 @@
# 2024-02-03
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# ROLLBACK in the middle of an RTREE query
#
if {![info exists testdir]} {
set testdir [file join [file dirname [info script]] .. .. test]
}
source $testdir/tester.tcl
set testprefix rtreeJ
do_execsql_test 1.0 {
CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2);
INSERT INTO t1 VALUES(1, 1, 1), (2, 2, 2);
} {}
do_execsql_test 1.1 {
SELECT * FROM t1
} {1 1.0 1.0 2 2.0 2.0}
# If a ROLLBACK occurs that backs out changes to the RTREE, then
# all pending queries to the RTREE are aborted.
#
do_test 1.2 {
db eval {
BEGIN;
INSERT INTO t1 VALUES(3, 3, 3);
INSERT INTO t1 VALUES(4, 4, 4);
}
set rc [catch {
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK }
}
lappend res $id $x1 $x2
}
} msg]
list $rc $msg
} {1 {abort due to ROLLBACK}}
do_execsql_test 1.3 {
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0}
# A COMMIT of changes to the RTREE does not affect pending queries
#
do_test 1.4 {
set res {}
db eval {
BEGIN;
INSERT INTO t1 VALUES(5, 5, 5);
INSERT INTO t1 VALUES(6, 6, 6);
}
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { COMMIT }
}
lappend res $id $x1 $x2
}
set res
} {1 1.0 1.0 2 2.0 2.0 5 5.0 5.0 6 6.0 6.0}
do_execsql_test 1.5 {
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 5 5.0 5.0 6 6.0 6.0}
do_execsql_test 1.6 {
DELETE FROM t1;
INSERT INTO t1 VALUES(1,1,1),(2,2,2),(3,3,3),(4,4,4);
CREATE TABLE t2(x);
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0 4 4.0 4.0}
# A rollback that does not affect the rtree table because
# the rtree table has not been written to does not cause
# a query abort.
#
do_test 1.7 {
set res {}
db eval {
BEGIN;
INSERT INTO t2(x) VALUES(12345);
}
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK }
}
lappend res $id $x1 $x2
}
set res
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0 4 4.0 4.0}
# ROLLBACK TO that affects the RTREE does cause a query abort.
#
do_test 1.8 {
db eval {
DELETE FROM t1 WHERE rowid>1;
BEGIN;
DELETE FROM t2;
INSERT INTO t2(x) VALUES(23456);
SAVEPOINT 'one';
INSERT INTO t1 VALUES(2,2,2),(3,3,3);
}
set rc [catch {
db eval { SELECT * FROM t1 } {
if {$id==1} {
db eval { ROLLBACK TO 'one'; }
}
lappend res $id $x1 $x2
}
} msg]
list $rc $msg
} {1 {abort due to ROLLBACK}}
do_execsql_test 1.9 {
COMMIT;
SELECT * FROM t1;
} {1 1.0 1.0}
# ROLLBACK TO that does not affect the RTREE does not cause a query abort.
#
do_execsql_test 1.10 {
DELETE FROM t1;
INSERT INTO t1 VALUES(1,1,1),(2,2,2),(3,3,3);
BEGIN;
DELETE FROM t2;
INSERT INTO t2(x) VALUES(34567);
SAVEPOINT 'one';
INSERT INTO t2(x) VALUES('a string');
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}
do_test 1.11 {
set rc [catch {
set res {}
db eval { SELECT * FROM t1 } {
if {$id==2} {
# db eval { ROLLBACK TO 'one'; }
}
lappend res $id $x1 $x2
}
set res
} msg]
list $rc $msg
} {0 {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}}
do_execsql_test 1.12 {
COMMIT;
SELECT * FROM t1;
} {1 1.0 1.0 2 2.0 2.0 3 3.0 3.0}
finish_test