arXiv:2611.05892cs.DBNov 2026

Manuscript Offprint

arXiv:2611.05892

When Is One Query per Template Enough? Adapting Learned Cost Models to New Hardware

Krish Patel

Krish Patel

University of Waterloo, Waterloo, Canada

kmp.patel@uwaterloo.ca ORCID 0009-0006-3064-0086

Abstract—A learned cost model that predicts query runtimes is trained on measurements from one machine. When the database moves to other hardware, its plans stay the same but their runtimes change, each by its own factor. We ask how to adapt such a model from kk queries measured on the new machine: which queries to run, how to use their runtimes, and how many are enough. On PostgreSQL we timed the same plans of 1,162 TPC-H and TPC-DS queries in six environments: a laptop and a server, each with all its processors or pinned to one, the server also pinned to two, and the laptop also with a small buffer pool. No single factor describes the slowdown between two environments: most of its variance lies between query templates, so a speed factor stops improving after a few queries. A correction that fits separate lines for serial and parallel plans and adds an offset for every measured template brings the moved model, with one measured query per template, to an error (geometric mean over templates of the median Q-error) of 1.10 on TPC-H and 1.19 on TPC-DS, close to the same correction on a model trained on the new machine; fine-tuning a DACE-style neural cost model on the same queries gains at most 2%. On templates never measured, no correction we tried is on average more than 2% better than the speed factor. We then fixed hypotheses and timed new data. On TPC-H at ten times the scale the errors follow these results, but two of five repeated tests miss. On the Join Order Benchmark, whose queries differ widely within a template, the model errs by 2.36 even when retrained on the new machine, no correction gains much, and only the test on new templates holds; boosted trees and a neural model fit these queries better, and correcting the neural model works as well as fine-tuning it. We report every test as registered.

Index Terms—query performance prediction, learned cost models, hardware change, model adaptation, PostgreSQL

I. INTRODUCTION

Predicting how long a query will run before it runs serves admission control, scheduling and provisioning. Learned cost models predict runtimes from plan features far better than raw optimizer costs when they are trained on the system they predict [1]–[4]. A runtime, however, is a property of a plan on a machine, and machines change: a database moves to new hardware, to a cloud instance of another size, or into a container with fewer processors. The plans stay, and every runtime the model learned from is now off, by an amount that depends on the query.

Systems handle a move in three ways. Some retrain on the new machine: T3 reruns its whole benchmark [5], QCFE reruns simplified queries to refit its per-operator coefficients and then retrains on 200 labeled queries [6], and DACE fine-tunes on a workload rerun on the new machine [7]. Some map every old prediction through one fitted line, the model shift

used to move performance models of configurable software [8], [9], whose simplest form rescales every prediction by one speed factor. Others describe the machine with features and train one model over many machines, as Redshift's Stage predictor does with the instance type [10]. This paper asks what lies between retraining and rescaling: if we can afford to run kk queries on the new machine, which queries should they be, how should their runtimes be used, and how large must kk be?

We study the question with the plans held fixed, so that every change in runtime comes from the environment. We timed 1,162 queries from 115 templates of TPC-H and TPC-DS on PostgreSQL in six environments that run identical plans on copies of the same database. Their 30 ordered pairs per workload change the processor count, the hardware, both, the buffer pool, or several of these at once. To test whether the findings hold beyond these data, we then fixed our hypotheses and measured new data: TPC-H at scale factor 10 and the Join Order Benchmark (JOB) on its real IMDB data, each in several environments. We contribute:

  • A dataset of identical plans timed in many environments, which as far as we know has no public counterpart, and a measurement of how runtimes shift when only the environment changes (Section V-A). The slowdown differs widely between templates. Whether a plan has parallel workers explains much of a change of processor count, and how long a query runs explains much of a move from laptop to server; per-operator timings point to the mechanisms.

  • A comparison of six corrections, five baselines and a no-move control, a fine-tuned transformer cost model and six query selection strategies for 5 to 200 measured queries (Sections V-B to V-D). On TPC-H and TPC-DS, one measured query per template with a per-template offset brings the moved model close to the same correction on a model trained on the new machine; refitting the model on the measured queries is far worse up to one query per template, also when the refit is told the templates, and fine-tuning a neural model on the same queries gains at most 2%. In all four workloads of both rounds the moved model, so corrected, is on average within 3% of the same correction on a model trained on the new machine.

  • A negative result for new templates (Section V-E), repeated on the new data: no correction we tried does much

better than the speed factor, and its error is close to that of retraining. The remaining error is the model's error on a new template; the move adds little to it.

  • Two rounds of confirmatory tests (Sections V-F and V-H). We committed hypotheses, the primary method and the neural baseline to our repository before the labels they are tested on existed, and report every test under its registered rule. The first round's five tests hold. The second round's mostly fail: at ten times the data the errors follow the first round, but two of the five repeated tests miss, and so does H6a; on the Join Order Benchmark, where a template's queries differ in runtime by a factor of 2.31, the model's own error leaves little for any correction to gain, and only the test on new templates holds.

• Recommendations for moving a runtime model (Section VI) and a public artifact with every query, plan, label and script.

References (52)