Skip to content

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

Look for query execution partitions only in the current schema. (#85096)

technomancy66a7c922c79b
diff --git a/src/metabase/audit_app/task/partitions.clj b/src/metabase/audit_app/task/partitions.clj
index 54afabd45da..b453dc2573d 100644
--- a/src/metabase/audit_app/task/partitions.clj
+++ b/src/metabase/audit_app/task/partitions.clj
@@ -57,10 +57,13 @@
"List the names of the currently-attached query_execution partition tables."
[conn]
(map :pg_class/relname
+ ;; the regclass::oid cast resolves to only the table in the current schema.
+ ;; appdb with a non-default schema is not supported for new installs, but
+ ;; there are users who have upgraded from ancient installs who have it.
(next.jdbc/execute! conn ["SELECT c.relname FROM pg_inherits i
JOIN pg_class p ON i.inhparent = p.oid
JOIN pg_class c ON i.inhrelid = c.oid
- WHERE p.relname='query_execution'"])))
+ WHERE p.oid = 'query_execution'::regclass::oid"])))
(defn fully-on-partitions?
"Has the database fully switched to managing its truncation with partitions?
@@ -71,13 +74,13 @@
[]
(and (= :postgres (mdb/db-type))
(with-open [conn (.getConnection (mdb/data-source))]
- (let [[oldest-partition] (sort (current-partitions conn))
- [oldest] (next.jdbc/execute! conn [(str "SELECT min(started_at) AS min FROM "
- oldest-partition)])
- oldest-date-time (some-> (:min oldest) t/instant
- (t/local-date-time "UTC"))]
- (or (nil? oldest-date-time)
- (= oldest-partition (partition-for oldest-date-time)))))))
+ (when-let [oldest-partition (first (sort (current-partitions conn)))]
+ (let [[oldest] (next.jdbc/execute! conn [(str "SELECT min(started_at) AS min FROM "
+ oldest-partition)])
+ oldest-date-time (some-> (:min oldest) t/instant
+ (t/local-date-time "UTC"))]
+ (or (nil? oldest-date-time)
+ (= oldest-partition (partition-for oldest-date-time))))))))
;;; and now for the side-effects...
diff --git a/test/metabase/audit_app/task/partitions_test.clj b/test/metabase/audit_app/task/partitions_test.clj
index 9f313f61188..df2ec03ac8e 100644
--- a/test/metabase/audit_app/task/partitions_test.clj
+++ b/test/metabase/audit_app/task/partitions_test.clj
@@ -54,9 +54,12 @@
(defn drop-all-tables [conn]
;; can't use current-partitions because we want to get rid of them regardless
;; of whether they're attached currently or not
- (doseq [t (next.jdbc/execute! conn ["SELECT table_name FROM information_schema.tables
- WHERE table_name LIKE 'query_execution_%'"])]
- (next.jdbc/execute! conn [(format "DROP TABLE %s" (:tables/table_name t))])))
+ (doseq [t (next.jdbc/execute! conn ["SELECT table_name, table_schema
+ FROM information_schema.tables
+ WHERE table_name LIKE 'query_execution_%'"])]
+ (next.jdbc/execute! conn [(format "DROP TABLE %s.%s"
+ (:tables/table_schema t)
+ (:tables/table_name t))])))
(deftest manage-partitions
(when (= :postgres (mdb/db-type))
@@ -114,8 +117,10 @@
(when (= :postgres (mdb/db-type))
(with-open [conn (.getConnection (mdb/data-source))]
(try
- (testing "initial partition"
+ (testing "no partitions"
(drop-all-tables conn)
+ (is (not (partitions/fully-on-partitions?))))
+ (testing "initial partition"
;; initial partition, upper bound is exclusive
(partitions/create-partition conn {:name "query_execution_2026_09"
:from "1970-01-01"
@@ -149,3 +154,27 @@
(partitions/create-partition conn {:name "query_execution_2026_08"
:from "1970-01-01"
:to "2099-01-01"}))))))
+
+(deftest current-partitions-current-schema-only
+ (when (= :postgres (mdb/db-type))
+ (with-open [conn (.getConnection (mdb/data-source))]
+ (try
+ (drop-all-tables conn)
+ (partitions/manage-partitions conn #t "2026-11-01" 25)
+ (next.jdbc/execute! conn ["CREATE SCHEMA IF NOT EXISTS metabase2"])
+ (next.jdbc/execute! conn ["CREATE TABLE IF NOT EXISTS metabase2.query_execution
+ (started_at TIMESTAMP) PARTITION BY RANGE (started_at)"])
+ (next.jdbc/execute! conn ["CREATE TABLE metabase2.query_execution_2026_01
+ PARTITION OF metabase2.query_execution
+ FOR VALUES FROM ('2026-01-01') TO ('2026-02-01')"])
+ (next.jdbc/execute! conn ["CREATE TABLE metabase2.query_execution_2026_11
+ PARTITION OF metabase2.query_execution
+ FOR VALUES FROM ('2026-11-01') TO ('2026-12-01')"])
+ (is (= ["query_execution_2026_11" "query_execution_2026_12"]
+ (sort (partitions/current-partitions conn))))
+ (finally
+ (drop-all-tables conn)
+ ;; make sure we end up in a good state for other tests
+ (partitions/create-partition conn {:name "query_execution_2026_08"
+ :from "1970-01-01"
+ :to "2099-01-01"}))))))