pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Koen De Groote <kdg.dev@gmail.com>
Cc: Michael Lewis <mlewis@entrata.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: Index no longer being used, destroying and recreating it restores use.
Date: Mon, 15 Jun 2020 14:01:00 -0400
Message-ID: <20200615180100.GD12122@momjian.us> (raw)
In-Reply-To: <CAGbX52EA+cfT06Wg0u3OUVP_jzsFzbrbfHvpmogPVBiXk6Jo_A@mail.gmail.com>
References: <ac76ccc8-a504-fffd-39b9-67495e918553@aklaver.com>
	<1251997.1591290525@sss.pgh.pa.us>
	<CAGbX52GzwrgBZNhpSGEYwob5NiVjz=_6_OB14oXTjdFu-9mO4A@mail.gmail.com>
	<92ea007a-abe0-1dd2-7e5c-080da70c2e2c@aklaver.com>
	<CAGbX52FSLneL7U7bdm5hpY6MTxY30tPBE08uGoezWPaZRjLh0A@mail.gmail.com>
	<CAHOFxGo6W9O9qfNk_CQ4vQ=1SFyi6R5+DyM-mpwmjSUZWPGudg@mail.gmail.com>
	<CAGbX52G_OvW0Z7C7-gWucL+iypaJh-x9zcG6FkB7K0oCsnZ1oA@mail.gmail.com>
	<CAGbX52FKg9a+GDds8j34VSeEBy0mXAeK75u-6ufHX6sTBPTsFg@mail.gmail.com>
	<CAHOFxGqS8yTOoQSg61eo7xsj=qQYewMBvqYJmc9Mw0Swwwmr6g@mail.gmail.com>
	<CAGbX52EA+cfT06Wg0u3OUVP_jzsFzbrbfHvpmogPVBiXk6Jo_A@mail.gmail.com>

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  <bruce@momjian.us>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee






view thread (17+ messages)  latest in thread

Message-ID: <20200615180100.GD12122@momjian.us>
Permalink:  ../20200615180100.GD12122@momjian.us/
Also on:    postgresql.org/message-id/20200615180100.GD12122@momjian.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bruce@momjian.us, kdg.dev@gmail.com, mlewis@entrata.com, adrian.klaver@aklaver.com, tgl@sss.pgh.pa.us
  Subject: Re: Index no longer being used, destroying and recreating it restores use.
  In-Reply-To: <20200615180100.GD12122@momjian.us>
  References: <ac76ccc8-a504-fffd-39b9-67495e918553@aklaver.com> <1251997.1591290525@sss.pgh.pa.us> <CAGbX52GzwrgBZNhpSGEYwob5NiVjz=_6_OB14oXTjdFu-9mO4A@mail.gmail.com> <92ea007a-abe0-1dd2-7e5c-080da70c2e2c@aklaver.com> <CAGbX52FSLneL7U7bdm5hpY6MTxY30tPBE08uGoezWPaZRjLh0A@mail.gmail.com> <CAHOFxGo6W9O9qfNk_CQ4vQ=1SFyi6R5+DyM-mpwmjSUZWPGudg@mail.gmail.com> <CAGbX52G_OvW0Z7C7-gWucL+iypaJh-x9zcG6FkB7K0oCsnZ1oA@mail.gmail.com> <CAGbX52FKg9a+GDds8j34VSeEBy0mXAeK75u-6ufHX6sTBPTsFg@mail.gmail.com> <CAHOFxGqS8yTOoQSg61eo7xsj=qQYewMBvqYJmc9Mw0Swwwmr6g@mail.gmail.com> <CAGbX52EA+cfT06Wg0u3OUVP_jzsFzbrbfHvpmogPVBiXk6Jo_A@mail.gmail.com> <20200615180100.GD12122@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox