|
| 1 | +# Test streaming of large transaction containing multiple subtransactions and rollbacks |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 4; |
| 7 | + |
| 8 | +# Create publisher node |
| 9 | +my $node_publisher = get_new_node('publisher'); |
| 10 | +$node_publisher->init(allows_streaming => 'logical'); |
| 11 | +$node_publisher->append_conf('postgresql.conf', 'logical_decoding_work_mem = 64kB'); |
| 12 | +$node_publisher->start; |
| 13 | + |
| 14 | +# Create subscriber node |
| 15 | +my $node_subscriber = get_new_node('subscriber'); |
| 16 | +$node_subscriber->init(allows_streaming => 'logical'); |
| 17 | +$node_subscriber->start; |
| 18 | + |
| 19 | +# Create some preexisting content on publisher |
| 20 | +$node_publisher->safe_psql('postgres', |
| 21 | + "CREATE TABLE test_tab (a int primary key, b varchar)"); |
| 22 | +$node_publisher->safe_psql('postgres', |
| 23 | + "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')"); |
| 24 | + |
| 25 | +# Setup structure on subscriber |
| 26 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab (a int primary key, b text, c INT, d INT, e INT)"); |
| 27 | + |
| 28 | +# Setup logical replication |
| 29 | +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 30 | +$node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR TABLE test_tab"); |
| 31 | + |
| 32 | +my $appname = 'tap_sub'; |
| 33 | +$node_subscriber->safe_psql('postgres', |
| 34 | +"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (streaming = on)" |
| 35 | +); |
| 36 | + |
| 37 | +$node_publisher->wait_for_catchup($appname); |
| 38 | + |
| 39 | +# Also wait for initial table sync to finish |
| 40 | +my $synced_query = |
| 41 | +"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');"; |
| 42 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 43 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 44 | + |
| 45 | +my $result = |
| 46 | + $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c) FROM test_tab"); |
| 47 | +is($result, qq(2|0), 'check initial data was copied to subscriber'); |
| 48 | + |
| 49 | +# large (streamed) transaction with DDL, DML and ROLLBACKs |
| 50 | +$node_publisher->safe_psql('postgres', q{ |
| 51 | +BEGIN; |
| 52 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(3,500) s(i); |
| 53 | +SAVEPOINT s1; |
| 54 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(501,1000) s(i); |
| 55 | +SAVEPOINT s2; |
| 56 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(1001,1500) s(i); |
| 57 | +SAVEPOINT s3; |
| 58 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(1501,2000) s(i); |
| 59 | +ROLLBACK TO s2; |
| 60 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(2001,2500) s(i); |
| 61 | +ROLLBACK TO s1; |
| 62 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(2501,3000) s(i); |
| 63 | +SAVEPOINT s4; |
| 64 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(3001,3500) s(i); |
| 65 | +SAVEPOINT s5; |
| 66 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(3501,4000) s(i); |
| 67 | +COMMIT; |
| 68 | +}); |
| 69 | + |
| 70 | +$node_publisher->wait_for_catchup($appname); |
| 71 | + |
| 72 | +$result = |
| 73 | + $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c) FROM test_tab"); |
| 74 | +is($result, qq(2000|0), 'check rollback to savepoint was reflected on subscriber and extra columns contain local defaults'); |
| 75 | + |
| 76 | +# large (streamed) transaction with subscriber receiving out of order |
| 77 | +# subtransaction ROLLBACKs |
| 78 | +$node_publisher->safe_psql('postgres', q{ |
| 79 | +BEGIN; |
| 80 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(4001,4500) s(i); |
| 81 | +SAVEPOINT s1; |
| 82 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(5001,5500) s(i); |
| 83 | +SAVEPOINT s2; |
| 84 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(6001,6500) s(i); |
| 85 | +SAVEPOINT s3; |
| 86 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(7001,7500) s(i); |
| 87 | +RELEASE s2; |
| 88 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(8001,8500) s(i); |
| 89 | +ROLLBACK TO s1; |
| 90 | +COMMIT; |
| 91 | +}); |
| 92 | + |
| 93 | +$node_publisher->wait_for_catchup($appname); |
| 94 | + |
| 95 | +$result = |
| 96 | + $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c) FROM test_tab"); |
| 97 | +is($result, qq(2500|0), 'check rollback to savepoint was reflected on subscriber'); |
| 98 | + |
| 99 | +# large (streamed) transaction with subscriber receiving rollback |
| 100 | +$node_publisher->safe_psql('postgres', q{ |
| 101 | +BEGIN; |
| 102 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(8501,9000) s(i); |
| 103 | +SAVEPOINT s1; |
| 104 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(9001,9500) s(i); |
| 105 | +SAVEPOINT s2; |
| 106 | +INSERT INTO test_tab SELECT i, md5(i::text) FROM generate_series(9501,10000) s(i); |
| 107 | +ROLLBACK; |
| 108 | +}); |
| 109 | + |
| 110 | +$node_publisher->wait_for_catchup($appname); |
| 111 | + |
| 112 | +$result = |
| 113 | + $node_subscriber->safe_psql('postgres', "SELECT count(*), count(c) FROM test_tab"); |
| 114 | +is($result, qq(2500|0), 'check rollback was reflected on subscriber'); |
| 115 | + |
| 116 | +$node_subscriber->stop; |
| 117 | +$node_publisher->stop; |
0 commit comments