Fix GEQO to not assume its join order heuristic always works.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 11 Feb 2015 01:37:19 +0000 (20:37 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 11 Feb 2015 01:37:19 +0000 (20:37 -0500)
commit1a179f36f7c612d3560a72396c559cd876dfab6b
tree790016599261d00485aae926fb06a8e1034dd25b
parent1f393fc923ec77f25fd37e16fd8ccb480df82ebb
Fix GEQO to not assume its join order heuristic always works.

Back in commit 400e2c934457bef4bc3cc9a3e49b6289bd761bc0 I rewrote GEQO's
gimme_tree function to improve its heuristic for modifying the given tour
into a legal join order.  In what can only be called a fit of hubris,
I supposed that this new heuristic would *always* find a legal join order,
and ripped out the old logic that allowed gimme_tree to sometimes fail.

The folly of this is exposed by bug #12760, in which the "greedy" clumping
behavior of merge_clump() can lead it into a dead end which could only be
recovered from by un-clumping.  We have no code for that and wouldn't know
exactly what to do with it if we did.  Rather than try to improve the
heuristic rules still further, let's just recognize that it *is* a
heuristic and probably must always have failure cases.  So, put back the
code removed in the previous commit to allow for failure (but comment it
a bit better this time).

It's possible that this code was actually fully correct at the time and
has only been broken by the introduction of LATERAL.  But having seen this
example I no longer have much faith in that proposition, so back-patch to
all supported branches.
src/backend/optimizer/geqo/geqo_eval.c
src/backend/optimizer/geqo/geqo_main.c
src/backend/optimizer/geqo/geqo_pool.c