|
| 1 | +# Test logical replication behavior with heap rewrites |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 2; |
| 7 | + |
| 8 | +sub wait_for_caught_up |
| 9 | +{ |
| 10 | + my ($node, $appname) = @_; |
| 11 | + |
| 12 | + $node->poll_query_until('postgres', |
| 13 | +"SELECT pg_current_wal_lsn() <= replay_lsn FROM pg_stat_replication WHERE application_name = '$appname';" |
| 14 | + ) or die "Timed out while waiting for subscriber to catch up"; |
| 15 | +} |
| 16 | + |
| 17 | +my $node_publisher = get_new_node('publisher'); |
| 18 | +$node_publisher->init(allows_streaming => 'logical'); |
| 19 | +$node_publisher->start; |
| 20 | + |
| 21 | +my $node_subscriber = get_new_node('subscriber'); |
| 22 | +$node_subscriber->init(allows_streaming => 'logical'); |
| 23 | +$node_subscriber->start; |
| 24 | + |
| 25 | +my $ddl = "CREATE TABLE test1 (a int, b text);"; |
| 26 | +$node_publisher->safe_psql('postgres', $ddl); |
| 27 | +$node_subscriber->safe_psql('postgres', $ddl); |
| 28 | + |
| 29 | +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 30 | +my $appname = 'encoding_test'; |
| 31 | + |
| 32 | +$node_publisher->safe_psql('postgres', |
| 33 | + "CREATE PUBLICATION mypub FOR ALL TABLES;"); |
| 34 | +$node_subscriber->safe_psql('postgres', |
| 35 | +"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;" |
| 36 | +); |
| 37 | + |
| 38 | +wait_for_caught_up($node_publisher, $appname); |
| 39 | + |
| 40 | +# Wait for initial sync to finish as well |
| 41 | +my $synced_query = |
| 42 | + "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');"; |
| 43 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 44 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 45 | + |
| 46 | +$node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');}); |
| 47 | + |
| 48 | +wait_for_caught_up($node_publisher, $appname); |
| 49 | + |
| 50 | +is($node_subscriber->safe_psql('postgres', q{SELECT a, b FROM test1}), |
| 51 | + qq(1|one |
| 52 | +2|two), |
| 53 | + 'initial data replicated to subscriber'); |
| 54 | + |
| 55 | +# DDL that causes a heap rewrite |
| 56 | +my $ddl2 = "ALTER TABLE test1 ADD c int NOT NULL DEFAULT 0;"; |
| 57 | +$node_subscriber->safe_psql('postgres', $ddl2); |
| 58 | +$node_publisher->safe_psql('postgres', $ddl2); |
| 59 | + |
| 60 | +wait_for_caught_up($node_publisher, $appname); |
| 61 | + |
| 62 | +$node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b, c) VALUES (3, 'three', 33);}); |
| 63 | + |
| 64 | +wait_for_caught_up($node_publisher, $appname); |
| 65 | + |
| 66 | +is($node_subscriber->safe_psql('postgres', q{SELECT a, b, c FROM test1}), |
| 67 | + qq(1|one|0 |
| 68 | +2|two|0 |
| 69 | +3|three|33), |
| 70 | + 'data replicated to subscriber'); |
| 71 | + |
| 72 | +$node_subscriber->stop; |
| 73 | +$node_publisher->stop; |
0 commit comments