BUG? Unpublished generated columns are reported "missing" by the subscription

Started by Peter Smith21 days ago7 messageshackers
Jump to latest
#1Peter Smith
smithpb2250@gmail.com

(This was discovered while investigating a similar topic [1]/messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com).

AFAIK generated columns should not normally be logically replicated:
- VIRTUAL gencols, never
- STORED gencols, according to the 'publish_generated_columns' parameter

So, why does the subscription tablesync complain below that the column
name "a" does not exist, given that it will not be replicated anyway?

For example, at the publisher-side

test_pub=# CREATE TABLE T_GENCOLS(a int generated always as (99) VIRTUAL);
CREATE TABLE
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# select * from t_gencols;
a
----
99
99
99
(3 rows)
test_pub=# CREATE PUBLICATION pub1 FOR TABLE t_gencols;
CREATE PUBLICATION

Then, on the subscriber-side

test_sub=# create table t_gencols(local_col text default
'subscriber-gencol-default');
CREATE TABLE
test_sub=# create subscription sub1 connection 'dbname=test_pub'
publication pub1;
NOTICE: created replication slot "sub1" on publisher
CREATE SUBSCRIPTION
test_sub=#
2026-11-04 09:57:53.983 AEDT [345164] LOG: logical replication apply
worker for subscription "sub1" has started
2026-11-04 09:57:53.995 AEDT [345166] LOG: logical replication table
synchronization worker for subscription "sub1", table "t_gencols" has
started
2026-11-04 09:57:54.059 AEDT [345166] ERROR: logical replication
target relation "public.t_gencols" is missing replicated column: "a"

~~~

OTOH, when the subscription is created with parameter copy_data=false
then it works as expected for normal replication, therefore I suspect
this may be a tablesync-only problem, probably in function
fetch_remote_table_info() although I have not yet debugged/proved
that.

Thoughts?

======
[1]: /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Kind Regards,
Peter Smith.
Fujitsu Australia

#2shveta malik
shveta.malik@gmail.com
In reply to: Peter Smith (#1)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

On Wed, Nov 4, 2026 at 6:31 AM Peter Smith <smithpb2250@gmail.com> wrote:

(This was discovered while investigating a similar topic [1]).

AFAIK generated columns should not normally be logically replicated:
- VIRTUAL gencols, never
- STORED gencols, according to the 'publish_generated_columns' parameter

So, why does the subscription tablesync complain below that the column
name "a" does not exist, given that it will not be replicated anyway?

For example, at the publisher-side

test_pub=# CREATE TABLE T_GENCOLS(a int generated always as (99) VIRTUAL);
CREATE TABLE
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# insert into t_gencols default values;
INSERT 0 1
test_pub=# select * from t_gencols;
a
----
99
99
99
(3 rows)
test_pub=# CREATE PUBLICATION pub1 FOR TABLE t_gencols;
CREATE PUBLICATION

Then, on the subscriber-side

test_sub=# create table t_gencols(local_col text default
'subscriber-gencol-default');
CREATE TABLE
test_sub=# create subscription sub1 connection 'dbname=test_pub'
publication pub1;
NOTICE: created replication slot "sub1" on publisher
CREATE SUBSCRIPTION
test_sub=#
2026-11-04 09:57:53.983 AEDT [345164] LOG: logical replication apply
worker for subscription "sub1" has started
2026-11-04 09:57:53.995 AEDT [345166] LOG: logical replication table
synchronization worker for subscription "sub1", table "t_gencols" has
started
2026-11-04 09:57:54.059 AEDT [345166] ERROR: logical replication
target relation "public.t_gencols" is missing replicated column: "a"

~~~

OTOH, when the subscription is created with parameter copy_data=false
then it works as expected for normal replication, therefore I suspect
this may be a tablesync-only problem, probably in function
fetch_remote_table_info() although I have not yet debugged/proved
that.

Thoughts?

+1, a genuine problem in tablesync, should be addressed. It appears
to be a corner case where the table has no publishable columns at all.
I verified that tablesync works correctly when the table also has
at-least one ordinary column (not generated one).

thanks
Shveta

