|
38 | 38 | data may linger in the database. We periodically sweep the |
39 | 39 | database, compacting it and performing regular cleanup so we can |
40 | 40 | maintain acceptable performance." |
41 | | - (:require [clojure.string :as str] |
| 41 | + (:require [clojure.java.jdbc :as sql] |
| 42 | + [clojure.string :as str] |
42 | 43 | [clojure.tools.logging :as log] |
43 | 44 | [metrics.counters :as counters :refer [counter inc!]] |
44 | 45 | [metrics.gauges :refer [gauge-fn]] |
45 | 46 | [metrics.timers :refer [time! timer]] |
46 | 47 | [metrics.reporters.jmx :as jmx-reporter] |
47 | 48 | [murphy :refer [try! with-final]] |
| 49 | + [next.jdbc :as nxt] |
48 | 50 | [puppetlabs.i18n.core :refer [trs tru]] |
49 | 51 | [puppetlabs.kitchensink.core :as kitchensink] |
50 | 52 | [puppetlabs.puppetdb.cli.tk-util :refer [run-tk-cli-cmd]] |
|
939 | 941 | (catch InterruptedException _ |
940 | 942 | (log/info (trs "Garbage collector interrupted"))))))) |
941 | 943 |
|
| 944 | +(defn analyze-partitioned-tables [db shutdown-for-ex] |
| 945 | + (with-nonfatal-exceptions-suppressed |
| 946 | + (with-monitored-execution shutdown-for-ex |
| 947 | + (try! |
| 948 | + (jdbc/with-db-connection db |
| 949 | + (jdbc/with-db-transaction [] |
| 950 | + (let [c (sql/db-connection jdbc/*db*)] |
| 951 | + (doseq [table ["reports" "resource_events"]] |
| 952 | + (try! |
| 953 | + (nxt/execute! c [(str "analyze " table)]) |
| 954 | + (catch InterruptedException _ |
| 955 | + (log/info (trs (str table " analysis interrupted"))))))))) |
| 956 | + (catch Exception ex |
| 957 | + (log/error ex)))))) |
| 958 | + |
942 | 959 | (defn start-garbage-collection |
943 | 960 | "Starts garbage collection of the databases represented in db-configs" |
944 | 961 | [{:keys [clean-lock] :as _context} |
|
952 | 969 | (schedule-with-fixed-delay sched #(invoke-periodic-gc db cfg request |
953 | 970 | shutdown-for-ex |
954 | 971 | clean-lock lock-status) |
955 | | - (to-millis interval) (to-millis interval))))))) |
| 972 | + (to-millis interval) (to-millis interval)))) |
| 973 | + ;; pg (up to at least 16) never analyzes partitioned table parents |
| 974 | + ;; Nearly fixed in 14, but removed before release: |
| 975 | + ;; https://www.postgresql.org/about/news/postgresql-14-beta-1-released-2213/ |
| 976 | + ;; https://www.postgresql.org/about/news/postgresql-14-rc-1-released-2309/ |
| 977 | + ;; |
| 978 | + ;; Assumes the analysis runs quickly enough to avoid needing any |
| 979 | + ;; enforced serialization, and to avoid (with the current single |
| 980 | + ;; threaded executor) ever delaying gc enough to matter. |
| 981 | + (let [hourly (* 60 60 1000) |
| 982 | + analyze #(analyze-partitioned-tables db shutdown-for-ex)] |
| 983 | + (schedule-with-fixed-delay sched analyze 0 hourly))))) |
956 | 984 |
|
957 | 985 |
|
958 | 986 | (defn database-lock-status [] |
|
0 commit comments