Re: Functions in C with Ornate Data Structures

From: "Stephen P(dot) Berry" <spb(at)meshuggeneh(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Stephen P(dot) Berry" <spb(at)meshuggeneh(dot)net>, 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 03:01:10
Message-ID: 20020119030110.12D32106FC@schadenfreude.meshuggeneh.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-novice

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In message <11292(dot)1011403856(at)sss(dot)pgh(dot)pa(dot)us>, Tom Lane writes:

>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:

I'd actually thought of doing something like this, but couldn't find
an actual explicit argument type for pointers[0], and I can't make
the assumption you describe for portability reasons (my three main
test platforms are alpha, sparc64, and x86).

I was also hoping that I could get away with not passing the problem
data structures internally at all...i.e., have a crunch_input() function
that initialises the linked list I need and populates it,
then a crunch_result() function that spits out the result. The
accumulator is just a dummy variable, and the interesting data structure(s)
aren't in the argument list for any of the functions. I tried
doing such a thing with an aggregate, but it didn't work---although,
interestingly, invoking the input and result functions manually
in a single session did. I took this to mean that I really didn't
understand aggregates, so I was assuming it was a novice-level question.
I was sorta hoping this would turn out to be a standard question (although
I couldn't find any useful references in the mailing list archives or
via web searches).

>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.

Is there any way to keep `intermediate' data used by user-defined
functions around indefinitely? I.e., have some sort of crunch_init()
function that creates a bunch of in-memory data structures, which
can then be used by subsequent (and independent) queries? I'm
assuming not...and if I want to do that sort of thing I should populate
a temporary table with the data from these `intermediate' results.
Or keep all this fancy stuff in standalone applications rather than
in user-defined functions.

It seems like the general class of thing I'm trying to accomplish
isn't that esoteric. Imagine trying to write a function to compute
the standard deviation of arbitrary precision numbers using the GMP
library or some such. Note that I'm not saying that that's what I'm
trying to do...I'm just offering it as a simple sample problem in
which one can't pass everything as an argument in an aggregate. How
does one set about doing such a thing in Postgres?

- -Steve

- -----
0 I was hoping that there would be, since the macro widgetry in
the version 1 function semantics clearly includes the concept
of pointers as a distinct type.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.3 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8SN5tG3kIaxeRZl8RAmPmAJ4ilTeyoC//MRG5JHf7AmNuR7oW/QCdHHqw
RoE/GplKts1rxNO85ADEebk=
=Oedz
-----END PGP SIGNATURE-----

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-01-19 03:28:07 Re: Functions in C with Ornate Data Structures
Previous Message Tom Lane 2002-01-19 01:30:56 Re: Functions in C with Ornate Data Structures

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-01-19 03:28:07 Re: Functions in C with Ornate Data Structures
Previous Message Tom Lane 2002-01-19 01:30:56 Re: Functions in C with Ornate Data Structures