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.96) (envelope-from ) id 1ve8Mi-009GRE-0G for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2026 09:01:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1ve8Mg-006SpO-31 for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Jan 2026 09:01:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ve8Mg-006SpG-1e for pgsql-hackers@lists.postgresql.org; Fri, 09 Jan 2026 09:01:03 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ve8Mf-0052YQ-2k for pgsql-hackers@lists.postgresql.org; Fri, 09 Jan 2026 09:01:02 +0000 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-382fa663249so28802781fa.0 for ; Fri, 09 Jan 2026 01:01:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767949260; x=1768554060; darn=lists.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=JxnaBngOZdMvmWt2FFBFUc8sq3oQPvUIVC6pvB0XMco=; b=Ovahs7jCytnBTEkcs8zks+VZR06KWMQAbjklVdsR5VlUn2ifehlgKoY19mnD7daHXp 7lkaebfltGCOcNmIY9WSwbMmmKTomY86yvoRFA5fLHORYPZvGd6ONLKCTWh1z48KqIJr 0fYG8z8VT/rE6H8ROjXd6U7DPS5h8V2sAW6Nl15PJEhowgqheNtWr6+bsfYgU7Xf0XWr ADGrV4M4nucfCBAdEUhG7UZXWQS9oAp6I0MYyUgbTqVBT4d1igA8eudw7W/nPZ3LQt4M B6R+gUkp3ava8iz8jUdPHiBu4yrEUQMbAb5P2gAmjJN6aA2uTg36KUoefZ/QaaTTc031 0yDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767949260; x=1768554060; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JxnaBngOZdMvmWt2FFBFUc8sq3oQPvUIVC6pvB0XMco=; b=QHapfKLe0sN6fP0cUUNAJMy9tSKeWcyjGc5suKb+E2eRm18ELCvDy3wpBCFo0hwVqn WH7oPwyHWV08Fd7J8nybmo9uDdV1Wzsu7H5SCRWGennl+kDCp1yaRbzEwKU6OCNDSYLK mlyRG6IbhSrK4PaegkXD6hCGUbTyUqvusdRR/okpb5sRnys4u4YGMw8UiNwsGBxPXo5k yOXL2QTXG5tVCQVu0uPJy5cwPRJwQ/kKMkYRetgsj0RerTvj1lr7PAPQ2JayJjcj7AzL jKy4Slg0G7V2zqf4wAP4iTCNe2SOV97VWO70GEo4npYlIkTxKBtKPvkq/ABZmHB6ADYB l+rw== X-Forwarded-Encrypted: i=1; AJvYcCVP7T+7g4lZ8TjiUZaKh9fLNbdu1/KpWDFt8xdRYUxuMfsT1apSwZOwMfeOzX6gA7wFQ5usbaqJxfMNrkv5@lists.postgresql.org X-Gm-Message-State: AOJu0YwkgsXcMyEL+fySlNlsZ2qfiIpro630T+xF2q4qU5HvhdYwZiig XITcTqsOhGMb8c5pI3w/C2LRSqzfRkern083+cSwxVbR8QbCaUuddiiqraUZiV8zZejsbEpd6tg 3M4qKMfnYJqOONfu50YbCDNWUO2BUpSU= X-Gm-Gg: AY/fxX6tarxubgKitI+QjJfzTS87Q+i/yuRFLihohh1RS2LgXwSpcIKeiY7fjFw4hZq CsY2jSQvxScY+kIvf3pXFAqh8z7wZ+ACZvAEhoGZ2nG6XKsqsXiujViQgrhOTS1AzoJMI/pD6Br mXjZWO468qNW6Hoyk68ABijcrvR5HB0wzwLhMapNQSzoEPkxQj9k8HlzILlR6pKFTrCMAnctH53 VyRY3Q66IeWfIp9z/Il5LuPRj2DVVg8b4RxfC/uwem8FUQfv02r/oa2jOMPriW+8PFo1bymeIxj eMePcay80AnYqz3qobLoGZ4w5KqrPg== X-Google-Smtp-Source: AGHT+IGU5c4vQqZ9xBBbt8lZOZkClypgcQjJ8ktD3j7z+jh4R/aIH9kR/ghpOK2AQiUqHHzJOFy2JwWa+ll0IXbDXKA= X-Received: by 2002:a05:651c:f0f:b0:37f:c5ca:a0dc with SMTP id 38308e7fff4ca-382ff67755bmr22950241fa.9.1767949259589; Fri, 09 Jan 2026 01:00:59 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Amit Kapila Date: Fri, 9 Jan 2026 14:30:47 +0530 X-Gm-Features: AQt7F2p3U6rlIHJ1EdvI2eB40Z4Ln1TFvx45C9aHfOKOcxTgs3MWx1kiEikt4NY Message-ID: Subject: Re: [Patch] add new parameter to pg_replication_origin_session_setup To: shveta malik Cc: "Zhijie Hou (Fujitsu)" , "Hayato Kuroda (Fujitsu)" , Doruk Yilmaz , "pgsql-hackers@lists.postgresql.org" 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 Mon, Jan 5, 2026 at 4:00=E2=80=AFPM shveta malik wrote: > > On Mon, Jan 5, 2026 at 3:15=E2=80=AFPM shveta malik wrote: > > > > On Tue, Dec 23, 2025 at 2:24=E2=80=AFPM Zhijie Hou (Fujitsu) > > wrote: > > > > > > Hi, > > > > > > When testing the new parameter in pg_replication_origin_session_setup= (), I > > > noticed a bug allowing the origin in use to be dropped. The issue ari= ses when > > > two backends set up the same origin; if the second backend resets the= origin > > > first, it resets the acquired_by flag regardless of whether the first= backend is > > > using it. This allows the origin to be dropped, enabling the slot in = shared > > > memory to be reused, which is unintended. > > > > > > About the fix, simply adding a check for acquired_by field does not w= ork, > > > because if the first backend resets the origin first, it still risks = being > > > dropped while second backend uses it. > > > > > > To fully resolve this, I tried to add a reference count (refcount) fo= r the > > > origin. The count is incremented when a backend sets up the origin an= d > > > decremented upon a reset. As a result, the replication origin is only= dropped > > > when the reference count reaches zero. > > > > > > Thanks to Kuroda-San for discussing and reviewing this patch off-list= . > > > > > > > Thanks Hou-San and Kuroda-San. > > > > What should be the expected behavior when Session1 resets the origin > > (changing acquired_pid from its own PID to 0), while Session2 is > > already connected to the origin and Session3 also attempts to reuse > > the same origin? > > > > Currently it asserts: > > > > Session1: > > select pg_replication_origin_create('origin'); > > SELECT pg_replication_origin_session_setup('origin'); > > > > Session2: > > SELECT pg_replication_origin_session_setup('origin',48028); > > > > Session1: > > SELECT pg_replication_origin_session_reset(); > > > > Session3: > > SELECT pg_replication_origin_session_setup('origin'); > > This asserts at: > > TRAP: failed Assert("session_replication_state->refcount =3D=3D 0"), Fi= le: > > "origin.c", Line: 1231, PID: 48037 > > > > I checked the behavior on HEAD. Session3 is able to set up the origin > and sets its own PID in acquired_pid. But it is unclear to me which > PID should be recorded in acquired_pid - Session2=E2=80=99s PID, since it= set > up the origin earlier, or Session3=E2=80=99s PID. Or does this even make = any > difference? > > I found one more related issue on HEAD, sharing it here: > > When the first backend creates and sets up the origin, followed by a > second backend setting it up, and then the first backend resets it > while the second backend attempts to drop it, an assertion is > triggered: > TRAP: failed Assert("session_replication_state->roident !=3D > InvalidRepOriginId"), File: "origin.c", Line: 1257, PID: 48438 > Can we address these problems by prohibiting leader worker to reset when pa workers are still associated with the origin? The way for leader to know if pa workers are associated with origin is by checking following condition: acquired_by =3D=3D MyProcpid AND refcount > 1. --=20 With Regards, Amit Kapila.