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 1ir9vt-00074B-CO for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:19:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ir9vr-0000bz-Qr for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:19:43 +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 1ir9vr-0000bs-DC for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:19:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ir9vo-0003KO-OA for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:19:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 00E0JbEK005229; Mon, 13 Jan 2020 19:19:37 -0500 From: Tom Lane To: Israel Brewster cc: "pgsql-generallists.postgresql.org" Subject: Re: Worse performance with higher work_mem? In-reply-to: <17AAA60F-6CC8-4D5A-9944-9C144A81E609@alaska.edu> References: <17AAA60F-6CC8-4D5A-9944-9C144A81E609@alaska.edu> Comments: In-reply-to Israel Brewster message dated "Mon, 13 Jan 2020 14:58:58 -0900" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <5227.1578961177.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 13 Jan 2020 19:19:37 -0500 Message-ID: <5228.1578961177@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Israel Brewster writes: > In looking at the explain analyze output, I noticed that it had an =E2=80= =9Cexternal merge Disk=E2=80=9D sort going on, accounting for about 1 seco= nd of the runtime (explain analyze output here: https://explain.depesz.com= /s/jx0q ). Since the machine has plenty= of RAM available, I went ahead and increased the work_mem parameter. Wher= eupon the query plan got much simpler, and performance of said query compl= etely tanked, increasing to about 15.5 seconds runtime (https://explain.de= pesz.com/s/Kl0S ), most of which was in= a HashAggregate. > How can I fix this? Thanks. Well, the brute-force way not to get that plan is "set enable_hashagg =3D false". But it'd likely be a better idea to try to improve the planner's rowcount estimates. The problem here seems to be lack of stats for either "time_bucket('1 week', read_time)" or "read_time::date". In the case of the latter, do you really need a coercion to date? If it's a timestamp column, I'd think not. As for the former, if the table doesn't get a lot of updates then creating an expression index on that expression might be useful. regards, tom lane