Add support for COPY TO in tablesync for partitioned tables.

Started by Ajin Cherianabout 1 year ago11 messageshackers
Jump to latest
#1Ajin Cherian
itsajin@gmail.com

Hi all,

After patch [1]https://github.com/postgres/postgres/commit/4bea91f21f61d01bd40a4191a4a8c82d0959fffe was committed, the COPY TO command is now supported
for partitioned tables. This change updates the tablesync logic to use
COPY TO for partitioned tables as well. This change will only be
invoked when the publication is configured with
publish_via_partition_root = true;
otherwise, partitioned tables will continue to be published using
their underlying partitions by default and publishing using partitions
already use COPY TO.

[1]: https://github.com/postgres/postgres/commit/4bea91f21f61d01bd40a4191a4a8c82d0959fffe

regards,
Ajin Cherian
Fujitsu Australia

Attachments:

v1-0001-Support-for-COPY-TO-for-partitioned-tables-in-tab.patchapplication/octet-stream; name=v1-0001-Support-for-COPY-TO-for-partitioned-tables-in-tab.patchDownload+3-3
#2Amit Kapila
amit.kapila16@gmail.com
In reply to: Ajin Cherian (#1)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Tue, Nov 11, 2025 at 8:39 AM Ajin Cherian <itsajin@gmail.com> wrote:

After patch [1] was committed, the COPY TO command is now supported
for partitioned tables. This change updates the tablesync logic to use
COPY TO for partitioned tables as well.

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

--
With Regards,
Amit Kapila.

#3Ajin Cherian
itsajin@gmail.com
In reply to: Amit Kapila (#2)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Tue, Nov 11, 2025 at 2:19 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

This was based on the tests done in the original thread [1]/messages/by-id/174219852967.294107.6195385625494034792.pgcf@coridan.postgresql.org and [2]/messages/by-id/CALdSSPi5GUx1XtVTEOmvZ73MDM9HrpzE7L_Dp55z30wfp7KMvw@mail.gmail.com

[1]: /messages/by-id/174219852967.294107.6195385625494034792.pgcf@coridan.postgresql.org
[2]: /messages/by-id/CALdSSPi5GUx1XtVTEOmvZ73MDM9HrpzE7L_Dp55z30wfp7KMvw@mail.gmail.com

regards,
Ajin Cherian
Fujitsu Australia

#4Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Ajin Cherian (#3)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Mon, Nov 10, 2025 at 7:37 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 11, 2025 at 2:19 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

This was based on the tests done in the original thread [1] and [2]

Thank you for working on this item. I think it's a good follow-up
patch for commit 4bea91f.

Have you conducted any performance tests with logical replication
setup? I've measured normal COPY TO cases but I think it would be
worth checking how much the performance increase we can see in logical
replication setup too.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#5Ajin Cherian
itsajin@gmail.com
In reply to: Masahiko Sawada (#4)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Thu, Nov 13, 2025 at 8:49 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Mon, Nov 10, 2025 at 7:37 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 11, 2025 at 2:19 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

This was based on the tests done in the original thread [1] and [2]

Thank you for working on this item. I think it's a good follow-up
patch for commit 4bea91f.

Have you conducted any performance tests with logical replication
setup? I've measured normal COPY TO cases but I think it would be
worth checking how much the performance increase we can see in logical
replication setup too.

Thanks for your interest in this patch.
I've tested the same setup as mentioned in [1]/messages/by-id/174219852967.294107.6195385625494034792.pgcf@coridan.postgresql.org but with 10 tables and
500 records each and measuring the total time it would take for all
the tablesync workers to finish sync (from log timings).
On the average:
Without patch
Tablesync time: 185.4 ms
Average COPY command times: 1.4168 ms

With patch
Tablesync time: 172.2 ms (7% improvement)
Average COPY command times: 0.633 ms

The improvement in performance is smaller as the table size increases.
There is better improvement for smaller tables.
Attaching my test scripts as well.

regards,
Ajin Cherian
Fujitsu Australia

[1]: /messages/by-id/174219852967.294107.6195385625494034792.pgcf@coridan.postgresql.org

Attachments:

log_analyzer_functions.shapplication/octet-stream; name=log_analyzer_functions.shDownload
test-tablesync-perf-v4.shapplication/octet-stream; name=test-tablesync-perf-v4.shDownload
#6Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Ajin Cherian (#5)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Thu, Nov 13, 2025 at 7:17 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Thu, Nov 13, 2025 at 8:49 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Mon, Nov 10, 2025 at 7:37 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 11, 2025 at 2:19 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

This was based on the tests done in the original thread [1] and [2]

Thank you for working on this item. I think it's a good follow-up
patch for commit 4bea91f.

Have you conducted any performance tests with logical replication
setup? I've measured normal COPY TO cases but I think it would be
worth checking how much the performance increase we can see in logical
replication setup too.

Thanks for your interest in this patch.
I've tested the same setup as mentioned in [1] but with 10 tables and
500 records each and measuring the total time it would take for all
the tablesync workers to finish sync (from log timings).
On the average:
Without patch
Tablesync time: 185.4 ms
Average COPY command times: 1.4168 ms

With patch
Tablesync time: 172.2 ms (7% improvement)
Average COPY command times: 0.633 ms

The improvement in performance is smaller as the table size increases.
There is better improvement for smaller tables.
Attaching my test scripts as well.

Thank you for the test! I've also done some performance tests and got
similar results.

The patch is pretty simple and looks good to me. I'll push the patch,
barring objections.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#7Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Masahiko Sawada (#6)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Wed, Nov 19, 2025 at 2:56 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Thu, Nov 13, 2025 at 7:17 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Thu, Nov 13, 2025 at 8:49 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Mon, Nov 10, 2025 at 7:37 PM Ajin Cherian <itsajin@gmail.com> wrote:

On Tue, Nov 11, 2025 at 2:19 PM Amit Kapila <amit.kapila16@gmail.com> wrote:

In the commit message, you mentioned: "Performance tests show it's
faster than the COPY (SELECT ...) TO variant as it avoids the
overheads of query processing and sending results to the COPY TO
command.". Can you share the performance data to substantiate this
point?

This was based on the tests done in the original thread [1] and [2]

Thank you for working on this item. I think it's a good follow-up
patch for commit 4bea91f.

Have you conducted any performance tests with logical replication
setup? I've measured normal COPY TO cases but I think it would be
worth checking how much the performance increase we can see in logical
replication setup too.

Thanks for your interest in this patch.
I've tested the same setup as mentioned in [1] but with 10 tables and
500 records each and measuring the total time it would take for all
the tablesync workers to finish sync (from log timings).
On the average:
Without patch
Tablesync time: 185.4 ms
Average COPY command times: 1.4168 ms

With patch
Tablesync time: 172.2 ms (7% improvement)
Average COPY command times: 0.633 ms

The improvement in performance is smaller as the table size increases.
There is better improvement for smaller tables.
Attaching my test scripts as well.

Thank you for the test! I've also done some performance tests and got
similar results.

The patch is pretty simple and looks good to me. I'll push the patch,
barring objections.

Pushed.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#8Noah Misch
noah@leadboat.com
In reply to: Masahiko Sawada (#7)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Thu, Nov 20, 2025 at 03:09:36PM -0800, Masahiko Sawada wrote:

Pushed.

This became commit 266543a. It doesn't account for the publisher version, so
replicating v18->v19 w/ publish_via_partition_root fails at tablesync w/
"ERROR: cannot copy from partitioned table".

I think commit 266543a also changes the behavior w/ foreign table partitions.
A v19 subscriber will fail w/ "cannot copy from foreign table", whereas an
older subscriber would have completed tablesync but handled no further foreign
table changes. This consequence of 266543a is a net good, since the v18
subscriber behavior is mostly a bug. Let's keep it. If someone really needs
the v18 subscriber behavior, adding a no-op row filter achieves that.

#9Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Noah Misch (#8)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Fri, Sep 4, 2026 at 9:00 PM Noah Misch <noah@leadboat.com> wrote:

On Thu, Nov 20, 2025 at 03:09:36PM -0800, Masahiko Sawada wrote:

Pushed.

This became commit 266543a. It doesn't account for the publisher version, so
replicating v18->v19 w/ publish_via_partition_root fails at tablesync w/
"ERROR: cannot copy from partitioned table".

Good catch. I've attached the patch to fix it.

I think commit 266543a also changes the behavior w/ foreign table partitions.
A v19 subscriber will fail w/ "cannot copy from foreign table", whereas an
older subscriber would have completed tablesync but handled no further foreign
table changes. This consequence of 266543a is a net good, since the v18
subscriber behavior is mostly a bug. Let's keep it. If someone really needs
the v18 subscriber behavior, adding a no-op row filter achieves that.

Agreed.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

Attachments:

t52640_9
v1-0001-Fix-tablesync-failure-for-partitioned-tables-on-o.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Fix-tablesync-failure-for-partitioned-tables-on-o.patchDownload+10-6
#10Noah Misch
noah@leadboat.com
In reply to: Masahiko Sawada (#9)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Tue, Sep 08, 2026 at 10:40:09AM -0700, Masahiko Sawada wrote:

On Fri, Sep 4, 2026 at 9:00 PM Noah Misch <noah@leadboat.com> wrote:

On Thu, Nov 20, 2025 at 03:09:36PM -0800, Masahiko Sawada wrote:

Pushed.

This became commit 266543a. It doesn't account for the publisher version, so
replicating v18->v19 w/ publish_via_partition_root fails at tablesync w/
"ERROR: cannot copy from partitioned table".

Good catch. I've attached the patch to fix it.

Looks reasonable. Thanks.

#11Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Noah Misch (#10)
Re: Add support for COPY TO in tablesync for partitioned tables.

On Tue, Sep 8, 2026 at 10:47 AM Noah Misch <noah@leadboat.com> wrote:

On Tue, Sep 08, 2026 at 10:40:09AM -0700, Masahiko Sawada wrote:

On Fri, Sep 4, 2026 at 9:00 PM Noah Misch <noah@leadboat.com> wrote:

On Thu, Nov 20, 2025 at 03:09:36PM -0800, Masahiko Sawada wrote:

Pushed.

This became commit 266543a. It doesn't account for the publisher version, so
replicating v18->v19 w/ publish_via_partition_root fails at tablesync w/
"ERROR: cannot copy from partitioned table".

Good catch. I've attached the patch to fix it.

Looks reasonable. Thanks.

Thank you for reviewing the patch. Pushed.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com