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 1oAkfy-00073N-1Y for pgsql-performance@arkaria.postgresql.org; Mon, 11 Jul 2022 04:05:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oAkfw-0003e2-UX for pgsql-performance@arkaria.postgresql.org; Mon, 11 Jul 2022 04:05:36 +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 1oAkfw-0003ds-Fw for pgsql-performance@lists.postgresql.org; Mon, 11 Jul 2022 04:05:36 +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 1oAkfr-0002dE-3p for pgsql-performance@lists.postgresql.org; Mon, 11 Jul 2022 04:05:35 +0000 Received: by mail-io1-xd32.google.com with SMTP id e5so3918077iof.2 for ; Sun, 10 Jul 2022 21:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=+WdwGARBcvPY1+zMl5NKH85MCwrpA72URa9J3acl8gM=; b=jVvjrrCT/A87LRRc1NSHHN4EDjkE5OinhWw/qE/n11h7Ov0TEPkooNZccJ9e+snayi ILNamVi2Lemv0FHMsLcSkGYQg0Dpiy04uoYcVQVQbY13Q4lF/Fi9rh8L+ntIC0mTNyIS 7U/cdVMvdeoXKsqx45DpuL2rfotpFRcv0b+l+N5S12Y9AMtnTv+NosdLOleXDg2UpA6m x8/vED7QUTEC/O4BvHTPEv/paS7551y6fFxYshXxyf0dRNRn6Mmy4r9bgtY3mdNF14jV 5zuNAsb/pBCSG0dxVFOdG961hafmsog6gTHGfAz+czH4ehvzdeBAp0ofoXounHRXsWhw YAIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=+WdwGARBcvPY1+zMl5NKH85MCwrpA72URa9J3acl8gM=; b=H0e6qR+brwzIc7fL9C5hb51CVTSbyfojqe5If0aefWzHFit1vWGkuQeVegplnsrzX7 VNvxhSoi6HtwWtjHptNX/tQxQp1acBBTqKNahGtIOVUZfalaKbk3TwzoiiQOI/SKdwCI BOs1w8d3doGyTlQkcvUXo6r2Q7bDu0kUBnCiWi2F2YPzg5Ua80ewvTtaamFs71Xd6Ili HnUJ58hB54eA9O/piUFJdCZNDC20zorXMoviPi/OYnDUAQwnDNNAyDrvDc5XgWellU26 q65dQZmRrh5ueN1jWRaRC/SZIKORPbx2l9fzpaJmxUaI5wY1JER4gD84jd0uAruPsxP3 xQLA== X-Gm-Message-State: AJIora9/SGnG2/xmVeGR1KSk6kbWhFO6Lf9w4T9AVZucKvjA5Gs5VKoi d2MHCUsPsud+3vADpQ0P5hU3OuKkgxibZw== X-Google-Smtp-Source: AGRyM1vBS93HNpzCY+0Nfe0o4BVcv0fbqenu22NmOoVocYjjMmlOcMj+WYSTHRi6UperkPxVLNTTZQ== X-Received: by 2002:a05:6638:389e:b0:33c:a040:f06d with SMTP id b30-20020a056638389e00b0033ca040f06dmr9217525jav.276.1657512330083; Sun, 10 Jul 2022 21:05:30 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id s23-20020a02ad17000000b0033ea1d9858bsm2483276jan.36.2022.07.10.21.05.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Jul 2022 21:05:29 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 2C0908009EE; Sun, 10 Jul 2022 23:05:28 -0500 (CDT) Date: Sun, 10 Jul 2022 23:05:28 -0500 From: Justin Pryzby To: Nathan Ward Cc: pgsql-performance@lists.postgresql.org Subject: Re: Occasional performance issue after changing table partitions Message-ID: <20220711040527.GQ13040@telsasoft.com> References: <24A451C3-1F45-48ED-B747-4BA17C249AF1@daork.net> <20220710140519.GN13040@telsasoft.com> <4954694D-9E02-4209-815B-07903F57779A@daork.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4954694D-9E02-4209-815B-07903F57779A@daork.net> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jul 11, 2022 at 03:21:38PM +1200, Nathan Ward wrote: > > Note that postgres doesn't automatically analyze parent tables, so you should > > maybe do that whenever the data changes enough for it to matter. > > Hmm. This raises some stuff I’m not familiar with - does analysing a parent table do anything? Yes You could check if you have stats now (maybe due to a global ANALYZE or analyzedb) and how the query plans change if you analyze. The transaction may be overly conservative. SELECT COUNT(1) FROM pg_stats WHERE tablename=PARENT; SELECT last_analyze, last_autoanalyze, relname FROM pg_stat_all_tables WHERE relname=PARENT; begin; SET default_statistics_target=10; ANALYZE; explain SELECT [...]; rollback; > I got the impression that analysing the parent was just shorthand for analysing all of the attached partitions. Could you let us know if the documentation left that impression ? See here (this was updated recently). https://www.postgresql.org/docs/13/sql-analyze.html#id-1.9.3.46.8 For partitioned tables, ANALYZE gathers statistics by sampling rows from all partitions; in addition, it will recurse into each partition and update its statistics. Each leaf partition is analyzed only once, even with multi-level partitioning. No statistics are collected for only the parent table (without data from its partitions), because with partitioning it's guaranteed to be empty. By contrast, if the table being analyzed has inheritance children, ANALYZE gathers two sets of statistics: one on the rows of the parent table only, and a second including rows of both the parent table and all of its children. This second set of statistics is needed when planning queries that process the inheritance tree as a whole. The child tables themselves are not individually analyzed in this case. The autovacuum daemon does not process partitioned tables, nor does it process inheritance parents if only the children are ever modified. It is usually necessary to periodically run a manual ANALYZE to keep the statistics of the table hierarchy up to date. > Perhaps because I attach a table with data, the parent sometimes decides it needs to run analyse on a bunch of things? No, that doesn't happen. > Or, maybe it uses the most recently attached partition, with bad statistics, to plan queries that only touch other partitions? This is closer to what I was talking about. To be clear, you are using relkind=p partitions (added in v10), and not inheritance parents, right ? -- Justin