pgsql: Fix long-obsolete code for separating filter conditions in cost_ - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix long-obsolete code for separating filter conditions in cost_
Date
Msg-id E1YSyuZ-00071a-Ng@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix long-obsolete code for separating filter conditions in cost_index().

This code relied on pointer equality to identify which restriction clauses
also appear in the indexquals (and, therefore, don't need to be applied as
simple filter conditions).  That was okay once upon a time, years ago,
before we introduced the equivalence-class machinery.  Now there's about a
50-50 chance that an equality clause appearing in the indexquals will be
the mirror image (commutator) of its mate in the restriction list.  When
that happens, we'd erroneously think that the clause would be re-evaluated
at each visited row, and therefore inflate the cost estimate for the
indexscan by the clause's cost.

Add some logic to catch this case.  It seems to me that it continues not to
be worthwhile to expend the extra predicate-proof work that createplan.c
will do on the finally-selected plan, but this case is common enough and
cheap enough to handle that we should do so.

This will make a small difference (about one cpu_operator_cost per row)
in simple cases; but in situations where there's an expensive function in
the indexquals, it can make a very large difference, as seen in recent
example from Jeff Janes.

This is a long-standing bug, but I'm hesitant to back-patch because of the
possibility of destabilizing plan choices that people may be happy with.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/497bac7d290df13d8b00ba48653a96015ff4741b

Modified Files
--------------
src/backend/optimizer/path/costsize.c   |   78 +++++++++++++++++++++++--------
src/backend/optimizer/plan/createplan.c |    3 ++
2 files changed, 61 insertions(+), 20 deletions(-)


pgsql-committers by date:

Previous
From: Robert Haas
Date:
Subject: pgsql: Remove residual NULL-pstate handling in addRangeTableEntry.
Next
From: Fujii Masao
Date:
Subject: pgsql: Fix an obsolete reference to SnapshotNow in comment.