Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecMPd-0000aZ-95 for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jan 2018 02:28:13 +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 1ecMPc-0005l4-8H for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jan 2018 02:28:12 +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 1ecLtj-0003Ke-8c for pgsql-bugs@lists.postgresql.org; Fri, 19 Jan 2018 01:55:15 +0000 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ecLtg-0004Zo-A9 for pgsql-bugs@lists.postgresql.org; Fri, 19 Jan 2018 01:55:14 +0000 Received: by mail-pg0-x242.google.com with SMTP id g16so199171pgn.7 for ; Thu, 18 Jan 2018 17:55:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=JPhNijiIW2CEASgzVSYgPIN3RgrYD7JD52XQS+/kDy0=; b=TA9hVF9IpbJEAVF+ssh4MAYwmbTPnZPnsJhdRdC97E296fuvxUiPkiVfqdDxI0cXgR zrQFD1VqExiwtWC+kd5SZagZNdkmq1KTevRDa33tWMW/uDNIG+YAdDu9TFEBBikxpW+4 FxRSgq00YFn6qrlaqSD99/LAeC/n8gefUcs4UCQSnjOe4pzYRnH3rAxlDueF5upr2p9Q J62ryHrS3eNZfwsE1WOpgSAiCr7dds1b0TATDPxQOyuFlLKt3RJJrEzC/JYL4zA+q9K7 sZMLXyMYXzSS/R6yPVP3tG9bSdyish/Gr1y4Md7J4DRY0pB1ld5MvFtNGXzopYxa/kF2 UpBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=JPhNijiIW2CEASgzVSYgPIN3RgrYD7JD52XQS+/kDy0=; b=uctXdC58BTWMAyYS7wdysB4Lchadsc0h5afDubtCvw2hj4dXFNBmO5Nc2vfrYL6+aj F+Ksv7pudA52CoDLumlMrxzNm3o+QMmJi+R0pLsyU/pwyK76bod84yuJh8ZxwA9aKcue F3sNnUyLTYED2VPkHEnDDnfYaUUBD3YbdF0NUdBbKjBHJhD1t4xTNDo5RJRRDnXBHYYi Ta6JlgZyiOp4zAa7RPH6jOXjWmAusrfqBQ4o/sVx31+HsY/t8ZhKAUYHM7almguX+oui eALmGyA7rHy98KTrdl4TZxUaxxw4a/6PI1nc0tPRXom1Chame+ybG1B5X5yzwqBZQZPa x1Sg== X-Gm-Message-State: AKwxytdg7dA82gqQdew3HS5nHIeQlkn8z+z+usVFed6UP4xMw+WjWyY2 1JRcYhsGQeGmy2N7xPKU0eI= X-Google-Smtp-Source: ACJfBotGlQI2mv2YO2ebNm5MFnBNkVdwlTFoJDMIzKGOVIhligbh8aER/VXGRYj5q0sqmEb2DL1yBg== X-Received: by 10.99.134.194 with SMTP id x185mr6556822pgd.252.1516326909594; Thu, 18 Jan 2018 17:55:09 -0800 (PST) Received: from dev.null (d108-172-153-241.bchsia.telus.net. [108.172.153.241]) by smtp.gmail.com with ESMTPSA id z85sm15929917pfk.110.2018.01.18.17.55.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 18 Jan 2018 17:55:08 -0800 (PST) Sender: Will Storey Date: Thu, 18 Jan 2018 17:55:06 -0800 From: Will Storey To: Tomas Vondra Cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15007: LIMIT not respected in sub-queries Message-ID: <20180119015506.wdohq7r7h4rj2jfq@dev.null> References: <20180111211642.1407.23425@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. 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. Thanks again!