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 1lOZLr-0006cS-7J for pgsql-performance@arkaria.postgresql.org; Tue, 23 Mar 2021 05:13:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lOZLq-0006um-4g for pgsql-performance@arkaria.postgresql.org; Tue, 23 Mar 2021 05:13:10 +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 1lOZLp-0006ue-O4 for pgsql-performance@lists.postgresql.org; Tue, 23 Mar 2021 05:13:09 +0000 Received: from mail-il1-x134.google.com ([2607:f8b0:4864:20::134]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lOZLl-0007I5-PG for pgsql-performance@lists.postgresql.org; Tue, 23 Mar 2021 05:13:08 +0000 Received: by mail-il1-x134.google.com with SMTP id v3so17042091ilj.12 for ; Mon, 22 Mar 2021 22:13:05 -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=LHBSlMc27ezZFHJjzkJIFMUPi3pjVj2+/lxEFPF8scs=; b=1pWRmSgJghk9yfbf85ws8j13Ml+/8acijICpBLJ5AVLQu+gKe7pTUSEUEfkS4/ui+L KqiXtcJTo+Hbhvl+FKMuj234+aITgKpq7ZUZ60ek6yWW//jDhq/eRtM8F3BwVbxuvbpC p82pqfxwcBDs4aubERl1WYIIW8Gp0b3xjJq9b5tNFtZphAgjlBud7/V5LZ6koyH1zW8i esMtBTyPZQGF/txcOy2UYbP0oolEm43SbJKonZ3BKrAp9dlULTjDx7Onh/GO0toav3bt eY/NN4BrKcADWVEYaqTdSBy6wJ2Ma/g0PAGnbKB/UylAuOvv029N3HKKNjUY2klj29SU UsXA== 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=LHBSlMc27ezZFHJjzkJIFMUPi3pjVj2+/lxEFPF8scs=; b=pnoJh5xxuHoBO2VjoUkYiR3GIFxPaKuq663cs8WoehtjhwF3k9QMICaaIJxjeaEI71 fi0sMqZRYySKmrYBLXk4Jj6OTRPZK9RdAC9ujLTFgNOjjoLt/9Z6Uuxpo39hJ9RXbZpH HWdSt64rGypv7cVtPU7oSosTNRd4prVid5/+bs+qZnUgbPxtVm2mRWZHsM0+c+HDOPfh C5neDhL65Cp6aCKSkSB+21b/GRLHwfEkhrXIn79xFEp69hoioWauTXfHwWmRol5g2CMF Nbj5xOMXfwl1VKNrUSoebMKYXSX3h/aKrihzf/uH/UyFH+hojdF9sMxU6hyhnkoFwFn7 F9Og== X-Gm-Message-State: AOAM530lcYevRAolyBkcuqDDkTBBpirfVcK1GHJf8ckFgu6iCKjFZlp6 Ht2/TMRy2dMIPffHAEY6zePF5rE39nsYOg== X-Google-Smtp-Source: ABdhPJyaISFEwSchYGZPHhbBd5tcP3kn36BuzEa+UVUNQSemv8oTTGgcMesjPRhm9NI+HGsRGauXqQ== X-Received: by 2002:a05:6e02:de2:: with SMTP id m2mr3278296ilj.274.1616476384819; Mon, 22 Mar 2021 22:13:04 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id e10sm5653906ile.23.2021.03.22.22.13.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Mar 2021 22:13:04 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 7709E8010CC; Tue, 23 Mar 2021 00:13:03 -0500 (CDT) Date: Tue, 23 Mar 2021 00:13:03 -0500 From: Justin Pryzby To: Paul McGarry Cc: pgsql-performance@lists.postgresql.org Subject: Re: Odd (slow) plan choice with min/max Message-ID: <20210323051303.GX4203@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, Mar 23, 2021 at 03:00:38PM +1100, Paul McGarry wrote: > I have a query where Postgresql (11.9 at the moment) is making an odd plan > choice, choosing to use index scans which require filtering out millions of > rows, rather than "just" doing an aggregate over the rows the where clause > targets which is much faster. > AFAICT it isn't a statistics problem, at least increasing the stats target > and analyzing the table doesn't seem to fix the problem. > explain analyze select min(risk_id),max(risk_id) from risk where > time>='2020-01-20 15:00:07+00' and time < '2020-01-21 15:00:08+00'; I'm guessing the time and ID columns are highly correlated... So the planner thinks it can get the smallest ID by scanning the ID index, but then ends up rejecting the first 161e6 rows for which the time is too low, and fails the >= condition. And thinks it can get the greatest ID by backward scanning the ID idx, but ends up rejecting/filtering the first 41e6 rows, for which the time is too high, failing the < condition. This is easy to reproduce: postgres=# DROP TABLE t; CREATE TABLE t AS SELECT a i,a j FROM generate_series(1,999999)a; CREATE INDEX ON t(j); ANALYZE t; postgres=# explain analyze SELECT min(j), max(j) FROM t WHERE i BETWEEN 9999 AND 99999; One solution seems to be to create an index on (i,j), but I don't know if there's a better way. -- Justin