|
3 | 3 | use warnings;
|
4 | 4 | use PostgresNode;
|
5 | 5 | use TestLib;
|
6 |
| -use Test::More tests => 3; |
| 6 | +use Test::More tests => 5; |
7 | 7 |
|
8 | 8 | # Bug #15114
|
9 | 9 |
|
|
98 | 98 | );
|
99 | 99 |
|
100 | 100 | $node_publisher->stop('fast');
|
| 101 | + |
| 102 | +# https://postgr.es/m/OS0PR01MB61133CA11630DAE45BC6AD95FB939%40OS0PR01MB6113.jpnprd01.prod.outlook.com |
| 103 | + |
| 104 | +# The bug was that when changing the REPLICA IDENTITY INDEX to another one, the |
| 105 | +# target table's relcache was not being invalidated. This leads to skipping |
| 106 | +# UPDATE/DELETE operations during apply on the subscriber side as the columns |
| 107 | +# required to search corresponding rows won't get logged. |
| 108 | +$node_publisher = get_new_node('publisher3'); |
| 109 | +$node_publisher->init(allows_streaming => 'logical'); |
| 110 | +$node_publisher->start; |
| 111 | + |
| 112 | +$node_subscriber = get_new_node('subscriber3'); |
| 113 | +$node_subscriber->init(allows_streaming => 'logical'); |
| 114 | +$node_subscriber->start; |
| 115 | + |
| 116 | +$node_publisher->safe_psql('postgres', |
| 117 | + "CREATE TABLE tab_replidentity_index(a int not null, b int not null)"); |
| 118 | +$node_publisher->safe_psql('postgres', |
| 119 | + "CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)" |
| 120 | +); |
| 121 | +$node_publisher->safe_psql('postgres', |
| 122 | + "CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)" |
| 123 | +); |
| 124 | + |
| 125 | +# use index idx_replidentity_index_a as REPLICA IDENTITY on publisher. |
| 126 | +$node_publisher->safe_psql('postgres', |
| 127 | + "ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_a" |
| 128 | +); |
| 129 | + |
| 130 | +$node_publisher->safe_psql('postgres', |
| 131 | + "INSERT INTO tab_replidentity_index VALUES(1, 1),(2, 2)"); |
| 132 | + |
| 133 | +$node_subscriber->safe_psql('postgres', |
| 134 | + "CREATE TABLE tab_replidentity_index(a int not null, b int not null)"); |
| 135 | +$node_subscriber->safe_psql('postgres', |
| 136 | + "CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)" |
| 137 | +); |
| 138 | +$node_subscriber->safe_psql('postgres', |
| 139 | + "CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)" |
| 140 | +); |
| 141 | +# use index idx_replidentity_index_b as REPLICA IDENTITY on subscriber because |
| 142 | +# it reflects the future scenario we are testing: changing REPLICA IDENTITY |
| 143 | +# INDEX. |
| 144 | +$node_subscriber->safe_psql('postgres', |
| 145 | + "ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b" |
| 146 | +); |
| 147 | + |
| 148 | +$publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 149 | +$node_publisher->safe_psql('postgres', |
| 150 | + "CREATE PUBLICATION tap_pub FOR TABLE tab_replidentity_index"); |
| 151 | +$node_subscriber->safe_psql('postgres', |
| 152 | + "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=tap_sub' PUBLICATION tap_pub" |
| 153 | +); |
| 154 | + |
| 155 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 156 | + |
| 157 | +# Also wait for initial table sync to finish |
| 158 | +my $synced_query = |
| 159 | + "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');"; |
| 160 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 161 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 162 | + |
| 163 | +is( $node_subscriber->safe_psql( |
| 164 | + 'postgres', "SELECT * FROM tab_replidentity_index"), |
| 165 | + qq(1|1 |
| 166 | +2|2), |
| 167 | + "check initial data on subscriber"); |
| 168 | + |
| 169 | +# Set REPLICA IDENTITY to idx_replidentity_index_b on publisher, then run UPDATE and DELETE. |
| 170 | +$node_publisher->safe_psql( |
| 171 | + 'postgres', qq[ |
| 172 | + ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b; |
| 173 | + UPDATE tab_replidentity_index SET a = -a WHERE a = 1; |
| 174 | + DELETE FROM tab_replidentity_index WHERE a = 2; |
| 175 | +]); |
| 176 | + |
| 177 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 178 | +is( $node_subscriber->safe_psql( |
| 179 | + 'postgres', "SELECT * FROM tab_replidentity_index"), |
| 180 | + qq(-1|1), |
| 181 | + "update works with REPLICA IDENTITY"); |
| 182 | + |
| 183 | +$node_publisher->stop('fast'); |
| 184 | +$node_subscriber->stop('fast'); |
0 commit comments