cockroachdb / cockroachdb/cockroach
sql: PL/pgSQL WHILE loops lose error CONTEXT reporting
- 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
Assessment
This issue has not been assessed yet.