Subject: pgpool: Fix COPY IN hang in extended query mode.

From: Tatsuo Ishii <[email-masked]>
Date: Sun, 07 Jun 2026 10:10:33 +0000

Fix COPY IN hang in extended query mode.

If COPY FROM STDIN is executed in extended query mode in streaming
replication mode, pgpool hung in CopyDataRow(). Consider following
scenario:

FE=> Parse(stmt="", query="COPY t1 FROM STDIN")
FE=> Bind(stmt="", portal="")
FE=> Execute(portal="")
FE=> CopyData (copy data="1
")
FE=> CopyData (copy data="2
")
FE=> CopyDone
FE=> Sync
<= BE ParseComplete
<= BE BindComplete
<= BE CopyInResponse

The backend responses with ParseComplete, BindComplete and
CopyInResponse, exepecting CopyData coming from pgpool. However, as
frontend sent CopyData before CopyInResponse coming from the backend,
the backend ignores tha CopData. Moreover, CopyDataRow has no chance
to forward Sync message to the backend, and the backend does not
respond with CommandComplete but CopyDataRow waited for it in vain,
causes a hang.

To fix the problems, add suspend_reading_from_frontend_copy_in flag to
the session context. The flag is set within Execute() to prevent
reading messages from frontend until COPY IN response comes from the
backend. After it arrives, CopyDataRow is called and process CopyData
until frontend sends CopyDone messages. CopyDataRow forwards it to the
backend, then reset query in progress flag and return. This will cause
pool_process_query's main loop to read Sync message from frontend, and
pgpool forwards it to the backend. After that CommandComplete and
ReadyForQuery arrives from the backend as expected.

Test case added to 126.copy_hang.

Reported-by: liujinyang-highgo
Reviewed-by: liujinyang-highgo
Author: Tatsuo Ishii <[email-masked]>
Discussion: https://github.com/pgpool/pgpool2/issues/162
Backpatch-through: v4.3

Branch
------
V4_4_STABLE

Details
-------
https://git.postgresql.org/gitweb?p=pgpool2.git;a=commitdiff;h=199a8ca6b2097fe6f2d6af8e6dbf792ca63d7450

Modified Files
--------------
src/context/pool_session_context.c                 | 30 +++++++++++
src/include/context/pool_session_context.h         | 11 ++++
src/protocol/pool_process_query.c                  | 14 ++---
src/protocol/pool_proto_modules.c                  | 59 +++++++++++++++++++++-
.../tests/076.copy_hang/copy-in-expected           | 31 ++++++++++++
.../tests/076.copy_hang/pgproto-copy-in.data       | 15 ++++++
src/test/regression/tests/076.copy_hang/test.sh    |  9 ++++
7 files changed, 162 insertions(+), 7 deletions(-)