<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SoccerDream AI</title>
    <description>The latest articles on DEV Community by SoccerDream AI (@soccerdream_ai).</description>
    <link>https://dev.to/soccerdream_ai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4230974%2Fa0526b18-fda8-4dd0-9676-6e92fe0d43b4.png</url>
      <title>DEV Community: SoccerDream AI</title>
      <link>https://dev.to/soccerdream_ai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/soccerdream_ai"/>
    <language>en</language>
    <item>
      <title>The same audit query, two answers: 17.5% of rows disagree with themselves</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Sun, 29 Nov 2026 05:24:18 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/the-same-audit-query-two-answers-175-of-rows-disagree-with-themselves-222n</link>
      <guid>https://dev.to/soccerdream_ai/the-same-audit-query-two-answers-175-of-rows-disagree-with-themselves-222n</guid>
      <description>&lt;p&gt;At 13:00:20 I ran one query against production. At 13:02:18 I ran it again, unchanged.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*), count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE hit) AS hits
FROM ai_predictions;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t0   51529 | 50494 | 28749
t90  51530 | 50494 | 28749
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One row. But the same instability applied to a &lt;em&gt;date&lt;/em&gt; instead of a &lt;em&gt;second&lt;/em&gt; is not rounding. I asked production "what did the direction distribution look like as of 15 November?" two defensible ways. On the 13,404 predictions where both answers exist, &lt;strong&gt;2,346 — 17.50% — came back different.&lt;/strong&gt; A metric that moves 17.5% depending on how you phrase the question is not a metric. It is a story about the query.&lt;/p&gt;

&lt;p&gt;One production instance throughout: PostgreSQL 15.18, 116 GB, &lt;code&gt;shared_buffers&lt;/code&gt; 8 GB, xid 946,255,838.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things move at once
&lt;/h2&gt;

&lt;p&gt;Growth is the small part. &lt;code&gt;ai_predictions&lt;/code&gt; holds 51,532 rows and has absorbed 26,211,299 row updates since it was last bulk-loaded — &lt;strong&gt;508 rewrites per surviving row&lt;/strong&gt;. Of those rows, 51,336 (99.62%) carry &lt;code&gt;updated_at&lt;/code&gt; later than &lt;code&gt;created_at&lt;/code&gt; by more than a second; 39,034 were rewritten more than a day after creation.&lt;/p&gt;

&lt;p&gt;A "row" is not a stable object. It is a slot that values are poured into; reading it at 13:00 and 13:02 may return two different facts sharing a primary key.&lt;/p&gt;

&lt;p&gt;The third mover is settlement: predictions are graded hours or weeks after the row exists, so two filters that sound like the same moment are not. Counting rows with &lt;code&gt;created_at &amp;lt; '2026-11-01'&lt;/code&gt; gives &lt;strong&gt;37,179&lt;/strong&gt;; counting rows with &lt;code&gt;settled_at &amp;lt; '2026-11-01'&lt;/code&gt; gives &lt;strong&gt;34,915&lt;/strong&gt;. Grade the graded ones and the rate shifts too: 57.29% under &lt;code&gt;created_at&lt;/code&gt; (37,104 rows, 21,258 hits) against 57.64% under &lt;code&gt;settled_at&lt;/code&gt; (34,912 rows, 20,123 hits). Neither is wrong: they answer &lt;em&gt;which predictions existed&lt;/em&gt; versus &lt;em&gt;which verdicts existed&lt;/em&gt;, and a rate without its filter beside it is unfalsifiable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The snapshot exists — it just has no name
&lt;/h2&gt;

&lt;p&gt;Postgres can give a stable answer, by default, inside a transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;REPEATABLE&lt;/span&gt; &lt;span class="no"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;txid_current_snapshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*), count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 90 s pass, then the same statement again, same transaction:&lt;/span&gt;
&lt;span class="c1"&gt;-- t0   51530 | 50494 | 946259841:946259841:&lt;/span&gt;
&lt;span class="c1"&gt;-- t90  51530 | 50494 | 946259841:946259841:   identical = True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ninety seconds, one insert committed by another session in between, byte-identical output. &lt;code&gt;946259841:946259841:&lt;/code&gt; is &lt;code&gt;xmin:xmax:xip_list&lt;/code&gt; — the boundary of what the transaction sees. Two numbers and an empty in-progress list froze a 51,532-row table.&lt;/p&gt;

&lt;p&gt;Why does nobody store that name? Because it is only valid inside the transaction; commit, and the next session gets a new one, the old one unaddressable. Postgres does not keep row versions for you to travel through — autovacuum removed 936 dead tuples here at 13:00:33, and those are exactly the versions a historical read would need.&lt;/p&gt;

&lt;p&gt;Reproducibility has to be purchased at write time, not read time. Two candidate watermarks already sit in the table. Both leak.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watermark one: the id column
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;id&lt;/code&gt; is a bigserial, so its order &lt;em&gt;is&lt;/em&gt; insertion order, and &lt;code&gt;id &amp;lt;= 1454196&lt;/code&gt; looks like "everything that existed at 13:00:20". Window functions over that order show the cost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS total, count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;prev_max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;late_inserted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev_max&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;max_backdate&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="k"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;prev_max&lt;/span&gt;
      &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;prev_max&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 51531 | 4755 | 5 days 13:34:15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4,755 rows (9.2%) carry a &lt;code&gt;created_at&lt;/code&gt; older than a row inserted before them; one was inserted more than five days after the timestamp it claims. By month, this is not noise, it is a migration:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;inserted out of order&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;2,031 (74.3%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;2,516 (95.4%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;164&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,757&lt;/td&gt;
&lt;td&gt;125 (1.3%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,886&lt;/td&gt;
&lt;td&gt;53 (0.2%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;14,353&lt;/td&gt;
&lt;td&gt;30 (0.2%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;June and July were backfilled wholesale: rows arrived in an order unrelated to the dates they carry. Since September the write path is honest at 0.2%, but 74–95% months make any id-based reconstruction of early history fiction. That query also took &lt;strong&gt;97.6 seconds&lt;/strong&gt;, sorting a table whose &lt;code&gt;n_live_tup&lt;/code&gt; claimed 89,575 against a truth of 51,532.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watermark two: the timestamp you filtered on
&lt;/h2&gt;

&lt;p&gt;Filter on &lt;code&gt;created_at &amp;lt;= T&lt;/code&gt; and it holds up better than expected: &lt;strong&gt;0&lt;/strong&gt; rows claim a &lt;code&gt;created_at&lt;/code&gt; before 1 November while being physically written after the first post-cutoff row.&lt;/p&gt;

&lt;p&gt;But it watermarks &lt;em&gt;existence&lt;/em&gt;, not state. Between 15 November and today, &lt;strong&gt;4,657 rows&lt;/strong&gt; created before that date were rewritten after it — 9.92% of the pre-cutoff cohort — and every rewrite is invisible to that filter. You read today's value, label it "as of 15 November", and the 17.50% appears.&lt;/p&gt;

&lt;h2&gt;
  
  
  A revisions table exists, and still cannot tell you when
&lt;/h2&gt;

&lt;p&gt;I expected to conclude "there is no history". There is: &lt;code&gt;ai_prediction_revisions&lt;/code&gt; holds &lt;strong&gt;206,310 rows&lt;/strong&gt; across 18,046 prediction slots, 6,858 matches and 2,102 models, capturing since 2026-10-30. Of those slots 16,414 have more than one revision; the mean is 11.44 and the maximum is &lt;strong&gt;898&lt;/strong&gt;. 6,202 slots (34.4%) contain at least one change of &lt;code&gt;direction&lt;/code&gt; — exactly the mutations the live table overwrites.&lt;/p&gt;

&lt;p&gt;The defect: 21 columns, none of them the prediction's primary key. &lt;code&gt;revisions.id&lt;/code&gt; is its own sequence (309,226 → 515,794), not a reference — joining on it returns &lt;strong&gt;0&lt;/strong&gt;. The only usable key is the pair &lt;code&gt;(match_id, model_id)&lt;/code&gt;, unique in &lt;code&gt;ai_predictions&lt;/code&gt; today — 51,532 rows, 51,532 distinct pairs — because of how the writer currently behaves. Nothing declares it; nothing stops it from ceasing to be true. &lt;strong&gt;A history table keyed by coincidence.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Its time axis is undocumented. If &lt;code&gt;created_at&lt;/code&gt; were the capture moment it would equal the live &lt;code&gt;updated_at&lt;/code&gt;; if it were a copied field it would always equal &lt;code&gt;created_at&lt;/code&gt;. Over 206,379 joins:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;what &lt;code&gt;revisions.created_at&lt;/code&gt; equals&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;share&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;the live &lt;code&gt;updated_at&lt;/code&gt; (capture time)&lt;/td&gt;
&lt;td&gt;361&lt;/td&gt;
&lt;td&gt;0.17%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the live &lt;code&gt;created_at&lt;/code&gt; (copied field)&lt;/td&gt;
&lt;td&gt;17,727&lt;/td&gt;
&lt;td&gt;8.59%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;later than the live &lt;code&gt;updated_at&lt;/code&gt; + 60 s&lt;/td&gt;
&lt;td&gt;5,785&lt;/td&gt;
&lt;td&gt;2.80%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Neither hypothesis survives, and the third row cannot be produced by either semantics. You cannot build an as-of reconstruction on a clock you cannot read. Coverage compounds it: 18,046 of 51,532 slots, &lt;strong&gt;35.0%&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The 46 tables named like backups (&lt;code&gt;ai_predictions_narrsync_bk_20261015&lt;/code&gt; … &lt;code&gt;_20261129&lt;/code&gt;) do not rescue this. They hold 41, 5, 13 and 20 rows on 1, 15, 28 and 29 November — the handful of rows a sync job touched, named after the run date, recording no transaction boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reconstructing an as-of answer, and its bill
&lt;/h2&gt;

&lt;p&gt;The revisions table is still the best instrument available, defects included. One statement per time point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_prediction_revisions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2026-11-15'&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM latest GROUP BY 1 ORDER BY 2 DESC;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Against the naive reading — current values filtered by &lt;code&gt;created_at&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;direction&lt;/th&gt;
&lt;th&gt;as-of reading (revisions)&lt;/th&gt;
&lt;th&gt;naive reading (live rows)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;home&lt;/td&gt;
&lt;td&gt;5,005&lt;/td&gt;
&lt;td&gt;19,213&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;away/draw&lt;/td&gt;
&lt;td&gt;3,439&lt;/td&gt;
&lt;td&gt;10,924&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;away&lt;/td&gt;
&lt;td&gt;3,196&lt;/td&gt;
&lt;td&gt;10,510&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;home/draw&lt;/td&gt;
&lt;td&gt;1,706&lt;/td&gt;
&lt;td&gt;5,930&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Totals differ because denominators differ (13,404 covered slots versus 46,926 live rows), so compare shares, not levels. On the 13,404 slots where both exist: &lt;strong&gt;2,346 disagree, 17.50%&lt;/strong&gt;. The query reads 206,310 rows and needs &lt;code&gt;created_at DESC, id DESC&lt;/code&gt;; without the serial tie-break the result depends on plan order — 180 ms for one time point, 307 ms for join-and-compare.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ledger: make the watermark part of the number
&lt;/h2&gt;

&lt;p&gt;The fix is refusing to publish a number with no address. Every audit result gets stamped with the snapshot it was computed inside, in a row holding &lt;code&gt;metric&lt;/code&gt;, &lt;code&gt;as_of_xid&lt;/code&gt;, &lt;code&gt;as_of_wall&lt;/code&gt;, the verbatim &lt;code&gt;row_filter&lt;/code&gt;, &lt;code&gt;row_count&lt;/code&gt;, &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;fingerprint&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fingerprint makes a rerun falsifiable. For &lt;code&gt;ai_predictions&lt;/code&gt; right now: 51,532 rows, &lt;code&gt;sum(id)&lt;/code&gt; 57,099,908,137, 28,749 hits, md5 &lt;code&gt;e4464ff27c34985f3bd4630a17960ba2&lt;/code&gt;. A changed digest means the table moved, and &lt;code&gt;sum(id)&lt;/code&gt; inside it says by how much; an unchanged digest with a changed value means the &lt;em&gt;query&lt;/em&gt; changed — a different bug with a different owner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;audit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row_filter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isolation_level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'REPEATABLE READ'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;autocommit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT txid_current_snapshot()::text, now()"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;row_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT INTO audit_ledger (metric, as_of_xid, as_of_wall, row_filter,"&lt;/span&gt;
                &lt;span class="s2"&gt;" row_count, value, fingerprint) VALUES (%s,%s,%s,%s,%s,%s,%s)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wall&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row_filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"as_of_xid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"value"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"fp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;REPEATABLE READ&lt;/code&gt; means every statement in one report sees the same snapshot, so the 51,529/51,530 divergence becomes structurally impossible. And &lt;code&gt;txid_current_snapshot()&lt;/code&gt; is a string you can paste into a ticket: &lt;em&gt;this number was true inside 946259841:946259841:&lt;/em&gt; is a claim someone else can check.&lt;/p&gt;

&lt;p&gt;The limits are honest: a snapshot is not re-readable after commit, so the ledger buys reproducibility of the &lt;em&gt;record&lt;/em&gt;, not time travel. The latter needs row versions written down at update time, with a real foreign key, one unambiguous capture timestamp and a &lt;code&gt;(prediction_id, captured_at)&lt;/code&gt; primary key. The revisions table is 70% of that design, missing the two columns that matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I changed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Every metric ships with its &lt;code&gt;WHERE&lt;/code&gt; clause and row count in the same sentence. A rate without a denominator is a rumour.&lt;/li&gt;
&lt;li&gt;Reports run inside one &lt;code&gt;REPEATABLE READ&lt;/code&gt; transaction and store &lt;code&gt;txid_current_snapshot()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt; is not a watermark. After the June–July backfill it is an insertion counter for a table that was rebuilt.&lt;/li&gt;
&lt;li&gt;No &lt;code&gt;ORDER BY&lt;/code&gt; ships without a tie-break down to a unique column: among 744 models with 20 or more graded rows there are only 212 distinct rounded rates, so ties are the norm and the top-20 boundary at 75.4% is unique today by luck.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;narrsync_bk&lt;/code&gt; tables are deltas, not snapshots, and are documented as such so nobody restores from one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The one-row gap between 13:00:20 and 13:02:18 was never the interesting number. The interesting number is 17.50% — the share of rows that answer "as of 15 November" differently depending on which clock you hand them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>48.9 seconds for 20 rows: auditing a Postgres database whose query statistics were never switched on</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Sat, 28 Nov 2026 05:10:20 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/489-seconds-for-20-rows-auditing-a-postgres-database-whose-query-statistics-were-never-switched-on-8hn</link>
      <guid>https://dev.to/soccerdream_ai/489-seconds-for-20-rows-auditing-a-postgres-database-whose-query-statistics-were-never-switched-on-8hn</guid>
      <description>&lt;p&gt;Last week I asked production for the twenty most recent rows of a 17-million-row table. It took &lt;strong&gt;48.9 seconds&lt;/strong&gt;. The same query, with three words added to the &lt;code&gt;ORDER BY&lt;/code&gt;, returns in &lt;strong&gt;0.21 ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That is a 230,000x gap, and it is not the interesting part. The interesting part is that none of the instruments I thought I had could have found it for me. One production instance throughout: PostgreSQL 15.18, 116 GB, &lt;code&gt;shared_buffers&lt;/code&gt; 8 GB, &lt;code&gt;work_mem&lt;/code&gt; 24 MB, &lt;code&gt;max_parallel_workers_per_gather&lt;/code&gt; = 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  The instrument was installed, not switched on
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; requires a preload at postmaster start. The extension installs the view; the preload installs the numbers. I had the first, not the second:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;extname&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_extension&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- dblink, pg_stat_statements, pg_trgm, plpgsql&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM pg_stat_statements;
-- ERROR: pg_stat_statements must be loaded via shared_preload_libraries
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing in the application surfaced this. When the audit question is always &lt;em&gt;"which query is expensive"&lt;/em&gt;, a view without data means the answer is a guess — not "approximately", structurally unavailable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is left: lifetime counters with no attribution
&lt;/h2&gt;

&lt;p&gt;Remove per-query history and all you have is &lt;code&gt;pg_stat_user_tables&lt;/code&gt;: a lifetime counter bundle that knows a table was scanned 4 billion times and refuses to say by whom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_live_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;n_tup_ins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_tup_upd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_tup_del&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;to_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_autovacuum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'MM-DD HH24:MI'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;last_av&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;to_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'MM-DD HH24:MI'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;last_an&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ai_predictions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'nami_matches'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="s1"&gt;'nami_europe_odds'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'odds_snapshot_ledger'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;n_live_tup&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;table&lt;/th&gt;
&lt;th&gt;live rows&lt;/th&gt;
&lt;th&gt;seq scans&lt;/th&gt;
&lt;th&gt;index scans&lt;/th&gt;
&lt;th&gt;updates&lt;/th&gt;
&lt;th&gt;last analyze&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;nami_europe_odds&lt;/td&gt;
&lt;td&gt;17,147,413&lt;/td&gt;
&lt;td&gt;14,041&lt;/td&gt;
&lt;td&gt;4,157,882,455&lt;/td&gt;
&lt;td&gt;82,289,979&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;nami_matches&lt;/td&gt;
&lt;td&gt;133,734&lt;/td&gt;
&lt;td&gt;2,333,243&lt;/td&gt;
&lt;td&gt;17,068,342,053&lt;/td&gt;
&lt;td&gt;1,368&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ai_predictions&lt;/td&gt;
&lt;td&gt;50,949&lt;/td&gt;
&lt;td&gt;100,991&lt;/td&gt;
&lt;td&gt;3,440,138,343&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;td&gt;11-10 06:30&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the last two rows together. &lt;code&gt;ai_predictions&lt;/code&gt; holds 50,949 rows and has served 3.44 billion index scans: &lt;strong&gt;67,521 scans per live row&lt;/strong&gt;, which over the 160 days this platform has prediction history is roughly 250 per second. &lt;code&gt;nami_matches&lt;/code&gt; holds 133,734 rows and has served 17.07 billion: &lt;strong&gt;127,630 per row&lt;/strong&gt;. A 50k-row table probed 250 times a second is not a bad schema. It is a read pattern nobody measured, and the counters are the only evidence it exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same counters cannot be reconciled
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ai_predictions&lt;/code&gt; reports 25,676,350 inserts, 75 deletes, and 50,949 live rows: inserts minus deletes is off by a factor of 504. The id sequence agrees something odd happened — it stands at 1,450,671, and the maximum surviving id is 1,450,671, minimum 8,733.&lt;/p&gt;

&lt;p&gt;Only a &lt;code&gt;TRUNCATE&lt;/code&gt; or a table rebuild holds 25.7M inserts and 50k rows without an incrementing delete counter. Neither leaves a trace, and &lt;code&gt;stats_reset&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt;, so no timestamp bounds the window. I can read the counter; I cannot reconstruct the interval. &lt;strong&gt;Any rate derived from it — rows per day, growth per month — is an invented number wearing a real column name.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The instrument that always works: watch the buffers
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; needs no preload. It reports what the planner thought, what actually happened, and how much of it happened in 8 kB pages. Five queries, measured back to back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COSTS&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;END&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;NULLIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*),0)
FROM ai_predictions p WHERE p.settled
GROUP BY 1 ORDER BY 2 DESC LIMIT 10;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;query&lt;/th&gt;
&lt;th&gt;plan cost&lt;/th&gt;
&lt;th&gt;actual&lt;/th&gt;
&lt;th&gt;shared buffers&lt;/th&gt;
&lt;th&gt;buffers in GiB&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;leaderboard (settled hit rate per model)&lt;/td&gt;
&lt;td&gt;36,962&lt;/td&gt;
&lt;td&gt;61.1 ms&lt;/td&gt;
&lt;td&gt;22,353&lt;/td&gt;
&lt;td&gt;0.17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;monthly hit rate&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;64.8 ms&lt;/td&gt;
&lt;td&gt;22,350&lt;/td&gt;
&lt;td&gt;0.17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;full count of &lt;code&gt;nami_europe_odds&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;262,731&lt;/td&gt;
&lt;td&gt;1,386.0 ms&lt;/td&gt;
&lt;td&gt;810,899&lt;/td&gt;
&lt;td&gt;6.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;top 20 rows by &lt;code&gt;changed_at DESC&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;609,662&lt;/td&gt;
&lt;td&gt;48,899.0 ms&lt;/td&gt;
&lt;td&gt;1,408,491&lt;/td&gt;
&lt;td&gt;10.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;fan-out join, prediction x price rows&lt;/td&gt;
&lt;td&gt;709,185&lt;/td&gt;
&lt;td&gt;6,418.7 ms&lt;/td&gt;
&lt;td&gt;2,378,473&lt;/td&gt;
&lt;td&gt;18.1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The real work of this platform is cheap. The two queries answering "how accurate is each model" and "how did accuracy move by month" cost 126 ms and 0.34 GiB of logical reads together. Everything expensive was something I wrote by accident.&lt;/p&gt;

&lt;p&gt;Cost does not rank time either: the 709k-cost join finished 7.6x faster than the 610k-cost sort, because the sort paid 34,606 physical block reads and the join came out of cache. Cost is a cached-page fiction. Buffers are not.&lt;/p&gt;

&lt;h2&gt;
  
  
  48.9 seconds for 20 rows
&lt;/h2&gt;

&lt;p&gt;Here is the pair, run minutes apart on the same table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COSTS&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_europe_odds&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Limit (actual time=48799.5..48898.9 rows=20)&lt;/span&gt;
&lt;span class="c1"&gt;--   -&amp;gt; Parallel Index Only Scan using idx_eu_match_company_time&lt;/span&gt;
&lt;span class="c1"&gt;--        (actual time=0.027..47911.0 rows=8611882 loops=2)&lt;/span&gt;
&lt;span class="c1"&gt;--        Heap Fetches: 407884&lt;/span&gt;
&lt;span class="c1"&gt;-- Buffers: shared hit=1,373,885 read=34,606&lt;/span&gt;

&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COSTS&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_europe_odds&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;NULLS&lt;/span&gt; &lt;span class="k"&gt;LAST&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Limit (actual time=0.052..0.201 rows=20)&lt;/span&gt;
&lt;span class="c1"&gt;--   -&amp;gt; Index Scan using idx_neo_changed_desc (actual time=0.052..0.199 rows=20)&lt;/span&gt;
&lt;span class="c1"&gt;-- Buffers: shared hit=16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;17,223,764 index tuples read to produce 20 rows, versus 16 pages. 270 MB pulled from disk. The cause is not a missing index; an index for exactly this order exists, 543 MB of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_neo_changed_desc&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;nami_europe_odds&lt;/span&gt;
  &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;btree&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;NULLS&lt;/span&gt; &lt;span class="k"&gt;LAST&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL's default for &lt;code&gt;DESC&lt;/code&gt; is &lt;code&gt;NULLS FIRST&lt;/code&gt;. The index was declared &lt;code&gt;NULLS LAST&lt;/code&gt;, so it cannot satisfy &lt;code&gt;ORDER BY changed_at DESC&lt;/code&gt;, and the planner reached for the only index whose leading columns cover the query. &lt;code&gt;changed_at&lt;/code&gt; holds &lt;strong&gt;zero&lt;/strong&gt; NULLs — &lt;code&gt;count(*) WHERE changed_at IS NULL&lt;/code&gt; is 0 — but the column is nullable, and the planner may not reason from "currently zero" to "cannot be NULL". A &lt;code&gt;NOT NULL&lt;/code&gt; constraint, or &lt;code&gt;NULLS LAST&lt;/code&gt; in the query, and the 48.9 seconds disappears.&lt;/p&gt;

&lt;p&gt;The 543 MB index has been scanned 3,841,275 times; the &lt;code&gt;..._changed_at_brin&lt;/code&gt; beside it, zero. Both facts are visible only because someone asked.&lt;/p&gt;

&lt;h2&gt;
  
  
  1,104 price rows per prediction
&lt;/h2&gt;

&lt;p&gt;The expensive query I actually wrote was an attribution question: which price snapshot drove which prediction. The obvious join does it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COSTS&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM ai_predictions p
JOIN nami_europe_odds o ON o.match_id = p.match_id;
-- Nested Loop (actual time=0.086..4895.280 rows=28121691 loops=2)
-- Index Only Scan using idx_neo_mid_company on nami_europe_odds o
--   (actual time=0.006..0.102 rows=1104 loops=50949)
-- Execution Time: 6418.706 ms
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;50,949 predictions multiplied by an average of 1,104 price rows per fixture yields &lt;strong&gt;56,243,382 intermediate rows&lt;/strong&gt; — a 1,104x amplification of a question that only needed counts. Wrap it in &lt;code&gt;GROUP BY model_id&lt;/code&gt; and it costs 12.3 seconds. The reader wanted one number per model; the database built 56 million rows to find 4,008.&lt;/p&gt;

&lt;p&gt;The guard I now put in front of every audit query costs one round trip and no execution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="no"&gt;ROW_BUDGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200_000&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plan_rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"EXPLAIN (FORMAT JSON) "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Plan"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Plan Rows"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Node Type"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"Limit"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Plans"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;guarded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;est&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plan_rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;est&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;ROW_BUDGET&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"refused: plan builds ~{est:,} rows (budget {ROW_BUDGET:,})"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the join above it reports 26,459,498 estimated rows and refuses in 1.07 ms — the planning time the server printed. The estimate is 2.13x under the truth, which is fine: the budget is a floor check, not a model. A query wrong by 2x in the millions is never a query I wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  1,340 indexes that have never been scanned
&lt;/h2&gt;

&lt;p&gt;Indexes charge every write, not the query you are optimizing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;tablename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexdef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'^CREATE (UNIQUE )?INDEX \S+ ON \S+ '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n, string_agg(indexname,' + ') AS names,
       pg_size_pretty(sum(pg_relation_size(indexrelid))) AS total,
       string_agg(s.idx_scan::text,' / ') AS scans
FROM pg_stat_user_indexes s
JOIN pg_indexes x ON x.indexname = s.indexrelname AND x.tablename = s.relname
WHERE s.schemaname = 'public' AND indexdef NOT LIKE '%WHERE%'
GROUP BY 1,2 HAVING count(*)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pg_relation_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;table&lt;/th&gt;
&lt;th&gt;shape&lt;/th&gt;
&lt;th&gt;indexes&lt;/th&gt;
&lt;th&gt;size&lt;/th&gt;
&lt;th&gt;scans&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;nami_europe_odds&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(match_id, company_id, changed_at DESC)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1,557 MB&lt;/td&gt;
&lt;td&gt;1,907,403,484 / 0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;nami_bd_odds_snapshot&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(nami_match_id, lottery_type, snapshot_time DESC)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;57 MB&lt;/td&gt;
&lt;td&gt;0 / 789,599&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;nami_matches&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(away_team_id, match_time DESC)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;6,008 kB&lt;/td&gt;
&lt;td&gt;0 / 9,543,153&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two byte-identical 1 GB-class b-trees on the hottest table: one served 1.9 billion scans, the other none, and all 82,289,979 updates to that table maintained both. Across the schema: &lt;strong&gt;3,079 indexes, 1,340 of them scanned zero times, occupying 3,060 MB.&lt;/strong&gt; A unique constraint is a guard, not a plan node, so some idle indexes are legitimate. A 520 MB duplicate with a scan count of exactly zero is not a guard. It is a write tax.&lt;/p&gt;

&lt;h2&gt;
  
  
  Never analyzed, and one worker
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;last_analyze&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt; on &lt;code&gt;nami_europe_odds&lt;/code&gt;: 17 million rows, 11 GB of heap, never analyzed at any point the statistics remember. And &lt;code&gt;max_parallel_workers_per_gather&lt;/code&gt; is 1, so the 48.9-second sort split its scan across exactly two processes.&lt;/p&gt;

&lt;p&gt;None of this needs a profiler. It needs five queries a week and a belief in buffer counts. The preload is the real fix — one line in &lt;code&gt;postgresql.conf&lt;/code&gt;, one restart — but it only tells you where the cost already went. &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; tells you what a query will cost before you ship it, which is the only moment the information is still cheap.&lt;/p&gt;

&lt;p&gt;What I changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plan first. Anything estimated above 200k intermediate rows is rewritten, not waited on.&lt;/li&gt;
&lt;li&gt;Specify &lt;code&gt;NULLS LAST&lt;/code&gt; when the index says &lt;code&gt;NULLS LAST&lt;/code&gt;, and add &lt;code&gt;NOT NULL&lt;/code&gt; wherever "zero NULLs today" is load-bearing for a plan.&lt;/li&gt;
&lt;li&gt;Quote a counter with no reset timestamp as a counter, never divided by a date.&lt;/li&gt;
&lt;li&gt;Delete a duplicate index only after reading its scan count; one side of that 1,557 MB pair had served 1.9 billion scans.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 20-row query now takes 0.21 ms. What took 48.9 seconds was never the database. It was me, not looking.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>One row, four directions: the label-provenance problem in 50,200 predictions</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Fri, 27 Nov 2026 05:04:23 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/one-row-four-directions-the-label-provenance-problem-in-50200-predictions-26lg</link>
      <guid>https://dev.to/soccerdream_ai/one-row-four-directions-the-label-provenance-problem-in-50200-predictions-26lg</guid>
      <description>&lt;p&gt;Two months ago I opened a row and read this sentence twice.&lt;/p&gt;

&lt;p&gt;The stored direction column said &lt;code&gt;home/draw&lt;/code&gt;. The summary column said the home side would not lose. Eight hundred characters deeper, inside the serialized analysis, the reasoning string said the away side would not lose. Same fixture, same model, same row, same second. One of those sentences was written by a copy step, and nobody marked which.&lt;/p&gt;

&lt;p&gt;That is the whole problem in one example: a prediction is not one claim, it is a claim plus every copy of itself, and the copies drift.&lt;/p&gt;

&lt;p&gt;Production context: &lt;code&gt;ai_predictions&lt;/code&gt;, 50,200 rows, 2026-06-21 to 2026-11-27, 4,020 distinct models, 49,686 settled, 28,352 correct (57.06%).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                          AS rows,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE hit)       AS correct,
       round(100.0*count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;nullif&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE settled),0),2) AS pct,
       count(DISTINCT model_id)          AS models
