Chandru Aroor <caroor@yahoo.com> writes:
> ... median is defines as follows:
> CREATE AGGREGATE public.median(anyelement) ( SFUNC=array_append, STYPE=anyarray, FINALFUNC=array_median
> I will be honest. I have no clue how that works or what it is supposed
> to do.
It's a user-defined aggregate. See
https://www.postgresql.org/docs/current/static/xaggr.html
array_append is a built-in function that, in this usage, would just serve
to collect all the input values into an array. array_median is not the
name of any built-in function, so it must be user-written code. What
I'd expect it to do, if it's trying to implement the usual definition of
median, is to sort the array and then take the middle element. Judging
from your description, it's not doing that.
If you're using PG 9.4 or later, you could skip trying to debug this
homegrown version of median and instead use the standard ordered-set
aggregate percentile_cont, or possibly percentile_disc. See
https://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-AGGREGATES
regards, tom lane