postgres=# INSERT INTO tab1 values (10, 100); INSERT 0 1
postgres=# CREATE OR REPLACE FUNCTION func1(var1 numeric) RETURNS NUMERIC AS $$ DECLARE result numeric; BEGIN SELECT c2 into result FROM tab1 WHERE c1=var1; RETURN result; END; $$ LANGUAGE plpgsql IMMUTABLE; CREATE FUNCTION
-- When using the IMMUTABLE function in creating an index for the first time, it is working fine. postgres=# CREATE INDEX idx1 ON tab1(func1(c1)); CREATE INDEX
-- Executing the similar query for 2nd time, We are getting the error postgres=# CREATE INDEX idx2 ON tab1(func1(c1)); ERROR: could not read block 0 in file "base/13675/16391": read only 0 of 8192 bytes CONTEXT: SQL statement "SELECT c2 FROM tab1 WHERE c1=var1" PL/pgSQL function func1(numeric) line 5 at SQL statement