public inbox for [email protected]  
help / color / mirror / Atom feed
From: Chao Li <[email protected]>
To: Postgres hackers <[email protected]>
Cc: Fujii Masao <[email protected]>
Cc: vignesh C <[email protected]>
Subject: Prevent remote libpq notices from being sent to clients
Date: Fri, 5 Jun 2026 17:31:31 +0800
Message-ID: <[email protected]> (raw)

Hi,

This is another issue with “[112faf137] Log remote NOTICE, WARNING, and similar messages using ereport()”. From the commit message, the intention of the feature is to log remote messages with ereport() to get better formatting:
```
    Log remote NOTICE, WARNING, and similar messages using ereport().

    Previously, NOTICE, WARNING, and similar messages received from remote
    servers over replication, postgres_fdw, or dblink connections were printed
    directly to stderr on the local server (e.g., the subscriber). As a result,
    these messages lacked log prefixes (e.g., timestamp), making them harder
    to trace and correlate with other log entries.

    This commit addresses the issue by introducing a custom notice receiver
    for replication, postgres_fdw, and dblink connections. These messages
    are now logged via ereport(), ensuring they appear in the logs with proper
    formatting and context, which improves clarity and aids in debugging.
```

So remote messages should only be output to the server log, but currently they can leak to the client if client_min_messages is set to log.

This is a simple repro:
```
evantest=# set client_min_messages=log;
SET
evantest=# select * from dblink('host=localhost dbname=postgres’,
'do $$ begin raise warning ''hello, client!!!''; end $$; select 1’)
as t(x int);
LOG:  received message via remote connection: WARNING:  hello, client!!!
 x
---
 1
(1 row)
```

The one-line fix is straightforward, just change the ereport() level from LOG to LOG_SERVER_ONLY. I also added a test in the patch.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






Attachments:

  [application/octet-stream] v1-0001-Avoid-sending-remote-libpq-notices-back-to-client.patch (3.0K, 2-v1-0001-Avoid-sending-remote-libpq-notices-back-to-client.patch)
  download | inline diff:
From be744e8ca867626f721ad146214cde9db395834e Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Fri, 5 Jun 2026 17:18:11 +0800
Subject: [PATCH v1] Avoid sending remote libpq notices back to clients

Commit 112faf137 added a custom libpq notice receiver so that remote
NOTICE, WARNING, and similar messages from dblink, postgres_fdw, and
replication connections are logged via ereport(). The receiver used LOG,
which preserves server logging, but LOG can also be sent to the local SQL
client when client_min_messages is set to log.

That exposes remote notices to the client as local LOG messages, unlike the
previous libpq default notice processor behavior, which printed them only to
backend stderr.

Use LOG_SERVER_ONLY instead. This keeps the messages in the server log while
preventing them from being sent to the SQL client. Add a dblink regression
test for the client-visible case.

Author: Chao Li <[email protected]>
---
 contrib/dblink/expected/dblink.out      | 12 ++++++++++++
 contrib/dblink/sql/dblink.sql           |  8 ++++++++
 src/include/libpq/libpq-be-fe-helpers.h |  2 +-
 3 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out
index 1d2759def9e..835367b3668 100644
--- a/contrib/dblink/expected/dblink.out
+++ b/contrib/dblink/expected/dblink.out
@@ -176,6 +176,18 @@ WHERE t.a > 7;
  9 | j | {a9,b9,c9}
 (2 rows)
 
+-- remote notices should be logged server-side only
+SET client_min_messages = log;
+SELECT *
+FROM dblink(connection_parameters(),
+            $$DO $do$ BEGIN RAISE NOTICE 'remote notice'; END $do$; SELECT 1$$)
+  AS t(x int);
+ x 
+---
+ 1
+(1 row)
+
+RESET client_min_messages;
 -- should generate "connection not available" error
 SELECT *
 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql
index d67a0a5992e..4c043a3ef87 100644
--- a/contrib/dblink/sql/dblink.sql
+++ b/contrib/dblink/sql/dblink.sql
@@ -128,6 +128,14 @@ SELECT *
 FROM dblink(connection_parameters(),'SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 
+-- remote notices should be logged server-side only
+SET client_min_messages = log;
+SELECT *
+FROM dblink(connection_parameters(),
+            $$DO $do$ BEGIN RAISE NOTICE 'remote notice'; END $do$; SELECT 1$$)
+  AS t(x int);
+RESET client_min_messages;
+
 -- should generate "connection not available" error
 SELECT *
 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
diff --git a/src/include/libpq/libpq-be-fe-helpers.h b/src/include/libpq/libpq-be-fe-helpers.h
index cff68cd1c37..05b15118284 100644
--- a/src/include/libpq/libpq-be-fe-helpers.h
+++ b/src/include/libpq/libpq-be-fe-helpers.h
@@ -498,7 +498,7 @@ libpqsrv_notice_receiver(void *arg, const PGresult *res)
 	if (len > 0 && message[len - 1] == '\n')
 		len--;
 
-	ereport(LOG,
+	ereport(LOG_SERVER_ONLY,
 			errmsg_internal("%s: %.*s", prefix, len, message));
 }
 
-- 
2.50.1 (Apple Git-155)



view thread (6+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Prevent remote libpq notices from being sent to clients
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox