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 1qUSFC-00CwLM-IF for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Aug 2023 13:31:58 +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 1qUSF9-007roR-OT for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Aug 2023 13:31:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qUSF9-007roI-Dq for pgsql-hackers@lists.postgresql.org; Fri, 11 Aug 2023 13:31:55 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qUSF2-001sp7-2I for pgsql-hackers@lists.postgresql.org; Fri, 11 Aug 2023 13:31:54 +0000 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-5236c9ff275so2655554a12.1 for ; Fri, 11 Aug 2023 06:31:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1691760706; x=1692365506; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=BHDOLcIiLGTcaoeQbfEbnutt3g3GdKJ4L3O3bvLHcrA=; b=GKEQ5gadF++b7Qunn5hmbTy3mQhkVqAJmUKFxkKmncjMDLMBw/vD2OLzL+EUZykuo/ oMKNZfQ+FOmD0avL2MGr61Lp20PenTBbD78Fbu1gVww8x5TfdoLBueFxlv9UQmh98RgD zOZ0WkH+UGWCmm/rrWmFJsofe8S84B7Lxliluq8bhyeQZZLPdrs0pBllhv8TLGbfpp0N sJsW87uacsOyRUFGx2XKwMCtI/Bx5eAgAl+houRuPvpi5d1Gk3qb5vK4o689MSLsYaqf ckh7bvqiZNWTfYsw/EAFEQrfywobPZXsQfiBrU0z2CdWXdKlEzDgnVCziERm5pc2nu94 eBPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691760706; x=1692365506; h=content-transfer-encoding:subject:from:cc:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=BHDOLcIiLGTcaoeQbfEbnutt3g3GdKJ4L3O3bvLHcrA=; b=gkOf1KZiPmVoGIMFtm/cnG0wA5ibeaXsLoogMpE+saG/eEwGGiZ+S32uOUp65KtpwN 0oLt0RPI5B9GPwoNtA3c4TOyC0IPBd0tEaP8She53B1vSKsmKfdNmvCheNTcEGqeRSKR aVM3gafLgi10vMvP01HoYzIEO0ZJdO6t+WkkeiHxhFjHc3jY+Ca4BwZ4UrvbnL/we9WX 3qTOHd7+VXM0OEXlP0h7w4jdZ7UJGU3cCyq4To6o2DuVHH3A9AaAiCF7R+AK3YIxyuEP 4CJUIql0tX2UQcZhBguZeh3LvUK+H3nAk1+qSq73HRwiScPdj7Nf+C2xDcHnbpEeUtnR Qjyg== X-Gm-Message-State: AOJu0YzsASw0LAIMk6/x/oxYHOaQw8s34hBlUtLOL9R9NzHX3zn+U0jK o9r9NIi0zx/VsLvMR7Eyj+0DywAmCpLuodaX1TqTElvw+HSsmTInIT9Fq1dRqCM7vXDNaTMWQ2c ApFwY1qKwup0cRONPyOUyUOmGVVjrsoxLK6zDgnovA6amHk6rscyozAE+cKBaLLHuOd3G7UOwfu Z7T90RTiR0F0yOTd8duc4iFm+o2w== X-Google-Smtp-Source: AGHT+IH5C+hbQQXXhF2ka4XsE9IxagRHG+BtxoDxrIw8l0dHHrGW/5Sofm//gD6CH3j9PgrxeI4KZA== X-Received: by 2002:aa7:cfc1:0:b0:523:2e0e:5462 with SMTP id r1-20020aa7cfc1000000b005232e0e5462mr1512059edy.42.1691760705545; Fri, 11 Aug 2023 06:31:45 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id bo12-20020a0564020b2c00b0052284228e3bsm2083294edb.8.2023.08.11.06.31.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 11 Aug 2023 06:31:45 -0700 (PDT) Message-ID: <2840876b-4cfe-240f-0a7e-29ffd66711e7@enterprisedb.com> Date: Fri, 11 Aug 2023 15:31:43 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: en-US To: PostgreSQL Hackers Cc: Andres Freund From: Tomas Vondra Subject: walsender "wakeup storm" on PG16, likely because of bc971f4025c (Optimize walsender wake up logic using condition variables) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, while working on some logical replication stuff, I noticed that on PG16 I often end up with a completely idle publisher (no user activity), that however looks like this in top: ... %CPU COMMAND ... 17.9 postgres: walsender user test ::1(43064) START_REPLICATION ... 16.6 postgres: walsender user test ::1(43128) START_REPLICATION ... 15.0 postgres: walsender user test ::1(43202) START_REPLICATION ... 6.6 postgres: walsender user test ::1(43236) START_REPLICATION ... 5.3 postgres: walsender user test ::1(43086) START_REPLICATION ... 4.3 postgres: walsender user test ::1(43180) START_REPLICATION ... 3.7 postgres: walsender user test ::1(43052) START_REPLICATION ... 3.7 postgres: walsender user test ::1(43158) START_REPLICATION ... 3.0 postgres: walsender user test ::1(43108) START_REPLICATION ... 3.0 postgres: walsender user test ::1(43214) START_REPLICATION That's an awful lot of CPU for a cluster doing essentially "nothing" (there's no WAL to decode/replicate, etc.). It usually disappears after a couple seconds, but sometimes it's a rather persistent state. The profile from the walsender processes looks like this: --99.94%--XLogSendLogical | |--99.23%--XLogReadRecord | XLogReadAhead | XLogDecodeNextRecord | ReadPageInternal | logical_read_xlog_page | | | |--97.80%--WalSndWaitForWal | | | | | |--68.48%--WalSndWait It seems to me the issue is in WalSndWait, which was reworked to use ConditionVariableCancelSleep() in bc971f4025c. The walsenders end up waking each other in a busy loop, until the timing changes just enough to break the cycle. I've been unable to reproduce this on PG15, and bc971f4025c seems like the most significant change to WalSndWait, which is why I suspect it's related to the issue. Reproducing this is simple, create a publisher with multiple subscribers (could even go to the same subscriber instance) and empty publications. If you trigger a "noop" it's likely to cause this high memory usage: --------------------------------------------------------------------- # init two clusters pg_ctl -D data-publisher init pg_ctl -D data-subscriber init # change the parameters to allow 10 subscriptions echo 'wal_level = logical' >> data-publisher/postgresql.conf echo 'port = 5433' >> data-subscriber/postgresql.conf echo 'max_worker_processes = 20' >> data-subscriber/postgresql.conf echo 'max_logical_replication_workers = 20' >> data-subscriber/postgresql.conf # setup empty publication createdb test psql test -c "create publication p"; # setup 10 subscriptions for i in `seq 1 10`; do createdb -p 5433 test$i psql -p 5433 test$i -c "create subscription s$i CONNECTION 'host=localhost port=5432 dbname=test' publication p"; done # emit logical messages, which are almost noop, 5s apart for i in `seq 1 10`; do psql test -c "select pg_logical_emit_message(false, 'x', 'x')"; sleep 5; done; --------------------------------------------------------------------- regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company