From 15aa7a3ac17a13b35bcf335f6f8230ea12eda598 Mon Sep 17 00:00:00 2001
From: Mark Dilger <hornschnorter@gmail.com>
Date: Thu, 7 Nov 2019 14:38:06 -0800
Subject: [PATCH v1 5/5] Refactoring SPI related errors.

Deprecating SPI_ERROR_OPUNKNOWN error code in favor of throwing an
exception instead.  This code was only returned by SPI_execute and
SPI_execute_plan in the event that they failed to handle all relevant
cases in a C switch statement.
---
 doc/src/sgml/spi.sgml      | 9 ---------
 src/backend/executor/spi.c | 9 +++++----
 src/include/executor/spi.h | 2 +-
 3 files changed, 6 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/spi.sgml b/doc/src/sgml/spi.sgml
index cca91f6ef6..af822cfbe7 100644
--- a/doc/src/sgml/spi.sgml
+++ b/doc/src/sgml/spi.sgml
@@ -520,15 +520,6 @@ typedef struct SPITupleTable
      </listitem>
     </varlistentry>
 
-    <varlistentry>
-     <term><symbol>SPI_ERROR_OPUNKNOWN</symbol></term>
-     <listitem>
-      <para>
-       if the command type is unknown (shouldn't happen)
-      </para>
-     </listitem>
-    </varlistentry>
-
     <varlistentry>
      <term><symbol>SPI_ERROR_UNCONNECTED</symbol></term>
      <listitem>
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 579e80a6a7..f2a90f0d0d 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -1711,8 +1711,6 @@ SPI_result_code_string(int code)
 	{
 		case SPI_ERROR_COPY:
 			return "SPI_ERROR_COPY";
-		case SPI_ERROR_OPUNKNOWN:
-			return "SPI_ERROR_OPUNKNOWN";
 		case SPI_ERROR_UNCONNECTED:
 			return "SPI_ERROR_UNCONNECTED";
 		case SPI_ERROR_ARGUMENT:
@@ -2459,7 +2457,7 @@ _SPI_convert_params(int nargs, Oid *argtypes,
 static int
 _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
 {
-	int			operation = queryDesc->operation;
+	CmdType		operation = queryDesc->operation;
 	int			eflags;
 	int			res;
 
@@ -2492,8 +2490,11 @@ _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
 			else
 				res = SPI_OK_UPDATE;
 			break;
+		case CMD_UNKNOWN:
+		case CMD_UTILITY:
+		case CMD_NOTHING:
 		default:
-			return SPI_ERROR_OPUNKNOWN;
+			elog(ERROR, "Unsupported command type %d encountered while executing query", (int)operation);
 	}
 
 #ifdef SPI_EXECUTOR_STATS
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index 575978737f..ac34bb3b5d 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -38,7 +38,7 @@ typedef struct _SPI_plan *SPIPlanPtr;
 
 #define SPI_ERROR_CONNECT		(-1)	/* not used anymore */
 #define SPI_ERROR_COPY			(-2)
-#define SPI_ERROR_OPUNKNOWN		(-3)
+#define SPI_ERROR_OPUNKNOWN		(-3)	/* not used anymore */
 #define SPI_ERROR_UNCONNECTED	(-4)
 #define SPI_ERROR_CURSOR		(-5)	/* not used anymore */
 #define SPI_ERROR_ARGUMENT		(-6)
-- 
2.20.1

