Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
 
# Copyright (c) 2026, PostgreSQL Global Development Group
 
# Logical replication tests for publications with EXCEPT clause
use strict;
use warnings;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# Initialize publisher node
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
$node_publisher->init(allows_streaming => 'logical');
$node_publisher->start;
 
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
 
# Initialize subscriber node
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
$node_subscriber->init;
$node_subscriber->start;
 
my $result;
 
sub test_except_root_partition
{
    my ($pubviaroot) = @_;
 
    # If the root partitioned table is in the EXCEPT clause, all its
    # partitions are excluded from publication, regardless of the
    # publish_via_partition_root setting.
    $node_publisher->safe_psql(
        'postgres', qq(
        CREATE PUBLICATION tap_pub_part FOR ALL TABLES EXCEPT (TABLE root1) WITH (publish_via_partition_root = $pubviaroot);
        INSERT INTO root1 VALUES (1), (101);
    ));
    $node_subscriber->safe_psql('postgres',
        "CREATE SUBSCRIPTION tap_sub_part CONNECTION '$publisher_connstr' PUBLICATION tap_pub_part"
    );
    $node_subscriber->wait_for_subscription_sync($node_publisher,
        'tap_sub_part');
 
    # Advance the replication slot to ignore changes generated before this point.
    $node_publisher->safe_psql('postgres',
        "SELECT slot_name FROM pg_replication_slot_advance('test_slot', pg_current_wal_lsn())"
    );
    $node_publisher->safe_psql('postgres',
        "INSERT INTO root1 VALUES (2), (102)");
 
    # Verify that data inserted into the partitioned table is not published when
    # it is in the EXCEPT clause.
    $result = $node_publisher->safe_psql('postgres',
        "SELECT count(*) = 0 FROM pg_logical_slot_get_binary_changes('test_slot', NULL, NULL, 'proto_version', '1', 'publication_names', 'tap_pub_part')"
    );
    is($result, qq(t),
        "no changes for the partitioned table in the EXCEPT clause are present in the replication slot (publish_via_partition_root = $pubviaroot)"
    );
 
    $node_publisher->wait_for_catchup('tap_sub_part');
 
    # Verify that no rows are replicated to subscriber for root or partitions.
    foreach my $table (qw(root1 part1 part2 part2_1))
    {
        $result = $node_subscriber->safe_psql('postgres',
            "SELECT count(*) FROM $table");
        is($result, qq(0), "no rows replicated to subscriber for $table");
    }
 
    $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_part");
    $node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_part");
}
 
# ============================================
# EXCEPT clause test cases for non-partitioned tables and inherited tables.
# ============================================
 
# Create tables on publisher
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE tab1 AS SELECT generate_series(1,10) AS a;
    CREATE TABLE parent (a int);
    CREATE TABLE child (b int) INHERITS (parent);
    CREATE TABLE parent1 (a int);
    CREATE TABLE child1 (b int) INHERITS (parent1);
));
 
# Create tables on subscriber
$node_subscriber->safe_psql(
    'postgres', qq(
    CREATE TABLE tab1 (a int);
    CREATE TABLE parent (a int);
    CREATE TABLE child (b int) INHERITS (parent);
    CREATE TABLE parent1 (a int);
    CREATE TABLE child1 (b int) INHERITS (parent1);
));
 
# Exclude tab1 (non-inheritance case), and also exclude parent and ONLY parent1
# to verify exclusion behavior for inherited tables, including the effect of
# ONLY in the EXCEPT clause.
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub FOR ALL TABLES EXCEPT (TABLE tab1, parent, only parent1)"
);
 
# Create a logical replication slot to help with later tests.
$node_publisher->safe_psql('postgres',
    "SELECT pg_create_logical_replication_slot('test_slot', 'pgoutput')");
 
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
);
 
# Wait for initial table sync to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
 
# Check the table data does not sync for the tables specified in the EXCEPT
# clause.
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1");
is($result, qq(0),
    'check there is no initial data copied for the tables specified in the EXCEPT clause'
);
 
# Insert some data into the table listed in the EXCEPT clause
$node_publisher->safe_psql(
    'postgres', qq(
    INSERT INTO tab1 VALUES(generate_series(11,20));
    INSERT INTO child VALUES(generate_series(11,20), generate_series(11,20));
));
 
# Verify that data inserted into a table listed in the EXCEPT clause is
# not published.
$result = $node_publisher->safe_psql('postgres',
    "SELECT count(*) = 0 FROM pg_logical_slot_get_binary_changes('test_slot', NULL, NULL, 'proto_version', '1', 'publication_names', 'tap_pub')"
);
is($result, qq(t),
    'verify no changes for table listed in the EXCEPT clause are present in the replication slot'
);
 
