pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Demitri Muna <postgresql@demitri.com>
Cc: Karsten Hilbert <Karsten.Hilbert@gmx.net>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Getting "could not read block" error when creating an index on a function.
Date: Wed, 30 Dec 2020 15:14:31 -0500
Message-ID: <2402314.1609359271@sss.pgh.pa.us> (raw)
In-Reply-To: <X+zY7uY9fMiN12/B@hermes.hilbert.loc>
References: <E88B1929-C94D-45C1-B11F-0B48B39D64B7@demitri.com>
	<2344514.1609347027@sss.pgh.pa.us>
	<B76CAF59-EC22-4907-8AD7-A4261C4DA5CA@demitri.com>
	<X+zY7uY9fMiN12/B@hermes.hilbert.loc>

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 would be 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





view thread (8+ messages)  latest in thread

Message-ID: <2402314.1609359271@sss.pgh.pa.us>
Permalink:  ../2402314.1609359271@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2402314.1609359271@sss.pgh.pa.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: tgl@sss.pgh.pa.us, postgresql@demitri.com, Karsten.Hilbert@gmx.net
  Subject: Re: Getting "could not read block" error when creating an index on a function.
  In-Reply-To: <2402314.1609359271@sss.pgh.pa.us>
  References: <E88B1929-C94D-45C1-B11F-0B48B39D64B7@demitri.com> <2344514.1609347027@sss.pgh.pa.us> <B76CAF59-EC22-4907-8AD7-A4261C4DA5CA@demitri.com> <X+zY7uY9fMiN12/B@hermes.hilbert.loc> <2402314.1609359271@sss.pgh.pa.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