Re: Getting "could not read block" error when creating an index on a function. - Mailing list pgsql-general

From Tom Lane
Subject Re: Getting "could not read block" error when creating an index on a function.
Date
Msg-id 2402314.1609359271@sss.pgh.pa.us
Whole thread Raw
In response to Re: Getting "could not read block" error when creating an index on a function.  (Karsten Hilbert <Karsten.Hilbert@gmx.net>)
Responses Re: Getting "could not read block" error when creating an index on a function.
List pgsql-general
Karsten Hilbert <Karsten.Hilbert@gmx.net> writes:
> Am Wed, Dec 30, 2020 at 02:37:59PM -0500 schrieb Demitri Muna:
>> I want to index the results of these repeated, unchanging calculations to speed up other queries. Which mechanism
wouldbe best to do this? Create additional columns? Create another table? 

> A materialized view ?

Yeah, or you might be able to do something with a before-insert-or-update
trigger that computes whatever desired value you want and fills it into a
derived column.  Indexing that column then gives the same results as
indexing the derived expression; but it sidesteps the semantic problems
because the time of computation of the expression is well-defined, even
if it's not immutable.

You might try to avoid a handwritten trigger by defining a generated
column instead, but we insist that generation expressions be immutable
so it won't really work.  (Of course, you could still lie about the
mutability of the expression, but I can't recommend that.  Per Henry
Spencer's well-known dictum, "If you lie to the compiler, it will get its
revenge".  He was speaking of C compilers, I suppose, but the principle
applies to database query optimizers too.)

            regards, tom lane



pgsql-general by date:

Previous
From: Christophe Pettus
Date:
Subject: Re: Getting "could not read block" error when creating an index on a function.
Next
From: Joao Miguel Ferreira
Date:
Subject: plpgsql unit testing and code coverage