From abecdc53a05b0300abfdf0c3d2356dcbe8a3a766 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 1 Dec 2023 10:08:17 +0900
Subject: [PATCH v1 3/9] Group more closely local sequence cache updates

Previously, some updates of the informations for SeqTable entries was
mixed in the middle of computations.  Grouping them makes the code
easier to follow and split later on.
---
 src/backend/commands/sequence.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 117518d480..ef71efdb82 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -489,10 +489,6 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
 				seqform, newdataform,
 				&need_seq_rewrite, &owned_by);
 
-	/* Clear local cache so that we don't think we have cached numbers */
-	/* Note that we do not change the currval() state */
-	elm->cached = elm->last;
-
 	/* If needed, rewrite the sequence relation itself */
 	if (need_seq_rewrite)
 	{
@@ -520,6 +516,10 @@ AlterSequence(ParseState *pstate, AlterSeqStmt *stmt)
 		fill_seq_with_data(seqrel, newdatatuple);
 	}
 
+	/* Clear local cache so that we don't think we have cached numbers */
+	/* Note that we do not change the currval() state */
+	elm->cached = elm->last;
+
 	/* process OWNED BY if given */
 	if (owned_by)
 		process_owned_by(seqrel, owned_by, stmt->for_identity);
@@ -683,7 +683,6 @@ nextval_internal(Oid relid, bool check_permissions)
 	seq = read_seq_tuple(seqrel, &buf, &seqdatatuple);
 	page = BufferGetPage(buf);
 
-	elm->increment = incby;
 	last = next = result = seq->last_value;
 	fetch = cache;
 	log = seq->log_cnt;
@@ -781,6 +780,7 @@ nextval_internal(Oid relid, bool check_permissions)
 	Assert(log >= 0);
 
 	/* save info in local cache */
+	elm->increment = incby;
 	elm->last = result;			/* last returned number */
 	elm->cached = last;			/* last fetched number */
 	elm->last_valid = true;
-- 
2.43.0

