Skip to main content

PGAIBench and AAA: can AI safely change production Postgres?

· 21 min read
Nikolay Samokhvalov
CEO & Founder
The AAA architecture for production Postgres, in three tiers. Top: two AI roles with no direct write access to databases. The Analyst investigates, drafts a change request, and rehearses it; the Auditor reviews the change request together with clone telemetry and can send it back, reject it, or approve it. Middle: the Actor, a deterministic runner and the sole write path to databases. Bottom: a two-node Clones cluster (primary and standby) and an illustrative fortified three-node Production Postgres HA cluster (one primary and two standbys), each fitted with a read-only telemetry emitter that safely broadcasts signals for Analyst, Auditor, and Actor to consume. After a change lands in production, a feedback loop returns production outcomes to both Auditor and Analyst so the system improves over time.

Modern engineering teams run far more Postgres clusters than human DBREs can watch by hand, leaving most databases underserved. Our goal is a new operating model where one experienced DBRE or SRE can reliably oversee 1,000 clusters, which requires safe, AI-driven database operations.

Frontier AI models already write valid SQL, and an agent with shell access can run it. The hard part of AI-driven database operations is knowing whether that SQL is safe to run on a live production database.

Ask any experienced database reliability engineer (DBRE) why they will not hand production Postgres to an AI agent, and the answer is rarely syntax. Databases are stateful, and real outages happen when valid SQL collides with real production state (often multi-terabyte) and live traffic. A schema change waits for a slow reporting query, and new queries on that table queue behind it. A config change pauses replica recovery, or an index that looks "unused" on the primary powers a critical query on a read replica.

To measure that gap, we built PGAIBench[1] v0.2 and tested frontier models on live two-node Postgres 18.7 clusters under concurrent load. Below, we also propose AAA (Analyst, Auditor, Actor), a clone-backed separation-of-duties architecture:

  • Analyst proposes changes without production write access.
  • Actor is a deterministic engine that rehearses every step on a live clone and applies only approved steps.
  • Auditor is an independent model that reviews the clone evidence.

Four key learnings stand out:

  • High capability does not prevent live outages. Acting alone, claude-fable-5.2 tied for the top score, yet still broke an implicit no-restart boundary in all 5 of 5 repeats on that task. Three runs broke live primaries, and the other two stopped in rehearsal only because its own post-change threshold tripped.
  • A deterministic clone gate stops mechanical failures at zero LLM cost, yet blind spots remain. Rehearsing on a two-node clone halted broken queries, lock stalls, and replication hangs automatically. Subtler errors still slipped past primary-only checks, such as x-ai/grok-4.7 dropping indexes that looked unused on the primary and crippling an async read replica.
  • Green clone summaries can both reassure and mislead AI reviewers. Real clone evidence cleared false alarms for most models, yet two failure modes persisted. z-ai/glm-5.3 saw a fast green test on a compact table and waved through a dangerous SET NOT NULL lock, while claude-haiku-5.5 overreacted to noisy clone telemetry and blocked almost everything.
  • The good news: separating duties cuts live harm sharply. When we replayed the models' primary changes through AAA (retrospective and in-sample), the clone gate and Auditor together blocked 12 of 13 measured harms (-92.3%) while approving 377 of 378 safe or unclassified changes (99.7%).

Watch the podcast discussion: Can AI safely change production Postgres?[2]

Links

  1. [1] PGAIBench https://pgaibench.dev/
  2. [2] Can AI safely change production Postgres? https://www.youtube.com/watch?v=2ndlAxb32Fo

The hidden costs of CREATE TEMP TABLE: catalog bloat and LWLock:SInvalWrite

· 21 min read
Nikolay Samokhvalov
CEO & Founder
An abstract drawing: thin lines, each one a database session, and orange marks that move along them.An abstract drawing: thin lines, each one a database session, and orange marks that move along them.

For years I warned against creating temporary tables in hot code because of catalog bloat. This time I measured it. The bloat was real, and it was only one of the costs.

What does CREATE TEMP TABLE cost if your code runs it in every transaction? On Postgres 18, two things:

  1. Catalog rows and files, every time. A temp table per transaction ran at 3783 TPS, one that each session keeps at 17866, and a CTE doing the same work at 86997 (16 vCPU, no idle sessions). Creating and dropping the table explains the 4.7x step from 17866 to 3783.
  2. The shared invalidation queue, when there are many sessions. With 5000 idle sessions and a 500 TPS target, temp-table DDL got 127 TPS in one run and 249 in another.

We measured it with DBLab Forge[1], a system that runs full-scale synthetic database experiments to study Postgres behavior and anomalies. The real case behind it is from Kirk Wolak[2], whose team moved a busy search system from Oracle and kept temp tables in its most frequent query.

Every number here comes from the DBLab Forge study brief[3]. Kirk and I discuss it in Postgres.FM episode 196[4].

Links

  1. [1] DBLab Forge https://postgres.ai/products/dblab-forge
  2. [2] Kirk Wolak https://www.linkedin.com/in/kirk-wolak-7706737/
  3. [3] DBLab Forge study brief https://console.postgres.ai/shared/brief/dfe99713-f5a8-483b-9aab-1606a883b75d
  4. [4] Postgres.FM episode 196 https://postgres.fm/episodes/the-hidden-costs-of-create-temp-table

UUIDv4 vs UUIDv7: what a random primary key costs in Postgres

· 20 min read
Nikolay Samokhvalov
CEO & Founder

Postgres 18 ships uuidv7()[1], which generates UUIDv7 as defined in RFC 9562[2]. Yet many tables still have id uuid default gen_random_uuid(), which is UUIDv4: 122 random bits. Does a random primary key matter?

