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 1n06qe-00038X-0J for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 19:00:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n06qa-0007pY-L9 for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 19:00:20 +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 1n06qa-0007pO-0b for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 19:00:20 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n06qT-0002EV-Ex for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 19:00:18 +0000 Received: by mail-ed1-x536.google.com with SMTP id x15so12427009edv.1 for ; Wed, 22 Dec 2021 11:00:13 -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=YP4weIl1aFVVsg+PLjJqrO1dmIoWARJy2qx0TNplEKI=; b=DVaZrAbz1n8Sy9jBnbCakqRe2tXya8bKLyqaPTMUHfQluBVMAQLI4Q+a4baTM7YGEB 5TWRb1gh9OJztaK8uLVl3EbIFKlZoNlU9U1TQ6UCTQWsLAM+uvcGvUS11yYiS3etBRX3 HUICz1LkwUi/Tvb0B8Oc7DQ76XyVpDAuqqZr3gla8YGp5YqZV5ERDKmRbjFaunIoOlMl 2Esxl9oqzbYzvyMPJDlmBYYdvM06DNEXJ8980KfBKRO5TR43r8Lhcjy5oouc5xdr+H8r yrKKzWxr+DTDU4TTx16A0GIudpUtbwG9UDzSjVYTiMrtDXbAkUIUYSjFN0IRfIHxD4xO WTTg== 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=YP4weIl1aFVVsg+PLjJqrO1dmIoWARJy2qx0TNplEKI=; b=oJM/j5qXmtMA7erCoOP6uLUDOaCyrTJ38T4J3xCD1ta6KlyA9LJufBUGjLfS8wZMiK aGELYn9QgLGw8Y+KjoYUakXsKDaE95alTWJ5Lp6/smWZL6L1bQ8zQIwxnqwvATwsQ1O3 2U1ZSi0h5HoWjpyUUqw9z8NvXRcJfLvF6cGjwacs9DbYpBP2EyFwADHju7Cfe9PLNyN7 jU4TKRwaAVllad7pl2UyqKv9vJVjUoukjhVFJtI6X/BS8U6Q/7MZxcneKXGjGSYZC8a0 NpdZW/cOOcnuoraeZ7yuz/iQRUoa+tk+cjjqiAtR3WyrZlaWb+D0L5/eXP4i8Oxo4GPO JSVw== X-Gm-Message-State: AOAM530sg4XdWBZjvM6yP/kwmJYBatx407ix50lN1NBOUJSIex3+w6ZP gmVcaKRykZ7g603VAUewtZHuCmcdCFLULHxLQBEUxBW5AYiMDRWM8yOyTiEzxqL0S0d4QndeoF2 35Ud4Vjl7EhPAqDqdlPGZ/e8pdsweOUzWt20Kobc3020h6UgbIPXVrgbjiYO/OFe/nHAZ6htj1J l0NeKmVnHq27holoE15+NnVviq46EeG67GMZrXEV24/3kLxZVNHny7 X-Google-Smtp-Source: ABdhPJyaFmaRFoCN9XFdXZ3pBDkGsR5DeFCMU9yjNkpSfq5L7KMeXA4qChK+GQXtNpsnT7zxlQUNfg== X-Received: by 2002:a17:906:c444:: with SMTP id ck4mr3423595ejb.89.1640199611799; Wed, 22 Dec 2021 11:00:11 -0800 (PST) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id m16sm1205814edd.61.2021.12.22.11.00.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Dec 2021 11:00:11 -0800 (PST) Message-ID: <05bf76b1-3b00-a86a-d492-496fb81a4544@enterprisedb.com> Date: Wed, 22 Dec 2021 20:00:09 +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: Fujii Masao , Amit Kapila Cc: PostgreSQL Hackers References: <712cad46-a9c8-1389-aef8-faf0203c9be9@enterprisedb.com> <4450cd74-1a41-ec03-83ee-fcabc183715d@enterprisedb.com> <6597cf88-a499-e5be-e027-62753b197500@oss.nttdata.com> <5c788a12-def6-daab-ac08-c006dfbaed7d@enterprisedb.com> <5774a616-8e87-d7a5-7856-6db0ff1a78b0@oss.nttdata.com> From: Tomas Vondra In-Reply-To: <5774a616-8e87-d7a5-7856-6db0ff1a78b0@oss.nttdata.com> 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/22/21 18:50, Fujii Masao wrote: > > > On 2021/12/22 21:11, Tomas Vondra wrote: >> Interesting idea, but I think it has a couple of issues :-( > > Thanks for the review! > >> 1) We'd need to know the LSN of the last WAL record for any given >> sequence, and we'd need to communicate that between backends somehow. >> Which seems rather tricky to do without affecting performance. > > How about using the page lsn for the sequence? nextval_internal() > already uses that to check whether it's less than or equal to checkpoint > redo location. > Hmm, maybe. > >> 2) SyncRepWaitForLSN() is used only in commit-like situations, and >> it's a simple wait, not a decision to write more WAL. Environments >> without sync replicas are affected by this too - yes, the data loss >> issue is not there, but the amount of WAL is still increased. > > How about reusing only a part of code in SyncRepWaitForLSN()? Attached > is the PoC patch that implemented what I'm thinking. > > >> IIRC sync_standby_names can change while a transaction is running, >> even just right before commit, at which point we can't just go back in >> time and generate WAL for sequences accessed earlier. But we still >> need to ensure the sequence is properly replicated. > > Yes. In the PoC patch, SyncRepNeedsWait() still checks > sync_standbys_defined and uses SyncRepWaitMode. But they should not be > checked nor used because their values can be changed on the fly, as you > pointed out. Probably SyncRepNeedsWait() will need to be changed so that > it doesn't use them. > Right. I think the data loss with sync standby is merely a symptom, not the root cause. We'd need to deduce the LSN for which to wait at commit. > >> 3) I don't think it'd actually reduce the amount of WAL records in >> environments with many sessions (incrementing the same sequence). In >> those cases the WAL (generated by in-progress xact from another >> session) is likely to not be flushed, so we'd generate the extra WAL >> record. (And if the other backends would need flush LSN of this new >> WAL record, which would make it more likely they have to generate WAL >> too.) > > With the PoC patch, only when previous transaction that executed > nextval() and caused WAL record is aborted, subsequent nextval() > generates additional WAL record. So this approach can reduce WAL volume > than other approach? > > In the PoC patch, to reduce WAL volume more, it might be better to make > nextval_internal() update XactLastRecEnd and assign XID rather than > emitting new WAL record, when SyncRepNeedsWait() returns true. > Yes, but I think there are other cases. For example the WAL might have been generated by another backend, in a transaction that might be still running. In which case I don't see how updating XactLastRecEnd in nextval_internal would fix this, right? I did some experiments with increasing CACHE for the sequence, and that mostly eliminates the overhead. See the message I sent a couple minutes ago. IMHO that's a reasonable solution for the tiny number of people using nextval() in a way that'd be affected by this (i.e. without writing anything else in the xact). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company