pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: 691525127@qq.com
Subject: BUG #15106: The AFTER trigger is created separately on view, and the DML operation can not trigger the trigger
Date: Mon, 12 Mar 2018 05:51:51 +0000
Message-ID: <152083391168.1215.16892140713507052796@wrigleys.postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      15106
Logged by:          pu qun
Email address:      691525127@qq.com
PostgreSQL version: 10.0
Operating system:   Red Hat Enterprise Linux Server release 6.0
Description:        

When the AFTER trigger is created separately on a view, the DML operation
can not trigger this trigger. 

The instruction of PostgreSQL's manual for triggers is as follows: 
(https://www.postgresql.org/docs/10/static/sql-createtrigger.html):
The following table summarizes which types of triggers may be used on
tables, views, and foreign tables:

 When                    Event                   Row-level                  
             Statement-level


BEFORE          INSERT/UPDATE/DELETE        Tables and foreign tables       
         Tables, views, and foreign tables 
                      TRUNCATE                        —                     
                    Tables 
AFTER           INSERT/UPDATE/DELETE        Tables and foreign tables       
         Tables, views, and foreign tables 
                      TRUNCATE                        —                     
                    Tables 
INSTEAD OF      INSERT/UPDATE/DELETE                 Views                  
                      — 
                      TRUNCATE                        —                     
                      —


According to the instructions, you can see that when the type of trigger
is'AFTER', the Statement-level trigger can be used on a view. 
That is to say, the syntax level AFTER trigger is created on the view, and
when the event is INSERT/UPDATE/DELETE, it can trigger the trigger. 
However, experiments prove that only creating a Statement-level AFTER
trigger, the operation of INSERT/UPDATE/DELETE will not trigger the trigger.


1. create a trigger function: 
postgres=# create or replace function debug() returns trigger as
postgres-# $$
postgres$#postgres$# declare
postgres$# begin
postgres$#   raise notice '%', TG_NAME;
postgres$#   return new;
postgres$# end;
postgres$#postgres$# $$
postgres-#language plpgsql;
CREATE FUNCTION

2. create a test table:
postgres=# create table digoal (id int);
CREATE TABLE

3. create view:
postgres=# create view v_digoal as select * from digoal;
CREATE VIEW

4. create a Statement-level AFTER trigger:  
postgres=# create trigger tg03 after insert on v_digoal for each statement
execute procedure debug();
CREATE TRIGGER

5. Do DML operation on view. it is found that the DML operation does not
trigger the trigger.
postgres=# insert into v_digoal values (2);
INSERT 0 1 


If the 'INSTEAD OF' type trigger is created,then do the DML operation will
trigger the AFTER Statement-level trigger which is created before.
1. create a  Row-levell INSTEAD OF trigger: 
postgres=# create trigger tg04 instead of insert on v_digoal for each row
EXECUTE PROCEDURE debug();
CREATE TRIGGER

2. Do the DML operation on the view again, it is found that the DML
operation not only triggers the newly created INSTEAD OF trigger, but also
triggers the above - mentioned AFTER trigger. 
postgres=# insert into v_digoal values (2);
NOTICE:  tg04
NOTICE:  tg03
INSERT 0 1

It can be seen from the above example, the DML operation cannot trigger the
trigger when the Statement-level AFTER trigger is created on view alone. 
But when create INSTEAD OF trigger at the same time, the DML operation can
trigger the AFTER trigger.
I want to know that this is correct? 
Why didn't the mmanual mention this point? 



view thread (4+ messages)  latest in thread

Message-ID: <152083391168.1215.16892140713507052796@wrigleys.postgresql.org>
Permalink:  ../152083391168.1215.16892140713507052796@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/152083391168.1215.16892140713507052796@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, 691525127@qq.com
  Subject: Re: BUG #15106: The AFTER trigger is created separately on view, and the DML operation can not trigger the trigger
  In-Reply-To: <152083391168.1215.16892140713507052796@wrigleys.postgresql.org>
  References: <152083391168.1215.16892140713507052796@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox