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: masao(dot)fujii(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, 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-24 15:11:17
Message-ID: nh-cbc6d0ed-b87f-456a-b466-1cf25edd8b34@google.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Nov 24, 2026 at 10:29 AM Fujii Masao <masao(dot)fujii(at)gmail(dot)com> wrote:
> Thanks for the patch! LGTM. I've attached an updated version (0001)
> with minor cleanup and a revised commit message, along with backpatch
> versions for older stable branches.
>
> While reviewing the code, I found another pre-existing issue in dblink:
> with fail_on_error = false, dblink() could return rows received before
> a remote error, although the docs says that no rows are returned in
> that case. Patch 0002 fixes this by discarding those rows.
>
> Since this issue also occurs without COPY, I've attached it as
> a separate patch.
>
> Thought?

Thanks for the updated patches. I applied v2-0001 and v2-0002 on master
(4a26e36f87), and the v18 version on REL_18_STABLE (8efeedc738). They
apply, build, and the dblink regression test passes on both. I did not test
the v17-v15 version.

I also tried it by hand on master. COPY TO STDOUT and COPY FROM STDIN
through dblink() now fail right away, and the backend does not spin. With
fail_on_error = false, "select 1 / (x - 3) from generate_series(1, 5) x"
and "select 42; select 1 / 0" now return no rows. Normal queries are
unchanged.

I checked that the tests catch the bugs. With only the 0002 code change
reverted, the regression test fails and 42 and 1 come back as rows. With
only the 0001 code change reverted, the backend spins at about 99% CPU on
COPY TO STDOUT, and pg_cancel_backend() and pg_terminate_backend() return
true but do nothing.

The new test pins the error text "could not obtain message string for
remote error". It is not very helpful, but it is the same text
dblink_exec() gives, as I said in my first mail. Also, after a COPY error a
named connection stays in COPY state until it is disconnected, and the next
query on it fails with "another command is already in progress". Before the
patch that case hung, so it is not worse.

I have no objections to committing the patches.

Regards,
Sai Pranav

Browse pgsql-bugs by date

  From Date Subject
Next Message Henson Choi 2026-11-24 15:19:03 Re: BUG #19354: JOHAB rejects valid byte sequences
Previous Message Henson Choi 2026-11-24 14:46:44 Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill