我有表调用它scdr_buz并且已经按月对其进行了分区,我已经在insert上创建了触发器来处理upsert并创建表,如果不存在然后upsert。我有序列i_buz_scdr序列有1个增量,但它的行为是在随机添加行而不是增量为1.这里是我的触发器的代码:
CREATE OR REPLACE FUNCTION tbl_scdr_buz_insert_trigger() RETURNS TRIGGER AS $$
BEGIN
EXECUTE 'UPDATE scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' sc SET c_total_calls = sc.c_total_calls + ($1).c_total_calls WHERE (sc.c_prefix_id = ($1).c_prefix_id AND sc.v_prefix_id = ($1).v_prefix_id AND sc.start_time = ($1).start_time)'
USING NEW;
EXECUTE 'INSERT INTO scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||'(customer_name, ...) Select ($1).*
WHERE NOT EXISTS (SELECT * FROM scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' WHERE (...))'
USING NEW;
RETURN NULL;
EXCEPTION
WHEN undefined_table THEN
EXECUTE 'CREATE TABLE IF NOT EXISTS scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' (CHECK ( start_time >= '''|| to_char(NEW.start_time, 'YYYY-MM-01 00:00') ||''' AND start_time < '''|| to_char(NEW.start_time + INTERVAL '1 month', 'YYYY-MM-01 00:00') ||''' )) INHERITS (scdr_buz)';
EXECUTE 'CREATE INDEX i_buz_scdr_'|| to_char(NEW.start_time, 'YYYY_MM') ||' ON scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' (switch_name, customer_name, client_name_id, vendor_name_id, vendor_connection, c_prefix_id, v_prefix_id, start_time, c_billing_prefix, v_billing_prefix)';
EXECUTE 'CREATE INDEX i_buz_scdr_starttime_'|| to_char(NEW.start_time, 'YYYY_MM') ||' ON scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' (start_time)';
EXECUTE 'UPDATE scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' sc SET c_total_calls = sc.c_total_calls + ($1).c_total_calls WHERE (sc.c_prefix_id = ($1).c_prefix_id AND sc.v_prefix_id = ($1).v_prefix_id AND sc.start_time = ($1).start_time)'
USING NEW;
EXECUTE 'INSERT INTO scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||'(customer_name, ...) Select ($1).*
WHERE NOT EXISTS (SELECT * FROM scdr_buz_'|| to_char(NEW.start_time, 'YYYY_MM') ||' WHERE (...))'
USING NEW;
RETURN NULL;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER fk_checkTrigger_buz_scdr
BEFORE INSERT ON scdr_buz
FOR EACH ROW
EXECUTE PROCEDURE tbl_scdr_buz_insert_trigger();
答案 0 :(得分:1)
它可能会增加1,但请记住,任何分区的每个插入都会增加1。另请注意,回滚不会回滚序列。