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 1kuegK-0000Ys-F1 for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 16:50:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuegI-00020t-Qu for pgsql-general@arkaria.postgresql.org; Wed, 30 Dec 2020 16:50:38 +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 1kuegI-00020k-JL for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 16:50:38 +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 1kuegG-0007gL-Kd for pgsql-general@lists.postgresql.org; Wed, 30 Dec 2020 16:50:37 +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 0BUGoRo12344515; Wed, 30 Dec 2020 11:50:27 -0500 From: Tom Lane To: Demitri Muna cc: pgsql-general@lists.postgresql.org Subject: Re: Getting "could not read block" error when creating an index on a function. In-reply-to: References: Comments: In-reply-to Demitri Muna message dated "Wed, 30 Dec 2020 11:16:17 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2344513.1609347027.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 30 Dec 2020 11:50:27 -0500 Message-ID: <2344514.1609347027@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Demitri Muna writes: > I=E2=80=99m getting stuck on a problem I was hoping I could get some hel= p with. I=E2=80=99m trying to create an index for the results of a functio= n that touches two tables like this and get the following error: > CREATE INDEX my_idx ON mytable (first_time(id)); > ERROR: could not read block 0 in file "base/16386/42868": read only 0 o= f 8192 bytes > CONTEXT: SQL function "first_time" during startup What's apparently happening is that some query in the function is trying to examine the newly-created index before it's been populated. 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. regards, tom lane