BUG? Unpublished generated columns are reported "missing" by the subscription
BUG? Unpublished generated columns are reported "missing" by the subscription
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
(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] https://www.postgresql.org/message-id/flat/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.gmail.com Kind Regards, Peter Smith. Fujitsu Australia
Re: BUG? Unpublished generated columns are reported "missing" by the subscription
От:
shveta malik <shveta.malik@gmail.com>
Дата:
On Wed, Nov 4, 2026 at 6:31 AM Peter Smith 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
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]. 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], 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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.gmail.com Regards, Ajin Cherian Fujitsu Australia
Re: BUG? Unpublished generated columns are reported "missing" by the subscription
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian 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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.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
Re: BUG? Unpublished generated columns are reported "missing" by the subscription
On Thu, Nov 5, 2026 at 10:47 AM Peter Smith wrote: > > On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian 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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.gmail.com regards, Ajin Cherian Fujitsu Australia
Re: BUG? Unpublished generated columns are reported "missing" by the subscription
От:
Peter Smith <smithpb2250@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 9:38 PM Ajin Cherian wrote: > ... > > Hi Peter, > > Thanks for your comments. Addressed. Also attached the new patch > posted in [1] as patch 0001. > > [1] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.gmail.com > Patch v2-0002 LGTM. ====== Kind Regards, Peter Smith. Fujitsu Australia
Re: BUG? Unpublished generated columns are reported "missing" by the subscription
От:
Shlok Kyal <shlok.kyal.oss@gmail.com>
Дата:
On Thu, 5 Nov 2026 at 16:09, Ajin Cherian wrote: > > On Thu, Nov 5, 2026 at 10:47 AM Peter Smith wrote: > > > > On Wed, Nov 4, 2026 at 9:06 PM Ajin Cherian 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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.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] https://www.postgresql.org/message-id/CAL5_ZS1bnYkK_GrA4N4SVKYgYsMMJxL9416m%3DvECAjrcwmQkkg%40mail.gmail.com > Hi Ajin, I have reviewed and tested the v2-0002 patch. It looks good to me. Thanks, Shlok Kyal