I'm attempting to create a function that can be called as part of daily processing to refresh all of our materialized views. I'd like this to be dynamic, as new materialized views may be added after this function is defined. Here's what I've got:
CREATE OR REPLACE FUNCTION refresh_all_materialized_views(schema_arg varchar)
RETURNS INT VOLATILE AS $$
DECLARE
row RECORD;
BEGIN
RAISE NOTICE 'Refreshing materialized view in schema %', schema_arg;
FOR row IN SELECT name from STV_MV_INFO where schema = schema_arg
LOOP
RAISE NOTICE 'Refreshing %.%', schema_arg, r.name;
EXECUTE 'REFRESH MATERIALIZED VIEW ' || schema_arg || '.' || r.name;
END LOOP;
RETURN 1;
END;
$$ LANGUAGE sql;
Redshift is throwing an error stating:
[42601][500310] Amazon Invalid operation: syntax error at or near "RECORD" Position: 125;
I'm not sure what's incorrect. If "RECORD" isn't a valid type, how does dynamic SQL like this get implemented? Thanks for the time!