Re: Valgrind - showing memory leaks

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Cc: Yasir <yasir(dot)hussain(dot)shah(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Valgrind - showing memory leaks
Date: 2025-05-08 20:37:26
Message-ID: 224556.1746736646@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

=?utf-8?Q?=C3=81lvaro?= Herrera <alvherre(at)kurilemu(dot)de> writes:
> If the table doesn't have check constraints, we end up doing
> MemoryContextAllocZero() with size 0 in CacheMemoryContext, which isn't
> great (IIUC we innecessarily allocate a chunk of size 8 in this case).
> I think we should make the allocation conditional on nchecks not being
> zero, otherwise I think we're indeed leaking memory permanently in
> CacheMemoryContext, since that allocation is not recorded anywhere:

Uh ... yeah it is, down at the bottom of the function:

/* Install array only after it's fully valid */
relation->rd_att->constr->check = check;
relation->rd_att->constr->num_check = found;

So it seems like valgrind is wrong here, or else we're leaking the
whole rd_att structure later on somehow.

In any case, you're right that asking for a zero-size chunk is
pretty pointless. I'd support doing

+ if (ncheck > 0)
+ check = (ConstrCheck *)
+ MemoryContextAllocZero(CacheMemoryContext,
+ ncheck * sizeof(ConstrCheck));
+ else
+ check = NULL;

but I think we have to make sure it's null if we don't palloc it.

> On the other hand, the bug I was thinking about, is that if the table
> has an invalid not-null constraint, we leak during detoasting in
> extractNotNullColumn(). We purposefully made that function leak that
> memory, because it was only used in DDL code (so the leak didn't
> matter), and to simplify code; commit ff239c3bf4e8. This uses the
> caller memory context, so it's not a permanent leak and I don't think we
> need any fixes. But it's no longer so obvious that extractNotNullColumn
> is okay to leak those few bytes.

Given your description it still sounds fine to me.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-05-08 20:48:27 Re: disabled SSL log_like tests
Previous Message Álvaro Herrera 2025-05-08 20:25:44 Re: Valgrind - showing memory leaks