034_alter_pub.txt

text/plain

# Copyright (c) 2021-2024, PostgreSQL Global Development Group

use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use Test::More;

my ($node_publisher, $node_subscriber, $publisher_connstr, $result, $offset);

# Initialize publisher
$node_publisher = PostgreSQL::Test::Cluster->new('publisher');
$node_publisher->init(allows_streaming => 'logical');
$node_publisher->start;
$node_publisher->safe_psql(
	'postgres', qq(
create table tab (a int) partition by range (a);
create table tab_1 partition of tab for values from (-10) to (0);
create table tab_2 partition of tab for values from (0) to (10);
create publication pub for table tab;
));

# Insert initial data
$node_publisher->safe_psql('postgres', "insert into tab values (-10)");

# Initialize subscriber
$node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
$node_subscriber->init;
$node_subscriber->start;
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
$node_subscriber->safe_psql(
	'postgres', qq(
create table tab (a int) partition by range (a);
create table tab_1 partition of tab for values from (-10) to (0);
create table tab_2 partition of tab for values from (0) to (10);
create subscription sub connection '$publisher_connstr' publication pub;
));

# Confirm the initial sync was done
$node_subscriber->wait_for_subscription_sync;
$result = $node_subscriber->safe_psql('postgres', "select * from tab;");
is($result, "-10", "initial sync was done");

# Confirm that child tables are target of the replication
$result = $node_subscriber->safe_psql(
	'postgres', "select relname from pg_class, pg_subscription_rel where (pg_class.oid = srrelid);"
);
is($result, "tab_1
tab_2", "child tables are listed in pg_subscription_rel");

# Altering the publication to make publish as parent
$node_publisher->safe_psql('postgres', "alter publication pub set (publish_via_partition_root = true)");

# Insert intermediate data
$node_publisher->safe_psql('postgres', "insert into tab values (9)");
$node_publisher->wait_for_catchup('sub');

# Confirm the data is not replicated because the parent table is not listed in pg_subscription_rel
$result = $node_subscriber->safe_psql('postgres', "select * from tab;");
is($result, "-10", "inserted tuple after the altering is not replicated");

# Update pg_subscription_rel
$node_subscriber->safe_psql('postgres', "alter subscription sub refresh publication with (copy_data = off)");
$node_publisher->wait_for_catchup('sub');
$result = $node_subscriber->safe_psql(
	'postgres', "select relname from pg_class, pg_subscription_rel where (pg_class.oid = srrelid);"
);
is($result, "tab", "parent table is listed in pg_subscription_rel");

# ...But the tuple is lost
$result = $node_subscriber->safe_psql('postgres', "select * from tab;");
is($result, "-10", "inserted tuple after the altering is lost");

done_testing();