max_stack_depth error in postgresql I am fairly new to PostgreSQL and would appreciate any help and recommendations.

I'm running the following function to update a table:

$BODY$
  DECLARE
    commercial_period date := (SELECT periods.period FROM common.periods WHERE periods.type = 'commercial');
   --period text := concat('_',right((date_part('year',residential_period))::text,2),'_',right((date_part('year',residential_period))::text,2)::integer+1);
  BEGIN
-------------------------------------------------------------------------------------------------------------
    --copies alternative data from temporal table to common.status
    UPDATE
      common.status
    SET
      implementation_status = A.implementation_status,
      latest_version = A.latest_version
    FROM
      monitoring_commercial_temporal.possible_alternatives AS A
    WHERE
      status.reference = A.reference
      AND (status.update_period = A.update_period
      OR status.update_period IS NULL);
-------------------------------------------------------------------------------------------------------------
    --sets as lastest version any site, not in the temporal list of alternatives
    UPDATE
      common.status
    SET
      latest_version = 'yes'
    WHERE
      (reference, update_period) NOT IN (SELECT reference, update_period FROM monitoring_commercial_temporal.possible_alternatives)
      AND update_period = commercial_period + interval '1 year'
      AND implementation_status &lt;&gt; 'non-preferred alternative'
      AND latest_version IS NULL;
-------------------------------------------------------------------------------------------------------------
   END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.monitoring_update_alternatives_commercial()
  OWNER TO postgres;


but I get the following error:


  ERROR:  stack depth limit exceeded HINT:  Increase the configuration
  parameter "max_stack_depth" (currently 2048kB), after ensuring the
  platform's stack depth limit is adequate.


&gt; CONTEXT:  SQL function "st_intersects" during inlining SQL statement
&gt; "SELECT (SELECT 
&gt;                                     array_agg(status.reference) 
&gt;                                   FROM 
&gt;                                     common.geometries AS A, 
&gt;                                     common.geometries AS B, 
&gt;                                     common.status,
&gt;                                     common.permission_details 
&gt;                                   WHERE 
&gt;                                     NEW.reference = A.reference 
&gt;                                     AND status.reference = B.reference 
&gt;                                     AND status.reference = permission_details.app
&gt;                                     AND NEW.reference &lt;&gt; status.reference
&gt;                                     AND ST_Intersects(B.geom, A.geom)
&gt;                                     AND (NEW.update_period = residential_period + INTERVAL '1 YEAR' OR NEW.update_period IS NULL)
&gt;                                     AND status.update_period = residential_period + INTERVAL '1 YEAR'
&gt;                                     AND permission_details.monitoring_type &lt;&gt; 'commercial'
&gt;                                     AND (((ST_Area(ST_Intersection(B.geom, A.geom))+0.01) /
&gt; (ST_Area(B.geom)+0.01) &gt;= 0.2)
&gt;                                     OR ((ST_Area(ST_Intersection(B.geom, A.geom))+0.01) /
&gt; (ST_Area(A.geom)+0.01) &gt;= 0.2)))" PL/pgSQL function
&gt; monitoring_update_status_before() line 72 at assignment SQL statement
&gt; "UPDATE 
&gt;     common.status AS A   SET
&gt;     trigger = TRUE   WHERE
&gt;     A.latest_version = NEW.reference
&gt;     AND A.update_period = NEW.update_period" PL/pgSQL function monitoring_update_status_after() line 5 at SQL statement


Then it keeps repeating the last 6 lines from the UPDATE
Presumably your trigger firing on UPDATE executes an UPDATE on the same table, which itself fires the triggers, and so on recursively until the stack is full and the stack depth limit exceeded error is raised.

Here's a bare bones example demonstrating the problem:

create table tst(id int primary key, col int, trigger boolean);

create function trg() returns trigger  as $$
 begin update tst set trigger=true where tst.id=NEW.id; return NEW; END
$$ language plpgsql;

create trigger t before update on tst for each row execute procedure trg();

insert into tst(id) values(1),(2);

update tst set col=1 where id=1;
ERROR:  stack depth limit exceeded
HINT:  Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
CONTEXT:  SQL statement "update tst set trigger=false where tst.id=NEW.id"
PL/pgSQL function trg() line 1 at SQL statement
SQL statement "update tst set trigger=false where tst.id=NEW.id"


The mistake in the above case is to issue an UPDATE of the table already being updated, inside the trigger. The correct way is to assign directly NEW.trigger, as in:

create function trg() returns trigger  as
$$
 begin NEW.trigger=true; return NEW; END
$$ language plpgsql;