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 1jyFux-00075p-OW for pgsql-performance@arkaria.postgresql.org; Wed, 22 Jul 2020 14:40:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jyFuw-0001iE-MJ for pgsql-performance@arkaria.postgresql.org; Wed, 22 Jul 2020 14:40:22 +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 1jyFuw-0001i6-AD for pgsql-performance@lists.postgresql.org; Wed, 22 Jul 2020 14:40:22 +0000 Received: from mail-il1-x141.google.com ([2607:f8b0:4864:20::141]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jyFut-0000Tl-GH for pgsql-performance@lists.postgresql.org; Wed, 22 Jul 2020 14:40:21 +0000 Received: by mail-il1-x141.google.com with SMTP id x9so1502344ila.3 for ; Wed, 22 Jul 2020 07:40:19 -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=e4J+VG4QXNdr9oKGtxhKCzYbcIlOtqImYCPt7/21NNs=; b=q/GREDV4eGFE6Q150dds5QGTCeKvYIDcXEDBZXiCiEiBXlKBb9oRnczqS86Mc96qgE Y0vWRa5SuplSalGljxvxUQySdBapN3ldSEDEhIWXmRmoBmebQHlL5SHXi23edKz8OlS2 Xr/Ad7Px/F6leAc5fvIDOARcNROiewzgcTBQ6rzvvW6DwE6y97uBRQjjMQzCDP6O2loK pKl0DsnFnm0sSHcXSPfTek74VRHAkMFcXc3jobNIqs+AslUCD49oPAx/4jYOqOQnG4l8 6FkqILCxo+42KPW9JCArTbb5F93iUoLB40Mx+1dZReKlXuvTah1rSgbV+bXeTeDofeoH p9KA== 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=e4J+VG4QXNdr9oKGtxhKCzYbcIlOtqImYCPt7/21NNs=; b=s6NupdShGnQdFSMzjvkRhU+U6ppQUPzjGB4bIoBmTLGf4emCVuwjxyEHyHWNi3LZkC 21AtbFtOO6seLHdaH0PBVhOBj6locEsI117pebXP1z3U2BF3UeaKGFeD2e78NZwxw6yH aseiBUafu8M6RXbirpB+sfpeOoCkHPaC42e2cXQegYGd1guMgD3TmkZUK79raRCfwnpj Ish6C0Vh20DUETPm9RMoTHtak715whuLKpS30Z1mMRxy2Onw/KmwB+rrT9XhErvF647d 3N57anNKZnEjUoH6w7DmINjVoeHAVvpcya0TyDkohe1afXEYBdKZlFjlVi6QyiuedUeN VW9Q== X-Gm-Message-State: AOAM533Hu0R7xBc2HOmj0b4TGTtgANnQ8zuBeusfJG0Q416xqz1070th rtb+Mx8XFJ/wjJFw5h9RX7AHLNntksI= X-Google-Smtp-Source: ABdhPJyQfuSjZMdqQN7TjlUDIPAZj2WFEiPLpRNDtzyjjPYrb87T/Y+7LY5cfvgD2sKhc3TeszGN1A== X-Received: by 2002:a92:cecd:: with SMTP id z13mr243840ilq.76.1595428818196; Wed, 22 Jul 2020 07:40:18 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id a11sm95041iow.26.2020.07.22.07.40.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Jul 2020 07:40:17 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 4A02A80016E; Wed, 22 Jul 2020 09:40:16 -0500 (CDT) Date: Wed, 22 Jul 2020 09:40:16 -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: <20200722144016.GV5748@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: > Hello, > > A description of what you are trying to achieve and what results you expect: > Our database is growing on a daily basis by about 2.5million rows per table (2 at the moment). Because of that, we decided to partition the data, especially, as we are pre-aggregating the data for weeks, months, quarters and years. Every aggregation is stored in a separate partition: > ... > 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. ... > PRIMARY KEY ( daterange, spatial_feature_id, visitor_profile_id, activity_type_combination_id, > activity_chain_id), ... > ) PARTITION BY LIST (daterange); > schemaname relname n_live_tup > mobility_insights location_statistics_y2019m03d 23569853 > mobility_insights location_statistics_y2019m03w 19264373 > mobility_insights location_statistics_y2019m03 18105295 > 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 I guess this isn't actually the problem query, since it takes 143ms and not dozens of seconds. I don't know what is the problem query, but maybe it might help to create an new index on spatial_feature_id, which could be scanned rather than scanning the unique index. Also, if daterange *and* spatial_feature_id are always *both* included, then this might work: postgres=# CREATE STATISTICS t_stats (mcv) ON daterange,spatial_feature_id FROM t ; ANALYZE t; -- Justin