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 1mEJzl-0003co-PP for pgsql-performance@arkaria.postgresql.org; Thu, 12 Aug 2021 23:20:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mEJzk-0007H5-Lw for pgsql-performance@arkaria.postgresql.org; Thu, 12 Aug 2021 23:20:16 +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 1mEJzk-0007Gi-BK for pgsql-performance@lists.postgresql.org; Thu, 12 Aug 2021 23:20:16 +0000 Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mEJze-0004fd-Jk for pgsql-performance@lists.postgresql.org; Thu, 12 Aug 2021 23:20:15 +0000 Received: by mail-io1-xd2e.google.com with SMTP id q16so8424962ioj.0 for ; Thu, 12 Aug 2021 16:20:10 -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=COMdymPXBXr/VZZu4gkwLYZtZWMf5J7BfPa81qjn9CI=; b=HNf3gyV4FphwRLmnJhI5enGI29guwReD1H2pU7kCIxMPHO0J4O1ZgVDtT31sW0XxeU e5uIFrFAezvdNoeHHegOolaWYlji1x7yjKJ3mGJIEo5ZS71DN9ztNplfXHKCmj89LLvh MyrhjZ/cHMf79p1eQK1tHWt8gkadyvl68sJa1Mjt15W2rH7Ciaa12sY5yEms2T6bbzQK svF6rkS/CvrvBaytEZaloUoV+BYxsrTBpwVZyMoTDYz9h6WxOcW+dlcDRK39W12HERg5 GwQ6w6ids2htpHU0GboEQX6ObXI70QtWWNeZPCUdeOZpytXnEzPoOYHHYSIgjDd93PoF qVxw== 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=COMdymPXBXr/VZZu4gkwLYZtZWMf5J7BfPa81qjn9CI=; b=XWTt0BV2c+T2jv909Z02ERyNrDDgdIqkcgYXmRt4Oj5Q15Y54DlKfybvQhC3sDyFFz vHIHTaoYvYYXTMfh6WuREK856gTLRYS5qW00vdk+Iy2D+jKf6fIJJJL1OENcqR1HPZsu 915HMaRysVQm8qaOG8Cpk7Z4g4jmMzhGsMiO+HRBA22ucxLdOpniNp0c+u7/LCiBfwTP TrvoMlXpWduJ7o4Yi6fc7nsvB79/4RCZgydpRU6xProkf7kLlGUaQEYrE34Z35wQm/7J LgsabFbs+xZKu4EI+Upil+I9cN5IWj5yMcK7b+TB2nMD8Q8iFx3thvCmSVTgJjmYPCWG Argg== X-Gm-Message-State: AOAM531mwzaqO1Ys+FiJem1kEOA8/qIEPhcCv4pS5DtRdeo7EJQ4rICb xjtqHND0aVza3sTHQLF0vZXQcw== X-Google-Smtp-Source: ABdhPJycFavdAb7TdGrgAYlS6lbdnoL9VnbSGs1yMGNsWEAPDIykmpbofNwyItKq5P4rOJ6owZpR9A== X-Received: by 2002:a5d:8986:: with SMTP id m6mr4719209iol.87.1628810408322; Thu, 12 Aug 2021 16:20:08 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id f7sm2343593ils.42.2021.08.12.16.20.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Aug 2021 16:20:07 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id BF925800B35; Thu, 12 Aug 2021 18:20:06 -0500 (CDT) Date: Thu, 12 Aug 2021 18:20:06 -0500 From: Justin Pryzby To: Matt Dupree Cc: pgsql-performance@lists.postgresql.org Subject: Re: Postgres using the wrong index index Message-ID: <20210812232006.GN10479@telsasoft.com> References: <20210811123856.GE10479@telsasoft.com> <20210812034510.GG10479@telsasoft.com> 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: Archived-At: Precedence: bulk On Thu, Aug 12, 2021 at 09:38:45AM -0400, Matt Dupree wrote: > > The rowcount estimate for the time column is bad for all these plans - do you > > know why ? You're using inheritence - have you analyzed the parent tables recently ? > > Yes. I used ANALYZE before posting, as it's one of the "things to try" > listed in the slow queries wiki. I even ran the queries immediately after > analyzing. No difference. Can you say more about why the bad row estimate > would cause Postgres to use the bigger index? I would expect Postgres to > use the smaller index if it's over-estimating how many rows will be > returned. The overestimate is in the table's "time" column (not index) and applies to all the plans. Is either half of the AND estimated correctly? If you do a query with only ">=", and a query with only "<=", do either of them give an accurate rowcount estimate ? |Index Scan using other_events_1004175222_pim_evdef_67951aef14bc_idx on public.other_events_1004175222 (cost=0.28..1,648,877.92 rows=1,858,891 width=32) (actual time=1.008..15.245 rows=23 loops=1) |Index Cond: ((other_events_1004175222."time" >= '1624777200000'::bigint) AND (other_events_1004175222."time" <= '1627369200000'::bigint)) It seems like postgres expects the scan to return a large number of matching rows, so tries to use the more selective index which includes the "type" column. But "type" is not very selective either (it has only 4 distinct values), and "time" is not the first column, so it reads a large fraction of the table, slowly. Could you check pg_stat_all_tables and be sure the last_analyzed is recent for both parent and child tables ? Could you send the histogram bounds for "time" ? SELECT tablename, attname, inherited, array_length(histogram_bounds,1), (histogram_bounds::text::text[])[1], (histogram_bounds::text::text[])[array_length(histogram_bounds,1)] FROM pg_stats ... ; -- Justin