cockroachdb / cockroachdb/cockroach

sql: PL/pgSQL error CONTEXT attached to continuations causes incorrect or missing statement attribution

Open
#172,495 0 comments 0 reactions 1 assignee Claimed by @michae2 View on GitHub
A-sql-plpgsql A-sql-routine branch-release-26.3 C-bug docs-known-limitation O-qa T-sql-queries
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

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.