@@ -47,8 +47,6 @@ typedef struct OSAPerQueryState
4747Aggref * aggref ;
4848/* Memory context containing this struct and other per-query data: */
4949MemoryContext qcontext ;
50- /* Memory context containing per-group data: */
51- MemoryContext gcontext ;
5250
5351/* These fields are used only when accumulating tuples: */
5452
@@ -86,6 +84,8 @@ typedef struct OSAPerGroupState
8684{
8785/* Link to the per-query state for this aggregate: */
8886OSAPerQueryState * qstate ;
87+ /* Memory context containing per-group data: */
88+ MemoryContext gcontext ;
8989/* Sort object we're accumulating data in: */
9090Tuplesortstate * sortstate ;
9191/* Number of normal rows inserted into sortstate: */
@@ -103,8 +103,17 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
103103{
104104OSAPerGroupState * osastate ;
105105OSAPerQueryState * qstate ;
106+ MemoryContext gcontext ;
106107MemoryContext oldcontext ;
107108
109+ /*
110+ * Check we're called as aggregate (and not a window function), and get
111+ * the Agg node's group-lifespan context (which might change from group to
112+ * group, so we shouldn't cache it in the per-query state).
113+ */
114+ if (AggCheckCallContext (fcinfo ,& gcontext )!= AGG_CONTEXT_AGGREGATE )
115+ elog (ERROR ,"ordered-set aggregate called in non-aggregate context" );
116+
108117/*
109118 * We keep a link to the per-query state in fn_extra; if it's not there,
110119 * create it, and do the per-query setup we need.
@@ -114,17 +123,10 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
114123{
115124Aggref * aggref ;
116125MemoryContext qcontext ;
117- MemoryContext gcontext ;
118126List * sortlist ;
119127int numSortCols ;
120128
121- /*
122- * Check we're called as aggregate (and not a window function), and
123- * get the Agg node's group-lifespan context
124- */
125- if (AggCheckCallContext (fcinfo ,& gcontext )!= AGG_CONTEXT_AGGREGATE )
126- elog (ERROR ,"ordered-set aggregate called in non-aggregate context" );
127- /* Need the Aggref as well */
129+ /* Get the Aggref so we can examine aggregate's arguments */
128130aggref = AggGetAggref (fcinfo );
129131if (!aggref )
130132elog (ERROR ,"ordered-set aggregate called in non-aggregate context" );
@@ -142,7 +144,6 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
142144qstate = (OSAPerQueryState * )palloc0 (sizeof (OSAPerQueryState ));
143145qstate -> aggref = aggref ;
144146qstate -> qcontext = qcontext ;
145- qstate -> gcontext = gcontext ;
146147
147148/* Extract the sort information */
148149sortlist = aggref -> aggorder ;
@@ -259,10 +260,11 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
259260}
260261
261262/* Now build the stuff we need in group-lifespan context */
262- oldcontext = MemoryContextSwitchTo (qstate -> gcontext );
263+ oldcontext = MemoryContextSwitchTo (gcontext );
263264
264265osastate = (OSAPerGroupState * )palloc (sizeof (OSAPerGroupState ));
265266osastate -> qstate = qstate ;
267+ osastate -> gcontext = gcontext ;
266268
267269/* Initialize tuplesort object */
268270if (use_tuples )
@@ -282,7 +284,7 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
282284
283285osastate -> number_of_rows = 0 ;
284286
285- /* Now register a shutdown callback to clean things up */
287+ /* Now register a shutdown callback to clean things upat end of group */
286288AggRegisterCallback (fcinfo ,
287289ordered_set_shutdown ,
288290PointerGetDatum (osastate ));