Re: Add tracking of backend memory allocated to pg_stat_activity

From: Andres Freund <andres(at)anarazel(dot)de>
To: Reid Thompson <reid(dot)thompson(at)crunchydata(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, "Drouvot, Bertrand" <bertranddrouvot(dot)pg(at)gmail(dot)com>
Subject: Re: Add tracking of backend memory allocated to pg_stat_activity
Date: 2022-11-09 16:13:49
Message-ID: 20221109161349.gaoisazggtpmp2bj@awork3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

2022-11-09 08:54:54 -0500, Reid Thompson wrote:
> Thanks for looking at this and for the feedback. Responses inline below.
> > > +void
> > > +pgstat_report_backend_allocated_bytes_decrease(uint64
> > > deallocation)
> > > +{
> > > +       volatile PgBackendStatus *beentry = MyBEEntry;
> > > +
> > > +       /*
> > > +        * Cases may occur where shared memory from a previous postmaster
> > > +        * invocation still exist. These are cleaned up at startup by
> > > +        * dsm_cleanup_using_control_segment. Limit decreasing memory allocated to
> > > +        * zero in case no corresponding prior increase exists or decrease has
> > > +        * already been accounted for.
> > > +        */
> >
> > I don't really follow - postmaster won't ever have a backend status
> > array, so how would they be tracked here?
>
> On startup, a check is made for leftover dsm control segments in the
> DataDir. It appears possible that in certain situations on startup we
> may find and destroy stale segments and thus decrement the allocation
> variable.
>
> I based this off of:
> /ipc/dsm.c
>
> dsm_postmaster_startup:
> 150 dsm_postmaster_startup(PGShmemHeader *shim)
> {
> ...
> 281 }

I don't think we should account for memory allocations done in postmaster in
this patch. They'll otherwise be counted again in each of the forked
backends. As this cleanup happens during postmaster startup, we'll have to
make sure accounting is reset during backend startup.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2022-11-09 16:17:29 Re: BUG #17434: CREATE/DROP DATABASE can be executed in the same transaction with other commands
Previous Message Alvaro Herrera 2022-11-09 14:32:07 Re: [PATCH] Teach pg_waldump to extract FPIs from the WAL