[PATCH v1] Fix initial sync of zero-column published tables

Started by Mingjie Gao17 days ago8 messageshackers
Jump to latest
#1Mingjie Gao
mingjie.gmj@gmail.com

Hi,

Initial table synchronization mishandles a zero-column published table
when the subscriber has additional columns. In text mode, COPY can put
an empty string into a subscriber-only column instead of its default.
Subsequent inserts through the apply worker use the default correctly.
In binary mode, the initial copy fails with a field-count mismatch.

I reproduced this on master (20devel) at:
77705d1e5a616339c3e92f586bbfa7b6bb562beb

For example, use two fresh instances with logical replication enabled
on the publisher:

-- Publisher:
CREATE TABLE lr02_t ();
INSERT INTO lr02_t DEFAULT VALUES;
CREATE PUBLICATION lr02_pub FOR TABLE lr02_t WITH (publish = 'insert');

-- Subscriber (replace the connection string):
CREATE TABLE lr02_t (v text DEFAULT 'subscriber-default');
CREATE SUBSCRIPTION lr02_sub
CONNECTION '<publisher connection string>'
PUBLICATION lr02_pub WITH (copy_data = true, binary = false);

On the subscriber, wait for this query to return 'r':

SELECT srsubstate FROM pg_subscription_rel
WHERE srsubid = (SELECT oid FROM pg_subscription
WHERE subname = 'lr02_sub');

Then run:

SELECT quote_nullable(v) FROM lr02_t;

The result is '' instead of 'subscriber-default'. Inserting another row
with DEFAULT VALUES on the publisher produces a correctly defaulted row
on the subscriber once replication catches up. Thus initial and
incremental replication disagree for the same source data.

Repeating the example on fresh instances with binary = true instead
makes initial synchronization fail with:

ERROR: row field count is 0, expected 1

make_copy_attnamelist() returns NIL for the zero-column source, but
BeginCopyFrom() interprets NIL as an unspecified column list and selects
all target non-dropped, non-generated columns. Those columns are then
treated as input columns, so their default expressions are not prepared.

The attached patch adds BeginCopyFromExtended() with a flag indicating
that the column list is explicit. Tablesync uses that entry point, so
an empty list remains empty. This is resolved before default expressions
and parser state are initialized. The existing BeginCopyFrom() interface
retains its behavior for SQL COPY, file_fdw and other callers.

The change keeps row parsing, default evaluation, constraints, triggers
and partition routing in the existing COPY path. There are no SQL syntax
or replication protocol changes. Its scope is the handling of an empty
input column list after remote column discovery; it does not change
which publisher columns are selected for replication.

The patch includes text and binary regression coverage in
008_diff_schema.pl. With only the test changes applied to the baseline,
three assertions fail and two dependent assertions are skipped. With
the fix, all 11 assertions pass.

To reproduce the failure, apply just the test portion to a clean
checkout of the baseline:

git apply --include=src/test/subscription/t/008_diff_schema.pl \
/path/to/v1-0001-Fix-initial-sync-of-zero-column-published-tables.patch

From a configured and built tree with TAP tests enabled, run:

make -j8 -C src/test/subscription check \
PROVE_TESTS=t/008_diff_schema.pl

For an out-of-tree build, run the make command from the build directory.
Applying the remaining source/header changes and rebuilding makes the
test pass.

Testing used Linux x86_64, GCC 15.2, -O2 -g, with assertions disabled.
The targeted subscription tests 004_sync, 008_diff_schema, 014_binary
and 031_column_list pass (64 assertions). A separate 24-assertion test
also passes, covering local defaults, identity values, NULLs, subscriber
stored generated columns, replica triggers, partition routing, dropped
publisher columns and NOT NULL checks in text and binary modes.

The test_setup, copy, copyselect, copydml and copyencoding regression
tests, and the file_fdw regression test, also pass. I have not tested
older major versions or run the full test suite.

Comments on the internal COPY interface change are welcome.

