Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql: Trigger Function on After Insert/Updates: Stack Depth Limit Error

I have a Postgresql database Trigger which is supposed to invoke a Function after either a new row is Insert or a row is Updated. Here is the Trigger:

CREATE TRIGGER firefly_new_geom
AFTER INSERT OR UPDATE 
ON public.entries FOR EACH STATEMENT 
EXECUTE PROCEDURE public.firefly_addupdate_creategeometry();

And here is the function which is being called:

   BEGIN
   UPDATE entries SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326) where geom is null;
   return new;
   END;

However, when I make any data change, directly using a client application, I get the message: Stack Depth Limit Exceeded. I thought, by having the 'where' clause I should not see this problem but still see this. Any idea? Thanks!

like image 448
IrfanClemson Avatar asked Jun 20 '26 04:06

IrfanClemson


1 Answers

I was in an infinite loop within the Trigger. Here is how I fixed the problem. Thanks to:

http://www.depesz.com/2012/02/01/waiting-for-9-2-trigger-depth/

The Trigger DDL:

CREATE TRIGGER firefly_new_geom
AFTER INSERT OR UPDATE 
ON public.entries FOR EACH STATEMENT 
EXECUTE PROCEDURE public.firefly_addupdate_creategeometry();

The Function:

 BEGIN
    IF pg_trigger_depth() <> 1 THEN
        RETURN NEW;
    END IF;
   UPDATE entries SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);
   return new;
END;
like image 82
IrfanClemson Avatar answered Jun 23 '26 02:06

IrfanClemson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!