Trigger Procedures

PL/pgSQL can be used to define trigger procedures on data changes or database events.

A trigger procedure is created with the CREATE FUNCTION command, declaring it as a function with no arguments and a return type of trigger (for data change triggers) or event_trigger (for database event triggers).

Special local variables named PG_something are automatically defined to describe the condition that triggered the call.

1. Triggers on Data Changes

A data change trigger is declared as a function with no arguments and a return type of trigger.

Note that the function must be declared with no arguments even if it expects to receive some arguments specified in CREATE TRIGGER--such arguments are passed via TG_ARGV, as described below.

When a PL/pgSQL function is called as a trigger, several special variables are created automatically in the top-level block.

They are:

NEW

  1. Data type RECORD; variable holding the new database row for INSERT/UPDATE operations in row-level triggers. This variable is unassigned in statement-level triggers and for DELETE operations.

OLD

  1. Data type RECORD; variable holding the old database row for UPDATE/DELETE operations in rowlevel triggers. This variable is unassigned in statement-level triggers and for INSERT operations.

TG_NAME

  1. Data type name; variable that contains the name of the trigger actually fired.

TG_WHEN

  1. Data type text; a string of BEFORE, AFTER, or INSTEAD OF, depending on the triggers definition.

TG_LEVEL

  1. Data type text; a string of either ROW or STATEMENT depending on the triggers definition.

TG_OP

  1. Data type text; a string of INSERT, UPDATE, DELETE, or TRUNCATE telling for which operation the trigger was fired.

TG_RELID

  1. Data type oid; the object ID of the table that caused the trigger invocation.

TG_RELNAME

  1. Data type name; the name of the table that caused the trigger invocation. This is now deprecated, and could disappear in a future release. Use TG_TABLE_NAME instead.

TG_TABLE_NAME

  1. Data type name; the name of the table that caused the trigger invocation.

TG_TABLE_SCHEMA

  1. Data type name; the name of the schema of the table that caused the trigger invocation.

TG_NARGS

  1. Data type integer; the number of arguments given to the trigger procedure in the CREATE TRIGGER statement.

TG_ARGV[]

  1. Data type array of text; the arguments from the CREATE TRIGGER statement. The index counts from 0. Invalid indexes (less than 0 or greater than or equal to tg_nargs) result in a null value.

A trigger function must return either NULL or a record/row value having exactly the structure of the table the trigger was fired for.

Row-level triggers fired BEFORE can return null to signal the trigger manager to skip the rest of theoperation for this row (i.e., subsequent triggers are not fired, and the INSERT/UPDATE/DELETE doesnot occur for this row).

If a nonnull value is returned then the operation proceeds with that row value. Returning a row value different from the original value of NEW alters the row that will be inserted orupdated. Thus, if the trigger function wants the triggering action to succeed normally without altering the row value, NEW (or a value equal thereto) has to be returned.

To alter the row to be stored, it is possible to replace single values directly in NEW and return the modified NEW, or to build a completenew record/row to return.

In the case of a before-trigger on DELETE, the returned value has no directeffect, but it has to be nonnull to allow the trigger action to proceed.

Note that NEW is null in DELETEtriggers, so returning that is usually not sensible. The usual idiom in DELETE triggers is to return OLD.INSTEAD OF triggers (which are always row-level triggers, and may only be used on views) can return null to signal that they did not perform any updates, and that the rest of the operation for this row should be skipped (i.e., subsequent triggers are not fired, and the row is not counted in therows-affected status for the surrounding INSERT/UPDATE/DELETE).

Otherwise a nonnull value shouldbe returned, to signal that the trigger performed the requested operation.

For INSERT and UPDATE operations, the return value should be NEW, which the trigger function may modify to support INSERTRETURNING and UPDATE RETURNING (this will also affect the row value passed to any subsequenttriggers, or passed to a special EXCLUDED alias reference within an INSERT statement with an ONCONFLICT DO UPDATE clause).

For DELETE operations, the return value should be OLD.The return value of a row-level trigger fired AFTER or a statement-level trigger fired BEFORE or AFTERis always ignored; it might as well be null. However, any of these types of triggers might still abort the entire operation by raising an error.

