Re: TABLE not synonymous with SELECT * FROM?

Lists: pgsql-hackers
From: "Colin 't Hart" <colin(at)sharpheart(dot)org>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-11 09:15:54
Message-ID: CAMon-aT8Z+5yArRsENrk=L=zSRjKCKHy1g=3KkBYT+h0QPXVDw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

According to http://www.postgresql.org/docs/9.3/static/sql-select.html#SQL-TABLE

"

The command

TABLE name

is completely equivalent to

SELECT * FROM name

It can be used as a top-level command or as a space-saving syntax
variant in parts of complex queries.

"

However, this isn't true:

colin(at)corundum:~$ psql
psql (9.4devel, server 9.3.1)
Type "help" for help.

eyedb=# table x;
a
---
1
(1 row)

eyedb=# table x limit 10;
a
---
1
(1 row)

eyedb=# table x where a = 1;
ERROR: syntax error at or near "where"
LINE 1: table x where a = 1;
^
eyedb=#

I would've thought it was implemented as a shortcut for "SELECT *
FROM" at the parse level (ie encounter "TABLE" and insert "SELECT *
FROM" into the parse tree and continue), but it seems there is more to
it.

Is the documentation wrong? Or is something broken?

Cheers,

Colin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Colin 't Hart" <colin(at)sharpheart(dot)org>
Cc: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-11 14:03:45
Message-ID: 16231.1384178625@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Colin 't Hart" <colin(at)sharpheart(dot)org> writes:
> I would've thought it was implemented as a shortcut for "SELECT *
> FROM" at the parse level (ie encounter "TABLE" and insert "SELECT *
> FROM" into the parse tree and continue), but it seems there is more to
> it.

If you look at the PG grammar you'll see that "TABLE relation_expr"
appears as one variant of simple_select, which means that you can attach
WITH, ORDER BY, FOR UPDATE, or LIMIT to it. The other things you mention
are only possible in a clause that actually starts with SELECT. AFAICS,
this comports with the SQL standard's syntax specification (look at the
difference between <query specification> and <query expression>).
The comment for simple_select saith

* Note that sort clauses cannot be included at this level --- SQL requires
* SELECT foo UNION SELECT bar ORDER BY baz
* to be parsed as
* (SELECT foo UNION SELECT bar) ORDER BY baz
* not
* SELECT foo UNION (SELECT bar ORDER BY baz)
* Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
* described as part of the select_no_parens production, not simple_select.
* This does not limit functionality, because you can reintroduce these
* clauses inside parentheses.

regards, tom lane


From: "Colin 't Hart" <colinthart(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-11 14:39:01
Message-ID: CAMon-aQw9VFtqK18kiW=eDZh+9d9oDWTm0V-Pr03FRMJF3vC0g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 11 November 2013 15:03, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> "Colin 't Hart" <colin(at)sharpheart(dot)org> writes:
>> I would've thought it was implemented as a shortcut for "SELECT *
>> FROM" at the parse level (ie encounter "TABLE" and insert "SELECT *
>> FROM" into the parse tree and continue), but it seems there is more to
>> it.
>
> If you look at the PG grammar you'll see that "TABLE relation_expr"
> appears as one variant of simple_select, which means that you can attach
> WITH, ORDER BY, FOR UPDATE, or LIMIT to it. The other things you mention
> are only possible in a clause that actually starts with SELECT. AFAICS,
> this comports with the SQL standard's syntax specification (look at the
> difference between <query specification> and <query expression>).
> The comment for simple_select saith
>
> * Note that sort clauses cannot be included at this level --- SQL requires
> * SELECT foo UNION SELECT bar ORDER BY baz
> * to be parsed as
> * (SELECT foo UNION SELECT bar) ORDER BY baz
> * not
> * SELECT foo UNION (SELECT bar ORDER BY baz)
> * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
> * described as part of the select_no_parens production, not simple_select.
> * This does not limit functionality, because you can reintroduce these
> * clauses inside parentheses.

Makes sense. I had been wondering about that order by stuff too.

Methinks we should fix the documentation, something like:

The command

TABLE name

is equivalent to

SELECT * FROM name

