PostgreSQL Error 42622: Name Too Long
PostgreSQL error code 42622 occurs when an identifier—such as a table name, column name, index name, or constraint name—exceeds PostgreSQL's maximum allowed length of 63 bytes. This limit is a compile-time constant (NAMEDATALEN - 1) and cannot be changed without recompiling PostgreSQL from source. It commonly appears in applications using ORMs that auto-generate long identifier names or in schemas with verbose naming conventions.
Top 3 Causes
1. Object Names Exceeding 63 Bytes
The most direct cause is simply naming a database object with more than 63 bytes. Note that multibyte characters (e.g., UTF-8 encoded text) consume more than 1 byte per character.
-- This will trigger error 42622 or silent truncation
CREATE TABLE user_account_personal_information_detail_extended_history (
id SERIAL PRIMARY KEY
);
-- identifier "user_account_personal_information_detail_extended_history"
-- will be truncated to 63 bytes
-- Check byte length before creating
SELECT
octet_length('user_account_personal_information_detail_extended_history')
AS byte_length;
-- Returns 57 in this case — but longer names WILL fail
2. Auto-Generated Index or Constraint Names
When you don't explicitly name indexes or foreign key constraints, PostgreSQL auto-generates names by combining table and column names. This combination can easily exceed 63 bytes.
-- Auto-generated name would be:
-- order_management_system_transactions_customer_account_reference_number_idx
-- which is clearly over 63 bytes
CREATE TABLE order_management_system_transactions (
id SERIAL PRIMARY KEY,
customer_account_reference_number VARCHAR(50)
);
-- This auto-generated index name will exceed the limit:
CREATE INDEX ON order_management_system_transactions
(customer_account_reference_number);
-- Fix: Always specify an explicit, shorter name
CREATE INDEX idx_oms_txn_cust_ref
ON order_management_system_transactions (customer_account_reference_number);
3. ORM or Migration Tools Auto-Generating Long Names
ORMs like Django, SQLAlchemy, or Hibernate generate identifier names based on model and field names. Long class or field names can result in auto-generated SQL identifiers that exceed PostgreSQL's limit.
-- What Django might auto-generate (problematic):
-- CREATE INDEX "myapp_useraccountpersonalinfo_user_profile_id_a1b2c3d4_idx"
-- ON "myapp_useraccountpersonalinfo" ("user_profile_id");
-- Explicitly override in migration to produce this instead:
CREATE INDEX idx_user_acct_info_profile_id
ON myapp_useraccountpersonalinfo (user_profile_id);
-- Verify all current index name lengths in your schema
SELECT
indexname,
tablename,
octet_length(indexname) AS byte_len
FROM pg_indexes
WHERE schemaname = 'public'
ORDER BY byte_len DESC;
Quick Fix Solutions
-- 1. Rename an existing object with a too-long name
ALTER TABLE old_extremely_long_table_name_that_is_problematic
RENAME TO short_table_name;
-- 2. Replace a long-named index
DROP INDEX IF EXISTS long_auto_generated_index_name_that_exceeds_the_limit;
CREATE INDEX idx_short_meaningful_name ON target_table (target_column);
-- 3. Rename a constraint
ALTER TABLE my_table
RENAME CONSTRAINT long_auto_generated_constraint_name TO fk_short_name;
-- 4. Check byte length of any string before using as identifier
SELECT octet_length('your_proposed_identifier_name') AS byte_length;
-- Must be <= 63
Prevention Tips
1. Enforce a naming convention with byte-length limits.
Establish team standards—for example, table names ≤ 30 chars, column names ≤ 25 chars, and index/constraint names ≤ 40 chars. Add an automated check in your CI/CD pipeline:
-- Run this as part of your schema validation
SELECT object_name, byte_length
FROM (
SELECT table_name AS object_name,
octet_length(table_name) AS byte_length
FROM information_schema.tables
WHERE table_schema = 'public'
) t
WHERE byte_length > 50
ORDER BY byte_length DESC;
2. Always explicitly name indexes and constraints.
Never rely on auto-generated names for indexes, foreign keys, or unique constraints. Explicitly providing short, meaningful names prevents both the 42622 error and hard-to-debug truncation issues down the line.
-- Good practice: always name your constraints and indexes explicitly
ALTER TABLE orders
ADD CONSTRAINT fk_orders_customer
FOREIGN KEY (customer_id) REFERENCES customers (id);
CREATE UNIQUE INDEX uidx_orders_ref_num
ON orders (reference_number);
Summary
PostgreSQL's 63-byte identifier limit is a hard boundary that applies to all object names. The key takeaways are: always verify identifier byte lengths before deployment, explicitly name all constraints and indexes, and enforce a team-wide naming convention to avoid surprises in production.
📖 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.
Top comments (0)