Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKbrY-0002Cm-PG for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Apr 2019 04:56:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hKbqX-000229-N3 for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Apr 2019 04:55:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKbqX-00021q-Fi for pgsql-bugs@lists.postgresql.org; Sun, 28 Apr 2019 04:55:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKbqP-0006TQ-31 for pgsql-bugs@lists.postgresql.org; Sun, 28 Apr 2019 04:55:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x3S4tC7e006703; Sun, 28 Apr 2019 00:55:12 -0400 From: Tom Lane To: tosites2019@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15783: Fail to select with a function in FROM clause plus another table In-reply-to: <15783-37dcbcb4c20f231d@postgresql.org> References: <15783-37dcbcb4c20f231d@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 28 Apr 2019 04:09:18 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6701.1556427312.1@sss.pgh.pa.us> Date: Sun, 28 Apr 2019 00:55:12 -0400 Message-ID: <6702.1556427312@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > select m.nome socio, p.tipo, sum(c.valor * m.percentual / 100) valor > from contas_pagas('2019-04-01', '2019-04-30', 'R') c, medico m > inner join planoconta p on (c.cod_planoconta = p.cod) > where (m.percentual > 0) > group by m.nome, p.tipo > -- ERROR > -- SQL Error [42P01]: ERROR: invalid reference to FROM-clause entry for > table "c" > -- Hint: There is an entry for table "c", but it cannot be referenced from > this part of the query. > In other words, we need to use cross join instead of put all tables in FROM > clause, if one of these tables was a function that returns a table. No, this has nothing to do with whether the relations are functions or tables. You've forgotten that JOIN binds more tightly than comma in a FROM-list, so that the relations available to that ON clause are only m and p. MySQL got this wrong for many years (maybe still does?), which has encouraged a lot of confusion, but the SQL standard is entirely clear on the point. regards, tom lane