diff --git a/doc/src/sgml/xaggr.sgml b/doc/src/sgml/xaggr.sgml index ef7cff4..1c67e97 100644 --- a/doc/src/sgml/xaggr.sgml +++ b/doc/src/sgml/xaggr.sgml @@ -511,6 +511,82 @@ SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY income) FROM households; + + Partial Aggregates + + + aggregate function + partial aggregates + + + + An aggregate function may also optionally support partial mode which will + allow the aggregate to participate in optimizations such as parallel + aggregation. Supporting aggregate must have a combinefn + set. This will allow PostgreSQL to combine + multiple separately aggregated states into a single state allowing any + finalfn to be called upon the resulting states. + + + + The combinefn must take two stype arguments and + return another stype value which is each of the input values + "added" together. For aggregates such as avg the + stype would track both the sum of the input values and the count + of the non-null input values. In this case the combinefn would + simply add both sums together and also add both non-null counts together and + return the result. + + + + For combinefn written in C the resulting aggregate state must + be allocated in the aggregate function's memory context. When one of the + input states is NULL it's invalid to simply return the other + non-null state as these input states will be allocated in the incorrect + memory context. + + + + For aggregates with an INTERNAL stype, some usages of + partial aggregation require that the aggregate function also have a + serialfn and deserialfn. This is the case for + parallel aggregation, as the resulting states must be transferred from the + parallel worker process into the main process, and this is simply not + possible to do just by passing the memory address of the + INTERNAL state. Instead these states must be serialized into a + serialtype by the serialfn to allow the state to be + copied between the processes. + + + + A serialfn should take a single INTERNAL argument + and return a value of serialtype. It's this value that the + deserialfn must accept to perform the reverse operation and + produce the equivalent INTERNAL state as the one which was + passed to the serialfn. The deserialfn must take + two parameters, the first of which must be of type serialtype + and the second must be of type INTERNAL. The + INTERNAL value is a "dummy" value and serves no purpose other + than to get around the fact that PostgreSQL + disallows the creation of functions which return an INTERNAL + type but do not take any INTERNAL arguments. This value will + always be passed to the deserialfn as NULL. The + deserialfn must return a value of type INTERNAL. + + + +CREATE AGGREGATE myavg (numeric) +( + stype = internal, + sfunc = numeric_avg_accum, + finalfunc = numeric_avg, + serialtype = bytea, + serialfunc = numeric_avg_serialize, + deserialfunc = numeric_avg_deserialize, + combinefunc = numeric_avg_combine +); + + Support Functions for Aggregates