diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 4506518768d85bcde2698439d55c34d3a07dbb3b..735322ee3f5842cf27dc14f4e46be04ac3743e11 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -22,6 +22,7 @@
 
 #include "postgres.h"
 
+#include "miscadmin.h"
 #include "nodes/plannodes.h"
 #include "nodes/relation.h"
 #include "utils/datum.h"
@@ -3667,6 +3668,9 @@ copyObject(void *from)
 	if (from == NULL)
 		return NULL;
 
+	/* Guard against stack overflow due to overly complex expressions */
+	check_stack_depth();
+
 	switch (nodeTag(from))
 	{
 			/*