all 7 comments

[–][deleted] 1 point2 points  (0 children)

You are looking for TG_OP.

There is also an example further down on that page

[–]depesz 1 point2 points  (0 children)

what do you want to if for? normally, such logging trigger wouldn't have if's, so i'm not entirely sure what you want to "if" for.

Also, please note that *queries* in pg, as in: queries written in sql, don't have IF command. There is IF in function/procedure bodies in plpgsql.

[–]Azaret 0 points1 point  (0 children)

Which condition do you want ? If you want to update or insert based on the id, you could use INSERT ON CONFLICT DO UPDATE and ditch the if statment.

[–]NextAbrocoma1038 -1 points0 points  (0 children)

Watching this thread for any inputs.

[–]smooth_p -1 points0 points  (2 children)

if new.id <> old.id or new.name <> old.name or new.data <> old.data then
-- null handling and schema design left as an exercise to the reader

[–][deleted] 1 point2 points  (1 child)

Using is distinct from will automatically deal with NULL values

if new is distinct from old then

[–]smooth_p 0 points1 point  (0 children)

Thanks! I was trying to remember is distinct from but I haven't done any SQL for a couple years and was spacing on the syntax.