Subject: Re: pg_stat_get_activity(): integer overflow due to (int) * (int) for MemoryContextAllocHuge()

From: Peter Eisentraut <[email-masked]>
Date: Wed, 27 Sep 2023 15:42:15 +0100

On 27.09.23 09:08, Michael Paquier wrote:
> On Wed, Sep 27, 2023 at 08:41:55AM +0200, Jakub Wartak wrote:
>> Attached patch adjusts pgstat_track_activity_query_size to be of
>> size_t from int and fixes the issue.
> 
> This cannot be backpatched, and using size_t is not really needed as
> track_activity_query_size is capped at 1MB.  Why don't you just tweak
> the calculation done in pgstat_read_current_status() instead, say with
> a cast?

I think it's preferable to use the right type to begin with, rather than 
fixing it up afterwards with casts.

---

From: Andres Freund <[email-masked]>
Date: Wed, 27 Sep 2023 10:29:25 -0700

Hi,

> fixing it up afterwards with casts.

I don't think going for size_t is a viable path for fixing this. I'm pretty
sure the initial patch would trigger a type mismatch from guc_tables.c - we
don't have infrastructure for size_t GUCs.

Nor do I think either of the patches here is a complete fix - there will still
be overflows on 32bit systems.

Perhaps we ought to error out (in BackendStatusShmemSize() or such) if
pgstat_track_activity_query_size * MaxBackends >= 4GB?

Frankly, it seems like a quite bad idea to have such a high limit for
pgstat_track_activity_query_size. The overhead such a high value has will
surprise people...

Greetings,

Andres Freund

---

From: Michael Paquier <[email-masked]>
Date: Thu, 28 Sep 2023 07:53:45 +0900

> don't have infrastructure for size_t GUCs.

Nothing marked as PGDLLIMPORT uses size_t in the tree currently, FWIW.

> pgstat_track_activity_query_size * MaxBackends >= 4GB?

Yeah, agreed that putting a check like that could catch errors more
quickly.

> pgstat_track_activity_query_size. The overhead such a high value has will

Still it could have some value for some users with large analytical
queries where the syslogger is not going to be a bottleneck?  It seems
too late to me to change that, but perhaps the docs could be improved
to tell that using a too high value can have performance consequences,
while mentioning the maximum value.
--
Michael

---

From: Andres Freund <[email-masked]>
Date: Wed, 27 Sep 2023 18:37:11 -0700

Hi,

> while mentioning the maximum value.

I don't think the issue is syslogger, the problem is that suddenly accessing
pg_stat_activity requires gigabytes of memory. That's insane.

Greetings,

Andres Freund