From 617023fc03dcb99d1930a3d76ae39041f9392bd5 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. 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 | 54 +++++++++++++++++++++ 4 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index 182047a4a40..090d4c81ca8 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -1175,6 +1175,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. * @@ -1183,20 +1204,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); @@ -1246,7 +1273,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 b884f6240e5..2498171dd45 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -1165,7 +1165,8 @@ copy_table(Relation rel) NULL, false, false); attnamelist = make_copy_attnamelist(relmapentry); - cstate = BeginCopyFrom(pstate, rel, NULL, NULL, false, copy_read_data, attnamelist, NIL); + cstate = BeginCopyFromExtended(pstate, rel, NULL, NULL, false, + copy_read_data, attnamelist, NIL, true); /* Do the copy */ (void) CopyFrom(cstate); diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h index cb0096aeb67..7171a141784 100644 --- a/src/include/commands/copy.h +++ b/src/include/commands/copy.h @@ -75,6 +75,11 @@ extern void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *ops_out, b 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 b4d44a200bb..f4e16132e45 100644 --- a/src/test/subscription/t/008_diff_schema.pl +++ b/src/test/subscription/t/008_diff_schema.pl @@ -31,6 +31,60 @@ $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; + INSERT INTO tab_nocols DEFAULT VALUES; + INSERT INTO tab_nocols DEFAULT VALUES; +}); +$node_subscriber->safe_psql('postgres', + "CREATE TABLE tab_nocols (local_value text DEFAULT 'subscriber-default');" +); +$node_subscriber->safe_psql('postgres', + "CREATE SUBSCRIPTION sub_nocols CONNECTION '$publisher_connstr' PUBLICATION pub_nocols;" +); + +# 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'); + +# 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'); + +# Clean up. +$node_subscriber->safe_psql( + 'postgres', q{ + DROP SUBSCRIPTION sub_nocols; + DROP TABLE tab_nocols; +}); +$node_publisher->safe_psql( + 'postgres', q{ + DROP PUBLICATION pub_nocols; + DROP TABLE tab_nocols; +}); + $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR ALL TABLES"); base-commit: e5b33ff4514c6b7da8d7b8cbb6e157fba302e432