BUG #17876: Function width_bucket() for float8 input returns value out of range

The Post Office <noreply@postgresql.org>

From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: mats@timescale.com
Date: 2023-03-29T08:11:54Z
Lists: pgsql-bugs
The following bug has been logged on the website:

Bug reference:      17876
Logged by:          Mats Kindahl
Email address:      mats@timescale.com
PostgreSQL version: 15.2
Operating system:   All
Description:        

On 64-bit platforms, the existing width_bucket() computation can
result in a NaN (0x8000000) if the difference between the `operand`,
`low`, or `high` parameters exceeds DBL_MAX. This is then cast to the
value -2147483648.

    mats=# WITH sample(operand, low, high, cnt) AS (
      VALUES 
	(10.5::float8, -1.797e+308::float8, 1.797e+308::float8, 1::int4),
	(10.5::float8, -1.797e+308::float8, 1.797e+308::float8, 2),
	(10.5::float8, -1.797e+308::float8, 1.797e+308::float8, 3),

	(1.797e+308::float8 / 2, -1.797e+308::float8, 1.797e+308::float8, 10),
	(-1.797e+308::float8 / 2, -1.797e+308::float8, 1.797e+308::float8, 10),

	(1.797e+308::float8 / 2, -1.797e+308::float8, 1.797e+308::float8, 16),
	(-1.797e+308::float8 / 2, -1.797e+308::float8, 1.797e+308::float8, 16),

	(1.797e+308::float8, -1.797e+308::float8 / 2, 1.797e+308::float8 / 2,
10),
	(-1.797e+308::float8, -1.797e+308::float8 / 2, 1.797e+308::float8 / 2,
10)
    ) SELECT width_bucket(operand, low, high, cnt) FROM sample;
     width_bucket 
    --------------
		1
      -2147483648
      -2147483648
      -2147483648
      -2147483648
      -2147483648
      -2147483648
	       11
		0
    (9 rows)

Commits

  1. Further tweaking of width_bucket() edge cases.

  2. Avoid overflow in width_bucket_float8().