diff --git a/src/test/modules/test_custom_types/test_custom_types--1.0.sql b/src/test/modules/test_custom_types/test_custom_types--1.0.sql
index ce0e905d636..88f34cb4efc 100644
--- a/src/test/modules/test_custom_types/test_custom_types--1.0.sql
+++ b/src/test/modules/test_custom_types/test_custom_types--1.0.sql
@@ -32,6 +32,14 @@ RETURNS boolean
 AS 'MODULE_PATHNAME'
 LANGUAGE C STRICT;
 
+--
+-- Typanalyze function whose result depends on attstattarget
+--
+CREATE FUNCTION int_custom_typanalyze_stattarget_sensitive(internal)
+RETURNS boolean
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT;
+
 --
 -- The int_custom type definition
 --
diff --git a/src/test/modules/test_custom_types/test_custom_types.c b/src/test/modules/test_custom_types/test_custom_types.c
index 41262044b47..8d91318d880 100644
--- a/src/test/modules/test_custom_types/test_custom_types.c
+++ b/src/test/modules/test_custom_types/test_custom_types.c
@@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(int_custom_in);
 PG_FUNCTION_INFO_V1(int_custom_out);
 PG_FUNCTION_INFO_V1(int_custom_typanalyze_false);
 PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid);
+PG_FUNCTION_INFO_V1(int_custom_typanalyze_stattarget_sensitive);
 PG_FUNCTION_INFO_V1(int_custom_eq);
 PG_FUNCTION_INFO_V1(int_custom_ne);
 PG_FUNCTION_INFO_V1(int_custom_lt);
@@ -110,6 +111,26 @@ int_custom_typanalyze_invalid(PG_FUNCTION_ARGS)
 	PG_RETURN_BOOL(true);
 }
 
+/*
+ * int_custom_typanalyze_stattarget_sensitive
+ *
+ * Accepts the column only when attstattarget is negative (i.e. "use the
+ * default"), and rejects it otherwise.  For expressions in extended
+ * statistics, examine_attribute() passes -1 while examine_expression()
+ * passes the computed statistics target, so this behaves differently in
+ * the two code paths.
+ */
+Datum
+int_custom_typanalyze_stattarget_sensitive(PG_FUNCTION_ARGS)
+{
+	VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0);
+
+	if (stats->attstattarget >= 0)
+		PG_RETURN_BOOL(false);
+
+	PG_RETURN_BOOL(std_typanalyze(stats));
+}
+
 /*
  * Comparison functions for int_custom type
  */
