From 1d4292c051526d6d4704ce2a95597a1da82bebbb Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Tue, 11 Feb 2020 20:12:20 -0300
Subject: [PATCH v11 4/4] Various tweaks, mostly naming

Got rid of TYPCATEGORY_MULTIRANGE. It doesn't serve any purpose that I
can see, so I just switched that to TYPCATEGORY_RANGE.

load_multirangetype_info() pointlessly open-coded
get_range_multirange_subtype().  Simplify.

Name changes:

get_multirange_subtype -> get_range_multirange_subtype
  This is more in line with how we name lsyscache.c functions, given
  that the cache it serves is on pg_range.  (Apparently in earlier
  versions of the patch there was a pg_multirange catalog, but not
  anymore)

MULTIRANGETYPE -> RANGEMULTIRANGE (the syscache)
  Mostly the same reasons as above.  The naming also makes us move it
  next to RANGETYPE; they're both in pg_range.  (It was in the place
  where pg_multirange would be, alphabetically.)
---
 src/backend/commands/typecmds.c     |  2 +-
 src/backend/parser/parse_coerce.c   | 10 +++++-----
 src/backend/utils/cache/lsyscache.c | 10 ++++------
 src/backend/utils/cache/syscache.c  | 23 ++++++++++++-----------
 src/backend/utils/cache/typcache.c  | 15 ++-------------
 src/backend/utils/fmgr/funcapi.c    |  4 ++--
 src/include/catalog/pg_type.dat     | 12 ++++++------
 src/include/catalog/pg_type.h       |  1 -
 src/include/utils/lsyscache.h       |  2 +-
 src/include/utils/syscache.h        |  2 +-
 src/include/utils/typcache.h        |  2 +-
 11 files changed, 35 insertions(+), 48 deletions(-)

diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c
index a8dea8d58e..bd792234f8 100644
--- a/src/backend/commands/typecmds.c
+++ b/src/backend/commands/typecmds.c
@@ -1589,7 +1589,7 @@ DefineRange(CreateRangeStmt *stmt)
 				   GetUserId(), /* owner's ID */
 				   -1,			/* internal size (always varlena) */
 				   TYPTYPE_MULTIRANGE,	/* type-type (multirange type) */
-				   TYPCATEGORY_MULTIRANGE,	/* type-category (multirange type) */
+				   TYPCATEGORY_RANGE,	/* type-category (range type) */
 				   false,		/* multirange types are never preferred */
 				   DEFAULT_TYPDELIM,	/* array element delimiter */
 				   F_MULTIRANGE_IN, /* input procedure */
diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c
index ff1310c8f4..27bfc4bf8c 100644
--- a/src/backend/parser/parse_coerce.c
+++ b/src/backend/parser/parse_coerce.c
@@ -1599,7 +1599,7 @@ check_generic_type_consistency(const Oid *actual_arg_types,
 	/* Get the element type based on the multirange type, if we have one */
 	if (OidIsValid(multirange_typeid))
 	{
-		multirange_typelem = get_multirange_subtype(multirange_typeid);
+		multirange_typelem = get_range_multirange_subtype(multirange_typeid);
 		if (!OidIsValid(multirange_typelem))
 			return false;		/* should be a multirange, but isn't */
 
@@ -1938,7 +1938,7 @@ enforce_generic_type_consistency(const Oid *actual_arg_types,
 		}
 		else
 		{
-			multirange_typelem = get_multirange_subtype(multirange_typeid);
+			multirange_typelem = get_range_multirange_subtype(multirange_typeid);
 			if (!OidIsValid(multirange_typelem))
 				ereport(ERROR,
 						(errcode(ERRCODE_DATATYPE_MISMATCH),
@@ -2202,7 +2202,7 @@ resolve_generic_type(Oid declared_type,
 		else if (context_declared_type == ANYMULTIRANGEOID)
 		{
 			Oid			context_base_type = getBaseType(context_actual_type);
-			Oid			multirange_typelem = get_multirange_subtype(context_base_type);
+			Oid			multirange_typelem = get_range_multirange_subtype(context_base_type);
 			Oid			range_typelem = get_range_subtype(multirange_typelem);
 			Oid			array_typeid = get_array_type(range_typelem);
 
@@ -2248,7 +2248,7 @@ resolve_generic_type(Oid declared_type,
 		{
 			/* Use the element type corresponding to actual type */
 			Oid			context_base_type = getBaseType(context_actual_type);
-			Oid			multirange_typelem = get_multirange_subtype(context_base_type);
+			Oid			multirange_typelem = get_range_multirange_subtype(context_base_type);
 
 			if (!OidIsValid(multirange_typelem))
 				ereport(ERROR,
@@ -2281,7 +2281,7 @@ resolve_generic_type(Oid declared_type,
 		else if (context_declared_type == ANYMULTIRANGEOID)
 		{
 			Oid			context_base_type = getBaseType(context_actual_type);
-			Oid			multirange_typelem = get_multirange_subtype(context_base_type);
+			Oid			multirange_typelem = get_range_multirange_subtype(context_base_type);
 
 			if (!OidIsValid(multirange_typelem))
 				ereport(ERROR,
diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c
index 56a145c666..7ba93c43d7 100644
--- a/src/backend/utils/cache/lsyscache.c
+++ b/src/backend/utils/cache/lsyscache.c
@@ -3133,7 +3133,7 @@ get_namespace_name_or_temp(Oid nspid)
 		return get_namespace_name(nspid);
 }
 
-/*				---------- PG_RANGE CACHE ----------				 */
+/*				---------- PG_RANGE CACHES ----------				 */
 
 /*
  * get_range_subtype
@@ -3211,20 +3211,18 @@ get_range_multirange(Oid rangeOid)
 		return InvalidOid;
 }
 
-/*				---------- PG_MULTIRANGE CACHE ----------				 */
-
 /*
- * get_multirange_subtype
+ * get_range_multirange_subtype
  *		Returns the subtype of a given multirange type
  *
  * Returns InvalidOid if the type is not a multirange type.
  */
 Oid
-get_multirange_subtype(Oid multirangeOid)
+get_range_multirange_subtype(Oid multirangeOid)
 {
 	HeapTuple	tp;
 
-	tp = SearchSysCache1(MULTIRANGETYPE, ObjectIdGetDatum(multirangeOid));
+	tp = SearchSysCache1(RANGEMULTIRANGE, ObjectIdGetDatum(multirangeOid));
 	if (HeapTupleIsValid(tp))
 	{
 		Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tp);
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index fb9179c42a..7654331a59 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -508,17 +508,6 @@ static const struct cachedesc cacheinfo[] = {
 		},
 		4
 	},
-	{RangeRelationId,			/* MULTIRANGE */
-		RangeMultirangeTypidIndexId,
-		1,
-		{
-			Anum_pg_range_rngmultitypid,
-			0,
-			0,
-			0
-		},
-		4
-	},
 	{NamespaceRelationId,		/* NAMESPACENAME */
 		NamespaceNameIndexId,
 		1,
@@ -662,6 +651,18 @@ static const struct cachedesc cacheinfo[] = {
 		},
 		64
 	},
+	{RangeRelationId,			/* RANGEMULTIRANGE */
+		RangeMultirangeTypidIndexId,
+		1,
+		{
+			Anum_pg_range_rngmultitypid,
+			0,
+			0,
+			0
+		},
+		4
+	},
+
 	{RangeRelationId,			/* RANGETYPE */
 		RangeTypidIndexId,
 		1,
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 6677bc8bdc..9383b24f65 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -897,7 +897,6 @@ load_rangetype_info(TypeCacheEntry *typentry)
 	typentry->rngelemtype = lookup_type_cache(subtypeOid, 0);
 }
 
-
 /*
  * load_multirangetype_info --- helper routine to set up multirange type
  * information
@@ -905,26 +904,16 @@ load_rangetype_info(TypeCacheEntry *typentry)
 static void
 load_multirangetype_info(TypeCacheEntry *typentry)
 {
-	Form_pg_range pg_range;
-	HeapTuple	tup;
 	Oid			rangetypeOid;
 
-	/* get information from pg_range */
-	tup = SearchSysCache1(MULTIRANGETYPE, ObjectIdGetDatum(typentry->type_id));
-	/* should not fail, since we already checked typtype ... */
-	if (!HeapTupleIsValid(tup))
+	rangetypeOid = get_range_multirange_subtype(typentry->type_id);
+	if (!OidIsValid(rangetypeOid))
 		elog(ERROR, "cache lookup failed for multirange type %u",
 			 typentry->type_id);
-	pg_range = (Form_pg_range) GETSTRUCT(tup);
-
-	rangetypeOid = pg_range->rngtypid;
-
-	ReleaseSysCache(tup);
 
 	typentry->rngtype = lookup_type_cache(rangetypeOid, TYPECACHE_RANGE_INFO);
 }
 
-
 /*
  * load_domaintype_info --- helper routine to set up domain constraint info
  *
diff --git a/src/backend/utils/fmgr/funcapi.c b/src/backend/utils/fmgr/funcapi.c
index defc801f7a..afc617939f 100644
--- a/src/backend/utils/fmgr/funcapi.c
+++ b/src/backend/utils/fmgr/funcapi.c
@@ -683,7 +683,7 @@ resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
 											  ANYRANGEOID);
 
 			/* check for inconsistent range and multirange results */
-			rngtype = get_multirange_subtype(mltrngtype);
+			rngtype = get_range_multirange_subtype(mltrngtype);
 
 			if (OidIsValid(anyrange_type) && anyrange_type != rngtype)
 				return false;
@@ -1020,7 +1020,7 @@ resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
 											  ANYRANGEOID);
 
 			/* check for inconsistent range and multirange results */
-			rngtype = get_multirange_subtype(mltrngtype);
+			rngtype = get_range_multirange_subtype(mltrngtype);
 
 			if (OidIsValid(anyrange_type) && anyrange_type != rngtype)
 				return false;
diff --git a/src/include/catalog/pg_type.dat b/src/include/catalog/pg_type.dat
index beaa70dce6..7d4bf4c5d8 100644
--- a/src/include/catalog/pg_type.dat
+++ b/src/include/catalog/pg_type.dat
@@ -489,34 +489,34 @@
 # multirange types
 { oid => '8009', array_type_oid => '8010', descr => 'multirange of integers',
   typname => 'int4multirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'i', typstorage => 'x' },
 { oid => '8011', array_type_oid => '8012', descr => 'multirange of numerics',
   typname => 'nummultirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'i', typstorage => 'x' },
 { oid => '8013', array_type_oid => '8014',
   descr => 'multirange of timestamps without time zone',
   typname => 'tsmultirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'd', typstorage => 'x' },
 { oid => '8015', array_type_oid => '8016',
   descr => 'multirange of timestamps with time zone',
   typname => 'tstzmultirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'd', typstorage => 'x' },
 { oid => '8017', array_type_oid => '8018', descr => 'multirange of dates',
   typname => 'datemultirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'i', typstorage => 'x' },
 { oid => '8019', array_type_oid => '8020', descr => 'multirange of bigints',
   typname => 'int8multirange', typlen => '-1', typbyval => 'f', typtype => 'm',
-  typcategory => 'M', typinput => 'multirange_in', typoutput => 'multirange_out',
+  typcategory => 'R', typinput => 'multirange_in', typoutput => 'multirange_out',
   typreceive => 'multirange_recv', typsend => 'multirange_send',
   typanalyze => 'multirange_typanalyze', typalign => 'd', typstorage => 'x' },
 
diff --git a/src/include/catalog/pg_type.h b/src/include/catalog/pg_type.h
index 600ac58c9d..c113320b9c 100644
--- a/src/include/catalog/pg_type.h
+++ b/src/include/catalog/pg_type.h
@@ -270,7 +270,6 @@ typedef FormData_pg_type *Form_pg_type;
 #define  TYPCATEGORY_ENUM		'E'
 #define  TYPCATEGORY_GEOMETRIC	'G'
 #define  TYPCATEGORY_NETWORK	'I' /* think INET */
-#define  TYPCATEGORY_MULTIRANGE	'M'
 #define  TYPCATEGORY_NUMERIC	'N'
 #define  TYPCATEGORY_PSEUDOTYPE 'P'
 #define  TYPCATEGORY_RANGE		'R'
diff --git a/src/include/utils/lsyscache.h b/src/include/utils/lsyscache.h
index 7d1674a3b6..fffd4c5a45 100644
--- a/src/include/utils/lsyscache.h
+++ b/src/include/utils/lsyscache.h
@@ -182,7 +182,7 @@ extern char *get_namespace_name_or_temp(Oid nspid);
 extern Oid	get_range_subtype(Oid rangeOid);
 extern Oid	get_range_collation(Oid rangeOid);
 extern Oid	get_range_multirange(Oid rangeOid);
-extern Oid	get_multirange_subtype(Oid multirangeOid);
+extern Oid	get_range_multirange_subtype(Oid multirangeOid);
 extern Oid	get_index_column_opclass(Oid index_oid, int attno);
 
 #define type_is_array(typid)  (get_element_type(typid) != InvalidOid)
diff --git a/src/include/utils/syscache.h b/src/include/utils/syscache.h
index bc51bbeb54..e8f393520a 100644
--- a/src/include/utils/syscache.h
+++ b/src/include/utils/syscache.h
@@ -66,7 +66,6 @@ enum SysCacheIdentifier
 	INDEXRELID,
 	LANGNAME,
 	LANGOID,
-	MULTIRANGETYPE,
 	NAMESPACENAME,
 	NAMESPACEOID,
 	OPERNAMENSP,
@@ -80,6 +79,7 @@ enum SysCacheIdentifier
 	PUBLICATIONOID,
 	PUBLICATIONREL,
 	PUBLICATIONRELMAP,
+	RANGEMULTIRANGE,
 	RANGETYPE,
 	RELNAMENSP,
 	RELOID,
diff --git a/src/include/utils/typcache.h b/src/include/utils/typcache.h
index fe240ea004..9957be4640 100644
--- a/src/include/utils/typcache.h
+++ b/src/include/utils/typcache.h
@@ -101,7 +101,7 @@ typedef struct TypeCacheEntry
 	/*
 	 * Fields computed when TYPCACHE_MULTIRANGE_INFO is required.
 	 */
-	struct TypeCacheEntry *rngtype;
+	struct TypeCacheEntry *rngtype;		/* underlying range type */
 
 	/*
 	 * Domain's base type and typmod if it's a domain type.  Zeroes if not
-- 
2.20.1