Example 1 shows an example of a trigger procedure in PL/pgSQL.

Example 1. A PL/pgSQL Trigger Procedure

This example trigger ensures that any time a row is inserted or updated in the table, the current user name and time are stamped into the row. And it checks that an employee’s name is given and that the salary is a positive value.

  1. CREATE TABLE emp (
  2. empname text,
  3. salary integer,
  4. last_date timestamp,
  5. last_user text
  6. );
  7. CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
  8. BEGIN
  9. -- Check that empname and salary are given
  10. IF NEW.empname IS NULL THEN
  11. RAISE EXCEPTION empname cannot be null’;
  12. END IF;
  13. IF NEW.salary IS NULL THEN
  14. RAISE EXCEPTION ’% cannot have null salary’, NEW.empname;
  15. END IF;
  16. -- Who works for us when they must pay for it?
  17. IF NEW.salary < 0 THEN
  18. RAISE EXCEPTION ’% cannot have a negative salary’, NEW.empname;
  19. END IF;
  20. -- Remember who changed the payroll when
  21. NEW.last_date := current_timestamp;
  22. NEW.last_user := current_user;
  23. RETURN NEW;
  24. END;
  25. $emp_stamp$ LANGUAGE plpgsql;
  26. CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
  27. FOR EACH ROW EXECUTE PROCEDURE emp_stamp();

Another way to log changes to a table involves creating a new table that holds a row for each insert, update, or delete that occurs. This approach can be thought of as auditing changes to a table.

Example 2 shows an example of an audit trigger procedure in PL/pgSQL.

Example 2. A PL/pgSQL Trigger Procedure For Auditing

This example trigger ensures that any insert, update or delete of a row in the emp table is recorded (i.e., audited) in the emp_audit table. The current time and user name are stamped into the row, together with the type of operation performed on it.

  1. CREATE TABLE emp (
  2. empname text NOT NULL,
  3. salary integer
  4. );
  5. CREATE TABLE emp_audit(
  6. operation char(1) NOT NULL,
  7. stamp timestamp NOT NULL,
  8. userid text NOT NULL,
  9. empname text NOT NULL,
  10. salary integer
  11. );
  12. CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
  13. BEGIN
  14. --
  15. -- Create a row in emp_audit to reflect the operation performed on emp,
  16. -- make use of the special variable TG_OP to work out the operation.
  17. --
  18. IF (TG_OP = DELETE’) THEN
  19. INSERT INTO emp_audit SELECT D’, now(), user, OLD.*;
  20. RETURN OLD;
  21. ELSIF (TG_OP = UPDATE’) THEN
  22. INSERT INTO emp_audit SELECT U’, now(), user, NEW.*;
  23. RETURN NEW;
  24. ELSIF (TG_OP = INSERT’) THEN
  25. INSERT INTO emp_audit SELECT I’, now(), user, NEW.*;
  26. RETURN NEW;
  27. END IF;
  28. RETURN NULL; -- result is ignored since this is an AFTER trigger
  29. END;
  30. $emp_audit$ LANGUAGE plpgsql;
  31. CREATE TRIGGER emp_audit
  32. AFTER INSERT OR UPDATE OR DELETE ON emp
  33. FOR EACH ROW EXECUTE PROCEDURE process_emp_audit();

A variation of the previous example uses a view joining the main table to the audit table, to show when each entry was last modified. This approach still records the full audit trail of changes to the table, but also presents a simplified view of the audit trail, showing just the last modified timestamp derived from the audit trail for each entry.

Example 3 shows an example of an audit trigger on a view in PL/pgSQL.

Example 3. A PL/pgSQL View Trigger Procedure For Auditing