# This should be published because ONLY parent1 was specified in the
# EXCEPT clause, so the exclusion applies only to the parent table and not
# to its child.
$node_publisher->safe_psql('postgres',
    "INSERT INTO child1 VALUES(generate_series(11,20), generate_series(11,20))"
);
 
# Verify that data inserted into a table listed in the EXCEPT clause is
# not replicated.
$node_publisher->wait_for_catchup('tap_sub');
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1");
is($result, qq(0), 'check replicated inserts on subscriber');
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM child");
is($result, qq(0), 'check replicated inserts on subscriber');
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM child1");
is($result, qq(10), 'check replicated inserts on subscriber');
 
$node_publisher->safe_psql('postgres',
    "CREATE TABLE tab2 AS SELECT generate_series(1,10) AS a");
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab2 (a int)");
 
# Replace the table list in the EXCEPT clause so that only tab2 is excluded.
$node_publisher->safe_psql('postgres',
    "ALTER PUBLICATION tap_pub SET ALL TABLES EXCEPT (TABLE tab2)");
 
# Refresh the subscription so the subscriber picks up the updated
# publication definition and initiates table synchronization.
$node_subscriber->safe_psql('postgres',
    "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
 
# Wait for initial table sync to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
 
# Verify that initial table synchronization does not occur for tables
# listed in the EXCEPT clause.
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab2");
is($result, qq(0),
    'check there is no initial data copied for the tables specified in the EXCEPT clause'
);
 
# Verify that table synchronization now happens for tab1. Table tab1 is
# included now since the table list of EXCEPT clause is only (tab2).
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1");
is($result, qq(20),
    'check that the data is copied as the tab1 is removed from EXCEPT clause'
);
 
# cleanup
$node_subscriber->safe_psql(
    'postgres', qq(
    DROP SUBSCRIPTION tap_sub;
    TRUNCATE TABLE tab1;
    DROP TABLE parent, parent1, child, child1, tab2;
));
$node_publisher->safe_psql(
    'postgres', qq(
    DROP PUBLICATION tap_pub;
    TRUNCATE TABLE tab1;
    DROP TABLE parent, parent1, child, child1, tab2;
));
 
# ============================================
# EXCEPT clause test cases for partitioned tables
# ============================================
# Setup partitioned table and partitions on the publisher that map to normal
# tables on the subscriber.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE TABLE root1(a int) PARTITION BY RANGE(a);
    CREATE TABLE part1 PARTITION OF root1 FOR VALUES FROM (0) TO (100);
    CREATE TABLE part2 PARTITION OF root1 FOR VALUES FROM (100) TO (200) PARTITION BY RANGE(a);
    CREATE TABLE part2_1 PARTITION OF part2 FOR VALUES FROM (100) TO (150);
));
 
$node_subscriber->safe_psql(
    'postgres', qq(
    CREATE TABLE root1(a int);
    CREATE TABLE part1(a int);
    CREATE TABLE part2(a int);
    CREATE TABLE part2_1(a int);
));
 
# Validate the behaviour with both publish_via_partition_root as true and false
test_except_root_partition('false');
test_except_root_partition('true');
 
# ============================================
# Test when a subscription is subscribing to multiple publications
# ============================================
 
# OK when a table is excluded by pub1 EXCEPT clause, but it is included by pub2
# FOR TABLE.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE PUBLICATION tap_pub1 FOR ALL TABLES EXCEPT (TABLE tab1);
    CREATE PUBLICATION tap_pub2 FOR TABLE tab1;
    INSERT INTO tab1 VALUES(1);
));
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub1, tap_pub2"
);
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
 
$node_publisher->safe_psql('postgres', qq(INSERT INTO tab1 VALUES(2)));
$node_publisher->wait_for_catchup('tap_sub');
 
$result =
  $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY a");
is( $result, qq(1
2),
    "check replication of a table in the EXCEPT clause of one publication but included by another"
);
 
$node_subscriber->safe_psql(
    'postgres', qq(
    DROP SUBSCRIPTION tap_sub;
    TRUNCATE tab1;
));
$node_publisher->safe_psql(
    'postgres', qq(
    DROP PUBLICATION tap_pub2;
    TRUNCATE tab1;
));
 
# OK when a table is excluded by pub1 EXCEPT clause, but it is included by pub2
# FOR ALL TABLES.
$node_publisher->safe_psql(
    'postgres', qq(
    CREATE PUBLICATION tap_pub2 FOR ALL TABLES;
    INSERT INTO tab1 VALUES(1);
));
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub1, tap_pub2"
);
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
 
$node_publisher->safe_psql('postgres', qq(INSERT INTO tab1 VALUES(2)));
$node_publisher->wait_for_catchup('tap_sub');
 
$result =
  $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY a");
is( $result, qq(1
2),
    "check replication of a table in the EXCEPT clause of one publication but included by another"
);
 
done_testing();