返回但不使用表拆分postgres在INSERT触发函数上写

问题描述

美好的一天,

我有以下问题:

我维护着一个具有巨大表的数据库-该表的内容在每日表中被拆分/聚集。
为此,我有一个触发函数,它将数据插入正确的表中

这是我的触发器:

CREATE OR REPLACE FUNCTION "public"."insert_example"()
  RETURNS "pg_catalog"."trigger" AS $BODY$
    DECLARE
        _tabledate text;
        _tablename text;
        _start_date text;
        _end_date text;
        
    BEGIN
        --Takes the current inbound "time" value and determines when midnight is for the given date
        _tabledate  := to_char(NEW."insert_date",'YYYYMMDD');
        _start_date     := to_char(NEW."insert_date",'YYYY-MM-DD 00:00:00');
        _end_date   := to_char(NEW."insert_date",'YYYY-MM-DD 23:59:59');
        _tablename  := 'zz_example_'||_tabledate;

        -- Check if the partition needed for the current record exists
        PERFORM 1
        FROM   pg_catalog.pg_class c
        JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
        WHERE  c.relkind = 'r'
        AND    c.relname = _tablename
        AND    n.nspname = 'public';

        -- If the partition needed does not yet exist,then we create it:
        -- Note that || is string concatenation (joining two strings to make one)
        IF NOT FOUND THEN
            EXECUTE '
                CREATE TABLE IF NOT EXISTS "'||quote_ident(_tablename)||'" ( LIKE "example_table" INCLUDING ALL ) 
                    INHERITS ("example_table");

                ALTER TABLE         "'||quote_ident(_tablename)||'"
                    ADD FOREIGN KEY ("log_id")                          REFERENCES "foreign_example1" ("log_id")                                                            ON DELETE SET NULL,ADD FOREIGN KEY ("detail_log_id")                   REFERENCES "foreign_example2" ("log_id")                                                            ON DELETE SET NULL,ADD FOREIGN KEY ("image_id")                        REFERENCES "foreign_example3" ("image_id")                                                  ON DELETE SET NULL,ADD CONSTRAINT date_check CHECK ("insert_date" >= timestamptz '||quote_literal(_start_date)||' and "insert_date" <= timestamptz '||quote_literal(_end_date)||');

                CREATE TRIGGER "'||quote_ident(_tablename)||'_create_alarm" AFTER INSERT ON "'||quote_ident(_tablename)||'"
                FOR EACH ROW
                EXECUTE PROCEDURE "public"."external_trigger_example"();
            ';
        END IF;
        --
        -- Insert the current record into the correct partition,which we are sure will Now exist.
        EXECUTE 'INSERT INTO public.' || quote_ident(_tablename) || ' VALUES ($1.*)' USING NEW;

        RETURN NULL;
        --RETURN NEW;
    END;
$BODY$
  LANGUAGE plpg@R_502_6308@ VOLATILE
  COST 100

此触发器实际上按预期方式工作,但这部分有问题:

RETURN NULL;
--RETURN NEW;

无论我在这里返回的内容是什么,都将写入表-以及包含的INSERT查询中的书面内容
因此,如果我会RETURN NEW-我会写重复的话

只要我保留RETURN NULL,这就没问题-但是这样做的时候,我无法进行任何INSERT RETURNING查询(当然不会返回任何内容

所以我的问题是: 如何返回刚插入的ID-不创建重复项?
还是我该如何编写此触发器以返回某些内容,而不是实际插入(仅使用触发器中包含的INSERT)?

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)