Re: Trigger to update records out of memory

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: DeJuan Jackson <djackson(at)speedfc(dot)com>
Cc: Robert Fitzpatrick <robert(at)webtent(dot)com>, PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Trigger to update records out of memory
Date: 2004-06-18 23:04:42
Message-ID: 4390.1087599882@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

DeJuan Jackson <djackson(at)speedfc(dot)com> writes:
> I think the IMMUTABLE might be your issue.

Nah, the problem is that the trigger is recursive.
The UPDATEs it performs internally trigger the trigger again,
resulting in another UPDATE, resulting in another trigger call...
eventually you run out of memory.

AFAICT the UPDATEs are the hardest possible way to do things
anyhow. You're in a BEFORE trigger, you can just alter the NEW
record to alter what will be stored.

Finally, RETURN NULL is not what you want in a BEFORE trigger;
that disables actually doing anything. (In this case it fails
before you ever get that far :-()

In short the function ought to look more like

CREATE OR REPLACE FUNCTION clear_common_groups () RETURNS trigger AS'
BEGIN
IF NEW.common_area = ''t'' THEN
NEW.common_area = ''f'';
END IF;
IF NEW.exterior_area = ''t'' THEN
NEW.exterior_area = ''f'';
END IF;
RETURN NEW;
END;
' LANGUAGE 'plpgsql';

I agree that the IMMUTABLE and other decorations are useless though...

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message raptor 2004-06-18 23:56:18 Re: virtual fields on VIEW?
Previous Message Robert Fitzpatrick 2004-06-18 22:26:53 UPDATE inside an Update trigger