This example uses a trigger on the view to make it updatable, and ensure that any insert, update or delete of a row in the view is recorded (i.e., audited) in the emp_audit table. The current time and user name are recorded, together with the type of operation performed, and the view displays the last modified time of each row.

  1. CREATE TABLE emp (
  2. empname text PRIMARY KEY,
  3. salary integer
  4. );
  5. CREATE TABLE emp_audit(
  6. operation char(1) NOT NULL,
  7. userid text NOT NULL,
  8. empname text NOT NULL,
  9. salary integer,
  10. stamp timestamp NOT NULL
  11. );
  12. CREATE VIEW emp_view AS
  13. SELECT e.empname,
  14. e.salary,
  15. max(ea.stamp) AS last_updated
  16. FROM emp e
  17. LEFT JOIN emp_audit ea ON ea.empname = e.empname
  18. GROUP BY 1, 2;
  19. CREATE OR REPLACE FUNCTION update_emp_view() RETURNS TRIGGER AS $$
  20. BEGIN
  21. --
  22. -- Perform the required operation on emp, and create a row in emp_audit
  23. -- to reflect the change made to emp.
  24. --
  25. IF (TG_OP = DELETE’) THEN
  26. DELETE FROM emp WHERE empname = OLD.empname;
  27. IF NOT FOUND THEN RETURN NULL; END IF;
  28. OLD.last_updated = now();
  29. INSERT INTO emp_audit VALUES(’D’, user, OLD.*);
  30. RETURN OLD;
  31. ELSIF (TG_OP = UPDATE’) THEN
  32. UPDATE emp SET salary = NEW.salary WHERE empname = OLD.empname;
  33. IF NOT FOUND THEN RETURN NULL; END IF;
  34. NEW.last_updated = now();
  35. INSERT INTO emp_audit VALUES(’U’, user, NEW.*);
  36. RETURN NEW;
  37. ELSIF (TG_OP = INSERT’) THEN
  38. INSERT INTO emp VALUES(NEW.empname, NEW.salary);
  39. NEW.last_updated = now();
  40. INSERT INTO emp_audit VALUES(’I’, user, NEW.*);
  41. RETURN NEW;
  42. END IF;
  43. END;
  44. $$ LANGUAGE plpgsql;
  45. CREATE TRIGGER emp_audit
  46. INSTEAD OF INSERT OR UPDATE OR DELETE ON emp_view
  47. FOR EACH ROW EXECUTE PROCEDURE update_emp_view();

One use of triggers is to maintain a summary table of another table. The resulting summary can be used in place of the original table for certain queries — often with vastly reduced run times. This technique is commonly used in Data Warehousing, where the tables of measured or observed data (called fact tables) might be extremely large.

Example 4 shows an example of a trigger procedure in PL/pgSQL that maintains a summary table for a fact table in a data warehouse.

Example 4. A PL/pgSQL Trigger Procedure For Maintaining A Summary Table

