Aggref *aggref;
/* Memory context containing this struct and other per-query data: */
MemoryContext qcontext;
- /* Memory context containing per-group data: */
- MemoryContext gcontext;
/* These fields are used only when accumulating tuples: */
{
/* Link to the per-query state for this aggregate: */
OSAPerQueryState *qstate;
+ /* Memory context containing per-group data: */
+ MemoryContext gcontext;
/* Sort object we're accumulating data in: */
Tuplesortstate *sortstate;
/* Number of normal rows inserted into sortstate: */
{
OSAPerGroupState *osastate;
OSAPerQueryState *qstate;
+ MemoryContext gcontext;
MemoryContext oldcontext;
+ /*
+ * Check we're called as aggregate (and not a window function), and get
+ * the Agg node's group-lifespan context (which might change from group to
+ * group, so we shouldn't cache it in the per-query state).
+ */
+ if (AggCheckCallContext(fcinfo, &gcontext) != AGG_CONTEXT_AGGREGATE)
+ elog(ERROR, "ordered-set aggregate called in non-aggregate context");
+
/*
* We keep a link to the per-query state in fn_extra; if it's not there,
* create it, and do the per-query setup we need.
{
Aggref *aggref;
MemoryContext qcontext;
- MemoryContext gcontext;
List *sortlist;
int numSortCols;
- /*
- * Check we're called as aggregate (and not a window function), and
- * get the Agg node's group-lifespan context
- */
- if (AggCheckCallContext(fcinfo, &gcontext) != AGG_CONTEXT_AGGREGATE)
- elog(ERROR, "ordered-set aggregate called in non-aggregate context");
- /* Need the Aggref as well */
+ /* Get the Aggref so we can examine aggregate's arguments */
aggref = AggGetAggref(fcinfo);
if (!aggref)
elog(ERROR, "ordered-set aggregate called in non-aggregate context");
qstate = (OSAPerQueryState *) palloc0(sizeof(OSAPerQueryState));
qstate->aggref = aggref;
qstate->qcontext = qcontext;
- qstate->gcontext = gcontext;
/* Extract the sort information */
sortlist = aggref->aggorder;
}
/* Now build the stuff we need in group-lifespan context */
- oldcontext = MemoryContextSwitchTo(qstate->gcontext);
+ oldcontext = MemoryContextSwitchTo(gcontext);
osastate = (OSAPerGroupState *) palloc(sizeof(OSAPerGroupState));
osastate->qstate = qstate;
+ osastate->gcontext = gcontext;
/* Initialize tuplesort object */
if (use_tuples)
osastate->number_of_rows = 0;
- /* Now register a shutdown callback to clean things up */
+ /* Now register a shutdown callback to clean things up at end of group */
AggRegisterCallback(fcinfo,
ordered_set_shutdown,
PointerGetDatum(osastate));