Skip to content

sql: allow cursor WITH HOLD inside of txn as long as it gets closed#94127

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:with-hold
Dec 27, 2022
Merged

sql: allow cursor WITH HOLD inside of txn as long as it gets closed#94127
craig[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:with-hold

Conversation

@rafiss
Copy link
Copy Markdown
Collaborator

@rafiss rafiss commented Dec 22, 2022

informs #77101

The DECLARE foo CURSOR WITH HOLD ... syntax is now supported in a limited fashion. Unlike PG's WITH HOLD, this version can only be executed in a transaction block still. If there are any open cursors that have WITH HOLD open at the time a transaction is commited, the COMMIT fails.

No release note, since there's not actually any new functionality.

Release note: None

@rafiss rafiss requested review from jordanlewis and otan December 22, 2022 06:52
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Member

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Looks good, one code organization comment to please take a look at.

ctx, sp := tracing.EnsureChildSpan(ctx, ex.server.cfg.AmbientCtx.Tracer, "commit sql txn")
defer sp.Finish()

for n, c := range ex.extraTxnState.sqlCursors.list() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be better to put this logic nearby where we close all of the cursors, perhaps even inside of closeAll. You might need to pass a boolean to closeAll because we also use it on server shutdown, but I feel this would prevent future refactorings from "forgetting" to do this check.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also need one for rollback too right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah, we'll need to make sure txnRollback and txnRestart transitions are handled too, but that seems fine

@otan
Copy link
Copy Markdown
Contributor

otan commented Dec 22, 2022

was gonna do this today, but thank you :')

name: s.String(),
constructor: func(ctx context.Context, p *planner) (_ planNode, _ error) {
if p.extendedEvalCtx.TxnImplicit {
if s.Hold {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can this no-op? or do we have an internal limitation that cursors require txns?

otan=# declare a cursor with hold for select 1;
DECLARE CURSOR

also wonder if we should have separate issues numbers for this and the closeAll scenario below for telemetry purposes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

by default, cursors require transactions (in PG too). with hold is special because it does not require a transaction, but that is the thing we don't support. no-oping would be confusing IMO, since the user may think that it was created successfully.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

i'm fine with one issue personally, because the underlying limitation for both is "cannot use a WITH HOLD cursor outside of an explicit transaction. both places where we return the unimplemented error will be fixed at the same time.

)
ex.extraTxnState.prepStmtsNamespaceMemAcc.Close(ctx)
ex.extraTxnState.sqlCursors.closeAll()
_ = ex.extraTxnState.sqlCursors.closeAll(false /* errorOnWithHold */)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we log any errors here just in case? same with below.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

The `DECLARE foo CURSOR WITH HOLD ...` syntax is now supported in a
limited fashion. Unlike PG's `WITH HOLD` this version can only be
executed in a transaction block still. If there are any open cursors
that have `WITH HOLD` open at the time a transaction is commited, the
COMMIT fails.

No release note, since there's not actually any new functionality.

Release note: None
@rafiss
Copy link
Copy Markdown
Collaborator Author

rafiss commented Dec 27, 2022

tftr!

bors r=otan

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Dec 27, 2022

Build succeeded:

@craig craig bot merged commit 257872b into cockroachdb:master Dec 27, 2022
@rafiss rafiss deleted the with-hold branch December 29, 2022 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants