cockroachdb / cockroachdb/cockroach
sql: PL/pgSQL error CONTEXT attached to continuations causes incorrect or missing statement attribution
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
The current design of PL/pgSQL error context reporting attaches error context information to PL/pgSQL _continuations_, instead of PL/pgSQL _statements_. This causes inaccurate error reporting for PL/pgSQL statements that don't anchor a continuation (assignments, RETURN, IF, DECLARE, etc):
1. If one of these statements is not inside a continuation, then no error CONTEXT is reported.
2. If one of these statements is inside a continuation, then sometimes the wrong statement is attributed in the CONTEXT.
Here are examples of both:
```sql
-- error context missing when assignment statement is outside a continuation
CREATE FUNCTION f(x INT) RETURNS INT AS $$
DECLARE y INT;
BEGIN
y := 1 / x;
RETURN y;
END
$$ LANGUAGE PLpgSQL;
SELECT f(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
-- same with RETURN statement
CREATE FUNCTION f1(x INT) RETURNS INT AS $$
BEGIN
RETURN 1 / x;
END
$$ LANGUAGE PLpgSQL;
SELECT f1(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
-- error context has wrong line number, points at line 4 (assignment) instead of line 5 (RETURN)
CREATE FUNCTION f2(x INT) RETURNS INT AS $$
DECLARE
y INT;
BEGIN
y := 100 + x;
RETURN y / (x - x);
EXCEPTION WHEN unique_violation THEN
RETURN -1;
END
$$ LANGUAGE PLpgSQL;
SELECT f2(5);
-- ERROR: division by zero
-- SQLSTATE: 22012
-- CONTEXT: PL/pgSQL function f2(bigint) line 4 at assignment
```
Jira issue: CRDB-65739
Contributor guide
Assessment
This issue has not been assessed yet.