Re: BUG #17564: Planner bug in combination of generate_series(), unnest() and ORDER BY

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, Martijn van Oosterhout <kleptog(at)gmail(dot)com>, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17564: Planner bug in combination of generate_series(), unnest() and ORDER BY
Date: 2022-08-03 21:44:17
Message-ID: 3242058.1659563057@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
>> FWIW, this reproduces the bug for me in v13 and v14, but not v15 or HEAD.

I decided I'd wasted entirely too much time trying to find a suitable
test case, so I pushed the bug fix with no new regression test.

Meanwhile, back at the question of whether db0d67db2 is buggy,
it looks like that reduces to whether it was intentional that that
made a large change in estimated sort costs. What I'm getting from
v15 and HEAD, as I said earlier, is:

Sort (cost=2946.79..3096.79 rows=60000 width=36)
Sort Key: (((unnest('{}'::jsonb[])) ->> 'timestamp'::text))
-> Result (cost=90.08..1446.79 rows=60000 width=36)
-> ProjectSet (cost=90.08..396.79 rows=60000 width=36)
-> Hash Join (cost=90.08..96.19 rows=60 width=0)
Hash Cond: (groups.group_id = results.group_id)
-> Seq Scan on groups (cost=0.00..5.66 rows=166 width=4)
-> Hash (cost=89.33..89.33 rows=60 width=4)
-> Index Only Scan using results_pkey on results (cost=0.28..89.33 rows=60 width=4)
Index Cond: (search_id = 3336)

After applying this patch, what I get from v13 and v14 is

Sort (cost=6208.59..6358.59 rows=60000 width=36)
Sort Key: (((unnest('{}'::jsonb[])) ->> 'timestamp'::text))
-> Result (cost=90.08..1446.79 rows=60000 width=36)
-> ProjectSet (cost=90.08..396.79 rows=60000 width=36)
-> Hash Join (cost=90.08..96.19 rows=60 width=0)
Hash Cond: (groups.group_id = results.group_id)
-> Seq Scan on groups (cost=0.00..5.66 rows=166 width=4)
-> Hash (cost=89.33..89.33 rows=60 width=4)
-> Index Only Scan using results_pkey on results (cost=0.28..89.33 rows=60 width=4)
Index Cond: (search_id = 3336)

So this plan is identical except for the sort costs, which seem to
be about half of what they were in the older branches. If that was
intentional, why didn't the commit message mention it? It's not
exactly a minor change, and enable_group_by_reordering doesn't
seem to have any effect on it.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2022-08-03 21:53:30 Re: BUG #17564: Planner bug in combination of generate_series(), unnest() and ORDER BY
Previous Message Martijn van Oosterhout 2022-08-03 21:12:11 Re: BUG #17564: Planner bug in combination of generate_series(), unnest() and ORDER BY