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 1n0evk-0007J3-Rm for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Dec 2021 07:23: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 1n0evC-00029a-Dh for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Dec 2021 07:23:22 +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 1n0evB-00029R-Jz for pgsql-hackers@lists.postgresql.org; Fri, 24 Dec 2021 07:23:22 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n0ev7-00026i-V1 for pgsql-hackers@lists.postgresql.org; Fri, 24 Dec 2021 07:23:19 +0000 Received: by mail-ed1-x534.google.com with SMTP id o6so30433139edc.4 for ; Thu, 23 Dec 2021 23:23:17 -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=MEQCetOyVPkBsQ+7Sf3TmMN+jqxpmrUZDS7X2JvuXZg=; b=eMDvL5cgsVFWbvICC0E8GGFSuPN5uxmGNqGfF1vPdAgF7agsuR7wxFqvK3ZTQTA0fK aGepfJ3ELC7rDJNTaH2DFWkStuVUfOSV089AGnf/NCS+QFxWlcyoY0zGsoFQ7hmAPvac 3tIa89bM+QBtUQkBBRTZPvi+bmFIMTwdR9iVRpKQm9tR3Y/WE2kjwY+O00Vg1ihKQSat xIlp64XJ2CkTFKAwyjPCFan4Es5k2/0Lm6Xx9WnO1I73Emy7GGNpUZeAD344SgctpOf5 3hnDYvQwY3POpBdB9EIf887YC0NR8a90bZEbTj0jTuZtZ0pxoNfJ8bbVNeq2CfYWPTTV IgRg== 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=MEQCetOyVPkBsQ+7Sf3TmMN+jqxpmrUZDS7X2JvuXZg=; b=PoGGvxC+ApWc8wX+HWbcCEUmurfRXk9nkrucI6Nz49geznImvI3TYi8pvc+rNdM0jW oxvqYfBB5fpvcbozqxUKNoC0CUUvj0IPfqhrKJHSRwnIi9wGF7aE5P00c2C4m/w4LGAN oEJcXB/S8B9bcuNBLxbh1aDq4y8PPT4vwOui1DmYnc0B3IzIMiJM/TjwiQv8gaZioYAC qU5fGbRLimxiuibdlPJnlUA1KzejHOt5i20n6EUJvvUJdKdemGL7QXn/sGt/CjTcMwSL 6qCvVfW7SXmyI/OgdySprNrWuhNBVcJhBMeYrsjUFjS8Z/l0nu/G/z6yPE6fVsc2bh5G pbdA== X-Gm-Message-State: AOAM533AMkpJKxKaCwrlprdUUeEqz0164/trCk9s8bAdDwGq0oVcO+1q nDnxAECCPjAnOqyMoh8jQ/stF2o2sH2c1jbf00PcTV9wr3N4UrI6JEMGKB+iIw0uOqloS5B2mCL zDEVQSEbm0FF6vZK6x1YQ3FCQznHN0j8TyukZzpvgvXL3K8dogbpuBT3754gt0HU6P8sHipLXd+ KNGsBASs8MSPvcqohoOE+CB2kRrVv4z2SF41RNwUv0zYUlnBBaUi82 X-Google-Smtp-Source: ABdhPJxqX0sYmS4kLQRq82a/pnYq+fj7dvirULSL7FvKBbtrXxhg5Kg0uY08I/UxVdQkxSuZHuvibg== X-Received: by 2002:aa7:cd12:: with SMTP id b18mr4903488edw.355.1640330595712; Thu, 23 Dec 2021 23:23:15 -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 sc3sm2478138ejc.93.2021.12.23.23.23.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 23 Dec 2021 23:23:15 -0800 (PST) Message-ID: Date: Fri, 24 Dec 2021 08:23:13 +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: Kyotaro Horiguchi Cc: masao.fujii@oss.nttdata.com, tgl@sss.pgh.pa.us, peter.eisentraut@enterprisedb.com, pgsql-hackers@lists.postgresql.org References: <683c128a-5961-9b7b-ad68-0ea850e62363@enterprisedb.com> <685b21d3-254d-48a7-8a26-e6e182b5e193@oss.nttdata.com> <20211224.143725.1750248643492340056.horikyota.ntt@gmail.com> From: Tomas Vondra In-Reply-To: <20211224.143725.1750248643492340056.horikyota.ntt@gmail.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/24/21 06:37, Kyotaro Horiguchi wrote: > At Thu, 23 Dec 2021 19:50:22 +0100, Tomas Vondra wrote in >> 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. > > I don't mean to stand on Fujii-san's side particularly, but it seems > to me sequences of RDBSs are not rolled back generally. Some googling > told me that at least Oracle (documented), MySQL, DB2 and MS-SQL > server doesn't rewind sequences at rollback, that is, sequences are > incremented independtly from transaction control. It seems common to > think that two nextval() calls for the same sequence must not return > the same value in any context. > Yes, sequences are not rolled back on abort generally. That would require much stricter locking, and that'd go against using sequences in concurrent sessions. But we're not talking about sequence rollback - we're talking about data loss, caused by failure to flush WAL for a sequence. But that affects the *current* code too, and to much greater extent. Consider this: BEGIN; SELECT nextval('s') FROM generate_series(1,1000) s(i); ROLLBACK; -- or crash of a different backend BEGIN; SELECT nextval('s'); COMMIT; With the current code, this may easily lose the WAL, and we'll generate duplicate values from the sequence. We pretty much ignore the COMMIT. With the proposed change to WAL logging, that is not possible. The COMMIT flushes enough WAL to prevent this issue. So this actually makes this issue less severe. Maybe I'm missing some important detail, though. Can you show an example where the proposed changes make the issue worse? >>> 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. > > So, strictly speaking, that is a violation of the constraint I > mentioned regardless whether the transaction is committed or > not. However we have technical limitations as below. > I don't follow. What violates what? If the transaction commits (and gets a confirmation from sync replica), the modified WAL logging prevents duplicate values. It does nothing for uncommitted transactions. Seems like an improvement to me. >> 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. > > It seems to me, from the fact that sequences are designed explicitly > untransactional and that behavior is widely adopted, the discussion > might be missing some significant use-cases. But there's a > possibility that the spec of sequence came from some technical > limitation in the past, but I'm not sure.. > No idea. IMHO from the correctness / behavior point of view, the modified logging is an improvement. The only issue is the additional overhead, and I think the cache addresses that quite well. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company