Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1YpQ-0002iV-54 for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Oct 2017 14:14:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e1YpP-0004Wo-Hi for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Oct 2017 14:14:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e1Yoe-0000hM-P1; Mon, 09 Oct 2017 14:13:56 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1YoT-0000vT-Kd; Mon, 09 Oct 2017 14:13:55 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 7426821C1CC9; Mon, 9 Oct 2017 17:13:44 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from localhost (unknown [46.101.213.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id E4B8E21C174D; Mon, 9 Oct 2017 17:13:43 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1507558424; bh=U86k55OQLwXcyU8UwR3H2ACXeKxWbDq7kXlQKFtCmns=; h=Date:From:To:Subject; b=bhEEQSHn2Qr0lBL/0ullUrqYWLaoZJx1BHTE7miraCAl1t6Wztfd7xHHF0wfgXfhh 2KjeJsCir4q64N1BRBDKt76B87jHxBlw9Nit/RDO1BlbVXSmQug1Ki84mfFK2pEH1o +iM/9w6pWFUJmQ/uGs7RLTPkxaF6LKROGSynGFfk= Date: Mon, 9 Oct 2017 17:13:41 +0300 From: Aleksander Alekseev To: PostgreSQL Hackers , pgsql-bugs@postgresql.org Subject: 10.0: Logical replication doesn't execute BEFORE UPDATE OF trigger Message-ID: <20171009141341.GA16999@e733.localdomain> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="5vNYLRcllDrimb99" Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --5vNYLRcllDrimb99 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi hackers, I've found something that looks like a bug. Steps to reproduce ------------------ There are 3 instances of PostgreSQL 10.0 - inst1, inst2 and inst3. There is a table `test` on every instance: ``` CREATE TABLE test(k TEXT PRIMARY KEY, v TEXT); ``` Both inst1 and inst2 have `allpub` publication: ``` CREATE PUBLICATION allpub FOR ALL TABLES; ``` =2E.. and inst3 is subscribed for both publications: ``` CREATE SUBSCRIPTION allsub1 CONNECTION 'host=3D10.128.0.16 user=3Deax dbname=3Deax' PUBLICATION allpub; CREATE SUBSCRIPTION allsub2 CONNECTION 'host=3D10.128.0.26 user=3Deax dbname=3Deax' PUBLICATION allpub; ``` So basically it's two masters, one replica configuration. To resolve insert/update conflicts I've created the following triggers on inst3: ``` CREATE OR REPLACE FUNCTION test_before_insert() RETURNS trigger AS $$ BEGIN RAISE NOTICE 'test_before_insert trigger executed'; IF EXISTS (SELECT 1 FROM test where k =3D new.k) THEN RAISE NOTICE 'test_before_insert trigger - merging data'; UPDATE test SET v =3D v || ';' || new.v WHERE k =3D new.k; RETURN NULL; END IF; RETURN NEW; END $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION test_before_update() RETURNS trigger AS $$ BEGIN RAISE NOTICE 'test_before_update trigger executed'; IF EXISTS (SELECT 1 FROM test where k =3D new.k) THEN RAISE NOTICE 'test_before_update trigger - merging data'; UPDATE test SET v =3D v || ';' || new.v WHERE k =3D new.k; DELETE FROM test where k =3D old.k; RETURN NULL; END IF; RETURN NEW; END $$ LANGUAGE plpgsql; create trigger test_before_insert_trigger before insert on test for each row execute procedure test_before_insert(); create trigger test_before_update_trigger before update of k on test for each row execute procedure test_before_update(); ALTER TABLE test ENABLE REPLICA TRIGGER test_before_insert_trigger; ALTER TABLE test ENABLE REPLICA TRIGGER test_before_update_trigger; ``` The INSERT trigger works just as expected, however the UPDATE trigger doesn't. On inst1: ``` insert into test values ('k1', 'v1'); ``` In inst2: ``` insert into test values ('k4', 'v4'); update test set k =3D 'k1' where k =3D 'k4'; ``` Now on inst3: ``` select * from test; ``` Expected result --------------- Rows are merged to: ``` k | v =20 ----+------- k1 | v1;v4 ``` This is what would happen if all insert/update queries would have been executed on one instance. Actual result ------------- Replication fails, log contains: ``` [3227] ERROR: duplicate key value violates unique constraint "test_pkey" [3227] DETAIL: Key (k)=3D(k1) already exists. [3176] LOG: worker process: logical replication worker for subscription 16= 402 (PID 3227) exited with exit code 1 ``` What do you think? --=20 Best regards, Aleksander Alekseev --5vNYLRcllDrimb99 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEECAokn0QZCoIHW4GpEEnCBLXCDtYFAlnbhBIACgkQEEnCBLXC DtbA0w//avwmkPQLPiaKXLpG3e+oIvwazRhbn3nRqZs47QIoiBqHQyHeZqJrvEkP zaUAoWm++8L3EogGaKckx0bEAACepjxs7hGHn3R/4rmco/Wn2GZzs7ZVxl5UY8Ex ndIAjX/yNbb4qK5nbQaC1Qv4tqiKo0Cm9+2+/NklBYTjXmAD4oLum3HIAVPfNYYR fh/9jCHF+Ka7D9S5S6ASyL+SD4QRWfu9sn52SV9Hbi8u7mjOr5GS72NImuglJyMh PaFJLhO9JevAReSytobnN8QbdAZ2aeC0wBLIpV8EWLd9jOD7vh098vAdzTrxi6zY PWf6jdPJlHWjNvfpds86GQ4yCMx9Rq4jT4uTExHNZNFpuacK0mFSugGk15wP4M1O mkXFKtvHxkjG2OrqabUDTuQCjuqCb5Ctce5ShnEyS7LKiqoJCHw9ytCTSQl383Fl oxMaUVkIsr+DOCcLywISIHmaN3+N7juSy1Taldn91BdBgCwTBwXRD6zYdeYn0Ex0 p3xqbLPd0VEHCSFg3xt5G+qyoICp3enR4y+7gG1G7ZZVn2YWfmE9YXZxVAFn77Y+ C6RmGxl0t5gXuv5jtvVRI/nsTGB9dgv7VZlLZIyH7FwqCmci+c8Zw9q844Qa/gLC sADjBf5bOodPF12vDPrpMbF3ml4qci95fwJasBQit8MO3KYEBIY= =T5XE -----END PGP SIGNATURE----- --5vNYLRcllDrimb99--