Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t9U20-00ASgu-7A for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Nov 2024 18:48:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t9U1w-00CPYE-Es for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Nov 2024 18:48:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t9U1w-00CPY6-39 for pgsql-bugs@lists.postgresql.org; Fri, 08 Nov 2024 18:48:24 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t9U1n-000tWH-O6 for pgsql-bugs@postgresql.org; Fri, 08 Nov 2024 18:48:23 +0000 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7205b6f51f3so2063300b3a.1 for ; Fri, 08 Nov 2024 10:48:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1731091694; x=1731696494; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=zAI6fiXAIo7vhkcXrkXaDUCjbxAM96Y5XDRD68M+PsY=; b=ca+ZWZKHJnRoMsIzjxDAgzvurUvIuLvDRyKjHGbHRT7gzpSa89dTziflrMGhiLSell 5wtYhDvfUi4aS+nxWX/MSlQyShHxaehMRaIxxazwqnVKGZHowOAMGemCv7Mww/su4uL0 1k5iRjuEFwoJcrif6V9trOnZ3Gdonaj4163AtCdk39Rx7MYgS+0/LtoUOy0fGQ0FC3y9 RJDAcG1xz/+32HCotVeLkH5emcnk6iPxhonk1714QdJa9OXEvJxO/tT+E8oULSsgLGyG KB12m4R9J15E+LpGzUYAgnWjunYbPEQz0C3K348/+L10bvSCATGvoQPsGWmUqQg7B1Gi vI/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731091694; x=1731696494; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zAI6fiXAIo7vhkcXrkXaDUCjbxAM96Y5XDRD68M+PsY=; b=GoDvW09zKqzrFvrjZvX52MZiqymhLRi1z6KHda+hpN0yRTb/rVNwEyKJCU4zrDYjo+ +Vb3RDwsxBKPKhUjlmg51M/te9ofgDz0QE3bwY6dl8u/+TLtN3qlFbb82X61WuSwUZWw Um3R2iXDosi+qLFwYZkTh0hcUdrxRUUhsPQQj+I+R7NsgV8/XdbEnEuQlnn4ZqOoOGUi dBNeseHgTCrQ+O0m+AxqMmVSKQ9uWXnpPBwMdEEBTaT0e49qmCXziiWZB7W6aaHGP3tk maFKHwSU6PmOO4sbsWwREzHkvaqzLdJaY846CWFRVTDy8CkY86jVZIzwvwWBVm0svQmy YuWw== X-Gm-Message-State: AOJu0YyG6Rf8XeqLa3GI04lP7NXXOFyrKbyIlD4iY2yAbPfXkuRWAHY0 5ykTrRLQb3KaclJuhfli09liuRYBqv4sJv7T4MR4bGM+XDQpWsPNWEnjmWgCJwzw/GAl4kEr8+M = X-Google-Smtp-Source: AGHT+IFm+/vvtgj/Li48mVgWKjKaQTHxw2YAdBF52bknOmX9vuN6di/zykoFv17ST/njqA+WMkD6gA== X-Received: by 2002:a05:6a21:328d:b0:1dc:32a:d409 with SMTP id adf61e73a8af0-1dc22b51816mr4695690637.39.1731091694419; Fri, 08 Nov 2024 10:48:14 -0800 (PST) Received: from jeff-laptop.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72407a1a800sm4123424b3a.164.2024.11.08.10.48.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 10:48:14 -0800 (PST) Message-ID: <33bcc1ef6c4acf80f4fb87c4d9d284c61abb3652.camel@j-davis.com> Subject: Re: HashAgg degenerate case From: Jeff Davis To: Andres Freund Cc: pgsql-bugs@postgresql.org Date: Fri, 08 Nov 2024 10:48:12 -0800 In-Reply-To: <4tbbgdqqxvmy37fk75p36azkovyhrjhnul46lntj52jlobphf3@nxaqlaqupg2l> References: <4tbbgdqqxvmy37fk75p36azkovyhrjhnul46lntj52jlobphf3@nxaqlaqupg2l> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2024-11-08 at 11:41 -0500, Andres Freund wrote: > That'll often be *way* slower though. Both because acquiring and > faulting-in > memory is far from free and because it'd often lead to starting to > grow the > hashtable from a small size again. I assume this statement also applies to my more recent message? https://www.postgresql.org/message-id/4e9bfa724b301b55a2a242ebebcddea62b2e1= 292.camel%40j-davis.com > I think this patch would lead to way bigger regressions than the > occasionally > too large hashtable does. I'm not saying that we shouldn't do > something about > that, but I don't think it can be this. The case I observed had a bucket array of ~8M, which took about 200MB, while the hash_mem_limit was only 128MB. I'm not quite sure how it got into that state (probably related to the doubling behavior of the bucket array), but once it did, it was incredibly slow because the first tuple always triggered spilling to disk. I can think of two approaches to solve it: 1. Detect the case when there is an obvious imbalance, like the metacxt using 90+% of the memory limit before a batch even begins, and then destroy/recreate the hash table. 2. Change the rules in hash_agg_check_limits() so that reasonable progress can be made regardless of the size of metacxt. For instance, don't count meta_mem as taking more than 75% of the limit. Or always permit a new group if there are fewer than 25% of the hash_ngroups_limit. Thoughts? Regards, Jeff Davis