Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sThYW-001WjK-Tt for pgsql-bugs@arkaria.postgresql.org; Tue, 16 Jul 2024 12:45:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sThYV-000RN9-C6 for pgsql-bugs@arkaria.postgresql.org; Tue, 16 Jul 2024 12:45:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sThYV-000RMz-32 for pgsql-bugs@lists.postgresql.org; Tue, 16 Jul 2024 12:45:19 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sThYS-002LG2-9A for pgsql-bugs@lists.postgresql.org; Tue, 16 Jul 2024 12:45:17 +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 46GCjF4O3319083; Tue, 16 Jul 2024 08:45:15 -0400 From: Tom Lane To: ivningman@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18542: Order by expression, that contains column from projection isn't working In-reply-to: <18542-adf5273ccfea5558@postgresql.org> References: <18542-adf5273ccfea5558@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Tue, 16 Jul 2024 09:12:36 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3319081.1721133915.1@sss.pgh.pa.us> Date: Tue, 16 Jul 2024 08:45:15 -0400 Message-ID: <3319082.1721133915@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > select z as y from > (select 1 as z) x > order by y + 1 > That query finishes with error: > ERROR: column "y" does not exist > Position: 48 > But wrapping it with select works fine: > select * from (select z as y from > (select 1 as z) x) c > order by y + 1 This is not a bug. See https://www.postgresql.org/docs/current/queries-order.html particularly "Note that an output column name has to stand alone, that is, it cannot be used in an expression". The fact that we allow this at all is a hangover from SQL92; per SQL99 and later, variables in ORDER BY should refer to input columns (that is, outputs of the FROM clause). regards, tom lane