Thread: pgsql: Restructure code that is responsible for ensuring that clauseless
pgsql: Restructure code that is responsible for ensuring that clauseless
From
tgl@postgresql.org (Tom Lane)
Date:
Log Message: ----------- Restructure code that is responsible for ensuring that clauseless joins are considered when it is necessary to do so because of a join-order restriction (that is, an outer-join or IN-subselect construct). The former coding was a bit ad-hoc and inconsistent, and it missed some cases, as exposed by Mario Weilguni's recent bug report. His specific problem was that an IN could be turned into a "clauseless" join due to constant-propagation removing the IN's joinclause, and if the IN's subselect involved more than one relation and there was more than one such IN linking to the same upper relation, then the only valid join orders involve "bushy" plans but we would fail to consider the specific paths needed to get there. (See the example case added to the join regression test.) On examining the code I wonder if there weren't some other problem cases too; in particular it seems that GEQO was defending against a different set of corner cases than the main planner was. There was also an efficiency problem, in that when we did realize we needed a clauseless join because of an IN, we'd consider clauseless joins against every other relation whether this was sensible or not. It seems a better design is to use the outer-join and in-clause lists as a backup heuristic, just as the rule of joining only where there are joinclauses is a heuristic: we'll join two relations if they have a usable joinclause *or* this might be necessary to satisfy an outer-join or IN-clause join order restriction. I refactored the code to have just one place considering this instead of three, and made sure that it covered all the cases that any of them had been considering. Backpatch as far as 8.1 (which has only the IN-clause form of the disease). By rights 8.0 and 7.4 should have the bug too, but they accidentally fail to fail, because the joininfo structure used in those releases preserves some memory of there having once been a joinclause between the inner and outer sides of an IN, and so it leads the code in the right direction anyway. I'll be conservative and not touch them. Modified Files: -------------- pgsql/src/backend/optimizer: README (r1.37 -> r1.38) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/README.diff?r1=1.37&r2=1.38) pgsql/src/backend/optimizer/geqo: geqo_eval.c (r1.84 -> r1.85) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/geqo/geqo_eval.c.diff?r1=1.84&r2=1.85) pgsql/src/backend/optimizer/path: joinrels.c (r1.85 -> r1.86) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/path/joinrels.c.diff?r1=1.85&r2=1.86) pgsql/src/backend/optimizer/util: joininfo.c (r1.47 -> r1.48) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/optimizer/util/joininfo.c.diff?r1=1.47&r2=1.48) pgsql/src/include/optimizer: paths.h (r1.95 -> r1.96) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/include/optimizer/paths.h.diff?r1=1.95&r2=1.96) pgsql/src/test/regress/expected: join.out (r1.28 -> r1.29) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/expected/join.out.diff?r1=1.28&r2=1.29) join_1.out (r1.10 -> r1.11) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/expected/join_1.out.diff?r1=1.10&r2=1.11) pgsql/src/test/regress/sql: join.sql (r1.19 -> r1.20) (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/test/regress/sql/join.sql.diff?r1=1.19&r2=1.20)