cockroachdb / cockroachdb/cockroach

sql: PL/pgSQL WHILE loops lose error CONTEXT reporting

Open
#172,493 1 comment 0 reactions 1 assignee Claimed by @amunnolimath-crl View on GitHub
A-sql-plpgsql A-sql-routine branch-release-26.3 C-bug O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Error CONTEXT reporting is lost when using WHILE loops. Here's an example:

```sql
-- this UDF uses LOOP and reports the context of the division by zero error
CREATE FUNCTION f_loop(n INT) RETURNS INT AS $$
DECLARE
x INT := 0;
BEGIN
LOOP
x := x + 1 / n;
EXIT;
END LOOP;
RETURN x;
END
$$ LANGUAGE PLpgSQL;
SELECT f_loop(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
-- CONTEXT: PL/pgSQL function f_loop(bigint) line 4 at LOOP

-- this equivalent UDF uses WHILE and does not report the context of the division by zero error
CREATE FUNCTION f_while(n INT) RETURNS INT AS $$
DECLARE
x INT := 0;
BEGIN
WHILE x < 1 LOOP
x := x + 1 / n;
EXIT;
END LOOP;
RETURN x;
END
$$ LANGUAGE PLpgSQL;
SELECT f_while(0);
-- ERROR: division by zero
-- SQLSTATE: 22012
```

Jira issue: CRDB-65737

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.