FROM   ai_predictions;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; rows  | settled | correct | pct  | models
-------+---------+---------+------+--------
 50200 |   49686 |   28352 | 57.06|   4020
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where a direction can live
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;prediction_direction&lt;/code&gt; is a &lt;code&gt;varchar(100)&lt;/code&gt; with exactly six values in production: &lt;code&gt;home&lt;/code&gt; 20,619, &lt;code&gt;away/draw&lt;/code&gt; 11,625, &lt;code&gt;away&lt;/code&gt; 11,189, &lt;code&gt;home/draw&lt;/code&gt; 6,413, &lt;code&gt;draw&lt;/code&gt; 347, &lt;code&gt;none&lt;/code&gt; 7. Clean alphabet, one column, easy to &lt;code&gt;GROUP BY&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The analysis blob does not respect that. It is &lt;code&gt;text&lt;/code&gt;, and it holds a JSON document on 46,935 rows, a bracketed prose block on 48, plain prose on 3,112, an unparseable fragment on 8, and nothing at all on 105. Inside the JSON, the label has four possible homes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM (
  SELECT jsonb_object_keys(full_analysis::jsonb) AS key
  FROM   ai_predictions
  WHERE  left(btrim(full_analysis),1) = '{'
    AND  full_analysis ~ '^\s*\{.*\}\s*$'
) k GROUP BY 1 ORDER BY 2 DESC LIMIT 6;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; key             | count
-----------------+-------
 asian_handicap  | 45928
 goal_line       | 43599
 confidence      | 25682
 direction       | 25664
 prediction      | 22044
 _dir_source     |  6482
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;direction&lt;/code&gt; appears 25,664 times at top level. &lt;code&gt;prediction&lt;/code&gt; — itself an object — appears 22,044 times, and on 21,237 of those rows it carries its own &lt;code&gt;direction&lt;/code&gt;. &lt;code&gt;_dir_source&lt;/code&gt; appears 6,482 times and records three candidate readings plus which one was used. &lt;code&gt;_dir_text_sync&lt;/code&gt; appears 3,338 times and records a rewrite. Four places, one claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normalize before you count, or you will overcount by 5 points
&lt;/h2&gt;

&lt;p&gt;The naive comparison is a string equality between the column and &lt;code&gt;full_analysis-&amp;gt;&amp;gt;'direction'&lt;/code&gt;. On 25,664 rows that reports 7,567 mismatches — 29.48%. Most of that 5-point gap is spelling, not disagreement: some models wrote &lt;code&gt;胜&lt;/code&gt;/&lt;code&gt;负&lt;/code&gt;/&lt;code&gt;平&lt;/code&gt; instead of &lt;code&gt;home&lt;/code&gt;/&lt;code&gt;away&lt;/code&gt;/&lt;code&gt;draw&lt;/code&gt;, and some wrote &lt;code&gt;draw/home&lt;/code&gt; where the column says &lt;code&gt;home/draw&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;ZH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"胜"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"home"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"主胜"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"home"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"负"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"away"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"客胜"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"away"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"平"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"draw"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"平局"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"draw"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;norm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;ZH&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;ZH&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"none"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"home"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"draw"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"away"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;   &lt;span class="c1"&gt;# order-independent pair&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that normalizer the same comparison gives 25,208 paired rows and 6,183 mismatches — &lt;strong&gt;24.53%&lt;/strong&gt;. Five percentage points of the naive number were a vocabulary problem. This is the cheapest lesson in the whole audit: before you measure disagreement, define the alphabet, or you will spend a week chasing models that only had a different accent.&lt;/p&gt;

&lt;h2&gt;
  
  
  The disagreements are persistent, not a bad month
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;to_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'YYYY-MM'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE left(btrim(full_analysis),1)='{') AS json_rows
FROM ai_predictions GROUP BY 1 ORDER BY 1;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; month   | rows  | paired | disagree | rate
---------+-------+--------+----------+------
 2026-06 |  2735 |   1861 |      933 | 50.1%
 2026-07 |  2637 |      8 |        1 | 12.5%
 2026-08 |   164 |      1 |        0 |  0.0%
 2026-09 |  9757 |   1937 |      652 | 33.7%
 2026-10 | 21886 |  13672 |     3286 | 24.0%
 2026-11 | 13021 |   7729 |     1311 | 17.0%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things stand out. First, the rate improves but never reaches zero: 17% of November rows still carry two different answers. Second, look at the &lt;code&gt;paired&lt;/code&gt; column: in July only 8 of 2,637 rows have a top-level &lt;code&gt;direction&lt;/code&gt; at all. The label moved house that month — it lived inside &lt;code&gt;prediction.direction&lt;/code&gt; instead — and moved back in September. A pipeline that reads one location would have reported "no narrative label" for 2,629 rows and silently dropped them from the audit.&lt;/p&gt;

&lt;h2&gt;
  
  
  When both labels exist, they disagree 94% of the time
&lt;/h2&gt;

&lt;p&gt;There are 762 rows where the blob carries a top-level &lt;code&gt;direction&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; a nested &lt;code&gt;prediction.direction&lt;/code&gt;. On 709 of them the two values differ — 93.0%. Of the 754 that are settled, here is the accuracy each reading would earn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; reading                  | n   | correct | rate
--------------------------+-----+---------+-------
 prediction_direction     | 754 |     377 | 50.0%
 top-level direction      | 754 |     334 | 44.3%
 prediction.direction     | 754 |     393 | 52.1%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three numbers from one row. None of them is a lie; each is a faithful reading of a different field. That is exactly why "the model is 52% accurate" is not a statement about the model until you name the field.&lt;/p&gt;

&lt;h2&gt;
  
  
  Only one of them is graded
&lt;/h2&gt;

&lt;p&gt;The settled flag and the hit flag are written by the settlement job, not by the narrative. Recomputing the outcome from &lt;code&gt;prediction_direction&lt;/code&gt; plus &lt;code&gt;direction_arity&lt;/code&gt; — exact match for &lt;code&gt;single&lt;/code&gt;, either side for &lt;code&gt;double&lt;/code&gt; — reproduces the stored &lt;code&gt;hit&lt;/code&gt; on &lt;strong&gt;48,911 of 49,453 rows (98.90%)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="k"&gt;WHERE&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual_result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ANY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;string_to_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction_direction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt;
       &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;actual_result&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;hit&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;prediction_direction&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'home'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'away'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'draw'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'home/draw'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'away/draw'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the column is the graded object, and the narrative is decoration from the grader's point of view. Which is fine — until someone quotes the narrative. On the 6,153 settled rows where the two readings differ, accuracy under the column is 52.46% and under the narrative reading 48.09%. Same rows, 4.4 points apart. If your public number is built from a text scrape instead of the column, you are publishing a different product.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rewrite ledger covers 7% of the disagreements
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;_dir_text_sync&lt;/code&gt; object is the honest part of this system. All 3,338 instances carry a note tagged as a platform rewrite, with &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;to&lt;/code&gt;. It first appears in September (2 rows), then 1,521 in October and 1,815 in November.&lt;/p&gt;

&lt;p&gt;What the rewrite did to the label's &lt;em&gt;cardinality&lt;/em&gt; explains most of the movement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; kind   | rows | acc of "from" | acc of "to"
--------+------+---------------+-------------
 widen  |  394 |        32.74% |      55.33%
 narrow | 2454 |        74.53% |      50.29%
 shift  |  397 |        70.03% |      54.66%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those gaps are arithmetic, not skill. Widening a single pick to a pair adds an outcome that occurs roughly a quarter of the time, so accuracy jumps ~23 points for free; narrowing a pair to a single removes the same quarter, so accuracy falls ~24 points. A 23-point swing in a published metric is available to anyone who can edit which of two strings is called "the direction".&lt;/p&gt;

