473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does a 'stable' deferred trigger execution order exist? Is housekeeping for deferred trigger fire events done in one of the system catalogues?

Hi,
If during a transaction a number of deferred triggers are fired, what will be
their execution order upon the commit?

Will they be executed in order of firing or alfabetically or something
entirely different? The docs only mention regular triggers being executed
alfabetically.

And equally important in case it is in firing order, is this order regarded as
'stable' between releases?
Also, are the fire-events of deferred triggers kept in a system catalogue
somewhere, .i.e. would it be possible upon a first time execution of a
deferred trigger to avoid any additional executions of that same trigger that
got scheduled earlier in the transaction due to multiple fire events ?


--
Best,


Frank.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
8 2634
Frank van Vugt <ft**********@f oxi.nl> writes:
If during a transaction a number of deferred triggers are fired, what will be
their execution order upon the commit?
Should be alphabetical within each triggering event, IIRC.
Also, are the fire-events of deferred triggers kept in a system catalogue
somewhere,


No. They're in a list in memory.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
If during a transaction a number of deferred triggers are fired, what
will be their execution order upon the commit?
Should be alphabetical within each triggering event, IIRC.


Mmm, yes, but are all the deferred triggers on the same event 'grouped'?

What I'm thinking about is something like:
BEGIN;
update foo1; => triggers deferred after insert trigger 'Z'
select bar;
update foo2; => triggers deferred after insert triggers 'B' and 'A'
COMMIT;
Now, will I see an execution order of 'Z-A-B' (on alfabet per event per
statement) or 'A-B-Z' (on alfabet per event per transaction)??

For what I want to accomplish, I don't care about the order of A/B, but
together they depend on the work that is done by the earlier triggered Z.

--
Best,


Frank.
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #3
Frank van Vugt wrote:
If during a transaction a number of deferred triggers are fired, what
will be their execution order upon the commit?


Should be alphabetical within each triggering event, IIRC.

Mmm, yes, but are all the deferred triggers on the same event 'grouped'?

What I'm thinking about is something like:
BEGIN;
update foo1; => triggers deferred after insert trigger 'Z'
select bar;
update foo2; => triggers deferred after insert triggers 'B' and 'A'
COMMIT;
Now, will I see an execution order of 'Z-A-B' (on alfabet per event per
statement) or 'A-B-Z' (on alfabet per event per transaction)??

For what I want to accomplish, I don't care about the order of A/B, but
together they depend on the work that is done by the earlier triggered Z.


The best way is to "raise notice" inside the trigger function and observe
the results
Regards
Gaetano Mendola

Nov 23 '05 #4
> Mmm, yes, but are all the deferred triggers on the same event 'grouped'?
What I'm thinking about is something like:
BEGIN;
update foo1; => triggers deferred after insert trigger 'Z'
select bar;
update foo2; => triggers deferred after insert triggers 'B' and 'A'
COMMIT;

What will the resulting trigger execution order be?


In case someone is interested, a little bit of fiddling around with the script
below seems to indicate that for different events, deferred triggers are
always executed in the order they fired. For one and the same event, they
will execute in the order in which they were initially defined.

I'm happy with the outcome, but still would like to find out though whether
this execution order is regarded as 'stable', i.e. is it part of any spec, is
it likely to be changed between versions, etc.


Best,


Frank.
*************** *************** *************** *************** *************** ************
drop table f cascade;
drop table f_update cascade;
drop function tr_f() cascade;
drop function tr_f_update_a_d ef() cascade;
drop function tr_f_update_b_d ef() cascade;
drop function tr_f_update_z_d ef() cascade;

create table f (id int);
create table f_update (id int);

create function tr_f() RETURNS trigger LANGUAGE 'plpgsql' STABLE STRICT
SECURITY INVOKER AS '
DECLARE
BEGIN
RAISE NOTICE ''tr_f() triggered'';
INSERT INTO f_update VALUES(1);

RETURN NULL;
END;';

