Re: Possible incorrect row estimation for Gather paths
Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
To: Richard Guo <guofenglinux@gmail.com>
Cc: Rafia Sabih <rafia.pghackers@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2024-07-22T08:47:24Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix rowcount estimate for gather (merge) paths
- 581df2148737 18.0 landed
Attachments
- v3-0001-Remove-unnecessary-assignment-of-path-rows-in-gat.patch (application/octet-stream) patch v3-0001
- v3-0002-Fix-row-estimation-in-gather-paths.patch (application/octet-stream) patch v3-0002
On Wed, Jul 17, 2024 at 3:59 AM Richard Guo <guofenglinux@gmail.com> wrote: > > I can reproduce this problem with the query below. > > explain (costs on) select * from tenk1 order by twenty; > QUERY PLAN > --------------------------------------------------------------------------------- > Gather Merge (cost=772.11..830.93 rows=5882 width=244) > Workers Planned: 1 > -> Sort (cost=772.10..786.80 rows=5882 width=244) > Sort Key: twenty > -> Parallel Seq Scan on tenk1 (cost=0.00..403.82 rows=5882 width=244) > (5 rows) I was looking for a test to add in the regress checks that wouldn't rely on explain cost since it is disabled. However, I've realised I could do something similar to what's done in stats_ext with the check_estimated_rows function. I've added the get_estimated_rows test function that extracts the estimated rows from the top node and uses it to check the gather nodes' estimates. get_estimated_rows uses a simple explain compared to check_estimated_rows which relies on an explain analyze. > I wonder if the changes in create_ordered_paths should also be reduced > to 'total_groups = gather_rows_estimate(path);'. It should already be the case with the patch v2. It does create rounding errors that are visible in the tests but they shouldn't exceed +1 or -1. > I think perhaps it's better to declare gather_rows_estimate() in > cost.h rather than optimizer.h. > (BTW, I wonder if compute_gather_rows() would be a better name?) Good point, I've moved the declaration and renamed it. > I noticed another issue in generate_useful_gather_paths() -- *rowsp > would have a random value if override_rows is true and we use > incremental sort for gather merge. I think we should fix this too. That seems to be the case. I've tried to find a query that could trigger this codepath without success. All grouping and distinct paths I've tried where fully sorted and didn't trigger an incremental sort. I will need a bit more time to check this. In the meantime, I've updated the patches with the review comments. Regards, Anthonin