Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 22nd, 2005, 08:49 AM
Terry Lee Tucker
Guest
 
Posts: n/a
Default Return Value of a Function

I need to return a row of data from a function. I've been looking the the HTML
docs and have found nothing of value. If I try to return a variable of type
RECORD, I get the following error:
NOTICE: plpgsql: ERROR during compile of last_log near line 0
ERROR: fmgr_info: function 0: cache lookup failed

I may have several other things wrong with this function, so my real question
is, "Can I return a value of type RECORD?"
--
Quote: 56
"Guard with jealous attention the public liberty. Suspect every one who
approaches that jewel. Unfortunately, nothing will preserve it but down-
right force. Whenever you give up that force, you are inevitably ruined."

--Patrick Henry

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend




  #2  
Old November 22nd, 2005, 08:49 AM
Richard Huxton
Guest
 
Posts: n/a
Default Re: Return Value of a Function

On Monday 02 February 2004 14:36, Terry Lee Tucker wrote:[color=blue]
>
> I may have several other things wrong with this function, so my real
> question is, "Can I return a value of type RECORD?"[/color]

Yes, but you can't necessarily do anything with it (unless you're calling it
from another function).

You could return a SETOF myrowtype where the set contains only one row -
that's probably what you're after. Then you can do things like:

SELECT * FROM set_returning_function(1,'a');

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

  #3  
Old November 22nd, 2005, 08:49 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Terry Lee Tucker <terry@esc1.com> writes:[color=blue]
> I need to return a row of data from a function. I've been looking the the HTML
> docs and have found nothing of value. If I try to return a variable of type
> RECORD, I get the following error:
> NOTICE: plpgsql: ERROR during compile of last_log near line 0
> ERROR: fmgr_info: function 0: cache lookup failed[/color]

What Postgres version are you running? We used to have some bugs that
would allow unhelpful error messages like that to emerge in corner
cases. I'm not completely sure that they're all gone. If you're on
7.4.* I'd be interested to see exactly what you did.
[color=blue]
> I may have several other things wrong with this function, so my real question
> is, "Can I return a value of type RECORD?"[/color]

If you know what you're doing. The calling query has to specify a
structure for the record.

(Joe, I couldn't find a self-contained example using a function-returning-
record after a bit of searching in the 7.4 docs. Surely there is one?)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

  #4  
Old November 22nd, 2005, 08:49 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Richard Huxton <dev@archonet.com> writes:[color=blue]
> You could return a SETOF myrowtype where the set contains only one row -
> that's probably what you're after. Then you can do things like:[/color]
[color=blue]
> SELECT * FROM set_returning_function(1,'a');[/color]

