1959
MySQLERRORCommonStored RoutinesHIGH confidence

AGGREGATE is not supported for stored functions

Production Risk

None — function creation is rejected.

What this means

The AGGREGATE keyword was specified in a CREATE FUNCTION statement, but MySQL does not support user-defined aggregate stored functions using this syntax. Aggregate functions require a different mechanism (loadable plugins in MySQL 8.0).

Why it happens
  1. 1CREATE AGGREGATE FUNCTION using the stored function syntax instead of a UDF plugin.
How to reproduce
trigger — this will error
trigger — this will error
CREATE AGGREGATE FUNCTION my_sum(val DOUBLE) RETURNS DOUBLE BEGIN ... END;

expected output

ERROR 1959 (HY000): AGGREGATE is not supported for stored functions.

Fix 1

Use a window function or GROUP BY with a built-in aggregate instead

Use a window function or GROUP BY with a built-in aggregate instead
SELECT SUM(val) FROM t GROUP BY category;

Why this works

Built-in aggregate functions (SUM, AVG, COUNT) cover most use cases.

Fix 2

Implement aggregate UDFs as loadable plugin functions (MySQL 8.0)

Implement aggregate UDFs as loadable plugin functions (MySQL 8.0)
-- Implement as a C++ plugin and install with CREATE FUNCTION ... SONAME

Why this works

True user-defined aggregates require writing a plugin in C++.

Version notes

Sources
Official documentation ↗

MySQL 8.0 — 1959 ER_SP_NO_AGGREGATE

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors