Re: SQL functions - bug? - Mailing list pgsql-sql

From Tom Lane
Subject Re: SQL functions - bug?
Date
Msg-id 12315.960220526@sss.pgh.pa.us
Whole thread Raw
In response to SQL functions - bug?  (Kovacs Zoltan Sandor <tip@pc10.radnoti-szeged.sulinet.hu>)
List pgsql-sql
Kovacs Zoltan Sandor <tip@pc10.radnoti-szeged.sulinet.hu> writes:
> There is a function "function_y(...)" which returns int4; a table z and
> two functions:

> CREATE FUNCTION function_x1() RETURNS int4 AS '
> select function_y(any_of_fields_of_table_z) from z;
> ' LANGUAGE 'SQL';

> This calls function_y(...) only with the first row of the query output of
> the select statement. Instead of this,

> CREATE FUNCTION function_x2() RETURNS int4 AS '
> select function_y(z_field_any) from z;
> select 1;
> ' LANGUAGE 'SQL';

> works properly (the important thing for me is to call function_y with
> all rows of the select query). So, the second workaround is OK, but in my
> opinion function_x1() also should call function_y(...) for as many rows as
> exist in the output. Is this a bug?

The only bug I could see in function_x1() is that perhaps the system
should raise an error if the final select of the function tries to yield
more than one tuple, rather than just stopping its evaluation after one
tuple.  (In effect, there's an implicit LIMIT 1 on that select.)

You've declared a function returning int4; that is to say, *one* int4
per call.  No more.  The behavior you are after requires a different
declaration:

regression=# CREATE FUNCTION function_x1() RETURNS SETOF int4 AS '
regression'# select f1 from int4_tbl;
regression'# ' LANGUAGE 'SQL';
CREATE
regression=# select function_x1(); ?column?
-------------          0     123456    -123456 2147483647-2147483647
(5 rows)

Functions returning sets have a lot of restrictions on them, some of
which you will no doubt find out the hard way :-(.  But the basic
feature works.
        regards, tom lane


pgsql-sql by date:

Previous
From: Jeff Hoffmann
Date:
Subject: Re: Default timestamp value
Next
From: Fabrice Scemama
Date:
Subject: Vacuum problem in my system ?