diff --git a/src/puppetlabs/puppetdb/scf/storage.clj b/src/puppetlabs/puppetdb/scf/storage.clj index dfa3ad962..d759daf06 100644 --- a/src/puppetlabs/puppetdb/scf/storage.clj +++ b/src/puppetlabs/puppetdb/scf/storage.clj @@ -537,17 +537,16 @@ (some-> params sutils/munge-jsonb-for-storage)])} *note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection)))) - (doseq [batch (partition-all 1000 new-params)] - ;; The batch might only have one item with params {}. - (when-let [rows (seq (for [[resource-hash params] batch - [k v] params] - [(sutils/munge-hash-for-storage resource-hash) - (name k) - (sutils/db-serialize v)]))] - (->> {:insert-into :resource_params - :columns [:resource :name :value] - :values rows} - *note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection))))))) + ;; Batch by row (3 bind params each) to stay under the 65,535 limit. + (doseq [rows (partition-all 10000 (for [[resource-hash params] new-params + [k v] params] + [(sutils/munge-hash-for-storage resource-hash) + (name k) + (sutils/db-serialize v)]))] + (->> {:insert-into :resource_params + :columns [:resource :name :value] + :values rows} + *note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection)))))) (def resource-ref? "Returns true of the map is a resource reference" diff --git a/test/puppetlabs/puppetdb/scf/storage_test.clj b/test/puppetlabs/puppetdb/scf/storage_test.clj index 0f21bfb5a..5a120b273 100644 --- a/test/puppetlabs/puppetdb/scf/storage_test.clj +++ b/test/puppetlabs/puppetdb/scf/storage_test.clj @@ -1306,6 +1306,25 @@ (is (= 4 (:c (first (query-to-vec "SELECT count(*) AS c FROM catalog_resources WHERE certname_id = (select id from certnames where certname = ?)" certname))))))) +(deftest-db replace-catalog-with-many-resource-params + ;; 1100 new resources x 30 params = 99,000 bind parameters, more than the + ;; JDBC driver accepts in one statement. + (let [catalog (reduce (fn [cat i] + (let [title (str "res-" i)] + (assoc-in cat [:resources {:type "Param_test" :title title}] + {:type "Param_test" :title title :exported false + :file "/tmp/params.pp" :line i :tags #{"param_test"} + :parameters (into {} (for [p (range 30)] + [(keyword (str "param" p)) (str "value" p)]))}))) + catalog + (range 1100))] + (add-certname! certname) + (replace-catalog! catalog) + (is (= (* 1100 30) + (:c (first (query-to-vec "SELECT count(*) AS c FROM catalog_resources cr + INNER JOIN resource_params rp ON rp.resource = cr.resource + WHERE cr.type = 'Param_test'"))))))) + (deftest-db change-line-resource-metadata (add-certname! certname) (replace-catalog! catalog)