cockroachdb / cockroachdb/cockroach

plpgsql: enable catching retryable errors in exception blocks

Open
#111,446 1 comment 0 reactions 0 assignees View on GitHub
A-sql-plpgsql A-sql-udf C-enhancement docs-done docs-known-limitation T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

It is possible to catch and handle arbitrary errors within a routine using a PLpgSQL exception block. Once an error is caught, any changes to database state that occurred within the exception block are rolled back, although the most recent values for the PLpgSQL variables are kept. The rollback behavior for exception handling is implemented in https://github.com/cockroachdb/cockroach/pull/110998, but for 23.2 we will disable catching Transaction Retry (40001, 40003) errors because they leave the transaction in a poisoned state that prevents rolling back to a previously created savepoint.

This issue tracks adding support for catching Transaction Retry errors. The read-committed retry loop is able to revert the transaction to a state where savepoint rollback is valid in some cases using [txn.PrepareForPartialRetry](https://github.com/cockroachdb/cockroach/blob/af05d816a1b0b060947114b22d400862b89383c5/pkg/kv/txn.go#L1130-L1135). We will likely do something similar for routine exception handling. Note that some errors can leave the transaction in a state where it must be restarted completely; we will have to find a user-friendly way to handle these cases.

Jira issue: CRDB-31899

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.