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 1vdpfA-005NkO-2x for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 13:02:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdpf8-0022Ck-2P for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Jan 2026 13:02:51 +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.96) (envelope-from ) id 1vdpf8-0022Cc-1I for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 13:02:51 +0000 Received: from mail-dy1-x132a.google.com ([2607:f8b0:4864:20::132a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdpf6-005HJa-2A for pgsql-hackers@lists.postgresql.org; Thu, 08 Jan 2026 13:02:50 +0000 Received: by mail-dy1-x132a.google.com with SMTP id 5a478bee46e88-2ae57f34e22so330097eec.1 for ; Thu, 08 Jan 2026 05:02:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767877366; x=1768482166; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=j6/l0NI/VdFJPzqoUBqkO6pnAJmInIFBTIjN6WAubcU=; b=XqU7G4PRlosZKQ4npPUXufE40bz4CIKcqygec6N98ocGoLwR/yTIjtx9jHr2SOtNLQ rQYrsxLvE0pjAhTE7C/e8Da00BUTjTT9kWGUIl3YLmirPPUPRTFpK8sdsE18FtFhh5p6 UCFR+kkZi7Z5//N52M7NXt84E7eWQv8ckZHzip+xd89rQlrx2v9Xn97Pyk8xWUX+WLd3 qs2JkNT0wcFeGMX92gyhj1q1VhEy9wne8ZYC5k6waOWHMwR/uo5bYg5G+xg69rRkoFwU VOgddKZ1UZ/6QTyI0fwi01GfE24A8sGPGDKGVeAt2scQT2PnGv15AFKrtzYMFUIkcIz/ 9YWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767877366; x=1768482166; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=j6/l0NI/VdFJPzqoUBqkO6pnAJmInIFBTIjN6WAubcU=; b=gMHL1UTIaAV0UFDO2MvKZQWxBzMy6TG+nDxix6qOuCdMqJdUrg8ozAaUjICxs2RJsp H70u84b4C1WSUE+Ma5RHlp4tVTO9/TrzFc47ZIGEmBwiuZhCqdvpZdjh0ec087cXEhgO OIYX7Hn49UnALIiu5tTKFCLTxbudfmmGrhzmHQGYoZG9P+mV6GW4cXGjDc39g71ga7ZW Dnq8r6GLP24wisXw2gEddhw2rD95YnuaNN5K1hskDaywgu3KIC/AKxBCMGt+7GHQ9Reo bjjGWW+++669gXVK9pstBEzKIPoRcF5LYGKtdvoEI08xxQJSi3HzlcJiqjwN9Zgaymyj 18tA== X-Forwarded-Encrypted: i=1; AJvYcCXCoDvEx3vY7L43Unsz8u3CJ33k0lW0ggCXBC8LdXLQ103EB/BIaAHXdCxI415npRsJiZqgM2kM4FiW+mXL@lists.postgresql.org X-Gm-Message-State: AOJu0YzmJd1EylK/nmOcMF7RoZs2jtmrpez0woNHfw9GxXGrRysOgwHz l5f746k70NIH1edCZatr+ijDwBKXU2Et7uSk7wvCTrpn5AsJEWz0RP/n X-Gm-Gg: AY/fxX5mrabYusEh16kTaKwfDWRAAf04F9vYKxSaPcjr86Vywqd6iKUOW1KFrV0fyza Zaf6mK86Xg42YFauen9dlevzkMBZS6mw5EOwekVz9k6Cf4yBbXTReyzfAmgq9uI6vPeJiapJZ/j GAc1GaYjdqpwjb1jE9UaVMUxiyE1UjknuzOK1AuJAlT4CB/arCwuKcoUkgOCLwCNdZhf81K9K7X GfVsHiiyIwtWRIEE5bKGbQz+EplQGqC0X1AhowdXQ47tOpjdGnexeqsI9ruk0Wf9poVfLvatn/s jRWoB4RKwrEKPl3nNhZe4BiYdHdnE+lH34qST6SGeWUDgEe2+vBhuBYg4W0kkOc8lGET34ZnUpJ GtvqQ78BZOEkHJ0bSTkq/K6ZQQbH1Ku7tXd0tDHSgxq1ykg6tUq8pHOawOVzvXJQJ3aioI9CdXq JEZ4Xr/y2GFz2qnPvomg== X-Google-Smtp-Source: AGHT+IETMy/VVPhBd7kxOhxhKzDiR8/IxRd7rkQw/E7q8H15W8/bWJgXcBCqaerGiDBGXwuicYoEvw== X-Received: by 2002:a05:7301:7214:b0:2ae:b073:ae29 with SMTP id 5a478bee46e88-2b17d2ba9admr4862510eec.29.1767877365347; Thu, 08 Jan 2026 05:02:45 -0800 (PST) Received: from smtpclient.apple ([64.32.14.230]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b17078dd8fsm10443982eec.20.2026.01.08.05.02.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jan 2026 05:02:44 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Refactor replication origin state reset helpers From: Chao Li In-Reply-To: Date: Thu, 8 Jan 2026 21:02:11 +0800 Cc: Masahiko Sawada , =?utf-8?Q?=C3=81lvaro_Herrera?= , Postgres hackers Content-Transfer-Encoding: quoted-printable Message-Id: <349AF48A-06DE-4016-B23A-F4A2834484C3@gmail.com> References: <202512291435.5edkf3hnichv@alvherre.pgsql> <26279DAC-5ADA-4E30-B866-5DED0EEC2BEF@gmail.com> To: Ashutosh Bapat X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Jan 8, 2026, at 17:44, Ashutosh Bapat = wrote: >=20 > Hi Masahiko, >=20 > Thanks for updating the patches. Here are some more comments. >=20 > On Thu, Jan 8, 2026 at 7:17=E2=80=AFAM Masahiko Sawada = wrote: >>=20 >> On Wed, Jan 7, 2026 at 5:15=E2=80=AFPM Chao Li = wrote: >>=20 >> I've made some cosmetic changes to both patches (comments and the >> commit messages). Please review them and let me know what you think. >=20 > 0001 > ------- >=20 > +/* > + * 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; > +} >=20 > 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? >=20 > @@ -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); >=20 > 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.". >=20 > 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? Hi Ashutosh, Thanks for your follow-up review. IMO, we don=E2=80=99t have to no more on 0001. As 0002 is a big = refactoring, where we group the 3 global variables into a structure and = rename the help function, 0001 can be considered as a preparation commit = that does some simple cleanup. So that, we can put main focus on the = real refactoring in 0002. >=20 > 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); >=20 > 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. Thanks for pointing out the idea. I can look into that tomorrow. >=20 > /* > * 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. >=20 > 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. >=20 > @@ -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) > { >=20 > * 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) > { >=20 > /* 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) >=20 > 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? >=20 > replorigin_session_setup(originid, MyLogicalRepWorker->leader_pid); > - replorigin_session_origin =3D originid; > + replorigin_xact_state.origin =3D originid; >=20 > 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? >=20 > * 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.origin >=20 > I think just replorigin_xact_state should suffice for the sake of = brevity. >=20 > static void > on_exit_clear_state(int code, Datum arg) > { > - replorigin_session_clear(true); > + replorigin_xact_clear(true); >=20 > The prologue still states clear origin, but it should mention > transaction state instead. > } >=20 I will look into these as well tomorrow. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/