]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Doc: improve documentation about width_bucket().
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 21 Jun 2025 16:52:37 +0000 (12:52 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 21 Jun 2025 16:52:37 +0000 (12:52 -0400)
Specify whether the bucket bounds are inclusive or exclusive,
and improve some other vague language.  Explain the behavior that
occurs when the "low" bound is greater than the "high" bound.
Make width_bucket_numeric's comment more like that for
width_bucket_float8, in particular noting that infinite
bounds are rejected (since they became possible in v14).

Reported-by: Ben Peachey Higdon <bpeacheyhigdon@gmail.com>
Author: Robert Treat <rob@xzilla.net>
Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Discussion: https://postgr.es/m/2BD74F86-5B89-4AC1-8F13-23CED3546AC1@gmail.com
Backpatch-through: 13

doc/src/sgml/func.sgml
src/backend/utils/adt/float.c
src/backend/utils/adt/numeric.c

index 58fe550d0d0c1bcd07335d5ea484738782084bd0..b279be73e60b1e145cef192955de8d2e3dc2561a 100644 (file)
@@ -1788,13 +1788,23 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
         which <parameter>operand</parameter> falls in a histogram
         having <parameter>count</parameter> equal-width buckets spanning the
         range <parameter>low</parameter> to <parameter>high</parameter>.
-        Returns <literal>0</literal>
+        The buckets have inclusive lower bounds and exclusive upper bounds.
+        Returns <literal>0</literal> for an input less
+        than <parameter>low</parameter>,
         or <literal><parameter>count</parameter>+1</literal> for an input
-        outside that range.
+        greater than or equal to <parameter>high</parameter>.
+        If <parameter>low</parameter> &gt; <parameter>high</parameter>,
+        the behavior is mirror-reversed, with bucket <literal>1</literal>
+        now being the one just below <parameter>low</parameter>, and the
+        inclusive bounds now being on the upper side.
        </para>
        <para>
         <literal>width_bucket(5.35, 0.024, 10.06, 5)</literal>
         <returnvalue>3</returnvalue>
+       </para>
+       <para>
+        <literal>width_bucket(9, 10, 0, 10)</literal>
+        <returnvalue>2</returnvalue>
        </para></entry>
       </row>
 
@@ -1806,8 +1816,8 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
        <para>
         Returns the number of the bucket in
         which <parameter>operand</parameter> falls given an array listing the
-        lower bounds of the buckets.  Returns <literal>0</literal> for an
-        input less than the first lower
+        inclusive lower bounds of the buckets.
+        Returns <literal>0</literal> for an input less than the first lower
         bound.  <parameter>operand</parameter> and the array elements can be
         of any type having standard comparison operators.
         The <parameter>thresholds</parameter> array <emphasis>must be
index 6a717f19bba0b0eb183af2a9ed50d6b5b5c96e9f..896915aed325c393ee41b782412790cfa1eb945e 100644 (file)
@@ -3918,8 +3918,8 @@ float84ge(PG_FUNCTION_ARGS)
  * in the histogram. width_bucket() returns an integer indicating the
  * bucket number that 'operand' belongs to in an equiwidth histogram
  * with the specified characteristics. An operand smaller than the
- * lower bound is assigned to bucket 0. An operand greater than the
- * upper bound is assigned to an additional bucket (with number
+ * lower bound is assigned to bucket 0. An operand greater than or equal
+ * to the upper bound is assigned to an additional bucket (with number
  * count+1). We don't allow "NaN" for any of the float8 inputs, and we
  * don't allow either of the histogram bounds to be +/- infinity.
  */
index e222308ed60be34a5f1574494fbea1c5b8eb8539..eb2541f8ed19ea757d5557f886bf951860696292 100644 (file)
@@ -1495,9 +1495,9 @@ generate_series_step_numeric(PG_FUNCTION_ARGS)
  * in the histogram. width_bucket() returns an integer indicating the
  * bucket number that 'operand' belongs to in an equiwidth histogram
  * with the specified characteristics. An operand smaller than the
- * lower bound is assigned to bucket 0. An operand greater than the
- * upper bound is assigned to an additional bucket (with number
- * count+1). We don't allow "NaN" for any of the numeric arguments.
+ * lower bound is assigned to bucket 0. An operand greater than or equal
+ * to the upper bound is assigned to an additional bucket (with number
+ * count+1). We don't allow "NaN" for any of the numeric inputs.
  */
 Datum
 width_bucket_numeric(PG_FUNCTION_ARGS)