dbTalk Databases Forums  

Re: [BUGS] [GENERAL] PL/pgSQL Function Help

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss Re: [BUGS] [GENERAL] PL/pgSQL Function Help in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] [GENERAL] PL/pgSQL Function Help - 12-16-2005 , 04:10 PM






Michael Fuhr <mike (AT) fuhr (DOT) org> writes:
Quote:
Here's a simplified version:

CREATE TYPE test_type AS (x integer);

CREATE FUNCTION test() RETURNS test_type AS $$
DECLARE
rec record;
BEGIN
SELECT INTO rec 1;
RETURN rec;
END;
$$ LANGUAGE plpgsql;

SELECT test();

I get an assertion failure if rec is declared as a record but not
if it's declared as a test_type. And only in 8.0.5, not in 8.1.1
or 8.2devel.
I find that the lack of an assertion failure in 8.1 is a happenstance of
unrelated changes. The problem is that plpgsql is making no effort at
all to ensure that the record type it returns is the same as it's
declared to return. Here's an interesting variant in CVS tip:

regression=# CREATE TYPE test_type AS (x integer);
CREATE TYPE
regression=# create or replace FUNCTION test() RETURNS test_type AS $$
regression$# DECLARE rec record;
regression$# BEGIN
regression$# SELECT INTO rec 1.1;
regression$# RETURN rec;
regression$# END;
regression$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
regression=# SELECT test();
test
-------
(1.1)
(1 row)

Bit of an odd-looking integer, eh? The lack of a crash is only because
we're not doing anything much with the function result except displaying
it, and since record_out only looks at the record value itself, it
doesn't have any preconceived ideas about what it will find. You can
still get the assert failure from toast_flatten_tuple_attribute though:

regression=# create table tt(f1 test_type);
CREATE TABLE
regression=# insert into tt values(test());
server closed the connection unexpectedly

We need to fix plpgsql to ensure that what it returns is of the expected
record type.

regards, tom lane

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


Reply With Quote
  #2  
Old   
Jim C. Nasby
 
Posts: n/a

Default Re: [BUGS] [GENERAL] PL/pgSQL Function Help - 12-16-2005 , 06:06 PM






On Fri, Dec 16, 2005 at 05:10:11PM -0500, Tom Lane wrote:
Quote:
We need to fix plpgsql to ensure that what it returns is of the expected
record type.
Should a test be added to regression for this? That's something I should
be able to do...
--
Jim C. Nasby, Sr. Engineering Consultant jnasby (AT) pervasive (DOT) com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

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

http://archives.postgresql.org


Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] [GENERAL] PL/pgSQL Function Help - 01-03-2006 , 04:50 PM



I wrote:
Quote:
Michael Fuhr <mike (AT) fuhr (DOT) org> writes:
I get an assertion failure if rec is declared as a record but not
if it's declared as a test_type. And only in 8.0.5, not in 8.1.1
or 8.2devel.

I find that the lack of an assertion failure in 8.1 is a happenstance of
unrelated changes. The problem is that plpgsql is making no effort at
all to ensure that the record type it returns is the same as it's
declared to return.
Patches committed to check this in 8.0 and up. 7.4 and 7.3 seem not to
have an issue.

regards, tom lane

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


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.