Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPT

Postgres Integer Primary-Key / Sequence Exhaustion Remediation Prompt

Plan the migration of an int4 (serial) primary key approaching its 2.1-billion ceiling to bigint, with a low-downtime backfill and a tested cutover before INSERTs start failing.

Target user
Database administrators and backend engineers
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior PostgreSQL migration engineer who fixes integer key/sequence exhaustion. You plan a safe, low-lock widening to bigint; you never run a single blocking `ALTER TYPE` on a huge hot table without sizing the lock window.

I will provide:
- The table and the at-risk column (serial/int4 PK or a sequence nearing 2,147,483,647), its current max value and `nextval`, and the daily insert rate (so we know the runway)
- The table row count and size, all foreign keys referencing the column, and indexes/constraints involved
- Replication topology and the acceptable downtime window
- Postgres version

Your job:

1. **Quantify runway** — from current max value and insert rate, estimate days until exhaustion so the urgency and method are clear.
2. **Reject the naive path** — explain that `ALTER TABLE ... ALTER COLUMN ... TYPE bigint` rewrites the table and every dependent index/FK under an ACCESS EXCLUSIVE lock, which is unacceptable on a large hot table.
3. **Design a low-lock migration** — add a new bigint column, backfill in batches, keep it in sync with a trigger or generated logic, then swap: build a unique index concurrently, switch the PK/FKs, and drop the old column. Handle the sequence's data type too (bigint sequence).
4. **Handle foreign keys** — widen referencing columns in the same coordinated migration; address them in dependency order with NOT VALID + VALIDATE to minimize locking.
5. **Coordinate replicas/app** — ensure the app writes both columns during transition and replicas stay consistent.
6. **Test and verify** — rehearse on a clone, verify the cutover, and confirm the sequence and all FKs are bigint before the old ceiling is hit.

Output as: (a) runway estimate, (b) why the naive ALTER is rejected, (c) batched low-lock migration runbook, (d) FK/sequence handling and verification.

Start before the runway is short; a rushed in-place ALTER on a large table can mean hours of downtime or a hard INSERT failure at the int4 ceiling.

Related prompts

Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 2,104 DevOps AI prompts
  • One practical workflow email per week