Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joyFI-0002rE-Po for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Jun 2020 23:59:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1joyFH-0004VD-Np for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Jun 2020 23:58:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joyFH-0004V4-Dk for pgsql-hackers@lists.postgresql.org; Fri, 26 Jun 2020 23:58:59 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1joyFD-0005AZ-Oe for pgsql-hackers@postgresql.org; Fri, 26 Jun 2020 23:58:59 +0000 Received: by mail-ej1-x644.google.com with SMTP id n24so10963589ejd.0 for ; Fri, 26 Jun 2020 16:58:55 -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=cf8mBrKQEG1EsqCgGMzVO2bSXmciQrava3rdsbIGRGE=; b=coBZRBGemyH62SLfe5yHolPJnPh6xXqdDpZl4ovQhJeEye/UHZCIy18u4GM18fakuB hqZLdfQkqXiHrJH5txyAy+C2nUr5FGxImOUKLe1OlMFtD6TTNiy6wgFXieqC26L7tVrn YscsNsljMFBCFwCg3ykrmvVoeFg6YL94cOXB22YUKTPjpd8kD6UIiDqcJTK5COGJXe7b xjFVTVrI53U9ldyZMucVxB/02RseKQ5rvz7MDdQ81y9raEkJd7pCULNr5xXJ47vJHOTc QshiiWrrn0cql6/JbA/J6tLqK2aLFEz7jlhNxyYSnlb0bZDKQP2lI8PZkZFobIGf2sxw gQfA== 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=cf8mBrKQEG1EsqCgGMzVO2bSXmciQrava3rdsbIGRGE=; b=R2vl1PFtzMvAb/wPWnV1MIWmnx0e8AJkK2WfdtTqxGqZcZVb0dQRqmJcblbFd873bu y2cakQ7r5YpeRvBAqc4CAm1KTJyDDigjzYtYXCRG5diCtK+7qOx66Q9c9G7T1V2/1afg 35VhQMigXefHFVS2zQnZuGTu8rWuK7apalbbjmxMuHOyTqfZjsf8mSzIJrdBZqG2/m/J +9d9ChpXBeBr9yPpcCuYM6PtPcEtUbaeMce9SuKp60t++bl61NHIOwiXdz3nTi8Wp3wI GgfO1k0/3VmC/AXSFo2MFFDtzV8kjQitWgz+axTSWpiw2/aoPw9BK93NcsHU/3IZ4Gpc Qe1g== X-Gm-Message-State: AOAM531dg9t7ZCV+EQWd9l9vyPtuHhMuuEYl5N3JJj09cPLLy5ugipgA UhYFtUPbpF8Mihup2pMUK6AQMg== X-Google-Smtp-Source: ABdhPJxaKTGXb84w2RX4ZykwklRxpyFuI/rvXLL+Wu9ZTyRHe5GRgW03w67O0M7LajTyp5B14BW6Sw== X-Received: by 2002:a17:906:f98e:: with SMTP id li14mr4745490ejb.174.1593215934780; Fri, 26 Jun 2020 16:58:54 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id e3sm782262edm.14.2020.06.26.16.58.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jun 2020 16:58:54 -0700 (PDT) Date: Sat, 27 Jun 2020 01:58:50 +0200 From: Tomas Vondra To: Bruce Momjian Cc: Robert Haas , Andres Freund , David Rowley , Jeff Davis , Justin Pryzby , Melanie Plageman , "pgsql-hackers@postgresql.org" Subject: Re: Default setting for enable_hashagg_disk Message-ID: <20200626235850.gvl3lpfyeobu4evi@development> References: <20200624231810.GB13627@momjian.us> <20200624233837.GA21436@momjian.us> <20200625171756.GB12486@momjian.us> <20200625235357.udlest4k6i2uekui@development> <20200626040210.GA13963@momjian.us> <20200626144414.rftlidu34rigiulv@development> <20200626163726.GA3337@momjian.us> <20200626174513.tmnkic7q5duugvhh@development> <20200626230020.GD3337@momjian.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200626230020.GD3337@momjian.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jun 26, 2020 at 07:00:20PM -0400, Bruce Momjian wrote: >On Fri, Jun 26, 2020 at 07:45:13PM +0200, Tomas Vondra wrote: >> On Fri, Jun 26, 2020 at 12:37:26PM -0400, Bruce Momjian wrote: >> > I was thinking more of being able to allocate a single value to be >> > shared by all active sesions. >> >> Not sure I understand. What "single value" do you mean? > >I was thinking of a full-cluster work_mem maximum allocation that could >be given to various backends that request it. > >Imagine we set the cluster-wide total of work_mem to 1GB. If a session >asks for 100MB, if there are no other active sessions, it can grant the >entire 100MB. If there are other sessions running, and 500MB has >already been allocated, maybe it is only given an active per-node >work_mem of 50MB. As the amount of unallocated cluster-wide work_mem >gets smaller, requests are granted smaller actual allocations. > >What we do now makes little sense, because we might have lots of free >memory, but we force nodes to spill to disk when they exceed a fixed >work_mem. I realize this is very imprecise, because you don't know what >future work_mem requests are coming, or how long until existing >allocations are freed, but it seems it would have to be better than what >we do now. > >> Wasn't the idea was to replace work_mem with something like query_mem? >> That'd be nice, but I think it's inherently circular - we don't know how >> to distribute this to different nodes until we know which nodes will >> need a buffer, but the buffer size is important for costing (so we need >> it when constructing the paths). >> >> Plus then there's the question whether all nodes should get the same >> fraction, or less sensitive nodes should get smaller chunks, etc. >> Ultimately this would be based on costing too, I think, but it makes it >> soe much complex ... > >Since work_mem affect the optimizer choices, I can imagine it getting >complex since nodes would have to ask the global work_mem allocator how >much memory it _might_ get, but then ask for final work_mem during >execution, and they might differ. Still, our spill costs are so high >for so many node types, that reducing spills seems like it would be a >win, even if it sometimes causes poorer plans. > I may not understand what you mean by "poorer plans" here, but I find it hard to accept that reducing spills is generally worth poorer plans. I agree larger work_mem for hashagg (and thus less spilling) may mean lower work_mem for so some other nodes that are less sensitive to this. But I think this needs to be formulated as a cost-based decision, although I don't know how to do that for the reasons I explained before (bottom-up plan construction vs. distributing the memory budget). FWIW some databases already do something like this - SQL Server has something called "memory grant" which I think mostly does what you described here. >> > Also, doesn't this blog entry also show that spiling to disk for ORDER >> > BY is similarly slow compared to hash aggs? >> > >> > https://momjian.us/main/blogs/pgblog/2012.html#February_2_2012 >> >> The post does not mention hashagg at all, so I'm not sure how could it >> show that? But I think you're right the spilling itself is not that far >> away, in most cases (thanks to the recent fixes made by Jeff). > >Yeah, I was just measuring ORDER BY spill, but it seems to be a similar >overhead to hashagg spill, which is being singled out in this discussion >as particularly expensive, and I am questioning that. > The difference between sort and hashagg spills is that for sorts there is no behavior change. Plans that did (not) spill in v12 will behave the same way on v13, modulo some random perturbation. For hashagg that's not the case - some queries that did not spill before will spill now. So even if the hashagg spills are roughly equal to sort spills, both are significantly more expensive than not spilling. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services