Skip to content

Commit 7f875fb

Browse files
committed
Fix edge case in plpgsql's make_callstmt_target().
If the plancache entry for the CALL statement is already stale, it's possible for us to fetch an old procedure OID out of it, and then fail with "cache lookup failed for function NNN". In ordinary usage this never happens because make_callstmt_target is called just once immediately after building the plancache entry. It can be forced however by setting up an erroneous CALL (that causes make_callstmt_target itself to report an error), then dropping/recreating the target procedure, then repeating the erroneous CALL. To fix, use SPI_plan_get_cached_plan() to fetch the plancache's plan, rather than assuming we can use SPI_plan_get_plan_sources(). This shouldn't add any noticeable overhead in the normal case, and in the stale-plan case we'd have had to replan anyway a little further down. The other callers of SPI_plan_get_plan_sources() seem OK, because either they don't need up-to-date plans or they know that the query was just (re) planned. But add some commentary in hopes of not falling into this trap again. Per bug #18574 from Song Hongyu. Back-patch to v14 where this coding was introduced. (Older branches have comparable code, but it's run after any required replanning, so there's no issue.) Discussion: https://postgr.es/m/18574-2ce7ba3249221389@postgresql.org
1 parent 7696b2e commit 7f875fb

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/backend/executor/spi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,8 @@ SPI_result_code_string(int code)
20512051
* SPI_plan_get_plan_sources --- get a SPI plan's underlying list of
20522052
* CachedPlanSources.
20532053
*
2054+
* CAUTION: there is no check on whether the CachedPlanSources are up-to-date.
2055+
*
20542056
* This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
20552057
* look directly into the SPIPlan for itself). It's not documented in
20562058
* spi.sgml because we'd just as soon not have too many places using this.

src/pl/plpgsql/src/pl_exec.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,8 +2254,8 @@ exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
22542254
static PLpgSQL_variable *
22552255
make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
22562256
{
2257-
List *plansources;
2258-
CachedPlanSource *plansource;
2257+
CachedPlan *cplan;
2258+
PlannedStmt *pstmt;
22592259
CallStmt *stmt;
22602260
FuncExpr *funcexpr;
22612261
HeapTuple func_tuple;
@@ -2272,16 +2272,15 @@ make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
22722272
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
22732273

22742274
/*
2275-
* Get the parsed CallStmt, and look up the called procedure
2275+
* Get the parsed CallStmt, and look up the called procedure. We use
2276+
* SPI_plan_get_cached_plan to cover the edge case where expr->plan is
2277+
* already stale and needs to be updated.
22762278
*/
2277-
plansources = SPI_plan_get_plan_sources(expr->plan);
2278-
if (list_length(plansources) != 1)
2279-
elog(ERROR, "query for CALL statement is not a CallStmt");
2280-
plansource = (CachedPlanSource *) linitial(plansources);
2281-
if (list_length(plansource->query_list) != 1)
2279+
cplan = SPI_plan_get_cached_plan(expr->plan);
2280+
if (cplan == NULL || list_length(cplan->stmt_list) != 1)
22822281
elog(ERROR, "query for CALL statement is not a CallStmt");
2283-
stmt = (CallStmt *) linitial_node(Query,
2284-
plansource->query_list)->utilityStmt;
2282+
pstmt = linitial_node(PlannedStmt, cplan->stmt_list);
2283+
stmt = (CallStmt *) pstmt->utilityStmt;
22852284
if (stmt == NULL || !IsA(stmt, CallStmt))
22862285
elog(ERROR, "query for CALL statement is not a CallStmt");
22872286

@@ -2357,6 +2356,8 @@ make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
23572356

23582357
row->nfields = nfields;
23592358

2359+
ReleaseCachedPlan(cplan, CurrentResourceOwner);
2360+
23602361
MemoryContextSwitchTo(oldcontext);
23612362

23622363
return (PLpgSQL_variable *) row;
@@ -4201,8 +4202,9 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
42014202
/*
42024203
* We could look at the raw_parse_tree, but it seems simpler to
42034204
* check the command tag. Note we should *not* look at the Query
4204-
* tree(s), since those are the result of rewriting and could have
4205-
* been transmogrified into something else entirely.
4205+
* tree(s), since those are the result of rewriting and could be
4206+
* stale, or could have been transmogrified into something else
4207+
* entirely.
42064208
*/
42074209
if (plansource->commandTag == CMDTAG_INSERT ||
42084210
plansource->commandTag == CMDTAG_UPDATE ||

0 commit comments

Comments
 (0)