Skip to content

Commit 0ae1245

Browse files
author
Amit Kapila
committed
Fix a WARNING for data origin discrepancies.
Previously, a WARNING was issued at the time of defining a subscription with origin=NONE only when the publisher subscribed to the same table from other publishers, indicating potential data origination from different origins. However, the publisher can subscribe to the partition ancestors or partition children of the table from other publishers, which could also result in mixed-origin data inclusion. So, give a WARNING in those cases as well. Reported-by: Sergey Tatarintsev <s.tatarintsev@postgrespro.ru> Author: Hou Zhijie <houzj.fnst@fujitsu.com> Author: Shlok Kyal <shlok.kyal.oss@gmail.com> Reviewed-by: Vignesh C <vignesh21@gmail.com> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Backpatch-through: 16, where it was introduced Discussion: https://postgr.es/m/5eda6a9c-63cf-404d-8a49-8dcb116a29f3@postgrespro.ru
1 parent 2e0f93d commit 0ae1245

File tree

3 files changed

+133
-14
lines changed

3 files changed

+133
-14
lines changed

doc/src/sgml/ref/create_subscription.sgml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,14 @@ CREATE SUBSCRIPTION <replaceable class="parameter">subscription_name</replaceabl
526526
<programlisting>
527527
# substitute &lt;pub-names&gt; below with your publication name(s) to be queried
528528
SELECT DISTINCT PT.schemaname, PT.tablename
529-
FROM pg_publication_tables PT,
529+
FROM pg_publication_tables PT
530+
JOIN pg_class C ON (C.relname = PT.tablename)
531+
JOIN pg_namespace N ON (N.nspname = PT.schemaname),
530532
pg_subscription_rel PS
531-
JOIN pg_class C ON (C.oid = PS.srrelid)
532-
JOIN pg_namespace N ON (N.oid = C.relnamespace)
533-
WHERE N.nspname = PT.schemaname AND
534-
C.relname = PT.tablename AND
533+
WHERE C.relnamespace = N.oid AND
534+
(PS.srrelid = C.oid OR
535+
C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION
536+
SELECT relid FROM pg_partition_tree(PS.srrelid))) AND
535537
PT.pubname IN (&lt;pub-names&gt;);
536538
</programlisting></para>
537539

src/backend/commands/subscriptioncmds.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,11 +2012,12 @@ AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId)
20122012
}
20132013

