| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests for subscription stats. |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Create publisher node. |
| 12 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 13 |
$node_publisher->init(allows_streaming => 'logical'); |
| 14 |
$node_publisher->start; |
| 15 |
|
| 16 |
# Create subscriber node. |
| 17 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 18 |
$node_subscriber->init; |
| 19 |
$node_subscriber->start; |
| 20 |
|
| 21 |
|
| 22 |
sub create_sub_pub_w_errors |
| 23 |
{ |
| 24 |
my ($node_publisher, $node_subscriber, $db, $table_name) = @_; |
| 25 |
# Initial table setup on both publisher and subscriber. On subscriber we |
| 26 |
# create the same tables but with primary keys. Also, insert some data that |
| 27 |
# will conflict with the data replicated from publisher later. |
| 28 |
$node_publisher->safe_psql( |
| 29 |
$db, |
| 30 |
qq[ |
| 31 |
BEGIN; |
| 32 |
CREATE TABLE $table_name(a int); |
| 33 |
ALTER TABLE $table_name REPLICA IDENTITY FULL; |
| 34 |
INSERT INTO $table_name VALUES (1); |
| 35 |
COMMIT; |
| 36 |
]); |
| 37 |
$node_subscriber->safe_psql( |
| 38 |
$db, |
| 39 |
qq[ |
| 40 |
BEGIN; |
| 41 |
CREATE TABLE $table_name(a int primary key); |
| 42 |
INSERT INTO $table_name VALUES (1); |
| 43 |
COMMIT; |
| 44 |
]); |
| 45 |
|
| 46 |
# Set up publication. |
| 47 |
my $pub_name = $table_name . '_pub'; |
| 48 |
my $publisher_connstr = $node_publisher->connstr . qq( dbname=$db); |
| 49 |
|
| 50 |
$node_publisher->safe_psql($db, |
| 51 |
qq(CREATE PUBLICATION $pub_name FOR TABLE $table_name)); |
| 52 |
|
| 53 |
# Create subscription. The tablesync for table on subscription will enter into |
| 54 |
# infinite error loop due to violating the unique constraint. |
| 55 |
my $sub_name = $table_name . '_sub'; |
| 56 |
$node_subscriber->safe_psql($db, |
| 57 |
qq(CREATE SUBSCRIPTION $sub_name CONNECTION '$publisher_connstr' PUBLICATION $pub_name) |
| 58 |
); |
| 59 |
|
| 60 |
$node_publisher->wait_for_catchup($sub_name); |
| 61 |
|
| 62 |
# Wait for the tablesync error to be reported. |
| 63 |
$node_subscriber->poll_query_until( |
| 64 |
$db, |
| 65 |
qq[ |
| 66 |
SELECT sync_error_count > 0 |
| 67 |
FROM pg_stat_subscription_stats |
| 68 |
WHERE subname = '$sub_name' |
| 69 |
]) |
| 70 |
or die |
| 71 |
qq(Timed out while waiting for tablesync errors for subscription '$sub_name'); |
| 72 |
|
| 73 |
# Truncate test_tab1 so that tablesync worker can continue. |
| 74 |
$node_subscriber->safe_psql($db, qq(TRUNCATE $table_name)); |
| 75 |
|
| 76 |
# Wait for initial tablesync to finish. |
| 77 |
$node_subscriber->poll_query_until( |
| 78 |
$db, |
| 79 |
qq[ |
| 80 |
SELECT count(1) = 1 FROM pg_subscription_rel |
| 81 |
WHERE srrelid = '$table_name'::regclass AND srsubstate in ('r', 's') |
| 82 |
]) |
| 83 |
or die |
| 84 |
qq(Timed out while waiting for subscriber to synchronize data for table '$table_name'.); |
| 85 |
|
| 86 |
# Check test table on the subscriber has one row. |
| 87 |
my $result = |
| 88 |
$node_subscriber->safe_psql($db, qq(SELECT a FROM $table_name)); |
| 89 |
is($result, qq(1), qq(Check that table '$table_name' now has 1 row.)); |
| 90 |
|
| 91 |
# Insert data to test table on the publisher, raising an error on the |
| 92 |
# subscriber due to violation of the unique constraint on test table. |
| 93 |
$node_publisher->safe_psql($db, qq(INSERT INTO $table_name VALUES (1))); |
| 94 |
|
| 95 |
# Wait for the subscriber to report both an apply error and an |
| 96 |
# insert_exists conflict. |
| 97 |
$node_subscriber->poll_query_until( |
| 98 |
$db, |
| 99 |
qq[ |
| 100 |
SELECT apply_error_count > 0 AND confl_insert_exists > 0 |
| 101 |
FROM pg_stat_subscription_stats |
| 102 |
WHERE subname = '$sub_name' |
| 103 |
]) |
| 104 |
or die |
| 105 |
qq(Timed out while waiting for apply error and insert_exists conflict for subscription '$sub_name'); |
| 106 |
|
| 107 |
# Truncate test table so that apply worker can continue. |
| 108 |
$node_subscriber->safe_psql($db, qq(TRUNCATE $table_name)); |
| 109 |
|
| 110 |
# Delete data from the test table on the publisher. This delete operation |
| 111 |
# should be skipped on the subscriber since the table is already empty. |
| 112 |
$node_publisher->safe_psql($db, qq(DELETE FROM $table_name;)); |
| 113 |
|
| 114 |
# Wait for the subscriber to report a delete_missing conflict. |
| 115 |
$node_subscriber->poll_query_until( |
| 116 |
$db, |
| 117 |
qq[ |
| 118 |
SELECT confl_delete_missing > 0 |
| 119 |
FROM pg_stat_subscription_stats |
| 120 |
WHERE subname = '$sub_name' |
| 121 |
]) |
| 122 |
or die |
| 123 |
qq(Timed out while waiting for delete_missing conflict for subscription '$sub_name'); |
| 124 |
|
| 125 |
return ($pub_name, $sub_name); |
| 126 |
} |
| 127 |
|
| 128 |
my $db = 'postgres'; |
| 129 |
|
| 130 |
# There shouldn't be any subscription errors before starting logical replication. |
| 131 |
my $result = $node_subscriber->safe_psql($db, |
| 132 |
qq(SELECT count(1) FROM pg_stat_subscription_stats)); |
| 133 |
is($result, qq(0), |
| 134 |
'Check that there are no subscription errors before starting logical replication.' |
| 135 |
); |
| 136 |
|
| 137 |
# Create the publication and subscription with sync and apply errors |
| 138 |
my $table1_name = 'test_tab1'; |
| 139 |
my ($pub1_name, $sub1_name) = |
| 140 |
create_sub_pub_w_errors($node_publisher, $node_subscriber, $db, |
| 141 |
$table1_name); |
| 142 |
|
| 143 |
# Apply errors, sync errors, and conflicts are > 0 and stats_reset timestamp is NULL |
| 144 |
is( $node_subscriber->safe_psql( |
| 145 |
$db, |
| 146 |
qq(SELECT apply_error_count > 0, |
| 147 |
sync_error_count > 0, |
| 148 |
confl_insert_exists > 0, |
| 149 |
confl_delete_missing > 0, |
| 150 |
stats_reset IS NULL |
| 151 |
FROM pg_stat_subscription_stats |
| 152 |
WHERE subname = '$sub1_name') |
| 153 |
), |
| 154 |
qq(t|t|t|t|t), |
| 155 |
qq(Check that apply errors, sync errors, and conflicts are > 0 and stats_reset is NULL for subscription '$sub1_name'.) |
| 156 |
); |
| 157 |
|
| 158 |
# Reset a single subscription |
| 159 |
$node_subscriber->safe_psql($db, |
| 160 |
qq(SELECT pg_stat_reset_subscription_stats((SELECT subid FROM pg_stat_subscription_stats WHERE subname = '$sub1_name'))) |
| 161 |
); |
| 162 |
|
| 163 |
# Apply errors, sync errors, and conflicts are 0 and stats_reset timestamp is not NULL |
| 164 |
is( $node_subscriber->safe_psql( |
| 165 |
$db, |
| 166 |
qq(SELECT apply_error_count = 0, |
| 167 |
sync_error_count = 0, |
| 168 |
confl_insert_exists = 0, |
| 169 |
confl_delete_missing = 0, |
| 170 |
stats_reset IS NOT NULL |
| 171 |
FROM pg_stat_subscription_stats |
| 172 |
WHERE subname = '$sub1_name') |
| 173 |
), |
| 174 |
qq(t|t|t|t|t), |
| 175 |
qq(Confirm that apply errors, sync errors, and conflicts are 0 and stats_reset is not NULL after reset for subscription '$sub1_name'.) |
| 176 |
); |
| 177 |
|
| 178 |
# Get reset timestamp |
| 179 |
my $reset_time1 = $node_subscriber->safe_psql($db, |
| 180 |
qq(SELECT stats_reset FROM pg_stat_subscription_stats WHERE subname = '$sub1_name') |
| 181 |
); |
| 182 |
|
| 183 |
# Reset single sub again |
| 184 |
$node_subscriber->safe_psql( |
| 185 |
$db, |
| 186 |
qq(SELECT pg_stat_reset_subscription_stats((SELECT subid FROM |
| 187 |
pg_stat_subscription_stats WHERE subname = '$sub1_name'))) |
| 188 |
); |
| 189 |
|
| 190 |
# check reset timestamp is newer after reset |
| 191 |
is( $node_subscriber->safe_psql( |
| 192 |
$db, |
| 193 |
qq(SELECT stats_reset > '$reset_time1'::timestamptz FROM |
| 194 |
pg_stat_subscription_stats WHERE subname = '$sub1_name') |
| 195 |
), |
| 196 |
qq(t), |
| 197 |
qq(Check reset timestamp for '$sub1_name' is newer after second reset.)); |
| 198 |
|
| 199 |
# Make second subscription and publication |
| 200 |
my $table2_name = 'test_tab2'; |
| 201 |
my ($pub2_name, $sub2_name) = |
| 202 |
create_sub_pub_w_errors($node_publisher, $node_subscriber, $db, |
| 203 |
$table2_name); |
| 204 |
|
| 205 |
# Apply errors, sync errors, and conflicts are > 0 and stats_reset timestamp is NULL |
| 206 |
is( $node_subscriber->safe_psql( |
| 207 |
$db, |
| 208 |
qq(SELECT apply_error_count > 0, |
| 209 |
sync_error_count > 0, |
| 210 |
confl_insert_exists > 0, |
| 211 |
confl_delete_missing > 0, |
| 212 |
stats_reset IS NULL |
| 213 |
FROM pg_stat_subscription_stats |
| 214 |
WHERE subname = '$sub2_name') |
| 215 |
), |
| 216 |
qq(t|t|t|t|t), |
| 217 |
qq(Confirm that apply errors, sync errors, and conflicts are > 0 and stats_reset is NULL for sub '$sub2_name'.) |
| 218 |
); |
| 219 |
|
| 220 |
# Reset all subscriptions |
| 221 |
$node_subscriber->safe_psql($db, |
| 222 |
qq(SELECT pg_stat_reset_subscription_stats(NULL))); |
| 223 |
|
| 224 |
# Apply errors, sync errors, and conflicts are 0 and stats_reset timestamp is not NULL |
| 225 |
is( $node_subscriber->safe_psql( |
| 226 |
$db, |
| 227 |
qq(SELECT apply_error_count = 0, |
| 228 |
sync_error_count = 0, |
| 229 |
confl_insert_exists = 0, |
| 230 |
confl_delete_missing = 0, |
| 231 |
stats_reset IS NOT NULL |
| 232 |
FROM pg_stat_subscription_stats |
| 233 |
WHERE subname = '$sub1_name') |
| 234 |
), |
| 235 |
qq(t|t|t|t|t), |
| 236 |
qq(Confirm that apply errors, sync errors, and conflicts are 0 and stats_reset is not NULL for sub '$sub1_name' after reset.) |
| 237 |
); |
| 238 |
|
| 239 |
is( $node_subscriber->safe_psql( |
| 240 |
$db, |
| 241 |
qq(SELECT apply_error_count = 0, |
| 242 |
sync_error_count = 0, |
| 243 |
confl_insert_exists = 0, |
| 244 |
confl_delete_missing = 0, |
| 245 |
stats_reset IS NOT NULL |
| 246 |
FROM pg_stat_subscription_stats |
| 247 |
WHERE subname = '$sub2_name') |
| 248 |
), |
| 249 |
qq(t|t|t|t|t), |
| 250 |
qq(Confirm that apply errors, sync errors, and conflicts are 0 and stats_reset is not NULL for sub '$sub2_name' after reset.) |
| 251 |
); |
| 252 |
|
| 253 |
$reset_time1 = $node_subscriber->safe_psql($db, |
| 254 |
qq(SELECT stats_reset FROM pg_stat_subscription_stats WHERE subname = '$sub1_name') |
| 255 |
); |
| 256 |
my $reset_time2 = $node_subscriber->safe_psql($db, |
| 257 |
qq(SELECT stats_reset FROM pg_stat_subscription_stats WHERE subname = '$sub2_name') |
| 258 |
); |
| 259 |
|
| 260 |
# Reset all subscriptions |
| 261 |
$node_subscriber->safe_psql($db, |
| 262 |
qq(SELECT pg_stat_reset_subscription_stats(NULL))); |
| 263 |
|
| 264 |
# check reset timestamp for sub1 is newer after reset |
| 265 |
is( $node_subscriber->safe_psql( |
| 266 |
$db, |
| 267 |
qq(SELECT stats_reset > '$reset_time1'::timestamptz FROM |
| 268 |
pg_stat_subscription_stats WHERE subname = '$sub1_name') |
| 269 |
), |
| 270 |
qq(t), |
| 271 |
qq(Confirm that reset timestamp for '$sub1_name' is newer after second reset.) |
| 272 |
); |
| 273 |
|
| 274 |
# check reset timestamp for sub2 is newer after reset |
| 275 |
is( $node_subscriber->safe_psql( |
| 276 |
$db, |
| 277 |
qq(SELECT stats_reset > '$reset_time2'::timestamptz FROM |
| 278 |
pg_stat_subscription_stats WHERE subname = '$sub2_name') |
| 279 |
), |
| 280 |
qq(t), |
| 281 |
qq(Confirm that reset timestamp for '$sub2_name' is newer after second reset.) |
| 282 |
); |
| 283 |
|
| 284 |
# Get subscription 1 oid |
| 285 |
my $sub1_oid = $node_subscriber->safe_psql($db, |
| 286 |
qq(SELECT oid FROM pg_subscription WHERE subname = '$sub1_name')); |
| 287 |
|
| 288 |
# Drop subscription 1 |
| 289 |
$node_subscriber->safe_psql($db, qq(DROP SUBSCRIPTION $sub1_name)); |
| 290 |
|
| 291 |
# Subscription stats for sub1 should be gone |
| 292 |
is( $node_subscriber->safe_psql( |
| 293 |
$db, qq(SELECT pg_stat_have_stats('subscription', 0, $sub1_oid))), |
| 294 |
qq(f), |
| 295 |
qq(Subscription stats for subscription '$sub1_name' should be removed.)); |
| 296 |
|
| 297 |
# Get subscription 2 oid |
| 298 |
my $sub2_oid = $node_subscriber->safe_psql($db, |
| 299 |
qq(SELECT oid FROM pg_subscription WHERE subname = '$sub2_name')); |
| 300 |
|
| 301 |
# Disassociate the subscription 2 from its replication slot and drop it |
| 302 |
$node_subscriber->safe_psql( |
| 303 |
$db, |
| 304 |
qq( |
| 305 |
ALTER SUBSCRIPTION $sub2_name DISABLE; |
| 306 |
ALTER SUBSCRIPTION $sub2_name SET (slot_name = NONE); |
| 307 |
DROP SUBSCRIPTION $sub2_name; |
| 308 |
)); |
| 309 |
|
| 310 |
# Subscription stats for sub2 should be gone |
| 311 |
is( $node_subscriber->safe_psql( |
| 312 |
$db, qq(SELECT pg_stat_have_stats('subscription', 0, $sub2_oid))), |
| 313 |
qq(f), |
| 314 |
qq(Subscription stats for subscription '$sub2_name' should be removed.)); |
| 315 |
|
| 316 |
# Since disabling subscription doesn't wait for walsender to release the replication |
| 317 |
# slot and exit, wait for the slot to become inactive. |
| 318 |
$node_publisher->poll_query_until($db, |
| 319 |
qq(SELECT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = '$sub2_name' AND active_pid IS NULL)) |
| 320 |
) or die "slot never became inactive"; |
| 321 |
|
| 322 |
$node_publisher->safe_psql($db, |
| 323 |
qq(SELECT pg_drop_replication_slot('$sub2_name'))); |
| 324 |
|
| 325 |
$node_subscriber->stop('fast'); |
| 326 |
$node_publisher->stop('fast'); |
| 327 |
|
| 328 |
done_testing(); |