forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plpgsql: implement rollback behavior for EXCEPTION blocks
When an exception occurs within the EXCEPTION block of a PLpgSQL routine, all changes to database state are rolled back, but the updated values for the variables are still visible to the exception handler. For example: ``` CREATE FUNCTION f() RETURNS INT AS $$ DECLARE i INT; BEGIN INSERT INTO xy VALUES (-100, -100); i := 100; RETURN 1 // 0; EXCEPTION WHEN division_by_zero THEN RETURN i; END $$ LANGUAGE PLpgSQL; ``` The above function would not modify `xy`, since the exception would revert the insert. However, the function would return `100`, since the variable assignment occurred before the error was thrown. This patch implements the required rollback behavior through internal savepoints; when the top-level routine of a PLpgSQL block begins executing, it creates a savepoint (if it has an exception handler). When the exception handler catches an error, it calls `RollbackSavepoint` to revert any changes to database state within the scope of the block. Note that some errors can leave the transaction in a poisoned state, where immediately rolling back the nested transaction is not possible. The transaction can be returned to a normal state in some cases (as is done for the read committed retry loop), but this will be left as a TODO in 23.2, tracked in cockroachdb#111446. Until this is fixed, catching `40001` and `40003` errors is not permitted. Fixes cockroachdb#105253 Release note (sql change): When a PLpgSQL exception handler catches an error, it now rolls back any changes to database state that occurred within the block. Exception blocks are not currently permitted to catch `40001` and `40003` errors.
- Loading branch information
1 parent
dc7642d
commit 9d9a3c8
Showing
19 changed files
with
605 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.