PostgreSQL 14
The goal of the function is to refresh a specific materialized view prescription - there are 8 mat views named schemaname.prescription - when the IF
condition is true
:
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
vm_prescription RECORD;
begin
FOR vm_prescription in
select relname, relnamespace, nspname, relowner, relkind, nspowner from
pg_catalog.pg_class join
pg_catalog.pg_namespace
on relnamespace = pg_catalog.pg_namespace.oid
and relkind = 'm' and relname = 'prescription'
order by nspname
LOOP
IF (st_intersects(NEW.geom, *here is the problem*)) THEN
EXECUTE format( 'refresh materialized view %I.%I', vm_prescription.nspname, vm_prescription.relname);
END IF;
END LOOP;
return null;
END;
$BODY$;
- I want to refresh the materialized view schemaname.prescription when the
NEW.geom
inserted (in the full table of course) intersects the geom of the schemaname.region. - I can't find the correct syntax for the second parameter of the
st_intersects
: I have to write something like(execute format('select geom from %I.region', vm_prescription.nspname))
, which is obviously false but I hope the idea is clear.
prescription
is the name of all materialized views, only schema name changes.