User-defined data types with user-defined aggregates

You cannot use SUMSQ with the complex column of the c_test table illustrated in Example of extending a built-in aggregate because the complex data type does not cast to the FLOAT data type. To use SUMSQ with the complex data type, you must overload the support functions of the SUMSQ aggregate.
CREATE FUNCTION ssq_init (dummy complex)
   RETURNING complex;
   RETURN ROW(0,0)::complex;
END FUNCTION;

CREATE FUNCTION ssq_iter (partial complex, c complex)
   RETURNING complex;
   RETURN ROW (
      (partial.real + c.real*c.real - c.imag*c.imag),
      (partial.imag + 2*c.real*c.imag) 
      )::complex;
END FUNCTION;

CREATE FUNCTION ssq_combine(p1 complex, p2 complex)
   RETURNING complex;
   RETURN ROW(p1.real + p2.real, 
           p1.imag + p2.imag)::complex;
END FUNCTION;

CREATE FUNCTION ssq_final(final complex)
   RETURNING complex;
   RETURN final::complex;
END FUNCTION;

When you overload support functions for a user-defined aggregate, you must prepare exactly the same functions as those declared in the CREATE AGGREGATE statement. In this example, that requirement means overloading each of the support functions.