It can be used as a top-level command or as a space-saving syntax
variant in parts of complex queries. Only the WITH, ORDER BY, LIMIT,
and Locking clauses and set operations can be used with TABLE; the
WHERE and ORDER BY clauses and any form of aggregation cannot be used.

Cheers,

Colin


From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-11 15:43:05
Message-ID: 1384184585646-5777733.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Colin 't Hart wrote
> Methinks we should fix the documentation, something like:
>
> The command
>
> TABLE name
>
> is equivalent to
>
> SELECT * FROM name
>
> It can be used as a top-level command or as a space-saving syntax
> variant in parts of complex queries. Only the WITH, ORDER BY, LIMIT,
> and Locking clauses and set operations can be used with TABLE; the
> WHERE and ORDER BY clauses and any form of aggregation cannot be used.

The paragraph is unnecessary if the Synopsis section of the SELECT
documentation is updated to correctly reflect all the valid clauses that can
be attached to TABLE. The current reading implies that you cannot attach
anything so when you said LIMIT worked I was surprised.

Also, testing seems to confirm that the allowance of LIMIT implies that
OFFSET is allowed as well.

If TABLE is allowed as a top-level command why doesn't it get its own page
in the SQL commands section? It really doesn't matter - and honestly while
I've known about it I've never actually thought to use it in actual queries
because as soon as you want to do something special you have to switch it
out for SELECT * FROM anyway - but it does seem inconsistent.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/TABLE-not-synonymous-with-SELECT-FROM-tp5777695p5777733.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.


From: cthart <colinthart(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-12 09:39:38
Message-ID: 1384249178503-5777883.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David Johnston wrote
> The paragraph is unnecessary if the Synopsis section of the SELECT
> documentation is updated to correctly reflect all the valid clauses that
> can be attached to TABLE. The current reading implies that you cannot
> attach anything so when you said LIMIT worked I was surprised.
>
> Also, testing seems to confirm that the allowance of LIMIT implies that
> OFFSET is allowed as well.
>
> If TABLE is allowed as a top-level command why doesn't it get its own page
> in the SQL commands section? It really doesn't matter - and honestly
> while I've known about it I've never actually thought to use it in actual
> queries because as soon as you want to do something special you have to
> switch it out for SELECT * FROM anyway - but it does seem inconsistent.

I'd be in favour of the first. Since it's sort-of synonymous for SELECT *
FROM it would make sense to include it on the same page to avoid having to
explain the same clauses again. But then it should be listed earlier, before
the clauses which can be used with it.

Yes, all LIMIT, OFFSET, FETCH stuff works. Also very useful in a WITH:

WITH x AS (
TABLE foo
ORDER BY colX DESC
LIMIT 10
)
...;

Note that set operations work too, and that's how I most often use it for
testing rewritten queries:
create table x as <original_query>;
create table y as <rewritten_query>;
table x except table y;
table y except table x;

Cheers,

Colin

--
View this message in context: http://postgresql.1045698.n5.nabble.com/TABLE-not-synonymous-with-SELECT-FROM-tp5777695p5777883.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.


From: "Colin 't Hart" <colinthart(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2013-11-13 09:28:07
Message-ID: CAMon-aQbbR5NfhD3hv9nyGP+bHcdbWTRx3gDNPpH41fMF=ZKiQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David et al,

How about something like this?

Cheers,

Colin

Attachment Content-Type Size
select.sgml.diff text/plain 4.6 KB

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Colin 't Hart <colinthart(at)gmail(dot)com>
Cc: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: TABLE not synonymous with SELECT * FROM?
Date: 2014-03-08 01:57:44
Message-ID: 20140308015744.GD16324@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Nov 13, 2013 at 10:28:07AM +0100, Colin 't Hart wrote:
> David et al,
>
> How about something like this?

I have applied a modified version of your patch. I didn't like the
idea of putting "SELECT" inside an OR syntax clauses --- the syntax is
already too complicated. I also didn't like moving the TABLE mention up
in the file. What I did do was to document the supported TABLE clauses,
and add some of your verbiage. Thanks.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ Everyone has their own god. +

Attachment Content-Type Size
select.diff text/x-diff 2.3 KB