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 1v6hQ5-008M6X-25 for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 03:34:21 +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 1v6hQ2-00FH4T-Pu for pgsql-hackers@arkaria.postgresql.org; Thu, 09 Oct 2025 03:34:19 +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.94.2) (envelope-from ) id 1v6hQ2-00FH4J-1B for pgsql-hackers@lists.postgresql.org; Thu, 09 Oct 2025 03:34:19 +0000 Received: from fhigh-b1-smtp.messagingengine.com ([202.12.124.152]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v6hPz-0019XA-3B for pgsql-hackers@postgresql.org; Thu, 09 Oct 2025 03:34:18 +0000 Received: from phl-compute-08.internal (phl-compute-08.internal [10.202.2.48]) by mailfhigh.stl.internal (Postfix) with ESMTP id 52BAF7A00B3; Wed, 8 Oct 2025 23:34:13 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Wed, 08 Oct 2025 23:34:13 -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:subject :subject:to:to; s=fm2; t=1759980853; x=1760067253; bh=5Q4ZdKkQ4N TAHoi91FBWP8kVJVXSrEE1pk10hjTlSEY=; b=J0mEiFy75esqmfJA/+SaWB1Q/E x6WlQyiXaUeJVfDga85nH95CQ5/Nx57SFcNskXg4KujZC5yV1VcP331kvGW1XbAR jeLy3dfX6LKtDrHUDqRpgzc62ECVJU6UNuL7Pwn/jFwmFNB1qaBJy0xLRySvrmJ3 FHo4JSIsb6Ik8mOXUrtRUkNK2Vt35WGrg2KIirhehr+xs8QUtGOfWo7jQVX5t/d9 0Tr/q8uOa4J49+DkNBtM8fLfYGV2bVCGLMlk0E8emr1bGNRofijg19S/10F2kCMu 4bJ8Tgj2iuDD+CWM9lUoSktOZe9uryu3UDdND2M0EjuLd00aOgT6J+6uxVgA== 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:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1759980853; x=1760067253; bh=5Q4ZdKkQ4NTAHoi91FBWP8kVJVXSrEE1pk1 0hjTlSEY=; b=aXvw4+prLuBB49bAO3QoUCZnIgGZpQqmFGpiaxPL4PJ+LJCdEUj itG6ZqML+ClhFCzQwWO7oBpnEIZCOEDEWqiRfbFVhTJzd/dR6is1B4RiCdnwait0 T3inL5aG5nEM+xxOr33lddgByImYLhv+9A4Z8s0TqUeqtYOh5ZaSqFp33rlrTkZQ Q9n89h8z4CYiISWIYIk1VR/DuB8Nj/jVT94oof8Ms/8GFD16+wpJhjCbPc8nZ0TM WJ3JBm5KlFoTgH67yTpUQlKki4T20m61hBYxSI12JqA2ok3yEBTH1CsjnIZAixoO 7yptnV9hq+ZtT12PDJE4t4bHdGhTZ3ylVlw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddutdehudduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnegfrh hlucfvnfffucdljedtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddv necuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqh huihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepteelieefudffhffhtdetleeggeeg fffhkeeuveetiefgudduvedutefggeeivdejnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihiidp nhgspghrtghpthhtohepgedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepkhhurh houggrrdhhrgihrghtohesfhhujhhithhsuhdrtghomhdprhgtphhtthhopehsmhhithhh phgsvddvhedtsehgmhgrihhlrdgtohhmpdhrtghpthhtohepihifrghtrgdrrgihrgesfh hujhhithhsuhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehpohhs thhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 8 Oct 2025 23:34:11 -0400 (EDT) Date: Thu, 9 Oct 2025 12:33:48 +0900 From: Michael Paquier To: "Hayato Kuroda (Fujitsu)" Cc: 'Peter Smith' , "Aya Iwata (Fujitsu)" , pgsql-hackers Subject: Re: [PROPOSAL] Termination of Background Workers for ALTER/DROP DATABASE Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="pghrxNMd99t7fBe5" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --pghrxNMd99t7fBe5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 09, 2025 at 02:28:56AM +0000, Hayato Kuroda (Fujitsu) wrote: > Sorry for posting many times. I noticed that CountOtherDBBackends() can b= e called > while creating the database. Should we also mention and test the case? How would you test that? A bgworker would not be able to connect to the database that's being created. The implementation done in v4 is a nice simplification compared to the original proposal, nice. I've gone through the patch and here are some comments. +my $basedir =3D $node->basedir(); +my $tablespace =3D "$basedir/tablespace"; We could use a temporary folder for the tablespaces. I've always prefered this practice. That's a bit, feel free to ignore this one, what you are doing is not wrong, either. + BGWORKER_EXIT_AT_DATABASE_DROP Perhaps BGWORKER_EXIT_AT_DATABASE_CHANGE? DROP is incorrect, as the database could be renamed or moved, as well. + * Exit the bgworker when its database is dropped, renamed, or moved. + * Requires BGWORKER_SHMEM_ACCESS and BGWORKER_BACKEND_DATABASE_CONNECTION. + */ +#define BGWORKER_EXIT_AT_DATABASE_DROP 0x0004 We could enforce this rule with an elog(ERROR) or an assert, perhaps? @@ -407,7 +407,8 @@ worker_spi_launch(PG_FUNCTION_ARGS)=20 memset(&worker, 0, sizeof(worker)); worker.bgw_flags =3D BGWORKER_SHMEM_ACCESS | - BGWORKER_BACKEND_DATABASE_CONNECTION; + BGWORKER_BACKEND_DATABASE_CONNECTION | + BGWORKER_EXIT_AT_DATABASE_DROP; I would suggest to make this part optional, with an argument that uses a default value to false (as in "do-not-set the flag") that can be given by the callers of worker_spi_launch(). Let's also add one bgworker that's used in your series of tests, and check that it is *not* cancelled when the flag is not set. +# Ensure the worker_spi dynamic worker is launched on the specified databa= se +sub launch_bgworker +{ + my ($node, $database) =3D @_; + + # Launch a background worker on the given database + my $result =3D $node->safe_psql( + $database, qq( + SELECT worker_spi_launch(4, oid) IS NOT NULL I'd recommend to make the worker number an argument of this function, and also do things so as the log_contains() call is able to check that the worker with the matching number is loged, rather than rely on "worker_spi dynamic" for all the comparisons. This is relevant to be able to mix multiple workers at the same time in the tests. -- Michael --pghrxNMd99t7fBe5 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmjnLRwACgkQnvQgOdby QH16CA/8CPjVfnJ+L5HzI6hJ9olkxcfjcU63uFrkx6IPBW8hsx2GkMIfmd2R9LCv EkOgcrShDKJmYVGUmuWm/FgoK7dmGKoamYrcrOP+W6gezIS2PCTPisalUdNruJby 7gaX/eyfT2EoHa3rxKd/dRB5+r3IDunagK566XKX4xucWrbWC1VJVQ81IkRMUADu NaTw5g5aURAUmNkgTvmpMOsMzD7AOIjtHS0I8oR0zz4vIYORbkZud/uHoX68ynZL 0Dy2lcTplzcfylA3RzP1v3H8BQPIFl3z/4k0L2nLR/8Mi5RbEWDJbgv4RptTv1Xs EjlahckzdJ5MuZbYqmrZmK43Vba894OiO2I24E87ivvYwQVyslmilHq/npxYmq+O Rl+SgMfQ3r/8ybct6DoL8iO+aqyKuxZgaE1JLvt+tLW7pAjdddfowk1JVaHImZPo kOu5/yBX+pIBDqV8yGwqARn7UwCaaMgMJLZv+xEBMfJ051v1A/ut8NQN03tACuQ7 J6u910wTp4o5/l890F4BPytLkYBkj8tYZaxdTUdmU+5Vj0Xyp+aX6cWXQwxAWm1h emEb04aA4cVRNIUJ/dwIC7ACXpRE2ZJHZBKTM3uWCDeGbZ/owttvwpMa0HkLMsAi R0MTy84+4241fjRJ11SHVn92rPgpnNdRC2rbHBfKmhRDpliuv/g= =rs2e -----END PGP SIGNATURE----- --pghrxNMd99t7fBe5--