Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W2Y0T-0002aU-6Q for pgsql-bugs@arkaria.postgresql.org; Mon, 13 Jan 2014 03:16:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W2Y0S-0005vK-MK for pgsql-bugs@arkaria.postgresql.org; Mon, 13 Jan 2014 03:16:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W2RBg-00083D-HB for pgsql-bugs@postgresql.org; Sun, 12 Jan 2014 19:59:12 +0000 Received: from mail-pd0-f176.google.com ([209.85.192.176]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W2RBa-000169-GS for pgsql-bugs@postgresql.org; Sun, 12 Jan 2014 19:59:11 +0000 Received: by mail-pd0-f176.google.com with SMTP id r10so2605565pdi.7 for ; Sun, 12 Jan 2014 11:59:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:message-id:date:from:user-agent :mime-version:to:cc:subject:references:in-reply-to:content-type; bh=9QNLaRtNZ7fU5cP6SYT3G5nI1jG1pjGY/J+4e9MtvJU=; b=PAQ50VnO9oPvovkOg4tHGXqDcbSG7yZyraXnce9Fx5s2lrJ/9MmO1xYmAvZcxqXXor wF+TP//iNWoV9N+dH0Q/RD4SbdeGja0cWz19y1Z/beaRlYwgQ0Pf67FJkLow20EwPBMB HiPgkLG1spbdcRQY0ETVBCaKpVu0I0cPe80a7ShnCpXefOPknWtQxwiQioChWMgfxkn6 zVFbe7NR4ZbNWe0dsPy28q3SnN7+RQiBjrj852PNGK3qR6bO0anxU6eGtKkrjTPl3v5L +QE/lgJM9XZXSESOar0kAYFODPfxGWeqix7RxJRW1/tKLyAx4uJ/0HPdZZMOJZnLHnMx EvAw== X-Gm-Message-State: ALoCoQlfmbSr+T7nN86YwM5XPFiR9FtBTIBpHSBLQytdoAfCPbQddL2drMqPpsHp6ZYM0HNNGVbS X-Received: by 10.68.237.228 with SMTP id vf4mr25440745pbc.131.1389556743445; Sun, 12 Jan 2014 11:59:03 -0800 (PST) Received: from [172.21.20.157] ([182.236.125.191]) by mx.google.com with ESMTPSA id gg10sm32613969pbc.46.2014.01.12.11.59.01 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 12 Jan 2014 11:59:02 -0800 (PST) Message-ID: <52D2F403.1050104@tracmap.co.nz> Date: Mon, 13 Jan 2014 08:58:59 +1300 From: Martin Junek User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Tom Lane CC: pgsql-bugs@postgresql.org Subject: Re: BUG #8771: Query execution plan broken after upgrade from 9.1.9 References: <20985.1389403922@sss.pgh.pa.us> In-Reply-To: <20985.1389403922@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary="------------090007060108080405030709" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------090007060108080405030709 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi Tom, thanks for the explanation. However, it sounds like a terrible side effect of the change you mentioned. Also in this example the function is not volatile. Couple more questions: - why it works as expected when I use " where id in (50000)" and does not work if used "where id in (select 50000)"? Will it still work that way in the foreseeable future? - why the function gets executed when the column is not in the select list at all? i.e. when I use "select id from...."? re "optimizing the slow function" - this example is very simplified just to demonstrate the problem (so the function is intentionally slow). In our case it's a big view containing tens of subqueries. The view would contain many records but we only need 50 (=one page of results) so our query was designed to evaluate all the subqueries only for the 50 records. Now if we upgrade to 9.1.11... you see the problem. In our case there's a workaround (split the query into two separate queries), but it just feels wrong. I'm also surprised that other users haven't encountered this issue. I'd appreciate if you reconsider this to be a bug in the query optimizer. Thanks for your help. Martin. On 11/01/14 14:32, Tom Lane wrote: > martin.junek@tracmap.co.nz writes: >> after upgrading from 9.1.9 to 9.1.11 one of our queries started to run >> terribly slow (went from few miliseconds to hours). The problem is better >> explained in the following SQL snippet (which is a very simplified version >> of the problem). If you run it on 9.1.9, all the SELECTs evaluate in few >> miliseconds, if you run it on 9.1.11, it will take probably hours (I didn't >> have the patience to wait for it). > I believe this is a result of this 9.1.11 change: > > * Avoid flattening a subquery whose SELECT list contains a volatile function wrapped inside a sub-SELECT (Tom Lane) > > This avoids unexpected results due to extra evaluations of the volatile function. > > full details of which can be found here: > http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=af38d140c71c21eda422fedc838525525d155cac > > That's an intentional change that is not going to get undone. If you > don't like the results, I'd suggest fixing the "slow function" to get it > marked as stable or immutable as appropriate. (The given example would be > best marked stable, but I suppose it's just an example and not your real > problem function.) When it's marked volatile, as this is by default, > that discourages the planner from rearranging the query in ways that would > change the number of function executions from what a naive implementation > would suggest. 9.1.11 is a bit more discouraged than previous releases, > but it's also less likely to produce surprising results when the function > is genuinely volatile. > > regards, tom lane --------------090007060108080405030709 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
Hi Tom,
thanks for the explanation. However, it sounds like a terrible side effect of the change you mentioned. Also in this example the function is not volatile.
Couple more questions:
- why it works as expected when I use " where id in (50000)" and does not work if used "where id in (select 50000)"? Will it still work that way in the foreseeable future?
- why the function gets executed when the column is not in the select list at all? i.e. when I use "select id from...."?

re "optimizing the slow function" - this example is very simplified just to demonstrate the problem (so the function is intentionally slow). In our case it's a big view containing tens of subqueries. The view would contain many records but we only need 50 (=one page of results) so our query was designed to evaluate all the subqueries only for the 50 records. Now if we upgrade to 9.1.11... you see the problem.

In our case there's a workaround (split the query into two separate queries), but it just feels wrong. I'm also surprised that other users haven't encountered this issue.

I'd appreciate if you reconsider this to be a bug in the query optimizer.

Thanks for your help.
Martin.



On 11/01/14 14:32, Tom Lane wrote:
martin.junek@tracmap.co.nz writes:
after upgrading from 9.1.9 to 9.1.11 one of our queries started to run
terribly slow (went from few miliseconds to hours). The problem is better
explained in the following SQL snippet (which is a very simplified version
of the problem). If you run it on 9.1.9, all the SELECTs evaluate in few
miliseconds, if you run it on 9.1.11, it will take probably hours (I didn't
have the patience to wait for it).
I believe this is a result of this 9.1.11 change:

  * Avoid flattening a subquery whose SELECT list contains a volatile function wrapped inside a sub-SELECT (Tom Lane)

    This avoids unexpected results due to extra evaluations of the volatile function.

full details of which can be found here:
http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=af38d140c71c21eda422fedc838525525d155cac

That's an intentional change that is not going to get undone.  If you
don't like the results, I'd suggest fixing the "slow function" to get it
marked as stable or immutable as appropriate.  (The given example would be
best marked stable, but I suppose it's just an example and not your real
problem function.)  When it's marked volatile, as this is by default,
that discourages the planner from rearranging the query in ways that would
change the number of function executions from what a naive implementation
would suggest.  9.1.11 is a bit more discouraged than previous releases,
but it's also less likely to produce surprising results when the function
is genuinely volatile.

			regards, tom lane

--------------090007060108080405030709--