Thread: Can a function be parameter in PL/PGSQL function?

Can a function be parameter in PL/PGSQL function?

From
"Karen Hill"
Date:
Is it possible to have a pl/pgsql function take another pl/pgsql
function as one of the parameters?

regards,
karen


Re: Can a function be parameter in PL/PGSQL function?

From
Jeff Davis
Date:
On Tue, 2007-01-30 at 12:32 -0800, Karen Hill wrote:
> Is it possible to have a pl/pgsql function take another pl/pgsql
> function as one of the parameters?
>

Not directly, but it could take a text string as a parameter and then
EXECUTE the text string after passing it to quote_ident().

Regards,
    Jeff Davis


Re: Can a function be parameter in PL/PGSQL function?

From
"Merlin Moncure"
Date:
On 30 Jan 2007 12:32:04 -0800, Karen Hill <karen_hill22@yahoo.com> wrote:
> Is it possible to have a pl/pgsql function take another pl/pgsql
> function as one of the parameters?

not exactly. you can take a string and execute it via dynamic sql, but
this is going to cause problems with record and array types and
generally just uglify your code.  If you want to implement a callback,
you can improvise one if you are willing to cut a little C:

http://archives.postgresql.org/pgsql-performance/2005-11/msg00012.php


I've followed this mechanism, and it works...

merlin

Re: Can a function be parameter in PL/PGSQL function?

From
elein
Date:
On Tue, Jan 30, 2007 at 12:32:04PM -0800, Karen Hill wrote:
> Is it possible to have a pl/pgsql function take another pl/pgsql
> function as one of the parameters?
>
> regards,
> karen
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match
>

Is this what you mean? (Where arg takes and returns text)

elein=# select arg( arg('xxx') );
 arg
-----
 xxx
(1 row)

--elein

Re: Can a function be parameter in PL/PGSQL function?

From
"Merlin Moncure"
Date:
On 2/3/07, elein <elein@varlena.com> wrote:
> On Tue, Jan 30, 2007 at 12:32:04PM -0800, Karen Hill wrote:
> > Is it possible to have a pl/pgsql function take another pl/pgsql
> > function as one of the parameters?

I think that OP meant (correct me if I'm wrong!) to be able to do
something like:

create function bar(text) returns text as $$ select $1 || 'xyz'; $$
language sql;

create function something_complex(regprocedure) returns void as [...]
language plpgsql;

select something_complex(bar(text));

...so you pass the callback 'bar' to the complex function which
executes it over something.  This isn't possible currently, but you
can fudge it with dynamic sql for simple things (no arrays, records,
or cursors), or work up something more general solution with a C
go-between.

merlin