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 1qYinh-002ToH-7u for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Aug 2023 08:01:13 +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 1qYing-00ARsX-4W for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Aug 2023 08:01:11 +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 1qYinf-00ARsG-0b for pgsql-hackers@lists.postgresql.org; Wed, 23 Aug 2023 08:01:11 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qYinY-000UGK-Ji for pgsql-hackers@postgresql.org; Wed, 23 Aug 2023 08:01:09 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 86B6E32008FE; Wed, 23 Aug 2023 04:01:01 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 23 Aug 2023 04:01:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1692777661; x= 1692864061; bh=+fXlGbZsV9tF6D0EnNbYb9UdoTiiFg7WqWl/Kx7qIuo=; b=V OY0rSz/z04M3eZFYgec1o4bj+H2UC+ERU6nq3CgxQWndq7bkwEkk20Pb3cHIQJqL OJ0Ti5Uq0W9BdtlwDr9S7aG4TNjM4XiM3Za2iJo6cHLUcMpU1BLpOuXIJTrnmDwk o9LLnrIw7dDK4fJGk8kG61nhaQm6t83hGFeTxA8oMRavmcPWUsuONIsL/pcbEVND tiLnccc/+MUn8unOUUrFmx5obeiaOC+x99DbQWspPOGdTVOsypurbpGWT6A/HypR oFN1ixqjy/7Qvq8M5vg4xRIkgjwOD6u0fa3tnvT3ElRFIPajue4OmKtCNGAOjlfP iwqrbpbI8S9JLNhfdv1Yg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedruddvfedgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesmhekreertddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepudelkeekgfdttdettdekgfduvdeiffelteegjeeihfet heekveeftedtuddvhfeunecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 23 Aug 2023 04:01:00 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 02711A9; Wed, 23 Aug 2023 10:00:56 +0200 (CEST) Date: Wed, 23 Aug 2023 10:00:56 +0200 From: Alvaro Herrera To: "Zhijie Hou (Fujitsu)" Cc: Thomas Munro , Amit Kapila , pgsql-hackers Subject: Re: subscription/015_stream sometimes breaks Message-ID: <20230823080056.tbesqtabjcghzdfq@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="trxvcc54u5zdf7ul" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --trxvcc54u5zdf7ul Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2023-Aug-23, Zhijie Hou (Fujitsu) wrote: > [1]-- > LWLockAcquire(LogicalRepWorkerLock, LW_SHARED); > > workers = logicalrep_workers_find(MyLogicalRepWorker->subid, true); > foreach(lc, workers) > { > LogicalRepWorker *w = (LogicalRepWorker *) lfirst(lc); > > ** if (isParallelApplyWorker(w)) > logicalrep_worker_stop_internal(w, SIGTERM); > } Hmm, I think if worker->in_use is false, we shouldn't consult the rest of the struct at all, so I propose to add the attached 0001 as a minimal fix. In fact, I'd go further and propose that if we do take that stance, then we don't need clear out the contents of this struct at all, so let's not. That's 0002. And the reason 0002 does not remove the zeroing of ->proc is that the tests gets stuck when I do that, and the reason for that looks to be some shoddy coding in WaitForReplicationWorkerAttach, so I propose we change that too, as in 0003. Thoughts? -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ --trxvcc54u5zdf7ul Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Don-t-use-LogicalRepWorker-until-in_use-is-verified.patch" From a28775fc5900cd740556a864e1826ceda268b794 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 23 Aug 2023 09:25:35 +0200 Subject: [PATCH 1/3] Don't use LogicalRepWorker until ->in_use is verified --- src/backend/replication/logical/launcher.c | 4 ++-- src/include/replication/worker_internal.h | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c index 72e44d5a02..ec5156aa41 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -862,7 +862,7 @@ logicalrep_sync_worker_count(Oid subid) { LogicalRepWorker *w = &LogicalRepCtx->workers[i]; - if (w->subid == subid && isTablesyncWorker(w)) + if (isTablesyncWorker(w) && w->subid == subid) res++; } @@ -889,7 +889,7 @@ logicalrep_pa_worker_count(Oid subid) { LogicalRepWorker *w = &LogicalRepCtx->workers[i]; - if (w->subid == subid && isParallelApplyWorker(w)) + if (isParallelApplyWorker(w) && w->subid == subid) res++; } diff --git a/src/include/replication/worker_internal.h b/src/include/replication/worker_internal.h index a428663859..8f4bed0958 100644 --- a/src/include/replication/worker_internal.h +++ b/src/include/replication/worker_internal.h @@ -327,8 +327,10 @@ extern void pa_decr_and_wait_stream_block(void); extern void pa_xact_finish(ParallelApplyWorkerInfo *winfo, XLogRecPtr remote_lsn); -#define isParallelApplyWorker(worker) ((worker)->type == WORKERTYPE_PARALLEL_APPLY) -#define isTablesyncWorker(worker) ((worker)->type == WORKERTYPE_TABLESYNC) +#define isParallelApplyWorker(worker) ((worker)->in_use && \ + (worker)->type == WORKERTYPE_PARALLEL_APPLY) +#define isTablesyncWorker(worker) ((worker)->in_use && \ + (worker)->type == WORKERTYPE_TABLESYNC) static inline bool am_tablesync_worker(void) @@ -339,12 +341,14 @@ am_tablesync_worker(void) static inline bool am_leader_apply_worker(void) { + Assert(MyLogicalRepWorker->in_use); return (MyLogicalRepWorker->type == WORKERTYPE_APPLY); } static inline bool am_parallel_apply_worker(void) { + Assert(MyLogicalRepWorker->in_use); return isParallelApplyWorker(MyLogicalRepWorker); } -- 2.30.2 --trxvcc54u5zdf7ul Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-don-t-clean-up-unnecessarily.patch" From 36336e6d8ef2913def59a53b6bc083a40181a85a Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 23 Aug 2023 09:53:51 +0200 Subject: [PATCH 2/3] don't clean up unnecessarily --- src/backend/replication/logical/launcher.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c index ec5156aa41..f9d6ebf2d8 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -795,12 +795,6 @@ logicalrep_worker_cleanup(LogicalRepWorker *worker) worker->in_use = false; worker->proc = NULL; - worker->dbid = InvalidOid; - worker->userid = InvalidOid; - worker->subid = InvalidOid; - worker->relid = InvalidOid; - worker->leader_pid = InvalidPid; - worker->parallel_apply = false; } /* -- 2.30.2 --trxvcc54u5zdf7ul Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0003-Don-t-rely-on-proc-being-NULL-either.patch" From 9dced5b0898be22004d442cb2893848663f967ba Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 23 Aug 2023 09:55:13 +0200 Subject: [PATCH 3/3] Don't rely on proc being NULL either --- src/backend/replication/logical/launcher.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c index f9d6ebf2d8..8bfceb5c27 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -201,11 +201,18 @@ WaitForReplicationWorkerAttach(LogicalRepWorker *worker, LWLockAcquire(LogicalRepWorkerLock, LW_SHARED); - /* Worker either died or has started. Return false if died. */ - if (!worker->in_use || worker->proc) + /* Not started? */ + if (!worker->in_use) { LWLockRelease(LogicalRepWorkerLock); - return worker->in_use; + return false; + } + + /* Already going? */ + if (worker->proc) + { + LWLockRelease(LogicalRepWorkerLock); + return true; } LWLockRelease(LogicalRepWorkerLock); @@ -794,7 +801,6 @@ logicalrep_worker_cleanup(LogicalRepWorker *worker) Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE)); worker->in_use = false; - worker->proc = NULL; } /* -- 2.30.2 --trxvcc54u5zdf7ul--