Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jCuQJ-0006my-PA for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Mar 2020 00:13:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jCuQI-00010O-HX for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Mar 2020 00:13:02 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1jCuQI-000103-57 for pgsql-hackers@lists.postgresql.org; Sat, 14 Mar 2020 00:13:02 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jCuQF-0007nY-5E for pgsql-hackers@lists.postgresql.org; Sat, 14 Mar 2020 00:13:01 +0000 Received: by mail-wr1-x443.google.com with SMTP id n15so14219556wrw.13 for ; Fri, 13 Mar 2020 17:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=49HT2WvYJ/E5HyPpSKq+Kr3rTzLcRMs+Cqf5RccFH7s=; b=wc73o0BeJX1xbZJ5OtRB/5UO9YH0e+KOi/4TWdmgQOBL1jlHhbu8nH85yZTRgcxQ5K sthIG271yYxqjIyS6Nin3daRxDu1xCnndbJGmcXwxYGcn0qJQ52BzugfStwjsnl/0DdH ekE6kv64Rk4Dy0qyYwSFvH7bkiuCnLD4eSEJHS/0rVKtPCGuctrU3odmwy+JpGA96i5H /F3LmKBm3S9+C1F7y55DHSJw9NEEbCL18JBw6AcctpzyBQlUZlh3EXU6iew6r57+AtPA 3eCsRFGTlefSnx+9JEHacsSzQzs4NdNd0RgrnWCGn7r3PJ0VZSvD8HgNuzE0x06B7BjX q0zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=49HT2WvYJ/E5HyPpSKq+Kr3rTzLcRMs+Cqf5RccFH7s=; b=c1ck07atVSNTt8ahvFiWihK3m0YU2+39Q8wfK6ZmfcyVGSfyLudDrtJnb6kEEw2/Hd xuxmQQ19pOUTz18ELdN+YlAZ1+bPVBiqAiuVs77WZ0BJyBcbxCfrY3Uj9Vf8armRIN8I +rV2GudiX+DOTmBLqdgFtld+ofpnch+UsNXtCRJVs5WmkPan/RIkny1ORPT/HIjqRf/g S9q4Lx1uS1XBkTWfcshm4U7dYKgA//+ZmNMFYonxLNSv9HmIJm/j+xMoWbqcoLdTmFwz sN9ZBuxuCFYrx+8D0Cb/boV0hQ8tsUkoKD8SPaFZGfmzUHgBCKWunGQgfS7gjuTcVVQE 99ZA== X-Gm-Message-State: ANhLgQ1XnLzWjiOwNnSUhgun+6fC1IgdACBndPxDNKGZ1pVc3Xgmd17v sj0PrJ0H0QxzqNNAuYrFrwD8+Q== X-Google-Smtp-Source: ADFU+vvEn97mR2EOSvZSU8MV1ml0K2rRze0IsQ8OXGLunbbxXMmOxviHZ0QMENBH9QKgKabds4MFQg== X-Received: by 2002:a5d:4282:: with SMTP id k2mr8137510wrq.306.1584144777553; Fri, 13 Mar 2020 17:12:57 -0700 (PDT) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id k5sm16342169wmj.18.2020.03.13.17.12.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Mar 2020 17:12:56 -0700 (PDT) Date: Sat, 14 Mar 2020 01:12:54 +0100 From: Tomas Vondra To: James Coleman Cc: Michael Paquier , Rafia Sabih , Peter Geoghegan , Simon Riggs , Shaun Thomas , Dmitry Dolgov <9erthalion6@gmail.com>, Alexander Korotkov , Tom Lane , PostgreSQL Developers , Alvaro Herrera Subject: Re: [PATCH] Incremental sort (was: PoC: Partial sort) Message-ID: <20200314001254.gdusqeanl7wrxmr7@development> References: <20190928230049.k4l2mayun7jomhxy@development> <20191129060146.GH2505@paquier.xyz> <20191201004716.cenkc33em2lnrrdl@development> <20200121142529.wpickvfefp7lok4p@development> <20200311024355.5h2v56jpgzvd7yox@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Mar 13, 2020 at 04:31:16PM -0400, James Coleman wrote: >On Fri, Mar 13, 2020 at 2:23 PM James Coleman wrote: >> >> On Tue, Mar 10, 2020 at 10:44 PM Tomas Vondra >> wrote: >> > 3) Most of the execution plans look reasonable, except that some of the >> > plans look like this: >> > >> > >> > QUERY PLAN >> > --------------------------------------------------------- >> > Limit >> > -> GroupAggregate >> > Group Key: t.a, t.b, t.c, t.d >> > -> Incremental Sort >> > Sort Key: t.a, t.b, t.c, t.d >> > Presorted Key: t.a, t.b, t.c >> > -> Incremental Sort >> > Sort Key: t.a, t.b, t.c >> > Presorted Key: t.a, t.b >> > -> Index Scan using t_a_b_idx on t >> > (10 rows) >> > >> > i.e. there are two incremental sorts on top of each other, with >> > different prefixes. But this this is not a new issue - it happens with >> > queries like this: >> > >> > SELECT a, b, c, d, count(*) FROM ( >> > SELECT * FROM t ORDER BY a, b, c >> > ) foo GROUP BY a, b, c, d limit 1000; >> > >> > i.e. there's a subquery with a subset of pathkeys. Without incremental >> > sort the plan looks like this: >> > >> > QUERY PLAN >> > --------------------------------------------- >> > Limit >> > -> GroupAggregate >> > Group Key: t.a, t.b, t.c, t.d >> > -> Sort >> > Sort Key: t.a, t.b, t.c, t.d >> > -> Sort >> > Sort Key: t.a, t.b, t.c >> > -> Seq Scan on t >> > (8 rows) >> > >> > so essentially the same plan shape. What bugs me though is that there >> > seems to be some sort of memory leak, so that this query consumes >> > gigabytes os RAM before it gets killed by OOM. But the memory seems not >> > to be allocated in any memory context (at least MemoryContextStats don't >> > show anything like that), so I'm not sure what's going on. >> > >> > Reproducing it is fairly simple: >> > >> > CREATE TABLE t (a bigint, b bigint, c bigint, d bigint); >> > INSERT INTO t SELECT >> > 1000*random(), 1000*random(), 1000*random(), 1000*random() >> > FROM generate_series(1,10000000) s(i); >> > CREATE INDEX idx ON t(a,b); >> > ANALYZE t; >> > >> > EXPLAIN ANALYZE SELECT a, b, c, d, count(*) >> > FROM (SELECT * FROM t ORDER BY a, b, c) foo GROUP BY a, b, c, d >> > LIMIT 100; >> >> While trying to reproduce this, instead of lots of memory usage, I got >> the attached assertion failure instead. > >And, without the EXPLAIN ANALYZE was able to get this one, which will >probably be a lot more helpful. > Hmmm, I'll try reproducing it, but can you investigate the values in the Assert? I mean, it fails on this: Assert(total_allocated == context->mem_allocated); so can you get a core or attach to the process using gdb, and see what's the expected / total value? BTW, I might have copied the wrong query - can you try with a higher value in the LIMIT clause? For example: EXPLAIN ANALYZE SELECT a, b, c, d, count(*) FROM (SELECT * FROM t ORDER BY a, b, c) foo GROUP BY a, b, c, d LIMIT 1000000; I think this might be the differenc ewhy you don't see the memory leak. Or maybe it was because of asserts? I'm not sure if I had enabled them in the build ... regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services