Re: Why LIMIT and OFFSET are commutative

From: Lew <lew(at)lwsc(dot)ehost-services(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why LIMIT and OFFSET are commutative
Date: 2007-11-27 14:26:34
Message-ID: mM-dnTN3T5WGtNHanZ2dnUVZ_uyinZ2d@comcast.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus wrote:
> Why Postgres does not throw error when SELECT ... LIMIT ... OFFSET is used ?
> That's not sql and should cause error.

Good point, it is not standard SQL. Postgres varies from the standard in
several ways, some of them useful. (INSERT ... RETURNING is a useful one.)
No SQL RDBMS follows the standard, AFAIK. LIMIT / OFFSET is a common
enhancement, but being non-standard, appears in different forms in different
dialects.

However, the LIMIT / OFFSET idiom *is* Postgres SQL, it *is* documented and
therefore it should *not* cause an error when used. That would just be whacky.

Given that the idiom is documented and does work in Postgres's particular
dialect of SQL, it then must work exactly as described in Postgres's
documentation of its particular dialect of SQL. And, hey, presto! It does.

I agree that they should document the extra flexibility it provides in the
order of its clauses. I find it hard to assess such flexibility as a bad
thing; I tend to appreciate it.

They do hint at it in one place - Section VI, Chapter I, _SELECT_, the LIMIT
clause,
<http://www.postgresql.org/docs/8.2/interactive/sql-select.html#SQL-LIMIT>
> The LIMIT clause consists of two independent sub-clauses:
>
> LIMIT { /count/ | ALL }
> OFFSET /start/
>
> /count/ specifies the maximum number of rows to return,
> while /start/ specifies the number of rows to skip
> before starting to return rows. When both are specified,
> /start/ rows are skipped before starting to count the
> /count/ rows to be returned.

The word "independent" indicates at least the either may appear without the
other, and implies that the order of the clauses doesn't matter.

Note also that the behavior of the clause is *exactly* as documented. You
really have to stop resisting that, now, and accept it.

--
Lew

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Fernando Xavier 2007-11-27 14:33:42 Connection idle broken
Previous Message Selena Deckelmann 2007-11-27 14:23:15 Re: POLL: Women-sized t-shirts for PostgreSQL