From aebe4fa0893f8094c0743172cc2d17efece3d45b Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Tue, 28 Nov 2023 21:11:00 +0100
Subject: [PATCH v20231128 6/8] subxact test

---
 src/test/subscription/t/034_sequences.pl | 35 ++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/src/test/subscription/t/034_sequences.pl b/src/test/subscription/t/034_sequences.pl
index d4290c1c69b..4fb69d71282 100644
--- a/src/test/subscription/t/034_sequences.pl
+++ b/src/test/subscription/t/034_sequences.pl
@@ -45,6 +45,14 @@ $node_publisher->safe_psql('postgres',
 $node_publisher->safe_psql('postgres',
 	"ALTER PUBLICATION seq_pub ADD SEQUENCE s");
 
+# function that alters the sequence in a completed subtransaction
+$node_publisher->safe_psql('postgres',
+	"CREATE FUNCTION regress_alter_sequence(s regclass, v integer) RETURNS void as \$\$
+BEGIN
+EXECUTE format('alter sequence %s restart %s', s, v);
+EXCEPTION WHEN others THEN NULL;
+END; \$\$ LANGUAGE plpgsql");
+
 $node_subscriber->safe_psql('postgres',
 	"CREATE SUBSCRIPTION seq_sub CONNECTION '$publisher_connstr' PUBLICATION seq_pub"
 );
@@ -227,4 +235,31 @@ $result = $node_subscriber->safe_psql(
 is( $result, '1098|0|t',
 	'alter sequence twice in a subtransaction');
 
+
+# alter the sequence in a subtransaction that commits, followed by advancing
+# the sequence in a subtransaction that gets rolled back, but then the main
+# one commits - the changes should still be replicated, because they belong
+# to the alter, which was in the committed part
+$node_publisher->safe_psql(
+	'postgres', qq(
+	BEGIN;
+	SELECT regress_alter_sequence('s', 1000); -- reset to 1000
+	SAVEPOINT s1;
+	INSERT INTO seq_test SELECT nextval('s') FROM generate_series(1,100);
+	ROLLBACK TO s1;
+	COMMIT;
+));
+
+$node_publisher->wait_for_catchup('seq_sub');
+
+# Check the data on subscriber
+$result = $node_subscriber->safe_psql(
+	'postgres', qq(
+	SELECT * FROM s;
+));
+
+is( $result, '1131|0|t',
+	'alter sequence in a subtransaction');
+
+
 done_testing();
-- 
2.41.0

