Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER TRIGGER command in PostgreSQL

I have an existed trigger in a Postgres 9.3 database, which is defined as follows:

CREATE TRIGGER trig
  AFTER UPDATE OR DELETE
  ON tab2
  FOR EACH ROW
  EXECUTE PROCEDURE proc3();

I want to change the trigger to be:

AFTER INSERT OR UPDATE OR DELETE

The manual on ALTER TRIGGER doesn't explain how to do it.

I want to change the trigger without dropping it. Is that possible?

like image 981
SMW Avatar asked Jun 26 '16 04:06

SMW


People also ask

How do you alter a trigger?

To modify a DML trigger In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand the database that you want, expand Tables, and then expand the table that contains the trigger that you want to modify. Expand Triggers, right-click the trigger to modify, and then click Modify.

What is Alter command in PostgreSQL?

The PostgreSQL ALTER TABLE command is used to add, delete or modify columns in an existing table. You would also use ALTER TABLE command to add and drop various constraints on an existing table.


2 Answers

Sorry, it's not possible to alter a trigger in this way. an OR REPLACE clause does not exists for triggers. However this is rarely a problem because in postgresql DDL statements can be wrapped in a transaction.

BEGIN;
DROP TRIGGER IF EXISTS trig on tab2;
CREATE TRIGGER trig
  AFTER INSERT OR UPDATE OR DELETE
  ON tab2
  FOR EACH ROW
  EXECUTE PROCEDURE proc3();

COMMIT;
like image 83
e4c5 Avatar answered Sep 20 '22 01:09

e4c5


The upcoming version of PostgreSQL will support CREATE OR REPLACE syntax:

CREATE OR REPLACE TRIGGER will either create a new trigger, or replace an existing trigger

To replace the current definition of an existing trigger, use CREATE OR REPLACE TRIGGER, specifying the existing trigger's name and parent table. All other properties are replaced.

  CREATE OR REPLACE TRIGGER trig
  AFTER INSERT OR UPDATE OR DELETE
  ON tab2
  FOR EACH ROW
  EXECUTE PROCEDURE proc3();
like image 37
Lukasz Szozda Avatar answered Sep 18 '22 01:09

Lukasz Szozda