The schema detailed here is partly based on the Grocery Store example from The Data Warehouse Toolkit by Ralph Kimball.

  1. --
  2. -- Main tables - time dimension and sales fact.
  3. --
  4. CREATE TABLE time_dimension (
  5. time_key integer NOT NULL,
  6. day_of_week integer NOT NULL,
  7. day_of_month integer NOT NULL,
  8. month integer NOT NULL,
  9. quarter integer NOT NULL,
  10. year integer NOT NULL
  11. );
  12. CREATE UNIQUE INDEX time_dimension_key ON time_dimension(time_key);
  13. CREATE TABLE sales_fact (
  14. time_key integer NOT NULL,
  15. product_key integer NOT NULL,
  16. store_key integer NOT NULL,
  17. amount_sold numeric(12,2) NOT NULL,
  18. units_sold integer NOT NULL,
  19. amount_cost numeric(12,2) NOT NULL
  20. );
  21. CREATE INDEX sales_fact_time ON sales_fact(time_key);
  22. --
  23. -- Summary table - sales by time.
  24. --
  25. CREATE TABLE sales_summary_bytime (
  26. time_key integer NOT NULL,
  27. amount_sold numeric(15,2) NOT NULL,
  28. units_sold numeric(12) NOT NULL,
  29. amount_cost numeric(15,2) NOT NULL
  30. );
  31. CREATE UNIQUE INDEX sales_summary_bytime_key ON sales_summary_bytime(time_key);
  32. -- Function and trigger to amend summarized column(s) on UPDATE, INSERT, DELETE.
  33. --
  34. CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER
  35. AS $maint_sales_summary_bytime$
  36. DECLARE
  37. delta_time_key integer;
  38. delta_amount_sold numeric(15,2);
  39. delta_units_sold numeric(12);
  40. delta_amount_cost numeric(15,2);
  41. BEGIN
  42. -- Work out the increment/decrement amount(s).
  43. IF (TG_OP = DELETE’) THEN
  44. delta_time_key = OLD.time_key;
  45. delta_amount_sold = -1 * OLD.amount_sold;
  46. delta_units_sold = -1 * OLD.units_sold;
  47. delta_amount_cost = -1 * OLD.amount_cost;
  48. ELSIF (TG_OP = UPDATE’) THEN
  49. -- forbid updates that change the time_key -
  50. -- (probably not too onerous, as DELETE + INSERT is how most
  51. -- changes will be made).
  52. IF ( OLD.time_key != NEW.time_key) THEN
  53. RAISE EXCEPTION Update of time_key : % -> % not allowed’,
  54. OLD.time_key, NEW.time_key;
  55. END IF;
  56. delta_time_key = OLD.time_key;
  57. delta_amount_sold = NEW.amount_sold - OLD.amount_sold;
  58. delta_units_sold = NEW.units_sold - OLD.units_sold;
  59. delta_amount_cost = NEW.amount_cost - OLD.amount_cost;
  60. ELSIF (TG_OP = INSERT’) THEN
  61. delta_time_key = NEW.time_key;
  62. delta_amount_sold = NEW.amount_sold;
  63. delta_units_sold = NEW.units_sold;
  64. delta_amount_cost = NEW.amount_cost;
  65. END IF;
  66. -- Insert or update the summary row with the new values.
  67. <<insert_update>>
  68. LOOP
  69. UPDATE sales_summary_bytime
  70. SET amount_sold = amount_sold + delta_amount_sold,
  71. units_sold = units_sold + delta_units_sold,
  72. amount_cost = amount_cost + delta_amount_cost
  73. WHERE time_key = delta_time_key;
  74. EXIT insert_update WHEN found;
  75. BEGIN
  76. INSERT INTO sales_summary_bytime (
  77. time_key,
  78. amount_sold,
  79. units_sold,
  80. amount_cost)
  81. VALUES (
  82. delta_time_key,
  83. delta_amount_sold,
  84. delta_units_sold,
  85. delta_amount_cost
  86. );
  87. EXIT insert_update;
  88. EXCEPTION
  89. WHEN UNIQUE_VIOLATION THEN
  90. -- do nothing
  91. END;
  92. END LOOP insert_update;
  93. RETURN NULL;
  94. END;
  95. $maint_sales_summary_bytime$ LANGUAGE plpgsql;
  96. CREATE TRIGGER maint_sales_summary_bytime
  97. AFTER INSERT OR UPDATE OR DELETE ON sales_fact
  98. FOR EACH ROW EXECUTE PROCEDURE maint_sales_summary_bytime();
  99. INSERT INTO sales_fact VALUES(1,1,1,10,3,15);
  100. INSERT INTO sales_fact VALUES(1,2,1,20,5,35);
  101. INSERT INTO sales_fact VALUES(2,2,1,40,15,135);
  102. INSERT INTO sales_fact VALUES(2,3,1,10,1,13);
  103. SELECT * FROM sales_summary_bytime;
  104. DELETE FROM sales_fact WHERE product_key = 1;
  105. SELECT * FROM sales_summary_bytime;
  106. UPDATE sales_fact SET units_sold = units_sold * 2;
  107. SELECT * FROM sales_summary_bytime;

2. Triggers on Events

PL/pgSQL can be used to define event triggers. PostgreSQL requires that a procedure that is to be called as an event trigger must be declared as a function with no arguments and a return type of event_trigger.

When a PL/pgSQL function is called as an event trigger, several special variables are created automatically in the top-level block.

They are:

TG_EVENT

  1. Data type text; a string representing the event the trigger is fired for.

TG_TAG

  1. Data type text; variable that contains the command tag for which the trigger is fired.

Example 5 shows an example of an event trigger procedure in PL/pgSQL.

Example 5. A PL/pgSQL Event Trigger Procedure

This example trigger simply raises a NOTICE message each time a supported command is executed.

  1. CREATE OR REPLACE FUNCTION snitch() RETURNS event_trigger AS $$
  2. BEGIN
  3. RAISE NOTICE snitch: % %’, tg_event, tg_tag;
  4. END;
  5. $$ LANGUAGE plpgsql;
  6. CREATE EVENT TRIGGER snitch ON ddl_command_start EXECUTE PROCEDURE snitch();

