postgres=# \c warehouse_db
You are now connected to database "warehouse_db" as user "postgres".
warehouse_db=# set search_path ='record';
SET
warehouse_db=# show search_path ;
search_path
-------------
record
(1 row)

warehouse_db=# create table warehouse_tb1(warehouse_id integer not null,warehouse_name text not null,year_created integer,street_address text,city character varying(100),state character varying(2),zip character varying(10),
warehouse_db(# constraint "prim_key" primary key (warehouse_id));
CREATE TABLE
warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (1,'Mark Corp',2009,'207-F Main Service Road Ease','New London','CT',4321);
INSERT 0 1
warehouse_db=#
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,city,state,zip)values (2,'Bill & Co',2014,'Lilly Road','New London','CT',4321);
INSERT 0 1
warehouse_db=# select warehouse_id,warehouse_name,state from warehouse_tb1;
warehouse_id | warehouse_name | state
--------------+----------------+-------
1 | Mark Corp | CT
2 | Bill & Co | CT
(2 rows)

warehouse_db=# create or replace function warehouse_audit_func() returns trigger as $first_trigger$
warehouse_db$# begin
warehouse_db$# insert into warehouse_audit (wlog_id,insertion_time) values (new.warehouse_id,current_timestamp);
warehouse_db$# return NEW;
warehouse_db$# end;
warehouse_db$# $first_trigger$ language plpgsql;
CREATE FUNCTION
warehouse_db=# create trigger audit_trigger
warehouse_db-# after insert on warehouse_tb1
warehouse_db-# for each row
warehouse_db-# execute procedure warehouse_audit_func();
CREATE TRIGGER
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,city,state,zip)values (3,'West point',2013,'Down Town','New London','CT',4321);
INSERT 0 1
warehouse_db=# select * from warehouse_audit ;
wlog_id | insertion_time
---------+-------------------------------
3 | 2015-10-14 10:49:05.819715+08
(1 row)

warehouse_db=#
warehouse_db=# select warehouse_id, warehouse_name,state from warehouse_tb1 ;
warehouse_id | warehouse_name | state
--------------+----------------+-------
1 | Mark Corp | CT
2 | Bill & Co | CT
3 | West point | CT
(3 rows)

warehouse_db=# drop table warehouse_audit cascade;
DROP TABLE
warehouse_db=# create table warehouse_audit(wlog_id int not null,insertion_time text not null,operation_detail character varying);
CREATE TABLE

warehouse_db=# CREATE OR REPLACE FUNCTION warehouse_audit_func_all()
warehouse_db-# RETURNS trigger AS $BODY$
warehouse_db$# BEGIN
warehouse_db$# --this IF block confirms the operation type to be INSERT.
warehouse_db$# IF (TG_OP = 'INSERT') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES
warehouse_db$# (new.warehouse_id, current_timestamp,'INSERT operation performed. Row
warehouse_db$# with id '||NEW.warehouse_id|| 'inserted');
warehouse_db$# RETURN NEW;
warehouse_db$# --this IF block confirms the operation type to be UPDATE.
warehouse_db$# ELSIF (TG_OP = 'UPDATE') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES
warehouse_db$# (NEW.warehouse_id, current_timestamp,'UPDATE operation performed. Row
warehouse_db$# with id '||NEW.warehouse_id||' updates values '||OLD||' with '|| NEW.*
warehouse_db$# ||'.');
warehouse_db$# RETURN NEW;
warehouse_db$# --this IF block confirms the operation type to be DELETE
warehouse_db$# ELSIF (TG_OP = 'DELETE') THEN
warehouse_db$# INSERT INTO warehouse_audit
warehouse_db$# (wlog_id, insertion_time, operation_detail)
warehouse_db$# VALUES (OLD.warehouse_id, current_timestamp,'DELETE operation
warehouse_db$# performed. Row with id '||OLD.warehouse_id|| 'deleted ');
warehouse_db$# RETURN OLD;
warehouse_db$# END IF;
warehouse_db$# RETURN NULL;
warehouse_db$# END;
warehouse_db$# $BODY$ LANGUAGE plpgsql;
CREATE FUNCTION

warehouse_db=# create trigger audit_all_ops_trigger
warehouse_db-# after insert or update or delete on warehouse_tb1
warehouse_db-# for each row
warehouse_db-# execute procedure warehouse_audit_func_all();
CREATE TRIGGER

warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (4,'North point',2011,'Down Town','Carson','LA',4324);
INSERT 0 1
warehouse_db=# insert into warehouse_tb1 (warehouse_id,warehouse_name,year_created,street_address,city,state,zip)values (5,'South point',2012,'Down Town','Avalon','LA',4325);
INSERT 0 1
warehouse_db=# update warehouse_tb1 set city = 'arcadia' where warehouse_id = '4';
UPDATE 1
warehouse_db=# delete from warehouse_tb1 where warehouse_id =4;
DELETE 1

postgres=# select wlog_id,insertion_time from warehouse_audit ;
wlog_id | insertion_time
---------+-------------------------------
4 | 2015-11-10 16:56:25.728301+08
5 | 2015-11-10 16:56:40.24055+08
4 | 2015-11-10 16:56:54.938282+08
4 | 2015-11-10 16:57:06.432421+08
(4 rows)

create triggers on views
warehouse_db=# create table tab_view(emp_id int not null,emp_name varchar(10),emp_city varchar(10));
CREATE TABLE
warehouse_db=# insert into tab_view values (1,'Adam','Chicago');
INSERT 0 1

warehouse_db=# insert into tab_view values (2,'John','Miami');
INSERT 0 1
warehouse_db=# insert into tab_view values (3,'Smith','Dallas');
INSERT 0 1
warehouse_db=#
warehouse_db=# create view view_select as select * from tab_view;
CREATE VIEW
warehouse_db=# create function triggerfunc_on_view() returns trigger as $$
warehouse_db$# begin
warehouse_db$# if (TG_OP = 'insert') then
warehouse_db$# insert into tab_view values (new.emp_id,new.emp_name,new.emp_city);
warehouse_db$# return new;
warehouse_db$# end if;
warehouse_db$# return null;
warehouse_db$# end;
warehouse_db$# $$language plpgsql;
CREATE FUNCTION
warehouse_db=#
warehouse_db=# create trigger trigger_on_view
warehouse_db-# instead of insert on view_select
warehouse_db-# for each row
warehouse_db-# execute procedure triggerfunc_on_view();
CREATE TRIGGER

warehouse_db=# select * from view_select ;
-[ RECORD 1 ]-----
emp_id | 1
emp_name | Adam
emp_city | Chicago
-[ RECORD 2 ]-----
emp_id | 2
emp_name | John
emp_city | Miami
-[ RECORD 3 ]-----
emp_id | 3
emp_name | Smith
emp_city | Dallas

warehouse_db=# select * from pg_trigger ;
-[ RECORD 1 ]--+-----------------------------
tgrelid | 90268
tgname | RI_ConstraintTrigger_a_90297
tgfoid | 1654
tgtype | 9
tgenabled | O
tgisinternal | t
tgconstrrelid | 90287
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 2 ]--+-----------------------------
tgrelid | 90268
tgname | RI_ConstraintTrigger_a_90298
tgfoid | 1655
tgtype | 17
tgenabled | O
tgisinternal | t
tgconstrrelid | 90287
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 3 ]--+-----------------------------
tgrelid | 90287
tgname | RI_ConstraintTrigger_c_90299
tgfoid | 1644
tgtype | 5
tgenabled | O
tgisinternal | t
tgconstrrelid | 90268
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 4 ]--+-----------------------------
tgrelid | 90287
tgname | RI_ConstraintTrigger_c_90300
tgfoid | 1645
tgtype | 17
tgenabled | O
tgisinternal | t
tgconstrrelid | 90268
tgconstrindid | 90274
tgconstraint | 90296
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 5 ]--+-----------------------------
tgrelid | 106664
tgname | audit_trigger
tgfoid | 106672
tgtype | 5
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 6 ]--+-----------------------------
tgrelid | 106664
tgname | audit_all_ops_trigger
tgfoid | 106681
tgtype | 29
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |
-[ RECORD 7 ]--+-----------------------------
tgrelid | 106686
tgname | trigger_on_view
tgfoid | 106690
tgtype | 69
tgenabled | O
tgisinternal | f
tgconstrrelid | 0
tgconstrindid | 0
tgconstraint | 0
tgdeferrable | f
tginitdeferred | f
tgnargs | 0
tgattr |
tgargs | \x
tgqual |

