# Copyright (c) 2026, PostgreSQL Global Development Group use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('utf8_slice'); $node->init( allows_streaming => 'logical', extra => [ '--locale=C', '--encoding=UTF8' ]); $node->append_conf('postgresql.conf', "output_plugin_libraries = 'dbcopies_decoding'\n"); $node->start; is($node->safe_psql('postgres', 'SHOW server_encoding'), 'UTF8', 'server encoding is UTF8'); $node->safe_psql('postgres', 'CREATE TABLE utf8_slice_test (val text)'); my $smiley = pack('C*', 0xF0, 0x9F, 0x98, 0x80); sub exercise_slice_size { local $Test::Builder::Level = $Test::Builder::Level + 1; my ($slot_name, $slice_size, $expect_success, $test_name) = @_; my $log_offset; my ($ret, $stdout, $stderr); $node->safe_psql('postgres', "SELECT pg_create_logical_replication_slot('$slot_name', 'dbcopies_decoding')" ); $node->safe_psql('postgres', q{INSERT INTO utf8_slice_test VALUES (U&'\+01F600')}); $log_offset = -s $node->logfile; ($ret, $stdout, $stderr) = $node->psql( 'postgres', qq[ SELECT data FROM pg_logical_slot_peek_changes('$slot_name', NULL, NULL, 'include-xids', '0', 'slice_size', '$slice_size'); ], on_error_die => 0); if ($expect_success) { is($ret, 0, "$test_name succeeds"); is($stderr, '', "$test_name emits no stderr"); ok(index($stdout, $smiley) >= 0, "$test_name returns the full UTF-8 character"); } else { isnt($ret, 0, "$test_name fails cleanly"); like($stderr, qr/invalid string value/, "$test_name reports invalid string value"); unlike($stderr, qr/invalid byte sequence for encoding "UTF8"/, "$test_name does not emit a UTF-8 byte-sequence error"); unlike( $stderr, qr/(?:PANIC:|lost connection to server|server closed the connection unexpectedly|terminating connection because of crash of another server process)/, "$test_name keeps the backend connection alive"); } $node->log_check( $test_name, $log_offset, log_unlike => [ qr/invalid byte sequence for encoding "UTF8"/, qr/PANIC:/, qr/server process .* was terminated/, qr/terminating connection because of crash of another server process/, ]); $node->safe_psql('postgres', "SELECT pg_drop_replication_slot('$slot_name')"); } exercise_slice_size( 'utf8_slice_ok', 8, 1, 'control case with slice_size=8'); exercise_slice_size( 'utf8_slice_boundary', 4, 0, 'boundary case with slice_size=4'); $node->stop; done_testing();