&lt;p&gt;And the ledger does not fully reconcile either. Of the 3,320 rewrites with both fields readable, the &lt;code&gt;from&lt;/code&gt; value differs from the stored column on 2,934 rows (88.4%) — expected. But the &lt;code&gt;to&lt;/code&gt; value &lt;em&gt;also&lt;/em&gt; differs from the stored column on 766 rows (23.1%), and 115 rewrites move the pick to the opposite side of the fixture entirely. That means at least two rewrite steps happened and only the last was persisted.&lt;/p&gt;

&lt;p&gt;Here is the part that should worry you: of the 6,183 disagreements, only &lt;strong&gt;435&lt;/strong&gt; have a ledger entry. The other 5,748 — 92.9% — are silent. A reader cannot tell whether those rows were rewritten, whether the column was written from a different source, or whether the narrative was generated before the column was corrected.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix is a write-time invariant, not a cleanup script
&lt;/h2&gt;

&lt;p&gt;You cannot repair this after the fact; the information needed to decide which copy was right is not in the row. What you can do is make the next write refuse to create a second truth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- one direction per row, stored once, referenced everywhere else&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;direction_alphabet&lt;/span&gt;
  &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction_direction&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'home'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'draw'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'away'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'home/draw'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'away/draw'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'none'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- every rewrite becomes an append-only row, never an edit in place&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;direction_rewrite_ledger&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;prediction_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;seq&lt;/span&gt;           &lt;span class="kt"&gt;int&lt;/span&gt;    &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;from_label&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt;   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;to_label&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt;   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rule_id&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt;   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;decided_at&lt;/span&gt;    &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prediction_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two cheap guards close most of the gap. The first is an assertion at write time: if the analysis text contains a label, it must equal the column, or the insert fails and the row goes to a review queue instead of production. The second is a daily count of rows where a label exists and disagrees without a ledger entry — the number above, 5,748, is the backlog that guard would have surfaced on day one instead of month five.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- the one query I would put on a dashboard&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE has_label)          AS labeled,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;has_label&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;has_ledger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;silent&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_analysis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'direction'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;has_label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_analysis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'_dir_text_sync'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;has_ledger&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;btrim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_analysis&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'{'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What this changes about "accuracy"
&lt;/h2&gt;

&lt;p&gt;I used to think a prediction row was a claim and a result. It is a claim, three to five copies of the claim, a ledger of who edited which copy, and a graded column that follows only one of them. When the copies agree, 75% of the time, none of this matters. When they disagree — one row in four — the number you publish depends on which copy you happened to read, and the difference between the readings is larger than most of the model improvements I have measured this year.&lt;/p&gt;

&lt;p&gt;The discipline is boring and it is the only one that works: name the field in every accuracy claim, grade exactly one of them, and make every rewrite leave a row behind. If a rewrite cannot leave a row, it should not be allowed to happen.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>51 distinct values for 50,046 predictions: the numeric contract behind a probability column</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Thu, 26 Nov 2026 05:06:32 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/51-distinct-values-for-50046-predictions-the-numeric-contract-behind-a-probability-column-4p94</link>
      <guid>https://dev.to/soccerdream_ai/51-distinct-values-for-50046-predictions-the-numeric-contract-behind-a-probability-column-4p94</guid>
      <description>&lt;p&gt;Three columns in the same row of the same table all describe "how much comes back per unit". One holds a number near 0.92, the other two hold numbers near 1.00, and the median distance between them is 8.24 percentage points. Nobody wrote a comment explaining why, so every query that mixes them is silently wrong by that margin.&lt;/p&gt;

&lt;p&gt;That is not a bug story. It is a story about what a numeric column promises: a scale, an alphabet of values, and a range. In &lt;code&gt;ai_predictions&lt;/code&gt; all three were quietly broken in different ways.&lt;/p&gt;

&lt;p&gt;Production context: 135 columns, 50,046 rows (2026-06-21 to 2026-11-26), 49,518 settled, 28,110 correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                        AS rows,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE hit)     AS correct,
       count(DISTINCT model_id)        AS models
FROM   ai_predictions;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; rows   | settled | correct | models
--------+---------+---------+--------
 50046  |  49518  |  28110  |  4019
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promise one: the scale you declared
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fair_home&lt;/code&gt;, &lt;code&gt;fair_draw&lt;/code&gt;, &lt;code&gt;fair_away&lt;/code&gt; are &lt;code&gt;numeric(6,3)&lt;/code&gt; — three decimals. You would assume a third decimal exists because something needs it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                                               AS non_null,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fair_home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fair_home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;needs_third&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;fair_home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                              &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;distinct_home&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;fair_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; non_null | needs_third | distinct_home
----------+-------------+---------------
    49305 |           0 |          1169
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero rows out of 49,305 use the third decimal. The declared scale is one digit wider than the data has needed, on three columns, for five months — harmless at four bytes a row, but a hint that the type was chosen by habit rather than by specification.&lt;/p&gt;

&lt;p&gt;The contrast sits in the same table: &lt;code&gt;ah_v5_p_up&lt;/code&gt; is &lt;code&gt;numeric(8,6)&lt;/code&gt;, 28,211 nulls and 21,835 values spread over 8,414 distinct points. Neither choice is documented.&lt;/p&gt;

&lt;p&gt;The consequence is arithmetic: 1,169 distinct home prices across 49,305 rows means the average price is shared by &lt;strong&gt;42.2 rows&lt;/strong&gt;. Any analysis treating the price triple as an identity is about to be wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise two: the alphabet you will ever hold
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;confidence_calibrated&lt;/code&gt; is &lt;code&gt;numeric(6,4)&lt;/code&gt; and is supposed to be the probability attached to the direction on that row. Here is its actual alphabet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;confidence_calibrated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;distinct_values&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confidence_calibrated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confidence_calibrated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;   &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;confidence_calibrated&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;confidence_calibrated&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS rows
FROM   ai_predictions WHERE confidence_calibrated IS NOT NULL
GROUP  BY 1 ORDER BY 2 DESC LIMIT 4;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; distinct_values |   lo   |   hi
-----------------+--------+--------
              51 | 0.4721 | 0.7295

  value  |  rows
---------+-------
 0.5596  | 12130
 0.5516  |  9696
 0.5990  |  9213
 0.5337  |  8387
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fifty-one distinct values for 50,046 rows, and the top four cover 39,426 rows — 78.78% of the table. This is not a probability estimate; it is a 51-entry lookup table wearing a continuous type.&lt;/p&gt;

&lt;p&gt;The obvious hypothesis is a lookup keyed on &lt;code&gt;(confidence, direction_arity)&lt;/code&gt;. Test it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;confidence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;direction_arity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n,
       count(DISTINCT confidence_calibrated) AS distinct_values
FROM   ai_predictions WHERE confidence_calibrated IS NOT NULL
GROUP  BY 1,2 HAVING count(DISTINCT confidence_calibrated) &amp;gt; 1
ORDER  BY 4 DESC LIMIT 4;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not a function of those two either: the query returns confidence 6 on single picks at the top, 6,983 rows over 32 distinct probabilities. Whatever the key is — model lineage, league, the calibration window at write time — it is not in the two columns a reader would guess, and it is not recorded on the row. A value you cannot reproduce from the row is a cached answer to a question nobody wrote down.&lt;/p&gt;

&lt;p&gt;The alphabet also destroys the ordering the name promises:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;confidence&lt;/th&gt;
&lt;th&gt;graded rows&lt;/th&gt;
&lt;th&gt;mean calibrated&lt;/th&gt;
&lt;th&gt;realized hit rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;0.4724&lt;/td&gt;
&lt;td&gt;46.09%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;8,721&lt;/td&gt;
&lt;td&gt;0.5336&lt;/td&gt;
&lt;td&gt;53.12%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;12,516&lt;/td&gt;
&lt;td&gt;0.5596&lt;/td&gt;
&lt;td&gt;55.74%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;4,400&lt;/td&gt;
&lt;td&gt;0.6007&lt;/td&gt;
&lt;td&gt;60.25%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;2,052&lt;/td&gt;
&lt;td&gt;0.6539&lt;/td&gt;
&lt;td&gt;65.69%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;550&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.7295&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;73.82%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.5693&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;58.62%&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Confidence 9 is the best-calibrated bucket in the table and also the last honest one. Confidence 10 sits 16.02 points below it, on 58 graded rows drawn from 53 different models — roughly one row per model, which is what you would expect if "10" is not a calibrated state but a rare output. Value 0.7295 occurs 550 times, precisely the number of confidence-9 rows, and inside confidence 9 it is the only value present: the mapping is a constant.&lt;/p&gt;

&lt;p&gt;The rank test is mechanical: the maximum probability ever assigned at confidence 9 is 0.7295, and counting how many confidence-10 rows fall below it gives 63 out of 63. Every row at the highest integer confidence is less likely, by the table's own numbers, than the best row at the second-highest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise three: the level, which is not a promise at all
&lt;/h2&gt;

&lt;p&gt;Scale and alphabet are static. The third assumption is that the level tracks reality:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;mean calibrated&lt;/th&gt;
&lt;th&gt;realized rate&lt;/th&gt;
&lt;th&gt;gap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;0.6195&lt;/td&gt;
&lt;td&gt;0.6230&lt;/td&gt;
&lt;td&gt;−0.35pp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;0.6115&lt;/td&gt;
&lt;td&gt;0.5089&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;+10.26pp&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;164&lt;/td&gt;
&lt;td&gt;0.5604&lt;/td&gt;
&lt;td&gt;0.5793&lt;/td&gt;
&lt;td&gt;−1.89pp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,757&lt;/td&gt;
&lt;td&gt;0.5641&lt;/td&gt;
&lt;td&gt;0.5931&lt;/td&gt;
&lt;td&gt;−2.90pp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,886&lt;/td&gt;
&lt;td&gt;0.5636&lt;/td&gt;
&lt;td&gt;0.5615&lt;/td&gt;
&lt;td&gt;+0.21pp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;12,867&lt;/td&gt;
&lt;td&gt;0.5624&lt;/td&gt;
&lt;td&gt;0.5356&lt;/td&gt;
&lt;td&gt;+2.68pp&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Weighted across the table the gap is +0.73pp, which looks excellent and is a lie of aggregation: only June (−0.35pp) and October (+0.21pp) are faithful. July is off by 10.26 points — the column claimed 61.15% and delivered 50.89%.&lt;/p&gt;

&lt;p&gt;Then the level itself moves: between July and August the mean drops 5.11 points, from 0.6115 to 0.5604, on 164 rows. No realized rate explains a step that size — July realized 0.5089, August 0.5793. The column stepped &lt;strong&gt;away&lt;/strong&gt; from July's reality and &lt;strong&gt;toward&lt;/strong&gt; nothing in particular. Since September it has sat between 0.5624 and 0.5641 while realized performance wandered from 0.5356 to 0.5931.&lt;/p&gt;

&lt;p&gt;A constant level with a drifting outcome is the signature of a lookup refitted once and then frozen — fine as an engineering choice, if the column is named for what it is and the refits are dated. Neither is true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two rows where the number is not small, it is absent
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;return_rate&lt;/code&gt; is &lt;code&gt;numeric(5,4)&lt;/code&gt;, max 0.9967, 1,345 distinct values. Two rows hold exactly 0.0000. Both belong to model 1, both at confidence 5, both calibrated to 0.5596; one graded correct, one not. Their stored prices are 2.290 / 3.570 / 3.520, and &lt;code&gt;1 / (1/2.290 + 1/3.570 + 1/3.520)&lt;/code&gt; = &lt;strong&gt;0.9991&lt;/strong&gt;. So arithmetic recovers 0.9991 for a row whose column says 0.0000. This is not precision loss but a sentinel — a handler that wrote zero and moved on — sitting in a field that is later averaged and summed. &lt;code&gt;WHERE return_rate &amp;gt; 0.9&lt;/code&gt; drops both rows without a warning.&lt;/p&gt;

&lt;p&gt;The same table has an integer with a 1-to-10 range contract and exactly one row at 0: &lt;code&gt;SELECT count(*) FROM ai_predictions WHERE confidence NOT BETWEEN 1 AND 10&lt;/code&gt; returns 1. Nothing broke — its calibrated value is 0.5837 and it was graded correct — but the guard that should have caught it does not exist, and one row is how a range contract always starts: never violated wholesale, violated once and then twice.&lt;/p&gt;

&lt;p&gt;Nulls are the same problem, quieter: &lt;code&gt;fair_home&lt;/code&gt; is null on 741 rows (1.48%), and &lt;strong&gt;246 of those have raw opening prices present&lt;/strong&gt; — the input existed, the derived column did not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The triple is not an identity
&lt;/h2&gt;

&lt;p&gt;Group by the full price triple and 49,305 rows collapse to 21,264 groups:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;fair_home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fair_draw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fair_away&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS c
           FROM ai_predictions WHERE fair_home IS NOT NULL GROUP BY 1,2,3)
SELECT count(*)&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE c&amp;gt;1) shared,
       sum(c) FILTER (WHERE c&amp;gt;1) rows_shared, max(c) mx FROM t;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; groups | shared_groups | rows_in_shared | largest
--------+---------------+----------------+---------
  21264 |          8904 |          36945 |     120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;36,945 rows — 74.93% — sit on a triple at least one other row also holds, and the largest group has 120 rows. Check what it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS rows, count(DISTINCT match_id) AS fixtures,
       count(DISTINCT model_id) AS models, min(created_at)::date AS first_write
FROM   ai_predictions
WHERE  fair_home = 1.810 AND fair_draw = 4.070 AND fair_away = 4.970;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; rows | fixtures | models | first_write
------+----------+--------+-------------
  120 |        1 |    120 | 2026-07-06
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One fixture, 120 rows, 120 distinct models, all written the same day. Nothing is duplicated — these are 120 independent analyses of one match against a single price set — but the row-level consequence stands: &lt;code&gt;(fair_home, fair_draw, fair_away)&lt;/code&gt; cannot serve as a dedupe key or a hash input. Deduping on it would collapse 8,904 groups and drop 28,041 rows.&lt;/p&gt;

&lt;p&gt;Only 4,363 of the 21,264 groups span more than one fixture (max 25), so the collision is overwhelmingly &lt;em&gt;within&lt;/em&gt; a fixture, driven by model fan-out rather than price granularity — and the same 74.93% supports both diagnoses.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a contract looks like
&lt;/h2&gt;

&lt;p&gt;None of the fixes needs an index or a rewrite; they need declarations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;confidence_range&lt;/span&gt;  &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confidence&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;VALID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;return_rate_range&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;return_rate&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;return_rate&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;VALID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;fair_positive&lt;/span&gt;     &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fair_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;fair_home&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;VALID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="no"&gt;COMMENT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;confidence_calibrated&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt;
  &lt;span class="s1"&gt;'Lookup, 51 entries, refit 2026-08. Not a per-row estimate.'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;NOT VALID&lt;/code&gt; is the point: it binds every future row while the one bad confidence and two bad return rates stay visible as a review queue instead of being silently backfilled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three habits
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Print the alphabet.&lt;/strong&gt; &lt;code&gt;count(DISTINCT x)&lt;/code&gt; beside &lt;code&gt;count(*)&lt;/code&gt;: 51 versus 50,046 instantly says whether you hold an estimate or a dictionary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never trust a declared scale.&lt;/strong&gt; Check whether the last digit is ever non-zero. Zero out of 49,305 means the type is a wish, not a spec.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate "shares a value" from "is a duplicate".&lt;/strong&gt; The same 74.93% is a fan-out artefact or a data-quality disaster depending on one &lt;code&gt;count(DISTINCT match_id)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The unifying failure is not any single bad value. It is that four columns in one row can each be plausible and mutually inconsistent, because the schema recorded types and never recorded meanings. A numeric column without a unit, an alphabet and a range is not data; it is a number that happens to parse.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>One timestamp column, two calendars: the audit where the same subtraction returned 0 or 8 hours</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Wed, 25 Nov 2026 05:04:30 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/one-timestamp-column-two-calendars-the-audit-where-the-same-subtraction-returned-0-or-8-hours-28i4</link>
      <guid>https://dev.to/soccerdream_ai/one-timestamp-column-two-calendars-the-audit-where-the-same-subtraction-returned-0-or-8-hours-28i4</guid>
      <description>&lt;p&gt;A timestamp column looks like the safest thing in a schema. It is a number, it sorts, it compares. Then one day a dashboard disagrees with a notebook by exactly one day, and you discover that "safe" was never a property of the type — it was a property of a session variable nobody had ever looked at.&lt;/p&gt;

&lt;p&gt;This is an audit of a table that stores the same kickoff time &lt;strong&gt;twice&lt;/strong&gt;, in two different types, and what that redundancy taught me about reproducibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  The table
&lt;/h2&gt;

&lt;p&gt;Production holds 96,384 fixtures in &lt;code&gt;nami_matches&lt;/code&gt;, spanning 2020-09-12 to 2026-11-29 across 1,163 distinct calendar days. Two columns describe the same instant:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;column&lt;/th&gt;
&lt;th&gt;type&lt;/th&gt;
&lt;th&gt;notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;match_time&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;timestamp without time zone&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the one every query uses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;match_time_beijing&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;timestamp with time zone&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;added later, 92,204 rows populated&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The server session default is &lt;code&gt;Asia/Shanghai&lt;/code&gt;. Both facts matter more than they look.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 1: which clock is the naive column on?
&lt;/h2&gt;

&lt;p&gt;A naive timestamp has no offset. It is a photograph of a wall clock with the wall removed. So the only way to know which wall it photographed is to test it against a column that &lt;em&gt;does&lt;/em&gt; carry an offset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE match_time_beijing = (match_time AT TIME ZONE 'Asia/Shanghai')) AS reads_as_beijing,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;match_time_beijing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="no"&gt;AT&lt;/span&gt; &lt;span class="kt"&gt;TIME&lt;/span&gt; &lt;span class="no"&gt;ZONE&lt;/span&gt; &lt;span class="s1"&gt;'UTC'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;reads_as_utc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS rows_with_twin
FROM nami_matches
WHERE match_time_beijing IS NOT NULL;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; reads_as_beijing | reads_as_utc | rows_with_twin
------------------+--------------+----------------
            92204 |            0 |          92204
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not "mostly". Exactly 92,204 of 92,204. The naive column holds Beijing wall-clock time, to the microsecond, for every row that has a twin. The question was settled by data, not by reading the code or the column name. A comment says what someone intended; this says what is stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 2: the same subtraction, two answers
&lt;/h2&gt;

&lt;p&gt;Here is where it stops being academic. Subtract the naive column from the tz-aware one. Same query text, two sessions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- session A&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="kt"&gt;TIME&lt;/span&gt; &lt;span class="no"&gt;ZONE&lt;/span&gt; &lt;span class="s1"&gt;'Asia/Shanghai'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_time_beijing&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;3600.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;offset_hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)
FROM nami_matches WHERE match_time_beijing IS NOT NULL
GROUP BY 1 ORDER BY 2 DESC;

