Search code examples
functionpostgresqltriggersstackdepth

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!


Solution

  • 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;