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 1oxHLs-0000BQ-3X for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Nov 2022 00:41:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oxHLq-0007hs-Sm for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Nov 2022 00:41:26 +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 1oxHLq-0007hj-DT for pgsql-hackers@lists.postgresql.org; Tue, 22 Nov 2022 00:41:26 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oxHLn-0002J3-Sz for pgsql-hackers@postgresql.org; Tue, 22 Nov 2022 00:41:25 +0000 Received: by mail-pf1-x42f.google.com with SMTP id v28so12847607pfi.12 for ; Mon, 21 Nov 2022 16:41:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=zGfcHkyUhfLSHLhqKOt/BewXCHWeb21cBcVtLaaP9fU=; b=X6vVRUR8HzNZnQ9FuxgoChIzLKk046h/998DquzZb27lQgCZC0Pen1Vpd6mwFvLGiM 9yXejyvX6b1yxcklApmpx6LKGzjGgGPM1gmpNxvqB6sWm2vgqZcPIWkZpg4VycMi8NFJ PYvHyoRdUUbXZJq0wId7QooYnfKWlnT5pwAA46/13lcQ9y9oArhgbOO+iAPk21HjasLo 8sjCQJbtYKPI2Qjt0RyrO0gJN3sz6mFwowlumVcGPAPNu4ya97TqbL9tG+sUe4nmjWsn tyPhOYxcTq5Dwau2vRZQYyON3fIsD6cCcsVCusXS6ERYphvIgp+Js9Bm+d0cui6ApcO4 mbfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zGfcHkyUhfLSHLhqKOt/BewXCHWeb21cBcVtLaaP9fU=; b=M1aiT2UpWfvSkg6SSLaHByids96eWncHfW0Jv17V/ff2qMKGICgMp6DA2/tl8qLEak StEE8Fom4O74M9cegc8ppAGtZCcBWy6jmWZyMOi6i2dhTf6WyHaZxAXWOZeHOA3lEscB JrTzsXr09IkC8wnXyF2AtVYdkpU9Q+osKh0neMcAiyjFnbDScrHzqdfw6Mp7xLYlhZPM aak8RiN7uyU+1lb+0NpPakf5FnvwqXcdlfmB085bpKPpYU9NaHizik2gcezEfpv2NrvT nd/ow4218p4PWfN88f/l3Usx0PLOhP7J14BQjXNecDTfU+t/yK/zO7YXSfWUmOH7gFvw Q87w== X-Gm-Message-State: ANoB5pmgnkXCd9uexGaBneDZJMtUB7f4R3sKj0aR/KIpx2IawM47Avvb 1yfQ0bOJ4R6o5ViK/SOqbZHka0XeBHk= X-Google-Smtp-Source: AA0mqf4xI4ET4j0BdS4H+YRGwjM7YEIBe32vuHO59SuX0D5XHtKQejz/pMKgBdJtqpDwx1S8K+nFFw== X-Received: by 2002:a63:e30c:0:b0:470:8098:f0de with SMTP id f12-20020a63e30c000000b004708098f0demr1225170pgh.527.1669077682339; Mon, 21 Nov 2022 16:41:22 -0800 (PST) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id 139-20020a630591000000b0046fb6e0e691sm8180984pgf.6.2022.11.21.16.41.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Nov 2022 16:41:21 -0800 (PST) Date: Mon, 21 Nov 2022 16:41:19 -0800 From: Nathan Bossart To: pgsql-hackers@postgresql.org Subject: wake up logical workers after ALTER SUBSCRIPTION Message-ID: <20221122004119.GA132961@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="C7zPtVaVf+AK4Oqc" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --C7zPtVaVf+AK4Oqc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi hackers, While working on avoiding unnecessary wakeups in logical/worker.c (as was done for walreceiver.c in 05a7be9), I noticed that the tests began taking much longer. This seems to be caused by the reduced frequency of calls to maybe_reread_subscription() in LogicalRepApplyLoop(). Presently, LogicalRepApplyLoop() only waits for up to a second, so the subscription info is re-read by workers relatively frequently. If LogicalRepApplyLoop() sleeps for longer, the subscription info may not be read for much longer. I think the fix for this problem can be considered independently, as relying on frequent wakeups seems less than ideal, and the patch seems to provide a small improvement even before applying the avoid-unnecessary-wakeups patch. On my machine, the attached patch improved 'check-world -j8' run time by ~12 seconds (from 3min 8sec to 2min 56 sec) and src/test/subscription test time by ~17 seconds (from 139 seconds to 122 seconds). I put the new logic in launcher.c, but it might make more sense to put it in logical/worker.c. I think that might require some new #includes in a couple of files, but otherwise, the patch would likely look about the same. Thoughts? -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --C7zPtVaVf+AK4Oqc Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-wake-up-logical-workers-after-ALTER-SUBSCRIPTION.patch" From 80452ebbadfe80f237ebf5c7cdf8fa5eb2a61e35 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 21 Nov 2022 16:01:01 -0800 Subject: [PATCH v1 1/1] wake up logical workers after ALTER SUBSCRIPTION --- src/backend/access/transam/xact.c | 2 ++ src/backend/commands/subscriptioncmds.c | 2 ++ src/backend/replication/logical/launcher.c | 37 ++++++++++++++++++++++ src/include/replication/logicallauncher.h | 3 ++ 4 files changed, 44 insertions(+) diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 8086b857b9..175ba770e6 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -2360,6 +2360,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 +2861,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..ff0fe5e27b 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1031,6 +1031,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/launcher.c b/src/backend/replication/logical/launcher.c index 73594c698a..af56220e3e 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -75,6 +75,7 @@ static void logicalrep_worker_onexit(int code, Datum arg); static void logicalrep_worker_detach(void); static void logicalrep_worker_cleanup(LogicalRepWorker *worker); +static Oid on_commit_wakeup_workers_subid = InvalidOid; static bool on_commit_launcher_wakeup = false; @@ -759,6 +760,42 @@ ApplyLauncherShmemInit(void) } } +/* + * Wakeup the stored subscription's workers on commit if requested. + */ +void +AtEOXact_LogicalRepWorkers(bool isCommit) +{ + if (isCommit && OidIsValid(on_commit_wakeup_workers_subid)) + { + List *workers; + ListCell *worker; + + LWLockAcquire(LogicalRepWorkerLock, LW_SHARED); + + workers = logicalrep_workers_find(on_commit_wakeup_workers_subid, true); + foreach(worker, workers) + logicalrep_worker_wakeup_ptr((LogicalRepWorker *) lfirst(worker)); + + LWLockRelease(LogicalRepWorkerLock); + } + + on_commit_wakeup_workers_subid = InvalidOid; +} + +/* + * 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) +{ + on_commit_wakeup_workers_subid = subid; +} + /* * Wakeup the launcher on commit if requested. */ diff --git a/src/include/replication/logicallauncher.h b/src/include/replication/logicallauncher.h index f1e2821e25..02cd3bdd4c 100644 --- a/src/include/replication/logicallauncher.h +++ b/src/include/replication/logicallauncher.h @@ -24,6 +24,9 @@ extern void ApplyLauncherShmemInit(void); extern void ApplyLauncherWakeupAtCommit(void); extern void AtEOXact_ApplyLauncher(bool isCommit); +extern void LogicalRepWorkersWakeupAtCommit(Oid subid); +extern void AtEOXact_LogicalRepWorkers(bool isCommit); + extern bool IsLogicalLauncher(void); #endif /* LOGICALLAUNCHER_H */ -- 2.25.1 --C7zPtVaVf+AK4Oqc--