diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index f4133953be..9a8255ab1a 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -100,6 +100,22 @@ int			max_stack_depth = 100;
 /* wait N seconds to allow attach from a debugger */
 int			PostAuthDelay = 0;
 
+/*
+ * Hook for extensions, to get notified when query cancel or DIE signal is
+ * received. This allows the extension to stop whatever it's doing as
+ * quickly as possible. Normally, you would sprinkle your code with
+ * CHECK_FOR_INTERRUPTS() in suitable places, but sometimes that's not
+ * possible, for example because you call a slow function in a 3rd party
+ * library that you have no control over. In the hook function, you might
+ * be able to abort such a slow operation somehow.
+ *
+ * This gets called after setting ProcDiePending, QueryCancelPending and/or
+ * RecoveryConfictPending, so the hook function can check those to
+ * determine what event happened.
+ *
+ * NB: This is called from a signal handler!
+ */
+cancel_pending_hook_type cancel_pending_hook = NULL;
 
 
 /* ----------------
@@ -2664,6 +2680,9 @@ die(SIGNAL_ARGS)
 	{
 		InterruptPending = true;
 		ProcDiePending = true;
+
+		if (cancel_pending_hook)
+			(*cancel_pending_hook)();
 	}
 
 	/* If we're still here, waken anything waiting on the process latch */
@@ -2697,6 +2716,9 @@ StatementCancelHandler(SIGNAL_ARGS)
 	{
 		InterruptPending = true;
 		QueryCancelPending = true;
+
+		if (cancel_pending_hook)
+			(*cancel_pending_hook)();
 	}
 
 	/* If we're still here, waken anything waiting on the process latch */
@@ -2819,6 +2841,8 @@ RecoveryConflictInterrupt(ProcSignalReason reason)
 					RecoveryConflictPending = true;
 					QueryCancelPending = true;
 					InterruptPending = true;
+					if (cancel_pending_hook)
+						(*cancel_pending_hook)();
 					break;
 				}
 
@@ -2829,6 +2853,8 @@ RecoveryConflictInterrupt(ProcSignalReason reason)
 				RecoveryConflictPending = true;
 				ProcDiePending = true;
 				InterruptPending = true;
+				if (cancel_pending_hook)
+					(*cancel_pending_hook)();
 				break;
 
 			default:
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index e167ee8fcb..f6648221ca 100644
--- a/src/include/miscadmin.h
+++ b/src/include/miscadmin.h
@@ -93,6 +93,10 @@ extern PGDLLIMPORT volatile uint32 CritSectionCount;
 /* in tcop/postgres.c */
 extern void ProcessInterrupts(void);
 
+/* Hook get notified when QueryCancelPending or ProcDiePending is raised */
+typedef void (*cancel_pending_hook_type) (void);
+extern PGDLLIMPORT cancel_pending_hook_type cancel_pending_hook;
+
 #ifndef WIN32
 
 #define CHECK_FOR_INTERRUPTS() \
diff --git a/src/pl/plpython/plpy_elog.c b/src/pl/plpython/plpy_elog.c
index e244104fed..07eac036ad 100644
--- a/src/pl/plpython/plpy_elog.c
+++ b/src/pl/plpython/plpy_elog.c
@@ -7,6 +7,7 @@
 #include "postgres.h"
 
 #include "lib/stringinfo.h"
+#include "miscadmin.h"
 
 #include "plpython.h"
 
@@ -120,6 +121,15 @@ PLy_elog_impl(int elevel, const char *fmt,...)
 
 	PG_TRY();
 	{
+		/*
+		 * If the error was a KeyboardException that we raised because
+		 * of query cancellation, then CHECK_FOR_INTERRUPTS() will throw
+		 * a better error message than we do here, with
+		 * "canceling statement due to user request" or similar message.
+		 * Give it a chance.
+		 */
+		CHECK_FOR_INTERRUPTS();
+
 		ereport(elevel,
 				(errcode(sqlerrcode ? sqlerrcode : ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
 				 errmsg_internal("%s", primary ? primary : "no exception data"),
diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c
index 6a66eba176..f11b0d2f6e 100644
--- a/src/pl/plpython/plpy_main.c
+++ b/src/pl/plpython/plpy_main.c
@@ -73,6 +73,9 @@ PyObject   *PLy_interp_globals = NULL;
 /* this doesn't need to be global; use PLy_current_execution_context() */
 static PLyExecutionContext *PLy_execution_contexts = NULL;
 
+static cancel_pending_hook_type prev_cancel_pending_hook;
+
+static void PLy_handle_cancel_interrupt(void);
 
 void
 _PG_init(void)
@@ -141,6 +144,10 @@ PLy_initialize(void)
 	if (PyErr_Occurred())
 		PLy_elog(FATAL, "untrapped error in initialization");
 
+	/* Get notified on SIGTERM or query cancellations */
+	prev_cancel_pending_hook = cancel_pending_hook;
+	cancel_pending_hook = PLy_handle_cancel_interrupt;
+
 	init_procedure_caches();
 
 	explicit_subtransactions = NIL;
@@ -464,3 +471,37 @@ PLy_pop_execution_context(void)
 		MemoryContextDelete(context->scratch_ctx);
 	pfree(context);
 }
+
+/*
+ * Raise a KeyboardInterrupt exception, to simulate a SIGINT.
+ */
+static int
+PLy_python_cancel_handler(void *arg)
+{
+	PyErr_SetNone(PyExc_KeyboardInterrupt);
+
+	/* return -1 to indicate that we set an exception. */
+	return -1;
+}
+
+/*
+ * Hook function, called when current query is being cancelled
+ * (on e.g. SIGINT or SIGTERM)
+ *
+ * NB: This is called from a signal handler!
+ */
+static void
+PLy_handle_cancel_interrupt(void)
+{
+	/*
+	 * We can't do much in a signal handler, so just tell the Python
+	 * interpreter to call us back when possible.
+	 *
+	 * We don't bother to check the return value, as there's nothing we could
+	 * do if it fails for some reason.
+	 */
+	(void) Py_AddPendingCall(PLy_python_cancel_handler, NULL);
+
+	if (prev_cancel_pending_hook)
+		prev_cancel_pending_hook();
+}
