From a52747a5d6dc3f1bcabad7b2e18fe2a0d82d383f Mon Sep 17 00:00:00 2001 From: mingjiegao Date: Fri, 6 Nov 2026 17:03:12 +0800 Subject: [PATCH v5] 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. --- src/backend/commands/copyfrom.c | 50 ++++++++++++---- 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, 112 insertions(+), 11 deletions(-) diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index 89f48c8951b..973d3512c84 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. + * + * If attnamelist is NIL, select all columns of the relation, except + * dropped or generated columns. + */ +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,26 @@ 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. See + * 'attnamelist_is_explicit'. * '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 columns of the relation, except + * dropped or generated columns. 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 +1621,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..841868e23ff 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. +$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: d94964f14f39936d69c6b8d3fc5020f0fdae1617