pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Luca Ferrari <fluca1978@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: order by <tablename>
Date: Thu, 10 Jun 2021 09:32:16 -0400
Message-ID: <1557628.1623331936@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKoxK+5uLxLGo3CSeYX41mxiATbUgQ3P-KtU2FrB5X7ju1=QcQ@mail.gmail.com>
References: <CAKoxK+5uLxLGo3CSeYX41mxiATbUgQ3P-KtU2FrB5X7ju1=QcQ@mail.gmail.com>

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





view thread (6+ messages)

Message-ID: <1557628.1623331936@sss.pgh.pa.us>
Permalink:  ../1557628.1623331936@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1557628.1623331936@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, fluca1978@gmail.com
  Subject: Re: order by <tablename>
  In-Reply-To: <1557628.1623331936@sss.pgh.pa.us>
  References: <CAKoxK+5uLxLGo3CSeYX41mxiATbUgQ3P-KtU2FrB5X7ju1=QcQ@mail.gmail.com> <1557628.1623331936@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox