Postgresql触发器,仅对statemnt执行一次更新查询

Postgresql触发器,仅对statemnt执行一次更新查询,postgresql,Postgresql,当我需要使用触发器时,我使用PostgreSQL 11.8和faced task。主要目标-当设置或更新表产品中的某些行时,在执行update或INXERT后,我需要为表产品中具有相同组标识的所有现有行的数据创建集合。我扣了这个扳机 CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER AS $maint_sales_summary_bytime$ DECLARE delta_tim

当我需要使用触发器时,我使用PostgreSQL 11.8和faced task。主要目标-当设置或更新表产品中的某些行时,在执行update或INXERT后,我需要为表产品中具有相同组标识的所有现有行的数据创建集合。我扣了这个扳机

CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER
AS $maint_sales_summary_bytime$
    DECLARE
        delta_time_key          timestamp;
        delta_group_identity   varchar;

    BEGIN



        IF (TG_OP = 'UPDATE') THEN

            delta_time_key = NEW.created_at;
            delta_group_identity = NEW.group_identity;

        ELSIF (TG_OP = 'INSERT') THEN

            delta_time_key = NEW.created_at;
            delta_group_identity = NEW.group_identity;

        END IF;

                UPDATE products
                SET 
                created_at = delta_time_key
                WHERE group_identity = delta_group_identity;    

        RETURN NULL;

    END;
$maint_sales_summary_bytime$ LANGUAGE plpgsql;

CREATE TRIGGER maint_sales_summary_bytime
AFTER INSERT OR UPDATE ON products
    FOR EACH ROW EXECUTE PROCEDURE maint_sales_summary_bytime();
但当我尝试执行它时

UPDATE products
SET name = 'hello test1'
WHERE id = 131545
我面临着78秒的时间和错误

UPDATE products
    SET name = 'hello test1'
    WHERE id = 131545
> 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 "SELECT 1 FROM ONLY "public"."brand" x WHERE "id" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function maint_sales_summary_bytime() line 23 at SQL statement
  SQL statement "UPDATE products
                  SET 
                                    created_at = delta_time_key
                  WHERE group_identity = delta_group_identity"
  PL/pgSQL function main
> Time: 86.868s

看起来像触发器尝试对products中的每一行执行此查询,但我只需要在对一行执行statment INSERT或UPDATE后执行此触发器一次

您的触发器是递归触发器

您可以尝试使用以下方法禁用递归性:

CREATE TRIGGER maint_sales_summary_bytime
AFTER INSERT OR UPDATE ON products
FOR EACH ROW 
WHEN (pg_trigger_depth() = 0)
EXECUTE PROCEDURE maint_sales_summary_bytime();

请参阅。

不相关,但是:开头的IF语句似乎没有用,因为两个分支做的事情完全相同。无论如何,也不需要变量,因为在插入或更新产品后,当每行pg_TRIGGER_depth=0时,您可以在UPDATE语句中引用新记录,也可以在产品上创建触发器maint_sales_summary_bytime执行过程maint_sales_summary_;我遇到了>错误:第4行或附近的语法错误:对于每一行,执行程序maint\u sales\u summary\u bytim。当pg\u TRIGGER\u depth=0执行程序maint\u sales\u summary\u bytime时,在插入或更新每一行的产品后,创建触发器maint\u sales\u summary\u bytime;很有魅力,谢谢好的,我已经确定了答案。