You're confusing SETOF with returning a composite type --- actually they
are orthogonal features that can be used separately. Obviously the docs
could stand to be improved in this area :-(

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

  #5  
Old November 22nd, 2005, 08:49 AM
Terry Lee Tucker
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Tom,

My version is: 7.2.3-RH

Is this my problem? The example you mention would be great to see if it canbe
located. I'm trying to write a function that will return the last record ina
sequence of logs, "last" being defined by an ORDER BY statement containing
time stamp criteria in conjuction with other data. The function would always
be called by trigger code.

Thanks...

On Monday 02 February 2004 11:42 am, Tom Lane wrote:[color=blue]
> Terry Lee Tucker <terry@esc1.com> writes:[color=green]
> > I need to return a row of data from a function. I've been looking the the
> > HTML docs and have found nothing of value. If I try to return a variable
> > of type RECORD, I get the following error:
> > NOTICE: plpgsql: ERROR during compile of last_log near line 0
> > ERROR: fmgr_info: function 0: cache lookup failed[/color]
>
> What Postgres version are you running? We used to have some bugs that
> would allow unhelpful error messages like that to emerge in corner
> cases. I'm not completely sure that they're all gone. If you're on
> 7.4.* I'd be interested to see exactly what you did.
>[color=green]
> > I may have several other things wrong with this function, so my real
> > question is, "Can I return a value of type RECORD?"[/color]
>
> If you know what you're doing. The calling query has to specify a
> structure for the record.
>
> (Joe, I couldn't find a self-contained example using a function-returning-
> record after a bit of searching in the 7.4 docs. Surely there is one?)
>
> regards, tom lane[/color]

--

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

  #6  
Old November 22nd, 2005, 08:49 AM
Joe Conway
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Tom Lane wrote:[color=blue]
> (Joe, I couldn't find a self-contained example using a function-returning-
> record after a bit of searching in the 7.4 docs. Surely there is one?)[/color]

Looks like only one (see bottom of "Examples" section):
http://www.postgresql.org/docs/curre...ql-select.html
Probably could use more examples somewhere.

We frequently direct people to Stephan Szabo's "Set Returning Functions"
page on Techdocs:
http://techdocs.postgresql.org/guide...rningFunctions

Joe



---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

  #7  
Old November 22nd, 2005, 08:50 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Joe Conway <mail@joeconway.com> writes:[color=blue]
> Tom Lane wrote:[color=green]
>> (Joe, I couldn't find a self-contained example using a function-returning-
>> record after a bit of searching in the 7.4 docs. Surely there is one?)[/color][/color]
[color=blue]
> Looks like only one (see bottom of "Examples" section):
> http://www.postgresql.org/docs/curre...ql-select.html
> Probably could use more examples somewhere.[/color]

Yeah, I would have expected to find functions-returning-RECORD discussed
somewhere in xfunc.sgml, probably as a subsection near here:
http://www.postgresql.org/docs/curre....html#AEN28789

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

  #8  
Old November 22nd, 2005, 08:50 AM
Joe Conway
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Terry Lee Tucker wrote:[color=blue]
> My version is: 7.2.3-RH[/color]

That would be the first problem -- you need at least 7.3.x to return a
composite type from PL/pgSQL. Might as well upgrade to 7.4.1 if you're
going to do it at all.

Joe




---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

  #9  
Old November 22nd, 2005, 08:50 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: Return Value of a Function

Terry Lee Tucker <terry@esc1.com> writes:[color=blue]
> My version is: 7.2.3-RH
> Is this my problem?[/color]

Probably. I don't recall the exact state of play of functions returning
rows in 7.2, but certainly Joe Conway has greatly improved it in the
last couple of releases. You should think about updating to 7.4.
[color=blue]
> I'm trying to write a function that will return the last record in a
> sequence of logs, "last" being defined by an ORDER BY statement containing
> time stamp criteria in conjuction with other data. The function would always
> be called by trigger code.[/color]

You could try declaring the function to return the specific rowtype of
the log table, rather than the generic RECORD type. I'm quite certain
generic RECORD didn't do anything useful in 7.2. But even then, the
most useful way to call it (namely, a function call in SELECT's FROM
clause) wasn't there in 7.2.

Probably what you should do as long as you're on 7.2 is just have the
function determine and return the primary key of the correct log table
entry, and then SELECT using that key in the calling trigger functions.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

  #10  
Old November 22nd, 2005, 08:50 AM
Terry Lee Tucker
Guest
 
Posts: n/a
Default Re: Return Value of a Function

We will be upgrading soon. Thanks for all the great advice.

On Monday 02 February 2004 01:58 pm, Tom Lane wrote:[color=blue]
> Terry Lee Tucker <terry@esc1.com> writes:[color=green]
> > My version is: 7.2.3-RH
> > Is this my problem?[/color]
>
> Probably. I don't recall the exact state of play of functions returning
> rows in 7.2, but certainly Joe Conway has greatly improved it in the
> last couple of releases. You should think about updating to 7.4.
>[color=green]
> > I'm trying to write a function that will return the last record in a
> > sequence of logs, "last" being defined by an ORDER BY statement
> > containing time stamp criteria in conjuction with other data. The
> > function would always be called by trigger code.[/color]
>
> You could try declaring the function to return the specific rowtype of
> the log table, rather than the generic RECORD type. I'm quite certain
> generic RECORD didn't do anything useful in 7.2. But even then, the
> most useful way to call it (namely, a function call in SELECT's FROM
> clause) wasn't there in 7.2.
>
> Probably what you should do as long as you're on 7.2 is just have the
> function determine and return the primary key of the correct log table
> entry, and then SELECT using that key in the calling trigger functions.
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)[/color]

--
Quote: 27
"The GOP Congress seems...resigned to grit its collective teeth and
swallow a massive Medicare prescription drug benefit. Deep down,
Republican lawmakers surely lack the appetite for this fat-drenched
legislative entree. Yet they look obligated to finish it, as if leaving
their meal untouched would be impolite. Instead, they should send this
pricey dish back to the kitchen and order a snack instead."

--Deroy Murdock

Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry@esc1.com

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

  #11  
Old November 22nd, 2005, 08:54 AM
elein
Guest
 
Posts: n/a
Default Re: Return Value of a Function

I have some light examples in
http://www.varlena.com/GeneralBits/Tidbits
based on Stephan's.

The discussion in issue
http://www.varlena.com/GeneralBits/24.html
shows the differences between setof and rows.

--elein

On Mon, Feb 02, 2004 at 09:47:11AM -0800, Joe Conway wrote:[color=blue]
> Tom Lane wrote:[color=green]
> >(Joe, I couldn't find a self-contained example using a function-returning-
> >record after a bit of searching in the 7.4 docs. Surely there is one?)[/color]
>
> Looks like only one (see bottom of "Examples" section):
> http://www.postgresql.org/docs/curre...ql-select.html
> Probably could use more examples somewhere.
>
> We frequently direct people to Stephan Szabo's "Set Returning Functions"
> page on Techdocs:
> http://techdocs.postgresql.org/guide...rningFunctions
>
> Joe
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match[/color]

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.