From 5c7f06a9cf72f80461302f741fddf5e018ac7575 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Fri, 2 Sep 2022 14:15:19 +0200
Subject: [PATCH v2 1/2] Check for interrupts in pg_trgm word similarity

Calculating similarity between large strings can be timesconsuming
and overrun configured statement timeouts. Check for interrupts in
the main loop to ensure query cancellation can be performed.

Author: Robins Tharakan <tharakan@gmail.com>
Discussion: https://postgr.es/m/CAEP4nAxvmfc_XWTz73bqXRhgjONi=1HaX4_NhsopA3L6UvnN1g@mail.gmail.com
---
 contrib/pg_trgm/trgm_op.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49b3609de9..eec06ef09c 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -7,6 +7,7 @@
 
 #include "catalog/pg_type.h"
 #include "lib/qunique.h"
+#include "miscadmin.h"
 #include "trgm.h"
 #include "tsearch/ts_locale.h"
 #include "utils/lsyscache.h"
@@ -495,6 +496,8 @@ iterate_word_similarity(int *trg2indexes,
 		/* Get index of next trigram */
 		int			trgindex = trg2indexes[i];
 
+		CHECK_FOR_INTERRUPTS();
+
 		/* Update last position of this trigram */
 		if (lower >= 0 || found[trgindex])
 		{
-- 
2.32.1 (Apple Git-133)

