From 9175425c8cfad588d03ffd66804f4f99efd61b61 Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Sat, 21 Nov 2026 07:06:17 +0000 Subject: [PATCH v2] dblink: Fix infinite loops when a remote command starts COPY Executing COPY TO STDOUT or COPY FROM STDIN through dblink() caused the calling backend to loop indefinitely at full CPU usage. Canceling the query did not stop the loop. The cause was that dblink's result collection and error cleanup loops did not handle COPY results. They kept calling PQgetResult() until it returned NULL, but in COPY mode it kept returning COPY results instead. Cancellation merely moved execution from one infinite loop to the other. Fix this by stopping both loops when a COPY result is received. Specifically, stop on PGRES_COPY_IN, PGRES_COPY_OUT, or PGRES_COPY_BOTH, as libpqsrv_get_result_last() already does. This allows dblink() to report the unsupported COPY transfer instead of hanging, and allows error cleanup to finish. Backpatch to all supported versions, as the affected loops have been present since PostgreSQL 9.2. ji xiaohang also proposed a fix, but this commit uses Raja Sai Pranav's patch instead. Reported-by: ji xiaohang <1165125080@qq.com> Author: Raja Sai Pranav Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/19895-7cc84a032390a6d9@postgresql.org Discussion: https://postgr.es/m/nh-1348d84f-b302-4883-a21c-70b0e090d3aa@google.com Backpatch-through: 15 --- contrib/dblink/dblink.c | 25 +++++++++++++++++++++++++ contrib/dblink/expected/dblink.out | 11 +++++++++++ contrib/dblink/sql/dblink.sql | 8 ++++++++ 3 files changed, 44 insertions(+) diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index baa0313d4da..dcf067d225e 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 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,32 @@ 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(); } +/* + * Return whether the result indicates a COPY transfer. + */ +static bool +is_copy_result(PGresult *res) +{ + ExecStatusType status = PQresultStatus(res); + + return status == PGRES_COPY_IN || + status == PGRES_COPY_OUT || + status == PGRES_COPY_BOTH; +} + /* * Execute query, and send any result rows to sinfo->tuplestore. */ @@ -1289,6 +1310,10 @@ storeQueryResult(volatile storeInfo *sinfo, PGconn *conn, const char *sql) sinfo->last_res = sinfo->cur_res; sinfo->cur_res = NULL; first = true; + + /* The caller reports that COPY transfers are not supported. */ + if (is_copy_result(sinfo->last_res)) + break; } } diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out index a00d74fff1d..f4a980ed3e3 100644 --- a/contrib/dblink/expected/dblink.out +++ b/contrib/dblink/expected/dblink.out @@ -181,6 +181,17 @@ SELECT * FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) WHERE t.a > 7; ERROR: connection not available +-- COPY to/from the client is not supported. +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 bcedf834752..6a0f3dafe64 100644 --- a/contrib/dblink/sql/dblink.sql +++ b/contrib/dblink/sql/dblink.sql @@ -133,6 +133,14 @@ SELECT * FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) WHERE t.a > 7; +-- COPY to/from the client is not supported. +SELECT * +FROM dblink(connection_parameters(), 'COPY foo TO STDOUT') + AS t(a int, b text, c text[]); +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.56.1