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 1iz0mT-0007Ly-5D for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Feb 2020 16:10:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iz0mR-0000nO-T3 for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Feb 2020 16:10:27 +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 1iz0mR-0000nC-K0 for pgsql-hackers@lists.postgresql.org; Tue, 04 Feb 2020 16:10:27 +0000 Received: from mail-lf1-f65.google.com ([209.85.167.65]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iz0mN-0005TO-L0 for pgsql-hackers@postgresql.org; Tue, 04 Feb 2020 16:10:27 +0000 Received: by mail-lf1-f65.google.com with SMTP id f24so12586690lfh.3 for ; Tue, 04 Feb 2020 08:10:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=K7FOsyFcG6JPfvMLqnogDYe6+EnPmQsCD7tJS+1pBo8=; b=ikK3xsEZG3zrFQW5+OwCqy90JijYuPCeES+GuACQoJoR5xBS7GbnBl5mijS2+Q89ZK 7I+rVFWwyY6PVTDBT6wPuMtTBaehPa0sbv0Akoiv/7B9ma9TEdz1GUrFO4vYj/B5kA0q 9bNC7/N6ze+f9kqnOQy6ExcZNFiCHpQSFkw1KQxsfYY9xJA4J8S+VlgLXiV5M0s15e8b 8QbCQjq2LVzlnBaX+/e32L0bQhMf59kayij4977n3spl0C9NqtcVKObxQR9u4EMMZpXK G+QqnBn7QU5DbhsK4KqVqlXwAlahy2ETT1Z0Jg1C7ZrrrhqAebqsPIkSE9sr5IcjBHJm 4igg== X-Gm-Message-State: APjAAAVFlOhgfMR6tV+H0dt2hGtVWlv40JVHzMP4KUAgz8x8dgqjvuSB 4hu7QzScvV/7EGCIX4x/pgbhxomr3xI= X-Google-Smtp-Source: APXvYqzWCwTAcMzTP7VsxbkD75GuNtgmIFKKL4MQ40qmMcjS0uHkIxoLjxoeCtJBkcM0JfyOfAffBg== X-Received: by 2002:a19:6449:: with SMTP id b9mr15341449lfj.5.1580832622288; Tue, 04 Feb 2020 08:10:22 -0800 (PST) Received: from [192.168.43.54] (85-76-100-178-nat.elisa-mobile.fi. [85.76.100.178]) by smtp.gmail.com with ESMTPSA id y29sm11872763ljd.88.2020.02.04.08.10.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Feb 2020 08:10:21 -0800 (PST) Subject: Re: Memory-Bounded Hash Aggregation To: Jeff Davis , Peter Geoghegan Cc: Tomas Vondra , Taylor Vesely , Adam Lee , Melanie Plageman , PostgreSQL Hackers References: <507ac540ec7c20136364b5272acbcd4574aa76ef.camel@j-davis.com> <20190703001753.s5e5cme3ucjgeu6c@development> <20190711155520.zqipi23o3yybx7ym@development> <9be86fc1adc315f69b8af4b379087ab451008d8a.camel@j-davis.com> <20190802064405.GA94036@mars.local> <617fea8258ba3df7bc8b21cd7f3afc219f2383d9.camel@j-davis.com> <20190802152123.ichmacgx3y3x7dc3@development> <20191214173225.3kzsbncr6brpxdll@development> <775d754c-9a61-5363-6998-f87d15f1b592@iki.fi> <6e2b4a4475a690fa515829950aeed935696b0df3.camel@j-davis.com> <97c46a59c27f3c38e486ca170fcbc618d97ab049.camel@j-davis.com> From: Heikki Linnakangas Message-ID: Date: Tue, 4 Feb 2020 18:10:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <97c46a59c27f3c38e486ca170fcbc618d97ab049.camel@j-davis.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/02/2020 20:29, Jeff Davis wrote: > 1. Use a minheap for the freelist. The original design used an array > that had to be sorted between a read (which frees a block) and a write > (which needs to sort the array to consume the lowest block number). The > comments said: > > * sorted. This is an efficient way to handle it because we expect > cycles > * of releasing many blocks followed by re-using many blocks, due to > * the larger read buffer. > > But I didn't find a case where that actually wins over a simple > minheap. With that in mind, a minheap seems closer to what one might > expect for that purpose, and more robust when the assumptions don't > hold up as well. If someone knows of a case where the re-sorting > behavior is important, please let me know. A minheap certainly seems more natural for that. I guess re-sorting the array would be faster in the extreme case that you free almost all of the blocks, and then consume almost all of the blocks, but I don't think the usage pattern is ever that extreme. Because if all the data fit in memory, we wouldn't be spilling in the first place. I wonder if a more advanced heap like the pairing heap or fibonacci heap would perform better? Probably doesn't matter in practice, so better keep it simple... > Changing to a minheap effectively solves the problem for HashAgg, > though in theory the memory consumption of the freelist itself could > become significant (though it's only 0.1% of the free space being > tracked). We could fairly easily spill parts of the freelist to disk, too, if necessary. But it's probably not worth the trouble. > 2. Lazily-allocate the read buffer. The write buffer was always lazily- > allocated, so this patch creates better symmetry. More importantly, it > means freshly-rewound tapes don't have any buffer allocated, so it > greatly expands the number of tapes that can be managed efficiently as > long as only a limited number are active at once. Makes sense. > 3. Allow expanding the number of tapes for an existing tape set. This > is useful for HashAgg, which doesn't know how many tapes will be needed > in advance. I'd love to change the LogicalTape API so that you could allocate and free tapes more freely. I wrote a patch to do that, as part of replacing tuplesort.c's polyphase algorithm with a simpler one (see [1]), but I never got around to committing it. Maybe the time is ripe to do that now? [1] https://www.postgresql.org/message-id/420a0ec7-602c-d406-1e75-1ef7ddc58d83@iki.fi - Heikki