From 58967f57238cdde84a2e9ca63413b6c772534962 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Sat, 24 Jan 2026 23:43:06 -0500
Subject: [PATCH v31 3/9] Add dependencies support to
 pg_restore_extended_stats()

This adds the ability to set dependencies values for an extended
statistics object.
---
 src/backend/statistics/extended_stats_funcs.c | 32 ++++++++++
 src/test/regress/expected/stats_import.out    | 63 +++++++++++++++++++
 src/test/regress/sql/stats_import.sql         | 44 +++++++++++++
 3 files changed, 139 insertions(+)

diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index ea45319fcee..68267b9462b 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -286,6 +286,7 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 	 * were provided to the function.
 	 */
 	has.ndistinct = !PG_ARGISNULL(NDISTINCT_ARG);
+	has.dependencies = !PG_ARGISNULL(DEPENDENCIES_ARG);
 
 	if (RecoveryInProgress())
 	{
@@ -382,6 +383,19 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 		success = false;
 	}
 
+	/* If the object can't support dependencies, we should not have it. */
+	if (has.dependencies && !enabled.dependencies)
+	{
+		ereport(WARNING,
+				errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				errmsg("cannot specify parameter \"%s\".",
+					   extarginfo[DEPENDENCIES_ARG].argname),
+				errhint("Extended statistics object "
+						"does not support statistics of this type."));
+		has.dependencies = false;
+		success = false;
+	}
+
 	/*
 	 * Populate the pg_statistic_ext_data result tuple.
 	 */
@@ -420,6 +434,24 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 		statext_ndistinct_free(ndistinct);
 	}
 
+	if (has.dependencies)
+	{
+		Datum		dependencies_datum = PG_GETARG_DATUM(DEPENDENCIES_ARG);
+		bytea	   *data = DatumGetByteaPP(dependencies_datum);
+		MVDependencies *dependencies = statext_dependencies_deserialize(data);
+
+		if (statext_dependencies_validate(dependencies, &stxform->stxkeys, numexprs, WARNING))
+		{
+			values[Anum_pg_statistic_ext_data_stxddependencies - 1] = dependencies_datum;
+			nulls[Anum_pg_statistic_ext_data_stxddependencies - 1] = false;
+			replaces[Anum_pg_statistic_ext_data_stxddependencies - 1] = true;
+		}
+		else
+			success = false;
+
+		statext_dependencies_free(dependencies);
+	}
+
 	upsert_pg_statistic_ext_data(values, nulls, replaces);
 
 cleanup:
diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index df850048aed..ed8521c5ac9 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -1144,6 +1144,9 @@ CREATE STATISTICS stats_import.test_stat
 CREATE STATISTICS stats_import.test_stat_ndistinct(ndistinct)
   ON name, comp
   FROM stats_import.test;
+CREATE STATISTICS stats_import.test_stat_dependencies(dependencies)
+  ON name, comp
+  FROM stats_import.test;
 -- Generate statistics on table with data
 ANALYZE stats_import.test;
 CREATE TABLE stats_import.test_clone ( LIKE stats_import.test )
@@ -1696,6 +1699,21 @@ WARNING:  could not validate "pg_ndistinct" object: invalid attribute number 1 f
  f
 (1 row)
 
+-- set ndistinct on a dependencies-only stat
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'n_distinct', '[{"attributes" : [1,3], "ndistinct" : 4}]'::pg_ndistinct);
+WARNING:  cannot specify parameter "n_distinct".
+HINT:  Extended statistics object does not support statistics of this type.
+ pg_restore_extended_stats 
+---------------------------
+ f
+(1 row)
+
 -- ok: ndistinct
 SELECT pg_catalog.pg_restore_extended_stats(
   'schemaname', 'stats_import',
@@ -1709,6 +1727,51 @@ SELECT pg_catalog.pg_restore_extended_stats(
  t
 (1 row)
 
+-- set dependencies on an ndistinct-only stat
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_ndistinct',
+  'inherited', false,
+  'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
+WARNING:  cannot specify parameter "dependencies".
+HINT:  Extended statistics object does not support statistics of this type.
+ pg_restore_extended_stats 
+---------------------------
+ f
+(1 row)
+
+-- dependencies value doesn't match definition
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'dependencies', '[{"attributes": [1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 1, "degree": 1.000000}]'::pg_dependencies);
+WARNING:  could not validate "pg_dependencies" object: invalid attribute number 1 found
+ pg_restore_extended_stats 
+---------------------------
+ f
+(1 row)
+
+-- ok: dependencies
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
+ pg_restore_extended_stats 
+---------------------------
+ t
+(1 row)
+
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import' AND
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index cbbfc41a376..52c0cedb2f5 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -815,6 +815,10 @@ CREATE STATISTICS stats_import.test_stat_ndistinct(ndistinct)
   ON name, comp
   FROM stats_import.test;
 
+CREATE STATISTICS stats_import.test_stat_dependencies(dependencies)
+  ON name, comp
+  FROM stats_import.test;
+
 -- Generate statistics on table with data
 ANALYZE stats_import.test;
 
@@ -1215,6 +1219,15 @@ SELECT pg_catalog.pg_restore_extended_stats(
   'inherited', false,
   'n_distinct', '[{"attributes" : [1,3], "ndistinct" : 4}]'::pg_ndistinct);
 
+-- set ndistinct on a dependencies-only stat
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'n_distinct', '[{"attributes" : [1,3], "ndistinct" : 4}]'::pg_ndistinct);
+
 -- ok: ndistinct
 SELECT pg_catalog.pg_restore_extended_stats(
   'schemaname', 'stats_import',
@@ -1224,6 +1237,37 @@ SELECT pg_catalog.pg_restore_extended_stats(
   'inherited', false,
   'n_distinct', '[{"attributes" : [2,3], "ndistinct" : 4}]'::pg_ndistinct);
 
+-- set dependencies on an ndistinct-only stat
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_ndistinct',
+  'inherited', false,
+  'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
+
+
+-- dependencies value doesn't match definition
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'dependencies', '[{"attributes": [1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 1, "degree": 1.000000}]'::pg_dependencies);
+
+-- ok: dependencies
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_dependencies',
+  'inherited', false,
+  'dependencies', '[{"attributes": [2], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3], "dependency": 2, "degree": 1.000000}]'::pg_dependencies);
+
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import' AND
-- 
2.52.0

