Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# Copyright (c) 2022-2026, PostgreSQL Global Development Group
 
# Test logical replication behavior with subscriber using available index
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# create publisher node
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
$node_publisher->init(allows_streaming => 'logical');
$node_publisher->start;
 
# create subscriber node
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
$node_subscriber->init;
$node_subscriber->start;
 
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
my $appname = 'tap_sub';
my $result = '';
 
# =============================================================================
# Testcase start: Subscription can use index with multiple rows and columns
#
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y text)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y text)");
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX test_replica_id_full_idx ON test_replica_id_full(x,y)");
 
# insert some initial data within the range 0-9 for x and y
$node_publisher->safe_psql('postgres',
    "INSERT INTO test_replica_id_full SELECT (i%10), (i%10)::text FROM generate_series(0,10) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# delete 2 rows
$node_publisher->safe_psql('postgres',
    "DELETE FROM test_replica_id_full WHERE x IN (5, 6)");
 
# update 2 rows
$node_publisher->safe_psql('postgres',
    "UPDATE test_replica_id_full SET x = 100, y = '200' WHERE x IN (1, 2)");
 
# wait until the index is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select (idx_scan = 4) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full updates 4 rows via index";
 
# make sure that the subscriber has the correct data after the UPDATE
$result = $node_subscriber->safe_psql('postgres',
    "select count(*) from test_replica_id_full WHERE (x = 100 and y = '200')"
);
is($result, qq(2),
    'ensure subscriber has the correct data at the end of the test');
 
# make sure that the subscriber has the correct data after the first DELETE
$result = $node_subscriber->safe_psql('postgres',
    "select count(*) from test_replica_id_full where x in (5, 6)");
is($result, qq(0),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full");
 
# Testcase end: Subscription can use index with multiple rows and columns
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription can use index on partitioned tables
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE users_table_part(user_id bigint, value_1 int, value_2 int) PARTITION BY RANGE (value_1)"
);
$node_publisher->safe_psql('postgres',
    "CREATE TABLE users_table_part_0 PARTITION OF users_table_part FOR VALUES FROM (0) TO (10)"
);
$node_publisher->safe_psql('postgres',
    "CREATE TABLE users_table_part_1 PARTITION OF users_table_part FOR VALUES FROM (10) TO (20)"
);
 
$node_publisher->safe_psql('postgres',
    "ALTER TABLE users_table_part REPLICA IDENTITY FULL");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE users_table_part_0 REPLICA IDENTITY FULL");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE users_table_part_1 REPLICA IDENTITY FULL");
 
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE users_table_part(user_id bigint, value_1 int, value_2 int) PARTITION BY RANGE (value_1)"
);
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE users_table_part_0 PARTITION OF users_table_part FOR VALUES FROM (0) TO (10)"
);
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE users_table_part_1 PARTITION OF users_table_part FOR VALUES FROM (10) TO (20)"
);
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX users_table_part_idx ON users_table_part(user_id, value_1)"
);
 
# insert some initial data
$node_publisher->safe_psql('postgres',
    "INSERT INTO users_table_part SELECT (i%100), (i%20), i FROM generate_series(0,100) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE users_table_part");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# update rows, moving them to other partitions
$node_publisher->safe_psql('postgres',
    "UPDATE users_table_part SET value_1 = 0 WHERE user_id = 4");
 
# delete rows from different partitions
$node_publisher->safe_psql('postgres',
    "DELETE FROM users_table_part WHERE user_id = 1 and value_1 = 1");
$node_publisher->safe_psql('postgres',
    "DELETE FROM users_table_part WHERE user_id = 12 and value_1 = 12");
 
# wait until the index is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select sum(idx_scan)=3 from pg_stat_all_indexes where indexrelname ilike 'users_table_part_%';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full updates partitioned table";
 
# make sure that the subscriber has the correct data
$result = $node_subscriber->safe_psql('postgres',
    "select sum(user_id+value_1+value_2) from users_table_part");
is($result, qq(10907),
    'ensure subscriber has the correct data at the end of the test');
$result = $node_subscriber->safe_psql('postgres',
    "select count(DISTINCT(user_id,value_1, value_2)) from users_table_part");
