aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/execExpr.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/execExpr.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/execExpr.c')
-rw-r--r--src/backend/executor/execExpr.c83
1 files changed, 45 insertions, 38 deletions
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index 4047d24b03e..2e061a5ee36 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -966,7 +966,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
- fcinfo = palloc0(sizeof(FunctionCallInfoData));
+ fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(opexpr->opfuncid, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
@@ -974,7 +974,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* Evaluate scalar directly into left function argument */
ExecInitExprRec(scalararg, state,
- &fcinfo->arg[0], &fcinfo->argnull[0]);
+ &fcinfo->args[0].value, &fcinfo->args[0].isnull);
/*
* Evaluate array argument into our return value. There's no
@@ -1263,7 +1263,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* lookup the source type's output function */
scratch.d.iocoerce.finfo_out = palloc0(sizeof(FmgrInfo));
- scratch.d.iocoerce.fcinfo_data_out = palloc0(sizeof(FunctionCallInfoData));
+ scratch.d.iocoerce.fcinfo_data_out = palloc0(SizeForFunctionCallInfo(1));
getTypeOutputInfo(exprType((Node *) iocoerce->arg),
&iofunc, &typisvarlena);
@@ -1275,7 +1275,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* lookup the result type's input function */
scratch.d.iocoerce.finfo_in = palloc0(sizeof(FmgrInfo));
- scratch.d.iocoerce.fcinfo_data_in = palloc0(sizeof(FunctionCallInfoData));
+ scratch.d.iocoerce.fcinfo_data_in = palloc0(SizeForFunctionCallInfo(3));
getTypeInputInfo(iocoerce->resulttype,
&iofunc, &typioparam);
@@ -1290,10 +1290,10 @@ ExecInitExprRec(Expr *node, ExprState *state,
* function, since they're constants.
*/
fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
- fcinfo_in->arg[1] = ObjectIdGetDatum(typioparam);
- fcinfo_in->argnull[1] = false;
- fcinfo_in->arg[2] = Int32GetDatum(-1);
- fcinfo_in->argnull[2] = false;
+ fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
+ fcinfo_in->args[1].isnull = false;
+ fcinfo_in->args[2].value = Int32GetDatum(-1);
+ fcinfo_in->args[2].isnull = false;
ExprEvalPushStep(state, &scratch);
break;
@@ -1735,7 +1735,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
- fcinfo = palloc0(sizeof(FunctionCallInfoData));
+ fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(proc, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
@@ -1750,9 +1750,9 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* evaluate left and right args directly into fcinfo */
ExecInitExprRec(left_expr, state,
- &fcinfo->arg[0], &fcinfo->argnull[0]);
+ &fcinfo->args[0].value, &fcinfo->args[0].isnull);
ExecInitExprRec(right_expr, state,
- &fcinfo->arg[1], &fcinfo->argnull[1]);
+ &fcinfo->args[1].value, &fcinfo->args[1].isnull);
scratch.opcode = EEOP_ROWCOMPARE_STEP;
scratch.d.rowcompare_step.finfo = finfo;
@@ -1878,7 +1878,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
/* Perform function lookup */
finfo = palloc0(sizeof(FmgrInfo));
- fcinfo = palloc0(sizeof(FunctionCallInfoData));
+ fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(typentry->cmp_proc, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
@@ -2187,7 +2187,7 @@ ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
/* Allocate function lookup data and parameter workspace for this call */
scratch->d.func.finfo = palloc0(sizeof(FmgrInfo));
- scratch->d.func.fcinfo_data = palloc0(sizeof(FunctionCallInfoData));
+ scratch->d.func.fcinfo_data = palloc0(SizeForFunctionCallInfo(nargs));
flinfo = scratch->d.func.finfo;
fcinfo = scratch->d.func.fcinfo_data;
@@ -2226,13 +2226,14 @@ ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
*/
Const *con = (Const *) arg;
- fcinfo->arg[argno] = con->constvalue;
- fcinfo->argnull[argno] = con->constisnull;
+ fcinfo->args[argno].value = con->constvalue;
+ fcinfo->args[argno].isnull = con->constisnull;
}
else
{
ExecInitExprRec(arg, state,
- &fcinfo->arg[argno], &fcinfo->argnull[argno]);
+ &fcinfo->args[argno].value,
+ &fcinfo->args[argno].isnull);
}
argno++;
}
@@ -2961,10 +2962,11 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
AggStatePerTrans pertrans = &aggstate->pertrans[transno];
int argno;
int setno;
- FunctionCallInfo trans_fcinfo = &pertrans->transfn_fcinfo;
+ FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
ListCell *arg;
ListCell *bail;
List *adjust_bailout = NIL;
+ NullableDatum *strictargs = NULL;
bool *strictnulls = NULL;
/*
@@ -3002,7 +3004,7 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
Assert(pertrans->numSortCols == 0);
Assert(list_length(pertrans->aggref->args) == 1);
- strictnulls = trans_fcinfo->argnull + 1;
+ strictargs = trans_fcinfo->args + 1;
source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
/*
@@ -3016,21 +3018,21 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
* value
*/
ExecInitExprRec(source_tle->expr, state,
- &trans_fcinfo->arg[argno + 1],
- &trans_fcinfo->argnull[argno + 1]);
+ &trans_fcinfo->args[argno + 1].value,
+ &trans_fcinfo->args[argno + 1].isnull);
}
else
{
- FunctionCallInfo ds_fcinfo = &pertrans->deserialfn_fcinfo;
+ FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
/* evaluate argument */
ExecInitExprRec(source_tle->expr, state,
- &ds_fcinfo->arg[0],
- &ds_fcinfo->argnull[0]);
+ &ds_fcinfo->args[0].value,
+ &ds_fcinfo->args[0].isnull);
/* Dummy second argument for type-safety reasons */
- ds_fcinfo->arg[1] = PointerGetDatum(NULL);
- ds_fcinfo->argnull[1] = false;
+ ds_fcinfo->args[1].value = PointerGetDatum(NULL);
+ ds_fcinfo->args[1].isnull = false;
/*
* Don't call a strict deserialization function with NULL
@@ -3044,8 +3046,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
scratch.d.agg_deserialize.aggstate = aggstate;
scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
- scratch.resvalue = &trans_fcinfo->arg[argno + 1];
- scratch.resnull = &trans_fcinfo->argnull[argno + 1];
+ scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
+ scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
ExprEvalPushStep(state, &scratch);
adjust_bailout = lappend_int(adjust_bailout,
@@ -3062,7 +3064,7 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
/*
* Normal transition function without ORDER BY / DISTINCT.
*/
- strictnulls = trans_fcinfo->argnull + 1;
+ strictargs = trans_fcinfo->args + 1;
foreach(arg, pertrans->aggref->args)
{
@@ -3073,8 +3075,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
* value
*/
ExecInitExprRec(source_tle->expr, state,
- &trans_fcinfo->arg[argno + 1],
- &trans_fcinfo->argnull[argno + 1]);
+ &trans_fcinfo->args[argno + 1].value,
+ &trans_fcinfo->args[argno + 1].isnull);
argno++;
}
}
@@ -3122,8 +3124,12 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
*/
if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
{
- scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK;
+ if (strictnulls)
+ scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
+ else
+ scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
scratch.d.agg_strict_input_check.nulls = strictnulls;
+ scratch.d.agg_strict_input_check.args = strictargs;
scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
ExprEvalPushStep(state, &scratch);
@@ -3177,7 +3183,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
Assert(as->d.jump.jumpdone == -1);
as->d.jump.jumpdone = state->steps_len;
}
- else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK)
+ else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
+ as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
{
Assert(as->d.agg_strict_input_check.jumpnull == -1);
as->d.agg_strict_input_check.jumpnull = state->steps_len;
@@ -3312,7 +3319,7 @@ ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
*/
ExprState *
ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
- const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
+ const TupleTableSlotOps * lops, const TupleTableSlotOps * rops,
int numCols,
const AttrNumber *keyColIdx,
const Oid *eqfunctions,
@@ -3389,7 +3396,7 @@ ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
- fcinfo = palloc0(sizeof(FunctionCallInfoData));
+ fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(foid, finfo);
fmgr_info_set_expr(NULL, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
@@ -3399,16 +3406,16 @@ ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
scratch.opcode = EEOP_INNER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = latt->atttypid;
- scratch.resvalue = &fcinfo->arg[0];
- scratch.resnull = &fcinfo->argnull[0];
+ scratch.resvalue = &fcinfo->args[0].value;
+ scratch.resnull = &fcinfo->args[0].isnull;
ExprEvalPushStep(state, &scratch);
/* right arg */
scratch.opcode = EEOP_OUTER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = ratt->atttypid;
- scratch.resvalue = &fcinfo->arg[1];
- scratch.resnull = &fcinfo->argnull[1];
+ scratch.resvalue = &fcinfo->args[1].value;
+ scratch.resnull = &fcinfo->args[1].isnull;
ExprEvalPushStep(state, &scratch);
/* evaluate distinctness */