Re: SUM(int2)/SUM(int4) do not detect overflow of the int8 accumulator

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Andrei Lepikhov <lepihov(at)gmail(dot)com>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Andres Freund <andres(at)anarazel(dot)de>, Nathan Bossart <nathandbossart(at)gmail(dot)com>
Subject: Re: SUM(int2)/SUM(int4) do not detect overflow of the int8 accumulator
Date: 2026-09-04 22:45:46
Message-ID: aptKGuwerVQSENDH@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Sep 04, 2026 at 08:55:07PM +1200, David Rowley wrote:
> Since Andrei has demonstrated that it's possible to hit that limit
> with a non-parallel query in less than 5 minutes, albeit that is
> passing INT_MAX (the most extreme case), it might be worth adding the
> checks. I was surprised that it only took 5 mins to do 4 billion rows,
> especially with generate_series. It's probably just a matter of time
> before someone discovers this with a real-world case out in the wild.

I guess so..

> If there's a measurable performance regression from adding the
> overflow checks, does the attached buy enough of it back? I couldn't
> really measure much of a performance difference from it on my Zen2
> machine, so I didn't try with the overflow patch.
>
> The patch adds PG_RETURN_INPUT(n) to avoid some of the branching in
> int4_sum() so that it immediately returns the aggstate when the value
> being aggregated is null. With my compiler, it cut int4_sum from 18
> down to 16 instructions.

FWIW, I've always been a fan of your compiler magic tricks like this
one. Even if you did not measure much of a performance difference at
runtime, less instructions overall across gcc and clang sounds like a
better deal to me anyway? It sounds like the sort of improvements
that could be done independently of what is being discussed here.
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-09-04 23:13:42 Re: REPACK (CONCURRENTLY) doesn't check the table AM
Previous Message Andrew Jackson 2026-09-04 22:42:27 Re: Add PAM Tests and Option For Custom PAM Config Location