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 1vchr4-009r62-01 for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jan 2026 10:30:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vchr2-001MrK-2s for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jan 2026 10:30:29 +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 1vchr2-001Mr6-1R for pgsql-hackers@lists.postgresql.org; Mon, 05 Jan 2026 10:30:29 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vchr1-004IzB-2u for pgsql-hackers@lists.postgresql.org; Mon, 05 Jan 2026 10:30:28 +0000 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-34c565c3673so832717a91.0 for ; Mon, 05 Jan 2026 02:30:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767609027; x=1768213827; 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=+2KBH4rBGphG+s3bNKNGAsDDQgKXdHMuSuTojt5tlWI=; b=Yf0zgpuDQO6nANs4Unor5/3cAGQfsRaENc8H0/jJc3ramhHq7LAVOQzFY21m0TiLM+ L9c6yC1nZj/B0pwM/5NxoHemlvO0aGUHK/7dX/QoDhxYDYPETi0zEZa0uTQ5DDKzZ7z7 nQ7gP9nv877WuMRHg9H7ky1YhqD8wmQOndIkyS/gS9RRREJ1M2fwdpXLAKUnanPF3gW1 WiPqJigY5FjeXRPHlq2aldPWGwGj+i4nwc10Nt1gs0NLcJspF3gqq2+eSpxdT/YEUV7k a3cJ0tHGbVa3AcVXUbEdZlHAiP1WquRki87YHd6cbtPeUHshJfA74VgDFvpZ9AwHX01q 9UnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767609027; x=1768213827; 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=+2KBH4rBGphG+s3bNKNGAsDDQgKXdHMuSuTojt5tlWI=; b=R5RElqs+/sH+OjsITZ0TRRpG6ent8jidx/Vyp9gh2IWp5KaHMmm+gUFs6ubNyR0KUa b397EXIYJgBzzU4R30LejBzYihR0Zv3NuUQbbh4PQh54frFRIbOiwD8ZsS8snEUmaEIU jlRMFpTxcUbdiTSg1913V3QAXXKErFmoQezZrmejgl8APHl2299ybutB1L2W+SYJTUZQ h6u5g1xfTicGjTQXJ/SheKRHKR8hXZQi/Bb3aF/C1UokmmQJ+4ak8dwZTcly21tQVCJ+ 6y7Si8+V9EInfXP6nYRYdn3IxGVfe5LSVa4hV0KYk2WXq3rJD3rX6WPfRDZO3b7GON80 I6rw== X-Forwarded-Encrypted: i=1; AJvYcCXzvNEk+1mOtxS4rbAip85i91KtZs8z8V54bOzSGmv8vDsLx6RpKSfJ5pzYqhyPFucCHlvag7Dcag5G9lyQ@lists.postgresql.org X-Gm-Message-State: AOJu0YysO5meVXi2Lv8h1BCi7TCOxJrfgS8l9qGCY89NrDr4a+QimDNV E4q49fjWTzxoLxEpdjacONxrWO4TwcqH26XzoPAUFaOM/XhPC9LCBi/7FfaTFwzrYJxNLg6XwP/ KyiXcmstn1P/3VSItF5jE64GIP+Nvx+jDrRJJ X-Gm-Gg: AY/fxX5vSHIqCg/a6TfCnQltrKNO6T3IAQjwUkWD2JQznlPrWW4IWyijAoJoUTEgpTk OstfIF0/Wx2zlp0udEJdjQ3cjpoLVev2kvlqDzZlMnqhVTX2chJ/bKk/dQ9T9W9cgxalrPedJCz RmGNM0RNlo2GKJqJdpfEvsC9jWJ+wmaHmKTmtS0IjFu9G/TOjKiUhKinfMjnWwMLB5joSmjSc3b 0Tvk4u9NDYa3a0wO9K04gWSqoMtA5AG8GmCfTADDMEfQ+CBvi3vsFVbu+RFqpngtxBUh809arSB imGKJj7pojM9UuuzA3Lbkb9lCYOnGwNfYVH2Bam2eyINrAbG5QU= X-Google-Smtp-Source: AGHT+IGJ0ANgka44T3abMBADP1ochgNxGqoRAxqXn7iS+XumqgjRxfXGgxgqLTuArXGTxNxd4r8ZmKd1sWslQ8gE2CA= X-Received: by 2002:a17:90b:4d81:b0:34c:2aac:21a7 with SMTP id 98e67ed59e1d1-34f45271dd8mr6287837a91.7.1767609026512; Mon, 05 Jan 2026 02:30:26 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: shveta malik Date: Mon, 5 Jan 2026 16:00:15 +0530 X-Gm-Features: AQt7F2ooxyLmSgTR9iod17LKHBU1lRLTj0EkilGXZvBJpAdF21EVhH7TeZ5aqFU Message-ID: Subject: Re: [Patch] add new parameter to pg_replication_origin_session_setup To: "Zhijie Hou (Fujitsu)" Cc: "Hayato Kuroda (Fujitsu)" , Amit Kapila , Doruk Yilmaz , "pgsql-hackers@lists.postgresql.org" , shveta malik 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 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 arise= s when > > two backends set up the same origin; if the second backend resets the o= rigin > > first, it resets the acquired_by flag regardless of whether the first b= ackend is > > using it. This allows the origin to be dropped, enabling the slot in sh= ared > > memory to be reused, which is unintended. > > > > About the fix, simply adding a check for acquired_by field does not wor= k, > > because if the first backend resets the origin first, it still risks be= ing > > dropped while second backend uses it. > > > > To fully resolve this, I tried to add a reference count (refcount) for = the > > origin. The count is incremented when a backend sets up the origin and > > decremented upon a reset. As a result, the replication origin is only d= ropped > > 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"), File= : > "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 s= et up the origin earlier, or Session3=E2=80=99s PID. Or does this even make an= y 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 thanks Shveta