pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: imchifan8@gmail.com
Subject: BUG #19898: int4_avg_accum_inv wraps below INT64_MIN instead of raising an error
Date: Thu, 12 Nov 2026 23:42:08 +0000
Message-ID: <19898-5617a53e503f6184@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      19898
Logged by:          Noah
Email address:      imchifan8@gmail.com
PostgreSQL version: 18.6
Operating system:   Linux/amd64
Description:        

pg_catalog.int4_avg_accum_inv silently wraps an inverse-transition sum below
INT64_MIN. Given a reachable state with count 4294967298 and sum INT64_MIN,
removing the input value 1 returns a sum of INT64_MAX instead of reporting
numeric-value-out-of-range.

The state is mathematically reachable by adding 1, then 2^32 copies of
INT32_MIN, then -1. Removing the leading 1 makes the exact next sum
INT64_MIN - 1, which cannot be represented by bigint and should raise an
out-of-range error.

How to repeat:

\set ON_ERROR_STOP on
-- This state is mathematically reachable: +1, followed by 2^32 copies of
-- INT32_MIN, followed by -1, yields count 4294967298 and sum INT64_MIN.
-- Removing the leading +1 makes the exact sum INT64_MIN - 1.
SELECT pg_catalog.int4_avg_accum_inv(
         ARRAY[4294967298::bigint, '-9223372036854775808'::bigint],
         1::integer) AS inverse_state;

The output was:

          inverse_state
----------------------------------
 {4294967297,9223372036854775807}
(1 row)

The returned count is reduced by one as expected, but the sum wraps from the
unrepresentable INT64_MIN - 1 result to INT64_MAX.

The issue was reproduced on PostgreSQL 18.6 on Linux/amd64. This occurs when
an int4 moving aggregate removes a value from a reachable state whose exact
next sum is below INT64_MIN.








view thread (2+ messages)  latest in thread

Message-ID: <19898-5617a53e503f6184@postgresql.org>
Permalink:  ../19898-5617a53e503f6184@postgresql.org/
Also on:    postgresql.org/message-id/19898-5617a53e503f6184@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, imchifan8@gmail.com
  Subject: Re: BUG #19898: int4_avg_accum_inv wraps below INT64_MIN instead of raising an error
  In-Reply-To: <19898-5617a53e503f6184@postgresql.org>
  References: <19898-5617a53e503f6184@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox