Every time I dump my structure.sql on a rails app, I get PROCEDURE over FUNCTION. FUNCTION is our default and I have to commit the file in parts which is annoying and sometimes I miss lines which is even worse, as it is a rather big structure.sql file.
git diff example:
-CREATE TRIGGER cache_comments_count AFTER INSERT OR DELETE OR UPDATE ON public.comments FOR EACH ROW EXECUTE PROCEDURE public.update_comments_counter();
+CREATE TRIGGER cache_comments_count AFTER INSERT OR DELETE OR UPDATE ON public.comments FOR EACH ROW EXECUTE FUNCTION public.update_comments_counter();
I’m sure there is a postgresql setting for this somewhere, but I can’t find it.
2
Answers
Whether you use Function or Procedure you get exactly the same. The documentation shows
This means you can use either term FUNCTION or PROCEDURE but either way function_name is always called. See demo. For demo I have separate triggers for
insert
andupdate
. Insert usingexecute procedure
and update usingexecute function
. This cannot be changed in Postgres it would have to be Rails setting. NOTE: Prior to v11 Postgres only allowed execute procedure even though you had to create a triggerfunction
that was called.The function
pg_get_triggerdef()
changed between Postgres 11 and 12 when Postgres introduced real procedures. Since Postgres 12 it always returns a syntax that usesEXECUTE FUNCTION
as in reality it is a function that is called when the trigger fires, not a procedure.So this code:
returns the following in Postgres 11 and earlier:
and the following in Postgres 12 and later:
I guess Rails uses
pg_get_triggerdef()
to obtain the trigger source. So there is nothing you can do. If you want a consistent result, you should use the same Postgres version everywhere.The column
action_statement
in the viewinformation_schema.triggers
also reflects the change in naming.