Fix the miss consideration of tuple_fraction during add_paths_to_append_rel

Started by Andy Fanover 3 years ago3 messageshackers
Jump to latest
#1Andy Fan
zhihui.fan1213@gmail.com

When I am working on "Pushing limit into subqueries of a union" [1]/messages/by-id/11228.1118365833@sss.pgh.pa.us, I
found we already have a great infrastructure to support this. For a query
like

subquery-1 UNION ALL subquery-2 LIMIT 3;

We have considered the root->tuple_fraction when planning the subqueries
without an extra Limit node as an overhead. But the reason it doesn't work
in my real case is flatten_simple_union_all flat the union all subqueries
into append relation and we didn't handle the root->tuple_fraction during
add_paths_to_append_rel.

Given the below query for example:
explain analyze
(select * from tenk1 order by hundred)
union all
(select * from tenk2 order by hundred)
limit 3;

Without the patch: Execution Time: 7.856 ms
with the patch: Execution Time: 0.224 ms

Any suggestion is welcome.

[1]: /messages/by-id/11228.1118365833@sss.pgh.pa.us

--
Best Regards
Andy Fan

Attachments:

v1-0001-Considering-root-tuple_fraction-during-create_app.patchapplication/octet-stream; name=v1-0001-Considering-root-tuple_fraction-during-create_app.patchDownload+47-2
#2Zhang Mingli
zmlpostgres@gmail.com
In reply to: Andy Fan (#1)
Re: Fix the miss consideration of tuple_fraction during add_paths_to_append_rel

HI,

On Apr 10, 2023, 16:35 +0800, Andy Fan <zhihui.fan1213@gmail.com>, wrote:

When I am working on "Pushing limit into subqueries of a union" [1], I
found we already have a great infrastructure to support this. For a query
like

subquery-1 UNION ALL subquery-2 LIMIT 3;

We have considered the root->tuple_fraction when planning the subqueries
without an extra Limit node as an overhead. But the reason it doesn't work
in my real case is flatten_simple_union_all flat the union all subqueries
into append relation and we didn't handle the root->tuple_fraction during
add_paths_to_append_rel.

Given the below query for example:
explain analyze
(select * from tenk1 order by hundred)
union all
(select * from tenk2 order by hundred)
limit 3;

Without the patch: Execution Time: 7.856 ms
with the patch:  Execution Time: 0.224 ms

Any suggestion is welcome.

[1] /messages/by-id/11228.1118365833@sss.pgh.pa.us

--
Best Regards
Andy Fan

There is spare indent at else if.

-		if (childrel->pathlist != NIL &&
+		if (cheapest_startup_path && cheapest_startup_path->param_info == NULL)
+			accumulate_append_subpath(cheapest_startup_path,
+									  &subpaths, NULL);
+			else if (childrel->pathlist != NIL &&
 			childrel->cheapest_total_path->param_info == NULL)
 			accumulate_append_subpath(childrel->cheapest_total_path,
 									  &subpaths, NULL);

Could we also consider tuple_fraction in partial_pathlist for  parallel append?

Regards,
Zhang Mingli

#3Andy Fan
zhihui.fan1213@gmail.com
In reply to: Zhang Mingli (#2)
Re: Fix the miss consideration of tuple_fraction during add_paths_to_append_rel

On Mon, Apr 10, 2023 at 9:56 PM Zhang Mingli <zmlpostgres@gmail.com> wrote:

There is spare indent at else if.

- if (childrel->pathlist != NIL &&
+ if (cheapest_startup_path && cheapest_startup_path->param_info == NULL)
+ accumulate_append_subpath(cheapest_startup_path,
+   &subpaths, NULL);
+ else if (childrel->pathlist != NIL &&
childrel->cheapest_total_path->param_info == NULL)
accumulate_append_subpath(childrel->cheapest_total_path,
&subpaths, NULL);

Could we also consider tuple_fraction in partial_pathlist for parallel
append?

Thanks for the suggestion, the v2 has fixed the indent issue and I did
something about parallel append. Besides that, I restrict the changes
happens under bms_equal(rel->relids, root->all_query_rels), which may
make this patch safer.

I have added this into https://commitfest.postgresql.org/43/4270/

--
Best Regards
Andy Fan

Attachments:

t47713_3
v2-0001-Considering-root-tuple_fraction-during-create_app.patchapplication/octet-stream; name=v2-0001-Considering-root-tuple_fraction-during-create_app.patchDownload+83-11