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 1rvES3-00Acm7-KG for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Apr 2024 10:48:12 +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 1rvES2-00CiKW-DG for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Apr 2024 10:48:10 +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 1rvES2-00CiKN-1H for pgsql-hackers@lists.postgresql.org; Fri, 12 Apr 2024 10:48:10 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rvERz-000XGm-J7 for pgsql-hackers@postgresql.org; Fri, 12 Apr 2024 10:48:09 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-5a9df87e7bbso555290eaf.0 for ; Fri, 12 Apr 2024 03:48:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712918885; x=1713523685; darn=postgresql.org; 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=9VYxhfrjOesq23SOq/2i4wK6i4xx+ML3tYP6U9taJUM=; b=cUqV4swbE/Du4A90zVrtdUeZbG80rlVRzUIvrZEh+iXiWtElIo9GlYbR/RUfc+QXGT ieejpCcWrqgvWN0YsJUnAZIhe0OFNt7sTdt0Lq4TZ+onRmxWiWLnrdcAGLEMdLBpqjrq gagliyErpjd1ioPr3d4Iu+QMW6cnbB11luZTCkmk9zKVx0PikjxeYPMBprnPPRYx7Lpg PyTu3puvLHAeIOMmAk77wwVTjuVH2eHV6yN9bC6SrUfrPwd56PvxFSSZGFrk/VrcV9+T gIK9eKFPmCPZnN4Hrv7aJAkWU3pUCNMIQcIwvN9kc9IiHfHAvCjrPXqV8hIIR2MyZbV1 kT5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712918885; x=1713523685; 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=9VYxhfrjOesq23SOq/2i4wK6i4xx+ML3tYP6U9taJUM=; b=nJnoGMEGvN13HFGZwxoikbzA05z4C3m2YMBOEwhYffRPqaAx7zE8S8EZkhEQ8lO/9c pd8NjXjW/p2Zc2gNQgsa6iJAHrEbQvUoern3e4wbqfb9sEP+ZLfJErZAXg/HyRePdEH+ ZyDMjW5ztvxWEY9XsfPljr76QrFlr0MeFeAlKISc5iUZLmBQGx35VGk6ygGfej77NEuY PMXPIeK5E3LC4uXPKtOw6r0NhznXvgpYB+TZwwz1shDsovYYazODARJfIUjPDu8X9hVa YzVLDH6+DAc0oXk4HurD/FvVUM5pH/CWWPzkwPTgWjAPD1x0/1ORiSLTPwOaOLGD7GmJ diVA== X-Forwarded-Encrypted: i=1; AJvYcCXeyMjrHjcjxH6F61QIbgAiPd6V8sNMCoanRwtXe6zCMXIx0IUgYHnS2b8BXiMhC0kBmV7n3IXy+HfOf8VprjOsL6HXiIP7MkDhXzu3 X-Gm-Message-State: AOJu0Yxe9nluyEU0GLD1swBqKZrPNMBhWQHmXgv8gKys5eojuZGcsvIb zyni4NnsoLPwA3jfFgyCMchLb3/djlST5h2bU7Kwwtcxs5BIkE9l9wNAIqd3gxaLYXQKWVnXCZq /2Xi/79Qe6Wj6tc0fgWlcpPH5LNs= X-Google-Smtp-Source: AGHT+IEhNmWwtokfLl6DT+S+9b7e3AxK5aI9MDD2SVm+dY92QXew0bRwVkMPAd/SgPrdbb5NwcG0rYR7fmMcg2LwWQY= X-Received: by 2002:a05:6871:e007:b0:22e:a036:c33a with SMTP id by7-20020a056871e00700b0022ea036c33amr2404808oac.6.1712918885496; Fri, 12 Apr 2024 03:48:05 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Amit Kapila Date: Fri, 12 Apr 2024 16:17:53 +0530 Message-ID: Subject: Re: promotion related handling in pg_sync_replication_slots() To: shveta malik Cc: Bharath Rupireddy , pgsql-hackers , "Zhijie Hou (Fujitsu)" 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 On Fri, Apr 12, 2024 at 7:57=E2=80=AFAM shveta malik wrote: > > On Sat, Apr 6, 2024 at 12:25=E2=80=AFPM Bharath Rupireddy > wrote: > > > > On Fri, Apr 5, 2024 at 10:31=E2=80=AFAM shveta malik wrote: > > > > > > Please find v2. Changes are: > > > > Thanks for the patch. Here are some comments. > > Thanks for reviewing. > > > > 1. Can we have a clear saying in the shmem variable who's syncing at > > the moment? Is it a slot sync worker or a backend via SQL function? > > Perhaps turn "bool syncing;" to "SlotSyncSource sync_source;" > > > > typedef enum SlotSyncSource > > { > > SLOT_SYNC_NONE, > > SLOT_SYNC_WORKER, > > SLOT_SYNC_BACKEND, > > } SlotSyncSource; > > > > Then, the check in ShutDownSlotSync can be: > > > > + /* > > + * Return if neither the slot sync worker is running nor the fu= nction > > + * pg_sync_replication_slots() is executing. > > + */ > > + if ((SlotSyncCtx->pid =3D=3D InvalidPid) && > > SlotSyncCtx->sync_source !=3D SLOT_SYNC_BACKEND) > > { > > I don't know if this will be help, especially after fixing the race condition I mentioned. But otherwise, also, at this stage it doesn't seem helpful to add the source of sync explicitly. > > 2. > > SyncReplicationSlots(WalReceiverConn *wrconn) > > { > > + /* > > + * Startup process signaled the slot sync to stop, so if meanwhile= user > > + * has invoked slot sync SQL function, simply return. > > + */ > > + SpinLockAcquire(&SlotSyncCtx->mutex); > > + if (SlotSyncCtx->stopSignaled) > > + { > > + ereport(LOG, > > + errmsg("skipping slot synchronization as slot sync > > shutdown is signaled during promotion")); > > + > > > > Unless I'm missing something, I think this can't detect if the backend > > via SQL function is already half-way through syncing in > > synchronize_one_slot. So, better move this check to (or also have it > > there) slot sync loop that calls synchronize_one_slot. To avoid > > spinlock acquisitions, we can perhaps do this check in when we acquire > > the spinlock for synced flag. > > If the sync via SQL function is already half-way, then promotion > should wait for it to finish. I don't think it is a good idea to move > the check to synchronize_one_slot(). The sync-call should either not > start (if it noticed the promotion) or finish the sync and then let > promotion proceed. But I would like to know others' opinion on this. > Agreed. --=20 With Regards, Amit Kapila.