Re: Postgres chooses slow query plan from time to time

From: Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>
To: Kristjan Mustkivi <sonicmonkey(at)gmail(dot)com>, pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Postgres chooses slow query plan from time to time
Date: 2021-09-13 13:50:46
Message-ID: 31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On 9/13/21 3:24 PM, Kristjan Mustkivi wrote:
> Dear community,
>
> I have a query that most of the time gets executed in a few
> milliseconds yet occasionally takes ~20+ seconds. The difference, as
> far as I am able to tell, comes whether it uses the table Primary Key
> (fast) or an additional index with smaller size. The table in question
> is INSERT ONLY - no updates or deletes done there.
>

It'd be really useful to have explain analyze for the slow execution.

My guess is there's a poor estimate, affecting some of the parameter
values, and it probably resolves itself after autoanalyze run.

I see you mentioned SET STATISTICS, so you tried increasing the
statistics target for some of the columns? Have you tried lowering
autovacuum_analyze_scale_factor to make autoanalyze more frequent?

It's also possible most values are independent, but some values have a
rather strong dependency, skewing the estimates. The MCV would help with
that, but those are in PG12 :-(

regards

--
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Michael Lewis 2021-09-13 14:19:40 Re: Postgres chooses slow query plan from time to time
Previous Message Kristjan Mustkivi 2021-09-13 13:24:57 Postgres chooses slow query plan from time to time