Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mySm9-0005pX-54 for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Dec 2021 06:00:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mySm6-0007e7-Io for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Dec 2021 06:00:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mySm5-0007dx-S3 for pgsql-hackers@lists.postgresql.org; Sat, 18 Dec 2021 06:00:54 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mySm2-0006I2-Ml for pgsql-hackers@lists.postgresql.org; Sat, 18 Dec 2021 06:00:52 +0000 Received: by mail-ed1-x531.google.com with SMTP id t5so16046296edd.0 for ; Fri, 17 Dec 2021 22:00:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=YH6qGONWyp2A8/h7t+GrrVepRxMf8iT0JYEyrppxZ9E=; b=Qg3hHM6Z/ehqPm7DWv9CdA75yMEU7wEH25Dq+mi6F1rN9n2IOyrqePdQuwemoLXS4P nuWMlb1k8bysUo+AV1DUkOYKAqaPypZEdlQ77iOM92rH0qPg1f6ex5bqrO/f97p5zsbJ QjDv4HCNqZ5tCfYpHTmmxaKYQ04cxuIp9au1hzKG8hJkrm90pLcJxnf9vsx+No95kl6u //4ukRIYsWVb7ewczGgLr37mbPm23WqjwZ/QjD17NZxfVKq/yPyZfCeSrMxmJjPfgEPw RxWYVyrARFdeWcARPbhRR1qPhOG7lJbHB5Z5m9FMIAeBVy5S79POL6/wpfI/7JggE97Z iiCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=YH6qGONWyp2A8/h7t+GrrVepRxMf8iT0JYEyrppxZ9E=; b=vGlUncJrf8lW/5ZWilepefAsTJlY9RvizjJ7I0Bu9s+rSnEVMteTMGM/zw8IH8Mhha RXiCe7vXS5bYIx2Nwab1k25PRmGgqF95fXoJeSsytSenQBvFJjyIzuWB6SWbrQmGHrVq QSvauuTgthnapIY/oseVyrvAQwst09M8F5oOED1aEo5rsnLWY/5A9d6QMVYlDzCfyYdj 5aNGzz1aMiANXMiu4umlch0ox7/jmyVhkhETjlZgtu21myhVPJQFngPcaCdszmI3OORM W/TjBkzgI9VKAxhkreKA08Ll+fvRyCKAjXeaFr2Kcfv/YPMBTXLKoHrBK7o+5GXf4soD xu3g== X-Gm-Message-State: AOAM533UJ5ynVoTiPxvqURfd4dRyGQOpoDCOMhTnBuCgHcLA9vj14JIg yLg1qrbXpAMkyvT43qyJozNyy8Xj4ZHLzQzwMzPXG+Pt2rWN3DLMsmbENFL7RQxlD8g0T3T7XC3 bEobSNhlJxsziJMYRVfqccV3Svsmz3WpBHJwz3YSHEuXVzYJGDYg1PLJmX6OuPx47JNtmBz4juX lXhdD9dj/DYAxUDudbT1ViS32WdmJQtPazYRFVa9Xk89l9tr2/orQk X-Google-Smtp-Source: ABdhPJwmNkBLJF/3szQr0Cm8uuex4n3xxfOFfmw0AhOfn2/dAzxo8jS2lfL3fmquX63DSSw7WFjzUg== X-Received: by 2002:a17:906:15c1:: with SMTP id l1mr5262703ejd.280.1639807248929; Fri, 17 Dec 2021 22:00:48 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id cb3sm32271edb.35.2021.12.17.22.00.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 22:00:48 -0800 (PST) Message-ID: <38a6a779-ee8d-de3c-fbd7-709285be88fa@enterprisedb.com> Date: Sat, 18 Dec 2021 07:00:45 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: sequences vs. synchronous replication Content-Language: en-US To: Tom Lane Cc: PostgreSQL Hackers References: <712cad46-a9c8-1389-aef8-faf0203c9be9@enterprisedb.com> <1452362.1639803168@sss.pgh.pa.us> From: Tomas Vondra In-Reply-To: <1452362.1639803168@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/18/21 05:52, Tom Lane wrote: > Tomas Vondra writes: >> The problem is exactly the same as in [1] - the aborted transaction >> generated WAL, but RecordTransactionAbort() ignores that and does not >> update LogwrtResult.Write, with the reasoning that aborted transactions >> do not matter. But sequences violate that, because we only write WAL >> once every 32 increments, so the following nextval() gets "committed" >> without waiting for the replica (because it did not produce WAL). > > Ugh. > >> I'm not sure this is a clear data corruption bug, but it surely walks >> and quacks like one. My proposal is to fix this by tracking the lsn of >> the last LSN for a sequence increment, and then check that LSN in >> RecordTransactionCommit() before calling XLogFlush(). > > (1) Does that work if the aborted increment was in a different > session? I think it is okay but I'm tired enough to not be sure. > Good point - it doesn't :-( At least not by simply storing LSN in a global variable or something like that. The second backend needs to know the LSN of the last WAL-logged sequence increment, but only the first backend knows that. So we'd need to share that between backends somehow. I doubt we want to track LSN for every individual sequence (because for clusters with many dbs / sequences that may be a lot). Perhaps we could track just a fixed number o LSN values in shared memory (say, 1024), and update/read just the element determined by hash(oid). That is, the backend WAL-logging sequence with given oid would set the current LSN to array[hash(oid) % 1024], and backend doing nextval() would simply remember the LSN in that slot. Yes, if there are conflicts that'll flush more than needed. Alternatively we could simply use the current insert LSN, but that's going to flush more stuff than needed all the time. > (2) I'm starting to wonder if we should rethink the sequence logging > mechanism altogether. It was cool when designed, but it seems > really problematic when you start thinking about replication > behaviors. Perhaps if wal_level > minimal, we don't do things > the same way? Maybe, but I have no idea how should the reworked WAL logging work. Any batching seems to have this issue, and loging individual increments is likely going to be slower. Of course, reworking how sequences are WAL-logged may invalidate the "sequence decoding" patch I've been working on :-( regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company