Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f5034e

Browse files
committed
Redesign API presented by nodeAgg.c for ordered-set and similar aggregates.
The previous design exposed the input and output ExprContexts of theAgg plan node, but work on grouping sets has suggested that we'll regretdoing that. Instead provide more narrowly-defined APIs that can beimplemented in multiple ways, namely a way to get a short-term memorycontext and a way to register an aggregate shutdown callback.Back-patch to 9.4 where the bad APIs were introduced, since we don'twant third-party code using these APIs and then having to change in 9.5.Andrew Gierth
1 parent8b6010b commit6f5034e

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

‎src/backend/executor/nodeAgg.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,44 +2199,56 @@ AggGetAggref(FunctionCallInfo fcinfo)
21992199
}
22002200

22012201
/*
2202-
*AggGetPerTupleEContext - fetchper-input-tuple ExprContext
2202+
*AggGetTempMemoryContext - fetchshort-term memory context for aggregates
22032203
*
2204-
* This is useful in agg final functions; the econtext returned is the
2205-
* same per-tuple context that the transfn was called in (which can
2206-
* safely get reset during the final function).
2204+
* This is useful in agg final functions; the context returned is one that
2205+
* the final function can safely reset as desired. This isn't useful for
2206+
* transition functions, since the context returned MAY (we don't promise)
2207+
* be the same as the context those are called in.
22072208
*
22082209
* As above, this is currently not useful for aggs called as window functions.
22092210
*/
2210-
ExprContext*
2211-
AggGetPerTupleEContext(FunctionCallInfofcinfo)
2211+
MemoryContext
2212+
AggGetTempMemoryContext(FunctionCallInfofcinfo)
22122213
{
22132214
if (fcinfo->context&&IsA(fcinfo->context,AggState))
22142215
{
22152216
AggState*aggstate= (AggState*)fcinfo->context;
22162217

2217-
returnaggstate->tmpcontext;
2218+
returnaggstate->tmpcontext->ecxt_per_tuple_memory;
22182219
}
22192220
returnNULL;
22202221
}
22212222

22222223
/*
2223-
*AggGetPerAggEContext -fetch per-output-tuple ExprContext
2224+
*AggRegisterCallback -register a cleanup callback for an aggregate
22242225
*
22252226
* This is useful for aggs to register shutdown callbacks, which will ensure
2226-
* that non-memory resources are freed.
2227+
* that non-memory resources are freed. The callback will occur just before
2228+
* the associated aggcontext (as returned by AggCheckCallContext) is reset,
2229+
* either between groups or as a result of rescanning the query. The callback
2230+
* will NOT be called on error paths. The typical use-case is for freeing of
2231+
* tuplestores or tuplesorts maintained in aggcontext, or pins held by slots
2232+
* created by the agg functions. (The callback will not be called until after
2233+
* the result of the finalfn is no longer needed, so it's safe for the finalfn
2234+
* to return data that will be freed by the callback.)
22272235
*
22282236
* As above, this is currently not useful for aggs called as window functions.
22292237
*/
2230-
ExprContext*
2231-
AggGetPerAggEContext(FunctionCallInfofcinfo)
2238+
void
2239+
AggRegisterCallback(FunctionCallInfofcinfo,
2240+
ExprContextCallbackFunctionfunc,
2241+
Datumarg)
22322242
{
22332243
if (fcinfo->context&&IsA(fcinfo->context,AggState))
22342244
{
22352245
AggState*aggstate= (AggState*)fcinfo->context;
22362246

2237-
returnaggstate->ss.ps.ps_ExprContext;
2247+
RegisterExprContextCallback(aggstate->ss.ps.ps_ExprContext,func,arg);
2248+
2249+
return;
22382250
}
2239-
returnNULL;
2251+
elog(ERROR,"aggregate function cannot register a callback in this context");
22402252
}
22412253

22422254

