Postgresql – Disable/enable PostgreSQL trigger inside function for the same table

postgresqltrigger

I have a trigger that protects updating some column. I also have a trigger that should update that same column after insert on that table. I've used triggers like this and it works if I need to update column on some other table. I just disable that protecting trigger before update and enable it again after the update inside function that do all the work. In this case, it doesn't work, since I need to disable the trigger for the same table.

ERROR: cannot ALTER TABLE "Table1" because it is being used by active queries in this session

Is there another way to do it? Is there way to disable all updates to that column except from within this function?

It's PostgreSQL 9.4.

Best Answer

One option would be to:

  1. Jack up the security on the table so normal users can't directly access it at all, and remove the protecting trigger.
  2. Create an updatable view on the table which is accessible by normal users, and add an INSTEAD OF trigger to protect the column from updates (assuming you want it still visible; if not, simply omit it completely from the view definition.)
  3. Create a function with definer's rights which bypasses the view and directly updates the column in the base table.