Re: Avoid memory leaks during base backups

From: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
To: alvherre(at)alvh(dot)no-ip(dot)org
Cc: bharath(dot)rupireddyforpostgres(at)gmail(dot)com, robertmhaas(at)gmail(dot)com, michael(at)paquier(dot)xyz, cary(dot)huang(at)highgo(dot)ca, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Avoid memory leaks during base backups
Date: 2022-10-21 06:41:02
Message-ID: 20221021.154102.1377055013780994404.horikyota.ntt@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

At Thu, 20 Oct 2022 19:47:07 +0200, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org> wrote in
> I agree that's a good idea, and the patch looks good to me, but I don't
> think asserting that they are null afterwards is useful.

+1 for this direction. And the patch is fine to me.

> oldcontext = MemoryContextSwitchTo(backupcontext);
> Assert(backup_state == NULL);
> Assert(tablespace_map == NULL);
> backup_state = (BackupState *) palloc0(sizeof(BackupState));
> tablespace_map = makeStringInfo();
> MemoryContextSwitchTo(oldcontext);

We can use MemoryContextAllocZero() for this purpose, but of couse not
mandatory.

+ * across. We keep the memory allocated in this memory context less,
+ * because any error before reaching pg_backup_stop() can leak the memory
+ * until pg_backup_start() is called again. While this is not smart, it
+ * helps to keep things simple.

I think the "less" is somewhat obscure. I feel we should be more
explicitly. And we don't need to put emphasis on "leak". I recklessly
propose this as the draft.

"The context is intended to be used by this function to store only
session-lifetime values. It is, if left alone, reset at the next call
to blow away orphan memory blocks from the previous failed call.
While this is not smart, it helps to keep things simple."

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2022-10-21 07:10:38 Crash after a call to pg_backup_start()
Previous Message Michael Paquier 2022-10-21 06:28:13 Re: Avoid memory leaks during base backups