-- session B — identical SQL, different session
SET TIME ZONE 'UTC';
SELECT round(extract(epoch FROM (match_time_beijing - match_time)) / 3600.0, 2) AS offset_hours,
       count(*)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;match_time_beijing&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;session A (Asia/Shanghai)   offset_hours =  0.00   count = 92204
session B (UTC)             offset_hours = -8.00   count = 92204
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both queries are correct. Both return one row covering every populated record. They disagree by eight hours, and neither prints a warning.&lt;/p&gt;

&lt;p&gt;The mechanism: when one operand is &lt;code&gt;timestamptz&lt;/code&gt; and the other is naive, PostgreSQL converts the naive value to &lt;code&gt;timestamptz&lt;/code&gt; &lt;strong&gt;using the session's &lt;code&gt;TimeZone&lt;/code&gt;&lt;/strong&gt; before subtracting. The naive side is an assumption; the session setting decides what the assumption is.&lt;/p&gt;

&lt;p&gt;This is the most dangerous class of timestamp bug in analytics, because it is invisible in review. The SQL is short, it has no joins, it returns a plausible number — and it is non-reproducible across machines, which is the definition of a result you cannot defend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 3: a third of the fixtures change calendar day
&lt;/h2&gt;

&lt;p&gt;If the naive column is Beijing local and the reporting layer buckets by UTC day, the disagreement is a whole day boundary — and it moves a third of the table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE match_time::date &amp;lt;&amp;gt; (match_time_beijing AT TIME ZONE 'UTC')::date) AS shifted,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_time_beijing&lt;/span&gt; &lt;span class="no"&gt;AT&lt;/span&gt; &lt;span class="kt"&gt;TIME&lt;/span&gt; &lt;span class="no"&gt;ZONE&lt;/span&gt; &lt;span class="s1"&gt;'Asia/Shanghai'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;consistent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n
FROM nami_matches
WHERE match_time_beijing IS NOT NULL;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; shifted | consistent |    n
---------+------------+-------
   30932 |          0 | 92204
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;30,932 rows — 33.55% — land on a different calendar date depending on which calendar you apply. Zero are consistent under Beijing, which reconfirms Test 1.&lt;/p&gt;

&lt;p&gt;The distribution explains itself once you look at kickoff hours. Beijing-local 00:00–07:59 is the previous UTC day:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Beijing hour band | rows   | with twin
------------------+--------+----------
00-07             | 33161  |    30932
08-15             | 10351  |    10351
16-23             | 52872  |    50921
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;European evening kickoffs sit at 21:00–23:00 Beijing — the 16–23 band, 52,872 rows. They stay on the same date in UTC. The rows that move are the ones that kick off after local midnight: 30,932 of the 33,161 in the 00–07 band, the remainder being the pre-2026 rows with no twin.&lt;/p&gt;

&lt;p&gt;So "matches per day" is not one number but at least two, and any daily aggregate published without stating its calendar is unfalsifiable — you cannot check it, because you do not know which day it meant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 4: the redundant column is not complete
&lt;/h2&gt;

&lt;p&gt;The obvious fix — "just use the timestamptz column" — collides with coverage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;year | rows  | rows with NULL twin
-----+-------+--------------------
2020 |   310 |                310
2021 |   815 |                815
2022 |   708 |                708
2023 |   823 |                823
2024 |   753 |                753
2025 |   577 |                577
2026 | 92398 |                194
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every row before 2026 has a NULL twin. Coverage is 92,204 / 96,384 = 95.66%, and the missing 4,180 are not scattered noise — they are an entire historical era plus a small tail of recent imports.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;COALESCE(match_time_beijing, match_time)&lt;/code&gt; does not repair the data; it silently mixes two calendars in one column. You get a result that is 95.66% correct, which is worse than 0% correct, because it passes eyeballing.&lt;/p&gt;

&lt;p&gt;Two honest options: backfill the twin from the naive column under the now-proven convention, with a &lt;code&gt;CHECK&lt;/code&gt; that the reconstruction is exact; or keep the naive column as the only source of truth and make the convention explicit in the name and a view. What you must not do is let both exist without proof that they agree — because today they agree, and tomorrow an import path will write one of them from a UTC feed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 5: the same trap inside a single row
&lt;/h2&gt;

&lt;p&gt;The clock problem has a sibling that does not involve time zones at all: picking the wrong column when two columns describe the same event.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ai_predictions&lt;/code&gt; has both &lt;code&gt;created_at&lt;/code&gt; and &lt;code&gt;updated_at&lt;/code&gt;. Asking "was this prediction written before kickoff?" gives two answers that differ by three orders of magnitude:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE p.created_at &amp;gt; m.match_time)                    AS late_by_created,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'1 hour'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;late_over_1h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE p.updated_at &amp;gt; m.match_time)                    AS late_by_updated,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;joined_rows&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; late_by_created | late_over_1h | late_by_updated | joined_rows
-----------------+--------------+-----------------+-------------
              83 |           25 |           49345 |       49865
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;0.17% versus 98.96%. Both are true statements about the table. Only one is a true statement about the &lt;em&gt;pipeline&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;updated_at&lt;/code&gt; is mutated by every downstream write — settlement, re-grading, backfills. It records the last touch, not the act of prediction. Using it to measure "when the model spoke" measures the maintenance job instead. &lt;code&gt;created_at&lt;/code&gt; is write-once, so it is the only column that can answer a provenance question.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;created_at&lt;/code&gt; also shows the honest shape of the workload: 49,928 predictions across 17,442 fixtures from 2026-06-21 to 2026-11-25, spread near-uniformly across all 24 hours (1,111 to 5,220 per hour). No batch fingerprint, no hourly spike — the distribution a continuously-fed queue produces, and the reason 83 late rows is believable rather than an artifact.&lt;/p&gt;

&lt;h2&gt;
  
  
  The guard
&lt;/h2&gt;

&lt;p&gt;Conventions decay. The only thing that survives is an assertion that fails loudly. This runs before any audit query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;audited_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expected_tz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Asia/Shanghai"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;autocommit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SHOW TimeZone"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;expected_tz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"session TimeZone is {actual!r}, audits assume {expected_tz!r}; "&lt;/span&gt;
            &lt;span class="s2"&gt;"naive/timestamptz arithmetic would shift by the offset difference"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# every report must name its calendar explicitly, never inherit the session&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SET TIME ZONE 'UTC'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;assert_twin_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""
        SELECT count(*) FROM nami_matches
        WHERE match_time_beijing IS NOT NULL
          AND match_time_beijing &amp;lt;&amp;gt; (match_time AT TIME ZONE 'Asia/Shanghai')
    """&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;drift&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;drift&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{drift} rows violate the naive-column convention"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;drift&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two rules, both cheap: verify the session before trusting a number, then pin it for the report instead of inheriting whatever the client sent. The second assertion makes the convention fail at 06:00 instead of at month end.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the audit actually produced
&lt;/h2&gt;

&lt;p&gt;Nothing here was a bug in the sense of a stack trace. The table was internally consistent; &lt;code&gt;ended_at − match_time&lt;/code&gt; lands in the 1–4 hour window for 42,298 of 42,300 settled fixtures (99.995%), which is exactly what a single clock would produce. The system was fine. The &lt;em&gt;measurements&lt;/em&gt; were not portable, and portability is the whole point of an audit.&lt;/p&gt;

&lt;p&gt;Five rules I now apply before writing any time-based metric:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prove the clock, do not read it.&lt;/strong&gt; Test the naive column against a tz-aware twin. 92,204/92,204 is a proof; "should be local" is a hope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never mix types in arithmetic.&lt;/strong&gt; Naive minus &lt;code&gt;timestamptz&lt;/code&gt; inherits the session. Cast both sides explicitly, or neither.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Name the calendar in the output.&lt;/strong&gt; "Matches per day" without "Beijing day" or "UTC day" is a rumour, and here the two differ on 33.55% of rows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check coverage before coalescing.&lt;/strong&gt; A 95.66%-populated repair column does not fix anything; it hides the 4,180 rows that have no answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask which column is write-once.&lt;/strong&gt; 0.17% versus 98.96% is not a rounding difference, it is a different question being answered.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The deeper lesson is about reproducibility. A result that depends on a session variable is not a result — it is a coincidence that happened to hold on your machine. If two analysts run the same query and get numbers eight hours apart, the schema let them both be right, and that is a defect in the schema, not in the analysts.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>Five SQL patterns that made my audit lie: 1,613 rows a naive dedupe would have deleted</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Tue, 24 Nov 2026 05:03:56 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/five-sql-patterns-that-made-my-audit-lie-1613-rows-a-naive-dedupe-would-have-deleted-251n</link>
      <guid>https://dev.to/soccerdream_ai/five-sql-patterns-that-made-my-audit-lie-1613-rows-a-naive-dedupe-would-have-deleted-251n</guid>
      <description>&lt;p&gt;An audit query told me my fixture table had 1,006 duplicate matches. The obvious fix — &lt;code&gt;GROUP BY ... HAVING count(*) &amp;gt; 1&lt;/code&gt; plus a delete — would have removed 1,613 rows from a 96,384-row table.&lt;/p&gt;

&lt;p&gt;Then I checked one group by hand. Six rows, same kickoff minute, six different scorelines: &lt;code&gt;3-0&lt;/code&gt;, &lt;code&gt;3-1&lt;/code&gt;, &lt;code&gt;4-1&lt;/code&gt;, &lt;code&gt;0-6&lt;/code&gt;, &lt;code&gt;3-1&lt;/code&gt;, &lt;code&gt;1-2&lt;/code&gt;. Not duplicates. Six different matches sharing a key that collapsed to nothing.&lt;/p&gt;

&lt;p&gt;This is the audit of the audit. Every number comes from a production PostgreSQL database holding 96,384 fixtures and 49,616 stored predictions.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;nami_matches&lt;/code&gt; is the fixture master (96,384 rows, 62 columns, unique &lt;code&gt;nami_id&lt;/code&gt;). &lt;code&gt;ai_predictions&lt;/code&gt; holds one row per model per attempt (49,616 rows, 130 columns) with a boolean &lt;code&gt;hit&lt;/code&gt; for graded outcomes. The question is simple: &lt;em&gt;is one fixture exactly one row?&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- The dedupe detector. Do not run the DELETE it implies.&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;home_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n
FROM nami_matches
GROUP BY home_team_id, away_team_id, match_time
HAVING count(*)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;1,006 groups, 2,619 rows. Summing &lt;code&gt;n - 1&lt;/code&gt; gives 1,613 redundant rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 1: GROUP BY is not a unique key when the key can be NULL
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;GROUP BY&lt;/code&gt; treats all NULLs as one group. Correct SQL, catastrophic semantics: a NULL team id is not the claim "these rows share a team", it is the absence of a claim. PostgreSQL turned 4,392 keyless rows into a handful of gigantic buckets.&lt;/p&gt;

&lt;p&gt;Measured: of the 1,006 groups, 725 involve a NULL team id and 281 have both ids present. Of the 2,619 rows in those groups, 2,593 carry a final score — 99.0%. A duplicate import does not produce 99% final scorelines on both copies. A real schedule does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                                                    AS groups,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nf"&gt;bool_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home_team_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;keyless&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE distinct_scores &amp;gt; 1)                 AS contradictory,
  sum(n)                                                      AS rows_in_groups
FROM (
  SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home_score_regular&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;away_score_regular&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;distinct_scores&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt;
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;home_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt;
  &lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) &amp;gt; 1
) g;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;894 of 1,006 groups contain at least two different scorelines — 1,493 rows inside groups that provably are not duplicates. 104 predictions hang off rows in these groups, 43 of them graded hits. A delete on the strength of &lt;code&gt;HAVING count(*) &amp;gt; 1&lt;/code&gt; destroys the fixtures and the evidence that they were distinct.&lt;/p&gt;

&lt;p&gt;The fix is not a better dedupe. It is refusing to dedupe on a nullable key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="no"&gt;CONCURRENTLY&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;ux_fixture_identity&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;home_team_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it &lt;strong&gt;fails to build&lt;/strong&gt;, on exactly those 281 groups. That failure is the useful outcome: the constraint names 281 rows for review instead of a script deleting 1,613 on a guess. Of those 281, 217 disagree on the scoreline and 64 agree — splitting "broken key" from "candidate duplicate" without deleting anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 2: a sentinel zero is a worse NULL
&lt;/h2&gt;

&lt;p&gt;The system also says "unknown" with &lt;code&gt;0&lt;/code&gt;. Not NULL, not a real id, just a placeholder — and because &lt;code&gt;0 = 0&lt;/code&gt;, it joins and groups like a legitimate value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE home_team_id = 0 OR away_team_id = 0) AS zero_team_rows,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;home_team_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;zero_pairs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE home_team_id = away_team_id)           AS self_matches,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;season_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                         &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;zero_season_rows&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2,632 rows carry a zero team id, 472 on both sides, and 22,417 rows (23.3%) carry &lt;code&gt;season_id = 0&lt;/code&gt;. That last number is the quiet killer: any fixture-to-season join either drops a quarter of the table, or aggregates a quarter of it into one phantom season. Among the 281 duplicate groups with both ids present, 217 disagree on the scoreline — &lt;code&gt;0-0 vs 0-0&lt;/code&gt; groups are not one match seen twice, they are a batch of unmapped fixtures ingested on the same clock. A sentinel is a NULL that lies about being a value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 3: string concatenation as a composite key
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;count(DISTINCT a, b)&lt;/code&gt; does not exist in PostgreSQL — you get &lt;code&gt;function count(integer, integer) does not exist&lt;/code&gt; — so the reflex is to concatenate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home_team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;tuple_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;home_team_id&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'-'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;away_team_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;concat_key&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;89,096 versus 88,943. The concatenated form loses 153 identities, silently. Two mechanisms: &lt;code&gt;NULL || '-' || NULL&lt;/code&gt; is NULL, so all 4,392 keyless rows become one value that &lt;code&gt;count(DISTINCT ...)&lt;/code&gt; then drops from the denominator entirely; and delimiters collide — &lt;code&gt;(1, 12)&lt;/code&gt; and &lt;code&gt;(11, 2)&lt;/code&gt; both become &lt;code&gt;'1-12'&lt;/code&gt;. Real ids here reach five digits, so the collision is latent, not impossible. The tuple form is a row constructor: no delimiter, no NULL merging. Use it, or hash a canonical &lt;code&gt;jsonb&lt;/code&gt; array. Never a string join.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 4: LEFT JOIN plus WHERE is an INNER JOIN
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM matches m
LEFT JOIN nami_matches n ON n.nami_id = m.nami_id;                   -- 87,059

SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nami_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nami_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                              &lt;span class="c1"&gt;-- 83,619&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3,440 rows — 3.95% — vanish. &lt;code&gt;WHERE&lt;/code&gt; on the right table rejects every row the outer join preserved with NULLs, because &lt;code&gt;NULL = 8&lt;/code&gt; is unknown, and unknown never passes a filter. Moving the predicate into &lt;code&gt;ON&lt;/code&gt; restores all 87,059. Its twin is the anti-join: &lt;code&gt;NOT IN&lt;/code&gt; against a subquery containing one NULL returns nothing, forever.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM nami_matches
WHERE competition_id NOT IN (
  SELECT competition_id FROM nami_matches WHERE status_id = 8
);                                                                  -- 30

SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;competition_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;competition_id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                                                  &lt;span class="c1"&gt;-- 47&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 17-row gap is exactly the count of fixtures with a NULL &lt;code&gt;competition_id&lt;/code&gt;. &lt;code&gt;NOT IN&lt;/code&gt; excluded them; &lt;code&gt;NOT EXISTS&lt;/code&gt; kept them. Two defensible answers to a question nobody wrote down precisely — which is the real problem. Pick one, comment it in the query, make it a test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 5: the denominator you did not choose
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                                     AS rows_total,
  count(hit)                                   AS rows_graded,
  sum(hit::int)                                AS hits,
  round(100.0 * sum(hit::int) / count(hit), 4) AS pct_over_graded,
  round(100.0 * sum(hit::int) / count(*)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;pct_over_all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;integer_division&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;49,616 rows total, 49,077 graded, 27,894 hits. Same numerator, two defensible denominators: &lt;strong&gt;56.8372%&lt;/strong&gt; and &lt;strong&gt;56.2198%&lt;/strong&gt;. A 0.62-point spread from choosing &lt;code&gt;count(*)&lt;/code&gt;. Of 49,151 rows flagged settled, 75 have &lt;code&gt;hit IS NULL&lt;/code&gt; and 249 have a NULL secondary grade — "flagged settled but ungraded" and "model was wrong" are different states, and averaging over both mixes them.&lt;/p&gt;

&lt;p&gt;Integer division truncates the same number to &lt;code&gt;56&lt;/code&gt;, losing 0.84 points. I tested it across all 4,000 model ids with at least one graded call: maximum deviation 0.98 points, and zero threshold flips at all eight gates I checked (50, 55, 56, 57, 58, 60, 65, 70). Real, bounded, and here — not guilty. Reporting that honestly matters more than the finding, because the next reader will assume it was the culprit.&lt;/p&gt;

&lt;p&gt;The third drift is fan-out. Joining a one-to-many table multiplies rows invisibly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS join_rows, count(DISTINCT p.match_id) AS distinct_fixtures
FROM ai_predictions p
JOIN nami_matches nm ON nm.id = p.match_id;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;49,553 rows, 17,400 fixtures — 2.85 predictions per fixture. Write &lt;code&gt;count(*)&lt;/code&gt; as "matches audited" and you claim a dataset 2.85× the one you have, with every rate weighted by how many models happened to look at each fixture.&lt;/p&gt;

&lt;h2&gt;
  
  
  The guard: six assertions instead of six comments
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;CHECKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fixture rows"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"SELECT count(*) FROM nami_matches"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;96384&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"identity tuples"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"SELECT count(DISTINCT (home_team_id, away_team_id, match_time)) FROM nami_matches"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;94771&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"graded rows never exceed rows"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"SELECT count(hit) - count(*) FROM ai_predictions"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"key either complete or absent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"SELECT count(*) FROM nami_matches "&lt;/span&gt;
     &lt;span class="s2"&gt;"WHERE (home_team_id IS NULL) &amp;lt;&amp;gt; (away_team_id IS NULL)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"no graded row without a fixture"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"SELECT count(*) FROM ai_predictions p LEFT JOIN nami_matches nm ON nm.id = p.match_id "&lt;/span&gt;
     &lt;span class="s2"&gt;"WHERE p.hit IS NOT NULL AND nm.id IS NULL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"join fan-out in range"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="s2"&gt;"SELECT count(*) FROM ai_predictions p JOIN nami_matches nm ON nm.id = p.match_id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40000&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;60000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;CHECKS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;scalar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;SystemExit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"DENOMINATOR DRIFT: {label} = {value}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it before the audit, not after. Two checks would have blocked the delete outright: the identity-tuple count (94,771, not 96,384) says the table is not keyed uniquely, and the asymmetric-null check says the key is partially absent rather than duplicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the audit concluded
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Claim&lt;/th&gt;
&lt;th&gt;Naive query&lt;/th&gt;
&lt;th&gt;After checking&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Duplicate groups&lt;/td&gt;
&lt;td&gt;1,006&lt;/td&gt;
&lt;td&gt;1,006, of which 725 keyless&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rows a dedupe deletes&lt;/td&gt;
&lt;td&gt;1,613&lt;/td&gt;
&lt;td&gt;0 justified&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Groups with ≥2 scorelines&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;894 (1,493 rows)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Predictions at risk&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;104 (43 graded hits)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Real duplicates&lt;/td&gt;
&lt;td&gt;unknown&lt;/td&gt;
&lt;td&gt;0 proven; 281 colliding keys, 217 self-contradictory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hit rate&lt;/td&gt;
&lt;td&gt;56%&lt;/td&gt;
&lt;td&gt;56.84% / 56.22%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The fixture table was never broken. The query was. It treated absence of a key as a shared value, and inferred duplication from a group count instead of a contradiction in the payload. A group containing &lt;code&gt;3-0&lt;/code&gt; and &lt;code&gt;0-6&lt;/code&gt; at the same kickoff is not a duplicate group. It is a group whose key failed.&lt;/p&gt;

&lt;p&gt;Two rules survived. Dedupe on contradiction, never on count: group by the candidate key, then ask whether the non-key columns disagree — if they do, the key is wrong. And pin every denominator in an assertion before the first &lt;code&gt;GROUP BY&lt;/code&gt;. The number a rate is divided by is a design decision, and design decisions belong in code that fails loudly, not in a query that succeeds quietly.&lt;/p&gt;

&lt;p&gt;The 1,613 rows are still there. They were never duplicates. They were unkeyed, and I had written a query that could not tell the difference.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>Only 19.66% of matches reach the model: auditing a coverage gate decided by source count, not difficulty</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Mon, 23 Nov 2026 05:07:36 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/only-1966-of-matches-reach-the-model-auditing-a-coverage-gate-decided-by-source-count-not-1cpg</link>
      <guid>https://dev.to/soccerdream_ai/only-1966-of-matches-reach-the-model-auditing-a-coverage-gate-decided-by-source-count-not-1cpg</guid>
      <description>&lt;p&gt;Every accuracy number I have shipped carried a hidden caveat: it was computed on the subset of matches the pipeline happened to touch. Nobody reported the subset. The accuracy travelled; the denominator stayed home.&lt;/p&gt;

&lt;p&gt;This post audits that denominator on a production football prediction system. The pool is 86,802 completed matches with an actual result from 2026-06-21 onward. The prediction table holds 49,471 rows across 3,861 models, 48,766 of them graded, at a pooled hit rate of 56.98%. That is the number everyone quotes. The number nobody quotes is 19.66%.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The denominator, stated plainly
&lt;/h2&gt;

&lt;p&gt;Coverage: of the matches that finished with a result, how many have at least one prediction row pointing at them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-06-21'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;home_score_regular&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;predicted&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM pool) AS pool,
       (SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;predicted&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;covered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM pool p JOIN predicted x ON x.match_id = p.id)::numeric
             / (SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;coverage_pct&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; pool  | covered | coverage_pct
-------+---------+--------------
 86802 |   17067 |        19.66
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four out of five finished matches never enter a model. That alone is not a bug — no system predicts everything. The problem is what the 80.34% is made of, because if the skipped matches differ systematically from the covered ones, then 56.98% is a number about a population you did not define.&lt;/p&gt;

&lt;p&gt;The monthly split shows the first systematic difference, and it is not about football at all:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;match month&lt;/th&gt;
&lt;th&gt;pool&lt;/th&gt;
&lt;th&gt;covered&lt;/th&gt;
&lt;th&gt;coverage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;1,601&lt;/td&gt;
&lt;td&gt;39&lt;/td&gt;
&lt;td&gt;2.44%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;11,364&lt;/td&gt;
&lt;td&gt;77&lt;/td&gt;
&lt;td&gt;0.68%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;19,307&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;0.05%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;19,624&lt;/td&gt;
&lt;td&gt;3,043&lt;/td&gt;
&lt;td&gt;15.51%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,414&lt;/td&gt;
&lt;td&gt;9,036&lt;/td&gt;
&lt;td&gt;42.20%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;13,492&lt;/td&gt;
&lt;td&gt;4,862&lt;/td&gt;
&lt;td&gt;36.04%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;August coverage is 0.05% — ten matches out of 19,307. The service was not running that month, and its history sits in the same table as the months when it was. Every pooled statistic over the whole table is a weighted average of a pre-launch and a production period, with weights set by fixture counts. October had both the most fixtures and the highest coverage, so it dominates the headline without anyone deciding that it should.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The gate is a join, not a judgement
&lt;/h2&gt;

&lt;p&gt;To see what separates covered from uncovered matches, the useful question is not "was this match hard?" but "did the pipeline have anything to read?" It reads quoted prices. So cross-tabulate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-06-21'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;home_score_regular&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;predicted&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;priced&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_europe_odds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;covered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;has_quotes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS matches
FROM pool p
LEFT JOIN predicted x ON x.match_id = p.id
LEFT JOIN priced    q ON q.match_id = p.id
GROUP BY 1, 2 ORDER BY 1, 2;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; covered | has_quotes | matches
---------+------------+---------
 f       | f          |   38044
 f       | t          |   31691
 t       | f          |     139
 t       | t          |   16928
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the corners. Of 38,183 matches with no quote anywhere in the price table, 139 were predicted — 0.36%. Of 48,619 with at least one quote, 16,928 were predicted — 34.82%. The gate multiplies your chance of being covered 96×, and has nothing to do with the fixture.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. It is not cherry-picking, and that is the interesting part
&lt;/h2&gt;

&lt;p&gt;The natural suspicion is that the system prefers easy matches. I expected to find that. I did not.&lt;/p&gt;

&lt;p&gt;For each match, take every source's last price in the seven days before kickoff, invert, average, normalise. The maximum of the resulting triple is a difficulty proxy: 0.40 is a near coin-flip, 0.85 a heavy favourite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TEMP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;imp&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;last&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;company_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_draw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_away&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;nami_europe_odds&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'7 days'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_home&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_draw&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eu_away&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;company_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS companies,
       avg(1.0/eu_home) AS rh, avg(1.0/eu_draw) AS rd, avg(1.0/eu_away) AS ra
FROM last GROUP BY match_id;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bucket by &lt;code&gt;greatest(ph, pd, pa)&lt;/code&gt; and measure coverage inside each bucket:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;market max probability&lt;/th&gt;
&lt;th&gt;matches&lt;/th&gt;
&lt;th&gt;covered&lt;/th&gt;
&lt;th&gt;coverage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt; 0.45&lt;/td&gt;
&lt;td&gt;17,469&lt;/td&gt;
&lt;td&gt;5,895&lt;/td&gt;
&lt;td&gt;33.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.45–0.55&lt;/td&gt;
&lt;td&gt;13,576&lt;/td&gt;
&lt;td&gt;4,822&lt;/td&gt;
&lt;td&gt;35.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.55–0.65&lt;/td&gt;
&lt;td&gt;8,694&lt;/td&gt;
&lt;td&gt;3,175&lt;/td&gt;
&lt;td&gt;36.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.65–0.75&lt;/td&gt;
&lt;td&gt;4,800&lt;/td&gt;
&lt;td&gt;1,736&lt;/td&gt;
&lt;td&gt;36.2%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;≥ 0.75&lt;/td&gt;
&lt;td&gt;3,693&lt;/td&gt;
&lt;td&gt;1,280&lt;/td&gt;
&lt;td&gt;34.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Flat. Hardest to easiest spans 2.8 points, and not monotonically — the peak is in the middle. Whatever the gate does, it is not filtering on how predictable the match looks.&lt;/p&gt;

&lt;p&gt;Now bucket by how many sources quoted a price at all:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;quote sources&lt;/th&gt;
&lt;th&gt;matches&lt;/th&gt;
&lt;th&gt;coverage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4,497&lt;/td&gt;
&lt;td&gt;2.4%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2–3&lt;/td&gt;
&lt;td&gt;3,513&lt;/td&gt;
&lt;td&gt;8.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4–8&lt;/td&gt;
&lt;td&gt;6,994&lt;/td&gt;
&lt;td&gt;18.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9–20&lt;/td&gt;
&lt;td&gt;10,664&lt;/td&gt;
&lt;td&gt;28.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20+&lt;/td&gt;
&lt;td&gt;22,564&lt;/td&gt;
&lt;td&gt;54.2%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Monotone, 2.4% to 54.2% — a factor of 22. Splitting both dimensions at once:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;sources&lt;/th&gt;
&lt;th&gt;max prob &amp;lt; 0.55&lt;/th&gt;
&lt;th&gt;max prob ≥ 0.55&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1–3&lt;/td&gt;
&lt;td&gt;4.3% (n=5,013)&lt;/td&gt;
&lt;td&gt;5.8% (n=2,997)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4–8&lt;/td&gt;
&lt;td&gt;17.0% (n=4,343)&lt;/td&gt;
&lt;td&gt;21.7% (n=2,651)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9–20&lt;/td&gt;
&lt;td&gt;27.6% (n=6,693)&lt;/td&gt;
&lt;td&gt;28.6% (n=3,971)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20+&lt;/td&gt;
&lt;td&gt;52.8% (n=14,996)&lt;/td&gt;
&lt;td&gt;56.9% (n=7,568)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Within a source band, going from a contested match to a lopsided one buys 1.03×–1.28× more coverage. Moving across bands buys 3.9×–12.3×. The gate is plumbing: it responds to how many feeds answered, not to whether the match is worth modelling.&lt;/p&gt;

&lt;p&gt;This is good news for the 56.98%: within the priced population, coverage looks near-random with respect to difficulty, so the hit rate is not inflated by an easier subpopulation. It is also the bad news, because a random gate is still a gate. The 31,691 priced-but-uncovered matches are not a sample you can ignore — they are the population you declined to score.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Three hit rates from the same 16,908 matches
&lt;/h2&gt;

&lt;p&gt;Here is where the denominator bites. Within the seven-day price window: 48,232 matches, 16,908 covered.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;aggregation&lt;/th&gt;
&lt;th&gt;value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;rows graded&lt;/td&gt;
&lt;td&gt;48,373&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hits&lt;/td&gt;
&lt;td&gt;27,561&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pooled (row-weighted)&lt;/td&gt;
&lt;td&gt;56.98%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;match-mean (per-match rate, then average)&lt;/td&gt;
&lt;td&gt;57.32%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;match-level "at least one hit"&lt;/td&gt;
&lt;td&gt;64.95% (10,982 / 16,908)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mean prediction rows per covered match&lt;/td&gt;
&lt;td&gt;2.86&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The distribution behind that mean is the part I did not expect: 11,548 covered matches get exactly one prediction row, 1,036 get two, 455 get three, and fifteen carry more than 100 (max 358). That is 68.3% single-row. The tail makes the pooled number look like it rests on a large sample; it is a large sample of rows and a thin sample of &lt;em&gt;matches&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Splitting by row count shows no accuracy premium in that tail: 57.36% for single-row matches, 58.54% for 2–3 rows, 56.68% for 4+. More models per match buys variance reduction and a bigger denominator, not accuracy.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What the gate costs
&lt;/h2&gt;

&lt;p&gt;First, the uncovered priced population has no accuracy estimate. Those 31,691 matches are 65% of the priced pool and skew toward thin feeds — 8,010 carry three or fewer quoted sources, where coverage collapses to 2.4%–8.0%. That region of the input space is effectively unmeasured. A "56.98% accuracy" headline therefore rests heaviest on the 20+ source band: 22,564 matches, 26% of all finished fixtures.&lt;/p&gt;

&lt;p&gt;Second, monthly drift moves more than any model comparison I have run:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;graded rows&lt;/th&gt;
&lt;th&gt;hit rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,645&lt;/td&gt;
&lt;td&gt;63.18%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,723&lt;/td&gt;
&lt;td&gt;50.42%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;td&gt;56.82%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,030&lt;/td&gt;
&lt;td&gt;59.37%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,148&lt;/td&gt;
&lt;td&gt;56.92%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;13,176&lt;/td&gt;
&lt;td&gt;55.56%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A 12.8-point swing between July and June on samples of 2,645 and 2,723 rows. Shift the reporting window by one month and the headline moves further than most model-level differences anyone argues about.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. A coverage-aware reporting contract
&lt;/h2&gt;

&lt;p&gt;The fix is not to raise coverage. It is to make coverage a mandatory field on every accuracy claim. This is the gate I run before any number leaves the building.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
    &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;         &lt;span class="c1"&gt;# finished matches with a result&lt;/span&gt;
    &lt;span class="n"&gt;covered&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;      &lt;span class="c1"&gt;# matches with &amp;gt;= 1 prediction row&lt;/span&gt;
    &lt;span class="n"&gt;min_sources&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="c1"&gt;# quoted-source floor for this slice&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;errs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;cov&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;covered&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cov&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.30&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"coverage {cov:.2%} too thin to quote a point estimate"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;min_sources&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"slice below 9 sources is unmeasured (coverage 2-19%)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errs&lt;/span&gt;

&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Claim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hit_rate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5698&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;86802&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;covered&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;17067&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_sources&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kc"&gt;False&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'coverage 19.66% too thin to quote a point estimate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="s1"&gt;'slice below 9 sources is unmeasured (coverage 2-19%)'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The claim fails its own gate, which is the correct outcome. The defensible version is narrower: 56.98% on 48,373 graded rows from 16,908 matches, restricted to fixtures with a pre-kickoff quote, skewed toward the 9+ source band, blended across six months of unequal pipeline availability. That sentence is worse as marketing and better as a measurement.&lt;/p&gt;

&lt;p&gt;One more structural fact limits every coverage report here: &lt;code&gt;nami_matches.competition_id&lt;/code&gt; holds 1,219 distinct values, but only 3,986 of 86,802 matches (4.59%) join to &lt;code&gt;nami_competitions.id&lt;/code&gt;. Any per-league coverage table rests on 4.59% of the pool, so I do not publish one. Fixing that key space is worth more than another model.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. What I am not claiming
&lt;/h2&gt;

&lt;p&gt;I am not claiming coverage caused the hit rate, or that raising coverage would preserve it. A gate that is random on difficulty means the covered set is not easier, which supports external validity inside the priced population — it says nothing about the 38,183 unpriced matches, where coverage is 0.36% and no estimate exists.&lt;/p&gt;

&lt;p&gt;Nor is the source gradient causal. Source count correlates with league tier, liquidity and feed subscription, none of which I separated. It survives conditioning on market determinism and is 4–12× larger — that is all I can say.&lt;/p&gt;

&lt;p&gt;The one claim I will defend: a hit rate without its coverage, its price-input floor and its window is not a measurement. It is a quotient of two numbers that happened to share a table.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This content is for data analysis and research reference only and does not constitute any decision-making advice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Analytics and research content. Not betting advice. 18+&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>The same workload cost ¥43,712 or ¥45,767: auditing an LLM cost ledger that disagrees with itself</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Sun, 22 Nov 2026 05:11:00 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/the-same-workload-cost-y43712-or-y45767-auditing-an-llm-cost-ledger-that-disagrees-with-itself-53j9</link>
      <guid>https://dev.to/soccerdream_ai/the-same-workload-cost-y43712-or-y45767-auditing-an-llm-cost-ledger-that-disagrees-with-itself-53j9</guid>
      <description>&lt;p&gt;Every team shipping LLM features builds a per-call log, a daily aggregate and a dashboard, at different times and from different assumptions. Then someone asks "what did last month cost" and you get two numbers four percent apart, with nobody able to say which is right.&lt;/p&gt;

&lt;p&gt;This is an audit of that situation: 2.3 million model calls over three months, one line-item table, one rollup table, and a disagreement of ¥2,055.24. The interesting part is not the gap's size but that it decomposes into a deletion and a freeze, while everything else reconciles to the cent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two grains of truth
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ai_call_logs&lt;/code&gt; holds one row per attempt with tokens, &lt;code&gt;cost_yuan&lt;/code&gt; and a status. &lt;code&gt;ai_cost_rollup_daily&lt;/code&gt; holds one row per day, model family and hour with &lt;code&gt;calls&lt;/code&gt;, &lt;code&gt;miss_tok&lt;/code&gt;, &lt;code&gt;hit_tok&lt;/code&gt;, &lt;code&gt;out_tok&lt;/code&gt;, &lt;code&gt;recorded_cost&lt;/code&gt; and &lt;code&gt;refreshed_at&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two grains, one money column each — and the aggregate carries no &lt;code&gt;id&lt;/code&gt;, no reference to the rows it summarised. You can compute both totals, but you cannot ask the rollup &lt;em&gt;which&lt;/em&gt; calls it saw. That omission is why drift survives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare by month, not by grand total
&lt;/h2&gt;

&lt;p&gt;Totals hide compensating errors, so let the line-item table define the window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;date_trunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'month'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) rows_written,
                     sum(cost_yuan) line_cost FROM ai_call_logs GROUP BY 1),
     roll AS (SELECT date_trunc('month', d)::date m, sum(calls) roll_calls,
                     sum(recorded_cost) roll_cost FROM ai_cost_rollup_daily GROUP BY 1)
SELECT COALESCE(l.m, r.m) AS month, rows_written, roll_calls, line_cost, roll_cost,
       round(roll_cost - line_cost, 2) AS delta
FROM line l FULL JOIN roll r USING (m) ORDER BY 1;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; month      | rows_written | roll_calls | line_cost | roll_cost | delta
 2026-08-01 |        29143 |     171354 |   1302.81 |   3360.29 | +2057.48
 2026-09-01 |      1489004 |    1489004 |  18891.37 |  18891.37 |    0.00
 2026-10-01 |       524167 |     524167 |  15985.26 |  15985.26 |    0.00
 2026-11-01 |       254281 |     254237 |   7532.69 |   7530.17 |   -2.52
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;September and October match to the cent on both calls and money. November is short by ¥2.52 and 44 calls because the month is still being written. August is off by ¥2,057.48, and that plus the ¥2.52 of in-flight November equals the grand-total gap: what is wrong with August?&lt;/p&gt;

&lt;h2&gt;
  
  
  The gap is a deletion, not a rounding error
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM ai_call_logs;
-- 429016 | 2776137 | 2296595

SELECT min(d), max(d), count(*)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recorded_cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_cost_rollup_daily&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2026-08-24'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 2026-08-08 | 2026-08-23 | 670 | 140438 | 2015.45&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_call_logs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 2026-08-24 05:02:48&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three facts stack up. The line-item table starts at id &lt;strong&gt;429,016&lt;/strong&gt;, not 1, so roughly 429,000 rows no longer exist. The first surviving call is dated &lt;strong&gt;2026-08-24&lt;/strong&gt;, while the rollup starts &lt;strong&gt;2026-08-08&lt;/strong&gt; — sixteen days earlier. Those orphan days hold &lt;strong&gt;140,438 calls&lt;/strong&gt; and &lt;strong&gt;¥2,015.45&lt;/strong&gt; with no rows behind them.&lt;/p&gt;

&lt;p&gt;That is 98.0% of the gap, and neither table is broken. A retention job prunes detail; a separate job keeps aggregates; nothing forces the two policies to agree. The rollup is now the only witness to two weeks of spend, and it cannot be itemised or re-derived. On 2026-08-24 itself the rollup says 3,405 calls and ¥104.88 while 1,632 rows and ¥62.85 survive — the cut ran into that day too.&lt;/p&gt;

&lt;p&gt;The useful test is not the average discrepancy but whether it is zero on days nobody touched. Re-running both sides per day, 2026-09-20 gives 17,871 rows and ¥563.52 on both, 2026-10-09 gives 13,703 and ¥229.67, 2026-11-10 gives 8,470 and ¥131.99. Identical to the second decimal, including the 10-09/10-10 boundary where the model mix changed. The rollup job is correct — correct &lt;em&gt;at the moment it runs&lt;/em&gt;, which is the second defect.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;refreshed_at&lt;/code&gt; says the aggregate is a snapshot, not a view
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; month      | min        | max        | rows      -- from: GROUP BY date_trunc('month', d)
 2026-08-01 | 2026-10-09 | 2026-10-09 |  976
 2026-09-01 | 2026-10-09 | 2026-10-09 | 1386
 2026-10-01 | 2026-10-09 | 2026-11-03 | 1284
 2026-11-01 | 2026-11-04 | 2026-11-22 |  762
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every August and September bucket was last touched on 2026-10-09 and never again. That is the normal life cycle of a backfill: it runs once and is trusted forever — which makes "the rollup is the source of truth" a claim with an expiry date. A row that arrives late — a retried call, a time-skewed writer, a restored backup — lands in the line-item table and never reaches a bucket frozen five weeks ago.&lt;/p&gt;

&lt;p&gt;The fix is not "refresh more often"; full-history refreshes get more expensive every month and eventually stop. Store &lt;code&gt;source_max_id&lt;/code&gt; per bucket instead, and reconciliation becomes a question the schema can answer: &lt;em&gt;is there any line item newer than the newest one this bucket counted?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;count(*)&lt;/code&gt; is not "calls"
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; status             | rows      | prompt_tokens  | cost
 success            | 1,044,058 | 18,445,937,152 | 43,711.76
 dedup_skip         | 1,149,914 |              0 |     0.00
 error              |    78,313 |     30,939,651 |     0.00
 fail               |    18,341 |              0 |     NULL
 rate_limited       |     4,201 |              0 |     0.00
 empty_after_retry  |     1,763 |              0 |     0.00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;dedup_skip&lt;/code&gt; is a cache decision written into the same log as a call: 50.06% of rows, zero tokens, zero cost. Every dashboard deriving "calls this month" from &lt;code&gt;count(*)&lt;/code&gt; overstates volume &lt;strong&gt;2.20×&lt;/strong&gt; — not because the number is wrong, but because "call" was never defined.&lt;/p&gt;

&lt;p&gt;The three failure statuses total 102,618 rows — 4.47% of real attempts — and carry 30.9 million prompt tokens billed to nobody, since the writer only sets &lt;code&gt;cost_yuan&lt;/code&gt; on success. That is about ¥65 of invisible spend at the ledger's blended rate, but the structural cost is worse: failures cluster in the same scenes as successes, so a reliability report built on &lt;code&gt;count(*)&lt;/code&gt; never sees them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two columns that lie about their units
&lt;/h2&gt;

&lt;p&gt;Of 1,044,063 successful calls, &lt;strong&gt;61,609 have &lt;code&gt;NULL&lt;/code&gt; cost&lt;/strong&gt; and another 8,654 are explicitly zero — 98.2 million tokens the finance column never sees. Divide by &lt;code&gt;count(*)&lt;/code&gt; and you average in 70,263 free calls, understating by about 6%; divide by &lt;code&gt;count(cost)&lt;/code&gt; and you exclude a sixteenth of the traffic.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cache_hit&lt;/code&gt; is worse, because it is named like a flag and typed &lt;code&gt;integer&lt;/code&gt; while it holds cached tokens. Among successful calls, 18,800 rows have &lt;code&gt;cache_hit &amp;gt; 0&lt;/code&gt; — &lt;strong&gt;1.80% of rows&lt;/strong&gt; — but those tokens are &lt;strong&gt;2.38% of all prompt tokens&lt;/strong&gt; (439,479,898 of 18,445,990,674). One column, two true percentages, 25% apart: a caching strategy judged on the first looks weaker than it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Average cost per call is a mixture, not a statistic
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; scene                  | calls   | cost      | per_call | tok_per_call
 prediction_other       | 180,701 | 19,787.84 |   0.1095 |       63,999
 prediction_top5_close  | 136,399 |  5,994.73 |   0.0439 |       20,352
 prediction_top10_mid   |  23,878 |  3,095.58 |   0.1296 |       38,521
 retrospective          |  78,843 |  3,040.82 |   0.0386 |        3,790
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unit cost spans ¥0.0066 to ¥0.1296 across scenes, a 19.6× range driven by context length (3,790 to 63,999 tokens per call). The global mean is ¥0.0419 against a median of ¥0.0165, so "average cost per call" is a statement about traffic mix, not about the system: change the batch schedule and the headline moves while nothing else does. The honest unit is cost per delivered artifact, with the scene breakdown beside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  46% of the spend has no fixture attached
&lt;/h2&gt;

&lt;p&gt;Of 1,044,059 successful calls in the window, 564,221 carry a &lt;code&gt;match_id&lt;/code&gt; and &lt;strong&gt;479,838 do not&lt;/strong&gt; — 45.96% of traffic holding &lt;strong&gt;¥9,668.76, or 22.1% of the money&lt;/strong&gt;. Some is legitimate, but the column cannot distinguish "not about a match" from "forgot to propagate the id". Without a taxonomy of fixture-scoped scenes, cost per prediction divides by a denominator that mixes product work with overhead.&lt;/p&gt;

&lt;p&gt;The same hazard applies to the model name. &lt;code&gt;GROUP BY model_name&lt;/code&gt; splits one model into 689,209 and 32,804 calls, because it is written both &lt;code&gt;deepseek-v4.1-flash&lt;/code&gt; and &lt;code&gt;DeepSeek-V4.1-flash&lt;/code&gt;; the &lt;code&gt;-1017&lt;/code&gt; variant has the same pair. A budget alert on the larger spelling misses 4.5% of that model's traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  A reconciler that fails loudly
&lt;/h2&gt;

&lt;p&gt;The one artifact worth keeping is the assertion, not the report: run it daily, exit non-zero, and the gap never accumulates for three months.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="no"&gt;SQL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""
WITH line AS (
  SELECT created_at::date AS d, count(*) FILTER (WHERE status='success') AS calls,
         coalesce(sum(cost_yuan),0) AS cost
  FROM ai_call_logs WHERE created_at &amp;gt;= %(since)s GROUP BY 1
), roll AS (
  SELECT d, sum(calls) AS calls, sum(recorded_cost) AS cost
  FROM ai_cost_rollup_daily WHERE d &amp;gt;= %(since)s GROUP BY 1
)
SELECT line.d, line.calls, roll.calls, line.cost, roll.cost
FROM line JOIN roll USING (d)
WHERE abs(line.cost - roll.cost) &amp;gt; 0.01 OR line.calls &amp;lt;&amp;gt; roll.calls;"""&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reconcile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"since"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DRIFT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# daily grain, one cent, no tolerance&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details make it work where a dashboard does not. It compares &lt;strong&gt;per day&lt;/strong&gt;, so a cancellation inside a month cannot hide a gap. It tolerates one cent rather than one percent: a 1% tolerance would have passed a ¥18,891 month wrong by ¥180. And it counts a missing bucket as drift rather than zero — absence looks identical to "no traffic" until you assert on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would change in the schema
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Never delete line items; archive them.&lt;/strong&gt; Detach a monthly partition instead. The 429,000 missing ids are unrecoverable evidence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;cost_yuan NOT NULL&lt;/code&gt; on success&lt;/strong&gt;, plus &lt;code&gt;pricing_version&lt;/code&gt;. An unpriced call is an incident, not a row state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Declare the grain in the name.&lt;/strong&gt; &lt;code&gt;ai_cost_rollup_hourly_by_family&lt;/code&gt; beats &lt;code&gt;ai_cost_rollup_daily&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every aggregate row records &lt;code&gt;source_max_id&lt;/code&gt;&lt;/strong&gt;, so reconciliation is a comparison instead of a hope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One semantic per column.&lt;/strong&gt; &lt;code&gt;status&lt;/code&gt; encodes outcome, cache decision and retry exhaustion at once; &lt;code&gt;calls&lt;/code&gt; counts all of them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The gap was ¥2,055.24 — 4.7% of three months of spend — invisible not because the data was wrong but because nothing compared the two tables. Both were correct. One had simply been edited by a retention policy the other never heard about.&lt;/p&gt;

