From a0cd991987bd23d79d5d273e7712afe40dba554b Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 25 Oct 2001 14:10:07 +0000
Subject: [PATCH] Add int2-to-int8 and int8-to-int2 conversion routines. 
 Needed to avoid breaking existing pg_dump scripts, which try to assign the
 result of count(*) to an int2 variable.  catversion bumped.

---
 src/backend/utils/adt/int8.c     | 25 ++++++++++++++++++++++++-
 src/include/catalog/catversion.h |  4 ++--
 src/include/catalog/pg_proc.h    |  7 ++++++-
 src/include/utils/int8.h         |  5 ++++-
 4 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c
index 41a60615884..b689cb764a4 100644
--- a/src/backend/utils/adt/int8.c
+++ b/src/backend/utils/adt/int8.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.34 2001/10/25 05:49:44 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.35 2001/10/25 14:10:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -697,6 +697,29 @@ int84(PG_FUNCTION_ARGS)
 	PG_RETURN_INT32(result);
 }
 
+Datum
+int28(PG_FUNCTION_ARGS)
+{
+	int16		val = PG_GETARG_INT16(0);
+
+	PG_RETURN_INT64((int64) val);
+}
+
+Datum
+int82(PG_FUNCTION_ARGS)
+{
+	int64		val = PG_GETARG_INT64(0);
+	int16		result;
+
+	result = (int16) val;
+
+	/* Test for overflow by reverse-conversion. */
+	if ((int64) result != val)
+		elog(ERROR, "int8 conversion to int2 is out of range");
+
+	PG_RETURN_INT16(result);
+}
+
 Datum
 i8tod(PG_FUNCTION_ARGS)
 {
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index c51934dac0c..dedd4695162 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: catversion.h,v 1.100 2001/10/25 05:49:56 momjian Exp $
+ * $Id: catversion.h,v 1.101 2001/10/25 14:10:06 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,5 +53,5 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	200110181
+#define CATALOG_VERSION_NO	200110251
 #endif
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index ca5345771ba..a504206ba49 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.218 2001/10/25 05:49:57 momjian Exp $
+ * $Id: pg_proc.h,v 1.219 2001/10/25 14:10:06 tgl Exp $
  *
  * NOTES
  *	  The script catalog/genbki.sh reads this file and generates .bki
@@ -885,6 +885,11 @@ DESCR("convert int8 to float8");
 DATA(insert OID = 483 (  int8			   PGUID 12 f t t t 1 f  20 "701" 100 0 0 100  dtoi8 - ));
 DESCR("convert float8 to int8");
 
+DATA(insert OID = 714 (  int2			   PGUID 12 f t t t 1 f  21 "20" 100 0 0 100  int82 - ));
+DESCR("convert int8 to int2");
+DATA(insert OID = 754 (  int8			   PGUID 12 f t t t 1 f  20 "21" 100 0 0 100  int28 - ));
+DESCR("convert int2 to int8");
+
 /* OIDS 500 - 599 */
 
 /* OIDS 600 - 699 */
diff --git a/src/include/utils/int8.h b/src/include/utils/int8.h
index 161bb838443..d083fb9f002 100644
--- a/src/include/utils/int8.h
+++ b/src/include/utils/int8.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: int8.h,v 1.28 2001/10/25 05:50:10 momjian Exp $
+ * $Id: int8.h,v 1.29 2001/10/25 14:10:07 tgl Exp $
  *
  * NOTES
  * These data types are supported on all 64-bit architectures, and may
@@ -98,6 +98,9 @@ extern Datum int48div(PG_FUNCTION_ARGS);
 extern Datum int48(PG_FUNCTION_ARGS);
 extern Datum int84(PG_FUNCTION_ARGS);
 
+extern Datum int28(PG_FUNCTION_ARGS);
+extern Datum int82(PG_FUNCTION_ARGS);
+
 extern Datum i8tod(PG_FUNCTION_ARGS);
 extern Datum dtoi8(PG_FUNCTION_ARGS);
 
-- 
GitLab