Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZKmu4-0006dN-Dd for pgsql-bugs@arkaria.postgresql.org; Thu, 30 Jul 2015 12:25:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZKmu3-0006ak-96 for pgsql-bugs@arkaria.postgresql.org; Thu, 30 Jul 2015 12:25:39 +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.84) (envelope-from ) id 1ZKmtP-0005ml-NC for pgsql-bugs@postgresql.org; Thu, 30 Jul 2015 12:24:59 +0000 Received: from mail-wi0-f171.google.com ([209.85.212.171]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZKmtI-0005zq-3R for pgsql-bugs@postgresql.org; Thu, 30 Jul 2015 12:24:58 +0000 Received: by wibud3 with SMTP id ud3so65989907wib.0 for ; Thu, 30 Jul 2015 05:24:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=OxxCFoO0dPHRWzWIGD6REVUg4T5nPHhqNmG1BjMe/ik=; b=nEjiMqYtjwqsxlkjb3059S2x6AO1gwjmVFSk25DOvUcpQ3N/fo8Z2AGkm7jRmdDoE+ 2mcsjEp2M1JBlgP7IT29tLGuoA9Kz5NkRJgBDx//TGr3t4SasN0W28Cald0Gp/xEAjIx piGRxq0sM4t5aSKTGv0E0Ej4BVL+ii9+LWDE2i58Gl88uZDNzeAgkY5X9ElWidnb4mJD Ur23OwiBXr40RZnWKvNXzipxvCcLtAI4uKFWvDfkNTx/6jjJmFaqYBmiPT1rXCTqTJDt V7MZ1MQPVEO/shFgLxlBpXFDXXb57wZ+7DjoLkXQ4w9YJHbqBnC5Rrn/eoZ1XY9AZEhU ZnJw== X-Gm-Message-State: ALoCoQmL2T9EXVLKeuAuzWOjMZUhwUWmnk0SJ6tvVNMXAjZn1elU7O5k+CYUVEH1xnV6+Vvk8PjH X-Received: by 10.195.13.200 with SMTP id fa8mr3079844wjd.9.1438259090139; Thu, 30 Jul 2015 05:24:50 -0700 (PDT) Received: from Markos-MacBook-Pro.local ([46.246.117.202]) by smtp.googlemail.com with ESMTPSA id z9sm29627107wiv.9.2015.07.30.05.24.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Jul 2015 05:24:49 -0700 (PDT) Message-ID: <55BA178F.9050503@joh.to> Date: Thu, 30 Jul 2015 14:24:47 +0200 From: Marko Tiikkaja User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Maxim Boguk CC: pgsql-bugs Subject: Re: BUG #13528: LATERAL vs. correlated scalar subquery References: <20150730085352.9098.47740@wrigleys.postgresql.org> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.6 (--) 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 On 7/30/15 1:48 PM, Maxim Boguk wrote: > You could see that the new plan have lower total cost than the old plan > (​cost=1867.28..1873.03 vs ​cost=0.00..125498.75). > I think it's primary reason why it been selected (planner could produce the > old plan but new plan wins on the cost basis). I'll have to admit I could've put more time into the original report, but I don't think that's accurate. If I disable hashagg and hashjoin and tune the query to tell the planner that only one row is to be expected, the plan looks like this: =#* explain analyze select u.elem, x.count from (SELECT u.elem FROM unnest(array[1]) u(elem) LIMIT 1) u(elem), lateral (select counts.count from counts where counts.a = u.elem) x; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.29..4778.86 rows=1 width=12) (actual time=0.060..52.380 rows=1 loops=1) Join Filter: (u.elem = data.a) Rows Removed by Join Filter: 99999 -> Limit (cost=0.00..0.01 rows=1 width=4) (actual time=0.010..0.011 rows=1 loops=1) -> Function Scan on unnest u (cost=0.00..1.00 rows=100 width=4) (actual time=0.009..0.009 rows=1 loops=1) -> GroupAggregate (cost=0.29..4774.33 rows=200 width=4) (actual time=0.047..45.634 rows=100000 loops=1) Group Key: data.a -> Index Only Scan using data_pkey on data (cost=0.29..4298.32 rows=94802 width=4) (actual time=0.042..19.381 rows=100000 loops=1) Heap Fetches: 100000 Planning time: 0.147 ms Execution time: 52.429 ms (11 rows) which to me suggests that the planner just doesn't realize that it can push the condition on counts.a into the view. .m -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs