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 1iCja0-0008DM-4a for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2019 12:06: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 1iCjZy-0007AD-D8 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2019 12:06:02 +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 1iCjZx-0007A6-TW for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2019 12:06:02 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iCjZq-0005Jl-CZ for pgsql-hackers@postgresql.org; Tue, 24 Sep 2019 12:06:00 +0000 Received: by mail-wr1-x442.google.com with SMTP id i18so1633389wru.11 for ; Tue, 24 Sep 2019 05:05:54 -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=lEvg6O6ZfaSX5lmt9dFENiOjBDvwJ8W1/ZfCSr84Mzg=; b=tB6CWWK2AmToM1fofkNJFSqqI7ZE9GxJRHZshWoH1qVP2L/Wr3BBwr3N7VRygZBtA6 9fiYfvwXE/OoBDTq0L27ik4fg38yKbfCV9bTAhMPpzKqgk5BVbh6KCbF0TyuwHBiJcn5 ehSPwgOc8APGkVyd7mygGSiihTxLdyJBwpto7X87Az8kXO7hoRjxhPgM9dUxiXef+qo6 XrTOw/UUANzHRDMKJIOU8yQ4VEikD0e2CrQGh7aSPoCtMcNmYyitClE4Y9u1s5M+GZmz Qy09AFzte0bpbHsVFnMKNKHzO0klZdJtvXVekuYKvXqXxnXFUGOYrMfBdO23D9U3kLBw UarA== 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=lEvg6O6ZfaSX5lmt9dFENiOjBDvwJ8W1/ZfCSr84Mzg=; b=Q8FN3Q2xTYFUHMYzPeLC03RiDcSDihqzHP6SxvL5VFqnOaiLQs0e4Ws7dm51W5POY7 7ogDACuwlFZj0E8jNGpqEY5t+dc+IVq/RaY0Z479yPrKRLZBmkXb6mdTdajOtwBYfdtq FYmT8MWb+x4GslibLbyN4Z23g+1kD9ORlSz5g5y39enTBNyH3J8aR9JANSiaVNLYooBj uoETX0WszlEXQzKEh79wIybXH705tTLzalmjhZSgmveB1P0QVFgPvvgzICS0MbF6te0l baS7qVnwProJdP85iexb2ijTeAJ0vzTTijGH66knqGYht0retdSOPRmFdQSjTHyK0iH3 8Jhg== X-Gm-Message-State: APjAAAVTnoNwnWvG0u5D2r4xGqEhAA/uOh78EHt4gsZG83rT8UVGbBjC VMi6Q0EuqhNQnmSSi81QgjUwWg== X-Google-Smtp-Source: APXvYqx7D/zImRi1KFxQQDkKhGR2nQhQuGgw2Qrk+5VOdhuwVTaXVHd5imPRfgtfw/u31O+YEr4i0g== X-Received: by 2002:adf:e388:: with SMTP id e8mr2075136wrm.306.1569326752872; Tue, 24 Sep 2019 05:05:52 -0700 (PDT) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id b12sm1627106wrt.21.2019.09.24.05.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2019 05:05:52 -0700 (PDT) Date: Tue, 24 Sep 2019 14:05:51 +0200 From: Tomas Vondra To: Michael Paquier Cc: Melanie Plageman , Jeff Davis , Pg Hackers Subject: Re: Memory Accounting Message-ID: <20190924115819.5u5i67add5evtea6@development> References: <027a129b8525601c6a680d27ce3a7172dab61aab.camel@j-davis.com> <20190724215228.tlyj74ie4l2jn7ji@development> <20190924052140.GA1982@paquier.xyz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20190924052140.GA1982@paquier.xyz> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Sep 24, 2019 at 02:21:40PM +0900, Michael Paquier wrote: >On Wed, Jul 24, 2019 at 11:52:28PM +0200, Tomas Vondra wrote: >> I think Heikki was asking about places with a lot of sub-contexts, which a >> completely different issue. It used to be the case that some aggregates >> created a separate context for each group - like array_agg. That would >> make Jeff's approach to accounting rather inefficient, because checking >> how much memory is used would be very expensive (having to loop over a >> large number of contexts). > >The patch has been marked as ready for committer for a week or so, but >it seems to me that this comment has not been addressed, no? Are we >sure that we want this method if it proves to be inefficient when >there are many sub-contexts and shouldn't we at least test such >scenarios with a worst-case, customly-made, function? I don't think so. Aggregates creating many memory contexts (context for each group) was discussed extensively in the thread about v11 [1] in 2015. And back then the conclusion was that that's a pretty awful pattern anyway, as it uses much more memory (no cross-context freelists), and has various other issues. In a way, those aggregates are wrong and should be fixed just like we fixed array_agg/string_agg (even without the memory accounting). The way I see it we can do either eager or lazy accounting. Eager might work better for aggregates with many contexts, but it does increase the overhead for the "regular" aggregates with just one or two contexts. Considering how rare those many-context aggregates are (I'm not aware of any such aggregate at the moment), it seems reasonable to pick the lazy accounting. (Note: Another factor affecting the lazy vs. eager efficiency is the number of palloc/pfree calls vs. calls to determine amount of mem used, but that's mostly orthogonal and we cn ignore it here). So I think the approach Jeff ended up with sensible - certainly as a first step. We may improve it in the future, of course, once we have more practical experience. Barring objections, I do plan to get this committed by the end of this CF (i.e. sometime later this week). [1] https://www.postgresql.org/message-id/1434311039.4369.39.camel%40jeff-desktop -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services