Debian Codegraph @

src/test/subscription/t/012_collation.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test collations, in particular nondeterministic ones
5 # (only works with ICU)
6 use strict;
7 use warnings FATAL => 'all';
8 use PostgreSQL::Test::Cluster;
9 use PostgreSQL::Test::Utils;
10 use Test::More;
11
12 if ($ENV{with_icu} ne 'yes')
13 {
14 plan skip_all => 'ICU not supported by this build';
15 }
16
17 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
18 $node_publisher->init(
19 allows_streaming => 'logical',
20 extra => [ '--locale=C', '--encoding=UTF8' ]);
21 $node_publisher->start;
22
23 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
24 $node_subscriber->init(extra => [ '--locale=C', '--encoding=UTF8' ]);
25 $node_subscriber->start;
26
27 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
28
29 # Test plan: Create a table with a nondeterministic collation in the
30 # primary key column. Pre-insert rows on the publisher and subscriber
31 # that are collation-wise equal but byte-wise different. (We use a
32 # string in different normal forms for that.) Set up publisher and
33 # subscriber. Update the row on the publisher, but don't change the
34 # primary key column. The subscriber needs to find the row to be
35 # updated using the nondeterministic collation semantics. We need to
36 # test for both a replica identity index and for replica identity
37 # full, since those have different code paths internally.
38
39 $node_subscriber->safe_psql('postgres',
40 q{CREATE COLLATION ctest_nondet (provider = icu, locale = 'und', deterministic = false)}
41 );
42
43 # table with replica identity index
44
45 $node_publisher->safe_psql('postgres',
46 q{CREATE TABLE tab1 (a text PRIMARY KEY, b text)});
47
48 $node_publisher->safe_psql('postgres',
49 q{INSERT INTO tab1 VALUES (U&'\00E4bc', 'foo')});
50
51 $node_subscriber->safe_psql('postgres',
52 q{CREATE TABLE tab1 (a text COLLATE ctest_nondet PRIMARY KEY, b text)});
53
54 $node_subscriber->safe_psql('postgres',
55 q{INSERT INTO tab1 VALUES (U&'\0061\0308bc', 'foo')});
56
57 # table with replica identity full
58
59 $node_publisher->safe_psql('postgres', q{CREATE TABLE tab2 (a text, b text)});
60 $node_publisher->safe_psql('postgres',
61 q{ALTER TABLE tab2 REPLICA IDENTITY FULL});
62
63 $node_publisher->safe_psql('postgres',
64 q{INSERT INTO tab2 VALUES (U&'\00E4bc', 'foo')});
65
66 $node_subscriber->safe_psql('postgres',
67 q{CREATE TABLE tab2 (a text COLLATE ctest_nondet, b text)});
68 $node_subscriber->safe_psql('postgres',
69 q{ALTER TABLE tab2 REPLICA IDENTITY FULL});
70
71 $node_subscriber->safe_psql('postgres',
72 q{INSERT INTO tab2 VALUES (U&'\0061\0308bc', 'foo')});
73
74 # set up publication, subscription
75
76 $node_publisher->safe_psql('postgres',
77 q{CREATE PUBLICATION pub1 FOR ALL TABLES});
78
79 $node_subscriber->safe_psql('postgres',
80 qq{CREATE SUBSCRIPTION sub1 CONNECTION '$publisher_connstr' PUBLICATION pub1 WITH (copy_data = false)}
81 );
82
83 $node_publisher->wait_for_catchup('sub1');
84
85 # test with replica identity index
86
87 $node_publisher->safe_psql('postgres',
88 q{UPDATE tab1 SET b = 'bar' WHERE b = 'foo'});
89
90 $node_publisher->wait_for_catchup('sub1');
91
92 is($node_subscriber->safe_psql('postgres', q{SELECT b FROM tab1}),
93 qq(bar), 'update with primary key with nondeterministic collation');
94
95 # test with replica identity full
96
97 $node_publisher->safe_psql('postgres',
98 q{UPDATE tab2 SET b = 'bar' WHERE b = 'foo'});
99
100 $node_publisher->wait_for_catchup('sub1');
101
102 is($node_subscriber->safe_psql('postgres', q{SELECT b FROM tab2}),
103 qq(bar),
104 'update with replica identity full with nondeterministic collation');
105
106 done_testing();