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 1i07zf-0008Cs-Mr for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2019 17:32:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i07ze-0002tJ-Fs for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2019 17:32:26 +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 1i07ze-0002on-3J for pgsql-performance@lists.postgresql.org; Tue, 20 Aug 2019 17:32:26 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i07za-0008Bt-W7 for pgsql-performance@postgresql.org; Tue, 20 Aug 2019 17:32:24 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id DEB8221C4E; Tue, 20 Aug 2019 13:32:21 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Tue, 20 Aug 2019 13:32:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:content-transfer-encoding:in-reply-to; s=fm2; bh=l rciS2ogTMDN4zHTIBpd1l47/y50q18MVz4Ql1LaxPA=; b=Zl+zhh8GDWZX+VVua JoG35hOiBN0EPletAXOzJKE4sRPgT/Hda6HC5w/qiHa/69V77ELq8Q+dJ2jYIqae WSaBg7b3r/JH322ZrH4QgXr7OqDk4UFzpkqT18rtYG8gZzEnL3JUXp2TrAJIKNTl H28ItiBYlUhu24IH2AHUwI3S2DyXydIVO4igvdoxdHpbsusT3l3MzSvEjK6UDm+u MXCOnRzXr8f0DqaL9D9F+9s3EQllwV/9VDUDs9nnUFJKIzoikCBuENqF6i22UP85 4bwKBCa2YkzQjWTxtDRsKifHCNlwtpmr8GsXcOnAfOgvnP48CgRdzYLJZHeiaEsz Ojmhw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=lrciS2ogTMDN4zHTIBpd1l47/y50q18MVz4Ql1Lax PA=; b=neZMHzyFiFkOA71tqznUXz8vLqRarL3vJTd/16YRKl9YiLzvV1Ev9HY1D nkMQ6EsMZ6aYuVX+Xc3sTUN1w646xj1NZPY/24to0/76pDuwNWx30VQkHoSNcqPf Qzw6iRj7elh/sHad2n28UBwGruKrUGp1O/BSNggWKWV3yIBHjR4viNm3rDVwRAGz VqfDFEsRz5M+g1uEWpa3A/yuStwxgOphrEvR0rs2TNMz7kBTDoENd2rBEHQhFgQJ Xp4W3A0CpbyKYDO1/KiKB0U4PnkopSFGs8q0jNRrreUmBZnOwzFAOzcl8itPF3Kr XRrKW3d1IbIBa5ZCKd676vrdabiqg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudeguddgudduudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkfhggtggugfgjsehtkeertddttddunecuhfhrohhmpeetnhgu rhgvshcuhfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucffoh hmrghinhepvdekrdhsohenucfkphepieejrdduiedtrddvudekrddvfeejnecurfgrrhgr mhepmhgrihhlfhhrohhmpegrnhgurhgvshesrghnrghrrgiivghlrdguvgenucevlhhush htvghrufhiiigvpedt X-ME-Proxy: Received: from intern.anarazel.de (c-67-160-218-237.hsd1.ca.comcast.net [67.160.218.237]) by mail.messagingengine.com (Postfix) with ESMTPA id D2889380074; Tue, 20 Aug 2019 13:32:20 -0400 (EDT) Date: Tue, 20 Aug 2019 10:32:19 -0700 From: Andres Freund To: Felix =?utf-8?Q?Geisend=C3=B6rfer?= Cc: pgsql-performance@postgresql.org Subject: Re: Extremely slow HashAggregate in simple UNION query Message-ID: <20190820173219.54dp463d7pkgg6un@alap3.anarazel.de> References: <9ADCDD19-555B-4DB0-B00C-1852AC361557@felixge.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <9ADCDD19-555B-4DB0-B00C-1852AC361557@felixge.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2019-08-20 17:11:58 +0200, Felix Geisendörfer wrote: > today I debugged a query that was executing about 100x slower than expected, and was very surprised by what I found. > > I'm posting to this list to see if this might be an issue that should be fixed in PostgreSQL itself. > > Below is a simplified version of the query in question: > > SET work_mem='64MB'; > EXPLAIN ANALYZE > SELECT * FROM generate_series(1, 1) a, generate_series(1, 1) b > UNION > SELECT * FROM generate_series(1, 1) a, generate_series(1, 1) b; > > HashAggregate (cost=80020.01..100020.01 rows=2000000 width=8) (actual time=19.349..23.123 rows=1 loops=1) FWIW, that's not a mis-estimate I'm getting on master ;). Obviously that doesn't actually address your concern... > 1. The query overestimates the final output rows by a factor of 2 million. [1] Right. There's not really that much we can do about that in general. That'll always be possible. Although we can obviously improve the estimates a good bit more. > I'm certainly a novice when it comes to PostgreSQL internals, but I'm > wondering if this could be fixed by taking a more dynamic approach for > allocating HashAggregate hash tables? Under-sizing the hashtable just out of caution will have add overhead to a lot more common cases. That requires copying data around during growth, which is far far from free. Or you can use hashtables that don't need to copy, but they're also considerably slower in the more common cases. > 3. Somehow EXPLAIN gets confused by this and only ends up tracking 23ms of the query execution instead of 45ms [5]. Well, there's plenty work that's not attributed to nodes. IIRC we don't track executor startup/shutdown overhead on a per-node basis. So I don't really think this is necessarily something that suspicious. Which indeed seems to be what's happening here (this is with 11, to be able to hit the problem with your reproducer): + 33.01% postgres postgres [.] tuplehash_iterate - 18.39% postgres libc-2.28.so [.] __memset_avx2_erms - 90.94% page_fault __memset_avx2_erms tuplehash_allocate tuplehash_create BuildTupleHashTableExt build_hash_table ExecInitAgg ExecInitNode InitPlan standard_ExecutorStart Greetings, Andres Freund