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 1p1Baz-0003tm-1L for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Dec 2022 19:21:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p1Baw-0006VR-Ao for pgsql-hackers@arkaria.postgresql.org; Fri, 02 Dec 2022 19:21:10 +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 1p1Bav-0006Uj-RQ for pgsql-hackers@lists.postgresql.org; Fri, 02 Dec 2022 19:21:10 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p1Bar-0002V3-9s for pgsql-hackers@postgresql.org; Fri, 02 Dec 2022 19:21:08 +0000 Received: by mail-pf1-x430.google.com with SMTP id c7so2393308pfc.12 for ; Fri, 02 Dec 2022 11:21:05 -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=ZyAkWVKRuGtWAxKATvKFZRDPZSz6rjbWOew3LGORZiU=; b=XzU8NA0kgoOML9ZiPIogv0U/EFXGUKkm8NVzuAfDFtLiXPyAFuH2btIW0oHXHSYpxk RUC3vLg6XBz/+PQpiHHthGMU6a5+MLn6pUYMY5FQoxFjzLCKrupgAHFe5Ipk3EInZ63X oLez3H9qNNbRsICPkT4OxsWCNsVmxG9B1X0bAVyDk94sMpa84c15uK8oFcu4HtBiERI5 sAbd76RD9Ahz1i5mV/en0+qRu2e4NtFkVvUv8DeXf2C74mSWB682ygIjk9tUYG/Rz0b5 cZ/HcFkFQJjrG3hheGuqDpYAu/uj553KUnlhmThB6248FLmOBbXjnsVX1ZH9VQfXC6ub hKYQ== 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=ZyAkWVKRuGtWAxKATvKFZRDPZSz6rjbWOew3LGORZiU=; b=ACtEyJFnjSBMjNuKGBjUuQvtuekphMuy4Elpuy/DiYDPaM7tRogLS/cpS4NroID6LH mDEHV+b2yo6hSkSwbHUR/umdEEgU6p8/UgJSoMryP3+qoIMsSXArqXa30TZQLHr+Coks dzzgtuEyJfAHFqbbnWBPIlxP33EPEV34Tc33kcEcfNf3Y6eFYcQAZ70vLjT/nCBTgD1g xEtPb+jjZD3T7hLrLcAFQFyywzcfSwKwmNHFF3uMwPmeLoh/fkel7PCsdrHTgtkjqVaA sfLxJQVTR13JOl8tPsg7N4+7VIFw3oho51TPEeI6dQLuyim9Qx72/vJjJk/f3NfzcMrG pVvw== X-Gm-Message-State: ANoB5pmsTUwq5TxrLEIZOxvH2t+pePFauxqNHvMA4jYfIItXsxO5wGWB B62SBfX4Tt8becNKY3I7AV8= X-Google-Smtp-Source: AA0mqf6PY0QCEtoJhgKhOiWGAP1C1LebIFxh4hRgZoPzjheFruGBUfcMyNZ/xN4LXVM4Soflwt1+ZQ== X-Received: by 2002:a63:1708:0:b0:477:ba9c:d8f4 with SMTP id x8-20020a631708000000b00477ba9cd8f4mr40699699pgl.283.1670008864190; Fri, 02 Dec 2022 11:21:04 -0800 (PST) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id q14-20020a170902a3ce00b0018968d1c6f3sm5899019plb.59.2022.12.02.11.21.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Dec 2022 11:21:03 -0800 (PST) Date: Fri, 2 Dec 2022 11:21:01 -0800 From: Nathan Bossart To: Thomas Munro Cc: "Hayato Kuroda (Fujitsu)" , "pgsql-hackers@postgresql.org" Subject: Re: wake up logical workers after ALTER SUBSCRIPTION Message-ID: <20221202192101.GB2277157@nathanxps13> References: <20221123205027.GA368966@nathanxps13> <20221127234528.GB878043@nathanxps13> <20221130041028.GA1604177@nathanxps13> <20221130050441.GA1677223@nathanxps13> <20221202002130.GA2124877@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ibTvN161/egqYuK8" Content-Disposition: inline In-Reply-To: <20221202002130.GA2124877@nathanxps13> 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 Thu, Dec 01, 2022 at 04:21:30PM -0800, Nathan Bossart wrote: > Okay, here is a new version of the patch. This seems to clear up > everything that I could find via the tests. I cleaned up the patch a bit. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --ibTvN161/egqYuK8 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v5-0001-wake-up-logical-workers-as-needed-instead-of-rely.patch" From 72adfb291ee84b8a20f6634639b15eff6d48cf9a Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 21 Nov 2022 16:01:01 -0800 Subject: [PATCH v5 1/1] wake up logical workers as needed instead of relying on periodic wakeups --- src/backend/access/transam/xact.c | 3 ++ src/backend/catalog/pg_subscription.c | 4 +++ src/backend/commands/alter.c | 7 ++++ src/backend/commands/subscriptioncmds.c | 4 +++ src/backend/replication/logical/worker.c | 46 ++++++++++++++++++++++++ src/include/replication/logicalworker.h | 3 ++ 6 files changed, 67 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/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c index a506fc3ec8..ab75506094 100644 --- a/src/backend/catalog/pg_subscription.c +++ b/src/backend/catalog/pg_subscription.c @@ -25,6 +25,7 @@ #include "catalog/pg_type.h" #include "miscadmin.h" #include "nodes/makefuncs.h" +#include "replication/logicalworker.h" #include "storage/lmgr.h" #include "utils/array.h" #include "utils/builtins.h" @@ -322,6 +323,9 @@ UpdateSubscriptionRelState(Oid subid, Oid relid, char state, /* Cleanup. */ table_close(rel, NoLock); + + /* Wake up the logical replication workers to handle this change quickly. */ + LogicalRepWorkersWakeupAtCommit(subid); } /* diff --git a/src/backend/commands/alter.c b/src/backend/commands/alter.c index 10b6fe19a2..da14e91552 100644 --- a/src/backend/commands/alter.c +++ b/src/backend/commands/alter.c @@ -59,6 +59,7 @@ #include "commands/user.h" #include "miscadmin.h" #include "parser/parse_func.h" +#include "replication/logicalworker.h" #include "rewrite/rewriteDefine.h" #include "tcop/utility.h" #include "utils/builtins.h" @@ -410,6 +411,12 @@ ExecRenameStmt(RenameStmt *stmt) stmt->newname); table_close(catalog, RowExclusiveLock); + /* + * Wake up the logical replication workers to handle this + * change quickly. + */ + LogicalRepWorkersWakeupAtCommit(address.objectId); + return address; } diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index d673557ea4..e29cdcbff9 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" @@ -1358,6 +1359,9 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, table_close(rel, RowExclusiveLock); + /* Wake up the logical replication workers to handle this change quickly. */ + LogicalRepWorkersWakeupAtCommit(subid); + ObjectAddressSet(myself, SubscriptionRelationId, subid); InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0); diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index f9efe6c4c6..d735be4f10 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 process assorted changes as soon as + * possible. + */ +void +LogicalRepWorkersWakeupAtCommit(Oid subid) +{ + MemoryContext oldcxt; + + oldcxt = MemoryContextSwitchTo(TopTransactionContext); + on_commit_wakeup_workers_subids = list_append_unique_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--