From 4e54662c4123d4dd45eef58eaeac402774f75ca9 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Sat, 21 Nov 2026 07:06:17 +0000
Subject: [PATCH v1 REL_18] dblink: Don't loop forever when a remote command
 starts a COPY

dblink() collects results with PQgetResult() until it returns NULL.
Once the remote side starts a COPY, libpq returns a COPY result on every
call and never NULL, so the backend spun at full CPU.  A query cancel
didn't help: the error handler drains results with the same kind of
loop, so it spun there instead.

Stop both loops at a COPY result, as libpqsrv_get_last_result() already
does.  The COPY is then reported as an error, the same way dblink_exec()
reports it.

Discussion: https://postgr.es/m/19895-7cc84a032390a6d9@postgresql.org
---
 contrib/dblink/dblink.c            | 24 ++++++++++++++++++++++++
 contrib/dblink/expected/dblink.out |  9 +++++++++
 contrib/dblink/sql/dblink.sql      |  6 ++++++
 3 files changed, 39 insertions(+)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index baa0313d4d..2402db4197 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -118,6 +118,7 @@ static void materializeQueryResult(FunctionCallInfo fcinfo,
 								   const char *conname,
 								   const char *sql,
 								   bool fail);
+static inline bool is_copy_result(PGresult *res);
 static PGresult *storeQueryResult(volatile storeInfo *sinfo, PGconn *conn, const char *sql);
 static void storeRow(volatile storeInfo *sinfo, PGresult *res, bool first);
 static remoteConn *getConnectionByName(const char *name);
@@ -1230,12 +1231,31 @@ materializeQueryResult(FunctionCallInfo fcinfo,
 		/* and clear out any pending data in libpq */
 		while ((res = libpqsrv_get_result(conn, dblink_we_get_result)) !=
 			   NULL)
+		{
+			bool		copy = is_copy_result(res);
+
 			PQclear(res);
+			if (copy)
+				break;
+		}
+
 		PG_RE_THROW();
 	}
 	PG_END_TRY();
 }
 
+/*
+ * Is this the result of a COPY?  libpq keeps returning such a result, never
+ * NULL, until the COPY is finished, so loops over PQgetResult() must stop.
+ */
+static inline bool
+is_copy_result(PGresult *res)
+{
+	return PQresultStatus(res) == PGRES_COPY_IN ||
+		PQresultStatus(res) == PGRES_COPY_OUT ||
+		PQresultStatus(res) == PGRES_COPY_BOTH;
+}
+
 /*
  * Execute query, and send any result rows to sinfo->tuplestore.
  */
@@ -1289,6 +1309,10 @@ storeQueryResult(volatile storeInfo *sinfo, PGconn *conn, const char *sql)
 			sinfo->last_res = sinfo->cur_res;
 			sinfo->cur_res = NULL;
 			first = true;
+
+			/* COPY is not supported; the caller reports it as an error */
+			if (is_copy_result(sinfo->last_res))
+				break;
 		}
 	}
 
diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out
index a00d74fff1..3f7decaac6 100644
--- a/contrib/dblink/expected/dblink.out
+++ b/contrib/dblink/expected/dblink.out
@@ -181,6 +181,15 @@ SELECT *
 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 ERROR:  connection not available
+-- COPY is not supported, and used to make dblink() loop forever
+SELECT *
+FROM dblink(connection_parameters(), 'COPY foo TO STDOUT') AS t(a int, b text, c text[]);
+ERROR:  could not obtain message string for remote error
+CONTEXT:  while executing query on unnamed dblink connection
+SELECT *
+FROM dblink(connection_parameters(), 'COPY foo FROM STDIN') AS t(a int, b text, c text[]);
+ERROR:  could not obtain message string for remote error
+CONTEXT:  while executing query on unnamed dblink connection
 -- Some options are prohibited outright
 SELECT *
 FROM dblink(connection_parameters() || ' sslkeylogfile=./noexist/log', 'SELECT * FROM foo')
diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql
index bcedf83475..13e2c5d7e8 100644
--- a/contrib/dblink/sql/dblink.sql
+++ b/contrib/dblink/sql/dblink.sql
@@ -133,6 +133,12 @@ SELECT *
 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
 WHERE t.a > 7;
 
+-- COPY is not supported, and used to make dblink() loop forever
+SELECT *
+FROM dblink(connection_parameters(), 'COPY foo FROM STDIN') AS t(a int, b text, c text[]);
+
 -- Some options are prohibited outright
 SELECT *
 FROM dblink(connection_parameters() || ' sslkeylogfile=./noexist/log', 'SELECT * FROM foo')
-- 
2.50.1 (Apple Git-155)

