473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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: te***@esc1.com

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

Nov 22 '05 #1
10 2548
On Monday 02 February 2004 14:36, Terry Lee Tucker wrote:

I may have several other things wrong with this function, so my real
question is, "Can I return a value of type RECORD?"


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 ma*******@postgresql.org

Nov 22 '05 #2
Terry Lee Tucker <te***@esc1.com> writes:
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
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.
I may have several other things wrong with this function, so my real question
is, "Can I return a value of type RECORD?"


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 ma*******@postgresql.org

Nov 22 '05 #3
Richard Huxton <de*@archonet.com> writes:
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');


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

Nov 22 '05 #4
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:
Terry Lee Tucker <te***@esc1.com> writes:
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


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.
I may have several other things wrong with this function, so my real
question is, "Can I return a value of type RECORD?"


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


--

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

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

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

Nov 22 '05 #5
Tom Lane wrote:
(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?)


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

Nov 22 '05 #6
Joe Conway <ma**@joeconway.com> writes:
Tom Lane wrote:
(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?)
Looks like only one (see bottom of "Examples" section):
http://www.postgresql.org/docs/curre...ql-select.html
Probably could use more examples somewhere.


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

Nov 22 '05 #7
Terry Lee Tucker wrote:
My version is: 7.2.3-RH


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

Nov 22 '05 #8
Terry Lee Tucker <te***@esc1.com> writes:
My version is: 7.2.3-RH
Is this my problem?
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.
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.


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 ma*******@postgresql.org)

Nov 22 '05 #9
We will be upgrading soon. Thanks for all the great advice.

On Monday 02 February 2004 01:58 pm, Tom Lane wrote:
Terry Lee Tucker <te***@esc1.com> writes:
My version is: 7.2.3-RH
Is this my problem?


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.
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.


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 ma*******@postgresql.org)


--
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: te***@esc1.com

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

Nov 22 '05 #10
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:
Tom Lane wrote:
(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?)


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


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

http://archives.postgresql.org

Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.