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 1r4J0X-008A3e-Ka for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Nov 2023 10:57:01 +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 1r4J0V-000QxI-Hm for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Nov 2023 10:56:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r4J0V-000QxA-70 for pgsql-hackers@lists.postgresql.org; Sat, 18 Nov 2023 10:56:59 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r4J0M-00736Q-Ou for pgsql-hackers@lists.postgresql.org; Sat, 18 Nov 2023 10:56:57 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-4079ed65471so2958715e9.1 for ; Sat, 18 Nov 2023 02:56:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1700305009; x=1700909809; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=dt4uq3FWlEFJn2eqZ1iZ2qmWngCEfWpFERNHP3RVaU0=; b=lY6taHa8RTjM6HYVUUT7qc6gnDZrFCmDVUT4l/E6KT/u4lW0/X2f2w7YPttx7K4qaf 3VT57c5OCyfsINMGfWuhg8QHvW3RqgKRF3QjpPfcnuoHJqGH1M3C/64BMgqvQ5sV/DmF pUmYKXyihEeqrbYtC+PGJg1i2uDLXDRiJWaW/6pGRxBPkyfTzvjEy9HhLj9cNdewk37u zMK7DDIC14b3qLA+6FPwtnb48ad+c6e/UVpsA9VwyMlnz246JyD134CBElFfNiUObhU8 stKtHt/rmkx8ZzSah9sV0V7itcfvmV1vL3v0Vj7mtPybvgRvRtwe3hkL2IiUeQMTc44W MyuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700305009; x=1700909809; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dt4uq3FWlEFJn2eqZ1iZ2qmWngCEfWpFERNHP3RVaU0=; b=AyNNWzESXw0f1XTyJOdQv0Hn81JDpp49mNTGjdtncEfHcZ0EiPFmBhL50cuZYdhODP /XSbHkfaCRxIAfZvd9vQY7uO5AiFHKkHaIlc9hs/GtoxBZXtILvob+o9iEgqgZHxs0a/ tsaA8QYVlTqLPHHrMZfUUj16kpxrkr21eM6WumLy+h+OarpejNr/pFN37YLaMuX9qSXm n17rTx1famqmC/8DTNTF7vgKaXV7YMdnAEI/Tnkxo5+EmL4mMy509+9mnFbgS64fUZgF YyOg+gs1mGWKIL86PWQFD0HHmCcNLOHbwUyGvZfP064V0QfyBL2TzC6eRWfZYaGUidRd ZqfQ== X-Gm-Message-State: AOJu0YxUuTSYvbL8YkPE8XJWjp40CnmZQj3ZG7ANY5kzRTvpZc6SaScZ TJadkXC57In8F/G1doJCxLs+VNJ8W8zAM8+N2+k= X-Google-Smtp-Source: AGHT+IF0pckAuTQrHjJWHwoS11oR4uhJuzcxOAXGComXZgokZEYiscyGTAckNBkogUDvpRNQdfLmWQ== X-Received: by 2002:a05:600c:1ca4:b0:405:359a:c965 with SMTP id k36-20020a05600c1ca400b00405359ac965mr1538971wms.4.1700305009050; Sat, 18 Nov 2023 02:56:49 -0800 (PST) Received: from [10.137.0.17] (ip-86-49-250-157.bb.vodafone.cz. [86.49.250.157]) by smtp.gmail.com with ESMTPSA id y3-20020a05600c17c300b0040586360a36sm10050348wmo.17.2023.11.18.02.56.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 18 Nov 2023 02:56:48 -0800 (PST) Message-ID: <49958b83-009a-9360-2969-ea42cd15fff7@enterprisedb.com> Date: Sat, 18 Nov 2023 11:56:47 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: long-standing data loss bug in initial sync of logical replication Content-Language: en-US To: Andres Freund Cc: PostgreSQL Hackers References: <20231118015443.sfxfbr4eqolxbmjc@awork3.anarazel.de> <20231118025445.crhaeeuvoe2g5dv6@awork3.anarazel.de> From: Tomas Vondra In-Reply-To: <20231118025445.crhaeeuvoe2g5dv6@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/18/23 03:54, Andres Freund wrote: > Hi, > > On 2023-11-17 17:54:43 -0800, Andres Freund wrote: >> On 2023-11-17 15:36:25 +0100, Tomas Vondra wrote: >>> Overall, this looks, walks and quacks like a cache invalidation issue, >>> likely a missing invalidation somewhere in the ALTER PUBLICATION code. > > I can confirm that something is broken with invalidation handling. > > To test this I just used pg_recvlogical to stdout. It's just interesting > whether something arrives, that's easy to discern even with binary output. > > CREATE PUBLICATION pb; > src/bin/pg_basebackup/pg_recvlogical --plugin=pgoutput --start --slot test -d postgres -o proto_version=4 -o publication_names=pb -o messages=true -f - > > S1: CREATE TABLE d(data text not null); > S1: INSERT INTO d VALUES('d1'); > S2: BEGIN; INSERT INTO d VALUES('d2'); > S1: ALTER PUBLICATION pb ADD TABLE d; > S2: COMMIT > S2: INSERT INTO d VALUES('d3'); > S1: INSERT INTO d VALUES('d4'); > RL: > > Without the 'd2' insert in an in-progress transaction, pgoutput *does* react > to the ALTER PUBLICATION. > > I think the problem here is insufficient locking. The ALTER PUBLICATION pb ADD > TABLE d basically modifies the catalog state of 'd', without a lock preventing > other sessions from having a valid cache entry that they could continue to > use. Due to this, decoding S2's transactions that started before S2's commit, > will populate the cache entry with the state as of the time of S1's last > action, i.e. no need to output the change. > > The reason this can happen is because OpenTableList() uses > ShareUpdateExclusiveLock. That allows the ALTER PUBLICATION to happen while > there's an ongoing INSERT. > I guess this would also explain why changing the lock mode from ShareUpdateExclusiveLock to ShareRowExclusiveLock changes the behavior. INSERT acquires RowExclusiveLock, which doesn't conflict only with the latter. > I think this isn't just a logical decoding issue. S2's cache state just after > the ALTER PUBLICATION is going to be wrong - the table is already locked, > therefore further operations on the table don't trigger cache invalidation > processing - but the catalog state *has* changed. It's a bigger problem for > logical decoding though, as it's a bit more lazy about invalidation processing > than normal transactions, allowing the problem to persist for longer. > Yeah. I'm wondering if there's some other operation acquiring a lock weaker than RowExclusiveLock that might be affected by this. Because then we'd need to get an even stronger lock ... > > I guess it's not really feasible to just increase the lock level here though > :(. The use of ShareUpdateExclusiveLock isn't new, and suddenly using AEL > would perhaps lead to new deadlocks and such? But it also seems quite wrong. > If this really is about the lock being too weak, then I don't see why would it be wrong? If it's required for correctness, it's not really wrong, IMO. Sure, stronger locks are not great ... I'm not sure about the risk of deadlocks. If you do ALTER PUBLICATION ... ADD TABLE it's not holding many other locks. It essentially gets a lock just a lock on pg_publication catalog, and then the publication row. That's it. If we increase the locks from ShareUpdateExclusive to ShareRowExclusive, we're making it conflict with RowExclusive. Which is just DML, and I think we need to do that. So maybe that's fine? For me, a detected deadlock is better than silently missing some of the data. > > We could brute force this in the logical decoding infrastructure, by > distributing invalidations from catalog modifying transactions to all > concurrent in-progress transactions (like already done for historic catalog > snapshot, c.f. SnapBuildDistributeNewCatalogSnapshot()). But I think that'd > be a fairly significant increase in overhead. > I have no idea what the overhead would be - perhaps not too bad, considering catalog changes are not too common (I'm sure there are extreme cases). And maybe we could even restrict this only to "interesting" catalogs, or something like that? (However I hate those weird differences in behavior, it can easily lead to bugs.) But it feels more like a band-aid than actually fixing the issue. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company