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 1kuhrq-0006iD-2d for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 20:14:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuhro-0004lg-Oc for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 20:14:44 +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 1kuhro-0004lY-Gv for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 20:14:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuhri-0001TJ-AD for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 20:14:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0BUKEVLX2402315; Wed, 30 Dec 2020 15:14:31 -0500 From: Tom Lane To: Demitri Muna cc: Karsten Hilbert , pgsql-general@lists.postgresql.org Subject: Re: Getting "could not read block" error when creating an index on a function. In-reply-to: References: <2344514.1609347027@sss.pgh.pa.us> Comments: In-reply-to Karsten Hilbert message dated "Wed, 30 Dec 2020 20:45:50 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2402313.1609359271.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 30 Dec 2020 15:14:31 -0500 Message-ID: <2402314.1609359271@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Karsten Hilbert 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? Creat= e 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