pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using SETOF in plpgsql function
8+ messages / 6 participants
[nested] [flat]

* Using SETOF in plpgsql function
@ 2000-08-23 10:07  hlefebvre <hlefebvre@lexbase.net>
  0 siblings, 2 replies; 8+ messages in thread

From: hlefebvre @ 2000-08-23 10:07 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I'd like to return a set of integer in an pl/pgsql function. How can I
do that ?

I've tried things like that, put I've an error when executing :

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
    DECLARE ID INTEGER;
BEGIN
    select a into id from foo;
    return ID ;
END;      

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
    DECLARE ID setof INTEGER;
BEGIN
    select a into id from foo;
    return ID ;
END; 

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
BEGIN
    select a into id from foo;
    return select a from foo; 
END;           

Any help is welcomed.
Thanks.



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* RE: Using SETOF in plpgsql function
@ 2000-08-23 12:15  Graham Vickrage <graham@digitalplanit.com>
  parent: hlefebvre <hlefebvre@lexbase.net>
  1 sibling, 1 reply; 8+ messages in thread

From: Graham Vickrage @ 2000-08-23 12:15 UTC (permalink / raw)
  To: hlefebvre <hlefebvre@lexbase.net>; pgsql-sql

As far as i know, you can only return single values from functions at the
moment.

Regards

Graham

-----Original Message-----
From: pgsql-sql-owner@hub.org [mailto:pgsql-sql-owner@hub.org]On Behalf
Of hlefebvre
Sent: 23 August 2000 11:08
To: pgsql-sql@postgresql.org
Subject: [SQL] Using SETOF in plpgsql function


Hello,

I'd like to return a set of integer in an pl/pgsql function. How can I
do that ?

I've tried things like that, put I've an error when executing :

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
    DECLARE ID INTEGER;
BEGIN
    select a into id from foo;
    return ID ;
END;

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
    DECLARE ID setof INTEGER;
BEGIN
    select a into id from foo;
    return ID ;
END;

CREATE FUNCTION SP_UPLES() RETURNS setof INTEGER AS '
BEGIN
    select a into id from foo;
    return select a from foo;
END;

Any help is welcomed.
Thanks.




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 12:35  hlefebvre <hlefebvre@lexbase.net>
  parent: Graham Vickrage <graham@digitalplanit.com>
  0 siblings, 0 replies; 8+ messages in thread

From: hlefebvre @ 2000-08-23 12:35 UTC (permalink / raw)
  To: Graham Vickrage <graham@digitalplanit.com>; +Cc: pgsql-sql



Graham Vickrage wrote:
> 
> As far as i know, you can only return single values from functions at the
> moment.
> 
> Regards
> 
> Graham

Hum, this is possible a least in SQL functions. 
But maybe impossible in PL/PGSQL



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 13:46  Andreas Tille <tillea@rki.de>
  parent: Jan Wieck <janwieck@Yahoo.com>
  2 siblings, 0 replies; 8+ messages in thread

From: Andreas Tille @ 2000-08-23 13:46 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

On Wed, 23 Aug 2000, Jan Wieck wrote:

>     You  can't. Not with PL/pgSQL nor with any other PL or C. The
>     problem is nested deeper and requires the  planned  querytree
>     redesign to get solved.
> 
>     Before  you  ask: The mentioned redesign will NOT be done for
>     7.1, and I'm not sure if we will be able to  do  it  for  7.2
>     yet.
Just to make sure you've got to know that this is a feature needed
by many users:  Add me to the list of users who have a big need for
this!

Kind regards

           Andreas.




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 13:48  Karel Zak <zakkr@zf.jcu.cz>
  parent: Jan Wieck <janwieck@Yahoo.com>
  2 siblings, 0 replies; 8+ messages in thread

From: Karel Zak @ 2000-08-23 13:48 UTC (permalink / raw)
  To: Jan Wieck <janwieck@Yahoo.com>; +Cc: hlefebvre <hlefebvre@lexbase.net>; pgsql-sql


On Wed, 23 Aug 2000, Jan Wieck wrote:

>     Before  you  ask: The mentioned redesign will NOT be done for
>     7.1, and I'm not sure if we will be able to  do  it  for  7.2
>     yet.

 I hope that 7.2 :-), my query/plan cache is still outside current
interest and if core developers not will work on something like query
path redesign, the query/plan cache will still out.. :-(

 Or already test query/plan cache anyone? IMHO it is good merge-able
to current source too.

					Karel

PS. sorry of my small sigh for this.... :-) 




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 14:24  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Jan Wieck <janwieck@Yahoo.com>
  2 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2000-08-23 14:24 UTC (permalink / raw)
  To: Jan Wieck <janwieck@Yahoo.com>; +Cc: hlefebvre <hlefebvre@lexbase.net>; pgsql-sql

Jan Wieck <janwieck@Yahoo.com> writes:
> hlefebvre wrote:
>> I'd like to return a set of integer in an pl/pgsql function. How can I
>> do that ?

