gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

512 行
38KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE AGGREGATE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-create-access-method.html" title="CREATE ACCESS METHOD" /><link rel="next" href="sql-createcast.html" title="CREATE CAST" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE AGGREGATE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-create-access-method.html" title="CREATE ACCESS METHOD">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createcast.html" title="CREATE CAST">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEAGGREGATE"><div class="titlepage"></div><a id="id-1.9.3.57.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE AGGREGATE</span></h2><p>CREATE AGGREGATE — define a new aggregate function</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ OR REPLACE ] AGGREGATE <em class="replaceable"><code>name</code></em> ( [ <em class="replaceable"><code>argmode</code></em> ] [ <em class="replaceable"><code>argname</code></em> ] <em class="replaceable"><code>arg_data_type</code></em> [ , ... ] ) (
  4. SFUNC = <em class="replaceable"><code>sfunc</code></em>,
  5. STYPE = <em class="replaceable"><code>state_data_type</code></em>
  6. [ , SSPACE = <em class="replaceable"><code>state_data_size</code></em> ]
  7. [ , FINALFUNC = <em class="replaceable"><code>ffunc</code></em> ]
  8. [ , FINALFUNC_EXTRA ]
  9. [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
  10. [ , COMBINEFUNC = <em class="replaceable"><code>combinefunc</code></em> ]
  11. [ , SERIALFUNC = <em class="replaceable"><code>serialfunc</code></em> ]
  12. [ , DESERIALFUNC = <em class="replaceable"><code>deserialfunc</code></em> ]
  13. [ , INITCOND = <em class="replaceable"><code>initial_condition</code></em> ]
  14. [ , MSFUNC = <em class="replaceable"><code>msfunc</code></em> ]
  15. [ , MINVFUNC = <em class="replaceable"><code>minvfunc</code></em> ]
  16. [ , MSTYPE = <em class="replaceable"><code>mstate_data_type</code></em> ]
  17. [ , MSSPACE = <em class="replaceable"><code>mstate_data_size</code></em> ]
  18. [ , MFINALFUNC = <em class="replaceable"><code>mffunc</code></em> ]
  19. [ , MFINALFUNC_EXTRA ]
  20. [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
  21. [ , MINITCOND = <em class="replaceable"><code>minitial_condition</code></em> ]
  22. [ , SORTOP = <em class="replaceable"><code>sort_operator</code></em> ]
  23. [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
  24. )
  25. CREATE [ OR REPLACE ] AGGREGATE <em class="replaceable"><code>name</code></em> ( [ [ <em class="replaceable"><code>argmode</code></em> ] [ <em class="replaceable"><code>argname</code></em> ] <em class="replaceable"><code>arg_data_type</code></em> [ , ... ] ]
  26. ORDER BY [ <em class="replaceable"><code>argmode</code></em> ] [ <em class="replaceable"><code>argname</code></em> ] <em class="replaceable"><code>arg_data_type</code></em> [ , ... ] ) (
  27. SFUNC = <em class="replaceable"><code>sfunc</code></em>,
  28. STYPE = <em class="replaceable"><code>state_data_type</code></em>
  29. [ , SSPACE = <em class="replaceable"><code>state_data_size</code></em> ]
  30. [ , FINALFUNC = <em class="replaceable"><code>ffunc</code></em> ]
  31. [ , FINALFUNC_EXTRA ]
  32. [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
  33. [ , INITCOND = <em class="replaceable"><code>initial_condition</code></em> ]
  34. [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
  35. [ , HYPOTHETICAL ]
  36. )
  37. <span class="phrase">or the old syntax</span>
  38. CREATE [ OR REPLACE ] AGGREGATE <em class="replaceable"><code>name</code></em> (
  39. BASETYPE = <em class="replaceable"><code>base_type</code></em>,
  40. SFUNC = <em class="replaceable"><code>sfunc</code></em>,
  41. STYPE = <em class="replaceable"><code>state_data_type</code></em>
  42. [ , SSPACE = <em class="replaceable"><code>state_data_size</code></em> ]
  43. [ , FINALFUNC = <em class="replaceable"><code>ffunc</code></em> ]
  44. [ , FINALFUNC_EXTRA ]
  45. [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
  46. [ , COMBINEFUNC = <em class="replaceable"><code>combinefunc</code></em> ]
  47. [ , SERIALFUNC = <em class="replaceable"><code>serialfunc</code></em> ]
  48. [ , DESERIALFUNC = <em class="replaceable"><code>deserialfunc</code></em> ]
  49. [ , INITCOND = <em class="replaceable"><code>initial_condition</code></em> ]
  50. [ , MSFUNC = <em class="replaceable"><code>msfunc</code></em> ]
  51. [ , MINVFUNC = <em class="replaceable"><code>minvfunc</code></em> ]
  52. [ , MSTYPE = <em class="replaceable"><code>mstate_data_type</code></em> ]
  53. [ , MSSPACE = <em class="replaceable"><code>mstate_data_size</code></em> ]
  54. [ , MFINALFUNC = <em class="replaceable"><code>mffunc</code></em> ]
  55. [ , MFINALFUNC_EXTRA ]
  56. [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
  57. [ , MINITCOND = <em class="replaceable"><code>minitial_condition</code></em> ]
  58. [ , SORTOP = <em class="replaceable"><code>sort_operator</code></em> ]
  59. )
  60. </pre></div><div class="refsect1" id="id-1.9.3.57.5"><h2>Description</h2><p>
  61. <code class="command">CREATE AGGREGATE</code> defines a new aggregate function.
  62. <code class="command">CREATE OR REPLACE AGGREGATE</code> will either define a new
  63. aggregate function or replace an existing definition. Some basic and
  64. commonly-used aggregate functions are included with the distribution; they
  65. are documented in <a class="xref" href="functions-aggregate.html" title="9.20. Aggregate Functions">Section 9.20</a>. If one defines new
  66. types or needs an aggregate function not already provided, then
  67. <code class="command">CREATE AGGREGATE</code> can be used to provide the desired
  68. features.
  69. </p><p>
  70. When replacing an existing definition, the argument types, result type,
  71. and number of direct arguments may not be changed. Also, the new definition
  72. must be of the same kind (ordinary aggregate, ordered-set aggregate, or
  73. hypothetical-set aggregate) as the old one.
  74. </p><p>
  75. If a schema name is given (for example, <code class="literal">CREATE AGGREGATE
  76. myschema.myagg ...</code>) then the aggregate function is created in the
  77. specified schema. Otherwise it is created in the current schema.
  78. </p><p>
  79. An aggregate function is identified by its name and input data type(s).
  80. Two aggregates in the same schema can have the same name if they operate on
  81. different input types. The
  82. name and input data type(s) of an aggregate must also be distinct from
  83. the name and input data type(s) of every ordinary function in the same
  84. schema.
  85. This behavior is identical to overloading of ordinary function names
  86. (see <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>).
  87. </p><p>
  88. A simple aggregate function is made from one or two ordinary
  89. functions:
  90. a state transition function
  91. <em class="replaceable"><code>sfunc</code></em>,
  92. and an optional final calculation function
  93. <em class="replaceable"><code>ffunc</code></em>.
  94. These are used as follows:
  95. </p><pre class="programlisting">
  96. <em class="replaceable"><code>sfunc</code></em>( internal-state, next-data-values ) ---&gt; next-internal-state
  97. <em class="replaceable"><code>ffunc</code></em>( internal-state ) ---&gt; aggregate-value
  98. </pre><p>
  99. </p><p>
  100. <span class="productname">PostgreSQL</span> creates a temporary variable
  101. of data type <em class="replaceable"><code>stype</code></em>
  102. to hold the current internal state of the aggregate. At each input row,
  103. the aggregate argument value(s) are calculated and
  104. the state transition function is invoked with the current state value
  105. and the new argument value(s) to calculate a new
  106. internal state value. After all the rows have been processed,
  107. the final function is invoked once to calculate the aggregate's return
  108. value. If there is no final function then the ending state value
  109. is returned as-is.
  110. </p><p>
  111. An aggregate function can provide an initial condition,
  112. that is, an initial value for the internal state value.
  113. This is specified and stored in the database as a value of type
  114. <code class="type">text</code>, but it must be a valid external representation
  115. of a constant of the state value data type. If it is not supplied
  116. then the state value starts out null.
  117. </p><p>
  118. If the state transition function is declared <span class="quote">“<span class="quote">strict</span>”</span>,
  119. then it cannot be called with null inputs. With such a transition
  120. function, aggregate execution behaves as follows. Rows with any null input
  121. values are ignored (the function is not called and the previous state value
  122. is retained). If the initial state value is null, then at the first row
  123. with all-nonnull input values, the first argument value replaces the state
  124. value, and the transition function is invoked at each subsequent row with
  125. all-nonnull input values.
  126. This is handy for implementing aggregates like <code class="function">max</code>.
  127. Note that this behavior is only available when
  128. <em class="replaceable"><code>state_data_type</code></em>
  129. is the same as the first
  130. <em class="replaceable"><code>arg_data_type</code></em>.
  131. When these types are different, you must supply a nonnull initial
  132. condition or use a nonstrict transition function.
  133. </p><p>
  134. If the state transition function is not strict, then it will be called
  135. unconditionally at each input row, and must deal with null inputs
  136. and null state values for itself. This allows the aggregate
  137. author to have full control over the aggregate's handling of null values.
  138. </p><p>
  139. If the final function is declared <span class="quote">“<span class="quote">strict</span>”</span>, then it will not
  140. be called when the ending state value is null; instead a null result
  141. will be returned automatically. (Of course this is just the normal
  142. behavior of strict functions.) In any case the final function has
  143. the option of returning a null value. For example, the final function for
  144. <code class="function">avg</code> returns null when it sees there were zero
  145. input rows.
  146. </p><p>
  147. Sometimes it is useful to declare the final function as taking not just
  148. the state value, but extra parameters corresponding to the aggregate's
  149. input values. The main reason for doing this is if the final function
  150. is polymorphic and the state value's data type would be inadequate to
  151. pin down the result type. These extra parameters are always passed as
  152. NULL (and so the final function must not be strict when
  153. the <code class="literal">FINALFUNC_EXTRA</code> option is used), but nonetheless they
  154. are valid parameters. The final function could for example make use
  155. of <code class="function">get_fn_expr_argtype</code> to identify the actual argument type
  156. in the current call.
  157. </p><p>
  158. An aggregate can optionally support <em class="firstterm">moving-aggregate mode</em>,
  159. as described in <a class="xref" href="xaggr.html#XAGGR-MOVING-AGGREGATES" title="37.12.1. Moving-Aggregate Mode">Section 37.12.1</a>. This requires
  160. specifying the <code class="literal">MSFUNC</code>, <code class="literal">MINVFUNC</code>,
  161. and <code class="literal">MSTYPE</code> parameters, and optionally
  162. the <code class="literal">MSSPACE</code>, <code class="literal">MFINALFUNC</code>,
  163. <code class="literal">MFINALFUNC_EXTRA</code>, <code class="literal">MFINALFUNC_MODIFY</code>,
  164. and <code class="literal">MINITCOND</code> parameters. Except for <code class="literal">MINVFUNC</code>,
  165. these parameters work like the corresponding simple-aggregate parameters
  166. without <code class="literal">M</code>; they define a separate implementation of the
  167. aggregate that includes an inverse transition function.
  168. </p><p>
  169. The syntax with <code class="literal">ORDER BY</code> in the parameter list creates
  170. a special type of aggregate called an <em class="firstterm">ordered-set
  171. aggregate</em>; or if <code class="literal">HYPOTHETICAL</code> is specified, then
  172. a <em class="firstterm">hypothetical-set aggregate</em> is created. These
  173. aggregates operate over groups of sorted values in order-dependent ways,
  174. so that specification of an input sort order is an essential part of a
  175. call. Also, they can have <em class="firstterm">direct</em> arguments, which are
  176. arguments that are evaluated only once per aggregation rather than once
  177. per input row. Hypothetical-set aggregates are a subclass of ordered-set
  178. aggregates in which some of the direct arguments are required to match,
  179. in number and data types, the aggregated argument columns. This allows
  180. the values of those direct arguments to be added to the collection of
  181. aggregate-input rows as an additional <span class="quote">“<span class="quote">hypothetical</span>”</span> row.
  182. </p><p>
  183. An aggregate can optionally support <em class="firstterm">partial aggregation</em>,
  184. as described in <a class="xref" href="xaggr.html#XAGGR-PARTIAL-AGGREGATES" title="37.12.4. Partial Aggregation">Section 37.12.4</a>.
  185. This requires specifying the <code class="literal">COMBINEFUNC</code> parameter.
  186. If the <em class="replaceable"><code>state_data_type</code></em>
  187. is <code class="type">internal</code>, it's usually also appropriate to provide the
  188. <code class="literal">SERIALFUNC</code> and <code class="literal">DESERIALFUNC</code> parameters so that
  189. parallel aggregation is possible. Note that the aggregate must also be
  190. marked <code class="literal">PARALLEL SAFE</code> to enable parallel aggregation.
  191. </p><p>
  192. Aggregates that behave like <code class="function">MIN</code> or <code class="function">MAX</code> can
  193. sometimes be optimized by looking into an index instead of scanning every
  194. input row. If this aggregate can be so optimized, indicate it by
  195. specifying a <em class="firstterm">sort operator</em>. The basic requirement is that
  196. the aggregate must yield the first element in the sort ordering induced by
  197. the operator; in other words:
  198. </p><pre class="programlisting">
  199. SELECT agg(col) FROM tab;
  200. </pre><p>
  201. must be equivalent to:
  202. </p><pre class="programlisting">
  203. SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
  204. </pre><p>
  205. Further assumptions are that the aggregate ignores null inputs, and that
  206. it delivers a null result if and only if there were no non-null inputs.
  207. Ordinarily, a data type's <code class="literal">&lt;</code> operator is the proper sort
  208. operator for <code class="function">MIN</code>, and <code class="literal">&gt;</code> is the proper sort
  209. operator for <code class="function">MAX</code>. Note that the optimization will never
  210. actually take effect unless the specified operator is the <span class="quote">“<span class="quote">less
  211. than</span>”</span> or <span class="quote">“<span class="quote">greater than</span>”</span> strategy member of a B-tree
  212. index operator class.
  213. </p><p>
  214. To be able to create an aggregate function, you must
  215. have <code class="literal">USAGE</code> privilege on the argument types, the state
  216. type(s), and the return type, as well as <code class="literal">EXECUTE</code>
  217. privilege on the supporting functions.
  218. </p></div><div class="refsect1" id="id-1.9.3.57.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  219. The name (optionally schema-qualified) of the aggregate function
  220. to create.
  221. </p></dd><dt><span class="term"><em class="replaceable"><code>argmode</code></em></span></dt><dd><p>
  222. The mode of an argument: <code class="literal">IN</code> or <code class="literal">VARIADIC</code>.
  223. (Aggregate functions do not support <code class="literal">OUT</code> arguments.)
  224. If omitted, the default is <code class="literal">IN</code>. Only the last argument
  225. can be marked <code class="literal">VARIADIC</code>.
  226. </p></dd><dt><span class="term"><em class="replaceable"><code>argname</code></em></span></dt><dd><p>
  227. The name of an argument. This is currently only useful for
  228. documentation purposes. If omitted, the argument has no name.
  229. </p></dd><dt><span class="term"><em class="replaceable"><code>arg_data_type</code></em></span></dt><dd><p>
  230. An input data type on which this aggregate function operates.
  231. To create a zero-argument aggregate function, write <code class="literal">*</code>
  232. in place of the list of argument specifications. (An example of such an
  233. aggregate is <code class="function">count(*)</code>.)
  234. </p></dd><dt><span class="term"><em class="replaceable"><code>base_type</code></em></span></dt><dd><p>
  235. In the old syntax for <code class="command">CREATE AGGREGATE</code>, the input data type
  236. is specified by a <code class="literal">basetype</code> parameter rather than being
  237. written next to the aggregate name. Note that this syntax allows
  238. only one input parameter. To define a zero-argument aggregate function
  239. with this syntax, specify the <code class="literal">basetype</code> as
  240. <code class="literal">"ANY"</code> (not <code class="literal">*</code>).
  241. Ordered-set aggregates cannot be defined with the old syntax.
  242. </p></dd><dt><span class="term"><em class="replaceable"><code>sfunc</code></em></span></dt><dd><p>
  243. The name of the state transition function to be called for each
  244. input row. For a normal <em class="replaceable"><code>N</code></em>-argument
  245. aggregate function, the <em class="replaceable"><code>sfunc</code></em>
  246. must take <em class="replaceable"><code>N</code></em>+1 arguments,
  247. the first being of type <em class="replaceable"><code>state_data_type</code></em> and the rest
  248. matching the declared input data type(s) of the aggregate.
  249. The function must return a value of type <em class="replaceable"><code>state_data_type</code></em>. This function
  250. takes the current state value and the current input data value(s),
  251. and returns the next state value.
  252. </p><p>
  253. For ordered-set (including hypothetical-set) aggregates, the state
  254. transition function receives only the current state value and the
  255. aggregated arguments, not the direct arguments. Otherwise it is the
  256. same.
  257. </p></dd><dt><span class="term"><em class="replaceable"><code>state_data_type</code></em></span></dt><dd><p>
  258. The data type for the aggregate's state value.
  259. </p></dd><dt><span class="term"><em class="replaceable"><code>state_data_size</code></em></span></dt><dd><p>
  260. The approximate average size (in bytes) of the aggregate's state value.
  261. If this parameter is omitted or is zero, a default estimate is used
  262. based on the <em class="replaceable"><code>state_data_type</code></em>.
  263. The planner uses this value to estimate the memory required for a
  264. grouped aggregate query. The planner will consider using hash
  265. aggregation for such a query only if the hash table is estimated to fit
  266. in <a class="xref" href="runtime-config-resource.html#GUC-WORK-MEM">work_mem</a>; therefore, large values of this
  267. parameter discourage use of hash aggregation.
  268. </p></dd><dt><span class="term"><em class="replaceable"><code>ffunc</code></em></span></dt><dd><p>
  269. The name of the final function called to compute the aggregate's
  270. result after all input rows have been traversed.
  271. For a normal aggregate, this function
  272. must take a single argument of type <em class="replaceable"><code>state_data_type</code></em>. The return
  273. data type of the aggregate is defined as the return type of this
  274. function. If <em class="replaceable"><code>ffunc</code></em>
  275. is not specified, then the ending state value is used as the
  276. aggregate's result, and the return type is <em class="replaceable"><code>state_data_type</code></em>.
  277. </p><p>
  278. For ordered-set (including hypothetical-set) aggregates, the
  279. final function receives not only the final state value,
  280. but also the values of all the direct arguments.
  281. </p><p>
  282. If <code class="literal">FINALFUNC_EXTRA</code> is specified, then in addition to the
  283. final state value and any direct arguments, the final function
  284. receives extra NULL values corresponding to the aggregate's regular
  285. (aggregated) arguments. This is mainly useful to allow correct
  286. resolution of the aggregate result type when a polymorphic aggregate
  287. is being defined.
  288. </p></dd><dt><span class="term"><code class="literal">FINALFUNC_MODIFY</code> = { <code class="literal">READ_ONLY</code> | <code class="literal">SHAREABLE</code> | <code class="literal">READ_WRITE</code> }</span></dt><dd><p>
  289. This option specifies whether the final function is a pure function
  290. that does not modify its arguments. <code class="literal">READ_ONLY</code> indicates
  291. it does not; the other two values indicate that it may change the
  292. transition state value. See <a class="xref" href="sql-createaggregate.html#SQL-CREATEAGGREGATE-NOTES" title="Notes">Notes</a> below for more detail. The
  293. default is <code class="literal">READ_ONLY</code>, except for ordered-set aggregates,
  294. for which the default is <code class="literal">READ_WRITE</code>.
  295. </p></dd><dt><span class="term"><em class="replaceable"><code>combinefunc</code></em></span></dt><dd><p>
  296. The <em class="replaceable"><code>combinefunc</code></em> function
  297. may optionally be specified to allow the aggregate function to support
  298. partial aggregation. If provided,
  299. the <em class="replaceable"><code>combinefunc</code></em> must
  300. combine two <em class="replaceable"><code>state_data_type</code></em>
  301. values, each containing the result of aggregation over some subset of
  302. the input values, to produce a
  303. new <em class="replaceable"><code>state_data_type</code></em> that
  304. represents the result of aggregating over both sets of inputs. This
  305. function can be thought of as
  306. an <em class="replaceable"><code>sfunc</code></em>, where instead of
  307. acting upon an individual input row and adding it to the running
  308. aggregate state, it adds another aggregate state to the running state.
  309. </p><p>
  310. The <em class="replaceable"><code>combinefunc</code></em> must be
  311. declared as taking two arguments of
  312. the <em class="replaceable"><code>state_data_type</code></em> and
  313. returning a value of
  314. the <em class="replaceable"><code>state_data_type</code></em>.
  315. Optionally this function may be <span class="quote">“<span class="quote">strict</span>”</span>. In this case the
  316. function will not be called when either of the input states are null;
  317. the other state will be taken as the correct result.
  318. </p><p>
  319. For aggregate functions
  320. whose <em class="replaceable"><code>state_data_type</code></em>
  321. is <code class="type">internal</code>,
  322. the <em class="replaceable"><code>combinefunc</code></em> must not
  323. be strict. In this case
  324. the <em class="replaceable"><code>combinefunc</code></em> must
  325. ensure that null states are handled correctly and that the state being
  326. returned is properly stored in the aggregate memory context.
  327. </p></dd><dt><span class="term"><em class="replaceable"><code>serialfunc</code></em></span></dt><dd><p>
  328. An aggregate function
  329. whose <em class="replaceable"><code>state_data_type</code></em>
  330. is <code class="type">internal</code> can participate in parallel aggregation only if it
  331. has a <em class="replaceable"><code>serialfunc</code></em> function,
  332. which must serialize the aggregate state into a <code class="type">bytea</code> value for
  333. transmission to another process. This function must take a single
  334. argument of type <code class="type">internal</code> and return type <code class="type">bytea</code>. A
  335. corresponding <em class="replaceable"><code>deserialfunc</code></em>
  336. is also required.
  337. </p></dd><dt><span class="term"><em class="replaceable"><code>deserialfunc</code></em></span></dt><dd><p>
  338. Deserialize a previously serialized aggregate state back into
  339. <em class="replaceable"><code>state_data_type</code></em>. This
  340. function must take two arguments of types <code class="type">bytea</code>
  341. and <code class="type">internal</code>, and produce a result of type <code class="type">internal</code>.
  342. (Note: the second, <code class="type">internal</code> argument is unused, but is required
  343. for type safety reasons.)
  344. </p></dd><dt><span class="term"><em class="replaceable"><code>initial_condition</code></em></span></dt><dd><p>
  345. The initial setting for the state value. This must be a string
  346. constant in the form accepted for the data type <em class="replaceable"><code>state_data_type</code></em>. If not
  347. specified, the state value starts out null.
  348. </p></dd><dt><span class="term"><em class="replaceable"><code>msfunc</code></em></span></dt><dd><p>
  349. The name of the forward state transition function to be called for each
  350. input row in moving-aggregate mode. This is exactly like the regular
  351. transition function, except that its first argument and result are of
  352. type <em class="replaceable"><code>mstate_data_type</code></em>, which might be different
  353. from <em class="replaceable"><code>state_data_type</code></em>.
  354. </p></dd><dt><span class="term"><em class="replaceable"><code>minvfunc</code></em></span></dt><dd><p>
  355. The name of the inverse state transition function to be used in
  356. moving-aggregate mode. This function has the same argument and
  357. result types as <em class="replaceable"><code>msfunc</code></em>, but it is used to remove
  358. a value from the current aggregate state, rather than add a value to
  359. it. The inverse transition function must have the same strictness
  360. attribute as the forward state transition function.
  361. </p></dd><dt><span class="term"><em class="replaceable"><code>mstate_data_type</code></em></span></dt><dd><p>
  362. The data type for the aggregate's state value, when using
  363. moving-aggregate mode.
  364. </p></dd><dt><span class="term"><em class="replaceable"><code>mstate_data_size</code></em></span></dt><dd><p>
  365. The approximate average size (in bytes) of the aggregate's state
  366. value, when using moving-aggregate mode. This works the same as
  367. <em class="replaceable"><code>state_data_size</code></em>.
  368. </p></dd><dt><span class="term"><em class="replaceable"><code>mffunc</code></em></span></dt><dd><p>
  369. The name of the final function called to compute the aggregate's
  370. result after all input rows have been traversed, when using
  371. moving-aggregate mode. This works the same as <em class="replaceable"><code>ffunc</code></em>,
  372. except that its first argument's type
  373. is <em class="replaceable"><code>mstate_data_type</code></em> and extra dummy arguments are
  374. specified by writing <code class="literal">MFINALFUNC_EXTRA</code>.
  375. The aggregate result type determined by <em class="replaceable"><code>mffunc</code></em>
  376. or <em class="replaceable"><code>mstate_data_type</code></em> must match that determined by the
  377. aggregate's regular implementation.
  378. </p></dd><dt><span class="term"><code class="literal">MFINALFUNC_MODIFY</code> = { <code class="literal">READ_ONLY</code> | <code class="literal">SHAREABLE</code> | <code class="literal">READ_WRITE</code> }</span></dt><dd><p>
  379. This option is like <code class="literal">FINALFUNC_MODIFY</code>, but it describes
  380. the behavior of the moving-aggregate final function.
  381. </p></dd><dt><span class="term"><em class="replaceable"><code>minitial_condition</code></em></span></dt><dd><p>
  382. The initial setting for the state value, when using moving-aggregate
  383. mode. This works the same as <em class="replaceable"><code>initial_condition</code></em>.
  384. </p></dd><dt><span class="term"><em class="replaceable"><code>sort_operator</code></em></span></dt><dd><p>
  385. The associated sort operator for a <code class="function">MIN</code>- or
  386. <code class="function">MAX</code>-like aggregate.
  387. This is just an operator name (possibly schema-qualified).
  388. The operator is assumed to have the same input data types as
  389. the aggregate (which must be a single-argument normal aggregate).
  390. </p></dd><dt><span class="term"><code class="literal">PARALLEL =</code> { <code class="literal">SAFE</code> | <code class="literal">RESTRICTED</code> | <code class="literal">UNSAFE</code> }</span></dt><dd><p>
  391. The meanings of <code class="literal">PARALLEL SAFE</code>, <code class="literal">PARALLEL
  392. RESTRICTED</code>, and <code class="literal">PARALLEL UNSAFE</code> are the same as
  393. in <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>. An aggregate will not be
  394. considered for parallelization if it is marked <code class="literal">PARALLEL
  395. UNSAFE</code> (which is the default!) or <code class="literal">PARALLEL RESTRICTED</code>.
  396. Note that the parallel-safety markings of the aggregate's support
  397. functions are not consulted by the planner, only the marking of the
  398. aggregate itself.
  399. </p></dd><dt><span class="term"><code class="literal">HYPOTHETICAL</code></span></dt><dd><p>
  400. For ordered-set aggregates only, this flag specifies that the aggregate
  401. arguments are to be processed according to the requirements for
  402. hypothetical-set aggregates: that is, the last few direct arguments must
  403. match the data types of the aggregated (<code class="literal">WITHIN GROUP</code>)
  404. arguments. The <code class="literal">HYPOTHETICAL</code> flag has no effect on
  405. run-time behavior, only on parse-time resolution of the data types and
  406. collations of the aggregate's arguments.
  407. </p></dd></dl></div><p>
  408. The parameters of <code class="command">CREATE AGGREGATE</code> can be
  409. written in any order, not just the order illustrated above.
  410. </p></div><div class="refsect1" id="SQL-CREATEAGGREGATE-NOTES"><h2>Notes</h2><p>
  411. In parameters that specify support function names, you can write
  412. a schema name if needed, for example <code class="literal">SFUNC = public.sum</code>.
  413. Do not write argument types there, however — the argument types
  414. of the support functions are determined from other parameters.
  415. </p><p>
  416. Ordinarily, PostgreSQL functions are expected to be true functions that
  417. do not modify their input values. However, an aggregate transition
  418. function, <span class="emphasis"><em>when used in the context of an aggregate</em></span>,
  419. is allowed to cheat and modify its transition-state argument in place.
  420. This can provide substantial performance benefits compared to making
  421. a fresh copy of the transition state each time.
  422. </p><p>
  423. Likewise, while an aggregate final function is normally expected not to
  424. modify its input values, sometimes it is impractical to avoid modifying
  425. the transition-state argument. Such behavior must be declared using
  426. the <code class="literal">FINALFUNC_MODIFY</code> parameter.
  427. The <code class="literal">READ_WRITE</code>
  428. value indicates that the final function modifies the transition state in
  429. unspecified ways. This value prevents use of the aggregate as a window
  430. function, and it also prevents merging of transition states for aggregate
  431. calls that share the same input values and transition functions.
  432. The <code class="literal">SHAREABLE</code> value indicates that the transition function
  433. cannot be applied after the final function, but multiple final-function
  434. calls can be performed on the ending transition state value. This value
  435. prevents use of the aggregate as a window function, but it allows merging
  436. of transition states. (That is, the optimization of interest here is not
  437. applying the same final function repeatedly, but applying different final
  438. functions to the same ending transition state value. This is allowed as
  439. long as none of the final functions are marked <code class="literal">READ_WRITE</code>.)
  440. </p><p>
  441. If an aggregate supports moving-aggregate mode, it will improve
  442. calculation efficiency when the aggregate is used as a window function
  443. for a window with moving frame start (that is, a frame start mode other
  444. than <code class="literal">UNBOUNDED PRECEDING</code>). Conceptually, the forward
  445. transition function adds input values to the aggregate's state when
  446. they enter the window frame from the bottom, and the inverse transition
  447. function removes them again when they leave the frame at the top. So,
  448. when values are removed, they are always removed in the same order they
  449. were added. Whenever the inverse transition function is invoked, it will
  450. thus receive the earliest added but not yet removed argument value(s).
  451. The inverse transition function can assume that at least one row will
  452. remain in the current state after it removes the oldest row. (When this
  453. would not be the case, the window function mechanism simply starts a
  454. fresh aggregation, rather than using the inverse transition function.)
  455. </p><p>
  456. The forward transition function for moving-aggregate mode is not
  457. allowed to return NULL as the new state value. If the inverse
  458. transition function returns NULL, this is taken as an indication that
  459. the inverse function cannot reverse the state calculation for this
  460. particular input, and so the aggregate calculation will be redone from
  461. scratch for the current frame starting position. This convention
  462. allows moving-aggregate mode to be used in situations where there are
  463. some infrequent cases that are impractical to reverse out of the
  464. running state value.
  465. </p><p>
  466. If no moving-aggregate implementation is supplied,
  467. the aggregate can still be used with moving frames,
  468. but <span class="productname">PostgreSQL</span> will recompute the whole
  469. aggregation whenever the start of the frame moves.
  470. Note that whether or not the aggregate supports moving-aggregate
  471. mode, <span class="productname">PostgreSQL</span> can handle a moving frame
  472. end without recalculation; this is done by continuing to add new values
  473. to the aggregate's state. This is why use of an aggregate as a window
  474. function requires that the final function be read-only: it must
  475. not damage the aggregate's state value, so that the aggregation can be
  476. continued even after an aggregate result value has been obtained for
  477. one set of frame boundaries.
  478. </p><p>
  479. The syntax for ordered-set aggregates allows <code class="literal">VARIADIC</code>
  480. to be specified for both the last direct parameter and the last
  481. aggregated (<code class="literal">WITHIN GROUP</code>) parameter. However, the
  482. current implementation restricts use of <code class="literal">VARIADIC</code>
  483. in two ways. First, ordered-set aggregates can only use
  484. <code class="literal">VARIADIC "any"</code>, not other variadic array types.
  485. Second, if the last direct parameter is <code class="literal">VARIADIC "any"</code>,
  486. then there can be only one aggregated parameter and it must also
  487. be <code class="literal">VARIADIC "any"</code>. (In the representation used in the
  488. system catalogs, these two parameters are merged into a single
  489. <code class="literal">VARIADIC "any"</code> item, since <code class="structname">pg_proc</code> cannot
  490. represent functions with more than one <code class="literal">VARIADIC</code> parameter.)
  491. If the aggregate is a hypothetical-set aggregate, the direct arguments
  492. that match the <code class="literal">VARIADIC "any"</code> parameter are the hypothetical
  493. ones; any preceding parameters represent additional direct arguments
  494. that are not constrained to match the aggregated arguments.
  495. </p><p>
  496. Currently, ordered-set aggregates do not need to support
  497. moving-aggregate mode, since they cannot be used as window functions.
  498. </p><p>
  499. Partial (including parallel) aggregation is currently not supported for
  500. ordered-set aggregates. Also, it will never be used for aggregate calls
  501. that include <code class="literal">DISTINCT</code> or <code class="literal">ORDER BY</code> clauses, since
  502. those semantics cannot be supported during partial aggregation.
  503. </p></div><div class="refsect1" id="id-1.9.3.57.8"><h2>Examples</h2><p>
  504. See <a class="xref" href="xaggr.html" title="37.12. User-Defined Aggregates">Section 37.12</a>.
  505. </p></div><div class="refsect1" id="id-1.9.3.57.9"><h2>Compatibility</h2><p>
  506. <code class="command">CREATE AGGREGATE</code> is a
  507. <span class="productname">PostgreSQL</span> language extension. The SQL
  508. standard does not provide for user-defined aggregate functions.
  509. </p></div><div class="refsect1" id="id-1.9.3.57.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alteraggregate.html" title="ALTER AGGREGATE"><span class="refentrytitle">ALTER AGGREGATE</span></a>, <a class="xref" href="sql-dropaggregate.html" title="DROP AGGREGATE"><span class="refentrytitle">DROP AGGREGATE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-create-access-method.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createcast.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE ACCESS METHOD </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE CAST</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1