aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/nodeAgg.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2005-03-12 20:25:06 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2005-03-12 20:25:06 +0000
commitfa5e44017a8cea141d1730e695c5cc2051158114 (patch)
treedaafe69293544a73d64701bd38a629d32d0727fc /src/backend/executor/nodeAgg.c
parentde004e44e2cb4c3def05ba3d9f1a6f23e802bea3 (diff)
downloadpostgresql-fa5e44017a8cea141d1730e695c5cc2051158114.tar.gz
postgresql-fa5e44017a8cea141d1730e695c5cc2051158114.zip
Adjust the API for aggregate function calls so that a C-coded function
can tell whether it is being used as an aggregate or not. This allows such a function to avoid re-pallocing a pass-by-reference transition value; normally it would be unsafe for a function to scribble on an input, but in the aggregate case it's safe to reuse the old transition value. Make int8inc() do this. This gets a useful improvement in the speed of COUNT(*), at least on narrow tables (it seems to be swamped by I/O when the table rows are wide). Per a discussion in early December with Neil Conway. I also fixed int_aggregate.c to check this, thereby turning it into something approaching a supportable technique instead of being a crude hack.
Diffstat (limited to 'src/backend/executor/nodeAgg.c')
-rw-r--r--src/backend/executor/nodeAgg.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 63a687e9869..8f641cc843b 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -40,12 +40,28 @@
* 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.
+ *
*
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.128 2005/01/28 19:33:56 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.129 2005/03/12 20:25:06 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -363,7 +379,7 @@ advance_transition_function(AggState *aggstate,
*/
/* MemSet(&fcinfo, 0, sizeof(fcinfo)); */
- fcinfo.context = NULL;
+ fcinfo.context = (void *) aggstate;
fcinfo.resultinfo = NULL;
fcinfo.isnull = false;
@@ -541,6 +557,7 @@ finalize_aggregate(AggState *aggstate,
FunctionCallInfoData fcinfo;
MemSet(&fcinfo, 0, sizeof(fcinfo));
+ fcinfo.context = (void *) aggstate;
fcinfo.flinfo = &peraggstate->finalfn;
fcinfo.nargs = 1;
fcinfo.arg[0] = pergroupstate->transValue;