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 1r4WPe-0095wj-DP for pgsql-hackers@arkaria.postgresql.org; Sun, 19 Nov 2023 01:15:50 +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 1r4WPa-002isR-Qc for pgsql-hackers@arkaria.postgresql.org; Sun, 19 Nov 2023 01:15:46 +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 1r4WPa-002isJ-BE for pgsql-hackers@lists.postgresql.org; Sun, 19 Nov 2023 01:15:46 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r4WPS-006FPF-NG for pgsql-hackers@lists.postgresql.org; Sun, 19 Nov 2023 01:15:44 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40839807e82so3813135e9.0 for ; Sat, 18 Nov 2023 17:15:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1700356536; x=1700961336; 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=55uU0DQciXETwhZ6UbZmSVIcVR41vBRe3hjrDyMTd1I=; b=QA9NjIFrufc7mSUIjw7pZTMnH7g8UBndpMqmlsIywG7nQQlGvILYmqv8c4aDq/A7Dd IqU1l6ATiR7EPu+EVUmR/UXQ7d4Wbx4V8JNpBd0EM5onBbMmUj9mUFQO6TlUhVdqHZYL vXEOX0cHq5l9csvqtdaslQpumu2XUZJtDsXjoZEYuVMXJSxfrntd5RmOBrzJXvahLD5u pHIeTUNJYeH2CZnoyD9KzcgU0oVJx1YTrTNWvo5GWDhFypZ8MmEHbS3jrf7hvJ1UziIt HG+foEFeSY2gV4JxxX0GaTrPnsjFYbd2E3XghEsJmBikH5UYNZUfCcjIUgXBDRBa917W xaiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700356536; x=1700961336; 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=55uU0DQciXETwhZ6UbZmSVIcVR41vBRe3hjrDyMTd1I=; b=oGL2zrqJMclqEHEfh5VRMxCgS8VHcRmWewX60fgjAsCXyCdegzGk5LgXn+ThCHJiFz uyD+ZDVC0psmE9P6FALAVT5QPlh0mNvlkodGWGgInPdPwGjUvoc8aO0dES/ADtnysD+u wS4E/dYL/VEuLARSFbRzQIwPp9ZVe1U0ShnYYGwTFYjHbbBdE4YxITD1XpE0AdDDXYTv vebq1ukPQ/ZSnqbyl/+Q1kUGZ/MVyf2NG5D5FaLv5TSoWL9NVAsFVnEHDDZzTjTHvZdH bbA810I4kGkErDp3iza9LPGkPlFUl6o/1REvU2OjZFlErOJDpbGlUUcQDMeLIUM2O4+W uR7Q== X-Gm-Message-State: AOJu0YxK8oR95ZYCutK1SpCfm0QBgxzAnNXM+bLc6usJtA68q5j/wV1t iyQNJwh077aATUlYAWLr91zGD57F5tgZy9ADaBg= X-Google-Smtp-Source: AGHT+IG7HR2mHuBxbqL1Xacno98zd0N9kv3VqW4jCFs1+H8XEPRAqF+xc03waXJH1hYB3nbVy6fa5g== X-Received: by 2002:a05:600c:a03:b0:3ff:516b:5c4c with SMTP id z3-20020a05600c0a0300b003ff516b5c4cmr2645400wmp.18.1700356535445; Sat, 18 Nov 2023 17:15:35 -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 v10-20020a05600c444a00b00407460234f9sm8319632wmn.21.2023.11.18.17.15.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 18 Nov 2023 17:15:35 -0800 (PST) Message-ID: <5cc4bbc8-b2ad-3e6e-9878-cef8ddde8ffe@enterprisedb.com> Date: Sun, 19 Nov 2023 02:15:33 +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> <49958b83-009a-9360-2969-ea42cd15fff7@enterprisedb.com> <20231118181257.qtebsnkrzj3icg36@awork3.anarazel.de> <3c5ad91c-330a-0c5a-974a-da3801ffc18b@enterprisedb.com> <20231118210519.yborkczu2aln6zdd@awork3.anarazel.de> From: Tomas Vondra In-Reply-To: <20231118210519.yborkczu2aln6zdd@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 22:05, Andres Freund wrote: > Hi, > > On 2023-11-18 21:45:35 +0100, Tomas Vondra wrote: >> On 11/18/23 19:12, Andres Freund wrote: >>>> 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. >>> >>> From what I can tell it needs to to be an AccessExlusiveLock. Completely >>> independent of logical decoding. The way the cache stays coherent is catalog >>> modifications conflicting with anything that builds cache entries. We have a >>> few cases where we do use lower level locks, but for those we have explicit >>> analysis for why that's ok (see e.g. reloptions.c) or we block until nobody >>> could have an old view of the catalog (various CONCURRENTLY) operations. >>> >> >> Yeah, I got too focused on the issue I triggered, which seems to be >> fixed by using SRE (still don't understand why ...). But you're probably >> right there may be other cases where SRE would not be sufficient, I >> certainly can't prove it'd be safe. > > I think it makes sense here: SRE prevents the problematic "scheduling" in your > test - with SRE no DML started before ALTER PUB ... ADD can commit after. > If understand correctly, with the current code (which only gets ShareUpdateExclusiveLock), we may end up in a situation like this (sessions A and B): A: starts "ALTER PUBLICATION p ADD TABLE t" and gets the SUE lock A: writes the invalidation message(s) into WAL B: inserts into table "t" B: commit A: commit With the stronger SRE lock, the commits would have to happen in the opposite order, because as you say it prevents the bad ordering. But why would this matter for logical decoding? We accumulate the the invalidations and execute them at transaction commit, or did I miss something? So what I think should happen is we get to apply B first, which won't see the table as part of the publication. It might even build the cache entries (syscache+relsync), reflecting that. But then we get to execute A, along with all the invalidations, and that should invalidate them. I'm clearly missing something, because the SRE does change the behavior, so there has to be a difference (and by my reasoning it shouldn't be). Or maybe it's the other way around? Won't B get the invalidation, but use a historical snapshot that doesn't yet see the table in publication? > I'm not sure there are any cases where using SRE instead of AE would cause > problems for logical decoding, but it seems very hard to prove. I'd be very > surprised if just using SRE would not lead to corrupted cache contents in some > situations. The cases where a lower lock level is ok are ones where we just > don't care that the cache is coherent in that moment. > Are you saying it might break cases that are not corrupted now? How could obtaining a stronger lock have such effect? > In a way, the logical decoding cache-invalidation situation is a lot more > atomic than the "normal" situation. During normal operation locking is > strictly required to prevent incoherent states when building a cache entry > after a transaction committed, but before the sinval entries have been > queued. But in the logical decoding case that window doesn't exist. > Because we apply the invalidations at commit time, so it happens as a single operation that can't interleave with other sessions? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company