我正在使用slony复制数据库并尝试创建一个触发器,该触发器将在对表进行INSERT
操作后触发。
在此触发器中,我尝试使用dblink更新另一个SAME数据库表。
但是我收到一个错误,说当我尝试更新第二个表时,我刚插入第一个表中的值不存在。
我正在使用dblink,因为如果我使用常规UPDATE语句更新第二个表,则不会触发slony同步。
第一张表:
CREATE TABLE "COFFRETS"
(
"NUM_SERIE" character varying NOT NULL,
"DATE_CREATION" timestamp without time zone NOT NULL DEFAULT now(),
"DATE_DERNIERE_MODIF" timestamp without time zone NOT NULL DEFAULT now(),
"NOMENCLATURE" integer NOT NULL,
"COMMANDES_DETAILS_ID" integer,
"DEBLOCAGES_ID" integer,
"ETAT" integer NOT NULL DEFAULT 1,
"EXPEDITIONS_ID" bigint,
"STOCKISTE_EXPE_ID" integer,
"STOCKISTE_RCPT_ID" integer,
"SITE_ID" integer,
"FACTURES_ID" integer,
"CMDDETECH_ID" integer,
"FACTURE_AVE_ID" integer,
"SHIPPING_ID" integer,
"SYNCHRONISE" boolean DEFAULT false,
CONSTRAINT "COFFRETS_pkey" PRIMARY KEY ("NUM_SERIE"),
CONSTRAINT "FK_SHIPPING" FOREIGN KEY ("SHIPPING_ID")
REFERENCES "SHIPPING" ("ID") MATCH SIMPLE
);
第二张表:
CREATE TABLE "SHIPPING"
(
"DATE_AJOUT" timestamp without time zone NOT NULL,
"DATE_DERNIERE_MODIF" timestamp without time zone NOT NULL,
"ORDRE_PRODUCTION" text,
"AIRE_APPRO" text,
"DATE_ENVOI" timestamp without time zone,
"DATE_LIVRAISON" timestamp without time zone,
"REF_CARRIER" text,
"QTE" numeric,
"NUM_CONTRAT" text,
"COMMENTAIRES" text,
"ID" serial NOT NULL,
"POSTE_TRAVAIL" text,
"POSTE_CONTRAT" integer,
CONSTRAINT "Pkey_ID_SHIPPING" PRIMARY KEY ("ID")
);
触发:
CREATE TRIGGER test
AFTER INSERT
ON "SHIPPING"
FOR EACH ROW
EXECUTE PROCEDURE "AffectationShipping"();
触发功能:
CREATE OR REPLACE FUNCTION "AffectationShipping"()
RETURNS trigger AS
$BODY$DECLARE
coffretNumSerie text;
message text;
num_site integer;
txt text;
BEGIN
RAISE NOTICE '-----------------------------------------------------------------------------';
RAISE NOTICE '- AffectationShipping -';
RAISE NOTICE '-----------------------------------------------------------------------------';
--lecture du numéro de site
num_site=0;
SELECT "Value" INTO num_site FROM "APPLICATION_PARAMETERS" WHERE "Name" = 'SITE_ID';
--Récupération du coffret concerné. un seul coffret car on a un shipping par coffret chez aquasnap
SELECT "COFFRETS"."NUM_SERIE" INTO coffretNumSerie
FROM "COFFRETS" INNER JOIN "DEBLOCAGES" ON
"COFFRETS"."DEBLOCAGES_ID" = "DEBLOCAGES"."ID"
WHERE "COFFRETS"."SHIPPING_ID" IS NULL AND
"DEBLOCAGES"."NumOrdreProduction" = NEW."ORDRE_PRODUCTION"
LIMIT 1;
IF coffretNumSerie != '' THEN
RAISE NOTICE 'ID = %', NEW."ID";
RAISE NOTICE 'param = %', (SELECT parametre_get('Chaine_connexion_bdd_Atelier')::text);
RAISE NOTICE 'Statement = %', ('UPDATE "COFFRETS" SET "SHIPPING_ID" = ' || NEW."ID" || ' WHERE "NUM_SERIE" = ''' ||coffretNumSerie ||''';');
PERFORM dblink_exec((SELECT parametre_get('Chaine_connexion_bdd_Atelier'))::text, ('UPDATE "COFFRETS" SET "SHIPPING_ID" = ' || NEW."ID" || ' WHERE "NUM_SERIE" = ''' ||coffretNumSerie ||''';'));
RAISE NOTICE 'Affectation du shipping Num.[%], ordre de production Num.[%] au coffret Num.[%].', NEW."ID" ,NEW."ORDRE_PRODUCTION",coffretNumSerie;
--Log d'un message d'information
message = 'Affectation du shipping Num.['|| NEW."ID" ||'], ordre de production Num.['|| NEW."ORDRE_PRODUCTION" ||'] au coffret Num.['|| coffretNumSerie ||'].';
--enregistrement de l'information
INSERT INTO "ERRORS_LOG" ("DATE","MESSAGE","ERROR_TYPES","LOCALIZATION", "TYPE_MESSAGE_ID", "SITE_ID" )
VALUES (now(),message,'Information' ,'Trigger associations coffrets - Shipping : AffectationShipping',4,num_site);
ELSE --LogErreur
RAISE NOTICE 'Aucun coffret correspondant au shipping Num.[%], ordre de production Num.[%].' , NEW."ID" ,NEW."ORDRE_PRODUCTION" ;
--composition du message d'erreur
message = 'Aucun coffret correspondant au shipping Num.['|| NEW."ID" ||'], ordre de production Num.[' || NEW."ORDRE_PRODUCTION" || '].';
--enregistrement de l'erreur de type "Gestion COFFRETS"
INSERT INTO "ERRORS_LOG" ("DATE","MESSAGE","ERROR_TYPES","LOCALIZATION", "TYPE_MESSAGE_ID", "SITE_ID" )
VALUES (now(),message,'Erreur' ,'Trigger associations coffrets - Shipping : AffectationShipping',3,num_site);
END IF;
RAISE NOTICE '-----------------------------------------------------------------------------';
RAISE NOTICE '-Fin AffectationShipping -';
RAISE NOTICE '-----------------------------------------------------------------------------';
RETURN NEW;
END;$BODY$
LANGUAGE plpgsql;
至于错误信息,我不知道如何用英语制作PostgreSQL打印信息。
基本上它表示发货表中不存在SHIPPING_ID
外键。
奇怪的是,当我尝试不使用dblink_exec
进行更新时,它可以正常工作。但正如我之前所说,我需要通过dblink进行此更新,以确保slony会注意到更新。
答案 0 :(得分:1)
dblink
通过单独连接访问目标数据库来运行。这有一些固有的后果:
最重要的是你: