CREATE AGGREGATE

Synopsis

Use the CREATE AGGREGATE statement to create a new aggregate function. There are three ways tocreate aggregates.

Syntax

  1. create_aggregate ::= create_aggregate_normal
  2. | create_aggregate_order_by
  3. | create_aggregate_old
  4. create_aggregate_normal ::= CREATE AGGREGATE aggregate_name (
  5. { aggregate_arg [ , ... ] | * } ) ( SFUNC
  6. = sfunc , STYPE = state_data_type
  7. [ , aggregate_normal_option [ ... ] ] )
  8. create_aggregate_order_by ::= CREATE AGGREGATE aggregate_name (
  9. [ aggregate_arg [ , ... ] ] ORDER BY
  10. aggregate_arg [ , ... ] ) ( SFUNC =
  11. sfunc , STYPE = state_data_type
  12. [ , aggregate_order_by_option [ ... ] ]
  13. )
  14. create_aggregate_old ::= CREATE AGGREGATE aggregate_name ( BASETYPE =
  15. base_type , SFUNC = sfunc , STYPE =
  16. state_data_type
  17. [ , aggregate_old_option [ ... ] ] )
  18. aggregate_arg ::= [ aggregate_argmode ] [ argname ] argtype
  19. aggregate_normal_option ::= SSPACE = state_data_size
  20. | FINALFUNC = ffunc
  21. | FINALFUNC_EXTRA
  22. | FINALFUNC_MODIFY =
  23. { READ_ONLY | SHAREABLE | READ_WRITE }
  24. | COMBINEFUNC = combinefunc
  25. | SERIALFUNC = serialfunc
  26. | DESERIALFUNC = deserialfunc
  27. | INITCOND = initial_condition
  28. | MSFUNC = msfunc
  29. | MINVFUNC = minvfunc
  30. | MSTYPE = mstate_data_type
  31. | MSSPACE = mstate_data_size
  32. | MFINALFUNC = mffunc
  33. | MFINALFUNC_EXTRA
  34. | MFINALFUNC_MODIFY =
  35. { READ_ONLY | SHAREABLE | READ_WRITE }
  36. | MINITCOND = minitial_condition
  37. | SORTOP = sort_operator
  38. | PARALLEL =
  39. { SAFE | RESTRICTED | UNSAFE }
  40. aggregate_order_by_option ::= SSPACE = state_data_size
  41. | FINALFUNC = ffunc
  42. | FINALFUNC_EXTRA
  43. | FINALFUNC_MODIFY =
  44. { READ_ONLY | SHAREABLE | READ_WRITE }
  45. | INITCOND = initial_condition
  46. | PARALLEL =
  47. { SAFE | RESTRICTED | UNSAFE }
  48. | HYPOTHETICAL
  49. aggregate_old_option ::= SSPACE = state_data_size
  50. | FINALFUNC = ffunc
  51. | FINALFUNC_EXTRA
  52. | FINALFUNC_MODIFY =
  53. { READ_ONLY | SHAREABLE | READ_WRITE }
  54. | COMBINEFUNC = combinefunc
  55. | SERIALFUNC = serialfunc
  56. | DESERIALFUNC = deserialfunc
  57. | INITCOND = initial_condition
  58. | MSFUNC = msfunc
  59. | MINVFUNC = minvfunc
  60. | MSTYPE = mstate_data_type
  61. | MSSPACE = mstate_data_size
  62. | MFINALFUNC = mffunc
  63. | MFINALFUNC_EXTRA
  64. | MFINALFUNC_MODIFY =
  65. { READ_ONLY | SHAREABLE | READ_WRITE }
  66. | MINITCOND = minitial_condition
  67. | SORTOP = sort_operator

create_aggregate

CREATE AGGREGATE - 图1

create_aggregate_normal

CREATE AGGREGATE - 图2

create_aggregate_order_by

CREATE AGGREGATE - 图3

create_aggregate_old

CREATE AGGREGATE - 图4

aggregate_arg

CREATE AGGREGATE - 图5

aggregate_normal_option

CREATE AGGREGATE - 图6

aggregate_order_by_option

CREATE AGGREGATE - 图7

aggregate_old_option

CREATE AGGREGATE - 图8

Semantics

The order of options does not matter. Even the mandatory options BASETYPE, SFUNC, and STYPEmay appear in any order.

See the semantics of each option in the [PostgreSQL docs][postgresql-docs-create-aggregate].

Examples

Normal syntax example.

  1. yugabyte=# CREATE AGGREGATE sumdouble (float8) (
  2. STYPE = float8,
  3. SFUNC = float8pl,
  4. MSTYPE = float8,
  5. MSFUNC = float8pl,
  6. MINVFUNC = float8mi
  7. );
  8. yugabyte=# CREATE TABLE normal_table(
  9. f float8,
  10. i int
  11. );
  12. yugabyte=# INSERT INTO normal_table(f, i) VALUES
  13. (0.1, 9),
  14. (0.9, 1);
  15. yugabyte=# SELECT sumdouble(f), sumdouble(i) FROM normal_table;

Order by syntax example.

  1. yugabyte=# CREATE AGGREGATE my_percentile_disc(float8 ORDER BY anyelement) (
  2. STYPE = internal,
  3. SFUNC = ordered_set_transition,
  4. FINALFUNC = percentile_disc_final,
  5. FINALFUNC_EXTRA = true,
  6. FINALFUNC_MODIFY = read_write
  7. );
  8. yugabyte=# SELECT my_percentile_disc(0.1), my_percentile_disc(0.9)
  9. WITHIN GROUP (ORDER BY typlen)
  10. FROM pg_type;

Old syntax example.

  1. yugabyte=# CREATE AGGREGATE oldcnt(
  2. SFUNC = int8inc,
  3. BASETYPE = 'ANY',
  4. STYPE = int8,
  5. INITCOND = '0'
  6. );
  7. yugabyte=# SELECT oldcnt(*) FROM pg_aggregate;

Zero-argument aggregate example.

  1. yugabyte=# CREATE AGGREGATE newcnt(*) (
  2. SFUNC = int8inc,
  3. STYPE = int8,
  4. INITCOND = '0',
  5. PARALLEL = SAFE
  6. );
  7. yugabyte=# SELECT newcnt(*) FROM pg_aggregate;

See also