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 1ib5Br-00058Z-2V for pgsql-bugs@arkaria.postgresql.org; Sat, 30 Nov 2019 16:01:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ib5Aq-0004He-18 for pgsql-bugs@arkaria.postgresql.org; Sat, 30 Nov 2019 16:00:44 +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 1ib5Ap-0004HX-PP for pgsql-bugs@lists.postgresql.org; Sat, 30 Nov 2019 16:00:43 +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 1ib5Am-0000yk-EP for pgsql-bugs@lists.postgresql.org; Sat, 30 Nov 2019 16:00:42 +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 xAUG0WN6019574; Sat, 30 Nov 2019 11:00:32 -0500 From: Tom Lane To: Pavel Stehule cc: martin.querleu@effisys.fr, PostgreSQL mailing lists Subject: Re: Strange query planner behavior In-reply-to: References: <798855a5-4abe-b1d0-68ab-f21966d2c3a1@effisys.fr> Comments: In-reply-to Pavel Stehule message dated "Sat, 30 Nov 2019 11:45:59 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <19572.1575129632.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Sat, 30 Nov 2019 11:00:32 -0500 Message-ID: <19573.1575129632@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Pavel Stehule writes: > so 30. 11. 2019 v 11:29 odesílatel EffiSYS / Martin Querleu < > martin.querleu@effisys.fr> napsal: >> I think the main question is whether the query planner is able to pre >> calculate subqueries with = to use the value returned to get the good query >> plan > SELECT * FROM LIVRAISON WHERE ID_MASTER = (SELECT 10) > this query is optimized as two independent queries - SELECT * FROM > LIVRAISON WHERE ID_MASTER and SELECT 10. Although "SELECT 10" has const > result, first query desn't calculate it. Postgres planner doesn't expect so > somebody will write these queries, and don't try to detect const table > results. More to the point: usually, when somebody writes something that way, it's because they *want* to hide the sub-select expression from the upper-level query. It's pretty common to use this syntax to prevent an expensive or volatile function from being recalculated multiple times, for instance. It would certainly not be that hard to pull up the expression out of a trivial scalar sub-select, but we'll reject any patch to do that, because it would make many more users unhappy than happy. If you don't want this behavior, don't write it that way. As Pavel suggests, "IN" is a reasonable alternative if you don't want to skip the "(SELECT ...)" wrapper for some reason. regards, tom lane