Unix Technical Forum

Changes to not deferred FK in 8.0.3 to 7.4?

This is a discussion on Changes to not deferred FK in 8.0.3 to 7.4? within the Pgsql General forums, part of the PostgreSQL category; --> Janning Vygen <vygen@gmx.de> writes: > On more related question: > I updated pg_trigger and pg_constraint and changed all my ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 04-09-2008, 05:01 AM
Tom Lane
 
Posts: n/a
Default Re: Changes to not deferred FK in 8.0.3 to 7.4?

Janning Vygen <vygen@gmx.de> writes:
> On more related question:
> I updated pg_trigger and pg_constraint and changed all my FK:


> UPDATE pg_trigger
> SET
> tgdeferrable = true,
> tginitdeferred = true
> WHERE tgconstrname LIKE 'fk_%'
> ;


> UPDATE pg_constraint
> SET
> condeferrable = true,
> condeferred = true
> WHERE conname LIKE 'fk_%'
> ;


No, only the triggers that are for checks should be marked
deferrable/deferred. These are the ones using functions
RI_FKey_check_ins
RI_FKey_check_upd
RI_FKey_noaction_del
RI_FKey_noaction_upd
You want the others nondeferrable because (a) that's the standard
behavior and (b) it'll ensure that the actions happen before the
checks are made.

regards, tom lane

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 04-09-2008, 05:01 AM
Janning Vygen
 
Posts: n/a
Default Re: Changes to not deferred FK in 8.0.3 to 7.4?

Am Montag, 18. Juli 2005 18:18 schrieb Tom Lane:
> Stephan Szabo <sszabo@megazone.bigpanda.com> writes:
> > On Mon, 18 Jul 2005, Tom Lane wrote:
> >> I don't see why.

> >
> > Except that before I think the order would have looked like (for 1 row)
> > Originating Action
> > Trigger A on originating table that does update
> > Trigger B on originating table that does update
> > Trigger A1 caused by A
> > Trigger B1 caused by B
> >
> > I think now it acts like:
> > Originating Action
> > Trigger A on originating table that does update
> > Trigger A1 caused by A
> > Trigger B on originating table that does update
> > Trigger B1 caused by B

>
> Ah, of course. So that could explain Janning's difference in results
> without having to assume any rearrangement from pg_dump (not but what
> we shouldn't take a second look at pg_dump's behavior anyway).


a FK results in a "referential action" which updates the FK attributes and a
"referential constraint" which checks if all FKs are ok, right?

So my understanding of what's going on is:

table1
/ \
table2 table3
\ /
table4

UPDATE Table1 PK = $2 WHERE PK = $1
-> UPDATE Table2 FK = $2 WHERE FK = $1
-> UPDATE Table4 FK1 = $2 WHERE FK1 = $1
-> no action
-> CHECK table4 FK1 in table2
-> CHECK table4 FK2 in table3 (***)
-> CHECK table2 FK in table 1
-> UPDATE Table3 FK = $2 WHERE FK = $1
-> UPDATE Table4 FK2 = $2 WHERE FK2 = $1
-> no action
-> CHECK table4 FK1 in table2
-> CHECK table4 FK2 in table3
-> CHECK table3 FK in table 1
-> no check on table1

if fk1 and fk2 on table4 overlap in one column, i get an error at (***)
because table3 is not updated at the moment. this error doesn't show up with
deferrable constraints because all check clauses are moved to end of the
transaction.

so i think my problem is the overlapping of a FK in table4. In 7.4 the
beahaviour was like this:

UPDATE Table1 PK = $2 WHERE PK = $1
-> UPDATE Table2 FK = $2 WHERE FK = $1
-> UPDATE Table3 FK = $2 WHERE FK = $1
-> no check on table1
-> UPDATE Table4 FK1 = $2 WHERE FK1 = $1
-> UPDATE Table4 FK2 = $2 WHERE FK2 = $1
-> no action
-> CHECK table4 FK1 in table2
-> CHECK table4 FK2 in table3 (***)
-> CHECK table4 FK1 in table2
-> CHECK table4 FK2 in table3
-> CHECK table2 FK in table 1
-> CHECK table3 FK in table 1

I dont' got an error because table3 is already updated at (***)
In my example there are NO circular references, they just overlap on table4
which is a common technique if you have natural primary keys.

My "feeling" is:
If you DON'T have circular references, you should not need defferable
constraints.

