| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests for various bugs found over time |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Bug #15114 |
| 12 |
|
| 13 |
# The bug was that determining which columns are part of the replica |
| 14 |
# identity index using RelationGetIndexAttrBitmap() would run |
| 15 |
# eval_const_expressions() on index expressions and predicates across |
| 16 |
# all indexes of the table, which in turn might require a snapshot, |
| 17 |
# but there wasn't one set, so it crashes. There were actually two |
| 18 |
# separate bugs, one on the publisher and one on the subscriber. The |
| 19 |
# fix was to avoid the constant expressions simplification in |
| 20 |
# RelationGetIndexAttrBitmap(), so it's safe to call in more contexts. |
| 21 |
|
| 22 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 23 |
$node_publisher->init(allows_streaming => 'logical'); |
| 24 |
$node_publisher->start; |
| 25 |
|
| 26 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 27 |
$node_subscriber->init; |
| 28 |
$node_subscriber->start; |
| 29 |
|
| 30 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 31 |
|
| 32 |
$node_publisher->safe_psql('postgres', |
| 33 |
"CREATE TABLE tab1 (a int PRIMARY KEY, b int)"); |
| 34 |
|
| 35 |
$node_publisher->safe_psql('postgres', |
| 36 |
"CREATE FUNCTION double(x int) RETURNS int IMMUTABLE LANGUAGE SQL AS 'select x * 2'" |
| 37 |
); |
| 38 |
|
| 39 |
# an index with a predicate that lends itself to constant expressions |
| 40 |
# evaluation |
| 41 |
$node_publisher->safe_psql('postgres', |
| 42 |
"CREATE INDEX ON tab1 (b) WHERE a > double(1)"); |
| 43 |
|
| 44 |
# and the same setup on the subscriber |
| 45 |
$node_subscriber->safe_psql('postgres', |
| 46 |
"CREATE TABLE tab1 (a int PRIMARY KEY, b int)"); |
| 47 |
|
| 48 |
$node_subscriber->safe_psql('postgres', |
| 49 |
"CREATE FUNCTION double(x int) RETURNS int IMMUTABLE LANGUAGE SQL AS 'select x * 2'" |
| 50 |
); |
| 51 |
|
| 52 |
$node_subscriber->safe_psql('postgres', |
| 53 |
"CREATE INDEX ON tab1 (b) WHERE a > double(1)"); |
| 54 |
|
| 55 |
$node_publisher->safe_psql('postgres', |
| 56 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 57 |
|
| 58 |
$node_subscriber->safe_psql('postgres', |
| 59 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 60 |
); |
| 61 |
|
| 62 |
$node_publisher->wait_for_catchup('sub1'); |
| 63 |
|
| 64 |
# This would crash, first on the publisher, and then (if the publisher |
| 65 |
# is fixed) on the subscriber. |
| 66 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (1, 2)"); |
| 67 |
|
| 68 |
$node_publisher->wait_for_catchup('sub1'); |
| 69 |
|
| 70 |
pass('index predicates do not cause crash'); |
| 71 |
|
| 72 |
# We'll re-use these nodes below, so drop their replication state. |
| 73 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 74 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 75 |
# Drop the tables too. |
| 76 |
$node_publisher->safe_psql('postgres', "DROP TABLE tab1"); |
| 77 |
|
| 78 |
$node_publisher->stop('fast'); |
| 79 |
$node_subscriber->stop('fast'); |
| 80 |
|
| 81 |
|
| 82 |
# Handling of temporary and unlogged tables with FOR ALL TABLES publications |
| 83 |
|
| 84 |
# If a FOR ALL TABLES publication exists, temporary and unlogged |
| 85 |
# tables are ignored for publishing changes. The bug was that we |
| 86 |
# would still check in that case that such a table has a replica |
| 87 |
# identity set before accepting updates. If it did not it would cause |
| 88 |
# an error when an update was attempted. |
| 89 |
|
| 90 |
$node_publisher->rotate_logfile(); |
| 91 |
$node_publisher->start(); |
| 92 |
|
| 93 |
# Although we don't use node_subscriber in this test, keep its logfile |
| 94 |
# name in step with node_publisher for later tests. |
| 95 |
$node_subscriber->rotate_logfile(); |
| 96 |
|
| 97 |
$node_publisher->safe_psql('postgres', |
| 98 |
"CREATE PUBLICATION pub FOR ALL TABLES"); |
| 99 |
|
| 100 |
is( $node_publisher->psql( |
| 101 |
'postgres', |
| 102 |
"CREATE TEMPORARY TABLE tt1 AS SELECT 1 AS a; UPDATE tt1 SET a = 2;"), |
| 103 |
0, |
| 104 |
'update to temporary table without replica identity with FOR ALL TABLES publication' |
| 105 |
); |
| 106 |
|
| 107 |
is( $node_publisher->psql( |
| 108 |
'postgres', |
| 109 |
"CREATE UNLOGGED TABLE tu1 AS SELECT 1 AS a; UPDATE tu1 SET a = 2;"), |
| 110 |
0, |
| 111 |
'update to unlogged table without replica identity with FOR ALL TABLES publication' |
| 112 |
); |
| 113 |
|
| 114 |
# Again, drop replication state but not tables. |
| 115 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub"); |
| 116 |
|
| 117 |
$node_publisher->stop('fast'); |
| 118 |
|
| 119 |
|
| 120 |
# Bug #16643 - https://postgr.es/m/[email protected] |
| 121 |
# |
| 122 |
# Initial sync doesn't complete; the protocol was not being followed per |
| 123 |
# expectations after commit 07082b08cc5d. |
| 124 |
my $node_twoways = PostgreSQL::Test::Cluster->new('twoways'); |
| 125 |
$node_twoways->init(allows_streaming => 'logical'); |
| 126 |
$node_twoways->start; |
| 127 |
for my $db (qw(d1 d2)) |
| 128 |
{ |
| 129 |
$node_twoways->safe_psql('postgres', "CREATE DATABASE $db"); |
| 130 |
$node_twoways->safe_psql($db, "CREATE TABLE t (f int)"); |
| 131 |
$node_twoways->safe_psql($db, "CREATE TABLE t2 (f int)"); |
| 132 |
} |
| 133 |
|
| 134 |
my $rows = 3000; |
| 135 |
$node_twoways->safe_psql( |
| 136 |
'd1', qq{ |
| 137 |
INSERT INTO t SELECT * FROM generate_series(1, $rows); |
| 138 |
INSERT INTO t2 SELECT * FROM generate_series(1, $rows); |
| 139 |
CREATE PUBLICATION testpub FOR TABLE t; |
| 140 |
SELECT pg_create_logical_replication_slot('testslot', 'pgoutput'); |
| 141 |
}); |
| 142 |
|
| 143 |
$node_twoways->safe_psql('d2', |
| 144 |
"CREATE SUBSCRIPTION testsub CONNECTION \$\$" |
| 145 |
. $node_twoways->connstr('d1') |
| 146 |
. "\$\$ PUBLICATION testpub WITH (create_slot=false, " |
| 147 |
. "slot_name='testslot')"); |
| 148 |
$node_twoways->safe_psql( |
| 149 |
'd1', qq{ |
| 150 |
INSERT INTO t SELECT * FROM generate_series(1, $rows); |
| 151 |
INSERT INTO t2 SELECT * FROM generate_series(1, $rows); |
| 152 |
}); |
| 153 |
$node_twoways->safe_psql('d1', 'ALTER PUBLICATION testpub ADD TABLE t2'); |
| 154 |
$node_twoways->safe_psql('d2', |
| 155 |
'ALTER SUBSCRIPTION testsub REFRESH PUBLICATION'); |
| 156 |
|
| 157 |
# We cannot rely solely on wait_for_catchup() here; it isn't sufficient |
| 158 |
# when tablesync workers might still be running. So in addition to that, |
| 159 |
# verify that tables are synced. |
| 160 |
$node_twoways->wait_for_subscription_sync($node_twoways, 'testsub', 'd2'); |
| 161 |
|
| 162 |
is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t"), |
| 163 |
$rows * 2, "2x$rows rows in t"); |
| 164 |
is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t2"), |
| 165 |
$rows * 2, "2x$rows rows in t2"); |
| 166 |
|
| 167 |
# Verify table data is synced with cascaded replication setup. This is mainly |
| 168 |
# to test whether the data written by tablesync worker gets replicated. |
| 169 |
my $node_pub = PostgreSQL::Test::Cluster->new('testpublisher1'); |
| 170 |
$node_pub->init(allows_streaming => 'logical'); |
| 171 |
$node_pub->start; |
| 172 |
|
| 173 |
my $node_pub_sub = PostgreSQL::Test::Cluster->new('testpublisher_subscriber'); |
| 174 |
$node_pub_sub->init(allows_streaming => 'logical'); |
| 175 |
$node_pub_sub->start; |
| 176 |
|
| 177 |
my $node_sub = PostgreSQL::Test::Cluster->new('testsubscriber1'); |
| 178 |
$node_sub->init; |
| 179 |
$node_sub->start; |
| 180 |
|
| 181 |
# Create the tables in all nodes. |
| 182 |
$node_pub->safe_psql('postgres', "CREATE TABLE tab1 (a int)"); |
| 183 |
$node_pub_sub->safe_psql('postgres', "CREATE TABLE tab1 (a int)"); |
| 184 |
$node_sub->safe_psql('postgres', "CREATE TABLE tab1 (a int)"); |
| 185 |
|
| 186 |
# Create a cascaded replication setup like: |
| 187 |
# N1 - Create publication testpub1. |
| 188 |
# N2 - Create publication testpub2 and also include subscriber which subscribes |
| 189 |
# to testpub1. |
| 190 |
# N3 - Create subscription testsub2 subscribes to testpub2. |
| 191 |
# |
| 192 |
# Note that subscription on N3 needs to be created before subscription on N2 to |
| 193 |
# test whether the data written by tablesync worker of N2 gets replicated. |
| 194 |
$node_pub->safe_psql('postgres', |
| 195 |
"CREATE PUBLICATION testpub1 FOR TABLE tab1"); |
| 196 |
|
| 197 |
$node_pub_sub->safe_psql('postgres', |
| 198 |
"CREATE PUBLICATION testpub2 FOR TABLE tab1"); |
| 199 |
|
| 200 |
my $publisher1_connstr = $node_pub->connstr . ' dbname=postgres'; |
| 201 |
my $publisher2_connstr = $node_pub_sub->connstr . ' dbname=postgres'; |
| 202 |
|
| 203 |
$node_sub->safe_psql('postgres', |
| 204 |
"CREATE SUBSCRIPTION testsub2 CONNECTION '$publisher2_connstr' PUBLICATION testpub2" |
| 205 |
); |
| 206 |
|
| 207 |
$node_pub_sub->safe_psql('postgres', |
| 208 |
"CREATE SUBSCRIPTION testsub1 CONNECTION '$publisher1_connstr' PUBLICATION testpub1" |
| 209 |
); |
| 210 |
|
| 211 |
$node_pub->safe_psql('postgres', |
| 212 |
"INSERT INTO tab1 values(generate_series(1,10))"); |
| 213 |
|
| 214 |
# Verify that the data is cascaded from testpub1 to testsub1 and further from |
| 215 |
# testpub2 (which had testsub1) to testsub2. |
| 216 |
$node_pub->wait_for_catchup('testsub1'); |
| 217 |
$node_pub_sub->wait_for_catchup('testsub2'); |
| 218 |
|
| 219 |
# Drop subscriptions as we don't need them anymore |
| 220 |
$node_pub_sub->safe_psql('postgres', "DROP SUBSCRIPTION testsub1"); |
| 221 |
$node_sub->safe_psql('postgres', "DROP SUBSCRIPTION testsub2"); |
| 222 |
|
| 223 |
# Drop publications as we don't need them anymore |
| 224 |
$node_pub->safe_psql('postgres', "DROP PUBLICATION testpub1"); |
| 225 |
$node_pub_sub->safe_psql('postgres', "DROP PUBLICATION testpub2"); |
| 226 |
|
| 227 |
# Clean up the tables on both publisher and subscriber as we don't need them |
| 228 |
$node_pub->safe_psql('postgres', "DROP TABLE tab1"); |
| 229 |
$node_pub_sub->safe_psql('postgres', "DROP TABLE tab1"); |
| 230 |
$node_sub->safe_psql('postgres', "DROP TABLE tab1"); |
| 231 |
|
| 232 |
$node_pub->stop('fast'); |
| 233 |
$node_pub_sub->stop('fast'); |
| 234 |
$node_sub->stop('fast'); |
| 235 |
|
| 236 |
# https://postgr.es/m/OS0PR01MB61133CA11630DAE45BC6AD95FB939%40OS0PR01MB6113.jpnprd01.prod.outlook.com |
| 237 |
|
| 238 |
# The bug was that when changing the REPLICA IDENTITY INDEX to another one, the |
| 239 |
# target table's relcache was not being invalidated. This leads to skipping |
| 240 |
# UPDATE/DELETE operations during apply on the subscriber side as the columns |
| 241 |
# required to search corresponding rows won't get logged. |
| 242 |
|
| 243 |
$node_publisher->rotate_logfile(); |
| 244 |
$node_publisher->start(); |
| 245 |
|
| 246 |
$node_subscriber->rotate_logfile(); |
| 247 |
$node_subscriber->start(); |
| 248 |
|
| 249 |
$node_publisher->safe_psql('postgres', |
| 250 |
"CREATE TABLE tab_replidentity_index(a int not null, b int not null)"); |
| 251 |
$node_publisher->safe_psql('postgres', |
| 252 |
"CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)" |
| 253 |
); |
| 254 |
$node_publisher->safe_psql('postgres', |
| 255 |
"CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)" |
| 256 |
); |
| 257 |
|
| 258 |
# use index idx_replidentity_index_a as REPLICA IDENTITY on publisher. |
| 259 |
$node_publisher->safe_psql('postgres', |
| 260 |
"ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_a" |
| 261 |
); |
| 262 |
|
| 263 |
$node_publisher->safe_psql('postgres', |
| 264 |
"INSERT INTO tab_replidentity_index VALUES(1, 1),(2, 2)"); |
| 265 |
|
| 266 |
$node_subscriber->safe_psql('postgres', |
| 267 |
"CREATE TABLE tab_replidentity_index(a int not null, b int not null)"); |
| 268 |
$node_subscriber->safe_psql('postgres', |
| 269 |
"CREATE UNIQUE INDEX idx_replidentity_index_a ON tab_replidentity_index(a)" |
| 270 |
); |
| 271 |
$node_subscriber->safe_psql('postgres', |
| 272 |
"CREATE UNIQUE INDEX idx_replidentity_index_b ON tab_replidentity_index(b)" |
| 273 |
); |
| 274 |
# use index idx_replidentity_index_b as REPLICA IDENTITY on subscriber because |
| 275 |
# it reflects the future scenario we are testing: changing REPLICA IDENTITY |
| 276 |
# INDEX. |
| 277 |
$node_subscriber->safe_psql('postgres', |
| 278 |
"ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b" |
| 279 |
); |
| 280 |
|
| 281 |
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 282 |
$node_publisher->safe_psql('postgres', |
| 283 |
"CREATE PUBLICATION tap_pub FOR TABLE tab_replidentity_index"); |
| 284 |
$node_subscriber->safe_psql('postgres', |
| 285 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 286 |
); |
| 287 |
|
| 288 |
# Wait for initial table sync to finish |
| 289 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); |
| 290 |
|
| 291 |
is( $node_subscriber->safe_psql( |
| 292 |
'postgres', "SELECT * FROM tab_replidentity_index"), |
| 293 |
qq(1|1 |
| 294 |
2|2), |
| 295 |
"check initial data on subscriber"); |
| 296 |
|
| 297 |
# Set REPLICA IDENTITY to idx_replidentity_index_b on publisher, then run UPDATE and DELETE. |
| 298 |
$node_publisher->safe_psql( |
| 299 |
'postgres', qq[ |
| 300 |
ALTER TABLE tab_replidentity_index REPLICA IDENTITY USING INDEX idx_replidentity_index_b; |
| 301 |
UPDATE tab_replidentity_index SET a = -a WHERE a = 1; |
| 302 |
DELETE FROM tab_replidentity_index WHERE a = 2; |
| 303 |
]); |
| 304 |
|
| 305 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 306 |
is( $node_subscriber->safe_psql( |
| 307 |
'postgres', "SELECT * FROM tab_replidentity_index"), |
| 308 |
qq(-1|1), |
| 309 |
"update works with REPLICA IDENTITY"); |
| 310 |
|
| 311 |
# Clean up |
| 312 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); |
| 313 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub"); |
| 314 |
$node_publisher->safe_psql('postgres', "DROP TABLE tab_replidentity_index"); |
| 315 |
$node_subscriber->safe_psql('postgres', "DROP TABLE tab_replidentity_index"); |
| 316 |
|
| 317 |
# Test schema invalidation by renaming the schema |
| 318 |
|
| 319 |
# Create tables on publisher |
| 320 |
$node_publisher->safe_psql('postgres', "CREATE SCHEMA sch1"); |
| 321 |
$node_publisher->safe_psql('postgres', "CREATE TABLE sch1.t1 (c1 int)"); |
| 322 |
|
| 323 |
# Create tables on subscriber |
| 324 |
$node_subscriber->safe_psql('postgres', "CREATE SCHEMA sch1"); |
| 325 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE sch1.t1 (c1 int)"); |
| 326 |
$node_subscriber->safe_psql('postgres', "CREATE SCHEMA sch2"); |
| 327 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE sch2.t1 (c1 int)"); |
| 328 |
|
| 329 |
# Setup logical replication that will cover t1 under both schema names |
| 330 |
$node_publisher->safe_psql('postgres', |
| 331 |
"CREATE PUBLICATION tap_pub_sch FOR ALL TABLES"); |
| 332 |
$node_subscriber->safe_psql('postgres', |
| 333 |
"CREATE SUBSCRIPTION tap_sub_sch CONNECTION '$publisher_connstr' PUBLICATION tap_pub_sch" |
| 334 |
); |
| 335 |
|
| 336 |
# Wait for initial table sync to finish |
| 337 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch'); |
| 338 |
|
| 339 |
# Check what happens to data inserted before and after schema rename |
| 340 |
$node_publisher->safe_psql( |
| 341 |
'postgres', |
| 342 |
"begin; |
| 343 |
insert into sch1.t1 values(1); |
| 344 |
alter schema sch1 rename to sch2; |
| 345 |
create schema sch1; |
| 346 |
create table sch1.t1(c1 int); |
| 347 |
insert into sch1.t1 values(2); |
| 348 |
insert into sch2.t1 values(3); |
| 349 |
commit;"); |
| 350 |
|
| 351 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch'); |
| 352 |
|
| 353 |
# Subscriber's sch1.t1 should receive the row inserted into the new sch1.t1, |
| 354 |
# but not the row inserted into the old sch1.t1 post-rename. |
| 355 |
my $result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch1.t1"); |
| 356 |
is( $result, qq(1 |
| 357 |
2), 'check data in subscriber sch1.t1 after schema rename'); |
| 358 |
|
| 359 |
# Subscriber's sch2.t1 won't have gotten anything yet ... |
| 360 |
$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch2.t1"); |
| 361 |
is($result, '', 'no data yet in subscriber sch2.t1 after schema rename'); |
| 362 |
|
| 363 |
# ... but it should show up after REFRESH. |
| 364 |
$node_subscriber->safe_psql('postgres', |
| 365 |
'ALTER SUBSCRIPTION tap_sub_sch REFRESH PUBLICATION'); |
| 366 |
|
| 367 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub_sch'); |
| 368 |
|
| 369 |
$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM sch2.t1"); |
| 370 |
is( $result, qq(1 |
| 371 |
3), 'check data in subscriber sch2.t1 after schema rename'); |
| 372 |
|
| 373 |
# Again, drop replication state but not tables. |
| 374 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_sch"); |
| 375 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_sch"); |
| 376 |
|
| 377 |
$node_publisher->stop('fast'); |
| 378 |
$node_subscriber->stop('fast'); |
| 379 |
|
| 380 |
# The bug was that when the REPLICA IDENTITY FULL is used with dropped |
| 381 |
# we fail to apply updates and deletes |
| 382 |
$node_publisher->rotate_logfile(); |
| 383 |
$node_publisher->start(); |
| 384 |
|
| 385 |
$node_subscriber->rotate_logfile(); |
| 386 |
$node_subscriber->start(); |
| 387 |
|
| 388 |
$node_publisher->safe_psql( |
| 389 |
'postgres', qq( |
| 390 |
CREATE TABLE dropped_cols (a int, b_drop int, c int); |
| 391 |
ALTER TABLE dropped_cols REPLICA IDENTITY FULL; |
| 392 |
CREATE PUBLICATION pub_dropped_cols FOR TABLE dropped_cols; |
| 393 |
-- some initial data |
| 394 |
INSERT INTO dropped_cols VALUES (1, 1, 1); |
| 395 |
)); |
| 396 |
|
| 397 |
$node_subscriber->safe_psql( |
| 398 |
'postgres', qq( |
| 399 |
CREATE TABLE dropped_cols (a int, b_drop int, c int); |
| 400 |
)); |
| 401 |
|
| 402 |
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 403 |
$node_subscriber->safe_psql('postgres', |
| 404 |
"CREATE SUBSCRIPTION sub_dropped_cols CONNECTION '$publisher_connstr' PUBLICATION pub_dropped_cols" |
| 405 |
); |
| 406 |
$node_subscriber->wait_for_subscription_sync; |
| 407 |
|
| 408 |
$node_publisher->safe_psql( |
| 409 |
'postgres', qq( |
| 410 |
ALTER TABLE dropped_cols DROP COLUMN b_drop; |
| 411 |
)); |
| 412 |
$node_subscriber->safe_psql( |
| 413 |
'postgres', qq( |
| 414 |
ALTER TABLE dropped_cols DROP COLUMN b_drop; |
| 415 |
)); |
| 416 |
|
| 417 |
$node_publisher->safe_psql( |
| 418 |
'postgres', qq( |
| 419 |
UPDATE dropped_cols SET a = 100; |
| 420 |
)); |
| 421 |
$node_publisher->wait_for_catchup('sub_dropped_cols'); |
| 422 |
|
| 423 |
is( $node_subscriber->safe_psql( |
| 424 |
'postgres', "SELECT count(*) FROM dropped_cols WHERE a = 100"), |
| 425 |
qq(1), |
| 426 |
'replication with RI FULL and dropped columns'); |
| 427 |
|
| 428 |
$node_publisher->stop('fast'); |
| 429 |
$node_subscriber->stop('fast'); |
| 430 |
|
| 431 |
# The bug was that pgoutput was incorrectly replacing missing attributes in |
| 432 |
# tuples with NULL. This could result in incorrect replication with |
| 433 |
# `REPLICA IDENTITY FULL`. |
| 434 |
|
| 435 |
$node_publisher->rotate_logfile(); |
| 436 |
$node_publisher->start(); |
| 437 |
|
| 438 |
$node_subscriber->rotate_logfile(); |
| 439 |
$node_subscriber->start(); |
| 440 |
|
| 441 |
# Set up a table with schema `(a int, b bool)` where the `b` attribute is |
| 442 |
# missing for one row due to the `ALTER TABLE ... ADD COLUMN ... DEFAULT` |
| 443 |
# fast path. |
| 444 |
$node_publisher->safe_psql( |
| 445 |
'postgres', qq( |
| 446 |
CREATE TABLE tab_default (a int); |
| 447 |
ALTER TABLE tab_default REPLICA IDENTITY FULL; |
| 448 |
INSERT INTO tab_default VALUES (1); |
| 449 |
ALTER TABLE tab_default ADD COLUMN b bool DEFAULT false NOT NULL; |
| 450 |
INSERT INTO tab_default VALUES (2, true); |
| 451 |
CREATE PUBLICATION pub1 FOR TABLE tab_default; |
| 452 |
)); |
| 453 |
|
| 454 |
# Replicate to the subscriber. |
| 455 |
$node_subscriber->safe_psql( |
| 456 |
'postgres', qq( |
| 457 |
CREATE TABLE tab_default (a int, b bool); |
| 458 |
CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1; |
| 459 |
)); |
| 460 |
|
| 461 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub1'); |
| 462 |
$result = |
| 463 |
$node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab_default"); |
| 464 |
is( $result, qq(1|f |
| 465 |
2|t), 'check snapshot on subscriber'); |
| 466 |
|
| 467 |
# Update all rows in the table and ensure the rows with the missing `b` |
| 468 |
# attribute replicate correctly. |
| 469 |
$node_publisher->safe_psql('postgres', "UPDATE tab_default SET a = a + 1"); |
| 470 |
$node_publisher->wait_for_catchup('sub1'); |
| 471 |
|
| 472 |
# When the bug is present, the `1|f` row will not be updated to `2|f` because |
| 473 |
# the publisher incorrectly fills in `NULL` for `b` and publishes an update |
| 474 |
# for `1|NULL`, which doesn't exist in the subscriber. |
| 475 |
$result = |
| 476 |
$node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab_default"); |
| 477 |
is( $result, qq(2|f |
| 478 |
3|t), 'check replicated update on subscriber'); |
| 479 |
|
| 480 |
my $publisher_host = $node_publisher->host; |
| 481 |
my $publisher_port = $node_publisher->port; |
| 482 |
my $connstr_db = |
| 483 |
"host=$publisher_host port=$publisher_port replication=database dbname=postgres"; |
| 484 |
|
| 485 |
# Test create and immediate drop of replication slot via replication commands |
| 486 |
# (this exposed a memory-management bug in v18) |
| 487 |
is( $node_publisher->psql( |
| 488 |
'postgres', |
| 489 |
qq[ |
| 490 |
CREATE_REPLICATION_SLOT test_slot LOGICAL pgoutput (SNAPSHOT export); |
| 491 |
DROP_REPLICATION_SLOT test_slot; |
| 492 |
], |
| 493 |
timeout => $PostgreSQL::Test::Utils::timeout_default, |
| 494 |
extra_params => [ '-d', $connstr_db ]), |
| 495 |
0, |
| 496 |
'create and immediate drop of replication slot'); |
| 497 |
|
| 498 |
# REPLICATION users should not be able to bypass LOAD restrictions. |
| 499 |
$node_publisher->safe_psql( |
| 500 |
'postgres', qq( |
| 501 |
CREATE USER repluser REPLICATION; |
| 502 |
)); |
| 503 |
|
| 504 |
my ($ret, $stdout, $stderr) = $node_publisher->psql( |
| 505 |
'postgres', |
| 506 |
qq[ |
| 507 |
SET ROLE repluser; |
| 508 |
CREATE_REPLICATION_SLOT fail_slot LOGICAL regress;', |
| 509 |
], |
| 510 |
timeout => $PostgreSQL::Test::Utils::timeout_default, |
| 511 |
extra_params => [ '-d', $connstr_db ]); |
| 512 |
|
| 513 |
is($ret, 3, 'loading unblessed output plugin fails'); |
| 514 |
like( |
| 515 |
$stderr, |
| 516 |
qr/ERROR: library "regress" may not be used as an output plugin/, |
| 517 |
'loading unblessed output plugin fails: stderr'); |
| 518 |
|
| 519 |
$node_publisher->stop('fast'); |
| 520 |
$node_subscriber->stop('fast'); |
| 521 |
|
| 522 |
# The bug was that when an ERROR was caught and handled by a (PL/pgSQL) |
| 523 |
# function, the apply worker reset the replication origin but continued |
| 524 |
# processing subsequent changes. So, we fail to update the replication origin |
| 525 |
# during further apply operations. This can lead to the apply worker requesting |
| 526 |
# the changes that have been applied again after restarting. |
| 527 |
|
| 528 |
$node_publisher->rotate_logfile(); |
| 529 |
$node_publisher->start(); |
| 530 |
|
| 531 |
$node_subscriber->rotate_logfile(); |
| 532 |
$node_subscriber->start(); |
| 533 |
|
| 534 |
# Set up a publication with a table |
| 535 |
$node_publisher->safe_psql( |
| 536 |
'postgres', qq( |
| 537 |
CREATE TABLE t1 (a int); |
| 538 |
CREATE PUBLICATION regress_pub FOR TABLE t1; |
| 539 |
)); |
| 540 |
|
| 541 |
# Set up a subscription which subscribes the publication |
| 542 |
$node_subscriber->safe_psql( |
| 543 |
'postgres', qq( |
| 544 |
CREATE TABLE t1 (a int); |
| 545 |
CREATE SUBSCRIPTION regress_sub CONNECTION '$publisher_connstr' PUBLICATION regress_pub; |
| 546 |
)); |
| 547 |
|
| 548 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'regress_sub'); |
| 549 |
|
| 550 |
# Create an AFTER INSERT trigger on the table that raises and subsequently |
| 551 |
# handles an exception. Subsequent insertions will trigger this exception, |
| 552 |
# causing the apply worker to invoke its error callback with an ERROR. However, |
| 553 |
# since the error is caught within the trigger, the apply worker will continue |
| 554 |
# processing changes. |
| 555 |
$node_subscriber->safe_psql( |
| 556 |
'postgres', q{ |
| 557 |
CREATE FUNCTION handle_exception_trigger() |
| 558 |
RETURNS TRIGGER AS $$ |
| 559 |
BEGIN |
| 560 |
BEGIN |
| 561 |
-- Raise an exception |
| 562 |
RAISE EXCEPTION 'This is a test exception'; |
| 563 |
EXCEPTION |
| 564 |
WHEN OTHERS THEN |
| 565 |
RETURN NEW; |
| 566 |
END; |
| 567 |
|
| 568 |
RETURN NEW; |
| 569 |
END; |
| 570 |
$$ LANGUAGE plpgsql; |
| 571 |
|
| 572 |
CREATE TRIGGER silent_exception_trigger |
| 573 |
AFTER INSERT OR UPDATE ON t1 |
| 574 |
FOR EACH ROW |
| 575 |
EXECUTE FUNCTION handle_exception_trigger(); |
| 576 |
|
| 577 |
ALTER TABLE t1 ENABLE ALWAYS TRIGGER silent_exception_trigger; |
| 578 |
}); |
| 579 |
|
| 580 |
# Obtain current remote_lsn value to check its advancement later |
| 581 |
my $remote_lsn = $node_subscriber->safe_psql('postgres', |
| 582 |
"SELECT remote_lsn FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'" |
| 583 |
); |
| 584 |
|
| 585 |
# Insert a tuple to replicate changes |
| 586 |
$node_publisher->safe_psql('postgres', "INSERT INTO t1 VALUES (1);"); |
| 587 |
$node_publisher->wait_for_catchup('regress_sub'); |
| 588 |
|
| 589 |
# Confirms the origin can be advanced |
| 590 |
$result = $node_subscriber->safe_psql('postgres', |
| 591 |
"SELECT remote_lsn > '$remote_lsn' FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'" |
| 592 |
); |
| 593 |
is($result, 't', |
| 594 |
'remote_lsn has advanced for apply worker raising an exception'); |
| 595 |
|
| 596 |
$node_publisher->stop('fast'); |
| 597 |
$node_subscriber->stop('fast'); |
| 598 |
|
| 599 |
# BUG #18988 |
| 600 |
# The bug happened due to a self-deadlock between the DROP SUBSCRIPTION |
| 601 |
# command and the walsender process for accessing pg_subscription. This |
| 602 |
# occurred when DROP SUBSCRIPTION attempted to remove a replication slot by |
| 603 |
# connecting to a newly created database whose caches are not yet |
| 604 |
# initialized. |
| 605 |
# |
| 606 |
# The bug is fixed by reducing the lock-level during DROP SUBSCRIPTION. |
| 607 |
$node_publisher->start(); |
| 608 |
|
| 609 |
$publisher_connstr = $node_publisher->connstr . ' dbname=regress_db'; |
| 610 |
$node_publisher->safe_psql( |
| 611 |
'postgres', qq( |
| 612 |
CREATE DATABASE regress_db; |
| 613 |
CREATE SUBSCRIPTION regress_sub1 CONNECTION '$publisher_connstr' PUBLICATION regress_pub WITH (connect=false); |
| 614 |
)); |
| 615 |
|
| 616 |
($ret, $stdout, $stderr) = |
| 617 |
$node_publisher->psql('postgres', q{DROP SUBSCRIPTION regress_sub1}); |
| 618 |
|
| 619 |
isnt($ret, 0, "replication slot does not exist: exit code not 0"); |
| 620 |
like( |
| 621 |
$stderr, |
| 622 |
qr/ERROR: could not drop replication slot "regress_sub1" on publisher/, |
| 623 |
"could not drop replication slot: error message"); |
| 624 |
|
| 625 |
$node_publisher->safe_psql('postgres', "DROP DATABASE regress_db"); |
| 626 |
|
| 627 |
$node_publisher->stop('fast'); |
| 628 |
|
| 629 |
# https://postgr.es/m/19c7623e882.4080fd5426212.311756747309556767%40zohocorp.com |
| 630 |
|
| 631 |
# The bug was that when an ERROR was raised while processing an INSERT ... ON |
| 632 |
# CONFLICT statement, the decoded change misses to be free'd. This can cause an |
| 633 |
# assertion failure if enabled. |
| 634 |
|
| 635 |
$node_publisher->rotate_logfile(); |
| 636 |
$node_publisher->start(); |
| 637 |
|
| 638 |
# Create a publication with the zero-division row filter. It always throws an |
| 639 |
# ERROR before publishing changes, when the filter is evaluated. |
| 640 |
$node_publisher->safe_psql( |
| 641 |
'postgres', qq( |
| 642 |
CREATE TABLE tab_upsert (a INT PRIMARY KEY, b INT); |
| 643 |
CREATE PUBLICATION pub_rowfilter_error FOR TABLE tab_upsert WHERE ((a / 0) > 0); |
| 644 |
SELECT * FROM pg_create_logical_replication_slot('upsert_slot', 'pgoutput'); |
| 645 |
INSERT INTO tab_upsert (a, b) VALUES (1, 1) |
| 646 |
ON CONFLICT(a) DO UPDATE SET b = excluded.b; |
| 647 |
)); |
| 648 |
|
| 649 |
# Decode the changes with a publication whose row filter causes a |
| 650 |
# division by zero error, and verify that the logical decoder doesn't crash. |
| 651 |
($ret, $stdout, $stderr) = $node_publisher->psql( |
| 652 |
'postgres', qq( |
| 653 |
SELECT * |
| 654 |
FROM pg_logical_slot_peek_binary_changes( |
| 655 |
'upsert_slot', |
| 656 |
NULL, |
| 657 |
NULL, |
| 658 |
'proto_version', '1', |
| 659 |
'publication_names', 'pub_rowfilter_error' |
| 660 |
); |
| 661 |
)); |
| 662 |
|
| 663 |
ok( $stderr =~ qr/division by zero/, |
| 664 |
'peek logical changes with row filter causing division by zero throws error' |
| 665 |
); |
| 666 |
|
| 667 |
# Clean up |
| 668 |
$node_publisher->safe_psql('postgres', "SELECT pg_drop_replication_slot('upsert_slot')"); |
| 669 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub_rowfilter_error"); |
| 670 |
$node_publisher->safe_psql('postgres', "DROP TABLE tab_upsert"); |
| 671 |
|
| 672 |
$node_publisher->stop('fast'); |
| 673 |
|
| 674 |
done_testing(); |