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.92) (envelope-from ) id 1jGa0G-0004Yn-61 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Mar 2020 03:13:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jGa0E-0000e8-TK for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Mar 2020 03:13:18 +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 1jGa0E-0000de-Fl for pgsql-hackers@lists.postgresql.org; Tue, 24 Mar 2020 03:13:18 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jGa0A-0007Vq-UX for pgsql-hackers@postgresql.org; Tue, 24 Mar 2020 03:13:17 +0000 Received: by mail-wm1-x342.google.com with SMTP id d1so1931951wmb.2 for ; Mon, 23 Mar 2020 20:13:14 -0700 (PDT) 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:in-reply-to; bh=v2/22+8lp/4GzNkRT/iRyt6gdyREA2/6p8UmAYohaLA=; b=xH81hClKz5BeCEzbmY65mlJLJk1na9nXACXYwuZ48IEdERZ5w//2Rau61ykmZ4svPZ M2JVnAWo3AjyLi/h54bNkJoYhl1VdQWuGJnNeKcDL1A77Wk5AFAG9WPCTKZcRrYY+PVH jPqLxDmAzcTF264UV8+JWPCU/IKIaG26B5kBx+Pgn5N5Dg0YDrH3IG2vaWLfiv/0Wt5C VZ/ner335mFCueyqwXs4HIT0GJdBMURcw7l2lhoA3614+AT/68eKqzyL+X8SFjK3mGyy JpliGteFzHDifFqlYZ8VLaUd4jycZHYBfiesSk5FI3pTXn1F8aRBbOB/dz/+ajRChGly DLGg== 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:in-reply-to; bh=v2/22+8lp/4GzNkRT/iRyt6gdyREA2/6p8UmAYohaLA=; b=jJwhDwkn8+hTyC8DhgMaJn4WsuXuW/deROJ3zmKLJPNd9wScGOCpv9A3lKElbLZmek FUlKKSTmdeRoJ1q9jY3/yuDb3xYa2ySvdLRXz4PoyFjWaRl6et1UOsnt6CJmX+nqloUZ Ouz9aX9XXurjSSn541WkLnzJSVZxsjgxsfiqqaJ37nDBtamACvuZRKEHNmA2D1q9zS9R 3b6guKH+K908Oj/CscJ1i9nfYdHs7ran4tq1yXelRZkLAru4XrJK5dqB/+yA+N0UyFqD mPWAcy5RLW97JyHvpZltWgDo/nFGWCMNI/shcnHDwsQ0VN8ehBx9vWe6B7PF6Wv44WwO NPsA== X-Gm-Message-State: ANhLgQ3EiFxMNxb9xQGvQLM1LJJ3eT8qp1WxTzL/aUr79W6RhQlZXHad M2jNoT0Gt1X2R97ZgiGxPGujLg== X-Google-Smtp-Source: ADFU+vtMTQKUHARlmgtVO2CFBaETHXh89Nt09g4YN3BKX5d6CTvEvt6iapyhIelYd4Qz9Lx+kKg7yQ== X-Received: by 2002:a1c:408b:: with SMTP id n133mr2681062wma.182.1585019593178; Mon, 23 Mar 2020 20:13:13 -0700 (PDT) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id 9sm2235711wmo.38.2020.03.23.20.13.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Mar 2020 20:13:11 -0700 (PDT) Date: Tue, 24 Mar 2020 04:13:09 +0100 From: Tomas Vondra To: Pengzhou Tang Cc: Richard Guo , Jesse Zhang , Richard Guo , Amit Kapila , Michael Paquier , PostgreSQL-development , Andres Freund , Andrew Gierth Subject: Re: Parallel grouping sets Message-ID: <20200324031309.nryb2c6reecgeh4t@development> References: <20200319020914.zkcjpm5r4yj7umqx@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="4jhy73xwzlhdkyvz" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --4jhy73xwzlhdkyvz Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Fri, Mar 20, 2020 at 07:57:02PM +0800, Pengzhou Tang wrote: >Hi Tomas, > >I rebased the code and resolved the comments you attached, some unresolved >comments are explained in 0002-fixes.patch, please take a look. > >I also make the hash spill working for parallel grouping sets, the plan >looks like: > >gpadmin=# explain select g100, g10, sum(g::numeric), count(*), max(g::text) >from gstest_p group by cube (g100,g10); > QUERY PLAN >------------------------------------------------------------------------------------------- > Finalize MixedAggregate (cost=1000.00..7639.95 rows=1111 width=80) > Filtered by: (GROUPINGSETID()) > Group Key: () > Hash Key: g100, g10 > Hash Key: g100 > Hash Key: g10 > Planned Partitions: 4 > -> Gather (cost=1000.00..6554.34 rows=7777 width=84) > Workers Planned: 7 > -> Partial MixedAggregate (cost=0.00..4776.64 rows=1111 width=84) > Group Key: () > Hash Key: g100, g10 > Hash Key: g100 > Hash Key: g10 > Planned Partitions: 4 > -> Parallel Seq Scan on gstest_p (cost=0.00..1367.71 >rows=28571 width=12) >(16 rows) > Hmmm, OK. I think there's some sort of memory leak, though. I've tried running a simple grouping set query on catalog_sales table from TPC-DS scale 100GB test. The query is pretty simple: select count(*) from catalog_sales group by cube (cs_warehouse_sk, cs_ship_mode_sk, cs_call_center_sk); with a partial MixedAggregate plan (attached). When executed, it however allocates more and more memory, and eventually gets killed by an OOM killer. This is on a machine with 8GB of RAM, work_mem=4MB (and 4 parallel workers). The memory context stats from a running process before it gets killed by OOM look like this TopMemoryContext: 101560 total in 6 blocks; 7336 free (6 chunks); 94224 used TopTransactionContext: 73816 total in 4 blocks; 11624 free (0 chunks); 62192 used ExecutorState: 1375731712 total in 174 blocks; 5391392 free (382 chunks); 1370340320 used HashAgg meta context: 315784 total in 10 blocks; 15400 free (2 chunks); 300384 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ... That's 1.3GB allocated in ExecutorState - that doesn't seem right. FWIW there are only very few groups (each attribute has fewer than 30 distinct values, so there's only about ~1000 groups. On master it works just fine, of course. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --4jhy73xwzlhdkyvz Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="plan.txt" QUERY PLAN ------------------------------------------------------------------------------------------------------ Finalize MixedAggregate (cost=1000.00..5901854.45 rows=10416 width=20) Filtered by: (GROUPINGSETID()) Group Key: () Hash Key: cs_warehouse_sk, cs_ship_mode_sk, cs_call_center_sk Hash Key: cs_warehouse_sk, cs_ship_mode_sk Hash Key: cs_warehouse_sk Hash Key: cs_ship_mode_sk, cs_call_center_sk Hash Key: cs_ship_mode_sk Hash Key: cs_call_center_sk, cs_warehouse_sk Hash Key: cs_call_center_sk -> Gather (cost=1000.00..5901348.48 rows=41664 width=24) Workers Planned: 4 -> Partial MixedAggregate (cost=0.00..5896182.08 rows=10416 width=24) Group Key: () Hash Key: cs_warehouse_sk, cs_ship_mode_sk, cs_call_center_sk Hash Key: cs_warehouse_sk, cs_ship_mode_sk Hash Key: cs_warehouse_sk Hash Key: cs_ship_mode_sk, cs_call_center_sk Hash Key: cs_ship_mode_sk Hash Key: cs_call_center_sk, cs_warehouse_sk Hash Key: cs_call_center_sk -> Parallel Seq Scan on catalog_sales (cost=0.00..4096256.32 rows=35996432 width=12) (22 rows) --4jhy73xwzlhdkyvz Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="stats.txt" TopMemoryContext: 101560 total in 6 blocks; 7336 free (6 chunks); 94224 used TopTransactionContext: 73816 total in 4 blocks; 11624 free (0 chunks); 62192 used ExecutorState: 1375731712 total in 174 blocks; 5391392 free (382 chunks); 1370340320 used HashAgg meta context: 315784 total in 10 blocks; 15400 free (2 chunks); 300384 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 524288 total in 7 blocks; 15336 free (2 chunks); 508952 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used ExprContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used Operator class cache: 8192 total in 1 blocks; 552 free (0 chunks); 7640 used smgr relation table: 16384 total in 2 blocks; 4592 free (2 chunks); 11792 used pgstat TabStatusArray lookup hash table: 8192 total in 1 blocks; 408 free (0 chunks); 7784 used TransactionAbortContext: 32768 total in 1 blocks; 32504 free (0 chunks); 264 used Portal hash: 8192 total in 1 blocks; 552 free (0 chunks); 7640 used TopPortalContext: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used Relcache by OID: 16384 total in 2 blocks; 3504 free (2 chunks); 12880 used CacheMemoryContext: 524288 total in 7 blocks; 257608 free (331 chunks); 266680 used index info: 3072 total in 2 blocks; 1128 free (1 chunks); 1944 used: pg_amop_opr_fam_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_operator_oid_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_aggregate_fnoid_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_proc_oid_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_type_oid_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_index_indexrelid_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_opclass_oid_index index info: 2048 total in 2 blocks; 672 free (2 chunks); 1376 used: pg_trigger_tgrelid_tgname_index index info: 2048 total in 2 blocks; 752 free (2 chunks); 1296 used: pg_rewrite_rel_rulename_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_class_oid_index index info: 2048 total in 2 blocks; 672 free (2 chunks); 1376 used: pg_attribute_relid_attnum_index index info: 3072 total in 2 blocks; 1128 free (1 chunks); 1944 used: pg_amproc_fam_proc_index index info: 2048 total in 2 blocks; 944 free (1 chunks); 1104 used: pg_authid_oid_index index info: 2048 total in 2 blocks; 720 free (3 chunks); 1328 used: pg_auth_members_member_role_index index info: 3072 total in 2 blocks; 1208 free (2 chunks); 1864 used: pg_shseclabel_object_index index info: 2048 total in 2 blocks; 1024 free (2 chunks); 1024 used: pg_database_datname_index index info: 2048 total in 2 blocks; 1024 free (2 chunks); 1024 used: pg_database_oid_index index info: 2048 total in 2 blocks; 1024 free (2 chunks); 1024 used: pg_authid_rolname_index WAL record construction: 49768 total in 2 blocks; 6360 free (0 chunks); 43408 used Parallel worker: 8192 total in 1 blocks; 7784 free (1 chunks); 408 used PrivateRefCount: 8192 total in 1 blocks; 2616 free (0 chunks); 5576 used MdSmgr: 8192 total in 1 blocks; 7736 free (4 chunks); 456 used LOCALLOCK hash: 8192 total in 1 blocks; 552 free (0 chunks); 7640 used Timezones: 104120 total in 2 blocks; 2616 free (0 chunks); 101504 used ErrorContext: 8192 total in 1 blocks; 7928 free (4 chunks); 264 used Grand total: 1377694648 bytes in 273 blocks; 5888440 free (763 chunks); 1371806208 used --4jhy73xwzlhdkyvz--