| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test TRUNCATE |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# setup |
| 12 |
|
| 13 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 14 |
$node_publisher->init(allows_streaming => 'logical'); |
| 15 |
$node_publisher->start; |
| 16 |
|
| 17 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 18 |
$node_subscriber->init; |
| 19 |
$node_subscriber->append_conf('postgresql.conf', |
| 20 |
qq(max_logical_replication_workers = 6)); |
| 21 |
$node_subscriber->start; |
| 22 |
|
| 23 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 24 |
|
| 25 |
$node_publisher->safe_psql('postgres', |
| 26 |
"CREATE TABLE tab1 (a int PRIMARY KEY)"); |
| 27 |
|
| 28 |
$node_subscriber->safe_psql('postgres', |
| 29 |
"CREATE TABLE tab1 (a int PRIMARY KEY)"); |
| 30 |
|
| 31 |
$node_publisher->safe_psql('postgres', |
| 32 |
"CREATE TABLE tab2 (a int PRIMARY KEY)"); |
| 33 |
|
| 34 |
$node_subscriber->safe_psql('postgres', |
| 35 |
"CREATE TABLE tab2 (a int PRIMARY KEY)"); |
| 36 |
|
| 37 |
$node_publisher->safe_psql('postgres', |
| 38 |
"CREATE TABLE tab3 (a int PRIMARY KEY)"); |
| 39 |
|
| 40 |
$node_subscriber->safe_psql('postgres', |
| 41 |
"CREATE TABLE tab3 (a int PRIMARY KEY)"); |
| 42 |
|
| 43 |
$node_publisher->safe_psql('postgres', |
| 44 |
"CREATE TABLE tab4 (x int PRIMARY KEY, y int REFERENCES tab3)"); |
| 45 |
|
| 46 |
$node_subscriber->safe_psql('postgres', |
| 47 |
"CREATE TABLE tab4 (x int PRIMARY KEY, y int REFERENCES tab3)"); |
| 48 |
|
| 49 |
$node_subscriber->safe_psql('postgres', |
| 50 |
"CREATE SEQUENCE seq1 OWNED BY tab1.a"); |
| 51 |
$node_subscriber->safe_psql('postgres', "ALTER SEQUENCE seq1 START 101"); |
| 52 |
|
| 53 |
$node_publisher->safe_psql('postgres', |
| 54 |
"CREATE PUBLICATION pub1 FOR TABLE tab1"); |
| 55 |
$node_publisher->safe_psql('postgres', |
| 56 |
"CREATE PUBLICATION pub2 FOR TABLE tab2 WITH (publish = insert)"); |
| 57 |
$node_publisher->safe_psql('postgres', |
| 58 |
"CREATE PUBLICATION pub3 FOR TABLE tab3, tab4"); |
| 59 |
$node_subscriber->safe_psql('postgres', |
| 60 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 61 |
); |
| 62 |
$node_subscriber->safe_psql('postgres', |
| 63 |
"CREATE SUBSCRIPTION sub2 CONNECTION '$publisher_connstr' PUBLICATION pub2" |
| 64 |
); |
| 65 |
$node_subscriber->safe_psql('postgres', |
| 66 |
"CREATE SUBSCRIPTION sub3 CONNECTION '$publisher_connstr' PUBLICATION pub3" |
| 67 |
); |
| 68 |
|
| 69 |
# Wait for initial sync of all subscriptions |
| 70 |
$node_subscriber->wait_for_subscription_sync; |
| 71 |
|
| 72 |
# insert data to truncate |
| 73 |
|
| 74 |
$node_subscriber->safe_psql('postgres', |
| 75 |
"INSERT INTO tab1 VALUES (1), (2), (3)"); |
| 76 |
|
| 77 |
$node_publisher->wait_for_catchup('sub1'); |
| 78 |
|
| 79 |
# truncate and check |
| 80 |
|
| 81 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab1"); |
| 82 |
|
| 83 |
$node_publisher->wait_for_catchup('sub1'); |
| 84 |
|
| 85 |
my $result = $node_subscriber->safe_psql('postgres', |
| 86 |
"SELECT count(*), min(a), max(a) FROM tab1"); |
| 87 |
is($result, qq(0||), 'truncate replicated'); |
| 88 |
|
| 89 |
$result = $node_subscriber->safe_psql('postgres', "SELECT nextval('seq1')"); |
| 90 |
is($result, qq(1), 'sequence not restarted'); |
| 91 |
|
| 92 |
# truncate with restart identity |
| 93 |
|
| 94 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab1 RESTART IDENTITY"); |
| 95 |
|
| 96 |
$node_publisher->wait_for_catchup('sub1'); |
| 97 |
|
| 98 |
$result = $node_subscriber->safe_psql('postgres', "SELECT nextval('seq1')"); |
| 99 |
is($result, qq(101), 'truncate restarted identities'); |
| 100 |
|
| 101 |
# test publication that does not replicate truncate |
| 102 |
|
| 103 |
$node_subscriber->safe_psql('postgres', |
| 104 |
"INSERT INTO tab2 VALUES (1), (2), (3)"); |
| 105 |
|
| 106 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab2"); |
| 107 |
|
| 108 |
$node_publisher->wait_for_catchup('sub2'); |
| 109 |
|
| 110 |
$result = $node_subscriber->safe_psql('postgres', |
| 111 |
"SELECT count(*), min(a), max(a) FROM tab2"); |
| 112 |
is($result, qq(3|1|3), 'truncate not replicated'); |
| 113 |
|
| 114 |
$node_publisher->safe_psql('postgres', |
| 115 |
"ALTER PUBLICATION pub2 SET (publish = 'insert, truncate')"); |
| 116 |
|
| 117 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab2"); |
| 118 |
|
| 119 |
$node_publisher->wait_for_catchup('sub2'); |
| 120 |
|
| 121 |
$result = $node_subscriber->safe_psql('postgres', |
| 122 |
"SELECT count(*), min(a), max(a) FROM tab2"); |
| 123 |
is($result, qq(0||), 'truncate replicated after publication change'); |
| 124 |
|
| 125 |
# test multiple tables connected by foreign keys |
| 126 |
|
| 127 |
$node_subscriber->safe_psql('postgres', |
| 128 |
"INSERT INTO tab3 VALUES (1), (2), (3)"); |
| 129 |
$node_subscriber->safe_psql('postgres', |
| 130 |
"INSERT INTO tab4 VALUES (11, 1), (111, 1), (22, 2)"); |
| 131 |
|
| 132 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab3, tab4"); |
| 133 |
|
| 134 |
$node_publisher->wait_for_catchup('sub3'); |
| 135 |
|
| 136 |
$result = $node_subscriber->safe_psql('postgres', |
| 137 |
"SELECT count(*), min(a), max(a) FROM tab3"); |
| 138 |
is($result, qq(0||), 'truncate of multiple tables replicated'); |
| 139 |
$result = $node_subscriber->safe_psql('postgres', |
| 140 |
"SELECT count(*), min(x), max(x) FROM tab4"); |
| 141 |
is($result, qq(0||), 'truncate of multiple tables replicated'); |
| 142 |
|
| 143 |
# test truncate of multiple tables, some of which are not published |
| 144 |
|
| 145 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub2"); |
| 146 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub2"); |
| 147 |
|
| 148 |
$node_subscriber->safe_psql('postgres', |
| 149 |
"INSERT INTO tab1 VALUES (1), (2), (3)"); |
| 150 |
$node_subscriber->safe_psql('postgres', |
| 151 |
"INSERT INTO tab2 VALUES (1), (2), (3)"); |
| 152 |
|
| 153 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab1, tab2"); |
| 154 |
|
| 155 |
$node_publisher->wait_for_catchup('sub1'); |
| 156 |
|
| 157 |
$result = $node_subscriber->safe_psql('postgres', |
| 158 |
"SELECT count(*), min(a), max(a) FROM tab1"); |
| 159 |
is($result, qq(0||), 'truncate of multiple tables some not published'); |
| 160 |
$result = $node_subscriber->safe_psql('postgres', |
| 161 |
"SELECT count(*), min(a), max(a) FROM tab2"); |
| 162 |
is($result, qq(3|1|3), 'truncate of multiple tables some not published'); |
| 163 |
|
| 164 |
# Test that truncate works for synchronous logical replication |
| 165 |
|
| 166 |
$node_publisher->safe_psql('postgres', |
| 167 |
"ALTER SYSTEM SET synchronous_standby_names TO 'sub1'"); |
| 168 |
$node_publisher->safe_psql('postgres', "SELECT pg_reload_conf()"); |
| 169 |
|
| 170 |
# insert data to truncate |
| 171 |
|
| 172 |
$node_publisher->safe_psql('postgres', |
| 173 |
"INSERT INTO tab1 VALUES (1), (2), (3)"); |
| 174 |
|
| 175 |
$node_publisher->wait_for_catchup('sub1'); |
| 176 |
|
| 177 |
$result = $node_subscriber->safe_psql('postgres', |
| 178 |
"SELECT count(*), min(a), max(a) FROM tab1"); |
| 179 |
is($result, qq(3|1|3), 'check synchronous logical replication'); |
| 180 |
|
| 181 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab1"); |
| 182 |
|
| 183 |
$node_publisher->wait_for_catchup('sub1'); |
| 184 |
|
| 185 |
$result = $node_subscriber->safe_psql('postgres', |
| 186 |
"SELECT count(*), min(a), max(a) FROM tab1"); |
| 187 |
is($result, qq(0||), |
| 188 |
'truncate replicated in synchronous logical replication'); |
| 189 |
|
| 190 |
$node_publisher->safe_psql('postgres', |
| 191 |
"ALTER SYSTEM RESET synchronous_standby_names"); |
| 192 |
$node_publisher->safe_psql('postgres', "SELECT pg_reload_conf()"); |
| 193 |
|
| 194 |
# test that truncate works for logical replication when there are multiple |
| 195 |
# subscriptions for a single table |
| 196 |
|
| 197 |
$node_publisher->safe_psql('postgres', "CREATE TABLE tab5 (a int)"); |
| 198 |
|
| 199 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab5 (a int)"); |
| 200 |
|
| 201 |
$node_publisher->safe_psql('postgres', |
| 202 |
"CREATE PUBLICATION pub5 FOR TABLE tab5"); |
| 203 |
$node_subscriber->safe_psql('postgres', |
| 204 |
"CREATE SUBSCRIPTION sub5_1 CONNECTION '$publisher_connstr' PUBLICATION pub5" |
| 205 |
); |
| 206 |
$node_subscriber->safe_psql('postgres', |
| 207 |
"CREATE SUBSCRIPTION sub5_2 CONNECTION '$publisher_connstr' PUBLICATION pub5" |
| 208 |
); |
| 209 |
|
| 210 |
# wait for initial data sync |
| 211 |
$node_subscriber->wait_for_subscription_sync; |
| 212 |
|
| 213 |
# insert data to truncate |
| 214 |
|
| 215 |
$node_publisher->safe_psql('postgres', |
| 216 |
"INSERT INTO tab5 VALUES (1), (2), (3)"); |
| 217 |
|
| 218 |
$node_publisher->wait_for_catchup('sub5_1'); |
| 219 |
$node_publisher->wait_for_catchup('sub5_2'); |
| 220 |
|
| 221 |
$result = $node_subscriber->safe_psql('postgres', |
| 222 |
"SELECT count(*), min(a), max(a) FROM tab5"); |
| 223 |
is($result, qq(6|1|3), 'insert replicated for multiple subscriptions'); |
| 224 |
|
| 225 |
$node_publisher->safe_psql('postgres', "TRUNCATE tab5"); |
| 226 |
|
| 227 |
$node_publisher->wait_for_catchup('sub5_1'); |
| 228 |
$node_publisher->wait_for_catchup('sub5_2'); |
| 229 |
|
| 230 |
$result = $node_subscriber->safe_psql('postgres', |
| 231 |
"SELECT count(*), min(a), max(a) FROM tab5"); |
| 232 |
is($result, qq(0||), 'truncate replicated for multiple subscriptions'); |
| 233 |
|
| 234 |
# check deadlocks |
| 235 |
$result = $node_subscriber->safe_psql('postgres', |
| 236 |
"SELECT deadlocks FROM pg_stat_database WHERE datname='postgres'"); |
| 237 |
is($result, qq(0), 'no deadlocks detected'); |
| 238 |
|
| 239 |
done_testing(); |