Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kz049-0003S5-Dk for pgsql-performance@arkaria.postgresql.org; Mon, 11 Jan 2021 16:29:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kz048-00036P-C4 for pgsql-performance@arkaria.postgresql.org; Mon, 11 Jan 2021 16:29:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kz048-00036H-0r for pgsql-performance@lists.postgresql.org; Mon, 11 Jan 2021 16:29:12 +0000 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kz045-00074h-J3 for pgsql-performance@lists.postgresql.org; Mon, 11 Jan 2021 16:29:11 +0000 Received: by mail-io1-xd32.google.com with SMTP id u26so191749iof.3 for ; Mon, 11 Jan 2021 08:29:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=I59HxqsDeelSBTTJS9pR1s5Ubs+1rrRa3VD0ciYQ7Zo=; b=BWdwN009FETWauDXpX6U2k1u+J+WYYkCqoODd1SyQWiisQjAZBUYpc5CSlWihZlWzb imZFPDiiw2Tt/OuecOK/x8lCu0We2CrCzzHW11yd81U1lyzRQubg7hQsUo5IYkb8qKIc 3YYPmYj2pemBQhuXYBusGHJGptAjeGmMlNkkOZTIN1NmvdH88X/OGyiZn0/WZ+P0qK9M EIH7mhS+VKtHCKgNRVq2crlALx+Z6vCNrx4cQ96l+VHrug61sXxCTMYLhhjzhkH0lrPn 5Hrx1oG/5htlmIblt5za26tY5Oo7I4g4tSv0cnP5cs2n+WE1QHlDUXxEdSYB9Elecbv5 L55g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=I59HxqsDeelSBTTJS9pR1s5Ubs+1rrRa3VD0ciYQ7Zo=; b=lKTalaqT/s5GGc7N2bHd0bDmcU901hgzxmAggbCOeqvXQp/kYlfxa+ONBM5e9xWsgy labAyg9MGiWXL4+ToxFRwBcDzDYEMQ9Cf5Mzpa4lj9F2A7XSerktWbpaRjqfDLAGBnol O1Ar6LW+u9UsmLOv+4o1cJroahciSfkA8BhGrsTBe7OWwTQUq+4wcQr21xOrV29LNMIo DZIfgcEpNGyphk+GhZ1eVpWYBT6myMDkOTz0xohgUiPlAIEHOEEP4K2S/FM2EE0E+f3I kIxBcv9ru0OX33gIIAkm36Xwx3CnSE9xWoQF6c9uOBtMWM6bW+EeitHJM9G0GOHlMrq6 cjTg== X-Gm-Message-State: AOAM530Npazi8jUg4nax1nGUWlzDOgQwy7aTrJuDgYhYVKW1GfuiIbHi pboxGf30Danb56ynwkR0xslPFw== X-Google-Smtp-Source: ABdhPJxy0TrNZnVgXTAli4sLbxPWoGl5uMy+ZHRIuIrVXEwY5z8J6uH4PUl4zFqFWCEgAuhu60TiOQ== X-Received: by 2002:a02:969a:: with SMTP id w26mr504484jai.96.1610382548118; Mon, 11 Jan 2021 08:29:08 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id m8sm21286ioh.16.2021.01.11.08.29.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Jan 2021 08:29:07 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id ACF2F800A3C; Mon, 11 Jan 2021 10:29:04 -0600 (CST) Date: Mon, 11 Jan 2021 10:29:04 -0600 From: Justin Pryzby To: =?iso-8859-1?Q?R=E9mi?= Chatenay Cc: pgsql-performance@lists.postgresql.org Subject: Re: How to deal with analyze gathering irrelevant stats Message-ID: <20210111162904.GS1849@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Jan 11, 2021 at 04:50:12PM +0100, Rémi Chatenay wrote: > We are experiencing some slow queries due to the query planner using an > incorrect index. It is using an unoptimized index because the stats are > computed during the night when the data is not the same as during the day. > > CREATE INDEX index_conversations_on_user_id_and_status ON > public.conversations USING btree (user_id, status); > > CREATE INDEX index_conversations_on_status ON public.conversations USING > btree (status) > > The slow query is the following: > > SELECT id FROM conversations WHERE status = 'in_progress' AND user_id = 123 > > There are hundreds of conversations with a status 'in_progress' at a given > time during the day but virtually none during the night. > > So when the analyze is run during the night, PG then thinks that using the > index_conversations_on_status will return almost no rows and so it uses > this index instead of the combined one. > > When the analyze is run during the day, PG correctly uses the right index > (index_conversations_on_user_id_and_status) > We currently run a manual weekly vacuum analyze during the night. I'm > wondering what are our possible solutions. One is to manually run the > analyze during the day but is there a way to tell PG to run the auto > analyze at a given time of the day for example ? I guess we are not the > first ones to have data patterns that differ between when the analyze is > run and the query is run. I think you could run manual ANALYZE during the day just for this one column: ANALYZE conversations (status); If it takes too long or causes a performance issue, you could do: SET default_statistics_target=10; ANALYZE conversations (status); You could also change to make autovacuum do this on its own, by setting: ALTER TABLE conversations SET (autovacuum_analyze_scale_factor=0.005); If that works but too slow, then maybe ALTER TABLE .. SET STATISTICS 10. -- Justin