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

View: New views
8 Messages — Rating Filter:   Alert me  

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

by Denis BUCHER :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


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

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

by Tom Lane-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Denis BUCHER <dbucherml@...> 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

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

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

by Denis BUCHER :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Tom,

Tom Lane a écrit :

> Denis BUCHER <dbucherml@...> 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
------------------------------
 tableoid
 cmax
 xmax
 cmin
 xmin
 ctid
 sn_id
 sn
 no_client
 no_art_bw
 sn_fc_date
 ........pg.dropped.6........
 ........pg.dropped.7........
 desc_fr
 sn_cm_date
 no_facture
 no_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

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

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

by Richard Huxton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tom Lane wrote:
> Denis BUCHER <dbucherml@...> 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

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

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

by Denis BUCHER :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tom,

Another question :

Tom Lane a écrit :

> Denis BUCHER <dbucherml@...> 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

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

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

by Tom Lane-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Richard Huxton <dev@...> 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

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

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

by Tom Lane-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Denis BUCHER <dbucherml@...> 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

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

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

by Denis BUCHER :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tom Lane a écrit :

> Denis BUCHER <dbucherml@...> 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

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