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 1n0TBA-0003Ea-QE for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Dec 2021 18: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 1n0TAd-0002Jc-Gh for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Dec 2021 18:50:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n0TAc-0002JT-QF for pgsql-hackers@lists.postgresql.org; Thu, 23 Dec 2021 18:50:31 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n0TAY-0006sY-8N for pgsql-hackers@lists.postgresql.org; Thu, 23 Dec 2021 18:50:29 +0000 Received: by mail-ed1-x530.google.com with SMTP id j21so24812228edt.9 for ; Thu, 23 Dec 2021 10:50:25 -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=NRw1wGGcOCBhc/XgYtWC/r5hdzNe0B91TsCC8Cer5A8=; b=FEYv8Xn6nJh6GaQiMkcdaPZkMuLW9azHt2TosKLhCiYOXZyMTs2LotN9R11lPVWWzI l+97gxIcILTOcMbrx7QE2h4MsnxvYsLh8yBEPywFNR48mcWD8Br1BZ4RZRB8MGOEHq8J HsEcVkZQzYa0SRDCgobfR5pV05EiyePA2yxAymEZhmhI7I4jOij2mWrJbQZyvne7bmqx CejVGVLsso4Tv+jDijf9XrOMkcUXmd308ros2FDGVE8LAcGUxvF31102262/Aoin9tq8 TBM583S1lVz5Z6gwKcH1JR6YOfdJiKqaM0TJk0fZTXskdzG1vjnZ18+noxBeTaeKX4hB MBPA== 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=NRw1wGGcOCBhc/XgYtWC/r5hdzNe0B91TsCC8Cer5A8=; b=al+VlrxCbGiDVyZ9pkgYX25qOi++1xogKHu7zjnqu7sRM7yr8f2QzT7jnTBLU81SpH 6NvIKSpNgYMWFOGtTTAbjTUWRn6rYS2e5jJqCrnBzAxHG9g5fU9Lvilz6R/68irzg8uM o4Gimub6BLD6JIbilBZ1MeRJ3YL8tV4Cj8TBFOcE3qnC9UdjPPK1z8nIZUnyi9MSoQHR uxuWAPztDtwR4Hp+EtHnybUP1LYXlwpk1nYbLPw4bTNAL78c9+UdJXq+kFeY7dEmLrI8 QvgcFrjPT1t5qUt3jUM9ihDK4D8/s8SDa5awsr8yFEtdv7tdRsa8We5dej5YD5FtGnX4 8+SA== X-Gm-Message-State: AOAM532uVVQ7xx35hGkVVM6wUfb6MIdZZRqyvgE/ngrWswErPuihaxyP uiKYc3U3TzvhX2rrsVrtEniibfIo3lyO+01pef6IPY7xh3SBKsH8jE8oEMnLVeSIVdb1UYgCJ7k sY9R8QDxSpvL91s1KHk5L+IhTQ4TyYT7jzpliN1JdAp7mBf6wuI9/Q7uPprvHgWC0vLMd1GYU0o g0gFZtHiiZRFvrYIb+CCH+3ub8smaGWEeoBviVu2mgyxk17yQmmxRz X-Google-Smtp-Source: ABdhPJwqkHdKNgMC2xZdfRvZDSRbtpJwijUQvThbyNBLEsxoKh+3Kfwyg4imsGRHVwAIsjAN5JXOOA== X-Received: by 2002:a17:906:c110:: with SMTP id do16mr2874668ejc.625.1640285424637; Thu, 23 Dec 2021 10:50:24 -0800 (PST) Received: from [10.137.0.18] (ip-89-177-29-12.net.upcbroadband.cz. [89.177.29.12]) by smtp.gmail.com with ESMTPSA id cq19sm2250528edb.27.2021.12.23.10.50.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Dec 2021 10:50:24 -0800 (PST) Message-ID: Date: Thu, 23 Dec 2021 19:50:22 +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 , Tom Lane Cc: Peter Eisentraut , PostgreSQL Hackers References: <712cad46-a9c8-1389-aef8-faf0203c9be9@enterprisedb.com> <1452362.1639803168@sss.pgh.pa.us> <38a6a779-ee8d-de3c-fbd7-709285be88fa@enterprisedb.com> <7869761b-fdc7-6f7d-dc95-9ba8315a8ecf@enterprisedb.com> <1637476.1639862832@sss.pgh.pa.us> <8e24a470-b092-3c9f-0614-acd9df3f724f@enterprisedb.com> <8231183d-1077-5394-1ffa-0c9d8c3905b0@enterprisedb.com> <2683973.1640048519@sss.pgh.pa.us> <683c128a-5961-9b7b-ad68-0ea850e62363@enterprisedb.com> <685b21d3-254d-48a7-8a26-e6e182b5e193@oss.nttdata.com> From: Tomas Vondra In-Reply-To: <685b21d3-254d-48a7-8a26-e6e182b5e193@oss.nttdata.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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/23/21 15:42, Fujii Masao wrote: > > > On 2021/12/23 3:49, Tomas Vondra wrote: >> Attached is a patch tweaking WAL logging - in wal_level=minimal we do >> the same thing as now, in higher levels we log every sequence fetch. > > Thanks for the patch! > > With the patch, I found that the regression test for sequences failed. > > +            fetch = log = fetch; > > This should be "log = fetch"? > > On second thought, originally a sequence doesn't guarantee that the > value already returned by nextval() will never be returned by subsequent > nextval() after the server crash recovery. That is, nextval() may return > the same value across crash recovery. Is this understanding right? For > example, this case can happen if the server crashes after nextval() > returned the value but before WAL for the sequence was flushed to the > permanent storage. I think the important step is commit. We don't guarantee anything for changes in uncommitted transactions. If you do nextval in a transaction and the server crashes before the WAL gets flushed before COMMIT, then yes, nextval may generate the same nextval again. But after commit that is not OK - it must not happen. > So it's not a bug that sync standby may return the same value as > already returned in the primary because the corresponding WAL has not > been replicated yet, isn't it? > No, I don't think so. Once the COMMIT happens (and gets confirmed by the sync standby), it should be possible to failover to the sync replica without losing any data in committed transaction. Generating duplicate values is a clear violation of that. IMHO the fact that we allow a transaction to commit (even just locally) without flushing all the WAL it depends on is clearly a data loss bug. > BTW, if the returned value is stored in database, the same value is > guaranteed not to be returned again after the server crash or by sync > standby. Because in that case the WAL of the transaction storing that > value is flushed and replicated. > True, assuming the table is WAL-logged etc. I agree the issue may be affecting a fairly small fraction of workloads, because most people use sequences to generate data for inserts etc. >> So I think this makes it acceptable / manageable. Of course, this >> means the values are much less monotonous (across backends), but I >> don't think we really promised that. And I doubt anyone is really >> using sequences like this (just nextval) in performance critical use >> cases. > > I think that this approach is not acceptable to some users. So, if we > actually adopt WAL-logging every sequence fetch, also how about exposing > SEQ_LOG_VALS as reloption for a sequence? If so, those who want to log > every sequence fetch can set this SEQ_LOG_VALS reloption to 0. OTOH, > those who prefer the current behavior in spite of the risk we're > discussing at this thread can set the reloption to 32 like it is for > now, for example. > I think it'd be worth explaining why you think it's not acceptable? I've demonstrated the impact on regular workloads (with other changes that write stuff to WAL) is not measurable, and enabling sequence caching eliminates most of the overhead for the rare corner case workloads if needed. It does generate a bit more WAL, but the sequence WAL records are pretty tiny. I'm opposed to adding relooptions that affect correctness - it just seems like a bad idea to me. Moreover setting the CACHE for a sequence does almost the same thing - if you set CACHE 32, we only generate WAL once every 32 increments. The only difference is that this cache is not shared between backends, so one backend will generate 1,2,3,... and another backend will generate 33,34,35,... etc. I don't think that's a problem, because if you want strictly monotonous / gap-less sequences you can't use our sequences anyway. Yes, with short-lived backends this may consume the sequences faster, but well - short-lived backends are expensive anyway and overflowing bigserial is still unlikely. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company