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 1m6XcX-0006lM-Dv for pgsql-performance@arkaria.postgresql.org; Thu, 22 Jul 2021 12:16:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m6XcW-0006zC-DY for pgsql-performance@arkaria.postgresql.org; Thu, 22 Jul 2021 12:16:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m6XcW-0006z4-1f for pgsql-performance@lists.postgresql.org; Thu, 22 Jul 2021 12:16:08 +0000 Received: from mail-il1-x134.google.com ([2607:f8b0:4864:20::134]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m6XcR-0004Vp-4c for pgsql-performance@lists.postgresql.org; Thu, 22 Jul 2021 12:16:07 +0000 Received: by mail-il1-x134.google.com with SMTP id p3so5145970ilg.8 for ; Thu, 22 Jul 2021 05:16:02 -0700 (PDT) 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=9hlQmLvCA9nER9OMO5HicdSJfDx9lBQ2F/3EjxlcJ1M=; b=cgzMb0o3qFQ+m4t0H1zJesk0TchB+gWABzfDMdrCwkBgSkPfjihWsa1xVJWvcPCD0P 0J1CInlQuNpfC3iC+8320L6I6Df/sn8301y/eqUukkPT3QtQLA67hq/eSyziAgUdrf27 6H42D8/qR+fP5RtRCP2C7RZx1/AQ0EUxnG1sNV32/IrFqi7KXSxlDw3tvFJWp5Ly+okK RYD9Be5+tQRBln38kCvRFEYdE2sc4Suo5SdDqTk1atJw3jg/MTunfpFcT6OqMa0spXcf P06Gk56I7pvI3Hw2yhWGvHWow2QPTXmHbYPdDqQ4M/rCYywnJDHMdqUZ7D1pO6JwZoax k1DA== 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=9hlQmLvCA9nER9OMO5HicdSJfDx9lBQ2F/3EjxlcJ1M=; b=RVGxkbMpy/n0gysSQbWdQjqs8ZvraZVI1bcMTAKfUO9rV02vqwYWYa+QMk+sneBa7k barQE3NwSl2lou1qVH064gJxe9lO/wdzc63ebPmAwbUKEmkZehcZbavgbssF9mG35zS8 Tewt677/pBNAeu+hdi7HFvsyhrr6FQj8AMUiuFYa2WZH7jvW/EpzUayLR4hXEItaXpaA ik0xiFtD4GI3xpAx5Wbp/iuaNl6aGi8qTnGOWozob7E+CiNrJWzLtVtIgHIfkJgnLTsu vOISe9505Sz0gj7IoYZviYL6FEa1NZ8v1hmSCJOvqPRbXLyV+g3eFC9rpvKZBQ7RBp1/ B5Rg== X-Gm-Message-State: AOAM533DRMLghg4rK32a7QL5rpWH3JUE0v+bpNlhKfPVVZXDAFL0OZhF 3uxncvVLdmzRXpP7hcgE71GWfg== X-Google-Smtp-Source: ABdhPJyDTf7WL6+38btY9InTv5AAfEnAHy8bQxFrQYo6FqiCj0I4sKHtHmFhWBRDnvDmc1aPmw+4Vg== X-Received: by 2002:a05:6e02:c7:: with SMTP id r7mr27436455ilq.52.1626956161593; Thu, 22 Jul 2021 05:16:01 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id o13sm14559221ilq.58.2021.07.22.05.16.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Jul 2021 05:16:01 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 1F90F800B38; Thu, 22 Jul 2021 07:16:00 -0500 (CDT) Date: Thu, 22 Jul 2021 07:15:59 -0500 From: Justin Pryzby To: Kamil Frydel Cc: pgsql-performance@lists.postgresql.org Subject: Re: Partitioned table statistics vs autoanalyze Message-ID: <20210722121559.GE19620@telsasoft.com> References: <0d1b394b-bec9-8a71-a336-44df7078b295@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0d1b394b-bec9-8a71-a336-44df7078b295@gmail.com> 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 Thu, Jul 22, 2021 at 01:32:51PM +0200, Kamil Frydel wrote: > table_1 and table_2 are hash partitioned using volume_id column. Usually we > make analyze on partitions. We do not make analyze on the partitioned table > (parent). > However, if we run 'analyze' on the partitioned table then planner starts > choosing hash join. As a comparison, the execution using nested loop takes > about 15 minutes and if it is done using hash join then the query lasts for > about 1 minute. When running 'analyze' for the partitioned table, postgres > inserts statistics for the partitioned table into pg_stats (pg_statistics). > Before that, there are only statistics for partitions. We suspect that this > is the reason for selecting bad query plan. > updated, the inheritance statistics will not be up to date unless you run > ANALYZE manually. > (https://www.postgresql.org/docs/13/sql-analyze.html) > > I would appreciate if anyone could shed some light on the following > questions: > 1) Is this above paragraph from docs still valid in PG 13 and does it apply > to declarative partitioning as well? Is running analyze manually on a > partitioned table needed to get proper plans for queries on partitioned > tables? Partitioned table (in the declarative way) is ”virtual” and does not > keep any data so it seems that there are no statistics that can be gathered > from the table itself and statistics from partitions should be sufficient. Up through v13, autoanalyze doesn't collect stats on parent tables (neither declarative nor inheritence). I agree that this doesn't seem to be well documented. I think it should also be mentioned here: https://www.postgresql.org/docs/current/routine-vacuuming.html#VACUUM-FOR-STATISTICS In v14 (which is currently in beta), autoanalyze will process the partitioned table automatically: https://www.postgresql.org/docs/14/release-14.html |Autovacuum now analyzes partitioned tables (Yuzuko Hosoya, Álvaro Herrera) |Insert, update, and delete tuple counts from partitions are now propagated to their parent tables so autovacuum knows when to process them. > 2) Why does the planner need these statistics since they seem to be unused > in the query plan. The query plan uses only partitions, not the partitioned > table. The "inherited" stats are used when you SELECT FROM table. The stats for the individual table would be needed when you SELECT FROM ONLY table (which makes no sense for a partitioned table). -- Justin