Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xRn86-00000001Jtu-1FZl for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Nov 2026 08:59:30 +0000 Received: from localhost ([127.0.0.1]:42546 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xRn84-00000009QYb-3e9g for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Nov 2026 08:59:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:42788) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xReRh-00000005nlT-0phn for pgsql-bugs@lists.postgresql.org; Thu, 12 Nov 2026 23:43:09 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xReRe-00000000lBA-29lF for pgsql-bugs@lists.postgresql.org; Thu, 12 Nov 2026 23:43:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=3w4DwuE1WfageDBcCAFkhNI4MzdomupVgB+cQn+96lA=; b=h4YhgMyglP5o3lAG9k3Ciwl7O0 6maW46273LZtpIPpKdMDBLV2u0J8zTxwuIi9xvjSFmdvtFTdgUntGsQ6P/vcjbGzdtdROxk+xJ7kc dz5CuupyhYh9U5bjqJvHGYaOsGGMIBumcdBUJJyU+EDGVLYWGF8dXs2siAbhCh/mcIpQHcPXHX+rb LMasDoFdoVENf/hhC8uowIUJwQdXEoz2QYWfY/t3j8Fy4DHqWPuu5goySvI7iPZCUBcfKpa3Up3ke 0h3Ujm5oYPZcyZ4VV4qBcEYcE6oDi43JsRsCmJ8z/MBcWLE2xrrmi7ERIBijarIvmoqVHQ34x5v+e fkbMm9Ug==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xReRc-00000001jdk-3QMs for pgsql-bugs@lists.postgresql.org; Thu, 12 Nov 2026 23:43:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xReRb-00000004MqD-2dAM for pgsql-bugs@lists.postgresql.org; Thu, 12 Nov 2026 23:43:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19898: int4_avg_accum_inv wraps below INT64_MIN instead of raising an error To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: imchifan8@gmail.com Reply-To: imchifan8@gmail.com, pgsql-bugs@lists.postgresql.org Date: Thu, 12 Nov 2026 23:42:08 +0000 Message-ID: <19898-5617a53e503f6184@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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: =20 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.