| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests that logical decoding messages |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Create publisher node |
| 12 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 13 |
$node_publisher->init(allows_streaming => 'logical'); |
| 14 |
$node_publisher->append_conf('postgresql.conf', 'autovacuum = off'); |
| 15 |
$node_publisher->start; |
| 16 |
|
| 17 |
# Create subscriber node |
| 18 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 19 |
$node_subscriber->init; |
| 20 |
$node_subscriber->start; |
| 21 |
|
| 22 |
# Create some preexisting content on publisher |
| 23 |
$node_publisher->safe_psql('postgres', |
| 24 |
"CREATE TABLE tab_test (a int primary key)"); |
| 25 |
|
| 26 |
# Setup structure on subscriber |
| 27 |
$node_subscriber->safe_psql('postgres', |
| 28 |
"CREATE TABLE tab_test (a int primary key)"); |
| 29 |
|
| 30 |
# Setup logical replication |
| 31 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 32 |
$node_publisher->safe_psql('postgres', |
| 33 |
"CREATE PUBLICATION tap_pub FOR TABLE tab_test"); |
| 34 |
|
| 35 |
$node_subscriber->safe_psql('postgres', |
| 36 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 37 |
); |
| 38 |
|
| 39 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 40 |
|
| 41 |
# Ensure a transactional logical decoding message shows up on the slot |
| 42 |
$node_subscriber->safe_psql('postgres', "ALTER SUBSCRIPTION tap_sub DISABLE"); |
| 43 |
|
| 44 |
# wait for the replication slot to become inactive on the publisher |
| 45 |
$node_publisher->poll_query_until( |
| 46 |
'postgres', |
| 47 |
"SELECT COUNT(*) FROM pg_catalog.pg_replication_slots WHERE slot_name = 'tap_sub' AND active='f'", |
| 48 |
1); |
| 49 |
|
| 50 |
$node_publisher->safe_psql('postgres', |
| 51 |
"SELECT pg_logical_emit_message(true, 'pgoutput', 'a transactional message')" |
| 52 |
); |
| 53 |
|
| 54 |
my $result = $node_publisher->safe_psql( |
| 55 |
'postgres', qq( |
| 56 |
SELECT get_byte(data, 0) |
| 57 |
FROM pg_logical_slot_peek_binary_changes('tap_sub', NULL, NULL, |
| 58 |
'proto_version', '1', |
| 59 |
'publication_names', 'tap_pub', |
| 60 |
'messages', 'true') |
| 61 |
)); |
| 62 |
|
| 63 |
# 66 77 67 == B M C == BEGIN MESSAGE COMMIT |
| 64 |
is( $result, qq(66 |
| 65 |
77 |
| 66 |
67), |
| 67 |
'messages on slot are B M C with message option'); |
| 68 |
|
| 69 |
$result = $node_publisher->safe_psql( |
| 70 |
'postgres', qq( |
| 71 |
SELECT get_byte(data, 1), encode(substr(data, 11, 8), 'escape') |
| 72 |
FROM pg_logical_slot_peek_binary_changes('tap_sub', NULL, NULL, |
| 73 |
'proto_version', '1', |
| 74 |
'publication_names', 'tap_pub', |
| 75 |
'messages', 'true') |
| 76 |
OFFSET 1 LIMIT 1 |
| 77 |
)); |
| 78 |
|
| 79 |
is($result, qq(1|pgoutput), |
| 80 |
"flag transactional is set to 1 and prefix is pgoutput"); |
| 81 |
|
| 82 |
$result = $node_publisher->safe_psql( |
| 83 |
'postgres', qq( |
| 84 |
SELECT get_byte(data, 0) |
| 85 |
FROM pg_logical_slot_get_binary_changes('tap_sub', NULL, NULL, |
| 86 |
'proto_version', '1', |
| 87 |
'publication_names', 'tap_pub') |
| 88 |
)); |
| 89 |
|
| 90 |
# no message and no BEGIN and COMMIT because of empty transaction optimization |
| 91 |
is($result, qq(), |
| 92 |
'option messages defaults to false so message (M) is not available on slot' |
| 93 |
); |
| 94 |
|
| 95 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab_test VALUES (1)"); |
| 96 |
|
| 97 |
my $message_lsn = $node_publisher->safe_psql('postgres', |
| 98 |
"SELECT pg_logical_emit_message(false, 'pgoutput', 'a non-transactional message')" |
| 99 |
); |
| 100 |
|
| 101 |
$node_publisher->safe_psql('postgres', "INSERT INTO tab_test VALUES (2)"); |
| 102 |
|
| 103 |
$result = $node_publisher->safe_psql( |
| 104 |
'postgres', qq( |
| 105 |
SELECT get_byte(data, 0), get_byte(data, 1) |
| 106 |
FROM pg_logical_slot_get_binary_changes('tap_sub', NULL, NULL, |
| 107 |
'proto_version', '1', |
| 108 |
'publication_names', 'tap_pub', |
| 109 |
'messages', 'true') |
| 110 |
WHERE lsn = '$message_lsn' AND xid = 0 |
| 111 |
)); |
| 112 |
|
| 113 |
is($result, qq(77|0), 'non-transactional message on slot is M'); |
| 114 |
|
| 115 |
# Ensure a non-transactional logical decoding message shows up on the slot when |
| 116 |
# it is emitted within an aborted transaction. The message won't emit until |
| 117 |
# something advances the LSN, hence, we intentionally forces the server to |
| 118 |
# switch to a new WAL file. |
| 119 |
$node_publisher->safe_psql( |
| 120 |
'postgres', qq( |
| 121 |
BEGIN; |
| 122 |
SELECT pg_logical_emit_message(false, 'pgoutput', |
| 123 |
'a non-transactional message is available even if the transaction is aborted 1'); |
| 124 |
INSERT INTO tab_test VALUES (3); |
| 125 |
SELECT pg_logical_emit_message(true, 'pgoutput', |
| 126 |
'a transactional message is not available if the transaction is aborted'); |
| 127 |
SELECT pg_logical_emit_message(false, 'pgoutput', |
| 128 |
'a non-transactional message is available even if the transaction is aborted 2'); |
| 129 |
ROLLBACK; |
| 130 |
SELECT pg_switch_wal(); |
| 131 |
)); |
| 132 |
|
| 133 |
$result = $node_publisher->safe_psql( |
| 134 |
'postgres', qq( |
| 135 |
SELECT get_byte(data, 0), get_byte(data, 1) |
| 136 |
FROM pg_logical_slot_peek_binary_changes('tap_sub', NULL, NULL, |
| 137 |
'proto_version', '1', |
| 138 |
'publication_names', 'tap_pub', |
| 139 |
'messages', 'true') |
| 140 |
)); |
| 141 |
|
| 142 |
is( $result, qq(77|0 |
| 143 |
77|0), |
| 144 |
'non-transactional message on slot from aborted transaction is M'); |
| 145 |
|
| 146 |
$node_subscriber->stop('fast'); |
| 147 |
$node_publisher->stop('fast'); |
| 148 |
|
| 149 |
done_testing(); |