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 1ujKsx-006FVK-Pq for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Aug 2025 16:51:36 +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 1ujKsw-00ATv4-IU for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Aug 2025 16:51:34 +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 1ujKsw-00ATuv-36 for pgsql-hackers@lists.postgresql.org; Tue, 05 Aug 2025 16:51:34 +0000 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ujKst-000uKa-1p for pgsql-hackers@lists.postgresql.org; Tue, 05 Aug 2025 16:51:33 +0000 Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-741a6272c8fso668988a34.3 for ; Tue, 05 Aug 2025 09:51:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20230601.gappssmtp.com; s=20230601; t=1754412689; x=1755017489; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=3R2gLdu1PsozJWGYykuxuSVc2aaokO0UVeMCqpqxKZo=; b=LF2qcHHmnErGlCyvGsV/M8Oep9qVoxRVo4ZOq+E14FJfxJtBOWNJAYesXv+k1WhqVo 6CLAPt3IYavs9ygpFPbdtV/6ddPWP1A9umWkwc2qbvh8Q6ufvbeUqkxW1wiEYvBpuYA3 Q0G9rAg+AD7ms0JCbnjwaaVorot9yTrkB9MOM3SFh64RkN3TT11clY35xUsXKoU1vWH2 i2nHh706jbHT3oU8Kd+PlBpYcmBsDbeo8NlDqK6/6m81Djy/iGm+IxlLB2yPhlTOe9Cv B9edL4vvUaC2wgyqqh5JmX1PSfPGqONOGiHKh9iyWGK2CVrq/PIE9CGfkrWiywBDw97y xJMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754412689; x=1755017489; h=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=3R2gLdu1PsozJWGYykuxuSVc2aaokO0UVeMCqpqxKZo=; b=tVwHMm+p+75wMPrp6LM1c8ZJIGjC06p+8SdmQjma+PA+eon/A1z4qcHEa4JGpvLShl 5m8XYaHSQWr12ihV6K5v7fVkXFekcl7QWIcZXL7kQSQUhA4mDYV7FhH8VyZOzq1Klbu6 yqww0GQoOjR0r5C/ervrR7U12nnz3j0rc+XDUCOQWO2u/2OUoy+A8YTw8m7TWpHYIMZO kpr/XZPkHMlr7EBYhtYKvNWB8wUUQfamso+w1kP3Ry6qFDHIGqlaS5+eFeGZGOwscT+w +VZ48ACXPa8Nq0kZQSZiaZgVQ1+HPOXAqy7CU7USk3OCeVrTUFkxaNcgOF4Yv5YFjLfF DYpQ== X-Forwarded-Encrypted: i=1; AJvYcCXa1XVN7Fh7XxLiUmIuPAmStsTJQQV1dBCV8v2HWS3Vn8C3EA6HkoHvVKijqHk4vg4Z52HdsluDRc63XsjL@lists.postgresql.org X-Gm-Message-State: AOJu0YyF4s511dJNadMI/plFauh8QNmjla0Jnc1m+0cWfAUpJDy5c29O zk1f25as6X6F6WYkLoUf1kiNbfxUiqsNsAMRGoAj4N/pfKLlXugt4BsEWGxBestG2c1/83S9iuU x24Rggx38VHjbmpZmWo8Fw5DbdIlTfoyOjS8JSd8waQ== X-Gm-Gg: ASbGnctmpxNPld2hIjqJtjiNBg8V99Gner8C16/Js101nkHDlJYuSWp5gMAG4VNY9V/ wUv5CL2XxzjOZ0qwWDqOhuft3/zz6urMMoxqJwLBQQtinIIKBy8iaLHc2ZW1I0uwWqHjTuxaYW+ f0rSxdlH7Ias/eF3K3O4V3F+fuAHbifz1NHjThzDYYgv4zgDHNZfXl9qndnRsUgnkljiNEiJDke lV4zosj8P2CEglw X-Google-Smtp-Source: AGHT+IG5NpySJN63bgTUdwD4XplDRtQvLOPLQHFfKjzd0xXwu8kTqTGFefYMHS4AbUGHz+io+V9P0Y3kg79/fgpeveI= X-Received: by 2002:a05:6808:5285:b0:402:18a1:843f with SMTP id 5614622812f47-433f03795b6mr8405037b6e.32.1754412688766; Tue, 05 Aug 2025 09:51:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Paul A Jungwirth Date: Tue, 5 Aug 2025 09:51:16 -0700 X-Gm-Features: Ac12FXy_swVY6sUZS8yReZEq8G4II2u1K4z2EJWDQfq5_8eNEPc3DwqCedZvpvQ Message-ID: Subject: Re: ReplicationSlotRelease() crashes when the instance is in the single user mode To: "Hayato Kuroda (Fujitsu)" Cc: Amit Kapila , Michael Paquier , "David G. Johnston" , "Zhijie Hou (Fujitsu)" , Tom Lane , Bertrand Drouvot , "pgsql-hackers@lists.postgresql.org" , Mutaamba Maasha Content-Type: multipart/mixed; boundary="0000000000001b2f9c063ba109ce" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001b2f9c063ba109ce Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Mutaamba (cc'd) and I reviewed this patch together. To summarize the patch and thread so far: The patch adds a new function, CheckSlotIsInSingleUserMode. If true then we raise an error. Otherwise we would trip an assert in ReplicationSlotRelease requiring the slot to have an active_pid, which is never set in single-user mode. We do want to support pg_drop_replication_slot in single-user mode, but not other replication slot functions. In particular, advancing the slot may call non-core code, which seems risky in single-user mode. The patch does not apply. The attached v5 fixes it with a small update to the context for the slot.h hunk. The commit message needs some explanation. Why are we doing this? What does the patch do? What alternatives did we consider? The current error message seems reasonable. The patch has no docs. If we plan to forbid some functions in single-user mode, we should document which ones (e.g. in func.sgml). There are no tests. Do we have any tests at all for single-user mode? The only one I see is in recovery/t/017_shm.pl. What if we had tests that ran the regress suite in single-user mode? Basically instead of psql we run postgres --single? Do we expect a lot of it would fail? Is it small enough the test could maintain a diff that expects those failures? I'm not saying we should do that as part of this patch, but is there any interest in that? Since single-user mode is most often used when things are broken, it's a harsh place to hit a bug. The patch lacks source comments. Something on CheckSlotIsInSingleUserMode explaining why would be good. In ReplicationSlotRelease, why only assert that `slot->active_pid !=3D 0`? Why not assert that it's MyProcPid (even outside single-user mode)? Can one backend really release a slot while another is using it? Can you drop it? Maybe you can copy it? Are we calling CheckSlotIsInSingleUserMode from everywhere that needs it? We tried to check all the functions that call ReplicationSlotCreate, ReplicationSlotRelease, and update_synced_slots_inactive_since (since they all have these asserts). To call out a few: The pg_replication_origin_* functions don't call the Assert-ing functions. binary_upgrade_logical_slot_has_caught_up - Not available from the command = line. WalSndErrorCleanup - Probably not used in single-user mode? We also see that PostgresMain calls ReplicationSlotRelease from its error handler. Presumably single-user mode would be executing PostgresSingleUserMain instead, but still perhaps we should call CheckSlotIsInSingleUserMode here just as a sanity-check? On Thu, Feb 27, 2025 at 9:42=E2=80=AFPM Hayato Kuroda (Fujitsu) wrote: > > I understand that we may not have a clear use case for this to work in > > single-user mode. But how will we define the boundary in similar > > cases? I mean, we should have some rule for such exposed functions, > > and it should be followed uniformly. Now, if one needs a bigger or > > complex change to make the function work in single-user mode, then it > > is easy to take an exception from what is currently being followed in > > the code. However, if the change is as trivial as you proposed in the > > first email, why not go with that and make this function work in > > single-user mode? > > Hmm, the opinion about this is completely opposite with other reviewers. = I want > to ask them again how you feel. I also added Tom who pointed out in the i= nitial > thread. > > Question: how you feel to restrict SQL functions for slot during the sing= le-user > mode? Nobody has considered use cases for it; we do not have concrete the= ory and > similar cases. And needed band-aid to work seems very small. No one has replied yet, but I vote for forbidding these functions. I can't articulate a full theory for which functions we restrict in single-user mode, and I think we should permit as much as possible. But any theory would weigh usefulness against risk. Here no one has found a use-case for several years, and executing user-supplied code in an unusual, higher-risk scenario seems like a good reason to be cautious. Also without tests for single-user mode, I'm extra wary. If single-user support is desired by someone, they could submit a patch. Yours, -- Paul ~{:-) pj@illuminatedcomputing.com --0000000000001b2f9c063ba109ce Content-Type: text/x-patch; charset="US-ASCII"; name="v5-0001-Prohibit-slot-operations-while-in-the-single-user.patch" Content-Disposition: attachment; filename="v5-0001-Prohibit-slot-operations-while-in-the-single-user.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mdyrz5en0 RnJvbSBiMTk4MTUxMTk5MDlkMDUyOWQ2YzUyODg5YWIwOTU5ZWM0NGU3ZTdmIE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBIYXlhdG8gS3Vyb2RhIDxrdXJvZGEuaGF5YXRvQGZ1aml0c3Uu Y29tPgpEYXRlOiBXZWQsIDE5IEZlYiAyMDI1IDExOjM3OjI2ICswOTAwClN1YmplY3Q6IFtQQVRD SCB2NF0gUHJvaGliaXQgc2xvdCBvcGVyYXRpb25zIHdoaWxlIGluIHRoZSBzaW5nbGUgdXNlciBt b2RlCgotLS0KIHNyYy9iYWNrZW5kL3JlcGxpY2F0aW9uL2xvZ2ljYWwvbG9naWNhbGZ1bmNzLmMg fCAgMiArKwogc3JjL2JhY2tlbmQvcmVwbGljYXRpb24vc2xvdC5jICAgICAgICAgICAgICAgICB8 IDEyICsrKysrKysrKysrKwogc3JjL2JhY2tlbmQvcmVwbGljYXRpb24vc2xvdGZ1bmNzLmMgICAg ICAgICAgICB8IDEyICsrKysrKysrKysrKwogc3JjL2luY2x1ZGUvcmVwbGljYXRpb24vc2xvdC5o ICAgICAgICAgICAgICAgICB8ICAxICsKIDQgZmlsZXMgY2hhbmdlZCwg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