PostgreSQL: Checking for NEW and OLD in a function for a trigger
问题 I want to create a trigger which counts rows and updates a field in an other table. My current solution works for INSERT statements but failes when I DELETE a row. My current function: CREATE OR REPLACE FUNCTION update_table_count() RETURNS trigger AS $$ DECLARE updatecount INT; BEGIN Select count(*) into updatecount From source_table Where id = new.id; Update dest_table set count=updatecount Where id = new.id; RETURN NEW; END; $$ LANGUAGE 'plpgsql'; The trigger is a pretty basic one, looking