Re: Nested query performance issue - Mailing list pgsql-performance

From Glenn Maynard
Subject Re: Nested query performance issue
Date
Msg-id d18085b50904092311i42fcd2fbwbe39cb9361a989f4@mail.gmail.com
Whole thread Raw
In response to Re: Nested query performance issue  (Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>)
Responses Re: Nested query performance issue
List pgsql-performance
On Thu, Apr 9, 2009 at 7:29 AM, Heikki Linnakangas
<heikki.linnakangas@enterprisedb.com> wrote:
> CREATE FUNCTION topnscores(game_id int , n int) RETURNS SETOF score LANGUAGE
> SQL AS $$
> SELECT * FROM score s WHERE s.game_id = $1 ORDER BY score DESC LIMIT $2
> $$;
>
> SELECT (sub.ts).id, (sub.ts).score, (sub.ts).game_id
> FROM (SELECT topnscores(g.id, 5) ts FROM game g) sub;

The inner query:

SELECT topnscores(g.id, 5) ts FROM game g

http://www.postgresql.org/docs/8.3/static/xfunc-sql.html says this is
deprecated (though no deprecation warning is being generated):

> Currently, functions returning sets can also be called in the select list of a query. For each row that the query
generatesby itself, the function returning set is invoked, and an output row is generated for each element of the
function'sresult set. Note, however, that this capability is deprecated and might be removed in future releases. 

It doesn't say how else to write this, though, and it's not obvious to
me.  "SELECT ts.* FROM topnscores(g.id, 5) AS ts, game g" doesn't work
("function expression in FROM cannot refer to other relations of same
query level").  Is there an equivalent way to do this so I won't have
deprecation looming over my back?  I'm likely to become very dependent
on this pattern.

--
Glenn Maynard

pgsql-performance by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Using IOZone to simulate DB access patterns
Next
From: Mark Kirkwood
Date:
Subject: Re: Using IOZone to simulate DB access patterns