Re: INSERT RULE QUERY ORDER - Mailing list pgsql-general

From Tom Lane
Subject Re: INSERT RULE QUERY ORDER
Date
Msg-id 13565.1060122275@sss.pgh.pa.us
Whole thread Raw
In response to Re: INSERT RULE QUERY ORDER  (Justin Tocci <jtocci@tlcusa.com>)
List pgsql-general
Justin Tocci <jtocci@tlcusa.com> writes:
> Thanks for the reply Tom, here's the rule that works:

> CREATE RULE tquotehistory_update AS ON UPDATE TO vtquotehistory DO INSTEAD
> (

> INSERT INTO tquotehistory_log ("ID", "Item", "Quote1", "DemandCost1",
> "Quote2", "DemandCost2", "DueDate", "POIntoInmass", "Weeks", "QuoteSent",
> "Reference", "Supplier", "TLCProposal", "Counter", "Notes") VALUES
> (old."ID", old."Item", old."Quote1", old."DemandCost1", old."Quote2",
> old."DemandCost2", old."DueDate", old."POIntoInmass", old."Weeks",
> old."QuoteSent", old."Reference", old."Supplier", old."TLCProposal",
> old."Counter", old."Notes");

> UPDATE tquotehistory SET "Item" = new."Item", "Quote1" = new."Quote1",
> "DemandCost1" = new."DemandCost1", "Quote2" = new."Quote2", "DemandCost2" =
> new."DemandCost2", "DueDate" = new."DueDate", "POIntoInmass" =
> new."POIntoInmass", "Weeks" = new."Weeks", "QuoteSent" = new."QuoteSent",
> "Reference" = new."Reference", "Supplier" = new."Supplier", "TLCProposal" =
> new."TLCProposal", "Counter" = new."Counter", "Notes" = new."Notes" WHERE
> (tquotehistory."ID" = old."ID");

> );

> Switch the order and the INSERT doesn't insert a record into the log, but
> the UPDATE updates and there is no error.

Hm.  Am I right in supposing that vtquotehistory is a view on
tquotehistory?  Does the UPDATE cause the row that was visible in the
view to be no longer visible in the view (or at least not matched by the
constraints on the original UPDATE command)?  If so, that's your problem
--- the "old" references in the INSERT will no longer find any matching
row in the view.

If your goal is to log operations on tquotehistory, my recommendation is
to forget about views and rules and just use a trigger on tquotehistory.
Triggers are *way* easier to understand, even if the notation looks
worse.

            regards, tom lane

pgsql-general by date:

Previous
From: "Joshua D. Drake"
Date:
Subject: Re: migrating data from 7.3.x down to 7.2.x
Next
From: "Joshua D. Drake"
Date:
Subject: Re: optimum postgres server configuration