#3Ajin Cherian
itsajin@gmail.com
In reply to: Peter Smith (#1)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

Hi,

Attached is an AI assisted patch to fix the issue Peter reported.

Since v18, tablesync no longer excludes generated columns in its
pg_attribute query, and instead relies on the column list from
pg_get_publication_tables() to skip unpublished columns. When none of
a table's columns are published, that function returns attrs = NULL,
which the subscriber reads as "no column list, so replicate all
columns". So the unpublished generated columns are replicated anyway.

0002 fixes this by also fetching "gpt.attrs IS NULL" from the
publisher and, for v18+ publishers, treating it as "no columns
published".

That leaves the table with zero replicated columns, so the fix depends
on the fix for the zero-column table bug being discussed in [1]/messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com.
Without it, the subscriber's COPY treats the empty column list as "all
local columns": the initial sync then loads '' instead of the local
default in text mode, or fails with "row field count is 0" in binary
mode.

To make the tests pass, I've included the v1 patch from [1]/messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com, unchanged, as 0001.

0002 adds tests to 011_generated.pl for a table whose only column is
VIRTUAL, and one whose only column is STORED with
publish_generated_columns = none, in both text and binary mode.

[1]: /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Regards,
Ajin Cherian
Fujitsu Australia

Attachments:

v1-0001-Fix-initial-sync-of-zero-column-published-tables.patchapplication/octet-stream; name=v1-0001-Fix-initial-sync-of-zero-column-published-tables.patchDownload+104-3
v1-0002-Fix-tablesync-of-tables-with-no-published-columns.patchapplication/octet-stream; name=v1-0002-Fix-tablesync-of-tables-with-no-published-columns.patchDownload+100-5
#4Peter Smith
smithpb2250@gmail.com
In reply to: Ajin Cherian (#3)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian <itsajin@gmail.com> wrote:
...

To make the tests pass, I've included the v1 patch from [1], unchanged, as 0001.

0002 adds tests to 011_generated.pl for a table whose only column is
VIRTUAL, and one whose only column is STORED with
publish_generated_columns = none, in both text and binary mode.

[1] /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Hi Ajin.

Thanks for the 0002 patch!

Basically, it LGTM, but here are a couple of very minor review comments.

======
src/backend/replication/logical/tablesync.c

1.
"SELECT DISTINCT"
" (CASE WHEN (array_length(gpt.attrs, 1) = c.relnatts)"
- " THEN NULL ELSE gpt.attrs END)"
+ " THEN NULL ELSE gpt.attrs END),"
+ " gpt.attrs IS NULL"
" FROM pg_get_publication_tables(ARRAY[%s], %u) gpt,"
" pg_class c"
" WHERE c.oid = gpt.relid",

I wondered if it might be helpful for the new result column to have an
alias like:

gpt.attrs IS NULL AS no_cols_published

It's not used anywhere, so it doesn't actually do anything, but OTOH I
think it makes the SQL a bit more self-documenting. What do you think?

======
src/test/subscription/t/011_generated.pl

2.
pub:
+ CREATE TABLE tab_virt_only_text (a int GENERATED ALWAYS AS (99) VIRTUAL);
+ CREATE TABLE tab_virt_only_bin (a int GENERATED ALWAYS AS (99) VIRTUAL);
+ CREATE TABLE tab_stored_only_text (b int GENERATED ALWAYS AS (77) STORED);
+ CREATE TABLE tab_stored_only_bin (b int GENERATED ALWAYS AS (77) STORED);
~
sub:
+ CREATE TABLE tab_virt_only_text (c text DEFAULT 'sub-default');
+ CREATE TABLE tab_virt_only_bin (c text DEFAULT 'sub-default');
+ CREATE TABLE tab_stored_only_text (b int, c text DEFAULT 'sub-default');
+ CREATE TABLE tab_stored_only_bin (b int, c text DEFAULT 'sub-default');

These are all different tables, so it's not necessary to have 3
different column names for them.

Wouldn't it be more intuitive to just call them "a" and "b" (or "a"
and "local_col")?

e.g.

pub:
CREATE TABLE tab_virt_only_text (a int GENERATED ALWAYS AS (99) VIRTUAL);
CREATE TABLE tab_virt_only_bin (a int GENERATED ALWAYS AS (99) VIRTUAL);
CREATE TABLE tab_stored_only_text (a int GENERATED ALWAYS AS (77) STORED);
CREATE TABLE tab_stored_only_bin (a int GENERATED ALWAYS AS (77) STORED);
~
sub:
CREATE TABLE tab_virt_only_text (local_col text DEFAULT 'sub-default');
CREATE TABLE tab_virt_only_bin (local_col text DEFAULT 'sub-default');
CREATE TABLE tab_stored_only_text (a int, local_col text DEFAULT 'sub-default');
CREATE TABLE tab_stored_only_bin (a int, local_col text DEFAULT 'sub-default');

Of course, this comment also needs to change accordingly:
+# The subscriber tables for tab_virt_only_* have no column named "a", and the
+# ones for tab_stored_only_* have a regular column "b".

~~~

3.
+$node_subscriber->safe_psql('postgres',
+ "CREATE SUBSCRIPTION regress_sub_gen_only_text CONNECTION
'$publisher_connstr' PUBLICATION regress_pub_gen_only_text WITH
(binary = false)"
+);
+$node_subscriber->safe_psql('postgres',
+ "CREATE SUBSCRIPTION regress_sub_gen_only_bin CONNECTION
'$publisher_connstr' PUBLICATION regress_pub_gen_only_bin WITH (binary
= true)"
+);

Those can be combined into a single safe_sql, right?

~~~

4.
+$node_subscriber->safe_psql('postgres',
+ "DROP SUBSCRIPTION regress_sub_gen_only_text");
+$node_subscriber->safe_psql('postgres',
+ "DROP SUBSCRIPTION regress_sub_gen_only_bin");

Those could be combined into a single safe_sql, right?

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

#5Ajin Cherian
itsajin@gmail.com
In reply to: Peter Smith (#4)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

On Thu, Nov 5, 2026 at 10:47 AM Peter Smith <smithpb2250@gmail.com> wrote:

On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian <itsajin@gmail.com> wrote:
...

To make the tests pass, I've included the v1 patch from [1], unchanged, as 0001.

0002 adds tests to 011_generated.pl for a table whose only column is
VIRTUAL, and one whose only column is STORED with
publish_generated_columns = none, in both text and binary mode.

[1] /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Hi Ajin.

Thanks for the 0002 patch!

Basically, it LGTM, but here are a couple of very minor review comments.

Hi Peter,

Thanks for your comments. Addressed. Also attached the new patch
posted in [1]/messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com as patch 0001.

[1]: /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

regards,
Ajin Cherian
Fujitsu Australia

Attachments:

t254543_5
v2-0001-Fix-initial-sync-of-zero-column-published-tables.patchapplication/octet-stream; name=v2-0001-Fix-initial-sync-of-zero-column-published-tables.patchDownload+93-12
v2-0002-Fix-tablesync-of-tables-with-no-published-columns.patchapplication/octet-stream; name=v2-0002-Fix-tablesync-of-tables-with-no-published-columns.patchDownload+97-5
#6Peter Smith
smithpb2250@gmail.com
In reply to: Ajin Cherian (#5)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

On Thu, Nov 5, 2026 at 9:38 PM Ajin Cherian <itsajin@gmail.com> wrote:

...

Hi Peter,

Thanks for your comments. Addressed. Also attached the new patch
posted in [1] as patch 0001.

[1] /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Patch v2-0002 LGTM.

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

#7Shlok Kyal
shlok.kyal.oss@gmail.com
In reply to: Ajin Cherian (#5)
Re: BUG? Unpublished generated columns are reported "missing" by the subscription

On Thu, 5 Nov 2026 at 16:09, Ajin Cherian <itsajin@gmail.com> wrote:

On Thu, Nov 5, 2026 at 10:47 AM Peter Smith <smithpb2250@gmail.com> wrote:

On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian <itsajin@gmail.com> wrote:
...

To make the tests pass, I've included the v1 patch from [1], unchanged, as 0001.

0002 adds tests to 011_generated.pl for a table whose only column is
VIRTUAL, and one whose only column is STORED with
publish_generated_columns = none, in both text and binary mode.

[1] /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Hi Ajin.

Thanks for the 0002 patch!

Basically, it LGTM, but here are a couple of very minor review comments.

Hi Peter,

Thanks for your comments. Addressed. Also attached the new patch
posted in [1] as patch 0001.

[1] /messages/by-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m=vECAjrcwmQkkg@mail.gmail.com

Hi Ajin,

I have reviewed and tested the v2-0002 patch.
It looks good to me.

Thanks,
Shlok Kyal