PG
PRO
2201GERRORTier 2 — Caution✅ HIGH confidence

invalid argument for width_bucket function

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 2201G is raised when WIDTH_BUCKET() receives invalid arguments — for example, a count of zero or fewer buckets, or a lower bound that equals the upper bound.

Why it happens

  1. 1Calling WIDTH_BUCKET with a non-positive bucket count
  2. 2Calling WIDTH_BUCKET where low equals high (empty range)

How to reproduce

WIDTH_BUCKET with zero buckets.

trigger — this will ERROR
SELECT WIDTH_BUCKET(value, 0, 100, 0) FROM data; -- 0 buckets invalid
ERROR: count must be greater than zero

Fix 1: Ensure bucket count is a positive integer and bounds are distinct

When using WIDTH_BUCKET in reporting or histogram queries.

fix
SELECT WIDTH_BUCKET(value, 0, 100, 10) FROM data; -- 10 buckets

Why this works

A positive bucket count and distinct low/high bounds are required for WIDTH_BUCKET to partition the range correctly.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for WIDTH_BUCKET argument validation. Stable across versions.

See also

📄 Reference pages

Mathematical FunctionsWIDTH_BUCKET
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →