create_aggregate.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 202 行
7
202 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE AGGREGATE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE AGGREGATE \- define a new aggregate function.SH SYNOPSIS.sp.nfCREATE AGGREGATE \fIname\fR ( \fIinput_data_type\fR [ , ... ] ) ( SFUNC = \fIsfunc\fR, STYPE = \fIstate_data_type\fR [ , FINALFUNC = \fIffunc\fR ] [ , INITCOND = \fIinitial_condition\fR ] [ , SORTOP = \fIsort_operator\fR ])or the old syntaxCREATE AGGREGATE \fIname\fR ( BASETYPE = \fIbase_type\fR, SFUNC = \fIsfunc\fR, STYPE = \fIstate_data_type\fR [ , FINALFUNC = \fIffunc\fR ] [ , INITCOND = \fIinitial_condition\fR ] [ , SORTOP = \fIsort_operator\fR ]).sp.fi.SH "DESCRIPTION".PP\fBCREATE AGGREGATE\fR defines a new aggregatefunction. Some basic and commonly-used aggregate functions areincluded with the distribution; they are documented in in the documentation. If one defines new types or needsan aggregate function not already provided, then \fBCREATEAGGREGATE\fR can be used to provide the desired features..PPIf a schema name is given (for example, CREATE AGGREGATEmyschema.myagg ...) then the aggregate function is created in thespecified schema. Otherwise it is created in the current schema..PPAn aggregate function is identified by its name and input data type(s).Two aggregates in the same schema can have the same name if they operate ondifferent input types. Thename and input data type(s) of an aggregate must also be distinct fromthe name and input data type(s) of every ordinary function in the sameschema..PPAn aggregate function is made from one or two ordinaryfunctions:a state transition function\fIsfunc\fR,and an optional final calculation function\fIffunc\fR.These are used as follows:.sp.nf\fIsfunc\fR( internal-state, next-data-values ) ---> next-internal-state\fIffunc\fR( internal-state ) ---> aggregate-value.sp.fi.PPPostgreSQL creates a temporary variableof data type \fIstype\fRto hold the current internal state of the aggregate. At each input row,the aggregate argument value(s) are calculated andthe state transition function is invoked with the current state valueand the new argument value(s) to calculate a newinternal state value. After all the rows have been processed,the final function is invoked once to calculate the aggregate's returnvalue. If there is no final function then the ending state valueis returned as-is..PPAn aggregate function may provide an initial condition,that is, an initial value for the internal state value.This is specified and stored in the database as a value of type\fBtext\fR, but it must be a valid external representationof a constant of the state value data type. If it is not suppliedthen the state value starts out null..PPIf the state transition function is declared ``strict'',then it cannot be called with null inputs. With such a transitionfunction, aggregate execution behaves as follows. Rows with any null inputvalues are ignored (the function is not called and the previous state valueis retained). If the initial state value is null, then at the first rowwith all-nonnull input values, the first argument value replaces the statevalue, and the transition function is invoked at subsequent rows withall-nonnull input values.This is handy for implementing aggregates like \fBmax\fR.Note that this behavior is only available when\fIstate_data_type\fRis the same as the first\fIinput_data_type\fR.When these types are different, you must supply a nonnull initialcondition or use a nonstrict transition function..PPIf the state transition function is not strict, then it will be calledunconditionally at each input row, and must deal with null inputsand null transition values for itself. This allows the aggregateauthor to have full control over the aggregate's handling of null values..PPIf the final function is declared ``strict'', then it will notbe called when the ending state value is null; instead a null resultwill be returned automatically. (Of course this is just the normalbehavior of strict functions.) In any case the final function hasthe option of returning a null value. For example, the final function for\fBavg\fR returns null when it sees there were zeroinput rows..PPAggregates that behave like \fBMIN\fR or \fBMAX\fR cansometimes be optimized by looking into an index instead of scanning everyinput row. If this aggregate can be so optimized, indicate it byspecifying a \fIsort operator\fR. The basic requirement is thatthe aggregate must yield the first element in the sort ordering induced bythe operator; in other words.sp.nfSELECT agg(col) FROM tab;.sp.fimust be equivalent to.sp.nfSELECT col FROM tab ORDER BY col USING sortop LIMIT 1;.sp.fiFurther assumptions are that the aggregate ignores null inputs, and thatit delivers a null result if and only if there were no non-null inputs.Ordinarily, a data type's < operator is the proper sortoperator for \fBMIN\fR, and > is the proper sortoperator for \fBMAX\fR. Note that the optimization will neveractually take effect unless the specified operator is the ``lessthan'' or ``greater than'' strategy member of a B-treeindex operator class..SH "PARAMETERS".TP\fB\fIname\fB\fRThe name (optionally schema-qualified) of the aggregate functionto create..TP\fB\fIinput_data_type\fB\fRAn input data type on which this aggregate function operates.To create a zero-argument aggregate function, write *in place of the list of input data types. (An example of such anaggregate is \fBcount(*)\fR.).TP\fB\fIbase_type\fB\fRIn the old syntax for \fBCREATE AGGREGATE\fR, the input data typeis specified by a basetype parameter rather than beingwritten next to the aggregate name. Note that this syntax allowsonly one input parameter. To define a zero-argument aggregate function,specify the basetype as"ANY" (not *)..TP\fB\fIsfunc\fB\fRThe name of the state transition function to be called for eachinput row. For an \fIN\fR-argumentaggregate function, the \fIsfunc\fRmust take \fIN\fR+1 arguments,the first being of type \fIstate_data_type\fR and the restmatching the declared input data type(s) of the aggregate.The function must return a value of type \fIstate_data_type\fR. This functiontakes the current state value and the current input data value(s),and returns the next state value..TP\fB\fIstate_data_type\fB\fRThe data type for the aggregate's state value..TP\fB\fIffunc\fB\fRThe name of the final function called to compute the aggregate'sresult after all input rows have been traversed. The functionmust take a single argument of type \fIstate_data_type\fR. The returndata type of the aggregate is defined as the return type of thisfunction. If \fIffunc\fRis not specified, then the ending state value is used as theaggregate's result, and the return type is \fIstate_data_type\fR..TP\fB\fIinitial_condition\fB\fRThe initial setting for the state value. This must be a stringconstant in the form accepted for the data type \fIstate_data_type\fR. If notspecified, the state value starts out null..TP\fB\fIsort_operator\fB\fRThe associated sort operator for a \fBMIN\fR- or\fBMAX\fR-like aggregate.This is just an operator name (possibly schema-qualified).The operator is assumed to have the same input data types asthe aggregate (which must be a single-argument aggregate)..PPThe parameters of \fBCREATE AGGREGATE\fR can bewritten in any order, not just the order illustrated above..PP.SH "EXAMPLES".PPSee in the documentation..SH "COMPATIBILITY".PP\fBCREATE AGGREGATE\fR is aPostgreSQL language extension. The SQLstandard does not provide for user-defined aggregate functions..SH "SEE ALSO"ALTER AGGREGATE [\fBalter_aggregate\fR(7)], DROP AGGREGATE [\fBdrop_aggregate\fR(l)]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?