CREATE FUNCTION tr_f_update_a_d ef() RETURNS trigger LANGUAGE 'plpgsql' STABLE
STRICT SECURITY INVOKER AS '
DECLARE
BEGIN
RAISE NOTICE ''tr_f_update_a _def() triggered'';

RETURN NULL;
END;';

CREATE FUNCTION tr_f_update_b_d ef() RETURNS trigger LANGUAGE 'plpgsql' STABLE
STRICT SECURITY INVOKER AS '
DECLARE
BEGIN
RAISE NOTICE ''tr_f_update_b _def() triggered'';

RETURN NULL;
END;';

CREATE FUNCTION tr_f_update_z_d ef() RETURNS trigger LANGUAGE 'plpgsql' STABLE
STRICT SECURITY INVOKER AS '
DECLARE
BEGIN
RAISE NOTICE ''tr_f_update_z _def() triggered'';

RETURN NULL;
END;';

CREATE TRIGGER f_iud AFTER INSERT OR UPDATE OR DELETE ON f FOR EACH ROW
EXECUTE PROCEDURE tr_f();
CREATE CONSTRAINT TRIGGER f_b_def AFTER INSERT ON f_update DEFERRABLE
INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE tr_f_update_b_d ef();
CREATE CONSTRAINT TRIGGER f_a_def AFTER INSERT ON f_update DEFERRABLE
INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE tr_f_update_a_d ef();
CREATE CONSTRAINT TRIGGER f_z_def AFTER INSERT ON f_update DEFERRABLE
INITIALLY DEFERRED FOR EACH ROW EXECUTE PROCEDURE tr_f_update_z_d ef();
BEGIN;
INSERT INTO f_update VALUES(1);
INSERT INTO f VALUES(1);
COMMIT;
*************** *************** *************** *************** *************** ************
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5
Frank van Vugt <ft**********@f oxi.nl> writes:
In case someone is interested, a little bit of fiddling around with
the script below seems to indicate that for different events, deferred
triggers are always executed in the order they fired. For one and the
same event, they will execute in the order in which they were
initially defined.


"Order in which they were defined"? Hmm, I thought we had agreed long
since to trigger these things in alphabetical order. Something is wrong
here.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #6
> > deferred triggers are always executed in the order they fired
for one and the same event, they will execute in order of definition
"Order in which they were defined"? Hmm, I thought we had agreed long
since to trigger these things in alphabetical order. Something is wrong
here.


All 'plain' triggers on one and the same event are indeed triggered in
alphabetical order. It's the deferred triggers that aren't, not for the set
of triggers on one event and not for the complete collection of triggers for
that transaction.
Allow me to repeat and possibly clarify an earlier (personal) point of
interest:

Any execution order for regular triggers would be as good as any other, given
the fact that it is a single atomic event that triggered them, a design
should not try to make use of a specific order. The same goes for deferred
triggers that fired on the same event, the particular order of trigger
execution within this set should not be of any interest.

However, the accumulated collection of deferred triggers at the end of a
transaction possibly exists of many sets, each of which was fired on a
different event, on a different point in time. Therefore, IMHO, it is
possible for a dependancy between certain sets to be valid and handled
properly by the fact that at least the sets itself are executed in the same
order as the original events they fired upon.

So, the one thing that would make me unhappy here is when the complete
collection of triggers would simply be executed in alphabetical order. And
since I'd obviously like to make use of the earlier described dependancies
between trigger-sets, I'm a bit cautious regarding a possible 'silent change
in behaviour' between versions.


--
Best,


