From 2acb68e56dabcbb776bd220224e19c42edb64181 Mon Sep 17 00:00:00 2001
From: Antonin Houska <ah@cybertec.at>
Date: Tue, 1 Dec 2026 14:13:29 +0100
Subject: [PATCH 2/2] Disable progress reporting in file_fdw.

Do not report progress of the underlying COPY command, as the progress
reporting API is not designed to handle multiple commands of the same type at
a time.

We might teach COPY to count the number of active executions and report only
the progress of the first one, however that's probably not worth the trouble.
One particular problem is that we'd have to track the number of COPY commands
per subtransaction, to be able to adjust the counter correctly on rollback.

Also, from user perspective, it would be weird to see that the progress
reporting is only active for file_fdw nodes in special cases. Furthermore, why
should only fdw_nodes report their progress while other executor nodes do not?
If the progress reporting happens to be important for queries, then proper
integration with the executor is needed.
---
 contrib/file_fdw/file_fdw.c                 | 16 ++++++--
 src/backend/commands/copy.c                 |  2 +-
 src/backend/commands/copyfrom.c             | 41 +++++++++++++--------
 src/backend/commands/copyfromparse.c        |  4 +-
 src/backend/replication/logical/tablesync.c |  2 +-
 src/include/commands/copy.h                 |  3 +-
 src/include/commands/copyfrom_internal.h    |  2 +
 7 files changed, 48 insertions(+), 22 deletions(-)

diff --git a/contrib/file_fdw/file_fdw.c b/contrib/file_fdw/file_fdw.c
index 29003f41e99..a529af8391c 100644
--- a/contrib/file_fdw/file_fdw.c
+++ b/contrib/file_fdw/file_fdw.c
@@ -698,6 +698,14 @@ fileBeginForeignScan(ForeignScanState *node, int eflags)
 	/*
 	 * Create CopyState from FDW options.  We always acquire all columns, so
 	 * as to match the expected ScanTupleSlot signature.
+	 *
+	 * Do not report progress, as the progress reporting API is not designed
+	 * to handle multiple commands of the same type at a time. We might teach
+	 * COPY to count the number of active executions and report only the
+	 * progress of the first one, however that's probably not worth the
+	 * trouble. (One particular problem is that we'd have to track the number
+	 * of COPY commands per subtransaction, to be able to adjust the counter
+	 * correctly on rollback.)
 	 */
 	cstate = BeginCopyFrom(NULL,
 						   node->ss.ss_currentRelation,
@@ -706,7 +714,8 @@ fileBeginForeignScan(ForeignScanState *node, int eflags)
 						   is_program,
 						   NULL,
 						   NIL,
-						   options);
+						   options,
+						   false);
 
 	/*
 	 * Save state in node->fdw_state.  We must save enough information to call
@@ -835,7 +844,8 @@ fileReScanForeignScan(ForeignScanState *node)
 									festate->is_program,
 									NULL,
 									NIL,
-									festate->options);
+									festate->options,
+									false);
 }
 
 /*
@@ -1217,7 +1227,7 @@ file_acquire_sample_rows(Relation onerel, int elevel,
 	 * Create CopyState from FDW options.
 	 */
 	cstate = BeginCopyFrom(NULL, onerel, NULL, filename, is_program, NULL, NIL,
-						   options);
+						   options, false);
 
 	/*
 	 * Create an expression context to evaluate DEFAULT expressions.  Use its
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 95b58cd2c84..6c2da756835 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -368,7 +368,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
 
 		cstate = BeginCopyFrom(pstate, rel, whereClause,
 							   stmt->filename, stmt->is_program,
-							   NULL, stmt->attlist, stmt->options);
+							   NULL, stmt->attlist, stmt->options, true);
 		*processed = CopyFrom(cstate);	/* copy from file to database */
 		EndCopyFrom(cstate);
 	}
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 665834561df..73a5cab6c21 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -525,8 +525,9 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
 
 			/* Update the row counter and progress of the COPY command */
 			*processed += inserted;
-			pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
-										 *processed);
+			if (cstate->progress)
+				pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
+											 *processed);
 		}
 
 		for (i = 0; i < nused; i++)
@@ -604,8 +605,9 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
 
 		/* Update the row counter and progress of the COPY command */
 		*processed += nused;
-		pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
-									 *processed);
+		if (cstate->progress)
+			pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
+										 *processed);
 
 		/* reset cur_lineno and line_buf_valid to what they were */
 		cstate->line_buf_valid = line_buf_valid;
@@ -1167,8 +1169,9 @@ CopyFrom(CopyFromState cstate)
 			cstate->escontext->error_occurred = false;
 
 			/* Report that this tuple was skipped by the ON_ERROR clause */
-			pgstat_progress_update_param(PROGRESS_COPY_TUPLES_SKIPPED,
-										 cstate->num_errors);
+			if (cstate->progress)
+				pgstat_progress_update_param(PROGRESS_COPY_TUPLES_SKIPPED,
+											 cstate->num_errors);
 
 			if (cstate->opts.reject_limit > 0 &&
 				cstate->num_errors > cstate->opts.reject_limit)
@@ -1202,8 +1205,9 @@ CopyFrom(CopyFromState cstate)
 				 * Report that this tuple was filtered out by the WHERE
 				 * clause.
 				 */
