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: transactions that enter the "aborted" state due to SQL-level errors should relinquish locks #140234

Closed
mgartner opened this issue Jan 31, 2025 · 4 comments · Fixed by #140160
Assignees
Labels
branch-release-20.1 Used to mark GA and release blockers, technical advisories, and bugs for 20.1 branch-release-20.2 Used to mark GA and release blockers, technical advisories, and bugs for 20.2 branch-release-21.1 Used to mark GA and release blockers, technical advisories, and bugs for 21.1 branch-release-21.2 Used to mark GA and release blockers, technical advisories, and bugs for 21.2 branch-release-22.1 Used to mark GA and release blockers, technical advisories, and bugs for 22.1 branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 branch-release-23.1 Used to mark GA and release blockers, technical advisories, and bugs for 23.1 branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 branch-release-24.3 Used to mark GA and release blockers, technical advisories, and bugs for 24.3 branch-release-25.1 C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@mgartner
Copy link
Collaborator

mgartner commented Jan 31, 2025

Since 719b702, which added support for savepoints, SQL-level errors that cause a transaction to enter the "aborted" state do not cause the transaction to end or rollback. As a result, any locks acquired in the transaction prior to the error remain held, blocking concurrent queries.

Here's an example:

CREATE TABLE t (k INT PRIMARY KEY, v INT);
-- CREATE

INSERT INTO t VALUES (1, 0);
-- INSERT 1

-- Session 1
BEGIN;
-- BEGIN

-- Session 1
UPDATE t SET v = 10 WHERE k = 1;
-- UPDATE 1

-- Session 1
SELECT 1/0;
-- ERROR: division by zero
-- SQLSTATE: 22012

-- Session 2
UPDATE t SET v = 100 WHERE k = 1;
-- Blocked by the "aborted" txn above.

-- Session 1
SELECT 1;
-- ERROR: current transaction is aborted, commands ignored until end of transaction block
-- SQLSTATE: 25P02

Once the transaction enters the "aborted" state, I believe it can never successfully commit, so holding the locks has no benefit and increases the chance of contention. This may only be true for transactions without savepoints—we should think through all possible cases here.

This creates some major rough edges with the transaction_timeout session setting. The docs state about this setting:

Aborts an explicit transaction when it runs longer than the configured duration. Stored in milliseconds; can be expressed in milliseconds or as an INTERVAL.

The name of the setting and this description make it sound like transaction_timeout is the perfect safeguard against a misbehaving application that fails to commit or rollback a transaction. However, due to the behavior described above, it actually provides no additional safety—while the transaction will be automatically aborted, any locks acquired will remain held and will not be released until the connection is closed.

Here's an example:

CREATE TABLE t (k INT PRIMARY KEY, v INT);
-- CREATE

-- Session 1
SET transaction_timeout = '8s';
-- SET

-- Session 1
BEGIN;
-- BEGIN

-- Session 1
UPDATE t SET v = 33 WHERE k =3;
-- UPDATE 1

-- Session 2
UPDATE t SET v = 333 WHERE k =3;
-- Hangs until the transaction in Session 1 is manually COMMITed or ABORTed.

-- Session 1
SELECT pg_sleep(100);
-- ERROR: query execution canceled due to transaction timeout
-- SQLSTATE: 57014

-- Session 1
SELECT 1;
-- ERROR: current transaction is aborted, commands ignored until end of transaction block

Jira issue: CRDB-47066

@mgartner mgartner added branch-release-22.1 Used to mark GA and release blockers, technical advisories, and bugs for 22.1 branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 branch-release-23.1 Used to mark GA and release blockers, technical advisories, and bugs for 23.1 branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-21.2 Used to mark GA and release blockers, technical advisories, and bugs for 21.2 branch-release-21.1 Used to mark GA and release blockers, technical advisories, and bugs for 21.1 branch-release-20.2 Used to mark GA and release blockers, technical advisories, and bugs for 20.2 branch-release-20.1 Used to mark GA and release blockers, technical advisories, and bugs for 20.1 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 branch-release-24.3 Used to mark GA and release blockers, technical advisories, and bugs for 24.3 branch-release-25.1 C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) T-sql-queries SQL Queries Team labels Jan 31, 2025
Copy link

blathers-crl bot commented Jan 31, 2025

This issue has multiple T-eam labels. Please make sure it only has one, or else issue synchronization will not work correctly.

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

@github-project-automation github-project-automation bot moved this to Triage in SQL Queries Jan 31, 2025
Copy link

blathers-crl bot commented Jan 31, 2025

This issue has multiple T-eam labels. Please make sure it only has one, or else issue synchronization will not work correctly.

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

@mgartner
Copy link
Collaborator Author

#140160 is a prototype of a fix for this.

See this internal Slack thread for more discussion: https://cockroachlabs.slack.com/archives/C0KB9Q03D/p1738249767226979

@mgartner
Copy link
Collaborator Author

Related to cockroachdb/docs#19330.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch-release-20.1 Used to mark GA and release blockers, technical advisories, and bugs for 20.1 branch-release-20.2 Used to mark GA and release blockers, technical advisories, and bugs for 20.2 branch-release-21.1 Used to mark GA and release blockers, technical advisories, and bugs for 21.1 branch-release-21.2 Used to mark GA and release blockers, technical advisories, and bugs for 21.2 branch-release-22.1 Used to mark GA and release blockers, technical advisories, and bugs for 22.1 branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 branch-release-23.1 Used to mark GA and release blockers, technical advisories, and bugs for 23.1 branch-release-23.2 Used to mark GA and release blockers, technical advisories, and bugs for 23.2 branch-release-24.1 Used to mark GA and release blockers, technical advisories, and bugs for 24.1 branch-release-24.2 Used to mark GA and release blockers, technical advisories, and bugs for 24.2 branch-release-24.3 Used to mark GA and release blockers, technical advisories, and bugs for 24.3 branch-release-25.1 C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants