Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e1uXx-0006Cz-FQ for pgsql-bugs@arkaria.postgresql.org; Tue, 10 Oct 2017 13:26:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e1uXx-0004sb-25 for pgsql-bugs@arkaria.postgresql.org; Tue, 10 Oct 2017 13:26:09 +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 1e1uXD-0001P0-10 for pgsql-bugs@postgresql.org; Tue, 10 Oct 2017 13:25:23 +0000 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e1uX8-0004nA-4H for pgsql-bugs@postgresql.org; Tue, 10 Oct 2017 13:25:21 +0000 Received: by mail-wm0-x22e.google.com with SMTP id f4so5333378wme.0 for ; Tue, 10 Oct 2017 06:25:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=4O/8qYFcIz0VQew1TR9nRdSYCbdlvp4B9adUOmd33Jc=; b=QQF0FYCfUthHwbgACoUYGn9XBG2rMytUHU0oU1pnfSZnuV7upmrNk29VIYF0NPEyVm toVB1OlOyBe1YTcsqsjzvhGlv0sifVg/xRK+KG3rkSJIIp7Z/J0gAeSeoSb23jXOuAkV GxgV0fXUgZnN84vgbFrOjAnQZEEOUp/UAh2VhH0vh/m735GBGmR9RZov2+xXLGrTV/G0 xL4ExPjuf9F7I0yDkSGdmGBTxSTGBP5NFnO5uufK1pxa1qIviKAxqPOG5pLPuDEHLKiw rTywA7c7n+g974SEeVVJDA7Q6cs6e2GItPc+suqMesf1hixDJ5tgZw7oGRfSZFN/KOTV lXig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=4O/8qYFcIz0VQew1TR9nRdSYCbdlvp4B9adUOmd33Jc=; b=g7Ki2uGKrcnryR5ayU9/4LNHFD2X2Hk2qnh7C5KosKnn1AfBB/3OrE15k8SUlXNZgk j9Mhfl1lZj6k3k4jNwfG1zOFdm0YOw4CglJdr5lMZSy/xLffx/T0eJ5Upf/zjrWCjs1c /OR6V9//t+EW0oyRtMk3rmMREWS3RXiJq/pPgBBM3+6qikPP7tBL2kk0GUCfAjnVw3VW gFoCLjcrdtI9kk88+OaQ6jPnG7r0e1MygYA4dFJlCOdUd4FxLg2TeWIpYf4Ep4H2At59 SvzavkDh8ewJWeuK+KUc4HK2t6SRrQxKLLd57x6EYFCYMSkicAoZlChcmuBFLzq3Z4Dv vPwA== X-Gm-Message-State: AMCzsaXzHfG7M/IA2E2kKJgb+aGBHAMX+3TxjeJBp0FXBwexkFXvkDrf 3V1jBc6EWRWsB4YG5teuLWNO60douVJJMMTQt569M3jsCSJsOGJ4ORLo7yyPETdQT11ujX2Enqh OyGQfXmS8HT8QMOCvmir5ap21t9xGsCtXeeAhTJ2zPe6XHo721JPV4q5gQlK1fZgn1SHkJ1Y1aZ 14Bh/NAGY= X-Google-Smtp-Source: AOwi7QDzgOWC4bXVPLy1QSFF555o3cCWQHijFGRfkrqDZtLiBfNvbx7AzH0QjIMrXGVq/Fi5jJ2miw== X-Received: by 10.223.134.68 with SMTP id 4mr13918481wrw.67.1507641915871; Tue, 10 Oct 2017 06:25:15 -0700 (PDT) Received: from [192.168.0.105] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id 76sm8646666wrb.9.2017.10.10.06.25.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Oct 2017 06:25:14 -0700 (PDT) Subject: Re: 10.0: Logical replication doesn't execute BEFORE UPDATE OF trigger To: Masahiko Sawada , Aleksander Alekseev Cc: PostgreSQL Hackers , pgsql-bugs References: <20171009141341.GA16999@e733.localdomain> From: Petr Jelinek Message-ID: Date: Tue, 10 Oct 2017 15:25:14 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit 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 On 10/10/17 09:53, Masahiko Sawada wrote: > On Tue, Oct 10, 2017 at 11:29 AM, Masahiko Sawada wrote: >> On Mon, Oct 9, 2017 at 11:13 PM, Aleksander Alekseev >> wrote: >>> 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; >>> ``` >>> >>> ... and inst3 is subscribed for both publications: >>> >>> ``` >>> CREATE SUBSCRIPTION allsub1 >>> CONNECTION 'host=10.128.0.16 user=eax dbname=eax' >>> PUBLICATION allpub; >>> >>> CREATE SUBSCRIPTION allsub2 >>> CONNECTION 'host=10.128.0.26 user=eax dbname=eax' >>> 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 = new.k) THEN >>> RAISE NOTICE 'test_before_insert trigger - merging data'; >>> UPDATE test SET v = v || ';' || new.v WHERE k = 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 = new.k) THEN >>> RAISE NOTICE 'test_before_update trigger - merging data'; >>> UPDATE test SET v = v || ';' || new.v WHERE k = new.k; >>> DELETE FROM test where k = 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 = 'k1' where k = 'k4'; >>> ``` >>> >>> Now on inst3: >>> >>> ``` >>> select * from test; >>> ``` >>> >>> Expected result >>> --------------- >>> >>> Rows are merged to: >>> >>> ``` >>> k | v >>> ----+------- >>> 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)=(k1) already exists. >>> [3176] LOG: worker process: logical replication worker for subscription 16402 (PID 3227) exited with exit code 1 >>> ``` >>> >>> What do you think? >>> >> >> 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. >> > > Attached patch store the updated columns bitmap set to RangeTblEntry. > In my environment this bug seems to be fixed by the patch. > Hi, let me start by saying that my view is that this is simply a documentation bug. Meaning that I didn't document that it does not work, but I also never intended it to work. Main reason is that we can't support the semantics of "UPDATE OF" correctly (see bellow). And I think it's better to not support something at all rather than making it behave differently in different cases. Now about the proposed patch, I don't think this is correct way to support this as it will only work when either PRIMARY KEY column was changed or when REPLICA IDENTITY is set to FULL for the table. And even then it will have very different semantics from how it works when the row is updated by SQL statement (all non-toasted columns will be reported as changed regardless of actually being changed or not). The more proper way to do this would be to run data comparison of the new tuple and the existing tuple values which a) will have different behavior than normal "UPDATE OF" triggers have because we still can't tell what columns were mentioned in the original query and b) will not exactly help performance (and perhaps c) one can write the trigger to behave this way already without impacting all other use-cases). -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs