Skip site navigation (1) Skip section navigation (2)

Re: Preventing Deletions with triggers

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Post <POSTJL(at)milwaukee(dot)k12(dot)wi(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Preventing Deletions with triggers
Date: 2004-05-21 15:28:52
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Jeff Post <POSTJL(at)milwaukee(dot)k12(dot)wi(dot)us> writes:
> CREATE or replace FUNCTION person_fake_delete() RETURNS TRIGGER AS '
>      BEGIN
>          OLD.status := 1; -- This does the fake deletion
>          RETURN NULL;  -- I thought this would prevent the delete from 
> actually happening.
>      END;
> ' LANGUAGE 'plpgsql';

> create trigger person_fake_delete
> before delete on person
> for each row EXECUTE PROCEDURE
> person_fake_delete();

> This however does not work.  the tupple is still deleted from the 
> table.    Any Ideas?

It works for me, in the sense that returning NULL prevents the
deletion.  However that assignment to OLD is a no-op: you can't change
the tuple that way.  You'd have to do something like

	UPDATE person SET status = 1 WHERE key = OLD.key;

("key" being whatever your primary key for the table is)

			regards, tom lane

In response to

pgsql-sql by date

Next:From: Alexander M. PravkingDate: 2004-05-22 17:54:19
Subject: Memory usage on subselect
Previous:From: Tom LaneDate: 2004-05-21 15:05:28
Subject: Re: Problem with JOINS

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group