pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: martin.querleu@effisys.fr, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: Strange query planner behavior
Date: Sat, 30 Nov 2019 11:00:32 -0500
Message-ID: <19573.1575129632@sss.pgh.pa.us> (raw)
In-Reply-To: <CAFj8pRB8H2EhPBSNviUManps9c_3it-0gNh347i12+BgnYxB=Q@mail.gmail.com>
References: <798855a5-4abe-b1d0-68ab-f21966d2c3a1@effisys.fr>
	<CAFj8pRBYY9D0UW44=AKeBG8BJXq4NHA4WqAofLDQWAUFCfLQXA@mail.gmail.com>
	<CAFj8pRB4CLf-m3KMjAiQ3QUpLCX38wiJTKzzuyyAE6MCOeff4w@mail.gmail.com>
	<b4834a66-c023-01c2-3b11-fb4af0d1e262@effisys.fr>
	<CAFj8pRB8H2EhPBSNviUManps9c_3it-0gNh347i12+BgnYxB=Q@mail.gmail.com>

Pavel Stehule <pavel.stehule@gmail.com> 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





view thread (10+ messages)  latest in thread

Message-ID: <19573.1575129632@sss.pgh.pa.us>
Permalink:  ../19573.1575129632@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/19573.1575129632@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pavel.stehule@gmail.com
  Subject: Re: Strange query planner behavior
  In-Reply-To: <19573.1575129632@sss.pgh.pa.us>
  References: <798855a5-4abe-b1d0-68ab-f21966d2c3a1@effisys.fr> <CAFj8pRBYY9D0UW44=AKeBG8BJXq4NHA4WqAofLDQWAUFCfLQXA@mail.gmail.com> <CAFj8pRB4CLf-m3KMjAiQ3QUpLCX38wiJTKzzuyyAE6MCOeff4w@mail.gmail.com> <b4834a66-c023-01c2-3b11-fb4af0d1e262@effisys.fr> <CAFj8pRB8H2EhPBSNviUManps9c_3it-0gNh347i12+BgnYxB=Q@mail.gmail.com> <19573.1575129632@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox