Thread: Problem with return type of function ??? (corrected)

Problem with return type of function ??? (corrected)

From
Denis BUCHER
Date:
Hello, (CORRECTED VERSION OF MY PREVIOUS EMAIL)

I have a strange problem, because it worked in a fonction for a table,
and now I created the same (?) function for another table and it doesn't
work...

The function is accepted but at runtime I get :

ERREUR:  wrong record type supplied in RETURN NEXT
CONTEXTE : PL/pgSQL function "find_sn_live" line 26 at return next

Does someone maybe knows what it could be ?

This is (a part of) my function :

> CREATE OR REPLACE FUNCTION rma.find_sn_live (varchar(30)) RETURNS SETOF rma.serial_number
>     AS $_$
> DECLARE
>    serialnumber ALIAS FOR $1;
>    row RECORD;
> BEGIN
> 
> FOR row IN
> SELECT * FROM rma.serial_number WHERE sn=serialnumber
> LOOP
> RETURN NEXT row;
> END LOOP;
> 
> END;
> $_$
>     LANGUAGE plpgsql STRICT;


Thanks a lot for any help !

Denis


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



Re: Problem with return type of function ??? (corrected)

From
Tom Lane
Date:
Denis BUCHER <dbucherml@hsolutions.ch> writes:
> I have a strange problem, because it worked in a fonction for a table,
> and now I created the same (?) function for another table and it doesn't
> work...

> The function is accepted but at runtime I get :

> ERREUR:  wrong record type supplied in RETURN NEXT
> CONTEXTE : PL/pgSQL function "find_sn_live" line 26 at return next

Does that table have any dropped columns?  If you don't remember
whether you ever dropped any columns, a quick look into pg_attribute
will tell you:
select attname from pg_attribute where attrelid = 'rma.serial_number'::regclass;

plpgsql isn't tremendously good with rowtypes that contain dropped
columns.  I believe this particular case is fixed in CVS HEAD, but the
patch was a bit invasive and won't get back-ported to existing releases.
The workaround is to drop and recreate the table without any dropped
columns.
        regards, tom lane


Re: Problem with return type of function ??? (corrected)

From
Denis BUCHER
Date:
Hello Tom,

Tom Lane a écrit :
> Denis BUCHER <dbucherml@hsolutions.ch> writes:
>> I have a strange problem, because it worked in a fonction for a table,
>> and now I created the same (?) function for another table and it doesn't
>> work...
> 
>> The function is accepted but at runtime I get :
> 
>> ERREUR:  wrong record type supplied in RETURN NEXT
>> CONTEXTE : PL/pgSQL function "find_sn_live" line 26 at return next
> 
> Does that table have any dropped columns?  If you don't remember
> whether you ever dropped any columns, a quick look into pg_attribute
> will tell you:
> select attname from pg_attribute where attrelid = 'rma.serial_number'::regclass;

Yes !
          attname

------------------------------tableoidcmaxxmaxcminxminctidsn_idsnno_clientno_art_bwsn_fc_date........pg.dropped.6................pg.dropped.7........desc_frsn_cm_dateno_factureno_commande
(17 lignes)

> plpgsql isn't tremendously good with rowtypes that contain dropped
> columns.  I believe this particular case is fixed in CVS HEAD, but the
> patch was a bit invasive and won't get back-ported to existing releases.
> The workaround is to drop and recreate the table without any dropped
> columns.

OH, I see...

Thanks a lot, I will try this later today when nobody uses the
application...


Denis


Re: Problem with return type of function ??? (corrected)

From
Richard Huxton
Date:
Tom Lane wrote:
> Denis BUCHER <dbucherml@hsolutions.ch> writes:
> 
> Does that table have any dropped columns?  If you don't remember
> whether you ever dropped any columns, a quick look into pg_attribute
> will tell you:
> select attname from pg_attribute where attrelid = 'rma.serial_number'::regclass;
> 
> plpgsql isn't tremendously good with rowtypes that contain dropped
> columns.  

I thought that only applied to columns dropped after the function was
defined. Live and learn.

--  Richard Huxton Archonet Ltd


Re: Problem with return type of function ??? (corrected)

From
Denis BUCHER
Date:
Hi Tom,

Another question :

Tom Lane a écrit :
> Denis BUCHER <dbucherml@hsolutions.ch> writes:
>> I have a strange problem, because it worked in a fonction for a table,
>> and now I created the same (?) function for another table and it doesn't
>> work...
> 
>> The function is accepted but at runtime I get :
> 
>> ERREUR:  wrong record type supplied in RETURN NEXT
>> CONTEXTE : PL/pgSQL function "find_sn_live" line 26 at return next
> 
> Does that table have any dropped columns?  If you don't remember
> whether you ever dropped any columns, a quick look into pg_attribute
> will tell you:
> select attname from pg_attribute where attrelid = 'rma.serial_number'::regclass;
> 
> plpgsql isn't tremendously good with rowtypes that contain dropped
> columns.  I believe this particular case is fixed in CVS HEAD, but the
> patch was a bit invasive and won't get back-ported to existing releases.
> The workaround is to drop and recreate the table without any dropped
> columns.

To do this it will be a little complicated because of table
dependencies... And it could bug again at the next DROP COLUMN... Is
there a way to change my function (RETURN SETOF part) to specify the
column names/types ?

Thanks a lot again

Denis


Re: Problem with return type of function ??? (corrected)

From
Tom Lane
Date:
Richard Huxton <dev@archonet.com> writes:
> Tom Lane wrote:
>> plpgsql isn't tremendously good with rowtypes that contain dropped
>> columns.  

> I thought that only applied to columns dropped after the function was
> defined. Live and learn.

There are/were some variants that go away if you recreate the function
or start a fresh session (to clear the compiled-function cache).  Not
all, unfortunately.
        regards, tom lane


Re: Problem with return type of function ??? (corrected)

From
Tom Lane
Date:
Denis BUCHER <dbucherml@hsolutions.ch> writes:
> To do this it will be a little complicated because of table
> dependencies... And it could bug again at the next DROP COLUMN... Is
> there a way to change my function (RETURN SETOF part) to specify the
> column names/types ?

No, not really.  You could maybe un-drop the columns with some manual
surgery on pg_attribute, but it doesn't seem like that's going to lead
to a nice solution.

If you were really desperate you could try back-porting the patch:
http://archives.postgresql.org/pgsql-committers/2009-08/msg00068.php
but I wouldn't want to vouch for its safety, considering it hasn't
been through a beta test cycle yet.
        regards, tom lane


Re: Problem with return type of function ??? (corrected)

From
Denis BUCHER
Date:
Tom Lane a écrit :
> Denis BUCHER <dbucherml@hsolutions.ch> writes:
>> To do this it will be a little complicated because of table
>> dependencies... And it could bug again at the next DROP COLUMN... Is
>> there a way to change my function (RETURN SETOF part) to specify the
>> column names/types ?
> 
> No, not really.  You could maybe un-drop the columns with some manual
> surgery on pg_attribute, but it doesn't seem like that's going to lead
> to a nice solution.
> 
> If you were really desperate you could try back-porting the patch:
> http://archives.postgresql.org/pgsql-committers/2009-08/msg00068.php
> but I wouldn't want to vouch for its safety, considering it hasn't
> been through a beta test cycle yet.

Finally there was no real dependencies of that table, and I've found out
that the "dependency" was my function ! Using SETOF (table) makes
impossible to DROP the table. Therefore I DROP my function and was able
to follow you advice, and it worked perfectly !

Thanks a lot for your help (as well as Richard's)

Denis