Re: aggregate function for median calculation - Mailing list pgsql-general

From Tom Lane
Subject Re: aggregate function for median calculation
Date
Msg-id 12236.993100885@sss.pgh.pa.us
Whole thread Raw
In response to Re: aggregate function for median calculation  ("Thalis A. Kalfigopoulos" <thalis@cs.pitt.edu>)
Responses Re: aggregate function for median calculation  ("Thalis A. Kalfigopoulos" <thalis@cs.pitt.edu>)
List pgsql-general
"Thalis A. Kalfigopoulos" <thalis@cs.pitt.edu> writes:
> I'm still a bit confused about how to declare the type of the transition state.
> I have a structure that will hold the current state:
> struct state {
>     int elem_count; //how many numbers have been assigned so far
>     int *elem_area; //ptr to area holding these integers whose median I'll later calculate
> }

> My question is: how do I declare this in Pg?

Since you haven't made this a full-fledged Postgres type, you don't.

There's no real *need* to make it a full-fledged type, actually, since
nobody except your two aggregate functions will ever manipulate the
value.  So I'd suggest cheating.  Make it a legal "varlena" value by
having the first word of the struct contain the total length in bytes
of the object (including itself).  Then pretend in the SQL transition
function declaration and aggregate declaration that the transition
datatype is any old varlena type --- text or bytea would do fine.
Nothing except your code will look at anything except the varlena
length, so the fact that the body of the value means something unusual
won't matter.

Or, if you want to be rigidly correct, you could make the transition
value be a legitimate int4 array (_int4), which is only a field or
three more overhead.  But I don't see any value in it, except possibly
for manual testing of your transition functions.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Update and cursor
Next
From: Tom Lane
Date:
Subject: Re: Problems with pgsql 7.1.2 and ExecEvalExpr