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 1jxw35-0004cR-NL for pgsql-performance@arkaria.postgresql.org; Tue, 21 Jul 2020 17:27:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jxw34-00068x-CJ for pgsql-performance@arkaria.postgresql.org; Tue, 21 Jul 2020 17:27:26 +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 1jxw33-00068q-W2 for pgsql-performance@lists.postgresql.org; Tue, 21 Jul 2020 17:27:26 +0000 Received: from mail-io1-xd43.google.com ([2607:f8b0:4864:20::d43]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jxw2w-0001Yj-PT for pgsql-performance@lists.postgresql.org; Tue, 21 Jul 2020 17:27:25 +0000 Received: by mail-io1-xd43.google.com with SMTP id z6so4773717iow.6 for ; Tue, 21 Jul 2020 10:27:18 -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:in-reply-to:user-agent; bh=XAne7+mEZcQCv1m6T3r/3QdIvLgq8AN7D0Kw2ytqjTI=; b=djAWh5VtZvZ5Ad5D0sqWR0wASz717P/2P2kvg0fYQAxr34Oqu7Gtqvfm8smSGBvUk6 iFfmzMhE+UljSQuPGHnTUyPPOBr7hg/xDcjD5epm1a9MIZlqWo7vGsbdMMEp6SAFOJWV bOzxXKJGG/LzdTDU4ndYAW9FsVDFb/dUbEMcfLA6VBgUcASkVGOuHhXxGOTWIGmXyZcg owsRP8B+Ou2/Ptmo7tK+HN4OVc6//KGez+alAy/R6gzl1RAEFG5w8uLvHPEQaA05c1H8 BW+kkgQhWP2kVHwfDn7VFRj04hcS0WTY5rjmdY1CkDMspnOLwGOetZFE8dYOQ9+eIU4o lQPA== 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:in-reply-to:user-agent; bh=XAne7+mEZcQCv1m6T3r/3QdIvLgq8AN7D0Kw2ytqjTI=; b=dOECFeY4eYvGuGCWbSnFEUQjE0DGN+xeysIDSrtdYOIJXkkHX9C3iNCV874/rKEVEl SeP9q2A8kGLpmyR7Z0GAr4Q0yualO6dT0K/IxKrUHqVMCqBwL5c0UMKwDqsZKkp+IB+R SV3/LQ1jpVCBxk96BdDKp+gQbChLNxxAwa6u80ynttVOYn5y/eQoiT3bV2VWljQDdYh2 vde1KkzjPR1BAUwiyusPPSFbgj6ENGqNoGfftFPf2O02vpsJlm1wwxLkT5H7nIXRhE7q KUkzAn7i2NMBwNIQdvRnt0UF6TL2oIGgy2DH//5CdAweUAgIapJwJINLJVBv+34aELIJ qpnA== X-Gm-Message-State: AOAM530wGoDwjvHwE7S/SvmwLq6bbGOka/hOtJEZDpd4SepL8sH7+s/+ kKIiLGsonin8BVf2U6zV5TcrlF7xdqk= X-Google-Smtp-Source: ABdhPJxElcW1DsnggiFOYm8EnY5Y0S2ulfCjoWBEvJqf0ys1JIoZhtBZKos5OWm3a+ZdrUxlR4HXKg== X-Received: by 2002:a6b:6e11:: with SMTP id d17mr29562758ioh.102.1595352436880; Tue, 21 Jul 2020 10:27:16 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id o19sm10811052iob.5.2020.07.21.10.27.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Jul 2020 10:27:16 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id BEA1380015B; Tue, 21 Jul 2020 12:27:13 -0500 (CDT) Date: Tue, 21 Jul 2020 12:27:13 -0500 From: Justin Pryzby To: Julian Wolf Cc: pgsql-performance Postgres Mailing List Subject: Re: Too few rows expected by Planner on partitioned tables Message-ID: <20200721172713.GJ5748@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 Tue, Jul 21, 2020 at 01:09:22PM +0000, Julian Wolf wrote: > Our problem is, that the planner always predicts one row to be returned, although only a part of the primary key is queried. This problem exceeds feasibility of performance rapidly - a query only involving a few days already takes dozens of seconds. All tables are analyzed and pg_stats looks reasonable IMHO. > daterange daterange NOT NULL, > spatial_feature_id INTEGER, > Aggregate (cost=2.79..2.80 rows=1 width=8) (actual time=143.073..143.073 rows=1 loops=1) > Buffers: shared hit=67334 > -> Index Scan using location_statistics_y2019m03w_pkey on location_statistics_y2019m03w st (cost=0.56..2.78 rows=1 width=8) (actual time=0.026..117.284 rows=516277 loops=1) > Index Cond: ((daterange = '[2019-03-04,2019-03-11)'::daterange) AND (spatial_feature_id = 12675)) > Buffers: shared hit=67334 > > As can be seen, the planner predicts one row to be returned, although it should be around 3% (11% of the entries are of the given ID, which are distributed over 4 weeks = date ranges) of the table. Using the partition table directly, does not change this fact. Is there a correlation between daterange and spacial_feature_id ? Are the estimates good if you query on *only* daterange? spacial_feature_id ? Maybe what you need is: https://www.postgresql.org/docs/devel/sql-createstatistics.html CREATE STATISTICS stats (dependencies) ON daterange, spacial_feature_id FROM location_statistics; ANALYZE location_statistics; -- Justin