Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lrKnM-0002dj-K5 for pgsql-general@arkaria.postgresql.org; Thu, 10 Jun 2021 13:32:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lrKnL-000163-Jx for pgsql-general@arkaria.postgresql.org; Thu, 10 Jun 2021 13:32:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lrKnL-00013A-Aa for pgsql-general@lists.postgresql.org; Thu, 10 Jun 2021 13:32:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lrKnF-0002sy-0i for pgsql-general@lists.postgresql.org; Thu, 10 Jun 2021 13:32:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 15ADWGoI1557629; Thu, 10 Jun 2021 09:32:16 -0400 From: Tom Lane To: Luca Ferrari cc: pgsql-general Subject: Re: order by In-reply-to: References: Comments: In-reply-to Luca Ferrari message dated "Thu, 10 Jun 2021 10:03:11 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1557627.1623331936.1@sss.pgh.pa.us> Date: Thu, 10 Jun 2021 09:32:16 -0400 Message-ID: <1557628.1623331936@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Luca Ferrari 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