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 1oxL48-0002YH-HV for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Nov 2022 04:39:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oxL47-0003be-Cr for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Nov 2022 04:39:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oxL46-0003aF-TC for pgsql-hackers@lists.postgresql.org; Tue, 22 Nov 2022 04:39:23 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oxL43-0004SP-VB for pgsql-hackers@postgresql.org; Tue, 22 Nov 2022 04:39:21 +0000 Received: by mail-pl1-x62e.google.com with SMTP id w23so12481583ply.12 for ; Mon, 21 Nov 2022 20:39:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yjClxU2h51E8fA2VD1K7xDTVWvoKHqjJnccxf2hPkE8=; b=Z9L6jkF+XIdtECXjXaKuHTHyzf6UKwJTAZZ4zpKydGmPa9AcJAgAu7LiGo7wErmQ0S bpFFAT0GfP5pgxlJLMqbKKooCisA9DUT63/ECDo3QKT9UEaCY8AgcEbKVRLJ/II1WLaJ KF0cVREeee2LcafN1VEPU0sVFoA7mao3JPD+3iC30pX66Yg1CAfYkYjyzY9/gvQc4RAd aZyJx/gVqRbAzGHslxXc0ljtIwG6kGMqXnLgfOdIWWPPs861cuouf0m28viX4bSqSqWd UI9KxriM2y10s93R5kDqqaAvNGnYJQT0pwuP2xr/4u5JGPdHF/l9Sk0QUl0lMZhwOO7l m35g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yjClxU2h51E8fA2VD1K7xDTVWvoKHqjJnccxf2hPkE8=; b=jPv7YJil78FCWqt6bEVhpTV3UnPxNaHNHtNbdpCL56h/pS5zX9qPLuf3aB97Q1WzSI qb5Xrk2kpFhfaV5YlHofRz5h5ZGWYL80cV/3KQ/Gc9DmN7WVm26S2M/AXcKX6EhX0Rz9 iKsJuIGXLSNTaHZRxgja+suJ/XcHhsoU55RiyxaADnxiL0Z7XrelrgZ/Y6oEoHYdZdVL Sf2y7Y0bVnZ0E20YeJuzcphu707sKRE6BqM/GIMUuH7pp6yYeFuMs6Vw9ku4VmWJtwAK owEtemY7CLBSTkC8fcEZt5/rAg2sZ4YRfRiQJ9wE7g89FOfMATqKkNvZHfjqADi/cBLn OSHw== X-Gm-Message-State: ANoB5pkCmyasC1mg8CDMgI0Y20xcXh4NQf8slQsVjL+3hQ3QcnhR+/wv hLKOU7E+7rHh5qlF0pPpRW4= X-Google-Smtp-Source: AA0mqf74Iu4/knXEFxjgZWz/ZqN4CZUTqPAMxJIdIc/ITw8HTvm3Y+fl+ILJNTsyoxOHtT99OXraCg== X-Received: by 2002:a17:902:6a86:b0:187:722:f4db with SMTP id n6-20020a1709026a8600b001870722f4dbmr2825520plk.87.1669091958965; Mon, 21 Nov 2022 20:39:18 -0800 (PST) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id j5-20020a170902c3c500b0018853416bbcsm10677261plj.7.2022.11.21.20.39.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Nov 2022 20:39:18 -0800 (PST) Date: Mon, 21 Nov 2022 20:39:16 -0800 From: Nathan Bossart To: "Hayato Kuroda (Fujitsu)" Cc: "pgsql-hackers@postgresql.org" Subject: Re: wake up logical workers after ALTER SUBSCRIPTION Message-ID: <20221122043916.GA230228@nathanxps13> References: <20221122004119.GA132961@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ibTvN161/egqYuK8" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ibTvN161/egqYuK8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Nov 22, 2022 at 03:03:52AM +0000, Hayato Kuroda (Fujitsu) wrote: > Just One comment: IIUC the statement "ALTER SUBSCRIPTION" can be executed > inside the transaction. So if two subscriptions are altered in the same > transaction, only one of them will awake. Is it expected behavior? > > I think we can hold a suboid list and record oids when the subscription are > altered, and then the backend process can consume all of list cells at the end of > the transaction. I think you are correct. I did it this way in v2. I've also moved the bulk of the logic to logical/worker.c. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --ibTvN161/egqYuK8 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-wake-up-logical-workers-after-ALTER-SUBSCRIPTION.patch" From 57bbd9e8d9da72c50d9a41704f3f42b37aba33ab Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 21 Nov 2022 16:01:01 -0800 Subject: [PATCH v2 1/1] wake up logical workers after ALTER SUBSCRIPTION --- src/backend/access/transam/xact.c | 3 ++ src/backend/commands/subscriptioncmds.c | 3 ++ src/backend/replication/logical/worker.c | 46 ++++++++++++++++++++++++ src/include/replication/logicalworker.h | 3 ++ 4 files changed, 55 insertions(+) diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 8086b857b9..dc00e66cfb 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -47,6 +47,7 @@ #include "pgstat.h" #include "replication/logical.h" #include "replication/logicallauncher.h" +#include "replication/logicalworker.h" #include "replication/origin.h" #include "replication/snapbuild.h" #include "replication/syncrep.h" @@ -2360,6 +2361,7 @@ CommitTransaction(void) AtEOXact_PgStat(true, is_parallel_worker); AtEOXact_Snapshot(true, false); AtEOXact_ApplyLauncher(true); + AtEOXact_LogicalRepWorkers(true); pgstat_report_xact_timestamp(0); CurrentResourceOwner = NULL; @@ -2860,6 +2862,7 @@ AbortTransaction(void) AtEOXact_HashTables(false); AtEOXact_PgStat(false, is_parallel_worker); AtEOXact_ApplyLauncher(false); + AtEOXact_LogicalRepWorkers(false); pgstat_report_xact_timestamp(0); } diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index d673557ea4..9f225008c4 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -34,6 +34,7 @@ #include "nodes/makefuncs.h" #include "pgstat.h" #include "replication/logicallauncher.h" +#include "replication/logicalworker.h" #include "replication/origin.h" #include "replication/slot.h" #include "replication/walreceiver.h" @@ -1031,6 +1032,8 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, form = (Form_pg_subscription) GETSTRUCT(tup); subid = form->oid; + LogicalRepWorkersWakeupAtCommit(subid); + /* must be owner */ if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index e48a3f589a..f61cce7abf 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -253,6 +253,8 @@ WalReceiverConn *LogRepWorkerWalRcvConn = NULL; Subscription *MySubscription = NULL; static bool MySubscriptionValid = false; +static List *on_commit_wakeup_workers_subids = NIL; + bool in_remote_transaction = false; static XLogRecPtr remote_final_lsn = InvalidXLogRecPtr; @@ -4092,3 +4094,47 @@ reset_apply_error_context_info(void) apply_error_callback_arg.remote_attnum = -1; set_apply_error_context_xact(InvalidTransactionId, InvalidXLogRecPtr); } + +/* + * Wakeup the stored subscriptions' workers on commit if requested. + */ +void +AtEOXact_LogicalRepWorkers(bool isCommit) +{ + if (isCommit && on_commit_wakeup_workers_subids != NIL) + { + ListCell *subid; + + LWLockAcquire(LogicalRepWorkerLock, LW_SHARED); + foreach(subid, on_commit_wakeup_workers_subids) + { + List *workers; + ListCell *worker; + + workers = logicalrep_workers_find(lfirst_oid(subid), true); + foreach(worker, workers) + logicalrep_worker_wakeup_ptr((LogicalRepWorker *) lfirst(worker)); + } + LWLockRelease(LogicalRepWorkerLock); + } + + on_commit_wakeup_workers_subids = NIL; +} + +/* + * Request wakeup of the workers for the given subscription ID on commit of the + * transaction. + * + * This is used to ensure that the workers reread the subscription info as soon + * as possible. + */ +void +LogicalRepWorkersWakeupAtCommit(Oid subid) +{ + MemoryContext oldcxt; + + oldcxt = MemoryContextSwitchTo(TopTransactionContext); + on_commit_wakeup_workers_subids = lappend_oid(on_commit_wakeup_workers_subids, + subid); + MemoryContextSwitchTo(oldcxt); +} diff --git a/src/include/replication/logicalworker.h b/src/include/replication/logicalworker.h index cd1b6e8afc..2c2340d758 100644 --- a/src/include/replication/logicalworker.h +++ b/src/include/replication/logicalworker.h @@ -16,4 +16,7 @@ extern void ApplyWorkerMain(Datum main_arg); extern bool IsLogicalWorker(void); +extern void LogicalRepWorkersWakeupAtCommit(Oid subid); +extern void AtEOXact_LogicalRepWorkers(bool isCommit); + #endif /* LOGICALWORKER_H */ -- 2.25.1 --ibTvN161/egqYuK8--