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.96) (envelope-from ) id 1vdmZe-004g0g-0P for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 09:44:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdmZb-001MaW-2p for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 09:44:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdmZb-001MaO-1Q for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 09:44:56 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdmZa-004rg7-2W for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 09:44:55 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-477ba2c1ca2so32264955e9.2 for ; Thu, 08 Jan 2026 01:44:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767865491; x=1768470291; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=SkLZwwQrvL4f2KLdKyEQy4bu9jW9MdjzwqFksAZlVRs=; b=cv0q7CCHOUJnDBdpKw47Ff+CvOqCyEzKMh2Po3pq8TRqzuTTur3C4S5plUMvaNkOM2 Ps05JB/CNppIttxqGTqpcjCeJDXZeFKB1OeTznNMuk9PNfnqgGgb2A6Ah18/W4dDtO0/ cpovrEhUSgArRlGRothCWzPBoVq1PIZ+Rjxh4ARu9tFgf/e5sw5rd570Yqrvk2lyNslH fxJ5G6GU0hF4kOTfeV5BqpSpJuJUVgjgpzAT2XJOut8vP9IGOfY2mAHLwA0+iEV4/5Yi T5fgfvAiTqGKD0PG+mZVguFxPh1uuuaVze4qSD1FkxeFotftiUe8YT54PgO3StnfvsNX I2PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767865491; x=1768470291; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=SkLZwwQrvL4f2KLdKyEQy4bu9jW9MdjzwqFksAZlVRs=; b=s6lwLGkF1R11iXfWWqGUvOQCss2U/L4iWp++vIkQKfjUJ+ZDR5FpEWc6966uXpozn9 iLH2fRSKUijdkOjNJNI8m5/1fEW1WDVnrMIMDyLKdNayAqznHa9J3pWGg2METqeG63Re 9W3iNp5SX4ET/pw2cNFnX5wwpg+LDs3fYvx8OQBiXJczmhnTbaE6ldL0/omx9+6m8XQs jjwX4r2xiehFfl5KGx0mkNzssmagWNZmTFi0iRQFxYYNeXiOdCeY8y4dfk7ZBN7GdLF0 rOlyPPOA5mHT430lnDRwB4GwlJn/VtLNe0Uzgd9QX5Y5drwau33PXJpi21ncnFfYfqqX /CeA== X-Forwarded-Encrypted: i=1; AJvYcCWiUmZwtzBU/zPHa6/BuxzEHxDZvY7er5aJKTDNslpxCdPc9IvuJdbVek3iIG6IIdXbnWdYYpQC1P43cV/B@lists.postgresql.org X-Gm-Message-State: AOJu0YwEhJ7r6MDjJT3Q6joXNufyIqNvNu3PPzolwRE3gjAfEuoC3eJI V2As8iC2lB/rHMVOA7gTDbiJzqrYxAKb3WJgMGKerM6iKV/8qicnAGbaRbTL1moAA0xTy8YXZdC +uR6GMySzBawS5ij/EJuOi7U7K6EcHp8= X-Gm-Gg: AY/fxX7UTSBkICOhMPJrk/WMmSjt72xGLx5ZVUvuCCsFsfZ0wzB6ClhY0jrABtcwICy 1rFw6F8NGQjrS/lLzgPLn23bC/rxAWslxJpmXgFUP1039JBFScOBCuG0rHrIg5f0NbR8kEr9ZeO rrnyaYgo7DughHWWCTYkwcej//hSmmVShq6iL9/zsyElOFZtNs8UaECs4FDfPTzj9HSF+aPoaHI TbM7lBxN8IPBC2A9m+Oq/x5CZA8fzTn16xqQvq+tldu8e8cNz21yzoOrnKWz5IGKZhuMH+40gw7 uDG7HYY= X-Google-Smtp-Source: AGHT+IGKlb38X+JKpS1AcLaKG5VJoet0BKY3H1qRr/hCWL2qpkZWgE3gOBR7paHljvD4/OPCKtoGocFecL/PyIc+XKc= X-Received: by 2002:a05:600c:a48:b0:477:9eb8:97d2 with SMTP id 5b1f17b1804b1-47d84b08652mr65579645e9.8.1767865491290; Thu, 08 Jan 2026 01:44:51 -0800 (PST) MIME-Version: 1.0 References: <202512291435.5edkf3hnichv@alvherre.pgsql> <26279DAC-5ADA-4E30-B866-5DED0EEC2BEF@gmail.com> In-Reply-To: From: Ashutosh Bapat Date: Thu, 8 Jan 2026 15:14:38 +0530 X-Gm-Features: AQt7F2p8PFn1A8TLfJ6AktfTIdrh7thbKE4Du-4JGZcFUsxbMhYm6dTOCVy4-vE Message-ID: Subject: Re: Refactor replication origin state reset helpers To: Masahiko Sawada Cc: Chao Li , =?UTF-8?Q?=C3=81lvaro_Herrera?= , Postgres hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Masahiko, Thanks for updating the patches. Here are some more comments. On Thu, Jan 8, 2026 at 7:17=E2=80=AFAM Masahiko Sawada wrote: > > On Wed, Jan 7, 2026 at 5:15=E2=80=AFPM Chao Li w= rote: > > I've made some cosmetic changes to both patches (comments and the > commit messages). Please review them and let me know what you think. 0001 ------- +/* + * Clear session replication origin state. + * + * replorigin_session_origin is also cleared if clear_origin is set. + */ +void +replorigin_session_clear(bool clear_origin) +{ + replorigin_session_origin_lsn =3D InvalidXLogRecPtr; + replorigin_session_origin_timestamp =3D 0; + if (clear_origin) + replorigin_session_origin =3D InvalidRepOriginId; +} All the other replorigin_session_* functions deal with session_replication_state, but this function does not deal with it. I see that in the next patch this function has been renamed as replorigin_xact_clear() which seems more appropriate. Do you intend to squash these two patches when committing? @@ -1482,8 +1493,8 @@ pg_replication_origin_xact_reset(PG_FUNCTION_ARGS) { replorigin_check_prerequisites(true, false); - replorigin_session_origin_lsn =3D InvalidXLogRecPtr; - replorigin_session_origin_timestamp =3D 0; + /* Clear only origin_lsn and origin_timestamp */ + replorigin_session_clear(false); The comment can explain why we are not clearing replorigin_session_origin here. Something like "This function is cancel the effects of pg_replication_origin_xact_setup(), which only sets origin_lsn and origin_timestamp, so we only clear those two fields here.". Next comment does not apply to this patch, but the inconsistency I am speaking about becomes apparent now. This function resets the state setup by pg_replication_origin_xact_setup(), which checks for session_replication_state being non-NULL. So I expected pg_replication_origin_xact_reset() also to check for the same condition or at least Assert it. Why is it not doing so? 0002 ------- - replorigin =3D (replorigin_session_origin !=3D InvalidRepOriginId && - replorigin_session_origin !=3D DoNotReplicateId); + replorigin =3D (replorigin_xact_state.origin !=3D InvalidRepOriginId && + replorigin_xact_state.origin !=3D DoNotReplicateId); There's another small deduplication opportunity here. Define function replorigin_xact_origin_isvalid() to check these two conditions and use that function here and in other places like RecordTransactionCommitPrepared(). I would go as far as making the function static inline, and use it instead of replorigin variable, whose name is certainly misleading - it doesn't talk about transaction at all. With static inline there, optimizer may be able to eliminate the multiple function call overhead. /* * Record commit timestamp. The value comes from plain commit timestamp * if replorigin is not enabled, or replorigin already set a value for us - * in replorigin_session_origin_timestamp otherwise. + * in replorigin_xact_state.origin_timestamp otherwise. suggestion "Record commit timestamp. Use one in replorigin_xact_state if set, otherwise use plain commit timestamp.". This reads better and is closer to the code.". If you agree, please change other similar comments too. @@ -5928,12 +5928,12 @@ XactLogCommitRecord(TimestampTz commit_time, } /* dump transaction origin information */ - if (replorigin_session_origin !=3D InvalidRepOriginId) + if (replorigin_xact_state.origin !=3D InvalidRepOriginId) { * Dump transaction origin information. We need this during recovery to * update the replication origin progress. */ - if (replorigin_session_origin !=3D InvalidRepOriginId) + if (replorigin_xact_state.origin !=3D InvalidRepOriginId) { /* followed by the record's origin, if any */ if ((curinsert_flags & XLOG_INCLUDE_ORIGIN) && - replorigin_session_origin !=3D InvalidRepOriginId) + replorigin_xact_state.origin !=3D InvalidRepOriginId) Not a change in this patch but the refactoring makes it more visible. What about the case of replorigin_session_origin =3D=3D DoNotReplicateId? Should there be a comment explaining why it's excluded here? replorigin_session_setup(originid, MyLogicalRepWorker->leader_pid); - replorigin_session_origin =3D originid; + replorigin_xact_state.origin =3D originid; replorigin_session_setup() is always followed by replorigin_xact_state.origin assignment. abort/commit handlers set the other two members. Do we want to create replorigin_xact_set_origin() and replorigin_xact_set_lsn_timestamp() functions to encapsulate these assignments? Those two will serve as counterparts to replorigin_xact_clear(). Or would that be an overkill? * successfully and that we don't need to do so again. In combination with - * setting up replorigin_session_origin_lsn and replorigin_session_origin + * setting up replorigin_xact_state.origin_lsn and replorigin_xact_state.o= rigin I think just replorigin_xact_state should suffice for the sake of brevity. static void on_exit_clear_state(int code, Datum arg) { - replorigin_session_clear(true); + replorigin_xact_clear(true); The prologue still states clear origin, but it should mention transaction state instead. } --=20 Best Wishes, Ashutosh Bapat