----- Original Message -----
Tom I have had another look at this problem
and you were correct in that the trigger functions were being called, just not
in the order that I had expected. There were "no bugs" in my code as such, just
a total misunderstanding on how trigger
functions are processed in a SQL command that effects more than one record -
hence the reason why I'm writing this email. I don't want other people to be
caught out with the same problem and it may well be worth putting a note in
the documentation under the trigger manager section and correcting
the example in Chapter 16.4.
I made the following assumption (based on
the documents): The sequence of events for
trigger functions would be exactly the same regardless of the SQL
statement/s to achieve the same result. That is I thought that for example:
DELETE FROM tbl_user WHERE id = '20'; DELETE FROM tbl_user WHERE id = '21';
would produce exactly the same sequence of events as DELETE FROM tbl_user
WHERE (id >= '20' AND id <= '21'); This is not the case.
The sequence of events for the first set of
SQL statements is as follows:
1: Trigger BEFORE EVENT for record with id
= 20;
2: Trigger AFTER EVENT for record with id =
20;
3: Trigger BEFORE EVENT for record with id
= 21;
4: Trigger AFTER EVENT for record with id =
21; The sequence of events for the second set
of SQL statements is as follows:
1: Trigger BEFORE EVENT for record with id
= 20;
2: Trigger BEFORE EVENT for record with id
= 21; 3: Trigger AFTER EVENT for record with id = 20;4: Trigger AFTER EVENT for record with id =
21;
According to the example in the
documentation - 'Chapter 16.4 Examples', the sequence of events for the
DELETE is as follows:
vac=> DELETE FROM ttest;
INFO: trigf (fired before): there are 2 tuples in ttest INFO: trigf (fired after ): there are 1 tuples in ttest INFO: trigf (fired before): there are 1 tuples in ttest INFO: trigf (fired after ): there are 0 tuples in ttest So you can see that the documented sequence
of events is not the same as what actually happens.
To prove this I created and ran the
example from the documentation (cut and paste exactly from the
documentation).
Here is the output that I got:
CISX=# INSERT INTO ttest VALUES('100');
NOTICE: trigf (fired before): there are 0 tuples in ttest
NOTICE: trigf (fired after ): there are 1 tuples in ttest
INSERT 17497 1
CISX=# INSERT INTO ttest VALUES('101');
NOTICE: trigf (fired before): there are 1 tuples in ttest
NOTICE: trigf (fired after ): there are 2 tuples in ttest
INSERT 17498 1
CISX=# INSERT INTO ttest VALUES('102');
NOTICE: trigf (fired before): there are 2 tuples in ttest
NOTICE: trigf (fired after ): there are 3 tuples in ttest
INSERT 17499 1
CISX=# INSERT INTO ttest VALUES('122');
NOTICE: trigf (fired before): there are 3 tuples in ttest
NOTICE: trigf (fired after ): there are 4 tuples in ttest
INSERT 17500 1
CISX=# DELETE FROM ttest WHERE x >= '100';
NOTICE: trigf (fired before): there are 4 tuples in ttest
NOTICE: trigf (fired before): there are 3 tuples in ttest
NOTICE: trigf (fired before): there are 2 tuples in ttest
NOTICE: trigf (fired before): there are 1 tuples in ttest
NOTICE: trigf (fired after ): there are 0 tuples in ttest
NOTICE: trigf (fired after ): there are 0 tuples in ttest
NOTICE: trigf (fired after ): there are 0 tuples in ttest
NOTICE: trigf (fired after ): there are 0 tuples in ttest
DELETE 4
As you can see the actual sequence of events differs to what is
documented and what I had logically expected - hence why I was questioning
whether there was a bug with triggers, albeit that I was pointing the finger at
the wrong thing. Next time I want be so keen to come forward with a bug without
investigating it further - sorry for that.
Regards
Donald Fraser.
|
- [BUGS] Trigger function not executing Donald Fraser
- Re: [BUGS] Trigger function not executing Tom Lane
- Re: [BUGS] Trigger function not executing Donald Fraser