warehouse_db=# select tgname from pg_trigger, pg_class where tgrelid=pg_class.oid and relname='warehouse_tb1';
-[ RECORD 1 ]------------------------
tgname | RI_ConstraintTrigger_a_90297
-[ RECORD 2 ]------------------------
tgname | RI_ConstraintTrigger_a_90298
-[ RECORD 3 ]------------------------
tgname | audit_trigger
-[ RECORD 4 ]------------------------
tgname | audit_all_ops_trigger

warehouse_db=# drop trigger audit_all_ops_trigger on warehouse_tb1;
DROP TRIGGER
warehouse_db=#
warehouse_db=# select tgname from pg_trigger, pg_class where tgrelid=pg_class.oid and relname='warehouse_tb1';
-[ RECORD 1 ]------------------------
tgname | RI_ConstraintTrigger_a_90297
-[ RECORD 2 ]------------------------
tgname | RI_ConstraintTrigger_a_90298
-[ RECORD 3 ]------------------------
tgname | audit_trigger

warehouse_db=# select current_user;
-[ RECORD 1 ]+---------
current_user | postgres

postgres=# select * from information_schema.triggers
postgres-# ;
trigger_catalog | trigger_schema | trigger_name | event_manipulation | event_object_catalog | event_object_schema | event_ob
ject_table | action_order | action_condition | action_statement | action_orientation | action_timing | act
ion_reference_old_table | action_reference_new_table | action_reference_old_row | action_reference_new_row | created
-----------------+----------------+-----------------------+--------------------+----------------------+---------------------+---------
-----------+--------------+------------------+----------------------------------------------+--------------------+---------------+----
------------------------+----------------------------+--------------------------+--------------------------+---------
postgres | public | audit_trigger | INSERT | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | INSERT | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | DELETE | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
postgres | public | audit_all_ops_trigger | UPDATE | postgres | public | warehous
e_tbl | | | EXECUTE PROCEDURE warehouse_audit_func_all() | ROW | AFTER |
| | | |
(4 rows)

postgres=# select trigger_name from information_schema.triggers;
trigger_name
-----------------------
audit_trigger
audit_all_ops_trigger
audit_all_ops_trigger
audit_all_ops_trigger
(4 rows)

list the triggers you have created as follows:

postgres=# SELECT * FROM pg_trigger;
tgrelid | tgname | tgfoid | tgtype | tgenabled | tgisinternal | tgconstrrelid | tgconstrindid | tgconstraint | tgdefer
rable | tginitdeferred | tgnargs | tgattr | tgargs | tgqual
---------+-----------------------+--------+--------+-----------+--------------+---------------+---------------+--------------+--------
------+----------------+---------+--------+--------+--------
24576 | audit_all_ops_trigger | 24590 | 29 | O | f | 0 | 0 | 0 | f
| f | 0 | | \x |
24595 | trigger_on_view | 24599 | 69 | O | f | 0 | 0 | 0 | f
| f | 0 | | \x |
(2 rows)

see triggers associated with a particular table

