v1-0001-Optimize-functional-dependency-calculation-for-un.patch
text/x-patch
Filename: v1-0001-Optimize-functional-dependency-calculation-for-un.patch
Type: text/x-patch
Part: 0
From 5d8b7293c3c72bf33bd533d1e3ff03c65a0b2fdc Mon Sep 17 00:00:00 2001
From: Ilia Evdokimov <ilya.evdokimov@tantorlabs.ru>
Date: Wed, 11 Jun 2025 19:31:05 +0300
Subject: [PATCH v1] Optimize functional dependency calculation for unique and
same values
When building extended statistics with dependencies, the calculation
always sorts the sample data to compute the degree of functional
dependency between columns. However, if the last column in the
dependency list contains either all-equal or all-unique values,
the dependency is trivially 1.0.
This patch adds a fast path to skip the expensive sorting step
in such cases, significantly reducing ANALYZE time for large tables
with high statistics targets.
---
src/backend/statistics/dependencies.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c
index eb2fc4366b4..3f896d7bc99 100644
--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -225,6 +225,7 @@ dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency)
MultiSortSupport mss;
SortItem *items;
AttrNumber *attnums_dep;
+ VacAttrStats *last_col_stats;
/* counters valid within a group */
int group_size = 0;
@@ -236,6 +237,17 @@ dependency_degree(StatsBuildData *data, int k, AttrNumber *dependency)
/* Make sure we have at least two input attributes. */
Assert(k >= 2);
+ /*
+ * If the last column has only one distinct value,
+ * or if it has as many distinct values as rows (implying a unique key),
+ * the dependency degree is 1.0.
+ */
+ last_col_stats = data->stats[dependency[k - 1]];
+ if (last_col_stats->stadistinct == 1.0 || last_col_stats->stadistinct == -1.0)
+ {
+ return 1.0;
+ }
+
/* sort info for all attributes columns */
mss = multi_sort_init(k);
--
2.34.1