| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# This test checks that constraints work on subscriber |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Initialize 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 |
# Setup structure on publisher |
| 22 |
$node_publisher->safe_psql('postgres', |
| 23 |
"CREATE TABLE tab_fk (bid int PRIMARY KEY);"); |
| 24 |
$node_publisher->safe_psql('postgres', |
| 25 |
"CREATE TABLE tab_fk_ref (id int PRIMARY KEY, junk text, bid int REFERENCES tab_fk (bid));" |
| 26 |
); |
| 27 |
|
| 28 |
# Setup structure on subscriber; column order intentionally different |
| 29 |
$node_subscriber->safe_psql('postgres', |
| 30 |
"CREATE TABLE tab_fk (bid int PRIMARY KEY);"); |
| 31 |
$node_subscriber->safe_psql('postgres', |
| 32 |
"CREATE TABLE tab_fk_ref (id int PRIMARY KEY, bid int REFERENCES tab_fk (bid), junk text);" |
| 33 |
); |
| 34 |
|
| 35 |
# Setup logical replication |
| 36 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 37 |
$node_publisher->safe_psql('postgres', |
| 38 |
"CREATE PUBLICATION tap_pub FOR ALL TABLES;"); |
| 39 |
|
| 40 |
$node_subscriber->safe_psql('postgres', |
| 41 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (copy_data = false)" |
| 42 |
); |
| 43 |
|
| 44 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 45 |
|
| 46 |
$node_publisher->safe_psql('postgres', |
| 47 |
"INSERT INTO tab_fk (bid) VALUES (1);"); |
| 48 |
# "junk" value is meant to be large enough to force out-of-line storage |
| 49 |
$node_publisher->safe_psql('postgres', |
| 50 |
"INSERT INTO tab_fk_ref (id, bid, junk) VALUES (1, 1, repeat(pi()::text,20000));" |
| 51 |
); |
| 52 |
|
| 53 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 54 |
|
| 55 |
# Check data on subscriber |
| 56 |
my $result = $node_subscriber->safe_psql('postgres', |
| 57 |
"SELECT count(*), min(bid), max(bid) FROM tab_fk;"); |
| 58 |
is($result, qq(1|1|1), 'check replicated tab_fk inserts on subscriber'); |
| 59 |
|
| 60 |
$result = $node_subscriber->safe_psql('postgres', |
| 61 |
"SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;"); |
| 62 |
is($result, qq(1|1|1), 'check replicated tab_fk_ref inserts on subscriber'); |
| 63 |
|
| 64 |
# Drop the fk on publisher |
| 65 |
$node_publisher->safe_psql('postgres', "DROP TABLE tab_fk CASCADE;"); |
| 66 |
|
| 67 |
# Insert data |
| 68 |
$node_publisher->safe_psql('postgres', |
| 69 |
"INSERT INTO tab_fk_ref (id, bid) VALUES (2, 2);"); |
| 70 |
|
| 71 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 72 |
|
| 73 |
# FK is not enforced on subscriber |
| 74 |
$result = $node_subscriber->safe_psql('postgres', |
| 75 |
"SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;"); |
| 76 |
is($result, qq(2|1|2), 'check FK ignored on subscriber'); |
| 77 |
|
| 78 |
# Add replica trigger |
| 79 |
$node_subscriber->safe_psql( |
| 80 |
'postgres', qq{ |
| 81 |
CREATE FUNCTION filter_basic_dml_fn() RETURNS TRIGGER AS \$\$ |
| 82 |
BEGIN |
| 83 |
IF (TG_OP = 'INSERT') THEN |
| 84 |
IF (NEW.id < 10) THEN |
| 85 |
RETURN NEW; |
| 86 |
ELSE |
| 87 |
RETURN NULL; |
| 88 |
END IF; |
| 89 |
ELSIF (TG_OP = 'UPDATE') THEN |
| 90 |
RETURN NULL; |
| 91 |
ELSE |
| 92 |
RAISE WARNING 'Unknown action'; |
| 93 |
RETURN NULL; |
| 94 |
END IF; |
| 95 |
END; |
| 96 |
\$\$ LANGUAGE plpgsql; |
| 97 |
CREATE TRIGGER filter_basic_dml_trg |
| 98 |
BEFORE INSERT OR UPDATE OF bid ON tab_fk_ref |
| 99 |
FOR EACH ROW EXECUTE PROCEDURE filter_basic_dml_fn(); |
| 100 |
ALTER TABLE tab_fk_ref ENABLE REPLICA TRIGGER filter_basic_dml_trg; |
| 101 |
}); |
| 102 |
|
| 103 |
# Insert data |
| 104 |
$node_publisher->safe_psql('postgres', |
| 105 |
"INSERT INTO tab_fk_ref (id, bid) VALUES (10, 10);"); |
| 106 |
|
| 107 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 108 |
|
| 109 |
# The trigger should cause the insert to be skipped on subscriber |
| 110 |
$result = $node_subscriber->safe_psql('postgres', |
| 111 |
"SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;"); |
| 112 |
is($result, qq(2|1|2), 'check replica insert trigger applied on subscriber'); |
| 113 |
|
| 114 |
# Update data |
| 115 |
$node_publisher->safe_psql('postgres', |
| 116 |
"UPDATE tab_fk_ref SET bid = 2 WHERE bid = 1;"); |
| 117 |
|
| 118 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 119 |
|
| 120 |
# The trigger should cause the update to be skipped on subscriber |
| 121 |
$result = $node_subscriber->safe_psql('postgres', |
| 122 |
"SELECT count(*), min(bid), max(bid) FROM tab_fk_ref;"); |
| 123 |
is($result, qq(2|1|2), |
| 124 |
'check replica update column trigger applied on subscriber'); |
| 125 |
|
| 126 |
# Update on a column not specified in the trigger, but it will trigger |
| 127 |
# anyway because logical replication ships all columns in an update. |
| 128 |
$node_publisher->safe_psql('postgres', |
| 129 |
"UPDATE tab_fk_ref SET id = 6 WHERE id = 1;"); |
| 130 |
|
| 131 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 132 |
|
| 133 |
$result = $node_subscriber->safe_psql('postgres', |
| 134 |
"SELECT count(*), min(id), max(id) FROM tab_fk_ref;"); |
| 135 |
is($result, qq(2|1|2), |
| 136 |
'check column trigger applied even on update for other column'); |
| 137 |
|
| 138 |
$node_subscriber->stop('fast'); |
| 139 |
$node_publisher->stop('fast'); |
| 140 |
|
| 141 |
done_testing(); |