aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/nodeWindowAgg.c
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2019-01-26 14:17:52 -0800
committerAndres Freund <andres@anarazel.de>2019-01-26 14:17:52 -0800
commita9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8 (patch)
tree4f34f2c902977a7ce0be7b1e32a12adf0cb223b7 /src/backend/executor/nodeWindowAgg.c
parent6d3ede5f1c654f923b2767b0b0c3b09569adaa18 (diff)
downloadpostgresql-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/nodeWindowAgg.c')
-rw-r--r--src/backend/executor/nodeWindowAgg.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/src/backend/executor/nodeWindowAgg.c b/src/backend/executor/nodeWindowAgg.c
index 7ae56074ca8..731391f1907 100644
--- a/src/backend/executor/nodeWindowAgg.c
+++ b/src/backend/executor/nodeWindowAgg.c
@@ -241,10 +241,9 @@ advance_windowaggregate(WindowAggState *winstate,
WindowStatePerFunc perfuncstate,
WindowStatePerAgg peraggstate)
{
+ LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
int numArguments = perfuncstate->numArguments;
- FunctionCallInfoData fcinfodata;
- FunctionCallInfo fcinfo = &fcinfodata;
Datum newVal;
ListCell *arg;
int i;
@@ -273,8 +272,8 @@ advance_windowaggregate(WindowAggState *winstate,
{
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++;
}
@@ -287,7 +286,7 @@ advance_windowaggregate(WindowAggState *winstate,
*/
for (i = 1; i <= numArguments; i++)
{
- if (fcinfo->argnull[i])
+ if (fcinfo->args[i].isnull)
{
MemoryContextSwitchTo(oldContext);
return;
@@ -306,7 +305,7 @@ advance_windowaggregate(WindowAggState *winstate,
if (peraggstate->transValueCount == 0 && peraggstate->transValueIsNull)
{
MemoryContextSwitchTo(peraggstate->aggcontext);
- peraggstate->transValue = datumCopy(fcinfo->arg[1],
+ peraggstate->transValue = datumCopy(fcinfo->args[1].value,
peraggstate->transtypeByVal,
peraggstate->transtypeLen);
peraggstate->transValueIsNull = false;
@@ -339,8 +338,8 @@ advance_windowaggregate(WindowAggState *winstate,
numArguments + 1,
perfuncstate->winCollation,
(void *) winstate, NULL);
- fcinfo->arg[0] = peraggstate->transValue;
- fcinfo->argnull[0] = peraggstate->transValueIsNull;
+ fcinfo->args[0].value = peraggstate->transValue;
+ fcinfo->args[0].isnull = peraggstate->transValueIsNull;
winstate->curaggcontext = peraggstate->aggcontext;
newVal = FunctionCallInvoke(fcinfo);
winstate->curaggcontext = NULL;
@@ -418,10 +417,9 @@ advance_windowaggregate_base(WindowAggState *winstate,
WindowStatePerFunc perfuncstate,
WindowStatePerAgg peraggstate)
{
+ LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
WindowFuncExprState *wfuncstate = perfuncstate->wfuncstate;
int numArguments = perfuncstate->numArguments;
- FunctionCallInfoData fcinfodata;
- FunctionCallInfo fcinfo = &fcinfodata;
Datum newVal;
ListCell *arg;
int i;
@@ -450,8 +448,8 @@ advance_windowaggregate_base(WindowAggState *winstate,
{
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++;
}
@@ -464,7 +462,7 @@ advance_windowaggregate_base(WindowAggState *winstate,
*/
for (i = 1; i <= numArguments; i++)
{
- if (fcinfo->argnull[i])
+ if (fcinfo->args[i].isnull)
{
MemoryContextSwitchTo(oldContext);
return true;
@@ -510,8 +508,8 @@ advance_windowaggregate_base(WindowAggState *winstate,
numArguments + 1,
perfuncstate->winCollation,
(void *) winstate, NULL);
- fcinfo->arg[0] = peraggstate->transValue;
- fcinfo->argnull[0] = peraggstate->transValueIsNull;
+ fcinfo->args[0].value = peraggstate->transValue;
+ fcinfo->args[0].isnull = peraggstate->transValueIsNull;
winstate->curaggcontext = peraggstate->aggcontext;
newVal = FunctionCallInvoke(fcinfo);
winstate->curaggcontext = NULL;
@@ -591,30 +589,31 @@ finalize_windowaggregate(WindowAggState *winstate,
*/
if (OidIsValid(peraggstate->finalfn_oid))
{
+ LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
int numFinalArgs = peraggstate->numFinalArgs;
- FunctionCallInfoData fcinfo;
bool anynull;
int i;
- InitFunctionCallInfoData(fcinfo, &(peraggstate->finalfn),
+ InitFunctionCallInfoData(fcinfodata.fcinfo, &(peraggstate->finalfn),
numFinalArgs,
perfuncstate->winCollation,
(void *) winstate, NULL);
- fcinfo.arg[0] = MakeExpandedObjectReadOnly(peraggstate->transValue,
- peraggstate->transValueIsNull,
- peraggstate->transtypeLen);
- fcinfo.argnull[0] = peraggstate->transValueIsNull;
+ fcinfo->args[0].value =
+ MakeExpandedObjectReadOnly(peraggstate->transValue,
+ peraggstate->transValueIsNull,
+ peraggstate->transtypeLen);
+ fcinfo->args[0].isnull = peraggstate->transValueIsNull;
anynull = peraggstate->transValueIsNull;
/* Fill any remaining argument positions with nulls */
for (i = 1; i < numFinalArgs; i++)
{
- fcinfo.arg[i] = (Datum) 0;
- fcinfo.argnull[i] = true;
+ fcinfo->args[i].value = (Datum) 0;
+ fcinfo->args[i].isnull = true;
anynull = true;
}
- if (fcinfo.flinfo->fn_strict && anynull)
+ if (fcinfo->flinfo->fn_strict && anynull)
{
/* don't call a strict function with NULL inputs */
*result = (Datum) 0;
@@ -623,9 +622,9 @@ finalize_windowaggregate(WindowAggState *winstate,
else
{
winstate->curaggcontext = peraggstate->aggcontext;
- *result = FunctionCallInvoke(&fcinfo);
+ *result = FunctionCallInvoke(fcinfo);
winstate->curaggcontext = NULL;
- *isnull = fcinfo.isnull;
+ *isnull = fcinfo->isnull;
}
}
else
@@ -1032,7 +1031,7 @@ static void
eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
Datum *result, bool *isnull)
{
- FunctionCallInfoData fcinfo;
+ LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
MemoryContext oldContext;
oldContext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
@@ -1043,24 +1042,25 @@ eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate,
* implementations to support varying numbers of arguments. The real info
* goes through the WindowObject, which is passed via fcinfo->context.
*/
- InitFunctionCallInfoData(fcinfo, &(perfuncstate->flinfo),
+ InitFunctionCallInfoData(*fcinfo, &(perfuncstate->flinfo),
perfuncstate->numArguments,
perfuncstate->winCollation,
(void *) perfuncstate->winobj, NULL);
/* Just in case, make all the regular argument slots be null */
- memset(fcinfo.argnull, true, perfuncstate->numArguments);
+ for (int argno = 0; argno < perfuncstate->numArguments; argno++)
+ fcinfo->args[argno].isnull = true;
/* Window functions don't have a current aggregate context, either */
winstate->curaggcontext = NULL;
- *result = FunctionCallInvoke(&fcinfo);
- *isnull = fcinfo.isnull;
+ *result = FunctionCallInvoke(fcinfo);
+ *isnull = fcinfo->isnull;
/*
* Make sure pass-by-ref data is allocated in the appropriate context. (We
* need this in case the function returns a pointer into some short-lived
* tuple, as is entirely possible.)
*/
- if (!perfuncstate->resulttypeByVal && !fcinfo.isnull &&
+ if (!perfuncstate->resulttypeByVal && !fcinfo->isnull &&
!MemoryContextContains(CurrentMemoryContext,
DatumGetPointer(*result)))
*result = datumCopy(*result,