Thread: Using SETOF in plpgsql function

Using SETOF in plpgsql function

From
hlefebvre
Date:
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.


RE: Using SETOF in plpgsql function

From
"Graham Vickrage"
Date:
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.



Re: Using SETOF in plpgsql function

From
hlefebvre
Date:

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


Re: Using SETOF in plpgsql function

From
Jan Wieck
Date:
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 #




Re: Using SETOF in plpgsql function

From
Karel Zak
Date:
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.... :-) 



Re: Using SETOF in plpgsql function

From
Andreas Tille
Date:
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.



Re: Using SETOF in plpgsql function

From
Tom Lane
Date:
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


Copy To - fixed width

From
"Adam Lang"
Date:
Is it possible to copy from a text file that has the fields as fixed width
with no delimiter to a table?

The other option is a CSV file... but then how do I handle if there is a
comma in one of the fields?

I'm transfering information from an AS/400 ... if it is into a text file, it
is fixed width, if I use Excel, it is Comma separated.



Re: Copy To - fixed width

From
"Adam Lang"
Date:
Sort of nevermind, can I can save it tab delimited, but I am still curious
if it can be done using fixed width fields.


Adam Lang
Systems Engineer
Rutgers Casualty Insurance Company
----- Original Message -----
From: "Adam Lang" <aalang@rutgersinsurance.com>
Cc: <pgsql-sql@postgresql.org>
Sent: Wednesday, August 23, 2000 10:38 AM
Subject: [SQL] Copy To - fixed width


> Is it possible to copy from a text file that has the fields as fixed width
> with no delimiter to a table?
>
> The other option is a CSV file... but then how do I handle if there is a
> comma in one of the fields?
>
> I'm transfering information from an AS/400 ... if it is into a text file,
it
> is fixed width, if I use Excel, it is Comma separated.



Re: Using SETOF in plpgsql function

From
Jan Wieck
Date:
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
mightclose  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
resultset. 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
fmgrand 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
functionwhich   fills the temp table with all the  return  values.  Now  fmgr   changes  the  execution  trees  func
nodein a way that it is   operating like an SQL function - holding a seqscan  over  the   temp table. After the last
resultis 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 #