pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedAdd support for COPY TO in tablesync for partitioned tables.
11+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2026-09-08 11fb89fdbd fa48e39526 Fix tablesync failure for partitioned tables on older publishers.
2025-11-20 266543a620 Use "COPY table TO" for partitioned tables in initial table synchronization.
* Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-11 03:08 Ajin Cherian <itsajin@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Ajin Cherian @ 2025-11-11 03:08 UTC (permalink / raw)
To: pgsql-hackers
Hi all,
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. 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.patch (application/octet-stream, 1.4K) download
From 370517cad57eb36cd93e1b5ef5135561732b1505 Mon Sep 17 00:00:00 2001
From: Ajin Cherian <itsajin@gmail.com>
Date: Tue, 11 Nov 2025 13:43:16 +1100
Subject: [PATCH v1] Support for COPY TO for partitioned tables in tablesync
This commit modifies tablesync logic to use COPY TO for partitioned tables. 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.
---
src/backend/replication/logical/tablesync.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index dcc6124cc73..fa8e3bf969a 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1068,8 +1068,9 @@ copy_table(Relation rel)
/* Start copy on the publisher. */
initStringInfo(&cmd);
- /* Regular table with no row filter or generated columns */
- if (lrel.relkind == RELKIND_RELATION && qual == NIL && !gencol_published)
+ /* Regular or partitioned table with no row filter or generated columns */
+ if ((lrel.relkind == RELKIND_RELATION || lrel.relkind == RELKIND_PARTITIONED_TABLE)
+ && qual == NIL && !gencol_published)
{
appendStringInfo(&cmd, "COPY %s",
quote_qualified_identifier(lrel.nspname, lrel.relname));
--
2.47.3
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-11 03:18 Amit Kapila <amit.kapila16@gmail.com>
parent: Ajin Cherian <itsajin@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Amit Kapila @ 2025-11-11 03:18 UTC (permalink / raw)
To: Ajin Cherian <itsajin@gmail.com>; +Cc: pgsql-hackers
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.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-11 03:37 Ajin Cherian <itsajin@gmail.com>
parent: Amit Kapila <amit.kapila16@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Ajin Cherian @ 2025-11-11 03:37 UTC (permalink / raw)
To: Amit Kapila <amit.kapila16@gmail.com>; +Cc: pgsql-hackers
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]
[1]- https://www.postgresql.org/message-id/174219852967.294107.6195385625494034792.pgcf%40coridan.postgre...
[2]- https://www.postgresql.org/message-id/CALdSSPi5GUx1XtVTEOmvZ73MDM9HrpzE7L_Dp55z30wfp7KMvw%40mail.gma...
regards,
Ajin Cherian
Fujitsu Australia
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-12 21:49 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Ajin Cherian <itsajin@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Masahiko Sawada @ 2025-11-12 21:49 UTC (permalink / raw)
To: Ajin Cherian <itsajin@gmail.com>; +Cc: Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-14 03:17 Ajin Cherian <itsajin@gmail.com>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Ajin Cherian @ 2025-11-14 03:17 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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.
regards,
Ajin Cherian
Fujitsu Australia
[1] - https://www.postgresql.org/message-id/174219852967.294107.6195385625494034792.pgcf%40coridan.postgre...
Attachments:
log_analyzer_functions.sh (application/octet-stream, 7.1K) download
test-tablesync-perf-v4.sh (application/octet-stream, 5.0K) download
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-19 22:56 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Ajin Cherian <itsajin@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Masahiko Sawada @ 2025-11-19 22:56 UTC (permalink / raw)
To: Ajin Cherian <itsajin@gmail.com>; +Cc: Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2025-11-20 23:09 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Masahiko Sawada @ 2025-11-20 23:09 UTC (permalink / raw)
To: Ajin Cherian <itsajin@gmail.com>; +Cc: Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2026-09-05 04:00 Noah Misch <noah@leadboat.com>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Noah Misch @ 2026-09-05 04:00 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Ajin Cherian <itsajin@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2026-09-08 17:40 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Noah Misch <noah@leadboat.com>
0 siblings, 1 reply; 11+ messages in thread
From: Masahiko Sawada @ 2026-09-08 17:40 UTC (permalink / raw)
To: Noah Misch <noah@leadboat.com>; +Cc: Ajin Cherian <itsajin@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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:
v1-0001-Fix-tablesync-failure-for-partitioned-tables-on-o.patch (text/x-patch, 2.7K) download
From c9f09d70b02b95e929261d0a293c692e7247fc2f Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Tue, 8 Sep 2026 10:18:13 -0700
Subject: [PATCH v1] Fix tablesync failure for partitioned tables on older
publishers.
Commit 266543a made initial table synchronization use "COPY table TO"
for a partitioned table, but decided that from the relation kind
alone. A partitioned table is only accepted there since v19, so a v19
subscriber replicating from an older publisher with
publish_via_partition_root failed at table sync.
Check the publisher version, and fall back to the "COPY (SELECT ...)
TO" variant otherwise.
Backpatch to v19, where 266543a changed the initial table
synchronization.
Reported-by: Noah Misch <noah@leadboat.com>
Reviewed-by:
Discussion: https://postgr.es/m/20260905040020.57.noahmisch@microsoft.com
Backpatch-through: 19
---
src/backend/replication/logical/tablesync.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index e5101997cd3..4015e861a64 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1084,6 +1084,7 @@ copy_table(Relation rel)
ParseState *pstate;
List *options = NIL;
bool gencol_published = false;
+ int server_version = walrcv_server_version(LogRepWorkerWalRcvConn);
/* Get the publisher relation info. */
fetch_remote_table_info(get_namespace_name(RelationGetNamespace(rel)),
@@ -1100,9 +1101,14 @@ copy_table(Relation rel)
/* Start copy on the publisher. */
initStringInfo(&cmd);
- /* Regular or partitioned table with no row filter or generated columns */
- if ((lrel.relkind == RELKIND_RELATION || lrel.relkind == RELKIND_PARTITIONED_TABLE)
- && qual == NIL && !gencol_published)
+ /*
+ * Regular or partitioned table with no row filter or generated columns.
+ *
+ * "COPY table TO" on a partitioned table is supported since v19.
+ */
+ if ((lrel.relkind == RELKIND_RELATION ||
+ (lrel.relkind == RELKIND_PARTITIONED_TABLE && server_version >= 190000)) &&
+ qual == NIL && !gencol_published)
{
appendStringInfo(&cmd, "COPY %s",
quote_qualified_identifier(lrel.nspname, lrel.relname));
@@ -1182,8 +1188,7 @@ copy_table(Relation rel)
* Prior to v16, initial table synchronization will use text format even
* if the binary option is enabled for a subscription.
*/
- if (walrcv_server_version(LogRepWorkerWalRcvConn) >= 160000 &&
- MySubscription->binary)
+ if (server_version >= 160000 && MySubscription->binary)
{
appendStringInfoString(&cmd, " WITH (FORMAT binary)");
options = list_make1(makeDefElem("format",
--
2.55.0
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2026-09-08 17:47 Noah Misch <noah@leadboat.com>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Noah Misch @ 2026-09-08 17:47 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Ajin Cherian <itsajin@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Add support for COPY TO in tablesync for partitioned tables.
@ 2026-09-08 18:15 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Noah Misch <noah@leadboat.com>
0 siblings, 0 replies; 11+ messages in thread
From: Masahiko Sawada @ 2026-09-08 18:15 UTC (permalink / raw)
To: Noah Misch <noah@leadboat.com>; +Cc: Ajin Cherian <itsajin@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; pgsql-hackers
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
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2026-09-08 18:15 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-11-11 03:08 Add support for COPY TO in tablesync for partitioned tables. Ajin Cherian <itsajin@gmail.com>
2025-11-11 03:18 ` Amit Kapila <amit.kapila16@gmail.com>
2025-11-11 03:37 ` Ajin Cherian <itsajin@gmail.com>
2025-11-12 21:49 ` Masahiko Sawada <sawada.mshk@gmail.com>
2025-11-14 03:17 ` Ajin Cherian <itsajin@gmail.com>
2025-11-19 22:56 ` Masahiko Sawada <sawada.mshk@gmail.com>
2025-11-20 23:09 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-05 04:00 ` Noah Misch <noah@leadboat.com>
2026-09-08 17:40 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-08 17:47 ` Noah Misch <noah@leadboat.com>
2026-09-08 18:15 ` Masahiko Sawada <sawada.mshk@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox