On Wed, 2 Dec 2026 at 16:45, Nikhil Sontakke <[email protected]> wrote:
>
> Hi,
>
> When a large transaction is streamed, pgoutput sends an Origin message
> with the first streamed block only. The origin it sends,
> txn->origin_id, is copied in ReorderBufferProcessTXN() from the first
> change of the block:
>
> if (streaming)
> {
> txn->origin_id = change->origin_id;
> rb->stream_start(rb, txn, change->lsn);
>
> Not every change carries an origin. Command ID, snapshot and
> invalidation changes never do, and ReorderBufferQueueMessage() does not
> set one for transactional logical messages. So if a streamed
> transaction starts with any of these, it is streamed with no origin at
> all. Common cases:
>
> - a transaction starting with TRUNCATE, which updates the catalogs: the
> XLOG_HEAP2_NEW_CID record comes before the catalog tuple change, and
> its command ID change has no origin;
> - a transaction starting with pg_logical_emit_message(true, ...).
>
> Transactions sent at commit are fine, since they take the origin from
> the commit record. This goes back to 14, when streaming was added.
>
> Reproduction (wal_level = logical):
>
> CREATE TABLE t (a int);
> CREATE PUBLICATION pub FOR TABLE t;
> SELECT pg_replication_origin_create('upstream');
> SELECT pg_create_logical_replication_slot('s', 'pgoutput');
>
> SELECT pg_replication_origin_session_setup('upstream');
> BEGIN;
> TRUNCATE t;
> INSERT INTO t SELECT generate_series(1, 5000);
> COMMIT;
> SELECT pg_replication_origin_session_reset();
>
> SET logical_decoding_work_mem = '64kB';
> SELECT chr(get_byte(data, 0)) AS type, count(*)
> FROM pg_logical_slot_get_binary_changes('s', NULL, NULL,
> 'proto_version', '2', 'publication_names', 'pub',
> 'streaming', 'on')
> WHERE chr(get_byte(data, 0)) IN ('O', 'S')
> GROUP BY 1;
>
> This returns 11 Stream Start messages and no Origin message. With
> INSERT instead of TRUNCATE as the first statement, one Origin message
> is sent.
>
> Impact: the built-in apply worker ignores the Origin message, and
> origin = none filtering is done per change at decode time, so built-in
> logical replication is not affected. Other consumers of the pgoutput
> protocol that rely on the Origin message, for example to avoid
> replicating a change back to where it came from, see such a
> transaction as local.
>
> The attached patch, for master, records the origin on the top-level
> transaction in ReorderBufferQueueChange(), from the first queued change
> that has one, and stops copying it from the first change of each
> block. Every streamable change comes from a WAL record that includes
> the origin, and since bf07ab492c4 a transaction is streamed only once
> it has a streamable change, so the origin is known before the first
> block is sent. The patch also sets the origin of transactional message
> changes; that needs a new origin argument to ReorderBufferQueueMessage().

Thanks for reporting the issue and the patch. I found a case where it
sends an origin that the old code did not.

The origin is recorded on the top-level transaction in
ReorderBufferQueueChange() when a change with an origin is queued, and
it is never removed. If that change belongs to a savepoint that is
rolled back before the transaction is streamed, the changes are
discarded but the origin stays. The first  streamed block then carries
an Origin message, although none of the changes in it has an origin.
Example (with the session origin set):
SELECT pg_replication_origin_session_setup('origin_a');
BEGIN;
SAVEPOINT s;
INSERT INTO t VALUES (1);       -- has the origin
ROLLBACK TO s;
SELECT pg_replication_origin_session_reset();    -- origin is reset
INSERT INTO t SELECT generate_series(1, 5000);   -- streamed
COMMIT;

The transaction is local, and it is decoded with no Origin message
when it is not streamed. Streamed with logical_decoding_work_mem =
64kB, it is sent with the origin origin_a.

Clearing the origin whenever a subtransaction aborts would not be
correct, since a surviving change outside the savepoint may carry the
same origin. The attached test has a second case for that. One way to
fix it would be to keep the origin on the transaction that queued the
change and merge it into  the top-level transaction only when a
subtransaction is assigned or committed.

The attached test (198_stream_origin_aborted_subxact.pl) fails with the patch.

Regards,
Vignesh
# 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();

Reply via email to