# Copyright (c) 2026, PostgreSQL Global Development Group

# The origin of a streamed transaction must not come from changes that were
# rolled back.  The origin is recorded on the top-level transaction when a
# change is queued; if that change belongs to a savepoint that is rolled back
# before the transaction is streamed, the origin must not be sent with the
# first streamed block, as none of the changes in it has an origin.
use strict;
use warnings FATAL => 'all';

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

my $node = PostgreSQL::Test::Cluster->new('publisher');
$node->init(allows_streaming => 'logical');
$node->start;

$node->safe_psql(
	'postgres', q(
	CREATE TABLE t (a int);
	CREATE PUBLICATION pub FOR TABLE t;
	SELECT pg_replication_origin_create('origin_a');
));

# Run a transaction, decode it with streaming and return
# "<number of Origin messages>|<origin names>".  The transaction must be
# streamed in more than one block.
sub streamed_origin
{
	my ($sql) = @_;

	$node->safe_psql('postgres',
		"SELECT pg_create_logical_replication_slot('s', 'pgoutput')");
	$node->safe_psql('postgres', $sql);

	my $result = $node->safe_psql(
		'postgres', q(
	SET logical_decoding_work_mem = '64kB';
	CREATE TEMP TABLE msgs AS
	SELECT chr(get_byte(data, 0)) AS type, data
	FROM pg_logical_slot_get_binary_changes(
		's', NULL, NULL,
		'proto_version', '2',
		'publication_names', 'pub',
		'streaming', 'on');
	SELECT (count(*) FILTER (WHERE type = 'S') > 1) || '|' ||
		   count(*) FILTER (WHERE type = 'O') || '|' ||
		   coalesce(string_agg(convert_from(substr(data, 10, length(data) - 10),
											'UTF8'), ',')
					FILTER (WHERE type = 'O'), '')
	FROM msgs;
));

	$node->safe_psql('postgres',
		"SELECT pg_drop_replication_slot('s'); TRUNCATE t;");
	return $result;
}

# The only change with an origin is rolled back before the transaction is
# streamed, so the streamed transaction has no origin, as when it is not
# streamed.
is( streamed_origin(
		q(
	SELECT pg_replication_origin_session_setup('origin_a');
	BEGIN;
	SAVEPOINT s;
	INSERT INTO t VALUES (1);
	ROLLBACK TO s;
	SELECT pg_replication_origin_session_reset();
	INSERT INTO t SELECT generate_series(1, 5000);
	COMMIT;
)),
	'true|0|',
	'origin of a rolled back savepoint is not sent');

# Control: a change with the origin outside the savepoint stays, so the origin
# is kept after the savepoint is rolled back.
is( streamed_origin(
		q(
	SELECT pg_replication_origin_session_setup('origin_a');
	BEGIN;
	INSERT INTO t VALUES (0);
	SAVEPOINT s;
	INSERT INTO t VALUES (1);
	ROLLBACK TO s;
	INSERT INTO t SELECT generate_series(1, 5000);
	COMMIT;
	SELECT pg_replication_origin_session_reset();
)),
	'true|1|origin_a',
	'origin of a change outside the rolled back savepoint is sent');

# A savepoint that is released, not rolled back, keeps its changes, so its
# origin is sent when the top-level transaction has none of its own.
is( streamed_origin(
		q(
	SELECT pg_replication_origin_session_setup('origin_a');
	BEGIN;
	SAVEPOINT s;
	INSERT INTO t VALUES (1);
	RELEASE s;
	SELECT pg_replication_origin_session_reset();
	INSERT INTO t SELECT generate_series(1, 5000);
	COMMIT;
)),
	'true|1|origin_a',
	'origin of a released savepoint is sent');

$node->stop('fast');
done_testing();
