PostgreSQL:更新主 table 上的记录并移动子分区中的记录
PostgreSQL: update record on master table and move record in child partitions
如何定义一个更新触发器函数来更新主控 table 中的记录,其中定义了分区。
我有一个 table,它上面定义了分区和插入触发器函数的代码,只要在主 table 中有插入,它就会在子 table 中插入数据。我的代码如下:
--Master table
CREATE TABLE "SMS_RECEIPT"
(
"ID" integer NOT NULL,
"ACCOUNT_INFO" character varying(255),
"CHARGE" character varying(255),
"DELIVERY_INFO" character varying(255),
"DESTINATION" character varying(255),
"MESSAGE_ID" character varying(255),
"RECEIPT_TYPE" integer,
"SMS_CENTRE" character varying(255),
"SMS_ID" character varying(255),
"SOURCE" character varying(255),
"STATUS" character varying(255),
timedate date,
"FLAG" character varying(255),
"STID" character varying(255),
CONSTRAINT "SMS_RECEIPT_pkey" PRIMARY KEY ("ID")
);
--child partition tables
CREATE TABLE SMS_RECEIPT_y2015m01 (
CHECK ( timedate >= '2015-01-01' AND timedate < '2015-01-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m02 (
CHECK ( timedate >= '2015-02-01' AND timedate < '2015-02-28' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m03 (
CHECK ( timedate >= '2015-03-01' AND timedate < '2015-03-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m04 (
CHECK ( timedate >= '2015-04-01' AND timedate < '2015-04-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m05 (
CHECK ( timedate >= '2015-05-01' AND timedate < '2015-05-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m06 (
CHECK ( timedate >= '2015-06-01' AND timedate < '2015-06-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m07 (
CHECK ( timedate >= '2015-07-01' AND timedate < '2015-07-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m08 (
CHECK ( timedate >= '2015-08-01' AND timedate < '2015-08-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m09 (
CHECK ( timedate >= '2015-09-01' AND timedate < '2015-09-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m010 (
CHECK ( timedate >= '2015-10-01' AND timedate < '2015-10-31' )
) INHERITS ("SMS_RECEIPT");
ALTER TABLE SMS_RECEIPT_y2015m01 ADD CONSTRAINT SMS_RECEIPT_y2015m01_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m02 ADD CONSTRAINT SMS_RECEIPT_y2015m02_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m03 ADD CONSTRAINT SMS_RECEIPT_y2015m03_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m04 ADD CONSTRAINT SMS_RECEIPT_y2015m04_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m05 ADD CONSTRAINT SMS_RECEIPT_y2015m05_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m06 ADD CONSTRAINT SMS_RECEIPT_y2015m06_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m07 ADD CONSTRAINT SMS_RECEIPT_y2015m07_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m08 ADD CONSTRAINT SMS_RECEIPT_y2015m08_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m09 ADD CONSTRAINT SMS_RECEIPT_y2015m09_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m010 ADD CONSTRAINT SMS_RECEIPT_y2015m010_pkey PRIMARY KEY ("ID");
CREATE INDEX idxSMS_RECEIPT_y2015m01_TIME_DATE ON SMS_RECEIPT_y2015m01 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m02_TIME_DATE ON SMS_RECEIPT_y2015m02 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m03_TIME_DATE ON SMS_RECEIPT_y2015m03 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m04_TIME_DATE ON SMS_RECEIPT_y2015m04 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m05_TIME_DATE ON SMS_RECEIPT_y2015m05 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m06_TIME_DATE ON SMS_RECEIPT_y2015m06 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m07_TIME_DATE ON SMS_RECEIPT_y2015m07 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m08_TIME_DATE ON SMS_RECEIPT_y2015m08 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m09_TIME_DATE ON SMS_RECEIPT_y2015m09 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m010_TIME_DATE ON SMS_RECEIPT_y2015m010 (timedate);
CREATE OR REPLACE FUNCTION SMS_RECEIPT_func_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.timedate >= '2015-01-01' AND NEW.timedate < '2015-01-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m01 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-02-01' AND NEW.timedate < '2015-02-28' ) THEN
INSERT INTO SMS_RECEIPT_y2015m02 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-03-01' AND NEW.timedate < '2015-03-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m03 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-04-01' AND NEW.timedate < '2015-04-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m04 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-05-01' AND NEW.timedate < '2015-05-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m05 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-06-01' AND NEW.timedate < '2015-06-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m06 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-07-01' AND NEW.timedate < '2015-07-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m07 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-08-01' AND NEW.timedate < '2015-08-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m08 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-09-01' AND NEW.timedate < '2015-09-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m09 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-10-01' AND NEW.timedate < '2015-10-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m010 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_SMS_RECEIPT_insert
BEFORE INSERT ON "SMS_RECEIPT"
FOR EACH ROW EXECUTE PROCEDURE SMS_RECEIPT_func_insert_trigger();
现在我必须编写更新触发器函数,只要主服务器中有更新table,记录就应该相应地跨分区移动。
当前,当我在 master table 上编写更新语句时,它会抛出错误:
ERROR: new row for relation "sms_receipt_y2015m03" violates check constraint "sms_receipt_y2015m03_timedate_check"
DETAIL: Failing row contains (2, asdf, asdf, asdf, asdf, asdf, 1234, asdfasd, asdfasdf, adsfad, adsfasd, 2015-04-22, adsf, were).
********** Error **********
ERROR: new row for relation "sms_receipt_y2015m03" violates check constraint "sms_receipt_y2015m03_timedate_check"
SQL state: 23514
Detail: Failing row contains (2, asdf, asdf, asdf, asdf, asdf, 1234, asdfasd, asdfasdf, adsfad, adsfasd, 2015-04-22, adsf, were).
所以,我写了一个更新前的触发函数如下:
Create or replace function sms_receipt_func_update_trigger()
returns trigger as $$
declare total integer;
begin
IF exists( select 1 from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m02 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m02 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m03 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m03 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m04 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m04 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m05 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m05 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m06 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m06 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m07 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m07 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m08 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m09 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m09 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m010 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m010 where "ID"=OLD."ID";
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
create trigger trigger_sms_receipt_update
before update on "SMS_RECEIPT"
for each row execute procedure
sms_receipt_func_update_trigger();
但是,我仍然无法解决这个问题。谁能告诉我如何修改更新触发器,以便完成这项工作。提前致谢。
对此进行了修改,以便使用视图和代替触发器。现在,它适用于插入方法。但是,如何处理更新的事情?
CREATE TABLE "SMS_RECEIPT"
(
"ID" integer NOT NULL,
"ACCOUNT_INFO" character varying(255),
"CHARGE" character varying(255),
"DELIVERY_INFO" character varying(255),
"DESTINATION" character varying(255),
"MESSAGE_ID" character varying(255),
"RECEIPT_TYPE" integer,
"SMS_CENTRE" character varying(255),
"SMS_ID" character varying(255),
"SOURCE" character varying(255),
"STATUS" character varying(255),
timedate date,
"FLAG" character varying(255),
"STID" character varying(255),
CONSTRAINT "SMS_RECEIPT_pkey" PRIMARY KEY ("ID")
);
create view "sms_receipt_view" as select * from "SMS_RECEIPT";
CREATE TABLE SMS_RECEIPT_y2015m01 (
CHECK ( timedate >= '2015-01-01' AND timedate < '2015-01-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m02 (
CHECK ( timedate >= '2015-02-01' AND timedate < '2015-02-28' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m03 (
CHECK ( timedate >= '2015-03-01' AND timedate < '2015-03-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m04 (
CHECK ( timedate >= '2015-04-01' AND timedate < '2015-04-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m05 (
CHECK ( timedate >= '2015-05-01' AND timedate < '2015-05-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m06 (
CHECK ( timedate >= '2015-06-01' AND timedate < '2015-06-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m07 (
CHECK ( timedate >= '2015-07-01' AND timedate < '2015-07-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m08 (
CHECK ( timedate >= '2015-08-01' AND timedate < '2015-08-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m09 (
CHECK ( timedate >= '2015-09-01' AND timedate < '2015-09-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m010 (
CHECK ( timedate >= '2015-10-01' AND timedate < '2015-10-31' )
) INHERITS ("SMS_RECEIPT");
CREATE UNIQUE INDEX SMS_RECEIPT_unique ON "SMS_RECEIPT" USING btree ("ID");
ALTER TABLE SMS_RECEIPT_y2015m01 ADD CONSTRAINT SMS_RECEIPT_y2015m01_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m02 ADD CONSTRAINT SMS_RECEIPT_y2015m02_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m03 ADD CONSTRAINT SMS_RECEIPT_y2015m03_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m04 ADD CONSTRAINT SMS_RECEIPT_y2015m04_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m05 ADD CONSTRAINT SMS_RECEIPT_y2015m05_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m06 ADD CONSTRAINT SMS_RECEIPT_y2015m06_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m07 ADD CONSTRAINT SMS_RECEIPT_y2015m07_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m08 ADD CONSTRAINT SMS_RECEIPT_y2015m08_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m09 ADD CONSTRAINT SMS_RECEIPT_y2015m09_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m010 ADD CONSTRAINT SMS_RECEIPT_y2015m010_pkey PRIMARY KEY ("ID");
CREATE INDEX idxSMS_RECEIPT_y2015m01_TIME_DATE ON SMS_RECEIPT_y2015m01 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m02_TIME_DATE ON SMS_RECEIPT_y2015m02 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m03_TIME_DATE ON SMS_RECEIPT_y2015m03 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m04_TIME_DATE ON SMS_RECEIPT_y2015m04 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m05_TIME_DATE ON SMS_RECEIPT_y2015m05 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m06_TIME_DATE ON SMS_RECEIPT_y2015m06 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m07_TIME_DATE ON SMS_RECEIPT_y2015m07 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m08_TIME_DATE ON SMS_RECEIPT_y2015m08 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m09_TIME_DATE ON SMS_RECEIPT_y2015m09 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m010_TIME_DATE ON SMS_RECEIPT_y2015m010 (timedate);
create or replace function sms_receipt_func_update_trigger()
returns trigger as $$
begin
update "sms_receipt_view" set timedate = new.timedate where "ID"=new."ID";
return new;
end;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION SMS_RECEIPT_func_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.timedate >= '2015-01-01' AND NEW.timedate < '2015-01-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m01 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-02-01' AND NEW.timedate < '2015-02-28' ) THEN
INSERT INTO SMS_RECEIPT_y2015m02 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-03-01' AND NEW.timedate < '2015-03-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m03 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-04-01' AND NEW.timedate < '2015-04-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m04 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-05-01' AND NEW.timedate < '2015-05-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m05 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-06-01' AND NEW.timedate < '2015-06-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m06 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-07-01' AND NEW.timedate < '2015-07-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m07 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-08-01' AND NEW.timedate < '2015-08-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m08 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-09-01' AND NEW.timedate < '2015-09-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m09 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-10-01' AND NEW.timedate < '2015-10-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m010 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_SMS_RECEIPT_insert
Instead of INSERT OR UPDATE ON "sms_receipt_view"
FOR EACH ROW EXECUTE PROCEDURE SMS_RECEIPT_func_insert_trigger();
create trigger trigger_sms_receipt_update
instead of update on "sms_receipt_view"
for each row execute procedure sms_receipt_func_update_trigger();
问题是您需要使用触发器 return 旧记录或新记录。
您需要一个 而不是 触发器。但它仅适用于视图。所以你需要:
- 创建视图(master上就可以了table)
- 在视图上创建触发器 "instead of"
- 仅在视图上更新和插入
在此处查看 table:http://www.postgresql.org/docs/9.4/static/sql-createtrigger.html
我测试了代码,发现几乎没有什么可以修复的。开始了。
首先我插入一条记录进行测试:
INSERT INTO sms_receipt_view(
"ID", "ACCOUNT_INFO", "CHARGE", "DELIVERY_INFO", "DESTINATION",
"MESSAGE_ID", "RECEIPT_TYPE", "SMS_CENTRE", "SMS_ID", "SOURCE",
"STATUS", timedate, "FLAG", "STID")
VALUES (1, 'acc_info', 'charge', 'delivery info', 'my dest',
'ssss id mess', 4, 'vodafone center', 'dff33', '3333 3 33333',
'ok', '20150601 15:25', 'myflag', 'stid');
其次,插入触发器必须在插入时触发,所以我 运行 代码:
DROP TRIGGER trigger_sms_receipt_insert ON sms_receipt_view;
CREATE TRIGGER trigger_sms_receipt_insert
INSTEAD OF INSERT
ON sms_receipt_view
FOR EACH ROW
EXECUTE PROCEDURE sms_receipt_func_insert_trigger();
然后我重写了更新触发器:
CREATE OR REPLACE FUNCTION sms_receipt_func_update_trigger()
RETURNS trigger AS
$BODY$
begin
--update "sms_receipt_view" set timedate = new.timedate where "ID"=new."ID";
--check if I need to move record
IF date_part('year', OLD.timedate) <> date_part('year', NEW.timedate) OR date_part('month', OLD.timedate) <> date_part('month', NEW.timedate) THEN
DELETE FROM "SMS_RECEIPT" WHERE "ID" = NEW."ID";
INSERT INTO sms_receipt_view ("ID", "ACCOUNT_INFO", "CHARGE", "DELIVERY_INFO", "DESTINATION", "MESSAGE_ID", "RECEIPT_TYPE",
"SMS_CENTRE", "SMS_ID", "SOURCE", "STATUS", timedate, "FLAG", "STID")
VALUES (NEW."ID", NEW."ACCOUNT_INFO", NEW."CHARGE", NEW."DELIVERY_INFO", NEW."DESTINATION", NEW."MESSAGE_ID", NEW."RECEIPT_TYPE",
NEW."SMS_CENTRE", NEW."SMS_ID", NEW."SOURCE", NEW."STATUS", NEW.timedate, NEW."FLAG", NEW."STID"
);
ELSE
UPDATE "SMS_RECEIPT"
SET "ACCOUNT_INFO" = NEW."ACCOUNT_INFO",
"CHARGE" = NEW."CHARGE",
"DELIVERY_INFO" = NEW."DELIVERY_INFO",
"DESTINATION" = NEW."DESTINATION",
"MESSAGE_ID" = NEW."MESSAGE_ID",
"RECEIPT_TYPE" = NEW."RECEIPT_TYPE",
"SMS_CENTRE" = NEW."SMS_CENTRE",
"SMS_ID" = NEW."SMS_ID",
"SOURCE" = NEW."SOURCE",
"STATUS" = NEW."STATUS",
timedate = NEW.timedate,
"FLAG" = NEW."FLAG",
"STID" = NEW."STID"
WHERE "ID" = NEW."ID";
END IF;
return NULL;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sms_receipt_func_update_trigger()
OWNER TO postgres;
首先我检查我们是否真的需要移动记录。如果没有,我只是通过 master table 更新。如果我需要移动我删除旧记录并在视图中插入新记录(因此我触发插入视图)。
警告:在我的模拟中,我认为密钥永远不会改变。如果您需要更新 id,则需要修改一些代码。
然后我测试了2个案例。在我的场景中,id 是:1
UPDATE sms_receipt_view
SET "SMS_CENTRE" = 'tim center'
--timedate = '20150501'
WHERE "ID" = 1;
UPDATE sms_receipt_view
SET "SMS_CENTRE" = 'tim center',
timedate = '20150501'
WHERE "ID" = 1;
进行一些测试,但应该可以。
如何定义一个更新触发器函数来更新主控 table 中的记录,其中定义了分区。 我有一个 table,它上面定义了分区和插入触发器函数的代码,只要在主 table 中有插入,它就会在子 table 中插入数据。我的代码如下:
--Master table
CREATE TABLE "SMS_RECEIPT"
(
"ID" integer NOT NULL,
"ACCOUNT_INFO" character varying(255),
"CHARGE" character varying(255),
"DELIVERY_INFO" character varying(255),
"DESTINATION" character varying(255),
"MESSAGE_ID" character varying(255),
"RECEIPT_TYPE" integer,
"SMS_CENTRE" character varying(255),
"SMS_ID" character varying(255),
"SOURCE" character varying(255),
"STATUS" character varying(255),
timedate date,
"FLAG" character varying(255),
"STID" character varying(255),
CONSTRAINT "SMS_RECEIPT_pkey" PRIMARY KEY ("ID")
);
--child partition tables
CREATE TABLE SMS_RECEIPT_y2015m01 (
CHECK ( timedate >= '2015-01-01' AND timedate < '2015-01-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m02 (
CHECK ( timedate >= '2015-02-01' AND timedate < '2015-02-28' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m03 (
CHECK ( timedate >= '2015-03-01' AND timedate < '2015-03-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m04 (
CHECK ( timedate >= '2015-04-01' AND timedate < '2015-04-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m05 (
CHECK ( timedate >= '2015-05-01' AND timedate < '2015-05-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m06 (
CHECK ( timedate >= '2015-06-01' AND timedate < '2015-06-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m07 (
CHECK ( timedate >= '2015-07-01' AND timedate < '2015-07-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m08 (
CHECK ( timedate >= '2015-08-01' AND timedate < '2015-08-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m09 (
CHECK ( timedate >= '2015-09-01' AND timedate < '2015-09-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m010 (
CHECK ( timedate >= '2015-10-01' AND timedate < '2015-10-31' )
) INHERITS ("SMS_RECEIPT");
ALTER TABLE SMS_RECEIPT_y2015m01 ADD CONSTRAINT SMS_RECEIPT_y2015m01_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m02 ADD CONSTRAINT SMS_RECEIPT_y2015m02_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m03 ADD CONSTRAINT SMS_RECEIPT_y2015m03_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m04 ADD CONSTRAINT SMS_RECEIPT_y2015m04_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m05 ADD CONSTRAINT SMS_RECEIPT_y2015m05_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m06 ADD CONSTRAINT SMS_RECEIPT_y2015m06_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m07 ADD CONSTRAINT SMS_RECEIPT_y2015m07_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m08 ADD CONSTRAINT SMS_RECEIPT_y2015m08_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m09 ADD CONSTRAINT SMS_RECEIPT_y2015m09_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m010 ADD CONSTRAINT SMS_RECEIPT_y2015m010_pkey PRIMARY KEY ("ID");
CREATE INDEX idxSMS_RECEIPT_y2015m01_TIME_DATE ON SMS_RECEIPT_y2015m01 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m02_TIME_DATE ON SMS_RECEIPT_y2015m02 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m03_TIME_DATE ON SMS_RECEIPT_y2015m03 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m04_TIME_DATE ON SMS_RECEIPT_y2015m04 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m05_TIME_DATE ON SMS_RECEIPT_y2015m05 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m06_TIME_DATE ON SMS_RECEIPT_y2015m06 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m07_TIME_DATE ON SMS_RECEIPT_y2015m07 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m08_TIME_DATE ON SMS_RECEIPT_y2015m08 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m09_TIME_DATE ON SMS_RECEIPT_y2015m09 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m010_TIME_DATE ON SMS_RECEIPT_y2015m010 (timedate);
CREATE OR REPLACE FUNCTION SMS_RECEIPT_func_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.timedate >= '2015-01-01' AND NEW.timedate < '2015-01-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m01 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-02-01' AND NEW.timedate < '2015-02-28' ) THEN
INSERT INTO SMS_RECEIPT_y2015m02 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-03-01' AND NEW.timedate < '2015-03-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m03 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-04-01' AND NEW.timedate < '2015-04-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m04 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-05-01' AND NEW.timedate < '2015-05-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m05 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-06-01' AND NEW.timedate < '2015-06-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m06 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-07-01' AND NEW.timedate < '2015-07-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m07 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-08-01' AND NEW.timedate < '2015-08-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m08 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-09-01' AND NEW.timedate < '2015-09-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m09 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-10-01' AND NEW.timedate < '2015-10-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m010 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_SMS_RECEIPT_insert
BEFORE INSERT ON "SMS_RECEIPT"
FOR EACH ROW EXECUTE PROCEDURE SMS_RECEIPT_func_insert_trigger();
现在我必须编写更新触发器函数,只要主服务器中有更新table,记录就应该相应地跨分区移动。
当前,当我在 master table 上编写更新语句时,它会抛出错误:
ERROR: new row for relation "sms_receipt_y2015m03" violates check constraint "sms_receipt_y2015m03_timedate_check"
DETAIL: Failing row contains (2, asdf, asdf, asdf, asdf, asdf, 1234, asdfasd, asdfasdf, adsfad, adsfasd, 2015-04-22, adsf, were).
********** Error **********
ERROR: new row for relation "sms_receipt_y2015m03" violates check constraint "sms_receipt_y2015m03_timedate_check"
SQL state: 23514
Detail: Failing row contains (2, asdf, asdf, asdf, asdf, asdf, 1234, asdfasd, asdfasdf, adsfad, adsfasd, 2015-04-22, adsf, were).
所以,我写了一个更新前的触发函数如下:
Create or replace function sms_receipt_func_update_trigger()
returns trigger as $$
declare total integer;
begin
IF exists( select 1 from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m02 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m02 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m03 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m03 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m04 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m04 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m05 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m05 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m06 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m06 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m07 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m07 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m08 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m01 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m09 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m09 where "ID"=OLD."ID";
ELSIF exists( select 1 from SMS_RECEIPT_y2015m010 where "ID"=OLD."ID" and timedate = OLD.timedate ) THEN
delete from SMS_RECEIPT_y2015m010 where "ID"=OLD."ID";
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
create trigger trigger_sms_receipt_update
before update on "SMS_RECEIPT"
for each row execute procedure
sms_receipt_func_update_trigger();
但是,我仍然无法解决这个问题。谁能告诉我如何修改更新触发器,以便完成这项工作。提前致谢。
对此进行了修改,以便使用视图和代替触发器。现在,它适用于插入方法。但是,如何处理更新的事情?
CREATE TABLE "SMS_RECEIPT"
(
"ID" integer NOT NULL,
"ACCOUNT_INFO" character varying(255),
"CHARGE" character varying(255),
"DELIVERY_INFO" character varying(255),
"DESTINATION" character varying(255),
"MESSAGE_ID" character varying(255),
"RECEIPT_TYPE" integer,
"SMS_CENTRE" character varying(255),
"SMS_ID" character varying(255),
"SOURCE" character varying(255),
"STATUS" character varying(255),
timedate date,
"FLAG" character varying(255),
"STID" character varying(255),
CONSTRAINT "SMS_RECEIPT_pkey" PRIMARY KEY ("ID")
);
create view "sms_receipt_view" as select * from "SMS_RECEIPT";
CREATE TABLE SMS_RECEIPT_y2015m01 (
CHECK ( timedate >= '2015-01-01' AND timedate < '2015-01-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m02 (
CHECK ( timedate >= '2015-02-01' AND timedate < '2015-02-28' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m03 (
CHECK ( timedate >= '2015-03-01' AND timedate < '2015-03-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m04 (
CHECK ( timedate >= '2015-04-01' AND timedate < '2015-04-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m05 (
CHECK ( timedate >= '2015-05-01' AND timedate < '2015-05-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m06 (
CHECK ( timedate >= '2015-06-01' AND timedate < '2015-06-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m07 (
CHECK ( timedate >= '2015-07-01' AND timedate < '2015-07-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m08 (
CHECK ( timedate >= '2015-08-01' AND timedate < '2015-08-31' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m09 (
CHECK ( timedate >= '2015-09-01' AND timedate < '2015-09-30' )
) INHERITS ("SMS_RECEIPT");
CREATE TABLE SMS_RECEIPT_y2015m010 (
CHECK ( timedate >= '2015-10-01' AND timedate < '2015-10-31' )
) INHERITS ("SMS_RECEIPT");
CREATE UNIQUE INDEX SMS_RECEIPT_unique ON "SMS_RECEIPT" USING btree ("ID");
ALTER TABLE SMS_RECEIPT_y2015m01 ADD CONSTRAINT SMS_RECEIPT_y2015m01_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m02 ADD CONSTRAINT SMS_RECEIPT_y2015m02_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m03 ADD CONSTRAINT SMS_RECEIPT_y2015m03_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m04 ADD CONSTRAINT SMS_RECEIPT_y2015m04_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m05 ADD CONSTRAINT SMS_RECEIPT_y2015m05_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m06 ADD CONSTRAINT SMS_RECEIPT_y2015m06_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m07 ADD CONSTRAINT SMS_RECEIPT_y2015m07_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m08 ADD CONSTRAINT SMS_RECEIPT_y2015m08_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m09 ADD CONSTRAINT SMS_RECEIPT_y2015m09_pkey PRIMARY KEY ("ID");
ALTER TABLE SMS_RECEIPT_y2015m010 ADD CONSTRAINT SMS_RECEIPT_y2015m010_pkey PRIMARY KEY ("ID");
CREATE INDEX idxSMS_RECEIPT_y2015m01_TIME_DATE ON SMS_RECEIPT_y2015m01 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m02_TIME_DATE ON SMS_RECEIPT_y2015m02 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m03_TIME_DATE ON SMS_RECEIPT_y2015m03 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m04_TIME_DATE ON SMS_RECEIPT_y2015m04 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m05_TIME_DATE ON SMS_RECEIPT_y2015m05 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m06_TIME_DATE ON SMS_RECEIPT_y2015m06 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m07_TIME_DATE ON SMS_RECEIPT_y2015m07 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m08_TIME_DATE ON SMS_RECEIPT_y2015m08 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m09_TIME_DATE ON SMS_RECEIPT_y2015m09 (timedate);
CREATE INDEX idxSMS_RECEIPT_y2015m010_TIME_DATE ON SMS_RECEIPT_y2015m010 (timedate);
create or replace function sms_receipt_func_update_trigger()
returns trigger as $$
begin
update "sms_receipt_view" set timedate = new.timedate where "ID"=new."ID";
return new;
end;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION SMS_RECEIPT_func_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.timedate >= '2015-01-01' AND NEW.timedate < '2015-01-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m01 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-02-01' AND NEW.timedate < '2015-02-28' ) THEN
INSERT INTO SMS_RECEIPT_y2015m02 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-03-01' AND NEW.timedate < '2015-03-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m03 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-04-01' AND NEW.timedate < '2015-04-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m04 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-05-01' AND NEW.timedate < '2015-05-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m05 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-06-01' AND NEW.timedate < '2015-06-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m06 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-07-01' AND NEW.timedate < '2015-07-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m07 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-08-01' AND NEW.timedate < '2015-08-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m08 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-09-01' AND NEW.timedate < '2015-09-30' ) THEN
INSERT INTO SMS_RECEIPT_y2015m09 VALUES (NEW.*);
ELSIF ( NEW.timedate >= '2015-10-01' AND NEW.timedate < '2015-10-31' ) THEN
INSERT INTO SMS_RECEIPT_y2015m010 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_SMS_RECEIPT_insert
Instead of INSERT OR UPDATE ON "sms_receipt_view"
FOR EACH ROW EXECUTE PROCEDURE SMS_RECEIPT_func_insert_trigger();
create trigger trigger_sms_receipt_update
instead of update on "sms_receipt_view"
for each row execute procedure sms_receipt_func_update_trigger();
问题是您需要使用触发器 return 旧记录或新记录。
您需要一个 而不是 触发器。但它仅适用于视图。所以你需要:
- 创建视图(master上就可以了table)
- 在视图上创建触发器 "instead of"
- 仅在视图上更新和插入
在此处查看 table:http://www.postgresql.org/docs/9.4/static/sql-createtrigger.html
我测试了代码,发现几乎没有什么可以修复的。开始了。
首先我插入一条记录进行测试:
INSERT INTO sms_receipt_view(
"ID", "ACCOUNT_INFO", "CHARGE", "DELIVERY_INFO", "DESTINATION",
"MESSAGE_ID", "RECEIPT_TYPE", "SMS_CENTRE", "SMS_ID", "SOURCE",
"STATUS", timedate, "FLAG", "STID")
VALUES (1, 'acc_info', 'charge', 'delivery info', 'my dest',
'ssss id mess', 4, 'vodafone center', 'dff33', '3333 3 33333',
'ok', '20150601 15:25', 'myflag', 'stid');
其次,插入触发器必须在插入时触发,所以我 运行 代码:
DROP TRIGGER trigger_sms_receipt_insert ON sms_receipt_view;
CREATE TRIGGER trigger_sms_receipt_insert
INSTEAD OF INSERT
ON sms_receipt_view
FOR EACH ROW
EXECUTE PROCEDURE sms_receipt_func_insert_trigger();
然后我重写了更新触发器:
CREATE OR REPLACE FUNCTION sms_receipt_func_update_trigger()
RETURNS trigger AS
$BODY$
begin
--update "sms_receipt_view" set timedate = new.timedate where "ID"=new."ID";
--check if I need to move record
IF date_part('year', OLD.timedate) <> date_part('year', NEW.timedate) OR date_part('month', OLD.timedate) <> date_part('month', NEW.timedate) THEN
DELETE FROM "SMS_RECEIPT" WHERE "ID" = NEW."ID";
INSERT INTO sms_receipt_view ("ID", "ACCOUNT_INFO", "CHARGE", "DELIVERY_INFO", "DESTINATION", "MESSAGE_ID", "RECEIPT_TYPE",
"SMS_CENTRE", "SMS_ID", "SOURCE", "STATUS", timedate, "FLAG", "STID")
VALUES (NEW."ID", NEW."ACCOUNT_INFO", NEW."CHARGE", NEW."DELIVERY_INFO", NEW."DESTINATION", NEW."MESSAGE_ID", NEW."RECEIPT_TYPE",
NEW."SMS_CENTRE", NEW."SMS_ID", NEW."SOURCE", NEW."STATUS", NEW.timedate, NEW."FLAG", NEW."STID"
);
ELSE
UPDATE "SMS_RECEIPT"
SET "ACCOUNT_INFO" = NEW."ACCOUNT_INFO",
"CHARGE" = NEW."CHARGE",
"DELIVERY_INFO" = NEW."DELIVERY_INFO",
"DESTINATION" = NEW."DESTINATION",
"MESSAGE_ID" = NEW."MESSAGE_ID",
"RECEIPT_TYPE" = NEW."RECEIPT_TYPE",
"SMS_CENTRE" = NEW."SMS_CENTRE",
"SMS_ID" = NEW."SMS_ID",
"SOURCE" = NEW."SOURCE",
"STATUS" = NEW."STATUS",
timedate = NEW.timedate,
"FLAG" = NEW."FLAG",
"STID" = NEW."STID"
WHERE "ID" = NEW."ID";
END IF;
return NULL;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sms_receipt_func_update_trigger()
OWNER TO postgres;
首先我检查我们是否真的需要移动记录。如果没有,我只是通过 master table 更新。如果我需要移动我删除旧记录并在视图中插入新记录(因此我触发插入视图)。
警告:在我的模拟中,我认为密钥永远不会改变。如果您需要更新 id,则需要修改一些代码。
然后我测试了2个案例。在我的场景中,id 是:1
UPDATE sms_receipt_view
SET "SMS_CENTRE" = 'tim center'
--timedate = '20150501'
WHERE "ID" = 1;
UPDATE sms_receipt_view
SET "SMS_CENTRE" = 'tim center',
timedate = '20150501'
WHERE "ID" = 1;
进行一些测试,但应该可以。