Re: Forget close an open relation in ReorderBufferProcessTXN()

From: Amit Langote <amitlangote09(at)gmail(dot)com>
To: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Cc: "osumi(dot)takamichi(at)fujitsu(dot)com" <osumi(dot)takamichi(at)fujitsu(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Japin Li <japinli(at)hotmail(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Forget close an open relation in ReorderBufferProcessTXN()
Date: 2021-05-31 03:21:26
Message-ID: CA+HiwqHLz8Ei-94Diqv9z9Jnk+d53v28x1no4M+wU4TGJgR5kQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, May 27, 2021 at 3:36 PM Amit Kapila <amit(dot)kapila16(at)gmail(dot)com> wrote:
> On Fri, May 21, 2021 at 1:12 PM Amit Langote <amitlangote09(at)gmail(dot)com> wrote:
> >
> > Hmm, maybe get_rel_syn_entry() should explicitly set map to NULL when
> > first initializing an entry. It's possible that without doing so, the
> > map remains set to a garbage value, which causes the invalidation
> > callback that runs into such partially initialized entry to segfault
> > upon trying to deference that garbage pointer.
> >
> > I've tried that in the attached v6 patches. Please check.
> >
>
> v6-0001
> =========
> + send_relation_and_attrs(ancestor, xid, ctx);
> +
> /* Map must live as long as the session does. */
> oldctx = MemoryContextSwitchTo(CacheMemoryContext);
> - relentry->map = convert_tuples_by_name(CreateTupleDescCopy(indesc),
> - CreateTupleDescCopy(outdesc));
> +
> + /*
> + * Make copies of the TupleDescs that will live as long as the map
> + * does before putting into the map.
> + */
> + indesc = CreateTupleDescCopy(indesc);
> + outdesc = CreateTupleDescCopy(outdesc);
> + relentry->map = convert_tuples_by_name(indesc, outdesc);
> + if (relentry->map == NULL)
> + {
> + /* Map not necessary, so free the TupleDescs too. */
> + FreeTupleDesc(indesc);
> + FreeTupleDesc(outdesc);
> + }
> +
> MemoryContextSwitchTo(oldctx);
> - send_relation_and_attrs(ancestor, xid, ctx);
>
> Why do we need to move send_relation_and_attrs() call? I think it
> doesn't matter much either way but OTOH, in the existing code, if
> there is an error (say 'out of memory' or some other) while building
> the map, we won't send relation attrs whereas with your change we will
> unnecessarily send those in such a case.

That's a good point. I've reverted that change in the attached.

> I feel there is no need to backpatch v6-0002. We can just make it a
> HEAD-only change as that doesn't cause any bug even though it is
> better not to send it. If we consider it as a HEAD-only change then
> probably we can register it for PG-15. What do you think?

Okay, I will see about creating a PG15 CF entry for 0002.

Please see attached v7-0001 with the part mentioned above fixed.

--
Amit Langote
EDB: http://www.enterprisedb.com

Attachment Content-Type Size
PG13-v7-0001-pgoutput-fix-memory-management-for-RelationSyncEn.patch application/octet-stream 5.6 KB
HEAD-v7-0001-pgoutput-fix-memory-management-of-RelationSyncEnt.patch application/octet-stream 5.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2021-05-31 03:33:51 Re: Multiple hosts in connection string failed to failover in non-hot standby mode
Previous Message Dilip Kumar 2021-05-31 03:20:16 Re: Decoding speculative insert with toast leaks memory