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/nodeAgg.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/nodeAgg.c')
-rw-r--r-- | src/backend/executor/nodeAgg.c | 83 |
1 files changed, 47 insertions, 36 deletions
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 508c9195743..2a260a71f24 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -553,7 +553,7 @@ advance_transition_function(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroupstate) { - FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; + FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; MemoryContext oldContext; Datum newVal; @@ -568,7 +568,7 @@ advance_transition_function(AggState *aggstate, for (i = 1; i <= numTransInputs; i++) { - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) return; } if (pergroupstate->noTransValue) @@ -584,7 +584,7 @@ advance_transition_function(AggState *aggstate, */ oldContext = MemoryContextSwitchTo( aggstate->curaggcontext->ecxt_per_tuple_memory); - pergroupstate->transValue = datumCopy(fcinfo->arg[1], + pergroupstate->transValue = datumCopy(fcinfo->args[1].value, pertrans->transtypeByVal, pertrans->transtypeLen); pergroupstate->transValueIsNull = false; @@ -613,8 +613,8 @@ advance_transition_function(AggState *aggstate, /* * OK to call the transition function */ - fcinfo->arg[0] = pergroupstate->transValue; - fcinfo->argnull[0] = pergroupstate->transValueIsNull; + fcinfo->args[0].value = pergroupstate->transValue; + fcinfo->args[0].isnull = pergroupstate->transValueIsNull; fcinfo->isnull = false; /* just in case transfn doesn't set it */ newVal = FunctionCallInvoke(fcinfo); @@ -717,7 +717,7 @@ process_ordered_aggregate_single(AggState *aggstate, bool isDistinct = (pertrans->numDistinctCols > 0); Datum newAbbrevVal = (Datum) 0; Datum oldAbbrevVal = (Datum) 0; - FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; + FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; Datum *newVal; bool *isNull; @@ -726,8 +726,8 @@ process_ordered_aggregate_single(AggState *aggstate, tuplesort_performsort(pertrans->sortstates[aggstate->current_set]); /* Load the column into argument 1 (arg 0 will be transition value) */ - newVal = fcinfo->arg + 1; - isNull = fcinfo->argnull + 1; + newVal = &fcinfo->args[1].value; + isNull = &fcinfo->args[1].isnull; /* * Note: if input type is pass-by-ref, the datums returned by the sort are @@ -803,7 +803,7 @@ process_ordered_aggregate_multi(AggState *aggstate, AggStatePerGroup pergroupstate) { ExprContext *tmpcontext = aggstate->tmpcontext; - FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo; + FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; TupleTableSlot *slot1 = pertrans->sortslot; TupleTableSlot *slot2 = pertrans->uniqslot; int numTransInputs = pertrans->numTransInputs; @@ -843,8 +843,8 @@ process_ordered_aggregate_multi(AggState *aggstate, /* Start from 1, since the 0th arg will be the transition value */ for (i = 0; i < numTransInputs; i++) { - fcinfo->arg[i + 1] = slot1->tts_values[i]; - fcinfo->argnull[i + 1] = slot1->tts_isnull[i]; + fcinfo->args[i + 1].value = slot1->tts_values[i]; + fcinfo->args[i + 1].isnull = slot1->tts_isnull[i]; } advance_transition_function(aggstate, pertrans, pergroupstate); @@ -897,7 +897,7 @@ finalize_aggregate(AggState *aggstate, AggStatePerGroup pergroupstate, Datum *resultVal, bool *resultIsNull) { - FunctionCallInfoData fcinfo; + LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS); bool anynull = false; MemoryContext oldContext; int i; @@ -917,10 +917,10 @@ finalize_aggregate(AggState *aggstate, { ExprState *expr = (ExprState *) lfirst(lc); - fcinfo.arg[i] = ExecEvalExpr(expr, - aggstate->ss.ps.ps_ExprContext, - &fcinfo.argnull[i]); - anynull |= fcinfo.argnull[i]; + fcinfo->args[i].value = ExecEvalExpr(expr, + aggstate->ss.ps.ps_ExprContext, + &fcinfo->args[i].isnull); + anynull |= fcinfo->args[i].isnull; i++; } @@ -934,27 +934,28 @@ finalize_aggregate(AggState *aggstate, /* set up aggstate->curperagg for AggGetAggref() */ aggstate->curperagg = peragg; - InitFunctionCallInfoData(fcinfo, &peragg->finalfn, + InitFunctionCallInfoData(*fcinfo, &peragg->finalfn, numFinalArgs, pertrans->aggCollation, (void *) aggstate, NULL); /* Fill in the transition state value */ - fcinfo.arg[0] = MakeExpandedObjectReadOnly(pergroupstate->transValue, - pergroupstate->transValueIsNull, - pertrans->transtypeLen); - fcinfo.argnull[0] = pergroupstate->transValueIsNull; + fcinfo->args[0].value = + MakeExpandedObjectReadOnly(pergroupstate->transValue, + pergroupstate->transValueIsNull, + pertrans->transtypeLen); + fcinfo->args[0].isnull = pergroupstate->transValueIsNull; anynull |= pergroupstate->transValueIsNull; /* Fill any remaining argument positions with nulls */ for (; 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 */ *resultVal = (Datum) 0; @@ -962,8 +963,8 @@ finalize_aggregate(AggState *aggstate, } else { - *resultVal = FunctionCallInvoke(&fcinfo); - *resultIsNull = fcinfo.isnull; + *resultVal = FunctionCallInvoke(fcinfo); + *resultIsNull = fcinfo->isnull; } aggstate->curperagg = NULL; } @@ -1018,12 +1019,13 @@ finalize_partialaggregate(AggState *aggstate, } else { - FunctionCallInfo fcinfo = &pertrans->serialfn_fcinfo; + FunctionCallInfo fcinfo = pertrans->serialfn_fcinfo; - fcinfo->arg[0] = MakeExpandedObjectReadOnly(pergroupstate->transValue, - pergroupstate->transValueIsNull, - pertrans->transtypeLen); - fcinfo->argnull[0] = pergroupstate->transValueIsNull; + fcinfo->args[0].value = + MakeExpandedObjectReadOnly(pergroupstate->transValue, + pergroupstate->transValueIsNull, + pertrans->transtypeLen); + fcinfo->args[0].isnull = pergroupstate->transValueIsNull; *resultVal = FunctionCallInvoke(fcinfo); *resultIsNull = fcinfo->isnull; @@ -2927,7 +2929,9 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, fmgr_info(aggtransfn, &pertrans->transfn); fmgr_info_set_expr((Node *) combinefnexpr, &pertrans->transfn); - InitFunctionCallInfoData(pertrans->transfn_fcinfo, + pertrans->transfn_fcinfo = + (FunctionCallInfo) palloc(SizeForFunctionCallInfo(2)); + InitFunctionCallInfoData(*pertrans->transfn_fcinfo, &pertrans->transfn, 2, pertrans->aggCollation, @@ -2947,6 +2951,7 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, else { Expr *transfnexpr; + size_t numInputs = pertrans->numTransInputs + 1; /* * Set up infrastructure for calling the transfn. Note that invtrans @@ -2965,9 +2970,11 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, fmgr_info(aggtransfn, &pertrans->transfn); fmgr_info_set_expr((Node *) transfnexpr, &pertrans->transfn); - InitFunctionCallInfoData(pertrans->transfn_fcinfo, + pertrans->transfn_fcinfo = + (FunctionCallInfo) palloc(SizeForFunctionCallInfo(numInputs)); + InitFunctionCallInfoData(*pertrans->transfn_fcinfo, &pertrans->transfn, - pertrans->numTransInputs + 1, + numInputs, pertrans->aggCollation, (void *) aggstate, NULL); @@ -3003,7 +3010,9 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, fmgr_info(aggserialfn, &pertrans->serialfn); fmgr_info_set_expr((Node *) serialfnexpr, &pertrans->serialfn); - InitFunctionCallInfoData(pertrans->serialfn_fcinfo, + pertrans->serialfn_fcinfo = + (FunctionCallInfo) palloc(SizeForFunctionCallInfo(1)); + InitFunctionCallInfoData(*pertrans->serialfn_fcinfo, &pertrans->serialfn, 1, InvalidOid, @@ -3017,7 +3026,9 @@ build_pertrans_for_aggref(AggStatePerTrans pertrans, fmgr_info(aggdeserialfn, &pertrans->deserialfn); fmgr_info_set_expr((Node *) deserialfnexpr, &pertrans->deserialfn); - InitFunctionCallInfoData(pertrans->deserialfn_fcinfo, + pertrans->deserialfn_fcinfo = + (FunctionCallInfo) palloc(SizeForFunctionCallInfo(2)); + InitFunctionCallInfoData(*pertrans->deserialfn_fcinfo, &pertrans->deserialfn, 2, InvalidOid, |