# Streaming of in-progress transactions changing CFS-compressed tables.
|
#
|
# With streaming = on, large (or forced-to-stream) transactions are sent
|
# to the subscriber in chunks before commit, while the publisher hammers
|
# the CFS write path -- concurrent smgr extends plus compression. The
|
# scenarios below cover a plain bulk stream, a stream mixing DDL and DML,
|
# and streams with subtransaction rollbacks (with and without DDL).
|
# Rolled-back changes must never appear on the subscriber, and the
|
# publisher's CFS sidecars must stay structurally valid (pg_cfs_check).
|
|
use strict;
|
use warnings FATAL => 'all';
|
use PostgreSQL::Test::Cluster;
|
use PostgreSQL::Test::Utils;
|
use Test::More;
|
|
# Start a publisher (logical streaming enabled, extra config applied)
|
# and a plain subscriber, named after the scenario so sections don't
|
# clash.
|
sub start_pair
|
{
|
my ($name, $pub_conf) = @_;
|
|
my $pub = PostgreSQL::Test::Cluster->new("pub_$name");
|
$pub->init(allows_streaming => 'logical');
|
$pub->append_conf('postgresql.conf', $pub_conf);
|
$pub->start;
|
|
my $sub = PostgreSQL::Test::Cluster->new("sub_$name");
|
$sub->init;
|
$sub->start;
|
|
return ($pub, $sub);
|
}
|
|
# Create publication tap_pub for test_tab and a streaming subscription
|
# tap_sub (application_name = tap_sub), then wait for the initial table
|
# sync.
|
sub setup_streaming_replication
|
{
|
my ($pub, $sub) = @_;
|
|
my $connstr = $pub->connstr . ' dbname=postgres';
|
$pub->safe_psql('postgres',
|
"CREATE PUBLICATION tap_pub FOR TABLE test_tab");
|
$sub->safe_psql('postgres',
|
"CREATE SUBSCRIPTION tap_sub CONNECTION '$connstr application_name=tap_sub' PUBLICATION tap_pub WITH (streaming = on)"
|
);
|
$sub->wait_for_subscription_sync($pub, 'tap_sub');
|
}
|
|
# ---------------------------------------------------------------------
|
# Large streamed transactions: bulk INSERT, then INSERT+UPDATE
|
# (formerly t/105_cfs_stream.pl)
|
# ---------------------------------------------------------------------
|
{
|
my ($node_publisher, $node_subscriber) =
|
start_pair('stream', 'logical_decoding_work_mem = 64kB');
|
|
$node_publisher->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int PRIMARY KEY, b bytea) WITH (algorithm = 'lz4')"
|
);
|
$node_subscriber->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int PRIMARY KEY, b bytea)");
|
|
setup_streaming_replication($node_publisher, $node_subscriber);
|
|
# A large transaction that must be streamed.
|
$node_publisher->safe_psql(
|
'postgres',
|
q{BEGIN;
|
INSERT INTO test_tab
|
SELECT i, sha256(i::text::bytea) FROM generate_series(1, 5000) s(i);
|
COMMIT;});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
is( $node_subscriber->safe_psql(
|
'postgres',
|
"SELECT count(*), count(*) FILTER (WHERE b = sha256(a::text::bytea))
|
FROM test_tab"),
|
'5000|5000',
|
'streamed transaction fully applied on subscriber');
|
|
# A second large transaction with an interleaved UPDATE load.
|
$node_publisher->safe_psql(
|
'postgres',
|
q{BEGIN;
|
INSERT INTO test_tab
|
SELECT i, sha256(i::text::bytea) FROM generate_series(5001, 10000) s(i);
|
UPDATE test_tab SET b = sha256((a + 1)::text::bytea)
|
WHERE a BETWEEN 1 AND 2500;
|
COMMIT;});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
is( $node_subscriber->safe_psql(
|
'postgres',
|
"SELECT count(*), count(*) FILTER (WHERE b = sha256((a + 1)::text::bytea))
|
FROM test_tab"),
|
'10000|2500',
|
'streamed insert+update transaction applied correctly');
|
|
is( $node_publisher->safe_psql(
|
'postgres',
|
"SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
|
't',
|
'publisher sidecars intact after streaming load');
|
|
$node_subscriber->stop;
|
$node_publisher->stop;
|
}
|
|
# ---------------------------------------------------------------------
|
# Streamed and non-streamed transactions mixing DDL and DML
|
# (formerly t/106_cfs_stream_ddl.pl)
|
# ---------------------------------------------------------------------
|
{
|
my ($node_publisher, $node_subscriber) =
|
start_pair('stream_ddl', 'logical_decoding_work_mem = 64kB');
|
|
# Preexisting content on the publisher
|
$node_publisher->safe_psql('postgres',
|
q{CREATE TABLE test_tab (a int primary key, b varchar) WITH (algorithm = 'lz4')}
|
);
|
$node_publisher->safe_psql('postgres',
|
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
|
|
# Structure on subscriber
|
$node_subscriber->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT, f INT)"
|
);
|
|
setup_streaming_replication($node_publisher, $node_subscriber);
|
|
my $result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*), count(c), count(d = 999) FROM test_tab");
|
is($result, qq(2|0|0),
|
'initial rows present on subscriber after table sync');
|
|
# a small (non-streamed) transaction with DDL and DML
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
|
ALTER TABLE test_tab ADD COLUMN c INT;
|
SAVEPOINT s1;
|
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4);
|
COMMIT;
|
});
|
|
# large (streamed) transaction with DDL and DML
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i FROM generate_series(5, 1000) s(i);
|
ALTER TABLE test_tab ADD COLUMN d INT;
|
SAVEPOINT s1;
|
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i FROM generate_series(1001, 2000) s(i);
|
COMMIT;
|
});
|
|
# a small (non-streamed) transaction with DDL and DML
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab VALUES (2001, sha256(2001::text::bytea), -2001, 2*2001);
|
ALTER TABLE test_tab ADD COLUMN e INT;
|
SAVEPOINT s1;
|
INSERT INTO test_tab VALUES (2002, sha256(2002::text::bytea), -2002, 2*2002, -3*2002);
|
COMMIT;
|
});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
$result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*), count(c), count(d), count(e) FROM test_tab");
|
is($result, qq(2002|1999|1002|1),
|
'data replicated in streaming mode, extra columns contain local defaults'
|
);
|
|
# A large (streamed) transaction with DDL and DML. One of the DDL is
|
# performed after DML to ensure that we invalidate the schema sent
|
# for test_tab so that the next transaction has to send the schema
|
# again.
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i FROM generate_series(2003,5000) s(i);
|
ALTER TABLE test_tab ADD COLUMN f INT;
|
COMMIT;
|
});
|
|
# A small transaction that won't get streamed. This is just to
|
# ensure that we send the schema again to reflect the last column
|
# added in the previous test.
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i, 4*i FROM generate_series(5001,5005) s(i);
|
COMMIT;
|
});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
$result = $node_subscriber->safe_psql('postgres',
|
"SELECT count(*), count(c), count(d), count(e), count(f) FROM test_tab"
|
);
|
is($result, qq(5005|5002|4005|3004|5),
|
'data replicated for both streaming and non-streaming transactions');
|
|
is( $node_publisher->safe_psql(
|
'postgres',
|
"SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
|
't',
|
'publisher sidecars intact after streamed DDL/DML mix');
|
|
$node_subscriber->stop;
|
$node_publisher->stop;
|
}
|
|
# ---------------------------------------------------------------------
|
# Streamed transactions with subtransaction aborts
|
# (formerly t/107_cfs_stream_subxact_abort.pl)
|
# ---------------------------------------------------------------------
|
{
|
my ($node_publisher, $node_subscriber) =
|
start_pair('stream_subxact',
|
'debug_logical_replication_streaming = immediate');
|
|
$node_publisher->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int primary key, b bytea) WITH (algorithm = 'lz4')"
|
);
|
$node_publisher->safe_psql('postgres',
|
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
|
|
$node_subscriber->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int primary key, b bytea)");
|
|
setup_streaming_replication($node_publisher, $node_subscriber);
|
|
my $result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*) FROM test_tab");
|
is($result, qq(2), 'initial rows present on subscriber after table sync');
|
|
# streamed transaction with subtransaction ROLLBACKs: ROLLBACK TO
|
# s1 undoes rows 4-7, so only rows 3, 8, 9 and 10 survive.
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
|
SAVEPOINT s1;
|
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea));
|
SAVEPOINT s2;
|
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea));
|
SAVEPOINT s3;
|
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea));
|
ROLLBACK TO s2;
|
INSERT INTO test_tab VALUES (7, sha256(7::text::bytea));
|
ROLLBACK TO s1;
|
INSERT INTO test_tab VALUES (8, sha256(8::text::bytea));
|
SAVEPOINT s4;
|
INSERT INTO test_tab VALUES (9, sha256(9::text::bytea));
|
SAVEPOINT s5;
|
INSERT INTO test_tab VALUES (10, sha256(10::text::bytea));
|
COMMIT;
|
});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
$result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT string_agg(a::text, ',' ORDER BY a) FROM test_tab");
|
is($result, qq(1,2,3,8,9,10),
|
'rolled-back subtransactions not replicated');
|
|
# streamed transaction rolled back entirely: nothing new appears.
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab VALUES (16, sha256(16::text::bytea));
|
SAVEPOINT s1;
|
INSERT INTO test_tab VALUES (17, sha256(17::text::bytea));
|
SAVEPOINT s2;
|
INSERT INTO test_tab VALUES (18, sha256(18::text::bytea));
|
ROLLBACK;
|
});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
$result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*) FROM test_tab");
|
is($result, qq(6), 'fully rolled-back streamed transaction left no rows');
|
|
is( $node_publisher->safe_psql(
|
'postgres',
|
"SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
|
't',
|
'publisher sidecars intact after streamed aborts');
|
|
$node_subscriber->stop;
|
$node_publisher->stop;
|
}
|
|
# ---------------------------------------------------------------------
|
# A streamed transaction mixing subtransactions, DDL, DML and ROLLBACKs
|
# (formerly t/108_cfs_stream_subxact_ddl_abort.pl)
|
# ---------------------------------------------------------------------
|
{
|
my ($node_publisher, $node_subscriber) =
|
start_pair('stream_subxact_ddl',
|
'debug_logical_replication_streaming = immediate');
|
|
# Preexisting content on the publisher
|
$node_publisher->safe_psql('postgres',
|
q{CREATE TABLE test_tab (a int primary key, b bytea) WITH (algorithm = 'lz4')}
|
);
|
$node_publisher->safe_psql('postgres',
|
"INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
|
|
# Structure on subscriber
|
$node_subscriber->safe_psql('postgres',
|
"CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT)"
|
);
|
|
setup_streaming_replication($node_publisher, $node_subscriber);
|
|
my $result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*), count(c) FROM test_tab");
|
is($result, qq(2|0),
|
'initial rows present on subscriber after table sync');
|
|
# streamed transaction with DDL, DML and ROLLBACKs
|
$node_publisher->safe_psql(
|
'postgres', q{
|
BEGIN;
|
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
|
ALTER TABLE test_tab ADD COLUMN c INT;
|
SAVEPOINT s1;
|
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4);
|
ALTER TABLE test_tab ADD COLUMN d INT;
|
SAVEPOINT s2;
|
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea), -5, 5*2);
|
ALTER TABLE test_tab ADD COLUMN e INT;
|
SAVEPOINT s3;
|
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea), -6, 6*2, -6*3);
|
ALTER TABLE test_tab DROP COLUMN c;
|
ROLLBACK TO s1;
|
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), 4);
|
COMMIT;
|
});
|
|
$node_publisher->wait_for_catchup('tap_sub');
|
|
$result =
|
$node_subscriber->safe_psql('postgres',
|
"SELECT count(*), count(c) FROM test_tab");
|
is($result, qq(4|1),
|
'rollback to savepoint reflected on subscriber, extra columns contain local defaults'
|
);
|
|
is( $node_publisher->safe_psql(
|
'postgres',
|
"SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
|
't',
|
'publisher sidecars intact after streamed subxact/DDL abort');
|
|
$node_subscriber->stop;
|
$node_publisher->stop;
|
}
|
|
done_testing();
|