diff options
author | Andres Freund <andres@anarazel.de> | 2019-01-26 14:17:52 -0800 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2019-01-26 14:17:52 -0800 |
commit | a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8 (patch) | |
tree | 4f34f2c902977a7ce0be7b1e32a12adf0cb223b7 /src/backend/executor/execSRF.c | |
parent | 6d3ede5f1c654f923b2767b0b0c3b09569adaa18 (diff) | |
download | postgresql-a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8.tar.gz postgresql-a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8.zip |
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
Diffstat (limited to 'src/backend/executor/execSRF.c')
-rw-r--r-- | src/backend/executor/execSRF.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/backend/executor/execSRF.c b/src/backend/executor/execSRF.c index 0c086c52904..265250186a1 100644 --- a/src/backend/executor/execSRF.c +++ b/src/backend/executor/execSRF.c @@ -109,7 +109,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, Oid funcrettype; bool returnsTuple; bool returnsSet = false; - FunctionCallInfoData fcinfo; + FunctionCallInfo fcinfo; PgStat_FunctionCallUsage fcusage; ReturnSetInfo rsinfo; HeapTupleData tmptup; @@ -141,6 +141,8 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, rsinfo.setResult = NULL; rsinfo.setDesc = NULL; + fcinfo = palloc(SizeForFunctionCallInfo(list_length(setexpr->args))); + /* * Normally the passed expression tree will be a SetExprState, since the * grammar only allows a function call at the top level of a table @@ -157,9 +159,9 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, * This path is similar to ExecMakeFunctionResultSet. */ returnsSet = setexpr->funcReturnsSet; - InitFunctionCallInfoData(fcinfo, &(setexpr->func), + InitFunctionCallInfoData(*fcinfo, &(setexpr->func), list_length(setexpr->args), - setexpr->fcinfo_data.fncollation, + setexpr->fcinfo->fncollation, NULL, (Node *) &rsinfo); /* @@ -174,7 +176,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, */ MemoryContextReset(argContext); oldcontext = MemoryContextSwitchTo(argContext); - ExecEvalFuncArgs(&fcinfo, setexpr->args, econtext); + ExecEvalFuncArgs(fcinfo, setexpr->args, econtext); MemoryContextSwitchTo(oldcontext); /* @@ -186,9 +188,9 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, { int i; - for (i = 0; i < fcinfo.nargs; i++) + for (i = 0; i < fcinfo->nargs; i++) { - if (fcinfo.argnull[i]) + if (fcinfo->args[i].isnull) goto no_function_result; } } @@ -196,7 +198,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, else { /* Treat setexpr as a generic expression */ - InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); + InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); } /* @@ -224,11 +226,11 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, /* Call the function or expression one time */ if (!setexpr->elidedFuncState) { - pgstat_init_function_usage(&fcinfo, &fcusage); + pgstat_init_function_usage(fcinfo, &fcusage); - fcinfo.isnull = false; + fcinfo->isnull = false; rsinfo.isDone = ExprSingleResult; - result = FunctionCallInvoke(&fcinfo); + result = FunctionCallInvoke(fcinfo); pgstat_end_function_usage(&fcusage, rsinfo.isDone != ExprMultipleResult); @@ -236,7 +238,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, else { result = - ExecEvalExpr(setexpr->elidedFuncState, econtext, &fcinfo.isnull); + ExecEvalExpr(setexpr->elidedFuncState, econtext, &fcinfo->isnull); rsinfo.isDone = ExprSingleResult; } @@ -277,7 +279,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, */ if (returnsTuple) { - if (!fcinfo.isnull) + if (!fcinfo->isnull) { HeapTupleHeader td = DatumGetHeapTupleHeader(result); @@ -338,7 +340,7 @@ ExecMakeTableFunctionResult(SetExprState *setexpr, else { /* Scalar-type case: just store the function result */ - tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo.isnull); + tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo->isnull); } /* @@ -547,7 +549,7 @@ restart: * rows from this SRF have been returned, otherwise ValuePerCall SRFs * would reference freed memory after the first returned row. */ - fcinfo = &fcache->fcinfo_data; + fcinfo = fcache->fcinfo; arguments = fcache->args; if (!fcache->setArgsValid) { @@ -587,7 +589,7 @@ restart: { for (i = 0; i < fcinfo->nargs; i++) { - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) { callit = false; break; @@ -678,6 +680,7 @@ init_sexpr(Oid foid, Oid input_collation, Expr *node, MemoryContext sexprCxt, bool allowSRF, bool needDescForSRF) { AclResult aclresult; + size_t numargs = list_length(sexpr->args); /* Check permission to call function */ aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE); @@ -704,8 +707,10 @@ init_sexpr(Oid foid, Oid input_collation, Expr *node, fmgr_info_set_expr((Node *) sexpr->expr, &(sexpr->func)); /* Initialize the function call parameter struct as well */ - InitFunctionCallInfoData(sexpr->fcinfo_data, &(sexpr->func), - list_length(sexpr->args), + sexpr->fcinfo = + (FunctionCallInfo) palloc(SizeForFunctionCallInfo(numargs)); + InitFunctionCallInfoData(*sexpr->fcinfo, &(sexpr->func), + numargs, input_collation, NULL, NULL); /* If function returns set, check if that's allowed by caller */ @@ -820,9 +825,9 @@ ExecEvalFuncArgs(FunctionCallInfo fcinfo, { ExprState *argstate = (ExprState *) lfirst(arg); - fcinfo->arg[i] = ExecEvalExpr(argstate, - econtext, - &fcinfo->argnull[i]); + fcinfo->args[i].value = ExecEvalExpr(argstate, + econtext, + &fcinfo->args[i].isnull); i++; } |