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 1naIn6-0007ns-Cb for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Apr 2022 15:02:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1naIn5-0004YR-62 for pgsql-hackers@arkaria.postgresql.org; Fri, 01 Apr 2022 15:02:19 +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 1naIn4-0004Xc-De for pgsql-hackers@lists.postgresql.org; Fri, 01 Apr 2022 15:02:18 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1naIn1-0007y7-Ch for pgsql-hackers@lists.postgresql.org; Fri, 01 Apr 2022 15:02:17 +0000 Received: by mail-ej1-x62d.google.com with SMTP id j15so6399567eje.9 for ; Fri, 01 Apr 2022 08:02:15 -0700 (PDT) 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=vw1lr5b4VgZ1lShG6FXo9TevR5MW3dPIgjrgYEfRG2E=; b=ItW+XO0jVvnXi2/j4vXRd1TTC6jTGJQewadOgS+DLZY1fiHc9cr+l2ZS4Ka4T1Nwy5 cX2pxrUrwQbVgwWcHBeHR7nO/UoJRr23fQmz+w0IsleTzVD/sxDPc1r+5Lv8LH9RPNI1 Erkh75bQj8lTq5upZT8QBrRq/jn1z8ZgzNbNwanfXl4k8/5lSGvR+6IyYtrKhIbSj9/Z ui41lBKRFti+mr9Ij3c8qdVBVYAfpIMHrJ8hUHleKqUF9dfKcUycYeaup77EOt90Zvvo +FWNvs/KQWxaZix7VF5BCF4+xIH2zXbOERx9VsBFKMBFREtAj5ybcjBknIhBdwleMJpi qcxw== 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=vw1lr5b4VgZ1lShG6FXo9TevR5MW3dPIgjrgYEfRG2E=; b=XB2lwAQKWvAtAOmaI/nX9xgB20W75KfISmdLuxOY5rssXXz+2Tz+YKgmsLfln+SDhg qgfnsL9zd4I3dl2+duDCcFx26qvLCJ+aPR3lrt/R9DgXNUuEllyxFKVFLHvthjUfqo+6 kY/sXLb3KLp3wuLgtAE1w29cvGJLnVx6Vo/t10+da1pBR4zWcCg4V67rZIcbGdndQhna Trmp5wzJq8GtY9+BdsWIGsEINijiuDknaq8VOYKX9iHVLBuz0zdo4wri5ht8zoOxKB5g oAfGJRVcRmykzHXMwQxF3KEZTRDPs2zpjGLtxKoUg5BxtpRRCKe69RdzaaCh70ySETFM 3gwA== X-Gm-Message-State: AOAM533n8+D5h2c7f8ctsbb8UGbUaWBI6UNUHyXFWSVc/7HGkbHsbW/m geIvcaxUC0EL/xwro+uwDKoWrYL0rh7pfcbmsNlJhSaH+sfXbzGXsd/PIuPs1Niyi5J+OJ+dR/2 JVJLz+BbYellZf4RZ60YCMR3aQ04nr4FXZ2wHdDnIdJX6EOVczWp/RNRCGZPhHt7Fo8ZCqg3Vz0 nAozaYqAuZPIq5OySrQpHZ8+Mq2cuTvLBo1KQcBMz2X6glsItT2bjBFAMpc8Bo X-Google-Smtp-Source: ABdhPJyaJ5NE3SCY2gT3OapgZZK1naGL6k7D2ygLV+5nmcNfqv++IxIUzosXnjv4+lGvzIUS2Eg+kw== X-Received: by 2002:a17:906:7307:b0:6da:92e1:9c83 with SMTP id di7-20020a170906730700b006da92e19c83mr165132ejc.459.1648825333739; Fri, 01 Apr 2022 08:02:13 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-250-165.net.upcbroadband.cz. [86.49.250.165]) by smtp.gmail.com with ESMTPSA id i25-20020a056402055900b004191a652e3bsm1293085edx.30.2022.04.01.08.02.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Apr 2022 08:02:12 -0700 (PDT) Message-ID: Date: Fri, 1 Apr 2022 17:02:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: logical decoding and replication of sequences Content-Language: en-US To: Amit Kapila Cc: Petr Jelinek , Peter Eisentraut , PostgreSQL Hackers References: <9da788c7-01eb-f11d-aa08-c5f61fe9f33a@enterprisedb.com> <456A528F-8F19-41C7-9857-927024D32396@enterprisedb.com> <74729cc5-960b-9295-912a-a7c37356a0c4@enterprisedb.com> <9d7635f8-2402-e038-64da-cbc3732ce8cf@enterprisedb.com> <83c682bf-7164-5976-608c-385cc394bb6c@enterprisedb.com> <494bb2d9-0c1c-b429-e6bb-231f00f2c36f@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 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 On 3/28/22 07:29, Amit Kapila wrote: > ... > > While thinking about this, I think I see a problem with the > non-transactional handling of sequences. It seems that we will skip > sending non-transactional sequence change if it occurs before the > decoding has reached a consistent point but the surrounding commit > occurs after a consistent point is reached. In such cases, the > corresponding DMLs like inserts will be sent but sequence changes > won't be sent. For example (this scenario is based on > twophase_snapshot.spec), > > Initial setup: > ============== > Create table t1_seq(c1 int); > Create Sequence seq1; > > Test Execution via multiple sessions (this test allows insert in > session-2 to happen before we reach a consistent point and commit > happens after a consistent point): > ======================================================================================================= > > Session-2: > Begin; > SELECT pg_current_xact_id(); > > Session-1: > SELECT 'init' FROM pg_create_logical_replication_slot('test_slot', > 'test_decoding', false, true); > > Session-3: > Begin; > SELECT pg_current_xact_id(); > > Session-2: > Commit; > Begin; > INSERT INTO t1_seq SELECT nextval('seq1') FROM generate_series(1,100); > > Session-3: > Commit; > > Session-2: > Commit 'foo' > > Session-1: > SELECT data FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, > 'include-xids', 'false', 'skip-empty-xacts', '1'); > > data > ---------------------------------------------- > BEGIN > table public.t1_seq: INSERT: c1[integer]:1 > table public.t1_seq: INSERT: c1[integer]:2 > table public.t1_seq: INSERT: c1[integer]:3 > table public.t1_seq: INSERT: c1[integer]:4 > table public.t1_seq: INSERT: c1[integer]:5 > table public.t1_seq: INSERT: c1[integer]:6 > > > Now, if we normally try to decode such an insert, the result would be > something like: > data > ------------------------------------------------------------------------------ > sequence public.seq1: transactional:0 last_value: 33 log_cnt: 0 is_called:1 > sequence public.seq1: transactional:0 last_value: 66 log_cnt: 0 is_called:1 > sequence public.seq1: transactional:0 last_value: 99 log_cnt: 0 is_called:1 > sequence public.seq1: transactional:0 last_value: 132 log_cnt: 0 is_called:1 > BEGIN > table public.t1_seq: INSERT: c1[integer]:1 > table public.t1_seq: INSERT: c1[integer]:2 > table public.t1_seq: INSERT: c1[integer]:3 > table public.t1_seq: INSERT: c1[integer]:4 > table public.t1_seq: INSERT: c1[integer]:5 > table public.t1_seq: INSERT: c1[integer]:6 > > This will create an inconsistent replica as sequence changes won't be > replicated. Hmm, that's interesting. I wonder if it can actually happen, though. Have you been able to reproduce that, somehow? > I thought about changing snapshot dealing of > non-transactional sequence changes similar to transactional ones but > that also won't work because it is only at commit we decide whether we > can send the changes. > I wonder if there's some earlier LSN (similar to the consistent point) which might be useful for this. Or maybe we should queue even the non-transactional changes, not per-transaction but in a global list, and then at each commit either discard inspect them (at that point we know the lowest LSN for all transactions and the consistent point). Seems complex, though. > For the transactional case, as we are considering the create sequence > operation as transactional, we would unnecessarily queue them even > though that is not required. Basically, they don't need to be > considered transactional and we can simply ignore such messages like > other DDLs. But for that probably we need to distinguish Alter/Create > case which may or may not be straightforward. Now, queuing them is > probably harmless unless it causes the transaction to spill/stream. > I'm not sure I follow. Why would we queue them unnecessarily? Also, there's the bug with decoding changes in transactions that create the sequence and add it to a publication. I think the agreement was that this behavior was incorrect, we should not decode changes until the subscription is refreshed. Doesn't that mean can't be any CREATE case, just ALTER? > I still couldn't think completely about cases where a mix of > transactional and non-transactional changes occur in the same > transaction as I think it somewhat depends on what we want to do about > the above cases. > Understood. I need to think about this too. >>> At all places, it is mentioned >>> as creating a sequence for transactional cases which at the very least >>> need some tweak. >>> >> >> Which places? >> > > In comments like: > a. When decoding sequences, we differentiate between sequences created > in a (running) transaction and sequences created in other (already > committed) transactions. > b. ... But for new sequences, we need to handle them in a transactional way, .. > c. ... Change needs to be handled as transactional, because the > sequence was created in a transaction that is still running ... > > It seems all these places indicate a scenario of creating a sequence > whereas we want to do transactional stuff mainly for Alter. > Right, I'll think about how to clarify the comments. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company