惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
J
Java Code Geeks
C
Check Point Blog
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
IT之家
IT之家
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
U
Unit 42
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
PostgreSQL 2200H Error: Causes and Solutions Complete Guide
umzzil nng · 2026-06-12 · via DEV Community

umzzil nng

PostgreSQL Error 2200H: Sequence Generator Limit Exceeded

PostgreSQL error code 2200H occurs when a sequence object reaches its defined MAXVALUE (or MINVALUE for descending sequences) and has no CYCLE option to wrap around. This is most commonly seen on tables using SERIAL (INT4) primary keys, which cap out at approximately 2.1 billion. Once the limit is hit, every subsequent INSERT attempting to use that sequence will fail immediately.


Top 3 Causes

1. SERIAL Column Hitting the INT4 Ceiling (~2.1 Billion)

SERIAL uses a 4-byte integer under the hood. High-volume systems — logging tables, event trackers, order systems — can exhaust this faster than expected.

-- Check how close your sequences are to their limit
SELECT
    sequencename,
    last_value,
    max_value,
    ROUND((last_value::NUMERIC / max_value) * 100, 2) AS used_pct,
    (max_value - last_value) AS remaining
FROM pg_sequences
WHERE schemaname = 'public'
ORDER BY used_pct DESC;

2. Custom MAXVALUE Set Too Low

When a sequence is manually created with an artificially low MAXVALUE and NO CYCLE (the default), it will throw 2200H as soon as the cap is reached.

-- Example of a problematic sequence definition
CREATE SEQUENCE bad_sequence
    START 1
    INCREMENT 1
    MAXVALUE 10000   -- Way too low for production use
    NO CYCLE;

-- Check a specific sequence definition
SELECT * FROM pg_sequences WHERE sequencename = 'bad_sequence';

3. Rollbacks Silently Consuming Sequence Values

PostgreSQL sequences are non-transactional by design — rolled-back transactions do not return their consumed values. In retry-heavy applications or batch jobs, sequence values can be consumed far faster than actual committed rows suggest.

-- Demonstrate sequence consumption on rollback
BEGIN;
SELECT nextval('orders_order_id_seq'); -- value consumed
ROLLBACK;
-- The value is gone. nextval will skip it permanently.

-- You can check current sequence value without advancing it
SELECT last_value FROM orders_order_id_seq;


Quick Fix Solutions

Fix 1: Expand the sequence's MAXVALUE immediately (zero downtime)

-- Immediate relief with no table lock required
ALTER SEQUENCE orders_order_id_seq MAXVALUE 9223372036854775807;

Fix 2: Change the column type to BIGINT

-- Upgrade the column and its backing sequence
ALTER TABLE orders ALTER COLUMN order_id TYPE BIGINT;
ALTER SEQUENCE orders_order_id_seq MAXVALUE 9223372036854775807;

Fix 3: Reset sequence to current max (emergency recovery)

-- If inserts are already failing, resync the sequence
SELECT SETVAL(
    'orders_order_id_seq',
    (SELECT MAX(order_id) FROM orders),
    true
);

Fix 4: Migrate to IDENTITY column (recommended long-term)

-- Best practice for new tables in PostgreSQL 10+
CREATE TABLE orders_new (
    order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);


Prevention Tips

1. Monitor sequence usage proactively.
Schedule the query below as a cron job or integrate it into your monitoring stack. Alert when usage exceeds 80%.

SELECT sequencename, last_value, max_value,
    ROUND((last_value::NUMERIC / NULLIF(max_value,0)) * 100, 2) AS used_pct
FROM pg_sequences
WHERE (last_value::NUMERIC / NULLIF(max_value,0)) > 0.8;

2. Standardize on BIGINT GENERATED ALWAYS AS IDENTITY or UUID for all new tables.
Treat SERIAL as a legacy type. BIGINT identity columns give you ~9.2 quintillion values, while UUID eliminates the exhaustion problem entirely.

-- Preferred modern pattern
CREATE TABLE events (
    event_id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
    payload JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);


Related Errors

  • 23505 unique_violation — Can occur if CYCLE is enabled and reused values collide with existing primary keys.
  • 55000 object_not_in_prerequisite_state — Raised when calling nextval() on an already-exhausted sequence without CYCLE.

📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.