Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tsI7R-000BHP-20 for pgsql-general@arkaria.postgresql.org; Wed, 12 Mar 2025 09:11:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tsI7O-000Kkc-Qx for pgsql-general@arkaria.postgresql.org; Wed, 12 Mar 2025 09:11:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tsI7O-000KkU-FV for pgsql-general@lists.postgresql.org; Wed, 12 Mar 2025 09:11:14 +0000 Received: from mail-oo1-xc30.google.com ([2607:f8b0:4864:20::c30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tsI7L-002PDl-0G for pgsql-general@lists.postgresql.org; Wed, 12 Mar 2025 09:11:14 +0000 Received: by mail-oo1-xc30.google.com with SMTP id 006d021491bc7-5fcd61e9bcdso2815768eaf.0 for ; Wed, 12 Mar 2025 02:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741770669; x=1742375469; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=U7k4cEi5SLeXlURorOzLkBPaG3VuyQUejPDzByfI8yc=; b=FDbl05P+Ovfo30PGC3xJihV2A/zaN949BAiBvS3/KjGMQRDHHNsqp7nJaL9ja8+/tf LnkGLcCigJkPXwaiebY/TOB7z1wIEyLBFg1LxloN1hRW9DtyHsbBzmAD9Kinr8HfOLIy 0PBK7XMPf+JnJJRphw8KPUmxrjmkuQhJIyBpOm3l26UEWXMVf4L2FtGsPXo5w7KNGvk0 bv5DZnDY9zwJmYsTVMiKCUOCwnKTSyumKOq8TLqSWkOe0qtLuGJn9DM6bi9Uugif+bxG es+3U0hWhLj2OQX+T5rdkug+U4XairhzuUgnR0XJb19KM8kyDFUynvfdGJXl2KsS0R+e PoNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741770669; x=1742375469; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=U7k4cEi5SLeXlURorOzLkBPaG3VuyQUejPDzByfI8yc=; b=RhwF9JJ+7+MigzdpcozrQUKwGGDa+OP/GlECRntX6WfmWDdnS76BmRjsokxeUDcayx 1E3mWpV/tBSWoCbXjtEpSbReUhjo4mKn+pMVPT0q5FQBdKcPriCAu1PsnqQuiOdHq3/n DC7BNprhCdy6vWcKdgl1wN7Nq+HiJLu/djBGXVER0IgTaYcHSGuijmVis9PVJsceZJCL N3bzzg4AnTIpRm2OfiibPMK/zlhCdAclZibtvk5IqKxPv7Ok8bRKZcfW1MTxa+KeLxVX pFSxkxamtnnN8RKmKHyB4D/R99RMxI4HIfgCp0E2wv7lRv+aLrPOnTaKNqCZuap7yLkj F4Sg== X-Gm-Message-State: AOJu0YwJxhctdlJPZDixHPU2lZD5bKw1AeSehxeIXr6Ngwz8g0VLP2Ja nkkYXmv6YbWT/EWwSk6xMMzL5wz6gIHfGtr034ybgrayn6eqd6kTu7JT2ykrb7CjQeyEZCZlUpL ePi32X5i5MqXfgEu2P6tRzAY9eE/GO9GN X-Gm-Gg: ASbGncvans0wkBy+oQ+TfJ2OMo0taLFHIt3cfVru71OujefBaghD0KjQz7nEb6QeWpD AvR2ZALe04nwybSWdsH74bc0IE16OhTF47QxJG4kW3k/zR77Ly2Lle/sA0qPL6McW+V7+c2vA8m Q1Ve7bgIBe4tw2cOAhWxlYs5E8 X-Google-Smtp-Source: AGHT+IH5J8t1XbDZoKQk6JXBlVpXkgPdZ8ZSILxzW6R2G2ttkgZQpfnNKFJxJVmng7poNaBcmswsAWWiY40rs7bZ7DM= X-Received: by 2002:a05:6820:125:b0:601:a706:1d42 with SMTP id 006d021491bc7-601a7063d1dmr4379784eaf.0.1741770669300; Wed, 12 Mar 2025 02:11:09 -0700 (PDT) MIME-Version: 1.0 From: Luca Ferrari Date: Wed, 12 Mar 2025 10:10:33 +0100 X-Gm-Features: AQ5f1Jrp7PtoiHbAC63jaUY9h4QBPCsEE0kFDQQTkkAtp8ODj6HjRuCs5dp1tH8 Message-ID: Subject: ERROR: could not read block 0 in file when creating an index out of a function To: pgsql-general Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi all, this is a little controversial, but hagin a function defined as immutable that selects a record out of a table, it is impossible to create an index over such function if the column has a constraint like a primary key. Here it is the use case: DROP TABLE if exists t; drop table if exists tt; CREATE TABLE IF NOT EXISTS t ( pk int primary key ); CREATE TABLE IF NOT EXISTS tt ( pk int ); INSERT INTO t SELECT v FROM generate_series( 1, 1000 ) v; INSERT INTO tt SELECT v FROM generate_series( 1, 1000 ) v; CREATE OR REPLACE FUNCTION f_t( i int ) RETURNS int AS $CODE$ DECLARE return_value int; BEGIN SELECT pk INTO return_value FROM t WHERE pk = i; RETURN return_value; END $CODE$ LANGUAGE plpgsql IMMUTABLE; CREATE OR REPLACE FUNCTION f_tt( i int ) RETURNS int AS $CODE$ DECLARE return_value int; BEGIN SELECT pk INTO return_value FROM tt WHERE pk = i; RETURN return_value; END $CODE$ LANGUAGE plpgsql IMMUTABLE; CREATE INDEX IF NOT EXISTS idx_tt ON tt( f_tt( pk ) ); CREATE INDEX IF NOT EXISTS idx_t ON t( f_t( pk ) ); The last index, created on table t throws the error: ERROR: could not read block 0 in file "base/357283/365810": read only 0 of 8192 bytes CONTEXT: SQL statement "SELECT pk FROM t WHERE pk = i" PL/pgSQL function f_t(integer) line 5 at SQL statement Now, according to the documentation, the function f_t is immutable since it is not modifying the database, so what is going on? And why is the same function working if the table has not the constraint on the column? Moreover: select oid, relname, relkind, pg_relation_filepath( oid ) from pg_class where pg_relation_filepath( oid ) = 'base/357283/365810'; oid | relname | relkind | pg_relation_filepath -----+---------+---------+---------------------- (0 rows) So at what is referencing the error exactly? Thanks, Luca