&lt;p&gt;If you take one thing: &lt;strong&gt;an aggregate without a back-reference is a rumour with a decimal point.&lt;/strong&gt; The moment you can ask a rollup which rows it saw, cost reconciliation stops being archaeology and becomes a one-line assertion.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>Your table grew 44 columns after row one: auditing schema drift before it rewrites your backtest</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Sat, 21 Nov 2026 05:06:16 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/your-table-grew-44-columns-after-row-one-auditing-schema-drift-before-it-rewrites-your-backtest-12l1</link>
      <guid>https://dev.to/soccerdream_ai/your-table-grew-44-columns-after-row-one-auditing-schema-drift-before-it-rewrites-your-backtest-12l1</guid>
      <description>&lt;p&gt;A prediction log looks like a stable object. You query it, you get numbers, the numbers move when the model changes. That is the story you tell yourself.&lt;/p&gt;

&lt;p&gt;Here is the same table, read honestly. &lt;code&gt;ai_predictions&lt;/code&gt; holds 48,737 rows written between 2026-06-21 and 2026-11-21, across 130 columns. Eight of those columns have never held a single value. Forty-four were born after the first row was inserted. Eleven stopped being written before this month began.&lt;/p&gt;

&lt;p&gt;Every one of those facts changes what a &lt;code&gt;WHERE&lt;/code&gt; clause means, and none appear in the query you wrote six weeks ago.&lt;/p&gt;

&lt;p&gt;All counts are from production, not an example schema.&lt;/p&gt;

&lt;h2&gt;
  
  
  The inventory query
&lt;/h2&gt;

&lt;p&gt;Postgres knows which columns exist. It does not know when each one started carrying data, or whether it still does. That is one pass over the catalog plus one aggregate per column, and you cannot write it as a single static &lt;code&gt;SELECT&lt;/code&gt; because a column name cannot be a parameter. Build one statement per column: on 48k rows the sweep takes about 90 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="no"&gt;DSN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://user:pass@127.0.0.1:5433/analytics"&lt;/span&gt;
&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DSN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""
  SELECT column_name FROM information_schema.columns
  WHERE table_name = 'ai_predictions' ORDER BY ordinal_position
"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;cols&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT min(created_at)::date FROM ai_predictions"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;birth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;col&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"""
      SELECT min(created_at)::date, max(created_at)::date, count({col})
      FROM ai_predictions WHERE {col} IS NOT NULL
    """&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"col"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;col&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;never&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"col"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;late&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birth&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;dead&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-01"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"columns={len(cols)} never_written={len(never)} "&lt;/span&gt;
      &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"born_late={len(late)} stopped={len(dead)}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"never_written:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;never&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output, verbatim from the run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;columns=130 never_written=8 born_late=44 stopped=11
never_written: ['user_id', 'iron_rule_results', 'cache_expires_at',
                'gate_want_visible', 'eu_band', 'eu_band_hit_baseline',
                'direction_gate', 'gate_reason']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eight dead columns out of 130 is 6.2% of the schema. Four — &lt;code&gt;eu_band&lt;/code&gt;, &lt;code&gt;eu_band_hit_baseline&lt;/code&gt;, &lt;code&gt;direction_gate&lt;/code&gt;, &lt;code&gt;gate_reason&lt;/code&gt; — read like a band-based gating feature that was designed, migrated, and never wired up. They document an intention, not behaviour.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the columns arrived
&lt;/h2&gt;

&lt;p&gt;Grouping all 122 written columns by the month of their first non-null value gives the shape of the project rather than the shape of the data. Seventy-eight were present on day one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;first seen&lt;/th&gt;
&lt;th&gt;new columns&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06-21 (first row)&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rest of 2026-06&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The 12 that landed in late June are typical: &lt;code&gt;goal_line_direction&lt;/code&gt;, &lt;code&gt;training_word_version&lt;/code&gt;, &lt;code&gt;opening_assessment&lt;/code&gt;, &lt;code&gt;dtl_at&lt;/code&gt;. The 13 in August are the second-market block plus the veto experiment. October's two are free-text reasoning fields. Each is a legitimate feature; each also carved a hole. Before the feature existed the column is null, and a null in a column that was not yet born is indistinguishable from a null in a column that was measured and came back empty.&lt;/p&gt;

&lt;p&gt;That is the whole problem in one sentence. &lt;code&gt;NULL&lt;/code&gt; means three different things and Postgres has one symbol for it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;never measured&lt;/strong&gt; — the feature did not exist yet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;not applicable&lt;/strong&gt; — the feature existed, this row had no input&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;measured, no value&lt;/strong&gt; — the pipeline wrote nothing where it should have&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Only the third is a bug. The first is your history. If you cannot tell them apart, you cannot audit anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Presence is not availability
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ah_v5_p_up&lt;/code&gt; is the probability output of a v5 scoring pass. Count non-null rows per month:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;ah_v5_p_up&lt;/code&gt; non-null&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;164&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,757&lt;/td&gt;
&lt;td&gt;43&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,886&lt;/td&gt;
&lt;td&gt;10,073&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;11,558&lt;/td&gt;
&lt;td&gt;10,615&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The column was added in July. It became real in October. Between those two facts, 5,536 rows were written and 1 of them carries the v5 value.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;WHERE ah_v5_p_up IS NOT NULL GROUP BY month&lt;/code&gt; returns a table whose first three rows are empty. Not an error, not a warning. An empty result that looks like "v5 had no disagreements in June", when v5 did not exist in June. Anyone reading the output without the row counts beside it will draw the wrong conclusion, and the conclusion will be self-consistent.&lt;/p&gt;

&lt;p&gt;The fix is not a filter, it is a habit: never publish a monthly series without the denominator next to it, and never let a filter quietly reduce the denominator to zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  Availability is not comparability
&lt;/h2&gt;

&lt;p&gt;The second failure is worse: the column is present and populated the whole time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ah_tier&lt;/code&gt; exists from the first row and holds 46,826 values. It is a small integer severity bucket. Filter &lt;code&gt;ah_tier &amp;gt;= 3&lt;/code&gt; and measure the settlement rate per month:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;scored rows&lt;/th&gt;
&lt;th&gt;rate, all&lt;/th&gt;
&lt;th&gt;rows with &lt;code&gt;ah_tier&amp;gt;=3&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;rate, filtered&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;62.30%&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;50.89%&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;161&lt;/td&gt;
&lt;td&gt;55.90%&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,681&lt;/td&gt;
&lt;td&gt;58.18%&lt;/td&gt;
&lt;td&gt;1,029&lt;/td&gt;
&lt;td&gt;64.53%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,719&lt;/td&gt;
&lt;td&gt;56.27%&lt;/td&gt;
&lt;td&gt;1,866&lt;/td&gt;
&lt;td&gt;58.31%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;10,204&lt;/td&gt;
&lt;td&gt;56.13%&lt;/td&gt;
&lt;td&gt;725&lt;/td&gt;
&lt;td&gt;59.72%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The column is not null in June. The value is simply never 3 or higher. The tier assignment logic that produces 3+ went live in September, so the filter returns zero rows for the first three months and then jumps to a +6.35pp lift.&lt;/p&gt;

&lt;p&gt;Read that as a result and you conclude the tier rule has predictive value. Read it as a schema event and you conclude the tier rule has &lt;strong&gt;no June comparison at all&lt;/strong&gt; — and the +6.35pp is measured against a September baseline that is itself 4.12pp &lt;em&gt;below&lt;/em&gt; June's 62.30%. The filtered September slice, 64.53%, is only 2.23pp above the unfiltered June number it is being quietly compared to. Two things changed at once: the population and the pipeline.&lt;/p&gt;

&lt;p&gt;This is the failure mode I care about most, because no invariant catches it. &lt;code&gt;ah_tier&lt;/code&gt; is non-null, in range, type-correct — and incomparable across the boundary where its generating rule changed. Only the month × filter cross-tab exposes it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backfilled versus forward-only
&lt;/h2&gt;

&lt;p&gt;Not every later column is a hole. Two of them behave completely differently.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;confidence_calibrated&lt;/code&gt; sits at 98–100% fill in every month back to June. That is not plausible for a later addition — the value was recomputed and written back over the whole history. A backfill.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;training_word_version&lt;/code&gt; shows a different signature:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;fill&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;9.3%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;10.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;92.1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;99.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Nine percent is the fingerprint of a pipeline emitting the field incidentally — some code path that happened to serialize the object. The jump to 92% in August is deliberate adoption. Nothing before August can be segmented by training word version, and no backfill is possible: you cannot reconstruct which word a model carried eight weeks ago if nobody wrote it down.&lt;/p&gt;

&lt;p&gt;Both columns pass a naive &lt;code&gt;count(*)&lt;/code&gt; completeness check. Only the monthly breakdown says which history is real.&lt;/p&gt;

&lt;p&gt;That gives a usable classification from two queries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fill roughly constant → &lt;strong&gt;backfilled&lt;/strong&gt;, comparable history&lt;/li&gt;
&lt;li&gt;fill steps from near-zero to high → &lt;strong&gt;forward-only&lt;/strong&gt;, history starts at the step&lt;/li&gt;
&lt;li&gt;fill rises then falls to zero → &lt;strong&gt;abandoned&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The eleven abandoned columns:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;column&lt;/th&gt;
&lt;th&gt;last non-null&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;hit_veto_ss&lt;/code&gt;, &lt;code&gt;hit_veto_tag&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;2026-10-31&lt;/td&gt;
&lt;td&gt;26,731 each&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;hit_1x2_semantic&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2026-10-07&lt;/td&gt;
&lt;td&gt;18,974&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;dict_win_rate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2026-10-19&lt;/td&gt;
&lt;td&gt;394&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;rec_tier&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2026-10-19&lt;/td&gt;
&lt;td&gt;113&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;direction_dtl&lt;/code&gt;, &lt;code&gt;hit_1x2_dtl&lt;/code&gt;, &lt;code&gt;dtl_upper&lt;/code&gt;, &lt;code&gt;dtl_lower&lt;/code&gt;, &lt;code&gt;dtl_at&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;2026-10-27&lt;/td&gt;
&lt;td&gt;340 each&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2026-10-04&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;hit_veto_ss&lt;/code&gt; is the interesting one: 26,731 rows accumulated over two months, then switched off on the last day of October. A dashboard built on it still renders — frozen denominator, flat trend line, forever. Nobody gets an alert because the query does not fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  The guard
&lt;/h2&gt;

