From c3b49a0d91578b8eb068c4512af5d108090334c0 Mon Sep 17 00:00:00 2001 From: Jan Vansteenkiste Date: Wed, 25 Nov 2026 15:02:27 +0100 Subject: [PATCH] Batch resource_params inserts by row, not by resource The resource_params insert in add-params! was batched per 1000 resources, but emits one row (3 bind parameters) per parameter. A catalog whose new resources average more than ~21 parameters exceeded the PostgreSQL JDBC driver's 65,535 bind-parameter limit, so the replace catalog command failed on every retry. Batch the flattened rows instead, 10,000 at a time. Fixes #682 Co-Authored-By: Claude Opus 5.5 --- src/puppetlabs/puppetdb/scf/storage.clj | 21 +++++++++---------- test/puppetlabs/puppetdb/scf/storage_test.clj | 19 +++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/puppetlabs/puppetdb/scf/storage.clj b/src/puppetlabs/puppetdb/scf/storage.clj index e75fb68f8..e771b3939 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)