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 1phvCL-0005dj-9a for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Mar 2023 16:32:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1phvCJ-0000BJ-7P for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Mar 2023 16:32:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1phvCI-0000BA-Qt for pgsql-hackers@lists.postgresql.org; Thu, 30 Mar 2023 16:32:22 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1phvCF-0003Ap-Mw for pgsql-hackers@postgresql.org; Thu, 30 Mar 2023 16:32:22 +0000 Received: by mail-oi1-x22b.google.com with SMTP id bl22so613365oib.11 for ; Thu, 30 Mar 2023 09:32:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680193938; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=LN3dfhh6SqzclfT+zSl8+jvzHxOjIph3HR/l54eXmw4=; b=F6AxpVL49wYgm9MR2sfftqd/6i6So+GkhKXat7fQOk7+QIUIwfZMfVe3xtZpfpYu5V h5rV6BGBjL7qigJGh6cSrujFSBvtXHSThsr/FuPAppInoOnTBwjBvyis6fXvqJk8v7Zb 8ojcb7Cu0epXuT945Il1kE451Rfr/WpFBU63VWpTJL/FzwDbHCK5o6LJEQZy7dXhmiBI RzFhoB7v4mT2qAb8enupODJ3XBtoDI1S30fho/GyDzbk3PgZFfOqSlEFQFz08OKWBU3h F9pTs0S18hLqhDe1m8NGUv2eOjP5atkDuxLAIYop2vxiw6cDRkZRagIo9zlLlg9CMn59 gIfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680193938; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LN3dfhh6SqzclfT+zSl8+jvzHxOjIph3HR/l54eXmw4=; b=GXTkEkfttlrwiSSFDZZY6yWnIO5LgqhYZQsYemiCUGC9RBpNy6g2GrGVwlnaxPE5i8 1luElhJmjrkKCGKxSp7EzELUZyZMWfLW+aYoBi4xAh+flCn6F8ylCddMOHqyJtQU7F3b a/PfyMz4TAU0K3028KgSG+MS0kyMZPf1yLtpjvnGOm4qW1Fuw7UGFJ8j0yG213lzn8Fz Ti8rR3CMOeKZiLyt4bF8fOZLt9X+/EPm4x0t6CiZFT6z3TtQS7sA9GK/9wPd3ALsCmDZ vU5RcnPsrcKRkwieDjvSWN8KYKY3QfWdfuFANoFkfnYsGmfIZO+ojYFCB9t7cJUZuMMq 8s+A== X-Gm-Message-State: AAQBX9fgOFiNnpzgNubJztcbs3/c8XWXBeyTzra1o0BNRZL1A+iPl0I5 V3uTKrVvwVLJiWRrLGF3Zs4lFv+sHp31ShRiY9c= X-Google-Smtp-Source: AKy350b1u/bi6oHaxRzgMNBINPAHZVWixhpO2iQjoWxcLMonAQvOU/GgzdIFF/A9grWb5qI/8nJQ8JfaQIS7oaDHFwE= X-Received: by 2002:aca:1a02:0:b0:389:50f2:4ab6 with SMTP id a2-20020aca1a02000000b0038950f24ab6mr3560036oia.1.1680193937811; Thu, 30 Mar 2023 09:32:17 -0700 (PDT) MIME-Version: 1.0 References: <1f2ad9fd-53ff-74cc-a602-72d45423bcab@gmail.com> <05c0c4f3-82eb-bc6d-c876-5a265c05927b@gmail.com> <20230106034036.2m4qnn7ep7b5ipet@awork3.anarazel.de> <490206c5-bcde-e558-628a-dc67dae4d439@gmail.com> <20230119024617.jzsgpcrgjdc6ysge@awork3.anarazel.de> <94d9d47e-72f4-393a-246e-e3fe8db7b741@gmail.com> <2d314c22b9e03415aa1c7d8fd1f698dae60effa7.camel@j-davis.com> <061eeb26-156e-6011-02b6-b4b1422e2aa6@gmail.com> <8a7da0c2-b7fe-1c78-4e47-a16239bf4839@gmail.com> <6ab352de1553c1e3d0c7d97c038dc470ca51e772.camel@j-davis.com> In-Reply-To: From: Masahiko Sawada Date: Fri, 31 Mar 2023 01:31:41 +0900 Message-ID: Subject: Re: Minimal logical decoding on standbys To: Jeff Davis Cc: "Drouvot, Bertrand" , Andres Freund , Robert Haas , Thomas Munro , Alvaro Herrera , Ibrar Ahmed , Amit Khandekar , fabriziomello@gmail.com, tushar , Rahila Syed , pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Thu, Mar 30, 2023 at 2:45=E2=80=AFPM Jeff Davis wrot= e: > > On Thu, 2023-03-02 at 23:58 -0800, Jeff Davis wrote: > > On Thu, 2023-03-02 at 11:45 -0800, Jeff Davis wrote: > > > In this case it looks easier to add the right API than to be sure > > > about > > > whether it's needed or not. > > > > I attached a sketch of one approach. I'm not very confident that it's > > the right API or even that it works as I intended it, but if others > > like the approach I can work on it some more. > > Another approach might be to extend WaitEventSets() to be able to wait > on Condition Variables, rather than Condition Variables waiting on > WaitEventSets. Thoughts? > +1 to extend CV. If we extend WaitEventSet() to be able to wait on CV, it would be able to make the code simple, but we would need to change both CV and WaitEventSet(). On Fri, Mar 10, 2023 at 8:34=E2=80=AFPM Drouvot, Bertrand wrote: > > I gave it a try, so please find attached v2-0001-Introduce-ConditionVaria= bleEventSleep.txt (implementing the comments above) and 0004_new_API.txt to= put the new API in the logical decoding on standby context. @@ -180,13 +203,25 @@ ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, * by something other than ConditionVariableSignal; though we don't * guarantee not to return spuriously, we'll avoid this obvious case. */ - SpinLockAcquire(&cv->mutex); - if (!proclist_contains(&cv->wakeup, MyProc->pgprocno, cvWaitLink)) + + if (cv) { - done =3D true; - proclist_push_tail(&cv->wakeup, MyProc->pgprocno, cvWaitLink); + SpinLockAcquire(&cv->mutex); + if (!proclist_contains(&cv->wakeup, MyProc->pgprocno, cvWaitLink)) + { + done =3D true; + proclist_push_tail(&cv->wakeup, MyProc->pgprocno, cvWaitLink); + } + SpinLockRelease(&cv->mutex); } This change looks odd to me since it accepts cv being NULL in spite of calling ConditionVariableEventSleep() for cv. I think that this is because in 0004_new_API.txt, we use ConditionVariableEventSleep() in both not-in-recovery case and recovery-in-progress cases in WalSndWaitForWal() as follows: - WalSndWait(wakeEvents, sleeptime, WAIT_EVENT_WAL_SENDER_WAIT_WAL); + ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, wakeEvents, NULL); + ConditionVariableEventSleep(cv, RecoveryInProgress, FeBeWaitSet, NULL, + sleeptime, wait_event); } But I don't think we need to use ConditionVariableEventSleep() in not-in-recovery cases. If I correctly understand the problem this patch wants to deal with, in logical decoding on standby cases, the walsender needs to be woken up on the following events: * condition variable * timeout * socket writable (if pq_is_send_pending() is true) (socket readable event should also be included to avoid wal_receiver_timeout BTW?) On the other hand, in not-in-recovery case, the events are: * socket readable * socket writable (if pq_is_send_pending() is true) * latch * timeout I think that we don't need to change for the latter case as WalSndWait() perfectly works. As for the former cases, since we need to wait for CV, timeout, or socket writable we can use ConditionVariableEventSleep(). Regards, -- Masahiko Sawada Amazon Web Services: https://aws.amazon.com