Skip to content

sql: add support for holdable cursors#141943

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
DrewKimball:holdable-cursor
Feb 25, 2025
Merged

sql: add support for holdable cursors#141943
craig[bot] merged 2 commits intocockroachdb:masterfrom
DrewKimball:holdable-cursor

Conversation

@DrewKimball
Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball commented Feb 24, 2025

sql: refactor persistent cursor helper

This commit refactors plpgsqlCursorHelper so that the common logic
needed for a cursor stored in a row container now lives on the
embedded persistedCursorHelper. This will be useful for the following
commit that will implement holdable cursors.

Informs #77101

Release note: None

sql: support holdable cursors

This commit adds support for holdable cursors with the following syntax:

DECLARE <name> CURSOR WITH HOLD FOR <query>;

A holdable cursor is read into a row container upon txn commit, and
remains active on the session until it or the session is closed.
A transaction rollback drops any cursors opened within that transaction,
holdable or not. Rollback does not drop holdable cursors from previous
transactions. Prepared transactions cannot create holdable cursors.

Fixes #77101

Release note (sql change): Holdable cursors declared using CURSOR WITH HOLD
are now supported. A holdable cursor fully executes a query upon txn commit
and stores the result in a row container, which is maintained by the
session.

This commit refactors `plpgsqlCursorHelper` so that the common logic
needed for a cursor stored in a row container now lives on the
embedded `persistedCursorHelper`. This will be useful for the following
commit that will implement holdable cursors.

Informs cockroachdb#77101

Release note: None
@DrewKimball DrewKimball requested review from a team and mw5h and removed request for a team February 24, 2025 19:38
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :lgtm:

Reviewed 2 of 2 files at r1, 5 of 5 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mw5h)


pkg/sql/sql_cursor.go line 380 at r2 (raw file):

	//   * The following rules apply only to cursors opened in the current txn:
	//   * txnCommit: close non-holdable cursors, persist holdable cursors.
	//   * txnRollback: close all cursors.

Is this correct? Holdable cursors opened outside of (i.e. before) the current txn shouldn't be closed when the current txn rolls back, right?

This commit adds support for holdable cursors with the following syntax:
```
DECLARE <name> CURSOR WITH HOLD FOR <query>;
```
A holdable cursor is read into a row container upon txn commit, and
remains active on the session until it or the session is closed.
A transaction rollback drops any cursors opened within that transaction,
holdable or not. Rollback does not drop holdable cursors from previous
transactions. Prepared transactions cannot create holdable cursors.

Fixes cockroachdb#77101

Release note (sql change): Holdable cursors declared using `CURSOR WITH HOLD`
are now supported. A holdable cursor fully executes a query upon txn commit
and stores the result in a row container, which is maintained by the
session.
Copy link
Copy Markdown
Collaborator Author

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mw5h and @yuzefovich)


pkg/sql/sql_cursor.go line 380 at r2 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Is this correct? Holdable cursors opened outside of (i.e. before) the current txn shouldn't be closed when the current txn rolls back, right?

That's right. It's a confusing comment - I'll reword it.

@DrewKimball DrewKimball force-pushed the holdable-cursor branch 2 times, most recently from 7558729 to a31c203 Compare February 24, 2025 23:33
@DrewKimball DrewKimball requested a review from a team as a code owner February 24, 2025 23:33
@DrewKimball
Copy link
Copy Markdown
Collaborator Author

I also found that I had to remove the Mutation tag from the optimizer Lock operator, since otherwise it's considered a mutation operator and incorrectly triggers errors when attempting to open a PL/pgSQL cursor that locks some rows.

Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 5 files at r5, 3 of 3 files at r6, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @mw5h)

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

I'm going to move the third commit to another PR. It's logically distinct, and the changes are going to be more extensive than I'd thought.

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

bors r=yuzefovich

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 25, 2025

Build failed:

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

bors retry

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 25, 2025

Build failed:

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

bors retry

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 25, 2025

@craig craig bot merged commit 8cd9b03 into cockroachdb:master Feb 25, 2025
21 checks passed
@DrewKimball DrewKimball deleted the holdable-cursor branch February 25, 2025 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support DECLARE cursor WITH HOLD

3 participants