I am using PostgreSQL 17 (with PostGIS extension) installed on a Ubuntu 24.04 system. I have a very strange problem where a certain query works as a straight query (or to create a table), but the same exact query fails when I try to place it in a MATERIALIZED VIEW. The problem seems to be related to using a function inside another function, but I am not sure how. Here is a simplified code to reproduce the problem:
CREATE OR REPLACE FUNCTION myfunc(
ts_array TIMESTAMP WITHOUT TIME ZONE[],tz text)
RETURNS float[] AS
$BODY$
SELECT array_agg(a) FROM
(SELECT extract( epoch FROM unnest($1) AT TIME ZONE $2 ) AS a) t;
$BODY$ LANGUAGE 'sql' IMMUTABLE;
CREATE OR REPLACE FUNCTION nest_myfunc(
ts_array TIMESTAMP WITHOUT TIME ZONE[],tz text)
RETURNS float[] AS
$BODY$
SELECT myfunc($1,$2);
$BODY$ LANGUAGE 'sql' IMMUTABLE;
CREATE TEMP TABLE blah AS
SELECT nest_myfunc(ARRAY['2021-01-01']::timestamp without time zone[],'GMT');
CREATE MATERIALIZED VIEW blah_mv AS
SELECT nest_myfunc(ARRAY['2021-01-01']::timestamp without time zone[],'GMT'::text);
The instruction to CREATE TEMP TABLE works without a problem, but CREATE MATERIALIZED VIEW returns:
ERROR: function myfunc(timestamp without time zone[], text) does not exist
LINE 2: SELECT myfunc($1,$2);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY:
SELECT myfunc($1,$2);
CONTEXT: SQL function "nest_myfunc" during startup
Time: 6.025 ms
What is going on here and how do I fix it?
(NOTE: This code used to work in older versions of PostgreSQL, but not in 17).
select public.myfunc($1,$2);because the matview query loses sight of thepublicschema: "while the query is running, thesearch_pathis temporarily changed topg_catalog, pg_temp."