我有这张桌子
CREATE TABLE "UserCouponSentMail"
(
"IdUser" integer NOT NULL,
"IdCoupon" integer NOT NULL,
"SendType" character varying(100),
"Date" timestamp without time zone NOT NULL DEFAULT ('now'::text)::timestamp without time zone,
CONSTRAINT "pk_UserCouponSentMail" PRIMARY KEY ("IdUser" , "IdCoupon" ),
CONSTRAINT "pk_UserCouponSentMail_GroceryCoupon" FOREIGN KEY ("IdCoupon")
REFERENCES "GroceryCoupon" ("IdGroceryCoupon") MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
和这个功能
CREATE OR REPLACE FUNCTION "UserCouponSentMailInsertOrUpdate"(integer, integer, character varying, timestamp without time zone)
RETURNS void AS
$BODY$
BEGIN
IF (EXISTS(SELECT * FROM "UserCouponSentMail" WHERE "IdUser" = $1 AND "IdCoupon" = $2)) THEN
UPDATE "UserCouponSentMail" SET
"SendType" = $3,
"Date" = $4
WHERE
"IdUser" = $1 AND "IdCoupon" = $2;
ELSE
INSERT INTO "UserCouponSentMail" VALUES ($1, $2, $3, $4);
END IF;
RETURN;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
而且我不知道为什么,但不知何故,我有时会在运行该功能时收到此错误
唯一违规:7 错误:重复键值违反唯一约束“pk_UserCouponSentMail”上下文:SQL 语句“INSERT INTO“UserCouponSentMail”值($1、$2、$3、$4)”PL/pgSQL 函数“UserCouponSentMailInsertOrUpdate”SQL 语句第 9 行
关于这怎么可能发生的任何想法?
两个脚本同时使用相同参数运行此函数的可能性几乎是不可能的。
我在 x86_64-redhat-linux-gnu 上使用 PostgreSQL 8.4.11,由 GCC gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3) 编译,64 位。
谢谢。