Relations as arguments to a stored procedure?

From: <kynn(at)panix(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Relations as arguments to a stored procedure?
Date: 2006-03-09 18:37:30
Message-ID: 200603091837.k29IbU228541@panix3.panix.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Is there a way to pass a relation (or view) as one of the arguments to
a stored procedure? Consider the following pseudo-code:

function foo ( table T, int X ) {
int Y = computationally_demanding_function( X );
foreach record R in T {
insert some_other_function( R, Y ) in RESULTS;
}
return RESULTS;
}

Since the computation of Y is time-consuming, I don't want to repeat
it for every record in the input table T. This rules out an
implementation in which foo takes a single record as its first
argument, and is applied to each record in some input table through a
SELECT statement.

Alternatively, one could define foo to take, as arguments, a single
record and a precomputed Y = computationally_demanding_function( X ),
but this requires users to know about
computationally_demanding_function, which may be completely extraneous
to their needs. All they know is that they have a relation T and a
parameter X, and want to perform a particular operation, which depends
expensively on X, on each record of T.

I'd find it surprising that procedures for a RDBMS would not accept
relations as parameters, but I can't find any info on this.

I also know about arrays, but it seems like working with arrays is a
real bear. Just getting documentation on them is already a major
undertaking. I've spent about one hour trying to find out how one
iterates over an array if one doesn't know its length beforehand (or
how to determine its length in the first place). I don't even yet
know how to initialize an array from the rows of a 1-column table; I
imagine finding the answer to that will be another multi-hour
assignment. These are the most basic questions I can imagine for an
array data structure, but finding answers for them appears to be
*insanely difficult*. What am I missing?

Thanks!

kj

P.S. Practically every time I post to this list I ask for good sources
of documentation (because obviously the ones I use are terrible).
Invariably I am referred to the online PostgreSQL documentation, but
as the questions above show, the online documentation is not working
for me. I guess I refuse to believe that questions as elementary and
fundamental as mine are not documented anywhere...

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Christopher A. Goodfellow 2006-03-09 18:48:40 Re: Invalid Page Header
Previous Message operationsengineer1 2006-03-09 17:33:11 Re: Storing sensitive data