Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kdbeo-0002Om-Te for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Nov 2020 16:10:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kdben-0001bg-TR for pgsql-hackers@arkaria.postgresql.org; Fri, 13 Nov 2020 16:10:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kdbem-0001au-Tr for pgsql-hackers@lists.postgresql.org; Fri, 13 Nov 2020 16:10:37 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kdbek-0006MI-2X for pgsql-hackers@postgresql.org; Fri, 13 Nov 2020 16:10:36 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 89D5A5C0164 for ; Fri, 13 Nov 2020 11:10:32 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Fri, 13 Nov 2020 11:10:32 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=imap.cc; h=to :from:subject:message-id:date:mime-version:content-type; s=fm1; bh=bSltit0eRZ22aEoYs8iywvPa4Mspp0iTT+7HnRsyJ3w=; b=bUIt+/M4SK6W xvCWiPU50vJsrhCX8mk5KZi/Et/btpsWBg3MPjkKdul3GQI1KqoKTnNm9O8r9AtB xkdpCLvByuCVUDD4GyambuvKfe67SbDeyanqhI1NkPqp1wQq7oF2rT1JytAo2TVa fioXI5Hw2QcJfqt09d+3fRvf5eC30OgJPTeluGPn/dhI+iqhUrNfQwMg3HwSjcnB 2KwiPYrk5XhGerr26HveMbnwPZuXSYjxbGlApct8eFPFlrziVmJ/OE+h4WL1OjhA L0KSVU7DvHMdX2cqGrPMUFYNkEW5ntaqDppXc7ed1gJMIM6cL9KM1SS6QqrxXIcr wlx/2q0IRQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=bSltit0eRZ22aEoYs8iywvPa4Mspp 0iTT+7HnRsyJ3w=; b=U9MrWJyl01AgZRFFVQefNFPPwzZEL/gqyh0otuwMI3xf/ 06/JiA+ai/XLnLn1z/+ZWsQ5I9lTjcKNsbyATH9FmBNBUpj6L+cV5Hbl6b2QG2Bp 7w4Ea+mPyYrUNQB5ucd4XzMez+QCLyoYoAg6jYOwIeusf+eN0/VE0rA3UfSX3b+M rx+WwTXvF7hWUY4glZy7JkK4y3zMwCwlz9bqqfKfyYn53nTg+Y1Z8a/sbLVF5PfN DoRphuXrHwtthzSQVzkY5iBvhZfB8TgmUP1mLhJUQS0sj0PLfy/H9fBS6Fa8c3No nn1JIAesSIND+YasxpwVlhZXASqXRDuBWW6IFdQVA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedruddvhedgkeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfedtmdenucfjughrpefvhffukffffgggtgesmhdtreertdefjeen ucfhrhhomheptehlvgigvgihuceurghshhhtrghnohhvuceosggrshhhthgrnhhovhesih hmrghprdgttgeqnecuggftrfgrthhtvghrnhepuefgffegleffhefgueeliefgjeejtdeh hffghfeukeehfeehtdfgjefgkefhudeunecukfhppeelvddrudegrdehjedrudefkeenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsrghshhht rghnohhvsehimhgrphdrtggt X-ME-Proxy: Received: from ev9d9.btn1.brandwatch.net (host-92-14-57-138.as43234.net [92.14.57.138]) by mail.messagingengine.com (Postfix) with ESMTPA id E7C1C328005A for ; Fri, 13 Nov 2020 11:10:31 -0500 (EST) To: PostgreSQL Hackers From: Alexey Bashtanov Subject: don't allocate HashAgg hash tables when running explain only Message-ID: <36823f65-050d-ae24-aa4d-a37726998240@imap.cc> Date: Fri, 13 Nov 2020 16:10:30 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------315BCB43534CDFF4202764F7" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------315BCB43534CDFF4202764F7 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, I got somewhat scared when my explain took a few seconds to complete and used a few gigs of RAM. To reproduce try the following: discard temp; create temp table a as select to_timestamp(generate_series(1, 7000)) i; analyze a; set work_mem to '3GB'; explain select distinct a1.i - a2.i from a a1, a a2; I would appreciate if someone could have a look at the patch attached, which makes executor skip initializing hash tables when doing explain only. Best, Alex --------------315BCB43534CDFF4202764F7 Content-Type: text/x-patch; charset=UTF-8; name="dont-build-agg-hashtable-on-explain-only-v1.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="dont-build-agg-hashtable-on-explain-only-v1.diff" diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index d87677d659..196fe09c52 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -3665,7 +3665,11 @@ ExecInitAgg(Agg *node, EState *estate, int eflags) &aggstate->hash_ngroups_limit, &aggstate->hash_planned_partitions); find_hash_columns(aggstate); - build_hash_tables(aggstate); + + /* Skip massive memory allocation when running only explain */ + if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY)) + build_hash_tables(aggstate); + aggstate->table_filled = false; /* Initialize this to 1, meaning nothing spilled, yet */ --------------315BCB43534CDFF4202764F7--