diff options
author | Andres Freund | 2019-01-26 22:17:52 +0000 |
---|---|---|
committer | Andres Freund | 2019-01-26 22:17:52 +0000 |
commit | a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8 (patch) | |
tree | 4f34f2c902977a7ce0be7b1e32a12adf0cb223b7 /src/pl | |
parent | 6d3ede5f1c654f923b2767b0b0c3b09569adaa18 (diff) |
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/[email protected]
Diffstat (limited to 'src/pl')
-rw-r--r-- | src/pl/plperl/plperl.c | 20 | ||||
-rw-r--r-- | src/pl/plpgsql/src/pl_exec.c | 8 | ||||
-rw-r--r-- | src/pl/plpgsql/src/pl_handler.c | 20 | ||||
-rw-r--r-- | src/pl/plpython/plpy_exec.c | 4 | ||||
-rw-r--r-- | src/pl/plpython/plpy_main.c | 8 | ||||
-rw-r--r-- | src/pl/tcl/pltcl.c | 16 |
6 files changed, 38 insertions, 38 deletions
diff --git a/src/pl/plperl/plperl.c b/src/pl/plperl/plperl.c index fe54b209035..35d5d121a08 100644 --- a/src/pl/plperl/plperl.c +++ b/src/pl/plperl/plperl.c @@ -1876,8 +1876,8 @@ PG_FUNCTION_INFO_V1(plperl_inline_handler); Datum plperl_inline_handler(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(fake_fcinfo, 0); InlineCodeBlock *codeblock = (InlineCodeBlock *) PG_GETARG_POINTER(0); - FunctionCallInfoData fake_fcinfo; FmgrInfo flinfo; plperl_proc_desc desc; plperl_call_data *volatile save_call_data = current_call_data; @@ -1899,10 +1899,10 @@ plperl_inline_handler(PG_FUNCTION_ARGS) * plperl_call_perl_func(). In particular note that this sets things up * with no arguments passed, and a result type of VOID. */ - MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo)); + MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0)); MemSet(&flinfo, 0, sizeof(flinfo)); MemSet(&desc, 0, sizeof(desc)); - fake_fcinfo.flinfo = &flinfo; + fake_fcinfo->flinfo = &flinfo; flinfo.fn_oid = InvalidOid; flinfo.fn_mcxt = CurrentMemoryContext; @@ -1920,7 +1920,7 @@ plperl_inline_handler(PG_FUNCTION_ARGS) desc.nargs = 0; desc.reference = NULL; - this_call_data.fcinfo = &fake_fcinfo; + this_call_data.fcinfo = fake_fcinfo; this_call_data.prodesc = &desc; /* we do not bother with refcounting the fake prodesc */ @@ -1940,7 +1940,7 @@ plperl_inline_handler(PG_FUNCTION_ARGS) if (!desc.reference) /* can this happen? */ elog(ERROR, "could not create internal procedure for anonymous code block"); - perlret = plperl_call_perl_func(&desc, &fake_fcinfo); + perlret = plperl_call_perl_func(&desc, fake_fcinfo); SvREFCNT_dec_current(perlret); @@ -2194,11 +2194,11 @@ plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo) for (i = 0; i < desc->nargs; i++) { - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) PUSHs(&PL_sv_undef); else if (desc->arg_is_rowtype[i]) { - SV *sv = plperl_hash_from_datum(fcinfo->arg[i]); + SV *sv = plperl_hash_from_datum(fcinfo->args[i].value); PUSHs(sv_2mortal(sv)); } @@ -2208,15 +2208,15 @@ plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo) Oid funcid; if (OidIsValid(desc->arg_arraytype[i])) - sv = plperl_ref_from_pg_array(fcinfo->arg[i], desc->arg_arraytype[i]); + sv = plperl_ref_from_pg_array(fcinfo->args[i].value, desc->arg_arraytype[i]); else if ((funcid = get_transform_fromsql(argtypes[i], current_call_data->prodesc->lang_oid, current_call_data->prodesc->trftypes))) - sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, fcinfo->arg[i])); + sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, fcinfo->args[i].value)); else { char *tmp; tmp = OutputFunctionCall(&(desc->arg_out_func[i]), - fcinfo->arg[i]); + fcinfo->args[i].value); sv = cstr2sv(tmp); pfree(tmp); } diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 5c6dbe4c5fa..45f10f901b7 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -491,8 +491,8 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n]; assign_simple_var(&estate, var, - fcinfo->arg[i], - fcinfo->argnull[i], + fcinfo->args[i].value, + fcinfo->args[i].isnull, false); /* @@ -543,12 +543,12 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, { PLpgSQL_rec *rec = (PLpgSQL_rec *) estate.datums[n]; - if (!fcinfo->argnull[i]) + if (!fcinfo->args[i].isnull) { /* Assign row value from composite datum */ exec_move_row_from_datum(&estate, (PLpgSQL_variable *) rec, - fcinfo->arg[i]); + fcinfo->args[i].value); } else { diff --git a/src/pl/plpgsql/src/pl_handler.c b/src/pl/plpgsql/src/pl_handler.c index 2a9af06cccc..ce03f1ef840 100644 --- a/src/pl/plpgsql/src/pl_handler.c +++ b/src/pl/plpgsql/src/pl_handler.c @@ -299,9 +299,9 @@ PG_FUNCTION_INFO_V1(plpgsql_inline_handler); Datum plpgsql_inline_handler(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(fake_fcinfo, 0); InlineCodeBlock *codeblock = castNode(InlineCodeBlock, DatumGetPointer(PG_GETARG_DATUM(0))); PLpgSQL_function *func; - FunctionCallInfoData fake_fcinfo; FmgrInfo flinfo; EState *simple_eval_estate; Datum retval; @@ -324,9 +324,9 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS) * plpgsql_exec_function(). In particular note that this sets things up * with no arguments passed. */ - MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo)); + MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0)); MemSet(&flinfo, 0, sizeof(flinfo)); - fake_fcinfo.flinfo = &flinfo; + fake_fcinfo->flinfo = &flinfo; flinfo.fn_oid = InvalidOid; flinfo.fn_mcxt = CurrentMemoryContext; @@ -336,7 +336,7 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS) /* And run the function */ PG_TRY(); { - retval = plpgsql_exec_function(func, &fake_fcinfo, simple_eval_estate, codeblock->atomic); + retval = plpgsql_exec_function(func, fake_fcinfo, simple_eval_estate, codeblock->atomic); } PG_CATCH(); { @@ -466,7 +466,7 @@ plpgsql_validator(PG_FUNCTION_ARGS) /* Postpone body checks if !check_function_bodies */ if (check_function_bodies) { - FunctionCallInfoData fake_fcinfo; + LOCAL_FCINFO(fake_fcinfo, 0); FmgrInfo flinfo; int rc; TriggerData trigdata; @@ -482,26 +482,26 @@ plpgsql_validator(PG_FUNCTION_ARGS) * Set up a fake fcinfo with just enough info to satisfy * plpgsql_compile(). */ - MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo)); + MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0)); MemSet(&flinfo, 0, sizeof(flinfo)); - fake_fcinfo.flinfo = &flinfo; + fake_fcinfo->flinfo = &flinfo; flinfo.fn_oid = funcoid; flinfo.fn_mcxt = CurrentMemoryContext; if (is_dml_trigger) { MemSet(&trigdata, 0, sizeof(trigdata)); trigdata.type = T_TriggerData; - fake_fcinfo.context = (Node *) &trigdata; + fake_fcinfo->context = (Node *) &trigdata; } else if (is_event_trigger) { MemSet(&etrigdata, 0, sizeof(etrigdata)); etrigdata.type = T_EventTriggerData; - fake_fcinfo.context = (Node *) &etrigdata; + fake_fcinfo->context = (Node *) &etrigdata; } /* Test-compile the function */ - plpgsql_compile(&fake_fcinfo, true); + plpgsql_compile(fake_fcinfo, true); /* * Disconnect from SPI manager diff --git a/src/pl/plpython/plpy_exec.c b/src/pl/plpython/plpy_exec.c index 47ed95dcc60..21371862418 100644 --- a/src/pl/plpython/plpy_exec.c +++ b/src/pl/plpython/plpy_exec.c @@ -436,10 +436,10 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc) { PLyDatumToOb *arginfo = &proc->args[i]; - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) arg = NULL; else - arg = PLy_input_convert(arginfo, fcinfo->arg[i]); + arg = PLy_input_convert(arginfo, fcinfo->args[i].value); if (arg == NULL) { diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c index 6a66eba1762..6edfcf207e5 100644 --- a/src/pl/plpython/plpy_main.c +++ b/src/pl/plpython/plpy_main.c @@ -299,8 +299,8 @@ plpython2_call_handler(PG_FUNCTION_ARGS) Datum plpython_inline_handler(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(fake_fcinfo, 0); InlineCodeBlock *codeblock = (InlineCodeBlock *) DatumGetPointer(PG_GETARG_DATUM(0)); - FunctionCallInfoData fake_fcinfo; FmgrInfo flinfo; PLyProcedure proc; PLyExecutionContext *exec_ctx; @@ -312,9 +312,9 @@ plpython_inline_handler(PG_FUNCTION_ARGS) if (SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC) != SPI_OK_CONNECT) elog(ERROR, "SPI_connect failed"); - MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo)); + MemSet(fcinfo, 0, SizeForFunctionCallInfo(0)); MemSet(&flinfo, 0, sizeof(flinfo)); - fake_fcinfo.flinfo = &flinfo; + fake_fcinfo->flinfo = &flinfo; flinfo.fn_oid = InvalidOid; flinfo.fn_mcxt = CurrentMemoryContext; @@ -352,7 +352,7 @@ plpython_inline_handler(PG_FUNCTION_ARGS) PLy_procedure_compile(&proc, codeblock->source_text); exec_ctx->curr_proc = &proc; - PLy_exec_function(&fake_fcinfo, &proc); + PLy_exec_function(fake_fcinfo, &proc); } PG_CATCH(); { diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c index 3b1454f8335..bfbf62305c3 100644 --- a/src/pl/tcl/pltcl.c +++ b/src/pl/tcl/pltcl.c @@ -587,6 +587,7 @@ pltcl_fetch_interp(Oid prolang, bool pltrusted) static void call_pltcl_start_proc(Oid prolang, bool pltrusted) { + LOCAL_FCINFO(fcinfo, 0); char *start_proc; const char *gucname; ErrorContextCallback errcallback; @@ -597,7 +598,6 @@ call_pltcl_start_proc(Oid prolang, bool pltrusted) Form_pg_proc procStruct; AclResult aclresult; FmgrInfo finfo; - FunctionCallInfoData fcinfo; PgStat_FunctionCallUsage fcusage; /* select appropriate GUC */ @@ -658,11 +658,11 @@ call_pltcl_start_proc(Oid prolang, bool pltrusted) */ InvokeFunctionExecuteHook(procOid); fmgr_info(procOid, &finfo); - InitFunctionCallInfoData(fcinfo, &finfo, + InitFunctionCallInfoData(*fcinfo, &finfo, 0, InvalidOid, NULL, NULL); - pgstat_init_function_usage(&fcinfo, &fcusage); - (void) FunctionCallInvoke(&fcinfo); + pgstat_init_function_usage(fcinfo, &fcusage); + (void) FunctionCallInvoke(fcinfo); pgstat_end_function_usage(&fcusage, true); /* Pop the error context stack */ @@ -869,7 +869,7 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, /************************************************** * For tuple values, add a list for 'array set ...' **************************************************/ - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj()); else { @@ -880,7 +880,7 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, HeapTupleData tmptup; Tcl_Obj *list_tmp; - td = DatumGetHeapTupleHeader(fcinfo->arg[i]); + td = DatumGetHeapTupleHeader(fcinfo->args[i].value); /* Extract rowtype info and find a tupdesc */ tupType = HeapTupleHeaderGetTypeId(td); tupTypmod = HeapTupleHeaderGetTypMod(td); @@ -901,14 +901,14 @@ pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, * Single values are added as string element * of their external representation **************************************************/ - if (fcinfo->argnull[i]) + if (fcinfo->args[i].isnull) Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj()); else { char *tmp; tmp = OutputFunctionCall(&prodesc->arg_out_func[i], - fcinfo->arg[i]); + fcinfo->args[i].value); UTF_BEGIN; Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewStringObj(UTF_E2U(tmp), -1)); |