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 1hqPBf-0005qK-AR for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Jul 2019 21:52:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hqPBd-00080V-H5 for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Jul 2019 21:52:37 +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 1hqPBc-00080L-UM for pgsql-hackers@lists.postgresql.org; Wed, 24 Jul 2019 21:52:37 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqPBZ-0007Dw-JO for pgsql-hackers@postgresql.org; Wed, 24 Jul 2019 21:52:36 +0000 Received: by mail-wr1-x444.google.com with SMTP id y4so48534467wrm.2 for ; Wed, 24 Jul 2019 14:52:33 -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=b6cygVziztRg0BLskBSZEM7HeC6UJGHh9ZZKBG+CuLk=; b=PSr417el6Li7tCMIkXeyltB/eBoa5NAsGSaHQmflT7uF1Qj3KIF9k+xYl3+rNeewPh dIFf47KJBCx9ZwpSnyLVf4k0N7V1pL5ZBCFPQE8dv8DnI6oiao92OfFvEYNb7WsOYKhW r+0EaO/a+gXJX7mwfJ4TkkaOU63gfG9Tkxifr+N4SlJss2W//Qz9WnIuVYHiye3cpFIM R1gW8sG+qf9ELN11qCvX7aDFgrYCWICOe0gSx3PbM3wgZ3KXfKkptN7gsoFlwvqDXHjp 7MrwqzIhDNnys0jyUSMX65Hm7lcGFLEWnj1LF2LfTJXnMoNMmhgfNROiqhmVyY8LlVdE ymOQ== 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=b6cygVziztRg0BLskBSZEM7HeC6UJGHh9ZZKBG+CuLk=; b=GUz8lv/oZlIxNjhVsT55JdkPI/YcUb0DAQhwSKTsHHDpy9FHsPpNOjQ8UBn4JDxVEO 70ImBVgEYm5F7YQDuj7LsoswJHZSVlaXuSkEE0eXc7VL2MDSwZU+9DkHS88//qYNVrfs dflAYqrRAsI9ZYC9/3+UEcE+VBssD2F09HXEswbF2TDqMuCg0V/SFu45MmgGytW2ibhv ybFnec1tyH8S4GXJFltOZiyUhSrkkOkv+WzyPXqTzSf9RGbAn16Aup9SpoAAgIqs0+0O T/ZPLHtzgjjCen7IzE3N9+du5YSxIeGPS8shfIKasED0bv25I8FzKNu0tNeK7XhXkaUO zjHQ== X-Gm-Message-State: APjAAAW14pgaRa3vKD941bpeos63fAcaRHI7rnNtx33LhaWRa4vLCpOM xnsOXMGPPbAnDHl88brzj+9j9Q== X-Google-Smtp-Source: APXvYqzQPUVXBsSc6W2SZI7PRXERJkZqCgBgD9vbkFpzRf1uDDul8R/GLNU1a6lD6dB2CFmEGV88Aw== X-Received: by 2002:adf:ef8d:: with SMTP id d13mr86762392wro.60.1564005152462; Wed, 24 Jul 2019 14:52:32 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id g11sm47906263wru.24.2019.07.24.14.52.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 24 Jul 2019 14:52:30 -0700 (PDT) Date: Wed, 24 Jul 2019 23:52:28 +0200 From: Tomas Vondra To: Melanie Plageman Cc: Jeff Davis , Pg Hackers Subject: Re: Memory Accounting Message-ID: <20190724215228.tlyj74ie4l2jn7ji@development> References: <027a129b8525601c6a680d27ce3a7172dab61aab.camel@j-davis.com> 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-dirty List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jul 23, 2019 at 06:18:26PM -0700, Melanie Plageman wrote: >On Thu, Jul 18, 2019 at 11:24 AM Jeff Davis wrote: > >> Previous discussion: >> https://postgr.es/m/1407012053.15301.53.camel%40jeff-desktop >> >> This patch introduces a way to ask a memory context how much memory it >> currently has allocated. Each time a new block (not an individual >> chunk, but a new malloc'd block) is allocated, it increments a struct >> member; and each time a block is free'd, it decrements the struct >> member. So it tracks blocks allocated by malloc, not what is actually >> used for chunks allocated by palloc. >> >> >Cool! I like how straight-forward this approach is. It seems easy to >build on, as well. > >Are there cases where we are likely to palloc a lot without needing to >malloc in a certain memory context? For example, do we have a pattern >where, for some kind of memory intensive operator, we might palloc in >a per tuple context and consistently get chunks without having to >malloc and then later, where we to try and check the bytes allocated >for one of these per tuple contexts to decide on some behavior, the >number would not be representative? > I think there's plenty of places where we quickly get into a state with enough chunks in the freelist - the per-tuple contexts are a good example of that, I think. >I think that is basically what Heikki is asking about with HashAgg, >but I wondered if there were other cases that you had already thought >through where this might happen. > 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 purpose is for Memory Bounded Hash Aggregation, but it may be >> useful in more cases as we try to better adapt to and control memory >> usage at execution time. >> >> >This approach seems like it would be good for memory intensive >operators which use a large, representative context. I think the >HashTableContext for HashJoin might be one? > Yes, that might me a good candidate (and it would be much simpler than the manual accounting we use now). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services