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 1qWc2j-003nQ5-Ql for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Aug 2023 12:24:02 +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 1qWc2i-00Gt0x-8q for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Aug 2023 12:24:00 +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 1qWc2h-00Gt0n-Pm for pgsql-hackers@lists.postgresql.org; Thu, 17 Aug 2023 12:24:00 +0000 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qWc2b-000fFp-Da for pgsql-hackers@lists.postgresql.org; Thu, 17 Aug 2023 12:23:59 +0000 Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-2ba0f27a4c2so114874321fa.2 for ; Thu, 17 Aug 2023 05:23:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1692275032; x=1692879832; h=message-id:date:content-transfer-encoding:mime-version:comments :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DQ8zkxNxFeiNxPXNx3I18A+BOvR6u0CD1KDMyWeJNZ8=; b=t/QTCKHQrcvxp0pDy5hRU6R2frOch0VseSURX/JBULkAG9Ai5K2y8QqlZ2Dy818wem kvlxOSHhXr5X9NTK86cm3M0vNtWsVes2Ya3C0mQT2ml8sBm8mILT1SG2W9PENuI91YTS UTMd6jTT/Ifl+if+moMO93WP0om38+KvtkME4IgxGkvy+u897dekFw1u6SmiLHmA1o6a dgW3NTlRf8p0/Do+3YE+LXm2jUGuFyuLt1lsAy4GyTBKm75dWWU4h3kfOaTvlPtLuzXb toQuly8XBrxyVvG/xNgduDQ2m2ZnRgycy/i0Rt38uNaW8FD/0nxCI4Acw/JcNT+otnQH yFJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692275032; x=1692879832; h=message-id:date:content-transfer-encoding:mime-version:comments :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=DQ8zkxNxFeiNxPXNx3I18A+BOvR6u0CD1KDMyWeJNZ8=; b=ORvW6a71NsMgSBzEnVXu+NBPu2y9jFSRldyIuL21FoWVb9F+6clGMCoBnxk/Zdmy7f wZRPKrJDWltBzh6eOqZnIV/Sf6du3maLQ1A0tCBSbUWhDpvNt7PU0SL62mg1JJRoHHOX RKQb8o1KLbiXVC7CQb6VgEHxDsNGeseoPIk9XC4YAj/wwX+3Wx7QZ2A/lKrOgKY/gQ4G 5SNrRFxAQEUxecTNYKCwSsvuDcKWe6QuUg9qHG+5uFyuZA67V7oRIY7H571bFdPb9M2b pCSqkvDEkick2zR1psdlfPO0i56r0bXJYHzQ/FGFbLd/xsV3M1ctCtANt2+8jJgRNW0U hxAA== X-Gm-Message-State: AOJu0YxNPNliMGaZeGrQWkoUA7NwaaWA4jjSLthtK7kIe23fU8LemXYT qwTy9pToODbcsXv6EjoffsdNsw== X-Google-Smtp-Source: AGHT+IGU4rWRSVY5JUIHtSB/sgPajvkxeqBhcXujMLchKX05Fn4CQdRE+a8k59c1IBjdA7lgVEh/9w== X-Received: by 2002:a2e:a417:0:b0:2b9:cf90:ab9c with SMTP id p23-20020a2ea417000000b002b9cf90ab9cmr3934482ljn.8.1692275031665; Thu, 17 Aug 2023 05:23:51 -0700 (PDT) Received: from antos ([88.103.134.59]) by smtp.gmail.com with ESMTPSA id s17-20020a7bc391000000b003fd2e89620asm2801363wmj.40.2023.08.17.05.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 05:23:51 -0700 (PDT) From: Antonin Houska To: Thomas Munro cc: Tomas Vondra , Andres Freund , PostgreSQL Hackers Subject: Re: walsender "wakeup storm" on PG16, likely because of bc971f4025c (Optimize walsender wake up logic using condition variables) In-reply-to: References: <2840876b-4cfe-240f-0a7e-29ffd66711e7@enterprisedb.com> <20230811175111.3h7pwmezbvoglb5t@awork3.anarazel.de> <977682d0-a23e-9529-ccc6-8872769c0f6e@enterprisedb.com> <3029.1692184823@antos> Comments: In-reply-to Thomas Munro message dated "Thu, 17 Aug 2023 10:58:45 +1200." X-Mailer: MH-E 8.6+git; nmh 1.7+dev; GNU Emacs 28.2.50 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 17 Aug 2023 14:25:40 +0200 Message-ID: <12156.1692275140@antos> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thomas Munro wrote: > On Wed, Aug 16, 2023 at 11:18=E2=80=AFPM Antonin Houska = wrote: > > I try to understand this patch (commit 5ffb7c7750) because I use condit= ion > > variable in an extension. One particular problem occured to me, please > > consider: > > > > ConditionVariableSleep() gets interrupted, so AbortTransaction() calls > > ConditionVariableCancelSleep(), but the signal was sent in between. Sho= uldn't > > at least AbortTransaction() and AbortSubTransaction() check the return = value > > of ConditionVariableCancelSleep(), and re-send the signal if needed? >=20 > I wondered about that in the context of our only in-tree user of > ConditionVariableSignal(), in parallel btree index creation, but since > it's using the parallel executor infrastructure, any error would be > propagated everywhere so all waits would be aborted. I see, ConditionVariableSignal() is currently used only to signal other workers running in the same transactions. The other parts use ConditionVariableBroadcast(), so no consumer should miss its signal. > > Note that I'm just thinking about such a problem, did not try to reprod= uce it. >=20 > Hmm. I looked for users of ConditionVariableSignal() in the usual web > tools and didn't find anything, so I guess your extension is not > released yet or not open source. I'm curious: what does it actually > do if there is an error in a CV-wakeup-consuming backend? I guess it > might be some kind of work-queue processing system... it seems > inevitable that if backends are failing with errors, and you don't > respond by retrying/respawning, you'll lose or significantly delay > jobs/events/something anyway (imagine only slightly different timing: > you consume the signal and start working on a job and then ereport, > which amounts to the same thing in the end now that your transaction > is rolled back?), and when you retry you'll see whatever condition was > waited for anyway. But that's just me imagining what some > hypothetical strawman system might look like... what does it really > do? If you're interested, the extension is pg_squeeze [1]. I think the use case= is rather special. All the work is done by a background worker, but an user function can be called to submit a "task" for the worker and wait for its completion. So the function sleeps on a CV and the worker uses the CV to wa= ke it up. If this function ends due to ERROR, the user is supposed to find a l= og message in the worker output sooner or later. It may sound weird, but that function exists primarily for regression tests, so ERROR is a problem anywa= y. > (FWIW when I worked on a couple of different work queue-like systems > and tried to use ConditionVariableSignal() I eventually concluded that > it was the wrong tool for the job because its wakeup order is > undefined. It's actually FIFO, but I wanted LIFO so that workers have > a chance to become idle and reduce the pool size, but I started to > think that once you want that level of control you really want to > build a bespoke wait list system, so I never got around to proposing > that we consider changing that.) >=20 > Our condition variables are weird. They're not associated with a > lock, so we made start-of-wait non-atomic: prepare first, then return > control and let the caller check its condition, then sleep. Typical > user space condition variable APIs force you to acquire some kind of > lock that protects the condition first, then check the condition, then > atomically release-associated-lock-and-start-sleeping, so there is no > data race but also no time where control is returned to the caller but > the thread is on the wait list consuming signals. That choice has > some pros (you can choose whatever type of lock you want to protect > your condition, or none at all if you can get away with memory > barriers and magic) and cons.. However, as I think Andres was getting > at, having a non-atomic start-of-wait doesn't seem to require us to > have a non-atomic end-of-wait and associated extra contention. So > maybe we should figure out how to fix that in 17. Thanks for sharing your point of view. I'm fine with this low-level approac= h: it's well documented and there are examples in the PG code showing how it should be used :-) --=20 Antonin Houska Web: https://www.cybertec-postgresql.com https://github.com/cybertec-postgresql/pg_squeeze/