| From: | Björn Kautler <Bjoern(at)kautler(dot)net> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | GROUP BY in CTE causes ELSE in outer query to be prematurely evaluated |
| Date: | 2026-03-04 10:46:12 |
| Message-ID: | CAKChYSo2n2_q_XtvO-3ow+Q0dSuWdzwydLbtr3Q12X65bqhL=g@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hi
If you have the query
WITH FOO AS (
SELECT 0 AS GROUPING
)
SELECT CASE
WHEN GROUPING >= 0 THEN 'non-negative'
ELSE CAST((1 / 0) AS VARCHAR)
END
FROM FOO;
it works successfully, having the ELSE as a safeguard against having coded
a bug, having forgotten a WHEN branch, so it fails fast.
So if you have
WITH FOO AS (
SELECT -1 AS GROUPING
)
SELECT CASE
WHEN GROUPING >= 0 THEN 'non-negative'
ELSE CAST((1 / 0) AS VARCHAR)
END
FROM FOO;
it fails with a division by zero error.
But if you have the query
WITH FOO AS (
SELECT 0 AS GROUPING
GROUP BY 1
)
SELECT CASE
WHEN GROUPING >= 0 THEN 'non-negative'
ELSE CAST((1 / 0) AS VARCHAR)
END
FROM FOO;
then it always fails with division by zero error, even though the result
should still be 'non-negative'.
Cheers
Björn
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Laurenz Albe | 2026-03-04 12:55:53 | Re: GROUP BY in CTE causes ELSE in outer query to be prematurely evaluated |
| Previous Message | PG Bug reporting form | 2026-03-04 08:43:13 | BUG #19425: Parametric settings in collation not working in rule syntax |