is($result, qq(99),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE users_table_part");
 
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE users_table_part");
 
# Testcase end: Subscription can use index on partitioned tables
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription will not use indexes with only expressions or
# partial index
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE people (firstname text, lastname text)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE people REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE people (firstname text, lastname text)");
 
# index with only an expression
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX people_names_expr_only ON people ((firstname || ' ' || lastname))"
);
 
# partial index
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX people_names_partial ON people(firstname) WHERE (firstname = 'first_name_1')"
);
 
# insert some initial data
$node_publisher->safe_psql('postgres',
    "INSERT INTO people SELECT 'first_name_' || i::text, 'last_name_' || i::text FROM generate_series(0,200) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE people");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# update 2 rows
$node_publisher->safe_psql('postgres',
    "UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_1'"
);
$node_publisher->safe_psql('postgres',
    "UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_2' AND lastname = 'last_name_2'"
);
 
# make sure none of the indexes is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$result = $node_subscriber->safe_psql('postgres',
    "select sum(idx_scan) from pg_stat_all_indexes where indexrelname IN ('people_names_expr_only', 'people_names_partial')"
);
is($result, qq(0),
    'ensure subscriber tap_sub_rep_full updates two rows via seq. scan with index on expressions'
);
 
$node_publisher->safe_psql('postgres',
    "DELETE FROM people WHERE firstname = 'first_name_3'");
$node_publisher->safe_psql('postgres',
    "DELETE FROM people WHERE firstname = 'first_name_4' AND lastname = 'last_name_4'"
);
 
# make sure the index is not used on the subscriber
$node_publisher->wait_for_catchup($appname);
$result = $node_subscriber->safe_psql('postgres',
    "select sum(idx_scan) from pg_stat_all_indexes where indexrelname IN ('people_names_expr_only', 'people_names_partial')"
);
is($result, qq(0),
    'ensure subscriber tap_sub_rep_full updates two rows via seq. scan with index on expressions'
);
 
# make sure that the subscriber has the correct data
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM people");
is($result, qq(199),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE people");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE people");
 
# Testcase end: Subscription will not use indexes with only expressions or
# partial index
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription can use index having expressions and columns
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE people (firstname text, lastname text)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE people REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE people (firstname text, lastname text)");
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX people_names ON people (firstname, lastname, (firstname || ' ' || lastname))"
);
 
# insert some initial data
$node_publisher->safe_psql('postgres',
    "INSERT INTO people SELECT 'first_name_' || i::text, 'last_name_' || i::text FROM generate_series(0, 20) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE people");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# update 1 row
$node_publisher->safe_psql('postgres',
    "UPDATE people SET firstname = 'no-name' WHERE firstname = 'first_name_1'"
);
 
# delete the updated row
$node_publisher->safe_psql('postgres',
    "DELETE FROM people WHERE firstname = 'no-name'");
 
# wait until the index is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select idx_scan=2 from pg_stat_all_indexes where indexrelname = 'people_names';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full deletes two rows via index scan with index on expressions and columns";
 
# make sure that the subscriber has the correct data
$result =
  $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM people");
is($result, qq(20),
    'ensure subscriber has the correct data at the end of the test');
 
$result = $node_subscriber->safe_psql('postgres',
    "SELECT count(*) FROM people WHERE firstname = 'no-name'");
is($result, qq(0),
    'ensure subscriber has the correct data at the end of the test');
 
# now, drop the index with the expression, we'll use sequential scan
$node_subscriber->safe_psql('postgres', "DROP INDEX people_names");
 
# delete 1 row
$node_publisher->safe_psql('postgres',
    "DELETE FROM people WHERE lastname = 'last_name_18'");
 
# make sure that the subscriber has the correct data
$node_publisher->wait_for_catchup($appname);
$result = $node_subscriber->safe_psql('postgres',
    "SELECT count(*) FROM people WHERE lastname = 'last_name_18'");
is($result, qq(0),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE people");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE people");
 
# Testcase end: Subscription can use index having expressions and columns
# =============================================================================
 
# =============================================================================
# Testcase start: Null values and missing column
 
$node_publisher->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int)");
 
$node_publisher->safe_psql('postgres',
    "ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL");
 
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y int)");
 
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX test_replica_id_full_idx ON test_replica_id_full(x,y)");
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full");
 
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# load some data, and update 2 tuples
$node_publisher->safe_psql('postgres',
    "INSERT INTO test_replica_id_full VALUES (1), (2), (3)");
