From: Demitri Muna <postgresql@demitri.com>
To: 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 14:37:59 -0500
Message-ID: <B76CAF59-EC22-4907-8AD7-A4261C4DA5CA@demitri.com> (raw)
In-Reply-To: <2344514.1609347027@sss.pgh.pa.us>
References: <E88B1929-C94D-45C1-B11F-0B48B39D64B7@demitri.com>
<2344514.1609347027@sss.pgh.pa.us>
Hi Tom,
> On Dec 30, 2020, at 11:50 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> I would call this a bug if it were a supported case, but really you are
> doing something you are not allowed to. Functions in indexed expressions
> are required to be immutable, and a function that looks at the contents of
> a table --- particularly the very table that the index is on --- is simply
> not going to be that. Marking such a function immutable to try to end-run
> around the restriction is unsafe.
Thank you, that makes perfect sense. In my mind it was immutable since the database is read-only, but I can see to PG it’s not. Can you suggest an alternate for what I’m trying to do? Given this schema (a “person” has a number of “events”):
CREATE TABLE person (
id SERIAL,
...
);
CREATE TABLE event (
id SERIAL,
patient_id INTEGER
event_timestamp TIMESTAMP,
…
);
I have a function (the one I was trying to index) that returns the earliest event for a person. I’m scanning another table with ~10B rows several times using a few of these “constant” values:
* first_event_timestamp(person_id) + ‘1 month’
* first_event_timestamp(person_id) + ‘13 months’
* etc.
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?
Thanks again,
Demitri
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: postgresql@demitri.com
Subject: Re: Getting "could not read block" error when creating an index on a function.
In-Reply-To: <B76CAF59-EC22-4907-8AD7-A4261C4DA5CA@demitri.com>
References: <E88B1929-C94D-45C1-B11F-0B48B39D64B7@demitri.com> <2344514.1609347027@sss.pgh.pa.us> <B76CAF59-EC22-4907-8AD7-A4261C4DA5CA@demitri.com>
* 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