Re: Per-thread leak in ECPG's memory.c

From: Thom Brown <thom(at)linux(dot)com>
To: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Per-thread leak in ECPG's memory.c
Date: 2026-06-30 01:21:20
Message-ID: CAA-aLv5krwC-5G=i73gM2g_szza92ascKFaHS8Efi5hpKTrW8g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, 30 Jun 2026 at 00:24, Thomas Munro <thomas(dot)munro(at)gmail(dot)com> wrote:
>
> On Tue, Jun 30, 2026 at 2:31 AM Thom Brown <thom(at)linux(dot)com> wrote:
> > On Mon, 29 Jun 2026 at 14:09, Thomas Munro <thomas(dot)munro(at)gmail(dot)com> wrote:
> > > Hmm, I wonder why ecpg_raise() frees auto-allocated values for all
> > > connections just because one connection raised an error.
>
> > Digging into bug reports from that time, we get:
> > https://www.postgresql.org/message-id/200611071423.kA7ENpJ1080586%40wwwmaster.postgresql.org
> >
> > "When using more than one database connection with ECPG, you might have
> > obtained and freed blocks of data on one connection before trying to open
> > the other.
> > If the second connection fails, ECPGraise will be called and call
> > ECPGfree_auto_mem. This can cause an invalid free() of a pointer you've
> > already freed."
>
> Thanks for finding that. Hmm, OK, but I was wondering about the
> opposite scenario, where you *haven't* freed blocks of data before
> doing something on another connection that frees everything for the
> thread:
>
> EXEC SQL AT con1 SELECT datname INTO :anything FROM pg_database;
> EXEC SQL AT con2 ... something that reaches ecpg_raise() ...
>
> /* Why should "anything" not be accessible, and mine to free(), here? */

I see, yeah, that does seem wrong. So would it just be a matter of
moving ecpg_clear_auto_mem() out of ecpg_do_prologue() and put it into
ecpg_do_epilogue() so that it cleans up at the end of the statement
instead of the start of the next one?

Thom

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daisuke Higuchi 2026-06-30 01:29:07 Re: [PATCH] Fix sequence value may rollback after CREATE DATABASE TEMPLATE with WAL_LOG strategy
Previous Message Yilin Zhang 2026-06-30 01:20:20 Re:Re: COPY FROM with RLS