From 7c3c72758982be209b55149ba1aa9939355099ee 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 869bceda5a4..8fd3b34891e 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -113,6 +113,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); @@ -1222,12 +1223,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. */ @@ -1281,6 +1302,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 5f0d94de03a..5c22b4ec663 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 -- The first-level connection's backend will crash on exit given OpenLDAP -- [2.4.24, 2.4.31]. We won't see evidence of any crash until the victim -- process terminates and the postmaster responds. If process termination diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql index 3574ea267ae..e0ac563e904 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[]); + -- The first-level connection's backend will crash on exit given OpenLDAP -- [2.4.24, 2.4.31]. We won't see evidence of any crash until the victim -- process terminates and the postmaster responds. If process termination -- 2.56.1