Skip to content

Commit 0400ae4

Browse files
committed
Cache typlens of a SQL function's input arguments.
This gets rid of repetitive get_typlen calls in postquel_sub_params, which show up as costing a few percent of the runtime in simple test cases (more with more parameters). In combination with the preceding patches, this gets us most of the way back down to the amount of per-call overhead that functions.c had before commit 0dca5d6. There are some more things that could be done, but this seems like an okay place to stop for v18.
1 parent 0313c5d commit 0400ae4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/backend/executor/functions.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ typedef struct SQLFunctionHashEntry
116116
char *src; /* function body text (for error msgs) */
117117

118118
SQLFunctionParseInfoPtr pinfo; /* data for parser callback hooks */
119+
int16 *argtyplen; /* lengths of the input argument types */
119120

120121
Oid rettype; /* actual return type */
121122
int16 typlen; /* length of the return type */
@@ -1100,6 +1101,15 @@ sql_compile_callback(FunctionCallInfo fcinfo,
11001101
PG_GET_COLLATION());
11011102
MemoryContextSwitchTo(oldcontext);
11021103

1104+
/*
1105+
* Now that we have the resolved argument types, collect their typlens for
1106+
* use in postquel_sub_params.
1107+
*/
1108+
func->argtyplen = (int16 *)
1109+
MemoryContextAlloc(hcontext, func->pinfo->nargs * sizeof(int16));
1110+
for (int i = 0; i < func->pinfo->nargs; i++)
1111+
func->argtyplen[i] = get_typlen(func->pinfo->argtypes[i]);
1112+
11031113
/*
11041114
* And of course we need the function body text.
11051115
*/
@@ -1427,6 +1437,7 @@ postquel_sub_params(SQLFunctionCachePtr fcache,
14271437
{
14281438
ParamListInfo paramLI;
14291439
Oid *argtypes = fcache->func->pinfo->argtypes;
1440+
int16 *argtyplen = fcache->func->argtyplen;
14301441

14311442
if (fcache->paramLI == NULL)
14321443
{
@@ -1463,7 +1474,7 @@ postquel_sub_params(SQLFunctionCachePtr fcache,
14631474
prm->isnull = fcinfo->args[i].isnull;
14641475
prm->value = MakeExpandedObjectReadOnly(fcinfo->args[i].value,
14651476
prm->isnull,
1466-
get_typlen(argtypes[i]));
1477+
argtyplen[i]);
14671478
/* Allow the value to be substituted into custom plans */
14681479
prm->pflags = PARAM_FLAG_CONST;
14691480
prm->ptype = argtypes[i];

0 commit comments

Comments
 (0)