| 1 |
|
| 2 |
# Copyright (c) 2024-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Logical replication tests for temporal tables |
| 5 |
# |
| 6 |
# A table can use a temporal PRIMARY KEY or UNIQUE index as its REPLICA IDENTITY. |
| 7 |
# This is a GiST index (not B-tree) and its last element uses WITHOUT OVERLAPS. |
| 8 |
# That element restricts other rows with overlaps semantics instead of equality, |
| 9 |
# but it is always at least as restrictive as a normal non-null unique index. |
| 10 |
# Therefore we can still apply logical decoding messages to the subscriber. |
| 11 |
use strict; |
| 12 |
use warnings FATAL => 'all'; |
| 13 |
use PostgreSQL::Test::Cluster; |
| 14 |
use PostgreSQL::Test::Utils; |
| 15 |
use Test::More; |
| 16 |
|
| 17 |
# setup |
| 18 |
|
| 19 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 20 |
$node_publisher->init(allows_streaming => 'logical'); |
| 21 |
$node_publisher->start; |
| 22 |
|
| 23 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 24 |
$node_subscriber->init; |
| 25 |
$node_subscriber->start; |
| 26 |
|
| 27 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 28 |
|
| 29 |
my ($result, $stdout, $stderr); |
| 30 |
|
| 31 |
sub create_tables() |
| 32 |
{ |
| 33 |
# create tables on publisher |
| 34 |
|
| 35 |
$node_publisher->safe_psql('postgres', |
| 36 |
"CREATE TABLE temporal_no_key (id int4range, valid_at daterange, a text)" |
| 37 |
); |
| 38 |
|
| 39 |
$node_publisher->safe_psql('postgres', |
| 40 |
"CREATE TABLE temporal_pk (id int4range, valid_at daterange, a text, PRIMARY KEY (id, valid_at WITHOUT OVERLAPS))" |
| 41 |
); |
| 42 |
|
| 43 |
$node_publisher->safe_psql('postgres', |
| 44 |
"CREATE TABLE temporal_unique (id int4range, valid_at daterange, a text, UNIQUE (id, valid_at WITHOUT OVERLAPS))" |
| 45 |
); |
| 46 |
|
| 47 |
# create tables on subscriber |
| 48 |
|
| 49 |
$node_subscriber->safe_psql('postgres', |
| 50 |
"CREATE TABLE temporal_no_key (id int4range, valid_at daterange, a text)" |
| 51 |
); |
| 52 |
|
| 53 |
$node_subscriber->safe_psql('postgres', |
| 54 |
"CREATE TABLE temporal_pk (id int4range, valid_at daterange, a text, PRIMARY KEY (id, valid_at WITHOUT OVERLAPS))" |
| 55 |
); |
| 56 |
|
| 57 |
$node_subscriber->safe_psql('postgres', |
| 58 |
"CREATE TABLE temporal_unique (id int4range, valid_at daterange, a text, UNIQUE (id, valid_at WITHOUT OVERLAPS))" |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
sub drop_everything() |
| 63 |
{ |
| 64 |
$node_publisher->safe_psql('postgres', |
| 65 |
"DROP TABLE IF EXISTS temporal_no_key"); |
| 66 |
$node_publisher->safe_psql('postgres', |
| 67 |
"DROP TABLE IF EXISTS temporal_pk"); |
| 68 |
$node_publisher->safe_psql('postgres', |
| 69 |
"DROP TABLE IF EXISTS temporal_unique"); |
| 70 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 71 |
$node_subscriber->safe_psql('postgres', |
| 72 |
"DROP TABLE IF EXISTS temporal_no_key"); |
| 73 |
$node_subscriber->safe_psql('postgres', |
| 74 |
"DROP TABLE IF EXISTS temporal_pk"); |
| 75 |
$node_subscriber->safe_psql('postgres', |
| 76 |
"DROP TABLE IF EXISTS temporal_unique"); |
| 77 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 78 |
} |
| 79 |
|
| 80 |
# ################################# |
| 81 |
# Test with REPLICA IDENTITY DEFAULT: |
| 82 |
# ################################# |
| 83 |
|
| 84 |
create_tables(); |
| 85 |
|
| 86 |
# sync initial data: |
| 87 |
|
| 88 |
$node_publisher->safe_psql( |
| 89 |
'postgres', |
| 90 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 91 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 92 |
$node_publisher->safe_psql( |
| 93 |
'postgres', |
| 94 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 95 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 96 |
$node_publisher->safe_psql( |
| 97 |
'postgres', |
| 98 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 99 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 100 |
|
| 101 |
$node_publisher->safe_psql('postgres', |
| 102 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 103 |
$node_subscriber->safe_psql('postgres', |
| 104 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 105 |
); |
| 106 |
$node_subscriber->wait_for_subscription_sync; |
| 107 |
|
| 108 |
$result = $node_subscriber->safe_psql('postgres', |
| 109 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 110 |
is( $result, |
| 111 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 112 |
'synced temporal_no_key DEFAULT'); |
| 113 |
|
| 114 |
$result = $node_subscriber->safe_psql('postgres', |
| 115 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 116 |
is($result, qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 117 |
'synced temporal_pk DEFAULT'); |
| 118 |
|
| 119 |
$result = $node_subscriber->safe_psql('postgres', |
| 120 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 121 |
is( $result, |
| 122 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 123 |
'synced temporal_unique DEFAULT'); |
| 124 |
|
| 125 |
# replicate with no key: |
| 126 |
|
| 127 |
$node_publisher->safe_psql( |
| 128 |
'postgres', |
| 129 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 130 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 131 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 132 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 133 |
|
| 134 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 135 |
"UPDATE temporal_no_key SET a = 'b' WHERE id = '[2,3)'"); |
| 136 |
is( $stderr, |
| 137 |
qq(psql:<stdin>:1: ERROR: cannot update table "temporal_no_key" because it does not have a replica identity and publishes updates |
| 138 |
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 139 |
"can't UPDATE temporal_no_key DEFAULT"); |
| 140 |
|
| 141 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 142 |
"DELETE FROM temporal_no_key WHERE id = '[3,4)'"); |
| 143 |
is( $stderr, |
| 144 |
qq(psql:<stdin>:1: ERROR: cannot delete from table "temporal_no_key" because it does not have a replica identity and publishes deletes |
| 145 |
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 146 |
"can't DELETE temporal_no_key DEFAULT"); |
| 147 |
|
| 148 |
$node_publisher->wait_for_catchup('sub1'); |
| 149 |
|
| 150 |
$result = $node_subscriber->safe_psql('postgres', |
| 151 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 152 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 153 |
[2,3)|[2000-01-01,2010-01-01)|a |
| 154 |
[3,4)|[2000-01-01,2010-01-01)|a |
| 155 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_no_key DEFAULT'); |
| 156 |
|
| 157 |
# replicate with a primary key: |
| 158 |
|
| 159 |
$node_publisher->safe_psql( |
| 160 |
'postgres', |
| 161 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 162 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 163 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 164 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 165 |
|
| 166 |
$node_publisher->safe_psql('postgres', |
| 167 |
"UPDATE temporal_pk SET a = 'b' WHERE id = '[2,3)'"); |
| 168 |
|
| 169 |
$node_publisher->safe_psql('postgres', |
| 170 |
"DELETE FROM temporal_pk WHERE id = '[3,4)'"); |
| 171 |
|
| 172 |
$node_publisher->wait_for_catchup('sub1'); |
| 173 |
|
| 174 |
$result = $node_subscriber->safe_psql('postgres', |
| 175 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 176 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 177 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 178 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_pk DEFAULT'); |
| 179 |
|
| 180 |
# replicate with a unique key: |
| 181 |
|
| 182 |
$node_publisher->safe_psql( |
| 183 |
'postgres', |
| 184 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 185 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 186 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 187 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 188 |
|
| 189 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 190 |
"UPDATE temporal_unique SET a = 'b' WHERE id = '[2,3)'"); |
| 191 |
is( $stderr, |
| 192 |
qq(psql:<stdin>:1: ERROR: cannot update table "temporal_unique" because it does not have a replica identity and publishes updates |
| 193 |
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 194 |
"can't UPDATE temporal_unique DEFAULT"); |
| 195 |
|
| 196 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 197 |
"DELETE FROM temporal_unique WHERE id = '[3,4)'"); |
| 198 |
is( $stderr, |
| 199 |
qq(psql:<stdin>:1: ERROR: cannot delete from table "temporal_unique" because it does not have a replica identity and publishes deletes |
| 200 |
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 201 |
"can't DELETE temporal_unique DEFAULT"); |
| 202 |
|
| 203 |
$node_publisher->wait_for_catchup('sub1'); |
| 204 |
|
| 205 |
$result = $node_subscriber->safe_psql('postgres', |
| 206 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 207 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 208 |
[2,3)|[2000-01-01,2010-01-01)|a |
| 209 |
[3,4)|[2000-01-01,2010-01-01)|a |
| 210 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_unique DEFAULT'); |
| 211 |
|
| 212 |
# cleanup |
| 213 |
|
| 214 |
drop_everything(); |
| 215 |
|
| 216 |
|
| 217 |
# ################################# |
| 218 |
# Test with REPLICA IDENTITY FULL: |
| 219 |
# ################################# |
| 220 |
|
| 221 |
create_tables(); |
| 222 |
|
| 223 |
$node_publisher->safe_psql('postgres', |
| 224 |
"ALTER TABLE temporal_no_key REPLICA IDENTITY FULL"); |
| 225 |
|
| 226 |
$node_publisher->safe_psql('postgres', |
| 227 |
"ALTER TABLE temporal_pk REPLICA IDENTITY FULL"); |
| 228 |
|
| 229 |
$node_publisher->safe_psql('postgres', |
| 230 |
"ALTER TABLE temporal_unique REPLICA IDENTITY FULL"); |
| 231 |
|
| 232 |
$node_subscriber->safe_psql('postgres', |
| 233 |
"ALTER TABLE temporal_no_key REPLICA IDENTITY FULL"); |
| 234 |
|
| 235 |
$node_subscriber->safe_psql('postgres', |
| 236 |
"ALTER TABLE temporal_pk REPLICA IDENTITY FULL"); |
| 237 |
|
| 238 |
$node_subscriber->safe_psql('postgres', |
| 239 |
"ALTER TABLE temporal_unique REPLICA IDENTITY FULL"); |
| 240 |
|
| 241 |
# sync initial data: |
| 242 |
|
| 243 |
$node_publisher->safe_psql( |
| 244 |
'postgres', |
| 245 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 246 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 247 |
$node_publisher->safe_psql( |
| 248 |
'postgres', |
| 249 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 250 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 251 |
$node_publisher->safe_psql( |
| 252 |
'postgres', |
| 253 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 254 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 255 |
|
| 256 |
$node_publisher->safe_psql('postgres', |
| 257 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 258 |
$node_subscriber->safe_psql('postgres', |
| 259 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 260 |
); |
| 261 |
$node_subscriber->wait_for_subscription_sync; |
| 262 |
|
| 263 |
$result = $node_subscriber->safe_psql('postgres', |
| 264 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 265 |
is( $result, |
| 266 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 267 |
'synced temporal_no_key FULL'); |
| 268 |
|
| 269 |
$result = $node_subscriber->safe_psql('postgres', |
| 270 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 271 |
is($result, qq{[1,2)|[2000-01-01,2010-01-01)|a}, 'synced temporal_pk FULL'); |
| 272 |
|
| 273 |
$result = $node_subscriber->safe_psql('postgres', |
| 274 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 275 |
is( $result, |
| 276 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 277 |
'synced temporal_unique FULL'); |
| 278 |
|
| 279 |
# replicate with no key: |
| 280 |
|
| 281 |
$node_publisher->safe_psql( |
| 282 |
'postgres', |
| 283 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 284 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 285 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 286 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 287 |
|
| 288 |
$node_publisher->safe_psql('postgres', |
| 289 |
"UPDATE temporal_no_key SET a = 'b' WHERE id = '[2,3)'"); |
| 290 |
|
| 291 |
$node_publisher->safe_psql('postgres', |
| 292 |
"DELETE FROM temporal_no_key WHERE id = '[3,4)'"); |
| 293 |
|
| 294 |
$node_publisher->wait_for_catchup('sub1'); |
| 295 |
|
| 296 |
$result = $node_subscriber->safe_psql('postgres', |
| 297 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 298 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 299 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 300 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_no_key FULL'); |
| 301 |
|
| 302 |
# replicate with a primary key: |
| 303 |
|
| 304 |
$node_publisher->safe_psql( |
| 305 |
'postgres', |
| 306 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 307 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 308 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 309 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 310 |
|
| 311 |
$node_publisher->safe_psql('postgres', |
| 312 |
"UPDATE temporal_pk SET a = 'b' WHERE id = '[2,3)'"); |
| 313 |
|
| 314 |
$node_publisher->safe_psql('postgres', |
| 315 |
"DELETE FROM temporal_pk WHERE id = '[3,4)'"); |
| 316 |
|
| 317 |
$node_publisher->wait_for_catchup('sub1'); |
| 318 |
|
| 319 |
$result = $node_subscriber->safe_psql('postgres', |
| 320 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 321 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 322 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 323 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_pk FULL'); |
| 324 |
|
| 325 |
# replicate with a unique key: |
| 326 |
|
| 327 |
$node_publisher->safe_psql( |
| 328 |
'postgres', |
| 329 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 330 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 331 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 332 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 333 |
|
| 334 |
$node_publisher->safe_psql('postgres', |
| 335 |
"UPDATE temporal_unique SET a = 'b' WHERE id = '[2,3)'"); |
| 336 |
|
| 337 |
$node_publisher->safe_psql('postgres', |
| 338 |
"DELETE FROM temporal_unique WHERE id = '[3,4)'"); |
| 339 |
|
| 340 |
$node_publisher->wait_for_catchup('sub1'); |
| 341 |
|
| 342 |
$result = $node_subscriber->safe_psql('postgres', |
| 343 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 344 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 345 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 346 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_unique FULL'); |
| 347 |
|
| 348 |
# cleanup |
| 349 |
|
| 350 |
drop_everything(); |
| 351 |
|
| 352 |
|
| 353 |
# ################################# |
| 354 |
# Test with REPLICA IDENTITY USING INDEX |
| 355 |
# ################################# |
| 356 |
|
| 357 |
# create tables on publisher |
| 358 |
|
| 359 |
$node_publisher->safe_psql('postgres', |
| 360 |
"CREATE TABLE temporal_pk (id int4range, valid_at daterange, a text, PRIMARY KEY (id, valid_at WITHOUT OVERLAPS))" |
| 361 |
); |
| 362 |
$node_publisher->safe_psql('postgres', |
| 363 |
"ALTER TABLE temporal_pk REPLICA IDENTITY USING INDEX temporal_pk_pkey"); |
| 364 |
|
| 365 |
$node_publisher->safe_psql('postgres', |
| 366 |
"CREATE TABLE temporal_unique (id int4range NOT NULL, valid_at daterange NOT NULL, a text, UNIQUE (id, valid_at WITHOUT OVERLAPS))" |
| 367 |
); |
| 368 |
$node_publisher->safe_psql('postgres', |
| 369 |
"ALTER TABLE temporal_unique REPLICA IDENTITY USING INDEX temporal_unique_id_valid_at_key" |
| 370 |
); |
| 371 |
|
| 372 |
# create tables on subscriber |
| 373 |
|
| 374 |
$node_subscriber->safe_psql('postgres', |
| 375 |
"CREATE TABLE temporal_pk (id int4range, valid_at daterange, a text, PRIMARY KEY (id, valid_at WITHOUT OVERLAPS))" |
| 376 |
); |
| 377 |
$node_subscriber->safe_psql('postgres', |
| 378 |
"ALTER TABLE temporal_pk REPLICA IDENTITY USING INDEX temporal_pk_pkey"); |
| 379 |
|
| 380 |
$node_subscriber->safe_psql('postgres', |
| 381 |
"CREATE TABLE temporal_unique (id int4range NOT NULL, valid_at daterange NOT NULL, a text, UNIQUE (id, valid_at WITHOUT OVERLAPS))" |
| 382 |
); |
| 383 |
$node_subscriber->safe_psql('postgres', |
| 384 |
"ALTER TABLE temporal_unique REPLICA IDENTITY USING INDEX temporal_unique_id_valid_at_key" |
| 385 |
); |
| 386 |
|
| 387 |
# sync initial data: |
| 388 |
|
| 389 |
$node_publisher->safe_psql( |
| 390 |
'postgres', |
| 391 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 392 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 393 |
$node_publisher->safe_psql( |
| 394 |
'postgres', |
| 395 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 396 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 397 |
|
| 398 |
$node_publisher->safe_psql('postgres', |
| 399 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 400 |
$node_subscriber->safe_psql('postgres', |
| 401 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 402 |
); |
| 403 |
$node_subscriber->wait_for_subscription_sync; |
| 404 |
|
| 405 |
$result = $node_subscriber->safe_psql('postgres', |
| 406 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 407 |
is( $result, |
| 408 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 409 |
'synced temporal_pk USING INDEX'); |
| 410 |
|
| 411 |
$result = $node_subscriber->safe_psql('postgres', |
| 412 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 413 |
is( $result, |
| 414 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 415 |
'synced temporal_unique USING INDEX'); |
| 416 |
|
| 417 |
# replicate with a primary key: |
| 418 |
|
| 419 |
$node_publisher->safe_psql( |
| 420 |
'postgres', |
| 421 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 422 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 423 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 424 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 425 |
|
| 426 |
$node_publisher->safe_psql('postgres', |
| 427 |
"UPDATE temporal_pk SET a = 'b' WHERE id = '[2,3)'"); |
| 428 |
|
| 429 |
$node_publisher->safe_psql('postgres', |
| 430 |
"DELETE FROM temporal_pk WHERE id = '[3,4)'"); |
| 431 |
|
| 432 |
$node_publisher->wait_for_catchup('sub1'); |
| 433 |
|
| 434 |
$result = $node_subscriber->safe_psql('postgres', |
| 435 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 436 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 437 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 438 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_pk USING INDEX'); |
| 439 |
|
| 440 |
# replicate with a unique key: |
| 441 |
|
| 442 |
$node_publisher->safe_psql( |
| 443 |
'postgres', |
| 444 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 445 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 446 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 447 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 448 |
|
| 449 |
$node_publisher->safe_psql('postgres', |
| 450 |
"UPDATE temporal_unique SET a = 'b' WHERE id = '[2,3)'"); |
| 451 |
|
| 452 |
$node_publisher->safe_psql('postgres', |
| 453 |
"DELETE FROM temporal_unique WHERE id = '[3,4)'"); |
| 454 |
|
| 455 |
$node_publisher->wait_for_catchup('sub1'); |
| 456 |
|
| 457 |
$result = $node_subscriber->safe_psql('postgres', |
| 458 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 459 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 460 |
[2,3)|[2000-01-01,2010-01-01)|b |
| 461 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_unique USING INDEX'); |
| 462 |
|
| 463 |
# cleanup |
| 464 |
|
| 465 |
drop_everything(); |
| 466 |
|
| 467 |
|
| 468 |
# ################################# |
| 469 |
# Test with REPLICA IDENTITY NOTHING |
| 470 |
# ################################# |
| 471 |
|
| 472 |
create_tables(); |
| 473 |
|
| 474 |
$node_publisher->safe_psql('postgres', |
| 475 |
"ALTER TABLE temporal_no_key REPLICA IDENTITY NOTHING"); |
| 476 |
|
| 477 |
$node_publisher->safe_psql('postgres', |
| 478 |
"ALTER TABLE temporal_pk REPLICA IDENTITY NOTHING"); |
| 479 |
|
| 480 |
$node_publisher->safe_psql('postgres', |
| 481 |
"ALTER TABLE temporal_unique REPLICA IDENTITY NOTHING"); |
| 482 |
|
| 483 |
$node_subscriber->safe_psql('postgres', |
| 484 |
"ALTER TABLE temporal_no_key REPLICA IDENTITY NOTHING"); |
| 485 |
|
| 486 |
$node_subscriber->safe_psql('postgres', |
| 487 |
"ALTER TABLE temporal_pk REPLICA IDENTITY NOTHING"); |
| 488 |
|
| 489 |
$node_subscriber->safe_psql('postgres', |
| 490 |
"ALTER TABLE temporal_unique REPLICA IDENTITY NOTHING"); |
| 491 |
|
| 492 |
# sync initial data: |
| 493 |
|
| 494 |
$node_publisher->safe_psql( |
| 495 |
'postgres', |
| 496 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 497 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 498 |
$node_publisher->safe_psql( |
| 499 |
'postgres', |
| 500 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 501 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 502 |
$node_publisher->safe_psql( |
| 503 |
'postgres', |
| 504 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 505 |
VALUES ('[1,2)', '[2000-01-01,2010-01-01)', 'a')"); |
| 506 |
|
| 507 |
$node_publisher->safe_psql('postgres', |
| 508 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 509 |
$node_subscriber->safe_psql('postgres', |
| 510 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 511 |
); |
| 512 |
$node_subscriber->wait_for_subscription_sync; |
| 513 |
|
| 514 |
$result = $node_subscriber->safe_psql('postgres', |
| 515 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 516 |
is( $result, |
| 517 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 518 |
'synced temporal_no_key NOTHING'); |
| 519 |
|
| 520 |
$result = $node_subscriber->safe_psql('postgres', |
| 521 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 522 |
is($result, qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 523 |
'synced temporal_pk NOTHING'); |
| 524 |
|
| 525 |
$result = $node_subscriber->safe_psql('postgres', |
| 526 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 527 |
is( $result, |
| 528 |
qq{[1,2)|[2000-01-01,2010-01-01)|a}, |
| 529 |
'synced temporal_unique NOTHING'); |
| 530 |
|
| 531 |
# replicate with no key: |
| 532 |
|
| 533 |
$node_publisher->safe_psql( |
| 534 |
'postgres', |
| 535 |
"INSERT INTO temporal_no_key (id, valid_at, a) |
| 536 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 537 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 538 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 539 |
|
| 540 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 541 |
"UPDATE temporal_no_key SET a = 'b' WHERE id = '[2,3)'"); |
| 542 |
is( $stderr, |
| 543 |
qq(psql:<stdin>:1: ERROR: cannot update table "temporal_no_key" because it does not have a replica identity and publishes updates |
| 544 |
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 545 |
"can't UPDATE temporal_no_key NOTHING"); |
| 546 |
|
| 547 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 548 |
"DELETE FROM temporal_no_key WHERE id = '[3,4)'"); |
| 549 |
is( $stderr, |
| 550 |
qq(psql:<stdin>:1: ERROR: cannot delete from table "temporal_no_key" because it does not have a replica identity and publishes deletes |
| 551 |
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 552 |
"can't DELETE temporal_no_key NOTHING"); |
| 553 |
|
| 554 |
$node_publisher->wait_for_catchup('sub1'); |
| 555 |
|
| 556 |
$result = $node_subscriber->safe_psql('postgres', |
| 557 |
"SELECT * FROM temporal_no_key ORDER BY id, valid_at"); |
| 558 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 559 |
[2,3)|[2000-01-01,2010-01-01)|a |
| 560 |
[3,4)|[2000-01-01,2010-01-01)|a |
| 561 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_no_key NOTHING'); |
| 562 |
|
| 563 |
# replicate with a primary key: |
| 564 |
|
| 565 |
$node_publisher->safe_psql( |
| 566 |
'postgres', |
| 567 |
"INSERT INTO temporal_pk (id, valid_at, a) |
| 568 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 569 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 570 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 571 |
|
| 572 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 573 |
"UPDATE temporal_pk SET a = 'b' WHERE id = '[2,3)'"); |
| 574 |
is( $stderr, |
| 575 |
qq(psql:<stdin>:1: ERROR: cannot update table "temporal_pk" because it does not have a replica identity and publishes updates |
| 576 |
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 577 |
"can't UPDATE temporal_pk NOTHING"); |
| 578 |
|
| 579 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 580 |
"DELETE FROM temporal_pk WHERE id = '[3,4)'"); |
| 581 |
is( $stderr, |
| 582 |
qq(psql:<stdin>:1: ERROR: cannot delete from table "temporal_pk" because it does not have a replica identity and publishes deletes |
| 583 |
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 584 |
"can't DELETE temporal_pk NOTHING"); |
| 585 |
|
| 586 |
$node_publisher->wait_for_catchup('sub1'); |
| 587 |
|
| 588 |
$result = $node_subscriber->safe_psql('postgres', |
| 589 |
"SELECT * FROM temporal_pk ORDER BY id, valid_at"); |
| 590 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 591 |
[2,3)|[2000-01-01,2010-01-01)|a |
| 592 |
[3,4)|[2000-01-01,2010-01-01)|a |
| 593 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_pk NOTHING'); |
| 594 |
|
| 595 |
# replicate with a unique key: |
| 596 |
|
| 597 |
$node_publisher->safe_psql( |
| 598 |
'postgres', |
| 599 |
"INSERT INTO temporal_unique (id, valid_at, a) |
| 600 |
VALUES ('[2,3)', '[2000-01-01,2010-01-01)', 'a'), |
| 601 |
('[3,4)', '[2000-01-01,2010-01-01)', 'a'), |
| 602 |
('[4,5)', '[2000-01-01,2010-01-01)', 'a')"); |
| 603 |
|
| 604 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 605 |
"UPDATE temporal_unique SET a = 'b' WHERE id = '[2,3)'"); |
| 606 |
is( $stderr, |
| 607 |
qq(psql:<stdin>:1: ERROR: cannot update table "temporal_unique" because it does not have a replica identity and publishes updates |
| 608 |
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 609 |
"can't UPDATE temporal_unique NOTHING"); |
| 610 |
|
| 611 |
($result, $stdout, $stderr) = $node_publisher->psql('postgres', |
| 612 |
"DELETE FROM temporal_unique WHERE id = '[3,4)'"); |
| 613 |
is( $stderr, |
| 614 |
qq(psql:<stdin>:1: ERROR: cannot delete from table "temporal_unique" because it does not have a replica identity and publishes deletes |
| 615 |
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.), |
| 616 |
"can't DELETE temporal_unique NOTHING"); |
| 617 |
|
| 618 |
$node_publisher->wait_for_catchup('sub1'); |
| 619 |
|
| 620 |
$result = $node_subscriber->safe_psql('postgres', |
| 621 |
"SELECT * FROM temporal_unique ORDER BY id, valid_at"); |
| 622 |
is( $result, qq{[1,2)|[2000-01-01,2010-01-01)|a |
| 623 |
[2,3)|[2000-01-01,2010-01-01)|a |
| 624 |
[3,4)|[2000-01-01,2010-01-01)|a |
| 625 |
[4,5)|[2000-01-01,2010-01-01)|a}, 'replicated temporal_unique NOTHING'); |
| 626 |
|
| 627 |
# cleanup |
| 628 |
|
| 629 |
drop_everything(); |
| 630 |
|
| 631 |
done_testing(); |