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 1rfzXQ-003Wja-UF for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Mar 2024 09:50:45 +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 1rfzXP-00CpmQ-7A for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Mar 2024 09:50:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rfzXO-00CpmF-N4 for pgsql-bugs@lists.postgresql.org; Fri, 01 Mar 2024 09:50:43 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rfzXK-002Iz0-UC for pgsql-bugs@lists.postgresql.org; Fri, 01 Mar 2024 09:50:41 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2d22b8801b9so23482831fa.0 for ; Fri, 01 Mar 2024 01:50:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dataegret-com.20230601.gappssmtp.com; s=20230601; t=1709286638; x=1709891438; darn=lists.postgresql.org; h=content-language:content-transfer-encoding:in-reply-to:mime-version :user-agent:date:message-id:from:references:to:subject:from:to:cc :subject:date:message-id:reply-to; bh=SXobm97+wjU19NBCZoQ1IlTZCYKgB/uMjJZgGBU804o=; b=OvHE1rVTiP5QhEqTWluoKfDgxSUFJ3EzAVbxKbKQfF/Miq9hQZjwZMn37gbx5Hg7TS QWgS9VaOYWNNlXWRVcfpygIX8f+/rGxqXnkAz7pvOFmklnd4Tj3y0r5H4Sc5L8G2L1as C6L26+gVOCs2YNukOclQR1qO0mE+uw7DZQeIKYJdVj707fQ7L+SjAVBdtHTgjn+JKC6o eZxWOnJ6ILzf0we64uGrW742pNWcQfGSFCwBUpdKrqcz5qIiR0YX8GGCBAmssR+YXc47 MkjTOb1mPDdtQDmzS5ejiDKxzvuqvWC2kXgcoiNhE0bRNhsJVnKg6czLJ/6OcW+eUbrE xfDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709286638; x=1709891438; h=content-language:content-transfer-encoding:in-reply-to:mime-version :user-agent:date:message-id:from:references:to:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SXobm97+wjU19NBCZoQ1IlTZCYKgB/uMjJZgGBU804o=; b=cCw0YtaPRCD6vGnLv7HaanZWXT2H1I2kInauvbDBY22TozPDbhpX1T+7jCacGPrBEX sXwT6AdDHUmL2DwSLn99z1OENGaCHugfJb0NP9LTwPavaL8NwZoBx+f2SjGkqPy+CXK3 eb5m9zqsxSj/NIWp0KYrMEu7RMCkOiWvWEHjeQGvslHbbWRDNY29YA1CodztepfnfIbf Uh8nBY+9TQQjBUWiVAj9pMfwYj9l9e5yoTw8IxQmlK0AKwmbaRYPXoMvAdOBgPoOfIcp ChZErqE2HdcXcPRwsh+uMVr0bTJeeJs9lov+syBo3bHti5EI7vhA9kSJf/bhA5+vB3Pz hQjA== X-Forwarded-Encrypted: i=1; AJvYcCW0E7vPPz5qjraSSJQNZFiY0C4vq25Gt1a/vBnO0LWG1iB66hSmITVvhNc4nVErmh9bfkY7uhRvhyLyeFsa+etuMTcWsp8WbtheGezUIPD3 X-Gm-Message-State: AOJu0YxY8uGK4KVt8onh2ofRUjfQrlx0IN5ODtCgAmWGVAQMXF09fLSO 9yLVwt5N1nHDa/lk09dgXUeaHsp7KWz37IX01uuJBn2UyKHf+0eyQJJ167wkX2R6MW3H0T90huc o2Z4= X-Google-Smtp-Source: AGHT+IF2LLKncnJ7R7r3BQOnFsN+FRyN0sjR33t33iIh8HT3P0B8mdrPRjAS+ELT+j/8T/7DQOjDHg== X-Received: by 2002:a2e:8850:0:b0:2d2:31b0:4f64 with SMTP id z16-20020a2e8850000000b002d231b04f64mr733512ljj.34.1709286637861; Fri, 01 Mar 2024 01:50:37 -0800 (PST) Received: from Alexeys-MacBook-Pro.local ([176.62.124.84]) by smtp.googlemail.com with ESMTPSA id m9-20020a05600c3b0900b0041294d015fbsm4877200wms.40.2024.03.01.01.50.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Mar 2024 01:50:37 -0800 (PST) Subject: Re: BUG #18349: ERROR: invalid DSA memory alloc request size 1811939328, CONTEXT: parallel worker To: Andrei Lepikhov , Tomas Vondra , pgsql-bugs@lists.postgresql.org References: <18349-83d33dd3d0c855c3@postgresql.org> <512ef568-336e-4087-ba23-f22b006d329d@postgrespro.ru> <7a1f5e5b-632f-4cc5-8078-b18a848e8924@postgrespro.ru> From: Alexey Ermakov Message-ID: <2ff6260b-8722-b301-cf7e-4e66a159778c@dataegret.com> Date: Fri, 1 Mar 2024 15:50:35 +0600 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.0; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 MIME-Version: 1.0 In-Reply-To: <7a1f5e5b-632f-4cc5-8078-b18a848e8924@postgrespro.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello! I reproduced generation of multiple temporary files and abnormal memory usage (in 'top' output both backend and parallel worker used around 3-4GB of RAM each, I often got OOM killer) but not memory allocation error so far. On machine where I first got error I always got the same request allocation size despite usage of different tables and conditions. Interesting that same number was on another bug report. Here is what I used: PostgreSQL 16.2 (Ubuntu 16.2-1.pgdg20.04+1), virtual machine with 8GB of RAM, 6GB swap and around 15-20GB free space for temp files. Almost all settings in postgresql.conf are default: ========================================================================= shared_buffers = 128MB effective_cache_size = 4GB work_mem = 1MB hash_mem_multiplier = 2 max_parallel_workers_per_gather = 1 jit = off random_page_cost = 4 seq_page_cost = 1 test data: ========================================================================= --test_1 table, 3.5M rows, 1..3M, n_distinct -0.8 create table test_1(a bigint); insert into test_1 select id from generate_series(1,3000000) gs(id); insert into test_1 select id from generate_series(1,500000) gs(id); analyze test_1; --test_2 table, 2 identical columns, 10M rows, 1..1M, n_distinct 933k create table test_2(a bigint, b bigint); insert into test_2 select gs.id, gs.id from generate_series(1,1000000) gs(id), generate_series(1,10) gs2(id); create index on test_2(b); analyze test_2; --test_3 table, 12.58M rows, 1..500k, 80k rows with same value, n_distinct 490k create table test_3(a bigint); insert into test_3 select gs.id from generate_series(1,500000) gs(id), generate_series(1,25) gs2(id); insert into test_3 select 999 from generate_series(1,80000); create index on test_3(a); analyze test_3; ========================================================================= set work_mem = '2MB'; explain (analyze, buffers) select test_2.a from test_2 left join test_3 on test_3.a = test_2.a and (test_3.a + 0) = test_3.a left join test_1 on test_1.a = test_2.a where test_2.b < 21500; ~224k temp files generated QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------  Gather  (cost=280156.31..385334.66 rows=308388 width=8) (actual time=16342.410..35577.099 rows=12349500 loops=1)    Workers Planned: 1    Workers Launched: 1    Buffers: shared hit=1077677 read=14901, temp read=245247 written=912164    ->  Parallel Hash Right Join  (cost=279156.31..353495.86 rows=181405 width=8) (actual time=16340.950..29837.671 rows=6174750 loops=2)          Hash Cond: (test_1.a = test_2.a)          Buffers: shared hit=1077677 read=14901, temp read=245247 written=912164          ->  Parallel Seq Scan on test_1  (cost=0.00..36075.24 rows=2058824 width=8) (actual time=0.142..169.721 rows=1750000 loops=2)                Buffers: shared hit=586 read=14901          ->  Parallel Hash  (cost=276962.04..276962.04 rows=133701 width=8) (actual time=4391.620..4391.624 rows=3087375 loops=2)                Buckets: 131072 (originally 131072)  Batches: 65536 (originally 4)  Memory Usage: 32352kB                Buffers: shared hit=1077043, temp read=72723 written=387876                ->  Nested Loop Left Join (cost=2814.38..276962.04 rows=133701 width=8) (actual time=2.884..567.014 rows=3087375 loops=2)                      Buffers: shared hit=1077043                      ->  Parallel Bitmap Heap Scan on test_2 (cost=2813.95..108618.25 rows=133701 width=8) (actual time=2.865..11.149 rows=107495 loops=2)                            Recheck Cond: (b < 21500)                            Heap Blocks: exact=652                            Buffers: shared hit=1422                            ->  Bitmap Index Scan on test_2_b_idx  (cost=0.00..2757.12 rows=227292 width=0) (actual time=3.901..3.901 rows=214990 loops=1)                                  Index Cond: (b < 21500)                                  Buffers: shared hit=250                      ->  Index Only Scan using test_3_a_idx on test_3  (cost=0.43..1.25 rows=1 width=8) (actual time=0.001..0.003 rows=29 loops=214990)                            Index Cond: (a = test_2.a)                            Filter: ((a + 0) = a)                            Heap Fetches: 0                            Buffers: shared hit=1075621  Planning:    Buffers: shared hit=8  Planning Time: 0.576 ms  Execution Time: 35907.097 ms Unfortunately that plan is not very stable with different parameters and on different versions of postgresql (I wasn't able to get it on machine with enough resources to test quickly). Sometimes planner wants to hash join with test_3 table instead of test_1 or join with test_1 table but in inner part. With other plans it won't reproduce. with work_mem = 4MB we get 141k files, with work_mem = 1MB we get 66k files. more files = more memory consumed = bigger execution time. with more than 700-1000k files system became unresponsive. in that test I've got: >Batches: 65536 (originally 4) with prod data largest number of batches I've seen: >Buckets: 131072 (originally 131072)  Batches: 262144 (originally 16)  Memory Usage: 4736kB Perhaps that helps somehow. >So, can Alexey apply a trivial patch and check whether the bug has gone? yes, I think it's possible to test with prod data but I don't fully understand how would you like to change given line. Thanks, Alexey Ermakov On 2024-02-29 09:36, Andrei Lepikhov wrote: > I found one possible weak point in the code of PHJ: > ExecParallelHashJoinSetUpBatches: > > pstate->batches = dsa_allocate0(hashtable->area, >     EstimateParallelHashJoinBatch(hashtable) * nbatch); > > It could explain why we have such a huge memory allocation with a size > not bonded to a power of 2. > Also, it makes more sense that we already have a second report [1] > with the same value of DSA allocation. > So, can Alexey apply a trivial patch and check whether the bug has gone? > > [1] > https://www.postgresql.org/message-id/flat/PAYP264MB3368AF4D63704E370B204C39FF582%40PAYP264MB3368.FRAP264.PROD.OUTLOOK.COM >