forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitfc069a3
Implement Self-Join Elimination
The Self-Join Elimination (SJE) feature removes an inner join of a plaintable to itself in the query tree if it is proven that the join can bereplaced with a scan without impacting the query result. Self-join andinner relation get replaced with the outer in query, equivalence classes,and planner info structures. Also, the inner restrictlist moves to theouter one with the removal of duplicated clauses. Thus, this optimizationreduces the length of the range table list (this especially makes sense forpartitioned relations), reduces the number of restriction clauses and,in turn, selectivity estimations, and potentially improves total plannerprediction for the query.This feature is dedicated to avoiding redundancy, which can appear afterpull-up transformations or the creation of an EquivalenceClass-derived clauselike the below. SELECT * FROM t1 WHERE x IN (SELECT t3.x FROM t1 t3); SELECT * FROM t1 WHERE EXISTS (SELECT t3.x FROM t1 t3 WHERE t3.x = t1.x); SELECT * FROM t1,t2, t1 t3 WHERE t1.x = t2.x AND t2.x = t3.x;In the future, we could also reduce redundancy caused by subquery pull-upafter unnecessary outer join removal in cases like the one below. SELECT * FROM t1 WHERE x IN (SELECT t3.x FROM t1 t3 LEFT JOIN t2 ON t2.x = t1.x);Also, it can drastically help to join partitioned tables, removing entrieseven before their expansion.The SJE proof is based on innerrel_is_unique() machinery.We can remove a self-join when for each outer row: 1. At most, one inner row matches the join clause; 2. Each matched inner row must be (physically) the same as the outer one; 3. Inner and outer rows have the same row mark.In this patch, we use the next approach to identify a self-join: 1. Collect all merge-joinable join quals which look like a.x = b.x; 2. Add to the list above the baseretrictinfo of the inner table; 3. Check innerrel_is_unique() for the qual list. If it returns false, skip this pair of joining tables; 4. Check uniqueness, proved by the baserestrictinfo clauses. To prove the possibility of self-join elimination, the inner and outer clauses must match exactly.The relation replacement procedure is not trivial and is partly combinedwith the one used to remove useless left joins. Tests covering this featurewere added to join.sql. Some of the existing regression tests changed dueto self-join removal logic.Discussion:https://postgr.es/m/flat/64486b0b-0404-e39e-322d-0801154901f3%40postgrespro.ruAuthor: Andrey Lepikhov <a.lepikhov@postgrespro.ru>Author: Alexander Kuzmenkov <a.kuzmenkov@postgrespro.ru>Co-authored-by: Alexander Korotkov <aekorotkov@gmail.com>Co-authored-by: Alena Rybakina <lena.ribackina@yandex.ru>Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Reviewed-by: Robert Haas <robertmhaas@gmail.com>Reviewed-by: Andres Freund <andres@anarazel.de>Reviewed-by: Simon Riggs <simon@2ndquadrant.com>Reviewed-by: Jonathan S. Katz <jkatz@postgresql.org>Reviewed-by: David Rowley <david.rowley@2ndquadrant.com>Reviewed-by: Thomas Munro <thomas.munro@enterprisedb.com>Reviewed-by: Konstantin Knizhnik <k.knizhnik@postgrespro.ru>Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>Reviewed-by: Hywel Carver <hywel@skillerwhale.com>Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at>Reviewed-by: Ronan Dunklau <ronan.dunklau@aiven.io>Reviewed-by: vignesh C <vignesh21@gmail.com>Reviewed-by: Zhihong Yu <zyu@yugabyte.com>Reviewed-by: Greg Stark <stark@mit.edu>Reviewed-by: Jaime Casanova <jcasanov@systemguards.com.ec>Reviewed-by: Michał Kłeczek <michal@kleczek.org>Reviewed-by: Alena Rybakina <lena.ribackina@yandex.ru>Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>1 parent3fb5862 commitfc069a3
File tree
19 files changed
+2983
-148
lines changed- doc/src/sgml
- src
- backend
- optimizer
- path
- plan
- prep
- rewrite
- utils/misc
- include
- nodes
- optimizer
- rewrite
- test/regress
- expected
- sql
- tools/pgindent
19 files changed
+2983
-148
lines changedLines changed: 16 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5544 | 5544 |
| |
5545 | 5545 |
| |
5546 | 5546 |
| |
| 5547 | + | |
| 5548 | + | |
| 5549 | + | |
| 5550 | + | |
| 5551 | + | |
| 5552 | + | |
| 5553 | + | |
| 5554 | + | |
| 5555 | + | |
| 5556 | + | |
| 5557 | + | |
| 5558 | + | |
| 5559 | + | |
| 5560 | + | |
| 5561 | + | |
| 5562 | + | |
5547 | 5563 |
| |
5548 | 5564 |
| |
5549 | 5565 |
| |
|
Lines changed: 2 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
852 | 852 |
| |
853 | 853 |
| |
854 | 854 |
| |
855 |
| - | |
| 855 | + | |
| 856 | + | |
856 | 857 |
| |
857 | 858 |
| |
858 | 859 |
| |
|
Lines changed: 39 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
4162 | 4162 |
| |
4163 | 4163 |
| |
4164 | 4164 |
| |
| 4165 | + | |
| 4166 | + | |
| 4167 | + | |
| 4168 | + | |
| 4169 | + | |
| 4170 | + | |
| 4171 | + | |
| 4172 | + | |
| 4173 | + | |
| 4174 | + | |
| 4175 | + | |
| 4176 | + | |
| 4177 | + | |
| 4178 | + | |
| 4179 | + | |
| 4180 | + | |
4165 | 4181 |
| |
4166 | 4182 |
| |
4167 | 4183 |
| |
| |||
4217 | 4233 |
| |
4218 | 4234 |
| |
4219 | 4235 |
| |
| 4236 | + | |
4220 | 4237 |
| |
4221 | 4238 |
| |
4222 | 4239 |
| |
| |||
4268 | 4285 |
| |
4269 | 4286 |
| |
4270 | 4287 |
| |
| 4288 | + | |
| 4289 | + | |
| 4290 | + | |
| 4291 | + | |
| 4292 | + | |
| 4293 | + | |
| 4294 | + | |
| 4295 | + | |
| 4296 | + | |
| 4297 | + | |
| 4298 | + | |
| 4299 | + | |
| 4300 | + | |
| 4301 | + | |
| 4302 | + | |
| 4303 | + | |
| 4304 | + | |
| 4305 | + | |
4271 | 4306 |
| |
4272 | 4307 |
| |
4273 | 4308 |
| |
| |||
4310 | 4345 |
| |
4311 | 4346 |
| |
4312 | 4347 |
| |
| 4348 | + | |
| 4349 | + | |
| 4350 | + | |
4313 | 4351 |
| |
| 4352 | + | |
4314 | 4353 |
| |
4315 | 4354 |
| |
4316 | 4355 |
| |
|
0 commit comments
Comments
(0)