Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecXpg-0006Nt-Cu for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jan 2018 14:39:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecXpf-0003MG-Ie for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jan 2018 14:39:51 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ecXpf-0003M7-DB for pgsql-bugs@lists.postgresql.org; Fri, 19 Jan 2018 14:39:51 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ecXpb-00048h-V7 for pgsql-bugs@lists.postgresql.org; Fri, 19 Jan 2018 14:39:50 +0000 Received: by mail-wm0-x244.google.com with SMTP id v71so4004638wmv.2 for ; Fri, 19 Jan 2018 06:39:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=F6BtUkRiOtthXFVRrxZnsdMeC8APgbqlkWmVU4MCCZU=; b=TV467d+QrqVXP+MlG+IR384ht8IG0GFRJ9OE3QY/6lKRqx9sCr7VYBlnFDk5GvFGqt szX59ixMaGEnr2xOZe8i3HF2vtefTKCdBG5lnybYslW09WCLgPuSutd/znNoLBEDggjR lG3OhK/O8xJ9HwI5VMHjJ1AqSrU9pt68tR5v0xtpmpp+uPpU19ovmfQZOa+vmw6hCfIV EpPSgel/feU9ff4n1EHVnQF5Q7AAaQZLIkuVzUy3p+irEDwVK5tA61YtgyvVUAPR0vaL y5ymBG82HYjKDcqS5wM/HNGyKhzfPTx+MX3hf5IP7JkRi8CV8E9Nyp7fzJiwjTIU9ykk vdOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=F6BtUkRiOtthXFVRrxZnsdMeC8APgbqlkWmVU4MCCZU=; b=cebITPt2erI6wutLJCAbW056Y9LKG2UY3QasnKytEmHZp9YWFot3EMPZXPZIrHlD5Z aOQwRuADbmvum7C+CTIIas83HWCKRD29dhFuQEhFQFa9Jij7IbxB7fqzXH9XIrfy+/ki MozVDp5szZ7rlPKwXdSTMipOEjVH5Dfzhrs1h2Y8yTtbKcyZ7yrspp1xoJWIQni0U42H 0awRCiIx8skMoaszeUb+iYLuZTo0HjhFRqBVY+MKzfU0s3CCEAY5cc01F39LU/Ev0SZO V9CCYEZEauizKwYSotwxvKKmi6yDcTKAL7VjjT+DISoqwNdx/RSASYh+SIVxo47XQtFM 4mNg== X-Gm-Message-State: AKwxytdhbMKWb8tXeiRFpDLHVT2HvNLXM9sTjVDcFlaHaBRpFgaN1Zmp PzdmtZ86lrtgscmw5GgtRd4lQ9rv3CDzJhQR7oiY6ISE7nYHJWrWOyOuaC0G3eNEf5K+RfDBUyn CoTtZcISnChqWcUUcmjTgjEBWK/vy9mzv+MAjhY43gwP966w3So8xKbmASc2TEbz0wpcUOZgVti COOlo3ED9/Kwnc X-Google-Smtp-Source: ACJfBouTyTWZrEi5hPazjJoDaHqfmmyf/7NMhq5jxe5qCdSVZk9AY0LXlB8H6uusnZSiOoQ9v0KZlg== X-Received: by 10.80.136.129 with SMTP id d1mr6987045edd.42.1516372786293; Fri, 19 Jan 2018 06:39:46 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id f55sm6864630ede.77.2018.01.19.06.39.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Jan 2018 06:39:45 -0800 (PST) Subject: Re: BUG #15007: LIMIT not respected in sub-queries To: Will Storey Cc: pgsql-bugs@lists.postgresql.org References: <20180111211642.1407.23425@wrigleys.postgresql.org> <20180119015506.wdohq7r7h4rj2jfq@dev.null> From: Tomas Vondra Message-ID: <7bbee86a-d4c9-2f9b-64bf-d4f372062676@2ndquadrant.com> Date: Fri, 19 Jan 2018 15:39:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20180119015506.wdohq7r7h4rj2jfq@dev.null> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 01/19/2018 02:55 AM, Will Storey wrote: > On Thu 2018-01-18 00:08:31 +0100, Tomas Vondra wrote: > > > >> I went through the test case, and I think I know what's going on. The >> script did not reproduce the issue for me, but I think I've been able to >> tweak the query to use a plan producing more issues. >> >> The key is to force a particular join order, and disable operations that >> would materialize intermediate results of the t1 scan. That is, we want >> something like this: >> >> -> nestloop >> -> nestloop t2 + t3 >> -> seq scan t1 >> >> Firstly, I've replaced the dynamic random() condition with a static one: >> >> random() <= 0.5 >> >> this is not strictly necessary, but it simplifies the plan. We need the >> random() call though, as I'll explain later. >> >> Then I've disabled a bunch of plan nodes that would materialize results >> of the t1 seq scan: >> >> set enable_material = off; >> set enable_sort = off; >> set enable_hashjoin = off; >> >> And finally, I've disabled join reordering by setting >> >> set join_collapse_limit = 1; >> >> Now, if you rewrite the query like this (which essentially just forces a >> particular join order, when combined with join_collapse_limit=1, nothing >> else): >> >> explain analyze SELECT * FROM >> (t3 JOIN t2 USING (t2_id)) JOIN >> (SELECT * FROM t1 >> WHERE t1_id IS NOT NULL AND >> t1_id < 100 AND >> t1_val LIKE 'h%' AND >> random() <= 0.5 >> LIMIT 5 >> ) AS t1 >> ON t3.t1_id = t1.t1_id >> WHERE t2.t2_val LIKE 'he%'; >> >> you will get plans like this: >> >> >> QUERY PLAN >> ------------------------------------------------------------------------ >> Nested Loop (cost=0.00..31.41 rows=5 width=21) >> (actual time=0.052..1.825 rows=7 loops=1) >> Join Filter: (t3.t1_id = t1.t1_id) >> Rows Removed by Join Filter: 73 >> -> Nested Loop (cost=0.00..16.10 rows=16 width=14) >> (actual time=0.030..0.917 rows=16 loops=1) >> Join Filter: (t3.t2_id = t2.t2_id) >> Rows Removed by Join Filter: 160 >> -> Seq Scan on t2 (cost=0.00..1.14 rows=11 width=10) >> (actual time=0.015..0.041 rows=11 loops=1) >> Filter: ((t2_val)::text ~~ 'he%'::text) >> -> Seq Scan on t3 (cost=0.00..1.16 rows=16 width=8) >> (actual time=0.003..0.037 rows=16 loops=11) >> -> Limit (cost=0.00..0.84 rows=5 width=7) >> (actual time=0.008..0.037 rows=5 loops=16) >> -> Seq Scan on t1 (cost=0.00..1.52 rows=9 width=7) >> (actual time=0.004..0.016 rows=5 loops=16) >> Filter: ((t1_id IS NOT NULL) AND (t1_id < 100) AND >> ((t1_val)::text ~~ 'h%'::text) AND >> (random() <= '0.5'::double precision)) >> Rows Removed by Filter: 5 >> Planning time: 0.625 ms >> Execution time: 1.911 ms >> (15 rows) >> >> >> This should be equivalent to the original query, and should produce the >> same results (modulo random() of course). >> >> But notice it actually does produce 7 rows! >> >> Nested Loop (cost=0.00..31.41 rows=5 width=21) >> (actual time=0.052..1.825 rows=7 loops=1) >> ^ >> >> The problem is that it ends up executing the sequential scan on t1 >> repeatedly (because it's the inner relation in a nested loop), and >> because random() is volatile, the results of the scan are likely different. >> >> Each rescan individually still respects the LIMIT, but when combined >> result may be larger - there may be more unique IDs, matching additional >> rows from the other tables. >> >>> >>> I believe a better way to write this query would be to use a CTE. >>> >> >> Yes, that will stabilize the output of the random() function, >> eliminating the nondeterminism during rescans. > > Wow, thank you for the great explanation of what is going on! > > It sounds like this is not really a bug then and is just something to > be expected when using LIMIT with such volatile joins. > The volatile part here is not the join, but the random function during rescans of a relation. But yeah, I don't think this qualifies as a bug. > > I suppose I expected that if there is a LIMIT then that would be the > maximum number of rows the subquery would ever provide. The planner > would have to force materializing/stabilizing in such cases it sounds > like. Maybe that is not possible or not a good idea. It does seem > like a pretty edge case. > Yes, something like that would be necessary. I don't know how difficult that would be, though. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services