So I don't see any benefit of changes the order of execution, but anyway: two
remarks:

from the docs (CREATE TABLE)
"A constraint that is not deferrable will be checked immediately after every
command." What means command in this sentence. Each Update which is triggered
by a FK or my original statement?" To me "statment" means the user statement.
so checks should be done after statement and all fired trigger statements are
complete. But this isn't the case. It should be
"A constraint that is not deferrable will be checked immediately after
completion of the triggering query".

From the realease notes8.0)
"Non-deferred AFTER triggers are now fired immediately after completion of the
triggering query, rather than upon finishing the current interactive command.
This makes a difference when the triggering query occurred within a function:
the trigger is invoked before the function proceeds to its next operation."

it should be mentioned, that is makes a difference if you have overlapping FKs
like i have.

I hope that all this stuff i just wrote is mostly correct and maybe it helps
you improving postgresql. If i can help any further with a complete example,
please let me know.

On more related question:
I updated pg_trigger and pg_constraint and changed all my FK:

UPDATE pg_trigger
SET
tgdeferrable = true,
tginitdeferred = true
WHERE tgconstrname LIKE 'fk_%'
;

UPDATE pg_constraint
SET
condeferrable = true,
condeferred = true
WHERE conname LIKE 'fk_%'
;

did i make it right this time updating the pg_catalog? Or is there more to do
in pg_catalog?

kind regards
janning


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 04-09-2008, 05:01 AM
Stephan Szabo
 
Posts: n/a
Default Re: Changes to not deferred FK in 8.0.3 to 7.4?

On Mon, 18 Jul 2005, Tom Lane wrote:

> Stephan Szabo <sszabo@megazone.bigpanda.com> writes:
> > On Mon, 18 Jul 2005, Tom Lane wrote:
> >> I don't see why.

>
> > Except that before I think the order would have looked like (for 1 row)
> > Originating Action
> > Trigger A on originating table that does update
> > Trigger B on originating table that does update
> > Trigger A1 caused by A
> > Trigger B1 caused by B

>
> > I think now it acts like:
> > Originating Action
> > Trigger A on originating table that does update
> > Trigger A1 caused by A
> > Trigger B on originating table that does update
> > Trigger B1 caused by B

>
> Ah, of course. So that could explain Janning's difference in results
> without having to assume any rearrangement from pg_dump (not but what
> we shouldn't take a second look at pg_dump's behavior anyway).
>
> >> Do you think that's enough, or do we need to add more mechanism to
> >> ensure that even non-deferred constraint checks fire after all
> >> referential actions are complete?

>
> > I think that's probably enough for now. We probably will need to do
> > something, but since we still haven't managed to work out all of these
> > timing problems, I think it needs a bunch of going over the spec before
> > trying to actually do any changes.

>
> Agreed, this doesn't seem like an area for hasty solutions.
>
> The short-term answer for Janning is definitely to make his check
> constraints deferred, but we should look at whether our current ordering
> of non-deferred checks is really per spec or not.


It seems like we're off from reading SQL03 in various ways, but I don't
fully understand all of the rules and how they're set up.

It seems to me that the referential action text is now implying that
after triggers caused by referential actions become part of the
trigger context of the statement that triggered them. GR5 sets SSC to the
current trigger execution context (presumably for the statement doing the
action) and the ref action texts add state changes to SSC.

Taking the above with the fact that the "statement" triggers for
referential action are always empty implies to me that a referential
action change isn't a statement; GR9-11 don't seem to affect this, but
give some weight, since otherwise separately triggered events would not
count for triggered data change events I think.

GR14 seems to codify the fact that referential actions that cause other
referential actions happen immediately.

I really don't understand what they've done with GR15 and 16. 15 would
seem to say that in the case of a cascade delete, the rows are marked for
deletion and then the before triggers are called, but that doesn't make
much sense to me.

-------------

So, it looks to me like the following (minus before deletion triggers
caused by ref actions) from combining the 14.* effect list and the above
for delete (update looks similar). I'm also a little unsure of the last
two. The notes on the rules that applies after trigger says, "All
constraints have already been checked..." which makes it sound like that
comes before the after triggrs to me. I'm going to keep looking through
this, but figured I'd throw something out for people to rip apart.

