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. communication between threads 线程间通信 Programming Concurrent Activities 程序设计中的并发活动 Ada task 任务 Java thread 线程

    Computer Science An Overview _J. Glenn Brookshear _11th Edition activation 激活 parallel processing 并行 ...

  2. P1379 八数码问题

    aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...

  3. linux 输入java 出现中文乱码

    http://jingyan.baidu.com/article/ab69b270de8b4f2ca7189f1d.html 1.查看当前linux的编码 没设置之前是:zh_CN.UTF_8 输入 ...

  4. yum-config-manager YUM安装遭遇: [Errno 256] No more mirrors to try CentOS yum之$releasever和$basearch

    YUM安装遭遇: [Errno 256] No more mirrors to try createrepo 有问题. CentOS yum之$releasever和$basearch分类: 操作系统 ...

  5. JS 去字符串空格 总结

    str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...

  6. HBase的架构以及各个模块的功能

    一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver->region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...

  7. There is no tracking information for the current branch

    There is no tracking information for the current branch. Please specify which branch you want to mer ...

  8. Qt数据库操作(qt-win-commercial-src-4.3.1,VC6,Oracle,SQL Server)

    qt-win-commercial-src-4.3.1.qt-x11-commercial-src-4.3.1Microsoft Visual C++ 6.0.KDevelop 3.5.0Window ...

  9. 【Java 基础篇】【第一课】HelloWorld

    有点C++基础,现在需要快速的学会java,掌握java,所以就这样了,写点博客,以后看起来也好回顾. 1.第一步 javaSDK和Eclipse下载就不说了,搞定了这两样之后: 2.打开Eclips ...

  10. 【上手centos】二、C/C++的编译与运行

    尝试了一下运行C/C++程序,觉得最好还是记下来吧,毕竟也算是从不知到已知呢么. 我用sublime写了2个程序,test.c和test.cpp,分别是C程序和C++程序 step1:编译: #gcc ...