-				pgstat_progress_update_param(PROGRESS_COPY_TUPLES_EXCLUDED,
-											 ++excluded);
+				if (cstate->progress)
+					pgstat_progress_update_param(PROGRESS_COPY_TUPLES_EXCLUDED,
+												 ++excluded);
 				continue;
 			}
 		}
@@ -1454,8 +1458,9 @@ CopyFrom(CopyFromState cstate)
 			 * for counting tuples inserted by an INSERT command.  Update
 			 * progress of the COPY command as well.
 			 */
-			pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
-										 ++processed);
+			if (cstate->progress)
+				pgstat_progress_update_param(PROGRESS_COPY_TUPLES_PROCESSED,
+											 ++processed);
 		}
 	}
 
@@ -1532,6 +1537,7 @@ CopyFrom(CopyFromState cstate)
  * 'data_source_cb': callback that provides the input data
  * 'attnamelist': List of char *, columns to include. NIL selects all cols.
  * 'options': List of DefElem. See copy_opt_item in gram.y for selections.
+ * 'progress': Should progress be reported?
  *
  * Returns a CopyFromState, to be passed to NextCopyFrom and related functions.
  */
@@ -1543,7 +1549,8 @@ BeginCopyFrom(ParseState *pstate,
 			  bool is_program,
 			  copy_data_source_cb data_source_cb,
 			  List *attnamelist,
-			  List *options)
+			  List *options,
+			  bool progress)
 {
 	CopyFromState cstate;
 	bool		pipe = (filename == NULL);
@@ -1849,8 +1856,10 @@ BeginCopyFrom(ParseState *pstate,
 	cstate->defaults = palloc0_array(bool, tupDesc->natts);
 
 	/* initialize progress */
-	pgstat_progress_start_command(PROGRESS_COMMAND_COPY,
-								  cstate->rel ? RelationGetRelid(cstate->rel) : InvalidOid);
+	cstate->progress = progress;
+	if (cstate->progress)
+		pgstat_progress_start_command(PROGRESS_COMMAND_COPY,
+									  cstate->rel ? RelationGetRelid(cstate->rel) : InvalidOid);
 	cstate->bytes_processed = 0;
 
 	/* We keep those variables in cstate. */
@@ -1926,7 +1935,8 @@ BeginCopyFrom(ParseState *pstate,
 		}
 	}
 
-	pgstat_progress_update_multi_param(3, progress_cols, progress_vals);
+	if (cstate->progress)
+		pgstat_progress_update_multi_param(3, progress_cols, progress_vals);
 
 	cstate->routine->CopyFromStart(cstate, tupDesc);
 
@@ -1958,7 +1968,8 @@ EndCopyFrom(CopyFromState cstate)
 							cstate->filename)));
 	}
 
-	pgstat_progress_end_command(false);
+	if (cstate->progress)
+		pgstat_progress_end_command(false);
 
 	MemoryContextDelete(cstate->copycontext);
 	pfree(cstate);
diff --git a/src/backend/commands/copyfromparse.c b/src/backend/commands/copyfromparse.c
index e35d10a3ee0..18e819b7e41 100644
--- a/src/backend/commands/copyfromparse.c
+++ b/src/backend/commands/copyfromparse.c
@@ -637,7 +637,9 @@ CopyLoadRawBuf(CopyFromState cstate)
 	cstate->raw_buf_len = nbytes;
 
 	cstate->bytes_processed += inbytes;
-	pgstat_progress_update_param(PROGRESS_COPY_BYTES_PROCESSED, cstate->bytes_processed);
+	if (cstate->progress)
+		pgstat_progress_update_param(PROGRESS_COPY_BYTES_PROCESSED,
+									 cstate->bytes_processed);
 
 	if (inbytes == 0)
 		cstate->raw_reached_eof = true;
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 7ddfb3e4677..c02c03c2b23 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1212,7 +1212,7 @@ copy_table(Relation rel)
 
 	attnamelist = make_copy_attnamelist(relmapentry);
 	cstate = BeginCopyFrom(pstate, rel, NULL, NULL, false, copy_read_data,
-						   attnamelist, options);
+						   attnamelist, options, true);
 
 	/* Do the copy */
 	(void) CopyFrom(cstate);
diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h
index abecfe51098..57ef567785b 100644
--- a/src/include/commands/copy.h
+++ b/src/include/commands/copy.h
@@ -113,7 +113,8 @@ extern void DoCopy(ParseState *pstate, const CopyStmt *stmt,
 extern void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, bool is_from, List *options);
 extern CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause,
 								   const char *filename,
-								   bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options);
+								   bool is_program, copy_data_source_cb data_source_cb,
+								   List *attnamelist, List *options, bool progress);
 extern void EndCopyFrom(CopyFromState cstate);
 extern bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext,
 						 Datum *values, bool *nulls);
diff --git a/src/include/commands/copyfrom_internal.h b/src/include/commands/copyfrom_internal.h
index 9d3e244ee55..d946e29084e 100644
--- a/src/include/commands/copyfrom_internal.h
+++ b/src/include/commands/copyfrom_internal.h
@@ -189,6 +189,8 @@ typedef struct CopyFromStateData
 #define RAW_BUF_BYTES(cstate) ((cstate)->raw_buf_len - (cstate)->raw_buf_index)
 
 	uint64		bytes_processed;	/* number of bytes processed so far */
+
+	bool	progress;			/* report progress? */
 } CopyFromStateData;
 
 extern void ReceiveCopyBegin(CopyFromState cstate);
-- 
2.51.0

