Re: Rules and Triggers - Mailing list pgsql-sql

From Ross J. Reedstrom
Subject Re: Rules and Triggers
Date
Msg-id 20020910232816.GA3983@rice.edu
Whole thread Raw
In response to Re: Rules and Triggers  ("Michael Paesold" <mpaesold@gmx.at>)
List pgsql-sql
On Tue, Sep 10, 2002 at 09:45:16PM +0200, Michael Paesold wrote:
> 
> Adam Erickson wrote:
> 
> > Correct me if I'm wrong, but rules constrain the SQL (ie. validation).
> > Triggers are carried out after the SQL is executed and the data is
> modified.
> > So, to answer your question, I think rules come first.  As to which order
> > the rules / triggers are executed probably depends on the order you put
> them
> > in when you created them but I pulled that straight out of my arse.  :)
> 
> That would be correct for triggers with AFTER INSERT/UPDATE/DELETE.
> But what about triggers BEFORE INSERT/UPDATE/DELETE?

An important thing to remember about rules vs. triggers: Rules operate
on an SQL _query_ that involves the table (or view) that the rule is
attached to, whereas triggers operate on the individual _tuples_ of the
table they are attached to. Rules act to rewrite the incoming query,
at the very earliest step: just after parsing, but before planning and
optimizing. So rules _always_ act first. Triggers act when storage gets
hit: when the optimizer has choosen a plan, and passes it to the executor
for, well, execution.

Ross


pgsql-sql by date:

Previous
From: Kevin Brannen
Date:
Subject: Re: Changing Column Type
Next
From: "Andres Sommerhoff"
Date:
Subject: R-Tree, GiST or B-Tree? I will need it?