Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pHXGB-0004Qf-0q for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Jan 2023 21:43:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pHXG9-0002Qd-RO for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Jan 2023 21:43:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pHXG9-0002QU-GZ for pgsql-hackers@lists.postgresql.org; Mon, 16 Jan 2023 21:43:17 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pHXG7-0004OL-9m for pgsql-hackers@lists.postgresql.org; Mon, 16 Jan 2023 21:43:17 +0000 Received: by mail-lf1-x131.google.com with SMTP id d30so39523712lfv.8 for ; Mon, 16 Jan 2023 13:43:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=7RpPQAFP+LlmPUTUMnHBtyc9yOe5fclJY3w2e4yyWg8=; b=g0Ua3SmdvrjBv2AmtaLoy88+n5C3xaXBHEntoQukewulMsFvpmkj4w/hTvq+qrTpUv 6YCvGbWawOPBf4DmZ+YUVuL7/S7ydcTPlYFh2nQpY1lO2IeDNPLXPHNY98tlG86SCFfk U+pfhbuIOJhHX5nMWqncQL6rAxAWwut/wX9McFhRXLJir0ffD6ez01usGG2bvrwbCR16 bcEj9Q2MeIm5IUaMUF+eiN+cuXMYOWVJKNot+nUfj7lTQhh4BlfPxLRpknCsPnq6nAa5 IRb26PC6Oshb42NoC+pOnqxdhwfoYwndtBZf9xFxy/1fiBcwDOb6yUq/Fclyk3bHn+1L pghA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7RpPQAFP+LlmPUTUMnHBtyc9yOe5fclJY3w2e4yyWg8=; b=0g0Vo7R6fTPD/gSXEaWXOyw37Cshzzvn8A1QSoAjcTZcKHNAFINOlOIyAEN2ddRjzD Lb30ZKdtFd3eXM68yboMizS81nGk17lsUjObJX3zs3dsFKOF524Z4GhVL6+/ExJFmOwA PWV4eW/MW5M9+EkPqhk8UoHXq99Aw2/0m5xfhEYBgIW9seCDu2VTB+nDGDGgyFi2xo2e fVPgmpHjBX/wVTX7XSNzZHPybLQrqJGdfK+bTCT4qTnrsgxRlC2t9L7k6tOmPbaUYuAR vInNr7M3wui/G3ZOG6P44u7IgNhlY6Ws7QbkHc/QZ9hmOMF2hIn5SbQGwQsSkiqHBYxx D/4w== X-Gm-Message-State: AFqh2koqbCjFNEb+sHoS4/K9LrtSiODAY+KiR/KxIyBQZQntKN2wAhKe GdPK7wMbBgViYkZ5i5CHkDYnN2DVvZbqVdmqx2s= X-Google-Smtp-Source: AMrXdXuHk0eqFp4iX+qRj8Zh+d8HQickyMvoAHUxMgxUV8VkxUr7o7MIMpdY96uI9kko8zFN9DxkrjXzg1dp2L0cP18= X-Received: by 2002:a05:6512:3a88:b0:4a2:4b43:9aad with SMTP id q8-20020a0565123a8800b004a24b439aadmr46126lfu.213.1673905394278; Mon, 16 Jan 2023 13:43:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Peter Smith Date: Tue, 17 Jan 2023 08:42:57 +1100 Message-ID: Subject: Re: Perform streaming logical transactions by background workers and parallel apply To: Amit Kapila Cc: "houzj.fnst@fujitsu.com" , Masahiko Sawada , shveta malik , "wangw.fnst@fujitsu.com" , "shiy.fnst@fujitsu.com" , PostgreSQL Hackers , Dilip Kumar Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jan 16, 2023 at 5:41 PM Amit Kapila wrote: > > On Mon, Jan 16, 2023 at 10:24 AM Peter Smith wrote: > > > > 2. > > > > /* > > + * Return the pid of the leader apply worker if the given pid is the pid of a > > + * parallel apply worker, otherwise return InvalidPid. > > + */ > > +pid_t > > +GetLeaderApplyWorkerPid(pid_t pid) > > +{ > > + int leader_pid = InvalidPid; > > + int i; > > + > > + LWLockAcquire(LogicalRepWorkerLock, LW_SHARED); > > + > > + for (i = 0; i < max_logical_replication_workers; i++) > > + { > > + LogicalRepWorker *w = &LogicalRepCtx->workers[i]; > > + > > + if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid) > > + { > > + leader_pid = w->leader_pid; > > + break; > > + } > > + } > > + > > + LWLockRelease(LogicalRepWorkerLock); > > + > > + return leader_pid; > > +} > > > > 2a. > > IIUC the IsParallelApplyWorker macro does nothing except check that > > the leader_pid is not InvalidPid anyway, so AFAIK this algorithm does > > not benefit from using this macro because we will want to return > > InvalidPid anyway if the given pid matches. > > > > So the inner condition can just say: > > > > if (w->proc && w->proc->pid == pid) > > { > > leader_pid = w->leader_pid; > > break; > > } > > > > Yeah, this should also work but I feel the current one is explicit and > more clear. OK. But, I have one last comment about this function -- I saw there are already other functions that iterate max_logical_replication_workers like this looking for things: - logicalrep_worker_find - logicalrep_workers_find - logicalrep_worker_launch - logicalrep_sync_worker_count So I felt this new function (currently called GetLeaderApplyWorkerPid) ought to be named similarly to those ones. e.g. call it something like "logicalrep_worker_find_pa_leader_pid". > > > ~ > > > > 2b. > > A possible alternative comment. > > > > BEFORE > > Return the pid of the leader apply worker if the given pid is the pid > > of a parallel apply worker, otherwise return InvalidPid. > > > > > > AFTER > > If the given pid has a leader apply worker then return the leader pid, > > otherwise, return InvalidPid. > > > > I don't think that is an improvement. > > > ====== > > > > src/backend/utils/adt/pgstatfuncs.c > > > > 3. > > > > @@ -434,6 +435,16 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) > > values[28] = Int32GetDatum(leader->pid); > > nulls[28] = false; > > } > > + else > > + { > > + int leader_pid = GetLeaderApplyWorkerPid(beentry->st_procpid); > > + > > + if (leader_pid != InvalidPid) > > + { > > + values[28] = Int32GetDatum(leader_pid); > > + nulls[28] = false; > > + } > > + > > > > 3a. > > There is an existing comment preceding this if/else but it refers only > > to leaders of parallel groups. Should that comment be updated to > > mention the leader apply worker too? > > > > Yeah, we can slightly adjust the comments. How about something like the below: > index 415e711729..7eb668634a 100644 > --- a/src/backend/utils/adt/pgstatfuncs.c > +++ b/src/backend/utils/adt/pgstatfuncs.c > @@ -410,9 +410,9 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) > > /* > * If a PGPROC entry was retrieved, display > wait events and lock > - * group leader information if any. To avoid > extra overhead, no > - * extra lock is being held, so there is no guarantee of > - * consistency across multiple rows. > + * group leader or apply leader information if > any. To avoid extra > + * overhead, no extra lock is being held, so > there is no guarantee > + * of consistency across multiple rows. > */ > if (proc != NULL) > { > @@ -428,7 +428,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) > /* > * Show the leader only for active > parallel workers. This > * leaves the field as NULL for the > leader of a parallel > - * group. > + * group or the leader of a parallel apply. > */ > if (leader && leader->pid != > beentry->st_procpid) > The updated comment LGTM. ------ Kind Regards, Peter Smith. Fujitsu Australia