postgreSQL PL/SQL编程学习笔记(五)——触发器(Triggers)的更多相关文章

  1. postgreSQL PL/SQL编程学习笔记(二)

    Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...

  2. postgreSQL PL/SQL编程学习笔记(六)——杂七杂八

    1 PL/pgSQL Under the Hood This part discusses some implementation details that are frequently import ...

  3. postgreSQL PL/SQL编程学习笔记(三)——游标(Cursors)

    Cursors Rather than executing a whole query at once, it is possible to set up a cursor that encapsul ...

  4. postgreSQL PL/SQL编程学习笔记(一)

    1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...

  5. postgreSQL PL/SQL编程学习笔记(四)

    Errors and Messages 1. Reporting Errors and Messages Use the RAISE statement to report messages and ...

  6. ORACLE PL/SQL编程之八:把触发器说透

    原文:ORACLE PL/SQL编程之八:把触发器说透 ORACLE PL/SQL编程之八: 把触发器说透 大家一定要评论呀,感谢!光发表就花了我将近一个下午. 本篇主要内容如下: 8.1 触发器类型 ...

  7. PL/SQL编程基础(五):异常处理(EXCEPTION)

    异常处理 异常产生所带来的问题: 使用EXCEPTION程序块进行异常处理: 实现用户自定义异常. 使用异常可以保证在程序中出现运行时异常时程序可以正常的执行完毕: 用户可以使用自定义异常进行操作. ...

  8. MySql-Mysql技术内幕~SQL编程学习笔记(N)

    1._rowid 类似Oracle的rowid mysql> ; +-------+----+----------------+-------------+---------------+--- ...

  9. MySql-Mysql技术内幕~SQL编程学习笔记(1)

    1.MySQL的历史,一些相关概念. 2.MySQL数据类型 *通常一个页内可以存放尽可能多的行,那么数据库的性能就越好,选择一个正确的数据类型至关重要. 1>UNSIGNED类型: 将数字类型 ...

随机推荐

  1. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  2. leetcode883

    int projectionArea(vector<vector<int>>& grid) { ; ; ; ; i < grid.size(); i++) { r ...

  3. Struts2处理逻辑的方式

    1.可以统一写一个action 对应方法名处理不同逻辑 2.也可以分别写Action 分别处理不同的逻辑

  4. Internet Explorer 无法打开该 Internet 站点,请求的站点不可用或无法找到

    笔者最近遇见一个神奇的问题,同事在开发时用的谷歌浏览器,实现了一个下载功能,测试也没问题:但测试人员反馈说他那边没法下载,报异常.弹出框 同事跑过来和我商讨这个问题,笔者当时就懵了,于是赶紧查找相关资 ...

  5. nand中间出现坏块,无法正常启动内…

    我板子的启动过程如下: ..showlogo.. Flash:   1 MB NAND:    SLC detected.256 MB In:      serial Out:     serial ...

  6. angularJS学习(三)——搭建学习环境

    1.安装Node.js 和Testacular 1.1. 安装Node.js及配置部分,在另一篇博文:node.js的安装里面讲到了,地址是:http://www.cnblogs.com/tianxu ...

  7. Linux的基本指令--目录和文件和文件属性和文件用户组

    目录和文件 一 .  ls:列出目录的内容,未给出目录名或是文件名时,就显示当前目录的信息. -a 列出隐藏文件,文件中以”.”开头的均为隐藏文件,如:~/.bashrc  -l 列出文件的详细信息 ...

  8. 【HDU4301】Divide Chocolate

    题意 有一块n*2的巧克力,将它分成k块,问有多少种方法. 分析 emmm是dp没错了. 最容易想到的状态定义是f[i][j],意思是前i行,分成j块的方案数.但是发现没法转移.(后面会说一下为什么· ...

  9. 【bzoj1019】[SHOI2008]汉诺塔

    1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1427  Solved: 872[Submit][Status] ...

  10. SOAP UI

    We use SoapUI-Pro-5.1.2 1. Basic introduction - Windows 2. Use project environment tab to manage the ...