Re: BUG #17544: Join order for INNER JOIN ... USING with GROUP BY on join column affects selected query plan - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #17544: Join order for INNER JOIN ... USING with GROUP BY on join column affects selected query plan
Date
Msg-id 1662281.1657648346@sss.pgh.pa.us
Whole thread Raw
In response to BUG #17544: Join order for INNER JOIN ... USING with GROUP BY on join column affects selected query plan  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> It seems like the join order of a simple INNER JOIN ... USING(...) affects
> query plan selection even when only joining two tables, when used with GROUP
> BY on the join column.
> ...
> Digging a bit deeper, this appears to be due to the USING clause causing the
> GROUP BY group_id to be rewritten to `groups.group_id` in the first query
> and `tasks.group_id` in the second query, and the former resulting in the
> simpler plan not being used.

Yeah.  In principle that shouldn't matter, but a confluence of
peculiarities of this specific scenario and a perhaps-overly-aggressive
optimization on the GROUP BY clause prevent the planner from finding
the good plan when the JOIN USING variable is resolved as coming from
the same table as the other GROUP BY column.  Technical details at [1].
I'm not sure whether we'll end up using the quick-hack patch shown there,
but if you're desperate for a fix you could apply that locally.

            regards, tom lane

[1] https://www.postgresql.org/message-id/1657885.1657647073%40sss.pgh.pa.us



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17548: Aggregate queries on partitioned tables can cause OOM.
Next
From: Andres Freund
Date:
Subject: Re: can't drop table due to reference from orphaned temp function