Best regards,
mingjiegao

Attachments:

t254504_1
v1-0001-Fix-initial-sync-of-zero-column-published-tables.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Fix-initial-sync-of-zero-column-published-tables.patchDownload+104-3
#2Mingjie Gao
mingjie.gmj@gmail.com
In reply to: Mingjie Gao (#1)
Re: [PATCH v1] Fix initial sync of zero-column published tables

Hi,

I've attached lr02.sql, a minimal psql reproducer for this issue in
text mode (binary = false). It uses separate publisher and subscriber
databases in the same instance.

On an affected build, the initial COPY row gets an empty string
instead of the subscriber's default, while a subsequent incremental
INSERT uses the default correctly.

Please run it on a test instance with logical replication enabled and
authentication configured for the subscription connection.

Best regards,
mingjiegao

Attachments:

lr02.sqltext/plain; charset=US-ASCII; name=lr02.sqlDownload
#3Peter Smith
smithpb2250@gmail.com
In reply to: Mingjie Gao (#1)
Re: [PATCH v1] Fix initial sync of zero-column published tables

Hi mingjiegao.

I was able to reproduce your reported tablesync bug for a table with zero cols.

Using:
test_pub=# CREATE TABLE t_nocols();
test_pub=# INSERT INTO T_NOCOLS DEFAULT VALUES;

and:
test_sub=# create table t_nocols(local_col text default
'subscriber-col-default');

~~~

Also confirmed the issue was fixed after applying the v1 patch.

~~~

Here are some minor review comments:

1.
Now that BeginCopyFrom is just a thin "wrapper" should all that
commentary describing each of the function parameters be moved to
BeginCopyFromExtended function-comment where all the work is done?

2.
The TAP test code for this patch seems more elaborate than usual.
AFAIK it is normal to accept that regressions show up as a timeout.

My LLM suggested simpler, more typical-looking test code, like below:

--------------------
# Zero-column publisher table; a subscriber-only column must get its
# default during initial sync in both text and binary mode.
$node_publisher->safe_psql('postgres', q{
CREATE TABLE tab_nocols_text ();
CREATE TABLE tab_nocols_bin ();
INSERT INTO tab_nocols_text DEFAULT VALUES;
INSERT INTO tab_nocols_bin DEFAULT VALUES;
CREATE PUBLICATION pub_nocols_text FOR TABLE tab_nocols_text;
CREATE PUBLICATION pub_nocols_bin FOR TABLE tab_nocols_bin;
});
$node_subscriber->safe_psql('postgres', q{
CREATE TABLE tab_nocols_text (c text DEFAULT 'sub-default');
CREATE TABLE tab_nocols_bin (c text DEFAULT 'sub-default');
});
$node_subscriber->safe_psql('postgres',
"CREATE SUBSCRIPTION sub_nocols_text CONNECTION
'$publisher_connstr' PUBLICATION pub_nocols_text WITH (binary =
false)");
$node_subscriber->safe_psql('postgres',
"CREATE SUBSCRIPTION sub_nocols_bin CONNECTION
'$publisher_connstr' PUBLICATION pub_nocols_bin WITH (binary =
true)");
$node_subscriber->wait_for_subscription_sync($node_publisher,
'sub_nocols_text');
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub_nocols_bin');

is($node_subscriber->safe_psql('postgres',
"SELECT c FROM tab_nocols_text"), 'sub-default',
'zero-column initial sync uses subscriber default (text)');
is($node_subscriber->safe_psql('postgres',
"SELECT c FROM tab_nocols_bin"), 'sub-default',
'zero-column initial sync uses subscriber default (binary)');
--------------------

======
Kind Regards,
Peter Smith.
Fujitsu Australia

#4Mingjie Gao
mingjie.gmj@gmail.com
In reply to: Peter Smith (#3)
Re: [PATCH v1] Fix initial sync of zero-column published tables

Hi Peter,

Thanks for reproducing the issue and testing the patch.

Now that BeginCopyFrom is just a thin "wrapper" should all that
commentary describing each of the function parameters be moved to
BeginCopyFromExtended function-comment where all the work is done?

Agreed. I've moved the parameter descriptions to the comment for
BeginCopyFromExtended().

The TAP test code for this patch seems more elaborate than usual.
AFAIK it is normal to accept that regressions show up as a timeout.

I've simplified the test to use wait_for_subscription_sync(). It still
checks both text and binary modes, and retains the incremental insert check
alongside the initial sync check.

Please find the updated v2 patch attached.

Best regards,
mingjiegao

Attachments:

t254504_4
v2-0001-Fix-initial-sync-of-zero-column-published-tables.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Fix-initial-sync-of-zero-column-published-tables.patchDownload+93-13
#5Peter Smith
smithpb2250@gmail.com
In reply to: Mingjie Gao (#4)
Re: [PATCH v1] Fix initial sync of zero-column published tables

On Thu, Nov 5, 2026 at 1:48 PM Mingjie Gao <mingjie.gmj@gmail.com> wrote:
...

Please find the updated v2 patch attached.

Some minor comments for the v2 test code.

======
src/test/subscription/t/008_diff_schema.pl

1.
+ CREATE PUBLICATION pub_nocols FOR TABLE tab_nocols
+ WITH (publish = 'insert');

Saying publish = 'insert' seems overkill.

~~~

2.
+ $node_subscriber->safe_psql('postgres', 'DROP SUBSCRIPTION sub_nocols');
+ $node_publisher->safe_psql('postgres',
+ 'DROP PUBLICATION pub_nocols; DROP TABLE tab_nocols');
+ $node_subscriber->safe_psql('postgres', 'DROP TABLE tab_nocols');

Those could be combined a bit...

~~~

3. Other misc refactoring

some spaces
some comments
some more consistent use of terminating ';'
etc.

~~~

I've made a few tmp changes (only cosmetic -- nothing functional).
e.g., see my attached version of the 008_diff_schema.pl changes for reference.

======
Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

nocfbot-PS-tests.txttext/plain; charset=US-ASCII; name=nocfbot-PS-tests.txtDownload+57-0
#6vignesh C
vignesh21@gmail.com
In reply to: Mingjie Gao (#4)
Re: [PATCH v1] Fix initial sync of zero-column published tables

On Thu, 5 Nov 2026 at 08:18, Mingjie Gao <mingjie.gmj@gmail.com> wrote:

Hi Peter,

Thanks for reproducing the issue and testing the patch.

Now that BeginCopyFrom is just a thin "wrapper" should all that
commentary describing each of the function parameters be moved to
BeginCopyFromExtended function-comment where all the work is done?

Agreed. I've moved the parameter descriptions to the comment for BeginCopyFromExtended().

The TAP test code for this patch seems more elaborate than usual.
AFAIK it is normal to accept that regressions show up as a timeout.

I've simplified the test to use wait_for_subscription_sync(). It still checks both text and binary modes, and retains the incremental insert check alongside the initial sync check.

Please find the updated v2 patch attached.

Thanks for the patch.
Few minor suggestions for the test:
1) Can we create the table in publisher and subscriber outside the
loop instead of doing it twice, similarly the publication can be
created outside the loop:
+# A zero-column publisher still sends rows.  Subscriber-only columns must
+# get their defaults during initial COPY as well as incremental replication.
+foreach my $binary ('false', 'true')
+{
+       $node_publisher->safe_psql(
+               'postgres', q{
+               CREATE TABLE tab_nocols ();
+               INSERT INTO tab_nocols DEFAULT VALUES;
+               INSERT INTO tab_nocols DEFAULT VALUES;
+               CREATE PUBLICATION pub_nocols FOR TABLE tab_nocols
+                       WITH (publish = 'insert');
+       });
+       $node_subscriber->safe_psql('postgres',
+               "CREATE TABLE tab_nocols (local_value text DEFAULT
'subscriber-default')"
+       );
2)  With the first changes, we can truncate the tables instead of dropping them:
+       $node_publisher->safe_psql('postgres',
+               'DROP PUBLICATION pub_nocols; DROP TABLE tab_nocols');
+       $node_subscriber->safe_psql('postgres', 'DROP TABLE tab_nocols');

3) Till which branch will this patch be applied?

Regards,
Vignesh

#7Mingjie Gao
mingjie.gmj@gmail.com
In reply to: vignesh C (#6)
Re: [PATCH v1] Fix initial sync of zero-column published tables

Hi Peter, Vignesh,

Thanks for the review. Attached is v3, based on the same master commit:
77705d1e5a616339c3e92f586bbfa7b6bb562beb.

Vignesh wrote:

1) Can we create the table in publisher and subscriber outside the
loop instead of doing it twice, similarly the publication can be
created outside the loop:

Done.

2) With the first changes, we can truncate the tables instead of dropping them:

Done. The subscription is dropped before truncating the tables between
modes. Each mode still checks initial sync and an incremental insert.

Peter wrote:

Saying publish = 'insert' seems overkill.

Removed.

Those could be combined a bit...

I've combined the cleanup and tidied the comments, spacing and
semicolons as suggested in your attachment. The C code is unchanged
from v2.

Vignesh wrote:

3) Till which branch will this patch be applied?

I reproduced the text-mode issue on PG10. I haven't reproduced it on
the other versions, but the code appears to have the same problem.
The binary case appears to be affected from PG16, which added binary
initial sync.

I think we should backpatch to the affected supported branches. I have
only tested the patch on master so far.

I tested v3 on master with assertions enabled. The regression tests and
the relevant logical replication tests passed.

Best regards,
mingjiegao

Attachments:

t254504_7
v3-0001-Fix-initial-sync-of-zero-column-published-tables.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Fix-initial-sync-of-zero-column-published-tables.patchDownload+111-14
#8Joao Rito
joaovictor.rito@gmail.com
In reply to: Mingjie Gao (#7)
Re: [PATCH v1] Fix initial sync of zero-column published tables

Hi Mingjie,

Thank you for the patch. I tried v3 and it looks good to me.

What I did:

- Applied v3 to current master (debfb3c8a8). It applies cleanly and builds
without warnings (--enable-cassert --enable-tap-tests).
- make check passes (243 tests), also contrib/file_fdw. The whole
src/test/subscription TAP suite passes with the patch (39 files, 618
tests; 012_collation was skipped because I built without ICU).
- I applied only the 008_diff_schema.pl hunk to unpatched master. The test
fails there: the initial sync of tab_nocols never completes (the table
stays in state 'd'). So the new test does catch the bug.
- I reproduced the problem by hand on unpatched master with a zero-column
publisher table (two rows) and a subscriber table with a local default
column:
- binary = false: the local column gets '' instead of its DEFAULT.
- binary = true: "ERROR: row field count is 0, expected 1", and the sync
never finishes.
- If the extra subscriber column is not text (a stored generated plus
identity column, an integer partition key, or an integer NOT NULL column
with a default), text mode also fails with "invalid input syntax for
type integer: """, and the sync keeps retrying.
With the patch all of these cases finish (state 'r') and the rows load,
with the local defaults where they are defined, in both text and binary
mode. This included a stored generated column plus an identity column, a
partitioned subscriber table, and a publisher table whose only column had
been dropped. A normal one-column publisher table with an extra subscriber
column behaves the same before and after.

The change to BeginCopyFrom() looks fine to me. Keeping it as a wrapper
around BeginCopyFromExtended() leaves the other callers untouched, and
passing true from copy_table() is the only place that needs the new
behavior.

I have not tested the back branches, a publication column list that selects
no columns, or a meson build, so I can not say anything about those.

Thanks again,

Joao Rito