Sequence of SQL command execution involving triggers and stored procedures.

Sequence of SQL command execution involving triggers and stored procedures.

am 18.09.2004 06:54:24 von orath

This is a multi-part message in MIME format.

------_=_NextPart_001_01C49D3B.91BA5806
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I am observing the following:
=20
Say there is a stored procedure i.e FUNCTION sp_foo(...) which has say,
SQL commands (any of SELECT,INSERT,UPDATE,DELETE) in its body, say S1
followed by S2 then S3.... Furthermore, execution of statement S2
results in trigger functions getting invoked (that contain yet other SQL
statements).
=20
By the time, the trigger functions execute (as a result of S2), S3 has
already executed.
=20
Note that sp_foo() was invoked via a SELECT i.e SELECT sp_foo(...).
=20
To work around this "behaviour", I wrapped sp_foo() within another
FUNCTION say sp_foo_wrapper(...) and in the body of sp_foo_wrapper() I
did this -
=20
1. INSERT in a scratch table // akin to setting a flag
2. Invoke sp_foo().
3. DELETE from the scratch table // akin to resetting the
flag
=20
Even then, by the time the triggers fired as a result of S2 (in the body
of sp_foo), the DELETE (in step 3 above) had executed.
=20
Questions:
1. Did I miss something very basic?
=20
2. Is this behavior random i.e the manifestation pertians to the
particular SQL commands in my code?
=20
3. How does one achieve, the chronolgy that I want i.e I want S3 AND/OR
step 3 to occur AFTER the triggers (as a result of S2) to fire?
=20
Thanx in advance,
=20
=20
=20
Omkar Rath
Software Engr.
VTG
Cisco Systems Inc.

------_=_NextPart_001_01C49D3B.91BA5806
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable




Message



I am obse=
rving the=20
following:

size=3D2> 

Say there=
is a=20
stored procedure i.e FUNCTION sp_foo(...) which has say, SQL commands =
(any=20
of SELECT,INSERT,UPDATE,DELETE) in its body, say S1 followed by S2 then S3.=
....=20
Furthermore, execution of statement S2 results in trigger functions ge=
tting=20
invoked (that contain yet other SQL statements).

size=3D2> 

By the ti=
me, the=20
trigger functions execute (as a result of S2), S3 has already=20
executed.

size=3D2> 

Note that=
sp_foo()=20
was invoked via a SELECT i.e SELECT sp_foo(...).

size=3D2> 

To work a=
round this=20
"behaviour", I wrapped sp_foo() within another FUNCTION say sp_foo_wrapper(=
....)=20
and in the body of sp_foo_wrapper() I did this -

size=3D2> 

1. INSERT=
=20
<something> in a scratch table // akin to setting a=20
flag

2. Invoke=
=20
sp_foo().

3. DELETE=
=20
<something> from the scratch table // akin to resetting the=20
flag

size=3D2> 

Even then=
, by the=20
time the triggers fired as a result of S2 (in the body of sp_foo), the DELE=
TE=20
(in step 3 above) had executed.

size=3D2> 

size=3D2>Questions:

1. Did I =
miss=20
something very basic?

size=3D2> 

2. Is thi=
s behavior=20
random i.e the manifestation pertians to the particular SQL commands in my=
=20
code?

size=3D2> 

3. How do=
es one=20
achieve, the chronolgy that I want i.e I want S3 AND/OR step 3 to occur AFT=
ER=20
the triggers (as a result of S2) to fire?

size=3D2> 

Thanx in=
=20
advance,

size=3D2> 

size=3D2> 

size=3D2> 

Omkar=20
Rath

Software=
=20
Engr.

size=3D2>VTG

Cisco Sys=
tems=20
Inc.


------_=_NextPart_001_01C49D3B.91BA5806--

Re: Sequence of SQL command execution involving triggers and stored procedures.

am 18.09.2004 18:39:21 von tgl

"Omkar Rath" writes:
> Say there is a stored procedure i.e FUNCTION sp_foo(...) which has say,
> SQL commands (any of SELECT,INSERT,UPDATE,DELETE) in its body, say S1
> followed by S2 then S3.... Furthermore, execution of statement S2
> results in trigger functions getting invoked (that contain yet other SQL
> statements).
>
> By the time, the trigger functions execute (as a result of S2), S3 has
> already executed.

In existing PG releases, AFTER triggers are always fired at the
completion of the whole interactive command (ie, the outer SELECT that
called sp_foo).

There are changes in 8.0 to fire them at the end of the specific query
that triggered them, which I believe will do what you want.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: 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