PostgreSQL document says:
The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain
Plpgsql functions are parsed and syntax-checked at definition time, then at first execution a plan is generated.
https://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING
then that plan is executed with the given parameters.
Temporary files seem to work as expected, except those that already exist on the first execution.
As mentioned in there use of dynamic SQL (EXECUTE) is a way to foil the planner allowing access to arbitrary tables.
https://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN