| 1 |
# Copyright (c) 2022-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Test logical replication behavior with subscriber using available index |
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
|
| 10 |
# create publisher node |
| 11 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 12 |
$node_publisher->init(allows_streaming => 'logical'); |
| 13 |
$node_publisher->start; |
| 14 |
|
| 15 |
# create subscriber node |
| 16 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 17 |
$node_subscriber->init; |
| 18 |
$node_subscriber->start; |
| 19 |
|
| 20 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 21 |
my $appname = 'tap_sub'; |
| 22 |
my $result = ''; |
| 23 |
|
| 24 |
# ============================================================================= |
| 25 |
# Testcase start: Subscription can use index with multiple rows and columns |
| 26 |
# |
| 27 |
|
| 28 |
# create tables pub and sub |
| 29 |
$node_publisher->safe_psql('postgres', |
| 30 |
"CREATE TABLE test_replica_id_full (x int, y text)"); |
| 31 |
$node_publisher->safe_psql('postgres', |
| 32 |
"ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL"); |
| 33 |
$node_subscriber->safe_psql('postgres', |
| 34 |
"CREATE TABLE test_replica_id_full (x int, y text)"); |
| 35 |
$node_subscriber->safe_psql('postgres', |
| 36 |
"CREATE INDEX test_replica_id_full_idx ON test_replica_id_full(x,y)"); |
| 37 |
|
| 38 |
# insert some initial data within the range 0-9 for x and y |
| 39 |
$node_publisher->safe_psql('postgres', |
| 40 |
"INSERT INTO test_replica_id_full SELECT (i%10), (i%10)::text FROM generate_series(0,10) i" |
| 41 |
); |
| 42 |
|
| 43 |
# create pub/sub |
| 44 |
$node_publisher->safe_psql('postgres', |
| 45 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full"); |
| 46 |
$node_subscriber->safe_psql('postgres', |
| 47 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 48 |
); |
| 49 |
|
| 50 |
# wait for initial table synchronization to finish |
| 51 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 52 |
|
| 53 |
# delete 2 rows |
| 54 |
$node_publisher->safe_psql('postgres', |
| 55 |
"DELETE FROM test_replica_id_full WHERE x IN (5, 6)"); |
| 56 |
|
| 57 |
# update 2 rows |
| 58 |
$node_publisher->safe_psql('postgres', |
| 59 |
"UPDATE test_replica_id_full SET x = 100, y = '200' WHERE x IN (1, 2)"); |
| 60 |
|
| 61 |
# wait until the index is used on the subscriber |
| 62 |
$node_publisher->wait_for_catchup($appname); |
| 63 |
$node_subscriber->poll_query_until('postgres', |
| 64 |
q{select (idx_scan = 4) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';} |
| 65 |
) |
| 66 |
or die |
| 67 |
"Timed out while waiting for check subscriber tap_sub_rep_full updates 4 rows via index"; |
| 68 |
|
| 69 |
# make sure that the subscriber has the correct data after the UPDATE |
| 70 |
$result = $node_subscriber->safe_psql('postgres', |
| 71 |
"select count(*) from test_replica_id_full WHERE (x = 100 and y = '200')" |
| 72 |
); |
| 73 |
is($result, qq(2), |
| 74 |
'ensure subscriber has the correct data at the end of the test'); |
| 75 |
|
| 76 |
# make sure that the subscriber has the correct data after the first DELETE |
| 77 |
$result = $node_subscriber->safe_psql('postgres', |
| 78 |
"select count(*) from test_replica_id_full where x in (5, 6)"); |
| 79 |
is($result, qq(0), |
| 80 |
'ensure subscriber has the correct data at the end of the test'); |
| 81 |
|
| 82 |
# cleanup pub |
| 83 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 84 |
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 85 |
# cleanup sub |
| 86 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 87 |
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 88 |
|
| 89 |
# Testcase end: Subscription can use index with multiple rows and columns |
| 90 |
# ============================================================================= |
| 91 |
|
| 92 |
# ============================================================================= |
| 93 |
# Testcase start: Subscription can use index on partitioned tables |
| 94 |
|
| 95 |
# create tables pub and sub |
| 96 |
$node_publisher->safe_psql('postgres', |
| 97 |
"CREATE TABLE users_table_part(user_id bigint, value_1 int, value_2 int) PARTITION BY RANGE (value_1)" |
| 98 |
); |
| 99 |
$node_publisher->safe_psql('postgres', |
| 100 |
"CREATE TABLE users_table_part_0 PARTITION OF users_table_part FOR VALUES FROM (0) TO (10)" |
| 101 |
); |
| 102 |
$node_publisher->safe_psql('postgres', |
| 103 |
"CREATE TABLE users_table_part_1 PARTITION OF users_table_part FOR VALUES FROM (10) TO (20)" |
| 104 |
); |
| 105 |
|
| 106 |
$node_publisher->safe_psql('postgres', |
| 107 |
"ALTER TABLE users_table_part REPLICA IDENTITY FULL"); |
| 108 |
$node_publisher->safe_psql('postgres', |
| 109 |
"ALTER TABLE users_table_part_0 REPLICA IDENTITY FULL"); |
| 110 |
$node_publisher->safe_psql('postgres', |
| 111 |
"ALTER TABLE users_table_part_1 REPLICA IDENTITY FULL"); |
| 112 |
|
| 113 |
$node_subscriber->safe_psql('postgres', |
| 114 |
"CREATE TABLE users_table_part(user_id bigint, value_1 int, value_2 int) PARTITION BY RANGE (value_1)" |
| 115 |
); |
| 116 |
$node_subscriber->safe_psql('postgres', |
| 117 |
"CREATE TABLE users_table_part_0 PARTITION OF users_table_part FOR VALUES FROM (0) TO (10)" |
| 118 |
); |
| 119 |
$node_subscriber->safe_psql('postgres', |
| 120 |
"CREATE TABLE users_table_part_1 PARTITION OF users_table_part FOR VALUES FROM (10) TO (20)" |
| 121 |
); |
| 122 |
$node_subscriber->safe_psql('postgres', |
| 123 |
"CREATE INDEX users_table_part_idx ON users_table_part(user_id, value_1)" |
| 124 |
); |
| 125 |
|
| 126 |
# insert some initial data |
| 127 |
$node_publisher->safe_psql('postgres', |
| 128 |
"INSERT INTO users_table_part SELECT (i%100), (i%20), i FROM generate_series(0,100) i" |
| 129 |
); |
| 130 |
|
| 131 |
# create pub/sub |
| 132 |
$node_publisher->safe_psql('postgres', |
| 133 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE users_table_part"); |
| 134 |
$node_subscriber->safe_psql('postgres', |
| 135 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 136 |
); |
| 137 |
|
| 138 |
# wait for initial table synchronization to finish |
| 139 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 140 |
|
| 141 |
# update rows, moving them to other partitions |
| 142 |
$node_publisher->safe_psql('postgres', |
| 143 |
"UPDATE users_table_part SET value_1 = 0 WHERE user_id = 4"); |
| 144 |
|
| 145 |
# delete rows from different partitions |
| 146 |
$node_publisher->safe_psql('postgres', |
| 147 |
"DELETE FROM users_table_part WHERE user_id = 1 and value_1 = 1"); |
| 148 |
$node_publisher->safe_psql('postgres', |
| 149 |
"DELETE FROM users_table_part WHERE user_id = 12 and value_1 = 12"); |
| 150 |
|
| 151 |
# wait until the index is used on the subscriber |
| 152 |
$node_publisher->wait_for_catchup($appname); |
| 153 |
$node_subscriber->poll_query_until('postgres', |
| 154 |
q{select sum(idx_scan)=3 from pg_stat_all_indexes where indexrelname ilike 'users_table_part_%';} |
| 155 |
) |
| 156 |
or die |
| 157 |
"Timed out while waiting for check subscriber tap_sub_rep_full updates partitioned table"; |
| 158 |
|
| 159 |
# make sure that the subscriber has the correct data |
| 160 |
$result = $node_subscriber->safe_psql('postgres', |
| 161 |
"select sum(user_id+value_1+value_2) from users_table_part"); |
| 162 |
is($result, qq(10907), |
| 163 |
'ensure subscriber has the correct data at the end of the test'); |
| 164 |
$result = $node_subscriber->safe_psql('postgres', |
| 165 |
"select count(DISTINCT(user_id,value_1, value_2)) from users_table_part"); |
| 166 |
is($result, qq(99), |
| 167 |
'ensure subscriber has the correct data at the end of the test'); |
| 168 |
|
| 169 |
# cleanup pub |
| 170 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 171 |
$node_publisher->safe_psql('postgres', "DROP TABLE users_table_part"); |
| 172 |
|
| 173 |
# cleanup sub |
| 174 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 175 |
$node_subscriber->safe_psql('postgres', "DROP TABLE users_table_part"); |
| 176 |
|
| 177 |
# Testcase end: Subscription can use index on partitioned tables |
| 178 |
# ============================================================================= |
| 179 |
|
| 180 |
# ============================================================================= |
| 181 |
# Testcase start: Subscription will not use indexes with only expressions or |
| 182 |
# partial index |
| 183 |
|
| 184 |
# create tables pub and sub |
| 185 |
$node_publisher->safe_psql('postgres', |
| 186 |
"CREATE TABLE people (firstname text, lastname text)"); |
| 187 |
$node_publisher->safe_psql('postgres', |
| 188 |
"ALTER TABLE people REPLICA IDENTITY FULL"); |
| 189 |
$node_subscriber->safe_psql('postgres', |
| 190 |
"CREATE TABLE people (firstname text, lastname text)"); |
| 191 |
|
| 192 |
# index with only an expression |
| 193 |
$node_subscriber->safe_psql('postgres', |
| 194 |
"CREATE INDEX people_names_expr_only ON people ((firstname || ' ' || lastname))" |
| 195 |
); |
| 196 |
|
| 197 |
# partial index |
| 198 |
$node_subscriber->safe_psql('postgres', |
| 199 |
"CREATE INDEX people_names_partial ON people(firstname) WHERE (firstname = 'first_name_1')" |
| 200 |
); |
| 201 |
|
| 202 |
# insert some initial data |
| 203 |
$node_publisher->safe_psql('postgres', |
| 204 |
"INSERT INTO people SELECT 'first_name_' || i::text, 'last_name_' || i::text FROM generate_series(0,200) i" |
| 205 |
); |
| 206 |
|
| 207 |
# create pub/sub |
| 208 |
$node_publisher->safe_psql('postgres', |
| 209 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE people"); |
| 210 |
$node_subscriber->safe_psql('postgres', |
| 211 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 212 |
); |
| 213 |
|
| 214 |
# wait for initial table synchronization to finish |
| 215 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 216 |
|
| 217 |
# update 2 rows |
| 218 |
$node_publisher->safe_psql('postgres', |
| 219 |
"UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_1'" |
| 220 |
); |
| 221 |
$node_publisher->safe_psql('postgres', |
| 222 |
"UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_2' AND lastname = 'last_name_2'" |
| 223 |
); |
| 224 |
|
| 225 |
# make sure none of the indexes is used on the subscriber |
| 226 |
$node_publisher->wait_for_catchup($appname); |
| 227 |
$result = $node_subscriber->safe_psql('postgres', |
| 228 |
"select sum(idx_scan) from pg_stat_all_indexes where indexrelname IN ('people_names_expr_only', 'people_names_partial')" |
| 229 |
); |
| 230 |
is($result, qq(0), |
| 231 |
'ensure subscriber tap_sub_rep_full updates two rows via seq. scan with index on expressions' |
| 232 |
); |
| 233 |
|
| 234 |
$node_publisher->safe_psql('postgres', |
| 235 |
"DELETE FROM people WHERE firstname = 'first_name_3'"); |
| 236 |
$node_publisher->safe_psql('postgres', |
| 237 |
"DELETE FROM people WHERE firstname = 'first_name_4' AND lastname = 'last_name_4'" |
| 238 |
); |
| 239 |
|
| 240 |
# make sure the index is not used on the subscriber |
| 241 |
$node_publisher->wait_for_catchup($appname); |
| 242 |
$result = $node_subscriber->safe_psql('postgres', |
| 243 |
"select sum(idx_scan) from pg_stat_all_indexes where indexrelname IN ('people_names_expr_only', 'people_names_partial')" |
| 244 |
); |
| 245 |
is($result, qq(0), |
| 246 |
'ensure subscriber tap_sub_rep_full updates two rows via seq. scan with index on expressions' |
| 247 |
); |
| 248 |
|
| 249 |
# make sure that the subscriber has the correct data |
| 250 |
$result = |
| 251 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM people"); |
| 252 |
is($result, qq(199), |
| 253 |
'ensure subscriber has the correct data at the end of the test'); |
| 254 |
|
| 255 |
# cleanup pub |
| 256 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 257 |
$node_publisher->safe_psql('postgres', "DROP TABLE people"); |
| 258 |
# cleanup sub |
| 259 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 260 |
$node_subscriber->safe_psql('postgres', "DROP TABLE people"); |
| 261 |
|
| 262 |
# Testcase end: Subscription will not use indexes with only expressions or |
| 263 |
# partial index |
| 264 |
# ============================================================================= |
| 265 |
|
| 266 |
# ============================================================================= |
| 267 |
# Testcase start: Subscription can use index having expressions and columns |
| 268 |
|
| 269 |
# create tables pub and sub |
| 270 |
$node_publisher->safe_psql('postgres', |
| 271 |
"CREATE TABLE people (firstname text, lastname text)"); |
| 272 |
$node_publisher->safe_psql('postgres', |
| 273 |
"ALTER TABLE people REPLICA IDENTITY FULL"); |
| 274 |
$node_subscriber->safe_psql('postgres', |
| 275 |
"CREATE TABLE people (firstname text, lastname text)"); |
| 276 |
$node_subscriber->safe_psql('postgres', |
| 277 |
"CREATE INDEX people_names ON people (firstname, lastname, (firstname || ' ' || lastname))" |
| 278 |
); |
| 279 |
|
| 280 |
# insert some initial data |
| 281 |
$node_publisher->safe_psql('postgres', |
| 282 |
"INSERT INTO people SELECT 'first_name_' || i::text, 'last_name_' || i::text FROM generate_series(0, 20) i" |
| 283 |
); |
| 284 |
|
| 285 |
# create pub/sub |
| 286 |
$node_publisher->safe_psql('postgres', |
| 287 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE people"); |
| 288 |
$node_subscriber->safe_psql('postgres', |
| 289 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 290 |
); |
| 291 |
|
| 292 |
# wait for initial table synchronization to finish |
| 293 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 294 |
|
| 295 |
# update 1 row |
| 296 |
$node_publisher->safe_psql('postgres', |
| 297 |
"UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_1'" |
| 298 |
); |
| 299 |
|
| 300 |
# delete the updated row |
| 301 |
$node_publisher->safe_psql('postgres', |
| 302 |
"DELETE FROM people WHERE firstname = 'no-name'"); |
| 303 |
|
| 304 |
# wait until the index is used on the subscriber |
| 305 |
$node_publisher->wait_for_catchup($appname); |
| 306 |
$node_subscriber->poll_query_until('postgres', |
| 307 |
q{select idx_scan=2 from pg_stat_all_indexes where indexrelname = 'people_names';} |
| 308 |
) |
| 309 |
or die |
| 310 |
"Timed out while waiting for check subscriber tap_sub_rep_full deletes two rows via index scan with index on expressions and columns"; |
| 311 |
|
| 312 |
# make sure that the subscriber has the correct data |
| 313 |
$result = |
| 314 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM people"); |
| 315 |
is($result, qq(20), |
| 316 |
'ensure subscriber has the correct data at the end of the test'); |
| 317 |
|
| 318 |
$result = $node_subscriber->safe_psql('postgres', |
| 319 |
"SELECT count(*) FROM people WHERE firstname = 'no-name'"); |
| 320 |
is($result, qq(0), |
| 321 |
'ensure subscriber has the correct data at the end of the test'); |
| 322 |
|
| 323 |
# now, drop the index with the expression, we'll use sequential scan |
| 324 |
$node_subscriber->safe_psql('postgres', "DROP INDEX people_names"); |
| 325 |
|
| 326 |
# delete 1 row |
| 327 |
$node_publisher->safe_psql('postgres', |
| 328 |
"DELETE FROM people WHERE lastname = 'last_name_18'"); |
| 329 |
|
| 330 |
# make sure that the subscriber has the correct data |
| 331 |
$node_publisher->wait_for_catchup($appname); |
| 332 |
$result = $node_subscriber->safe_psql('postgres', |
| 333 |
"SELECT count(*) FROM people WHERE lastname = 'last_name_18'"); |
| 334 |
is($result, qq(0), |
| 335 |
'ensure subscriber has the correct data at the end of the test'); |
| 336 |
|
| 337 |
# cleanup pub |
| 338 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 339 |
$node_publisher->safe_psql('postgres', "DROP TABLE people"); |
| 340 |
# cleanup sub |
| 341 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 342 |
$node_subscriber->safe_psql('postgres', "DROP TABLE people"); |
| 343 |
|
| 344 |
# Testcase end: Subscription can use index having expressions and columns |
| 345 |
# ============================================================================= |
| 346 |
|
| 347 |
# ============================================================================= |
| 348 |
# Testcase start: Null values and missing column |
| 349 |
|
| 350 |
$node_publisher->safe_psql('postgres', |
| 351 |
"CREATE TABLE test_replica_id_full (x int)"); |
| 352 |
|
| 353 |
$node_publisher->safe_psql('postgres', |
| 354 |
"ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL"); |
| 355 |
|
| 356 |
$node_subscriber->safe_psql('postgres', |
| 357 |
"CREATE TABLE test_replica_id_full (x int, y int)"); |
| 358 |
|
| 359 |
$node_subscriber->safe_psql('postgres', |
| 360 |
"CREATE INDEX test_replica_id_full_idx ON test_replica_id_full(x,y)"); |
| 361 |
|
| 362 |
# create pub/sub |
| 363 |
$node_publisher->safe_psql('postgres', |
| 364 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full"); |
| 365 |
|
| 366 |
$node_subscriber->safe_psql('postgres', |
| 367 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 368 |
); |
| 369 |
|
| 370 |
# wait for initial table synchronization to finish |
| 371 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 372 |
|
| 373 |
# load some data, and update 2 tuples |
| 374 |
$node_publisher->safe_psql('postgres', |
| 375 |
"INSERT INTO test_replica_id_full VALUES (1), (2), (3)"); |
| 376 |
$node_publisher->safe_psql('postgres', |
| 377 |
"UPDATE test_replica_id_full SET x = x + 1 WHERE x = 1"); |
| 378 |
|
| 379 |
# check if the index is used even when the index has NULL values |
| 380 |
$node_publisher->wait_for_catchup($appname); |
| 381 |
$node_subscriber->poll_query_until('postgres', |
| 382 |
q{select idx_scan=1 from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';} |
| 383 |
) |
| 384 |
or die |
| 385 |
"Timed out while waiting for check subscriber tap_sub_rep_full updates test_replica_id_full table"; |
| 386 |
|
| 387 |
# make sure that the subscriber has the correct data |
| 388 |
$result = $node_subscriber->safe_psql('postgres', |
| 389 |
"select sum(x) from test_replica_id_full WHERE y IS NULL"); |
| 390 |
is($result, qq(7), |
| 391 |
'ensure subscriber has the correct data at the end of the test'); |
| 392 |
|
| 393 |
# make sure that the subscriber has the correct data |
| 394 |
$result = $node_subscriber->safe_psql('postgres', |
| 395 |
"select count(*) from test_replica_id_full WHERE y IS NULL"); |
| 396 |
is($result, qq(3), |
| 397 |
'ensure subscriber has the correct data at the end of the test'); |
| 398 |
|
| 399 |
# cleanup pub |
| 400 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 401 |
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 402 |
|
| 403 |
# cleanup sub |
| 404 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 405 |
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 406 |
|
| 407 |
# Testcase end: Null values And missing column |
| 408 |
# ============================================================================= |
| 409 |
|
| 410 |
# ============================================================================= |
| 411 |
# Testcase start: Subscription using a unique index when Pub/Sub has different |
| 412 |
# data |
| 413 |
# |
| 414 |
# The subscriber has duplicate tuples that publisher does not have. When |
| 415 |
# publisher updates/deletes 1 row, subscriber uses indexes and updates/deletes |
| 416 |
# exactly 1 row. |
| 417 |
# |
| 418 |
|
| 419 |
# create tables pub and sub |
| 420 |
$node_publisher->safe_psql('postgres', |
| 421 |
"CREATE TABLE test_replica_id_full (x int, y int)"); |
| 422 |
$node_publisher->safe_psql('postgres', |
| 423 |
"ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL"); |
| 424 |
$node_subscriber->safe_psql('postgres', |
| 425 |
"CREATE TABLE test_replica_id_full (x int, y int)"); |
| 426 |
$node_subscriber->safe_psql('postgres', |
| 427 |
"CREATE UNIQUE INDEX test_replica_id_full_idxy ON test_replica_id_full(x,y)" |
| 428 |
); |
| 429 |
|
| 430 |
# insert some initial data |
| 431 |
$node_publisher->safe_psql('postgres', |
| 432 |
"INSERT INTO test_replica_id_full SELECT i, i FROM generate_series(0,21) i" |
| 433 |
); |
| 434 |
|
| 435 |
# create pub/sub |
| 436 |
$node_publisher->safe_psql('postgres', |
| 437 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full"); |
| 438 |
$node_subscriber->safe_psql('postgres', |
| 439 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 440 |
); |
| 441 |
|
| 442 |
# wait for initial table synchronization to finish |
| 443 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 444 |
|
| 445 |
# duplicate the data in subscriber for y column |
| 446 |
$node_subscriber->safe_psql('postgres', |
| 447 |
"INSERT INTO test_replica_id_full SELECT i+100, i FROM generate_series(0,21) i" |
| 448 |
); |
| 449 |
|
| 450 |
# now, we update only 1 row on the publisher and expect the subscriber to only |
| 451 |
# update 1 row although there are two tuples with y = 15 on the subscriber |
| 452 |
$node_publisher->safe_psql('postgres', |
| 453 |
"UPDATE test_replica_id_full SET x = 2000 WHERE y = 15"); |
| 454 |
|
| 455 |
# wait until the index is used on the subscriber |
| 456 |
$node_publisher->wait_for_catchup($appname); |
| 457 |
$node_subscriber->poll_query_until('postgres', |
| 458 |
q{select (idx_scan = 1) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idxy';} |
| 459 |
) |
| 460 |
or die |
| 461 |
"Timed out while waiting for check subscriber tap_sub_rep_full updates one row via index"; |
| 462 |
|
| 463 |
# make sure that the subscriber has the correct data |
| 464 |
# we only updated 1 row |
| 465 |
$result = $node_subscriber->safe_psql('postgres', |
| 466 |
"SELECT count(*) FROM test_replica_id_full WHERE x = 2000"); |
| 467 |
is($result, qq(1), |
| 468 |
'ensure subscriber has the correct data at the end of the test'); |
| 469 |
|
| 470 |
# cleanup pub |
| 471 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 472 |
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 473 |
# cleanup sub |
| 474 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 475 |
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 476 |
|
| 477 |
# Testcase start: Subscription using a unique index when Pub/Sub has different |
| 478 |
# data |
| 479 |
# ============================================================================= |
| 480 |
|
| 481 |
# ============================================================================= |
| 482 |
# Testcase start: Subscription can use hash index |
| 483 |
# |
| 484 |
|
| 485 |
# create tables on pub and sub |
| 486 |
$node_publisher->safe_psql('postgres', |
| 487 |
"CREATE TABLE test_replica_id_full (x int, y text)"); |
| 488 |
$node_publisher->safe_psql('postgres', |
| 489 |
"ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL"); |
| 490 |
$node_subscriber->safe_psql('postgres', |
| 491 |
"CREATE TABLE test_replica_id_full (x int, y text)"); |
| 492 |
$node_subscriber->safe_psql('postgres', |
| 493 |
"CREATE INDEX test_replica_id_full_idx ON test_replica_id_full USING HASH (x)" |
| 494 |
); |
| 495 |
|
| 496 |
# insert some initial data |
| 497 |
$node_publisher->safe_psql('postgres', |
| 498 |
"INSERT INTO test_replica_id_full SELECT i, (i%10)::text FROM generate_series(0,10) i" |
| 499 |
); |
| 500 |
|
| 501 |
# create pub/sub |
| 502 |
$node_publisher->safe_psql('postgres', |
| 503 |
"CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full"); |
| 504 |
$node_subscriber->safe_psql('postgres', |
| 505 |
"CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full" |
| 506 |
); |
| 507 |
|
| 508 |
# wait for initial table synchronization to finish |
| 509 |
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); |
| 510 |
|
| 511 |
# delete 2 rows |
| 512 |
$node_publisher->safe_psql('postgres', |
| 513 |
"DELETE FROM test_replica_id_full WHERE x IN (5, 6)"); |
| 514 |
|
| 515 |
# update 2 rows |
| 516 |
$node_publisher->safe_psql('postgres', |
| 517 |
"UPDATE test_replica_id_full SET x = 100, y = '200' WHERE x IN (1, 2)"); |
| 518 |
|
| 519 |
# wait until the index is used on the subscriber |
| 520 |
$node_publisher->wait_for_catchup($appname); |
| 521 |
$node_subscriber->poll_query_until('postgres', |
| 522 |
q{select (idx_scan = 4) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';} |
| 523 |
) |
| 524 |
or die |
| 525 |
"Timed out while waiting for check subscriber tap_sub_rep_full deletes 2 rows and updates 2 rows via index"; |
| 526 |
|
| 527 |
# make sure that the subscriber has the correct data after the UPDATE |
| 528 |
$result = $node_subscriber->safe_psql('postgres', |
| 529 |
"select count(*) from test_replica_id_full WHERE (x = 100 and y = '200')" |
| 530 |
); |
| 531 |
is($result, qq(2), |
| 532 |
'ensure subscriber has the correct data at the end of the test'); |
| 533 |
|
| 534 |
# make sure that the subscriber has the correct data after the first DELETE |
| 535 |
$result = $node_subscriber->safe_psql('postgres', |
| 536 |
"select count(*) from test_replica_id_full where x in (5, 6)"); |
| 537 |
is($result, qq(0), |
| 538 |
'ensure subscriber has the correct data at the end of the test'); |
| 539 |
|
| 540 |
# cleanup pub |
| 541 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full"); |
| 542 |
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 543 |
# cleanup sub |
| 544 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full"); |
| 545 |
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full"); |
| 546 |
|
| 547 |
# Testcase end: Subscription can use hash index |
| 548 |
# ============================================================================= |
| 549 |
|
| 550 |
$node_subscriber->stop('fast'); |
| 551 |
$node_publisher->stop('fast'); |
| 552 |
|
| 553 |
done_testing(); |