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 1irAHH-0007jg-1k for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:41:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1irAHE-0003NZ-NE for pgsql-general@arkaria.postgresql.org; Tue, 14 Jan 2020 00:41:48 +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 1irAHE-0003Kt-DC for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:41:48 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1irAHB-0002Qk-73 for pgsql-general@lists.postgresql.org; Tue, 14 Jan 2020 00:41:47 +0000 Received: by mail-pl1-x635.google.com with SMTP id b22so4489486pls.12 for ; Mon, 13 Jan 2020 16:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alaska-edu.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=BWRiTVas/SMH9ShF9YsYGiRhNOKPf1zKr1w/MjvLVvc=; b=JnXIgd+pl0+tF9DTfxzVOu3jUL3tgZ7yN2pjAR9xZXQkLeYiE1cS/h5ddz85Zd0Q+E 0ZaIWdH/VEkj80mDgPoU4RxOJW0vhrKKlWkxHM0HDE0DosgvWMEd2F/8cPxZ8TN0CglB LER6S161kXiqM0tjoOBEoTBNH/rpjJwo01gl+7YipyWDY1l4qcceGqJAsO1s/oATS7W+ klYFXTGkNcjbhnh1Jy02BGh8d1iDkPJqbmRbUshhvrTsr2su+7XBMzcF++sedJqAQ7qg z6oBhgGJ5ekotQj7T8DeVS9cXtbiDmiK3n4XJHC+8dW8gBoE7ANCy1SvCiptzrmmAp3j rfLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=BWRiTVas/SMH9ShF9YsYGiRhNOKPf1zKr1w/MjvLVvc=; b=Qnt4TxSzAf7nOXov0UnDk0FVzCT2IY1hlpWocDTxHgcU/k0XC7uGT7e54thT7oL38L OXK71kGSPZnEdt5d8ChL8oYMpULOwaQry3kr0SrvVxrBiLp0q+EnZBqROq3e0F+yBPnz +GJSfADNyIkuT2qIi2lf35VgexchmZJnJyLTMBYx+OQhrA6AZcrdCXj/nsxqEkv299mJ IkBPwukZ9km9NEkmoGPLFNH3xS/CdCWN6gZl13VQBXpuFsODqcHxK3Fu9PHzKuz6rqZG f5uqDfIJYUC+fJy54ZuHVKGxX/2AznUxRksh2dHgDsiakCojPZhSkeIbhN+EhRyw/QCY 7Djg== X-Gm-Message-State: APjAAAVmpswQpyXpwdfCfL5BynqMvYPXpysUR4E6Y/EEHCFwbwUshnsI yFMZ8NEgfqD+Arv1qKhbkTvMDw== X-Google-Smtp-Source: APXvYqzwZE91rhNy0mU4Y+CoL8RYREUAv3vhMCtd6migr6Hrb8gBtl6GyzsndTrwkwTfxyu1KyslIg== X-Received: by 2002:a17:902:7296:: with SMTP id d22mr17580099pll.55.1578962502923; Mon, 13 Jan 2020 16:41:42 -0800 (PST) Received: from tokuuke.giseis.alaska.edu ([137.229.32.97]) by smtp.gmail.com with ESMTPSA id o31sm14891934pgb.56.2020.01.13.16.41.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Jan 2020 16:41:42 -0800 (PST) From: Israel Brewster Message-Id: <8B3CD048-3DF2-47D6-875A-C935819C48FF@alaska.edu> Content-Type: multipart/alternative; boundary="Apple-Mail=_A6E2A5C3-BFAF-4401-8A03-725597A3C805" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Worse performance with higher work_mem? Date: Mon, 13 Jan 2020 15:41:41 -0900 In-Reply-To: <5228.1578961177@sss.pgh.pa.us> Cc: "pgsql-generallists.postgresql.org" To: Tom Lane References: <17AAA60F-6CC8-4D5A-9944-9C144A81E609@alaska.edu> <5228.1578961177@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_A6E2A5C3-BFAF-4401-8A03-725597A3C805 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jan 13, 2020, at 3:19 PM, Tom Lane wrote: >=20 > 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 second 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. Whereupon the query plan got much = simpler, and performance of said query completely tanked, increasing to = about 15.5 seconds runtime (https://explain.depesz.com/s/Kl0S = ), most of which was in a = HashAggregate. >> How can I fix this? Thanks. >=20 > 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. >=20 Thanks for the suggestions. Disabling hash aggregates actually made = things even worse: (https://explain.depesz.com/s/cjDg = ), so even if that wasn=E2=80=99t a = brute-force option, it doesn=E2=80=99t appear to be a good one. Creating = an index on the time_bucket expression didn=E2=80=99t seem to make any = difference, and my data does get a lot of additions (though virtually no = changes) anyway (about 1 additional record per second). As far as = coercion to date, that=E2=80=99s so I can do queries bounded by date, = and actually have all results from said date included. That said, I = could of course simply make sure that when I get a query parameter of, = say, 2020-1-13, I expand that into a full date-time for the end of the = day. However, doing so for a test query didn=E2=80=99t seem to make much = of a difference either: https://explain.depesz.com/s/X5VT = So, to summarise: Set enable_hasagg=3Doff: worse Index on time_bucket expression: no change in execution time or query = plan that I can see Get rid of coercion to date: *slight* improvement. 14.692 seconds = instead of 15.5 seconds. And it looks like the row count estimates were = actually worse. Lower work_mem, forcing a disk sort and completely different query plan: = Way, way better (around 6 seconds) =E2=80=A6so so far, it looks like the best option is to lower the = work_mem, run the query, then set it back? --- Israel Brewster Software Engineer Alaska Volcano Observatory=20 Geophysical Institute - UAF=20 2156 Koyukuk Drive=20 Fairbanks AK 99775-7320 Work: 907-474-5172 cell: 907-328-9145 > regards, tom lane --Apple-Mail=_A6E2A5C3-BFAF-4401-8A03-725597A3C805 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On = Jan 13, 2020, at 3:19 PM, Tom Lane <tgl@sss.pgh.pa.us> = wrote:

Israel Brewster <ijbrewster@alaska.edu> 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 second of the runtime (explain analyze output = here: https://explain.depesz.com/s/jx0q <https://explain.depesz.com/s/jx0q>). Since the machine = has plenty of RAM available, I went ahead and increased the work_mem = parameter. Whereupon the query plan got much simpler, and performance of = said query completely tanked, increasing to about 15.5 seconds runtime = (https://explain.depesz.com/s/Kl0S <https://explain.depesz.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.


Thanks = for the suggestions. Disabling hash aggregates actually made things even = worse: (https://explain.depesz.com/s/cjDg), so even if that = wasn=E2=80=99t a brute-force option, it doesn=E2=80=99t appear to be a = good one. Creating an index on the time_bucket expression didn=E2=80=99t = seem to make any difference, and my data does get a lot of additions = (though virtually no changes) anyway (about 1 additional record per = second). As far as coercion to date, that=E2=80=99s so I can do queries = bounded by date, and actually have all results from said date included. = That said, I could of course simply make sure that when I get a query = parameter of, say, 2020-1-13, I expand that into a full date-time for = the end of the day. However, doing so for a test query didn=E2=80=99t = seem to make much of a difference either: https://explain.depesz.com/s/X5VT

So, to summarise:

Set enable_hasagg=3Doff: worse
Index on = time_bucket expression: no change in execution time or query plan that I = can see
Get rid of coercion to date: *slight* improvement. = 14.692 seconds instead of 15.5 seconds. And it looks like the row count = estimates were actually worse.
Lower work_mem, forcing a disk = sort and completely different query plan: Way, way better (around 6 = seconds)

=E2=80=A6so so far, it = looks like the best option is to lower the work_mem, run the query, then = set it back?
---
Israel Brewster
Software Engineer
Alaska Volcano = Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK = 99775-7320
Work: 907-474-5172
cell: =  907-328-9145

regards, tom lane

= --Apple-Mail=_A6E2A5C3-BFAF-4401-8A03-725597A3C805--