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 1kuhIT-0005hs-Mu for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 19:38:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuhIS-0001N6-FX for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 19:38:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuhIR-0001Mz-RM for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 19:38:12 +0000 Received: from dal3relay119.mxroute.com ([64.40.27.119]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kuhIO-00014l-F7 for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 19:38:10 +0000 Received: from filter004.mxroute.com ([149.28.56.236] 149.28.56.236.vultr.com) (Authenticated sender: mN4UYu2MZsgR) by dal3relay119.mxroute.com (ZoneMTA) with ESMTPSA id 176b528501b0007d69.001 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Wed, 30 Dec 2020 19:38:06 +0000 X-Zone-Loop: 9c47c3f5af6f8a2de6691fc362a51b640520052a2796 X-Originating-IP: [149.28.56.236] Received: from sunfire.mxrouting.net (sunfire.mxrouting.net [49.12.120.198]) by filter004.mxroute.com (Postfix) with ESMTPS id B915A3EA13 for ; Wed, 30 Dec 2020 19:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=demitri.com ; s=x; h=Message-Id:In-Reply-To:To:References:Date:Subject:Mime-Version: Content-Transfer-Encoding:Content-Type:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=VQ1+m2fhxHCUvq7lz6xT24MBtiEGnKzgBfH3vFQ54cM=; b=np4fnPN9hLS9nR5E8JIkVej58e LfPbbUR/Ff+AjCz1LPKsQFFOrakhM3axGaDuLSKKYOkJ/yZZi4+f+w2YOVCFncXsHfTiRYSw5O97C u4RORjkcnny76sq71qmi6UwBwjGcvOaKm8MbMZKxrhjm6xW7mnA/Ho0LZ6e5hDPF++M3jpGYMe2dJ 1/WfPelUekKIDl0RR3PWpCUygg21xkkUkuNcB42p/BR4o/Z2Pk3/9rpmM2O0IeUWNzWly0S9AAW7T lR/UnVgPisqatPm/fN4NEBPMseg0h9dwoSHX+kEhyCLauvuF7KqTJTZfwt9z1O9rg+H5eBl/pfMsd p497M+0g==; From: Demitri Muna Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Getting "could not read block" error when creating an index on a function. Date: Wed, 30 Dec 2020 14:37:59 -0500 References: <2344514.1609347027@sss.pgh.pa.us> To: pgsql-general@lists.postgresql.org In-Reply-To: <2344514.1609347027@sss.pgh.pa.us> Message-Id: X-Mailer: Apple Mail (2.3445.104.17) X-AuthUser: _demitri_@demitri.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Tom, > On Dec 30, 2020, at 11:50 AM, Tom Lane wrote: >=20 > 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=E2=80=99s not. Can you = suggest an alternate for what I=E2=80=99m trying to do? Given this = schema (a =E2=80=9Cperson=E2=80=9D has a number of =E2=80=9Cevents=E2=80=9D= ): CREATE TABLE person ( id SERIAL, ... ); CREATE TABLE event ( id SERIAL, patient_id INTEGER event_timestamp TIMESTAMP, =E2=80=A6 ); I have a function (the one I was trying to index) that returns the = earliest event for a person. I=E2=80=99m scanning another table with = ~10B rows several times using a few of these =E2=80=9Cconstant=E2=80=9D = values: * first_event_timestamp(person_id) + =E2=80=981 month=E2=80=99 * first_event_timestamp(person_id) + =E2=80=9813 months=E2=80=99 * 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