>     You  can't. Not with PL/pgSQL nor with any other PL or C. The
>     problem is nested deeper and requires the  planned  querytree
>     redesign to get solved.

Not really.  Coincidentally enough, I am just in the middle of removing
execQual.c's hard-wired assumption that only SQL-language functions
can return sets.  (This is a side effect of fixing the function manager
so that SQL functions can be called in all contexts, eg used as index
functions.)  If you want to fix plpgsql so that it retains state and
can produce multiple elements of a set over repeated calls, the same
way that SQL functions do, then it could be done today.

We may well want to rip out that whole approach to set functions later
when we redo querytrees, but the real limitation so far has been bogus
assumptions in the function-call API, not querytrees.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 14:27  Jan Wieck <janwieck@Yahoo.com>
  parent: hlefebvre <hlefebvre@lexbase.net>
  1 sibling, 3 replies; 8+ messages in thread

From: Jan Wieck @ 2000-08-23 14:27 UTC (permalink / raw)
  To: hlefebvre <hlefebvre@lexbase.net>; +Cc: pgsql-sql

hlefebvre wrote:
> Hello,
>
> I'd like to return a set of integer in an pl/pgsql function. How can I
> do that ?

    You  can't. Not with PL/pgSQL nor with any other PL or C. The
    problem is nested deeper and requires the  planned  querytree
    redesign to get solved.

    Before  you  ask: The mentioned redesign will NOT be done for
    7.1, and I'm not sure if we will be able to  do  it  for  7.2
    yet.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Using SETOF in plpgsql function
@ 2000-08-23 16:53  Jan Wieck <janwieck@Yahoo.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Jan Wieck @ 2000-08-23 16:53 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Jan Wieck <janwieck@Yahoo.com>; hlefebvre <hlefebvre@lexbase.net>; pgsql-sql

Tom Lane wrote:
> Jan Wieck <janwieck@Yahoo.com> writes:
> > hlefebvre wrote:
> >> I'd like to return a set of integer in an pl/pgsql function. How can I
> >> do that ?
>
> >     You  can't. Not with PL/pgSQL nor with any other PL or C. The
> >     problem is nested deeper and requires the  planned  querytree
> >     redesign to get solved.
>
> Not really.  Coincidentally enough, I am just in the middle of removing
> execQual.c's hard-wired assumption that only SQL-language functions
> can return sets.  (This is a side effect of fixing the function manager
> so that SQL functions can be called in all contexts, eg used as index
> functions.)  If you want to fix plpgsql so that it retains state and
> can produce multiple elements of a set over repeated calls, the same
> way that SQL functions do, then it could be done today.

    Not   that   easy.  PL/pgSQL  isn't  a  state  machine.   The
    precompiled code is kind of a nested tree of  statements.   A
    RETURN causes a controlled return() through all nested levels
    of the PL executors C calls. This might close  SPI  calls  in
    execution as well. Imagine a code construct like

        FOR rec IN SELECT * FROM customer LOOP
            RETURN rec.cust_id AND RESUME;
        END LOOP;

    which  would  be  the correct syntax for returning sets. What
    happens in PL/pgSQL while execution is, that at the beginning
    of  the  loop the SPI query for SELECT is performed, and then
    the loop executed for all rows in the SPI result set. And  of
    course, you can have nested loops, why not.

    Now  you want to return the first value. If you really return
    to the fmgr at this time, the connection to the  SPI  manager
    must  be  closed,  loosing the result set. So how to continue
    later?

    If we want to make it now for  sets  of  scalar  values  (not
    tuple sets), we could add another feature to the fmgr and the
    PL handlers, which we need later anyway.

    In the case of a call to a PL or C function returning a  set,
    the  fmgr  creates  a temp table and calls the function which
    fills the temp table with all the  return  values.  Now  fmgr
    changes  the  execution  trees  func node in a way that it is
    operating like an SQL function - holding a seqscan  over  the
    temp table. After the last result is returned, the temp table
    is removed. This'd work for tuple sets as well (so  the  temp
    table then is our tuple-source).


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #





^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2000-08-23 16:53 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-08-23 10:07 Using SETOF in plpgsql function hlefebvre <hlefebvre@lexbase.net>
2000-08-23 12:15 ` Graham Vickrage <graham@digitalplanit.com>
2000-08-23 12:35   ` hlefebvre <hlefebvre@lexbase.net>
2000-08-23 14:27 ` Jan Wieck <janwieck@Yahoo.com>
2000-08-23 13:46   ` Andreas Tille <tillea@rki.de>
2000-08-23 13:48   ` Karel Zak <zakkr@zf.jcu.cz>
2000-08-23 14:24   ` Tom Lane <tgl@sss.pgh.pa.us>
2000-08-23 16:53     ` Jan Wieck <janwieck@Yahoo.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox