From e89cb24c2f98cfd706fc053ac60703d374a3e83e Mon Sep 17 00:00:00 2001
From: Mark Dilger <hornschnorter@gmail.com>
Date: Thu, 7 Nov 2019 10:10:17 -0800
Subject: [PATCH v1 3/5] Refactoring SPI trigger related errors.

Deprecating SPI_ERROR_REL_DUPLICATE and SPI_ERROR_REL_NOT_FOUND error
codes in favor of throwing an error.  These errors should not be
reachable from within the core code.

Specifically, only the function SPI_register_trigger_data is calling
SPI_register_relation, which was the only function returning
SPI_ERROR_REL_DUPLICATE.  The call to SPI_register_relation is for the
purpose of registering the NEW and OLD trigger relations, and given how
the code is written, it cannot make the mistake of registering them more
than once.  Hence, SPI_ERROR_REL_DUPLICATE was unreachable by a user,
except through poorly written third-party C code that had some other
usage pattern.

SPI_unregister_relation was the only function returning
SPI_ERROR_REL_NOT_FOUND.  No calls are made to SPI_unregister_relation
from core, so once again, it should be unreachable by a user except
through poorly written third-party C code.
---
 doc/src/sgml/spi.sgml      | 27 ---------------------------
 src/backend/executor/spi.c |  8 ++------
 src/include/executor/spi.h |  4 ++--
 3 files changed, 4 insertions(+), 35 deletions(-)

diff --git a/doc/src/sgml/spi.sgml b/doc/src/sgml/spi.sgml
index d1e6582d96..abe1475256 100644
--- a/doc/src/sgml/spi.sgml
+++ b/doc/src/sgml/spi.sgml
@@ -2756,15 +2756,6 @@ int SPI_register_relation(EphemeralNamedRelation <parameter>enr</parameter>)
      </listitem>
     </varlistentry>
 
-    <varlistentry>
-     <term><symbol>SPI_ERROR_REL_DUPLICATE</symbol></term>
-     <listitem>
-      <para>
-       if the name specified in the <varname>name</varname> field of
-       <parameter>enr</parameter> is already registered for this connection
-      </para>
-     </listitem>
-    </varlistentry>
    </variablelist>
   </para>
  </refsect1>
@@ -2861,15 +2852,6 @@ int SPI_unregister_relation(const char * <parameter>name</parameter>)
      </listitem>
     </varlistentry>
 
-    <varlistentry>
-     <term><symbol>SPI_ERROR_REL_NOT_FOUND</symbol></term>
-     <listitem>
-      <para>
-       if <parameter>name</parameter> is not found in the registry for the
-       current connection
-      </para>
-     </listitem>
-    </varlistentry>
    </variablelist>
   </para>
  </refsect1>
@@ -2976,15 +2958,6 @@ int SPI_register_trigger_data(TriggerData *<parameter>tdata</parameter>)
      </listitem>
     </varlistentry>
 
-    <varlistentry>
-     <term><symbol>SPI_ERROR_REL_DUPLICATE</symbol></term>
-     <listitem>
-      <para>
-       if the name of any trigger data transient relation is already
-       registered for this connection
-      </para>
-     </listitem>
-    </varlistentry>
    </variablelist>
   </para>
  </refsect1>
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index c3943ebbb9..7d227a11d2 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -1725,10 +1725,6 @@ SPI_result_code_string(int code)
 			return "SPI_ERROR_NOATTRIBUTE";
 		case SPI_ERROR_TYPUNKNOWN:
 			return "SPI_ERROR_TYPUNKNOWN";
-		case SPI_ERROR_REL_DUPLICATE:
-			return "SPI_ERROR_REL_DUPLICATE";
-		case SPI_ERROR_REL_NOT_FOUND:
-			return "SPI_ERROR_REL_NOT_FOUND";
 		case SPI_OK_CONNECT:
 			return "SPI_OK_CONNECT";
 		case SPI_OK_FINISH:
@@ -2875,7 +2871,7 @@ SPI_register_relation(EphemeralNamedRelation enr)
 
 	match = _SPI_find_ENR_by_name(enr->md.name);
 	if (match)
-		res = SPI_ERROR_REL_DUPLICATE;
+		elog(ERROR, "duplicate relation registration");
 	else
 	{
 		if (_SPI_current->queryEnv == NULL)
@@ -2914,7 +2910,7 @@ SPI_unregister_relation(const char *name)
 		res = SPI_OK_REL_UNREGISTER;
 	}
 	else
-		res = SPI_ERROR_REL_NOT_FOUND;
+		elog(ERROR, "unknown relation unregistration");
 
 	_SPI_end_call(false);
 
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index 2d505610ff..7a43b9d21e 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -47,8 +47,8 @@ typedef struct _SPI_plan *SPIPlanPtr;
 #define SPI_ERROR_NOATTRIBUTE	(-9)
 #define SPI_ERROR_NOOUTFUNC		(-10)	/* not used anymore */
 #define SPI_ERROR_TYPUNKNOWN	(-11)
-#define SPI_ERROR_REL_DUPLICATE (-12)
-#define SPI_ERROR_REL_NOT_FOUND (-13)
+#define SPI_ERROR_REL_DUPLICATE (-12)	/* not used anymore */
+#define SPI_ERROR_REL_NOT_FOUND (-13)	/* not used anymore */
 
 #define SPI_OK_CONNECT			1
 #define SPI_OK_FINISH			2
-- 
2.20.1

