Skip to content

Commit 631e617

Browse files
craig[bot]yuzefovich
andcommitted
Merge #145391
145391: sql: fix a bug when persisting some cursors r=yuzefovich a=yuzefovich We recently added support for WITH HOLD cursors which are persisted, which exposed a bug with tracking of the internal position of the current row. IIUC the pre-existing bug didn't have any effect without WITH HOLD option. Fixes: #145362. Release note (bug fix): CockroachDB could previously encounter an internal error when fetching from the WITH HOLD cursor with FETCH FIRST and FETCH ABSOLUTE. The bug is only present in 25.2 alpha and beta versions. Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents d7a829c + ebdd482 commit 631e617

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/sql/logictest/testdata/logic_test/cursor

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,23 @@ statement ok
920920
CLOSE curs;
921921

922922
subtest end
923+
924+
# Regression test for incorrectly advancing internal position when persisting
925+
# the WITH HOLD cursor (#145362).
926+
subtest regression_145362
927+
928+
statement ok
929+
CREATE TABLE empty (k INT PRIMARY KEY)
930+
931+
statement ok
932+
BEGIN;
933+
DECLARE foo CURSOR WITH HOLD FOR SELECT * FROM empty;
934+
COMMIT;
935+
936+
query empty
937+
FETCH ABSOLUTE 1 FROM foo;
938+
939+
statement ok
940+
CLOSE foo;
941+
942+
subtest end

pkg/sql/sql_cursor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ type sqlCursor struct {
365365
// Next implements the Rows interface.
366366
func (s *sqlCursor) Next(ctx context.Context) (bool, error) {
367367
more, err := s.Rows.Next(ctx)
368-
if err == nil {
368+
if more && err == nil {
369369
s.curRow++
370370
}
371371
return more, err

0 commit comments

Comments
 (0)