Frank.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #7
Frank van Vugt <ft**********@f oxi.nl> writes:
"Order in which they were defined"? Hmm, I thought we had agreed long
since to trigger these things in alphabetical order. Something is wrong
here.
Allow me to repeat and possibly clarify an earlier (personal) point of
interest: Any execution order for regular triggers would be as good as any other, given
the fact that it is a single atomic event that triggered them, a design
should not try to make use of a specific order. The same goes for deferred
triggers that fired on the same event, the particular order of trigger
execution within this set should not be of any interest.
This is perhaps true for "cleanly designed" applications, but people
have requested that we nail down the execution order, and we have
responded by specifying that it's alphabetical within an event.
(Not order of creation --- alphabetical lets you insert later-created
triggers where you need to in the firing order, order-of-creation
does not.) The intention was certainly to apply this to AFTER as well
as BEFORE triggers. We'll need to look and see why it's not working.
However, the accumulated collection of deferred triggers at the end of a
transaction possibly exists of many sets, each of which was fired on a
different event, on a different point in time. Therefore, IMHO, it is
possible for a dependancy between certain sets to be valid and handled
properly by the fact that at least the sets itself are executed in the same
order as the original events they fired upon.


Right. This is handled by appending new pending-trigger events to a
global list when they are detected. Barring oddities such as different
deferral specifications, they should be executed in order of detection.
I would have expected triggers for the same event to be inserted in
alphabetical order ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #8
> > Any execution order for regular triggers would be as good as any other
This is perhaps true for "cleanly designed" applications, but people
have requested that we nail down the execution order, and we have
responded by specifying that it's alphabetical within an event.
I understand and actually meant to say that since nothing should 'depend' on
any particular order, executing these triggers alphabetically seems a logical
approach with the extra bonus you mention.
The intention was certainly to apply this to AFTER as well
as BEFORE triggers. We'll need to look and see why it's not working.
Just to avoid any misunderstandin gs:

- regular triggers DO execute in alphabetical order

- it's the deferred triggers that execute per event in order of definition
at least the sets itself are executed in the same order as the original
events they fired upon.

Right. This is handled by appending new pending-trigger events to a
global list when they are detected. Barring oddities such as different
deferral specifications, they should be executed in order of detection.


That's the big 'YES' I was looking for ;-) Thanks.
I would have expected triggers for the same event to be inserted in
alphabetical order ...


Yep, me too, but apart from the fact that I'm o.k. with the way it currently
works, I imagine this is not exactly a high-priority issue right now ;-)
Thanks again for the replies.

--
Best,


Frank.
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2263
by: ZRexRider | last post by:
Hi, I have trigger that enforces the creation of a sortorder that is always 1 digit higher than the current highest on Inserts. This trigger works great if I add one row at a time so I think the logic is sound. However, I have a Stored Procedure that copies a bunch of rows into this table and all of the SortOrder values come up as 0. This stored procedure is doing an "Insert Into" and will insert numerous rows (10-20) at once.
33
4770
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL, PARENT_CATEGORY_ID INTEGER, CATEGORY_ICON IMAGE, DEPTH INTEGER,
6
7143
by: Mary | last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger" table containing numerous triggers - each of which is activated by an UPDATE to a different column of this "trigger" table. When the triggers are fired, various other operations are performed on other tables in the database. The triggers are not created on these other tables because other programs perform updates to these tables and we do not want the triggers to fire...
1
1885
by: Jesper | last post by:
Hi, Background. In an engineering application I consider using events to control the flow in the program under the following philosophy: When a value X change it fires a "i'm changed" event. A value Y will is a function of X and will therefore subscribe to X-changed event. Y will recalculate and fire its own "i'm changed" in order for other values dependent on Y to recalculate. This would be
13
5045
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
6
4475
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are clicked. I have read several posts which say to put your column generating section in the Page_Init section and it will solve the problem....however, it hasn't solved mine. Can somebody please take a look at this and provide any insight if possible?
0
1934
by: Bob | last post by:
Hi, I have a very odd postgresql problem. I have some subqueries contained within a function which are looking for unrefernced data and then delete any rows that are found: CREATE OR REPLACE FUNCTION housekeeping() RETURNS TRIGGER AS ' BEGIN
2
3057
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change.
11
7873
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG' does not exist drop table log_errors_tab;
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.