Re: Wrong results with grouping sets - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Wrong results with grouping sets
Date
Msg-id 3472344.1771858107@sss.pgh.pa.us
Whole thread Raw
In response to Wrong results with grouping sets  (Richard Guo <guofenglinux@gmail.com>)
Responses Re: Wrong results with grouping sets
List pgsql-hackers
Richard Guo <guofenglinux@gmail.com> writes:
> On Thu, Oct 10, 2024 at 4:06 PM Richard Guo <guofenglinux@gmail.com> wrote:
>> While we can fix this issue by propagating the hasGroupRTE mark from
>> the EXISTS subquery to the parent, a better fix might be to remove the
>> subquery's RTE_GROUP entry, since we have dropped the subquery's
>> groupClause before the pull-up (see simplify_EXISTS_query).

> Here is the patch.

I happened to notice ffe12d1d2 after David mentioned
simplify_EXISTS_query in another thread, and nearly fell off my chair
when I read this bit:

            query->rtable = list_delete_cell(query->rtable, lc);

How can that possibly be safe?  It will change the rangetable index of
every following RTE.  It might appear to work as long as the RTE_GROUP
RTE is always last.  But I don't think you can rely on that, or should
rely on it even if it does happen to still be always true even after
query rewrite and other early-planning manipulations.

A safer way might be to convert the RTE into an unreferenced
RTE_RESULT, or some other innocuous RTE type.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Daniel Gustafsson
Date:
Subject: Re: DOCS - Server Applications [option] should be [option...]
Next
From: Bertrand Drouvot
Date:
Subject: Re: Check for memset_explicit() and explicit_memset()