1

I'm new to Postgresql 14; I am trying to create a trigger on a table that creates a view every time the table is modified by INSERT / UPDATE / DELETE. After applying the trigger I am getting this error when trying to update/insert/delete anything in my table:

ERROR: control reached end of trigger procedure without RETURN; CONTEXT: PL/pgSQL function createmyview(); SQL state: 2F005

I am assuming my trigger function is missing something...

This is my trigger function:

CREATE FUNCTION createmyVIEW() RETURNS trigger AS $$
BEGIN
    DROP VIEW IF EXISTS public.myVIEW; 

    CREATE VIEW public.myVIEW AS

    SELECT * FROM data.mytable;
END
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

Applying the trigger:

CREATE TRIGGER syncView AFTER INSERT OR UPDATE OR DELETE ON data.mytable
FOR EACH STATEMENT EXECUTE PROCEDURE createmyView();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
stefd
  • 11
  • 3
  • 6
    Why do you want to re-create a view on every data change? The view will automatically reflect changes in the underlying tables when they are modified. – The Impaler Feb 07 '22 at 04:24
  • You're right! I was not aware not of that... Thank you – stefd Feb 07 '22 at 04:58

1 Answers1

3

Try Using 'RETURN' in the function, like:

CREATE FUNCTION createmyVIEW() RETURNS trigger AS $$
BEGIN
  DROP VIEW IF EXISTS public.myVIEW; 
  CREATE VIEW public.myVIEW AS
  SELECT * FROM data.mytable;
RETURN NULL;
END
$$ LANGUAGE 'plpgsql' SECURITY DEFINER;
Mohit_s
  • 31
  • 3