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 1n00TT-0003S6-8r for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 12:12:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n00TS-00088I-7K for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Dec 2021 12:12: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 1n00TR-000889-In for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 12:12:01 +0000 Received: from mail-ed1-x530.google.com ([2a00:1450:4864:20::530]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n00TO-0007Qm-Up for pgsql-hackers@lists.postgresql.org; Wed, 22 Dec 2021 12:12:00 +0000 Received: by mail-ed1-x530.google.com with SMTP id y22so7899141edq.2 for ; Wed, 22 Dec 2021 04:11:58 -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=dLLPI6oDENz5W6j04P04QsnoVJvFk2oWb4qOQxeKAXI=; b=Qc/69MukvAGA7nbMIN6i8BNXz3HFuRPnoxz0hueZ1PRgP3cvKL0DXoQ6jfe9tHkd/q wHjhf4oC9OMMfwYZZK8TJFtaS7BwpTCqOUSUtYkMR38ayKRAx5SABGDxGP3tAe34cIuQ DbKg9BaMARun7tXpxAFDrhDHST03SehVE5TU9e1ELY7pHNzzNGrNKo6eyefmHoOMrOky x72X8G6BNaqtNtCTJ5Z2oNlvURU1OT3GS/mpkkwMSfYVSdqwg7U919m0dMMprgRqS4nR 1kso9atfrbbTR4+0sqU5mXT5568YqEoMC6rEecQZUpMuffUXGSEsPA2pBLP0ROMbrltv lRSw== 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=dLLPI6oDENz5W6j04P04QsnoVJvFk2oWb4qOQxeKAXI=; b=sFlljNeVyt/YvIcCAILsFPU96sq7YamubM5/4mqA5qP4R4Ev0Nwf8RcdLuPjdnXbgT x5n+nbojZPmWoJYkoFyxUlfOL1/AXCmlb0aLZInCH8KJlplfJ5qDCJUQwzr0OmYrlTp5 P8xSe+YWS9M4AgBX9pMFyhV0Eb549p3+vQ6MxTeVcY2+EVMM2O9WCkargpZjsl8Mi4mT h9Qg06sMJYvkRWaAYnNg3719kWPFj0WyPDE2lb43AgmeZvVv9EwtGBt06h5OhwqvQRXY H69BKe52fs3JLwTAB4gatjP0sk9Y9dIf/z5Y66BtyoRpw/jx1ND3rF7LJcaAjvzZtgjN pfUQ== X-Gm-Message-State: AOAM532YZkrxBd5nhkdFPTXbkyBagSrae35Z73DvvItn/QUOq+b11MWi AGbCPOYiMBzNhfwUvejxp8V7BLFrwZWKwsVBv/TxyftyH0g/guHJLb+GliBQLJzLyvj1MsAJvLS +ANCigMaQlgOt5hvv8WfK6EI08JcFjSfg3rPdJePbHIDFibpdRGETnkKv2FkfNFD9wm/WxfPH6S cFbeNnhBBMU2lB/5CeDgMEOFfDyWJZnHiGFlhRz1/YVk/SCUULNSOl X-Google-Smtp-Source: ABdhPJwnJH/i0Dtq2ll5e2F01u2wNDMIj/wxLod/ewmCMl64WXokQjHZ6AJ7I9Ty7E3Fb/Du35Mq4Q== X-Received: by 2002:a17:906:e2cd:: with SMTP id gr13mr2104803ejb.318.1640175117453; Wed, 22 Dec 2021 04:11:57 -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 p7sm817427edu.84.2021.12.22.04.11.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Dec 2021 04:11:57 -0800 (PST) Message-ID: <5c788a12-def6-daab-ac08-c006dfbaed7d@enterprisedb.com> Date: Wed, 22 Dec 2021 13:11:54 +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> From: Tomas Vondra In-Reply-To: <6597cf88-a499-e5be-e027-62753b197500@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/22/21 05:56, Fujii Masao wrote: > > > On 2021/12/22 10:57, Tomas Vondra wrote: >> >> >> On 12/19/21 04:03, Amit Kapila wrote: >>> On Sat, Dec 18, 2021 at 7:24 AM Tomas Vondra >>> wrote: >>>> >>>> while working on logical decoding of sequences, I ran into an issue >>>> with >>>> nextval() in a transaction that rolls back, described in [1]. But after >>>> thinking about it a bit more (and chatting with Petr Jelinek), I think >>>> this issue affects physical sync replication too. >>>> >>>> Imagine you have a primary <-> sync_replica cluster, and you do this: >>>> >>>>     CREATE SEQUENCE s; >>>> >>>>     -- shutdown the sync replica >>>> >>>>     BEGIN; >>>>     SELECT nextval('s') FROM generate_series(1,50); >>>>     ROLLBACK; >>>> >>>>     BEGIN; >>>>     SELECT nextval('s'); >>>>     COMMIT; >>>> >>>> The natural expectation would be the COMMIT gets stuck, waiting for the >>>> sync replica (which is not running), right? But it does not. >>>> >>> >>> How about if we always WAL log the first sequence change in a >>> transaction? >>> >> >> I've been thinking about doing something like this, but I think it >> would not have any significant advantages compared to using >> "SEQ_LOG_VALS 0". It would still have the same performance hit for >> plain nextval() calls, and there's no measurable impact on simple >> workloads that already write WAL in transactions even with >> SEQ_LOG_VALS 0. > > Just idea; if wal_level > minimal, how about making nextval_internal() > (1) check whether WAL is replicated to sync standbys, up to the page lsn > of the sequence, and (2) forcibly emit a WAL record if not replicated > yet? The similar check is performed at the beginning of > SyncRepWaitForLSN(), so probably we can reuse that code. > Interesting idea, but I think it has a couple of issues :-( 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. 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. 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. 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.) So I don't think this would actually help much. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company