pgsql: Fix plpgsql's reporting of plan-time errors in possibly-simple e - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix plpgsql's reporting of plan-time errors in possibly-simple e
Date
Msg-id E1U0iYG-0000CU-Av@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix plpgsql's reporting of plan-time errors in possibly-simple expressions.

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.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/0900ac2d0dba3168ba574e5b0b61170237b4fdea

Modified Files
--------------
src/backend/executor/spi.c            |   72 +++++++++++++++++++++++++++++++++
src/include/executor/spi.h            |    3 +
src/pl/plpgsql/src/pl_exec.c          |   42 ++++++++++---------
src/test/regress/expected/plpgsql.out |   15 +++++++
src/test/regress/sql/plpgsql.sql      |   13 ++++++
5 files changed, 126 insertions(+), 19 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix grammar for subscripting or field selection from a sub-SELEC
Next
From: Tom Lane
Date:
Subject: pgsql: Fix plpgsql's reporting of plan-time errors in possibly-simple e