Primary key coverage identity

From CarlosCaPe/octorato · View interactive version

Primary key coverage identity is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Primary Key Coverage & Identity Columns

How to use this skill

Skill file

Primary Key Coverage & Identity Columns

Source: PostgreSQL Best Practices

-- "Primary keys: Prefer BIGINT identity or UUID" and Audit Finding #5

What

Ensuring every table has a primary key, and that PK columns use the modern

GENERATED BY DEFAULT AS IDENTITY pattern instead of serial + nextval().

Why

Tables without primary keys are a schema smell:

How

Audit: Find tables without primary keys

SELECT t.tablename
FROM pg_tables t
LEFT JOIN (
    SELECT tc.table_name
    FROM information_schema.table_constraints tc
    WHERE tc.constraint_type = 'PRIMARY KEY'
      AND tc.table_schema = 'public'
) pk ON pk.table_name = t.tablename
WHERE t.schemaname = 'public'
  AND pk.table_name IS NULL
ORDER BY t.tablename;

Add a PK to an existing table (BIGINT identity)

-- Step 1: Add the identity column
ALTER TABLE public."EmailConfig"
    ADD COLUMN "Id" bigint GENERATED BY DEFAULT AS IDENTITY;

-- Step 2: Backfill (identity auto-generates for new rows)
-- Existing rows already got values from Step 1

-- Step 3: Add the PK constraint
ALTER TABLE public."EmailConfig"
    ADD CONSTRAINT pk_emailconfig PRIMARY KEY ("Id");

Add a PK using an existing column

-- If the table already has a candidate key column
ALTER TABLE public."Regions"
    ADD CONSTRAINT pk_regions PRIMARY KEY ("RegionId");

New table template (always include PK)

CREATE TABLE public."MyNewTable" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    "Name" text NOT NULL,
    "CreatedAt" timestamptz NOT NULL DEFAULT now(),
    "UpdatedAt" timestamptz NOT NULL DEFAULT now()
);

BIGINT vs UUID Decision

| Type | Pros | Cons | Use When |

|------|------|------|----------|

| bigint IDENTITY | Fast, compact (8 bytes), sequential, index-friendly | Not globally unique | Single-database, sequential access |

| uuid v4 | Globally unique, no coordination needed | 16 bytes, random = index fragmentation | Distributed systems, external IDs |

| uuid v7 | Globally unique + time-sortable | 16 bytes, requires extension | Distributed + temporal ordering |

Default recommendation: bigint GENERATED BY DEFAULT AS IDENTITY unless

there is a specific distributed/external ID requirement.

GENERATED BY DEFAULT vs GENERATED ALWAYS

| Mode | Explicit INSERT Allowed? | Use When |

|------|--------------------------|----------|

| BY DEFAULT | Yes -- app can provide the ID | Backward compat, data migration, testing |

| ALWAYS | No (unless OVERRIDING SYSTEM VALUE) | Strict auto-generation |

Default recommendation: BY DEFAULT for backward compatibility with

existing application code. See Skill #17 for serial-to-identity conversion.

When to Use

Where We Used It

Related Skills

References

-- "Schema design and data types" and Audit Finding #5

-- Finding #6

Gotchas

via pg_depend -- dropping the column drops the sequence automatically

candidate key column before adding a UNIQUE or PK constraint

keyless tables


Category: DDL | Origin: , ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.