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

From Thalis A. Kalfigopoulos
Subject Re: aggregate function for median calculation
Date
Msg-id Pine.LNX.4.21.0106202104480.24987-100000@aluminum.cs.pitt.edu
Whole thread Raw
In response to Re: aggregate function for median calculation  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: aggregate function for median calculation  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Tue, 19 Jun 2001, Tom Lane wrote:

> Peter Eisentraut <peter_e@gmx.net> writes:
> > Sure, you create a (static) global variable and reallocate memory for it
> > in each call and free it by the finalizer function.
>
> A static would be a bad idea (consider a query with multiple instances
> of this aggregate being evaluated in parallel).
>
> But there's no reason that the transition function can't palloc a larger
> and larger chunk of memory for each result (as long as you don't run out
> of memory, anyway).
>
>             regards, tom lane
>

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
}

So the transition function is:
struct state *trans_function(struct state *last_state,int new_element){
    //Allocate mem for a new state structure to hold all previous numbers + new one
    //Copy over all the previous elements from last_state and add the new_element
    //Return ptr to this new state struct
}

My question is: how do I declare this in Pg?
declare function transition_func(???,int4) RETURNS ??? AS 'path_to_.so_file','trans_function' LANGUAGE 'C';

I assume that I have mixed up what can be a transition state :-/

Any help welcome.


TIA,
thalis


pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Web site gripes
Next
From: Tom Lane
Date:
Subject: Re: postgres.h missing? (fwd)