Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8d148bb

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 2bb969f commit 8d148bb

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
@@ -2044,6 +2044,8 @@ SPI_result_code_string(int code)
20442044
* SPI_plan_get_plan_sources --- get a SPI plan's underlying list of
20452045
* CachedPlanSources.
20462046
*
2047+
* CAUTION: there is no check on whether the CachedPlanSources are up-to-date.
2048+
*
20472049
* This is exported so that PL/pgSQL can use it (this beats letting PL/pgSQL
20482050
* look directly into the SPIPlan for itself). It's not documented in
20492051
* 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
@@ -2276,8 +2276,8 @@ exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
22762276
static PLpgSQL_variable *
22772277
make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
22782278
{
2279-
List *plansources;
2280-
CachedPlanSource *plansource;
2279+
CachedPlan *cplan;
2280+
PlannedStmt *pstmt;
22812281
CallStmt *stmt;
22822282
FuncExpr *funcexpr;
22832283
HeapTuple func_tuple;
@@ -2294,16 +2294,15 @@ make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
22942294
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
22952295

22962296
/*
2297-
* Get the parsed CallStmt, and look up the called procedure
2297+
* Get the parsed CallStmt, and look up the called procedure. We use
2298+
* SPI_plan_get_cached_plan to cover the edge case where expr->plan is
2299+
* already stale and needs to be updated.
22982300
*/
2299-
plansources = SPI_plan_get_plan_sources(expr->plan);
2300-
if (list_length(plansources) != 1)
2301-
elog(ERROR, "query for CALL statement is not a CallStmt");
2302-
plansource = (CachedPlanSource *) linitial(plansources);
2303-
if (list_length(plansource->query_list) != 1)
2301+
cplan = SPI_plan_get_cached_plan(expr->plan);
2302+
if (cplan == NULL || list_length(cplan->stmt_list) != 1)
23042303
elog(ERROR, "query for CALL statement is not a CallStmt");
2305-
stmt = (CallStmt *) linitial_node(Query,
2306-
plansource->query_list)->utilityStmt;
2304+
pstmt = linitial_node(PlannedStmt, cplan->stmt_list);
2305+
stmt = (CallStmt *) pstmt->utilityStmt;
23072306
if (stmt == NULL || !IsA(stmt, CallStmt))
23082307
elog(ERROR, "query for CALL statement is not a CallStmt");
23092308

@@ -2383,6 +2382,8 @@ make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
23832382

23842383
row->nfields = nfields;
23852384

2385+
ReleaseCachedPlan(cplan, CurrentResourceOwner);
2386+
23862387
MemoryContextSwitchTo(oldcontext);
23872388

23882389
return (PLpgSQL_variable *) row;
@@ -4245,8 +4246,9 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
42454246
/*
42464247
* We could look at the raw_parse_tree, but it seems simpler to
42474248
* check the command tag. Note we should *not* look at the Query
4248-
* tree(s), since those are the result of rewriting and could have
4249-
* been transmogrified into something else entirely.
4249+
* tree(s), since those are the result of rewriting and could be
4250+
* stale, or could have been transmogrified into something else
4251+
* entirely.
42504252
*/
42514253
if (plansource->commandTag == CMDTAG_INSERT ||
42524254
plansource->commandTag == CMDTAG_UPDATE ||

0 commit comments

Comments
 (0)