Functions as first-class values

Started by Jon Smarkabout 15 years ago3 messagesgeneral
Jump to latest
#1Jon Smark
jon.smark@yahoo.com

Hi,

Is there support in PL/pgSQL for treating functions as first-class values?
Consider the pseudo-code simple example below, which illustrates how this
feature can be useful. I reckon that this behaviour can be emulated using
EXECUTE, but I suspect there are serious performance penalties.
Your thoughts?

Thanks in advance!
Jon

CREATE FUNCTION f1 (int) RETURNS boolean ...
CREATE FUNCTION f2 (int) RETURNS boolean ...
CREATE FUNCTION f3 (int) RETURNS boolean ...

CREATE FUNCTION do_stuff ...
BEGIN
CASE cond
WHEN 'a' THEN func := f1;
WHEN 'b' THEN func := f2;
WHEN 'c' THEN func := f3;
END CASE

SELECT * FROM foobar WHERE func (foobar.age);
END;

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Jon Smark (#1)
Re: Functions as first-class values

Hello

2011/4/6 Jon Smark <jon.smark@yahoo.com>:

Hi,

Is there support in PL/pgSQL for treating functions as first-class values?
Consider the pseudo-code simple example below, which illustrates how this
feature can be useful.  I reckon that this behaviour can be emulated using
EXECUTE, but I suspect there are serious performance penalties.
Your thoughts?

Thanks in advance!
Jon

CREATE FUNCTION f1 (int) RETURNS boolean ...
CREATE FUNCTION f2 (int) RETURNS boolean ...
CREATE FUNCTION f3 (int) RETURNS boolean ...

CREATE FUNCTION do_stuff ...
BEGIN
       CASE cond
               WHEN 'a' THEN func := f1;
               WHEN 'b' THEN func := f2;
               WHEN 'c' THEN func := f3;
       END CASE

       SELECT * FROM foobar WHERE func (foobar.age);
END;

no it isn't possible.

Regards

Pavel Stehule

Show quoted text

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Merlin Moncure
mmoncure@gmail.com
In reply to: Pavel Stehule (#2)
Re: Functions as first-class values

On Wed, Apr 6, 2011 at 9:10 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

Hello

2011/4/6 Jon Smark <jon.smark@yahoo.com>:

Hi,

Is there support in PL/pgSQL for treating functions as first-class values?
Consider the pseudo-code simple example below, which illustrates how this
feature can be useful.  I reckon that this behaviour can be emulated using
EXECUTE, but I suspect there are serious performance penalties.
Your thoughts?

Thanks in advance!
Jon

CREATE FUNCTION f1 (int) RETURNS boolean ...
CREATE FUNCTION f2 (int) RETURNS boolean ...
CREATE FUNCTION f3 (int) RETURNS boolean ...

CREATE FUNCTION do_stuff ...
BEGIN
       CASE cond
               WHEN 'a' THEN func := f1;
               WHEN 'b' THEN func := f2;
               WHEN 'c' THEN func := f3;
       END CASE

       SELECT * FROM foobar WHERE func (foobar.age);
END;

no it isn't possible.

it can be done with a small C wrapper though. see archives.

merlin