![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi every body, If : create table T(id int, x int, ..., ... ); alter table T alter id set default nextval('my_sequence'); alter table T alter x set default currval('my_sequence'); Can i be sure that when an insert occurs, id is assigned before x ? Merci d'avance ;-) |
#3
| |||
| |||
|
|
Hi every body, If : create table T(id int, x int, ..., ... ); alter table T alter id set default nextval('my_sequence'); alter table T alter x set default currval('my_sequence'); Can i be sure that when an insert occurs, id is assigned before x ? |
#4
| |||
| |||
|
|
I think, it is a nice idea, but also a wrong idea... I agree. In the first place I wanted to perform the assignation of x |
#5
| |||
| |||
|
|
Andreas Kretschmer écrivait : I think, it is a nice idea, but also a wrong idea... I agree. In the first place I wanted to perform the assignation of x with a trigger, but it seems that if : create table T(id int, x int, ..., ... ); alter table T alter id set default nextval('my_sequence'); in a after insert trigger I can't do : new.x=new.id because the default clause is not executed yet... Am I right ? |
#6
| |||
| |||
|
|
no you can't do that in an after insert trigger because the record is already inserted and modifying new will have no effect. |
#7
| |||
| |||
|
|
no you can't do that in an after insert trigger because the record is already inserted and modifying new will have no effect. Wrong. Phrases "before" and "after" refer only to commit processing. Triggers marked as "before transaction" are executed before commit processing, during the initial block modification phase. Triggers marked "after transaction" are executed after the block modification phase, during the commit processing. In other words, "before" trigger will throw an error immediately, while the "after" trigger will throw an error only during the commit processing. |
#8
| |||
| |||
|
|
Mladen Gogala wrote: no you can't do that in an after insert trigger because the record is already inserted and modifying new will have no effect. Wrong. Phrases "before" and "after" refer only to commit processing. Triggers marked as "before transaction" are executed before commit processing, during the initial block modification phase. Triggers marked "after transaction" are executed after the block modification phase, during the commit processing. In other words, "before" trigger will throw an error immediately, while the "after" trigger will throw an error only during the commit processing. Sorry, but Jasen is right and you are wrong. BEFORE and AFTER in triggers has nothing to do with commit. "after" is *not* short for "after transaction". |
|
The documentation for the CREATE TRIGGER command describes in detail when the trigger gets fired. |
|
Here is the proof of concept that it will work with a BEFORE trigger: test=> CREATE TABLE t(id int, x int); test=> CREATE SEQUENCE my_sequence; test=> ALTER TABLE t ALTER id SET DEFAULT nextval('my_sequence'); test=> CREATE FUNCTION t_set_x_trig() RETURNS trigger test-> LANGUAGE plpgsql AS test-> $$BEGIN NEW.x := NEW.id; RETURN NEW; END;$$; test=> CREATE TRIGGER t_set_x BEFORE INSERT ON t FOR EACH ROW test- EXECUTE PROCEDURE t_set_x_trig(); test=> INSERT INTO t(id, x) VALUES (DEFAULT, DEFAULT); test=> SELECT * FROM t; id | x ----+--- 1 | 1 (1 row) Yours, Laurenz Albe |
#9
| |||
| |||
|
|
Here is the proof of concept that it will work with a BEFORE trigger: test=> CREATE TABLE t(id int, x int); test=> CREATE SEQUENCE my_sequence; test=> ALTER TABLE t ALTER id SET DEFAULT nextval('my_sequence'); test=> CREATE FUNCTION t_set_x_trig() RETURNS trigger test- LANGUAGE plpgsql AS test-> $$BEGIN NEW.x := NEW.id; RETURN NEW; END;$$; test=> CREATE TRIGGER t_set_x BEFORE INSERT ON t FOR EACH ROW test- EXECUTE PROCEDURE t_set_x_trig(); test=> INSERT INTO t(id, x) VALUES (DEFAULT, DEFAULT); test=> SELECT * FROM t; id | x ----+--- 1 | 1 (1 row) Yours, Laurenz Albe I apologize, but I am not really sure what should this prove. I don't really have time now but I will create and explain my examples later today. |
#10
| |||
| |||
|
|
Funny thing is that the same is the case with Oracle: |
![]() |
| Thread Tools | |
| Display Modes | |
| |