cockroachdb / cockroachdb/cockroach

plpgsql: error for txn control inside exception-handling block is eager

Open
#119,750 0 comments 0 reactions 0 assignees View on GitHub
A-sql-plpgsql C-bug T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

This is a tracking issue for a difference in behavior between CRDB and postgres. Postgres does not allow COMMIT or ROLLBACK statements inside a PL/pgSQL block that has an exception handler. However, postgres checks this condition lazily, so that the error is only thrown if the transaction control statement is actually executed:
```
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
IF a > 0 THEN
COMMIT;
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo';
END;
$$;

CALL p(0); -- Postgres does not raise an error.
CALL p(1); -- Postgres raises an error.
```
CRDB eagerly checks for this situation, and throws the error in response to the `CREATE PROCEDURE` statement instead. This behavior is unlikely to change because users are unlikely to want to write such a stored procedure.

Jira issue: CRDB-36295

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.