cockroachdb / cockroachdb/cockroach

sql: add full call stack to PL/pgSQL error CONTEXT

Open
#172,536 0 comments 0 reactions 1 assignee Claimed by @michae2 View on GitHub
A-sql-pgcompat A-sql-plpgsql A-sql-routine branch-release-26.3 C-enhancement docs-known-limitation O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

When debugging nested routine calls, it will be handy to have the full call stack in error CONTEXTs, instead of just the last frame. (This might become more important after https://github.com/cockroachdb/cockroach/issues/93049.) Here's an example. In CRDB all four calls fail with the same CONTEXT, but in PG the CONTEXT has multiple stack frames, which is more helpful.

```sql
CREATE FUNCTION safe_divide(numerator INT, denominator INT) RETURNS INT AS $$
BEGIN
IF denominator = 0 THEN
RAISE EXCEPTION 'division by zero';
END IF;
RETURN numerator / denominator;
END
$$ LANGUAGE PLpgSQL;

CREATE FUNCTION compute_ratio(x INT) RETURNS INT AS $$
DECLARE
r INT;
BEGIN
SELECT safe_divide(100, x) INTO r;
SELECT safe_divide(x, x - 1) INTO r;
RETURN r;
END
$$ LANGUAGE PLpgSQL;

CREATE FUNCTION compute_average(total INT, cnt INT) RETURNS INT AS $$
DECLARE
avg INT;
BEGIN
SELECT safe_divide(total, cnt) INTO avg;
RETURN avg;
END
$$ LANGUAGE PLpgSQL;

SELECT safe_divide(1, 0);

SELECT compute_ratio(0);

SELECT compute_ratio(1);

SELECT compute_average(10, 0);
```

Jira issue: CRDB-65784

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.