&lt;p&gt;The defect isn't that the schema changed. It's that the change was invisible at query time. So make it visible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;assert_available&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;col&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;min_fill&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Refuse to compare across a month where the column is mostly absent."""&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"""
      SELECT count(*) AS n,
             count({col}) AS filled
      FROM {table}
      WHERE date_trunc('month', created_at) = date_trunc('month', %s::timestamp)
    """&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{month}-01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filled&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_fill&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{table}.{col} at {month}: {filled}/{n} = {rate:.1%} "&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt; {min_fill:.0%}. Column not available in this period."&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call it for every month before you plot it. Here, &lt;code&gt;assert_available(cur, 'ai_predictions', 'ah_v5_p_up', '2026-06')&lt;/code&gt; raises on 0/2,735 and the chart never gets built. The failure moves from "a reader interprets a blank as a zero" to "the job crashes at 03:00 naming the column and the month". That is the trade you want.&lt;/p&gt;

&lt;p&gt;A registry makes the same knowledge durable rather than tribal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;schema_drift_registry&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;table_name&lt;/span&gt;   &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;column_name&lt;/span&gt;  &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
                 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'core'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'backfilled'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'forward_only'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'abandoned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'never_used'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;first_seen&lt;/span&gt;   &lt;span class="kt"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_seen&lt;/span&gt;    &lt;span class="kt"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;n_observed&lt;/span&gt;   &lt;span class="kt"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;note&lt;/span&gt;         &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Populate it from the inventory query and review the diff monthly. A column flipping from &lt;code&gt;forward_only&lt;/code&gt; to &lt;code&gt;abandoned&lt;/code&gt; is a change worth a ticket; one flipping to &lt;code&gt;never_used&lt;/code&gt; after six months should be dropped from the schema instead of confusing the next reader.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this table actually taught me
&lt;/h2&gt;

&lt;p&gt;Three numbers, same database, same day: 130 columns declared, 122 ever written; 44 with no history before their birth date; 11 that stopped before this month began.&lt;/p&gt;

&lt;p&gt;None of these are data-quality problems in the sense of wrong values. Every value that exists is correct. The defect is temporal: the shape of the record changed while the analyses kept treating it as one continuous population.&lt;/p&gt;

&lt;p&gt;If you take one thing: &lt;strong&gt;&lt;code&gt;NULL&lt;/code&gt; is not a value, it is a timestamp.&lt;/strong&gt; Before you group by it, ask when that column started, when it stopped, and whether the &lt;code&gt;WHERE&lt;/code&gt; clause you inherited is filtering rows — or filtering history.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>7,472 write bursts and 48,216 rows: what a prediction log can and cannot prove</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Fri, 20 Nov 2026 05:05:39 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/7472-write-bursts-and-48216-rows-what-a-prediction-log-can-and-cannot-prove-ml2</link>
      <guid>https://dev.to/soccerdream_ai/7472-write-bursts-and-48216-rows-what-a-prediction-log-can-and-cannot-prove-ml2</guid>
      <description>&lt;p&gt;Every prediction platform eventually has to answer one question from a customer, a regulator, or its own postmortem: &lt;em&gt;was this row produced by an analysis of this match, or was it replayed?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The model card describes intent, not execution. The only artifact describing execution is the row itself — its write time, its measured duration, its payload. So I treated our own production log as a forensic object.&lt;/p&gt;

&lt;p&gt;The table is &lt;code&gt;ai_predictions&lt;/code&gt;: 48,216 rows, 47,167 graded, 3,853 models, 17,235 matches, 116 distinct days from 2026-06-21 to 2026-11-20. Orientation: 26,806 hits over 47,167 graded rows, 56.83%.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cluster the writes into bursts
&lt;/h2&gt;

&lt;p&gt;A prediction is not an event, it is a member of a batch. To see batches you do not need a job table — you need the gap between consecutive writes. I used 120 seconds: any longer silence starts a new burst.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;over&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'120 seconds'&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="k"&gt;over&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;burst_id&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;distinct&lt;/span&gt; &lt;span class="n"&gt;burst_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;bursts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) as rows,
       max(n) as largest_burst,
       percentile_disc(0.5) within group (order by n) as p50_size
from (select burst_id, count(*)&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bursts | rows  | largest_burst | p50_size
7472   | 48215 | 645           | 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7,472 bursts for 48,215 rows, mean 6.45, median 2. The distribution is bimodal: 2,951 bursts hold exactly one row, while 41 bursts hold more than 100 and together account for 7,540 rows — 15.6% of the log written in 41 windows.&lt;/p&gt;

&lt;p&gt;The largest ran on 2026-10-10, 00:44:10 to 01:24:58: 645 rows, 214 matches, 160 distinct models, 2,448.4 seconds of wall clock. That is already a finding. 160 models produced 645 predictions across 214 matches in 41 minutes — a fan-out, not a per-match loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does a model write the same match twice in one burst?
&lt;/h2&gt;

&lt;p&gt;Yes: of 32,110 (burst, model) pairs, 8,070 contain more than one row; 1,008 contain five or more; the maximum is 50 rows from one model inside one contiguous burst. A model emitting 50 rows in one window is iterating a slate.&lt;/p&gt;

&lt;h2&gt;
  
  
  The arithmetic of concurrency
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;analysis_duration_ms&lt;/code&gt; over 29,991 non-null rows: p5 = 4,465 ms, p50 = 58,730 ms, p95 = 182,816 ms, max 1,569,409 ms. Median ~58.7 seconds per prediction.&lt;/p&gt;

&lt;p&gt;Now compare with the burst above: 645 rows in 2,448 seconds, i.e. 3.8 seconds of wall clock per row. You cannot spend 58.7 seconds of compute per row and emit one row every 3.8 seconds unless rows are produced concurrently. The ratio per burst is the implied parallelism:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;analysis_duration_ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;lag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;over&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;analysis_duration_ms&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;gap&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'120 seconds'&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="k"&gt;over&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;burst_id&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;burst_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;analysis_duration_ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1000.0&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;compute_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;epoch&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;span_s&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;having&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) &amp;gt;= 5
)
select count(*)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;bursts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;percentile_disc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;compute_s&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;nullif&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;span_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;p50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;percentile_disc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;within&lt;/span&gt; &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;compute_s&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;nullif&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;span_s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;p90&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;span_s&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bursts | p50  | p90
1477   | 3.77 | 10.54
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Across 1,477 bursts of at least five instrumented rows, the median burst packs 3.77 seconds of measured compute into every second of wall clock; p90 is 10.54. For the 645-row burst, only 277 rows carry a duration and those 277 alone sum to 16,520.6 seconds inside a 2,448-second window — a lower bound of 6.7x. Extrapolating the measured mean of 59.6 s/row to all 645 gives ~38,400 seconds, about 15.7x.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First honest conclusion:&lt;/strong&gt; the log proves concurrency. It cannot prove &lt;em&gt;which worker&lt;/em&gt; produced which row, because there is no &lt;code&gt;run_id&lt;/code&gt; and no &lt;code&gt;worker_id&lt;/code&gt;. Two rows 400 ms apart from two processes are indistinguishable from two threads of one process, and both are indistinguishable from a replay that copies a stored analysis. Concurrency is measurable; provenance is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The missing 37.8%
&lt;/h2&gt;

&lt;p&gt;18,225 rows (37.8%) have &lt;code&gt;analysis_duration_ms IS NULL&lt;/code&gt;, and not at random:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;month&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;null duration&lt;/th&gt;
&lt;th&gt;share&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2026-06&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;2,735&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-07&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;2,637&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-08&lt;/td&gt;
&lt;td&gt;164&lt;/td&gt;
&lt;td&gt;164&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-09&lt;/td&gt;
&lt;td&gt;9,757&lt;/td&gt;
&lt;td&gt;5,563&lt;/td&gt;
&lt;td&gt;57.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-10&lt;/td&gt;
&lt;td&gt;21,886&lt;/td&gt;
&lt;td&gt;4,317&lt;/td&gt;
&lt;td&gt;19.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2026-11&lt;/td&gt;
&lt;td&gt;11,037&lt;/td&gt;
&lt;td&gt;2,809&lt;/td&gt;
&lt;td&gt;25.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Instrumentation arrived in September, stabilised in October, then drifted back to 25.5% in November. Any cost-per-prediction, latency SLO, or "how long does the model think" figure computed on non-null rows is a &lt;strong&gt;sample stratified by month&lt;/strong&gt;, and months are not exchangeable: model composition, match volume and prompt length all moved. The correct denominator is 48,216 and the correct summary is "63.2% of rows have a measured duration".&lt;/p&gt;

&lt;h2&gt;
  
  
  Latency carries no quality signal
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;duration bucket&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;graded&lt;/th&gt;
&lt;th&gt;hit rate&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt; 10 s&lt;/td&gt;
&lt;td&gt;5,440&lt;/td&gt;
&lt;td&gt;5,371&lt;/td&gt;
&lt;td&gt;58.09%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10–60 s&lt;/td&gt;
&lt;td&gt;9,666&lt;/td&gt;
&lt;td&gt;9,391&lt;/td&gt;
&lt;td&gt;57.56%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1–3 min&lt;/td&gt;
&lt;td&gt;13,313&lt;/td&gt;
&lt;td&gt;13,081&lt;/td&gt;
&lt;td&gt;56.87%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt; 3 min&lt;/td&gt;
&lt;td&gt;1,572&lt;/td&gt;
&lt;td&gt;1,562&lt;/td&gt;
&lt;td&gt;57.17%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;18,225&lt;/td&gt;
&lt;td&gt;17,638&lt;/td&gt;
&lt;td&gt;56.41%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Full spread 1.68 points across buckets differing by an order of magnitude in size, and the ordering is not monotonic. The 362 rows finishing under one second sit in the same band as rows taking four minutes.&lt;/p&gt;

&lt;p&gt;I report this as a negative result because it constrains the fix. If longer analysis produced better predictions, the response to a 15.7x concurrency finding would be "add capacity". Since it does not, the response is "record what happened, then decide".&lt;/p&gt;

&lt;h2&gt;
  
  
  Identical payloads across different matches
&lt;/h2&gt;

&lt;p&gt;Hashing &lt;code&gt;full_analysis&lt;/code&gt; is the cheapest provenance check available without a schema change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) as dup_hashes, sum(n) as rows_in_dup_groups,
       sum(case when dm &amp;gt; 1 then n else 0 end) as rows_spanning_multiple_matches,
       max(n) as largest_group, max(dm) as matches_in_largest_group
from (
  select md5(full_analysis) h, count(*)&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;distinct&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;dm&lt;/span&gt;
  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
  &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;full_analysis&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_analysis&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
  &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;having&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) &amp;gt; 1
) g;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dup_hashes | rows_in_dup_groups | rows_spanning_multiple_matches | largest_group | matches_in_largest_group
736        | 3780               | 3361                           | 123           | 77
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;736 payloads repeat, covering 3,780 rows, and 3,361 of those rows sit in groups spanning more than one match. The largest single text appears 123 times across 77 matches. It is not prose — it is a 240-character JSON stub beginning &lt;code&gt;{"prediction": {"direction": "home", "confidence": 6, "score_prediction": "2-1"}&lt;/code&gt;, a fixed default stamped onto whatever match was in the loop when real analysis failed.&lt;/p&gt;

&lt;p&gt;That is the finding a schema cannot catch alone, and why payload hashing belongs in the audit. 7,539 rows carry 100–500 characters; 40,510 carry more than 500. Length will not separate a stub from an analysis, but a hash will.&lt;/p&gt;

&lt;h2&gt;
  
  
  The column that exists and records nothing
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;cache_expires_at&lt;/code&gt; is declared on the table. Populated: 0 of 48,216. A column that is always null is worse than an absent column, because it invites analysis that silently returns zero: anyone auditing cache reuse against it concludes "no caching occurred" — true, or merely unrecorded. Same rule for &lt;code&gt;prediction_source&lt;/code&gt;: free-text (&lt;code&gt;match_center&lt;/code&gt; 30,062, &lt;code&gt;system&lt;/code&gt; 11,458, &lt;code&gt;scheduled&lt;/code&gt; 5,343, &lt;code&gt;match_center+ahfill&lt;/code&gt; 986, then 16 one-off values). Free-text provenance is not provenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the schema should have recorded
&lt;/h2&gt;

&lt;p&gt;Five columns would have made every question above answerable without forensics:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;alter&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;
  &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="n"&gt;run_id&lt;/span&gt;             &lt;span class="kt"&gt;uuid&lt;/span&gt;    &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- one value per batch invocation&lt;/span&gt;
  &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="n"&gt;worker_id&lt;/span&gt;          &lt;span class="kt"&gt;text&lt;/span&gt;    &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- process that wrote the row&lt;/span&gt;
  &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="n"&gt;input_snapshot_id&lt;/span&gt;  &lt;span class="kt"&gt;text&lt;/span&gt;    &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- hash of the frozen input bundle&lt;/span&gt;
  &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="n"&gt;contract_version&lt;/span&gt;   &lt;span class="kt"&gt;text&lt;/span&gt;    &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- prompt + training-word version&lt;/span&gt;
  &lt;span class="k"&gt;add&lt;/span&gt; &lt;span class="k"&gt;column&lt;/span&gt; &lt;span class="n"&gt;duration_ms&lt;/span&gt;        &lt;span class="kt"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;unique&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="n"&gt;ai_pred_run_model_match&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="n"&gt;ai_pred_input_snapshot&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_snapshot_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Did this row come from this match's data" then becomes a join instead of an argument: group by &lt;code&gt;run_id&lt;/code&gt;, compare &lt;code&gt;sum(duration_ms)&lt;/code&gt; to the run's wall clock, assert &lt;code&gt;input_snapshot_id&lt;/code&gt; is unique per (run, match).&lt;/p&gt;

&lt;h2&gt;
  
  
  A verifier you can run before adding anything
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="no"&gt;DSN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://user:pass@127.0.0.1:5433/soccerdream"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DSN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""select count(*), count(*) filter (where analysis_duration_ms is null),
                          count(distinct md5(full_analysis)) from ai_predictions"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nulls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;distinct_payloads&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"rows={rows} null_duration={nulls} ({100.0*nulls/rows:.1f}%) "&lt;/span&gt;
          &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"distinct_payloads={distinct_payloads}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""select count(*) from (
          select md5(full_analysis) h from ai_predictions
          where full_analysis is not null and length(full_analysis) &amp;gt; 100
          group by 1 having count(distinct match_id) &amp;gt; 1) g"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cross&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"payloads_reused_across_matches={cross}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;nulls&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"duration coverage regressed"&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;cross&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"identical analysis text spans multiple matches"&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"invariants ok"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run against today's table it fails both assertions — 37.8% nulls, 736 cross-match payloads. The failure is the baseline; the useful question is whether tomorrow's number is smaller.&lt;/p&gt;

&lt;h2&gt;
  
  
  The general lesson
&lt;/h2&gt;

&lt;p&gt;A prediction log does not document what the model thought. It documents what the process did, and only to the depth the schema chose to record. Ours records time and duration — enough to establish fan-out batches, heavy concurrency, 50 rows from one model in one window, and 3,361 payloads shared across matches. It does not record run, worker, or input identity, which makes the difference between "analysed" and "replayed" unprovable for 100% of rows.&lt;/p&gt;

&lt;p&gt;That is a schema gap, not a modelling failure. The cheapest audit this week is the burst reconstruction above: it needs nothing but &lt;code&gt;created_at&lt;/code&gt;, and it tells you within an hour whether your pipeline is per-match or per-batch — and whether anyone could prove it either way.&lt;/p&gt;

&lt;p&gt;Analytics and research content. Not betting advice. 18+&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>Your leakage audit is reading the wrong clock: 46.6% vs 0.42% on the same 14,781 rows</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Thu, 19 Nov 2026 05:06:04 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/your-leakage-audit-is-reading-the-wrong-clock-466-vs-042-on-the-same-14781-rows-3l57</link>
      <guid>https://dev.to/soccerdream_ai/your-leakage-audit-is-reading-the-wrong-clock-466-vs-042-on-the-same-14781-rows-3l57</guid>
      <description>&lt;p&gt;Ask a team whether their model has seen the future and you will get a boolean. We have one too: a &lt;code&gt;learned_at&lt;/code&gt; column that records the moment a prediction was fed back into the training loop. If &lt;code&gt;learned_at&lt;/code&gt; is later than the match result, the model learned after the fact. If it is earlier, the model cheated.&lt;/p&gt;

&lt;p&gt;That is the whole audit as usually written. It is also wrong, and not by a small margin.&lt;/p&gt;

&lt;p&gt;Run it against our production table and &lt;strong&gt;46.6% of the learned rows appear to have learned before the result existed.&lt;/strong&gt; Anchor the same rows on kickoff time instead, and the number collapses to &lt;strong&gt;0.42%&lt;/strong&gt;. Same table, same column, same rows — and only one anchor measures leakage rather than measuring your own pipeline's lateness.&lt;/p&gt;

&lt;p&gt;Scope: 47,357 predictions, 47,138 graded, 3,807 models, 17,017 matches, 2026-06-21 to 2026-11-19 — &lt;code&gt;ai_predictions&lt;/code&gt; and &lt;code&gt;nami_matches&lt;/code&gt;, reproducible with the SQL shown.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 0: trust the timestamp granularity before the timestamp
&lt;/h2&gt;

&lt;p&gt;An audit of time is an audit of a column, and a column promises less than you think. Check whether a timestamp was &lt;em&gt;observed&lt;/em&gt; or &lt;em&gt;bucketed&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                                                                    AS rows,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;sec_zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE extract(minute FROM created_at) = 0
                     AND extract(second FROM created_at) = 0)                  AS top_of_hour,
  count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
                     &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;sec_zero_minute_not&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rows 47357 | sec_zero 1859 | top_of_hour 1859 | sec_zero_minute_not 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the last two columns together, because that pair is the tell. 1,859 rows have a second component of exactly zero, and &lt;strong&gt;not one of them has a non-zero minute&lt;/strong&gt;. Organic writes would land a zero second on a random minute every sixtieth record — roughly 31 rows. We have zero. A real clock does not produce that distribution; &lt;code&gt;date_trunc&lt;/code&gt; or a hand-written literal does.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;created_at&lt;/code&gt; holds two populations: 45,498 genuine sub-second writes and 1,859 rows whose timestamp is a bucket. Where they live matters more than that they exist — grouping those rows by month:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026-06  1859 / 2735     2026-07  0 / 2637     2026-08  0 / 164
2026-09     0 / 9757     2026-10  0 / 21886    2026-11  0 / 10178
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every bucketed row is in the first month — 68% of June, then nothing, ever again. That is an import batch from launch week, not a live defect. What is not fine is any time-series claim spanning June and July: it compares a bucket against a clock, June resolving to the hour and July to the millisecond.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;learned_at&lt;/code&gt; passes the same test: 14,781 rows, zero on the hour. One column partially synthetic, the other fully observed — indistinguishable without it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: assert the invariant that must never break
&lt;/h2&gt;

&lt;p&gt;Before hunting leakage, prove the loop is at least ordered. A learning write cannot precede the prediction it learned from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE learned_at &amp;lt; created_at) AS learned_before_written,
       count(*)&lt;/span&gt;                                        &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;learned_rows&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;learned_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;learned_before_written 0 | learned_rows 14781&lt;/code&gt; — zero violations. That eliminates the most boring bug: a backfill stamping &lt;code&gt;learned_at&lt;/code&gt; from a queue's creation time. The offset itself says the loop is a batch job, not a trigger — &lt;code&gt;learned_at - created_at&lt;/code&gt; runs p5 449 min, &lt;strong&gt;p50 4,095 min (2.8 d)&lt;/strong&gt;, p95 55,834 min. A design cost, not leakage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: the anchor choice, where the audit actually fails
&lt;/h2&gt;

&lt;p&gt;Now the comparison everyone writes. Result availability is &lt;code&gt;settled_at&lt;/code&gt;; leakage is &lt;code&gt;learned_at &amp;lt; settled_at&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE learned_at &amp;lt; settled_at) AS learned_before_settled,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;learned_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;settled_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)                                          AS n
FROM ai_predictions
WHERE learned_at IS NOT NULL AND settled_at IS NOT NULL;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;learned_before_settled 6,889 (46.62%) | after 7,887 (53.38%) | n 14,776
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;46.6% of the loop appears to read the answer sheet before it exists — an incident, if you trust the anchor.&lt;/p&gt;

&lt;p&gt;Anchor on the match instead of on your own bookkeeping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE p.learned_at &amp;lt; m.match_time)                      AS before_kickoff,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;learned_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt;
                          &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;learned_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;interval&lt;/span&gt; &lt;span class="s1"&gt;'3 hour'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;in_play&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE p.learned_at &amp;gt;= m.match_time + interval '3 hour') AS after,
       count(*)&lt;/span&gt;                                                                 &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;learned_at&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before_kickoff 62 (0.42%) | in_play 2,280 (15.43%) | after 12,439 (84.15%) | n 14,781
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;62 rows. &lt;strong&gt;0.42%.&lt;/strong&gt; The discrepancy is not noise; it is 6,827 rows, and every one is a statement about &lt;code&gt;settled_at&lt;/code&gt;, not about learning.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;settled_at&lt;/code&gt; is when &lt;em&gt;we&lt;/em&gt; wrote the grade; grading is async and retried. It drifts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;settled_at - match_time (n=14,776)
  &amp;lt;0           208   1.41%     &amp;lt;- graded before the whistle
  0..6h      7,245  49.03%     &amp;lt;- live path
  6h..7d       948   6.42%
  7d..60d    1,279   8.66%
 60d..90d    5,096  34.49%     &amp;lt;- repair batch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cleanly bimodal: half the grades land within six hours of the whistle, then a second mass at 60–90 days — 5,096 rows, a third of the learned population, graded two to three months late by a repair job.&lt;/p&gt;

&lt;p&gt;Compare accuracy under both anchors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;anchor = settled_at   learn_before_settle 6,889 rows → 63.78%
                      learn_after_settle  7,887 rows → 51.76%
anchor = match_time   before_kickoff         62 rows → 52.46%
                      in_play             2,280 rows → 57.68%
                      after              12,439 rows → 57.33%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The settled-anchored split shows a 12-point accuracy advantage for the "suspicious" group — a smoking gun, if you want one. The kickoff-anchored split shows a 5-point spread across groups of wildly different size, which reads like what it is — nothing.&lt;/p&gt;

&lt;p&gt;The 12-point gap is an artefact of &lt;em&gt;when grading happened&lt;/em&gt;, not of &lt;em&gt;what the model knew&lt;/em&gt;: the 84-day repair batch is dominated by older matches the live path had already abandoned. Sorting on &lt;code&gt;settled_at&lt;/code&gt; sorts on operational history — a confound wearing the costume of a finding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: the 62 rows that are real, and their fingerprint
&lt;/h2&gt;

&lt;p&gt;0.42% is small but not zero; small numbers deserve rows, not percentages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;models 57 | matches 18 | lead min 435 min (7.25 h), p50 32,480 min (22.6 d), max 112,490 min (78.1 d)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;61 of the 62 belong to matches already played; one to a fixture still ahead. Twenty-two days early on average, 78 at the extreme, over 57 models but only 18 matches — a batch touching a few fixtures many times, not a systemic lookahead.&lt;/p&gt;

&lt;p&gt;The lead times expose the mechanism: several rows share the exact value 47,850 minutes. Identical offsets to the minute across different models mean one loop computed an offset and applied it to a set. Those records were not learned early, they were &lt;strong&gt;stamped&lt;/strong&gt; from a template.&lt;/p&gt;

&lt;p&gt;Bug or mislabelled backfill, the conclusion is not what the boolean suggests: these 62 rows do not corrupt the other 14,719. They contaminate 18 matches — and nothing at all if the learning write refused to run for a fixture that has not kicked off.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: make the check executable
&lt;/h2&gt;

&lt;p&gt;Rules survive only if they run on every deploy. This audit exits non-zero on a broken invariant — CI or scheduler, no dashboard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="no"&gt;CHECKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"learned_before_created"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"""SELECT count(*) FROM ai_predictions
        WHERE learned_at IS NOT NULL AND learned_at &amp;lt; created_at"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"learned_before_kickoff"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"""SELECT count(*) FROM ai_predictions p
        JOIN nami_matches m ON m.id = p.match_id
        WHERE p.learned_at IS NOT NULL AND p.learned_at &amp;lt; m.match_time"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"graded_before_kickoff"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"""SELECT count(*) FROM ai_predictions p
        JOIN nami_matches m ON m.id = p.match_id
        WHERE p.settled_at IS NOT NULL AND p.settled_at &amp;lt; m.match_time"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;con&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;readonly&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;CHECKS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                       &lt;span class="c1"&gt;# every invariant must return 0&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"check"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"actual"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"""SELECT count(*) FROM ai_predictions
        WHERE extract(second FROM created_at) = 0
          AND extract(minute FROM created_at) &amp;lt;&amp;gt; 0"""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;                      &lt;span class="c1"&gt;# bucket with a stray minute&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;stray&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"check"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"granularity_ambiguous"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"actual"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"postgresql://soccerdream@127.0.0.1:5433/soccerdream"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On today's snapshot the kickoff-anchored invariant is the only one that fails — 62 instead of 0. &lt;code&gt;graded_before_kickoff&lt;/code&gt; fails too, at 208 rows across 40 matches, some stamped as much as 861 hours ahead of the whistle. A separate defect we did not know we had until the anchor was correct: a grade cannot exist before the thing being graded has been played.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three rules that transfer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pick the anchor the model cannot write to.&lt;/strong&gt; &lt;code&gt;settled_at&lt;/code&gt; and &lt;code&gt;learned_at&lt;/code&gt; are both our own writes. If the grader is late, the learner looks early. Kickoff time comes from the feed and enters the row before any model touches it — the only clock here a bug cannot rewind. Without one, you are comparing two of your own lies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Validate granularity before validating differences.&lt;/strong&gt; The 1,859 bucketed rows were invisible for months because a timestamp is a timestamp. One grouped count — seconds zero, minutes non-zero — separates observation from truncation and tells you which months are safe to average.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refuse writes that are chronologically impossible.&lt;/strong&gt; 62 and 208 are both small enough to ignore as a percentage and large enough to poison a fixture. A &lt;code&gt;CHECK&lt;/code&gt; constraint costs one line and turns silent contamination into a failed insert. Our auditor now fails the build on both, which is the only reason we found the 208.&lt;/p&gt;

&lt;p&gt;The headline number is not 46.6%, and it is not 0.42% either. It is that the difference between them is made entirely of our own operational lag — the part of the pipeline most teams never audit, because they audit the model instead of the clock it is judged against.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
    <item>
      <title>96 rows out of 47,114: when 92% of your joins return nothing and 97% of the rest return the wrong match</title>
      <dc:creator>SoccerDream AI</dc:creator>
      <pubDate>Wed, 18 Nov 2026 05:03:14 +0000</pubDate>
      <link>https://dev.to/soccerdream_ai/96-rows-out-of-47114-when-92-of-your-joins-return-nothing-and-97-of-the-rest-return-the-wrong-jml</link>
      <guid>https://dev.to/soccerdream_ai/96-rows-out-of-47114-when-92-of-your-joins-return-nothing-and-97-of-the-rest-return-the-wrong-jml</guid>
      <description>&lt;p&gt;A join returns rows. That is all it promises. It does not promise the rows mean anything.&lt;/p&gt;

&lt;p&gt;We learned this the expensive way. A league dashboard was reading 46 competitions. The database holds 751 for the same predictions. Nobody had changed a query; nobody had deleted a row. The join had been &lt;em&gt;succeeding&lt;/em&gt; for months — returning thousands of rows, every one attached to the wrong match.&lt;/p&gt;

&lt;p&gt;This is the audit, run against production: &lt;code&gt;ai_predictions&lt;/code&gt; (47,209 rows, 47,114 graded), &lt;code&gt;matches&lt;/code&gt; (85,580) and &lt;code&gt;nami_matches&lt;/code&gt; (89,601). The lesson is narrower than "test your joins": &lt;strong&gt;row counts cannot validate a join, and the only thing that can is a semantic assertion on the payload.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The join everyone writes
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ai_predictions&lt;/code&gt; has a column named &lt;code&gt;match_id&lt;/code&gt;. &lt;code&gt;matches&lt;/code&gt; has a primary key named &lt;code&gt;id&lt;/code&gt;. The query writes itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;competition_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS n
FROM ai_predictions p
JOIN matches m ON m.id = p.match_id
WHERE p.settled
GROUP BY 1;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It runs in under a second. It returns 46 rows. It is wrong.&lt;/p&gt;

&lt;p&gt;The first warning sign is parent coverage, which nobody computes because the query already returned data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM ai_predictions WHERE settled)                            AS settled,
  COUNT(*)&lt;/span&gt;                                                                       &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;joined_by_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) / (SELECT COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;pct&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;settled  joined_by_id  pct
  47114          3736  7.93
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;92.07% of graded predictions have no parent in &lt;code&gt;matches&lt;/code&gt;. On a foreign key that is a catastrophe. Here it produced silence, because a join returning &lt;em&gt;some&lt;/em&gt; rows looks like one that works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The resolved rows are worse than the missing ones
&lt;/h2&gt;

&lt;p&gt;The 7.93% that resolved are not correct. They are collisions — two unrelated integer sequences that happen to overlap. Three independent checks prove it, and any one alone would have caught it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check 1 — the scores disagree.&lt;/strong&gt; A prediction stores the final score it was graded against; so does the match row. If the join is real they must match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS comparable,
       COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_home_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score&lt;/span&gt;
                          &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_away_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;away_score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;agree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE p.match_home_score = m.home_score
                          AND p.match_away_score = m.away_score) / COUNT(*)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;pct&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settled&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_home_score&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;comparable  agree  pct
      3581     96  2.68
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;97.32% of resolved rows hang off a match with a different final score. The same check against the &lt;em&gt;correct&lt;/em&gt; key returns 99.77% — coming up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check 2 — the calendar disagrees.&lt;/strong&gt; Compare the day the prediction was written with the day the joined match kicked off:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|created_at - kickoff|    rows
0 days                       0
1-3 days                     6
4-30 days                2,479
31-180 days              1,251
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero same-day. Six within three days. 3,730 rows point at a match that had already been played — sometimes weeks before the prediction existed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check 3 — the labels disagree.&lt;/strong&gt; Join both tables to the same 3,736 predictions and compare competition IDs: 3,736 of 3,736 disagree, 100%. And the true competitions behind those rows number just &lt;strong&gt;4&lt;/strong&gt; — four real competitions scattered across 46 invented ones.&lt;/p&gt;

&lt;p&gt;That is the failure mode: a broken join does not produce empty output. It produces plausible, confidently wrong output, and the error is concentrated rather than random.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why nothing stopped it: three ID spaces, zero foreign keys
&lt;/h2&gt;

&lt;p&gt;The schema holds more than one identity per match, and every one is a bare &lt;code&gt;integer&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;matches.id                 811  ..  92,780         (dense, curated)
matches.nami_id        4,336,437 .. 4,670,168      (vendor natural key)
nami_matches.nami_id   4,336,437 .. 4,670,170
nami_matches.id              3 .. 25,340,015       (sparse surrogate)
ai_predictions.match_id  3,974 .. 24,360,892
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ranges overlap, which is why the collision rate is 7.9% instead of 0%. The column type cannot tell the spaces apart, and neither can the planner. The constraint that would have failed loudly is missing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;conname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_constraint&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;contype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'f'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;conrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ai_predictions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'matches'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;matches_competition_id_fkey  matches  competitions
matches_home_team_id_fkey    matches  teams
matches_away_team_id_fkey    matches  teams
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;matches&lt;/code&gt; has three outbound foreign keys. &lt;code&gt;ai_predictions&lt;/code&gt; has none — its &lt;code&gt;match_id&lt;/code&gt;, &lt;code&gt;model_id&lt;/code&gt; and &lt;code&gt;user_id&lt;/code&gt; are unconstrained integers, each a join waiting to mis-resolve.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key that is actually correct
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ai_predictions.match_id&lt;/code&gt; references &lt;code&gt;nami_matches.id&lt;/code&gt;, the sparse surrogate of the raw feed. Not &lt;code&gt;matches.id&lt;/code&gt;, not &lt;code&gt;matches.nami_id&lt;/code&gt;. Same query, different target:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;joined&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_home_score&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p_home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_away_score&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p_away&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score_regular&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;n_home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;away_score_regular&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;n_away&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
  &lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settled&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*)                                                        AS settled,
       COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;match_time&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                      &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;no_parent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE created_at &amp;gt; match_time)                 AS written_after_kickoff,
       COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;n_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
                          &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p_home&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n_home&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;p_away&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n_away&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;score_mismatch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE n_home IS NOT NULL AND p_home IS NOT NULL
                          AND p_home = n_home AND p_away = n_away)
             / COUNT(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;n_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p_home&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;score_agreement_pct&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;joined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;settled  no_parent  written_after_kickoff  score_mismatch  score_agreement_pct
  47114          0                     81             106                99.77
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;100% parent coverage, zero orphans, scores agreeing on 46,680 of 46,786 comparable rows. The league dimension goes from 46 competitions to &lt;strong&gt;751&lt;/strong&gt; and from 3,736 rows to 47,114 — a 12.6× larger evidence base, obtained by changing one identifier in an &lt;code&gt;ON&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;The residual deserves naming rather than rounding: 81 rows written after kickoff and 106 score disagreements (0.23%). Those are real defects, not join errors. A working key does not mean a clean table; it means the defects are visible at the right grain instead of laundered into wrong-parent rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  The missing bridge
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;matches&lt;/code&gt; remains worth keeping — curated competition and team foreign keys, lottery classification, scheduling metadata. It is just reachable by four hops, not one, since &lt;code&gt;matches.nami_id&lt;/code&gt; resolves against &lt;code&gt;nami_matches.nami_id&lt;/code&gt; for 85,314 of 85,580 rows (99.69%):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ai_predictions.match_id -&amp;gt; nami_matches.id -&amp;gt; nami_matches.nami_id
                        -&amp;gt; matches.nami_id -&amp;gt; matches.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The fix: make the join assert on its own payload
&lt;/h2&gt;

&lt;p&gt;A foreign key on &lt;code&gt;nami_matches.id&lt;/code&gt; prevents future orphans, and we added one. But it would &lt;em&gt;not&lt;/em&gt; have caught this bug: &lt;code&gt;match_id&lt;/code&gt; values inside &lt;code&gt;matches.id&lt;/code&gt;'s range are valid children of &lt;code&gt;matches.id&lt;/code&gt;, because they happen to be. Referential integrity was never violated. Semantics were.&lt;/p&gt;

&lt;p&gt;So the durable fix is a validated view that carries the assertion inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="k"&gt;REPLACE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;v_predictions_graded&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;prediction_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prediction_direction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hit_1x2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;nami_match_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;competition_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score_regular&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;away_score_regular&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_home_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score_regular&lt;/span&gt;
        &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_away_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;away_score_regular&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;score_consistent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;written_before_kickoff&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ai_predictions&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;nami_matches&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;settled&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;match_home_score&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home_score_regular&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consumers read the view, not the table. The two booleans then become a contract test that fails the build instead of the dashboard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;

&lt;span class="no"&gt;DSN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://user:pass@127.0.0.1:5433/soccerdream"&lt;/span&gt;
&lt;span class="no"&gt;SQL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"""
WITH t AS (
  SELECT p.created_at, p.match_home_score AS p_home, p.match_away_score AS p_away,
         n.match_time, n.home_score_regular AS n_home, n.away_score_regular AS n_away
  FROM ai_predictions p LEFT JOIN nami_matches n ON n.id = p.match_id
  WHERE p.settled)
