• Tom Lane's avatar
    Fix plpgsql's reporting of plan-time errors in possibly-simple expressions. · 0900ac2d
    Tom Lane authored
    exec_simple_check_plan and exec_eval_simple_expr attempted to call
    GetCachedPlan directly.  This meant that if an error was thrown during
    planning, the resulting context traceback would not include the line
    normally contributed by _SPI_error_callback.  This is already inconsistent,
    but just to be really odd, a re-execution of the very same expression
    *would* show the additional context line, because we'd already have cached
    the plan and marked the expression as non-simple.
    
    The problem is easy to demonstrate in 9.2 and HEAD because planning of a
    cached plan doesn't occur at all until GetCachedPlan is done.  In earlier
    versions, it could only be an issue if initial planning had succeeded, then
    a replan was forced (already somewhat improbable for a simple expression),
    and the replan attempt failed.  Since the issue is mainly cosmetic in older
    branches anyway, it doesn't seem worth the risk of trying to fix it there.
    It is worth fixing in 9.2 since the instability of the context printout can
    affect the results of GET STACKED DIAGNOSTICS, as per a recent discussion
    on pgsql-novice.
    
    To fix, introduce a SPI function that wraps GetCachedPlan while installing
    the correct callback function.  Use this instead of calling GetCachedPlan
    directly from plpgsql.
    
    Also introduce a wrapper function for extracting a SPI plan's
    CachedPlanSource list.  This lets us stop including spi_priv.h in
    pl_exec.c, which was never a very good idea from a modularity standpoint.
    
    In passing, fix a similar inconsistency that could occur in SPI_cursor_open,
    which was also calling GetCachedPlan without setting up a context callback.
    0900ac2d
spi.c 64.4 KB