Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Force an analyze on pending index table (#75816)

Nathan Voxland7bd450f1dec1
diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.clj
index 128bd4fed5e..c55118c4c89 100644
--- a/src/metabase/search/appdb/index.clj
+++ b/src/metabase/search/appdb/index.clj
@@ -230,6 +230,15 @@
(log/infof "New pending index %s" pending)
pending)))))))
+(defn- analyze-table!
+ "Refresh the table's statistics so size estimates are accurate as soon as it becomes active.
+ Best-effort: a failure here must never block activation."
+ [table-name]
+ (try
+ (specialization/analyze-table! table-name)
+ (catch Exception e
+ (log/warnf e "Failed to analyze index table %s" table-name))))
+
(defn activate-table!
"Make the pending index active if it exists. Returns true if it did so."
[]
@@ -238,11 +247,13 @@
;; The atoms are the only source of truth, we must not update the metadata.
(boolean
(when-let [pending (:pending @*indexes*)]
+ (analyze-table! pending)
(reset! *indexes* {:pending nil, :active pending}) true))
;; Ensure the metadata is updated and pruned.
(let [{:keys [pending]} (sync-tracking-atoms!)]
(log/infof "Activating pending index %s" pending)
(when pending
+ (analyze-table! pending)
(let [active (keyword (search-index-metadata/active-pending! :appdb (search.spec/index-version-hash)))]
(reset! *indexes* {:pending nil :active active})
(log/infof "Activated pending index %s" active)))
diff --git a/src/metabase/search/appdb/specialization/api.clj b/src/metabase/search/appdb/specialization/api.clj
index 9369804b745..75881bd11c7 100644
--- a/src/metabase/search/appdb/specialization/api.clj
+++ b/src/metabase/search/appdb/specialization/api.clj
@@ -39,6 +39,14 @@
{:arglists '([index-table p-value])}
db-type)
+(defmulti analyze-table!
+ "Refresh the database's statistics for the given table, so that [[index-size-estimate]] is accurate immediately,
+ rather than waiting for the database to re-sample it in the background. No-op for databases whose estimate is exact."
+ {:arglists '([table-name])}
+ db-type)
+
+(defmethod analyze-table! :default [_table-name] nil)
+
(defmulti index-size-estimate
"An estimated row count for the index `table-name`, ideally without scanning the whole table (used for the
`metabase_search_appdb_index_size` metric, which is scraped frequently)."
diff --git a/src/metabase/search/appdb/specialization/postgres.clj b/src/metabase/search/appdb/specialization/postgres.clj
index 116579cf56b..2aa6eb2e353 100644
--- a/src/metabase/search/appdb/specialization/postgres.clj
+++ b/src/metabase/search/appdb/specialization/postgres.clj
@@ -99,6 +99,10 @@
:group-by [:search_index.model]
:having [:is-not expr nil]}))
+(defmethod specialization/analyze-table! :postgres
+ [table-name]
+ (t2/query (str "ANALYZE " (name table-name))))
+
(defmethod specialization/index-size-estimate :postgres
[table-name]
;; Use the planner's row estimate (pg_class.reltuples) instead of a full count(*). reltuples/relpages are
diff --git a/test/metabase/search/appdb/metrics_test.clj b/test/metabase/search/appdb/metrics_test.clj
index d5f503c9c37..6dab89d588a 100644
--- a/test/metabase/search/appdb/metrics_test.clj
+++ b/test/metabase/search/appdb/metrics_test.clj
@@ -33,6 +33,18 @@
(is (= exact (#'metrics/active-index-size))))
(testing "returns an exact count on H2"
(is (= exact (#'metrics/active-index-size)))))))))))
+ (testing "is accurate immediately after a fresh index table is rotated in (no waiting for autoanalyze) -- see #75064"
+ (search.tu/with-temp-index-table
+ (binding [search.ingestion/*force-sync* true]
+ (mt/dataset test-data
+ (mt/with-temp [:model/User {} (when-not (t2/exists? :model/User 1) {:id 1})
+ :model/Collection {col-id :id} {:name "Collection"}
+ :model/Card {} {:name "Quarterly Forecast" :collection_id col-id}]
+ ;; a full (not in-place) reindex populates a pending table, then activates it
+ (search.engine/reindex! :search.engine/appdb {})
+ (let [exact (t2/count (search.index/active-table))]
+ (is (pos? exact))
+ (is (= exact (#'metrics/active-index-size)))))))))
(testing "returns nil when this instance has no active index to serve from"
(search.tu/with-temp-index-table
(reset! @#'search.index/*indexes* {:active nil :pending nil})