diff --git a/src/backend/utils/adt/like.c b/src/backend/utils/adt/like.c
index 284c5aeb886a853df452d526348f3d5ce720d730..b6db4f3b018a4bab795a0f3f8c9a56d8a73192c0 100644
--- a/src/backend/utils/adt/like.c
+++ b/src/backend/utils/adt/like.c
@@ -21,6 +21,7 @@
 
 #include "catalog/pg_collation.h"
 #include "mb/pg_wchar.h"
+#include "miscadmin.h"
 #include "utils/builtins.h"
 #include "utils/pg_locale.h"
 
diff --git a/src/backend/utils/adt/like_match.c b/src/backend/utils/adt/like_match.c
index 3ec24872d0075723f6a45af53bc0dd96bb5aab71..83ece28722cb25e0f50c329e2a2fbe9a925d0788 100644
--- a/src/backend/utils/adt/like_match.c
+++ b/src/backend/utils/adt/like_match.c
@@ -83,6 +83,9 @@ MatchText(char *t, int tlen, char *p, int plen,
 	if (plen == 1 && *p == '%')
 		return LIKE_TRUE;
 
+	/* Since this function recurses, it could be driven to stack overflow */
+	check_stack_depth();
+
 	/*
 	 * In this loop, we advance by char when matching wildcards (and thus on
 	 * recursive entry to this function we are properly char-synced). On other