Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: mark implicit transactions with AOST as read-only #120097

Merged
merged 2 commits into from Mar 8, 2024

Conversation

michae2
Copy link
Collaborator

@michae2 michae2 commented Mar 8, 2024

sql: mark implicit transactions with AOST as read-only

In handleAOST we were setting the transaction AOST but not marking it as read-only. It needs to also be marked as read-only to disallow mutation statements and locking statements.

Fixes: #120081

Release note (sql change): Mutation statements such as UPDATE and DELETE as well as locking statements such as SELECT FOR UPDATE are not allowed in read-only transactions or AS OF SYSTEM TIME transactions. Fix an oversight where we were allowing mutation statements and locking statements in implicit single-statement transactions using AS OF SYSTEM TIME.


sql: disallow SET transaction_read_only = false during AOST txn

Fixes: #44200

Release note (bug fix): Fix a bug in which it was possible to SET transaction_read_only = false during an AS OF SYSTEM TIME transaction.

@michae2 michae2 requested a review from a team as a code owner March 8, 2024 07:09
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@michae2
Copy link
Collaborator Author

michae2 commented Mar 8, 2024

Should we backport this? I'd like to but am slightly worried that someone is inadvertently already using SELECT ... AS OF SYSTEM TIME ... FOR UPDATE.

@mgartner
Copy link
Collaborator

mgartner commented Mar 8, 2024

Should we backport this? I'd like to but am slightly worried that someone is inadvertently already using SELECT ... AS OF SYSTEM TIME ... FOR UPDATE.

Is the current behavior well-defined and reasonable? What about the current behavior of UPDATE or DELETE statements?

Copy link
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 finds! :lgtm:

I'm guessing the current code could lead to an undefined behavior / broken assumptions somewhere, so I'd lean towards backporting it.

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

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

super small nits. thanks for the fix!

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


pkg/sql/conn_executor_exec.go line 1298 at r1 (raw file):

				return err
			}
			p.extendedEvalCtx.TxnReadOnly = ex.state.readOnly.Load()

nit: i think we may not need p.extendedEvalCtx.TxnReadOnly = ex.state.readOnly.Load() here, since this should happen when reesetPlanner is called later. but i don't think it hurts either.


pkg/sql/exec_util.go line 3306 at r2 (raw file):

	// call into the connEx, which modifies its TxnState.
	// NOTE(andrei): I couldn't find good documentation on transaction_read_only,
	// but I've tested its behavior in Postgres 11.

unrelated to your PR, but this comment could mention that transaction_read_only/default_transaction_read_only have similar behavior as transaction_isolation/default_transaction_isolation. (just to make it seem less like a scary special thing)


pkg/sql/logictest/testdata/logic_test/txn line 1334 at r1 (raw file):


statement error cannot execute DELETE in a read-only transaction
DELETE FROM kv

nit: seems like this is a duplicate of the existing line above this

In `handleAOST` we were setting the transaction AOST but not marking it
as read-only. It needs to also be marked as read-only to disallow
mutation statements and locking statements.

Fixes: cockroachdb#120081

Release note (sql change): Mutation statements such as UPDATE and DELETE
as well as locking statements such as SELECT FOR UPDATE are not allowed
in read-only transactions or AS OF SYSTEM TIME transactions. Fix an
oversight where we were allowing mutation statements and locking
statements in implicit single-statement transactions using AS OF SYSTEM
TIME.
Fixes: cockroachdb#44200

Release note (bug fix): Fix a bug in which it was possible to `SET
transaction_read_only = false` during an AS OF SYSTEM TIME transaction.
@michae2 michae2 added backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2. labels Mar 8, 2024
Copy link
Collaborator Author

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Is the current behavior well-defined and reasonable? What about the current behavior of UPDATE or DELETE statements?

Good point, it is not well-defined. Might even allow corruption. I'll backport.

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


pkg/sql/conn_executor_exec.go line 1298 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: i think we may not need p.extendedEvalCtx.TxnReadOnly = ex.state.readOnly.Load() here, since this should happen when reesetPlanner is called later. but i don't think it hurts either.

I left it in for consistency, since we're setting other fields of the evalctx here.


pkg/sql/exec_util.go line 3306 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

unrelated to your PR, but this comment could mention that transaction_read_only/default_transaction_read_only have similar behavior as transaction_isolation/default_transaction_isolation. (just to make it seem less like a scary special thing)

Good point, done.


pkg/sql/logictest/testdata/logic_test/txn line 1334 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: seems like this is a duplicate of the existing line above this

Whoops, thanks! Done.

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

lgtm! thank you

@michae2
Copy link
Collaborator Author

michae2 commented Mar 8, 2024

YW, TFTRs!

Failure is #119288

bors r=yuzefovich,rafiss

@craig
Copy link
Contributor

craig bot commented Mar 8, 2024

Build succeeded:

@craig craig bot merged commit 72646a5 into cockroachdb:master Mar 8, 2024
18 of 19 checks passed
Copy link

blathers-crl bot commented Mar 8, 2024

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 238ca33 to blathers/backport-release-23.1-120097: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.1.x failed. See errors above.


error creating merge commit from 238ca33 to blathers/backport-release-23.2-120097: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 23.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: disallow SFU with AOST in implicit txn sql: transaction_read_only can be reset in AOST txns
5 participants