Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eq73V-0002Jz-QY for pgsql-bugs@arkaria.postgresql.org; Mon, 26 Feb 2018 00:54:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eq73T-00051I-V8 for pgsql-bugs@arkaria.postgresql.org; Mon, 26 Feb 2018 00:54:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eq73T-000512-LQ for pgsql-bugs@lists.postgresql.org; Mon, 26 Feb 2018 00:54:11 +0000 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eq73P-0007hP-Ul for pgsql-bugs@lists.postgresql.org; Mon, 26 Feb 2018 00:54:10 +0000 Received: by mail-wr0-x241.google.com with SMTP id p104so19463312wrc.12 for ; Sun, 25 Feb 2018 16:54:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=CoKUXOaHOF4p+emkYSyc/++pJym9U0UZA5Hytw3Sh+o=; b=Mfl+4VK3/TT3ywrrIb4zQurqljhFAcJITtvuAwDpq5ROKvLxWIRTjuwNs5ONYGO9DB +iqiCMzzD5TgUwZizSP+brHetUBfMhGzUnnInKpLPh73xIPMk2+UUs6yaSz98oeV8wkk FpKZOI6T6etjLIA8JduPeERpPSGhVPAcuw4eaFN+w08DIB12cdJ9AZS7l/OmdWX7KWU/ GVCwzdxc8YrK+aIUSgX8yexBHgnG5yCOENg0IQEjhz8L/XneIphiilznySPGqecrCL0r KAA0pomiFbdCbkBUKf/r2qxnwQ/CqjyhvWD1c/zPwtD+yZ3gsMuZNVKu6e0F3ne7985Z SFow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=CoKUXOaHOF4p+emkYSyc/++pJym9U0UZA5Hytw3Sh+o=; b=DmW1zGztY1TXKnCgS8UCS7qQ1YJD4Yx40f/G85f/D3RUC3viYay7lPri9UBaJLFQac c4O3CU3bCttJnPgKofrssICCq1tfeFu2gy1jfQJdaBNQ4mcH3kHBPrUPKpN+kYERPHuz jEGGn0NrLUUVARNZUa+nd4C7J+i/dEyJKSVwaA7hTuZSZ+/lXJ8LdJ3CLDbgnB9uYXLP aRa0SdPq9AYdPZ6h7jK6Y3xfI/7Iubk4fdvbU9/47qZrRLzu5v7Uj3ynns68iSFoSuDD 0uo6azMrmKcxgosZOw/JrfDGsqnk1HkVOcflwlIHUzM8CDsoPxFIG08J10i04gCyiv6b 08wg== X-Gm-Message-State: APf1xPBkTwt9/hqv3vbyRFBkwcSUY7XiHjc9pZUO+0kR862ZMS2gtyMj OBu4cBMFma+kIhBlNAT8NA/Mhw== X-Google-Smtp-Source: AH8x227jMbAcg1V/mV6LVE5IrL8g9D1/YfVJlGR3DSYu3i6sD4Zh8BAuUeYlX2rUDeDzpg48lJgbwA== X-Received: by 10.223.177.202 with SMTP id r10mr7237530wra.97.1519606445956; Sun, 25 Feb 2018 16:54:05 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id b30sm16261198wra.34.2018.02.25.16.54.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Feb 2018 16:54:05 -0800 (PST) Subject: Re: BUG #15088: Can create subquery with duplicate column names To: tudorb@gmail.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <151960525439.21246.8882390517759748280@wrigleys.postgresql.org> From: Tomas Vondra Message-ID: Date: Mon, 26 Feb 2018 01:54:01 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <151960525439.21246.8882390517759748280@wrigleys.postgresql.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/26/2018 01:34 AM, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 15088 > Logged by: Tudor Bosman > Email address: tudorb@gmail.com > PostgreSQL version: 9.5.11 > Operating system: Ubuntu 16.04 > Description: > > This may not be a bug, but you can create a subquery that has ambiguous > column names, and then you have no way to disambiguate between them, as the > originating table names are no longer in scope. > > tudor=# create table t1 (x integer); > CREATE TABLE > tudor=# insert into t1 values (1); > INSERT 0 1 > tudor=# create table t2 (x integer); > CREATE TABLE > tudor=# insert into t2 values (2); > INSERT 0 1 > tudor=# select * from t1 cross join t2; > x | x > ---+--- > 1 | 2 > (1 row) > tudor=# select t1.x, t2.x from t1 cross join t2; > x | x > ---+--- > 1 | 2 > (1 row) > tudor=# select * from (select t1.x, t2.x from t1 cross join t2) a; > x | x > ---+--- > 1 | 2 > (1 row) > > ... and the result now has two columns named x, and I can't tell them > apart: > > tudor=# select t1.x from (select t1.x, t2.x from t1 cross join t2) a; > ERROR: missing FROM-clause entry for table "t1" > LINE 1: select t1.x from (select t1.x, t2.x from t1 cross join t2) a... > Not really. That error means the "t1" table name is unknown at that part of the plan, because you've the subselect has alias "a". You might also write it like this: select t1.x from (select t1.x, t2.x from t1 cross join t2) AS a; but the "AS" keyword is optional. So the "t1.x" reference is incorrect, and the database complains about it. You should have written the query like this: select a.x from (select t1.x, t2.x from t1 cross join t2) a; at which point you'll get test=# select a.x from (select t1.x, t2.x from t1 cross join t2) a; ERROR: column reference "x" is ambiguous LINE 1: select a.x from (select t1.x, t2.x from t1 cross join t2) a; ^ which seems rather reasonable, I guess. The point is, you'll get an error if (and only if) the database can't resolve any reference. I wonder if SQL standard says something about this behavior, but if I had to be I'd put my money on "our" behavior. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services