2201G
PostgreSQLERRORNotableData ExceptionHIGH confidence

invalid argument for width_bucket function

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
trigger — this will error
SELECT WIDTH_BUCKET(value, 0, 100, 0) FROM data; -- 0 buckets invalid

expected output

ERROR:  count must be greater than zero

Fix

Ensure bucket count is a positive integer and bounds are distinct

WHEN When using WIDTH_BUCKET in reporting or histogram queries.

Ensure bucket count is a positive integer and bounds are distinct
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 documentation ↗

Class 22 — Data Exception

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors