Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mQGQi-0004P6-QB for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Sep 2021 21:57:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mQGQg-0002BR-EI for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Sep 2021 21:57:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mQGQg-0002BJ-6M for pgsql-hackers@lists.postgresql.org; Tue, 14 Sep 2021 21:57:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mQGQe-0008DF-2c for pgsql-hackers@postgresql.org; Tue, 14 Sep 2021 21:57:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 18ELuwdn905669; Tue, 14 Sep 2021 17:56:59 -0400 From: Tom Lane To: Artur Zakirov cc: Sergey Fedchenko , Daniel Danzberger , Andres Freund , Sergei Kornilov , pgsql-hackers@postgresql.org, Peter Eisentraut , Marc Dean , Petr Jelinek , "michael.paul.powers@gmail.com" , Alvaro Herrera , Robert Welin Subject: Re: BUG #15293: Stored Procedure Triggered by Logical Replication is Unable to use Notification Events In-reply-to: <284756.1631467798@sss.pgh.pa.us> References: <20190628212224.GA11418@alvherre.pgsql> <923262c473b494c4aec825a360d6f0499c144552.camel@dd-wrt.com> <1623669444.237579337@f317.i.mail.ru> <3113945.1630956452@sss.pgh.pa.us> <284756.1631467798@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 12 Sep 2021 13:29:58 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <905636.1631656587.0@sss.pgh.pa.us> Date: Tue, 14 Sep 2021 17:56:58 -0400 Message-ID: <905668.1631656618@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <905636.1631656587.1@sss.pgh.pa.us> I wrote: > Hence, I present the attached, which also tweaks things to avoid an > extra pq_flush in the end-of-command code path, and improves the > comments to discuss the issue of NOTIFYs sent by procedures. Hearing no comments, I pushed that. > I'm inclined to think we should flat-out reject LISTEN in any process > that is not attached to a frontend, at least until somebody takes the > trouble to add infrastructure that would let it be useful. I've not > done that here though; I'm not quite sure what we should test for. After a bit of looking around, it seems that MyBackendType addresses that problem nicely, so I propose the attached. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="forbid-LISTEN-in-background.patch"; charset="us-ascii" Content-ID: <905636.1631656587.2@sss.pgh.pa.us> Content-Description: forbid-LISTEN-in-background.patch diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 27fbf1f3aa..bf085aa93b 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -803,6 +803,23 @@ standard_ProcessUtility(PlannedStmt *pstmt, ListenStmt *stmt = (ListenStmt *) parsetree; CheckRestrictedOperation("LISTEN"); + + /* + * We don't allow LISTEN in background processes, as there is + * no mechanism for them to collect NOTIFY messages, so they'd + * just block cleanout of the async SLRU indefinitely. + * (Authors of custom background workers could bypass this + * restriction by calling Async_Listen directly, but then it's + * on them to provide some mechanism to process the message + * queue.) Note there seems no reason to forbid UNLISTEN. + */ + if (MyBackendType != B_BACKEND) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + /* translator: %s is name of a SQL command, eg LISTEN */ + errmsg("cannot execute %s within a background process", + "LISTEN"))); + Async_Listen(stmt->conditionname); } break; ------- =_aaaaaaaaaa0--