20142014
/*
2015-
* Check and log a warning if the publisher has subscribed to the same table
2016-
* from some other publisher. This check is required only if "copy_data = true"
2017-
* and "origin = none" for CREATE SUBSCRIPTION and
2018-
* ALTER SUBSCRIPTION ... REFRESH statements to notify the user that data
2019-
* having origin might have been copied.
2015+
* Check and log a warning if the publisher has subscribed to the same table,
2016+
* its partition ancestors (if it's a partition), or its partition children (if
2017+
* it's a partitioned table), from some other publishers. This check is
2018+
* required only if "copy_data = true" and "origin = none" for CREATE
2019+
* SUBSCRIPTION and ALTER SUBSCRIPTION ... REFRESH statements to notify the
2020+
* user that data having origin might have been copied.
20202021
*
20212022
* This check need not be performed on the tables that are already added
20222023
* because incremental sync for those tables will happen through WAL and the
@@ -2046,7 +2047,9 @@ check_publications_origin(WalReceiverConn *wrconn, List *publications,
20462047
"SELECT DISTINCT P.pubname AS pubname\n"
20472048
"FROM pg_publication P,\n"
20482049
" LATERAL pg_get_publication_tables(P.pubname) GPT\n"
2049-
" JOIN pg_subscription_rel PS ON (GPT.relid = PS.srrelid),\n"
2050+
" JOIN pg_subscription_rel PS ON (GPT.relid = PS.srrelid OR"
2051+
" GPT.relid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION"
2052+
" SELECT relid FROM pg_partition_tree(PS.srrelid))),\n"
20502053
" pg_class C JOIN pg_namespace N ON (N.oid = C.relnamespace)\n"
20512054
"WHERE C.oid = GPT.relid AND P.pubname IN (");
20522055
get_publications_str(publications, &cmd, true);

src/test/subscription/t/030_origin.pl

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,123 @@
204204
$node_B->wait_for_catchup($subname_AB2);
205205

206206
# clear the operations done by this test
207-
$node_A->safe_psql('postgres', "DROP TABLE tab_new");
208-
$node_B->safe_psql('postgres', "DROP TABLE tab_new");
209-
$node_A->safe_psql('postgres', "DROP SUBSCRIPTION $subname_AB2");
207+
$node_A->safe_psql(
208+
'postgres', qq(
209+
DROP TABLE tab_new;
210+
DROP SUBSCRIPTION $subname_AB2;
211+
DROP SUBSCRIPTION $subname_AB;
212+
DROP PUBLICATION tap_pub_A;
213+
));
214+
$node_B->safe_psql(
215+
'postgres', qq(
216+
DROP TABLE tab_new;
217+
DROP SUBSCRIPTION $subname_BA;
218+
DROP PUBLICATION tap_pub_B;
219+
));
220+
221+
###############################################################################
222+
# Specifying origin = NONE and copy_data = on must raise WARNING if we subscribe
223+
# to a partitioned table and this table contains any remotely originated data.
224+
#
225+
# node_B
226+
# __________________________
227+
# | tab_main | --------------> node_C (tab_main)
228+
# |__________________________|
229+
# | tab_part1 | tab_part2 | <-------------- node_A (tab_part2)
230+
# |____________|_____________|
231+
# | tab_part2_1 |
232+
# |_____________|
233+
#
234+
# node_B
235+
# __________________________
236+
# | tab_main |
237+
# |__________________________|
238+
# | tab_part1 | tab_part2 | <-------------- node_A (tab_part2)
239+
# |____________|_____________|
240+
# | tab_part2_1 | --------------> node_C (tab_part2_1)
241+
# |_____________|
242+
###############################################################################
243+
244+
# create a table on node A which will act as a source for a partition on node B
245+
$node_A->safe_psql(
246+
'postgres', qq(
247+
CREATE TABLE tab_part2(a int);
248+
CREATE PUBLICATION tap_pub_A FOR TABLE tab_part2;
249+
));
250+
251+
# create a partition table on node B
252+
$node_B->safe_psql(
253+
'postgres', qq(
254+
CREATE TABLE tab_main(a int) PARTITION BY RANGE(a);
255+
CREATE TABLE tab_part1 PARTITION OF tab_main FOR VALUES FROM (0) TO (5);
256+
CREATE TABLE tab_part2(a int) PARTITION BY RANGE(a);
257+
CREATE TABLE tab_part2_1 PARTITION OF tab_part2 FOR VALUES FROM (5) TO (10);
258+
ALTER TABLE tab_main ATTACH PARTITION tab_part2 FOR VALUES FROM (5) to (10);
259+
CREATE SUBSCRIPTION tap_sub_A_B CONNECTION '$node_A_connstr' PUBLICATION tap_pub_A;
260+
));
261+
262+
# create a table on node C
263+
$node_C->safe_psql(
264+
'postgres', qq(
265+
CREATE TABLE tab_main(a int);
266+
CREATE TABLE tab_part2_1(a int);
267+
));
268+
269+
# create a logical replication setup between node B and node C with
270+
# subscription on node C having origin = NONE and copy_data = on
271+
$node_B->safe_psql(
272+
'postgres', qq(
273+
CREATE PUBLICATION tap_pub_B FOR TABLE tab_main WITH (publish_via_partition_root);
274+
CREATE PUBLICATION tap_pub_B_2 FOR TABLE tab_part2_1;
275+
));
276+
277+
($result, $stdout, $stderr) = $node_C->psql(
278+
'postgres', "
279+
CREATE SUBSCRIPTION tap_sub_B_C CONNECTION '$node_B_connstr' PUBLICATION tap_pub_B WITH (origin = none, copy_data = on);
280+
");
281+
282+
# A warning must be logged as a partition 'tab_part2' in node B is subscribed to
283+
# node A so partition 'tab_part2' can have remotely originated data
284+
like(
285+
$stderr,
286+
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/,
287+
"Create subscription with origin = none and copy_data when the publisher's partition is subscribing from different origin"
288+
);
289+
$node_C->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_B_C");
290+
291+
($result, $stdout, $stderr) = $node_C->psql(
292+
'postgres', "
293+
CREATE SUBSCRIPTION tap_sub_B_C CONNECTION '$node_B_connstr' PUBLICATION tap_pub_B_2 WITH (origin = none, copy_data = on);
294+
");
295+
296+
# A warning must be logged as ancestor of table 'tab_part2_1' in node B is
297+
# subscribed to node A so table 'tab_part2_1' can have remotely originated
298+
# data
299+
like(
300+
$stderr,
301+
qr/WARNING: ( [A-Z0-9]+:)? subscription "tap_sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/,
302+
"Create subscription with origin = none and copy_data when the publisher's ancestor is subscribing from different origin"
303+
);
304+
305+
# clear the operations done by this test
306+
$node_C->safe_psql(
307+
'postgres', qq(
308+
DROP SUBSCRIPTION tap_sub_B_C;
309+
DROP TABLE tab_main;
310+
DROP TABLE tab_part2_1;
311+
));
312+
$node_B->safe_psql(
313+
'postgres', qq(
314+
DROP SUBSCRIPTION tap_sub_A_B;
315+
DROP PUBLICATION tap_pub_B;
316+
DROP PUBLICATION tap_pub_B_2;
317+
DROP TABLE tab_main;
318+
));
319+
$node_A->safe_psql(
320+
'postgres', qq(
321+
DROP PUBLICATION tap_pub_A;
322+
DROP TABLE tab_part2;
323+
));
210324

211325
# shutdown
212326
$node_B->stop('fast');

0 commit comments

Comments
 (0)