Skip to content

Commit b7ae039

Browse files
author
Amit Kapila
committed
Ignore dropped and generated columns from the column list.
We don't allow different column lists for the same table in the different publications of the single subscription. A publication with a column list except for dropped and generated columns should be considered the same as a publication with no column list (which implicitly includes all columns as part of the columns list). However, as we were not excluding the dropped and generated columns from the column list combining such publications leads to an error "cannot use different column lists for table ...". We decided not to backpatch this fix as there is a risk of users seeing this as a behavior change and also we didn't see any field report of this case. Author: Shi yu Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/OSZPR01MB631091CCBC56F195B1B9ACB0FDFE9@OSZPR01MB6310.jpnprd01.prod.outlook.com
1 parent dca8b01 commit b7ae039

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

src/backend/catalog/pg_publication.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,36 @@ pg_get_publication_tables(PG_FUNCTION_ARGS)
11531153
nulls[2] = true;
11541154
}
11551155

1156+
/* Show all columns when the column list is not specified. */
1157+
if (nulls[1] == true)
1158+
{
1159+
Relation rel = table_open(relid, AccessShareLock);
1160+
int nattnums = 0;
1161+
int16 *attnums;
1162+
TupleDesc desc = RelationGetDescr(rel);
1163+
int i;
1164+
1165+
attnums = (int16 *) palloc(desc->natts * sizeof(int16));
1166+
1167+
for (i = 0; i < desc->natts; i++)
1168+
{
1169+
Form_pg_attribute att = TupleDescAttr(desc, i);
1170+
1171+
if (att->attisdropped || att->attgenerated)
1172+
continue;
1173+
1174+
attnums[nattnums++] = att->attnum;
1175+
}
1176+
1177+
if (nattnums > 0)
1178+
{
1179+
values[1] = PointerGetDatum(buildint2vector(attnums, nattnums));
1180+
nulls[1] = false;
1181+
}
1182+
1183+
table_close(rel, AccessShareLock);
1184+
}
1185+
11561186
rettuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
11571187

11581188
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(rettuple));

src/backend/catalog/system_views.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,8 @@ CREATE VIEW pg_publication_tables AS
371371
C.relname AS tablename,
372372
( SELECT array_agg(a.attname ORDER BY a.attnum)
373373
FROM pg_attribute a
374-
WHERE a.attrelid = GPT.relid AND a.attnum > 0 AND
375-
NOT a.attisdropped AND
376-
(a.attnum = ANY(GPT.attrs) OR GPT.attrs IS NULL)
374+
WHERE a.attrelid = GPT.relid AND
375+
a.attnum = ANY(GPT.attrs)
377376
) AS attnames,
378377
pg_get_expr(GPT.qual, GPT.relid) AS rowfilter
379378
FROM pg_publication P,

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,16 +1058,31 @@ pgoutput_column_list_init(PGOutputData *data, List *publications,
10581058
/* Build the column list bitmap in the per-entry context. */
10591059
if (!pub_no_list) /* when not null */
10601060
{
1061+
int i;
1062+
int nliveatts = 0;
1063+
TupleDesc desc = RelationGetDescr(relation);
1064+
10611065
pgoutput_ensure_entry_cxt(data, entry);
10621066

10631067
cols = pub_collist_to_bitmapset(cols, cfdatum,
10641068
entry->entry_cxt);
10651069

1070+
/* Get the number of live attributes. */
1071+
for (i = 0; i < desc->natts; i++)
1072+
{
1073+
Form_pg_attribute att = TupleDescAttr(desc, i);
1074+
1075+
if (att->attisdropped || att->attgenerated)
1076+
continue;
1077+
1078+
nliveatts++;
1079+
}
1080+
10661081
/*
10671082
* If column list includes all the columns of the table,
10681083
* set it to NULL.
10691084
*/
1070-
if (bms_num_members(cols) == RelationGetNumberOfAttributes(relation))
1085+
if (bms_num_members(cols) == nliveatts)
10711086
{
10721087
bms_free(cols);
10731088
cols = NULL;

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202301092
60+
#define CATALOG_VERSION_NO 202301131
6161

6262
#endif

src/test/regress/expected/rules.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ pg_publication_tables| SELECT p.pubname,
14461446
c.relname AS tablename,
14471447
( SELECT array_agg(a.attname ORDER BY a.attnum) AS array_agg
14481448
FROM pg_attribute a
1449-
WHERE ((a.attrelid = gpt.relid) AND (a.attnum > 0) AND (NOT a.attisdropped) AND ((a.attnum = ANY ((gpt.attrs)::smallint[])) OR (gpt.attrs IS NULL)))) AS attnames,
1449+
WHERE ((a.attrelid = gpt.relid) AND (a.attnum = ANY ((gpt.attrs)::smallint[])))) AS attnames,
14501450
pg_get_expr(gpt.qual, gpt.relid) AS rowfilter
14511451
FROM pg_publication p,
14521452
LATERAL pg_get_publication_tables((p.pubname)::text) gpt(relid, attrs, qual),

src/test/subscription/t/031_column_list.pl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,53 @@
11841184
is( $result, qq(t
11851185
t), 'check the number of columns in the old tuple');
11861186

1187+
# TEST: Generated and dropped columns are not considered for the column list.
1188+
# So, the publication having a column list except for those columns and a
1189+
# publication without any column (aka all columns as part of the columns
1190+
# list) are considered to have the same column list.
1191+
$node_publisher->safe_psql(
1192+
'postgres', qq(
1193+
CREATE TABLE test_mix_4 (a int PRIMARY KEY, b int, c int, d int GENERATED ALWAYS AS (a + 1) STORED);
1194+
ALTER TABLE test_mix_4 DROP COLUMN c;
1195+
1196+
CREATE PUBLICATION pub_mix_7 FOR TABLE test_mix_4 (a, b);
1197+
CREATE PUBLICATION pub_mix_8 FOR TABLE test_mix_4;
1198+
1199+
-- initial data
1200+
INSERT INTO test_mix_4 VALUES (1, 2);
1201+
));
1202+
1203+
$node_subscriber->safe_psql(
1204+
'postgres', qq(
1205+
DROP SUBSCRIPTION sub1;
1206+
CREATE TABLE test_mix_4 (a int PRIMARY KEY, b int, c int, d int);
1207+
));
1208+
1209+
$node_subscriber->safe_psql(
1210+
'postgres', qq(
1211+
CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub_mix_7, pub_mix_8;
1212+
));
1213+
1214+
$node_subscriber->wait_for_subscription_sync;
1215+
1216+
is( $node_subscriber->safe_psql(
1217+
'postgres', "SELECT * FROM test_mix_4 ORDER BY a"),
1218+
qq(1|2||),
1219+
'initial synchronization with multiple publications with the same column list'
1220+
);
1221+
1222+
$node_publisher->safe_psql(
1223+
'postgres', qq(
1224+
INSERT INTO test_mix_4 VALUES (3, 4);
1225+
));
1226+
1227+
$node_publisher->wait_for_catchup('sub1');
1228+
1229+
is( $node_subscriber->safe_psql(
1230+
'postgres', "SELECT * FROM test_mix_4 ORDER BY a"),
1231+
qq(1|2||
1232+
3|4||),
1233+
'replication with multiple publications with the same column list');
11871234

11881235
# TEST: With a table included in multiple publications with different column
11891236
# lists, we should catch the error when creating the subscription.

0 commit comments

Comments
 (0)