Skip to content

Commit eeb0eba

Browse files
author
Amit Kapila
committed
Fix the initial sync tables with no columns.
The copy command formed for initial sync was using parenthesis for tables with no columns leading to syntax error. This patch avoids adding parenthesis for such tables. Reported-by: Justin G Author: Vignesh C Reviewed-by: Peter Smith, Amit Kapila Backpatch-through: 15 Discussion: http://postgr.es/m/18203-df37fe354b626670@postgresql.org
1 parent ff68cc6 commit eeb0eba

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/backend/replication/logical/tablesync.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,22 +1124,30 @@ copy_table(Relation rel)
11241124
/* Regular table with no row filter */
11251125
if (lrel.relkind == RELKIND_RELATION && qual == NIL)
11261126
{
1127-
appendStringInfo(&cmd, "COPY %s (",
1127+
appendStringInfo(&cmd, "COPY %s",
11281128
quote_qualified_identifier(lrel.nspname, lrel.relname));
11291129

1130-
/*
1131-
* XXX Do we need to list the columns in all cases? Maybe we're
1132-
* replicating all columns?
1133-
*/
1134-
for (int i = 0; i < lrel.natts; i++)
1130+
/* If the table has columns, then specify the columns */
1131+
if (lrel.natts)
11351132
{
1136-
if (i > 0)
1137-
appendStringInfoString(&cmd, ", ");
1133+
appendStringInfoString(&cmd, " (");
11381134

1139-
appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i]));
1135+
/*
1136+
* XXX Do we need to list the columns in all cases? Maybe we're
1137+
* replicating all columns?
1138+
*/
1139+
for (int i = 0; i < lrel.natts; i++)
1140+
{
1141+
if (i > 0)
1142+
appendStringInfoString(&cmd, ", ");
1143+
1144+
appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i]));
1145+
}
1146+
1147+
appendStringInfoString(&cmd, ")");
11401148
}
11411149

1142-
appendStringInfoString(&cmd, ") TO STDOUT");
1150+
appendStringInfoString(&cmd, " TO STDOUT");
11431151
}
11441152
else
11451153
{

src/test/subscription/t/001_rep_changes.pl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
"CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)"
5858
);
5959

60+
# Replicate the changes without columns
61+
$node_publisher->safe_psql('postgres', "CREATE TABLE tab_no_col()");
62+
$node_publisher->safe_psql('postgres',
63+
"INSERT INTO tab_no_col default VALUES");
64+
6065
# Setup structure on subscriber
6166
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_notrep (a int)");
6267
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_ins (a int)");
@@ -87,13 +92,16 @@
8792
"CREATE INDEX idx_no_replidentity_index ON tab_no_replidentity_index(c1)"
8893
);
8994

95+
# replication of the table without columns
96+
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_no_col()");
97+
9098
# Setup logical replication
9199
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
92100
$node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub");
93101
$node_publisher->safe_psql('postgres',
94102
"CREATE PUBLICATION tap_pub_ins_only WITH (publish = insert)");
95103
$node_publisher->safe_psql('postgres',
96-
"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"
104+
"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"
97105
);
98106
$node_publisher->safe_psql('postgres',
99107
"ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins");
@@ -141,6 +149,9 @@
141149
$node_publisher->safe_psql('postgres',
142150
"INSERT INTO tab_no_replidentity_index VALUES(1)");
143151

152+
$node_publisher->safe_psql('postgres',
153+
"INSERT INTO tab_no_col default VALUES");
154+
144155
$node_publisher->wait_for_catchup('tap_sub');
145156

146157
$result = $node_subscriber->safe_psql('postgres',
@@ -169,6 +180,10 @@
169180
1,
170181
"value replicated to subscriber without replica identity index");
171182

183+
$result =
184+
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_no_col");
185+
is($result, qq(2), 'check replicated changes for table having no columns');
186+
172187
# insert some duplicate rows
173188
$node_publisher->safe_psql('postgres',
174189
"INSERT INTO tab_full SELECT generate_series(1,10)");

0 commit comments

Comments
 (0)