Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1rk3-0004Va-OO for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Oct 2017 10:26:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e1rk1-0003pj-KC for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Oct 2017 10:26:25 +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 1e1rk0-0003pW-PM; Tue, 10 Oct 2017 10:26:24 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1rjs-00017h-Ow; Tue, 10 Oct 2017 10:26:23 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 0071221C2049; Tue, 10 Oct 2017 13:26:13 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from localhost (unknown [192.168.27.1]) (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 C443E21C1E26; Tue, 10 Oct 2017 13:26:13 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1507631173; bh=lSDVa2d+d4InrSdZzQNv7qKF481i75ocGrdQfz4FIfg=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Y/e5UmKsw2i6lgdyS9GBKrkfBCj5NE34V/NRWK3jWNnVg4S9tVU4gDmYDwJnhvBnh WFsDR+ejaf1kMYQV1dB2JBSlhm17Fi8+1pqxDxqiNwlK7bZDC5CWcWYtefUfwxwaij gPSxU26j7V6viz892A9IwV29QJkaf8fr373r9rmg= Date: Tue, 10 Oct 2017 13:26:13 +0300 From: Aleksander Alekseev To: Masahiko Sawada Cc: PostgreSQL Hackers , pgsql-bugs Subject: Re: [BUGS] 10.0: Logical replication doesn't execute BEFORE UPDATE OF trigger Message-ID: <20171010102613.GA28161@e733.localdomain> References: <20171009141341.GA16999@e733.localdomain> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="1LKvkjL3sHcu1TtY" Content-Disposition: inline In-Reply-To: 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-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --1LKvkjL3sHcu1TtY Content-Type: multipart/mixed; boundary="gKMricLos+KVdGMg" Content-Disposition: inline --gKMricLos+KVdGMg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi Masahiko, > > I think the cause of this issue is that the apply worker doesn't set > > updatedCols of RangeTblEntry when applying updates. So TriggerEnabled > > always ends up with false. I'll make a patch and submit. > > >=20 > Attached patch store the updated columns bitmap set to RangeTblEntry. > In my environment this bug seems to be fixed by the patch. Thanks a lot for a quick response. I can confirm that your patch fixes the issue and passes all tests. Hopefully someone will merge it shortly. Here is another patch from me. It adds a corresponding TAP test. Before applying your patch: ``` t/001_rep_changes.pl .. ok t/002_types.pl ........ ok =20 t/003_constraints.pl .. 1/5 # Failed test 'check replica trigger with specified list of affected colu= mns applied on subscriber' # at t/003_constraints.pl line 151. # got: 'k2|v1' # expected: 'k2|v1 # triggered|true' # Looks like you failed 1 test of 5. t/003_constraints.pl .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/5 subtests t/004_sync.pl ......... ok t/005_encoding.pl ..... ok t/006_rewrite.pl ...... ok t/007_ddl.pl .......... ok ``` After: ``` t/001_rep_changes.pl .. ok t/002_types.pl ........ ok t/003_constraints.pl .. ok t/004_sync.pl ......... ok t/005_encoding.pl ..... ok t/006_rewrite.pl ...... ok t/007_ddl.pl .......... ok All tests successful. ``` --=20 Best regards, Aleksander Alekseev --gKMricLos+KVdGMg Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="subscription_new_tap_test.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/test/subscription/t/003_constraints.pl b/src/test/subscrip= tion/t/003_constraints.pl index 06863aef84..f1fc5ae863 100644 --- a/src/test/subscription/t/003_constraints.pl +++ b/src/test/subscription/t/003_constraints.pl @@ -3,7 +3,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests =3D> 4; +use Test::More tests =3D> 5; =20 # Initialize publisher node my $node_publisher =3D get_new_node('publisher'); @@ -21,6 +21,9 @@ $node_publisher->safe_psql('postgres', $node_publisher->safe_psql('postgres', "CREATE TABLE tab_fk_ref (id int PRIMARY KEY, bid int REFERENCES tab_fk (b= id));" ); +$node_publisher->safe_psql('postgres', +"CREATE TABLE tab_upd_tst (k TEXT PRIMARY KEY, v TEXT);" +); =20 # Setup structure on subscriber $node_subscriber->safe_psql('postgres', @@ -28,6 +31,9 @@ $node_subscriber->safe_psql('postgres', $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_fk_ref (id int PRIMARY KEY, bid int REFERENCES tab_fk (b= id));" ); +$node_subscriber->safe_psql('postgres', +"CREATE TABLE tab_upd_tst (k TEXT PRIMARY KEY, v TEXT);" +); =20 # Setup logical replication my $publisher_connstr =3D $node_publisher->connstr . ' dbname=3Dpostgres'; @@ -112,5 +118,38 @@ $result =3D $node_subscriber->safe_psql('postgres', "SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;"); is($result, qq(2|1|2), 'check replica trigger applied on subscriber'); =20 +# Add replica trigger with specified list of affected columns +$node_subscriber->safe_psql( + 'postgres', qq{ +CREATE OR REPLACE FUNCTION upd_fn() +RETURNS trigger AS \$\$ +BEGIN + INSERT INTO tab_upd_tst VALUES ('triggered', 'true'); + RETURN NEW; +END +\$\$ LANGUAGE plpgsql; + +CREATE TRIGGER upd_trg +BEFORE UPDATE OF k ON tab_upd_tst +FOR EACH ROW EXECUTE PROCEDURE upd_fn(); + +ALTER TABLE tab_upd_tst ENABLE REPLICA TRIGGER upd_trg; +}); + +# Insert data +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_upd_tst (k, v) VALUES ('k1', 'v1');"); +$node_publisher->safe_psql('postgres', + "UPDATE tab_upd_tst SET k =3D 'k2' WHERE k =3D 'k1';"); + +$node_publisher->poll_query_until('postgres', $caughtup_query) + or die "Timed out while waiting for subscriber to catch up"; + +# Trigger should be executed +$result =3D + $node_subscriber->safe_psql('postgres', "SELECT k, v FROM tab_upd_tst OR= DER BY k"); +is( $result, qq(k2|v1 +triggered|true), 'check replica trigger with specified list of affected co= lumns applied on subscriber'); + $node_subscriber->stop('fast'); $node_publisher->stop('fast'); --gKMricLos+KVdGMg-- --1LKvkjL3sHcu1TtY Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEECAokn0QZCoIHW4GpEEnCBLXCDtYFAlncoEIACgkQEEnCBLXC DtZaQBAAiZ7v7QE/sepZxmEnwbXIzCaccslc61byL0FG+H2hq0Cu/0PERH0m7LCF oT2hiLxp1ipfJ4ggMwGpYnOs/C1mL6RbFVmOcHkGEoyKaKaa2/2a+9cx0wRVMmEb /n6X3Fya+J5954uxt7Ij3FTkhgWom4gBRpmLUa7qVxP8CYiO1ih8gL/4Mcr8aRDp r8/hrr7oz3SL3zPIk/vjjGcOLZ8thvYHO7R8S0YYE7o38YmcgDRCJTPofgR/BM54 72X4JsNT3ty3njBdn+Zd9xFBBKmQyeC4Wy+u76uMjIa1AemdnObvFsj8CMij5Qa8 VbLhG+PDhpfFzxy0X+7WbjioSOhExed5yRQuoQOPkbd+iNTx10oySvDMYhsAsk4f slfhdir0O0kwdl0aCH2TI8Eo7GXxm+0Y5++eqh8m3dzigN5rzn3jwMse1TG2nNKX G6kFqcpUMnG3vvJbnlzf/jttKHk/2lV37iMvfokC0+D9wgQRzTOFVmcVJNwyftLp d6BEuXF8W1bMB/XvCl/iQlbUlaOQIJ72hQuH0T51POQPpaelPaaum0Y0e0UdWiYv jwBitqaUZnJcMzZcjJtZw0KUX70YF5QVIKraDOskJ/kr5fxOiRAaucE7S/QEicyI /pfxzcMoYJcLAk3OA5OhiEovQyGuf4MLbzbUFzRMMpg8yEuQjXE= =zpKE -----END PGP SIGNATURE----- --1LKvkjL3sHcu1TtY--