‎src/backend/utils/adt/orderedsetaggs.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ typedef struct OSAPerQueryState
4949
MemoryContextqcontext;
5050
/* Memory context containing per-group data: */
5151
MemoryContextgcontext;
52-
/* Agg plan node's output econtext: */
53-
ExprContext*peraggecontext;
5452

5553
/* These fields are used only when accumulating tuples: */
5654

@@ -117,7 +115,6 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
117115
Aggref*aggref;
118116
MemoryContextqcontext;
119117
MemoryContextgcontext;
120-
ExprContext*peraggecontext;
121118
List*sortlist;
122119
intnumSortCols;
123120

@@ -133,10 +130,6 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
133130
elog(ERROR,"ordered-set aggregate called in non-aggregate context");
134131
if (!AGGKIND_IS_ORDERED_SET(aggref->aggkind))
135132
elog(ERROR,"ordered-set aggregate support function called for non-ordered-set aggregate");
136-
/* Also get output exprcontext so we can register shutdown callback */
137-
peraggecontext=AggGetPerAggEContext(fcinfo);
138-
if (!peraggecontext)
139-
elog(ERROR,"ordered-set aggregate called in non-aggregate context");
140133

141134
/*
142135
* Prepare per-query structures in the fn_mcxt, which we assume is the
@@ -150,7 +143,6 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
150143
qstate->aggref=aggref;
151144
qstate->qcontext=qcontext;
152145
qstate->gcontext=gcontext;
153-
qstate->peraggecontext=peraggecontext;
154146

155147
/* Extract the sort information */
156148
sortlist=aggref->aggorder;
@@ -291,9 +283,9 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
291283
osastate->number_of_rows=0;
292284

293285
/* Now register a shutdown callback to clean things up */
294-
RegisterExprContextCallback(qstate->peraggecontext,
295-
ordered_set_shutdown,
296-
PointerGetDatum(osastate));
286+
AggRegisterCallback(fcinfo,
287+
ordered_set_shutdown,
288+
PointerGetDatum(osastate));
297289

298290
MemoryContextSwitchTo(oldcontext);
299291

@@ -1310,7 +1302,7 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS)
13101302
sortColIdx=osastate->qstate->sortColIdx;
13111303

13121304
/* Get short-term context we can use for execTuplesMatch */
1313-
tmpcontext=AggGetPerTupleEContext(fcinfo)->ecxt_per_tuple_memory;
1305+
tmpcontext=AggGetTempMemoryContext(fcinfo);
13141306

13151307
/* insert the hypothetical row into the sort */
13161308
slot=osastate->qstate->tupslot;

‎src/include/fmgr.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef struct Node *fmNodePtr;
2323
typedefstructAggref*fmAggrefPtr;
2424

2525
/* Likewise, avoid including execnodes.h here */
26-
typedefstructExprContext*fmExprContextPtr;
26+
typedefvoid (*fmExprContextCallbackFunction) (Datumarg);
2727

2828
/* Likewise, avoid including stringinfo.h here */
2929
typedefstructStringInfoData*fmStringInfo;
@@ -656,8 +656,10 @@ extern void **find_rendezvous_variable(const char *varName);
656656
externintAggCheckCallContext(FunctionCallInfofcinfo,
657657
MemoryContext*aggcontext);
658658
externfmAggrefPtrAggGetAggref(FunctionCallInfofcinfo);
659-
externfmExprContextPtrAggGetPerTupleEContext(FunctionCallInfofcinfo);
660-
externfmExprContextPtrAggGetPerAggEContext(FunctionCallInfofcinfo);
659+
externMemoryContextAggGetTempMemoryContext(FunctionCallInfofcinfo);
660+
externvoidAggRegisterCallback(FunctionCallInfofcinfo,
661+
fmExprContextCallbackFunctionfunc,
662+
Datumarg);
661663

662664
/*
663665
* We allow plugin modules to hook function entry/exit. This is intended

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp