Re: Wrong rows estimations with joins of CTEs slows queries by more than factor 500

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jian Guo <gjian(at)vmware(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, Hans Buschmann <buschmann(at)nidsa(dot)net>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Zhenghua Lyu <zlyu(at)vmware(dot)com>
Subject: Re: Wrong rows estimations with joins of CTEs slows queries by more than factor 500
Date: 2023-11-21 06:18:19
Message-ID: CAMbWs4-8XWq_GVMMxJ_LsD4Zxi34QoE0Lo8hwT1mWwTOLZyWOg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Nov 21, 2023 at 1:46 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> * Do we really need to use make_tlist_from_pathtarget? Why isn't
> the tlist of the cteplan good enough (indeed, more so)?

I think you are right. The cteplan->targetlist is built for the CTE's
best path by build_path_tlist(), which is almost the same as
make_tlist_from_pathtarget() except that it also replaces nestloop
params. So cteplan->targetlist is good enough here.

> * I don't love having this code assume that it knows how to find
> the Path the cteplan was made from. It'd be better to make
> SS_process_ctes save that somewhere, maybe in a list paralleling
> root->cte_plan_ids.

Fair point.

I've updated the patch to v2 for the changes.

> Alternatively: maybe it's time to do what the comments in
> SS_process_ctes vaguely speculate about, and just save the Path
> at that point, with construction of the plan left for createplan()?
> That might be a lot of refactoring for not much gain, so not sure.

I'm not sure if this is worth the effort. And it seems that we have the
same situation with SubLinks where we construct the plan in subselect.c
rather than createplan.c.

Thanks
Richard

Attachment Content-Type Size
v2-0001-Propagate-pathkeys-from-CTEs-up-to-the-outer-query.patch application/octet-stream 9.2 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message John Naylor 2023-11-21 06:31:45 Re: Why is hot_standby_feedback off by default?
Previous Message Michael Paquier 2023-11-21 06:09:20 Typo with amtype = 's' in opr_sanity.sql