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 1rrvo2-003ep7-PU for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2024 08:17:15 +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 1rrvo0-00GbEl-Kh for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2024 08:17:12 +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 1rrvo0-00GbEc-6p for pgsql-hackers@lists.postgresql.org; Wed, 03 Apr 2024 08:17:12 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rrvnx-000OEc-Ca for pgsql-hackers@lists.postgresql.org; Wed, 03 Apr 2024 08:17:11 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-415446af364so3647445e9.0 for ; Wed, 03 Apr 2024 01:17:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712132227; x=1712737027; darn=lists.postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=UGIQhSJp/OlY9XFySiZyeTfpPC8dYhdNGgGcZEM0YM8=; b=Is+7BcEJHJBEmVxGejD6sgBSOY/bjTygRHMVSGEyDNjvunOt3expZdGKpGF7ccmnzk ytlAEkSCjJQUHtCcr5jDYJquSbRnBLZ2twL511Edy6HuC7+jWQ53pADsyqXeaV1snvYM O7PZvC/v7pkFTZ0IuDCnIFoBUTLaTwQ8PH65bG17Wh3cyd6w+EymPgUANgJ/BQbECg1J NcXN/OvfN268rFWPpUveHl+lAn3xbd1b3ZXqHIykbEtRxqGMblVDWlsV6fxCOxoPhLXC LCPLQJfZOYJMjSdV1d5WiTfJGhGbTqF7D4n03TR7KhryOrxdXRfACWsX9rckaE6n23+i LPlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712132227; x=1712737027; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UGIQhSJp/OlY9XFySiZyeTfpPC8dYhdNGgGcZEM0YM8=; b=XH8fXlUfaU9pYFYVlRPWtHBE3mtirj+ihnbv1SdHd3JJPWrBLOrFYOSdB1ytg0UwsW prpt2yN6oBSGie0j7giYdZzRPaGTq6JgxAapkwSKgH/vIVBQtJLJb9sDI7CoYMTWAO0+ Ca52EhefnssLwdchFMcZOYIWPbOQu97HBqQQVhfRWMc30yvbV2YdTf4aP8cPvEdginXh sgK+0dhuEq4pdE8ONppv+i8kv1cDEQcM1GmufO32tLhD7vbaErs73xJ63ehl2l2KqKKI ceLbRNgsNQjp8CjItB4CmQgCvXvx9hROaMYiaqHXEIPySCJ8A4XI37rIzaLnQxiocANz E+CQ== X-Forwarded-Encrypted: i=1; AJvYcCVF58XDLAvFVJV8Abcq17WcCooqBagFWZaN9eLshRnQtR6Xilwip+R0NA5V9SbKE4EkobvrFfnrsmwo1w941ND8H7LTn1DpyU/p7A2kfo2v6Fh2 X-Gm-Message-State: AOJu0YxA2RLb7t45WtBAtAUxc8slDy9TEwMXXhSHNPkwtUAa78RK6vdv jI/xEu/YNySvLN6NRjetnm7x6q+9XmzXgHUygfNEL8CjVVkyH4fu X-Google-Smtp-Source: AGHT+IGXav4SjSYOBvjJerHp69rSIf7BAG4UsSrPoGr9QTE28V2WH97r0KwgGKyZUHNWT2Vk+U7CkA== X-Received: by 2002:a05:600c:3b9f:b0:414:7fcc:7106 with SMTP id n31-20020a05600c3b9f00b004147fcc7106mr1608298wms.4.1712132227129; Wed, 03 Apr 2024 01:17:07 -0700 (PDT) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-236-134-5.eu-west-3.compute.amazonaws.com. [15.236.134.5]) by smtp.gmail.com with ESMTPSA id l20-20020a05600c1d1400b004146a304863sm24085770wms.34.2024.04.03.01.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 01:17:06 -0700 (PDT) Date: Wed, 3 Apr 2024 08:17:05 +0000 From: Bertrand Drouvot To: Bharath Rupireddy Cc: shveta malik , Masahiko Sawada , Amit Kapila , Nathan Bossart , PostgreSQL Hackers Subject: Re: Introduce XID age and inactive timeout based replication slot invalidation Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Wed, Apr 03, 2024 at 11:17:41AM +0530, Bharath Rupireddy wrote: > On Wed, Apr 3, 2024 at 8:38 AM shveta malik wrote: > > > > > > Or a simple solution is that the slotsync worker updates > > > > inactive_since as it does for non-synced slots, and disables > > > > timeout-based slot invalidation for synced slots. > > > > I like this idea better, it takes care of such a case too when the > > user is relying on sync-function rather than worker and does not want > > to get the slots invalidated in between 2 sync function calls. > > Please find the attached v31 patches implementing the above idea: Thanks! Some comments regarding v31-0002: === testing the behavior T1 === > - synced slots don't get invalidated due to inactive timeout because > such slots not considered active at all as they don't perform logical > decoding (of course, they will perform in fast_forward mode to fix the > other data loss issue, but they don't generate changes for them to be > called as *active* slots) It behaves as described. OTOH non synced logical slots on the standby and physical slots on the standby are invalidated which is what is expected. T2 === In case the slot is invalidated on the primary, primary: postgres=# select slot_name, inactive_since, invalidation_reason from pg_replication_slots where slot_name = 's1'; slot_name | inactive_since | invalidation_reason -----------+-------------------------------+--------------------- s1 | 2024-04-03 06:56:28.075637+00 | inactive_timeout then on the standby we get: standby: postgres=# select slot_name, inactive_since, invalidation_reason from pg_replication_slots where slot_name = 's1'; slot_name | inactive_since | invalidation_reason -----------+------------------------------+--------------------- s1 | 2024-04-03 07:06:43.37486+00 | inactive_timeout shouldn't the slot be dropped/recreated instead of updating inactive_since? === code CR1 === + Invalidates replication slots that are inactive for longer the + specified amount of time s/for longer the/for longer that/? CR2 === + true) as such synced slots don't actually perform + logical decoding. We're switching in fast forward logical due to [1], so I'm not sure that's 100% accurate here. I'm not sure we need to specify a reason. CR3 === + errdetail("This slot has been invalidated because it was inactive for more than the time specified by replication_slot_inactive_timeout parameter."))); I think we can remove "parameter" (see for example the error message in validate_remote_info()) and reduce it a bit, something like? "This slot has been invalidated because it was inactive for more than replication_slot_inactive_timeout"? CR4 === + appendStringInfoString(&err_detail, _("The slot has been inactive for more than the time specified by replication_slot_inactive_timeout parameter.")); Same. CR5 === + /* + * This function isn't expected to be called for inactive timeout based + * invalidation. A separate function InvalidateInactiveReplicationSlot is + * to be used for that. Do you think it's worth to explain why? CR6 === + if (replication_slot_inactive_timeout == 0) + return false; + else if (slot->inactive_since > 0) "else" is not needed here. CR7 === + SpinLockAcquire(&slot->mutex); + + /* + * Check if the slot needs to be invalidated due to + * replication_slot_inactive_timeout GUC. We do this with the spinlock + * held to avoid race conditions -- for example the inactive_since + * could change, or the slot could be dropped. + */ + now = GetCurrentTimestamp(); We should not call GetCurrentTimestamp() while holding a spinlock. CR8 === +# Testcase start: Invalidate streaming standby's slot as well as logical +# failover slot on primary due to inactive timeout GUC. Also, check the logical s/inactive timeout GUC/replication_slot_inactive_timeout/? CR9 === +# Start: Helper functions used for this test file +# End: Helper functions used for this test file I think that's the first TAP test with this comment. Not saying we should not but why did you feel the need to add those? [1]: https://www.postgresql.org/message-id/OS0PR01MB5716B3942AE49F3F725ACA92943B2@OS0PR01MB5716.jpnprd01.prod.outlook.com Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com