Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8Ll3-009ROV-K7 for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 14:41:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r8Ll0-00B4Fy-8s for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2023 14:41:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8Lkz-00B4Fq-TF for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 14:41:41 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r8Lkr-009BYW-MH for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2023 14:41:40 +0000 Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a00a9c6f283so918580966b.0 for ; Wed, 29 Nov 2023 06:41:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1701268892; x=1701873692; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Udw1DpvjQOJMv7Hohb/FkgzeaGzlRDTC384y6V/jQNs=; b=LFtCPK0nfdhXoVzy0pfU6IQ4j5K3VEJI7jizptvaBjcFWEUcvNITIH8P8y+wVPwAbz cE+KT/2JeO6xaxU+vc1NFrz/6EmB4ChzjNvZPPIPII1siXDv2xTpYhd+DyXXQByEjdzZ 1SSqbnFuN7LOmha50T2OIskcwJy9I4iNGn/YcDbChpBYk4e27yanH36tPVnLn++ceQiT jdBM2bGrOWP74m2L+e+1mOTN/TsYsX65a2FiS2tBTl7US3PqF14vDG09G+93gg5YLQ25 1uZZJctno109BOM5dRG8z/ho2BA1r8o9zPmFygzDVUft4YvhXG2ur0SXr5d61UiwjScw 7hHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701268892; x=1701873692; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Udw1DpvjQOJMv7Hohb/FkgzeaGzlRDTC384y6V/jQNs=; b=F8zIIhbGErXOZw11lmvI+E+gu7/5zE3UOsPjtD3rIpBPhPC6032QJ5W+bo9TistETj 0EDHmPTP9kyYo/OZxvwsk+WvimilL8eb2w/dC73aOsfGi6D8KKDp2GJYSwPU6QIJdZmG Yk6BtHsbYMR6fz8Yzs9FPbslMAG8JOIlVXaxJoqKMXabheuCaK2OqOywym3OCX+6Zher zJjp6IHgL1wZGMkwn8ZeiUsCOt13myWsFBXfxduj42h7z1LaEMGn0k2PD8JItvL0jzWc zBFkc581SicUTuEbqCaT9GI0u1DiTq9hTDrlgrzfI1Q4gRHiN6zKCZOIg3j9A2skiV6d TyvA== X-Gm-Message-State: AOJu0Yx173Ac7zqHLLAGvY1EFhlkTBiXu/nDeoOAxGsNSQZ8bObaxSR1 F7/6IfjxT3lg1cNxgmliMvxZUw== X-Google-Smtp-Source: AGHT+IEGg0Quk9NeYbu7DA+jYhdIBXz3M0341lVTzQUZ79rJW9QfnfGM2GbF80hdcGfZh8kSVCGB5Q== X-Received: by 2002:a17:906:749b:b0:a17:7750:d989 with SMTP id e27-20020a170906749b00b00a177750d989mr1285049ejl.12.1701268892045; Wed, 29 Nov 2023 06:41:32 -0800 (PST) Received: from [10.137.0.17] (srv1.mobissw.com. [89.235.0.226]) by smtp.gmail.com with ESMTPSA id b20-20020a170906195400b009a1dbf55665sm7928298eje.161.2023.11.29.06.41.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Nov 2023 06:41:31 -0800 (PST) Message-ID: <418ff5ac-1664-9038-8ac6-0dd308b5df07@enterprisedb.com> Date: Wed, 29 Nov 2023 15:41:30 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: logical decoding and replication of sequences, take 2 Content-Language: en-US To: Amit Kapila Cc: "Hayato Kuroda (Fujitsu)" , "Zhijie Hou (Fujitsu)" , Ashutosh Bapat , PostgreSQL Hackers , Masahiko Sawada , Peter Eisentraut , Dilip Kumar References: <3c145928-9cf9-eb15-6b24-c84d791a12c0@enterprisedb.com> <28e0279f-d02a-f7d7-568d-13f70ee8f60b@enterprisedb.com> <652f6e81-f71b-b760-b1bb-ab078b8dbe53@enterprisedb.com> <36f3a200-f185-887c-da08-fdc866436e6c@enterprisedb.com> <195be236-7580-72c6-97c7-59f06c4e093c@enterprisedb.com> <4954a4f8-ba9d-d352-571b-7b94372ea5ae@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/29/23 14:42, Amit Kapila wrote: > On Wed, Nov 29, 2023 at 2:59 AM Tomas Vondra > wrote: >> >> I have been hacking on improving the improvements outlined in my >> preceding e-mail, but I have some bad news - I ran into an issue that I >> don't know how to solve :-( >> >> Consider this transaction: >> >> BEGIN; >> ALTER SEQUENCE s RESTART 1000; >> >> SAVEPOINT s1; >> ALTER SEQUENCE s RESTART 2000; >> ROLLBACK TO s1; >> >> INSERT INTO seq_test SELECT nextval('s') FROM generate_series(1,40); >> COMMIT; >> >> If you try this with the approach relying on rd_newRelfilelocatorSubid >> and rd_createSubid, it fails like this on the subscriber: >> >> ERROR: could not map filenode "base/5/16394" to relation OID >> >> This happens because ReorderBufferQueueSequence tries to do this in the >> non-transactional branch: >> >> reloid = RelidByRelfilenumber(rlocator.spcOid, rlocator.relNumber); >> >> and the relfilenode is the one created by the first ALTER. But this is >> obviously wrong - the changes should have been treated as transactional, >> because they are tied to the first ALTER. So how did we get there? >> >> Well, the whole problem is that in case of abort, AtEOSubXact_cleanup >> resets the two fields to InvalidSubTransactionId. Which means the >> rollback in the above transaction also forgets about the first ALTER. >> Now that I look at the RelationData comments, it actually describes >> exactly this situation: >> >> * >> * rd_newRelfilelocatorSubid is the ID of the highest subtransaction >> * the most-recent relfilenumber change has survived into or zero if >> * not changed in the current transaction (or we have forgotten >> * changing it). This field is accurate when non-zero, but it can be >> * zero when a relation has multiple new relfilenumbers within a >> * single transaction, with one of them occurring in a subsequently >> * aborted subtransaction, e.g. >> * BEGIN; >> * TRUNCATE t; >> * SAVEPOINT save; >> * TRUNCATE t; >> * ROLLBACK TO save; >> * -- rd_newRelfilelocatorSubid is now forgotten >> * >> >> The root of this problem is that we'd need some sort of "history" for >> the field, so that when a subxact aborts, we can restore the previous >> value. But we obviously don't have that, and I doubt we want to add that >> to relcache - for example, it'd either need to impose some limit on the >> history (and thus a failure when we reach the limit), or it'd need to >> handle histories of arbitrary length. >> > > Yeah, I think that would be really tricky and we may not want to go there. > >> At this point I don't see a solution for this, which means the best way >> forward with the sequence decoding patch seems to be the original >> approach, on the decoding side. >> > > One thing that worries me about that approach is that it can suck with > the workload that has a lot of DDLs that create XLOG_SMGR_CREATE > records. We have previously fixed some such workloads in logical > decoding where decoding a transaction containing truncation of a table > with a lot of partitions (1000 or more) used to take a very long time. > Don't we face performance issues in such scenarios? > I don't think we do, really. We will have to decode the SMGR records and add the relfilenodes to the hash table(s), but I think that affects the lookup performance too much. What I think might be a problem is if we have many top-level transactions, especially if those transactions do something that creates a relfilenode. Because then we'll have to do a hash_search for each of them, and that might be measurable even if each lookup is O(1). And we do the lookup for every sequence change ... > How do we see this work w.r.t to some sort of global sequences? There > is some recent discussion where I have raised a similar point [1]. > > [1] - https://www.postgresql.org/message-id/CAA4eK1JF%3D4_Eoq7FFjHSe98-_ooJ5QWd0s2_pj8gR%2B_dvwKxvA%40mail.gmail.com > I think those are very different things, even though called "sequences". AFAIK solutions like snowflakeID or UUIDs don't require replication of any shared state (that's kinda the whole point), so I don't see why would it need some special support in logical decoding. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company