WITH Tr AS (SELECT
n.nspname AS trigger_schema,
t.tgname AS trigger_name,
em.text AS event_manipulation,
c.relname AS trigger_table,
np.nspname AS routine_schema,
p.proname AS routine_name,
p.prosrc AS routine_definition,
(SELECT DISTINCT action_condition FROM INFORMATION_SCHEMA.triggers AS it WHERE it.trigger_schema=n.nspname AND it.event_object_table=c.relname AND it.trigger_name=t.tgname) AS action_condition,
CASE t.tgtype::integer & 1
WHEN 1 THEN 'ROW'
ELSE 'STATEMENT'
END AS action_orientation,
CASE t.tgtype::integer & 66
WHEN 2 THEN 'BEFORE'
WHEN 64 THEN 'INSTEAD OF'
ELSE 'AFTER'
END AS action_timing
FROM pg_namespace n,
pg_class c,
pg_trigger t,
pg_proc p,
pg_namespace np,
( VALUES (4,'INSERT'), (8,'DELETE'), (16,'UPDATE'), (32,'TRUNCATE')) em(num, text)
WHERE n.oid = c.relnamespace
AND c.oid = t.tgrelid
AND t.tgfoid=p.oid
AND p.pronamespace=np.oid
AND (t.tgtype::integer & em.num) <> 0
AND NOT t.tgisinternal
AND NOT pg_is_other_temp_schema(n.oid))
SELECT Tr.trigger_schema, trigger_table, action_timing, event_manipulation, action_orientation, action_condition, routine_name, regexp_replace(routine_definition,'[\r\n]','<br>','g') AS routine_definition,
array_agg(event_object_column) AS update_columns
FROM Tr LEFT JOIN information_schema.triggered_update_columns AS TUC
ON Tr.trigger_schema=TUC.trigger_schema AND Tr.trigger_name=TUC.trigger_name AND
Tr.trigger_table=TUC.event_object_table
WHERE action_orientation='ROW'
AND event_manipulation='UPDATE'
AND routine_definition ~* 'crypt[(].+, gen_salt'
AND routine_definition!~* 'if[[:space:]].*(new[.].+(<>|is[[:space:]]+distinct[[:space:]]+from).+old[.]).*[[:space:]]then'
AND (action_condition!~*'(new[.].+(<>|is[[:space:]]+distinct[[:space:]]+from).+old[.])' OR action_condition IS NULL)
GROUP BY Tr.trigger_schema, trigger_table, action_timing, event_manipulation, action_orientation, action_condition, routine_name, routine_definition
ORDER BY Tr.trigger_schema, trigger_table, routine_name;