*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.11 2010/01/02 16:57:32 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/tsearch2/tsearch2.c,v 1.12 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
MemoryContext aggcontext;
MemoryContext oldcontext;
- if (fcinfo->context && IsA(fcinfo->context, AggState))
- aggcontext = ((AggState *) fcinfo->context)->aggcontext;
- else if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
- aggcontext = ((WindowAggState *) fcinfo->context)->wincontext;
- else
- {
+ if (!AggCheckCallContext(fcinfo, &aggcontext))
elog(ERROR, "tsa_rewrite_accum called in non-aggregate context");
- aggcontext = NULL; /* keep compiler quiet */
- }
if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
{
-<!-- $PostgreSQL: pgsql/doc/src/sgml/xaggr.sgml,v 1.38 2009/06/20 18:45:28 tgl Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/xaggr.sgml,v 1.39 2010/02/08 20:39:51 tgl Exp $ -->
<sect1 id="xaggr">
<title>User-Defined Aggregates</title>
<para>
A function written in C can detect that it is being called as an
- aggregate transition or final function by seeing if it was passed
- an <structname>AggState</> or <structname>WindowAggState</> node
- as the function call <quote>context</>,
- for example by:
+ aggregate transition or final function by calling
+ <function>AggCheckCallContext</>, for example:
<programlisting>
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
</programlisting>
One reason for checking this is that when it is true for a transition
function, the first input
* is used to run finalize functions and compute the output tuple;
* this context can be reset once per output tuple.
*
- * Beginning in PostgreSQL 8.1, the executor's AggState node is passed as
- * the fmgr "context" value in all transfunc and finalfunc calls. It is
- * not really intended that the transition functions will look into the
- * AggState node, but they can use code like
- * if (fcinfo->context && IsA(fcinfo->context, AggState))
- * to verify that they are being called by nodeAgg.c and not as ordinary
- * SQL functions. The main reason a transition function might want to know
- * that is that it can avoid palloc'ing a fixed-size pass-by-ref transition
- * value on every call: it can instead just scribble on and return its left
- * input. Ordinarily it is completely forbidden for functions to modify
- * pass-by-ref inputs, but in the aggregate case we know the left input is
- * either the initial transition value or a previous function result, and
- * in either case its value need not be preserved. See int8inc() for an
- * example. Notice that advance_transition_function() is coded to avoid a
- * data copy step when the previous transition value pointer is returned.
- * Also, some transition functions make use of the aggcontext to store
- * working state.
+ * The executor's AggState node is passed as the fmgr "context" value in
+ * all transfunc and finalfunc calls. It is not recommended that the
+ * transition functions look at the AggState node directly, but they can
+ * use AggCheckCallContext() to verify that they are being called by
+ * nodeAgg.c (and not as ordinary SQL functions). The main reason a
+ * transition function might want to know this is so that it can avoid
+ * palloc'ing a fixed-size pass-by-ref transition value on every call:
+ * it can instead just scribble on and return its left input. Ordinarily
+ * it is completely forbidden for functions to modify pass-by-ref inputs,
+ * but in the aggregate case we know the left input is either the initial
+ * transition value or a previous function result, and in either case its
+ * value need not be preserved. See int8inc() for an example. Notice that
+ * advance_transition_function() is coded to avoid a data copy step when
+ * the previous transition value pointer is returned. Also, some
+ * transition functions want to store working state in addition to the
+ * nominal transition value; they can use the memory context returned by
+ * AggCheckCallContext() to do that.
+ *
+ * Note: AggCheckCallContext() is available as of PostgreSQL 9.0. The
+ * AggState is available as context in earlier releases (back to 8.1),
+ * but direct examination of the node is needed to use it before 9.0.
*
*
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.171 2010/01/02 16:57:41 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.172 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
ExecReScan(((PlanState *) node)->lefttree, exprCtxt);
}
+/*
+ * AggCheckCallContext - test if a SQL function is being called as an aggregate
+ *
+ * The transition and/or final functions of an aggregate may want to verify
+ * that they are being called as aggregates, rather than as plain SQL
+ * functions. They should use this function to do so. The return value
+ * is nonzero if being called as an aggregate, or zero if not. (Specific
+ * nonzero values are AGG_CONTEXT_AGGREGATE or AGG_CONTEXT_WINDOW, but more
+ * values could conceivably appear in future.)
+ *
+ * If aggcontext isn't NULL, the function also stores at *aggcontext the
+ * identity of the memory context that aggregate transition values are
+ * being stored in.
+ */
+int
+AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
+{
+ if (fcinfo->context && IsA(fcinfo->context, AggState))
+ {
+ if (aggcontext)
+ *aggcontext = ((AggState *) fcinfo->context)->aggcontext;
+ return AGG_CONTEXT_AGGREGATE;
+ }
+ if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
+ {
+ if (aggcontext)
+ *aggcontext = ((WindowAggState *) fcinfo->context)->wincontext;
+ return AGG_CONTEXT_WINDOW;
+ }
+
+ /* this is just to prevent "uninitialized variable" warnings */
+ if (aggcontext)
+ *aggcontext = NULL;
+ return 0;
+}
+
/*
* aggregate_dummy - dummy execution routine for aggregate functions
*
* Copyright (c) 2003-2010, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.33 2010/01/02 16:57:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.34 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
-#include "nodes/execnodes.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine input data type")));
- if (fcinfo->context && IsA(fcinfo->context, AggState))
- aggcontext = ((AggState *) fcinfo->context)->aggcontext;
- else if (fcinfo->context && IsA(fcinfo->context, WindowAggState))
- aggcontext = ((WindowAggState *) fcinfo->context)->wincontext;
- else
+ if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "array_agg_transfn called in non-aggregate context");
- aggcontext = NULL; /* keep compiler quiet */
}
state = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
PG_RETURN_NULL(); /* returns null iff no input values */
/* cannot be called directly because of internal-type argument */
- Assert(fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)));
+ Assert(AggCheckCallContext(fcinfo, NULL));
state = (ArrayBuildState *) PG_GETARG_POINTER(0);
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.164 2010/01/02 16:57:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.165 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
CHECKFLOATVAL(sumX2, isinf(transvalues[2]) || isinf(newval), true);
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we construct a
* new array with the updated transition data and return it.
*/
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
transvalues[0] = N;
transvalues[1] = sumX;
CHECKFLOATVAL(sumX2, isinf(transvalues[2]) || isinf(newval), true);
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we construct a
* new array with the updated transition data and return it.
*/
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
transvalues[0] = N;
transvalues[1] = sumX;
isinf(newvalY), true);
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we construct a
* new array with the updated transition data and return it.
*/
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
transvalues[0] = N;
transvalues[1] = sumX;
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.77 2010/01/07 04:53:34 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.78 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "funcapi.h"
#include "libpq/pqformat.h"
-#include "nodes/nodes.h"
#include "utils/int8.h"
{
/*
* When int8 is pass-by-reference, we provide this special case to avoid
- * palloc overhead for COUNT(): when called from nodeAgg, we know that the
- * argument is modifiable local storage, so just update it in-place. (If
- * int8 is pass-by-value, then of course this is useless as well as
- * incorrect, so just ifdef it out.)
+ * palloc overhead for COUNT(): when called as an aggregate, we know that
+ * the argument is modifiable local storage, so just update it
+ * in-place. (If int8 is pass-by-value, then of course this is useless as
+ * well as incorrect, so just ifdef it out.)
*/
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
int64 *arg = (int64 *) PG_GETARG_POINTER(0);
int64 result;
else
#endif
{
- /* Not called by nodeAgg, so just do it the dumb way */
+ /* Not called as an aggregate, so just do it the dumb way */
int64 arg = PG_GETARG_INT64(0);
int64 result;
* Copyright (c) 1998-2010, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.121 2010/01/07 04:53:34 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.122 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
}
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to avoid palloc overhead. If not, we need to return
* the new value of the transition variable. (If int8 is pass-by-value,
* then of course this is useless as well as incorrect, so just ifdef it
* out.)
*/
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
}
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to avoid palloc overhead. If not, we need to return
* the new value of the transition variable. (If int8 is pass-by-value,
* then of course this is useless as well as incorrect, so just ifdef it
* out.)
*/
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
{
int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
}
/*
- * Note that we cannot special-case the nodeAgg case here, as we do for
+ * Note that we cannot special-case the aggregate case here, as we do for
* int2_sum and int4_sum: numeric is of variable size, so we cannot modify
* our first parameter in-place.
*/
Int8TransTypeData *transdata;
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we need to make
* a copy of it before scribbling on it.
*/
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
transarray = PG_GETARG_ARRAYTYPE_P(0);
else
transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
Int8TransTypeData *transdata;
/*
- * If we're invoked by nodeAgg, we can cheat and modify our first
+ * If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we need to make
* a copy of it before scribbling on it.
*/
- if (fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)))
+ if (AggCheckCallContext(fcinfo, NULL))
transarray = PG_GETARG_ARRAYTYPE_P(0);
else
transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.175 2010/02/01 03:14:43 itagaki Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.176 2010/02/08 20:39:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "libpq/md5.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
-#include "nodes/execnodes.h"
#include "parser/scansup.h"
#include "regex/regex.h"
#include "utils/builtins.h"
int L,
bool length_not_specified);
static bytea *bytea_overlay(bytea *t1, bytea *t2, int sp, int sl);
-static StringInfo makeStringAggState(fmNodePtr context);
+static StringInfo makeStringAggState(FunctionCallInfo fcinfo);
/*****************************************************************************
* actually used at all, and on subsequent calls the delimiter precedes
* the associated value.
*/
+
+/* subroutine to initialize state */
static StringInfo
-makeStringAggState(fmNodePtr context)
+makeStringAggState(FunctionCallInfo fcinfo)
{
StringInfo state;
MemoryContext aggcontext;
MemoryContext oldcontext;
- if (context && IsA(context, AggState))
- aggcontext = ((AggState *) context)->aggcontext;
- else if (context && IsA(context, WindowAggState))
- aggcontext = ((WindowAggState *) context)->wincontext;
- else
+ if (!AggCheckCallContext(fcinfo, &aggcontext))
{
/* cannot be called directly because of internal-type argument */
elog(ERROR, "string_agg_transfn called in non-aggregate context");
- aggcontext = NULL; /* keep compiler quiet */
}
- /* Create state in aggregate context */
+ /*
+ * Create state in aggregate context. It'll stay there across subsequent
+ * calls.
+ */
oldcontext = MemoryContextSwitchTo(aggcontext);
state = makeStringInfo();
MemoryContextSwitchTo(oldcontext);
state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
- /* Append the element unless not null. */
+ /* Append the element unless null. */
if (!PG_ARGISNULL(1))
{
if (state == NULL)
- state = makeStringAggState(fcinfo->context);
+ state = makeStringAggState(fcinfo);
appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); /* value */
}
state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
- /* Append the value unless not null. */
+ /* Append the value unless null. */
if (!PG_ARGISNULL(1))
{
+ /* On the first time through, we ignore the delimiter. */
if (state == NULL)
- state = makeStringAggState(fcinfo->context);
+ state = makeStringAggState(fcinfo);
else if (!PG_ARGISNULL(2))
appendStringInfoText(state, PG_GETARG_TEXT_PP(2)); /* delimiter */
{
StringInfo state;
- if (PG_ARGISNULL(0))
- PG_RETURN_NULL();
-
/* cannot be called directly because of internal-type argument */
- Assert(fcinfo->context &&
- (IsA(fcinfo->context, AggState) ||
- IsA(fcinfo->context, WindowAggState)));
+ Assert(AggCheckCallContext(fcinfo, NULL));
+
+ state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
- state = (StringInfo) PG_GETARG_POINTER(0);
if (state != NULL)
PG_RETURN_TEXT_P(cstring_to_text(state->data));
else
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/fmgr.h,v 1.63 2010/01/02 16:58:00 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/fmgr.h,v 1.64 2010/02/08 20:39:52 tgl Exp $
*
*-------------------------------------------------------------------------
*/
extern void load_file(const char *filename, bool restricted);
extern void **find_rendezvous_variable(const char *varName);
+/*
+ * Support for aggregate functions
+ *
+ * This is actually in executor/nodeAgg.c, but we declare it here since the
+ * whole point is for callers of it to not be overly friendly with nodeAgg.
+ */
+
+/* AggCheckCallContext can return one of the following codes, or 0: */
+#define AGG_CONTEXT_AGGREGATE 1 /* regular aggregate */
+#define AGG_CONTEXT_WINDOW 2 /* window function */
+
+extern int AggCheckCallContext(FunctionCallInfo fcinfo,
+ MemoryContext *aggcontext);
+
/*
* !!! OLD INTERFACE !!!