Skip to content

Commit e6ed34f

Browse files
committed
Ensure generic plan gets used for a plpgsql expression with no parameters.
Now that a NULL ParamListInfo pointer causes significantly different behavior in plancache.c, be sure to pass it that way when the expression is known not to reference any plpgsql variables. Saves a few setup cycles anyway.
1 parent 0a6cc28 commit e6ed34f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,11 +3012,6 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
30123012
int rc;
30133013
PLpgSQL_expr *expr = stmt->sqlstmt;
30143014

3015-
/*
3016-
* Set up ParamListInfo (hook function and possibly data values)
3017-
*/
3018-
paramLI = setup_param_list(estate, expr);
3019-
30203015
/*
30213016
* On the first call for this statement generate the plan, and detect
30223017
* whether the statement is INSERT/UPDATE/DELETE
@@ -3049,6 +3044,11 @@ exec_stmt_execsql(PLpgSQL_execstate *estate,
30493044
}
30503045
}
30513046

3047+
/*
3048+
* Set up ParamListInfo (hook function and possibly data values)
3049+
*/
3050+
paramLI = setup_param_list(estate, expr);
3051+
30523052
/*
30533053
* If we have INTO, then we only need one row back ... but if we have INTO
30543054
* STRICT, ask for two rows, so that we can verify the statement returns
@@ -5000,12 +5000,18 @@ setup_param_list(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
50005000
{
50015001
ParamListInfo paramLI;
50025002

5003+
/*
5004+
* We must have created the SPIPlan already (hence, query text has been
5005+
* parsed/analyzed at least once); else we cannot rely on expr->paramnos.
5006+
*/
5007+
Assert(expr->plan != NULL);
5008+
50035009
/*
50045010
* Could we re-use these arrays instead of palloc'ing a new one each time?
50055011
* However, we'd have to re-fill the array each time anyway, since new
50065012
* values might have been assigned to the variables.
50075013
*/
5008-
if (estate->ndatums > 0)
5014+
if (!bms_is_empty(expr->paramnos))
50095015
{
50105016
Bitmapset *tmpset;
50115017
int dno;
@@ -5048,12 +5054,19 @@ setup_param_list(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
50485054
/*
50495055
* Also make sure this is set before parser hooks need it. There is
50505056
* no need to save and restore, since the value is always correct once
5051-
* set.
5057+
* set. (Should be set already, but let's be sure.)
50525058
*/
50535059
expr->func = estate->func;
50545060
}
50555061
else
5062+
{
5063+
/*
5064+
* Expression requires no parameters. Be sure we represent this case
5065+
* as a NULL ParamListInfo, so that plancache.c knows there is no
5066+
* point in a custom plan.
5067+
*/
50565068
paramLI = NULL;
5069+
}
50575070
return paramLI;
50585071
}
50595072

0 commit comments

Comments
 (0)