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 1jktPx-0005Da-QX for pgsql-general@arkaria.postgresql.org; Mon, 15 Jun 2020 18:01:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jktPv-0000Ty-Oo for pgsql-general@arkaria.postgresql.org; Mon, 15 Jun 2020 18:01:07 +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 1jktPv-0000Tr-HD for pgsql-general@lists.postgresql.org; Mon, 15 Jun 2020 18:01:07 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jktPs-00033O-Nk for pgsql-general@lists.postgresql.org; Mon, 15 Jun 2020 18:01:07 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1jktPo-0002I9-2B; Mon, 15 Jun 2020 14:01:00 -0400 Date: Mon, 15 Jun 2020 14:01:00 -0400 From: Bruce Momjian To: Koen De Groote Cc: Michael Lewis , Adrian Klaver , Tom Lane , PostgreSQL General Subject: Re: Index no longer being used, destroying and recreating it restores use. Message-ID: <20200615180100.GD12122@momjian.us> References: <1251997.1591290525@sss.pgh.pa.us> <92ea007a-abe0-1dd2-7e5c-080da70c2e2c@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jun 9, 2020 at 02:23:51PM +0200, Koen De Groote wrote: > Right. In that case, the function I ended up with is this: > > create or replace function still_needs_backup(bool, bool) > returns BOOLEAN as $$ > BEGIN                 >     PERFORM 1 from item where shouldbebackedup=$1 and backupperformed=$2; >       IF FOUND THEN >         RETURN TRUE; >       ELSE >         RETURN FALSE; >       END IF; > END; > $$ > language plpgsql; > > > And the index for that should be this, I think: "CREATE INDEX CONCURRENTLY > index_test ON item USING btree (still_needs_backup, itemCreated, filepath) > WHERE still_needs_backup(true, false) = true;" > However postgres throws an error here, saying "ERROR:  functions in index > predicate must be marked IMMUTABLE". > > I tried it also without the first argument, same error. > > And I don't think I can do that, because the return is not IMMUTABLE. It is at > best STABLE, but certainly not IMMUTABLE. > > So yeah, I'm probably not understanding the suggestion properly? Either way, I > still have questions about the earlier function I created, namely how reliable > that performance is. If not the same thing will happen as with the re-created > index. I think you need to look at EXPLAIN ANALYZE and see how close the estimate is from the actual counts for various stages. The original query had these quite different, leading to misestimation and wrong plans. If the new EXPLAIN ANALYZE has estimates closer to actual, the problem should not reappear. -- Bruce Momjian https://momjian.us EnterpriseDB https://enterprisedb.com The usefulness of a cup is in its emptiness, Bruce Lee