From 8daf7bfbd7659be4f64e7963c07e3818200ece26 Mon Sep 17 00:00:00 2001 From: mingjiegao Date: Fri, 6 Nov 2026 17:03:12 +0800 Subject: [PATCH v4] Fix initial sync of zero-column published tables During initial table synchronization, a published table with no columns gives tablesync an empty list of input columns. COPY FROM interprets that list (NIL) as an omitted column list and expects a field for every subscriber column except dropped and generated columns. The publisher sends no fields. In text format, COPY reads the empty line for each row as one empty field. A single text column on the subscriber then receives an empty string in place of its default, while an integer column rejects the empty string. In binary format, each row carries a field count of zero. With one input column on the subscriber, the sync worker repeatedly fails with "row field count is 0, expected 1". Add BeginCopyFromExtended() so tablesync can mark its column list as explicit. An empty list then selects zero input columns, and COPY applies local defaults just as it does for other omitted columns. SQL COPY cannot specify an explicitly empty column list. Keep BeginCopyFrom() as a wrapper that passes false for the new flag so its existing callers retain the usual interpretation of NIL. --- Changes since v3: - Expand the commit message as suggested by Vignesh. - Rebase on master at f03a0f87963. src/backend/commands/copyfrom.c | 49 ++++++++++++---- src/backend/replication/logical/tablesync.c | 3 +- src/include/commands/copy.h | 5 ++ src/test/subscription/t/008_diff_schema.pl | 65 +++++++++++++++++++++ 4 files changed, 111 insertions(+), 11 deletions(-) diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index 89f48c8951b..b838ef325bc 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -1522,6 +1522,27 @@ CopyFrom(CopyFromState cstate) return processed; } +/* + * Wrapper for BeginCopyFromExtended() with attnamelist_is_explicit = false. + * + * A NIL attnamelist selects all target columns that are neither dropped nor + * generated. + */ +CopyFromState +BeginCopyFrom(ParseState *pstate, + Relation rel, + Node *whereClause, + const char *filename, + bool is_program, + copy_data_source_cb data_source_cb, + List *attnamelist, + List *options) +{ + return BeginCopyFromExtended(pstate, rel, whereClause, filename, + is_program, data_source_cb, attnamelist, + options, false); +} + /* * Setup to read tuples from a file for COPY FROM. * @@ -1530,20 +1551,25 @@ CopyFrom(CopyFromState cstate) * 'filename': Name of server-local file to read, NULL for STDIN * 'is_program': true if 'filename' is program to execute * 'data_source_cb': callback that provides the input data - * 'attnamelist': List of char *, columns to include. NIL selects all cols. + * 'attnamelist': List of char *, columns to include. * 'options': List of DefElem. See copy_opt_item in gram.y for selections. + * 'attnamelist_is_explicit': If true, a NIL attnamelist means zero input + * columns. Otherwise, NIL selects all target columns that are neither + * dropped nor generated. This is needed when a logical replication source + * has no columns. * * Returns a CopyFromState, to be passed to NextCopyFrom and related functions. */ CopyFromState -BeginCopyFrom(ParseState *pstate, - Relation rel, - Node *whereClause, - const char *filename, - bool is_program, - copy_data_source_cb data_source_cb, - List *attnamelist, - List *options) +BeginCopyFromExtended(ParseState *pstate, + Relation rel, + Node *whereClause, + const char *filename, + bool is_program, + copy_data_source_cb data_source_cb, + List *attnamelist, + List *options, + bool attnamelist_is_explicit) { CopyFromState cstate; bool pipe = (filename == NULL); @@ -1594,7 +1620,10 @@ BeginCopyFrom(ParseState *pstate, /* process common options or initialization */ /* Generate or convert list of attributes to process */ - cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist); + if (attnamelist_is_explicit && attnamelist == NIL) + cstate->attnumlist = NIL; + else + cstate->attnumlist = CopyGetAttnums(tupDesc, cstate->rel, attnamelist); num_phys_attrs = tupDesc->natts; diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index 4015e861a64..723012fb0c0 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -1211,7 +1211,8 @@ copy_table(Relation rel) NULL, false, false); attnamelist = make_copy_attnamelist(relmapentry); - cstate = BeginCopyFrom(pstate, rel, NULL, NULL, false, copy_read_data, attnamelist, options); + cstate = BeginCopyFromExtended(pstate, rel, NULL, NULL, false, + copy_read_data, attnamelist, options, true); /* Do the copy */ (void) CopyFrom(cstate); diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h index abecfe51098..74203f3547f 100644 --- a/src/include/commands/copy.h +++ b/src/include/commands/copy.h @@ -114,6 +114,11 @@ extern void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *opts_out, extern CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause, const char *filename, bool is_program, copy_data_source_cb data_source_cb, List *attnamelist, List *options); +extern CopyFromState BeginCopyFromExtended(ParseState *pstate, Relation rel, + Node *whereClause, const char *filename, + bool is_program, copy_data_source_cb data_source_cb, + List *attnamelist, List *options, + bool attnamelist_is_explicit); extern void EndCopyFrom(CopyFromState cstate); extern bool NextCopyFrom(CopyFromState cstate, ExprContext *econtext, Datum *values, bool *nulls); diff --git a/src/test/subscription/t/008_diff_schema.pl b/src/test/subscription/t/008_diff_schema.pl index 96e3d6f5888..5482b69c451 100644 --- a/src/test/subscription/t/008_diff_schema.pl +++ b/src/test/subscription/t/008_diff_schema.pl @@ -31,6 +31,71 @@ $node_subscriber->safe_psql('postgres', # Setup logical replication my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; + +# A zero-column publisher still sends rows. Subscriber-only columns must +# get their defaults during initial COPY as well as incremental replication. +$node_publisher->safe_psql( + 'postgres', q{ + CREATE TABLE tab_nocols (); + CREATE PUBLICATION pub_nocols FOR TABLE tab_nocols; +}); +$node_subscriber->safe_psql('postgres', + "CREATE TABLE tab_nocols (local_value text DEFAULT 'subscriber-default');" +); + +foreach my $binary ('false', 'true') +{ + $node_publisher->safe_psql( + 'postgres', q{ + INSERT INTO tab_nocols DEFAULT VALUES; + INSERT INTO tab_nocols DEFAULT VALUES; + }); + $node_subscriber->safe_psql('postgres', + "CREATE SUBSCRIPTION sub_nocols CONNECTION '$publisher_connstr' PUBLICATION pub_nocols WITH (binary = $binary);" + ); + + # Test initial sync. + $node_subscriber->wait_for_subscription_sync($node_publisher, 'sub_nocols'); + + is( $node_subscriber->safe_psql( + 'postgres', q{ + SELECT count(*) FROM tab_nocols + WHERE local_value = 'subscriber-default'; + }), + '2', + "initial zero-column rows use local defaults (binary = $binary)"); + + # Test incremental replication. + $node_publisher->safe_psql('postgres', + "INSERT INTO tab_nocols DEFAULT VALUES;"); + + $node_publisher->wait_for_catchup('sub_nocols'); + + is( $node_subscriber->safe_psql( + 'postgres', q{ + SELECT count(*) FROM tab_nocols + WHERE local_value = 'subscriber-default'; + }), + '3', + "incremental zero-column rows use local defaults (binary = $binary)"); + + # Drop the subscription before resetting both tables for the next mode. + $node_subscriber->safe_psql( + 'postgres', q{ + DROP SUBSCRIPTION sub_nocols; + TRUNCATE tab_nocols; + }); + $node_publisher->safe_psql('postgres', 'TRUNCATE tab_nocols;'); +} + +# Clean up before creating the FOR ALL TABLES publication. +$node_publisher->safe_psql( + 'postgres', q{ + DROP PUBLICATION pub_nocols; + DROP TABLE tab_nocols; +}); +$node_subscriber->safe_psql('postgres', 'DROP TABLE tab_nocols;'); + $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR ALL TABLES"); base-commit: f03a0f879631d2e310f01d9bf0016d5298f4dbe0