SELECT COUNT(*), COUNT(*) FILTER (WHERE n_home IS NULL),
       COUNT(*) FILTER (WHERE created_at &amp;gt; match_time),
       COUNT(*) FILTER (WHERE p_home = n_home AND p_away = n_away),
       COUNT(*) FILTER (WHERE n_home IS NOT NULL AND p_home IS NOT NULL)
FROM t
"""&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_join_integrity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DSN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SET statement_timeout = '120s'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orphans&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;late&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;agree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;comparable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;orphans&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"orphans={orphans}/{n}"&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;agree&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;comparable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mf"&gt;99.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"score disagreement too high"&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;late&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"late-written rows={late} exceed known defect class"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Against the old join target the first assertion fails by 92 points. Against the correct one it passes with 0 orphans, 99.77% agreement and 81 late rows — three numbers a reviewer can argue with.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the wrong join cost
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;naive &lt;code&gt;matches.id&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;correct &lt;code&gt;nami_matches.id&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;rows reached&lt;/td&gt;
&lt;td&gt;3,736&lt;/td&gt;
&lt;td&gt;47,114&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;competitions&lt;/td&gt;
&lt;td&gt;46&lt;/td&gt;
&lt;td&gt;751&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;score agreement&lt;/td&gt;
&lt;td&gt;2.68%&lt;/td&gt;
&lt;td&gt;99.77%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;orphans&lt;/td&gt;
&lt;td&gt;unmeasurable&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rows both joinable and semantically true&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;96&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;46,680&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;96 of 47,114. &lt;strong&gt;0.20%.&lt;/strong&gt; That is the end-to-end correctness of the join as written; every per-league rate built on it was a statistic about collisions.&lt;/p&gt;

&lt;p&gt;The generalisable part is that join success rate is a vanity metric: it measures whether two integer domains intersect, which in any schema with more than one surrogate key they always will. The only metric that means something is semantic agreement on a field both sides independently observed — here the final score, plus the calendar. Two checks, a few lines of SQL, available the whole time.&lt;/p&gt;

&lt;p&gt;For every join in your reporting layer, name the field that would have to agree if the join were real. If you cannot name one, you do not have a join. You have a Cartesian product with good manners.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Analytics and research content. Not betting advice. 18+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;本内容仅供数据分析与研究参考，不构成任何决策建议。&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>python</category>
      <category>architecture</category>
      <category>database</category>
    </item>
  </channel>
</rss>