On delete statement:
Make a new trigger context
Run before triggers
Mark for deletion
begin 11.8 rules:
If there are any referential actions,
For each action,
If it's a cascade,
Mark referencing rows for deletion
Add after row triggers for that to the context created above
If it's a restrict,
Error or not
If it's a SET *,
Run row level before triggers for the triggered update
Identify for replacement
Add after row triggers for triggered update
If any state changes were done by the preceding, apply 11.8 rules
again until there aren't any more changes
end 11.8 rules
Delete the rows marked for deletion
Evaluate constraints
Run after triggers


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 04-09-2008, 05:02 AM
Janning Vygen
 
Posts: n/a
Default Re: Changes to not deferred FK in 8.0.3 to 7.4?

[sorry for resending again. i am not at my usual desktop at the moment and
used the wrong sender address]

Am Dienstag, 19. Juli 2005 15:40 schrieb Tom Lane:
> Janning Vygen <vygen@gmx.de> writes:
> > On more related question:
> > I updated pg_trigger and pg_constraint and changed all my FK:
> >
> > UPDATE pg_trigger
> > SET
> > tgdeferrable = true,
> > tginitdeferred = true
> > WHERE tgconstrname LIKE 'fk_%'
> > ;
> >
> > UPDATE pg_constraint
> > SET
> > condeferrable = true,
> > condeferred = true
> > WHERE conname LIKE 'fk_%'
> > ;

>
> No, only the triggers that are for checks should be marked
> deferrable/deferred. These are the ones using functions
> RI_FKey_check_ins
> RI_FKey_check_upd
> RI_FKey_noaction_del
> RI_FKey_noaction_upd
> You want the others nondeferrable because (a) that's the standard
> behavior and (b) it'll ensure that the actions happen before the
> checks are made.


ok thanks. i do it now like this:

UPDATE pg_trigger
SET
tgdeferrable = true,
tginitdeferred = true
WHERE tgconstrname LIKE 'fk_%'
AND tgfoid IN (
SELECT oid FROM pg_proc
WHERE proname IN (
'RI_FKey_check_ins', 'RI_FKey_check_upd',
'RI_FKey_noaction_del', 'RI_FKey_noaction_upd')
)
;

UPDATE pg_constraint
SET
condeferrable = true,
condeferred = true
WHERE conname LIKE 'fk_%'
;

COMMIT;

This should work i hope, but i feel a little bit unsure if hacking the
pg_catalog is a good way to do it. Maybe I should have take the long, but
secure way by modifying the schema with ddl statements.

kind regards,
janning


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 04-09-2008, 05:02 AM
Janning Vygen
 
Posts: n/a
Default Re: Changes to not deferred FK in 8.0.3 to 7.4?

Am Dienstag, 19. Juli 2005 15:40 schrieb Tom Lane:
> Janning Vygen <vygen@gmx.de> writes:
> > On more related question:
> > I updated pg_trigger and pg_constraint and changed all my FK:
> >
> > UPDATE pg_trigger
> > SET
> > tgdeferrable = true,
> > tginitdeferred = true
> > WHERE tgconstrname LIKE 'fk_%'
> > ;
> >
> > UPDATE pg_constraint
> > SET
> > condeferrable = true,
> > condeferred = true
> > WHERE conname LIKE 'fk_%'
> > ;

>
> No, only the triggers that are for checks should be marked
> deferrable/deferred. These are the ones using functions
> RI_FKey_check_ins
> RI_FKey_check_upd
> RI_FKey_noaction_del
> RI_FKey_noaction_upd
> You want the others nondeferrable because (a) that's the standard
> behavior and (b) it'll ensure that the actions happen before the
> checks are made.


ok thanks. i do it now like this:

UPDATE pg_trigger
SET
tgdeferrable = true,
tginitdeferred = true
WHERE tgconstrname LIKE 'fk_%'
AND tgfoid IN (
SELECT oid FROM pg_proc
WHERE proname IN (
'RI_FKey_check_ins', 'RI_FKey_check_upd',
'RI_FKey_noaction_del', 'RI_FKey_noaction_upd')
)
;

UPDATE pg_constraint
SET
condeferrable = true,
condeferred = true
WHERE conname LIKE 'fk_%'
;

COMMIT;

This should work i hope, but i feel a little bit unsure if hacking the
pg_catalog is a good way to do it. Maybe I should have take the long, but
secure way by modifying the schema with ddl statements.

kind regards,
janning


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:14 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com