postgres=# select tgname from pg_trigger ,pg_class where tgrelid=pg_class.oid and relname='warehouse_tbl';
tgname
-----------------------
audit_all_ops_trigger
(1 row)

postgres=# drop trigger audit_trigger on warehouse_tbl;
DROP TRIGGER
postgres=# drop trigger audit_all_ops_trigger on warehouse_tbl;
DROP TRIGGER
postgres=# drop trigger audit_all_ops_trigger on warehouse_tbl;

PostgreSQL trigger (function) examples的更多相关文章

  1. ORA-04091: table xxxx is mutating, trigger/function may not see it

    今天同事让我看一个触发器为什么老是报错,当执行DML语句触发触发器后,会报ORA-04091错误:ORA-04091: table xxxx is mutating, trigger/function ...

  2. ORA-04091: table is mutating, trigger/function may not see it

    今天在论坛里发现了一个关于ORA-04091的老帖子,收获良多,特此整理一下 关于ORA-04091: table is mutating, trigger/function may not see ...

  3. ORA-04091: table xxx is mutating, trigger/function may not see it

    Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as tbcs SQL> SQL ...

  4. 【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题

    问题描述 经过前两篇文章,分别使用VM搭建了Kafka服务,创建了Azure Function项目,并且都在本地运行成功. [Azure Developer]在Azure VM (Windows) 中 ...

  5. PostgreSQL function examples

    warehouse_db=# CREATE TABLE warehouse_tbl(warehouse_id INTEGER NOT NULL,warehouse_name TEXT NOT NULL ...

  6. postgresql数据库3种程序(rule,trigger ,FUNCTION )

    1. CREATE [ OR REPLACE ] RULE name AS ON event TO table_name [ WHERE condition ] DO [ ALSO | INSTEAD ...

  7. Postgresql存储过程调试:PostgreSQL 之 Function NOTICE

    转载自http://zhenghaoju700.blog.163.com/blog/static/13585951820116782843994/ 先安装一个PostgreSQL(见补充知识) 比较O ...

  8. 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法

    错误的原因该错误是在编写trigger时常遇到的问题,其根本原因是由于对本表的操作造成的.对于使用了for each row 的触发器,做了DML操作(delete,update,insert),还没 ...

  9. PostgreSQL 之 CREATE FUNCTION

    官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...

随机推荐

  1. CSS子元素margin-top对于父元素的影响

    父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. html代码如下 <styl ...

  2. C# .Net实现URL绝对路径和相对路径之间互相转换

    网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...

  3. 图算法(一)——基本图算法(BFS,DFS及其应用)(2)

    2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和 ...

  4. php YAF

    Yaf 的特点: 用C语言开发的PHP框架, 相比原生的PHP, 几乎不会带来额外的性能开销. 所有的框架类, 不需要编译, 在PHP启动的时候加载, 并常驻内存. 更短的内存周转周期, 提高内存利用 ...

  5. 转一个 C#基础类库

    转自:http://www.cnblogs.com/sufei/archive/2012/12/07/2807170.html http://www.sufeinet.com/thread-655-1 ...

  6. java整合spring和hadoop HDFS

    http://blog.csdn.net/kokjuis/article/details/53586406 http://download.csdn.net/detail/kokjuis/970932 ...

  7. Android 环境快速搭建-详细步骤-win7-64bit

    电脑装了win7 64位的系统,重新来搭建了安卓环境,发现有一种非常便捷,快速的方法就可以搭建起来了~ 步骤一:下载java sdk 进入http://www.oracle.com/us/sun/in ...

  8. 设计模式:职责链模式(Chain Of Responsibility)

    定  义:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 结构图: 处理请求类: //抽象处理类 abs ...

  9. 远程通知APNs(Apple Push Notification Server)

    推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: 推送通知的过程可以分为以 ...

  10. H264(NAL简介与I帧判断)

    1.NAL全称Network Abstract Layer, 即网络抽象层.         在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(N ...