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 1jDCvZ-0006XI-S9 for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Mar 2020 19:58:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jDCvX-0005S6-H6 for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Mar 2020 19:58:31 +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 1jDCvX-0005PR-3c for pgsql-hackers@lists.postgresql.org; Sat, 14 Mar 2020 19:58:31 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jDCvS-0000Jd-N9 for pgsql-hackers@lists.postgresql.org; Sat, 14 Mar 2020 19:58:30 +0000 Received: by mail-wr1-x442.google.com with SMTP id t2so6329943wrx.12 for ; Sat, 14 Mar 2020 12:58:26 -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=SHIOxEqAtMfUvRDz+Z5tGbQZMOaxMfv2Gc2x00Df8h0=; b=vcKyLjT+JYVcfgGxZDA2tfwxhjUnp8QVuy1FJCbfD/9unXBCO9mMGNQ+Deq7Lpmp9x 9BPCXI+z34NMW6Uo5D3pvYrR3EWOC2mN9KLHm/D5CUf1JFX7Hv93rqf7APpRGmNJCRzV 9Larxn8ASCg0GM/xJB21Z/h01axd5xncPGMSvCnH58p3OialGMjEwxAaUjhBucfyrnox xmKeQVAiD2iaBZH9jCn7troMJgT0fDfztQ3pE0smQDwtpE9hKMhaDWKRlqqX/oE4PWOO xkOPfKcPwClFEfJdyqIBIvT+dBdZic/CdtIaqiXzuDXvrIMr3+0iXik4Z+dx+h9pw3Kq YWLA== 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=SHIOxEqAtMfUvRDz+Z5tGbQZMOaxMfv2Gc2x00Df8h0=; b=N6HUKanLB3y1h6aKl9nFos0p85CoofrPEE+lC56UzIQigJtykx0h0N+aqUmLss3w/e FylikIoESh1xwEcH44v3UzMuTb9qz3pvHkPngz1bUpQ0HoGCENzHsUcZcDIH4Hk77slM JW/inwAO6K+KAGAxkjwyaaEzccrcNTkZEQTP1xiQQVrvKynNSzowqNFjWzOgZQxEZkBC S9YqpUDzCUwsUTJRcqIcN0kxJDPswv0AwrpRNGdzcrKlLeW8OL04ps2nVKZQEF4rMXNk LpwSuaNWFrZ2LKSXiUT6CGwUUSganqg3BFqQEs5NXN8MMh7yhz6ID7NWsmWT+CCA8wwu tyTQ== X-Gm-Message-State: ANhLgQ2Z5NYUzV4fYAK9QcoLHQOmy5v9ggnq2h3WVwUtscfh2k8X3OWI vEqPbP1zaQ+oC/evnLVEhCCB7w== X-Google-Smtp-Source: ADFU+vv94OdxDyD2IBrtqXeIbTZL/BCKzsudx4aMpdHGvU8q1DSFJ66q/TPuBMxihrvix/OH70DXBg== X-Received: by 2002:adf:a506:: with SMTP id i6mr24009179wrb.146.1584215904760; Sat, 14 Mar 2020 12:58:24 -0700 (PDT) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id x16sm20645208wrg.44.2020.03.14.12.58.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Mar 2020 12:58:23 -0700 (PDT) Date: Sat, 14 Mar 2020 20:58:22 +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: <20200314195822.bc2mqyvolo2w5l2w@development> References: <20200311024355.5h2v56jpgzvd7yox@development> <20200314001254.gdusqeanl7wrxmr7@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="sicffm3iwx4yx2za" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --sicffm3iwx4yx2za Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Sat, Mar 14, 2020 at 02:41:09PM -0400, James Coleman wrote: > >It looks like the issue is actually into the `tuplecontext`, which is >currently a child context of `sortcontext`: > >#3 0x0000558cd153b565 in AllocSetCheck >(context=context@entry=0x558cd28e0b70) at aset.c:1573 >1573 Assert(total_allocated == context->mem_allocated); >(gdb) p total_allocated >$1 = 16384 >(gdb) p context->mem_allocated >$2 = 8192 >(gdb) p context->name >$3 = 0x558cd16c8ccd "Caller tuples" > >I stuck in several more AllocSetCheck calls in aset.c and got the >attached backtrace. > I think the problem is pretty simple - tuplesort_reset does call tuplesort_reset, which resets the sortcontext. But that *deletes* the tuplecontext, so the state->tuplecontext gets stale. I'd haven't looked into the exact details, but it clearly confuses the accouting. The attached patch fixes the issue for me - I'm not claiming it's the right fix, but it's the simplest thing I could think of. Maybe the tuplesort_rest should work differently, not sure. And it seems to resolve the memory leak too - I suspect we've freed the context (so it was not part of the tree of contexts) but the struct was still valid and we kept allocating memory in it - but it was invisible to MemoryContextDump etc. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --sicffm3iwx4yx2za Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="tuplesort-fix.patch" diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index c2bd38f39f..91c0189577 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -1419,6 +1419,10 @@ tuplesort_reset(Tuplesortstate *state) state->slabFreeHead = NULL; state->growmemtuples = true; + state->tuplecontext = AllocSetContextCreate(state->sortcontext, + "Caller tuples", + ALLOCSET_DEFAULT_SIZES); + if (state->memtupsize < INITIAL_MEMTUPSIZE) { if (state->memtuples) --sicffm3iwx4yx2za--