| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test generated columns |
| 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->start; |
| 20 |
|
| 21 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 22 |
|
| 23 |
$node_publisher->safe_psql('postgres', |
| 24 |
"CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED, c int GENERATED ALWAYS AS (a * 3) VIRTUAL)" |
| 25 |
); |
| 26 |
|
| 27 |
$node_subscriber->safe_psql('postgres', |
| 28 |
"CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 22) STORED, c int GENERATED ALWAYS AS (a * 33) VIRTUAL, d int)" |
| 29 |
); |
| 30 |
|
| 31 |
# data for initial sync |
| 32 |
|
| 33 |
$node_publisher->safe_psql('postgres', |
| 34 |
"INSERT INTO tab1 (a) VALUES (1), (2), (3)"); |
| 35 |
|
| 36 |
$node_publisher->safe_psql('postgres', |
| 37 |
"CREATE PUBLICATION pub1 FOR ALL TABLES"); |
| 38 |
$node_subscriber->safe_psql('postgres', |
| 39 |
"CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1" |
| 40 |
); |
| 41 |
|
| 42 |
# Wait for initial sync of all subscriptions |
| 43 |
$node_subscriber->wait_for_subscription_sync; |
| 44 |
|
| 45 |
my $result = |
| 46 |
$node_subscriber->safe_psql('postgres', "SELECT a, b, c FROM tab1"); |
| 47 |
is( $result, qq(1|22|33 |
| 48 |
2|44|66 |
| 49 |
3|66|99), 'generated columns initial sync'); |
| 50 |
|
| 51 |
# data to replicate |
| 52 |
|
| 53 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (4), (5)"); |
| 54 |
|
| 55 |
$node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 5"); |
| 56 |
|
| 57 |
$node_publisher->wait_for_catchup('sub1'); |
| 58 |
|
| 59 |
$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1"); |
| 60 |
is( $result, qq(1|22|33| |
| 61 |
2|44|66| |
| 62 |
3|66|99| |
| 63 |
4|88|132| |
| 64 |
6|132|198|), 'generated columns replicated'); |
| 65 |
|
| 66 |
# try it with a subscriber-side trigger |
| 67 |
|
| 68 |
$node_subscriber->safe_psql( |
| 69 |
'postgres', q{ |
| 70 |
CREATE FUNCTION tab1_trigger_func() RETURNS trigger |
| 71 |
LANGUAGE plpgsql AS $$ |
| 72 |
BEGIN |
| 73 |
NEW.d := NEW.a + 10; |
| 74 |
RETURN NEW; |
| 75 |
END $$; |
| 76 |
|
| 77 |
CREATE TRIGGER test1 BEFORE INSERT OR UPDATE ON tab1 |
| 78 |
FOR EACH ROW |
| 79 |
EXECUTE PROCEDURE tab1_trigger_func(); |
| 80 |
|
| 81 |
ALTER TABLE tab1 ENABLE REPLICA TRIGGER test1; |
| 82 |
}); |
| 83 |
|
| 84 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (7), (8)"); |
| 85 |
|
| 86 |
$node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 9 WHERE a = 7"); |
| 87 |
|
| 88 |
$node_publisher->wait_for_catchup('sub1'); |
| 89 |
|
| 90 |
$result = |
| 91 |
$node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY 1"); |
| 92 |
is( $result, qq(1|22|33| |
| 93 |
2|44|66| |
| 94 |
3|66|99| |
| 95 |
4|88|132| |
| 96 |
6|132|198| |
| 97 |
8|176|264|18 |
| 98 |
9|198|297|19), 'generated columns replicated with trigger'); |
| 99 |
|
| 100 |
# cleanup |
| 101 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 102 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 103 |
|
| 104 |
# ============================================================================= |
| 105 |
# Exercise logical replication of a generated column to a subscriber side |
| 106 |
# regular column. This is done both when the publication parameter |
| 107 |
# 'publish_generated_columns' is set to 'none' (to confirm existing default |
| 108 |
# behavior), and is set to 'stored' (to confirm replication occurs). |
| 109 |
# |
| 110 |
# The test environment is set up as follows: |
| 111 |
# |
| 112 |
# - Publication pub1 on the 'postgres' database. |
| 113 |
# pub1 has publish_generated_columns as 'none'. |
| 114 |
# |
| 115 |
# - Publication pub2 on the 'postgres' database. |
| 116 |
# pub2 has publish_generated_columns as 'stored'. |
| 117 |
# |
| 118 |
# - Subscription sub1 on the 'postgres' database for publication pub1. |
| 119 |
# |
| 120 |
# - Subscription sub2 on the 'test_pgc_true' database for publication pub2. |
| 121 |
# ============================================================================= |
| 122 |
|
| 123 |
$node_subscriber->safe_psql('postgres', "CREATE DATABASE test_pgc_true"); |
| 124 |
|
| 125 |
# -------------------------------------------------- |
| 126 |
# Test Case: Generated to regular column replication |
| 127 |
# Publisher table has generated column 'b'. |
| 128 |
# Subscriber table has regular column 'b'. |
| 129 |
# -------------------------------------------------- |
| 130 |
|
| 131 |
# Create table and publications. Insert data to verify initial sync. |
| 132 |
$node_publisher->safe_psql( |
| 133 |
'postgres', qq( |
| 134 |
CREATE TABLE tab_gen_to_nogen (a int, b int GENERATED ALWAYS AS (a * 2) STORED); |
| 135 |
INSERT INTO tab_gen_to_nogen (a) VALUES (1), (2), (3); |
| 136 |
CREATE PUBLICATION regress_pub1_gen_to_nogen FOR TABLE tab_gen_to_nogen WITH (publish_generated_columns = none); |
| 137 |
CREATE PUBLICATION regress_pub2_gen_to_nogen FOR TABLE tab_gen_to_nogen WITH (publish_generated_columns = stored); |
| 138 |
)); |
| 139 |
|
| 140 |
# Create the table and subscription in the 'postgres' database. |
| 141 |
$node_subscriber->safe_psql( |
| 142 |
'postgres', qq( |
| 143 |
CREATE TABLE tab_gen_to_nogen (a int, b int); |
| 144 |
CREATE SUBSCRIPTION regress_sub1_gen_to_nogen CONNECTION '$publisher_connstr' PUBLICATION regress_pub1_gen_to_nogen WITH (copy_data = true); |
| 145 |
)); |
| 146 |
|
| 147 |
# Create the table and subscription in the 'test_pgc_true' database. |
| 148 |
$node_subscriber->safe_psql( |
| 149 |
'test_pgc_true', qq( |
| 150 |
CREATE TABLE tab_gen_to_nogen (a int, b int); |
| 151 |
CREATE SUBSCRIPTION regress_sub2_gen_to_nogen CONNECTION '$publisher_connstr' PUBLICATION regress_pub2_gen_to_nogen WITH (copy_data = true); |
| 152 |
)); |
| 153 |
|
| 154 |
# Wait for the initial synchronization of both subscriptions. |
| 155 |
$node_subscriber->wait_for_subscription_sync($node_publisher, |
| 156 |
'regress_sub1_gen_to_nogen', 'postgres'); |
| 157 |
$node_subscriber->wait_for_subscription_sync($node_publisher, |
| 158 |
'regress_sub2_gen_to_nogen', 'test_pgc_true'); |
| 159 |
|
| 160 |
# Verify that generated column data is not copied during the initial |
| 161 |
# synchronization when publish_generated_columns is set to 'none'. |
| 162 |
$result = $node_subscriber->safe_psql('postgres', |
| 163 |
"SELECT a, b FROM tab_gen_to_nogen ORDER BY a"); |
| 164 |
is( $result, qq(1| |
| 165 |
2| |
| 166 |
3|), 'tab_gen_to_nogen initial sync, when publish_generated_columns=none'); |
| 167 |
|
| 168 |
# Verify that generated column data is copied during the initial synchronization |
| 169 |
# when publish_generated_columns is set to 'stored'. |
| 170 |
$result = $node_subscriber->safe_psql('test_pgc_true', |
| 171 |
"SELECT a, b FROM tab_gen_to_nogen ORDER BY a"); |
| 172 |
is( $result, qq(1|2 |
| 173 |
2|4 |
| 174 |
3|6), |
| 175 |
'tab_gen_to_nogen initial sync, when publish_generated_columns=stored'); |
| 176 |
|
| 177 |
# Insert data to verify incremental replication. |
| 178 |
$node_publisher->safe_psql('postgres', |
| 179 |
"INSERT INTO tab_gen_to_nogen VALUES (4), (5)"); |
| 180 |
|
| 181 |
# Verify that the generated column data is not replicated during incremental |
| 182 |
# replication when publish_generated_columns is set to 'none'. |
| 183 |
$node_publisher->wait_for_catchup('regress_sub1_gen_to_nogen'); |
| 184 |
$result = $node_subscriber->safe_psql('postgres', |
| 185 |
"SELECT a, b FROM tab_gen_to_nogen ORDER BY a"); |
| 186 |
is( $result, qq(1| |
| 187 |
2| |
| 188 |
3| |
| 189 |
4| |
| 190 |
5|), |
| 191 |
'tab_gen_to_nogen incremental replication, when publish_generated_columns=none' |
| 192 |
); |
| 193 |
|
| 194 |
# Verify that generated column data is replicated during incremental |
| 195 |
# synchronization when publish_generated_columns is set to 'stored'. |
| 196 |
$node_publisher->wait_for_catchup('regress_sub2_gen_to_nogen'); |
| 197 |
$result = $node_subscriber->safe_psql('test_pgc_true', |
| 198 |
"SELECT a, b FROM tab_gen_to_nogen ORDER BY a"); |
| 199 |
is( $result, qq(1|2 |
| 200 |
2|4 |
| 201 |
3|6 |
| 202 |
4|8 |
| 203 |
5|10), |
| 204 |
'tab_gen_to_nogen incremental replication, when publish_generated_columns=stored' |
| 205 |
); |
| 206 |
|
| 207 |
# cleanup |
| 208 |
$node_subscriber->safe_psql('postgres', |
| 209 |
"DROP SUBSCRIPTION regress_sub1_gen_to_nogen"); |
| 210 |
$node_subscriber->safe_psql('test_pgc_true', |
| 211 |
"DROP SUBSCRIPTION regress_sub2_gen_to_nogen"); |
| 212 |
$node_publisher->safe_psql( |
| 213 |
'postgres', qq( |
| 214 |
DROP PUBLICATION regress_pub1_gen_to_nogen; |
| 215 |
DROP PUBLICATION regress_pub2_gen_to_nogen; |
| 216 |
)); |
| 217 |
$node_subscriber->safe_psql('test_pgc_true', "DROP table tab_gen_to_nogen"); |
| 218 |
$node_subscriber->safe_psql('postgres', "DROP DATABASE test_pgc_true"); |
| 219 |
|
| 220 |
# ============================================================================= |
| 221 |
# The following test cases demonstrate how publication column lists interact |
| 222 |
# with the publication parameter 'publish_generated_columns'. |
| 223 |
# |
| 224 |
# Test: Column lists take precedence, so generated columns in a column list |
| 225 |
# will be replicated even when publish_generated_columns is 'none'. |
| 226 |
# |
| 227 |
# Test: When there is a column list, only those generated columns named in the |
| 228 |
# column list will be replicated even when publish_generated_columns is |
| 229 |
# 'stored'. |
| 230 |
# ============================================================================= |
| 231 |
|
| 232 |
# -------------------------------------------------- |
| 233 |
# Test Case: Publisher replicates the column list, including generated columns, |
| 234 |
# even when the publish_generated_columns option is set to 'none'. |
| 235 |
# -------------------------------------------------- |
| 236 |
|
| 237 |
# Create table and publication. Insert data to verify initial sync. |
| 238 |
$node_publisher->safe_psql( |
| 239 |
'postgres', qq( |
| 240 |
CREATE TABLE tab2 (a int, gen1 int GENERATED ALWAYS AS (a * 2) STORED); |
| 241 |
INSERT INTO tab2 (a) VALUES (1), (2); |
| 242 |
CREATE PUBLICATION pub1 FOR table tab2(gen1) WITH (publish_generated_columns=none); |
| 243 |
)); |
| 244 |
|
| 245 |
# Create table and subscription. |
| 246 |
$node_subscriber->safe_psql( |
| 247 |
'postgres', qq( |
| 248 |
CREATE TABLE tab2 (a int, gen1 int); |
| 249 |
CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1 WITH (copy_data = true); |
| 250 |
)); |
| 251 |
|
| 252 |
# Wait for initial sync. |
| 253 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub1'); |
| 254 |
|
| 255 |
# Initial sync test when publish_generated_columns is 'none'. |
| 256 |
# Verify 'gen1' is replicated regardless of the 'none' parameter value. |
| 257 |
$result = |
| 258 |
$node_subscriber->safe_psql('postgres', "SELECT * FROM tab2 ORDER BY gen1"); |
| 259 |
is( $result, qq(|2 |
| 260 |
|4), |
| 261 |
'tab2 initial sync, when publish_generated_columns=none'); |
| 262 |
|
| 263 |
# Insert data to verify incremental replication. |
| 264 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab2 VALUES (3), (4)"); |
| 265 |
|
| 266 |
# Incremental replication test when publish_generated_columns is 'none'. |
| 267 |
# Verify 'gen1' is replicated regardless of the 'none' parameter value. |
| 268 |
$node_publisher->wait_for_catchup('sub1'); |
| 269 |
$result = |
| 270 |
$node_subscriber->safe_psql('postgres', "SELECT * FROM tab2 ORDER BY gen1"); |
| 271 |
is( $result, qq(|2 |
| 272 |
|4 |
| 273 |
|6 |
| 274 |
|8), |
| 275 |
'tab2 incremental replication, when publish_generated_columns=none'); |
| 276 |
|
| 277 |
# cleanup |
| 278 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 279 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 280 |
|
| 281 |
# -------------------------------------------------- |
| 282 |
# Test Case: Even when publish_generated_columns is set to 'stored', the |
| 283 |
# publisher only publishes the data of columns specified in the column list, |
| 284 |
# skipping other generated and non-generated columns. |
| 285 |
# -------------------------------------------------- |
| 286 |
|
| 287 |
# Create table and publication. Insert data to verify initial sync. |
| 288 |
$node_publisher->safe_psql( |
| 289 |
'postgres', qq( |
| 290 |
CREATE TABLE tab3 (a int, gen1 int GENERATED ALWAYS AS (a * 2) STORED, gen2 int GENERATED ALWAYS AS (a * 2) STORED); |
| 291 |
INSERT INTO tab3 (a) VALUES (1), (2); |
| 292 |
CREATE PUBLICATION pub1 FOR table tab3(gen1) WITH (publish_generated_columns=stored); |
| 293 |
)); |
| 294 |
|
| 295 |
# Create table and subscription. |
| 296 |
$node_subscriber->safe_psql( |
| 297 |
'postgres', qq( |
| 298 |
CREATE TABLE tab3 (a int, gen1 int, gen2 int); |
| 299 |
CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1 WITH (copy_data = true); |
| 300 |
)); |
| 301 |
|
| 302 |
# Wait for initial sync. |
| 303 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'sub1'); |
| 304 |
|
| 305 |
# Initial sync test when publish_generated_columns is 'stored'. |
| 306 |
# Verify only 'gen1' is replicated regardless of the 'stored' parameter value. |
| 307 |
$result = |
| 308 |
$node_subscriber->safe_psql('postgres', "SELECT * FROM tab3 ORDER BY gen1"); |
| 309 |
is( $result, qq(|2| |
| 310 |
|4|), |
| 311 |
'tab3 initial sync, when publish_generated_columns=stored'); |
| 312 |
|
| 313 |
# Insert data to verify incremental replication. |
| 314 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab3 VALUES (3), (4)"); |
| 315 |
|
| 316 |
# Incremental replication test when publish_generated_columns is 'stored'. |
| 317 |
# Verify only 'gen1' is replicated regardless of the 'stored' parameter value. |
| 318 |
$node_publisher->wait_for_catchup('sub1'); |
| 319 |
$result = |
| 320 |
$node_subscriber->safe_psql('postgres', "SELECT * FROM tab3 ORDER BY gen1"); |
| 321 |
is( $result, qq(|2| |
| 322 |
|4| |
| 323 |
|6| |
| 324 |
|8|), |
| 325 |
'tab3 incremental replication, when publish_generated_columns=stored'); |
| 326 |
|
| 327 |
# cleanup |
| 328 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 329 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 330 |
|
| 331 |
# ============================================================================= |
| 332 |
# The following test verifies the expected error when replicating to a |
| 333 |
# generated subscriber column. Test the following combinations: |
| 334 |
# - regular -> generated |
| 335 |
# - generated -> generated |
| 336 |
# ============================================================================= |
| 337 |
|
| 338 |
# -------------------------------------------------- |
| 339 |
# A "regular -> generated" or "generated -> generated" replication fails, |
| 340 |
# reporting an error that the generated column on the subscriber side cannot |
| 341 |
# be replicated. |
| 342 |
# |
| 343 |
# Test Case: regular -> generated and generated -> generated |
| 344 |
# Publisher table has regular column 'c2' and generated column 'c3'. |
| 345 |
# Subscriber table has generated columns 'c2' and 'c3'. |
| 346 |
# -------------------------------------------------- |
| 347 |
|
| 348 |
# Create table and publication. Insert data into the table. |
| 349 |
$node_publisher->safe_psql( |
| 350 |
'postgres', qq( |
| 351 |
CREATE TABLE t1(c1 int, c2 int, c3 int GENERATED ALWAYS AS (c1 * 2) STORED); |
| 352 |
CREATE PUBLICATION pub1 for table t1(c1, c2, c3); |
| 353 |
INSERT INTO t1 VALUES (1); |
| 354 |
)); |
| 355 |
|
| 356 |
# Create table and subscription. |
| 357 |
$node_subscriber->safe_psql( |
| 358 |
'postgres', qq( |
| 359 |
CREATE TABLE t1(c1 int, c2 int GENERATED ALWAYS AS (c1 + 2) STORED, c3 int GENERATED ALWAYS AS (c1 + 2) STORED); |
| 360 |
CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1; |
| 361 |
)); |
| 362 |
|
| 363 |
# Verify that an error occurs. |
| 364 |
my $offset = -s $node_subscriber->logfile; |
| 365 |
$node_subscriber->wait_for_log( |
| 366 |
qr/ERROR: ( [A-Z0-9]+:)? logical replication target relation "public.t1" has incompatible generated columns: "c2", "c3"/, |
| 367 |
$offset); |
| 368 |
|
| 369 |
# cleanup |
| 370 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub1"); |
| 371 |
$node_publisher->safe_psql('postgres', "DROP PUBLICATION pub1"); |
| 372 |
|
| 373 |
done_testing(); |