Barely, while the index fits in memory. Once it outgrows memory, a lot: in our runs at 100M rows, UUIDv4 inserted at 0.62 to 0.69 of the rate of an ordered key on the same server, and its index crowded the table out of shared_buffers.

WAL is a separate cost, driven less by memory than by how many index leaves each checkpoint cycle touches. At 90M to 100M rows, UUIDv4 wrote 1.5 to 1.9 times the WAL per row, while UUIDv7 kept up with int8.

0.62–0.69×
UUIDv4 insert rate
vs an ordered key on the same server, at 100M rows
1.5–1.9×
UUIDv4 WAL per row
vs an ordered key on the same server, at 90M–100M rows
+33%
UUIDv4 primary key size
vs UUIDv7, at 100M rows

I asked DBLab Forge[3] to measure it: three tables with the same schema and the same insert stream, differing only in the primary key (UUIDv4, UUIDv7 or int8), seven runs on cloud servers, and every hypothesis written down with a falsifier before the first row. This post is the short version; the full brief[4] has every number with its source.

Kirk Wolak and I talk about it in Postgres.FM episode 194, "UUIDv4 vs UUIDv7"[5] (transcript included). The video shows the charts as we talk.

Links

  1. [1] uuidv7() https://www.postgresql.org/docs/18/functions-uuid.html
  2. [2] RFC 9562 https://www.rfc-editor.org/rfc/rfc9562#section-5.7
  3. [3] DBLab Forge https://postgres.ai/products/dblab-forge
  4. [4] full brief https://console.postgres.ai/shared/brief/9fffe2da-00c1-470b-a79d-a210ae2306bc
  5. [5] Postgres.FM episode 194, "UUIDv4 vs UUIDv7" https://postgres.fm/episodes/uuidv4-vs-uuidv7

What your Postgres is waiting for, and why the dashboard can't tell you

· 12 min read
Dmitry Fomin
Sr. Postgres engineer

A checkout request was slow. The dashboard said the time went to the commit path and to disk reads. The team tried the two fixes that follow from that, and the slowest requests stayed about as slow. The cause was a lock that the dashboard ranked fifth, at 2% of the time. This post explains why a wait dashboard can be right about the time and wrong about the problem, using only what Postgres exposes today. It ends where the next post, about a tool that finds such causes, begins.

Dump/restore done. Now what?

· 15 min read
Nikolay Samokhvalov
CEO & Founder

Your pg_restore finished without errors. The data is there. The indexes are in place. Is it time to switch traffic? A successful restore can still leave you with painfully slow queries—and the first read can do more than just read. What is missing from the migration runbook?

Kirk Wolak and I explain why in Postgres.FM episode 193, “Dump/restore done. Now what?”[1] (transcript included).

Links

  1. [1] Postgres.FM episode 193, “Dump/restore done. Now what?” https://postgres.fm/episodes/dump-restore-done-now-what

DBLab 4.2: clone upgrades, simplified install, retention, and more

· 7 min read
Denis Morozov
Staff Engineer

DBLab 4.0 made database branching instant[1]. 4.1 added the controls around it — protection leases, Teleport, metrics. 4.2 closes the last item on 4.1's own roadmap[2] — testing a Postgres major version upgrade on a clone — and removes the two remaining points of friction around it: getting configured in the first place, and cleaning up what accumulates afterwards.

Links

  1. [1] database branching instant https://postgres.ai/blog/20250721-dblab-engine-4-0-released
  2. [2] 4.1's own roadmap https://postgres.ai/blog/20260408-dblab-engine-4-1-released

DBLab 4.1: protection leases, Teleport, Prometheus, and more

· 5 min read
Denis Morozov
Staff Engineer

DBLab 4.0 introduced instant database branching with O(1) economics[1]. With 4.1, we're making it safe to hand off to a platform team: automatic resource governance, enterprise access control, production-safe data refresh, and native observability.

Links

  1. [1] instant database branching with O(1) economics https://postgres.ai/blog/20250721-dblab-engine-4-0-released

How moving one word can speed up a query 10–50x

· 6 min read
Maxim Boguk
Postgres Expert
Nikolay Samokhvalov
CEO & Founder

One of these queries is 32x faster than the other. Which one, and why?

Query 1:

select pt.*
from post_tags pt
where pt.tag_id = any($1)
and exists (
    select
    from posts
    where posts.post_id = pt.post_id
      and not deleted
);

Query 2:

select pt.*
from post_tags pt
where pt.tag_id = any($1)
and not exists (
    select
    from posts
    where posts.post_id = pt.post_id
      and deleted
);

These two queries are logically equivalent here because posts.post_id is a primary key — the subquery matches at most one row, so AND NOT deleted vs. AND deleted simply flips a boolean. A foreign key on post_tags.post_id guarantees no orphans.

Read on to find out. This pattern was first spotted by Maxim Boguk on a 400 GiB production table, where the speedup was approximately 50x.

The hidden cost of invalid indexes in Postgres (yes, even on Supabase/RDS/Neon)

· 4 min read
Dmitry Fomin
Sr. Postgres engineer

When CREATE INDEX CONCURRENTLY or REINDEX INDEX CONCURRENTLY fails, Postgres leaves behind an invalid index. Many engineers assume these indexes are harmless placeholders waiting to be cleaned up. After all, the planner won't use them for queries, right?

Wrong. Invalid indexes are far from harmless. They continue to consume resources, generate I/O, block optimizations, and even cause lock contention — all while providing zero query performance benefit.

In this article, we'll demonstrate — with real evidence — the hidden costs of invalid indexes that every Postgres administrator should understand.