|
| 1 | + |
| 2 | +# Copyright (c) 2021-2024, PostgreSQL Global Development Group |
| 3 | + |
| 4 | +# Basic logical replication test |
| 5 | +use strict; |
| 6 | +use warnings FATAL => 'all'; |
| 7 | +use PostgreSQL::Test::Cluster; |
| 8 | +use PostgreSQL::Test::Utils; |
| 9 | +use Test::More; |
| 10 | + |
| 11 | +# Initialize publisher node |
| 12 | +my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 13 | +$node_publisher->init(allows_streaming => 'logical'); |
| 14 | +$node_publisher->start; |
| 15 | + |
| 16 | +# Create subscriber node |
| 17 | +my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 18 | +$node_subscriber->init; |
| 19 | +$node_subscriber->start; |
| 20 | + |
| 21 | +# Create some preexisting content on publisher |
| 22 | +$node_publisher->safe_psql('postgres', |
| 23 | + "CREATE TABLE tab_ins AS SELECT a, a + 1 as b FROM generate_series(1,1002) AS a"); |
| 24 | + |
| 25 | +# Replicate the changes without columns |
| 26 | +$node_publisher->safe_psql('postgres', "CREATE TABLE tab_no_col()"); |
| 27 | +$node_publisher->safe_psql('postgres', |
| 28 | + "INSERT INTO tab_no_col default VALUES"); |
| 29 | + |
| 30 | +# Setup structure on subscriber |
| 31 | +$node_subscriber->safe_psql('postgres', "CREATE EXTENSION postgres_fdw"); |
| 32 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_ins (a int, b int)"); |
| 33 | + |
| 34 | +# Setup logical replication |
| 35 | +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 36 | +$node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR TABLE tab_ins"); |
| 37 | + |
| 38 | +my $publisher_host = $node_publisher->host; |
| 39 | +my $publisher_port = $node_publisher->port; |
| 40 | +$node_subscriber->safe_psql('postgres', |
| 41 | + "CREATE SERVER tap_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '$publisher_host', port '$publisher_port', dbname 'postgres')" |
| 42 | +); |
| 43 | + |
| 44 | +$node_subscriber->safe_psql('postgres', |
| 45 | + "CREATE USER MAPPING FOR PUBLIC SERVER tap_server" |
| 46 | +); |
| 47 | + |
| 48 | +$node_subscriber->safe_psql('postgres', |
| 49 | + "CREATE FOREIGN TABLE f_tab_ins (a int, b int) SERVER tap_server OPTIONS(table_name 'tab_ins')" |
| 50 | +); |
| 51 | +$node_subscriber->safe_psql('postgres', |
| 52 | + "CREATE SUBSCRIPTION tap_sub SERVER tap_server PUBLICATION tap_pub WITH (password_required=false)" |
| 53 | +); |
| 54 | + |
| 55 | +# Wait for initial table sync to finish |
| 56 | +$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); |
| 57 | + |
| 58 | +my $result = |
| 59 | + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM (SELECT f.b = l.b as match FROM tab_ins l, f_tab_ins f WHERE l.a = f.a) WHERE match"); |
| 60 | +is($result, qq(1002), 'check initial data was copied to subscriber'); |
| 61 | + |
| 62 | +$node_publisher->safe_psql('postgres', |
| 63 | + "INSERT INTO tab_ins SELECT a, a + 1 FROM generate_series(1003,1050) a"); |
| 64 | + |
| 65 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 66 | + |
| 67 | +$result = |
| 68 | + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM (SELECT f.b = l.b as match FROM tab_ins l, f_tab_ins f WHERE l.a = f.a) WHERE match"); |
| 69 | +is($result, qq(1050), 'check initial data was copied to subscriber'); |
| 70 | + |
| 71 | +done_testing(); |
0 commit comments