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 1myOvG-0000rc-Si for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Dec 2021 01:54:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1myOvE-0000jw-4g for pgsql-hackers@arkaria.postgresql.org; Sat, 18 Dec 2021 01:54:04 +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 1myOvD-0000jm-HU for pgsql-hackers@lists.postgresql.org; Sat, 18 Dec 2021 01:54:03 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1myOv4-0004Xx-JR for pgsql-hackers@lists.postgresql.org; Sat, 18 Dec 2021 01:54:02 +0000 Received: by mail-ed1-x531.google.com with SMTP id z5so14509124edd.3 for ; Fri, 17 Dec 2021 17:53:53 -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:content-language:to:from :subject:content-transfer-encoding; bh=zJMhQqQDStRr9oaMnA4OWc2zNyBW8qvBfln5em5zbEI=; b=AnekNomsy3Pk11g3U4619Gz3zaR7wZ71QeufY0X09+LnbQ2ZSmtJsmaYC7Vtxt4xUm L7dPXivJIQkJd3dheG0YzuxEY7FUPjET8WJbQwY+zkrhE0ur44YVaSANnvHrpJGOjgkk b3CKQYib962n5cPZzpUI4T0ho7ngLi583ntyx1IU6ADYr5uQ7Gf6VWqOOSeL8g0/5KT0 WNDA0y+VxZjT/1voHOGg7BP7rdcVY/bvkqJKX1Yw7cjWmOEkZe99/CRduo1u/e7w7yE8 LOkuwKC7NjkMAwqz6lGTcWPu5R2Y7vBpUaznusTkPHEPNHpPA4n1ZbzkNoLRY1iLDtyT RsHw== 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 :content-language:to:from:subject:content-transfer-encoding; bh=zJMhQqQDStRr9oaMnA4OWc2zNyBW8qvBfln5em5zbEI=; b=uUFarDHFOU1NYASBBmzoOli7SEVcGjcfjhgavlNam3AYBwZNdqIMbSuxxhonOYN60T VF06FDU46ZAiNfqBw+cCJ2atyQmG6ACv8HuL/ldThdTkQ/hEfTt3z1yDlG/mFqV06K7I ItvhbUn2spmTYlgGxijWXw22rGRymrUQUxWbOhBvqBhik46YjHMHVKCdr4E1NmRqWJNK Sba6yLHtMKXOI9K+4jWjnv/DwWBrNVefOtjjEcMsnoHWBhBWtYcFC2RB0ShYBK3RksNU Uakte9jqtnTWZCT9CwHJWzAuxOP4+QARTCDovXcrai89gziNJyx1z3KvNmq8Oyu+HzGo OmNA== X-Gm-Message-State: AOAM530scFI05dXdtjM48b6oUehwUQcmlXtYzyouJOWy/F/wHr7FdtL5 RYXFq49ESBAZPfupJ7Q2ooEK2tJgv+ClBPlz/3caaMpNHC98nzVosMrOT+v3zc9NKsEH2/MIZMK MsQwp8nskynT6dqETtCJgTOSN74lhl5Yqi6KKVBrn9BXS3HEUefbvDLcSNAUOu0WNDm9x9D8c6N Mb1CLNWf/ChXQNwPwIrc+TqxBD2Y2gWDfjZxlw/xQkWgJcuvfZFu6qNa9RJyPbGAqE9oP0kLkCH xFPlYXBZ0OhqMnYPhyar9KJQKdPJgjrb0T6XhKnA9CHfbP5ywSWiA== X-Google-Smtp-Source: ABdhPJwO3jg1nuTVz4WpXq5Y3R2QdlmqphOHSkHBbhXXtQBJtkzyBHqqiFBtYEYAJIdafiHzMGKMGA== X-Received: by 2002:a17:906:b04c:: with SMTP id bj12mr4532081ejb.716.1639792433136; Fri, 17 Dec 2021 17:53:53 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-251-241.net.upcbroadband.cz. [86.49.251.241]) by smtp.gmail.com with ESMTPSA id dm7sm23146edb.7.2021.12.17.17.53.52 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Dec 2021 17:53:52 -0800 (PST) Message-ID: <712cad46-a9c8-1389-aef8-faf0203c9be9@enterprisedb.com> Date: Sat, 18 Dec 2021 02:53:49 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Content-Language: en-US To: PostgreSQL Hackers From: Tomas Vondra Subject: sequences vs. synchronous replication 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 Hi, 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. The problem is exactly the same as in [1] - the aborted transaction generated WAL, but RecordTransactionAbort() ignores that and does not update LogwrtResult.Write, with the reasoning that aborted transactions do not matter. But sequences violate that, because we only write WAL once every 32 increments, so the following nextval() gets "committed" without waiting for the replica (because it did not produce WAL). I'm not sure this is a clear data corruption bug, but it surely walks and quacks like one. My proposal is to fix this by tracking the lsn of the last LSN for a sequence increment, and then check that LSN in RecordTransactionCommit() before calling XLogFlush(). regards [1] https://www.postgresql.org/message-id/ae3cab67-c31e-b527-dd73-08f196999ad4%40enterprisedb.com -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company