Help with distinctly non-intuitive rule behaviour

From: "Simon Kinsella" <simon(at)bluefiresystems(dot)co(dot)uk>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Help with distinctly non-intuitive rule behaviour
Date: 2006-03-05 21:18:12
Message-ID: 20060305211817.2DBF3199CFD@smtp04l.fasthosts.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

Can this possibly be right?

From my troubleshooting of a DELETE rule it appears that the rule's WHERE
condition sees the *results* of the rule-actions. Apart from being pretty
odd, this is a proving to be a big problem in my situation.

Here is a cut-down example:

Given a table 'users', let's say we would like to 'soft-delete' users from
the table by rewriting DELETE as UPDATE using the rule below. For active
users the users.user_departed field is 'infinity'::TIMESTAMP. The UPDATE
action sets the timestamp of the user's deletion, if and only if the current
timestamp is in the future:

CREATE RULE rule_soft_delete_user AS ON DELETE TO users
WHERE user_departed > now()
DO INSTEAD
UPDATE users SET user_departed = now()
WHERE user_id = OLD.user_id;

However, this does not work. I know this because I get a foreign-key
violation resulting from an unmolested DELETE action even for a user who has
not been soft-deleted. If as a test I change the UPDATE action so that it
does something other than change the timestamp then the rule is applied.

This seems pretty weird - can it be true?

---
Simon Kinsella
This message has been scanned for viruses.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2006-03-05 21:59:38 Re: Help with distinctly non-intuitive rule behaviour
Previous Message Stephan Szabo 2006-03-05 20:23:42 Re: functions in WHERE clause