From 493cb5cd74b2dbc2c92bf0377f89ee2c00effde4 Mon Sep 17 00:00:00 2001
From: Greg Nancarrow <gregn4422@gmail.com>
Date: Mon, 21 Jun 2021 21:20:53 +1000
Subject: [PATCH] Remove useless int64 range checks on BIGINT sequence
 MINVALUE/MAXVALUE values.

Remove checks that test whether BIGINT sequence MINVALUE/MAXVALUE values are
outside of the range [PG_INT64_MIN,PG_INT64_MAX].  These checks don't work
(i.e. always evaluate to false) as the values are held in an "int64" which can't
hold values outside that range. BIGINT sequence MINVALUE/MAXVALUE values are
anyway getting int64 range-checked prior to these existing checks, when
converted to int64 by defGetInt64().
---
 src/backend/commands/sequence.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 0415df9ccb..a39a68884e 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -1460,9 +1460,14 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqdataform->log_cnt = 0;
 	}
 
+	/*
+	 * Validate that the (int64) seqmax value is within [min,max] range,
+	 * according to the sequence data type. In the case of BIGINT
+	 * (seqtypid==INT8OID), seqmax can't hold an out-of-range value, so
+	 * no further checking is needed here.
+	 */
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
-		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX))
-		|| (seqform->seqtypid == INT8OID && (seqform->seqmax < PG_INT64_MIN || seqform->seqmax > PG_INT64_MAX)))
+		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
 	{
 		char		bufx[100];
 
@@ -1497,9 +1502,14 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqdataform->log_cnt = 0;
 	}
 
+	/*
+	 * Validate that the (int64) seqmin value is within [min,max] range,
+	 * according to the sequence data type. In the case of BIGINT
+	 * (seqtypid==INT8OID), seqmin can't hold an out-of-range value, so no
+	 * further checking is needed here.
+	 */
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
-		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX))
-		|| (seqform->seqtypid == INT8OID && (seqform->seqmin < PG_INT64_MIN || seqform->seqmin > PG_INT64_MAX)))
+		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
 	{
 		char		bufm[100];
 
-- 
2.27.0

