Re: order by <tablename>

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Luca Ferrari <fluca1978(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: order by <tablename>
Date: 2021-06-10 13:32:16
Message-ID: 1557628.1623331936@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Luca Ferrari <fluca1978(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ray O'Donnell 2021-06-10 13:39:55 Re: SELECT in VCHAR column for strings with TAB
Previous Message Matthias Apitz 2021-06-10 13:30:50 SELECT in VCHAR column for strings with TAB