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 1iTDiR-0005av-3E for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Nov 2019 23:30:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iTDiP-00019r-U8 for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Nov 2019 23:30:53 +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 1iTDiP-00019k-CD for pgsql-bugs@lists.postgresql.org; Fri, 08 Nov 2019 23:30:53 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iTDiL-0003gd-MD for pgsql-bugs@lists.postgresql.org; Fri, 08 Nov 2019 23:30:51 +0000 Received: by mail-lj1-x244.google.com with SMTP id n21so7893828ljg.12 for ; Fri, 08 Nov 2019 15:30:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=e3SQCXcztnoZXTwwwpNHUsYYubsF133ukm67iDuymIY=; b=Lhtt9AXIFFt015j8X3niEUwC9uxb/TnzTrxT5CUMYCNcDm9mikfGHjKl5QUX1YkK0P ffCKE9IBz9L8+pXiVS7GyqvQW2l0wH3vVwbJKdTNjafr4FV0RLZyRZneDrwnb0v4iRPM LgDgQ6vYtsiFofh+LIG4QqwvPJGpyV3FsSid+vM6iFOkX0gX6wmFyWVCiJ3foKh9xx5D cJ/rvqOH3PRCPyJPFnhad5xTrnxq40eoN2TvquQUROGRmDIi53up9jhhmf63/f4YuE+7 eJvMty2mo1Q6SFH1M6HH7uw4R/+ESDMtpH7L+8TIus32nV5+LKWvD7zhySbwiZ23/HDD niQA== 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:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=e3SQCXcztnoZXTwwwpNHUsYYubsF133ukm67iDuymIY=; b=srNqHGGVouXmgscGhg9Qfuc/eccryf+S6ZlWQGYY1om8bLBtMyq3fsAPf2PXLrDHiy 72+HRJvvUdfdWklgEBJy7HmJHPGf3JXYNS8AQ0x7PgB3h7KmWTbt0Qw4f2rVJtQU4OgF BZFTPYTWfJ9lpMeX4Q/oCqwmIb4VZ1smYYkEAm277it9MrgJvJexZcKWvbPOv8nkf2hn zXJgcxjmE5sZTIYUeNu/IrVeZvKFkPXXxkGXoerrU4YMzWj6k43SeIZ4Apc8a33zvq9m 9MaEz+65ZbmHAY4h8d3bp2sc1rGk8i/xQTIRwdsGmoA8EvRgcA1hJyLKYMH4FJPGzPIS A0CA== X-Gm-Message-State: APjAAAWMybjHyN2f+Fyc3DjlL1kh/lCQAdo5fA1NinOY5vQuVJPR4FkF BfjEK0VLwAlP1MOLI1ppbJhGPm4Rc85Nsg== X-Google-Smtp-Source: APXvYqxma5vM+mMNxdUtn5rNcv1uazZs3D7mrXxwX6LxYNfOUHfxDZAeoHrcQ45ugtGAbMGCsKhDgg== X-Received: by 2002:a2e:a410:: with SMTP id p16mr8465351ljn.46.1573255848056; Fri, 08 Nov 2019 15:30:48 -0800 (PST) Received: from localhost ([217.20.186.97]) by smtp.gmail.com with ESMTPSA id m8sm2858947ljj.80.2019.11.08.15.30.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2019 15:30:47 -0800 (PST) Date: Sat, 9 Nov 2019 00:30:44 +0100 From: Tomas Vondra To: jtc331@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16104: Invalid DSA Memory Alloc Request in Parallel Hash Message-ID: <20191108233044.awe5owizhowebyy5@development> References: <16104-dc11ed911f1ab9df@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <16104-dc11ed911f1ab9df@postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Nov 08, 2019 at 09:52:16PM +0000, PG Bug reporting form wrote: >The following bug has been logged on the website: > >Bug reference: 16104 >Logged by: James Coleman >Email address: jtc331@gmail.com >PostgreSQL version: 11.5 >Operating system: Debian >Description: > >We have a query that, after a recent logical migration to 11.5, ends up with >a parallel hash join (I don't think the query plan/query itself is important >here, but if needed after the rest of the explanation, I can try to redact >it for posting). The query results in this error: > >ERROR: invalid DSA memory alloc request size 1375731712 > >(the size changes sometimes significantly, but always over a GB) > >At first glance it sounded eerily similar to this report which preceded the >final release of 11.0: >https://www.postgresql.org/message-id/flat/CAEepm%3D1x48j0P5gwDUXyo6c9xRx0t_57UjVaz6X98fEyN-mQ4A%40mail.gmail.com#465f3a61bea2719bc4a7102541326dde >but I confirmed that the patch for that bug was applied and is in 11.5 (and >earlier). > >We managed to reproduce this on a replica, and so were able to attach gdb in >production to capture a backtrace: > >#0 errfinish (dummy=dummy@entry=0) at >./build/../src/backend/utils/error/elog.c:423 >#1 0x000055a7c0a00f79 in elog_finish (elevel=elevel@entry=20, >fmt=fmt@entry=0x55a7c0babc18 "invalid DSA memory alloc request size %zu") at >./build/../src/backend/utils/error/elog.c:1385 >#2 0x000055a7c0a2308b in dsa_allocate_extended (area=0x55a7c1d6aa38, >size=1140850688, flags=flags@entry=4) at >./build/../src/backend/utils/mmgr/dsa.c:677 >#3 0x000055a7c079bd17 in ExecParallelHashJoinSetUpBatches >(hashtable=hashtable@entry=0x55a7c1db2740, nbatch=nbatch@entry=2097152) at >./build/../src/backend/executor/nodeHash.c:2889 > ... I've briefly looked at this today, and I think the root cause is somewhat similar to what is described in [1] where we simply increase the number of batches in an effort to keep batch contents in work_mem, but ignoring that each batch requires quite a bit of memory. So we end up with a lot of batches where each is small enough to fit into work_mem, but we need much more than work_mem to track the batches. This seems to be about the same problem, except that instead of forgeting about BufFile, the parallel hash join ignores this: pstate->batches = dsa_allocate0(hashtable->area, EstimateParallelHashJoinBatch(hashtable) * nbatch); Looking at the backtrace, you ended up with nbatch=2097152 and it failed trying to allocate 1140850688 bytes, i.e. exactly 544B per batch. The EstimateParallelHashJoinBatch combines a bunch of structs and does depend on the number of participants, and by my rough calculation 544B means ~13 participants. Any idea if that matches the number of parallel workers for this query? Can you try running the query with fewer workers per gather? IMHO this has the same issue as the non-parallel case described in [1] in that we don't really consider this memory when planning the query, which means we may accept hash join for queries where we know the number of batches will be too high during planning. We need to reject parallel hash join in those cases. Of course, I don't know if this is such case - perhaps this is a case of underestimation and we only learn the actual number while executing the query, at which point it's too late to reject PHJ. I suppose the right solution in that case would be some form of BNL, as discussed in [2] but I'm not sure how far are we from having that, or if that works for PHJ. regards [1] https://www.postgresql.org/message-id/flat/20190504003414.bulcbnge3rhwhcsh@development [2] https://www.postgresql.org/message-id/CA%2BhUKGKWWmf%3DWELLG%3DaUGbcugRaSQbtm0tKYiBut-B2rVKX63g%40mail.gmail.com -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services