postgresql

Event Triggers

Introduction#

Event Triggers will be fired whenever event associated with them occurs in database.

Remarks#

Please use below link for complete overview of Event Triggers in PostgreSQL

https://www.postgresql.org/docs/9.3/static/event-trigger-definition.html

Logging DDL Command Start Events

Event Type-

  • DDL_COMMAND_START
  • DDL_COMMAND_END
  • SQL_DROP

This is example for creating an Event Trigger and logging DDL_COMMAND_START events.

CREATE TABLE TAB_EVENT_LOGS(
  DATE_TIME TIMESTAMP,
  EVENT_NAME TEXT,
  REMARKS TEXT
);

CREATE OR REPLACE FUNCTION FN_LOG_EVENT()
  RETURNS EVENT_TRIGGER
  LANGUAGE SQL
  AS 
  $main$
    INSERT INTO TAB_EVENT_LOGS(DATE_TIME,EVENT_NAME,REMARKS)
      VALUES(NOW(),TG_TAG,'Event Logging');
  $main$;

CREATE EVENT TRIGGER TRG_LOG_EVENT ON DDL_COMMAND_START
  EXECUTE PROCEDURE FN_LOG_EVENT();

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow