Optimization issue of branching UNION ALL
Andrei Lepikhov <a.lepikhov@postgrespro.ru>
From: Andrey Lepikhov <a.lepikhov@postgrespro.ru>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2022-12-21T04:14:02Z
Lists: pgsql-hackers
Attachments
- t.sh (application/x-shellscript)
- union_all_optimization.patch (text/x-patch) patch
Hi, Client report on a corner case have shown up possible minor non-optimality in procedure of transformation of simple UNION ALL statement tree. Complaint is about auto-generated query with 1E4 simple union all's (see t.sh to generate a demo script). The reason: in REL_11_STABLE it is planned and executed in a second, but REL_12_STABLE and beyond makes matters worse: planning of such a query needs tons of gigabytes of RAM. Superficial study revealed possibly unnecessary operations that could be avoided: 1. Walking across a query by calling substitute_phv_relids() even if lastPHId shows that no one phv is presented. 2. Iterative passes along the append_rel_list for replacing vars in the translated_vars field. I can't grasp real necessity of passing all the append_rel_list during flattening of an union all leaf subquery. No one can reference this leaf, isn't it? In attachment you can see some sketch that reduces a number of planner cycles/copyings. -- Regards Andrey Lepikhov Postgres Professional
Commits
-
Avoid O(N^2) cost when pulling up lots of UNION ALL subqueries.
- e42e31243027 16.0 landed
-
Add some recursion and looping defenses in prepjointree.c.
- d572003f74bb 12.14 landed
- 8cd700cc5a67 11.19 landed
- 4fceb454f2df 13.10 landed
- 97431d673992 14.7 landed
- 5beb7881fb27 16.0 landed
- 1a3daa5bb2e0 15.2 landed