Re: Hey! ORDER BY in VIEWS?

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Hey! ORDER BY in VIEWS?
Date: 2001-07-15 21:40:30
Message-ID: web-85979@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Tom,

> Hmm, I just realized that there's a bug here: let's say you have
>
> CREATE VIEW latest AS
> SELECT * FROM news ORDER BY story_timestamp DESC LIMIT 1;
>
> ie, this view gives you the latest news story. If you do
>
> SELECT * FROM latest WHERE story_text LIKE '%Joe Smith%';
>
> what you will get in 7.1 is the latest story mentioning Joe Smith,
> because the planner will push down the WHERE clause into the view's
> SELECT, where it'll be applied before the LIMIT. Perhaps some would
> call this useful behavior, but I'd say it has to be considered a bug
> :-(.
> The outer WHERE should not cause the VIEW to return a different row
> than it otherwise would.

Now you see why SQL92 doesn't support ORDER BY in views. ;-)

Why, exactly, do we need to support ORDER BY ... LIMIT in VIEWS, anyway?
I'm frankly unclear on the utility of this ... I make SQL jump through
some pretty fancy hoops, myself (4 section UNION query with nested
subselects, anyone?) and I've never needed ... or wanted ... a view with
a built-in LIMIT.

If we gotta have 'em, though, Tom, you'd have to code in an exception to
the VIEW optimizer that doesn't push down WHERE clauses if the VIEW has
an ORDER BY ... LIMIT statement. Sure you wanna get into this?

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-07-15 21:50:23 Re: Hey! ORDER BY in VIEWS?
Previous Message Tom Lane 2001-07-15 21:14:43 Re: Hey! ORDER BY in VIEWS?