From f8b5649dd70a92982a2590e9dcee8bdebbb581a1 Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Tue, 21 Nov 2023 10:12:25 +0530 Subject: [PATCH v3] Specify parentheses for tablesync worker copy command only if the table has some column. Copy command was specifying parentheses for tables having no columns too, fixed it by specifying the parentheses and columns only if table has some columns. --- src/backend/replication/logical/tablesync.c | 28 +++++++++++++-------- src/test/subscription/t/001_rep_changes.pl | 17 ++++++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index b7b933d468..013ce71f99 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -1043,22 +1043,30 @@ copy_table(Relation rel) /* Regular table with no row filter */ if (lrel.relkind == RELKIND_RELATION && qual == NIL) { - appendStringInfo(&cmd, "COPY %s (", + appendStringInfo(&cmd, "COPY %s", quote_qualified_identifier(lrel.nspname, lrel.relname)); - /* - * XXX Do we need to list the columns in all cases? Maybe we're - * replicating all columns? - */ - for (int i = 0; i < lrel.natts; i++) + /* If the table has columns, then specify the columns */ + if (lrel.natts) { - if (i > 0) - appendStringInfoString(&cmd, ", "); + appendStringInfoString(&cmd, " ("); - appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i])); + /* + * XXX Do we need to list the columns in all cases? Maybe we're + * replicating all columns? + */ + for (int i = 0; i < lrel.natts; i++) + { + if (i > 0) + appendStringInfoString(&cmd, ", "); + + appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i])); + } + + appendStringInfoString(&cmd, ")"); } - appendStringInfo(&cmd, ") TO STDOUT"); + appendStringInfo(&cmd, " TO STDOUT"); } else { diff --git a/src/test/subscription/t/001_rep_changes.pl b/src/test/subscription/t/001_rep_changes.pl index 6ed9265b2b..6e6ebd80e1 100644 --- a/src/test/subscription/t/001_rep_changes.pl +++ b/src/test/subscription/t/001_rep_changes.pl @@ -57,6 +57,11 @@ $node_publisher->safe_psql('postgres', "CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)" ); +# Replicate the changes without columns +$node_publisher->safe_psql('postgres', "CREATE TABLE tab_no_col()"); +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_no_col default VALUES"); + # Setup structure on subscriber $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_notrep (a int)"); $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_ins (a int)"); @@ -87,13 +92,16 @@ $node_subscriber->safe_psql('postgres', "CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)" ); +# replication of the table without columns +$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_no_col()"); + # Setup logical replication my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub"); $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub_ins_only WITH (publish = insert)"); $node_publisher->safe_psql('postgres', - "ALTER PUBLICATION tap_pub ADD TABLE tab_rep, tab_full, tab_full2, tab_mixed, tab_include, tab_nothing, tab_full_pk, tab_no_replidentity_index" + "ALTER PUBLICATION tap_pub ADD TABLE tab_rep, tab_full, tab_full2, tab_mixed, tab_include, tab_nothing, tab_full_pk, tab_no_replidentity_index, tab_no_col" ); $node_publisher->safe_psql('postgres', "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins"); @@ -141,6 +149,9 @@ $node_publisher->safe_psql('postgres', "UPDATE tab_include SET a = -a"); $node_publisher->safe_psql('postgres', "INSERT INTO tab_no_replidentity_index VALUES(1)"); +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_no_col default VALUES"); + $node_publisher->wait_for_catchup('tap_sub'); $result = $node_subscriber->safe_psql('postgres', @@ -169,6 +180,10 @@ is( $node_subscriber->safe_psql( 1, "value replicated to subscriber without replica identity index"); +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_no_col"); +is($result, qq(2), 'check replicated changes for table having no columns'); + # insert some duplicate rows $node_publisher->safe_psql('postgres', "INSERT INTO tab_full SELECT generate_series(1,10)"); -- 2.34.1