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 1vdPEd-0008rk-2k for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 08:49:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdPEc-00D1gM-2X for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 08:49:43 +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 1vdPEc-00D1g3-1Q for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 08:49:43 +0000 Received: from mail-dy1-x132b.google.com ([2607:f8b0:4864:20::132b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdPEa-0053BV-1g for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 08:49:42 +0000 Received: by mail-dy1-x132b.google.com with SMTP id 5a478bee46e88-2abf5900cd5so1290747eec.1 for ; Wed, 07 Jan 2026 00:49:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767775779; x=1768380579; 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=9/si9q+fuBcW5QmSXK+/0Aon/tU+zePYLhXp5EoQgGs=; b=c3awQeRhF2LuPUSLocly3eDPZf3GT2MHtoh1yI6kZ1J2hbO8s37Z4rYBACutiDSSev 4z2axHKE+sCf51iXeZRrRFzcQ1Na/tG/KMCPwGlpK1p4QlP8FzbScLmc9KqUYDejGLrP xLf77F1ADs7az1w2hkOOb9e7S6cF1iYOUavuueKJ5Q08K3dqnugT6ep7Jvu5amex7zGy NRkLCAKyN8syPNlgfDw7DOnC6LmvP/4kiTGA5jHSr8yhiBe/ZXnCEj+8RKg+yxNXWV9b cGlkblhiV8IORMIfI2/Df14UzccOMF8bp2G327uF2MtHdh5fNMO4gjFNzLM66d5/gfpX dqBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767775779; x=1768380579; 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=9/si9q+fuBcW5QmSXK+/0Aon/tU+zePYLhXp5EoQgGs=; b=WzKr+e5JsBCJTiGXcnHoi2ephTTiWhVXoY+8O3bwN7fxIoA6QJfnKgmvMwzWok7S8i K/EFhkw72kuwPe9bcC+D/oxsdNdrYorHTKFKzgRBJf1JP90uOAsavHXTBxXRkMOXUeeY fgL7UStzcCvpusjmwABbfD5iOGFpGOJYr2EbJi4PFLY/aLgi9oxq7Bq0XWiCMdBD14XI yGDz4lXeYMi4UdFXpq1DiAbLbU3dr0yLQlvjoa0kj9/EbzSJ5ociBVBrY0wSCZWgBMab kudDFWpiOERSfPxid6GXT/4cSaZLZlrUdzoK7045j2I1vxzBir4oSppZvAY5P1GewZo/ P9gw== X-Forwarded-Encrypted: i=1; AJvYcCWAht8FmGBp5idr01mJPEwQ0j6ndaL/nr6x/mLMrNaUoTxW0jZzC2gg6VRWn0GKzJ6+irjaShNgK+ZtRYXu@lists.postgresql.org X-Gm-Message-State: AOJu0YwfLhSTGLeWu8jls50LXgUNuvKsTBqnWSgVNPkV0TJuWVknz8Tq YSuJPGd93CaFC+/0AWpzk3sg+vP2ts0+Z3x8U6Iio2GsrAGMLsVr1YYfYJZUBtfuhAk= X-Gm-Gg: AY/fxX4RuBEFqn8ZuNdN3nGAbUcTlft/eOLyO8EMpyoY+kzJNBpGNP88h8HYUbMZ5EI QVtYZeYzefa3DdKLVCAgpyNS8Eof7opxDs4i+SZGIaN+80/QA6lV9h7mbHUIH8Rgo+rY+gNixJB hJJ2zEHM4PRt5wU0gSDO+Iv4T836gJjjA7ZjFvoIJ0SL1mTRgx/z4JxcAJNDns9Ou/5CPraufPt xumWWsRsKlZANmoc/JRXkbJ4vzzAN0Wvu/klF6QggMIvnhQgjOqofqBUWSswVf3OH2GpEV9Ug/F TKv/uGdLvKvWimNWfE5w/aFsVxizDmx6aBkqPl3hXTNuQieahSArgSGbAFZ8M3kAgPSERREQPrr u4JceMetZZEjUhOuvM7MZMJMyn7Eku2kJni7ZO3LdZXeQKtvTFSkSAJwZojfMhif23q2AUJfzuI N/kSk86YTa+BF0N/ZRzAGU X-Google-Smtp-Source: AGHT+IE2oFE+CPAlPs27AVPBoPHm9t9LVLKJlheP7Xbl7C5g0sbqfHgWVAkgepSkmRHnrVIVynlYvA== X-Received: by 2002:a05:7301:6448:b0:2ae:60f7:7f42 with SMTP id 5a478bee46e88-2b17d21b755mr1240782eec.11.1767775778509; Wed, 07 Jan 2026 00:49:38 -0800 (PST) Received: from smtpclient.apple ([142.171.105.12]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b1707b13bdsm5929664eec.24.2026.01.07.00.49.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Jan 2026 00:49:38 -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: Wed, 7 Jan 2026 16:49:02 +0800 Cc: Ashutosh Bapat , =?utf-8?Q?=C3=81lvaro_Herrera?= , Postgres hackers Content-Transfer-Encoding: quoted-printable Message-Id: <26279DAC-5ADA-4E30-B866-5DED0EEC2BEF@gmail.com> References: <202512291435.5edkf3hnichv@alvherre.pgsql> To: Masahiko Sawada 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 7, 2026, at 15:21, Chao Li wrote: >=20 >=20 > On Wed, Jan 7, 2026 at 8:30=E2=80=AFAM Masahiko Sawada = wrote: > On Mon, Dec 29, 2025 at 11:17=E2=80=AFPM Chao Li = wrote: > > > > On Tue, Dec 30, 2025 at 1:07=E2=80=AFPM Chao Li = wrote: > >> > >> > >> On Tue, Dec 30, 2025 at 12:48=E2=80=AFPM Ashutosh Bapat = wrote: > >>> > >>> On Mon, Dec 29, 2025 at 8:14=E2=80=AFPM =C3=81lvaro Herrera = wrote: > >>> > > >>> > On 2025-Dec-24, Ashutosh Bapat wrote: > >>> > > >>> > > If we go this route, we at least need to declare the new = functions as > >>> > > static inline and move them to a header file instead of .c = file. > >>> > > >>> > Hmm, why would we make them static inline instead of standard = (extern) > >>> > functions? We use static inline functions when we want to avoid = the > >>> > overhead of a function call in a hot code path, but I doubt = that's the > >>> > case here. Am I mistaken on this? > >>> > > >>> > >>> I wasn't aware that we are using static inline only in hot code = paths. > >>> Looking around I see most of the static inline functions are from > >>> modules which are used in hot code paths. So, yeah that seems to = be > >>> the convention. I also see some exceptions like those in > >>> basebackup_sink.h - I don't think all of those are used in hot = code > >>> paths. > >>> > >>> In this case, we are moving three assignments into their own > >>> functions. CPU instructions to call extern functions will be > >>> significant compared to CPU instructions for those assignments. = static > >>> inline functions, OTOH, would have similar performance as the = existing > >>> code while providing modularization. If you feel that's not a good > >>> enough reason, I am ok keeping them extern. > >>> > >>> > > Further, does it make sense to put together all the state = variables > >>> > > into a single structure? > >>> > > >>> > Yeah -- keeping the threaded-backend project in mind, moving = them to a > >>> > single struct seems to make sense. I think it's a separate = patch though > >>> > because it'd be more invasive than Chao's initial patch, as = those > >>> > variables are used in many places. > >>> > > >> > >> > >> Attached v3 patch set. Comparing to v2, the changes are: > >> > >> 0001: > >> * Combine the two cleanup functions into one and control them by a = bool flag. > >> * Change the helper function to be extern. > >> * Move out cleanup from reset function. > >> > >> 0002: Consolidate replication origin session globals into a single = state struct. > > > > > > Fixed a bug in v4. > > >=20 > I've reviewed both patches. Here are some comments: >=20 > Thank you very much for the review. > =20 > 0001 patch: >=20 > +/* > + * Clear session replication origin state. > + * > + * If xact_only is true, only clear the per-transaction state. > + */ > +void > +replorigin_session_clear_state(bool xact_only) > +{ > + replorigin_session_origin_lsn =3D InvalidXLogRecPtr; > + replorigin_session_origin_timestamp =3D 0; > + if (!xact_only) > + replorigin_session_origin =3D InvalidRepOriginId; > +} >=20 > Given that we already have session_replication_state, I am concerned > that the name replorigin_session_clear_state creates ambiguity. Could > we rename it to something like replorigin_session_clear()? >=20 > Agreed.=20 > Additionally, I feel that the term "per-transaction state" in the > comments does not accurately describe these two fields. How about > renaming the xact_only parameter to clear_origin? This would make it > explicit that setting the flag to true clears > replorigin_session_origin as well. >=20 > Fixed. > =20 > 0002 patch: >=20 > +typedef struct RepOriginSessionState > +{ > + RepOriginId origin; > + XLogRecPtr origin_lsn; > + TimestampTz origin_timestamp; > +} RepOriginSessionState; > + > +extern PGDLLIMPORT RepOriginSessionState replorigin_session_state; >=20 > replorigin_session_state is quite confusable with the existing > session_replication_state. Given that these values are used to add the > additional information to the transaction, how about the name > something like "replorigin_xact_state" or "replorigin_xact_origin"? >=20 >=20 > Okay, I take replorigin_xact_state, and I renamed the structure name = as well. > Given replorigin_session_clear() only sets members of = RepOriginXactState, I also renamed it to replorigin_xact_clear(). >=20 > -- > +RepOriginSessionState replorigin_session_state =3D { > + InvalidRepOriginId, InvalidXLogRecPtr, 0 > +}; >=20 > I think using designated initializers here would be better for > readability and robustness against future struct changes. >=20 > Fixed.=20 >=20 >=20 > Best regards, > -- > Chao Li (Evan) > HighGo Software Co., Ltd. > https://www.highgo.com/ = The CF CI failed a test case, but I don=E2=80=99t think that=E2=80=99s = mine. I just did a self-review for v5 and couldn=E2=80=99t find any = logic change. So I would guess the CI failure was intermittent. I = don=E2=80=99t know how to rerun the CI test other than bumping the patch = version and re-posting. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/