• Tom Lane's avatar
    Allow planner to use expression-index stats for function calls in WHERE. · 39df0f15
    Tom Lane authored
    Previously, a function call appearing at the top level of WHERE had a
    hard-wired selectivity estimate of 0.3333333, a kludge conveniently dated
    in the source code itself to July 1992.  The expectation at the time was
    that somebody would soon implement estimator support functions analogous
    to those for operators; but no such code has appeared, nor does it seem
    likely to in the near future.  We do have an alternative solution though,
    at least for immutable functions on single relations: creating an
    expression index on the function call will allow ANALYZE to gather stats
    about the function's selectivity.  But the code in clause_selectivity()
    failed to make use of such data even if it exists.
    
    Refactor so that that will happen.  I chose to make it try this technique
    for any clause type for which clause_selectivity() doesn't have a special
    case, not just functions.  To avoid adding unnecessary overhead in the
    common case where we don't learn anything new, make selfuncs.c provide an
    API that hooks directly to examine_variable() and then var_eq_const(),
    rather than the previous coding which laboriously constructed an OpExpr
    only so that it could be expensively deconstructed again.
    
    I preserved the behavior that the default estimate for a function call
    is 0.3333333.  (For any other expression node type, it's 0.5, as before.)
    I had originally thought to make the default be 0.5 across the board, but
    changing a default estimate that's survived for twenty-three years seems
    like something not to do without a lot more testing than I care to put
    into it right now.
    
    Per a complaint from Jehan-Guillaume de Rorthais.  Back-patch into 9.5,
    but not further, at least for the moment.
    39df0f15
selfuncs.c 220 KB