Re: SQL functions - bug?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kovacs Zoltan Sandor <tip(at)pc10(dot)radnoti-szeged(dot)sulinet(dot)hu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL functions - bug?
Date: 2000-06-05 15:55:26
Message-ID: 12315.960220526@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Kovacs Zoltan Sandor <tip(at)pc10(dot)radnoti-szeged(dot)sulinet(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2000-06-05 16:19:56 Re: Vacuum problem in my system ?
Previous Message Jeff Hoffmann 2000-06-05 15:52:38 Re: Default timestamp value