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.89) (envelope-from ) id 1hfs74-0001Kc-4X for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Jun 2019 20:32:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hfs72-0002QI-04 for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Jun 2019 20:32:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfs71-0002QB-Gf for pgsql-hackers@lists.postgresql.org; Tue, 25 Jun 2019 20:32:19 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfs6u-0001Gc-9y for pgsql-hackers@lists.postgresql.org; Tue, 25 Jun 2019 20:32:18 +0000 Received: by mail-wr1-x442.google.com with SMTP id d18so105661wrs.5 for ; Tue, 25 Jun 2019 13:32:12 -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:user-agent; bh=SS0lypD7PYe1QVbYpgvnjy8ciC4l/MReXIudaDno6iI=; b=12tprA1960yLW3quDOYUFGArrMR2U5gS9PVblbh/C4W8CLUvHntX+PSvhBeIL4XcYm d/J9AsW1cU/ftkgX97Z5AKKvE0fxTc8JLbz2KdeVcEiusBDoOBGKgDYyse+cM2cb/2M3 MeQC5j0Wg3Vm1yBDpaL4Hd1KYIINPgYdunj6/oji9ZwZ2OwCCOJzgeRhGHs9LegrRRnD jzUo1BJTMwEMI1xRzYR0pPC0pzy1NRXc2IUk3t43CrRZoqD6Bl9lCGxi9o9ByHNaLzzW eIkYPoRNsIvwrK4BVarhZgEPEUvW/vR9VgLqzQHyNtCGg9DKyRtQiGIU1DCdaNydHv+R nO/A== 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:user-agent; bh=SS0lypD7PYe1QVbYpgvnjy8ciC4l/MReXIudaDno6iI=; b=Tz6WHPTnr9EYWopvcdhoGn/40jXmXJEehTw+UJHbG/kvSxrZoyy/yoslirdqzfii6J pMDzDOL4oz7AuQHs2o2G9+7cXrDIyYGV+ZhxsIrHttqgWoDSdf4jSmUl4jrZK91mCdu8 gcI6frLXGGE4367wRRLufBFhgcDwvgKLz9Mg1f5NReD6Z9ONYns+L0P2khk/f/GijJmj aYwYXxwQf+pjAxo2IiXFUKzc7jzrLAi22B2A0rsBIW1IgzHAR4aLcx6Fexcqu+ENnCf8 FB9NMJp28wTP1yl3f4RepahJBjHF16KSm9e7thWWfUgdj8iMjbehkOck/X4SroRScKG8 mEyw== X-Gm-Message-State: APjAAAUUzrJc9oLlMuFelubli91wtSN3yEsWHApUSCH1TCopbLel2Y0M NTK8NolbiMesh5TjAwKgYHpiHg== X-Google-Smtp-Source: APXvYqy5KHmIt0BQWkVAGAAw3/qPwrhOLYDtKwGjTmp9WxY5DlAWccpM/1TjSh5CkeojKc4F7MMN/Q== X-Received: by 2002:a5d:4fce:: with SMTP id h14mr46953wrw.231.1561494729959; Tue, 25 Jun 2019 13:32:09 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id v27sm35409100wrv.45.2019.06.25.13.32.08 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 25 Jun 2019 13:32:09 -0700 (PDT) Date: Tue, 25 Jun 2019 22:32:07 +0200 From: Tomas Vondra To: Peter Geoghegan Cc: James Coleman , Simon Riggs , Rafia Sabih , Shaun Thomas , Dmitry Dolgov <9erthalion6@gmail.com>, Alexander Korotkov , Tom Lane , PostgreSQL Developers Subject: Re: [PATCH] Incremental sort (was: PoC: Partial sort) Message-ID: <20190625203207.lvmmat7d2ilnr64t@development> References: <20190624201601.iqy3ddig72l5lvbz@development> <20190625160235.6ctryjzvw5je7cet@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jun 25, 2019 at 12:13:01PM -0700, Peter Geoghegan wrote: >On Tue, Jun 25, 2019 at 11:03 AM James Coleman wrote: >> No, I haven't confirmed that it's called less frequently, and I'd be >> extremely surprised if it were given the diff doesn't suggest any >> changes to that at all. > >I must have misunderstood, then. I thought that you were suggesting >that that might have happened. > >> If you think it's important enough to do so, I can instrument it to >> confirm, but I was mostly wanting to know if there were any other >> plausible explanations, and I think you've provided one: there *are* >> changes in the patch to memory contexts in tuplesort.c, so if memory >> fragmentation is a real concern this patch could definitely notice >> changes in that regard. > >Sounds like it's probably fragmentation. That's generally hard to measure. > I'm not sure I'm really conviced this explains the difference, because the changes in tuplesort.c are actually fairly small - we do split the tuplesort context into two, but vast majority of the stuff is allocated in one of the contexts (essentially just the tuplesort state gets moved to a new context). I wouldn't expect this to have such strong impact on locality/fragmentation. But maybe it does - in that case it seems it might be worthwile to do it separately, irrespectedly of the incremental sort patch. I wonder if perf would show that as cache hits/misses, or something? It shouldn't be that difficult to separate this change into a separate patch, and benchmark it on it's own, though. FWIW while looking at the tuplesort.c changes, I've noticed some inaccurate comments in tuplesort_free. Firstly, the top-level comment says: /* * tuplesort_free * * Internal routine for freeing resources of tuplesort. */ without mentioning which resources it actually releases, so it kinda suggests it releases everything. But that's not true - AFAICS it only releases the per-sort resources. IMO this is a poor function name, and people will easily keep resources longer than they think - we should rename it to something like tuplesort_free_batch(). And then at the end tuplesort_free() does this: /* * Free the per-sort memory context, thereby releasing all working memory, * including the Tuplesortstate struct itself. */ MemoryContextReset(state->sortcontext); But that's clearly not true, because the tuplesortstate is allocated in the maincontext, not sortcontext. In general, the comments seem to be a bit confused by what 'sort' means. Sometimes it means the whole sort operation, sometimes it means one of the batches, etc. And the fact that the per-batch context is called sortcontext does not really improve the situation. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services