Rules and actions involving multiple rows

Started by Jason Goddenalmost 23 years ago4 messagesgeneral
Jump to latest
#1Jason Godden
jasongodden@optushome.com.au

Hi All,

Just have a quick question regarding rules:

In the past I've always used triggers to fire before or after events however I
see that the same thing can be achieved through rules (for logging etc...) so
I created a rule which fires on update (not DO INSTEAD) however it only seems
to work for a single row - whereas the triggers work for all rows updated in
a transaction. Is this correct and if so I presume I should be sticking to
triggers if I want this kind of behaviour?

Rgds,

Jason

#2Alvaro Herrera
alvherre@dcc.uchile.cl
In reply to: Jason Godden (#1)
Re: Rules and actions involving multiple rows

On Sun, Jul 06, 2003 at 02:48:59PM +1000, Jason Godden wrote:

In the past I've always used triggers to fire before or after events however I
see that the same thing can be achieved through rules (for logging etc...) so
I created a rule which fires on update (not DO INSTEAD) however it only seems
to work for a single row - whereas the triggers work for all rows updated in
a transaction.

Rules are a once-per-statement thing, and thus appear to be "for a
single row." Triggers can be once-per-row or once-per-statement. Only
you know what is more appropiate in your scenario...

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Limitate a mirar... y algun dia veras"

#3Dmitry Tkach
dmitry@openratings.com
In reply to: Jason Godden (#1)
Re: Rules and actions involving multiple rows

Jason Godden wrote:

Hi All,

Just have a quick question regarding rules:

In the past I've always used triggers to fire before or after events however I
see that the same thing can be achieved through rules (for logging etc...) so
I created a rule which fires on update (not DO INSTEAD) however it only seems
to work for a single row - whereas the triggers work for all rows updated in
a transaction. Is this correct and if so I presume I should be sticking to
triggers if I want this kind of behaviour?

Rgds,

Hmmm... Works for me... :

testdb=# create table x (x int);
CREATE TABLE
testdb=# create table old_x (x int);
CREATE TABLE
testdb=# insert into x values (1);
INSERT 17204 1
testdb=# insert into x values (2);
INSERT 17205 1
testdb=# create rule xup as on update to x do insert into old_x values
(old.*);
CREATE RULE
testdb=# update x set x=0;
UPDATE 2
testdb=# select * from old_x;
x
---
1
2
(2 rows)

Or were you talking about something else?

Dima

#4Bruce Momjian
bruce@momjian.us
In reply to: Jason Godden (#1)
Re: Rules and actions involving multiple rows

For me, triggers are for modifying the row being
updated/deleted/inserted, while rules are for modifying other rows as
part of the query.

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

Jason Godden wrote:

Hi All,

Just have a quick question regarding rules:

In the past I've always used triggers to fire before or after events however I
see that the same thing can be achieved through rules (for logging etc...) so
I created a rule which fires on update (not DO INSTEAD) however it only seems
to work for a single row - whereas the triggers work for all rows updated in
a transaction. Is this correct and if so I presume I should be sticking to
triggers if I want this kind of behaviour?

Rgds,

Jason

---------------------------(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

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073