Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tKdgv-009tlE-Jz for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Dec 2024 13:20:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tKdgt-008Rmi-2m for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Dec 2024 13:20:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tKdgs-008RmH-K1 for pgsql-hackers@lists.postgresql.org; Mon, 09 Dec 2024 13:20:47 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tKdgr-001u6V-2p for pgsql-hackers@postgresql.org; Mon, 09 Dec 2024 13:20:46 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2163dc5155fso10751175ad.0 for ; Mon, 09 Dec 2024 05:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pritambaral.com; s=google; t=1733750443; x=1734355243; darn=postgresql.org; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=R3eg0FwvbYlDcRKuWoqzuDKHnc/oZEzAn0iOn894rtY=; b=SgowXJ6sORtmiHxBEJJfFajdyK6Ehuj+N5NGcj/yZg7Qcz2PB9BcJOLxZrnEQtNwAr xxxALFx1dCw2g0O2uXFv5BsqJVDHY/6Bdelc8BnpPwqqs/GuATNq7qB3lK2KyOtrDMjR 5C6SFJpPgHWE6pVr+9k/sJSUUJd8Y0r1IvqP4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733750443; x=1734355243; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R3eg0FwvbYlDcRKuWoqzuDKHnc/oZEzAn0iOn894rtY=; b=UXSp6NIIcSUonPumzkKXcibiD3RDOzcXntsYAvqwMGB1Iz5e8425FfHKaejaNWh47U fo7OrnKxgcgio7WaagXmmTlqxMMlzD/QCu8RqK24YShLc3QJucCeq82lW5ouw48PMK2j QMyO9UFPVaKPm3ya0wryahhnUAGUP7dLyI0hPJkdwTvXDtplBHBmcMbT0vgWKxrvsmx8 Xt5+52JR0irNYObNZUcg6s0eu4BuaR4W9T0t/cr4r09n/SI+roVPz4W5LIX4A56Lk8tK 8sRor+Djsv6nc3kd6GH+ZQJIl2LGbWJj+I/0HXts8FUJnL8CH/PkfrbX0DFNUYRziVnH 0alw== X-Gm-Message-State: AOJu0Yzu4MKej40gE0QT1mQingyT5Q8tljV6jon+qPQA/I5cl65FRmri Abw4/dor2oh89q02FKK2NCFJ42upVtPRzVifDBPXZZl5vfHQdhh5Ux11Yr0X8JlXygP6lvkSKbI Hpt4= X-Gm-Gg: ASbGncsTP38fR7ZoXMPremZJYqtG6vvoqrhV5W3l8JKuYCsnyzss839Mi6zPncCyQLi ghtZUEijeNAEpOwWmJVIluqZaV2MGuJHQVYRFmfG/keKKFnISxBnZeSg5Cra9A2liSMgKEJiXyD /aZBjNeb1tRDp2UG5GabM0kOyN7NxGyb6WoxnwELD/8noWz26jj9GaQySUbcKbXNqrZz8FZoT17 msPsvGakMMLYBWpRXCAFQaQ0JnI0wIuyVYlzjd9Ovg064XDpnsJHnU= X-Google-Smtp-Source: AGHT+IEipRPyz1HTNxSR503cqFITQPq3ookHfnowEannKzdacIWSmajXXazr/W1gWrwRrDmnBde+3A== X-Received: by 2002:a17:903:41cf:b0:216:4a06:e87a with SMTP id d9443c01a7336-2164a06eae7mr67013165ad.40.1733750443622; Mon, 09 Dec 2024 05:20:43 -0800 (PST) Received: from [192.168.1.234] ([106.51.64.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-215f8e3e5f2sm72411175ad.13.2024.12.09.05.20.42 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Dec 2024 05:20:43 -0800 (PST) Content-Type: multipart/mixed; boundary="------------92y2WSq062DfO7YTH09wypdf" Message-ID: <26277b42-a950-4c56-8280-3371f2d0bf7c@pritambaral.com> Date: Mon, 9 Dec 2024 18:50:41 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-GB To: pgsql-hackers From: Pritam Baral Subject: Subscription sometimes loses txns after initial table sync List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------92y2WSq062DfO7YTH09wypdf Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit This was discovered when testing the plan for a major version upgrade via logical replication. Said plan requires that some tables be synced before others. So I implemented it using ALTER PUBLICATION ... ADD TABLE ... followed by ALTER SUBSCRIPTION ... REFRESH PUBLICATION. A test for correctness revealed that sometimes, for some tables added this way, txns after the initial data copy are lost by the subscription. A reproducer script is attached. It has been tested with PG 17.2, 14.15, and even 12.22 (on either side of the replication setup). The script runs at a default scale of 100 tables with 10k inserts each. This scale is enough to demonstrate a failure rate of 1% to 9% of tables on my modest laptop. In attempts to analyse why this happens, it has been observed that the sender sometimes does not pick up a published table, even when the receiver that started the sender process has seen the table as available (as returned by pg_get_publication_tables()) and has thus begun COPYing its data. When the COPY finishes (and the tablesync worker is finished), the apply loop on the receiver expects to receive (and apply) subsequent changes for such tables, but simply isn't sent any. This was observed by dumping every CopyData message sent over the wire. The attached script (like the original migration plan) uses a single publication and adds tables to it successively. Curiously, when the script was changed to use a dedicated publication per table (and thus, ALTER SUBSCRIPTION ... ADD PUBLICATION instead of ALTER SUBSCRIPTION ... REFRESH PUBLICATION), the no. of tables with data loss jumped to 100%. -- #!/usr/bin/env regards Chhatoi Pritam Baral --------------92y2WSq062DfO7YTH09wypdf Content-Type: application/x-shellscript; name="sub-loss-repro.sh" Content-Disposition: attachment; filename="sub-loss-repro.sh" Content-Transfer-Encoding: base64 IyEvdXNyL2Jpbi9lbnYgYmFzaAoKZXhwb3J0IFBHVVNFUj0ke1BHVVNFUjotcG9zdGdyZXN9 CmV4cG9ydCBQR0RBVEFCQVNFPSR7UEdEQVRBQkFTRTotcG9zdGdyZXN9CmV4cG9ydCBTUkNf UE9SVD0ke1NSQ19QT1JUOi01NDMyfQpleHBvcnQgRFNUX1BPUlQ9JHtEU1RfUE9SVDotNTQz M30KCk5UQUJMRVM9JHtOVEFCTEVTOi0xMDB9Ck5ST1dTPSR7TlJPV1M6LTEwMDAwfQoKUFNR TD0iJCh3aGljaCBwc3FsKSIKcHNxbCgpIHsKICAgICR7UFNRTH0gLVggLXYgT05fRVJST1Jf U1RPUD0xICIkQCIKfQoKZm9yIHBvcnQgaW4geyR7U1JDX1BPUlR9LCR7RFNUX1BPUlR9fTsg ZG8KICAgIGZvciB0bnVtIGluICQoc2VxIDAgJHtOVEFCTEVTfSk7IGRvCiAgICAgICAgZWNo byAiQ1JFQVRFIFRBQkxFIHQke3RudW19IChpZCBJTlRFR0VSIFBSSU1BUlkgS0VZKTsiOwog ICAgZG9uZSB8IHBzcWwgLXAgJHBvcnQ7CmRvbmUKCnBzcWwgLXAgJHtTUkNfUE9SVH0gLWMg IkNSRUFURSBQVUJMSUNBVElPTiBwdWIgRk9SIFRBQkxFIHQwOyIKcHNxbCAtcCAke0RTVF9Q T1JUfSA8PEVPRgpDUkVBVEUgU1VCU0NSSVBUSU9OIHN1YgogIENPTk5FQ1RJT04gJ3BvcnQ9 JHtTUkNfUE9SVH0gdXNlcj0ke1BHVVNFUn0gZGJuYW1lPSR7UEdEQVRBQkFTRX0nC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