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 1lRd5h-0006bF-Q0 for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Mar 2021 15:49:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRd5g-0002dB-Og for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Mar 2021 15:49:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRd5g-0002d4-HR for pgsql-hackers@lists.postgresql.org; Wed, 31 Mar 2021 15:49:08 +0000 Received: from forward2-smtp.messagingengine.com ([66.111.4.226]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRd5e-00005j-24 for pgsql-hackers@lists.postgresql.org; Wed, 31 Mar 2021 15:49:07 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailforward.nyi.internal (Postfix) with ESMTP id 069411941277; Wed, 31 Mar 2021 11:49:05 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 31 Mar 2021 11:49:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=N3Ya5GsQ1+k/ZAKleRXm0hwrxph9BTgWOvLbVLkSw x0=; b=rP0N2BSvRo/FfEauClsJjP8tktew/Cs0egd9fGI/zc8VoVBesklNaHKm1 m6ExNRLZmMKoPApGW3mjL/GA6sJMxxySiuiI+bnaVHSxRX0c6n1uc9ApzO3wUusR joI/+OIIh4Szw+ci6H0rvWaqXuvR63+qh3Dpgntiyn6+scZ7NKKKFW4SbmPHkKeU YUJJlKzQlnD3jAN8PdO+SDx3wXClACLugSOIGZh7gtMjBlzlkAwxhptMYTKRzz7v VodL4H/eVc8EEOWoqthY9clOQ7yospY2OshXMRj2hMHAPeWv30nZhspvou6HMarY NEVpS+5tQ5QbZZArtdR5w2n6haLoQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudeivddgleejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefheenucfhrhhomheprfgvthgv rhcugfhishgvnhhtrhgruhhtuceophgvthgvrhdrvghishgvnhhtrhgruhhtsedvnhguqh hurggurhgrnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeefheelfeduveejieehkeel ueffudejgfffffehieduvdfhleegkeekgefgvedukeenucfkphepledurddugedrvdefvd drieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehp vghtvghrrdgvihhsvghnthhrrghuthesvdhnughquhgrughrrghnthdrtghomh X-ME-Proxy: Received: from laptop206oxuk.local (p5b0ee806.dip0.t-ipconnect.de [91.14.232.6]) by mail.messagingengine.com (Postfix) with ESMTPA id 6669E1080064; Wed, 31 Mar 2021 11:49:03 -0400 (EDT) Subject: Re: Allow an alias to be attached directly to a JOIN ... USING To: Tom Lane Cc: David Steele , Georgios Kokolatos , pgsql-hackers@lists.postgresql.org References: <454638cf-d563-ab76-a585-2564428062af@2ndquadrant.com> <88018eae-d949-be66-7fec-924213213b49@technowledgy.de> <160502135578.7362.9015197269135176959.pgcf@coridan.postgresql.org> <4c4727e2-2797-7f02-3529-f504e843d2ea@2ndquadrant.com> <19cab8ea-7106-2017-e018-d65feffb90ad@pgmasters.net> <64f2b50d-861f-ee4f-4272-e668cee5a154@2ndquadrant.com> <785329.1616455091@sss.pgh.pa.us> From: Peter Eisentraut Message-ID: <3d945d71-25d8-22b9-cc4e-aaad817eeda3@2ndquadrant.com> Date: Wed, 31 Mar 2021 17:49:01 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <785329.1616455091@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 23.03.21 00:18, Tom Lane wrote: > However, ParseNamespaceItem as it stands needs some help for this. > It has a wired-in assumption that p_rte->eref describes the table > and column aliases exposed by the nsitem. 0001 below fixes this by > creating a separate p_names field in an nsitem. (There are some > comments in 0001 referencing JOIN USING aliases, but no actual code > for the feature.) That saves one indirection in common code paths, > so it's possibly a win on its own. Then 0002 is your patch rebased > onto that infrastructure, and with some cleanup of my own. Makes sense. I've committed it based on that. > Speaking of decompiled views, I feel like ruleutils.c could do with > a little more work to teach it that these aliases are available. > Right now, it resorts to ugly workarounds: Yeah, the whole has_dangerous_join_using() can probably be unwound and removed with this. But it's a bit of work. > One other cosmetic thing is that this: > > regression=# select tu.* from (t1 join t2 using(a) as tu) tx; > ERROR: missing FROM-clause entry for table "tu" > LINE 1: select tu.* from (t1 join t2 using(a) as tu) tx; > ^ > > is a relatively dumb error message, compared to > > regression=# select t1.* from (t1 join t2 using(a) as tu) tx; > ERROR: invalid reference to FROM-clause entry for table "t1" > LINE 1: select t1.* from (t1 join t2 using(a) as tu) tx; > ^ > HINT: There is an entry for table "t1", but it cannot be referenced from this part of the query. > > I didn't look into why that isn't working, but maybe errorMissingRTE > needs to trawl all of the ParseNamespaceItems not just the RTEs. Yes, I've prototyped that and it would have the desired effect. Might need some code rearranging, like either change searchRangeTableForRel() to not return an RTE or make a similar function for ParseNamespaceItem search. Needs some more thought. I have left a test case in that would show any changes here.