Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ebwp1-000799-85 for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Jan 2018 23:08:43 +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 1ebwoz-0001Y9-SU for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Jan 2018 23:08:41 +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_2) (envelope-from ) id 1ebwoz-0001Y0-Fc for pgsql-bugs@lists.postgresql.org; Wed, 17 Jan 2018 23:08:41 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ebwou-0002Vv-SZ for pgsql-bugs@lists.postgresql.org; Wed, 17 Jan 2018 23:08:39 +0000 Received: by mail-wm0-x244.google.com with SMTP id v123so19081053wmd.5 for ; Wed, 17 Jan 2018 15:08:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=GyyS1UafBPgWR11Eg9G6xPL2NGPV+eLgVRX0+JS8GJw=; b=q7blIAOPZDp6xvxo/OC+FmoKXgR4f5RkceQOO5yVkMNludDEED+zbfjf+ClH9h0zYx 69JAKpEmK7liKDJIY8pzIbWB1zdS5dJ99l6cA+OjET97hgrUnsjolH5bQyOP6OAoskeD JUjpNqzDGBtvTdsdf7wqaNoPaGnFzLDxob3tvmS4kLZUbrvOoIC1ExT9xJw2F2Nyb3KM H/Zx0Ixzh2Qk2O6sYgJKdcTegQQBzTqr+KAGtgDdvcKrYgtbcrK9dAs1AX/V9ya0cgSE 0neiN9XeDLP592+on0nmvFmweT2PX29nOH4HB0nvQe6CQneDVY6TCBBBIWK+7SJ52JZY WtWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=GyyS1UafBPgWR11Eg9G6xPL2NGPV+eLgVRX0+JS8GJw=; b=fCfaFZyOFmC4nwSUf2FZpe17M167LajrIOSDxJTz8aN5sWF0SzLsld51UIq+IvAG13 DDgoKKl/ci8FVC57HBzbCjN6HX9pJR1MXXBqDPc1bPX1LeX/cNOfSklpMUFT0fNO+oAc OZl27HJGAgPSMDPrWv19nyaAegaGbqVRVYFeRoZ5WFDnjGAySmkvP3yppEitJi2lcuTe zK4InnZ3oPYIafzOW9CTiTSU3GZvRvM8p6KiGNg+Iqq+g98ZlO0jaeaqo7Q+8LW2dvWi o8Y+e27uoZe2Iokmy2qGx1WvcZ2tKGM+dq+FjSn9e1KO6iWFgRaymo9evBUD2OzbRWO8 kNZw== X-Gm-Message-State: AKwxytecQtNa1VVA934HvdXbkdIKBkizEpFhC/Y7IYF8wMu3RGXpaU+i ovK1Mm/f+nq3HbpWz3R0l8GKRw== X-Google-Smtp-Source: ACJfBouI2Sg8aOJqjm5p0IhveYvczwANoHVmlFZ2oNnmZhmVOTIfW+zwk11MWljcf7dzN4+sqZtJzw== X-Received: by 10.28.210.145 with SMTP id j139mr149785wmg.59.1516230514917; Wed, 17 Jan 2018 15:08:34 -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 x203sm14363402wmd.11.2018.01.17.15.08.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Jan 2018 15:08:34 -0800 (PST) Subject: Re: BUG #15007: LIMIT not respected in sub-queries To: will@summercat.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <20180111211642.1407.23425@wrigleys.postgresql.org> From: Tomas Vondra Message-ID: Date: Thu, 18 Jan 2018 00:08:31 +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: <20180111211642.1407.23425@wrigleys.postgresql.org> 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 Hi, On 01/11/2018 10:16 PM, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 15007 > Logged by: Will Storey > Email address: will@summercat.com > PostgreSQL version: 10.1 > Operating system: Ubuntu 16.04 > Description: > > Hello, > > I am not sure this is a bug. But it is surprising to me and seems to > contradict the documentation in terms of join nesting. > > I have a SELECT query with a sub-SELECT in it. The sub-SELECT has a LIMIT > clause. I've found that sometimes I receive more rows (at most one extra in > my testing) than the LIMIT, where I expected only as many rows as the LIMIT. > This depends on the query plan. With some plans it never happens, and with > others it happens frequently. > > In looking into this behaviour, I came across hints that this is a known > quirk. I found bug reports related specifically to UPDATE/DELETE that sound > similar to this, but no mention that the behaviour can happen with SELECT: > > https://dba.stackexchange.com/questions/69471/postgres-update-limit-1?noredirect=1&lq=1 > (note the comments on the accepted answer) > https://www.postgresql.org/message-id/1399649764731-5803406.post%40n5.nabble.com > (and the thread) > https://www.postgresql.org/message-id/1385918761589-5781081.post%40n5.nabble.com > > This happens with both PostgreSQL 10.1 on Ubuntu 16.04 (from the PostgreSQL > repos: PostgreSQL 10.1 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu > 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, 64-bit) as well as on PostgreSQL > 9.6.5 (where I initially encountered the behaviour). > I think you're right those issues are related - a change of plan may affect the results in a somewhat non-deterministic way. In the cases you linked it's UPDATE on additional rows, in your case it's more about random() during rescans. I'll get to that in a minute. > Unfortunately my test case is not very clean and it is somewhat long, so > I've put it in a gist on GitHub: > > https://gist.github.com/horgh/f3e8ede81d866844e7d162d677968bf0 > > The SELECT query (run by the Perl program) quickly prints out that it > receives 6 rows. > > As you can see in the EXPLAIN ANALYZE output, the innermost Nested Loop has > loops > 1. I believe this is the cause of the behaviour. If I tweak the test > to have a plan where that node runs before the Seq Scan, there are never > more than 5 rows. > 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. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services