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 1iTr1r-0007if-Pq for pgsql-bugs@arkaria.postgresql.org; Sun, 10 Nov 2019 17:29:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iTr1q-0000Kg-Ey for pgsql-bugs@arkaria.postgresql.org; Sun, 10 Nov 2019 17:29:34 +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 1iTr1q-0000KY-3f for pgsql-bugs@lists.postgresql.org; Sun, 10 Nov 2019 17:29:34 +0000 Received: from mail-lj1-x242.google.com ([2a00:1450:4864:20::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iTr1m-0005AG-26 for pgsql-bugs@lists.postgresql.org; Sun, 10 Nov 2019 17:29:33 +0000 Received: by mail-lj1-x242.google.com with SMTP id v8so11267746ljh.5 for ; Sun, 10 Nov 2019 09:29:29 -0800 (PST) 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:content-transfer-encoding:in-reply-to; bh=F7mDDcfDlcgxjtjqoNdNkd92JHr1XCKrbbISCSMNrkU=; b=XR7M1kwiCSw0d8WvwETil1RYs/nfGqvdWCG78xN7pt5xvk1HCDG5VHAUq9F/VcSkAn 5Tu52+hos1akgzTfDZgWfG7gc72WE4WQ9DO8cRusVd6CK9XLwn76+vR/w8/Koo16cAvT wLUZnyLKhySeudEJuZZxjYDfLiFgGL8ymjBE0Aw8eVnKhAogtbhC4D6ZO9s6kgsrJ2JG nMRflmOEa2mVA20y/DepeCgnAR6JhS31WT/VACOfXuLxYtMHnhB5iqLwM+kc3xKzNebb gFIlhxfglDyV3xgaVT+wKFfx5NtC89d5ncXjKbQrkiXzvHZ9hedqSGIATolMTZmTDOiN j2eg== 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:content-transfer-encoding :in-reply-to; bh=F7mDDcfDlcgxjtjqoNdNkd92JHr1XCKrbbISCSMNrkU=; b=sReEXo3RBBH+vL0USmwI2GvPtHl18u+tdLxEZ56YNDDiIUPAKpeoQo5KhHP8seUJrn heKre5lmBAzbl5tyHiQKZ8XLbBh2+u2Bz6XYTW3+kBzT90s8kz6BWErTddESWdp0srp6 6Ar1SlXojmEbFpdHPgbBLaWlBxova4zlcAjCflxRlsR41L4WQ1+uB77zoZhhea/HsUMp pAw42HzlUzbijW+zTYitlJbfYOmPfXfywk4FS8zNW+dltmk4A5e15tkSQMI51wsgrZMX RJUYjehHS8Yt4lVkS2vPTXaApSYzejswBHTKpY0io0ViR5JhsFxh/m4TnYVgMwxdkekr e+2g== X-Gm-Message-State: APjAAAVqDavYE9LyRJ2MIdS2rGNH/1HPIui3+MtAlCQFDe424UGft1WC 8m12xYAB2d9sS3CMFypn+0PZFw== X-Google-Smtp-Source: APXvYqzS/XY2yWSytzQx9O1kuMvbhFR+Ud3Htv9+6Mad2/iZqWGzxkyiuaNsngPyP7sZYegBeOza2Q== X-Received: by 2002:a2e:9a0c:: with SMTP id o12mr3410087lji.141.1573406968323; Sun, 10 Nov 2019 09:29:28 -0800 (PST) Received: from localhost ([217.8.174.100]) by smtp.gmail.com with ESMTPSA id o196sm7088902lff.59.2019.11.10.09.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2019 09:29:27 -0800 (PST) Date: Sun, 10 Nov 2019 18:29:25 +0100 From: Tomas Vondra To: James Coleman Cc: Thomas Munro , PostgreSQL mailing lists Subject: Re: BUG #16104: Invalid DSA Memory Alloc Request in Parallel Hash Message-ID: <20191110172925.mlkglpwzwego3wtp@development> References: <20191109095456.csw3f5rhc364z5fg@development> <20191109182735.ewficcv2hibbyzl4@development> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Nov 10, 2019 at 09:15:06AM -0500, James Coleman wrote: >On Sat, Nov 9, 2019 at 10:44 PM Thomas Munro wrote: >> >> On Sun, Nov 10, 2019 at 3:25 PM James Coleman wrote: >> > So I should have run the earlier attached plan with VERBOSE, but >> > here's the interesting thing: the parallel hash node's seq scan node >> > outputs two columns: let's call them (from the redacted plan) >> > items.system_id and items.account_id. The first (system_id) is both >> > not null and unique; the second (account_id) definitely has massive >> > skew. I'm not very up-to-speed on how the hash building works, but I >> > would have (perhaps naïvely?) assumed that the first column being >> > unique would make the hash keys very likely not to collide in any >> > significantly skewed way. Am I missing something here? >> >> Hrm. So the compound key is unique then. I was assuming up until now >> that it had duplicates. The hashes of the individual keys are >> combined (see ExecHashGetHashValue()), so assuming there is nothing >> funky about the way citext gets hashed (and it's not jumping out at >> me), your unique keys should give you uniform hash values and thus >> partition size, and repartitioning should be an effective way of >> reducing hash table size. So now it sounds like you have a simple >> case of underestimation, but now I'm confused about how you got a >> 344MB hash table with work_mem = 150MB: > Why would the compound key matter? The join only does this Hash Cond: (group_details.group_number = items.system_id) so the only thing that really matters when it comes to skew is system_id, i.e. the citext column. The other column is used by aggregation, but that's irrelevant here, no? >Looking at the source, citext's hash is a pretty standard call to >hash_any, so I don't see how that would lead to any oddities (barring >an intentional hash collision etc., but this is real data). > >Do you have any theories about where the underestimation is happening? >It knows the number of rows reasonably well. The one thing I'm not >sure about yet is the row width = 16. The account_id is a bigint, so >we can assume it knows the size properly there. The system_id being >citext...I think the 8 bytes it has for that is probably a bit low on >average, but I'm not sure yet by how much (I'm going to run a query to >find out). Hmm. Query came back, and average length is just shy of >7.9...so the 16 byte row size is looking pretty good. So I'm at a loss >of why/what it would be underestimating (does it know about >uniqueness/non-uniqueness? could that be a factor?)? > I think the easiest thing we can do is running a couple of queries collecting useful stats, like -- the most frequent system_id values select system_id, count(*), sum(length(system_id)) from items gtoup by system_id order by count(*) desc limit 100; -- the largest group by system_id select system_id, count(*), sum(length(system_id)) from items gtoup by system_id order by sum(length(system_id)) desc limit 100; That should tell us if there's something off. >I also don't know why it seems to regularly fail on the primary, but >not on the sync, unless we adjust the work_mem up. I've double-checked >all GUCs and the only differences are things related to replication >that you'd expect to be different on primary/replica. > >I know there are some things that execute differently on replicas, so >I assume it's something like that, but I don't know what specifically >would cause this here. > Hmm, I haven't realized it fails on primary more often. That's certainly strange, I don't have a very good idea why that could be happening. Do the query plans look the same? How does the EXPLAIN ANALYZE look like? >> Buckets: 4194304 (originally 4194304) Batches: 32768 (originally >> 4096) Memory Usage: 344448kB >> >> And I'm confused about what was different when it wanted the crazy >> number of batches. > >I'm not quite sure how to find out; if you have any ideas, I'd love to >hear them. The one thing I can think of to try is to slowly increase >work_mem (since setting it to 500MB reproduced the error on the >replica) and see if the bucket info starts to trend up. > Chances are it actually happened to detect skew and disabled the growth, hence the table grew above work_mem. We should probably add info about this (skew, disabling growth) to EXPLAIN ANALYZE verbose mode. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services