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.94.2) (envelope-from ) id 1qiTWZ-006Woo-CB for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 05:43:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qiTWX-006kfH-G1 for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Sep 2023 05:43:49 +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.94.2) (envelope-from ) id 1qiTWV-006keT-SZ for pgsql-hackers@lists.postgresql.org; Tue, 19 Sep 2023 05:43:49 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qiTWS-005O5l-HD for pgsql-hackers@lists.postgresql.org; Tue, 19 Sep 2023 05:43:46 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 3BE945C01BA; Tue, 19 Sep 2023 01:43:43 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Tue, 19 Sep 2023 01:43:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1695102223; x=1695188623; bh=H/ SL3/dBUcxQ1pbRWwrEEL65eoawklYRFZVVOo8rKsM=; b=S602WetQflKkgAfR6j vuPeri0G7nAPMIfTF5CQhlcf3WMbJHbL4NnGBulJJhKHTWUVCdmMfikVKBjnGqac zuNtcpJXsPrTk+gGFHDIlQd18LdgKfsBHlP7yQQ2X6MZDtZSg3Ub/imaXELPtkLI UU8NyAf+dL0E3Jx+FnCvg8bGiUtrlN5qHyUvLq1mKpetfUruLC3G2rNPiiVbOXiG 3KXOFGLHkigBvssK9i6xAYyzLv0S3PhALqcJN60mfNX3T2riA/AldMKPlEgEhIvS yZggk6Osi6bMF9dXldPWKFkq6QfgJIjcl2JM5MV9X47FqHVqfQUz3Yl4q+JgIeIH dDVg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1695102223; x=1695188623; bh=H/SL3/dBUcxQ1 pbRWwrEEL65eoawklYRFZVVOo8rKsM=; b=R6X1kKQHneeNygRFExkFVIS9Hasdq a9EwycH8PAOlax/ywrDcrprgciEWu3WKeVG3hc2mzx0uReGyVMYrCi+N0B23Xhme hlqZVeYBZY4vwLhZ/js3Aj3JQJ8COVIOGIiPGXCovjzTN6ex4V2h5XCJd+oO2MQq 2KEY0Hak2UHX15iR+fJu9YNfu+L9MafRTHE0EyPThk50FBoHVUGe6ikKORhfxLek CNXbHFlusntf9LmpdUpO9jz3GLLamSftgim8HW9xbnhaprh4C0ldalc2h0oolFo6 iuJwTX9JwuWzYkbfAthnGAxiCu2sidDBsf/Z+bOkNKvjE9DP0Sm85AmxQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudejledguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdejtddmnecujfgurhepfffhvfevuffkfhggtggujgesghdtreer tddtvdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlse hprghquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpeetleeifedufffhhfdtteel geeggeffhfekueevteeigfduudevudetgfegiedvjeenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdig hiii X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 19 Sep 2023 01:43:41 -0400 (EDT) Date: Tue, 19 Sep 2023 14:43:37 +0900 From: Michael Paquier To: "Zhijie Hou (Fujitsu)" Cc: PostgreSQL Hackers Subject: Re: Move global variables of pgoutput to plugin private scope. Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="+Z1PPzwJeus/X6hw" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --+Z1PPzwJeus/X6hw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 19, 2023 at 04:10:39AM +0000, Zhijie Hou (Fujitsu) wrote: > Currently we have serval global variables in pgoutput, but each of them is > inherently local to an individual pgoutput instance. This could cause iss= ues if > we switch to different output plugin instance in one session and could mi= ss to > reset their value in case of errors. The analysis for each variable is as > follows: (Moved the last block of the message as per the relationship between RelationSyncCache and publications_valid). > - static HTAB *RelationSyncCache =3D NULL; >=20 > pgoutput creates this hash table under cacheMemoryContext to remember the > relation schemas that have been sent, but it's local to an individual pgo= utput > instance, and because it's under global memory context, the hashtable is = not > properly cleared in error paths which means it has a risk of being access= ed in > a different output plugin instance. This was also mentioned in another th= read[2]. >=20 > So I think we'd better allocate this under output plugin private context.= =20 >=20 > But note that, instead of completely moving the hash table into the output > plugin private data, we need to to keep the static pointer variable for t= he map to > be accessed by the syscache callbacks. This is because syscache callbacks= won't > be un-registered even after shutting down the output plugin, so we need a > static pointer to cache the map pointer so that callbacks can check it. > > - static bool publications_valid; >=20 > I thought we need to move this to private data as well, but we need to ac= cess this in a > syscache callback, which means we need to keep the static variable. FWIW, I think that keeping publications_valid makes the code kind of confusing once 0001 is applied, because this makes the handling of the cached data for relations and publications even more inconsistent than it is now, with a mixed bag of two different logics caused by the relationship between the synced relation cache and the publication cache: RelationSyncCache tracks if relations should be rebuilt, while publications_valid does it for the publication data, but both are still static and could be shared by multiple pgoutput contexts. On top of that, publications_valid is hidden at the top of pgoutput.c within a bunch of declarations and no comments to explain why it's here (spoiler: to handle the cache rebuilds with its reset in the cache callback). I agree that CacheMemoryContext is not really a good idea to cache the data only proper to a pgoutput session and that tracking a context in the output data makes the whole cleanup attractive, but it also seems to me that we should avoid entirely the use of relcache callbacks if the intention is to have one RelationSyncEntry per pgoutput. The patch does something different than HEAD and than having one RelationSyncEntry per pgoutout: RelationSyncEntry can reference=20 *everything*, with its data stored in multiple memory contexts as of one per pgoutput. It looks like RelationSyncEntry should be a list or a hash table, at least, so as it can refer to multiple pgoutput states. Knowing that a session can only use one replication slot with MyReplicationSlot, not sure that's worth bothering with. As a whole, 0001 with its changes for RelationSyncCache don't seem like an improvement to me. > - static bool publish_no_origin; >=20 > This flag is also local to pgoutput instance, and we didn't reset the fla= g in > output shutdown callback, so if we consume changes from different slots, = then > the second call would reuse the flag value that is set in the first call = which > is unexpected. To completely avoid this issue, we think we'd better move = this > flag to output plugin private data structure. Yep, that's incorrect. > - static bool in_streaming; >=20 > While on it, I feel we can also move this flag to private data, although = I didn't > see problems for this one. Moving this one to the private state data makes sense to me, as it tracks the streaming of one PGOutputData. Note that we name twice RelSchemaSyncCache in the code, but it does not exist.. -- Michael --+Z1PPzwJeus/X6hw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmUJNQkACgkQnvQgOdby QH0LVw//cyKZj1HBdRwN8NYcwcdJiY0lbAe/J1HoRDj+zfM7u+paeQRjwme/cU9b +pGG8ma4KftdhYvPR1RCrvfco+RqO/eaVv1KLrW8ZYYy7MvkU6IC0+i0Ni4irUsT 56jh9F3EWsIylPgA7t5uBp/sxPcn4pxr9awuhHS1IQCXqkyWPb9IA/59tgIRQkbG Lsaf+qkjAeSf3PqsAm8BqZFldFCGcDlPtYHrFqQI0Jfrl5oQNb0u+qs4hFaywTbK cdNwP+XLilpSLwXiu4SYj7iBrMzDAzs5OjkmwyewCFX7TUgtDVOBp58ukybiv6Sc 1xfrx6TnEuyF7eaJuZnRjxaHa+Qbf9YtDJ6KAJo9igiwiWJTMhpz74OSF7pRtrV6 C80+o1WY5xeDsBDJmmm/4E01CxWbXmWWDghD6Wrlkn9C+yxBISN2w9707K56FaVX tjg40/7yh/oUobg0BPsZ/vgsk/cjTleFMPEw5FlydrIWWCLWhT9tXzCmtyUtBTul 63ncg2D7wUs/3yflo+tlRP7iPhYPBBS15awZqlDmB+lF6u9lqgN+5F8gYT7GHeAU PllG8xX6VL+dcC1c9DC4t91Ut0lmE9ZLBzvX+bBwB9cfJSrVjF2zktwCk/Jv7nZJ bTBNoNDeAlgtX9bsj3y1AUHlYnmFpZoXs/K0NjrgcY3y8IQY4uo= =qjYE -----END PGP SIGNATURE----- --+Z1PPzwJeus/X6hw--