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 1veKfE-00BzOB-0O for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2026 22:09:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1veKfD-008tlc-03 for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2026 22:08:59 +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 1veKfC-008tlR-1u for pgsql-hackers@lists.postgresql.org; Fri, 09 Jan 2026 22:08:59 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1veKfB-0058c7-2M for pgsql-hackers@lists.postgresql.org; Fri, 09 Jan 2026 22:08:58 +0000 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-59b8466b4a8so475109e87.1 for ; Fri, 09 Jan 2026 14:08:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767996536; x=1768601336; 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=/zXuCg6bV0GZm2CcI4V8pZVcJ+NyMq/w3lyeBi2iLnQ=; b=nmiDBpsNPi1ZCsc8jPLLWRDRzXCGo+JCo39xB3tp6cHl+ArrZuyN0MPbLKMOkr6n7v Stc/jTsXRBmbyJUUUSRtrWfjO8BF4C+LDIgJXBUZ8JfIYINgwttUSKUyGDyLxctUs1fg bl8u9t3fV26JnSj4QuG4UsvrmuGlGLTP/nZ8TgaISm0Dox8AIIIqZeo18V2K9lYVDv79 rA9ZegVKrtxG1cJURczphWq2GShA5JHRv35/DobUeNIWsgnU0ZRpr92NIoXLdX8lInQj 8tYzLhybaUton5hbeKhdJuupSr9tNQJJMtO8Wt7WegCeclNB8UR2iZn4BbKzIAsJH9Oh 0V8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767996536; x=1768601336; 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=/zXuCg6bV0GZm2CcI4V8pZVcJ+NyMq/w3lyeBi2iLnQ=; b=EYRNX2uEe4BuuXLmjqiOqfO4OQNxg7iQIZevJ6xDlZdOLO2r0/ZXF8cRJDLvWd28IE nfsa4DCA2RVnsJDISaYFE4Zmgjpcx/F8gMs2SOZMyEXyU1glz45ILUfGQx8Edq7Zm/cn SRNaUT2OOkbS0NwsIlvheWzAZPH7vg8SkSYXxIwt+fkcN4Ej6Fx0rrCZqwymT4+fov51 F7Sy5HJMp6jOlJsMca3OfqYCVuU9MxO5nLNGyY+ItfOsLlfQGPk/63SiORCgwNCymiWL iK652Qd/Wy3y1fp07ghBNatio7GKM1/NR9LBdLP9/jjw2f5gcA+Ld6kdpdpmwwBqPfKw Owjg== X-Forwarded-Encrypted: i=1; AJvYcCUUP/E092VlVJlCfR7Z1GE4Yk1U+hO+3V+Ih1sR2kx5W3enKXoDmuWcf7UYVVIe2cxhaeroBrlruBKAgu/D@lists.postgresql.org X-Gm-Message-State: AOJu0Ywe7dnR2Qs9VoApGumfEuSypsPrV7FfGaT6APTzY1WoJB4CSIy+ Ew9b7pehPa/ZIXrXB6NDq21WrXV1jxeOgYL+TjPkops01ZgUGjJF7eHHy5IvjUb7SvpBsWTVzp0 tvGYfHvv6VBAqdX5qSkb1ejzFI7tfh5Q= X-Gm-Gg: AY/fxX4Er6EMhmB3QTEQaU1PgZDCzQ5TtOuv77VbAhqxYrLCLYJ/dQFFarvVaWtDbv+ c9nVhP9/ec8FIHWjMTuSUBMdu+LBkxvsAeG5rwL2/4xKoh4leLCrW4OE4oNBFk7VZHSdXFS60jX 575BQ1Y7hWuIHRZTLrEbhpKcC8AElZ9mv3t+YJkoRq8wQj5b0QtNLuKP9f45HCyRYRO+x+Y5IHy 2SSgf/4e6YgnuBl1RCEqjNiFg9oN7UkfheGFVtlLhtj7/A0ldhLTHliXChN0kCwJPqG6wAH X-Google-Smtp-Source: AGHT+IHbnk96YABPruki1zC1q/qpetN41HARsj2eT4Oh6AedX9ZN7lN5ZmT+Vw/das5CeT60Uw4xs0smM3Gs8UBh23A= X-Received: by 2002:a05:6512:3d05:b0:59a:10d9:71b with SMTP id 2adb3069b0e04-59b6f02829amr3888153e87.20.1767996535369; Fri, 09 Jan 2026 14:08:55 -0800 (PST) MIME-Version: 1.0 References: <202512291435.5edkf3hnichv@alvherre.pgsql> <26279DAC-5ADA-4E30-B866-5DED0EEC2BEF@gmail.com> In-Reply-To: From: Masahiko Sawada Date: Fri, 9 Jan 2026 14:08:19 -0800 X-Gm-Features: AZwV_QhQGMwRnwjrbCIqJp6S68ND_1ho6yrQLzRay_VTAvwRIQjFtqANh5BKYb8 Message-ID: Subject: Re: Refactor replication origin state reset helpers To: Chao Li Cc: Ashutosh Bapat , =?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 On Thu, Jan 8, 2026 at 10:50=E2=80=AFPM Chao Li wr= ote: > > > > On Jan 8, 2026, at 17:44, Ashutosh Bapat w= rote: > > 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? > > > Renamed the help function to the same name as in 0002. > > > @@ -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.". > > > Updated the comment in 0001. > > > 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? > > > Added the same check in 0002. > > > 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. > > > Good point. Added replorigin_xact_origin_isvalid() in 0002 as you suggest= ed. > > > /* > * 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. > > > Agreed. Updated the comment in both places. > > > @@ -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? > > > Added a comment to the 3 places. > > 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? > > > Actually, when I added the new structure, I had the idea of adding replor= igin_xact_set_origin() and replorigin_xact_set_lsn_timestamp() as I saw a l= ot of duplicate code for the assignment. But I hesitated to do that because= I worried people might consider that=E2=80=99s too much. > > Given you raised the same idea, let me add the helpers as static inline. = We now have 3 static inline helpers, I guess we have no reason to leave rep= lorigin_xact_clear() alone as external. So I made it static inline as well. > > > * successfully and that we don't need to do so again. In combination with > - * setting up replorigin_session_origin_lsn and replorigin_session_origi= n > + * setting up replorigin_xact_state.origin_lsn and replorigin_xact_state= .origin > > I think just replorigin_xact_state should suffice for the sake of brevity= . > > > Updated the comment to eliminate the duplication. > > > 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. > } > > > Good catch. Updated the header comment. > > All comments are addressed in v8. Thank you for updating the patch! The 0001 patch looks good to me. I have some comments on the 0002 patch: - replorigin =3D (replorigin_session_origin !=3D InvalidRepOriginId && - replorigin_session_origin !=3D DoNotReplicateId); + replorigin =3D replorigin_xact_origin_isvalid(); I'm not sure it's a good refactoring TBH. While it can remove some lines, we still need to check if replorigin_xact_state.origin !=3D InvalidRepOriginId at several places. For example, Datum pg_replication_origin_session_is_setup(PG_FUNCTION_ARGS) { replorigin_check_prerequisites(false, false); PG_RETURN_BOOL(replorigin_xact_state.origin !=3D InvalidRepOriginId); } and /* * Dump transaction origin information. We need this during recovery to * update the replication origin progress. * * Note that DoNotReplicateId is intentionally excluded here. */ if (replorigin_xact_state.origin !=3D InvalidRepOriginId) The readers might be confused why we cannot use replorigin_xact_origin_isvalid() here in spite of its function name, replorigin_xact_origin_isvalid, sounds quite suitable. The comment mentions the fact that DoNotReplicated is excluded but doesn't mention why. Also, even if we want this kind of change, it should be implemented in a separate patch because it's not related to the main idea of consolidating the replication origin global variables. --- - * Record commit timestamp. The value comes from plain commit timestam= p - * if replorigin is not enabled, or replorigin already set a value for = us - * in replorigin_session_origin_timestamp otherwise. + * Record commit timestamp. Use one in replorigin_xact_state if set, + * otherwise use plain commit timestamp. and - * Record commit timestamp. The value comes from plain commit - * timestamp if there's no replication origin; otherwise, the - * timestamp was already set in replorigin_session_origin_timestamp= by - * replication. + * Record commit timestamp. Use one in replorigin_xact_state if set= , + * otherwise use plain commit timestamp. I think the previous comments are clearer, so I'm not sure we need to change these comments by this patch. Can we just change replorigin_session_origin_timestamp to replorigin_xact_state.origin_timestamp? --- + if (session_replication_state =3D=3D NULL) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("no replication origin is configured"))); I'm not sure what the original author intended but given that pg_replication_origin_xact_reset() just clears the local state I'm not sure that we should strictly require the session_replication_state to be set up nor it improves the behavior. If we raise an error in that case, it might be more user-friendly but we can live even without it. --- In origin.h: +/* + * Clear the per-transaction replication origin state. + * + * replorigin_session_origin is also cleared if clear_origin is set. + */ +static inline void +replorigin_xact_clear(bool clear_origin) +{ + replorigin_xact_state.origin_lsn =3D InvalidXLogRecPtr; + replorigin_xact_state.origin_timestamp =3D 0; + if (clear_origin) + replorigin_xact_state.origin =3D InvalidRepOriginId; +} Why does this function need to move to origin.h from origin.c? Regards, --=20 Masahiko Sawada Amazon Web Services: https://aws.amazon.com