[PATCH v1] Avoid deforming unused trailing columns in COPY TO

Started by Tomás Senart9 days ago4 messageshackers
Jump to latest
#1Tomás Senart
ts@perfloop.ai

Hi,

COPY TO from a table deforms every attribute of each row, even when its
column list ends well before the last column. The attached patch deforms
only up to the highest attribute that is sent. Text, CSV and binary
output read only the listed attributes, so their output does not change.
JSON output still deforms the whole row, and so does the partition path
that converts tuples to the root table's row type.

slot_getallattrs() walks each attribute to find its offset and stores a
Datum and a null flag for it. With the patch, the walk stops at the
highest exported attribute.

This helps exports of leading columns from wide tables. One in-core
caller is the initial table synchronization of a publication with a
column list: the publisher runs COPY t (published columns) TO STDOUT.
pg_dump lists every column, so it gains only when a table's last
columns are generated or dropped.

Measurements on a 300,000-row table with 60 columns (234 MB) of int,
bigint, numeric, text, varchar, timestamptz and bool, some half NULL.
COPY ... TO '/dev/null', warm shared_buffers, -O2 build without
assertions, x86-64 VM with 8 vCPUs. Median ms per COPY over alternating
unpatched/patched pairs (3 pairs for narrow exports, 11 for the rest):

columns exported unpatched patched
first 5 of 60, CSV 290 191 -34%
first 5 of 60, text 252 174 -31%
column 30 of 60, CSV 162 127 -22%
id and column 60, CSV 170 167 no change
all 60, CSV 1257 1266 no change
pgbench_accounts, all 287 286 no change

Run-to-run spread was about 10%, so differences under 5% are noise. For
a full row the patched code does the same deforming as before. With the
patch, COPY t (first 5 columns) costs about the same as
COPY (SELECT first 5 columns FROM t), which took 189 and 195 ms.

There is no new regression test, because the output does not change.
With assertions enabled, the regress suite and the subscription TAP
tests (including 031_column_list) pass, and pgindent is clean. The
output of 15 COPY TO commands matched an unpatched build byte for byte:
column lists in and out of order, a dropped column, columns added with a
default, a zero-column table, a partitioned table with a reordered
partition, and the text, CSV, binary and JSON formats.

The patch is against master. It is a performance change, so it is not
meant for back-patching.

Tool-use disclosure: Perfloop found the issue and made the patch.
Human owner: me, Tomás Senart.

Attachments:

t259244_1
v1-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patchDownload+10-5
#2Chao Li
li.evan.chao@gmail.com
In reply to: Tomás Senart (#1)
Re: [PATCH v1] Avoid deforming unused trailing columns in COPY TO

On Nov 16, 2026, at 21:00, Tomás Senart <ts@perfloop.ai> wrote:

Hi,

COPY TO from a table deforms every attribute of each row, even when its
column list ends well before the last column. The attached patch deforms
only up to the highest attribute that is sent. Text, CSV and binary
output read only the listed attributes, so their output does not change.
JSON output still deforms the whole row, and so does the partition path
that converts tuples to the root table's row type.

slot_getallattrs() walks each attribute to find its offset and stores a
Datum and a null flag for it. With the patch, the walk stops at the
highest exported attribute.

This helps exports of leading columns from wide tables. One in-core
caller is the initial table synchronization of a publication with a
column list: the publisher runs COPY t (published columns) TO STDOUT.
pg_dump lists every column, so it gains only when a table's last
columns are generated or dropped.

Measurements on a 300,000-row table with 60 columns (234 MB) of int,
bigint, numeric, text, varchar, timestamptz and bool, some half NULL.
COPY ... TO '/dev/null', warm shared_buffers, -O2 build without
assertions, x86-64 VM with 8 vCPUs. Median ms per COPY over alternating
unpatched/patched pairs (3 pairs for narrow exports, 11 for the rest):

columns exported unpatched patched
first 5 of 60, CSV 290 191 -34%
first 5 of 60, text 252 174 -31%
column 30 of 60, CSV 162 127 -22%
id and column 60, CSV 170 167 no change
all 60, CSV 1257 1266 no change
pgbench_accounts, all 287 286 no change

Run-to-run spread was about 10%, so differences under 5% are noise. For
a full row the patched code does the same deforming as before. With the
patch, COPY t (first 5 columns) costs about the same as
COPY (SELECT first 5 columns FROM t), which took 189 and 195 ms.

There is no new regression test, because the output does not change.
With assertions enabled, the regress suite and the subscription TAP
tests (including 031_column_list) pass, and pgindent is clean. The
output of 15 COPY TO commands matched an unpatched build byte for byte:
column lists in and out of order, a dropped column, columns added with a
default, a zero-column table, a partitioned table with a reordered
partition, and the text, CSV, binary and JSON formats.

The patch is against master. It is a performance change, so it is not
meant for back-patching.

Tool-use disclosure: Perfloop found the issue and made the patch.
Human owner: me, Tomás Senart.
<v1-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patch>

Thanks for the patch. I haven't done a load test to measure the performance improvement. After reviewing the code changes, I have a few comments to raise first.

1
```
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -87,6 +87,7 @@ typedef struct CopyToStateData
 	Relation	rel;			/* relation to copy to */
 	QueryDesc  *queryDesc;		/* executable query to copy from */
 	List	   *attnumlist;		/* integer list of attnums to copy */
+	int			max_attnum;		/* highest attribute to fetch from the slot */
 	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 */
```

If we define max_attnum as type AttrNumber, which is int16, and place it after the two bool fields, it will fit into the existing padding space, so we won't increase the size of this structure.

2
```
 		else
 		{
-			/* Deconstruct the tuple */
-			slot_getallattrs(slot);
+			/* Deconstruct the prefix needed for output. */
+			slot_getsomeattrs(slot, cstate->max_attnum);
 			copyslot = slot;
 		}
```

I think this slot_getsomeattrs() call is not needed, because after the else clause, the code immediately calls CopyOneRowTo(), which calls slot_getsomeattrs() again. So, I think we can just remove this slot_getsomeattrs() call and only keep copyslot = slot.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Tomás Senart
ts@perfloop.ai
In reply to: Chao Li (#2)
Re: [PATCH v1] Avoid deforming unused trailing columns in COPY TO

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

Attachments:

t259244_3
v2-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patchDownload+8-7
#4Chao Li
li.evan.chao@gmail.com
In reply to: Tomás Senart (#3)
Re: [PATCH v1] Avoid deforming unused trailing columns in COPY TO

On Nov 19, 2026, at 04:52, Tomás Senart <ts@perfloop.ai> wrote:

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
<v2-0001-Avoid-deforming-unused-trailing-columns-in-COPY-T.patch>

V2 looks good to me.

I just ran a simple performance test on my MacBook M4 with a 50-column table containing 1 million rows, copying the first 3 columns, middle 3 columns, and last 3 columns. The test shows some improvements:
```
Master (091b2926044) v2
First 3 columns 155ms 133ms
Middle 3 columns 156ms 140ms
Last 3 columns 157ms 159ms
```

The durations shown in the table are averages from multiple runs. The tests were run with builds that had assertions and debugging disabled.

We can see that copying the first 3 columns and middle 3 columns shows a noticeable performance improvement. When copying the last 3 columns, there is little difference before and after the patch.

I ran the tests with builds disabling asserts and debug.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/