select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

FUNCTIONS / MATHEMATICAL FUNCTIONS AND OPERATORS

Returns the number of the bucket in which operand falls in a histogram having count equal-width buckets spanning the range low to high.

Signatures & examples

width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer
Returns the number of the bucket in which operand falls in a histogram having count equal-width buckets spanning the range low to high. The buckets have inclusive lower bounds and exclusive upper bounds. Returns 0 for an input less than low, or count+1 for an input greater than or equal to high. If low > high, the behavior is mirror-reversed, with bucket 1 now being the one just below low, and the inclusive bounds now being on the upper side.
width_bucket(5.35, 0.024, 10.06, 5) → 3
width_bucket(9, 10, 0, 10) → 2
width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer
Returns the number of the bucket in which operand falls in a histogram having count equal-width buckets spanning the range low to high. The buckets have inclusive lower bounds and exclusive upper bounds. Returns 0 for an input less than low, or count+1 for an input greater than or equal to high. If low > high, the behavior is mirror-reversed, with bucket 1 now being the one just below low, and the inclusive bounds now being on the upper side.
width_bucket(5.35, 0.024, 10.06, 5) → 3
width_bucket(9, 10, 0, 10) → 2
width_bucket ( operand anycompatible, thresholds anycompatiblearray ) → integer
Returns the number of the bucket in which operand falls given an array listing the inclusive lower bounds of the buckets. Returns 0 for an input less than the first lower bound. operand and the array elements can be of any type having standard comparison operators. The thresholds array must be sorted, smallest first, or unexpected results will be obtained.
width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) → 2
Version history 3
  1. PG 13 → 14changed
  2. PG 12 → 13changed
  3. PG 9.4 → 9.5changed

Definition snapshot: 2026-09-14T13:14:25.684213+00:00@99e6866c · English manual source