Re: Functions in C with Ornate Data Structures

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Stephen P(dot) Berry" <spb(at)meshuggeneh(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Functions in C with Ornate Data Structures
Date: 2002-01-19 01:30:56
Message-ID: 11292.1011403856@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-novice

"Stephen P. Berry" <spb(at)meshuggeneh(dot)net> writes:
> If my data types were simpler, I could simply use an AGGREGATE function.
> Unfortunately, I don't know of any way to schlep something as complex
> as a doubly-linked list of arrays of arbitrary precision numbers.

You could, but the amount of data copying needed would be annoying.
However, there's no law that says you can't cheat. I'd suggest that
you build this as an aggregate function whose nominal state value is
simply a pointer to data structures that are off somewhere else.

For example, assuming that you are willing to cheat to the extent of
assuming sizeof(pointer) = sizeof(integer), try something like this:

CREATE AGGREGATE crunch_number (
basetype = float8, -- or whatever the input column type is
sfunc = crunch_func,
stype = integer,
ffunc = crunch_finish,
initcond = 0);

where crunch_func(integer) returns integer is your data accumulation
function, and it looks like

datstruct *ptr = (datstruct *) PG_GET_INT32(0);
double newdataval = PG_GET_FLOAT8(1);

if (ptr == NULL)
{
/* first call of query; initialize datastructures */
}

/* update datastructures using newdataval */

PG_RETURN_INT32((int32) ptr);

Finally, crunch_finish(integer) returns float8 (or whatever is needed)
contains your code to compute the final result and release the working
datastructure.

Now, the important detail: you can't allocate your working
datastructures with a simple palloc(), because these functions will be
called in a short-lived memory context. What I'd suggest is that in
your setup step, you create a private memory context that is a child
of TransactionCommandContext; then allocate all your datastructures in
that. Then in the crunch_finish step, you needn't bother with retail
releasing of the data structures, just destroy the private context
and you're done.

regards, tom lane

PS: this is not a novice-level question ;-). You should be asking this
kind of stuff on pgsql-hackers, methinks. There really isn't any other
list that discusses C coding inside the backend.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephen P. Berry 2002-01-19 03:01:10 Re: Functions in C with Ornate Data Structures
Previous Message Laurette Cisneros 2002-01-19 00:59:18 Re: pg_dump question

Browse pgsql-novice by date

  From Date Subject
Next Message Stephen P. Berry 2002-01-19 03:01:10 Re: Functions in C with Ornate Data Structures
Previous Message Stephen P. Berry 2002-01-19 00:24:33 Functions in C with Ornate Data Structures