diff --git a/src/pl/plpython/plpy_spi.c b/src/pl/plpython/plpy_spi.c
index ff87b27de0..64ca47f218 100644
--- a/src/pl/plpython/plpy_spi.c
+++ b/src/pl/plpython/plpy_spi.c
@@ -28,7 +28,7 @@
 static PyObject *PLy_spi_execute_query(char *query, long limit);
 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *tuptable,
 											  uint64 rows, int status);
-static void PLy_spi_exception_set(PyObject *excclass, ErrorData *edata);
+static void PLy_spi_exception_set(ErrorData *edata);
 
 
 /* prepare(query="select * from foo")
@@ -470,8 +470,6 @@ PLy_commit(PyObject *self, PyObject *args)
 	PG_CATCH();
 	{
 		ErrorData  *edata;
-		PLyExceptionEntry *entry;
-		PyObject   *exc;
 
 		/* Save error info */
 		MemoryContextSwitchTo(oldcontext);
@@ -481,17 +479,8 @@ PLy_commit(PyObject *self, PyObject *args)
 		/* was cleared at transaction end, reset pointer */
 		exec_ctx->scratch_ctx = NULL;
 
-		/* Look up the correct exception */
-		entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
-							HASH_FIND, NULL);
-
-		/*
-		 * This could be a custom error code, if that's the case fallback to
-		 * SPIError
-		 */
-		exc = entry ? entry->exc : PLy_exc_spi_error;
 		/* Make Python raise the exception */
-		PLy_spi_exception_set(exc, edata);
+		PLy_spi_exception_set(edata);
 		FreeErrorData(edata);
 
 		return NULL;
@@ -517,8 +506,6 @@ PLy_rollback(PyObject *self, PyObject *args)
 	PG_CATCH();
 	{
 		ErrorData  *edata;
-		PLyExceptionEntry *entry;
-		PyObject   *exc;
 
 		/* Save error info */
 		MemoryContextSwitchTo(oldcontext);
@@ -528,17 +515,8 @@ PLy_rollback(PyObject *self, PyObject *args)
 		/* was cleared at transaction end, reset pointer */
 		exec_ctx->scratch_ctx = NULL;
 
-		/* Look up the correct exception */
-		entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
-							HASH_FIND, NULL);
-
-		/*
-		 * This could be a custom error code, if that's the case fallback to
-		 * SPIError
-		 */
-		exc = entry ? entry->exc : PLy_exc_spi_error;
 		/* Make Python raise the exception */
-		PLy_spi_exception_set(exc, edata);
+		PLy_spi_exception_set(edata);
 		FreeErrorData(edata);
 
 		return NULL;
@@ -594,8 +572,6 @@ void
 PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
 {
 	ErrorData  *edata;
-	PLyExceptionEntry *entry;
-	PyObject   *exc;
 
 	/* Save error info */
 	MemoryContextSwitchTo(oldcontext);
@@ -607,17 +583,8 @@ PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
 	MemoryContextSwitchTo(oldcontext);
 	CurrentResourceOwner = oldowner;
 
-	/* Look up the correct exception */
-	entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
-						HASH_FIND, NULL);
-
-	/*
-	 * This could be a custom error code, if that's the case fallback to
-	 * SPIError
-	 */
-	exc = entry ? entry->exc : PLy_exc_spi_error;
 	/* Make Python raise the exception */
-	PLy_spi_exception_set(exc, edata);
+	PLy_spi_exception_set(edata);
 	FreeErrorData(edata);
 }
 
@@ -626,12 +593,21 @@ PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
  * internal query and error position.
  */
 static void
-PLy_spi_exception_set(PyObject *excclass, ErrorData *edata)
+PLy_spi_exception_set(ErrorData *edata)
 {
+	PLyExceptionEntry *entry;
+	PyObject   *excclass;
 	PyObject   *args = NULL;
 	PyObject   *spierror = NULL;
 	PyObject   *spidata = NULL;
 
+	/* Look up the correct exception */
+	entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
+						HASH_FIND, NULL);
+
+	/* Fall back to SPIError if no entry */
+	excclass = entry ? entry->exc : PLy_exc_spi_error;
+
 	args = Py_BuildValue("(s)", edata->message);
 	if (!args)
 		goto failure;
