pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@enterprisedb.com>
To: Kristjan Mustkivi <sonicmonkey@gmail.com>
To: pgsql-performance@lists.postgresql.org
Subject: Re: Postgres chooses slow query plan from time to time
Date: Mon, 13 Sep 2021 15:50:46 +0200
Message-ID: <31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com> (raw)
In-Reply-To: <CAOQPKavhdTrgQo8Uss0M+K0Du+_WYmdoGyMpC0hJQkKM+-X25Q@mail.gmail.com>
References: <CAOQPKavhdTrgQo8Uss0M+K0Du+_WYmdoGyMpC0hJQkKM+-X25Q@mail.gmail.com>

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





view thread (19+ messages)  latest in thread

Message-ID: <31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com>
Permalink:  ../31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com/
Also on:    postgresql.org/message-id/31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tomas.vondra@enterprisedb.com, sonicmonkey@gmail.com, pgsql-performance@lists.postgresql.org
  Subject: Re: Postgres chooses slow query plan from time to time
  In-Reply-To: <31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox