# Logical replication from CFS-compressed tables. # # The publisher stores its tables compressed (lz4/zstd); the subscriber # holds plain uncompressed twins. Each scenario below uses a fresh # publisher/subscriber pair and exercises one storage event on the # publisher side -- a server restart, a heap rewrite, a TRUNCATE, # partition DETACH/ATTACH -- checking that the replication stream keeps # delivering correct rows and that the publisher's CFS sidecars 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) and a plain subscriber, # named after the scenario so sections don't clash. sub start_pair { my ($name) = @_; my $pub = PostgreSQL::Test::Cluster->new("pub_$name"); $pub->init(allows_streaming => 'logical'); $pub->start; my $sub = PostgreSQL::Test::Cluster->new("sub_$name"); $sub->init; $sub->start; return ($pub, $sub); } # Create a publication on the publisher and the matching subscription on # the subscriber, then wait for the initial table sync. sub setup_replication { my ($pub, $sub, $pub_name, $sub_name, $for_clause) = @_; my $connstr = $pub->connstr . ' dbname=postgres'; $pub->safe_psql('postgres', "CREATE PUBLICATION $pub_name $for_clause"); $sub->safe_psql('postgres', "CREATE SUBSCRIPTION $sub_name CONNECTION '$connstr' PUBLICATION $pub_name" ); $sub->wait_for_subscription_sync($pub, $sub_name); } # --------------------------------------------------------------------- # Bulk changes, a large toasted value and a publisher restart # (formerly t/101_cfs_rep_changes.pl) # --------------------------------------------------------------------- { my ($node_publisher, $node_subscriber) = start_pair('changes'); # CFS table on the publisher, plain twin schema on the subscriber. $node_publisher->safe_psql('postgres', "CREATE TABLE rep_tab (a int PRIMARY KEY, b text) WITH (algorithm = 'lz4')" ); $node_subscriber->safe_psql('postgres', "CREATE TABLE rep_tab (a int PRIMARY KEY, b text)"); setup_replication($node_publisher, $node_subscriber, 'tap_pub', 'tap_sub', 'FOR TABLE rep_tab'); # Bulk rows plus one large, deliberately poorly compressible value # that is pushed out-of-line into the toast relation. $node_publisher->safe_psql( 'postgres', q{INSERT INTO rep_tab SELECT g, md5(g::text) FROM generate_series(1, 1000) g; INSERT INTO rep_tab SELECT 5000, (SELECT string_agg(md5(i::text), '') FROM generate_series(1, 16000) i);}); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT count(*), count(*) FILTER (WHERE b = md5(a::text)) FROM rep_tab WHERE a <= 1000"), '1000|1000', 'bulk rows replicated'); is( $node_subscriber->safe_psql( 'postgres', "SELECT length(b) FROM rep_tab WHERE a = 5000"), '512000', 'large toasted value replicated'); # Restart the publisher, then write more; the stream must continue. $node_publisher->restart; $node_publisher->safe_psql('postgres', "INSERT INTO rep_tab SELECT g, md5(g::text) FROM generate_series(1001, 2000) g" ); $node_publisher->safe_psql('postgres', "UPDATE rep_tab SET b = md5('updated') WHERE a = 5000"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT count(*) FROM rep_tab"), '2001', 'rows after publisher restart replicated'); is( $node_subscriber->safe_psql( 'postgres', "SELECT b = md5('updated') FROM rep_tab WHERE a = 5000"), 't', 'update after publisher restart replicated'); # The publisher's CFS storage must be sound after all of it. is( $node_publisher->safe_psql( 'postgres', "SELECT ok FROM pg_cfs_check('rep_tab'::regclass, true)"), 't', 'publisher sidecars intact'); $node_subscriber->stop; $node_publisher->stop; } # --------------------------------------------------------------------- # Heap rewrite (ADD COLUMN with a volatile default) under replication # (formerly t/102_cfs_rewrite.pl) # --------------------------------------------------------------------- { my ($node_publisher, $node_subscriber) = start_pair('rewrite'); $node_publisher->safe_psql('postgres', "CREATE TABLE test1 (a int, b text) WITH (algorithm = 'lz4');"); $node_subscriber->safe_psql('postgres', "CREATE TABLE test1 (a int, b text);"); setup_replication($node_publisher, $node_subscriber, 'mypub', 'mysub', 'FOR ALL TABLES'); $node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');}); $node_publisher->wait_for_catchup('mysub'); is( $node_subscriber->safe_psql('postgres', q{SELECT a, b FROM test1}), qq(1|one 2|two), 'initial data replicated to subscriber'); # DDL that causes a heap rewrite (and a CFS sidecar swap). A # constant DEFAULT would be a fast default without rewrite since # PG11, so use a serial column: its volatile nextval() default # forces the rewrite. my $ddl = "ALTER TABLE test1 ADD c serial;"; $node_subscriber->safe_psql('postgres', $ddl); $node_publisher->safe_psql('postgres', $ddl); $node_publisher->wait_for_catchup('mysub'); $node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b, c) VALUES (3, 'three', 33);}); $node_publisher->wait_for_catchup('mysub'); is( $node_subscriber->safe_psql('postgres', q{SELECT a, b, c FROM test1}), qq(1|one|1 2|two|2 3|three|33), 'data replicated to subscriber after heap rewrite'); # The rewritten table's new sidecars must be structurally valid. is( $node_publisher->safe_psql( 'postgres', "SELECT ok FROM pg_cfs_check('test1'::regclass, true)"), 't', 'publisher sidecars intact after rewrite'); $node_subscriber->stop; $node_publisher->stop; } # --------------------------------------------------------------------- # TRUNCATE of CFS tables under replication # (formerly t/103_cfs_truncate.pl) # --------------------------------------------------------------------- { my ($node_publisher, $node_subscriber) = start_pair('truncate'); $node_publisher->safe_psql( 'postgres', q{CREATE TABLE tab1 (a int) WITH (algorithm = 'lz4'); CREATE TABLE tab2 (a int) WITH (algorithm = 'zstd');}); $node_subscriber->safe_psql('postgres', "CREATE TABLE tab1 (a int); CREATE TABLE tab2 (a int);"); setup_replication($node_publisher, $node_subscriber, 'tap_pub', 'tap_sub', 'FOR TABLE tab1, tab2'); $node_publisher->safe_psql('postgres', "INSERT INTO tab1 SELECT generate_series(1, 100)"); $node_publisher->safe_psql('postgres', "INSERT INTO tab2 SELECT generate_series(1, 100)"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1"), '100', 'tab1 synced'); # Truncate both tables in one command; the subscriber must follow. $node_publisher->safe_psql('postgres', "TRUNCATE tab1, tab2"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT (SELECT count(*) FROM tab1) + (SELECT count(*) FROM tab2)" ), '0', 'truncate replicated'); # Writes after the truncate flow again; the fresh sidecars are # valid. $node_publisher->safe_psql('postgres', "INSERT INTO tab1 SELECT generate_series(1, 10)"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1"), '10', 'writes after truncate replicated'); is( $node_publisher->safe_psql( 'postgres', "SELECT ok FROM pg_cfs_check('tab1'::regclass, true)" ), 't', 'tab1 sidecars intact after truncate'); $node_subscriber->stop; $node_publisher->stop; } # --------------------------------------------------------------------- # Replication from a partitioned table with CFS partitions, including # DETACH/ATTACH PARTITION (formerly t/104_cfs_partition.pl) # --------------------------------------------------------------------- { my ($node_publisher, $node_subscriber) = start_pair('partition'); # The partitions are CFS relations with different policies (the # root carries an lz4 default, one leaf overrides it with zstd). $node_publisher->safe_psql( 'postgres', q{CREATE TABLE tab_part (a int) PARTITION BY RANGE (a) WITH (algorithm = 'lz4'); CREATE TABLE tab_part_1 PARTITION OF tab_part FOR VALUES FROM (1) TO (1000); CREATE TABLE tab_part_2 PARTITION OF tab_part FOR VALUES FROM (1000) TO (2000) WITH (algorithm = 'zstd');}); $node_subscriber->safe_psql( 'postgres', q{CREATE TABLE tab_part (a int) PARTITION BY RANGE (a); CREATE TABLE tab_part_1 PARTITION OF tab_part FOR VALUES FROM (1) TO (1000); CREATE TABLE tab_part_2 PARTITION OF tab_part FOR VALUES FROM (1000) TO (2000);} ); setup_replication($node_publisher, $node_subscriber, 'tap_pub', 'tap_sub', 'FOR TABLE tab_part WITH (publish_via_partition_root = true)'); $node_publisher->safe_psql('postgres', "INSERT INTO tab_part SELECT generate_series(1, 1500)"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT count(*) FROM tab_part"), '1500', 'inserts into both CFS partitions replicated'); # DETACH: further writes to the detached partition are no longer # published. $node_publisher->safe_psql('postgres', "ALTER TABLE tab_part DETACH PARTITION tab_part_2"); $node_publisher->safe_psql('postgres', "INSERT INTO tab_part_2 SELECT generate_series(1501, 1600)"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT count(*) FROM tab_part_1"), '999', 'partition 1 unchanged after detach'); # ATTACH back: the partition rejoins the publication. $node_publisher->safe_psql('postgres', "ALTER TABLE tab_part ATTACH PARTITION tab_part_2 FOR VALUES FROM (1000) TO (2000)" ); $node_publisher->safe_psql('postgres', "INSERT INTO tab_part VALUES (1601)"); $node_publisher->wait_for_catchup('tap_sub'); is( $node_subscriber->safe_psql( 'postgres', "SELECT count(*) FROM tab_part_2"), '502', 'partition 2 receives writes after attach'); # The publisher's partition sidecars must be structurally valid. is( $node_publisher->safe_psql( 'postgres', "SELECT string_agg((pg_cfs_check(oid, true)).ok::text, ',' ORDER BY relname) FROM pg_class WHERE relname IN ('tab_part_1', 'tab_part_2')"), 'true,true', 'partition sidecars intact'); $node_subscriber->stop; $node_publisher->stop; } done_testing();