Trigger Execution Order in Postgres

SQL specifies that multiple triggers should be fired in time-of-creation order. PostgreSQL uses name order, which was judged to be more convenient.

1
create table data (
2
id serial primary key,
3
value text not null
4
);
5
6
create or replace function c()
7
returns trigger as $$
8
begin
9
raise notice 'c';
10
return new;
11
end;
12
$$ language plpgsql;
13
14
create or replace function b()
15
returns trigger as $$
16
begin
17
raise notice 'b';
18
return new;
19
end;
20
$$ language plpgsql;
21
22
create or replace function a()
23
returns trigger as $$
24
begin
25
raise notice 'a';
26
return new;
27
end;
28
$$ language plpgsql;
29
30
create trigger c
31
before insert on data
32
for each row
33
execute function c();
34
35
create trigger b
36
before insert on data
37
for each row
38
execute function b();
39
40
create trigger a
41
before insert on data
42
for each row
43
execute function a();

The triggers were created with order c,b,a, but executed in order a,b,c.

1
insert into data (value) values ('hello');
2
3
NOTICE: a
4
NOTICE: b
5
NOTICE: c

Wow! You read the whole thing. People who make it this far sometimes want to receive emails when I post something new.

I also have an RSS feed.