1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-30 21:42:05 +03:00

Redesign API presented by nodeAgg.c for ordered-set and similar aggregates.

The previous design exposed the input and output ExprContexts of the
Agg plan node, but work on grouping sets has suggested that we'll regret
doing that.  Instead provide more narrowly-defined APIs that can be
implemented in multiple ways, namely a way to get a short-term memory
context and a way to register an aggregate shutdown callback.

Back-patch to 9.4 where the bad APIs were introduced, since we don't
want third-party code using these APIs and then having to change in 9.5.

Andrew Gierth
This commit is contained in:
Tom Lane
2014-07-03 18:25:33 -04:00
parent 8b6010b835
commit 6f5034eda0
3 changed files with 34 additions and 28 deletions

View File

@ -2199,44 +2199,56 @@ AggGetAggref(FunctionCallInfo fcinfo)
}
/*
* AggGetPerTupleEContext - fetch per-input-tuple ExprContext
* AggGetTempMemoryContext - fetch short-term memory context for aggregates
*
* This is useful in agg final functions; the econtext returned is the
* same per-tuple context that the transfn was called in (which can
* safely get reset during the final function).
* This is useful in agg final functions; the context returned is one that
* the final function can safely reset as desired. This isn't useful for
* transition functions, since the context returned MAY (we don't promise)
* be the same as the context those are called in.
*
* As above, this is currently not useful for aggs called as window functions.
*/
ExprContext *
AggGetPerTupleEContext(FunctionCallInfo fcinfo)
MemoryContext
AggGetTempMemoryContext(FunctionCallInfo fcinfo)
{
if (fcinfo->context && IsA(fcinfo->context, AggState))
{
AggState *aggstate = (AggState *) fcinfo->context;
return aggstate->tmpcontext;
return aggstate->tmpcontext->ecxt_per_tuple_memory;
}
return NULL;
}
/*
* AggGetPerAggEContext - fetch per-output-tuple ExprContext
* AggRegisterCallback - register a cleanup callback for an aggregate
*
* This is useful for aggs to register shutdown callbacks, which will ensure
* that non-memory resources are freed.
* that non-memory resources are freed. The callback will occur just before
* the associated aggcontext (as returned by AggCheckCallContext) is reset,
* either between groups or as a result of rescanning the query. The callback
* will NOT be called on error paths. The typical use-case is for freeing of
* tuplestores or tuplesorts maintained in aggcontext, or pins held by slots
* created by the agg functions. (The callback will not be called until after
* the result of the finalfn is no longer needed, so it's safe for the finalfn
* to return data that will be freed by the callback.)
*
* As above, this is currently not useful for aggs called as window functions.
*/
ExprContext *
AggGetPerAggEContext(FunctionCallInfo fcinfo)
void
AggRegisterCallback(FunctionCallInfo fcinfo,
ExprContextCallbackFunction func,
Datum arg)
{
if (fcinfo->context && IsA(fcinfo->context, AggState))
{
AggState *aggstate = (AggState *) fcinfo->context;
return aggstate->ss.ps.ps_ExprContext;
RegisterExprContextCallback(aggstate->ss.ps.ps_ExprContext, func, arg);
return;
}
return NULL;
elog(ERROR, "aggregate function cannot register a callback in this context");
}