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 1oWLj4-0008EV-1S for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Sep 2022 17:54:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oWLj2-0007NM-55 for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Sep 2022 17:54:04 +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 1oWLj1-0007ND-Nk for pgsql-hackers@lists.postgresql.org; Thu, 08 Sep 2022 17:54:03 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oWLiy-0000Kf-Vw for pgsql-hackers@lists.postgresql.org; Thu, 08 Sep 2022 17:54:02 +0000 Received: by mail-pj1-x102e.google.com with SMTP id s14-20020a17090a6e4e00b0020057c70943so3087329pjm.1 for ; Thu, 08 Sep 2022 10:54:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=GHq5pwQKz5lVl73l5jrCAH09xq8t0ttkGVDLBO4YoAE=; b=dLFUnKSqabwjZry6vrX4VFqvvSjuyvimHjTFvIAnybsVlOVk5TEHK0MQx+MJ3jqyqB lRne+oFkxfBT3lDtNHr9JK6Ak6iVVnO+zCzqT5crUWlK630HMOSbbE6Ja5Ilq112Pj/M Ot99+G/fdb1/HSwTSUtcon++9Qxf3+01pYyHRPJzLraaf9x//jtRI1Fv1wmKtbuiquBM Zqt4kj9uH/77GxIj8gWtydM9Gad/KRzxs/i0E23b2EyunrWaNHbwFry9bBLJlKSG4i2B NaEdwLo8aNAt1LgWKrKF4uISZ8wCMBIWR+HFl4lXWUiljYZc4orI3epjK2S5lKSSFt4i gO/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=GHq5pwQKz5lVl73l5jrCAH09xq8t0ttkGVDLBO4YoAE=; b=7O28WV4p9ZKBPVvqIrRzK/0m+eJ7wOwUqOWi6t/ICIKgHePhpugW9jeblppu2uacHb tuMDo1dHu7o2N+wtGMVwMjpspIbyE9L7mHfqrpV0B+J3MZdTKGyQVixHmGH1Pet7vPx9 B/U0kkfAZc2r9DMgnN/ZsdyFM6HoHap7HSgzA3njfKWTJfAzqAGSoPgdnyHmZBUW/58U DbvhwMZkgo+rScFGGRutI23/5tCmRWmo/gFC06jmfvFP7UqKpBiOw4kS1V8SwPo1zOm3 ZHd7mBvdUoLqjxBOQSVeZ/NAyrFzw7tCDqGn5uiw4GjkXTqQcOcRu3KuE7k/XZo3FAWx 4llg== X-Gm-Message-State: ACgBeo2qhiDj5/g7Zs+XjxdtzPOg8/WUIjcZgYbQkQC5zIQAm2mLVFOp 6zDje+gItRKvdldGiO/21ww= X-Google-Smtp-Source: AA6agR5o+euQP9KOB5+ZNvg8XKhOFLBM+TQSA2pAfID8/PW5tUyjBSMRVh/xuR/wi2UiJl5q7tvNnA== X-Received: by 2002:a17:90b:3901:b0:1fd:73f8:1922 with SMTP id ob1-20020a17090b390100b001fd73f81922mr5347174pjb.211.1662659639818; Thu, 08 Sep 2022 10:53:59 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id e6-20020a17090301c600b0017545ee859esm69819plh.8.2022.09.08.10.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 10:53:59 -0700 (PDT) Date: Thu, 8 Sep 2022 10:53:56 -0700 From: Nathan Bossart To: Bharath Rupireddy Cc: Cary Huang , PostgreSQL Hackers , SATYANARAYANA NARLAPURAM Subject: Re: Switching XLog source from archive to streaming when primary available Message-ID: <20220908175356.GA2225645@nathanxps13> References: <165610083853.2517.17504309233817317001.pgcf@coridan.postgresql.org> <20220906215704.GA2084086@nathanxps13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Sep 08, 2022 at 05:16:53PM +0530, Bharath Rupireddy wrote: > On Wed, Sep 7, 2022 at 3:27 AM Nathan Bossart wrote: >> It's not clear to me how this is expected to interact with the pg_wal phase >> of standby recovery. As the docs note [0], standby servers loop through >> archive recovery, recovery from pg_wal, and streaming replication. Does >> this cause the pg_wal phase to be skipped (i.e., the standby goes straight >> from archive recovery to streaming replication)? I wonder if it'd be >> better for this mechanism to simply move the standby to the pg_wal phase so >> that the usual ordering isn't changed. > > It doesn't change any behaviour as such for XLOG_FROM_PG_WAL. In > standby mode when recovery_command is specified, the initial value of > currentSource would be XLOG_FROM_ARCHIVE (see [1]). If the archive is > exhausted of WAL or the standby fails to fetch from the archive, then > it switches to XLOG_FROM_STREAM. If the standby fails to receive WAL > from primary, it switches back to XLOG_FROM_ARCHIVE. This continues > unless the standby gets promoted. With the patch, we enable the > standby to try fetching from the primary, instead of waiting for WAL > in the archive to get exhausted or for an error to occur in the > standby while receiving from the archive. Okay. I see that you are checking for XLOG_FROM_ARCHIVE. >> Shouldn't the last_switch_time be set when the state machine first enters >> XLOG_FROM_ARCHIVE? IIUC this logic is currently counting time spent >> elsewhere (e.g., XLOG_FROM_STREAM) when determining whether to force a >> source switch. This would mean that a standby that has spent a lot of time >> in streaming replication before failing would flip to XLOG_FROM_ARCHIVE, >> immediately flip back to XLOG_FROM_STREAM, and then likely flip back to >> XLOG_FROM_ARCHIVE when it failed again. Given the standby will wait for >> wal_retrieve_retry_interval before going back to XLOG_FROM_ARCHIVE, it >> seems like we could end up rapidly looping between sources. Perhaps I am >> misunderstanding how this is meant to work. > > last_switch_time indicates the time when the standby last attempted to > switch to primary. For instance, a standby: > 1) for the first time, sets last_switch_time = current_time when in archive mode > 2) if current_time < last_switch_time + interval, continues to be in > archive mode > 3) if current_time >= last_switch_time + interval, attempts to switch > to primary and sets last_switch_time = current_time > 3.1) if successfully switches to primary, continues in there and for > any reason fails to fetch from primary, then enters archive mode and > loops from step (2) > 3.2) if fails to switch to primary, then enters archive mode and loops > from step (2) Let's say I have this new parameter set to 5 minutes, and I have a standby that's been at step 3.1 for 5 days before failing and going back to step 2. Won't the standby immediately jump back to step 3.1? I think we should place the limit on how long the server stays in XLOG_FROM_ARCHIVE, not how long it's been since we last tried XLOG_FROM_STREAM. >> I wonder if the lower bound should be higher to avoid switching >> unnecessarily rapidly between WAL sources. I see that >> WaitForWALToBecomeAvailable() ensures that standbys do not switch from >> XLOG_FROM_STREAM to XLOG_FROM_ARCHIVE more often than once per >> wal_retrieve_retry_interval. Perhaps wal_retrieve_retry_interval should be >> the lower bound for this GUC, too. Or maybe WaitForWALToBecomeAvailable() >> should make sure that the standby makes at least once attempt to restore >> the file from archive before switching to streaming replication. > > No, we need a way to disable the feature, so I'm not changing the > lower bound. And let's not make this GUC dependent on any other GUC, I > would like to keep it simple for better usability. However, I've > increased the default value to 5min and added a note in the docs about > the lower values. > > I'm attaching the v3 patch with the review comments addressed, please > review it further. My general point is that we should probably offer some basic preventative measure against flipping back and forth between streaming and archive recovery while making zero progress. As I noted, maybe that's as simple as having WaitForWALToBecomeAvailable() attempt to restore a file from archive at least once before the new parameter forces us to switch to streaming replication. There might be other ways to handle this. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com