Re: aggregate function for median calculation

From: "Thalis A(dot) Kalfigopoulos" <thalis(at)cs(dot)pitt(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Peter Eisentraut <peter_e(at)gmx(dot)net>, Alex Pilosov <alex(at)pilosoft(dot)com>
Subject: Re: aggregate function for median calculation
Date: 2001-06-21 01:34:11
Message-ID: Pine.LNX.4.21.0106202104480.24987-100000@aluminum.cs.pitt.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 19 Jun 2001, Tom Lane wrote:

> Peter Eisentraut <peter_e(at)gmx(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-06-21 02:01:17 Re: postgres.h missing? (fwd)
Previous Message Bruce Momjian 2001-06-21 01:31:18 Re: Web site gripes