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 1n05lZ-000071-0z for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 17:51:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n05lW-0008Ku-Ak for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 17:51:02 +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 1n05lV-0008Kl-TP for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 17:51:01 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n05lN-0001kH-Nn for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 17:51:00 +0000 Received: from [192.168.11.7] (p2638163-ipngn12701funabasi.chiba.ocn.ne.jp [180.59.132.163]) by oss.nttdata.com (Postfix) with ESMTPSA id 87A6360792; Thu, 23 Dec 2021 02:50:49 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.4 at oss.nttdata.com Content-Type: multipart/mixed; boundary="------------gogDTIAEvsi10SC20jP961SP" Message-ID: <5774a616-8e87-d7a5-7856-6db0ff1a78b0@oss.nttdata.com> Date: Thu, 23 Dec 2021 02:50:49 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: sequences vs. synchronous replication Content-Language: en-US To: Tomas Vondra , 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> From: Fujii Masao In-Reply-To: <5c788a12-def6-daab-ac08-c006dfbaed7d@enterprisedb.com> 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. --------------gogDTIAEvsi10SC20jP961SP Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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. > 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. > 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. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------gogDTIAEvsi10SC20jP961SP Content-Type: text/plain; charset=UTF-8; name="sequence_log.patch" Content-Disposition: attachment; filename="sequence_log.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2NvbW1hbmRzL3NlcXVlbmNlLmMgYi9zcmMvYmFj a2VuZC9jb21tYW5kcy9zZXF1ZW5jZS5jCmluZGV4IDcyYmZkYzA3YTQuLjM4Y2Q1NWI4MWEg MTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL2NvbW1hbmRzL3NlcXVlbmNlLmMKKysrIGIvc3Jj L2JhY2tlbmQvY29tbWFuZHMvc2VxdWVuY2UuYwpAQCAtMzcsNiArMzcsNyBAQAogI2luY2x1 ZGUgIm1pc2NhZG1pbi5oIgogI2luY2x1ZGUgIm5vZGVzL21ha2VmdW5jcy5oIgogI2luY2x1 ZGUgInBhcnNlci9wYXJzZV90eXBlLmgiCisjaW5jbHVkZSAicmVwbGljYXRpb24vc3luY3Jl cC5oIgogI2luY2x1ZGUgInN0b3JhZ2UvbG1nci5oIgogI2luY2x1ZGUgInN0b3JhZ2UvcHJv Yy5oIgogI2luY2x1ZGUgInN0b3JhZ2Uvc21nci5oIgpAQCAtNjc2LDggKzY3Nyw5IEBAIG5l eHR2YWxfaW50ZXJuYWwoT2lkIHJlbGlkLCBib29sIGNoZWNrX3Blcm1pc3Npb25zKQogCWVs c2UKIAl7CiAJCVhMb2dSZWNQdHIJcmVkb3B0ciA9IEdldFJlZG9SZWNQdHIoKTsKKwkJWExv Z1JlY1B0cglwYWdlbHNuID0gUGFnZUdldExTTihwYWdlKTsKIAotCQlpZiAoUGFnZUdldExT TihwYWdlKSA8PSByZWRvcHRyKQorCQlpZiAocGFnZWxzbiA8PSByZWRvcHRyIHx8IFN5bmNS ZXBOZWVkc1dhaXQocGFn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