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 1pXK3f-0006yg-WA for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Mar 2023 10:51:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pXK3c-0004kp-Id for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Mar 2023 10:51:36 +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 1pXK3c-0004kg-38 for pgsql-hackers@lists.postgresql.org; Wed, 01 Mar 2023 10:51:36 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pXK3W-0003qj-C9 for pgsql-hackers@postgresql.org; Wed, 01 Mar 2023 10:51:34 +0000 Received: by mail-wm1-x32f.google.com with SMTP id m14-20020a7bce0e000000b003e00c739ce4so7548315wmc.5 for ; Wed, 01 Mar 2023 02:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=bmSxLxR2XvlTPEf1t2+HobFV1bkaoo77cnU6coQw3+8=; b=M0hsfDFh3rRwtu3/YGT4YuFyIoSdqD6TB5lGmZxr2P5Gjsi4S8Ysbu9PzGRzqyc7AP P7q+CrxDrJJQA36QYz7ptRQ07tso4mUsSaTlZRsOW6/5afRBSIv0Wi7w+79n/DiOYf0T SgzdvIb+mGZ/xDu93LQtLeaj94Zhcb9h3sRYAugr4orpmaHoOTIFVlqA7+PWiEqlTetz r7KqLoy5E5jvsuBaGsuDaKH7A48+yWkr89vP/Ai/x1ctkVFFftq1M5JrunwXwOYkJfmE 4Mk7Z+ATvosYCiqITDqVeLNrgbYFrq2qX2quL1hDcfSB37VYVhupy5tJAw3TQ+ncCnrs 7ISg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bmSxLxR2XvlTPEf1t2+HobFV1bkaoo77cnU6coQw3+8=; b=0SV9DHBre15hYkbRpWQ9mvn9igKkFHBq1z2Pvh0i/Uq24WSkH78O/Tdn/sCVij+CEE CZNnykeGune6aiEHPovtEvlemt+SrMzBOhzNPyivoP63feX1tIIAzo7p2vIIOzo2uGNO 6r8pDOjvExZagh1V2NzilaUfLKCvNnogwFlv/QY7+RCCrrvmbraDL6LNBqbwa2VxYAHH 3K+CE4m0i34EMPODsEGDg10KWtJ5PLo5yAvH6ETrIP2648Z5VG16c1VSLoHIPOcyP8Cq 6uXtP4kPW8kxp01weiTPGOO94KvQJZ0NkFwvzwZvFXsegZswv1BLGEVqUcyfUKgd5pcg 7NEg== X-Gm-Message-State: AO0yUKU1bl4+dkDZNigdBekCqk2Z7hx7yOcNCH+aaCcpDeMVq2sJ9DOS P2Azbi92Ah85++30MnRmyXY= X-Google-Smtp-Source: AK7set/bdOpDB1eAnOepIliq5TrM7UEkFCNhvq+fxfJrm033pj9bRCla+nOrth5scjsIyzs1X8Ei3g== X-Received: by 2002:a05:600c:1652:b0:3eb:2da4:f32d with SMTP id o18-20020a05600c165200b003eb2da4f32dmr4425689wmn.26.1677667887845; Wed, 01 Mar 2023 02:51:27 -0800 (PST) Received: from [192.168.22.82] ([54.239.6.185]) by smtp.gmail.com with ESMTPSA id i13-20020a1c540d000000b003db06224953sm15594764wmb.41.2023.03.01.02.51.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Mar 2023 02:51:27 -0800 (PST) Message-ID: <061eeb26-156e-6011-02b6-b4b1422e2aa6@gmail.com> Date: Wed, 1 Mar 2023 11:51:25 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: Minimal logical decoding on standbys To: Jeff Davis , Andres Freund Cc: Robert Haas , Thomas Munro , Alvaro Herrera , Ibrar Ahmed , Amit Khandekar , fabriziomello@gmail.com, tushar , Rahila Syed , pgsql-hackers 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> Content-Language: en-US From: "Drouvot, Bertrand" In-Reply-To: <2d314c22b9e03415aa1c7d8fd1f698dae60effa7.camel@j-davis.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 3/1/23 1:48 AM, Jeff Davis wrote: > On Mon, 2023-02-27 at 09:40 +0100, Drouvot, Bertrand wrote: >> Please find attached V51 tiny rebase due to a6cd1fc692 (for 0001) and >> 8a8661828a (for 0005). > > [ Jumping into this thread late, so I apologize if these comments have > already been covered. ] > Thanks for looking at it! > Regarding v51-0004: > > * Why is the CV sleep not being canceled? I think that's an oversight, I'll look at it. > * Comments on WalSndWaitForWal need to be updated to explain the > difference between the flush (primary) and the replay (standby) cases. > Yeah, will do. > Overall, it seems like what you really want for the sleep/wakeup logic > in WalSndWaitForLSN Typo for WalSndWaitForWal()? > is something like this: > > condVar = RecoveryInProgress() ? replayCV : flushCV; > waitEvent = RecoveryInProgress() ? > WAIT_EVENT_WAL_SENDER_WAIT_REPLAY : > WAIT_EVENT_WAL_SENDER_WAIT_FLUSH; > > ConditionVariablePrepareToSleep(condVar); > for(;;) > { > ... > sleeptime = WalSndComputeSleepTime(GetCurrentTimestamp()); > socketEvents = WL_SOCKET_READABLE; > if (pq_is_send_pending()) > socketEvents = WL_SOCKET_WRITABLE; > ConditionVariableTimedSleepOrEvents( > condVar, sleeptime, socketEvents, waitEvent); > } > ConditionVariableCancelSleep(); > > > But the problem is that ConditionVariableTimedSleepOrEvents() doesn't > exist, and I think that's what Andres was suggesting here[1]. > WalSndWait() only waits for a timeout or a socket event, but not a CV; > ConditionVariableTimedSleep() only waits for a timeout or a CV, but not > a socket event. > > I'm also missing how WalSndWait() works currently. It calls > ModifyWaitEvent() with NULL for the latch, so how does WalSndWakeup() > wake it up? I think it works because the latch is already assigned to the FeBeWaitSet in pq_init()->AddWaitEventToSet() (for latch_pos). > > Assuming I'm wrong, and WalSndWait() does use the latch, then I guess > it could be extended by having two different latches in the WalSnd > structure, and waking them up separately and waiting on the right one. I'm not sure this is needed in this particular case, because: Why not "simply" call ConditionVariablePrepareToSleep() without any call to ConditionVariableTimedSleep() later? In that case the walsender will be put in the wait queue (thanks to ConditionVariablePrepareToSleep()) and will be waked up by the event on the socket, the timeout or the CV broadcast (since IIUC they all rely on the same latch). So, something like: condVar = RecoveryInProgress() ? replayCV : flushCV; ConditionVariablePrepareToSleep(condVar); for(;;) { ... sleeptime = WalSndComputeSleepTime(GetCurrentTimestamp()); socketEvents = WL_SOCKET_READABLE; if (pq_is_send_pending()) socketEvents = WL_SOCKET_WRITABLE; WalSndWait(wakeEvents, sleeptime, WAIT_EVENT_WAL_SENDER_WAIT_WAL); <-- Note: the code within the loop does not change at all } ConditionVariableCancelSleep(); If the walsender is waked up by the CV broadcast, then it means the flush/replay occurred and then we should exit the loop right after due to: " /* check whether we're done */ if (loc <= RecentFlushPtr) break; " meaning that in this particular case there is only one wake up due to the CV broadcast before exiting the loop. That looks weird to use ConditionVariablePrepareToSleep() without actually using ConditionVariableTimedSleep() but it looks to me that it would achieve the same goal: having the walsender being waked up by the event on the socket, the timeout or the CV broadcast. In that case we would be missing the WAIT_EVENT_WAL_SENDER_WAIT_REPLAY and/or the WAIT_EVENT_WAL_SENDER_WAIT_FLUSH wait events thought (and we'd just provide the WAIT_EVENT_WAL_SENDER_WAIT_WAL one) but I'm not sure that's a big issue. What do you think? Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com