$node_publisher->safe_psql('postgres',
    "UPDATE test_replica_id_full SET x = x + 1 WHERE x = 1");
 
# check if the index is used even when the index has NULL values
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select idx_scan=1 from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full updates test_replica_id_full table";
 
# make sure that the subscriber has the correct data
$result = $node_subscriber->safe_psql('postgres',
    "select sum(x) from test_replica_id_full WHERE y IS NULL");
is($result, qq(7),
    'ensure subscriber has the correct data at the end of the test');
 
# make sure that the subscriber has the correct data
$result = $node_subscriber->safe_psql('postgres',
    "select count(*) from test_replica_id_full WHERE y IS NULL");
is($result, qq(3),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full");
 
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full");
 
# Testcase end: Null values And missing column
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription using a unique index when Pub/Sub has different
# data
#
# The subscriber has duplicate tuples that publisher does not have. When
# publisher updates/deletes 1 row, subscriber uses indexes and updates/deletes
# exactly 1 row.
#
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y int)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y int)");
$node_subscriber->safe_psql('postgres',
    "CREATE UNIQUE INDEX test_replica_id_full_idxy ON test_replica_id_full(x,y)"
);
 
# insert some initial data
$node_publisher->safe_psql('postgres',
    "INSERT INTO test_replica_id_full SELECT i, i FROM generate_series(0,21) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# duplicate the data in subscriber for y column
$node_subscriber->safe_psql('postgres',
    "INSERT INTO test_replica_id_full SELECT i+100, i FROM generate_series(0,21) i"
);
 
# now, we update only 1 row on the publisher and expect the subscriber to only
# update 1 row although there are two tuples with y = 15 on the subscriber
$node_publisher->safe_psql('postgres',
    "UPDATE test_replica_id_full SET x = 2000 WHERE y = 15");
 
# wait until the index is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select (idx_scan = 1) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idxy';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full updates one row via index";
 
# make sure that the subscriber has the correct data
# we only updated 1 row
$result = $node_subscriber->safe_psql('postgres',
    "SELECT count(*) FROM test_replica_id_full WHERE x = 2000");
is($result, qq(1),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full");
 
# Testcase start: Subscription using a unique index when Pub/Sub has different
# data
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription does not use an invalid index
#
# A failed CREATE INDEX CONCURRENTLY leaves behind a live but invalid
# index, which is not required to contain every row.  The apply worker
# must not choose it for REPLICA IDENTITY FULL lookups.
#
 
# create tables pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE test_invalid (x int, y int)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE test_invalid REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE test_invalid (x int, y int)");
 
# insert some initial data, including the row the index build trips over
$node_publisher->safe_psql('postgres',
    "INSERT INTO test_invalid SELECT i, i FROM generate_series(1,10) i");
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_invalid FOR TABLE test_invalid");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_invalid CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_invalid"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# leave an invalid index behind: the build fails on the y = 5 row
my ($cic_ret, $cic_out, $cic_err) = $node_subscriber->psql('postgres',
    "CREATE INDEX CONCURRENTLY test_invalid_idx ON test_invalid (x, (1/(y-5)))"
);
isnt($cic_ret, 0, 'CREATE INDEX CONCURRENTLY fails');
$result = $node_subscriber->safe_psql('postgres',
        "SELECT indisvalid FROM pg_index"
      . " WHERE indexrelid = 'test_invalid_idx'::regclass");
is($result, qq(f), 'and leaves an invalid index behind');
 
# the update must still be applied
$node_publisher->safe_psql('postgres',
    "UPDATE test_invalid SET y = 99 WHERE x = 7");
$node_publisher->wait_for_catchup($appname);
$result = $node_subscriber->safe_psql('postgres',
    "SELECT y FROM test_invalid WHERE x = 7");
is($result, qq(99),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_invalid");
$node_publisher->safe_psql('postgres', "DROP TABLE test_invalid");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_invalid");
$node_subscriber->safe_psql('postgres', "DROP TABLE test_invalid");
 
# Testcase end: Subscription does not use an invalid index
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription can use hash index
#
 
# create tables on pub and sub
$node_publisher->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y text)");
$node_publisher->safe_psql('postgres',
    "ALTER TABLE test_replica_id_full REPLICA IDENTITY FULL");
$node_subscriber->safe_psql('postgres',
    "CREATE TABLE test_replica_id_full (x int, y text)");
$node_subscriber->safe_psql('postgres',
    "CREATE INDEX test_replica_id_full_idx ON test_replica_id_full USING HASH (x)"
);
 
# insert some initial data
$node_publisher->safe_psql('postgres',
    "INSERT INTO test_replica_id_full SELECT i, (i%10)::text FROM generate_series(0,10) i"
);
 
# create pub/sub
$node_publisher->safe_psql('postgres',
    "CREATE PUBLICATION tap_pub_rep_full FOR TABLE test_replica_id_full");
$node_subscriber->safe_psql('postgres',
    "CREATE SUBSCRIPTION tap_sub_rep_full CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub_rep_full"
);
 
# wait for initial table synchronization to finish
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
 
# delete 2 rows
$node_publisher->safe_psql('postgres',
    "DELETE FROM test_replica_id_full WHERE x IN (5, 6)");
 
# update 2 rows
$node_publisher->safe_psql('postgres',
    "UPDATE test_replica_id_full SET x = 100, y = '200' WHERE x IN (1, 2)");
 
# wait until the index is used on the subscriber
$node_publisher->wait_for_catchup($appname);
$node_subscriber->poll_query_until('postgres',
    q{select (idx_scan = 4) from pg_stat_all_indexes where indexrelname = 'test_replica_id_full_idx';}
  )
  or die
  "Timed out while waiting for check subscriber tap_sub_rep_full deletes 2 rows and updates 2 rows via index";
 
# make sure that the subscriber has the correct data after the UPDATE
$result = $node_subscriber->safe_psql('postgres',
    "select count(*) from test_replica_id_full WHERE (x = 100 and y = '200')"
);
is($result, qq(2),
    'ensure subscriber has the correct data at the end of the test');
 
# make sure that the subscriber has the correct data after the first DELETE
$result = $node_subscriber->safe_psql('postgres',
    "select count(*) from test_replica_id_full where x in (5, 6)");
is($result, qq(0),
    'ensure subscriber has the correct data at the end of the test');
 
# cleanup pub
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_rep_full");
$node_publisher->safe_psql('postgres', "DROP TABLE test_replica_id_full");
# cleanup sub
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_rep_full");
$node_subscriber->safe_psql('postgres', "DROP TABLE test_replica_id_full");
 
# Testcase end: Subscription can use hash index
# =============================================================================
 
# =============================================================================
# Testcase start: Subscription keeps using an index that concurrent DDL has
# demoted from replica identity
#
# DROP INDEX CONCURRENTLY clears indisvalid and indisreplident and commits that
# before waiting for the lock the apply worker holds, so the worker can be left
# holding an index the catalogs no longer call the replica identity.  The drop
# gets no further while apply holds the table, so the index is still complete
# and still maintained, and the change must be applied through it rather than
# dropped as a missing-tuple conflict.
#
# REINDEX CONCURRENTLY reaches the same state by swapping a new index in, but
# the apply path is the same one, so it is not tested separately.
 
SKIP:
{
    skip 'Injection points not supported by this build', 5
      unless $ENV{enable_injection_points} eq 'yes';
    skip 'Extension injection_points not installed', 5
      unless $node_subscriber->check_extension('injection_points');
 
    $node_subscriber->safe_psql('postgres',
        'CREATE EXTENSION injection_points');
 
    # create tables pub and sub, using a unique index as replica identity
    $node_publisher->safe_psql(
        'postgres', q[
        CREATE TABLE test_dropri (x int NOT NULL, y int);
        CREATE UNIQUE INDEX test_dropri_ri ON test_dropri (x);
        ALTER TABLE test_dropri REPLICA IDENTITY USING INDEX test_dropri_ri;
        INSERT INTO test_dropri SELECT i, i FROM generate_series(1,20) i;
        CREATE PUBLICATION tap_pub_dropri FOR TABLE test_dropri;
    ]);
    $node_subscriber->safe_psql(
        'postgres', q[
        CREATE TABLE test_dropri (x int NOT NULL, y int);
        CREATE UNIQUE INDEX test_dropri_ri ON test_dropri (x);
        ALTER TABLE test_dropri REPLICA IDENTITY USING INDEX test_dropri_ri;
    ]);
    $node_subscriber->safe_psql('postgres',
        "CREATE SUBSCRIPTION tap_sub_dropri CONNECTION '$publisher_connstr application_name=dropri' PUBLICATION tap_pub_dropri"
    );
 
    # wait for initial table synchronization to finish
    $node_subscriber->wait_for_subscription_sync($node_publisher, 'dropri');
 
    # Let the worker take the index and stop before opening the relation's
    # indexes.  The point is attached server-wide: the apply worker is not a
    # session this test can attach anything in.
    $node_subscriber->safe_psql('postgres',
        "SELECT injection_points_attach('apply-update-before-open-indices', 'wait')"
    );
    $node_publisher->safe_psql('postgres',
        "UPDATE test_dropri SET y = 99 WHERE x = 7");
    $node_subscriber->wait_for_event(
        'logical replication apply worker',
        'apply-update-before-open-indices');
 
    # This commits the loss of the replica identity, leaving relreplident set
    # to 'i' with no index claiming to be that identity, then parks waiting
    # for the apply worker's lock on the table.
    my $log_offset = -s $node_subscriber->logfile;
    my $drop = $node_subscriber->background_psql('postgres');
    $drop->query_until(
        qr/starting_drop/, q[
        \echo starting_drop
        DROP INDEX CONCURRENTLY test_dropri_ri;
    ]);
    $node_subscriber->poll_query_until('postgres',
            "SELECT count(*) = 0 FROM pg_index"
          . " WHERE indrelid = 'test_dropri'::regclass AND indisreplident")
      or die "timed out waiting for the identity index to be invalidated";
 
    # Detach before waking, so the worker cannot park on the point again.
    $node_subscriber->safe_psql(
        'postgres',
        "SELECT injection_points_detach('apply-update-before-open-indices');
         SELECT injection_points_wakeup('apply-update-before-open-indices');"
    );
 
    # The straddling change goes through, found by the demoted index.
    $node_publisher->wait_for_catchup('dropri');
    $result = $node_subscriber->safe_psql('postgres',
        "SELECT y FROM test_dropri WHERE x = 7");
    is($result, qq(99), 'change straddling the drop is applied');
    ok($drop->quit, 'DROP INDEX CONCURRENTLY completes');
 
    # From the next change on, the relation map entry is rebuilt, finds no
    # replica identity, and apply stops with the usual error.
    $node_publisher->safe_psql('postgres',
        "UPDATE test_dropri SET y = 123 WHERE x = 8");
    ok( $node_subscriber->poll_query_until(
            'postgres', q[
            SELECT apply_error_count > 0 FROM pg_stat_subscription_stats
            WHERE subname = 'tap_sub_dropri']),
        'later changes wait for a replica identity');
    like(
        slurp_file($node_subscriber->logfile, $log_offset),
        qr/logical replication target relation "public\.test_dropri" has neither REPLICA IDENTITY index nor PRIMARY KEY/,
        'and say why');
 
    # Give the relation a replica identity again and they resume.
    $node_subscriber->safe_psql(
        'postgres', q[
        CREATE UNIQUE INDEX test_dropri_ri2 ON test_dropri (x);
        ALTER TABLE test_dropri REPLICA IDENTITY USING INDEX test_dropri_ri2;
    ]);
    $node_publisher->wait_for_catchup('dropri');
    $result = $node_subscriber->safe_psql('postgres',
        "SELECT y FROM test_dropri WHERE x = 8");
    is($result, qq(123), 'replication resumes');
 
    # cleanup pub
    $node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_dropri");
    $node_publisher->safe_psql('postgres', "DROP TABLE test_dropri");
    # cleanup sub
    $node_subscriber->safe_psql('postgres',
        "DROP SUBSCRIPTION tap_sub_dropri");
    $node_subscriber->safe_psql('postgres', "DROP TABLE test_dropri");
    $node_subscriber->safe_psql('postgres',
        "DROP EXTENSION injection_points");
}
 
# Testcase end: Subscription keeps using an index that concurrent DDL has
# demoted from replica identity
# =============================================================================
 
$node_subscriber->stop('fast');
$node_publisher->stop('fast');
 
done_testing();