Hi Chao,

Thanks for the review. Both points are right, and v2 attached does both.

1. max_attnum is now an AttrNumber placed after the two bool fields,
so it fits in the existing padding and CopyToStateData does not grow.
2. The slot_getsomeattrs() call in the else branch is gone.
CopyOneRowTo() already deforms the needed prefix, so that branch now
only sets copyslot = slot.

With assertions enabled, the regress suite (243 tests) and the
subscription TAP tests, including 031_column_list, pass. The code
change is otherwise the same as v1.

Regards,
Tomás
From 649e51e97a31a8e4c352ce7da20cd7705cf3d25e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Senart?= <[email protected]>
Date: Sun, 15 Nov 2026 20:00:00 +0000
Subject: [PATCH v2] Avoid deforming unused trailing columns in COPY TO
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

COPY TO from a table deformed every attribute of each row, even when
its column list ends well before the last column.  Deform only up to
the highest attribute that is sent.

Text, CSV and binary output read only the listed attributes.  JSON
output still deforms the whole row, and so does the partition path
that converts tuples to the root table's row type.

This helps exports of leading columns from wide tables, including the
COPY that the publisher runs for the initial table synchronization of
a publication with a column list.

---
 src/backend/commands/copyto.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c
index 9ca06ed..bc90119 100644
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -90,6 +90,7 @@ typedef struct CopyToStateData
 	char	   *filename;		/* filename, or NULL for STDOUT */
 	bool		is_program;		/* is 'filename' a program to popen? */
 	bool		json_row_delim_needed;	/* need delimiter before next row */
+	AttrNumber	max_attnum;		/* highest attribute to fetch from the slot */
 	StringInfo	json_buf;		/* reusable buffer for JSON output,
 								 * initialized in BeginCopyTo */
 	TupleDesc	tupDesc;		/* Descriptor for JSON output; for a column
@@ -1326,8 +1327,13 @@ DoCopyTo(CopyToState cstate)
 
 		cstate->routine->CopyToOutFunc(cstate, attr->atttypid,
 									   &cstate->out_functions[attnum - 1]);
+		cstate->max_attnum = Max(cstate->max_attnum, attnum);
 	}
 
+	/* JSON output may need the whole row to construct a composite datum. */
+	if (cstate->opts.format == COPY_FORMAT_JSON)
+		cstate->max_attnum = num_phys_attrs;
+
 	/*
 	 * Create a temporary memory context that we can reset once per row to
 	 * recover palloc'd memory.  This avoids any problems with leaks inside
@@ -1419,11 +1425,7 @@ CopyRelationTo(CopyToState cstate, Relation rel, Relation root_rel, uint64 *proc
 		if (map != NULL)
 			copyslot = execute_attr_map_slot(map, slot, root_slot);
 		else
-		{
-			/* Deconstruct the tuple */
-			slot_getallattrs(slot);
 			copyslot = slot;
-		}
 
 		/* Format and send the data */
 		CopyOneRowTo(cstate, copyslot);
@@ -1457,8 +1459,8 @@ CopyOneRowTo(CopyToState cstate, TupleTableSlot *slot)
 	MemoryContextReset(cstate->rowcontext);
 	oldcontext = MemoryContextSwitchTo(cstate->rowcontext);
 
-	/* Make sure the tuple is fully deconstructed */
-	slot_getallattrs(slot);
+	/* Fetch only the prefix containing the attributes needed for output. */
+	slot_getsomeattrs(slot, cstate->max_attnum);
 
 	cstate->routine->CopyToOneRow(cstate, slot);
 
-- 
2.55.0

Reply via email to