Re: order by - Mailing list pgsql-general

From Tom Lane
Subject Re: order by
Date
Msg-id 1557628.1623331936@sss.pgh.pa.us
Whole thread Raw
In response to order by  (Luca Ferrari <fluca1978@gmail.com>)
List pgsql-general
Luca Ferrari <fluca1978@gmail.com> writes:
> The ORDER BY rejects non existent columns (right) but accepts the
> table itself as an ordering expression.

As others have noted, this is basically taking the table name as a
whole-row variable, and then sorting per the rules for composite
types.  I write to point out that you can often get some insight into
what the parser thought it was doing by examining the reverse-listing
for the query.  The simplest way to do that is to create a view and
examine the view:

regression=# create view v as  
regression-# select * from t order by t;
CREATE VIEW
regression=# \d+ v
                              View "public.v"
 Column |  Type   | Collation | Nullable | Default | Storage | Description 
--------+---------+-----------+----------+---------+---------+-------------
 v      | integer |           |          |         | plain   | 
View definition:
 SELECT t.v
   FROM t
  ORDER BY t.*;

The form "t.*" is a more explicit way to write a whole-row variable.

(IIRC, accepting it without "*" is a PostQUEL-ism that we've never
got rid of.  I think that with "*", there's at least some support
for the concept in the SQL standard.  But I'm insufficiently
caffeinated to want to go digging for that.)

            regards, tom lane



pgsql-general by date:

Previous
From: Matthias Apitz
Date:
Subject: SELECT in VCHAR column for strings with TAB
Next
From: Ray O'Donnell
Date:
Subject: Re: SELECT in VCHAR column for strings with TAB