Re: BUG #19895: dblink() never returns and spins at 100% CPU when the remote command is COPY ... TO STDOUT;

From: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 1165125080(at)qq(dot)com
Subject: Re: BUG #19895: dblink() never returns and spins at 100% CPU when the remote command is COPY ... TO STDOUT;
Date: 2026-11-21 07:11:49
Message-ID: nh-1348d84f-b302-4883-a21c-70b0e090d3aa@google.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

I reproduced this on master (4a26e36f87). The backend stays active with no
wait event, and pg_cancel_backend() and pg_terminate_backend() both return
true but do nothing.

dblink() collects results in storeQueryResult() by calling 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 loop never ends. A cancel
does reach CHECK_FOR_INTERRUPTS() in that loop, but the PG_CATCH block in
materializeQueryResult() then drains results with the same kind of loop, so
the backend spins there instead.

The attached patch stops both loops at a COPY result, the same way
libpqsrv_get_last_result() already does. The COPY is then reported as an
error, as dblink_exec() already reports it today. The error text ("could
not obtain message string for remote error") isn't very helpful, but it's
the same as dblink_exec() gives, so I left it alone.

The patch adds COPY TO STDOUT and COPY FROM STDIN cases to
contrib/dblink/sql/dblink.sql. The dblink tests pass with the patch.
Without it, the test hangs until meson kills it.

v1 is for master and applies to REL_19_STABLE. The REL_18 version has the
same code change with a small conflict resolved, and I tested it the same
way on REL_18_STABLE. REL_17_STABLE needs the test hunk adjusted; I can
send that version too.

Regards,
Sai Pranav

Attachment Content-Type Size
v1-0001-dblink-Don-t-loop-forever-when-a-remote-command-s.patch text/x-patch 4.3 KB
REL_18-v1-0001-dblink-Don-t-loop-forever-when-a-remote-command-s.patch text/x-patch 4.2 KB

Browse pgsql-bugs by date

  From Date Subject
Previous Message Raja Sai pranav 2026-11-21 07:05:57 Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off