|
44 | 44 | * is used to run finalize functions and compute the output tuple;
|
45 | 45 | * this context can be reset once per output tuple.
|
46 | 46 | *
|
47 |
| - * Beginning in PostgreSQL 8.1, the executor's AggState node is passed as |
48 |
| - * the fmgr "context" value in all transfunc and finalfunc calls. It is |
49 |
| - * not really intended that the transition functions will look into the |
50 |
| - * AggState node, but they can use code like |
51 |
| - * if (fcinfo->context && IsA(fcinfo->context, AggState)) |
52 |
| - * to verify that they are being called by nodeAgg.c and not as ordinary |
53 |
| - * SQL functions. The main reason a transition function might want to know |
54 |
| - * that is that it can avoid palloc'ing a fixed-size pass-by-ref transition |
55 |
| - * value on every call: it can instead just scribble on and return its left |
56 |
| - * input. Ordinarily it is completely forbidden for functions to modify |
57 |
| - * pass-by-ref inputs, but in the aggregate case we know the left input is |
58 |
| - * either the initial transition value or a previous function result, and |
59 |
| - * in either case its value need not be preserved. See int8inc() for an |
60 |
| - * example. Notice that advance_transition_function() is coded to avoid a |
61 |
| - * data copy step when the previous transition value pointer is returned. |
62 |
| - * Also, some transition functions make use of the aggcontext to store |
63 |
| - * working state. |
| 47 | + * The executor's AggState node is passed as the fmgr "context" value in |
| 48 | + * all transfunc and finalfunc calls. It is not recommended that the |
| 49 | + * transition functions look at the AggState node directly, but they can |
| 50 | + * use AggCheckCallContext() to verify that they are being called by |
| 51 | + * nodeAgg.c (and not as ordinary SQL functions). The main reason a |
| 52 | + * transition function might want to know this is so that it can avoid |
| 53 | + * palloc'ing a fixed-size pass-by-ref transition value on every call: |
| 54 | + * it can instead just scribble on and return its left input. Ordinarily |
| 55 | + * it is completely forbidden for functions to modify pass-by-ref inputs, |
| 56 | + * but in the aggregate case we know the left input is either the initial |
| 57 | + * transition value or a previous function result, and in either case its |
| 58 | + * value need not be preserved. See int8inc() for an example. Notice that |
| 59 | + * advance_transition_function() is coded to avoid a data copy step when |
| 60 | + * the previous transition value pointer is returned. Also, some |
| 61 | + * transition functions want to store working state in addition to the |
| 62 | + * nominal transition value; they can use the memory context returned by |
| 63 | + * AggCheckCallContext() to do that. |
| 64 | + * |
| 65 | + * Note: AggCheckCallContext() is available as of PostgreSQL 9.0. The |
| 66 | + * AggState is available as context in earlier releases (back to 8.1), |
| 67 | + * but direct examination of the node is needed to use it before 9.0. |
64 | 68 | *
|
65 | 69 | *
|
66 | 70 | * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
67 | 71 | * Portions Copyright (c) 1994, Regents of the University of California
|
68 | 72 | *
|
69 | 73 | * IDENTIFICATION
|
70 |
| - * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.171 2010/01/02 16:57:41 momjian Exp $ |
| 74 | + * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.172 2010/02/08 20:39:51 tgl Exp $ |
71 | 75 | *
|
72 | 76 | *-------------------------------------------------------------------------
|
73 | 77 | */
|
@@ -1969,6 +1973,42 @@ ExecReScanAgg(AggState *node, ExprContext *exprCtxt)
|
1969 | 1973 | ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
|
1970 | 1974 | }
|
1971 | 1975 |
|
| 1976 | +/* |
| 1977 | + * AggCheckCallContext - test if a SQL function is being called as an aggregate |
| 1978 | + * |
| 1979 | + * The transition and/or final functions of an aggregate may want to verify |
| 1980 | + * that they are being called as aggregates, rather than as plain SQL |
| 1981 | + * functions. They should use this function to do so. The return value |
| 1982 | + * is nonzero if being called as an aggregate, or zero if not. (Specific |
| 1983 | + * nonzero values are AGG_CONTEXT_AGGREGATE or AGG_CONTEXT_WINDOW, but more |
| 1984 | + * values could conceivably appear in future.) |
| 1985 | + * |
| 1986 | + * If aggcontext isn't NULL, the function also stores at *aggcontext the |
| 1987 | + * identity of the memory context that aggregate transition values are |
| 1988 | + * being stored in. |
| 1989 | + */ |
| 1990 | +int |
| 1991 | +AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext) |
| 1992 | +{ |
| 1993 | + if (fcinfo->context && IsA(fcinfo->context, AggState)) |
| 1994 | + { |
| 1995 | + if (aggcontext) |
| 1996 | + *aggcontext = ((AggState *) fcinfo->context)->aggcontext; |
| 1997 | + return AGG_CONTEXT_AGGREGATE; |
| 1998 | + } |
| 1999 | + if (fcinfo->context && IsA(fcinfo->context, WindowAggState)) |
| 2000 | + { |
| 2001 | + if (aggcontext) |
| 2002 | + *aggcontext = ((WindowAggState *) fcinfo->context)->wincontext; |
| 2003 | + return AGG_CONTEXT_WINDOW; |
| 2004 | + } |
| 2005 | + |
| 2006 | + /* this is just to prevent "uninitialized variable" warnings */ |
| 2007 | + if (aggcontext) |
| 2008 | + *aggcontext = NULL; |
| 2009 | + return 0; |
| 2010 | +} |
| 2011 | + |
1972 | 2012 | /*
|
1973 | 2013 | * aggregate_dummy - dummy execution routine for aggregate functions
|
1974 | 2014 | *
|
|
0 commit comments