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 1mDnVh-0004Ig-JK for pgsql-performance@arkaria.postgresql.org; Wed, 11 Aug 2021 12:39:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDnVg-0001hX-IJ for pgsql-performance@arkaria.postgresql.org; Wed, 11 Aug 2021 12:39:04 +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 1mDnVg-0001ge-6H for pgsql-performance@lists.postgresql.org; Wed, 11 Aug 2021 12:39:04 +0000 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mDnVd-0006AQ-26 for pgsql-performance@lists.postgresql.org; Wed, 11 Aug 2021 12:39:03 +0000 Received: by mail-io1-xd29.google.com with SMTP id f11so3427225ioj.3 for ; Wed, 11 Aug 2021 05:39:00 -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=u99Kp85D6+41z4PpmsOco4EFPmMIgPiSMakDciT4/ik=; b=N+46uVZ2w3OCUikRGGU6aAc5tFJF8CYhRNXVmRT355+sGjkAROh7RzDwwxOB9YcUrm wFEFROslehgAStu1qkC7ANfKbCTiIdmU6PCYezDDNVMDXRthb+trvZI/xZMwolz9rk5S K8Ta2QY0QmP0OLXNTI6YEtpexVqeilp6U/c15DTGR4zosK8DMAvqq2YLCEUlI3UDR8wA +lEIUYVqsuG13k+NbgorzTd0lxl0S1rtjH15dTEmWxCtONbcz80J9qN+s0EAHzmZjsQR zlmrBF+RSKY4iiRy/TB1RO4lxskdjkY/gQSSpVZdkqAB2NNR4M3FtBoTS1Isbn/G6zVV CF+A== 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=u99Kp85D6+41z4PpmsOco4EFPmMIgPiSMakDciT4/ik=; b=AM80d21p15aaJgO8f1Wayo4rYNKjllM7runCYOCVqTX76tVJJoSDWCfPk3FJ8VxnP9 UkJ1AZ8x/gVeuZElSfgdhufvq2TVfweV5hmAnxE/P/yGLoYQ6pfhvAYJyWW1+Q2TvhVP 250KOwINO8SrY7DXNmPo97N4plI1gRZ7wnJHmIV9HPie4OAzuTx15L6TWimafoW/JWhO vH9eFZAWgv3Smp606fz8B2/IJXp93l+RWO7rVOhEds9hvR78sA/oeiim3AHHV4thPOHc 9JHtLje1Us5gA7Jo5l5tkdMqNal9pZgAfxG9u8pnb1j6v66w2dh1feFbYeDXg/7Ohn4X UemA== X-Gm-Message-State: AOAM531L9/lb0AcS6nLEjkRfx5COjPg+Dm1aLLhD1cep2qUhJ3CWsDXf YYlggQ1oIkTCDWpmE7QrALRX7A== X-Google-Smtp-Source: ABdhPJz+FIKBMPcoT3wuEsmZ7QFGsr+wZAPZ60rzUhilvm4SOj0LbNZciXBxi4twPp1BRMl3j2c41g== X-Received: by 2002:a5e:9e44:: with SMTP id j4mr320348ioq.171.1628685538402; Wed, 11 Aug 2021 05:38:58 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id u9sm10501025ilv.75.2021.08.11.05.38.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Aug 2021 05:38:57 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id D44DA800837; Wed, 11 Aug 2021 07:38:56 -0500 (CDT) Date: Wed, 11 Aug 2021 07:38:56 -0500 From: Justin Pryzby To: Matt Dupree Cc: pgsql-performance@lists.postgresql.org Subject: Re: Postgres using the wrong index index Message-ID: <20210811123856.GE10479@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: Archived-At: Precedence: bulk On Tue, Aug 10, 2021 at 12:47:20PM -0400, Matt Dupree wrote: > Here's the plan: https://explain.depesz.com/s/uNGg > > Note that the index being used is Could you show the plan if you force use of the intended index ? For example by doing begin; DROP INDEX indexbeingused; explain thequery; rollback; Or: begin; UPDATE pg_index SET indisvalid=false WHERE indexrelid='indexbeingused'::regclass explain thequery; rollback; Could you show the table statistics for the time, user_id, and type columns on all 4 tables ? | SELECT (SELECT sum(x) FROM unnest(most_common_freqs) x) frac_MCV, tablename, attname, inherited, null_frac, n_distinct, array_length(most_common_vals,1) n_mcv, array_length(histogram_bounds,1) n_hist, correlation FROM pg_stats WHERE attname='...' AND tablename='...' ORDER BY 1 DESC; It might be interesting to see both query plans when index scans are disabled and bitmap scan are used instead (this might be as simple as begin; SET LOCAL enable_indexscan=off ...; rollback;); > Also note that these child tables have 100s of partial indexes. You > can find history on why we have things set up this way here > . I have read it before :) > SELECT relname, relpages, reltuples, relallvisible, pg_table_size(oid) > FROM pg_class WHERE relname = 'other_events_1004175222'; Could you also show the table stats for the two indexes ? One problem is that the rowcount estimate is badly off: | Index Scan using other_events_1004175222_pim_core_custom_2_8e65d072fbdd_idx on public.other_events_1004175222 (cost=0.57..1,213,327.64 rows=1,854,125 width=32) (actual time=450.588..29,057.269 rows=23 loops=1) To my eyes, this looks like a typo ; it's used in the index predicate as well as the query, but maybe it's still relevant ? | #close_onborading -- Justin