Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
PostgreSQL raises 22012 the moment any row divides by zero, for integers and numeric alike. One bad row is enough: the query returns nothing and an UPDATE changes nothing.
The usual fix is NULLIF(divisor, 0), which turns the division into NULL for that row, or a CASE that picks a value you want instead. Below are three cases verified on PostgreSQL 17.
01Rate calculation where the denominator is 0
orders * 100.0 / visits works for the day with 200 visits, but the day with 0 visits makes the whole SELECT fail.
Setup
CREATE TABLE daily_stats (day date PRIMARY KEY, visits int, orders int);
INSERT INTO daily_stats VALUES ('2026-11-28', 200, 10), ('2026-11-29', 0, 0);CREATE TABLE
INSERT 0 2SELECT day, orders * 100.0 / visits AS conv_rate FROM daily_stats ORDER BY day;ERROR: 22012: division by zeroSELECT day, visits FROM daily_stats WHERE visits = 0; day | visits
------------+--------
2026-11-29 | 0
(1 row)Find the rows with WHERE visits = 0. Then wrap the divisor: orders * 100.0 / NULLIF(visits, 0). The 0-visit day shows an empty (NULL) rate and the other day shows 5.0000000000000000.
SELECT day, orders * 100.0 / NULLIF(visits, 0) AS conv_rate FROM daily_stats ORDER BY day; day | conv_rate
------------+--------------------
2026-11-28 | 5.0000000000000000
2026-11-29 |
(2 rows)02UPDATE that divides by a total that can be 0
SET progress = done_tasks * 100 / total_tasks fails because project 2 has total_tasks = 0. The check after the error shows progress is still empty for both rows: nothing was updated, not even project 1.
Setup
CREATE TABLE projects (id int PRIMARY KEY, done_tasks int, total_tasks int, progress numeric);
INSERT INTO projects VALUES (1, 3, 4, NULL), (2, 0, 0, NULL);CREATE TABLE
INSERT 0 2UPDATE projects SET progress = done_tasks * 100 / total_tasks;ERROR: 22012: division by zeroSELECT id, done_tasks, total_tasks, progress FROM projects ORDER BY id; id | done_tasks | total_tasks | progress
----+------------+-------------+----------
1 | 3 | 4 |
2 | 0 | 0 |
(2 rows)Decide what the value should be when the total is 0 and write it with CASE WHEN total_tasks = 0 THEN 0 ELSE done_tasks * 100 / total_tasks END. The UPDATE now changes 2 rows: 75 and 0.
UPDATE projects SET progress = CASE WHEN total_tasks = 0 THEN 0 ELSE done_tasks * 100 / total_tasks END;
SELECT id, done_tasks, total_tasks, progress FROM projects ORDER BY id;UPDATE 2
id | done_tasks | total_tasks | progress
----+------------+-------------+----------
1 | 3 | 4 | 75
2 | 0 | 0 | 0
(2 rows)03Division inside a WHERE condition
WHERE amount / qty > 1000 is evaluated for every row, including the one with qty = 0, so the filter itself raises 22012.
Setup
CREATE TABLE order_lines (id int PRIMARY KEY, amount numeric, qty int);
INSERT INTO order_lines VALUES (1, 5000, 2), (2, 0, 0), (3, 300, 3);CREATE TABLE
INSERT 0 3SELECT id, amount / qty AS unit_price FROM order_lines WHERE amount / qty > 1000;ERROR: 22012: division by zeroSELECT id, amount, qty FROM order_lines WHERE qty = 0; id | amount | qty
----+--------+-----
2 | 0 | 0
(1 row)Put the guard inside the expression: WHERE CASE WHEN qty <> 0 THEN amount / qty END > 1000. For qty = 0 the CASE gives NULL, the row is not matched, and only id 1 (unit price 2500) is returned.
SELECT id, amount / qty AS unit_price FROM order_lines WHERE CASE WHEN qty <> 0 THEN amount / qty END > 1000; id | unit_price
----+-----------------------
1 | 2500.0000000000000000
(1 row)FAQ
How do I avoid division by zero in PostgreSQL?
Use x / NULLIF(y, 0). When y is 0 the result is NULL instead of an error. Wrap it in COALESCE(..., 0) if you need 0 instead of NULL.
Why did my UPDATE change no rows after division by zero?
The statement is atomic. If one row fails with 22012, the whole UPDATE is cancelled, including rows that would have worked.
How do I filter on a division without division by zero?
Put the check inside the expression: CASE WHEN y <> 0 THEN x / y END > 10. Rows with y = 0 become NULL and are simply not matched.
Does 1.0 / 0 behave differently from 1 / 0?
No. Integer and numeric division by zero both raise 22012.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12